Add utility method to close secondary shells after tests
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotUtils.java
index 6185cc3c6edcce6491f81e5a868b262b6e83d2f0..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);
     }
 
     /**
@@ -397,6 +435,10 @@ public final class SWTBotUtils {
         String gtkVersion = System.getProperty("org.eclipse.swt.internal.gtk.version");
         if (gtkVersion != null) {
             System.out.println("GTK version=" + gtkVersion);
+            // Try to print the GTK theme information as behavior can change depending on the theme
+            String gtkTheme = System.getProperty("org.eclipse.swt.internal.gtk.theme");
+            System.out.println("GTK theme=" + (gtkTheme == null ? "unknown" : gtkTheme));
+
             String overlayScrollbar = System.getenv("LIBOVERLAY_SCROLLBAR");
             if (overlayScrollbar != null) {
                 System.out.println("LIBOVERLAY_SCROLLBAR=" + overlayScrollbar);
@@ -549,7 +591,6 @@ public final class SWTBotUtils {
         SWTBotTreeItem currentItem = tracesNode;
         for (String segment : elementPath.segments()) {
             currentItem = getTraceProjectItem(projectExplorerBot, currentItem, segment);
-            currentItem.select();
             currentItem.doubleClick();
         }
 
@@ -643,6 +684,26 @@ public final class SWTBotUtils {
         });
     }
 
+    /**
+     * Clear the trace folder (using the UI)
+     *
+     * @param bot
+     *            a given workbench bot
+     * @param projectName
+     *            the name of the project (needs to exist)
+     */
+    public static void clearTracesFolderUI(SWTWorkbenchBot bot, String projectName) {
+        SWTBotTreeItem tracesFolder = selectTracesFolder(bot, projectName);
+        tracesFolder.contextMenu().menu("Clear").click();
+        String CONFIRM_CLEAR_DIALOG_TITLE = "Confirm Clear";
+        bot.waitUntil(Conditions.shellIsActive(CONFIRM_CLEAR_DIALOG_TITLE));
+
+        SWTBotShell shell = bot.shell(CONFIRM_CLEAR_DIALOG_TITLE);
+        shell.bot().button("Yes").click();
+        bot.waitUntil(Conditions.shellCloses(shell));
+        bot.waitWhile(ConditionHelpers.treeItemHasChildren(tracesFolder));
+    }
+
     /**
      * Clear the experiment folder
      *
This page took 0.026922 seconds and 5 git commands to generate.