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