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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:4900f76a4364
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 */
5
6 const BLOCKLIST_TIMER = "blocklist-background-update-timer";
7 const PREF_BLOCKLIST_URL = "extensions.blocklist.url";
8 const PREF_BLOCKLIST_ENABLED = "extensions.blocklist.enabled";
9 const PREF_APP_DISTRIBUTION = "distribution.id";
10 const PREF_APP_DISTRIBUTION_VERSION = "distribution.version";
11 const PREF_APP_UPDATE_CHANNEL = "app.update.channel";
12 const PREF_GENERAL_USERAGENT_LOCALE = "general.useragent.locale";
13 const CATEGORY_UPDATE_TIMER = "update-timer";
14
15 // Get the HTTP server.
16 Components.utils.import("resource://testing-common/httpd.js");
17 var testserver;
18 var gOSVersion;
19 var gBlocklist;
20
21 // This is a replacement for the timer service so we can trigger timers
22 var timerService = {
23
24 hasTimer: function(id) {
25 var catMan = Components.classes["@mozilla.org/categorymanager;1"]
26 .getService(Components.interfaces.nsICategoryManager);
27 var entries = catMan.enumerateCategory(CATEGORY_UPDATE_TIMER);
28 while (entries.hasMoreElements()) {
29 var entry = entries.getNext().QueryInterface(Components.interfaces.nsISupportsCString).data;
30 var value = catMan.getCategoryEntry(CATEGORY_UPDATE_TIMER, entry);
31 var timerID = value.split(",")[2];
32 if (id == timerID) {
33 return true;
34 }
35 }
36 return false;
37 },
38
39 fireTimer: function(id) {
40 gBlocklist.QueryInterface(Components.interfaces.nsITimerCallback).notify(null);
41 },
42
43 QueryInterface: function(iid) {
44 if (iid.equals(Components.interfaces.nsIUpdateTimerManager)
45 || iid.equals(Components.interfaces.nsISupports))
46 return this;
47
48 throw Components.results.NS_ERROR_NO_INTERFACE;
49 }
50 };
51
52 var TimerServiceFactory = {
53 createInstance: function (outer, iid) {
54 if (outer != null)
55 throw Components.results.NS_ERROR_NO_AGGREGATION;
56 return timerService.QueryInterface(iid);
57 }
58 };
59 var registrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
60 registrar.registerFactory(Components.ID("{61189e7a-6b1b-44b8-ac81-f180a6105085}"), "TimerService",
61 "@mozilla.org/updates/timer-manager;1", TimerServiceFactory);
62
63 function failHandler(metadata, response) {
64 do_throw("Should not have attempted to retrieve the blocklist when it is disabled");
65 }
66
67 function pathHandler(metadata, response) {
68 var ABI = "noarch-spidermonkey";
69 // the blacklist service special-cases ABI for Universal binaries,
70 // so do the same here.
71 if ("@mozilla.org/xpcom/mac-utils;1" in Components.classes) {
72 var macutils = Components.classes["@mozilla.org/xpcom/mac-utils;1"]
73 .getService(Components.interfaces.nsIMacUtils);
74 if (macutils.isUniversalBinary)
75 ABI += "-u-" + macutils.architecturesInBinary;
76 }
77 do_check_eq(metadata.queryString,
78 "xpcshell@tests.mozilla.org&1&XPCShell&1&2007010101&" +
79 "XPCShell_" + ABI + "&locale&updatechannel&" +
80 gOSVersion + "&1.9&distribution&distribution-version");
81 gBlocklist.observe(null, "quit-application", "");
82 gBlocklist.observe(null, "xpcom-shutdown", "");
83 testserver.stop(do_test_finished);
84 }
85
86 function run_test() {
87 var osVersion;
88 var sysInfo = Components.classes["@mozilla.org/system-info;1"]
89 .getService(Components.interfaces.nsIPropertyBag2);
90 try {
91 osVersion = sysInfo.getProperty("name") + " " + sysInfo.getProperty("version");
92 if (osVersion) {
93 try {
94 osVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
95 }
96 catch (e) {
97 }
98 gOSVersion = encodeURIComponent(osVersion);
99 }
100 }
101 catch (e) {
102 }
103
104 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
105
106 testserver = new HttpServer();
107 testserver.registerPathHandler("/1", failHandler);
108 testserver.registerPathHandler("/2", pathHandler);
109 testserver.start(-1);
110 gPort = testserver.identity.primaryPort;
111
112 // Initialise the blocklist service
113 gBlocklist = Components.classes["@mozilla.org/extensions/blocklist;1"]
114 .getService(Components.interfaces.nsIBlocklistService)
115 .QueryInterface(Components.interfaces.nsIObserver);
116 gBlocklist.observe(null, "profile-after-change", "");
117
118 do_check_true(timerService.hasTimer(BLOCKLIST_TIMER));
119
120 do_test_pending();
121
122 // This should have no effect as the blocklist is disabled
123 Services.prefs.setCharPref(PREF_BLOCKLIST_URL, "http://localhost:" + gPort + "/1");
124 Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, false);
125 timerService.fireTimer(BLOCKLIST_TIMER);
126
127 // Some values have to be on the default branch to work
128 var defaults = Services.prefs.QueryInterface(Components.interfaces.nsIPrefService)
129 .getDefaultBranch(null);
130 defaults.setCharPref(PREF_APP_UPDATE_CHANNEL, "updatechannel");
131 defaults.setCharPref(PREF_APP_DISTRIBUTION, "distribution");
132 defaults.setCharPref(PREF_APP_DISTRIBUTION_VERSION, "distribution-version");
133 defaults.setCharPref(PREF_GENERAL_USERAGENT_LOCALE, "locale");
134
135 // This should correctly escape everything
136 Services.prefs.setCharPref(PREF_BLOCKLIST_URL, "http://localhost:" + gPort + "/2?" +
137 "%APP_ID%&%APP_VERSION%&%PRODUCT%&%VERSION%&%BUILD_ID%&" +
138 "%BUILD_TARGET%&%LOCALE%&%CHANNEL%&" +
139 "%OS_VERSION%&%PLATFORM_VERSION%&%DISTRIBUTION%&%DISTRIBUTION_VERSION%");
140 Services.prefs.setBoolPref(PREF_BLOCKLIST_ENABLED, true);
141 timerService.fireTimer(BLOCKLIST_TIMER);
142 }

mercurial