toolkit/mozapps/extensions/test/xpcshell/test_bug335238.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_bug335238.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,181 @@
     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 +
     1.9 +const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
    1.10 +const PREF_SELECTED_LOCALE = "general.useragent.locale";
    1.11 +
    1.12 +// Disables security checking our updates which haven't been signed
    1.13 +Services.prefs.setBoolPref("extensions.checkUpdateSecurity", false);
    1.14 +
    1.15 +const Ci = Components.interfaces;
    1.16 +const Cu = Components.utils;
    1.17 +
    1.18 +Cu.import("resource://testing-common/httpd.js");
    1.19 +
    1.20 +// This is the data we expect to see sent as part of the update url.
    1.21 +var EXPECTED = [
    1.22 +  {
    1.23 +    id: "bug335238_1@tests.mozilla.org",
    1.24 +    version: "1.3.4",
    1.25 +    maxAppVersion: "5",
    1.26 +    status: "userEnabled",
    1.27 +    appId: "xpcshell@tests.mozilla.org",
    1.28 +    appVersion: "1",
    1.29 +    appOs: "XPCShell",
    1.30 +    appAbi: "noarch-spidermonkey",
    1.31 +    locale: "en-US",
    1.32 +    reqVersion: "2"
    1.33 +  },
    1.34 +  {
    1.35 +    id: "bug335238_2@tests.mozilla.org",
    1.36 +    version: "28at",
    1.37 +    maxAppVersion: "7",
    1.38 +    status: "userDisabled",
    1.39 +    appId: "xpcshell@tests.mozilla.org",
    1.40 +    appVersion: "1",
    1.41 +    appOs: "XPCShell",
    1.42 +    appAbi: "noarch-spidermonkey",
    1.43 +    locale: "en-US",
    1.44 +    reqVersion: "2"
    1.45 +  },
    1.46 +  {
    1.47 +    id: "bug335238_3@tests.mozilla.org",
    1.48 +    version: "58",
    1.49 +    maxAppVersion: "*",
    1.50 +    status: "userDisabled,softblocked",
    1.51 +    appId: "xpcshell@tests.mozilla.org",
    1.52 +    appVersion: "1",
    1.53 +    appOs: "XPCShell",
    1.54 +    appAbi: "noarch-spidermonkey",
    1.55 +    locale: "en-US",
    1.56 +    reqVersion: "2"
    1.57 +  },
    1.58 +  {
    1.59 +    id: "bug335238_4@tests.mozilla.org",
    1.60 +    version: "4",
    1.61 +    maxAppVersion: "2+",
    1.62 +    status: "userEnabled,blocklisted",
    1.63 +    appId: "xpcshell@tests.mozilla.org",
    1.64 +    appVersion: "1",
    1.65 +    appOs: "XPCShell",
    1.66 +    appAbi: "noarch-spidermonkey",
    1.67 +    locale: "en-US",
    1.68 +    reqVersion: "2"
    1.69 +  }
    1.70 +];
    1.71 +
    1.72 +var ADDONS = [
    1.73 +  {id: "bug335238_1@tests.mozilla.org",
    1.74 +   addon: "test_bug335238_1"},
    1.75 +  {id: "bug335238_2@tests.mozilla.org",
    1.76 +   addon: "test_bug335238_2"},
    1.77 +  {id: "bug335238_3@tests.mozilla.org",
    1.78 +   addon: "test_bug335238_3"},
    1.79 +  {id: "bug335238_4@tests.mozilla.org",
    1.80 +   addon: "test_bug335238_4"}
    1.81 +];
    1.82 +
    1.83 +// This is a replacement for the blocklist service
    1.84 +var BlocklistService = {
    1.85 +  getAddonBlocklistState: function(aAddon, aAppVersion, aToolkitVersion) {
    1.86 +    if (aAddon.id == "bug335238_3@tests.mozilla.org")
    1.87 +      return Ci.nsIBlocklistService.STATE_SOFTBLOCKED;
    1.88 +    if (aAddon.id == "bug335238_4@tests.mozilla.org")
    1.89 +      return Ci.nsIBlocklistService.STATE_BLOCKED;
    1.90 +    return Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
    1.91 +  },
    1.92 +
    1.93 +  getPluginBlocklistState: function(aPlugin, aVersion, aAppVersion, aToolkitVersion) {
    1.94 +    return Ci.nsIBlocklistService.STATE_NOT_BLOCKED;
    1.95 +  },
    1.96 +
    1.97 +  isAddonBlocklisted: function(aAddon, aAppVersion, aToolkitVersion) {
    1.98 +    return this.getAddonBlocklistState(aAddon, aAppVersion, aToolkitVersion) ==
    1.99 +           Ci.nsIBlocklistService.STATE_BLOCKED;
   1.100 +  },
   1.101 +
   1.102 +  QueryInterface: function(iid) {
   1.103 +    if (iid.equals(Ci.nsIBlocklistService)
   1.104 +     || iid.equals(Ci.nsISupports))
   1.105 +      return this;
   1.106 +
   1.107 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.108 +  }
   1.109 +};
   1.110 +
   1.111 +var BlocklistServiceFactory = {
   1.112 +  createInstance: function (outer, iid) {
   1.113 +    if (outer != null)
   1.114 +      throw Components.results.NS_ERROR_NO_AGGREGATION;
   1.115 +    return BlocklistService.QueryInterface(iid);
   1.116 +  }
   1.117 +};
   1.118 +var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
   1.119 +registrar.registerFactory(Components.ID("{61189e7a-6b1b-44b8-ac81-f180a6105085}"), "BlocklistService",
   1.120 +                          "@mozilla.org/extensions/blocklist;1", BlocklistServiceFactory);
   1.121 +
   1.122 +var server;
   1.123 +
   1.124 +var updateListener = {
   1.125 +  pendingCount: 0,
   1.126 +
   1.127 +  onUpdateAvailable: function(aAddon) {
   1.128 +    do_throw("Should not have seen an update for " + aAddon.id);
   1.129 +  },
   1.130 +
   1.131 +  onUpdateFinished: function() {
   1.132 +    if (--this.pendingCount == 0)
   1.133 +      server.stop(do_test_finished);
   1.134 +  }
   1.135 +}
   1.136 +
   1.137 +var requestHandler = {
   1.138 +  handle: function(metadata, response)
   1.139 +  {
   1.140 +    var expected = EXPECTED[metadata.path.substring(1)];
   1.141 +    var params = metadata.queryString.split("&");
   1.142 +    do_check_eq(params.length, 10);
   1.143 +    for (var k in params) {
   1.144 +      var pair = params[k].split("=");
   1.145 +      var name = decodeURIComponent(pair[0]);
   1.146 +      var value = decodeURIComponent(pair[1]);
   1.147 +      do_check_eq(expected[name], value);
   1.148 +    }
   1.149 +    response.setStatusLine(metadata.httpVersion, 404, "Not Found");
   1.150 +  }
   1.151 +}
   1.152 +
   1.153 +function run_test() {
   1.154 +  do_test_pending();
   1.155 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
   1.156 +
   1.157 +  server = new HttpServer();
   1.158 +  server.registerPathHandler("/0", requestHandler);
   1.159 +  server.registerPathHandler("/1", requestHandler);
   1.160 +  server.registerPathHandler("/2", requestHandler);
   1.161 +  server.registerPathHandler("/3", requestHandler);
   1.162 +  server.start(4444);
   1.163 +
   1.164 +  Services.prefs.setBoolPref(PREF_MATCH_OS_LOCALE, false);
   1.165 +  Services.prefs.setCharPref(PREF_SELECTED_LOCALE, "en-US");
   1.166 +
   1.167 +  startupManager();
   1.168 +  installAllFiles([do_get_addon(a.addon) for each (a in ADDONS)], function() {
   1.169 +
   1.170 +    restartManager();
   1.171 +    AddonManager.getAddonByID(ADDONS[1].id, callback_soon(function(addon) {
   1.172 +      do_check_true(!(!addon));
   1.173 +      addon.userDisabled = true;
   1.174 +      restartManager();
   1.175 +
   1.176 +      AddonManager.getAddonsByIDs([a.id for each (a in ADDONS)], function(installedItems) {
   1.177 +        installedItems.forEach(function(item) {
   1.178 +          updateListener.pendingCount++;
   1.179 +          item.findUpdates(updateListener, AddonManager.UPDATE_WHEN_USER_REQUESTED);
   1.180 +        });
   1.181 +      });
   1.182 +    }));
   1.183 +  });
   1.184 +}

mercurial