Bug 1404946 - Rename wait.until to PollPromise. r?whimboo draft
authorAndreas Tolfsen <ato@sny.no>
Mon, 02 Oct 2017 16:59:25 +0100
changeset 675702 640a46fb507265427e8cde4c1c1d5c497ded2646
parent 675701 8b94da14a37324255a53e96aa8e8570f08747609
child 675703 7976980398b17eccb3a8b33401341247748dae7a
push id83206
push userbmo:ato@sny.no
push dateThu, 05 Oct 2017 17:53:52 +0000
reviewerswhimboo
bugs1404946
milestone58.0a1
Bug 1404946 - Rename wait.until to PollPromise. r?whimboo Now that we have selective imports through Cu.import we can give wait.until a more descriptive name that matches TimedPromise. This patch renames the wait.until utility to PollPromise. MozReview-Commit-ID: 9gsvAV27mMl
testing/marionette/driver.js
testing/marionette/element.js
testing/marionette/sync.js
testing/marionette/test_sync.js
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -42,17 +42,20 @@ Cu.import("chrome://marionette/content/e
 Cu.import("chrome://marionette/content/event.js");
 Cu.import("chrome://marionette/content/interaction.js");
 Cu.import("chrome://marionette/content/l10n.js");
 Cu.import("chrome://marionette/content/legacyaction.js");
 Cu.import("chrome://marionette/content/modal.js");
 Cu.import("chrome://marionette/content/proxy.js");
 Cu.import("chrome://marionette/content/reftest.js");
 Cu.import("chrome://marionette/content/session.js");
-const {wait, TimedPromise} = Cu.import("chrome://marionette/content/sync.js", {});
+const {
+  PollPromise,
+  TimedPromise,
+} = Cu.import("chrome://marionette/content/sync.js", {});
 
 Cu.importGlobalProperties(["URL"]);
 
 this.EXPORTED_SYMBOLS = ["GeckoDriver", "Context"];
 
 const FRAME_SCRIPT = "chrome://marionette/content/listener.js";
 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 
@@ -1483,30 +1486,30 @@ GeckoDriver.prototype.setWindowRect = as
       win.resizeTo(width, height);
     });
   }
 
   // Wait until window size has changed.  We can't wait for the
   // user-requested window size as this may not be achievable on the
   // current system.
   const windowResizeChange = async () => {
-    return wait.until((resolve, reject) => {
+    return new PollPromise((resolve, reject) => {
       let curRect = this.curBrowser.rect;
       if (curRect.width != origRect.width &&
           curRect.height != origRect.height) {
         resolve();
       } else {
         reject();
       }
     });
   };
 
   // Wait for the window position to change.
   async function windowPosition(x, y) {
-    return wait.until((resolve, reject) => {
+    return new PollPromise((resolve, reject) => {
       if ((x == win.screenX && y == win.screenY) ||
           (win.screenX != origRect.x || win.screenY != origRect.y)) {
         resolve();
       } else {
         reject();
       }
     });
   }
@@ -3043,18 +3046,18 @@ GeckoDriver.prototype.maximizeWindow = a
   }
 
   const origSize = {
     outerWidth: win.outerWidth,
     outerHeight: win.outerHeight,
   };
 
   // Wait for the window size to change.
-  async function windowSizeChange() {
-    return wait.until((resolve, reject) => {
+  async function windowSizeChange(f) {
+    return new PollPromise((resolve, reject) => {
       let curSize = {
         outerWidth: win.outerWidth,
         outerHeight: win.outerHeight,
       };
       if (curSize.outerWidth != origSize.outerWidth ||
           curSize.outerHeight != origSize.outerHeight) {
         resolve();
       } else {
--- a/testing/marionette/element.js
+++ b/testing/marionette/element.js
@@ -10,17 +10,17 @@ const {classes: Cc, interfaces: Ci, util
 Cu.import("chrome://marionette/content/assert.js");
 Cu.import("chrome://marionette/content/atom.js");
 const {
   InvalidSelectorError,
   NoSuchElementError,
   pprint,
   StaleElementReferenceError,
 } = Cu.import("chrome://marionette/content/error.js", {});
-Cu.import("chrome://marionette/content/sync.js");
+const {PollPromise} = Cu.import("chrome://marionette/content/sync.js", {});
 
 this.EXPORTED_SYMBOLS = ["element"];
 
 const XMLNS = "http://www.w3.org/1999/xhtml";
 const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 
 const uuidGen = Cc["@mozilla.org/uuid-generator;1"]
     .getService(Ci.nsIUUIDGenerator);
@@ -244,17 +244,17 @@ element.find = function(container, strat
   let searchFn;
   if (opts.all) {
     searchFn = findElements.bind(this);
   } else {
     searchFn = findElement.bind(this);
   }
 
   return new Promise((resolve, reject) => {
-    let findElements = wait.until((resolve, reject) => {
+    let findElements = new PollPromise((resolve, reject) => {
       let res = find_(container, strategy, selector, searchFn, opts);
       if (res.length > 0) {
         resolve(Array.from(res));
       } else {
         reject([]);
       }
     }, opts.timeout);
 
--- a/testing/marionette/sync.js
+++ b/testing/marionette/sync.js
@@ -7,24 +7,17 @@
 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
 const {
   error,
   TimeoutError,
 } = Cu.import("chrome://marionette/content/error.js", {});
 
 /* exported TimedPromise */
-this.EXPORTED_SYMBOLS = ["wait", "TimedPromise"];
-
-/**
- * Poll-waiting utilities.
- *
- * @namespace
- */
-this.wait = {};
+this.EXPORTED_SYMBOLS = ["PollPromise", "TimedPromise"];
 
 const {TYPE_ONE_SHOT, TYPE_REPEATING_SLACK} = Ci.nsITimer;
 
 /**
  * @callback Condition
  *
  * @param {function(*)} resolve
  *     To be called when the condition has been met.  Will return the
@@ -57,17 +50,17 @@ const {TYPE_ONE_SHOT, TYPE_REPEATING_SLA
  * <code>reject</code> will evaluate <var>func</var> again until
  * the <var>timeout</var> duration has elapsed or <var>func</var>
  * throws.  The passed value to <code>reject</code> will also be
  * returned to the caller once the wait has expired.
  *
  * Usage:
  *
  * <pre><code>
- *     let els = wait.until((resolve, reject) => {
+ *     let els = new PollPromise((resolve, reject) => {
  *       let res = document.querySelectorAll("p");
  *       if (res.length > 0) {
  *         resolve(Array.from(res));
  *       } else {
  *         reject([]);
  *       }
  *     });
  * </pre></code>
@@ -84,17 +77,17 @@ const {TYPE_ONE_SHOT, TYPE_REPEATING_SLA
  *
  * @return {Promise.<*>}
  *     Yields the value passed to <var>func</var>'s
  *     <code>resolve</code> or <code>reject</code> callbacks.
  *
  * @throws {*}
  *     If <var>func</var> throws, its error is propagated.
  */
-wait.until = function(func, timeout = 2000, interval = 10) {
+function PollPromise(func, timeout = 2000, interval = 10) {
   const timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
 
   return new Promise((resolve, reject) => {
     const start = new Date().getTime();
     const end = start + timeout;
 
     let evalFn = () => {
       new Promise(func).then(resolve, rejected => {
@@ -118,17 +111,17 @@ wait.until = function(func, timeout = 20
 
   }).then(res => {
     timer.cancel();
     return res;
   }, err => {
     timer.cancel();
     throw err;
   });
-};
+}
 
 /**
  * The <code>TimedPromise</code> object represents the timed, eventual
  * completion (or failure) of an asynchronous operation, and its
  * resulting value.
  *
  * In contrast to a regular {@link Promise}, it times out after
  * <var>timeout</var>.
--- a/testing/marionette/test_sync.js
+++ b/testing/marionette/test_sync.js
@@ -1,76 +1,76 @@
 /* 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/. */
 
 const {utils: Cu} = Components;
 
-Cu.import("chrome://marionette/content/sync.js");
+const {PollPromise} = Cu.import("chrome://marionette/content/sync.js", {});
 
 const DEFAULT_TIMEOUT = 2000;
 
-add_task(async function test_until_types() {
+add_task(async function test_PollPromise_types() {
   for (let typ of [true, false, "foo", 42, [], {}]) {
-    strictEqual(typ, await wait.until(resolve => resolve(typ)));
+    strictEqual(typ, await new PollPromise(resolve => resolve(typ)));
   }
 });
 
-add_task(async function test_until_timeoutElapse() {
+add_task(async function test_PollPromise_timeoutElapse() {
   let nevals = 0;
   let start = new Date().getTime();
-  await wait.until((resolve, reject) => {
+  await new PollPromise((resolve, reject) => {
     ++nevals;
     reject();
   });
   let end = new Date().getTime();
   greaterOrEqual((end - start), DEFAULT_TIMEOUT);
   greaterOrEqual(nevals, 15);
 });
 
-add_task(async function test_until_rethrowError() {
+add_task(async function test_PollPromise_rethrowError() {
   let nevals = 0;
   let err;
   try {
-    await wait.until(() => {
+    await PollPromise(() => {
       ++nevals;
       throw new Error();
     });
   } catch (e) {
     err = e;
   }
   equal(1, nevals);
   ok(err instanceof Error);
 });
 
-add_task(async function test_until_noTimeout() {
+add_task(async function test_PollPromise_noTimeout() {
   // run at least once when timeout is 0
   let nevals = 0;
   let start = new Date().getTime();
-  await wait.until((resolve, reject) => {
+  await new PollPromise((resolve, reject) => {
     ++nevals;
     reject();
   }, 0);
   let end = new Date().getTime();
   equal(1, nevals);
   less((end - start), DEFAULT_TIMEOUT);
 });
 
-add_task(async function test_until_timeout() {
+add_task(async function test_PollPromise_timeout() {
   let nevals = 0;
   let start = new Date().getTime();
-  await wait.until((resolve, reject) => {
+  await new PollPromise((resolve, reject) => {
     ++nevals;
     reject();
   }, 100);
   let end = new Date().getTime();
   greater(nevals, 1);
   greaterOrEqual((end - start), 100);
 });
 
-add_task(async function test_until_interval() {
+add_task(async function test_PollPromise_interval() {
   let nevals = 0;
-  await wait.until((resolve, reject) => {
+  await new PollPromise((resolve, reject) => {
     ++nevals;
     reject();
   }, 100, 100);
   equal(2, nevals);
 });