Add utility method to close secondary shells after tests
authorMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Mon, 19 Sep 2016 04:47:02 +0000 (00:47 -0400)
committerMarc-Andre Laperle <marc-andre.laperle@ericsson.com>
Thu, 6 Oct 2016 19:31:01 +0000 (15:31 -0400)
When running SWTBot tests, sometimes when one test fails and leave a
shell lingering, it makes all the subsequent tests fail. This change
introduces a utility method to close all the secondary shells
(non-main shell) so that subsequent tests have a better chance of
completing successfully. This will help getting a more representative
account of test failures when things go wrong.

Change-Id: I4fe48c13de8588e9ddb21ae64d2c3b6a9c04b4ae
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/81303
Reviewed-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-by: Hudson CI
ctf/org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ctf/ui/swtbot/tests/TestInvalidCtfTrace.java
lttng/org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests/src/org/eclipse/tracecompass/lttng2/kernel/ui/swtbot/tests/FindDialogTestBase.java
lttng/org.eclipse.tracecompass.lttng2.kernel.ui.swtbot.tests/src/org/eclipse/tracecompass/lttng2/kernel/ui/swtbot/tests/KernelTestBase.java
releng/org.eclipse.tracecompass.integration.swtbot.tests/src/org/eclipse/tracecompass/integration/swtbot/tests/projectexplorer/ProjectExplorerTraceActionsTest.java
tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/shared/org/eclipse/tracecompass/tmf/ui/swtbot/tests/shared/SWTBotUtils.java
tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/ColumnHeaderMenuTest.java
tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/CopyToClipboardTest.java
tmf/org.eclipse.tracecompass.tmf.ui.swtbot.tests/src/org/eclipse/tracecompass/tmf/ui/swtbot/tests/viewers/events/TmfAlignTimeAxisTest.java

index b59b611a861dab8fa30a43c957152cc567da33cd..a6888ccc1adbf52a3fbaa5405090feb519b0bd2d 100644 (file)
@@ -190,6 +190,7 @@ public class TestInvalidCtfTrace {
     @After
     public void teardown() {
         SWTBotUtils.clearTracesFolder(fBot, PROJET_NAME);
+        SWTBotUtils.closeSecondaryShells(fBot);
     }
 
     /**
index aeb7b150f46b7f25e62da2191dda25d49f3ae3d7..0caaf6a3cdcc79f17ddcddbd9e7799b78af04299 100644 (file)
@@ -38,7 +38,7 @@ import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
 import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractTimeGraphView;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
-import org.junit.AfterClass;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -94,10 +94,10 @@ public abstract class FindDialogTestBase extends KernelTestBase {
     }
 
     /**
-     * After class method to close the dialog
+     * After method to close the dialog
      */
-    @AfterClass
-    public static void afterTest() {
+    @After
+    public void afterTest() {
         closeDialog(getDialogBot());
     }
 
index 1c5609c9473f79d63556970f73ef808721269dfa..a35085cc7fb42f2f5152c8c09aea6f4eece1b5c3 100644 (file)
@@ -41,6 +41,7 @@ import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers;
 import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils;
 import org.eclipse.tracecompass.tmf.ui.views.statistics.TmfStatisticsView;
 import org.eclipse.ui.IEditorReference;
+import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -118,6 +119,14 @@ public class ProjectExplorerTraceActionsTest {
         fLogger.removeAllAppenders();
     }
 
+    /**
+     * Test tear down method.
+     */
+    @After
+    public void afterTest() {
+        SWTBotUtils.closeSecondaryShells(fBot);
+    }
+
     /**
      * Test that the expected context menu items are there
      * <p>
index 5232ca4abd4e7a0500714c0aa732772798d88b6c..189db0eb91136f70ce059e30a1fbd723d38e7d7e 100644 (file)
@@ -21,6 +21,7 @@ import java.util.List;
 import java.util.TimeZone;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.log4j.Logger;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
@@ -52,6 +53,7 @@ import org.eclipse.swtbot.swt.finder.keyboard.Keyboard;
 import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
 import org.eclipse.swtbot.swt.finder.results.Result;
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
+import org.eclipse.swtbot.swt.finder.utils.MessageFormat;
 import org.eclipse.swtbot.swt.finder.utils.SWTUtils;
 import org.eclipse.swtbot.swt.finder.waits.Conditions;
 import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
@@ -95,8 +97,10 @@ public final class SWTBotUtils {
     private static final String WINDOW_MENU = "Window";
     private static final String PREFERENCES_MENU_ITEM = "Preferences";
     private static boolean fPrintedEnvironment = false;
+    private static Logger log = Logger.getLogger(SWTBotUtils.class);
 
     private SWTBotUtils() {
+
     }
 
     private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
@@ -218,6 +222,9 @@ public final class SWTBotUtils {
 
         SWTBotUtils.waitForJobs();
 
+        closeSecondaryShells(bot);
+        SWTBotUtils.waitForJobs();
+
         final SWTBotView projectViewBot = bot.viewById(IPageLayout.ID_PROJECT_EXPLORER);
         projectViewBot.setFocus();
 
@@ -292,11 +299,42 @@ public final class SWTBotUtils {
      *            swtbotshells for all the shells
      */
     public static void focusMainWindow(SWTBotShell[] shellBots) {
+        SWTBotShell mainShell = getMainShell(shellBots);
+        if (mainShell != null) {
+            mainShell.activate();
+        }
+    }
+
+    private static SWTBotShell getMainShell(SWTBotShell[] shellBots) {
+        SWTBotShell mainShell = null;
         for (SWTBotShell shellBot : shellBots) {
             if (shellBot.getText().toLowerCase().contains("eclipse")) {
-                shellBot.activate();
+                mainShell = shellBot;
             }
         }
+        return mainShell;
+    }
+
+    /**
+     * Close all non-main shells that are visible.
+     *
+     * @param bot
+     *            the workbench bot
+     */
+    public static void closeSecondaryShells(SWTWorkbenchBot bot) {
+        SWTBotShell[] shells = bot.shells();
+        SWTBotShell mainShell = getMainShell(shells);
+        if (mainShell == null) {
+            return;
+        }
+
+        // Close all non-main shell but make sure we don't close an invisible
+        // shell such the special "limbo shell" that Eclipse needs to work
+        Arrays.stream(shells)
+                .filter(shell -> shell != mainShell)
+                .filter(SWTBotShell::isVisible)
+                .peek(shell -> log.debug(MessageFormat.format("Closing ligering shell with title {0}", shell.getText())))
+                .forEach(SWTBotShell::close);
     }
 
     /**
index d57be3115910c77841c96514d68b7055df093cca..7f47ad28c62d0e5bd4b9b98f74a43e667fa8d8d7 100644 (file)
@@ -148,6 +148,7 @@ public class TmfAlignTimeAxisTest {
         SWTBotUtils.activeEventsEditor(bot).close();
         /* Switch back to Tracing perspective since several tests change the perspective */
         SWTBotUtils.switchToTracingPerspective();
+        SWTBotUtils.closeSecondaryShells(fBot);
     }
 
     /**
This page took 0.028563 seconds and 5 git commands to generate.