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