2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.Assert.assertNotNull;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
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;
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;
45 @Test(singleThreaded = true)
46 public class QuitHandlerTest
48 private static String saveProjectFile = "test-output/tempSaveFile.jvp";
50 private static String saveFastaFile = "test-output/tempSaveFile.fa";
52 @BeforeClass(alwaysRun = true)
53 public void setUpJvOptionPane()
55 JvOptionPane.setInteractiveMode(false);
56 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57 Jalview2XML.setDebugDelaySave(3);
61 * @throws java.lang.Exception
63 @BeforeClass(alwaysRun = true)
64 public static void setUpBeforeClass() throws Exception
66 Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
69 * set news feed last read to a future time to ensure no
70 * 'unread' news item is displayed
72 Date oneHourFromNow = new Date(
73 System.currentTimeMillis() + 3600 * 1000);
74 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
78 { "--nowebservicediscovery", "--nosplash", "--nonews" });
81 @AfterClass(alwaysRun = true)
82 public static void resetProps()
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);
92 Jalview2XML.setDebugDelaySave(20);
93 // load normal testprops
94 Cache.loadProperties("test/jalview/testProps.jvprops");
97 @BeforeMethod(alwaysRun = true)
98 public static void tearDownAfterClass() throws Exception
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);
108 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
109 Jalview2XML.setDebugDelaySave(3);
110 // set the project file
111 Desktop.instance.setProjectFile(new File(saveProjectFile));
114 @AfterMethod(alwaysRun = true)
115 public static void cleanup()
118 List<String> files = new ArrayList<>();
119 files.add(saveProjectFile);
120 files.add(saveFastaFile);
121 for (String filename : files)
123 File file = new File(filename);
131 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
132 public void testInstantQuit() throws Exception
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);
139 long start = System.currentTimeMillis();
141 // if a save is attempted it will delay 3s
142 Jalview2XML.setDebugDelaySave(3);
143 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
145 // loaded file but haven't done anything, should just quit
146 QResponse response = QuitHandler.getQuitResponse(true);
147 long end = System.currentTimeMillis();
149 Assert.assertEquals(response, QResponse.QUIT);
150 Assert.assertTrue(end - start < 500,
151 "Quit-with-no-save-needed took too long (" + (end - start)
155 @Test(groups = { "Functional" }, singleThreaded = true, priority = 10)
156 public void testWaitForSaveQuit() throws Exception
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);
163 long start = System.currentTimeMillis();
165 // start a long save (3s)
166 Jalview2XML.setDebugDelaySave(3);
167 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
168 Desktop.instance.saveState_actionPerformed(false);
170 // give the saveState thread time to start!
173 // af.saveAlignment(saveProjectFile, FileFormat.Jalview);
174 QResponse response = QuitHandler.getQuitResponse(true);
175 long end = System.currentTimeMillis();
177 Assert.assertEquals(response, QResponse.QUIT);
178 Assert.assertTrue(end - start > 2900,
179 "Quit-whilst-saving was too short (" + (end - start) + "ms)");
182 @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
183 public void testSavedProjectChanges() throws Exception
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);
194 // don't want to hang around here
195 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
196 af.saveAlignment(saveProjectFile, FileFormat.Jalview);
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);
204 // if not saved this would be CANCEL_QUIT
205 Assert.assertEquals(response, QResponse.QUIT);
208 @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
209 public void testSavedAlignmentChanges() throws Exception
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);
220 // no hanging around needed here
221 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
222 af.saveAlignment(saveFastaFile, FileFormat.Fasta);
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);
228 // if not saved this would be CANCEL_QUIT
229 Assert.assertEquals(response, QResponse.QUIT);
232 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
233 public void testUnsavedChanges() throws Exception
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);
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);
248 Assert.assertEquals(response, QResponse.CANCEL_QUIT);
251 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
252 public void testNoGUIUnsavedChanges() throws Exception
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);
263 // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
264 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
266 QResponse response = QuitHandler.getQuitResponse(false,
267 QuitHandler.defaultOkQuit, () -> {
268 // set FORCE_QUIT without the force quit
269 QuitHandler.setResponse(QResponse.FORCE_QUIT);
271 }, QuitHandler.defaultCancelQuit);
273 QResponse response = QuitHandler.getQuitResponse(false);
275 Assert.assertEquals(response, QResponse.QUIT);
278 @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
279 public void testForceQuit() throws Exception
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);
286 long start = System.currentTimeMillis();
288 // start a long save (10s)
289 Jalview2XML.setDebugDelaySave(10);
290 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
291 Desktop.instance.saveState_actionPerformed(false);
293 // give the saveState thread time to start!
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();
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)
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)