Bug 1252871 - Move promiseFindAddonUpdates to AddonTestUtils r?aswan draft
authorMatthew Wein <mwein@mozilla.com>
Mon, 26 Sep 2016 10:49:05 -0700
changeset 430955 91d415f9703be44fc44664e2c6d02a0bae7bc2d9
parent 430954 9a4e0d9bafe946341bd4dd725474af24eeaedef2
child 430956 8b632e081b6613d0e13415c011e83d80e5df9729
child 431288 2cab1d210c941bc41a5116dca94990071dcbcefd
push id33946
push usermwein@mozilla.com
push dateFri, 28 Oct 2016 12:01:17 +0000
reviewersaswan
bugs1252871
milestone52.0a1
Bug 1252871 - Move promiseFindAddonUpdates to AddonTestUtils r?aswan MozReview-Commit-ID: 9lmpuQSQWw1
toolkit/mozapps/extensions/internal/AddonTestUtils.jsm
toolkit/mozapps/extensions/test/xpcshell/head_addons.js
--- a/toolkit/mozapps/extensions/internal/AddonTestUtils.jsm
+++ b/toolkit/mozapps/extensions/internal/AddonTestUtils.jsm
@@ -1083,16 +1083,71 @@ var AddonTestUtils = {
    * @return {Promise<Addon>}
    *        Resolves to the add-on with the given ID.
    */
   promiseAddonByID(id) {
     return new Promise(resolve => AddonManager.getAddonByID(id, resolve));
   },
 
   /**
+   * Returns a promise that will be resolved when an add-on update check is
+   * complete. The value resolved will be an AddonInstall if a new version was
+   * found.
+   */
+  promiseFindAddonUpdates(addon, reason = AddonManager.UPDATE_WHEN_PERIODIC_UPDATE) {
+    let equal = this.testScope.equal;
+    return new Promise((resolve, reject) => {
+      let result = {};
+      addon.findUpdates({
+        onNoCompatibilityUpdateAvailable: function(addon2) {
+          if ("compatibilityUpdate" in result) {
+            throw new Error("Saw multiple compatibility update events");
+          }
+          equal(addon, addon2, "onNoCompatibilityUpdateAvailable");
+          result.compatibilityUpdate = false;
+        },
+
+        onCompatibilityUpdateAvailable: function(addon2) {
+          if ("compatibilityUpdate" in result) {
+            throw new Error("Saw multiple compatibility update events");
+          }
+          equal(addon, addon2, "onCompatibilityUpdateAvailable");
+          result.compatibilityUpdate = true;
+        },
+
+        onNoUpdateAvailable: function(addon2) {
+          if ("updateAvailable" in result) {
+            throw new Error("Saw multiple update available events");
+          }
+          equal(addon, addon2, "onNoUpdateAvailable");
+          result.updateAvailable = false;
+        },
+
+        onUpdateAvailable: function(addon2, install) {
+          if ("updateAvailable" in result) {
+            throw new Error("Saw multiple update available events");
+          }
+          equal(addon, addon2, "onUpdateAvailable");
+          result.updateAvailable = install;
+        },
+
+        onUpdateFinished: function(addon2, error) {
+          equal(addon, addon2, "onUpdateFinished");
+          if (error == AddonManager.UPDATE_STATUS_NO_ERROR) {
+            resolve(result);
+          } else {
+            result.error = error;
+            reject(result);
+          }
+        }
+      }, reason);
+    });
+  },
+
+  /**
    * A promise-based variant of AddonManager.getAddonsWithOperationsByTypes
    *
    * @param {Array<string>} types
    *        The first argument to AddonManager.getAddonsWithOperationsByTypes
    * @return {Promise<Array<Addon>>}
    *        Resolves to an array of add-ons with the given operations
    *        pending.
    */
--- a/toolkit/mozapps/extensions/test/xpcshell/head_addons.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/head_addons.js
@@ -66,16 +66,17 @@ const {
   manuallyInstall,
   manuallyUninstall,
   promiseAddonByID,
   promiseAddonEvent,
   promiseAddonsByIDs,
   promiseAddonsWithOperationsByTypes,
   promiseCompleteAllInstalls,
   promiseConsoleOutput,
+  promiseFindAddonUpdates,
   promiseInstallAllFiles,
   promiseInstallFile,
   promiseRestartManager,
   promiseSetExtensionModifiedTime,
   promiseShutdownManager,
   promiseStartupManager,
   promiseWriteProxyFileToDir,
   registerDirectory,
@@ -1266,69 +1267,15 @@ function saveJSON(aData, aFile) {
 function callback_soon(aFunction) {
   return function(...args) {
     do_execute_soon(function() {
       aFunction.apply(null, args);
     }, aFunction.name ? "delayed callback " + aFunction.name : "delayed callback");
   }
 }
 
-/**
- * Returns a promise that will be resolved when an add-on update check is
- * complete. The value resolved will be an AddonInstall if a new version was
- * found.
- */
-function promiseFindAddonUpdates(addon, reason = AddonManager.UPDATE_WHEN_PERIODIC_UPDATE) {
-  return new Promise((resolve, reject) => {
-    let result = {};
-    addon.findUpdates({
-      onNoCompatibilityUpdateAvailable: function(addon2) {
-        if ("compatibilityUpdate" in result) {
-          do_throw("Saw multiple compatibility update events");
-        }
-        equal(addon, addon2, "onNoCompatibilityUpdateAvailable");
-        result.compatibilityUpdate = false;
-      },
-
-      onCompatibilityUpdateAvailable: function(addon2) {
-        if ("compatibilityUpdate" in result) {
-          do_throw("Saw multiple compatibility update events");
-        }
-        equal(addon, addon2, "onCompatibilityUpdateAvailable");
-        result.compatibilityUpdate = true;
-      },
-
-      onNoUpdateAvailable: function(addon2) {
-        if ("updateAvailable" in result) {
-          do_throw("Saw multiple update available events");
-        }
-        equal(addon, addon2, "onNoUpdateAvailable");
-        result.updateAvailable = false;
-      },
-
-      onUpdateAvailable: function(addon2, install) {
-        if ("updateAvailable" in result) {
-          do_throw("Saw multiple update available events");
-        }
-        equal(addon, addon2, "onUpdateAvailable");
-        result.updateAvailable = install;
-      },
-
-      onUpdateFinished: function(addon2, error) {
-        equal(addon, addon2, "onUpdateFinished");
-        if (error == AddonManager.UPDATE_STATUS_NO_ERROR) {
-          resolve(result);
-        } else {
-          result.error = error;
-          reject(result);
-        }
-      }
-    }, reason);
-  });
-}
-
 function writeProxyFileToDir(aDir, aAddon, aId) {
   awaitPromise(promiseWriteProxyFileToDir(aDir, aAddon, aId));
 
   let file = aDir.clone();
   file.append(aId);
   return file
 }