Bug 1449255: Part 7 - Rename test_bug449027 to test_blocklist_appversion.js and modernize. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Tue, 27 Mar 2018 14:47:47 -0700
changeset 773473 8e391ea428c8421c1e076874c4a7ceeac46b8537
parent 773472 6721d9aa869bf510b51a35c192171be40e5f9ae0
child 773474 652994715180be882e0c54fd8676a384684f1328
push id104243
push usermaglione.k@gmail.com
push dateWed, 28 Mar 2018 00:15:32 +0000
reviewersaswan
bugs1449255, 449027
milestone61.0a1
Bug 1449255: Part 7 - Rename test_bug449027 to test_blocklist_appversion.js and modernize. r?aswan MozReview-Commit-ID: EUsLNs9WaOI
toolkit/mozapps/extensions/test/xpcshell/test_blocklist_appversion.js
toolkit/mozapps/extensions/test/xpcshell/test_bug449027.js
toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini
rename from toolkit/mozapps/extensions/test/xpcshell/test_bug449027.js
rename to toolkit/mozapps/extensions/test/xpcshell/test_blocklist_appversion.js
--- a/toolkit/mozapps/extensions/test/xpcshell/test_bug449027.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_blocklist_appversion.js
@@ -1,17 +1,20 @@
 /* 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 URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul";
+
+const Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
 
-ChromeUtils.import("resource://testing-common/httpd.js");
 ChromeUtils.import("resource://testing-common/MockRegistrar.jsm");
 
+var testserver = AddonTestUtils.createHttpServer({hosts: ["example.com"]});
+testserver.registerDirectory("/data/", do_get_file("data"));
+
 var ADDONS = [{
   id: "test_bug449027_1@tests.mozilla.org",
   name: "Bug 449027 Addon Test 1",
   version: "5",
   start: false,
   appBlocks: false,
   toolkitBlocks: false
 }, {
@@ -220,198 +223,125 @@ var PLUGINS = [
   new MockPluginTag("test_bug449027_20", "5", false, true, true),
   new MockPluginTag("test_bug449027_21", "5", false, true, true),
   new MockPluginTag("test_bug449027_22", "5", false, true, true),
   new MockPluginTag("test_bug449027_23", "5", false, true, true),
   new MockPluginTag("test_bug449027_24", "5", false, true, true),
   new MockPluginTag("test_bug449027_25", "5", false, true, true)
 ];
 
-var gCallback = null;
-var gTestserver = null;
 var gNewBlocks = [];
 
 // A fake plugin host for the blocklist service to use
 var PluginHost = {
   getPluginTags(countRef) {
     countRef.value = PLUGINS.length;
     return PLUGINS;
   },
 
-  QueryInterface(iid) {
-    if (iid.equals(Ci.nsIPluginHost)
-     || iid.equals(Ci.nsISupports))
-      return this;
-
-    throw Cr.NS_ERROR_NO_INTERFACE;
-  }
+  QueryInterface: XPCOMUtils.generateQI(["nsIPluginHost"]),
 };
 
-// Don't need the full interface, attempts to call other methods will just
-// throw which is just fine
-var WindowWatcher = {
-  openWindow(parent, url, name, features, args) {
-    // Should be called to list the newly blocklisted items
-    Assert.equal(url, URI_EXTENSION_BLOCKLIST_DIALOG);
-    Assert.notEqual(gCallback, null);
-
-    args = args.wrappedJSObject;
-
-    gNewBlocks = [];
-    var list = args.list;
-    for (let listItem of list)
-      gNewBlocks.push(listItem.name + " " + listItem.version);
-
-    // Call the callback after the blocklist has finished up
-    do_timeout(0, gCallback);
+var BlocklistPrompt = {
+  prompt(list) {
+    gNewBlocks = list.map(item => `${item.name} ${item.version}`);
   },
 
-  QueryInterface(iid) {
-    if (iid.equals(Ci.nsIWindowWatcher)
-     || iid.equals(Ci.nsISupports))
-      return this;
+  QueryInterface: XPCOMUtils.generateQI(["nsIBlocklistPrompt"]),
+};
+
+
+async function loadBlocklist(file) {
+  let blocklistUpdated = TestUtils.topicObserved("blocklist-updated");
 
-    throw Cr.NS_ERROR_NO_INTERFACE;
-  }
-};
+  Services.prefs.setCharPref("extensions.blocklist.url",
+                             "http://example.com/data/" + file);
+  Services.blocklist.QueryInterface(Ci.nsITimerCallback).notify(null);
+
+  await blocklistUpdated;
+}
 
 MockRegistrar.register("@mozilla.org/plugin/host;1", PluginHost);
-MockRegistrar.register("@mozilla.org/embedcomp/window-watcher;1", WindowWatcher);
+
+let factory = XPCOMUtils.generateSingletonFactory(function() { return BlocklistPrompt; });
+Cm.registerFactory(Components.ID("{26d32654-30c7-485d-b983-b4d2568aebba}"),
+                   "Blocklist Prompt",
+                   "@mozilla.org/addons/blocklist-prompt;1", factory);
 
-function create_addon(addon) {
-  var installrdf = "<?xml version=\"1.0\"?>\n" +
-                   "\n" +
-                   "<RDF xmlns=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
-                   "     xmlns:em=\"http://www.mozilla.org/2004/em-rdf#\">\n" +
-                   "  <Description about=\"urn:mozilla:install-manifest\">\n" +
-                   "    <em:id>" + addon.id + "</em:id>\n" +
-                   "    <em:version>" + addon.version + "</em:version>\n" +
-                   "    <em:bootstrap>true</em:bootstrap>\n" +
-                   "    <em:targetApplication>\n" +
-                   "      <Description>\n" +
-                   "        <em:id>xpcshell@tests.mozilla.org</em:id>\n" +
-                   "        <em:minVersion>3</em:minVersion>\n" +
-                   "        <em:maxVersion>3</em:maxVersion>\n" +
-                   "      </Description>\n" +
-                   "    </em:targetApplication>\n" +
-                   "    <em:name>" + addon.name + "</em:name>\n" +
-                   "  </Description>\n" +
-                   "</RDF>\n";
-  var target = gProfD.clone();
-  target.append("extensions");
-  target.append(addon.id);
-  target.append("install.rdf");
-  target.create(target.NORMAL_FILE_TYPE, 0o644);
-  var stream = Cc["@mozilla.org/network/file-output-stream;1"]
-                 .createInstance(Ci.nsIFileOutputStream);
-  stream.init(target, 0x04 | 0x08 | 0x20, 0o664, 0); // write, create, truncate
-  stream.write(installrdf, installrdf.length);
-  stream.close();
+function createAddon(addon) {
+  return promiseInstallXPI({
+    name: addon.name,
+    id: addon.id,
+    version: addon.version,
+    bootstrap: true,
+    targetApplications: [{
+      id: "xpcshell@tests.mozilla.org",
+      minVersion: "3",
+      maxVersion: "3"}],
+  });
 }
 
 /**
  * Checks that items are blocklisted correctly according to the current test.
  * If a lastTest is provided checks that the notification dialog got passed
  * the newly blocked items compared to the previous test.
  */
-function check_state(test, lastTest, callback) {
-  AddonManager.getAddonsByIDs(ADDONS.map(a => a.id), function(addons) {
-    for (var i = 0; i < ADDONS.length; i++) {
-      var blocked = addons[i].blocklistState == Ci.nsIBlocklistService.STATE_BLOCKED;
-      if (blocked != ADDONS[i][test])
-        do_throw("Blocklist state did not match expected for extension " + (i + 1) + ", test " + test);
-    }
+async function checkState(test, lastTest, callback) {
+  let addons = await AddonManager.getAddonsByIDs(ADDONS.map(a => a.id));
+
+  for (var i = 0; i < ADDONS.length; i++) {
+    var blocked = addons[i].blocklistState == Ci.nsIBlocklistService.STATE_BLOCKED;
+    equal(blocked, ADDONS[i][test],
+          `Blocklist state should match expected for extension ${i + 1}, test ${test}`);
+  }
 
+  for (i = 0; i < PLUGINS.length; i++) {
+    equal(PLUGINS[i].blocklisted, PLUGINS[i][test],
+          `Blocklist state should match expected for plugin ${i + 1}, test ${test}`);
+  }
+
+  if (lastTest) {
+    var expected = 0;
     for (i = 0; i < PLUGINS.length; i++) {
-      if (PLUGINS[i].blocklisted != PLUGINS[i][test])
-        do_throw("Blocklist state did not match expected for plugin " + (i + 1) + ", test " + test);
+      if (PLUGINS[i][test] && !PLUGINS[i][lastTest]) {
+        ok(gNewBlocks.includes(`${PLUGINS[i].name} ${PLUGINS[i].version}`),
+           `Plugin ${i + 1} should have been listed in the blocklist notification for test ${test}`);
+        expected++;
+      }
     }
 
-    if (lastTest) {
-      var expected = 0;
-      for (i = 0; i < PLUGINS.length; i++) {
-        if (PLUGINS[i][test] && !PLUGINS[i][lastTest]) {
-          if (!gNewBlocks.includes(PLUGINS[i].name + " " + PLUGINS[i].version))
-            do_throw("Plugin " + (i + 1) + " should have been listed in the blocklist notification for test " + test);
-          expected++;
-        }
-      }
-
-      Assert.equal(expected, gNewBlocks.length);
-    }
-    executeSoon(callback);
-  });
-}
-
-function load_blocklist(file) {
-  Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/" + file);
-  var blocklist = Cc["@mozilla.org/extensions/blocklist;1"]
-                    .getService(Ci.nsITimerCallback);
-  blocklist.notify(null);
+    Assert.equal(expected, gNewBlocks.length);
+  }
 }
 
-function run_test() {
-  // Setup for test
-  dump("Setting up tests\n");
-  // Rather than keeping lots of identical add-ons in version control, just
-  // write them into the profile.
-  for (let addon of ADDONS)
-    create_addon(addon);
+add_task(async function test() {
+  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "3", "8");
+  await promiseStartupManager();
 
-  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "3", "8");
-  startupManager();
-
-  gTestserver = new HttpServer();
-  gTestserver.registerDirectory("/data/", do_get_file("data"));
-  gTestserver.start(-1);
-  gPort = gTestserver.identity.primaryPort;
+  for (let addon of ADDONS) {
+    await createAddon(addon);
+  }
 
-  do_test_pending();
-  check_test_pt1();
-}
-
-/**
- * Checks the initial state is correct
- */
-function check_test_pt1() {
-  dump("Checking pt 1\n");
+  let addons = await AddonManager.getAddonsByIDs(ADDONS.map(a => a.id));
+  for (var i = 0; i < ADDONS.length; i++) {
+    ok(addons[i], `Addon ${i + 1} should have been correctly installed`);
+  }
 
-  AddonManager.getAddonsByIDs(ADDONS.map(a => a.id), function(addons) {
-    for (var i = 0; i < ADDONS.length; i++) {
-      if (!addons[i])
-        do_throw("Addon " + (i + 1) + " did not get installed correctly");
-    }
-
-    executeSoon(function checkstate1() { check_state("start", null, run_test_pt2); });
-  });
-}
+  await checkState("start");
+});
 
 /**
  * Load the toolkit based blocks
  */
-function run_test_pt2() {
-  dump("Running test pt 2\n");
-  gCallback = check_test_pt2;
-  load_blocklist("test_bug449027_toolkit.xml");
-}
+add_task(async function test_pt2() {
+  await loadBlocklist("test_bug449027_toolkit.xml");
 
-function check_test_pt2() {
-  dump("Checking pt 2\n");
-  check_state("toolkitBlocks", "start", run_test_pt3);
-}
+  await checkState("toolkitBlocks", "start");
+});
 
 /**
  * Load the application based blocks
  */
-function run_test_pt3() {
-  dump("Running test pt 3\n");
-  gCallback = check_test_pt3;
-  load_blocklist("test_bug449027_app.xml");
-}
+add_task(async function test_pt3() {
+  await loadBlocklist("test_bug449027_app.xml");
 
-function check_test_pt3() {
-  dump("Checking pt 3\n");
-  check_state("appBlocks", "toolkitBlocks", end_test);
-}
-
-function end_test() {
-  gTestserver.stop(do_test_finished);
-}
+  await checkState("appBlocks", "toolkitBlocks");
+});
--- a/toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini
+++ b/toolkit/mozapps/extensions/test/xpcshell/xpcshell.ini
@@ -17,16 +17,20 @@ skip-if = os == "android"
 [test_XPIStates.js]
 [test_XPIcancel.js]
 [test_addonStartup.js]
 [test_asyncBlocklistLoad.js]
 tags = blocklist
 [test_backgroundupdate.js]
 [test_bad_json.js]
 [test_badschema.js]
+[test_blocklist_appversion.js]
+# Bug 676992: test consistently hangs on Android
+skip-if = os == "android"
+tags = blocklist
 [test_blocklist_gfx.js]
 tags = blocklist
 [test_blocklist_metadata_filters.js]
 # Bug 676992: test consistently hangs on Android
 skip-if = os == "android"
 tags = blocklist
 [test_blocklist_osabi.js]
 # Bug 676992: test consistently hangs on Android
@@ -46,20 +50,16 @@ tags = blocklist
 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_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
 [test_bug465190.js]
 # Bug 676992: test consistently hangs on Android
 skip-if = os == "android"
 [test_bug468528.js]