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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 3 */
michael@0 4
michael@0 5 // This verifies that add-ons URIs can be mapped to add-on IDs
michael@0 6 //
michael@0 7 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 8
michael@0 9 // Enable loading extensions from the user scopes
michael@0 10 Services.prefs.setIntPref("extensions.enabledScopes",
michael@0 11 AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_USER);
michael@0 12
michael@0 13 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
michael@0 14
michael@0 15 const profileDir = gProfD.clone();
michael@0 16 profileDir.append("extensions");
michael@0 17 const userExtDir = gProfD.clone();
michael@0 18 userExtDir.append("extensions2");
michael@0 19 userExtDir.append(gAppInfo.ID);
michael@0 20 registerDirectory("XREUSysExt", userExtDir.parent);
michael@0 21
michael@0 22 function TestProvider(result) {
michael@0 23 this.result = result;
michael@0 24 }
michael@0 25 TestProvider.prototype = {
michael@0 26 uri: Services.io.newURI("hellow://world", null, null),
michael@0 27 id: "valid@id",
michael@0 28 startup: function() {},
michael@0 29 shutdown: function() {},
michael@0 30 mapURIToAddonID: function(aURI) {
michael@0 31 if (aURI.spec === this.uri.spec) {
michael@0 32 return this.id;
michael@0 33 }
michael@0 34 throw Components.Exception("Not mapped", result);
michael@0 35 }
michael@0 36 };
michael@0 37
michael@0 38 function TestProviderNoMap() {}
michael@0 39 TestProviderNoMap.prototype = {
michael@0 40 startup: function() {},
michael@0 41 shutdown: function() {}
michael@0 42 };
michael@0 43
michael@0 44 function check_mapping(uri, id) {
michael@0 45 do_check_eq(AddonManager.mapURIToAddonID(uri), id);
michael@0 46 let svc = Components.classes["@mozilla.org/addons/integration;1"].
michael@0 47 getService(Components.interfaces.amIAddonManager);
michael@0 48 let val = {};
michael@0 49 do_check_true(svc.mapURIToAddonID(uri, val));
michael@0 50 do_check_eq(val.value, id);
michael@0 51 }
michael@0 52
michael@0 53 function resetPrefs() {
michael@0 54 Services.prefs.setIntPref("bootstraptest.active_version", -1);
michael@0 55 }
michael@0 56
michael@0 57 function waitForPref(aPref, aCallback) {
michael@0 58 function prefChanged() {
michael@0 59 Services.prefs.removeObserver(aPref, prefChanged);
michael@0 60 aCallback();
michael@0 61 }
michael@0 62 Services.prefs.addObserver(aPref, prefChanged, false);
michael@0 63 }
michael@0 64
michael@0 65 function getActiveVersion() {
michael@0 66 return Services.prefs.getIntPref("bootstraptest.active_version");
michael@0 67 }
michael@0 68
michael@0 69 function run_test() {
michael@0 70 do_test_pending();
michael@0 71
michael@0 72 resetPrefs();
michael@0 73
michael@0 74 run_test_early();
michael@0 75 }
michael@0 76
michael@0 77 function run_test_early() {
michael@0 78 startupManager();
michael@0 79
michael@0 80 installAllFiles([do_get_addon("test_chromemanifest_1")], function() {
michael@0 81 restartManager();
michael@0 82 AddonManager.getAddonByID("addon1@tests.mozilla.org", function(addon) {
michael@0 83 let uri = addon.getResourceURI(".");
michael@0 84 let id = addon.id;
michael@0 85 check_mapping(uri, id);
michael@0 86
michael@0 87 shutdownManager();
michael@0 88
michael@0 89 // Force an early call, to check that mappings will get correctly
michael@0 90 // initialized when the manager actually starts up.
michael@0 91 // See bug 957089
michael@0 92
michael@0 93 // First force-initialize the XPIProvider.
michael@0 94 let s = Components.utils.import(
michael@0 95 "resource://gre/modules/addons/XPIProvider.jsm", {});
michael@0 96
michael@0 97 // Make the early API call.
michael@0 98 do_check_null(s.XPIProvider.mapURIToAddonID(uri));
michael@0 99 do_check_null(AddonManager.mapURIToAddonID(uri));
michael@0 100
michael@0 101 // Actually start up the manager.
michael@0 102 startupManager(false);
michael@0 103
michael@0 104 // Check that the mapping is there now.
michael@0 105 check_mapping(uri, id);
michael@0 106 do_check_eq(s.XPIProvider.mapURIToAddonID(uri), id);
michael@0 107
michael@0 108 run_test_nomapping();
michael@0 109 });
michael@0 110 });
michael@0 111 }
michael@0 112
michael@0 113 function run_test_nomapping() {
michael@0 114 do_check_eq(AddonManager.mapURIToAddonID(TestProvider.prototype.uri), null);
michael@0 115 try {
michael@0 116 let svc = Components.classes["@mozilla.org/addons/integration;1"].
michael@0 117 getService(Components.interfaces.amIAddonManager);
michael@0 118 let val = {};
michael@0 119 do_check_false(svc.mapURIToAddonID(TestProvider.prototype.uri, val));
michael@0 120 }
michael@0 121 catch (ex) {
michael@0 122 do_throw(ex);
michael@0 123 }
michael@0 124
michael@0 125 run_test_1();
michael@0 126 }
michael@0 127
michael@0 128
michael@0 129 // Tests that add-on URIs are mappable after an install
michael@0 130 function run_test_1() {
michael@0 131 prepare_test({ }, [
michael@0 132 "onNewInstall"
michael@0 133 ]);
michael@0 134
michael@0 135 AddonManager.getInstallForFile(do_get_addon("test_bootstrap1_1"), function(install) {
michael@0 136 ensure_test_completed();
michael@0 137
michael@0 138 let addon = install.addon;
michael@0 139 prepare_test({
michael@0 140 "bootstrap1@tests.mozilla.org": [
michael@0 141 ["onInstalling", false],
michael@0 142 "onInstalled"
michael@0 143 ]
michael@0 144 }, [
michael@0 145 "onInstallStarted",
michael@0 146 "onInstallEnded",
michael@0 147 ], function() {
michael@0 148 let uri = addon.getResourceURI(".");
michael@0 149 check_mapping(uri, addon.id);
michael@0 150
michael@0 151 waitForPref("bootstraptest.active_version", function() {
michael@0 152 run_test_2(uri);
michael@0 153 });
michael@0 154 });
michael@0 155 install.install();
michael@0 156 });
michael@0 157 }
michael@0 158
michael@0 159 // Tests that add-on URIs are still mappable, even after the add-on gets
michael@0 160 // disabled in-session.
michael@0 161 function run_test_2(uri) {
michael@0 162 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(b1) {
michael@0 163 prepare_test({
michael@0 164 "bootstrap1@tests.mozilla.org": [
michael@0 165 ["onDisabling", false],
michael@0 166 "onDisabled"
michael@0 167 ]
michael@0 168 });
michael@0 169
michael@0 170 b1.userDisabled = true;
michael@0 171 ensure_test_completed();
michael@0 172
michael@0 173 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(newb1) {
michael@0 174 do_check_true(newb1.userDisabled);
michael@0 175 check_mapping(uri, newb1.id);
michael@0 176
michael@0 177 do_execute_soon(() => run_test_3(uri));
michael@0 178 });
michael@0 179 });
michael@0 180 }
michael@0 181
michael@0 182 // Tests that add-on URIs aren't mappable if the add-on was never started in a
michael@0 183 // session
michael@0 184 function run_test_3(uri) {
michael@0 185 restartManager();
michael@0 186
michael@0 187 do_check_eq(AddonManager.mapURIToAddonID(uri), null);
michael@0 188
michael@0 189 run_test_4();
michael@0 190 }
michael@0 191
michael@0 192 // Tests that add-on URIs are mappable after a restart + reenable
michael@0 193 function run_test_4() {
michael@0 194 restartManager();
michael@0 195
michael@0 196 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(b1) {
michael@0 197 prepare_test({
michael@0 198 "bootstrap1@tests.mozilla.org": [
michael@0 199 ["onEnabling", false],
michael@0 200 "onEnabled"
michael@0 201 ]
michael@0 202 });
michael@0 203
michael@0 204 b1.userDisabled = false;
michael@0 205 ensure_test_completed();
michael@0 206
michael@0 207 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(newb1) {
michael@0 208 let uri = newb1.getResourceURI(".");
michael@0 209 check_mapping(uri, newb1.id);
michael@0 210
michael@0 211 do_execute_soon(run_test_5);
michael@0 212 });
michael@0 213 });
michael@0 214 }
michael@0 215
michael@0 216 // Tests that add-on URIs are mappable after a restart
michael@0 217 function run_test_5() {
michael@0 218 restartManager();
michael@0 219
michael@0 220 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(b1) {
michael@0 221 let uri = b1.getResourceURI(".");
michael@0 222 check_mapping(uri, b1.id);
michael@0 223
michael@0 224 do_execute_soon(run_test_invalidarg);
michael@0 225 });
michael@0 226 }
michael@0 227
michael@0 228 // Tests that the AddonManager will bail when mapURIToAddonID is called with an
michael@0 229 // invalid argument
michael@0 230 function run_test_invalidarg() {
michael@0 231 restartManager();
michael@0 232
michael@0 233 let tests = [undefined,
michael@0 234 null,
michael@0 235 1,
michael@0 236 "string",
michael@0 237 "chrome://global/content/",
michael@0 238 function() {}
michael@0 239 ];
michael@0 240 for (var test of tests) {
michael@0 241 try {
michael@0 242 AddonManager.mapURIToAddonID(test);
michael@0 243 throw new Error("Shouldn't be able to map the URI in question");
michael@0 244 }
michael@0 245 catch (ex if ex.result) {
michael@0 246 do_check_eq(ex.result, Components.results.NS_ERROR_INVALID_ARG);
michael@0 247 }
michael@0 248 catch (ex) {
michael@0 249 do_throw(ex);
michael@0 250 }
michael@0 251 }
michael@0 252
michael@0 253 run_test_provider();
michael@0 254 }
michael@0 255
michael@0 256 // Tests that custom providers are correctly handled
michael@0 257 function run_test_provider() {
michael@0 258 restartManager();
michael@0 259
michael@0 260 const provider = new TestProvider(Components.results.NS_ERROR_NOT_AVAILABLE);
michael@0 261 AddonManagerPrivate.registerProvider(provider);
michael@0 262
michael@0 263 check_mapping(provider.uri, provider.id);
michael@0 264
michael@0 265 let u2 = provider.uri.clone();
michael@0 266 u2.path = "notmapped";
michael@0 267 do_check_eq(AddonManager.mapURIToAddonID(u2), null);
michael@0 268
michael@0 269 AddonManagerPrivate.unregisterProvider(provider);
michael@0 270
michael@0 271 run_test_provider_nomap();
michael@0 272 }
michael@0 273
michael@0 274 // Tests that custom providers are correctly handled, even not implementing
michael@0 275 // mapURIToAddonID
michael@0 276 function run_test_provider_nomap() {
michael@0 277 restartManager();
michael@0 278
michael@0 279 const provider = new TestProviderNoMap();
michael@0 280 AddonManagerPrivate.registerProvider(provider);
michael@0 281
michael@0 282 do_check_eq(AddonManager.mapURIToAddonID(TestProvider.prototype.uri), null);
michael@0 283
michael@0 284 AddonManagerPrivate.unregisterProvider(provider);
michael@0 285
michael@0 286 do_test_finished();
michael@0 287 }
michael@0 288
michael@0 289

mercurial