Bug 1146349 - Update browser-chrome mochitests to deal with async native event synthesization. r=Gijs
authorKartikaya Gupta <kgupta@mozilla.com>
Tue, 14 Apr 2015 11:36:36 -0400
changeset 257929 024cf684488b67fd129f15e5a82a0e51304735be
parent 257928 45168f9387c7ce77027a199c3bfb773f6984e017
child 257930 371a7026a1dfe35089c8f91014b2eab3a20a18c3
push id8007
push userraliiev@mozilla.com
push dateMon, 11 May 2015 19:23:16 +0000
treeherdermozilla-aurora@e2ce1aac996e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1146349
milestone40.0a1
Bug 1146349 - Update browser-chrome mochitests to deal with async native event synthesization. r=Gijs
browser/base/content/test/newtab/head.js
--- a/browser/base/content/test/newtab/head.js
+++ b/browser/base/content/test/newtab/head.js
@@ -509,31 +509,29 @@ function startAndCompleteDragOperation(a
     synthesizeNativeMouseLDown(aSource);
     synthesizeNativeMouseDrag(aDest);
     // In some tests, aSource and aDest are at the same position, so to ensure
     // a drag session is created (instead of it just turning into a click) we
     // move the mouse 10 pixels away and then back.
     synthesizeNativeMouseDrag(aDest, 10);
     synthesizeNativeMouseDrag(aDest);
     // Finally, release the drag and have it run the callback when done.
-    synthesizeNativeMouseLUp(aDest);
-    aCallback();
+    synthesizeNativeMouseLUp(aDest).then(aCallback, Cu.reportError);
   } else if (isWindows) {
     // on Windows once the drag is initiated, Windows doesn't spin our
     // message loop at all, so with async event synthesization the async
     // messages never get processed while a drag is in progress. So if
     // we did a mousedown followed by a mousemove, we would never be able
     // to successfully dispatch the mouseup. Instead, we just skip the move
     // entirely, so and just generate the up at the destination. This way
     // Windows does the drag and also terminates it right away. Note that
     // this only works for tests where aSource and aDest are sufficiently
     // far to trigger a drag, otherwise it may just end up doing a click.
     synthesizeNativeMouseLDown(aSource);
-    synthesizeNativeMouseLUp(aDest);
-    aCallback();
+    synthesizeNativeMouseLUp(aDest).then(aCallback, Cu.reportError);
   } else if (isLinux) {
     // Start by pressing the left mouse button.
     synthesizeNativeMouseLDown(aSource);
 
     // Move the mouse in 5px steps until the drag operation starts.
     // Note that we need to do this with pauses in between otherwise the
     // synthesized events get coalesced somewhere in the guts of GTK. In order
     // to successfully initiate a drag session in the case where aSource and
@@ -559,18 +557,17 @@ function startAndCompleteDragOperation(a
     // the mousemove synthesization is "more async" than the mouseup
     // synthesization - they use different gdk APIs. If we don't wait, the
     // up could get processed before the moves, dropping the item in the
     // wrong position.
     aDest.addEventListener("dragenter", function onDragEnter() {
       aDest.removeEventListener("dragenter", onDragEnter);
 
       // Finish the drop operation.
-      synthesizeNativeMouseLUp(aDest, null);
-      aCallback();
+      synthesizeNativeMouseLUp(aDest).then(aCallback, Cu.reportError);
     });
   } else {
     throw "Unsupported platform";
   }
 }
 
 /**
  * Helper function that creates a temporary iframe in the about:newtab
@@ -616,17 +613,17 @@ function synthesizeNativeMouseLDown(aEle
 }
 
 /**
  * Fires a synthetic 'mouseup' event on the current about:newtab page.
  * @param aElement The element used to determine the cursor position.
  */
 function synthesizeNativeMouseLUp(aElement) {
   let msg = isWindows ? 4 : (isMac ? 2 : 7);
-  synthesizeNativeMouseEvent(aElement, msg);
+  return synthesizeNativeMouseEvent(aElement, msg);
 }
 
 /**
  * Fires a synthetic mouse drag event on the current about:newtab page.
  * @param aElement The element used to determine the cursor position.
  * @param aOffsetX The left offset that is added to the position.
  */
 function synthesizeNativeMouseDrag(aElement, aOffsetX) {
@@ -645,26 +642,35 @@ function synthesizeNativeMouseMove(aElem
 
 /**
  * Fires a synthetic mouse event on the current about:newtab page.
  * @param aElement The element used to determine the cursor position.
  * @param aOffsetX The left offset that is added to the position (optional).
  * @param aOffsetY The top offset that is added to the position (optional).
  */
 function synthesizeNativeMouseEvent(aElement, aMsg, aOffsetX = 0, aOffsetY = 0) {
-  let rect = aElement.getBoundingClientRect();
-  let win = aElement.ownerDocument.defaultView;
-  let x = aOffsetX + win.mozInnerScreenX + rect.left + rect.width / 2;
-  let y = aOffsetY + win.mozInnerScreenY + rect.top + rect.height / 2;
+  return new Promise((resolve, reject) => {
+    let rect = aElement.getBoundingClientRect();
+    let win = aElement.ownerDocument.defaultView;
+    let x = aOffsetX + win.mozInnerScreenX + rect.left + rect.width / 2;
+    let y = aOffsetY + win.mozInnerScreenY + rect.top + rect.height / 2;
+
+    let utils = win.QueryInterface(Ci.nsIInterfaceRequestor)
+                   .getInterface(Ci.nsIDOMWindowUtils);
 
-  let utils = win.QueryInterface(Ci.nsIInterfaceRequestor)
-                 .getInterface(Ci.nsIDOMWindowUtils);
-
-  let scale = utils.screenPixelsPerCSSPixel;
-  utils.sendNativeMouseEvent(x * scale, y * scale, aMsg, 0, null);
+    let scale = utils.screenPixelsPerCSSPixel;
+    let observer = {
+      observe: function(aSubject, aTopic, aData) {
+        if (aTopic == "mouseevent") {
+          resolve();
+        }
+      }
+    };
+    utils.sendNativeMouseEvent(x * scale, y * scale, aMsg, 0, null, observer);
+  });
 }
 
 /**
  * Sends a custom drag event to a given DOM element.
  * @param aEventType The drag event's type.
  * @param aTarget The DOM element that the event is dispatched to.
  * @param aData The event's drag data (optional).
  */