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