a6a964289d5378924f0b56ea9e0c23b81bedf19a
[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     // load normal testprops
74     Cache.loadProperties("test/jalview/testProps.jvprops");
75   }
76
77   @BeforeMethod(alwaysRun = true)
78   public static void tearDownAfterClass() throws Exception
79   {
80     // reset quit response
81     QuitHandler.setResponse(QResponse.NULL);
82     // reset mock response
83     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
84     // close desktop windows/frames
85     if (Desktop.instance != null)
86       Desktop.instance.closeAll_actionPerformed(null);
87     // reset debug delay
88     Cache.setProperty("DEBUG_DELAY_SAVE", "false");
89     Jalview2XML.setDebugDelaySave(3);
90     // set the project file
91     Desktop.instance.setProjectFile(new File(saveProjectFile));
92   }
93
94   @AfterMethod(alwaysRun = true)
95   public static void cleanup()
96   {
97     // delete save files
98     List<String> files = new ArrayList<>();
99     files.add(saveProjectFile);
100     files.add(saveFastaFile);
101     for (String filename : files)
102     {
103       File file = new File(filename);
104       if (file.exists())
105       {
106         file.delete();
107       }
108     }
109   }
110
111   @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
112   public void testInstantQuit() throws Exception
113   {
114     String inFile = "examples/uniref50.fa";
115     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
116             DataSourceType.FILE);
117     assertNotNull(af, "Didn't read input file " + inFile);
118
119     long start = System.currentTimeMillis();
120
121     // if a save is attempted it will delay 3s
122     Jalview2XML.setDebugDelaySave(3);
123     Cache.setProperty("DEBUG_DELAY_SAVE", "true");
124
125     // loaded file but haven't done anything, should just quit
126     QResponse response = QuitHandler.getQuitResponse(true);
127     long end = System.currentTimeMillis();
128
129     Assert.assertEquals(response, QResponse.QUIT);
130     Assert.assertTrue(end - start < 500,
131             "Quit-with-no-save-needed took too long (" + (end - start)
132                     + "ms)");
133   }
134
135   @Test(groups = { "Functional" }, singleThreaded = true, priority = 10)
136   public void testWaitForSaveQuit() throws Exception
137   {
138     String inFile = "examples/uniref50.fa";
139     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
140             DataSourceType.FILE);
141     assertNotNull(af, "Didn't read input file " + inFile);
142
143     long start = System.currentTimeMillis();
144
145     // start a long save (3s)
146     Jalview2XML.setDebugDelaySave(3);
147     Cache.setProperty("DEBUG_DELAY_SAVE", "true");
148     Desktop.instance.saveState_actionPerformed(false);
149
150     // give the saveState thread time to start!
151     Thread.sleep(500);
152
153     // af.saveAlignment(saveProjectFile, FileFormat.Jalview);
154     QResponse response = QuitHandler.getQuitResponse(true);
155     long end = System.currentTimeMillis();
156
157     Assert.assertEquals(response, QResponse.QUIT);
158     Assert.assertTrue(end - start > 2900,
159             "Quit-whilst-saving was too short (" + (end - start) + "ms)");
160   }
161
162   @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
163   public void testSavedProjectChanges() throws Exception
164   {
165     String inFile = "examples/uniref50.fa";
166     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
167             DataSourceType.FILE);
168     assertNotNull(af, "Didn't read input file " + inFile);
169     AlignViewport viewport = af.getViewport();
170     // pretend something has happened
171     viewport.setSavedUpToDate(false);
172     Jalview2XML.setStateSavedUpToDate(false);
173
174     // don't want to hang around here
175     Cache.setProperty("DEBUG_DELAY_SAVE", "false");
176     af.saveAlignment(saveProjectFile, FileFormat.Jalview);
177
178     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION (to
179     // mean [CANCEL] -- file should already be saved so this doesn't happen and
180     // we get a QUIT response)
181     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
182     QResponse response = QuitHandler.getQuitResponse(true);
183
184     // if not saved this would be CANCEL_QUIT
185     Assert.assertEquals(response, QResponse.QUIT);
186   }
187
188   @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
189   public void testSavedAlignmentChanges() throws Exception
190   {
191     String inFile = "examples/uniref50.fa";
192     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
193             DataSourceType.FILE);
194     assertNotNull(af, "Didn't read input file " + inFile);
195     AlignViewport viewport = af.getViewport();
196     // pretend something has happened
197     viewport.setSavedUpToDate(false);
198     Jalview2XML.setStateSavedUpToDate(false);
199
200     // no hanging around needed here
201     Cache.setProperty("DEBUG_DELAY_SAVE", "false");
202     af.saveAlignment(saveFastaFile, FileFormat.Fasta);
203
204     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
205     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
206     QResponse response = QuitHandler.getQuitResponse(true);
207
208     // if not saved this would be CANCEL_QUIT
209     Assert.assertEquals(response, QResponse.QUIT);
210   }
211
212   @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
213   public void testUnsavedChanges() throws Exception
214   {
215     String inFile = "examples/uniref50.fa";
216     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
217             DataSourceType.FILE);
218     assertNotNull(af, "Didn't read input file " + inFile);
219     AlignViewport viewport = af.getViewport();
220     // pretend something has happened
221     viewport.setSavedUpToDate(false);
222     Jalview2XML.setStateSavedUpToDate(false);
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     Assert.assertEquals(response, QResponse.CANCEL_QUIT);
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   }
257
258   @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
259   public void testForceQuit() throws Exception
260   {
261     String inFile = "examples/uniref50.fa";
262     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
263             DataSourceType.FILE);
264     assertNotNull(af, "Didn't read input file " + inFile);
265
266     long start = System.currentTimeMillis();
267
268     // start a long save (10s)
269     Jalview2XML.setDebugDelaySave(10);
270     Cache.setProperty("DEBUG_DELAY_SAVE", "true");
271     Desktop.instance.saveState_actionPerformed(false);
272
273     // give the saveState thread time to start!
274     Thread.sleep(100);
275
276     // this will select "Force Quit"
277     JvOptionPane.setMockResponse(JvOptionPane.YES_OPTION);
278     QResponse response = QuitHandler.getQuitResponse(true,
279             QuitHandler.defaultOkQuit, () -> {
280               // set FORCE_QUIT without the force quit
281               jalview.bin.Console.debug(
282                       "Setting FORCE_QUIT without actually quitting");
283               QuitHandler.setResponse(QResponse.FORCE_QUIT);
284             }, QuitHandler.defaultCancelQuit);
285     long end = System.currentTimeMillis();
286
287     Assert.assertEquals(response, QResponse.FORCE_QUIT);
288     // if the wait (min wait is 1s) wasn't long enough...
289     Assert.assertTrue(end - start > 1000,
290             "Force-Quit-whilst-saving was too short (" + (end - start)
291                     + "ms)");
292     // if the wait was too long (probably waited for file to save)
293     Assert.assertTrue(end - start < 9090,
294             "Force-Quit-whilst-saving was too long (" + (end - start)
295                     + "ms)");
296
297   }
298
299 }