Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2267)

Unified Diff: editor/tools/plugins/com.google.dart.tools.ui_test/src-framework/com/google/dart/ui/test/util/EventSender.java

Issue 18548007: UI tests for 'Rename' and 'Extract Local Variable' refactorings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes for review comments Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: editor/tools/plugins/com.google.dart.tools.ui_test/src-framework/com/google/dart/ui/test/util/EventSender.java
diff --git a/editor/tools/plugins/com.google.dart.tools.ui.swtbot_test/src/com/google/dart/tools/ui/internal/refactoring/EventSender.java b/editor/tools/plugins/com.google.dart.tools.ui_test/src-framework/com/google/dart/ui/test/util/EventSender.java
similarity index 65%
rename from editor/tools/plugins/com.google.dart.tools.ui.swtbot_test/src/com/google/dart/tools/ui/internal/refactoring/EventSender.java
rename to editor/tools/plugins/com.google.dart.tools.ui_test/src-framework/com/google/dart/ui/test/util/EventSender.java
index 2fe0f82d28dd98b66988b0a4c35a9736576fb869..67ee648df191b160ed29d69329075d2974912bfd 100644
--- a/editor/tools/plugins/com.google.dart.tools.ui.swtbot_test/src/com/google/dart/tools/ui/internal/refactoring/EventSender.java
+++ b/editor/tools/plugins/com.google.dart.tools.ui_test/src-framework/com/google/dart/ui/test/util/EventSender.java
@@ -11,10 +11,11 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
-package com.google.dart.tools.ui.internal.refactoring;
+package com.google.dart.ui.test.util;
import com.google.dart.tools.internal.corext.refactoring.util.ExecutionUtils;
+import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
@@ -27,340 +28,323 @@ import org.eclipse.swt.widgets.Scrollable;
* Event emulator used for send different mouse events to given SWT control.
*/
public class EventSender {
- private final Control m_control;
- private int m_stateMask;
- private int m_dragButton;
- private int m_lastDragX;
- private int m_lastDragY;
-
- ////////////////////////////////////////////////////////////////////////////
- //
- // Constructor
- //
- ////////////////////////////////////////////////////////////////////////////
/**
- * Constructor of create event emulator for given <code>control</code>.
+ * Posts low-level {@link SWT.MouseDown} event.
*/
- public EventSender(Control control) {
- m_control = control;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- //
- // High-Level events emulate
- //
- ////////////////////////////////////////////////////////////////////////////
- public void setStateMask(int stateMask) {
- m_stateMask = stateMask;
+ public static void postMouseDown(int button) {
+ postMouseDownUp(SWT.MouseDown, button);
}
/**
- * Emulates CTRL key down.
+ * Posts low-level {@link SWT.MouseMove} event with absolute coordinates.
*/
- public void ctrlDown() {
- setStateMask(SWT.CTRL);
+ public static void postMouseMoveAbs(int x, int y) {
+ Event event = new Event();
+ event.type = SWT.MouseMove;
+ event.x = x;
+ event.y = y;
+ postEvent(event);
}
/**
- * Emulates CTRL key up.
+ * Posts low-level {@link SWT.MouseMove} event with absolute coordinates.
*/
- public void ctrlUp() {
- setStateMask(SWT.NONE);
+ public static void postMouseMoveAbs(Point p) {
+ postMouseMoveAbs(p.x, p.y);
}
/**
- * Emulate mouse enter to given location <code>(x, y)</code>.
+ * Posts low-level {@link SWT.MouseUp} event.
*/
- public void mouseEnter(int x, int y) {
- Event event = createEvent(x, y, 0);
- m_control.notifyListeners(SWT.MouseEnter, event);
+ public static void postMouseUp(int button) {
+ postMouseDownUp(SWT.MouseUp, button);
}
- /**
- * Emulate mouse click use given location and <code>button</code>.
- */
- public void click(Point location, int button) {
- click(location.x, location.y, button);
+ public static void sendCharacter(boolean shift, char character) {
+ if (shift) {
+ postKeyEvent(SWT.KeyDown, SWT.SHIFT, (char) 0);
+ }
+ postKeyEvent(SWT.KeyDown, 0, character);
+ postKeyEvent(SWT.KeyUp, 0, character);
+ if (shift) {
+ postKeyEvent(SWT.KeyUp, SWT.SHIFT, (char) 0);
+ }
+ }
+
+ public static void sendCharacter(char character) {
+ sendCharacter(Character.isUpperCase(character), character);
+ }
+
+ public static void sendKey(int key) {
+ postKeyEvent(SWT.KeyDown, key, (char) 0);
+ postKeyEvent(SWT.KeyUp, key, (char) 0);
}
/**
- * Emulate mouse click use given location <code>(x, y)</code> and <code>button</code>.
+ * Sends "key" event with modifiers.
*/
- public void click(int x, int y, int button) {
- Event event = createEvent(x, y, button);
- m_control.notifyListeners(SWT.MouseDown, event);
- updateStateMask(event, button);
- m_control.notifyListeners(SWT.MouseUp, event);
+ public static void sendKey(int modifiers, int key) {
+ postModifiers(SWT.KeyDown, modifiers);
+ try {
+ postKeyEvent(SWT.KeyDown, key, (char) 0);
+ postKeyEvent(SWT.KeyUp, key, (char) 0);
+ } finally {
+ postModifiers(SWT.KeyUp, modifiers);
+ }
+ }
+
+ public static void sendText(String text) {
+ char[] charArray = text.toCharArray();
+ for (int i = 0; i < charArray.length; i++) {
+ char c = charArray[i];
+ sendCharacter(c);
+ }
}
/**
- * Emulates mouse click in last location.
+ * Waits given number of milliseconds and runs events loop every 1 millisecond.<br>
+ * At least one events loop will be executed.
*/
- public void click(int button) {
- click(m_lastDragX, m_lastDragY, button);
+ public static void waitEventLoop(int time) {
+ try {
+ long start = System.currentTimeMillis();
+ do {
+ Thread.sleep(0);
+ while (Display.getCurrent().readAndDispatch()) {
+ // do nothing
+ }
+ } while (System.currentTimeMillis() - start < time);
+ } catch (Throwable e) {
+ throw ExecutionUtils.propagate(e);
+ }
}
/**
- * Emulates mouse click using button <code>1</code> in last location.
+ * Check that the current {@link Thread} is the UI thread and posts {@link Event} into the
+ * {@link Display}.
*/
- public void click() {
- click(1);
+ private static void postEvent(Event event) {
+ Display display = Display.getCurrent();
+ Assert.isNotNull(display, "Events can only be sent from the UI thread");
+ display.post(event);
}
/**
- * Emulate mouse double click use given location <code>(x, y)</code> and <code>button</code>.
+ * Posts low-level {@link SWT.KeyDown} or {@link SWT.KeyUp} event.
*/
- public void doubleClick(int x, int y, int button) {
- Event event = createEvent(x, y, button);
- m_control.notifyListeners(SWT.MouseDown, event);
- updateStateMask(event, button);
- m_control.notifyListeners(SWT.MouseUp, event);
- event.stateMask = m_stateMask;
- m_control.notifyListeners(SWT.MouseDown, event);
- m_control.notifyListeners(SWT.MouseDoubleClick, event);
- updateStateMask(event, button);
- m_control.notifyListeners(SWT.MouseUp, event);
+ private static void postKeyEvent(int type, int keyCode, char character) {
+ Event event = new Event();
+ event.type = type;
+ event.keyCode = keyCode;
+ event.character = character;
+ postEvent(event);
}
/**
- * Emulate mouse move to given location <code>(x, y)</code>.
+ * Posts modifiers up/down event.
*/
- public EventSender moveTo(int x, int y) {
- saveLastMouseLocation(x, y);
- // send event
- Event event = createEvent(x, y, 0);
- m_control.notifyListeners(SWT.MouseMove, event);
- // process "async" runnables
- waitEventLoop(0);
- return this;
+ private static void postModifiers(int event, int modifiers) {
+ if ((modifiers & SWT.SHIFT) != 0) {
+ postKeyEvent(event, SWT.SHIFT, (char) 0);
+ }
+ if ((modifiers & SWT.CTRL) != 0) {
+ postKeyEvent(event, SWT.CTRL, (char) 0);
+ }
+ if ((modifiers & SWT.ALT) != 0) {
+ postKeyEvent(event, SWT.CTRL, (char) 0);
+ }
}
/**
- * Start emulate operation drag use given location <code>(x, y)</code> and <code>button</code>.
+ * Posts low-level {@link SWT.MouseDown} or {@link SWT.MouseUp} event.
*/
- public void startDrag(int x, int y, int button) {
- saveLastMouseLocation(x, y);
- m_dragButton = button;
- //
- Event event = createEvent(x, y, button);
- m_control.notifyListeners(SWT.MouseDown, event);
+ private static void postMouseDownUp(int type, int button) {
+ Event event = new Event();
+ event.type = type;
+ event.button = button;
+ postEvent(event);
}
-// /**
-// * Emulate mouse drag to given location.
-// */
-// public void dragTo(org.eclipse.wb.draw2d.geometry.Point location) {
-// dragTo(location.x, location.y);
-// }
+ private static final void updateStateMask(Event event, int button) {
+ switch (button) {
+ case 1:
+ event.stateMask |= SWT.BUTTON1;
+ break;
+ case 2:
+ event.stateMask |= SWT.BUTTON2;
+ break;
+ case 3:
+ event.stateMask |= SWT.BUTTON3;
+ break;
+ case 4:
+ event.stateMask |= SWT.BUTTON4;
+ break;
+ case 5:
+ event.stateMask |= SWT.BUTTON5;
+ break;
+ }
+ }
+
+ private final Control control;
+
+ private int stateMask;
+ private int dragButton;
+ private int lastDragX;
+ private int lastDragY;
/**
- * Emulate mouse drag to given location <code>(x, y)</code>.
+ * Constructor to create event emulator for given <code>control</code>.
*/
- public void dragTo(int x, int y) {
- saveLastMouseLocation(x, y);
- // send event
- Event event = createEvent(x, y, m_dragButton);
- updateStateMask(event, m_dragButton);
- m_control.notifyListeners(SWT.MouseMove, event);
- // process "async" runnables
- waitEventLoop(0);
+ public EventSender(Control control) {
+ this.control = control;
}
/**
- * Ending emulate operation mouse drag.
+ * Emulates mouse click using button <code>1</code> in last location.
*/
- public void endDrag() {
- Event event = createEvent(m_lastDragX, m_lastDragY, m_dragButton);
- updateStateMask(event, m_dragButton);
- m_control.notifyListeners(SWT.MouseUp, event);
- m_dragButton = 0;
- m_lastDragX = 0;
- m_lastDragY = 0;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- //
- // High level keyboard
- //
- ////////////////////////////////////////////////////////////////////////////
- public void keyDown(int key) {
- keyDown(key, (char) key);
+ public void click() {
+ click(1);
}
- public void keyUp(int key) {
- keyUp(key, (char) key);
+ /**
+ * Emulates mouse click in last location.
+ */
+ public void click(int button) {
+ click(lastDragX, lastDragY, button);
}
- public void keyDown(int key, char c) {
- Event event = createKeyEvent(key, c);
- m_control.notifyListeners(SWT.KeyDown, event);
+ /**
+ * Emulate mouse click use given location <code>(x, y)</code> and <code>button</code>.
+ */
+ public void click(int x, int y, int button) {
+ Event event = createEvent(x, y, button);
+ control.notifyListeners(SWT.MouseDown, event);
+ updateStateMask(event, button);
+ control.notifyListeners(SWT.MouseUp, event);
}
- public void keyUp(int key, char c) {
- Event event = createKeyEvent(key, c);
- m_control.notifyListeners(SWT.KeyUp, event);
+ /**
+ * Emulate mouse click use given location and <code>button</code>.
+ */
+ public void click(Point location, int button) {
+ click(location.x, location.y, button);
}
public Event createKeyEvent(int key, char c) {
Event event = new Event();
- event.widget = m_control;
- event.stateMask = m_stateMask;
+ event.widget = control;
+ event.stateMask = stateMask;
event.keyCode = key;
event.character = c;
return event;
}
- ////////////////////////////////////////////////////////////////////////////
- //
- // UI utils
- //
- ////////////////////////////////////////////////////////////////////////////
/**
- * Waits given number of milliseconds and runs events loop every 1 millisecond.<br>
- * At least one events loop will be executed.
+ * Emulates CTRL key down.
*/
- public static void waitEventLoop(int time) {
- try {
- long start = System.currentTimeMillis();
- do {
- Thread.sleep(0);
- while (Display.getCurrent().readAndDispatch()) {
- // do nothing
- }
- } while (System.currentTimeMillis() - start < time);
- } catch (Throwable e) {
- throw ExecutionUtils.propagate(e);
- }
+ public void ctrlDown() {
+ setStateMask(SWT.CTRL);
}
- ////////////////////////////////////////////////////////////////////////////
- //
- // Scrolling
- //
- ////////////////////////////////////////////////////////////////////////////
/**
- * Scrolls vertical {@link ScrollBar} and sends {@link SWT#Selection} event.
+ * Emulates CTRL key up.
*/
- public void setVerticalBarSelection(int selection) {
- ScrollBar verticalBar = ((Scrollable) m_control).getVerticalBar();
- verticalBar.setSelection(selection);
- verticalBar.notifyListeners(SWT.Selection, new Event());
+ public void ctrlUp() {
+ setStateMask(SWT.NONE);
}
/**
- * Sends {@link SWT#MouseHover} event with given location.
+ * Emulate mouse double click use given location <code>(x, y)</code> and <code>button</code>.
*/
- public void mouseHover(Point location) {
- mouseEnter(location.x, location.y);
+ public void doubleClick(int x, int y, int button) {
+ Event event = createEvent(x, y, button);
+ control.notifyListeners(SWT.MouseDown, event);
+ updateStateMask(event, button);
+ control.notifyListeners(SWT.MouseUp, event);
+ event.stateMask = stateMask;
+ control.notifyListeners(SWT.MouseDown, event);
+ control.notifyListeners(SWT.MouseDoubleClick, event);
+ updateStateMask(event, button);
+ control.notifyListeners(SWT.MouseUp, event);
}
/**
- * Sends {@link SWT#MouseHover} event with given location.
+ * Emulate mouse drag to given location <code>(x, y)</code>.
*/
- public void mouseHover(int x, int y) {
- Event event = createEvent(x, y, 0);
- m_control.notifyListeners(SWT.MouseHover, event);
+ public void dragTo(int x, int y) {
+ saveLastMouseLocation(x, y);
+ // send event
+ Event event = createEvent(x, y, dragButton);
+ updateStateMask(event, dragButton);
+ control.notifyListeners(SWT.MouseMove, event);
+ // process "async" runnables
+ waitEventLoop(0);
}
- ////////////////////////////////////////////////////////////////////////////
- //
- // Keyboard
- //
- ////////////////////////////////////////////////////////////////////////////
- public static void sendText(String text) {
- char[] charArray = text.toCharArray();
- for (int i = 0; i < charArray.length; i++) {
- char c = charArray[i];
- sendCharacter(c);
- }
+ /**
+ * Ending emulate operation mouse drag.
+ */
+ public void endDrag() {
+ Event event = createEvent(lastDragX, lastDragY, dragButton);
+ updateStateMask(event, dragButton);
+ control.notifyListeners(SWT.MouseUp, event);
+ dragButton = 0;
+ lastDragX = 0;
+ lastDragY = 0;
}
- public static void sendCharacter(char character) {
- sendCharacter(Character.isUpperCase(character), character);
+ public void keyDown(int key) {
+ keyDown(key, (char) key);
}
- public static void sendKey(int key) {
- postKeyEvent(SWT.KeyDown, key, (char) 0);
- postKeyEvent(SWT.KeyUp, key, (char) 0);
+ public void keyDown(int key, char c) {
+ Event event = createKeyEvent(key, c);
+ control.notifyListeners(SWT.KeyDown, event);
}
- /**
- * Sends "key" event with modifiers.
- */
- public static void sendKey(int modifiers, int key) {
- postModifiers(SWT.KeyDown, modifiers);
- try {
- postKeyEvent(SWT.KeyDown, key, (char) 0);
- postKeyEvent(SWT.KeyUp, key, (char) 0);
- } finally {
- postModifiers(SWT.KeyUp, modifiers);
- }
+ public void keyUp(int key) {
+ keyUp(key, (char) key);
}
- public static void sendCharacter(boolean shift, char character) {
- if (shift) {
- postKeyEvent(SWT.KeyDown, SWT.SHIFT, (char) 0);
- }
- postKeyEvent(SWT.KeyDown, 0, character);
- postKeyEvent(SWT.KeyUp, 0, character);
- if (shift) {
- postKeyEvent(SWT.KeyUp, SWT.SHIFT, (char) 0);
- }
+ public void keyUp(int key, char c) {
+ Event event = createKeyEvent(key, c);
+ control.notifyListeners(SWT.KeyUp, event);
}
- ////////////////////////////////////////////////////////////////////////////
- //
- // Utils
- //
- ////////////////////////////////////////////////////////////////////////////
/**
- * Remembers this mouse location as last.
+ * Emulate mouse enter to given location <code>(x, y)</code>.
*/
- private void saveLastMouseLocation(int x, int y) {
- m_lastDragX = x;
- m_lastDragY = y;
+ public void mouseEnter(int x, int y) {
+ Event event = createEvent(x, y, 0);
+ control.notifyListeners(SWT.MouseEnter, event);
}
- private Event createEvent(int x, int y, int button) {
- Event event = new Event();
- event.widget = m_control;
- event.stateMask = m_stateMask;
- event.button = button;
- event.x = x;
- event.y = y;
- return event;
+ /**
+ * Sends {@link SWT#MouseHover} event with given location.
+ */
+ public void mouseHover(int x, int y) {
+ Event event = createEvent(x, y, 0);
+ control.notifyListeners(SWT.MouseHover, event);
}
- private static final void updateStateMask(Event event, int button) {
- switch (button) {
- case 1:
- event.stateMask |= SWT.BUTTON1;
- break;
- case 2:
- event.stateMask |= SWT.BUTTON2;
- break;
- case 3:
- event.stateMask |= SWT.BUTTON3;
- break;
- case 4:
- event.stateMask |= SWT.BUTTON4;
- break;
- case 5:
- event.stateMask |= SWT.BUTTON5;
- break;
- }
+ /**
+ * Sends {@link SWT#MouseHover} event with given location.
+ */
+ public void mouseHover(Point location) {
+ mouseEnter(location.x, location.y);
}
- ////////////////////////////////////////////////////////////////////////////
- //
- // Low-level events
- //
- ////////////////////////////////////////////////////////////////////////////
/**
- * Posts low-level {@link SWT.MouseMove} event with coordinates relative to control.
+ * Emulate mouse move to given location <code>(x, y)</code>.
*/
- public void postMouseMove(Point p) {
- postMouseMove(p.x, p.y);
+ public EventSender moveTo(int x, int y) {
+ saveLastMouseLocation(x, y);
+ // send event
+ Event event = createEvent(x, y, 0);
+ control.notifyListeners(SWT.MouseMove, event);
+ // process "async" runnables
+ waitEventLoop(0);
+ return this;
}
/**
@@ -368,111 +352,68 @@ public class EventSender {
*/
public void postMouseMove(int x, int y) {
Display display = Display.getCurrent();
- Point p = display.map(m_control, null, x, y);
+ Point p = display.map(control, null, x, y);
postMouseMoveAbs(p.x, p.y);
}
public void postMouseMove(int x, int y, int button) {
Display display = Display.getCurrent();
- Point p = display.map(m_control, null, x, y);
- // prepare event
- Event event;
- {
- event = new Event();
- event.type = SWT.MouseMove;
- event.x = p.x;
- event.y = p.y;
- event.button = button;
- }
+ Point p = display.map(control, null, x, y);
// post event
- Display.getCurrent().post(event);
- }
-
- /**
- * Posts low-level {@link SWT.MouseMove} event with absolute coordinates.
- */
- public static void postMouseMoveAbs(Point p) {
- postMouseMoveAbs(p.x, p.y);
+ Event event = new Event();
+ event.type = SWT.MouseMove;
+ event.x = p.x;
+ event.y = p.y;
+ event.button = button;
+ postEvent(event);
}
/**
- * Posts low-level {@link SWT.MouseMove} event with absolute coordinates.
+ * Posts low-level {@link SWT.MouseMove} event with coordinates relative to control.
*/
- public static void postMouseMoveAbs(int x, int y) {
- // prepare event
- Event event;
- {
- event = new Event();
- event.type = SWT.MouseMove;
- event.x = x;
- event.y = y;
- }
- // post event
- Display.getCurrent().post(event);
+ public void postMouseMove(Point p) {
+ postMouseMove(p.x, p.y);
}
- /**
- * Posts low-level {@link SWT.MouseDown} event.
- */
- public static void postMouseDown(int button) {
- postMouseDownUp(SWT.MouseDown, button);
+ public void setStateMask(int stateMask) {
+ this.stateMask = stateMask;
}
/**
- * Posts low-level {@link SWT.MouseUp} event.
+ * Scrolls vertical {@link ScrollBar} and sends {@link SWT#Selection} event.
*/
- public static void postMouseUp(int button) {
- postMouseDownUp(SWT.MouseUp, button);
+ public void setVerticalBarSelection(int selection) {
+ ScrollBar verticalBar = ((Scrollable) control).getVerticalBar();
+ verticalBar.setSelection(selection);
+ verticalBar.notifyListeners(SWT.Selection, new Event());
}
/**
- * Posts low-level {@link SWT.MouseDown} or {@link SWT.MouseUp} event.
+ * Start emulate operation drag use given location <code>(x, y)</code> and <code>button</code>.
*/
- private static void postMouseDownUp(int type, int button) {
- // prepare event
- final Event event;
- {
- event = new Event();
- event.type = type;
- event.button = button;
- }
- // post event
- Display.getCurrent().post(event);
+ public void startDrag(int x, int y, int button) {
+ saveLastMouseLocation(x, y);
+ dragButton = button;
+ //
+ Event event = createEvent(x, y, button);
+ control.notifyListeners(SWT.MouseDown, event);
}
- ////////////////////////////////////////////////////////////////////////////
- //
- // Low-level keyboard events
- //
- ////////////////////////////////////////////////////////////////////////////
- /**
- * Posts low-level {@link SWT.KeyDown} or {@link SWT.KeyUp} event.
- */
- private static void postKeyEvent(int type, int keyCode, char character) {
- // prepare event
- final Event event;
- {
- event = new Event();
- event.type = type;
- event.keyCode = keyCode;
- event.character = character;
- }
- // post event
- Display.getCurrent().post(event);
+ private Event createEvent(int x, int y, int button) {
+ Event event = new Event();
+ event.widget = control;
+ event.stateMask = stateMask;
+ event.button = button;
+ event.x = x;
+ event.y = y;
+ return event;
}
/**
- * Posts modifiers up/down event.
+ * Remembers this mouse location as last.
*/
- private static void postModifiers(int event, int modifiers) {
- if ((modifiers & SWT.SHIFT) != 0) {
- postKeyEvent(event, SWT.SHIFT, (char) 0);
- }
- if ((modifiers & SWT.CTRL) != 0) {
- postKeyEvent(event, SWT.CTRL, (char) 0);
- }
- if ((modifiers & SWT.ALT) != 0) {
- postKeyEvent(event, SWT.CTRL, (char) 0);
- }
+ private void saveLastMouseLocation(int x, int y) {
+ lastDragX = x;
+ lastDragY = y;
}
}

Powered by Google App Engine
This is Rietveld 408576698