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 if (Desktop.instance != null)
70 Desktop.instance.closeAll_actionPerformed(null);
72 Jalview2XML.setDebugDelaySave(20);
73 // load normal testprops
74 Cache.loadProperties("test/jalview/testProps.jvprops");
77 @BeforeMethod(alwaysRun = true)
78 public static void tearDownAfterClass() throws Exception
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);
88 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
89 Jalview2XML.setDebugDelaySave(3);
90 // set the project file
91 Desktop.instance.setProjectFile(new File(saveProjectFile));
94 @AfterMethod(alwaysRun = true)
95 public static void cleanup()
98 List<String> files = new ArrayList<>();
99 files.add(saveProjectFile);
100 files.add(saveFastaFile);
101 for (String filename : files)
103 File file = new File(filename);
111 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
112 public void testInstantQuit() throws Exception
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);
119 long start = System.currentTimeMillis();
121 // if a save is attempted it will delay 3s
122 Jalview2XML.setDebugDelaySave(3);
123 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
125 // loaded file but haven't done anything, should just quit
126 QResponse response = QuitHandler.getQuitResponse(true);
127 long end = System.currentTimeMillis();
129 Assert.assertEquals(response, QResponse.QUIT);
130 Assert.assertTrue(end - start < 500,
131 "Quit-with-no-save-needed took too long (" + (end - start)
135 @Test(groups = { "Functional" }, singleThreaded = true, priority = 10)
136 public void testWaitForSaveQuit() throws Exception
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);
143 long start = System.currentTimeMillis();
145 // start a long save (3s)
146 Jalview2XML.setDebugDelaySave(3);
147 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
148 Desktop.instance.saveState_actionPerformed(false);
150 // give the saveState thread time to start!
153 // af.saveAlignment(saveProjectFile, FileFormat.Jalview);
154 QResponse response = QuitHandler.getQuitResponse(true);
155 long end = System.currentTimeMillis();
157 Assert.assertEquals(response, QResponse.QUIT);
158 Assert.assertTrue(end - start > 2900,
159 "Quit-whilst-saving was too short (" + (end - start) + "ms)");
162 @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
163 public void testSavedProjectChanges() throws Exception
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);
174 // don't want to hang around here
175 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
176 af.saveAlignment(saveProjectFile, FileFormat.Jalview);
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);
184 // if not saved this would be CANCEL_QUIT
185 Assert.assertEquals(response, QResponse.QUIT);
188 @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
189 public void testSavedAlignmentChanges() throws Exception
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);
200 // no hanging around needed here
201 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
202 af.saveAlignment(saveFastaFile, FileFormat.Fasta);
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);
208 // if not saved this would be CANCEL_QUIT
209 Assert.assertEquals(response, QResponse.QUIT);
212 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
213 public void testUnsavedChanges() throws Exception
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);
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 Assert.assertEquals(response, QResponse.CANCEL_QUIT);
231 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
232 public void testNoGUIUnsavedChanges() throws Exception
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);
243 // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
244 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
246 QResponse response = QuitHandler.getQuitResponse(false,
247 QuitHandler.defaultOkQuit, () -> {
248 // set FORCE_QUIT without the force quit
249 QuitHandler.setResponse(QResponse.FORCE_QUIT);
251 }, QuitHandler.defaultCancelQuit);
253 QResponse response = QuitHandler.getQuitResponse(false);
255 Assert.assertEquals(response, QResponse.QUIT);
258 @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
259 public void testForceQuit() throws Exception
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);
266 long start = System.currentTimeMillis();
268 // start a long save (10s)
269 Jalview2XML.setDebugDelaySave(10);
270 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
271 Desktop.instance.saveState_actionPerformed(false);
273 // give the saveState thread time to start!
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();
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)
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)