1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
const STORAGE_SYNC_PREF = "webextensions.storage.sync.enabled";
Cu.import("resource://gre/modules/Preferences.jsm");
/**
* Utility function to ensure that all supported APIs for getting are
* tested.
*
* @param {string} areaName
* either "local" or "sync" according to what we want to test
* @param {string} prop
* "key" to look up using the storage API
* @param {Object} value
* "value" to compare against
*/
async function checkGetImpl(areaName, prop, value) {
let storage = browser.storage[areaName];
let data = await storage.get(null);
browser.test.assertEq(value, data[prop], `null getter worked for ${prop} in ${areaName}`);
data = await storage.get(prop);
browser.test.assertEq(value, data[prop], `string getter worked for ${prop} in ${areaName}`);
data = await storage.get([prop]);
browser.test.assertEq(value, data[prop], `array getter worked for ${prop} in ${areaName}`);
data = await storage.get({[prop]: undefined});
browser.test.assertEq(value, data[prop], `object getter worked for ${prop} in ${areaName}`);
}
add_task(function* test_local_cache_invalidation() {
function background(checkGet) {
browser.test.onMessage.addListener(async msg => {
if (msg === "set-initial") {
await browser.storage.local.set({"test-prop1": "value1", "test-prop2": "value2"});
browser.test.sendMessage("set-initial-done");
} else if (msg === "check") {
await checkGet("local", "test-prop1", "value1");
await checkGet("local", "test-prop2", "value2");
browser.test.sendMessage("check-done");
}
});
browser.test.sendMessage("ready");
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["storage"],
},
background: `(${background})(${checkGetImpl})`,
});
yield extension.startup();
yield extension.awaitMessage("ready");
extension.sendMessage("set-initial");
yield extension.awaitMessage("set-initial-done");
Services.obs.notifyObservers(null, "extension-invalidate-storage-cache", "");
extension.sendMessage("check");
yield extension.awaitMessage("check-done");
yield extension.unload();
});
add_task(function* test_config_flag_needed() {
function background() {
let promises = [];
let apiTests = [
{method: "get", args: ["foo"]},
{method: "set", args: [{foo: "bar"}]},
{method: "remove", args: ["foo"]},
{method: "clear", args: []},
];
apiTests.forEach(testDef => {
promises.push(browser.test.assertRejects(
browser.storage.sync[testDef.method](...testDef.args),
"Please set webextensions.storage.sync.enabled to true in about:config",
`storage.sync.${testDef.method} is behind a flag`));
});
Promise.all(promises).then(() => browser.test.notifyPass("flag needed"));
}
ok(!Preferences.get(STORAGE_SYNC_PREF));
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["storage"],
},
background: `(${background})(${checkGetImpl})`,
});
yield extension.startup();
yield extension.awaitFinish("flag needed");
yield extension.unload();
});
add_task(function* test_reloading_extensions_works() {
// Just some random extension ID that we can re-use
const extensionId = "my-extension-id@1";
function loadExtension() {
function background() {
browser.storage.sync.set({"a": "b"}).then(() => {
browser.test.notifyPass("set-works");
});
}
return ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["storage"],
},
background: `(${background})()`,
}, extensionId);
}
Preferences.set(STORAGE_SYNC_PREF, true);
let extension1 = loadExtension();
yield extension1.startup();
yield extension1.awaitFinish("set-works");
yield extension1.unload();
let extension2 = loadExtension();
yield extension2.startup();
yield extension2.awaitFinish("set-works");
yield extension2.unload();
Preferences.reset(STORAGE_SYNC_PREF);
});
do_register_cleanup(() => {
Preferences.reset(STORAGE_SYNC_PREF);
});
add_task(function* test_backgroundScript() {
async function backgroundScript(checkGet) {
let globalChanges, gResolve;
function clearGlobalChanges() {
globalChanges = new Promise(resolve => { gResolve = resolve; });
}
clearGlobalChanges();
let expectedAreaName;
browser.storage.onChanged.addListener((changes, areaName) => {
browser.test.assertEq(expectedAreaName, areaName,
"Expected area name received by listener");
gResolve(changes);
});
async function checkChanges(areaName, changes, message) {
function checkSub(obj1, obj2) {
for (let prop in obj1) {
browser.test.assertTrue(obj1[prop] !== undefined,
`checkChanges ${areaName} ${prop} is missing (${message})`);
browser.test.assertTrue(obj2[prop] !== undefined,
`checkChanges ${areaName} ${prop} is missing (${message})`);
browser.test.assertEq(obj1[prop].oldValue, obj2[prop].oldValue,
`checkChanges ${areaName} ${prop} old (${message})`);
browser.test.assertEq(obj1[prop].newValue, obj2[prop].newValue,
`checkChanges ${areaName} ${prop} new (${message})`);
}
}
const recentChanges = await globalChanges;
checkSub(changes, recentChanges);
checkSub(recentChanges, changes);
clearGlobalChanges();
}
/* eslint-disable dot-notation */
async function runTests(areaName) {
expectedAreaName = areaName;
let storage = browser.storage[areaName];
// Set some data and then test getters.
try {
await storage.set({"test-prop1": "value1", "test-prop2": "value2"});
await checkChanges(areaName,
{"test-prop1": {newValue: "value1"}, "test-prop2": {newValue: "value2"}},
"set (a)");
await checkGet(areaName, "test-prop1", "value1");
await checkGet(areaName, "test-prop2", "value2");
let data = await storage.get({"test-prop1": undefined, "test-prop2": undefined, "other": "default"});
browser.test.assertEq("value1", data["test-prop1"], "prop1 correct (a)");
browser.test.assertEq("value2", data["test-prop2"], "prop2 correct (a)");
browser.test.assertEq("default", data["other"], "other correct");
data = await storage.get(["test-prop1", "test-prop2", "other"]);
browser.test.assertEq("value1", data["test-prop1"], "prop1 correct (b)");
browser.test.assertEq("value2", data["test-prop2"], "prop2 correct (b)");
browser.test.assertFalse("other" in data, "other correct");
// Remove data in various ways.
await storage.remove("test-prop1");
await checkChanges(areaName, {"test-prop1": {oldValue: "value1"}}, "remove string");
data = await storage.get(["test-prop1", "test-prop2"]);
browser.test.assertFalse("test-prop1" in data, "prop1 absent (remove string)");
browser.test.assertTrue("test-prop2" in data, "prop2 present (remove string)");
await storage.set({"test-prop1": "value1"});
await checkChanges(areaName, {"test-prop1": {newValue: "value1"}}, "set (c)");
data = await storage.get(["test-prop1", "test-prop2"]);
browser.test.assertEq(data["test-prop1"], "value1", "prop1 correct (c)");
browser.test.assertEq(data["test-prop2"], "value2", "prop2 correct (c)");
await storage.remove(["test-prop1", "test-prop2"]);
await checkChanges(areaName,
{"test-prop1": {oldValue: "value1"}, "test-prop2": {oldValue: "value2"}},
"remove array");
data = await storage.get(["test-prop1", "test-prop2"]);
browser.test.assertFalse("test-prop1" in data, "prop1 absent (remove array)");
browser.test.assertFalse("test-prop2" in data, "prop2 absent (remove array)");
// test storage.clear
await storage.set({"test-prop1": "value1", "test-prop2": "value2"});
// Make sure that set() handler happened before we clear the
// promise again.
await globalChanges;
clearGlobalChanges();
await storage.clear();
await checkChanges(areaName,
{"test-prop1": {oldValue: "value1"}, "test-prop2": {oldValue: "value2"}},
"clear");
data = await storage.get(["test-prop1", "test-prop2"]);
browser.test.assertFalse("test-prop1" in data, "prop1 absent (clear)");
browser.test.assertFalse("test-prop2" in data, "prop2 absent (clear)");
// Make sure we can store complex JSON data.
// known previous values
await storage.set({"test-prop1": "value1", "test-prop2": "value2"});
// Make sure the set() handler landed.
await globalChanges;
clearGlobalChanges();
await storage.set({
"test-prop1": {
str: "hello",
bool: true,
null: null,
undef: undefined,
obj: {},
arr: [1, 2],
date: new Date(0),
regexp: /regexp/,
func: function func() {},
window,
},
});
await storage.set({"test-prop2": function func() {}});
const recentChanges = await globalChanges;
browser.test.assertEq("value1", recentChanges["test-prop1"].oldValue, "oldValue correct");
browser.test.assertEq("object", typeof(recentChanges["test-prop1"].newValue), "newValue is obj");
clearGlobalChanges();
data = await storage.get({"test-prop1": undefined, "test-prop2": undefined});
let obj = data["test-prop1"];
browser.test.assertEq("hello", obj.str, "string part correct");
browser.test.assertEq(true, obj.bool, "bool part correct");
browser.test.assertEq(null, obj.null, "null part correct");
browser.test.assertEq(undefined, obj.undef, "undefined part correct");
browser.test.assertEq(undefined, obj.func, "function part correct");
browser.test.assertEq(undefined, obj.window, "window part correct");
browser.test.assertEq("1970-01-01T00:00:00.000Z", obj.date, "date part correct");
browser.test.assertEq("/regexp/", obj.regexp, "regexp part correct");
browser.test.assertEq("object", typeof(obj.obj), "object part correct");
browser.test.assertTrue(Array.isArray(obj.arr), "array part present");
browser.test.assertEq(1, obj.arr[0], "arr[0] part correct");
browser.test.assertEq(2, obj.arr[1], "arr[1] part correct");
browser.test.assertEq(2, obj.arr.length, "arr.length part correct");
obj = data["test-prop2"];
browser.test.assertEq("[object Object]", {}.toString.call(obj), "function serialized as a plain object");
browser.test.assertEq(0, Object.keys(obj).length, "function serialized as an empty object");
} catch (e) {
browser.test.fail(`Error: ${e} :: ${e.stack}`);
browser.test.notifyFail("storage");
}
}
browser.test.onMessage.addListener(msg => {
let promise;
if (msg === "test-local") {
promise = runTests("local");
} else if (msg === "test-sync") {
promise = runTests("sync");
}
promise.then(() => browser.test.sendMessage("test-finished"));
});
browser.test.sendMessage("ready");
}
let extensionData = {
background: `(${backgroundScript})(${checkGetImpl})`,
manifest: {
permissions: ["storage"],
},
};
Preferences.set(STORAGE_SYNC_PREF, true);
let extension = ExtensionTestUtils.loadExtension(extensionData);
yield extension.startup();
yield extension.awaitMessage("ready");
extension.sendMessage("test-local");
yield extension.awaitMessage("test-finished");
extension.sendMessage("test-sync");
yield extension.awaitMessage("test-finished");
Preferences.reset(STORAGE_SYNC_PREF);
yield extension.unload();
});
|