3 import static org.testng.Assert.assertNotNull;
6 import java.util.ArrayList;
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;
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;
25 @Test(singleThreaded = true)
26 public class QuitHandlerTest
28 private static String saveProjectFile = "test-output/tempSaveFile.jvp";
30 private static String saveFastaFile = "test-output/tempSaveFile.fa";
32 @BeforeClass(alwaysRun = true)
33 public void setUpJvOptionPane()
35 JvOptionPane.setInteractiveMode(false);
36 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
37 Jalview2XML.setDebugDelaySave(3);
41 * @throws java.lang.Exception
43 @BeforeClass(alwaysRun = true)
44 public static void setUpBeforeClass() throws Exception
46 Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
49 * set news feed last read to a future time to ensure no
50 * 'unread' news item is displayed
52 Date oneHourFromNow = new Date(
53 System.currentTimeMillis() + 3600 * 1000);
54 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
58 { "-nowebservicediscovery", "-nosplash", "-nonews" });
61 @AfterClass(alwaysRun = true)
62 public static void resetProps()
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);
71 Jalview2XML.setDebugDelaySave(20);
74 @BeforeMethod(alwaysRun = true)
75 public static void tearDownAfterClass() throws Exception
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);
84 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
85 Jalview2XML.setDebugDelaySave(3);
86 // set the project file
87 Desktop.instance.setProjectFile(new File(saveProjectFile));
90 @AfterMethod(alwaysRun = true)
91 public static void cleanup()
94 List<String> files = new ArrayList<>();
95 files.add(saveProjectFile);
96 files.add(saveFastaFile);
97 for (String filename : files)
99 File file = new File(filename);
107 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
108 public void testInstantQuit() throws Exception
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);
115 long start = System.currentTimeMillis();
117 // if a save is attempted it will delay 3s
118 Jalview2XML.setDebugDelaySave(3);
119 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
121 // loaded file but haven't done anything, should just quit
122 QResponse response = QuitHandler.getQuitResponse(true);
123 long end = System.currentTimeMillis();
125 Assert.assertEquals(response, QResponse.QUIT);
126 Assert.assertTrue(end - start < 500,
127 "Quit-with-no-save-needed took too long (" + (end - start)
129 Desktop.instance.closeAll_actionPerformed(null);
132 @Test(groups = { "Functional" }, singleThreaded = true, priority = 10)
133 public void testWaitForSaveQuit() throws Exception
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);
140 long start = System.currentTimeMillis();
142 // start a long save (3s)
143 Jalview2XML.setDebugDelaySave(3);
144 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
145 Desktop.instance.saveState_actionPerformed(false);
147 // give the saveState thread time to start!
150 // af.saveAlignment(saveProjectFile, FileFormat.Jalview);
151 QResponse response = QuitHandler.getQuitResponse(true);
152 long end = System.currentTimeMillis();
154 Assert.assertEquals(response, QResponse.QUIT);
155 Assert.assertTrue(end - start > 2900,
156 "Quit-whilst-saving was too short (" + (end - start) + "ms)");
158 Desktop.instance.closeAll_actionPerformed(null);
161 @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
162 public void testSavedProjectChanges() throws Exception
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);
173 // don't want to hang around here
174 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
175 af.saveAlignment(saveProjectFile, FileFormat.Jalview);
177 Thread.sleep(1000); // give time for file to save
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);
185 // if not saved this would be CANCEL_QUIT
186 Assert.assertEquals(response, QResponse.QUIT);
187 Desktop.instance.closeAll_actionPerformed(null);
190 @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
191 public void testSavedAlignmentChanges() throws Exception
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);
202 // no hanging around needed here
203 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
204 af.saveAlignment(saveFastaFile, FileFormat.Fasta);
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);
210 // if not saved this would be CANCEL_QUIT
211 Assert.assertEquals(response, QResponse.QUIT);
212 Desktop.instance.closeAll_actionPerformed(null);
215 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
216 public void testUnsavedChanges() throws Exception
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);
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);
231 Assert.assertEquals(response, QResponse.CANCEL_QUIT);
232 Desktop.instance.closeAll_actionPerformed(null);
235 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
236 public void testNoGUIUnsavedChanges() throws Exception
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);
247 // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
248 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
250 QResponse response = QuitHandler.getQuitResponse(false,
251 QuitHandler.defaultOkQuit, () -> {
252 // set FORCE_QUIT without the force quit
253 QuitHandler.setResponse(QResponse.FORCE_QUIT);
255 }, QuitHandler.defaultCancelQuit);
257 QResponse response = QuitHandler.getQuitResponse(false);
259 Assert.assertEquals(response, QResponse.QUIT);
260 Desktop.instance.closeAll_actionPerformed(null);
263 @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
264 public void testForceQuit() throws Exception
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);
271 long start = System.currentTimeMillis();
273 // start a long save (10s)
274 Jalview2XML.setDebugDelaySave(10);
275 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
276 Desktop.instance.saveState_actionPerformed(false);
278 // give the saveState thread time to start!
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);
290 }, QuitHandler.defaultCancelQuit);
291 long end = System.currentTimeMillis();
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)
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)
303 Desktop.instance.closeAll_actionPerformed(null);