swtbot: Add SWTBotTimeGraph and update SWTBotSash
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.swtbot.tests / shared / org / eclipse / tracecompass / tmf / ui / swtbot / tests / shared / SWTBotSash.java
index 7d69fecc2b7d31cece859d5fee586baa18f79573..f4428a2c8422b0adc5cb825254e12e1f5bee7b74 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2015 Ericsson
+ * Copyright (c) 2015, 2016 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
 
 package org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared;
 
-import java.awt.AWTException;
-import java.awt.Robot;
-import java.awt.event.InputEvent;
-
+import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Sash;
 import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;
-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.widgets.AbstractSWTBotControl;
-import org.hamcrest.SelfDescribing;
 
 /**
- * SWT Bot sash control
+ * SWTBot class representing a Sash
  */
 public class SWTBotSash extends AbstractSWTBotControl<Sash> {
 
@@ -35,99 +31,63 @@ public class SWTBotSash extends AbstractSWTBotControl<Sash> {
      *
      * @param w
      *            the sash
-     * @param description
-     *            the description
      * @throws WidgetNotFoundException
      *             if there is no widget
      */
-    public SWTBotSash(Sash w, SelfDescribing description) throws WidgetNotFoundException {
-        super(w, description);
+    public SWTBotSash(Sash w) throws WidgetNotFoundException {
+        super(w);
     }
 
     /**
-     * Get the central point of the sash
+     * Get the bounds of the sash
      *
-     * @return the center point, good for dragging
+     * @return the bounds relative to the parent
      */
-    public Point getPoint() {
-        return UIThreadRunnable.syncExec(new Result<Point>() {
-
+    public Rectangle getBounds() {
+        return syncExec(new Result<Rectangle>() {
             @Override
-            public Point run() {
-                return widget.toDisplay(0, widget.getSize().y / 2);
+            public Rectangle run() {
+                return widget.getBounds();
             }
-
         });
     }
 
     /**
-     * Simulate a drag
+     * Drag the sash from its middle point to the destination point
      *
      * @param dst
-     *            to this destination
+     *            the destination point relative to the parent
      */
     public void drag(final Point dst) {
-        final Point src = getPoint();
-        /*
-         * example of a move
-         *
-         * dn : MouseEvent{Sash {} time=262463957 data=null button=1
-         * stateMask=0x0 x=5 y=59 count=1}
-         *
-         * move : MouseEvent{Sash {} time=262464038 data=null button=0
-         * stateMask=0x80000 x=131 y=103 count=0}
-         *
-         * move : MouseEvent{Sash {} time=262464171 data=null button=0
-         * stateMask=0x80000 x=90 y=116 count=0}
-         *
-         * up : MouseEvent{Sash {} time=262464796 data=null button=1
-         * stateMask=0x80000 x=5 y=116 count=1}
-         */
-        try {
-            final Robot awtRobot = new Robot();
-
-            // move a maximum of 10 points / event
-            final int magDist = (src.x - dst.x) * (src.x - dst.x) + (src.y - dst.y) * (src.y - dst.y);
-
-            final int steps = Math.max(1, (int) Math.sqrt(magDist / 100.0));
-
-            final int stepX = (dst.x - src.x) / steps;
-            final int stepY = (dst.y - src.y) / steps;
-            syncExec(new VoidResult() {
-                @Override
-                public void run() {
-                    awtRobot.mouseMove(src.x, src.y);
-                    SWTBotUtils.delay(15);
-                    awtRobot.mousePress(InputEvent.BUTTON1_MASK);
+        Rectangle bounds = getBounds();
+        int x = bounds.width / 2;
+        int y = bounds.height / 2;
+        notify(SWT.MouseEnter);
+        notify(SWT.Activate);
+        notify(SWT.Selection, createSelectionEvent(bounds.x + x, bounds.y + y, SWT.NONE));
+        notify(SWT.MouseDown, createMouseEvent(x, y, 1, SWT.NONE, 1));
+        notify(SWT.DragDetect, createMouseEvent(x, y, 0, SWT.NONE, 0));
+        notify(SWT.Move);
+        notify(SWT.Selection, createSelectionEvent(dst.x, dst.y, SWT.NONE));
+        notify(SWT.MouseMove, createMouseEvent(x, y, 0, SWT.BUTTON1, 0));
+        notify(SWT.Selection, createSelectionEvent(dst.x, dst.y, SWT.BUTTON1));
+        notify(SWT.MouseUp, createMouseEvent(x, y, 1, SWT.NONE, 1));
+        notify(SWT.MouseExit);
+    }
 
-                }
-            });
-            for (int i = 0; i < steps; i++) {
-                final int index = i;
-                asyncExec(new VoidResult() {
-                    @Override
-                    public void run() {
-                        int x = src.x + index * stepX;
-                        int y = src.y + index * stepY;
-                        awtRobot.mouseMove(x, y);
-                    }
-                });
-                // drag delay
-                SWTBotUtils.delay(10);
+    private Event createSelectionEvent(int x, int y, int stateMask) {
+        return syncExec(new Result<Event>() {
+            @Override
+            public Event run() {
+                boolean vertical = (widget.getStyle() | SWT.VERTICAL) != 0;
+                Point size = widget.getSize();
+                Event event = createSelectionEvent(stateMask);
+                event.x = vertical ? x : 0;
+                event.y = vertical ? 0 : y;
+                event.width = size.x;
+                event.height = size.y;
+                return event;
             }
-            // drop delay
-            SWTBotUtils.delay(100);
-            syncExec(new VoidResult() {
-                @Override
-                public void run() {
-                    awtRobot.mouseRelease(InputEvent.BUTTON1_MASK);
-                }
-            });
-
-        } catch (final AWTException e) {
-            // log.error(e.getMessage(), e);
-            throw new RuntimeException(e);
-        }
+        });
     }
-
 }
This page took 0.025648 seconds and 5 git commands to generate.