JAL-4386 Corrected failed test case.
[jalview.git] / test / jalview / gui / QuitHandlerTest.java
index 2caa8ec..b148d0b 100644 (file)
@@ -1,3 +1,23 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.gui;
 
 import static org.testng.Assert.assertNotNull;
@@ -55,7 +75,7 @@ public class QuitHandlerTest
 
     Jalview.main(
             new String[]
-            { "-nowebservicediscovery", "-nosplash", "-nonews" });
+            { "--nowebservicediscovery", "--nosplash", "--nonews" });
   }
 
   @AfterClass(alwaysRun = true)
@@ -66,9 +86,12 @@ public class QuitHandlerTest
     // reset mock response
     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
     // close desktop windows/frames
-    jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
+    if (Desktop.instance != null)
+      Desktop.instance.closeAll_actionPerformed(null);
     // reset debug delay
     Jalview2XML.setDebugDelaySave(20);
+    // load normal testprops
+    Cache.loadProperties("test/jalview/testProps.jvprops");
   }
 
   @BeforeMethod(alwaysRun = true)
@@ -79,10 +102,13 @@ public class QuitHandlerTest
     // reset mock response
     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
     // close desktop windows/frames
-    jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
+    if (Desktop.instance != null)
+      Desktop.instance.closeAll_actionPerformed(null);
     // reset debug delay
     Cache.setProperty("DEBUG_DELAY_SAVE", "false");
     Jalview2XML.setDebugDelaySave(3);
+    // set the project file
+    Desktop.instance.setProjectFile(new File(saveProjectFile));
   }
 
   @AfterMethod(alwaysRun = true)
@@ -113,6 +139,7 @@ public class QuitHandlerTest
     long start = System.currentTimeMillis();
 
     // if a save is attempted it will delay 3s
+    Jalview2XML.setDebugDelaySave(3);
     Cache.setProperty("DEBUG_DELAY_SAVE", "true");
 
     // loaded file but haven't done anything, should just quit
@@ -120,10 +147,9 @@ public class QuitHandlerTest
     long end = System.currentTimeMillis();
 
     Assert.assertEquals(response, QResponse.QUIT);
-    Assert.assertTrue(end - start < 1000,
+    Assert.assertTrue(end - start < 500,
             "Quit-with-no-save-needed took too long (" + (end - start)
                     + "ms)");
-    Desktop.instance.closeAll_actionPerformed(null);
   }
 
   @Test(groups = { "Functional" }, singleThreaded = true, priority = 10)
@@ -137,22 +163,20 @@ public class QuitHandlerTest
     long start = System.currentTimeMillis();
 
     // start a long save (3s)
+    Jalview2XML.setDebugDelaySave(3);
     Cache.setProperty("DEBUG_DELAY_SAVE", "true");
-    af.saveAlignment(saveProjectFile, FileFormat.Jalview);
+    Desktop.instance.saveState_actionPerformed(false);
 
-    QResponse response = QuitHandler.getQuitResponse(true,
-            QuitHandler.defaultOkQuit, () -> {
-              // set FORCE_QUIT without the force quit
-              QuitHandler.setResponse(QResponse.FORCE_QUIT);
-              return null;
-            }, QuitHandler.defaultCancelQuit);
+    // give the saveState thread time to start!
+    Thread.sleep(500);
+
+    // af.saveAlignment(saveProjectFile, FileFormat.Jalview);
+    QResponse response = QuitHandler.getQuitResponse(true);
     long end = System.currentTimeMillis();
 
     Assert.assertEquals(response, QResponse.QUIT);
     Assert.assertTrue(end - start > 2900,
             "Quit-whilst-saving was too short (" + (end - start) + "ms)");
-
-    Desktop.instance.closeAll_actionPerformed(null);
   }
 
   @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
@@ -167,22 +191,18 @@ public class QuitHandlerTest
     viewport.setSavedUpToDate(false);
     Jalview2XML.setStateSavedUpToDate(false);
 
-    // no hanging around needed here
+    // don't want to hang around here
     Cache.setProperty("DEBUG_DELAY_SAVE", "false");
     af.saveAlignment(saveProjectFile, FileFormat.Jalview);
 
-    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
+    // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION (to
+    // mean [CANCEL] -- file should already be saved so this doesn't happen and
+    // we get a QUIT response)
     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
-    QResponse response = QuitHandler.getQuitResponse(true,
-            QuitHandler.defaultOkQuit, () -> {
-              // set FORCE_QUIT without the force quit
-              QuitHandler.setResponse(QResponse.FORCE_QUIT);
-              return null;
-            }, QuitHandler.defaultCancelQuit);
+    QResponse response = QuitHandler.getQuitResponse(true);
 
     // if not saved this would be CANCEL_QUIT
     Assert.assertEquals(response, QResponse.QUIT);
-    Desktop.instance.closeAll_actionPerformed(null);
   }
 
   @Test(groups = { "Functional" }, singleThreaded = true, priority = 9)
@@ -203,16 +223,10 @@ public class QuitHandlerTest
 
     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
-    QResponse response = QuitHandler.getQuitResponse(true,
-            QuitHandler.defaultOkQuit, () -> {
-              // set FORCE_QUIT without the force quit
-              QuitHandler.setResponse(QResponse.FORCE_QUIT);
-              return null;
-            }, QuitHandler.defaultCancelQuit);
+    QResponse response = QuitHandler.getQuitResponse(true);
 
     // if not saved this would be CANCEL_QUIT
     Assert.assertEquals(response, QResponse.QUIT);
-    Desktop.instance.closeAll_actionPerformed(null);
   }
 
   @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
@@ -229,15 +243,9 @@ public class QuitHandlerTest
 
     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
-    QResponse response = QuitHandler.getQuitResponse(true,
-            QuitHandler.defaultOkQuit, () -> {
-              // set FORCE_QUIT without the force quit
-              QuitHandler.setResponse(QResponse.FORCE_QUIT);
-              return null;
-            }, QuitHandler.defaultCancelQuit);
+    QResponse response = QuitHandler.getQuitResponse(true);
 
     Assert.assertEquals(response, QResponse.CANCEL_QUIT);
-    Desktop.instance.closeAll_actionPerformed(null);
   }
 
   @Test(groups = { "Functional" }, singleThreaded = true, priority = 1)
@@ -254,15 +262,58 @@ public class QuitHandlerTest
 
     // this is only a two button dialog [Quit] [Cancel] so use NO_OPTION
     JvOptionPane.setMockResponse(JvOptionPane.NO_OPTION);
+    /*
     QResponse response = QuitHandler.getQuitResponse(false,
             QuitHandler.defaultOkQuit, () -> {
               // set FORCE_QUIT without the force quit
               QuitHandler.setResponse(QResponse.FORCE_QUIT);
               return null;
             }, QuitHandler.defaultCancelQuit);
+            */
+    QResponse response = QuitHandler.getQuitResponse(false);
 
     Assert.assertEquals(response, QResponse.QUIT);
-    Desktop.instance.closeAll_actionPerformed(null);
+  }
+
+  @Test(groups = { "Functional" }, singleThreaded = true, priority = 11)
+  public void testForceQuit() throws Exception
+  {
+    String inFile = "examples/uniref50.fa";
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(inFile,
+            DataSourceType.FILE);
+    assertNotNull(af, "Didn't read input file " + inFile);
+
+    long start = System.currentTimeMillis();
+
+    // start a long save (10s)
+    Jalview2XML.setDebugDelaySave(10);
+    Cache.setProperty("DEBUG_DELAY_SAVE", "true");
+    Desktop.instance.saveState_actionPerformed(false);
+
+    // give the saveState thread time to start!
+    Thread.sleep(100);
+
+    // this will select "Force Quit"
+    JvOptionPane.setMockResponse(JvOptionPane.YES_OPTION);
+    QResponse response = QuitHandler.getQuitResponse(true,
+            QuitHandler.defaultOkQuit, () -> {
+              // set FORCE_QUIT without the force quit
+              jalview.bin.Console.debug(
+                      "Setting FORCE_QUIT without actually quitting");
+              QuitHandler.setResponse(QResponse.FORCE_QUIT);
+            }, QuitHandler.defaultCancelQuit);
+    long end = System.currentTimeMillis();
+
+    Assert.assertEquals(response, QResponse.FORCE_QUIT);
+    // if the wait (min wait is 1s) wasn't long enough...
+    Assert.assertTrue(end - start > 1000,
+            "Force-Quit-whilst-saving was too short (" + (end - start)
+                    + "ms)");
+    // if the wait was too long (probably waited for file to save)
+    Assert.assertTrue(end - start < 9090,
+            "Force-Quit-whilst-saving was too long (" + (end - start)
+                    + "ms)");
+
   }
 
 }