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 7386e341f3ecca0004bd6b67d920fe1910bfe26a..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;
@@ -249,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);
+        }
     }
 
     /**
This page took 0.025823 seconds and 5 git commands to generate.