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
|
const { SharedDataMap } = ChromeUtils.import(
"resource://nimbus/lib/SharedDataMap.jsm"
);
const { FileTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/FileTestUtils.sys.mjs"
);
const { TestUtils } = ChromeUtils.importESModule(
"resource://testing-common/TestUtils.sys.mjs"
);
const PATH = FileTestUtils.getTempFile("shared-data-map").path;
function with_sharedDataMap(test) {
let testTask = async () => {
const sandbox = sinon.createSandbox();
const instance = new SharedDataMap("xpcshell", {
path: PATH,
isParent: true,
});
try {
await test({ instance, sandbox });
} finally {
sandbox.restore();
}
};
// Copy the name of the test function to identify the test
Object.defineProperty(testTask, "name", { value: test.name });
add_task(testTask);
}
with_sharedDataMap(async function test_set_notify({ instance, sandbox }) {
await instance.init();
let updateStub = sandbox.stub();
instance.on("parent-store-update:foo", updateStub);
instance.set("foo", "bar");
Assert.equal(updateStub.callCount, 1, "Update event sent");
Assert.equal(updateStub.firstCall.args[1], "bar", "Update event sent value");
});
with_sharedDataMap(async function test_set_child_notify({ instance, sandbox }) {
await instance.init();
let updateStub = sandbox.stub();
const childInstance = new SharedDataMap("xpcshell", {
path: PATH,
isParent: false,
});
childInstance.on("child-store-update:foo", updateStub);
let childStoreUpdate = new Promise(resolve =>
childInstance.on("child-store-update:foo", resolve)
);
instance.set("foo", "bar");
await childStoreUpdate;
Assert.equal(updateStub.callCount, 1, "Update event sent");
Assert.equal(updateStub.firstCall.args[1], "bar", "Update event sent value");
});
with_sharedDataMap(async function test_async({ instance, sandbox }) {
const spy = sandbox.spy(instance._store, "load");
await instance.init();
instance.set("foo", "bar");
Assert.equal(spy.callCount, 1, "Should init async");
Assert.equal(instance.get("foo"), "bar", "It should retrieve a string value");
});
with_sharedDataMap(async function test_saveSoon({ instance, sandbox }) {
await instance.init();
const stub = sandbox.stub(instance._store, "saveSoon");
instance.set("foo", "bar");
Assert.equal(stub.callCount, 1, "Should call save soon when setting a value");
});
with_sharedDataMap(async function test_init_safe({ instance, sandbox }) {
let stub = sandbox.stub(instance._store, "load");
sandbox.replaceGetter(instance._store, "data", () => {
throw new Error("expected xpcshell");
});
try {
await instance.init();
Assert.ok(stub.calledOnce, "Load should be called");
} catch (e) {
Assert.ok(false, "Error should be caught in SharedDataMap");
}
});
with_sharedDataMap(async function test_childInit({ instance, sandbox }) {
sandbox.stub(instance, "isParent").get(() => false);
const stubA = sandbox.stub(instance._store, "ensureDataReady");
const stubB = sandbox.stub(instance._store, "load");
await instance.init();
Assert.equal(
stubA.callCount,
0,
"It should not try to initialize sync from child"
);
Assert.equal(
stubB.callCount,
0,
"It should not try to initialize async from child"
);
});
with_sharedDataMap(async function test_parentChildSync_synchronously({
instance: parentInstance,
sandbox,
}) {
await parentInstance.init();
parentInstance.set("foo", { bar: 1 });
const childInstance = new SharedDataMap("xpcshell", {
path: PATH,
isParent: false,
});
await parentInstance.ready();
await childInstance.ready();
await TestUtils.waitForCondition(
() => childInstance.get("foo"),
"Wait for child to sync"
);
Assert.deepEqual(
childInstance.get("foo"),
parentInstance.get("foo"),
"Parent and child should be in sync"
);
});
with_sharedDataMap(async function test_parentChildSync_async({
instance: parentInstance,
sandbox,
}) {
const childInstance = new SharedDataMap("xpcshell", {
path: PATH,
isParent: false,
});
await parentInstance.init();
parentInstance.set("foo", { bar: 1 });
await parentInstance.ready();
await childInstance.ready();
await TestUtils.waitForCondition(
() => childInstance.get("foo"),
"Wait for child to sync"
);
Assert.deepEqual(
childInstance.get("foo"),
parentInstance.get("foo"),
"Parent and child should be in sync"
);
});
with_sharedDataMap(async function test_earlyChildSync({
instance: parentInstance,
sandbox,
}) {
const childInstance = new SharedDataMap("xpcshell", {
path: PATH,
isParent: false,
});
Assert.equal(childInstance.has("baz"), false, "Should not fail");
await parentInstance.init();
parentInstance.set("baz", { bar: 1 });
await TestUtils.waitForCondition(
() => childInstance.get("baz"),
"Wait for child to sync"
);
Assert.deepEqual(
childInstance.get("baz"),
parentInstance.get("baz"),
"Parent and child should be in sync"
);
});
with_sharedDataMap(async function test_updateStoreData({ instance, sandbox }) {
await instance.init();
Assert.ok(!instance.get("foo"), "No value initially");
instance.set("foo", "foo");
instance.set("bar", "bar");
instance._removeEntriesByKeys(["bar"]);
Assert.ok(instance.get("foo"), "We keep one of the values");
Assert.ok(!instance.get("bar"), "The other value is removed");
});
|