Bug 1144816 - focus the content window before popping up the context menu, rs=gijs
authorAlex Verstak <averstak@google.com>
Fri, 20 Mar 2015 02:07:00 -0700
changeset 263452 62b25fdb0b790d4631a1c434463340590befcf52
parent 263451 9d219be15f7dbde7ed3ebc963cefb1a51d3c0150
child 263453 4e033cac2931ca8425706701424afccbe09e4aad
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgijs
bugs1144816
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1144816 - focus the content window before popping up the context menu, rs=gijs
browser/base/content/test/referrer/head.js
--- a/browser/base/content/test/referrer/head.js
+++ b/browser/base/content/test/referrer/head.js
@@ -94,36 +94,55 @@ function getReferrerTestDescription(aTes
  * @param aOptions The options for synthesizeMouseAtCenter.
  */
 function clickTheLink(aWindow, aLinkId, aOptions) {
   ContentTask.spawn(aWindow.gBrowser.selectedBrowser,
                     {id: aLinkId, options: aOptions},
                     function(data) {
     let element = content.document.getElementById(data.id);
     let options = data.options;
-    element.focus();
 
     // EventUtils.synthesizeMouseAtCenter(element, options, content);
     // Alas, EventUtils doesn't work in the content task environment.
-    var domWindowUtils =
-        content.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
-        .getInterface(Components.interfaces.nsIDOMWindowUtils);
-    var rect = element.getBoundingClientRect();
-    var left = rect.left + rect.width / 2;
-    var top = rect.top + rect.height / 2;
-    var button = options.button || 0;
-    function sendMouseEvent(type) {
-      domWindowUtils.sendMouseEvent(type, left, top, button,
-                                    1, 0, false, 0, 0, true);
+    function doClick() {
+      var domWindowUtils =
+          content.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
+          .getInterface(Components.interfaces.nsIDOMWindowUtils);
+      var rect = element.getBoundingClientRect();
+      var left = rect.left + rect.width / 2;
+      var top = rect.top + rect.height / 2;
+      var button = options.button || 0;
+      function sendMouseEvent(type) {
+        domWindowUtils.sendMouseEvent(type, left, top, button,
+                                      1, 0, false, 0, 0, true);
+      }
+      if ("type" in options) {
+        sendMouseEvent(options.type);  // e.g., "contextmenu"
+      } else {
+        sendMouseEvent("mousedown");
+        sendMouseEvent("mouseup");
+      }
     }
-    if ("type" in options) {
-      sendMouseEvent(options.type);  // e.g., "contextmenu"
+
+    // waitForFocus(doClick, content);
+    let focusManager = Components.classes["@mozilla.org/focus-manager;1"].
+                       getService(Components.interfaces.nsIFocusManager);
+    let desiredWindow = {};
+    focusManager.getFocusedElementForWindow(content, true, desiredWindow);
+    desiredWindow = desiredWindow.value;
+    if (desiredWindow == focusManager.focusedWindow) {
+      // The window is already focused - click away.
+      doClick();
     } else {
-      sendMouseEvent("mousedown");
-      sendMouseEvent("mouseup");
+      // Focus the window first, then click.
+      desiredWindow.addEventListener("focus", function onFocus() {
+        desiredWindow.removeEventListener("focus", onFocus, true);
+        setTimeout(doClick, 0);
+      }, true);
+      desiredWindow.focus();
     }
   });
 }
 
 /**
  * Extracts the referrer result from the target window.
  * @param aWindow The window where the referrer target has loaded.
  * @return {Promise}