Bug 1449255: Part 6 - Rename test_bug430120 to test_blocklist_url_parameters.js and modernize. r=aswan
authorKris Maglione <maglione.k@gmail.com>
Tue, 27 Mar 2018 14:11:46 -0700
changeset 777714 6845d3bd0b1b73c2e272e4b9359e4f936e0ea501
parent 777713 f9ccbbe67876b6e6fee36d6e9c312ad47e78230f
child 777715 2145915f5d8e88ed0906b64544f05d445c579b3e
push id105269
push userbmo:gl@mozilla.com
push dateThu, 05 Apr 2018 08:11:19 +0000
reviewersaswan
bugs1449255, 430120
milestone61.0a1
Bug 1449255: Part 6 - Rename test_bug430120 to test_blocklist_url_parameters.js and modernize. r=aswan MozReview-Commit-ID: 9f3RyR6jt4b
toolkit/mozapps/extensions/test/xpcshell/test_blocklist_url_parameters.js
toolkit/mozapps/extensions/test/xpcshell/test_bug430120.js
toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini
rename from toolkit/mozapps/extensions/test/xpcshell/test_bug430120.js
rename to toolkit/mozapps/extensions/test/xpcshell/test_blocklist_url_parameters.js
--- a/toolkit/mozapps/extensions/test/xpcshell/test_bug430120.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_blocklist_url_parameters.js
@@ -1,126 +1,105 @@
 /* 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 BLOCKLIST_TIMER                 = "blocklist-background-update-timer";
 const PREF_BLOCKLIST_URL              = "extensions.blocklist.url";
 const PREF_BLOCKLIST_ENABLED          = "extensions.blocklist.enabled";
 const PREF_APP_DISTRIBUTION           = "distribution.id";
 const PREF_APP_DISTRIBUTION_VERSION   = "distribution.version";
 const PREF_APP_UPDATE_CHANNEL         = "app.update.channel";
-const CATEGORY_UPDATE_TIMER           = "update-timer";
+
+Cu.importGlobalProperties(["URLSearchParams"]);
 
 // Get the HTTP server.
-ChromeUtils.import("resource://testing-common/httpd.js");
-ChromeUtils.import("resource://testing-common/MockRegistrar.jsm");
-var testserver;
-var gOSVersion;
-var gBlocklist;
-
-// This is a replacement for the timer service so we can trigger timers
-var timerService = {
+var testserver = AddonTestUtils.createHttpServer({hosts: ["example.com"]});
 
-  hasTimer(id) {
-    var catMan = Cc["@mozilla.org/categorymanager;1"]
-                   .getService(Ci.nsICategoryManager);
-    var entries = catMan.enumerateCategory(CATEGORY_UPDATE_TIMER);
-    while (entries.hasMoreElements()) {
-      var entry = entries.getNext().QueryInterface(Ci.nsISupportsCString).data;
-      var value = catMan.getCategoryEntry(CATEGORY_UPDATE_TIMER, entry);
-      var timerID = value.split(",")[2];
-      if (id == timerID) {
-        return true;
-      }
-    }
-    return false;
-  },
-
-  fireTimer(id) {
-    gBlocklist.QueryInterface(Ci.nsITimerCallback).notify(null);
-  },
-
-  QueryInterface(iid) {
-    if (iid.equals(Ci.nsIUpdateTimerManager)
-     || iid.equals(Ci.nsISupports))
-      return this;
-
-    throw Cr.NS_ERROR_NO_INTERFACE;
-  }
-};
-
-MockRegistrar.register("@mozilla.org/updates/timer-manager;1", timerService);
-
-function failHandler(metadata, response) {
-  do_throw("Should not have attempted to retrieve the blocklist when it is disabled");
+async function updateBlocklist(file) {
+  let blocklistUpdated = TestUtils.topicObserved("blocklist-updated");
+  Services.blocklist.QueryInterface(Ci.nsITimerCallback).notify(null);
+  return blocklistUpdated;
 }
 
-function pathHandler(metadata, response) {
-  var ABI = "noarch-spidermonkey";
-  // the blacklist service special-cases ABI for Universal binaries,
-  // so do the same here.
-  if ("@mozilla.org/xpcom/mac-utils;1" in Cc) {
-    var macutils = Cc["@mozilla.org/xpcom/mac-utils;1"]
-                     .getService(Ci.nsIMacUtils);
-    if (macutils.isUniversalBinary)
-      ABI += "-u-" + macutils.architecturesInBinary;
-  }
-  Assert.equal(metadata.queryString,
-               "xpcshell@tests.mozilla.org&1&XPCShell&1&" +
-               gAppInfo.appBuildID + "&" +
-               "XPCShell_" + ABI + "&locale&updatechannel&" +
-               gOSVersion + "&1.9&distribution&distribution-version");
-  gBlocklist.observe(null, "quit-application", "");
-  gBlocklist.observe(null, "xpcom-shutdown", "");
-  do_test_finished();
-}
-
-function run_test() {
-  var osVersion;
-  try {
-    osVersion = Services.sysinfo.getProperty("name") + " " + Services.sysinfo.getProperty("version");
-    if (osVersion) {
-      try {
-        osVersion += " (" + Services.sysinfo.getProperty("secondaryLibrary") + ")";
-      } catch (e) {
-      }
-      gOSVersion = encodeURIComponent(osVersion);
-    }
-  } catch (e) {
-  }
-
+add_task(async function setup() {
   createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
 
-  testserver = AddonTestUtils.createHttpServer({hosts: ["example.com"]});
-  testserver.registerPathHandler("/1", failHandler);
-  testserver.registerPathHandler("/2", pathHandler);
+  await promiseStartupManager();
+});
 
-  // Initialise the blocklist service
-  gBlocklist = Services.blocklist.QueryInterface(Ci.nsIObserver);
-  gBlocklist.observe(null, "profile-after-change", "");
-
-  Assert.ok(timerService.hasTimer(BLOCKLIST_TIMER));
-
-  do_test_pending();
+add_task(async function test_blocklist_disabled() {
+  testserver.registerPathHandler("/1", () => {
+    ok(false, "Should not have attempted to retrieve the blocklist when it is disabled");
+  });
 
   // This should have no effect as the blocklist is disabled
   Services.prefs.setCharPref(PREF_BLOCKLIST_URL, "http://example.com/1");
   Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, false);
-  timerService.fireTimer(BLOCKLIST_TIMER);
+
+  // No await here. We're not expecting it to resolve until the
+  // blocklist is re-enabled.
+  updateBlocklist();
+});
+
+add_task(async function test_blocklist_disabled() {
+  var ABI = "noarch-spidermonkey";
+  // the blacklist service special-cases ABI for Universal binaries,
+  // so do the same here.
+  const CONTRACT = "@mozilla.org/xpcom/mac-utils;1";
+  if (CONTRACT in Cc) {
+    let macutils = Cc[CONTRACT].getService(Ci.nsIMacUtils);
+    if (macutils.isUniversalBinary) {
+      ABI += "-u-" + macutils.architecturesInBinary;
+    }
+  }
+
+  let osVersion;
+  try {
+    osVersion = Services.sysinfo.getProperty("name") + " " + Services.sysinfo.getProperty("version");
+    if (osVersion) {
+      osVersion += " (" + Services.sysinfo.getProperty("secondaryLibrary") + ")";
+    }
+  } catch (e) {}
+
+  const EXPECTED = {
+    app_id: "xpcshell@tests.mozilla.org",
+    app_version: 1,
+    product: "XPCShell",
+    version: 1,
+    build_id: gAppInfo.appBuildID,
+    build_target: "XPCShell_" + ABI,
+    locale: "locale",
+    channel: "updatechannel",
+    os_version: osVersion,
+    platform_version: "1.9",
+    distribution: "distribution",
+    distribution_version: "distribution-version",
+  };
+
+  const PARAMS = Object.keys(EXPECTED).map(key => `${key}=%${key.toUpperCase()}%`).join("&");
+
+  let gotRequest = new Promise(resolve => {
+    testserver.registerPathHandler("/2", (request, response) => {
+      let params = new URLSearchParams(request.queryString);
+      for (let [key, val] of Object.entries(EXPECTED)) {
+        equal(String(params.get(key)), val, `Expected value for ${key} parameter`);
+      }
+
+      resolve();
+    });
+  });
 
   // Some values have to be on the default branch to work
   var defaults = Services.prefs.QueryInterface(Ci.nsIPrefService)
                        .getDefaultBranch(null);
-  defaults.setCharPref(PREF_APP_UPDATE_CHANNEL, "updatechannel");
-  defaults.setCharPref(PREF_APP_DISTRIBUTION, "distribution");
-  defaults.setCharPref(PREF_APP_DISTRIBUTION_VERSION, "distribution-version");
-  Services.locale.setRequestedLocales(["locale"]);
+  defaults.setCharPref(PREF_APP_UPDATE_CHANNEL, EXPECTED.channel);
+  defaults.setCharPref(PREF_APP_DISTRIBUTION, EXPECTED.distribution);
+  defaults.setCharPref(PREF_APP_DISTRIBUTION_VERSION, EXPECTED.distribution_version);
+  Services.locale.setRequestedLocales([EXPECTED.locale]);
 
   // This should correctly escape everything
-  Services.prefs.setCharPref(PREF_BLOCKLIST_URL, "http://example.com/2?" +
-                     "%APP_ID%&%APP_VERSION%&%PRODUCT%&%VERSION%&%BUILD_ID%&" +
-                     "%BUILD_TARGET%&%LOCALE%&%CHANNEL%&" +
-                     "%OS_VERSION%&%PLATFORM_VERSION%&%DISTRIBUTION%&%DISTRIBUTION_VERSION%");
+  Services.prefs.setCharPref(PREF_BLOCKLIST_URL, "http://example.com/2?" + PARAMS);
   Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, true);
-  timerService.fireTimer(BLOCKLIST_TIMER);
-}
+
+  await updateBlocklist();
+  await gotRequest;
+});
--- a/toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini
+++ b/toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini
@@ -32,30 +32,30 @@ tags = blocklist
 # Bug 676992: test consistently hangs on Android
 skip-if = os == "android"
 tags = blocklist
 [test_blocklist_prefs.js]
 tags = blocklist
 [test_blocklist_regexp.js]
 skip-if = os == "android"
 tags = blocklist
+[test_blocklist_url_parameters.js]
+# Bug 676992: test consistently hangs on Android
+skip-if = os == "android"
+tags = blocklist
 [test_blocklistchange.js]
 # Times out during parallel runs on desktop
 requesttimeoutfactor = 2
 tags = blocklist
 [test_bootstrap.js]
 skip-if = true # Bug 1358846 Bug 1365021 Bug 676992
 [test_bootstrap_const.js]
 [test_bootstrap_globals.js]
 [test_bug1180901_2.js]
 skip-if = os != "win"
-[test_bug430120.js]
-# Bug 676992: test consistently hangs on Android
-skip-if = os == "android"
-tags = blocklist
 [test_bug449027.js]
 # Bug 676992: test consistently hangs on Android
 skip-if = os == "android"
 tags = blocklist
 [test_bug455906.js]
 # Bug 676992: test consistently hangs on Android
 skip-if = os == "android"
 tags = blocklist