services/healthreport/tests/xpcshell/test_provider_addons.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

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

mercurial