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