Clover icon

Coverage Report

  1. Project Clover database Tue Mar 10 2026 14:58:44 GMT
  2. Package jalview.gui

File QuitHandlerTest.java

 

Code metrics

6
105
12
1
319
202
15
0.14
8.75
12
1.25

Classes

Class Line # Actions
QuitHandlerTest 46 105 15
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.gui;
22   
23    import static org.testng.Assert.assertNotNull;
24   
25    import java.io.File;
26    import java.util.ArrayList;
27    import java.util.Date;
28    import java.util.List;
29   
30    import org.testng.Assert;
31    import org.testng.annotations.AfterClass;
32    import org.testng.annotations.AfterMethod;
33    import org.testng.annotations.BeforeClass;
34    import org.testng.annotations.BeforeMethod;
35    import org.testng.annotations.Test;
36   
37    import jalview.bin.Cache;
38    import jalview.bin.Jalview;
39    import jalview.gui.QuitHandler.QResponse;
40    import jalview.io.DataSourceType;
41    import jalview.io.FileFormat;
42    import jalview.io.FileLoader;
43    import jalview.project.Jalview2XML;
44   
45    @Test(singleThreaded = true)
 
46    public class QuitHandlerTest
47    {
48    private static String saveProjectFile = "test-output/tempSaveFile.jvp";
49   
50    private static String saveFastaFile = "test-output/tempSaveFile.fa";
51   
 
52  0 toggle @BeforeClass(alwaysRun = true)
53    public void setUpJvOptionPane()
54    {
55  0 JvOptionPane.setInteractiveMode(false);
56  0 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57  0 Jalview2XML.setDebugDelaySave(3);
58    }
59   
60    /**
61    * @throws java.lang.Exception
62    */
 
63  0 toggle @BeforeClass(alwaysRun = true)
64    public static void setUpBeforeClass() throws Exception
65    {
66  0 Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
67   
68    /*
69    * set news feed last read to a future time to ensure no
70    * 'unread' news item is displayed
71    */
72  0 Date oneHourFromNow = new Date(
73    System.currentTimeMillis() + 3600 * 1000);
74  0 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
75   
76  0 Jalview.main(
77    new String[]
78    { "--nowebservicediscovery", "--nosplash", "--nonews" });
79    }
80   
 
81  0 toggle @AfterClass(alwaysRun = true)
82    public static void resetProps()
83    {
84    // reset quit response
85  0 QuitHandler.setResponse(QResponse.NULL);
86    // reset mock response
87  0 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
88    // close desktop windows/frames
89  0 if (Desktop.getInstance() != null)
90  0 Desktop.getInstance().closeAll_actionPerformed(null);
91    // reset debug delay
92  0 Jalview2XML.setDebugDelaySave(20);
93    // load normal testprops
94  0 Cache.loadProperties("test/jalview/testProps.jvprops");
95    }
96   
 
97  0 toggle @BeforeMethod(alwaysRun = true)
98    public static void tearDownAfterClass() throws Exception
99    {
100    // reset quit response
101  0 QuitHandler.setResponse(QResponse.NULL);
102    // reset mock response
103  0 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
104    // close desktop windows/frames
105  0 if (Desktop.getInstance() != null)
106  0 Desktop.getInstance().closeAll_actionPerformed(null);
107    // reset debug delay
108  0 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
109  0 Jalview2XML.setDebugDelaySave(3);
110    // set the project file
111  0 Desktop.getInstance().setProjectFile(new File(saveProjectFile));
112    }
113   
 
114  0 toggle @AfterMethod(alwaysRun = true)
115    public static void cleanup()
116    {
117    // delete save files
118  0 List<String> files = new ArrayList<>();
119  0 files.add(saveProjectFile);
120  0 files.add(saveFastaFile);
121  0 for (String filename : files)
122    {
123  0 File file = new File(filename);
124  0 if (file.exists())
125    {
126  0 file.delete();
127    }
128    }
129    }
130   
 
131  0 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
132    public void testInstantQuit() throws Exception
133    {
134  0 String inFile = "examples/uniref50.fa";
135  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
136    DataSourceType.FILE);
137  0 assertNotNull(af, "Didn't read input file " + inFile);
138   
139  0 long start = System.currentTimeMillis();
140   
141    // if a save is attempted it will delay 3s
142  0 Jalview2XML.setDebugDelaySave(3);
143  0 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
144   
145    // loaded file but haven't done anything, should just quit
146  0 QResponse response = QuitHandler.getQuitResponse(true);
147  0 long end = System.currentTimeMillis();
148   
149  0 Assert.assertEquals(response, QResponse.QUIT);
150  0 Assert.assertTrue(end - start < 500,
151    "Quit-with-no-save-needed took too long (" + (end - start)
152    + "ms)");
153    }
154   
 
155  0 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 10)
156    public void testWaitForSaveQuit() throws Exception
157    {
158  0 String inFile = "examples/uniref50.fa";
159  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
160    DataSourceType.FILE);
161  0 assertNotNull(af, "Didn't read input file " + inFile);
162   
163  0 long start = System.currentTimeMillis();
164   
165    // start a long save (3s)
166  0 Jalview2XML.setDebugDelaySave(3);
167  0 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
168  0 Desktop.getInstance().saveState_actionPerformed(false);
169   
170    // give the saveState thread time to start!
171  0 Thread.sleep(500);
172   
173    // af.saveAlignment(saveProjectFile, FileFormat.Jalview);
174  0 QResponse response = QuitHandler.getQuitResponse(true);
175  0 long end = System.currentTimeMillis();
176   
177  0 Assert.assertEquals(response, QResponse.QUIT);
178  0 Assert.assertTrue(end - start > 2900,
179    "Quit-whilst-saving was too short (" + (end - start) + "ms)");
180    }
181   
 
182  0 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
183    public void testSavedProjectChanges() throws Exception
184    {
185  0 String inFile = "examples/uniref50.fa";
186  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
187    DataSourceType.FILE);
188  0 assertNotNull(af, "Didn't read input file " + inFile);
189  0 AlignViewport viewport = af.getViewport();
190    // pretend something has happened
191  0 viewport.setSavedUpToDate(false);
192  0 Jalview2XML.setStateSavedUpToDate(false);
193   
194    // don't want to hang around here
195  0 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
196  0 af.saveAlignment(saveProjectFile, FileFormat.Jalview);
197   
198    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION (to
199    // mean [CANCEL] -- file should already be saved so this doesn't happen and
200    // we get a QUIT response)
201  0 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
202  0 QResponse response = QuitHandler.getQuitResponse(true);
203   
204    // if not saved this would be CANCEL_QUIT
205  0 Assert.assertEquals(response, QResponse.QUIT);
206    }
207   
 
208  0 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
209    public void testSavedAlignmentChanges() throws Exception
210    {
211  0 String inFile = "examples/uniref50.fa";
212  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
213    DataSourceType.FILE);
214  0 assertNotNull(af, "Didn't read input file " + inFile);
215  0 AlignViewport viewport = af.getViewport();
216    // pretend something has happened
217  0 viewport.setSavedUpToDate(false);
218  0 Jalview2XML.setStateSavedUpToDate(false);
219   
220    // no hanging around needed here
221  0 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
222  0 af.saveAlignment(saveFastaFile, FileFormat.Fasta);
223   
224    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
225  0 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
226  0 QResponse response = QuitHandler.getQuitResponse(true);
227   
228    // if not saved this would be CANCEL_QUIT
229  0 Assert.assertEquals(response, QResponse.QUIT);
230    }
231   
 
232  0 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
233    public void testUnsavedChanges() throws Exception
234    {
235  0 String inFile = "examples/uniref50.fa";
236  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
237    DataSourceType.FILE);
238  0 assertNotNull(af, "Didn't read input file " + inFile);
239  0 AlignViewport viewport = af.getViewport();
240    // pretend something has happened
241  0 viewport.setSavedUpToDate(false);
242  0 Jalview2XML.setStateSavedUpToDate(false);
243   
244    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
245  0 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
246  0 QResponse response = QuitHandler.getQuitResponse(true);
247   
248  0 Assert.assertEquals(response, QResponse.CANCEL_QUIT);
249    }
250   
 
251  0 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
252    public void testNoGUIUnsavedChanges() throws Exception
253    {
254  0 String inFile = "examples/uniref50.fa";
255  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
256    DataSourceType.FILE);
257  0 assertNotNull(af, "Didn't read input file " + inFile);
258  0 AlignViewport viewport = af.getViewport();
259    // pretend something has happened
260  0 viewport.setSavedUpToDate(false);
261  0 Jalview2XML.setStateSavedUpToDate(false);
262   
263    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
264  0 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
265    /*
266    QResponse response = QuitHandler.getQuitResponse(false,
267    QuitHandler.defaultOkQuit, () -> {
268    // set FORCE_QUIT without the force quit
269    QuitHandler.setResponse(QResponse.FORCE_QUIT);
270    return null;
271    }, QuitHandler.defaultCancelQuit);
272    */
273  0 QResponse response = QuitHandler.getQuitResponse(false);
274   
275  0 Assert.assertEquals(response, QResponse.QUIT);
276    }
277   
 
278  0 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
279    public void testForceQuit() throws Exception
280    {
281  0 String inFile = "examples/uniref50.fa";
282  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
283    DataSourceType.FILE);
284  0 assertNotNull(af, "Didn't read input file " + inFile);
285   
286  0 long start = System.currentTimeMillis();
287   
288    // start a long save (10s)
289  0 Jalview2XML.setDebugDelaySave(10);
290  0 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
291  0 Desktop.getInstance().saveState_actionPerformed(false);
292   
293    // give the saveState thread time to start!
294  0 Thread.sleep(100);
295   
296    // this will select "Force Quit"
297  0 JvOptionPane.setMockResponse(JvOptionPane.YES_OPTION);
298  0 QResponse response = QuitHandler.getQuitResponse(true,
299    QuitHandler.defaultOkQuit, () -> {
300    // set FORCE_QUIT without the force quit
301  0 jalview.bin.Console.debug(
302    "Setting FORCE_QUIT without actually quitting");
303  0 QuitHandler.setResponse(QResponse.FORCE_QUIT);
304    }, QuitHandler.defaultCancelQuit);
305  0 long end = System.currentTimeMillis();
306   
307  0 Assert.assertEquals(response, QResponse.FORCE_QUIT);
308    // if the wait (min wait is 1s) wasn't long enough...
309  0 Assert.assertTrue(end - start > 1000,
310    "Force-Quit-whilst-saving was too short (" + (end - start)
311    + "ms)");
312    // if the wait was too long (probably waited for file to save)
313  0 Assert.assertTrue(end - start < 9090,
314    "Force-Quit-whilst-saving was too long (" + (end - start)
315    + "ms)");
316   
317    }
318   
319    }