Remove extra item.select() calls
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotUtils.java
index 3e0638adeccb254ed24315291d4048191106aebe..0be83df52f13c5aaf175c74d4b7e20d5929c5533 100644 (file)
@@ -16,6 +16,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.util.Arrays;
 import java.util.List;
 import java.util.TimeZone;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -47,6 +48,8 @@ import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
 import org.eclipse.swtbot.swt.finder.SWTBot;
 import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
+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.SWTUtils;
@@ -88,6 +91,7 @@ import org.hamcrest.Matcher;
 @SuppressWarnings("restriction")
 public final class SWTBotUtils {
 
+    private static final long MAX_JOBS_WAIT_TIME = 300000;
     private static final String WINDOW_MENU = "Window";
     private static final String PREFERENCES_MENU_ITEM = "Preferences";
     private static boolean fPrintedEnvironment = false;
@@ -98,14 +102,66 @@ public final class SWTBotUtils {
     private static final String TRACING_PERSPECTIVE_ID = TracingPerspectiveFactory.ID;
 
     /**
-     * Waits for all Eclipse jobs to finish
+     * Waits for all Eclipse jobs to finish. Times out after
+     * SWTBotUtils#MAX_JOBS_WAIT_TIME by default.
+     *
+     * @throws TimeoutException
+     *             once the waiting time passes the default maximum value
      */
     public static void waitForJobs() {
+        waitForJobs(MAX_JOBS_WAIT_TIME);
+    }
+
+    /**
+     * Waits for all Eclipse jobs to finish
+     *
+     * @param maxWait
+     *            the maximum time to wait, in milliseconds. Once the waiting
+     *            time passes the maximum value, a TimeoutException is thrown
+     * @throws TimeoutException
+     *             once the waiting time passes the maximum value
+     */
+    public static void waitForJobs(long maxWait) {
+        long waitStart = System.currentTimeMillis();
         while (!Job.getJobManager().isIdle()) {
+            if (System.currentTimeMillis() - waitStart > maxWait) {
+                printJobs();
+                throw new TimeoutException("Timed out waiting for jobs to finish.");
+            }
+
             delay(100);
         }
     }
 
+    private static void printJobs() {
+        Job[] jobs = Job.getJobManager().find(null);
+        for (Job job : jobs) {
+            System.err.println(job.toString() + " state: " + jobStateToString(job.getState()));
+            Thread thread = job.getThread();
+            if (thread != null) {
+                for (StackTraceElement stractTraceElement : thread.getStackTrace()) {
+                    System.err.println("  " + stractTraceElement);
+                }
+            }
+            System.err.println();
+        }
+    }
+
+    private static String jobStateToString(int jobState) {
+        switch (jobState) {
+        case Job.RUNNING:
+            return "RUNNING";
+        case Job.WAITING:
+            return "WAITING";
+        case Job.SLEEPING:
+            return "SLEEPING";
+        case Job.NONE:
+            return "NONE";
+        default:
+            return "UNKNOWN";
+        }
+    }
+
     /**
      * Sleeps current thread for a given time.
      *
@@ -493,7 +549,6 @@ public final class SWTBotUtils {
         SWTBotTreeItem currentItem = tracesNode;
         for (String segment : elementPath.segments()) {
             currentItem = getTraceProjectItem(projectExplorerBot, currentItem, segment);
-            currentItem.select();
             currentItem.doubleClick();
         }
 
@@ -587,6 +642,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
      *
@@ -646,6 +721,11 @@ public final class SWTBotUtils {
     public static SWTBotTreeItem selectProject(SWTWorkbenchBot bot, String projectName) {
         SWTBotView projectExplorerBot = bot.viewByTitle("Project Explorer");
         projectExplorerBot.show();
+        // FIXME: Bug 496519. Sometimes, the tree becomes disabled for a certain
+        // amount of time. This can happen during a long running operation
+        // (BusyIndicator.showWhile) which brings up the modal dialog "operation
+        // in progress" and this disables all shells
+        projectExplorerBot.bot().waitUntil(Conditions.widgetIsEnabled(projectExplorerBot.bot().tree()));
         SWTBotTreeItem treeItem = projectExplorerBot.bot().tree().getTreeItem(projectName);
         treeItem.select();
         return treeItem;
@@ -757,7 +837,29 @@ public final class SWTBotUtils {
 
         bot.waitUntil(ConditionHelpers.IsTreeNodeAvailable(nodeNames[0], tree));
         SWTBotTreeItem currentNode = tree.getTreeItem(nodeNames[0]);
-        for (int i = 1; i < nodeNames.length; i++) {
+        return getTreeItem(bot, currentNode, Arrays.copyOfRange(nodeNames, 1, nodeNames.length));
+    }
+
+    /**
+     * Get the tree item from a parent tree item at the specified location
+     *
+     * @param bot
+     *            the SWTBot
+     * @param treeItem
+     *            the treeItem to find the tree item under
+     * @param nodeNames
+     *            the path to the tree item, in the form of node names (from
+     *            parent to child).
+     * @return the tree item
+     */
+    public static SWTBotTreeItem getTreeItem(SWTBot bot, SWTBotTreeItem treeItem, String... nodeNames) {
+        if (nodeNames.length == 0) {
+            return treeItem;
+        }
+
+        SWTBotTreeItem currentNode = treeItem;
+        for (int i = 0; i < nodeNames.length; i++) {
+            bot.waitUntil(ConditionHelpers.treeItemHasChildren(treeItem));
             currentNode.expand();
 
             String nodeName = nodeNames[i];
@@ -777,6 +879,21 @@ public final class SWTBotUtils {
         return currentNode;
     }
 
+    /**
+     * Press the keyboard shortcut that goes to the top of a tree widget. The
+     * key combination can differ on different platforms.
+     *
+     * @param keyboard
+     *            the keyboard to use
+     */
+    public static void pressShortcutGoToTreeTop(Keyboard keyboard) {
+        if (SWTUtils.isMac()) {
+            keyboard.pressShortcut(Keystrokes.ALT, Keystrokes.UP);
+        } else {
+            keyboard.pressShortcut(Keystrokes.HOME);
+        }
+    }
+
     /**
      * Get the active events editor. Note that this will wait until such editor
      * is available.
@@ -786,26 +903,26 @@ public final class SWTBotUtils {
      * @return the active events editor
      */
     public static SWTBotEditor activeEventsEditor(final SWTWorkbenchBot workbenchBot) {
-        final SWTBotEditor editor[] = new SWTBotEditor[1];
-        workbenchBot.waitUntil(new DefaultCondition() {
-            @Override
-            public boolean test() throws Exception {
-                List<SWTBotEditor> editors = workbenchBot.editors(WidgetMatcherFactory.withPartId(TmfEventsEditor.ID));
-                for (SWTBotEditor e : editors) {
-                    if (e.isActive() && !e.getWidget().isDisposed()) {
-                        editor[0] = e;
-                        return true;
-                    }
-                }
-                return false;
-            }
+        ConditionHelpers.ActiveEventsEditor condition = new ConditionHelpers.ActiveEventsEditor(workbenchBot, null);
+        workbenchBot.waitUntil(condition);
+        return condition.getActiveEditor();
+    }
 
-            @Override
-            public String getFailureMessage() {
-                return "Active events editor not found";
-            }
-        });
-        return editor[0];
+    /**
+     * Get the active events editor. Note that this will wait until such editor
+     * is available.
+     *
+     * @param workbenchBot
+     *            a given workbench bot
+     * @param editorTitle
+     *            the desired editor title. If null, any active events editor
+     *            will be considered valid.
+     * @return the active events editor
+     */
+    public static SWTBotEditor activeEventsEditor(final SWTWorkbenchBot workbenchBot, String editorTitle) {
+        ConditionHelpers.ActiveEventsEditor condition = new ConditionHelpers.ActiveEventsEditor(workbenchBot, editorTitle);
+        workbenchBot.waitUntil(condition);
+        return condition.getActiveEditor();
     }
 
     /**
This page took 0.02786 seconds and 5 git commands to generate.