Bug 1252871 - Move promiseFindAddonUpdates to AddonTestUtils r=aswan
authorMatthew Wein <mwein@mozilla.com>
Mon, 26 Sep 2016 10:49:05 -0700
changeset 320167 7bcf61591212a2baa3272a5f321cc03f2689c3cc
parent 320166 593e3e011fc7f1fb7b5a4810475776f419715bba
child 320168 568c403cde43c611d3befebd5a8fe1cc1cb81582
push id20751
push userphilringnalda@gmail.com
push dateSun, 30 Oct 2016 18:06:35 +0000
treeherderfx-team@e3279760cd97 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
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
 }