a8a7f08a40431146fae94dc82f2561c72d5e42b3
[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     // don't want to hang around here
174     Cache.setProperty("DEBUG_DELAY_SAVE", "false");
175     af.saveAlignment(saveProjectFile, FileFormat.Jalview);
176
177     Thread.sleep(1000); // give time for file to save
178
179     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION (to
180     // mean [CANCEL] -- file should already be saved so this doesn't happen and
181     // we get a QUIT response)
182     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
183     QResponse response = QuitHandler.getQuitResponse(true);
184
185     // if not saved this would be CANCEL_QUIT
186     Assert.assertEquals(response, QResponse.QUIT);
187     Desktop.instance.closeAll_actionPerformed(null);
188   }
189
190   @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
191   public void testSavedAlignmentChanges() throws Exception
192   {
193     String inFile = "examples/uniref50.fa";
194     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
195             DataSourceType.FILE);
196     assertNotNull(af, "Didn't read input file " + inFile);
197     AlignViewport viewport = af.getViewport();
198     // pretend something has happened
199     viewport.setSavedUpToDate(false);
200     Jalview2XML.setStateSavedUpToDate(false);
201
202     // no hanging around needed here
203     Cache.setProperty("DEBUG_DELAY_SAVE", "false");
204     af.saveAlignment(saveFastaFile, FileFormat.Fasta);
205
206     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
207     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
208     QResponse response = QuitHandler.getQuitResponse(true);
209
210     // if not saved this would be CANCEL_QUIT
211     Assert.assertEquals(response, QResponse.QUIT);
212     Desktop.instance.closeAll_actionPerformed(null);
213   }
214
215   @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
216   public void testUnsavedChanges() throws Exception
217   {
218     String inFile = "examples/uniref50.fa";
219     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
220             DataSourceType.FILE);
221     assertNotNull(af, "Didn't read input file " + inFile);
222     AlignViewport viewport = af.getViewport();
223     // pretend something has happened
224     viewport.setSavedUpToDate(false);
225     Jalview2XML.setStateSavedUpToDate(false);
226
227     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
228     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
229     QResponse response = QuitHandler.getQuitResponse(true);
230
231     Assert.assertEquals(response, QResponse.CANCEL_QUIT);
232     Desktop.instance.closeAll_actionPerformed(null);
233   }
234
235   @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
236   public void testNoGUIUnsavedChanges() throws Exception
237   {
238     String inFile = "examples/uniref50.fa";
239     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
240             DataSourceType.FILE);
241     assertNotNull(af, "Didn't read input file " + inFile);
242     AlignViewport viewport = af.getViewport();
243     // pretend something has happened
244     viewport.setSavedUpToDate(false);
245     Jalview2XML.setStateSavedUpToDate(false);
246
247     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
248     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
249     /*
250     QResponse response = QuitHandler.getQuitResponse(false,
251             QuitHandler.defaultOkQuit, () -> {
252               // set FORCE_QUIT without the force quit
253               QuitHandler.setResponse(QResponse.FORCE_QUIT);
254               return null;
255             }, QuitHandler.defaultCancelQuit);
256             */
257     QResponse response = QuitHandler.getQuitResponse(false);
258
259     Assert.assertEquals(response, QResponse.QUIT);
260     Desktop.instance.closeAll_actionPerformed(null);
261   }
262
263   @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
264   public void testForceQuit() throws Exception
265   {
266     String inFile = "examples/uniref50.fa";
267     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
268             DataSourceType.FILE);
269     assertNotNull(af, "Didn't read input file " + inFile);
270
271     long start = System.currentTimeMillis();
272
273     // start a long save (10s)
274     Jalview2XML.setDebugDelaySave(10);
275     Cache.setProperty("DEBUG_DELAY_SAVE", "true");
276     Desktop.instance.saveState_actionPerformed(false);
277
278     // give the saveState thread time to start!
279     Thread.sleep(100);
280
281     // this will select "Force Quit"
282     JvOptionPane.setMockResponse(JvOptionPane.YES_OPTION);
283     QResponse response = QuitHandler.getQuitResponse(true,
284             QuitHandler.defaultOkQuit, () -> {
285               // set FORCE_QUIT without the force quit
286               jalview.bin.Console.debug(
287                       "Setting FORCE_QUIT without actually quitting");
288               QuitHandler.setResponse(QResponse.FORCE_QUIT);
289               return null;
290             }, QuitHandler.defaultCancelQuit);
291     long end = System.currentTimeMillis();
292
293     Assert.assertEquals(response, QResponse.FORCE_QUIT);
294     // if the wait (min wait is 1s) wasn't long enough...
295     Assert.assertTrue(end - start > 1000,
296             "Force-Quit-whilst-saving was too short (" + (end - start)
297                     + "ms)");
298     // if the wait was too long (probably waited for file to save)
299     Assert.assertTrue(end - start < 9090,
300             "Force-Quit-whilst-saving was too long (" + (end - start)
301                     + "ms)");
302
303     Desktop.instance.closeAll_actionPerformed(null);
304   }
305
306 }