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 // 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);
183 // if not saved this would be CANCEL_QUIT
184 Assert.assertEquals(response, QResponse.QUIT);
185 Desktop.instance.closeAll_actionPerformed(null);
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);
210 Desktop.instance.closeAll_actionPerformed(null);
213 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
214 public void testUnsavedChanges() throws Exception
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);
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);
229 Assert.assertEquals(response, QResponse.CANCEL_QUIT);
230 Desktop.instance.closeAll_actionPerformed(null);
233 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
234 public void testNoGUIUnsavedChanges() throws Exception
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);
245 // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
246 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
248 QResponse response = QuitHandler.getQuitResponse(false,
249 QuitHandler.defaultOkQuit, () -> {
250 // set FORCE_QUIT without the force quit
251 QuitHandler.setResponse(QResponse.FORCE_QUIT);
253 }, QuitHandler.defaultCancelQuit);
255 QResponse response = QuitHandler.getQuitResponse(false);
257 Assert.assertEquals(response, QResponse.QUIT);
258 Desktop.instance.closeAll_actionPerformed(null);
261 @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
262 public void testForceQuit() throws Exception
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);
269 long start = System.currentTimeMillis();
271 // start a long save (10s)
272 Jalview2XML.setDebugDelaySave(10);
273 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
274 Desktop.instance.saveState_actionPerformed(false);
276 // give the saveState thread time to start!
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);
288 }, QuitHandler.defaultCancelQuit);
289 long end = System.currentTimeMillis();
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)
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)
301 Desktop.instance.closeAll_actionPerformed(null);