tmf: Try to make the shell fully visible in SWTBot at start
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotUtils.java
index b9c5bf2fc0e9dbf04b5eddc52d8ae166abb96fc9..b8bbe60a511dbdb8a62c5a4b3cacfa748e603ea3 100644 (file)
@@ -31,6 +31,7 @@ import org.eclipse.jface.bindings.keys.ParseException;
 import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Table;
 import org.eclipse.swt.widgets.TableItem;
 import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
@@ -42,6 +43,7 @@ import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
 import org.eclipse.swtbot.swt.finder.results.Result;
 import org.eclipse.swtbot.swt.finder.results.VoidResult;
 import org.eclipse.swtbot.swt.finder.waits.Conditions;
+import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox;
 import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu;
@@ -207,6 +209,20 @@ public final class SWTBotUtils {
         }
     }
 
+    /**
+     * Close a view with an id
+     *
+     * @param viewId
+     *            the view id, like "org.eclipse.linuxtools.tmf.ui.views.histogram"
+     * @param bot
+     *            the workbench bot
+     */
+    public static void closeViewById(String viewId, SWTWorkbenchBot bot) {
+        final SWTBotView view = bot.viewById(viewId);
+        view.close();
+        bot.waitUntil(ConditionHelpers.ViewIsClosed(view));
+    }
+
     /**
      * Switch to the tracing perspective
      */
@@ -234,15 +250,52 @@ public final class SWTBotUtils {
         });
     }
 
+    /**
+     * Initialize the environment for SWTBot
+     */
+    public static void initialize() {
+        failIfUIThread();
+
+        SWTWorkbenchBot bot = new SWTWorkbenchBot();
+        UIThreadRunnable.syncExec(() -> {
+            Shell shell = bot.activeShell().widget;
+
+            // Only adjust shell if it appears to be the top-most
+            if (shell.getParent() == null) {
+                makeShellFullyVisible(shell);
+            }
+        });
+    }
+
     /**
      * If the test is running in the UI thread then fail
      */
-    public static void failIfUIThread() {
+    private static void failIfUIThread() {
         if (Display.getCurrent() != null && Display.getCurrent().getThread() == Thread.currentThread()) {
             fail("SWTBot test needs to run in a non-UI thread. Make sure that \"Run in UI thread\" is unchecked in your launch configuration or"
                     + " that useUIThread is set to false in the pom.xml");
         }
+    }
 
+    /**
+     * Try to make the shell fully visible in the display. If the shell cannot
+     * fit the display, it will be positioned so that top-left corner is at
+     * <code>(0, 0)</code> in display-relative coordinates.
+     *
+     * @param shell
+     *            the shell to make fully visible
+     */
+    private static void makeShellFullyVisible(Shell shell) {
+        Rectangle displayBounds = shell.getDisplay().getBounds();
+        Point absCoord = shell.toDisplay(0, 0);
+        Point shellSize = shell.getSize();
+
+        Point newLocation = new Point(absCoord.x, absCoord.y);
+        newLocation.x = Math.max(0, Math.min(absCoord.x, displayBounds.width - shellSize.x));
+        newLocation.y = Math.max(0, Math.min(absCoord.y, displayBounds.height - shellSize.y));
+        if (!newLocation.equals(absCoord)) {
+            shell.setLocation(newLocation);
+        }
     }
 
     /**
@@ -507,4 +560,35 @@ public final class SWTBotUtils {
 
         return currentNode;
     }
+
+    /**
+     * Get the active events editor. Note that this will wait until such editor
+     * is available.
+     *
+     * @param workbenchBot
+     *            a given workbench bot
+     * @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;
+            }
+
+            @Override
+            public String getFailureMessage() {
+                return "Active events editor not found";
+            }
+        });
+        return editor[0];
+    }
 }
This page took 0.027048 seconds and 5 git commands to generate.