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);
75 @BeforeMethod(alwaysRun = true)
76 public static void tearDownAfterClass() throws Exception
78 // reset quit response
79 QuitHandler.setResponse(QResponse.NULL);
80 // reset mock response
81 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
82 // close desktop windows/frames
83 if (Desktop.instance != null)
84 Desktop.instance.closeAll_actionPerformed(null);
86 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
87 Jalview2XML.setDebugDelaySave(3);
88 // set the project file
89 Desktop.instance.setProjectFile(new File(saveProjectFile));
92 @AfterMethod(alwaysRun = true)
93 public static void cleanup()
96 List<String> files = new ArrayList<>();
97 files.add(saveProjectFile);
98 files.add(saveFastaFile);
99 for (String filename : files)
101 File file = new File(filename);
109 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
110 public void testInstantQuit() throws Exception
112 String inFile = "examples/uniref50.fa";
113 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
114 DataSourceType.FILE);
115 assertNotNull(af, "Didn't read input file " + inFile);
117 long start = System.currentTimeMillis();
119 // if a save is attempted it will delay 3s
120 Jalview2XML.setDebugDelaySave(3);
121 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
123 // loaded file but haven't done anything, should just quit
124 QResponse response = QuitHandler.getQuitResponse(true);
125 long end = System.currentTimeMillis();
127 Assert.assertEquals(response, QResponse.QUIT);
128 Assert.assertTrue(end - start < 500,
129 "Quit-with-no-save-needed took too long (" + (end - start)
133 @Test(groups = { "Functional" }, singleThreaded = true, priority = 10)
134 public void testWaitForSaveQuit() throws Exception
136 String inFile = "examples/uniref50.fa";
137 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
138 DataSourceType.FILE);
139 assertNotNull(af, "Didn't read input file " + inFile);
141 long start = System.currentTimeMillis();
143 // start a long save (3s)
144 Jalview2XML.setDebugDelaySave(3);
145 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
146 Desktop.instance.saveState_actionPerformed(false);
148 // give the saveState thread time to start!
151 // af.saveAlignment(saveProjectFile, FileFormat.Jalview);
152 QResponse response = QuitHandler.getQuitResponse(true);
153 long end = System.currentTimeMillis();
155 Assert.assertEquals(response, QResponse.QUIT);
156 Assert.assertTrue(end - start > 2900,
157 "Quit-whilst-saving was too short (" + (end - start) + "ms)");
160 @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
161 public void testSavedProjectChanges() throws Exception
163 String inFile = "examples/uniref50.fa";
164 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
165 DataSourceType.FILE);
166 assertNotNull(af, "Didn't read input file " + inFile);
167 AlignViewport viewport = af.getViewport();
168 // pretend something has happened
169 viewport.setSavedUpToDate(false);
170 Jalview2XML.setStateSavedUpToDate(false);
172 // don't want to hang around here
173 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
174 af.saveAlignment(saveProjectFile, FileFormat.Jalview);
176 // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION (to
177 // mean [CANCEL] -- file should already be saved so this doesn't happen and
178 // we get a QUIT response)
179 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
180 QResponse response = QuitHandler.getQuitResponse(true);
182 // if not saved this would be CANCEL_QUIT
183 Assert.assertEquals(response, QResponse.QUIT);
186 @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
187 public void testSavedAlignmentChanges() throws Exception
189 String inFile = "examples/uniref50.fa";
190 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
191 DataSourceType.FILE);
192 assertNotNull(af, "Didn't read input file " + inFile);
193 AlignViewport viewport = af.getViewport();
194 // pretend something has happened
195 viewport.setSavedUpToDate(false);
196 Jalview2XML.setStateSavedUpToDate(false);
198 // no hanging around needed here
199 Cache.setProperty("DEBUG_DELAY_SAVE", "false");
200 af.saveAlignment(saveFastaFile, FileFormat.Fasta);
202 // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
203 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
204 QResponse response = QuitHandler.getQuitResponse(true);
206 // if not saved this would be CANCEL_QUIT
207 Assert.assertEquals(response, QResponse.QUIT);
210 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
211 public void testUnsavedChanges() throws Exception
213 String inFile = "examples/uniref50.fa";
214 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
215 DataSourceType.FILE);
216 assertNotNull(af, "Didn't read input file " + inFile);
217 AlignViewport viewport = af.getViewport();
218 // pretend something has happened
219 viewport.setSavedUpToDate(false);
220 Jalview2XML.setStateSavedUpToDate(false);
222 // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
223 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
224 QResponse response = QuitHandler.getQuitResponse(true);
226 Assert.assertEquals(response, QResponse.CANCEL_QUIT);
229 @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
230 public void testNoGUIUnsavedChanges() throws Exception
232 String inFile = "examples/uniref50.fa";
233 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
234 DataSourceType.FILE);
235 assertNotNull(af, "Didn't read input file " + inFile);
236 AlignViewport viewport = af.getViewport();
237 // pretend something has happened
238 viewport.setSavedUpToDate(false);
239 Jalview2XML.setStateSavedUpToDate(false);
241 // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
242 JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
244 QResponse response = QuitHandler.getQuitResponse(false,
245 QuitHandler.defaultOkQuit, () -> {
246 // set FORCE_QUIT without the force quit
247 QuitHandler.setResponse(QResponse.FORCE_QUIT);
249 }, QuitHandler.defaultCancelQuit);
251 QResponse response = QuitHandler.getQuitResponse(false);
253 Assert.assertEquals(response, QResponse.QUIT);
256 @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
257 public void testForceQuit() throws Exception
259 String inFile = "examples/uniref50.fa";
260 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
261 DataSourceType.FILE);
262 assertNotNull(af, "Didn't read input file " + inFile);
264 long start = System.currentTimeMillis();
266 // start a long save (10s)
267 Jalview2XML.setDebugDelaySave(10);
268 Cache.setProperty("DEBUG_DELAY_SAVE", "true");
269 Desktop.instance.saveState_actionPerformed(false);
271 // give the saveState thread time to start!
274 // this will select "Force Quit"
275 JvOptionPane.setMockResponse(JvOptionPane.YES_OPTION);
276 QResponse response = QuitHandler.getQuitResponse(true,
277 QuitHandler.defaultOkQuit, () -> {
278 // set FORCE_QUIT without the force quit
279 jalview.bin.Console.debug(
280 "Setting FORCE_QUIT without actually quitting");
281 QuitHandler.setResponse(QResponse.FORCE_QUIT);
283 }, QuitHandler.defaultCancelQuit);
284 long end = System.currentTimeMillis();
286 Assert.assertEquals(response, QResponse.FORCE_QUIT);
287 // if the wait (min wait is 1s) wasn't long enough...
288 Assert.assertTrue(end - start > 1000,
289 "Force-Quit-whilst-saving was too short (" + (end - start)
291 // if the wait was too long (probably waited for file to save)
292 Assert.assertTrue(end - start < 9090,
293 "Force-Quit-whilst-saving was too long (" + (end - start)