Bug 1521707 - Move most of urlbar's head-common.js functions to a UrlbarTestUtils.jsm module. r=mak
authorMark Banner <standard8@mozilla.com>
Wed, 23 Jan 2019 14:04:55 +0000
changeset 515102 248f94519729a71c76d3b1bd51c53bcb5f6942ff
parent 515101 51025d682291eeae4040de994f1b76573e1d1596
child 515103 21cb7caef246d374a8f73a14806d9fd328adbf8a
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1521707
milestone66.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 1521707 - Move most of urlbar's head-common.js functions to a UrlbarTestUtils.jsm module. r=mak For now, the existing functions remain in head-common.js and are wrappers around the UrlbarTestUtils to avoid churn whilst we work out the actual APIs we require. Differential Revision: https://phabricator.services.mozilla.com/D17260
browser/components/urlbar/moz.build
browser/components/urlbar/tests/UrlbarTestUtils.jsm
browser/components/urlbar/tests/browser/head-common.js
--- a/browser/components/urlbar/moz.build
+++ b/browser/components/urlbar/moz.build
@@ -15,13 +15,16 @@ EXTRA_JS_MODULES += [
     'UrlbarProvidersManager.jsm',
     'UrlbarProviderUnifiedComplete.jsm',
     'UrlbarTokenizer.jsm',
     'UrlbarUtils.jsm',
     'UrlbarValueFormatter.jsm',
     'UrlbarView.jsm',
 ]
 
+TESTING_JS_MODULES += [
+    'tests/UrlbarTestUtils.jsm',
+]
 BROWSER_CHROME_MANIFESTS += [
     'tests/browser/browser.ini',
     'tests/legacy/browser.ini',
 ]
 XPCSHELL_TESTS_MANIFESTS += ['tests/unit/xpcshell.ini']
new file mode 100644
--- /dev/null
+++ b/browser/components/urlbar/tests/UrlbarTestUtils.jsm
@@ -0,0 +1,93 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const EXPORTED_SYMBOLS = ["UrlbarTestUtils"];
+
+ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetters(this, {
+  BrowserTestUtils: "resource://testing-common/BrowserTestUtils.jsm",
+  TestUtils: "resource://testing-common/TestUtils.jsm",
+});
+
+var UrlbarTestUtils = {
+  promiseSearchComplete(win, dontAnimate = false) {
+    return BrowserTestUtils.waitForPopupEvent(win.gURLBar.popup, "shown").then(() => {
+      function searchIsComplete() {
+        let isComplete = win.gURLBar.controller.searchStatus >=
+                         Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH;
+        if (isComplete) {
+          dump(`Restore popup dontAnimate value to ${dontAnimate}\n`);
+          win.gURLBar.popup.setAttribute("dontanimate", dontAnimate);
+        }
+        return isComplete;
+      }
+
+      // Wait until there are at least two matches.
+      return BrowserTestUtils.waitForCondition(searchIsComplete, "waiting urlbar search to complete");
+    });
+  },
+
+  promiseAutocompleteResultPopup(inputText, win, waitForFocus, fireInputEvent = false) {
+    let dontAnimate = !!win.gURLBar.popup.getAttribute("dontanimate");
+    waitForFocus(() => {
+      dump(`Disable popup animation. Change dontAnimate value from ${dontAnimate} to true.\n`);
+      win.gURLBar.popup.setAttribute("dontanimate", "true");
+      win.gURLBar.focus();
+      win.gURLBar.value = inputText;
+      if (fireInputEvent) {
+        // This is necessary to get the urlbar to set gBrowser.userTypedValue.
+        let event = win.document.createEvent("Events");
+        event.initEvent("input", true, true);
+        win.gURLBar.dispatchEvent(event);
+      }
+      win.gURLBar.controller.startSearch(inputText);
+    }, win);
+
+    return this.promiseSearchComplete(win, dontAnimate);
+  },
+
+  async waitForAutocompleteResultAt(win, index) {
+    let searchString = win.gURLBar.controller.searchString;
+    await BrowserTestUtils.waitForCondition(
+      () => win.gURLBar.popup.richlistbox.itemChildren.length > index &&
+            win.gURLBar.popup.richlistbox.itemChildren[index].getAttribute("ac-text") == searchString.trim(),
+      `Waiting for the autocomplete result for "${searchString}" at [${index}] to appear`);
+    // Ensure the addition is complete, for proper mouse events on the entries.
+    await new Promise(resolve => win.requestIdleCallback(resolve, {timeout: 1000}));
+    return win.gURLBar.popup.richlistbox.itemChildren[index];
+  },
+
+  promiseSuggestionsPresent(win, msg = "") {
+    return TestUtils.waitForCondition(this.suggestionsPresent.bind(this, win),
+                                      msg || "Waiting for suggestions");
+  },
+
+  suggestionsPresent(win) {
+    let controller = win.gURLBar.popup.input.controller;
+    let matchCount = controller.matchCount;
+    for (let i = 0; i < matchCount; i++) {
+      let url = controller.getValueAt(i);
+      let mozActionMatch = url.match(/^moz-action:([^,]+),(.*)$/);
+      if (mozActionMatch) {
+        let [, type, paramStr] = mozActionMatch;
+        let params = JSON.parse(paramStr);
+        if (type == "searchengine" && "searchSuggestion" in params) {
+          return true;
+        }
+      }
+    }
+    return false;
+  },
+
+  promiseSpeculativeConnection(httpserver) {
+    return BrowserTestUtils.waitForCondition(() => {
+      if (httpserver) {
+        return httpserver.connectionNumber == 1;
+      }
+      return false;
+    }, "Waiting for connection setup");
+  },
+};
--- a/browser/components/urlbar/tests/browser/head-common.js
+++ b/browser/components/urlbar/tests/browser/head-common.js
@@ -3,16 +3,17 @@
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetters(this, {
   HttpServer: "resource://testing-common/httpd.js",
   PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
   PlacesTestUtils: "resource://testing-common/PlacesTestUtils.jsm",
   Preferences: "resource://gre/modules/Preferences.jsm",
   SearchTestUtils: "resource://testing-common/SearchTestUtils.jsm",
+  UrlbarTestUtils: "resource://testing-common/UrlbarTestUtils.jsm",
   UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.jsm",
 });
 
 SearchTestUtils.init(Assert, registerCleanupFunction);
 
 function is_element_visible(element, msg) {
   isnot(element, null, "Element should not be null, when checking visibility");
   ok(BrowserTestUtils.is_visible(element), msg || "Element should be visible");
@@ -40,51 +41,24 @@ function promisePopupShown(popup) {
   return BrowserTestUtils.waitForPopupEvent(popup, "shown");
 }
 
 function promisePopupHidden(popup) {
   return BrowserTestUtils.waitForPopupEvent(popup, "hidden");
 }
 
 function promiseSearchComplete(win = window, dontAnimate = false) {
-  return promisePopupShown(win.gURLBar.popup).then(() => {
-    function searchIsComplete() {
-      let isComplete = win.gURLBar.controller.searchStatus >=
-                       Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH;
-      if (isComplete) {
-        info(`Restore popup dontAnimate value to ${dontAnimate}`);
-        win.gURLBar.popup.setAttribute("dontanimate", dontAnimate);
-      }
-      return isComplete;
-    }
-
-    // Wait until there are at least two matches.
-    return BrowserTestUtils.waitForCondition(searchIsComplete, "waiting urlbar search to complete");
-  });
+  return UrlbarTestUtils.promiseSearchComplete(win, dontAnimate);
 }
 
 function promiseAutocompleteResultPopup(inputText,
                                         win = window,
                                         fireInputEvent = false) {
-  let dontAnimate = !!win.gURLBar.popup.getAttribute("dontanimate");
-  waitForFocus(() => {
-    info(`Disable popup animation. Change dontAnimate value from ${dontAnimate} to true.`);
-    win.gURLBar.popup.setAttribute("dontanimate", "true");
-    win.gURLBar.focus();
-    win.gURLBar.value = inputText;
-    if (fireInputEvent) {
-      // This is necessary to get the urlbar to set gBrowser.userTypedValue.
-      let event = document.createEvent("Events");
-      event.initEvent("input", true, true);
-      win.gURLBar.dispatchEvent(event);
-    }
-    win.gURLBar.controller.startSearch(inputText);
-  }, win);
-
-  return promiseSearchComplete(win, dontAnimate);
+  return UrlbarTestUtils.promiseAutocompleteResultPopup(inputText,
+    win, waitForFocus, fireInputEvent);
 }
 
 function promisePageActionPanelOpen() {
   let dwu = window.windowUtils;
   return BrowserTestUtils.waitForCondition(() => {
     // Wait for the main page action button to become visible.  It's hidden for
     // some URIs, so depending on when this is called, it may not yet be quite
     // visible.  It's up to the caller to make sure it will be visible.
@@ -196,48 +170,22 @@ function promiseNodeVisible(node) {
       info(`promiseNodeVisible OK, node.id=${node.id} node.localeName=${node.localName}\n`);
       return true;
     }
     return false;
   });
 }
 
 function promiseSpeculativeConnection(httpserver) {
-  return BrowserTestUtils.waitForCondition(() => {
-    if (httpserver) {
-      return httpserver.connectionNumber == 1;
-    }
-    return false;
-  }, "Waiting for connection setup");
+  return UrlbarTestUtils.promiseSpeculativeConnection(httpserver);
 }
 
 async function waitForAutocompleteResultAt(index) {
-  let searchString = gURLBar.controller.searchString;
-  await BrowserTestUtils.waitForCondition(
-    () => gURLBar.popup.richlistbox.itemChildren.length > index &&
-          gURLBar.popup.richlistbox.itemChildren[index].getAttribute("ac-text") == searchString.trim(),
-    `Waiting for the autocomplete result for "${searchString}" at [${index}] to appear`);
-  // Ensure the addition is complete, for proper mouse events on the entries.
-  await new Promise(resolve => window.requestIdleCallback(resolve, {timeout: 1000}));
-  return gURLBar.popup.richlistbox.itemChildren[index];
+  return UrlbarTestUtils.waitForAutocompleteResultAt(window, index);
 }
 
 function promiseSuggestionsPresent(msg = "") {
-  return TestUtils.waitForCondition(suggestionsPresent,
-                                    msg || "Waiting for suggestions");
+  return UrlbarTestUtils.promiseSuggestionsPresent(window, msg);
 }
 
 function suggestionsPresent() {
-  let controller = gURLBar.popup.input.controller;
-  let matchCount = controller.matchCount;
-  for (let i = 0; i < matchCount; i++) {
-    let url = controller.getValueAt(i);
-    let mozActionMatch = url.match(/^moz-action:([^,]+),(.*)$/);
-    if (mozActionMatch) {
-      let [, type, paramStr] = mozActionMatch;
-      let params = JSON.parse(paramStr);
-      if (type == "searchengine" && "searchSuggestion" in params) {
-        return true;
-      }
-    }
-  }
-  return false;
+  return UrlbarTestUtils.suggestionsPresent(window);
 }