|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 "use strict"; |
|
5 |
|
6 const {utils: Cu, classes: Cc, interfaces: Ci} = Components; |
|
7 |
|
8 |
|
9 Cu.import("resource://gre/modules/Metrics.jsm"); |
|
10 Cu.import("resource://gre/modules/services/healthreport/providers.jsm"); |
|
11 |
|
12 // The hack, it burns. This could go away if extensions code exposed its |
|
13 // test environment setup functions as a testing-only JSM. See similar |
|
14 // code in Sync's head_helpers.js. |
|
15 let gGlobalScope = this; |
|
16 function loadAddonManager() { |
|
17 let ns = {}; |
|
18 Cu.import("resource://gre/modules/Services.jsm", ns); |
|
19 let head = "../../../../toolkit/mozapps/extensions/test/xpcshell/head_addons.js"; |
|
20 let file = do_get_file(head); |
|
21 let uri = ns.Services.io.newFileURI(file); |
|
22 ns.Services.scriptloader.loadSubScript(uri.spec, gGlobalScope); |
|
23 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); |
|
24 startupManager(); |
|
25 } |
|
26 |
|
27 function run_test() { |
|
28 loadAddonManager(); |
|
29 run_next_test(); |
|
30 } |
|
31 |
|
32 add_test(function test_constructor() { |
|
33 let provider = new AddonsProvider(); |
|
34 |
|
35 run_next_test(); |
|
36 }); |
|
37 |
|
38 add_task(function test_init() { |
|
39 let storage = yield Metrics.Storage("init"); |
|
40 let provider = new AddonsProvider(); |
|
41 yield provider.init(storage); |
|
42 yield provider.shutdown(); |
|
43 |
|
44 yield storage.close(); |
|
45 }); |
|
46 |
|
47 function monkeypatchAddons(provider, addons) { |
|
48 if (!Array.isArray(addons)) { |
|
49 throw new Error("Must define array of addon objects."); |
|
50 } |
|
51 |
|
52 Object.defineProperty(provider, "_createDataStructure", { |
|
53 value: function _createDataStructure() { |
|
54 return AddonsProvider.prototype._createDataStructure.call(provider, addons); |
|
55 }, |
|
56 }); |
|
57 } |
|
58 |
|
59 add_task(function test_collect() { |
|
60 let storage = yield Metrics.Storage("collect"); |
|
61 let provider = new AddonsProvider(); |
|
62 yield provider.init(storage); |
|
63 |
|
64 let now = new Date(); |
|
65 |
|
66 // FUTURE install add-on via AddonManager and don't use monkeypatching. |
|
67 let testAddons = [ |
|
68 { |
|
69 id: "addon0", |
|
70 userDisabled: false, |
|
71 appDisabled: false, |
|
72 version: "1", |
|
73 type: "extension", |
|
74 scope: 1, |
|
75 foreignInstall: false, |
|
76 hasBinaryComponents: false, |
|
77 installDate: now, |
|
78 updateDate: now, |
|
79 }, |
|
80 // This plugin entry should get ignored. |
|
81 { |
|
82 id: "addon1", |
|
83 userDisabled: false, |
|
84 appDisabled: false, |
|
85 version: "2", |
|
86 type: "plugin", |
|
87 scope: 1, |
|
88 foreignInstall: false, |
|
89 hasBinaryComponents: false, |
|
90 installDate: now, |
|
91 updateDate: now, |
|
92 }, |
|
93 // Is counted but full details are omitted because it is a theme. |
|
94 { |
|
95 id: "addon2", |
|
96 userDisabled: false, |
|
97 appDisabled: false, |
|
98 version: "3", |
|
99 type: "theme", |
|
100 scope: 1, |
|
101 foreignInstall: false, |
|
102 hasBinaryComponents: false, |
|
103 installDate: now, |
|
104 updateDate: now, |
|
105 }, |
|
106 { |
|
107 id: "addon3", |
|
108 userDisabled: false, |
|
109 appDisabled: false, |
|
110 version: "4", |
|
111 type: "service", |
|
112 scope: 1, |
|
113 foreignInstall: false, |
|
114 hasBinaryComponents: false, |
|
115 installDate: now, |
|
116 updateDate: now, |
|
117 description: "addon3 description" |
|
118 }, |
|
119 ]; |
|
120 |
|
121 monkeypatchAddons(provider, testAddons); |
|
122 |
|
123 let testPlugins = { |
|
124 "Test Plug-in": |
|
125 { |
|
126 "version": "1.0.0.0", |
|
127 "description": "Plug-in for testing purposes.™ (हिन्दी 中文 العربية)", |
|
128 "blocklisted": false, |
|
129 "disabled": false, |
|
130 "clicktoplay": false, |
|
131 "mimeTypes":[ |
|
132 "application/x-test" |
|
133 ], |
|
134 }, |
|
135 "Second Test Plug-in": |
|
136 { |
|
137 "version": "1.0.0.0", |
|
138 "description": "Second plug-in for testing purposes.", |
|
139 "blocklisted": false, |
|
140 "disabled": false, |
|
141 "clicktoplay": false, |
|
142 "mimeTypes":[ |
|
143 "application/x-second-test" |
|
144 ], |
|
145 }, |
|
146 "Java Test Plug-in": |
|
147 { |
|
148 "version": "1.0.0.0", |
|
149 "description": "Dummy Java plug-in for testing purposes.", |
|
150 "blocklisted": false, |
|
151 "disabled": false, |
|
152 "clicktoplay": false, |
|
153 "mimeTypes":[ |
|
154 "application/x-java-test" |
|
155 ], |
|
156 }, |
|
157 }; |
|
158 |
|
159 let pluginTags = Cc["@mozilla.org/plugin/host;1"] |
|
160 .getService(Ci.nsIPluginHost) |
|
161 .getPluginTags({}); |
|
162 |
|
163 for (let tag of pluginTags) { |
|
164 if (tag.name in testPlugins) { |
|
165 let p = testPlugins[tag.name]; |
|
166 p.id = tag.filename+":"+tag.name+":"+p.version+":"+p.description; |
|
167 } |
|
168 } |
|
169 |
|
170 yield provider.collectConstantData(); |
|
171 |
|
172 // Test addons measurement. |
|
173 |
|
174 let addons = provider.getMeasurement("addons", 2); |
|
175 let data = yield addons.getValues(); |
|
176 |
|
177 do_check_eq(data.days.size, 0); |
|
178 do_check_eq(data.singular.size, 1); |
|
179 do_check_true(data.singular.has("addons")); |
|
180 |
|
181 let json = data.singular.get("addons")[1]; |
|
182 let value = JSON.parse(json); |
|
183 do_check_eq(typeof(value), "object"); |
|
184 do_check_eq(Object.keys(value).length, 2); |
|
185 do_check_true("addon0" in value); |
|
186 do_check_true(!("addon1" in value)); |
|
187 do_check_true(!("addon2" in value)); |
|
188 do_check_true("addon3" in value); |
|
189 |
|
190 let serializer = addons.serializer(addons.SERIALIZE_JSON); |
|
191 let serialized = serializer.singular(data.singular); |
|
192 do_check_eq(typeof(serialized), "object"); |
|
193 do_check_eq(Object.keys(serialized).length, 3); // Our entries, plus _v. |
|
194 do_check_true("addon0" in serialized); |
|
195 do_check_true("addon3" in serialized); |
|
196 do_check_eq(serialized._v, 2); |
|
197 |
|
198 // Test plugins measurement. |
|
199 |
|
200 let plugins = provider.getMeasurement("plugins", 1); |
|
201 data = yield plugins.getValues(); |
|
202 |
|
203 do_check_eq(data.days.size, 0); |
|
204 do_check_eq(data.singular.size, 1); |
|
205 do_check_true(data.singular.has("plugins")); |
|
206 |
|
207 json = data.singular.get("plugins")[1]; |
|
208 value = JSON.parse(json); |
|
209 do_check_eq(typeof(value), "object"); |
|
210 do_check_eq(Object.keys(value).length, pluginTags.length); |
|
211 |
|
212 do_check_true(testPlugins["Test Plug-in"].id in value); |
|
213 do_check_true(testPlugins["Second Test Plug-in"].id in value); |
|
214 do_check_true(testPlugins["Java Test Plug-in"].id in value); |
|
215 |
|
216 for (let id in value) { |
|
217 let item = value[id]; |
|
218 let testData = testPlugins[item.name]; |
|
219 for (let prop in testData) { |
|
220 if (prop == "mimeTypes" || prop == "id") { |
|
221 continue; |
|
222 } |
|
223 do_check_eq(testData[prop], item[prop]); |
|
224 } |
|
225 |
|
226 for (let mime of testData.mimeTypes) { |
|
227 do_check_true(item.mimeTypes.indexOf(mime) != -1); |
|
228 } |
|
229 } |
|
230 |
|
231 serializer = plugins.serializer(plugins.SERIALIZE_JSON); |
|
232 serialized = serializer.singular(data.singular); |
|
233 do_check_eq(typeof(serialized), "object"); |
|
234 do_check_eq(Object.keys(serialized).length, pluginTags.length+1); // Our entries, plus _v. |
|
235 for (let name in testPlugins) { |
|
236 do_check_true(testPlugins[name].id in serialized); |
|
237 } |
|
238 do_check_eq(serialized._v, 1); |
|
239 |
|
240 // Test counts measurement. |
|
241 |
|
242 let counts = provider.getMeasurement("counts", 2); |
|
243 data = yield counts.getValues(); |
|
244 do_check_eq(data.days.size, 1); |
|
245 do_check_eq(data.singular.size, 0); |
|
246 do_check_true(data.days.hasDay(now)); |
|
247 |
|
248 value = data.days.getDay(now); |
|
249 do_check_eq(value.size, 4); |
|
250 do_check_eq(value.get("extension"), 1); |
|
251 do_check_eq(value.get("plugin"), pluginTags.length); |
|
252 do_check_eq(value.get("theme"), 1); |
|
253 do_check_eq(value.get("service"), 1); |
|
254 |
|
255 yield provider.shutdown(); |
|
256 yield storage.close(); |
|
257 }); |
|
258 |