toolkit/mozapps/extensions/test/xpcshell/test_bug449027.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_bug449027.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,448 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 + */
     1.8 +const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul";
     1.9 +
    1.10 +const Ci = Components.interfaces;
    1.11 +const Cu = Components.utils;
    1.12 +
    1.13 +Cu.import("resource://testing-common/httpd.js");
    1.14 +
    1.15 +var ADDONS = [{
    1.16 +  id: "test_bug449027_1@tests.mozilla.org",
    1.17 +  name: "Bug 449027 Addon Test 1",
    1.18 +  version: "5",
    1.19 +  start: false,
    1.20 +  appBlocks: false,
    1.21 +  toolkitBlocks: false
    1.22 +}, {
    1.23 +  id: "test_bug449027_2@tests.mozilla.org",
    1.24 +  name: "Bug 449027 Addon Test 2",
    1.25 +  version: "5",
    1.26 +  start: false,
    1.27 +  appBlocks: true,
    1.28 +  toolkitBlocks: false
    1.29 +}, {
    1.30 +  id: "test_bug449027_3@tests.mozilla.org",
    1.31 +  name: "Bug 449027 Addon Test 3",
    1.32 +  version: "5",
    1.33 +  start: false,
    1.34 +  appBlocks: true,
    1.35 +  toolkitBlocks: false
    1.36 +}, {
    1.37 +  id: "test_bug449027_4@tests.mozilla.org",
    1.38 +  name: "Bug 449027 Addon Test 4",
    1.39 +  version: "5",
    1.40 +  start: false,
    1.41 +  appBlocks: false,
    1.42 +  toolkitBlocks: false
    1.43 +}, {
    1.44 +  id: "test_bug449027_5@tests.mozilla.org",
    1.45 +  name: "Bug 449027 Addon Test 5",
    1.46 +  version: "5",
    1.47 +  start: false,
    1.48 +  appBlocks: false,
    1.49 +  toolkitBlocks: false
    1.50 +}, {
    1.51 +  id: "test_bug449027_6@tests.mozilla.org",
    1.52 +  name: "Bug 449027 Addon Test 6",
    1.53 +  version: "5",
    1.54 +  start: false,
    1.55 +  appBlocks: true,
    1.56 +  toolkitBlocks: false
    1.57 +}, {
    1.58 +  id: "test_bug449027_7@tests.mozilla.org",
    1.59 +  name: "Bug 449027 Addon Test 7",
    1.60 +  version: "5",
    1.61 +  start: false,
    1.62 +  appBlocks: true,
    1.63 +  toolkitBlocks: false
    1.64 +}, {
    1.65 +  id: "test_bug449027_8@tests.mozilla.org",
    1.66 +  name: "Bug 449027 Addon Test 8",
    1.67 +  version: "5",
    1.68 +  start: false,
    1.69 +  appBlocks: true,
    1.70 +  toolkitBlocks: false
    1.71 +}, {
    1.72 +  id: "test_bug449027_9@tests.mozilla.org",
    1.73 +  name: "Bug 449027 Addon Test 9",
    1.74 +  version: "5",
    1.75 +  start: false,
    1.76 +  appBlocks: true,
    1.77 +  toolkitBlocks: false
    1.78 +}, {
    1.79 +  id: "test_bug449027_10@tests.mozilla.org",
    1.80 +  name: "Bug 449027 Addon Test 10",
    1.81 +  version: "5",
    1.82 +  start: false,
    1.83 +  appBlocks: true,
    1.84 +  toolkitBlocks: false
    1.85 +}, {
    1.86 +  id: "test_bug449027_11@tests.mozilla.org",
    1.87 +  name: "Bug 449027 Addon Test 11",
    1.88 +  version: "5",
    1.89 +  start: false,
    1.90 +  appBlocks: true,
    1.91 +  toolkitBlocks: false
    1.92 +}, {
    1.93 +  id: "test_bug449027_12@tests.mozilla.org",
    1.94 +  name: "Bug 449027 Addon Test 12",
    1.95 +  version: "5",
    1.96 +  start: false,
    1.97 +  appBlocks: true,
    1.98 +  toolkitBlocks: false
    1.99 +}, {
   1.100 +  id: "test_bug449027_13@tests.mozilla.org",
   1.101 +  name: "Bug 449027 Addon Test 13",
   1.102 +  version: "5",
   1.103 +  start: false,
   1.104 +  appBlocks: true,
   1.105 +  toolkitBlocks: false
   1.106 +}, {
   1.107 +  id: "test_bug449027_14@tests.mozilla.org",
   1.108 +  name: "Bug 449027 Addon Test 14",
   1.109 +  version: "5",
   1.110 +  start: false,
   1.111 +  appBlocks: false,
   1.112 +  toolkitBlocks: false
   1.113 +}, {
   1.114 +  id: "test_bug449027_15@tests.mozilla.org",
   1.115 +  name: "Bug 449027 Addon Test 15",
   1.116 +  version: "5",
   1.117 +  start: false,
   1.118 +  appBlocks: true,
   1.119 +  toolkitBlocks: true
   1.120 +}, {
   1.121 +  id: "test_bug449027_16@tests.mozilla.org",
   1.122 +  name: "Bug 449027 Addon Test 16",
   1.123 +  version: "5",
   1.124 +  start: false,
   1.125 +  appBlocks: true,
   1.126 +  toolkitBlocks: true
   1.127 +}, {
   1.128 +  id: "test_bug449027_17@tests.mozilla.org",
   1.129 +  name: "Bug 449027 Addon Test 17",
   1.130 +  version: "5",
   1.131 +  start: false,
   1.132 +  appBlocks: false,
   1.133 +  toolkitBlocks: false
   1.134 +}, {
   1.135 +  id: "test_bug449027_18@tests.mozilla.org",
   1.136 +  name: "Bug 449027 Addon Test 18",
   1.137 +  version: "5",
   1.138 +  start: false,
   1.139 +  appBlocks: false,
   1.140 +  toolkitBlocks: false
   1.141 +}, {
   1.142 +  id: "test_bug449027_19@tests.mozilla.org",
   1.143 +  name: "Bug 449027 Addon Test 19",
   1.144 +  version: "5",
   1.145 +  start: false,
   1.146 +  appBlocks: true,
   1.147 +  toolkitBlocks: true
   1.148 +}, {
   1.149 +  id: "test_bug449027_20@tests.mozilla.org",
   1.150 +  name: "Bug 449027 Addon Test 20",
   1.151 +  version: "5",
   1.152 +  start: false,
   1.153 +  appBlocks: true,
   1.154 +  toolkitBlocks: true
   1.155 +}, {
   1.156 +  id: "test_bug449027_21@tests.mozilla.org",
   1.157 +  name: "Bug 449027 Addon Test 21",
   1.158 +  version: "5",
   1.159 +  start: false,
   1.160 +  appBlocks: true,
   1.161 +  toolkitBlocks: true
   1.162 +}, {
   1.163 +  id: "test_bug449027_22@tests.mozilla.org",
   1.164 +  name: "Bug 449027 Addon Test 22",
   1.165 +  version: "5",
   1.166 +  start: false,
   1.167 +  appBlocks: true,
   1.168 +  toolkitBlocks: true
   1.169 +}, {
   1.170 +  id: "test_bug449027_23@tests.mozilla.org",
   1.171 +  name: "Bug 449027 Addon Test 23",
   1.172 +  version: "5",
   1.173 +  start: false,
   1.174 +  appBlocks: true,
   1.175 +  toolkitBlocks: true
   1.176 +}, {
   1.177 +  id: "test_bug449027_24@tests.mozilla.org",
   1.178 +  name: "Bug 449027 Addon Test 24",
   1.179 +  version: "5",
   1.180 +  start: false,
   1.181 +  appBlocks: true,
   1.182 +  toolkitBlocks: true
   1.183 +}, {
   1.184 +  id: "test_bug449027_25@tests.mozilla.org",
   1.185 +  name: "Bug 449027 Addon Test 25",
   1.186 +  version: "5",
   1.187 +  start: false,
   1.188 +  appBlocks: true,
   1.189 +  toolkitBlocks: true
   1.190 +}];
   1.191 +
   1.192 +function MockPluginTag(name, version, start, appBlocks, toolkitBlocks)
   1.193 +{
   1.194 +  this.name = name;
   1.195 +  this.version = version;
   1.196 +  this.start = start;
   1.197 +  this.appBlocks = appBlocks;
   1.198 +  this.toolkitBlocks = toolkitBlocks;
   1.199 +}
   1.200 +Object.defineProperty(MockPluginTag.prototype, "blocklisted", {
   1.201 +  get: function MockPluginTag_getBlocklisted() {
   1.202 +    let bls = AM_Cc["@mozilla.org/extensions/blocklist;1"].getService(Ci.nsIBlocklistService);
   1.203 +    return bls.getPluginBlocklistState(this) == bls.STATE_BLOCKED;
   1.204 +  }
   1.205 +});
   1.206 +
   1.207 +var PLUGINS = [
   1.208 +  new MockPluginTag("test_bug449027_1", "5", false, false, false),
   1.209 +  new MockPluginTag("test_bug449027_2", "5", false, true, false),
   1.210 +  new MockPluginTag("test_bug449027_3", "5", false, true, false),
   1.211 +  new MockPluginTag("test_bug449027_4", "5", false, false, false),
   1.212 +  new MockPluginTag("test_bug449027_5", "5", false, false, false),
   1.213 +  new MockPluginTag("test_bug449027_6", "5", false, true, false),
   1.214 +  new MockPluginTag("test_bug449027_7", "5", false, true, false),
   1.215 +  new MockPluginTag("test_bug449027_8", "5", false, true, false),
   1.216 +  new MockPluginTag("test_bug449027_9", "5", false, true, false),
   1.217 +  new MockPluginTag("test_bug449027_10", "5", false, true, false),
   1.218 +  new MockPluginTag("test_bug449027_11", "5", false, true, false),
   1.219 +  new MockPluginTag("test_bug449027_12", "5", false, true, false),
   1.220 +  new MockPluginTag("test_bug449027_13", "5", false, true, false),
   1.221 +  new MockPluginTag("test_bug449027_14", "5", false, false, false),
   1.222 +  new MockPluginTag("test_bug449027_15", "5", false, true, true),
   1.223 +  new MockPluginTag("test_bug449027_16", "5", false, true, true),
   1.224 +  new MockPluginTag("test_bug449027_17", "5", false, false, false),
   1.225 +  new MockPluginTag("test_bug449027_18", "5", false, false, false),
   1.226 +  new MockPluginTag("test_bug449027_19", "5", false, true, true),
   1.227 +  new MockPluginTag("test_bug449027_20", "5", false, true, true),
   1.228 +  new MockPluginTag("test_bug449027_21", "5", false, true, true),
   1.229 +  new MockPluginTag("test_bug449027_22", "5", false, true, true),
   1.230 +  new MockPluginTag("test_bug449027_23", "5", false, true, true),
   1.231 +  new MockPluginTag("test_bug449027_24", "5", false, true, true),
   1.232 +  new MockPluginTag("test_bug449027_25", "5", false, true, true)
   1.233 +];
   1.234 +
   1.235 +var gCallback = null;
   1.236 +var gTestserver = null;
   1.237 +var gNewBlocks = [];
   1.238 +
   1.239 +// A fake plugin host for the blocklist service to use
   1.240 +var PluginHost = {
   1.241 +  getPluginTags: function(countRef) {
   1.242 +    countRef.value = PLUGINS.length;
   1.243 +    return PLUGINS;
   1.244 +  },
   1.245 +
   1.246 +  QueryInterface: function(iid) {
   1.247 +    if (iid.equals(Ci.nsIPluginHost)
   1.248 +     || iid.equals(Ci.nsISupports))
   1.249 +      return this;
   1.250 +
   1.251 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.252 +  }
   1.253 +}
   1.254 +
   1.255 +var PluginHostFactory = {
   1.256 +  createInstance: function (outer, iid) {
   1.257 +    if (outer != null)
   1.258 +      throw Components.results.NS_ERROR_NO_AGGREGATION;
   1.259 +    return PluginHost.QueryInterface(iid);
   1.260 +  }
   1.261 +};
   1.262 +
   1.263 +// Don't need the full interface, attempts to call other methods will just
   1.264 +// throw which is just fine
   1.265 +var WindowWatcher = {
   1.266 +  openWindow: function(parent, url, name, features, arguments) {
   1.267 +    // Should be called to list the newly blocklisted items
   1.268 +    do_check_eq(url, URI_EXTENSION_BLOCKLIST_DIALOG);
   1.269 +    do_check_neq(gCallback, null);
   1.270 +
   1.271 +    var args = arguments.wrappedJSObject;
   1.272 +
   1.273 +    gNewBlocks = [];
   1.274 +    var list = args.list;
   1.275 +    for (let listItem of list)
   1.276 +      gNewBlocks.push(listItem.name + " " + listItem.version);
   1.277 +
   1.278 +    // Call the callback after the blocklist has finished up
   1.279 +    do_timeout(0, gCallback);
   1.280 +  },
   1.281 +
   1.282 +  QueryInterface: function(iid) {
   1.283 +    if (iid.equals(Ci.nsIWindowWatcher)
   1.284 +     || iid.equals(Ci.nsISupports))
   1.285 +      return this;
   1.286 +
   1.287 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.288 +  }
   1.289 +}
   1.290 +
   1.291 +var WindowWatcherFactory = {
   1.292 +  createInstance: function createInstance(outer, iid) {
   1.293 +    if (outer != null)
   1.294 +      throw Components.results.NS_ERROR_NO_AGGREGATION;
   1.295 +    return WindowWatcher.QueryInterface(iid);
   1.296 +  }
   1.297 +};
   1.298 +var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
   1.299 +registrar.registerFactory(Components.ID("{721c3e73-969e-474b-a6dc-059fd288c428}"),
   1.300 +                          "Fake Plugin Host",
   1.301 +                          "@mozilla.org/plugin/host;1", PluginHostFactory);
   1.302 +registrar.registerFactory(Components.ID("{1dfeb90a-2193-45d5-9cb8-864928b2af55}"),
   1.303 +                          "Fake Window Watcher",
   1.304 +                          "@mozilla.org/embedcomp/window-watcher;1", WindowWatcherFactory);
   1.305 +
   1.306 +function create_addon(addon) {
   1.307 +  var installrdf = "<?xml version=\"1.0\"?>\n" +
   1.308 +                   "\n" +
   1.309 +                   "<RDF xmlns=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
   1.310 +                   "     xmlns:em=\"http://www.mozilla.org/2004/em-rdf#\">\n" +
   1.311 +                   "  <Description about=\"urn:mozilla:install-manifest\">\n" +
   1.312 +                   "    <em:id>" + addon.id + "</em:id>\n" +
   1.313 +                   "    <em:version>" + addon.version + "</em:version>\n" +
   1.314 +                   "    <em:targetApplication>\n" +
   1.315 +                   "      <Description>\n" +
   1.316 +                   "        <em:id>xpcshell@tests.mozilla.org</em:id>\n" +
   1.317 +                   "        <em:minVersion>3</em:minVersion>\n" +
   1.318 +                   "        <em:maxVersion>3</em:maxVersion>\n" +
   1.319 +                   "      </Description>\n" +
   1.320 +                   "    </em:targetApplication>\n" +
   1.321 +                   "    <em:name>" + addon.name + "</em:name>\n" +
   1.322 +                   "  </Description>\n" +
   1.323 +                   "</RDF>\n";
   1.324 +  var target = gProfD.clone();
   1.325 +  target.append("extensions");
   1.326 +  target.append(addon.id);
   1.327 +  target.append("install.rdf");
   1.328 +  target.create(target.NORMAL_FILE_TYPE, 0644);
   1.329 +  var stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
   1.330 +                         .createInstance(Ci.nsIFileOutputStream);
   1.331 +  stream.init(target, 0x04 | 0x08 | 0x20, 0664, 0); // write, create, truncate
   1.332 +  stream.write(installrdf, installrdf.length);
   1.333 +  stream.close();
   1.334 +}
   1.335 +
   1.336 +/**
   1.337 + * Checks that items are blocklisted correctly according to the current test.
   1.338 + * If a lastTest is provided checks that the notification dialog got passed
   1.339 + * the newly blocked items compared to the previous test.
   1.340 + */
   1.341 +function check_state(test, lastTest, callback) {
   1.342 +  AddonManager.getAddonsByIDs([a.id for each (a in ADDONS)], function(addons) {
   1.343 +    for (var i = 0; i < ADDONS.length; i++) {
   1.344 +      var blocked = addons[i].blocklistState == Ci.nsIBlocklistService.STATE_BLOCKED;
   1.345 +      if (blocked != ADDONS[i][test])
   1.346 +        do_throw("Blocklist state did not match expected for extension " + (i + 1) + ", test " + test);
   1.347 +    }
   1.348 +
   1.349 +    for (i = 0; i < PLUGINS.length; i++) {
   1.350 +      if (PLUGINS[i].blocklisted != PLUGINS[i][test])
   1.351 +        do_throw("Blocklist state did not match expected for plugin " + (i + 1) + ", test " + test);
   1.352 +    }
   1.353 +
   1.354 +    if (lastTest) {
   1.355 +      var expected = 0;
   1.356 +      for (i = 0; i < ADDONS.length; i++) {
   1.357 +        if (ADDONS[i][test] && !ADDONS[i][lastTest]) {
   1.358 +          if (gNewBlocks.indexOf(ADDONS[i].name + " " + ADDONS[i].version) < 0)
   1.359 +            do_throw("Addon " + (i + 1) + " should have been listed in the blocklist notification for test " + test);
   1.360 +          expected++;
   1.361 +        }
   1.362 +      }
   1.363 +
   1.364 +      for (i = 0; i < PLUGINS.length; i++) {
   1.365 +        if (PLUGINS[i][test] && !PLUGINS[i][lastTest]) {
   1.366 +          if (gNewBlocks.indexOf(PLUGINS[i].name + " " + PLUGINS[i].version) < 0)
   1.367 +            do_throw("Plugin " + (i + 1) + " should have been listed in the blocklist notification for test " + test);
   1.368 +          expected++;
   1.369 +        }
   1.370 +      }
   1.371 +
   1.372 +      do_check_eq(expected, gNewBlocks.length);
   1.373 +    }
   1.374 +    do_execute_soon(callback);
   1.375 +  });
   1.376 +}
   1.377 +
   1.378 +function load_blocklist(file) {
   1.379 +  Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/" + file);
   1.380 +  var blocklist = Components.classes["@mozilla.org/extensions/blocklist;1"]
   1.381 +                            .getService(Ci.nsITimerCallback);
   1.382 +  blocklist.notify(null);
   1.383 +}
   1.384 +
   1.385 +function run_test() {
   1.386 +  // Setup for test
   1.387 +  dump("Setting up tests\n");
   1.388 +  // Rather than keeping lots of identical add-ons in version control, just
   1.389 +  // write them into the profile.
   1.390 +  for (let addon of ADDONS)
   1.391 +    create_addon(addon);
   1.392 +
   1.393 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "3", "8");
   1.394 +  startupManager();
   1.395 +
   1.396 +  gTestserver = new HttpServer();
   1.397 +  gTestserver.registerDirectory("/data/", do_get_file("data"));
   1.398 +  gTestserver.start(-1);
   1.399 +  gPort = gTestserver.identity.primaryPort;
   1.400 +
   1.401 +  do_test_pending();
   1.402 +  check_test_pt1();
   1.403 +}
   1.404 +
   1.405 +/**
   1.406 + * Checks the initial state is correct
   1.407 + */
   1.408 +function check_test_pt1() {
   1.409 +  dump("Checking pt 1\n");
   1.410 +
   1.411 +  AddonManager.getAddonsByIDs([a.id for each (a in ADDONS)], function(addons) {
   1.412 +    for (var i = 0; i < ADDONS.length; i++) {
   1.413 +      if (!addons[i])
   1.414 +        do_throw("Addon " + (i + 1) + " did not get installed correctly");
   1.415 +    }
   1.416 +
   1.417 +    do_execute_soon(function checkstate1() {check_state("start", null, run_test_pt2);});
   1.418 +  });
   1.419 +}
   1.420 +
   1.421 +/**
   1.422 + * Load the toolkit based blocks
   1.423 + */
   1.424 +function run_test_pt2() {
   1.425 +  dump("Running test pt 2\n");
   1.426 +  gCallback = check_test_pt2;
   1.427 +  load_blocklist("test_bug449027_toolkit.xml");
   1.428 +}
   1.429 +
   1.430 +function check_test_pt2() {
   1.431 +  dump("Checking pt 2\n");
   1.432 +  check_state("toolkitBlocks", "start", run_test_pt3);
   1.433 +}
   1.434 +
   1.435 +/**
   1.436 + * Load the application based blocks
   1.437 + */
   1.438 +function run_test_pt3() {
   1.439 +  dump("Running test pt 3\n");
   1.440 +  gCallback = check_test_pt3;
   1.441 +  load_blocklist("test_bug449027_app.xml");
   1.442 +}
   1.443 +
   1.444 +function check_test_pt3() {
   1.445 +  dump("Checking pt 3\n");
   1.446 +  check_state("appBlocks", "toolkitBlocks", end_test);
   1.447 +}
   1.448 +
   1.449 +function end_test() {
   1.450 +  gTestserver.stop(do_test_finished);
   1.451 +}

mercurial