Clover icon

Coverage Report

  1. Project Clover database Tue Oct 29 2024 21:36:55 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.9837398598.4%
 

Contributing tests

This file is covered by 7 tests. .

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  1 toggle @BeforeClass(alwaysRun = true)
53    public void setUpJvOptionPane()
54    {
55  1 JvOptionPane.setInteractiveMode(false);
56  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57  1 Jalview2XML.setDebugDelaySave(3);
58    }
59   
60    /**
61    * @throws java.lang.Exception
62    */
 
63  1 toggle @BeforeClass(alwaysRun = true)
64    public static void setUpBeforeClass() throws Exception
65    {
66  1 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  1 Date oneHourFromNow = new Date(
73    System.currentTimeMillis() + 3600 * 1000);
74  1 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
75   
76  1 Jalview.main(
77    new String[]
78    { "--nowebservicediscovery", "--nosplash", "--nonews" });
79    }
80   
 
81  1 toggle @AfterClass(alwaysRun = true)
82    public static void resetProps()
83    {
84    // reset quit response
85  1 QuitHandler.setResponse(QResponse.NULL);
86    // reset mock response
87  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
88    // close desktop windows/frames
89  1 if (Desktop.instance != null)
90  1 Desktop.instance.closeAll_actionPerformed(null);
91    // reset debug delay
92  1 Jalview2XML.setDebugDelaySave(20);
93    // load normal testprops
94  1 Cache.loadProperties("test/jalview/testProps.jvprops");
95    }
96   
 
97  7 toggle @BeforeMethod(alwaysRun = true)
98    public static void tearDownAfterClass() throws Exception
99    {
100    // reset quit response
101  7 QuitHandler.setResponse(QResponse.NULL);
102    // reset mock response
103  7 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
104    // close desktop windows/frames
105  7 if (Desktop.instance != null)
106  7 Desktop.instance.closeAll_actionPerformed(null);
107    // reset debug delay
108  7 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
109  7 Jalview2XML.setDebugDelaySave(3);
110    // set the project file
111  7 Desktop.instance.setProjectFile(new File(saveProjectFile));
112    }
113   
 
114  7 toggle @AfterMethod(alwaysRun = true)
115    public static void cleanup()
116    {
117    // delete save files
118  7 List<String> files = new ArrayList<>();
119  7 files.add(saveProjectFile);
120  7 files.add(saveFastaFile);
121  7 for (String filename : files)
122    {
123  14 File file = new File(filename);
124  14 if (file.exists())
125    {
126  4 file.delete();
127    }
128    }
129    }
130   
 
131  1 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
132    public void testInstantQuit() throws Exception
133    {
134  1 String inFile = "examples/uniref50.fa";
135  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
136    DataSourceType.FILE);
137  1 assertNotNull(af, "Didn't read input file " + inFile);
138   
139  1 long start = System.currentTimeMillis();
140   
141    // if a save is attempted it will delay 3s
142  1 Jalview2XML.setDebugDelaySave(3);
143  1 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
144   
145    // loaded file but haven't done anything, should just quit
146  1 QResponse response = QuitHandler.getQuitResponse(true);
147  1 long end = System.currentTimeMillis();
148   
149  1 Assert.assertEquals(response, QResponse.QUIT);
150  1 Assert.assertTrue(end - start < 500,
151    "Quit-with-no-save-needed took too long (" + (end - start)
152    + "ms)");
153    }
154   
 
155  1 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 10)
156    public void testWaitForSaveQuit() throws Exception
157    {
158  1 String inFile = "examples/uniref50.fa";
159  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
160    DataSourceType.FILE);
161  1 assertNotNull(af, "Didn't read input file " + inFile);
162   
163  1 long start = System.currentTimeMillis();
164   
165    // start a long save (3s)
166  1 Jalview2XML.setDebugDelaySave(3);
167  1 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
168  1 Desktop.instance.saveState_actionPerformed(false);
169   
170    // give the saveState thread time to start!
171  1 Thread.sleep(500);
172   
173    // af.saveAlignment(saveProjectFile, FileFormat.Jalview);
174  1 QResponse response = QuitHandler.getQuitResponse(true);
175  1 long end = System.currentTimeMillis();
176   
177  1 Assert.assertEquals(response, QResponse.QUIT);
178  1 Assert.assertTrue(end - start > 2900,
179    "Quit-whilst-saving was too short (" + (end - start) + "ms)");
180    }
181   
 
182  1 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
183    public void testSavedProjectChanges() throws Exception
184    {
185  1 String inFile = "examples/uniref50.fa";
186  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
187    DataSourceType.FILE);
188  1 assertNotNull(af, "Didn't read input file " + inFile);
189  1 AlignViewport viewport = af.getViewport();
190    // pretend something has happened
191  1 viewport.setSavedUpToDate(false);
192  1 Jalview2XML.setStateSavedUpToDate(false);
193   
194    // don't want to hang around here
195  1 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
196  1 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  1 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
202  1 QResponse response = QuitHandler.getQuitResponse(true);
203   
204    // if not saved this would be CANCEL_QUIT
205  1 Assert.assertEquals(response, QResponse.QUIT);
206    }
207   
 
208  1 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
209    public void testSavedAlignmentChanges() throws Exception
210    {
211  1 String inFile = "examples/uniref50.fa";
212  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
213    DataSourceType.FILE);
214  1 assertNotNull(af, "Didn't read input file " + inFile);
215  1 AlignViewport viewport = af.getViewport();
216    // pretend something has happened
217  1 viewport.setSavedUpToDate(false);
218  1 Jalview2XML.setStateSavedUpToDate(false);
219   
220    // no hanging around needed here
221  1 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
222  1 af.saveAlignment(saveFastaFile, FileFormat.Fasta);
223   
224    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
225  1 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
226  1 QResponse response = QuitHandler.getQuitResponse(true);
227   
228    // if not saved this would be CANCEL_QUIT
229  1 Assert.assertEquals(response, QResponse.QUIT);
230    }
231   
 
232  1 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
233    public void testUnsavedChanges() throws Exception
234    {
235  1 String inFile = "examples/uniref50.fa";
236  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
237    DataSourceType.FILE);
238  1 assertNotNull(af, "Didn't read input file " + inFile);
239  1 AlignViewport viewport = af.getViewport();
240    // pretend something has happened
241  1 viewport.setSavedUpToDate(false);
242  1 Jalview2XML.setStateSavedUpToDate(false);
243   
244    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
245  1 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
246  1 QResponse response = QuitHandler.getQuitResponse(true);
247   
248  1 Assert.assertEquals(response, QResponse.CANCEL_QUIT);
249    }
250   
 
251  1 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
252    public void testNoGUIUnsavedChanges() throws Exception
253    {
254  1 String inFile = "examples/uniref50.fa";
255  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
256    DataSourceType.FILE);
257  1 assertNotNull(af, "Didn't read input file " + inFile);
258  1 AlignViewport viewport = af.getViewport();
259    // pretend something has happened
260  1 viewport.setSavedUpToDate(false);
261  1 Jalview2XML.setStateSavedUpToDate(false);
262   
263    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
264  1 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  1 QResponse response = QuitHandler.getQuitResponse(false);
274   
275  1 Assert.assertEquals(response, QResponse.QUIT);
276    }
277   
 
278  1 toggle @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
279    public void testForceQuit() throws Exception
280    {
281  1 String inFile = "examples/uniref50.fa";
282  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
283    DataSourceType.FILE);
284  1 assertNotNull(af, "Didn't read input file " + inFile);
285   
286  1 long start = System.currentTimeMillis();
287   
288    // start a long save (10s)
289  1 Jalview2XML.setDebugDelaySave(10);
290  1 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
291  1 Desktop.instance.saveState_actionPerformed(false);
292   
293    // give the saveState thread time to start!
294  1 Thread.sleep(100);
295   
296    // this will select "Force Quit"
297  1 JvOptionPane.setMockResponse(JvOptionPane.YES_OPTION);
298  1 QResponse response = QuitHandler.getQuitResponse(true,
299    QuitHandler.defaultOkQuit, () -> {
300    // set FORCE_QUIT without the force quit
301  2 jalview.bin.Console.debug(
302    "Setting FORCE_QUIT without actually quitting");
303  2 QuitHandler.setResponse(QResponse.FORCE_QUIT);
304    }, QuitHandler.defaultCancelQuit);
305  1 long end = System.currentTimeMillis();
306   
307  1 Assert.assertEquals(response, QResponse.FORCE_QUIT);
308    // if the wait (min wait is 1s) wasn't long enough...
309  1 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  1 Assert.assertTrue(end - start < 9090,
314    "Force-Quit-whilst-saving was too long (" + (end - start)
315    + "ms)");
316   
317    }
318   
319    }