Bug 962248 - rewrite zoom test to be less terrible, r?dthayer draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Wed, 09 May 2018 09:07:37 +0100
changeset 793637 85c3ee4e5e4ebd51f9ee17ae9f58b74d160967bd
parent 793594 b52b2eb81d1e52d259d55d948281c7f6ddf1270c
push id109445
push usergijskruitbosch@gmail.com
push dateThu, 10 May 2018 14:36:35 +0000
reviewersdthayer
bugs962248
milestone62.0a1
Bug 962248 - rewrite zoom test to be less terrible, r?dthayer MozReview-Commit-ID: ByUM61fsOgh
browser/components/customizableui/test/browser_934951_zoom_in_toolbar.js
browser/components/customizableui/test/head.js
--- a/browser/components/customizableui/test/browser_934951_zoom_in_toolbar.js
+++ b/browser/components/customizableui/test/browser_934951_zoom_in_toolbar.js
@@ -1,91 +1,65 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 /* eslint-disable mozilla/no-arbitrary-setTimeout */
 
 "use strict";
 
-const kTimeoutInMS = 20000;
+let gZoomResetButton;
+
+async function waitForZoom(zoom) {
+  if (parseInt(gZoomResetButton.label) == zoom) {
+    return;
+  }
+  await promiseAttributeMutation(gZoomResetButton, "label", v => {
+    return parseInt(v) == zoom;
+  });
+}
 
 // Bug 934951 - Zoom controls percentage label doesn't update when it's in the toolbar and you navigate.
 add_task(async function() {
   CustomizableUI.addWidgetToArea("zoom-controls", CustomizableUI.AREA_NAVBAR);
+  gZoomResetButton = document.getElementById("zoom-reset-button");
   let tab1 = BrowserTestUtils.addTab(gBrowser, "about:mozilla");
   await BrowserTestUtils.browserLoaded(tab1.linkedBrowser);
   let tab2 = BrowserTestUtils.addTab(gBrowser, "about:robots");
   await BrowserTestUtils.browserLoaded(tab2.linkedBrowser);
   gBrowser.selectedTab = tab1;
-  let zoomResetButton = document.getElementById("zoom-reset-button");
 
   registerCleanupFunction(() => {
     info("Cleaning up.");
     CustomizableUI.reset();
     gBrowser.removeTab(tab2);
     gBrowser.removeTab(tab1);
   });
 
-  is(parseInt(zoomResetButton.label, 10), 100, "Default zoom is 100% for about:mozilla");
-  let zoomChangePromise = BrowserTestUtils.waitForEvent(window, "FullZoomChange");
+  is(parseInt(gZoomResetButton.label, 10), 100, "Default zoom is 100% for about:mozilla");
   FullZoom.enlarge();
-  await zoomChangePromise;
-  is(parseInt(zoomResetButton.label, 10), 110, "Zoom is changed to 110% for about:mozilla");
+  await waitForZoom(110);
+  is(parseInt(gZoomResetButton.label, 10), 110, "Zoom is changed to 110% for about:mozilla");
 
-  let tabSelectPromise = promiseObserverNotification("browser-fullZoom:location-change");
+  let tabSelectPromise = TestUtils.topicObserved("browser-fullZoom:location-change");
   gBrowser.selectedTab = tab2;
   await tabSelectPromise;
-  await new Promise(resolve => executeSoon(resolve));
-  is(parseInt(zoomResetButton.label, 10), 100, "Default zoom is 100% for about:robots");
+  await waitForZoom(100);
+  is(parseInt(gZoomResetButton.label, 10), 100, "Default zoom is 100% for about:robots");
 
   gBrowser.selectedTab = tab1;
-  let zoomResetPromise = BrowserTestUtils.waitForEvent(window, "FullZoomChange");
+  await waitForZoom(110);
   FullZoom.reset();
-  await zoomResetPromise;
-  is(parseInt(zoomResetButton.label, 10), 100, "Default zoom is 100% for about:mozilla");
+  await waitForZoom(100);
+  is(parseInt(gZoomResetButton.label, 10), 100, "Default zoom is 100% for about:mozilla");
 
   // Test zoom label updates while navigating pages in the same tab.
   FullZoom.enlarge();
-  await zoomChangePromise;
-  is(parseInt(zoomResetButton.label, 10), 110, "Zoom is changed to 110% for about:mozilla");
-  let attributeChangePromise = promiseAttributeMutation(zoomResetButton, "label", (v) => {
-    return parseInt(v, 10) == 100;
-  });
+  await waitForZoom(110);
+  is(parseInt(gZoomResetButton.label, 10), 110, "Zoom is changed to 110% for about:mozilla");
   await promiseTabLoadEvent(tab1, "about:home");
-  await attributeChangePromise;
-  is(parseInt(zoomResetButton.label, 10), 100, "Default zoom is 100% for about:home");
-  await promiseTabHistoryNavigation(-1, function() {
-    return parseInt(zoomResetButton.label, 10) == 110;
-  });
-  is(parseInt(zoomResetButton.label, 10), 110, "Zoom is still 110% for about:mozilla");
+  await waitForZoom(100);
+  is(parseInt(gZoomResetButton.label, 10), 100, "Default zoom is 100% for about:home");
+  gBrowser.selectedBrowser.goBack();
+  await waitForZoom(110);
+  is(parseInt(gZoomResetButton.label, 10), 110, "Zoom is still 110% for about:mozilla");
   FullZoom.reset();
 });
 
-function promiseObserverNotification(aObserver) {
-  return new Promise((resolve, reject) => {
-    function notificationCallback(e) {
-      Services.obs.removeObserver(notificationCallback, aObserver);
-      clearTimeout(timeoutId);
-      resolve();
-    }
-    let timeoutId = setTimeout(() => {
-      Services.obs.removeObserver(notificationCallback, aObserver);
-      reject("Notification '" + aObserver + "' did not happen within 20 seconds.");
-    }, kTimeoutInMS);
-    Services.obs.addObserver(notificationCallback, aObserver);
-  });
-}
-
-function promiseTabSelect() {
-  return new Promise((resolve, reject) => {
-    let container = window.gBrowser.tabContainer;
-    let timeoutId = setTimeout(() => {
-      container.removeEventListener("TabSelect", callback);
-      reject("TabSelect did not happen within 20 seconds");
-    }, kTimeoutInMS);
-    function callback(e) {
-      container.removeEventListener("TabSelect", callback);
-      clearTimeout(timeoutId);
-      executeSoon(resolve);
-    }
-    container.addEventListener("TabSelect", callback);
-  });
-}
--- a/browser/components/customizableui/test/head.js
+++ b/browser/components/customizableui/test/head.js
@@ -21,17 +21,16 @@ Services.scriptloader.loadSubScript("chr
 var gCUITestUtils = new CustomizableUITestUtils(window);
 
 Services.prefs.setBoolPref("browser.uiCustomization.skipSourceNodeCheck", true);
 registerCleanupFunction(() => Services.prefs.clearUserPref("browser.uiCustomization.skipSourceNodeCheck"));
 
 var {synthesizeDragStart, synthesizeDrop} = EventUtils;
 
 const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
-const kTabEventFailureTimeoutInMs = 20000;
 
 const kForceOverflowWidthPx = 300;
 
 function createDummyXULButton(id, label, win = window) {
   let btn = document.createElementNS(kNSXUL, "toolbarbutton");
   btn.id = id;
   btn.setAttribute("label", label || id);
   btn.className = "toolbarbutton-1 chromeclass-toolbar-additional";
@@ -374,50 +373,16 @@ function waitFor(aTimeout = 100) {
 function promiseTabLoadEvent(aTab, aURL) {
   let browser = aTab.linkedBrowser;
 
   BrowserTestUtils.loadURI(browser, aURL);
   return BrowserTestUtils.browserLoaded(browser);
 }
 
 /**
- * Navigate back or forward in tab history and wait for it to finish.
- *
- * @param aDirection   Number to indicate to move backward or forward in history.
- * @param aConditionFn Function that returns the result of an evaluated condition
- *                     that needs to be `true` to resolve the promise.
- * @return {Promise} resolved when navigation has finished.
- */
-function promiseTabHistoryNavigation(aDirection = -1, aConditionFn) {
-  return new Promise((resolve, reject) => {
-
-    let timeoutId = setTimeout(() => {
-      gBrowser.removeEventListener("pageshow", listener, true);
-      reject("Pageshow did not happen within " + kTabEventFailureTimeoutInMs + "ms");
-    }, kTabEventFailureTimeoutInMs);
-
-    function listener(event) {
-      gBrowser.removeEventListener("pageshow", listener, true);
-      clearTimeout(timeoutId);
-
-      if (aConditionFn) {
-        waitForCondition(aConditionFn).then(() => resolve(),
-                                            aReason => reject(aReason));
-      } else {
-        resolve();
-      }
-    }
-    gBrowser.addEventListener("pageshow", listener, true);
-
-    gBrowser.contentWindowAsCPOW.history.go(aDirection);
-
-  });
-}
-
-/**
  * Wait for an attribute on a node to change
  *
  * @param aNode      Node on which the mutation is expected
  * @param aAttribute The attribute we're interested in
  * @param aFilterFn  A function to check if the new value is what we want.
  * @return {Promise} resolved when the requisite mutation shows up.
  */
 function promiseAttributeMutation(aNode, aAttribute, aFilterFn) {