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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
import {
_ASRouterPreferences,
ASRouterPreferences as ASRouterPreferencesSingleton,
TEST_PROVIDERS,
} from "lib/ASRouterPreferences.jsm";
const FAKE_PROVIDERS = [{ id: "foo" }, { id: "bar" }];
const PROVIDER_PREF_BRANCH =
"browser.newtabpage.activity-stream.asrouter.providers.";
const DEVTOOLS_PREF =
"browser.newtabpage.activity-stream.asrouter.devtoolsEnabled";
const SNIPPETS_USER_PREF = "browser.newtabpage.activity-stream.feeds.snippets";
const CFR_USER_PREF_ADDONS =
"browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons";
const CFR_USER_PREF_FEATURES =
"browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features";
/** NUMBER_OF_PREFS_TO_OBSERVE includes:
* 1. asrouter.providers. pref branch
* 2. asrouter.devtoolsEnabled
* 3. browser.newtabpage.activity-stream.feeds.snippets (user preference - snippets)
* 4. browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons (user preference - cfr)
* 4. browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features (user preference - cfr)
* 5. services.sync.username
*/
const NUMBER_OF_PREFS_TO_OBSERVE = 6;
describe("ASRouterPreferences", () => {
let ASRouterPreferences;
let sandbox;
let addObserverStub;
let stringPrefStub;
let boolPrefStub;
let resetStub;
let hasUserValueStub;
let childListStub;
let setStringPrefStub;
beforeEach(() => {
ASRouterPreferences = new _ASRouterPreferences();
sandbox = sinon.createSandbox();
addObserverStub = sandbox.stub(global.Services.prefs, "addObserver");
stringPrefStub = sandbox.stub(global.Services.prefs, "getStringPref");
resetStub = sandbox.stub(global.Services.prefs, "clearUserPref");
setStringPrefStub = sandbox.stub(global.Services.prefs, "setStringPref");
FAKE_PROVIDERS.forEach(provider => {
stringPrefStub
.withArgs(`${PROVIDER_PREF_BRANCH}${provider.id}`)
.returns(JSON.stringify(provider));
});
boolPrefStub = sandbox
.stub(global.Services.prefs, "getBoolPref")
.returns(false);
hasUserValueStub = sandbox
.stub(global.Services.prefs, "prefHasUserValue")
.returns(false);
childListStub = sandbox.stub(global.Services.prefs, "getChildList");
childListStub
.withArgs(PROVIDER_PREF_BRANCH)
.returns(
FAKE_PROVIDERS.map(provider => `${PROVIDER_PREF_BRANCH}${provider.id}`)
);
});
afterEach(() => {
sandbox.restore();
});
function getPrefNameForProvider(providerId) {
return `${PROVIDER_PREF_BRANCH}${providerId}`;
}
function setPrefForProvider(providerId, value) {
stringPrefStub
.withArgs(getPrefNameForProvider(providerId))
.returns(JSON.stringify(value));
}
it("ASRouterPreferences should be an instance of _ASRouterPreferences", () => {
assert.instanceOf(ASRouterPreferencesSingleton, _ASRouterPreferences);
});
describe("#init", () => {
it("should set ._initialized to true", () => {
ASRouterPreferences.init();
assert.isTrue(ASRouterPreferences._initialized);
});
it("should migrate the provider prefs", () => {
ASRouterPreferences.uninit();
// Should be migrated because they contain bucket and not collection
const MIGRATE_PROVIDERS = [
{ id: "baz", bucket: "buk" },
{ id: "qux", bucket: "buk" },
];
// Should be cleared to defaults because it throws on setStringPref
const ERROR_PROVIDER = { id: "err", bucket: "buk" };
// Should not be migrated because, although modified, it lacks bucket
const MODIFIED_SAFE_PROVIDER = { id: "safe" };
const ALL_PROVIDERS = [
...MIGRATE_PROVIDERS,
...FAKE_PROVIDERS, // Should not be migrated because they're unmodified
MODIFIED_SAFE_PROVIDER,
ERROR_PROVIDER,
];
// The migrator should attempt to read prefs for all of these providers
const TRY_PROVIDERS = [
...MIGRATE_PROVIDERS,
MODIFIED_SAFE_PROVIDER,
ERROR_PROVIDER,
];
// Update the full list of provider prefs
childListStub
.withArgs(PROVIDER_PREF_BRANCH)
.returns(
ALL_PROVIDERS.map(provider => getPrefNameForProvider(provider.id))
);
// Stub the pref values so the migrator can read them
ALL_PROVIDERS.forEach(provider => {
stringPrefStub
.withArgs(getPrefNameForProvider(provider.id))
.returns(JSON.stringify(provider));
});
// Consider these providers' prefs "modified"
TRY_PROVIDERS.forEach(provider => {
hasUserValueStub
.withArgs(`${PROVIDER_PREF_BRANCH}${provider.id}`)
.returns(true);
});
// Spoof an error when trying to set the pref for this provider so we can
// test that the pref is gracefully reset on error
setStringPrefStub
.withArgs(getPrefNameForProvider(ERROR_PROVIDER.id))
.throws();
ASRouterPreferences.init();
// The migrator should have tried to check each pref for user modification
ALL_PROVIDERS.forEach(provider =>
assert.calledWith(hasUserValueStub, getPrefNameForProvider(provider.id))
);
// Test that we don't call getStringPref for providers that don't have a
// user-defined value
FAKE_PROVIDERS.forEach(provider =>
assert.neverCalledWith(
stringPrefStub,
getPrefNameForProvider(provider.id)
)
);
// But we do call it for providers that do have a user-defined value
TRY_PROVIDERS.forEach(provider =>
assert.calledWith(stringPrefStub, getPrefNameForProvider(provider.id))
);
// Test that we don't call setStringPref to migrate providers that don't
// have a bucket property
assert.neverCalledWith(
setStringPrefStub,
getPrefNameForProvider(MODIFIED_SAFE_PROVIDER.id)
);
/**
* For a given provider, return a sinon matcher that matches if the value
* looks like a migrated version of the original provider. Requires that:
* its id matches the original provider's id; it has no bucket; and its
* collection is set to the value of the original provider's bucket.
* @param {object} provider the provider object to compare to
* @returns {object} custom matcher object for sinon
*/
function providerJsonMatches(provider) {
return sandbox.match(migrated => {
const parsed = JSON.parse(migrated);
return (
parsed.id === provider.id &&
!("bucket" in parsed) &&
parsed.collection === provider.bucket
);
});
}
// Test that we call setStringPref to migrate providers that have a bucket
// property and don't have a collection property
MIGRATE_PROVIDERS.forEach(provider =>
assert.calledWith(
setStringPrefStub,
getPrefNameForProvider(provider.id),
providerJsonMatches(provider) // Verify the migrated pref value
)
);
// Test that we clear the pref for providers that throw when we try to
// read or write them
assert.calledWith(resetStub, getPrefNameForProvider(ERROR_PROVIDER.id));
});
it(`should set ${NUMBER_OF_PREFS_TO_OBSERVE} observers and not re-initialize if already initialized`, () => {
ASRouterPreferences.init();
assert.callCount(addObserverStub, NUMBER_OF_PREFS_TO_OBSERVE);
ASRouterPreferences.init();
ASRouterPreferences.init();
assert.callCount(addObserverStub, NUMBER_OF_PREFS_TO_OBSERVE);
});
});
describe("#uninit", () => {
it("should set ._initialized to false", () => {
ASRouterPreferences.init();
ASRouterPreferences.uninit();
assert.isFalse(ASRouterPreferences._initialized);
});
it("should clear cached values for ._initialized, .devtoolsEnabled", () => {
ASRouterPreferences.init();
// trigger caching
// eslint-disable-next-line no-unused-vars
const result = [
ASRouterPreferences.providers,
ASRouterPreferences.devtoolsEnabled,
];
assert.isNotNull(
ASRouterPreferences._providers,
"providers should not be null"
);
assert.isNotNull(
ASRouterPreferences._devtoolsEnabled,
"devtolosEnabled should not be null"
);
ASRouterPreferences.uninit();
assert.isNull(ASRouterPreferences._providers);
assert.isNull(ASRouterPreferences._devtoolsEnabled);
});
it("should clear all listeners and remove observers (only once)", () => {
const removeStub = sandbox.stub(global.Services.prefs, "removeObserver");
ASRouterPreferences.init();
ASRouterPreferences.addListener(() => {});
ASRouterPreferences.addListener(() => {});
assert.equal(ASRouterPreferences._callbacks.size, 2);
ASRouterPreferences.uninit();
// Tests to make sure we don't remove observers that weren't set
ASRouterPreferences.uninit();
assert.callCount(removeStub, NUMBER_OF_PREFS_TO_OBSERVE);
assert.calledWith(removeStub, PROVIDER_PREF_BRANCH);
assert.calledWith(removeStub, DEVTOOLS_PREF);
assert.isEmpty(ASRouterPreferences._callbacks);
});
});
describe(".providers", () => {
it("should return the value the first time .providers is accessed", () => {
ASRouterPreferences.init();
const result = ASRouterPreferences.providers;
assert.deepEqual(result, FAKE_PROVIDERS);
// once per pref
assert.calledTwice(stringPrefStub);
});
it("should return the cached value the second time .providers is accessed", () => {
ASRouterPreferences.init();
const [, secondCall] = [
ASRouterPreferences.providers,
ASRouterPreferences.providers,
];
assert.deepEqual(secondCall, FAKE_PROVIDERS);
// once per pref
assert.calledTwice(stringPrefStub);
});
it("should just parse the pref each time if ASRouterPreferences hasn't been initialized yet", () => {
// Intentionally not initialized
const [firstCall, secondCall] = [
ASRouterPreferences.providers,
ASRouterPreferences.providers,
];
assert.deepEqual(firstCall, FAKE_PROVIDERS);
assert.deepEqual(secondCall, FAKE_PROVIDERS);
assert.callCount(stringPrefStub, 4);
});
it("should skip the pref without throwing if a pref is not parsable", () => {
stringPrefStub.withArgs(`${PROVIDER_PREF_BRANCH}foo`).returns("not json");
ASRouterPreferences.init();
assert.deepEqual(ASRouterPreferences.providers, [{ id: "bar" }]);
});
it("should include TEST_PROVIDERS if devtools is turned on", () => {
boolPrefStub.withArgs(DEVTOOLS_PREF).returns(true);
ASRouterPreferences.init();
assert.deepEqual(ASRouterPreferences.providers, [
...TEST_PROVIDERS,
...FAKE_PROVIDERS,
]);
});
});
describe(".devtoolsEnabled", () => {
it("should read the pref the first time .devtoolsEnabled is accessed", () => {
ASRouterPreferences.init();
const result = ASRouterPreferences.devtoolsEnabled;
assert.deepEqual(result, false);
assert.calledOnce(boolPrefStub);
});
it("should return the cached value the second time .devtoolsEnabled is accessed", () => {
ASRouterPreferences.init();
const [, secondCall] = [
ASRouterPreferences.devtoolsEnabled,
ASRouterPreferences.devtoolsEnabled,
];
assert.deepEqual(secondCall, false);
assert.calledOnce(boolPrefStub);
});
it("should just parse the pref each time if ASRouterPreferences hasn't been initialized yet", () => {
// Intentionally not initialized
const [firstCall, secondCall] = [
ASRouterPreferences.devtoolsEnabled,
ASRouterPreferences.devtoolsEnabled,
];
assert.deepEqual(firstCall, false);
assert.deepEqual(secondCall, false);
assert.calledTwice(boolPrefStub);
});
});
describe("#getUserPreference(providerId)", () => {
it("should return the user preference for snippets", () => {
boolPrefStub.withArgs(SNIPPETS_USER_PREF).returns(true);
assert.isTrue(ASRouterPreferences.getUserPreference("snippets"));
});
});
describe("#getAllUserPreferences", () => {
it("should return all user preferences", () => {
boolPrefStub.withArgs(SNIPPETS_USER_PREF).returns(true);
boolPrefStub.withArgs(CFR_USER_PREF_ADDONS).returns(false);
boolPrefStub.withArgs(CFR_USER_PREF_FEATURES).returns(true);
const result = ASRouterPreferences.getAllUserPreferences();
assert.deepEqual(result, {
snippets: true,
cfrAddons: false,
cfrFeatures: true,
});
});
});
describe("#enableOrDisableProvider", () => {
it("should enable an existing provider if second param is true", () => {
setPrefForProvider("foo", { id: "foo", enabled: false });
assert.isFalse(ASRouterPreferences.providers[0].enabled);
ASRouterPreferences.enableOrDisableProvider("foo", true);
assert.calledWith(
setStringPrefStub,
getPrefNameForProvider("foo"),
JSON.stringify({ id: "foo", enabled: true })
);
});
it("should disable an existing provider if second param is false", () => {
setPrefForProvider("foo", { id: "foo", enabled: true });
assert.isTrue(ASRouterPreferences.providers[0].enabled);
ASRouterPreferences.enableOrDisableProvider("foo", false);
assert.calledWith(
setStringPrefStub,
getPrefNameForProvider("foo"),
JSON.stringify({ id: "foo", enabled: false })
);
});
it("should not throw if the id does not exist", () => {
assert.doesNotThrow(() => {
ASRouterPreferences.enableOrDisableProvider("does_not_exist", true);
});
});
it("should not throw if pref is not parseable", () => {
stringPrefStub
.withArgs(getPrefNameForProvider("foo"))
.returns("not valid");
assert.doesNotThrow(() => {
ASRouterPreferences.enableOrDisableProvider("foo", true);
});
});
});
describe("#setUserPreference", () => {
it("should do nothing if the pref doesn't exist", () => {
ASRouterPreferences.setUserPreference("foo", true);
assert.notCalled(boolPrefStub);
});
it("should set the given pref", () => {
const setStub = sandbox.stub(global.Services.prefs, "setBoolPref");
ASRouterPreferences.setUserPreference("snippets", true);
assert.calledWith(setStub, SNIPPETS_USER_PREF, true);
});
});
describe("#resetProviderPref", () => {
it("should reset the pref and user prefs", () => {
ASRouterPreferences.resetProviderPref();
FAKE_PROVIDERS.forEach(provider => {
assert.calledWith(resetStub, getPrefNameForProvider(provider.id));
});
assert.calledWith(resetStub, SNIPPETS_USER_PREF);
assert.calledWith(resetStub, CFR_USER_PREF_ADDONS);
assert.calledWith(resetStub, CFR_USER_PREF_FEATURES);
});
});
describe("observer, listeners", () => {
it("should invalidate .providers when the pref is changed", () => {
const testProvider = { id: "newstuff" };
const newProviders = [...FAKE_PROVIDERS, testProvider];
ASRouterPreferences.init();
assert.deepEqual(ASRouterPreferences.providers, FAKE_PROVIDERS);
stringPrefStub
.withArgs(getPrefNameForProvider(testProvider.id))
.returns(JSON.stringify(testProvider));
childListStub
.withArgs(PROVIDER_PREF_BRANCH)
.returns(
newProviders.map(provider => getPrefNameForProvider(provider.id))
);
ASRouterPreferences.observe(
null,
null,
getPrefNameForProvider(testProvider.id)
);
// Cache should be invalidated so we access the new value of the pref now
assert.deepEqual(ASRouterPreferences.providers, newProviders);
});
it("should invalidate .devtoolsEnabled and .providers when the pref is changed", () => {
ASRouterPreferences.init();
assert.isFalse(ASRouterPreferences.devtoolsEnabled);
boolPrefStub.withArgs(DEVTOOLS_PREF).returns(true);
childListStub.withArgs(PROVIDER_PREF_BRANCH).returns([]);
ASRouterPreferences.observe(null, null, DEVTOOLS_PREF);
// Cache should be invalidated so we access the new value of the pref now
// Note that providers needs to be invalidated because devtools adds test content to it.
assert.isTrue(ASRouterPreferences.devtoolsEnabled);
assert.deepEqual(ASRouterPreferences.providers, TEST_PROVIDERS);
});
it("should call listeners added with .addListener", () => {
const callback1 = sinon.stub();
const callback2 = sinon.stub();
ASRouterPreferences.init();
ASRouterPreferences.addListener(callback1);
ASRouterPreferences.addListener(callback2);
ASRouterPreferences.observe(null, null, getPrefNameForProvider("foo"));
assert.calledWith(callback1, getPrefNameForProvider("foo"));
ASRouterPreferences.observe(null, null, DEVTOOLS_PREF);
assert.calledWith(callback2, DEVTOOLS_PREF);
});
it("should not call listeners after they are removed with .removeListeners", () => {
const callback = sinon.stub();
ASRouterPreferences.init();
ASRouterPreferences.addListener(callback);
ASRouterPreferences.observe(null, null, getPrefNameForProvider("foo"));
assert.calledWith(callback, getPrefNameForProvider("foo"));
callback.reset();
ASRouterPreferences.removeListener(callback);
ASRouterPreferences.observe(null, null, DEVTOOLS_PREF);
assert.notCalled(callback);
});
});
describe("#_transformPersonalizedCfrScores", () => {
it("should report JSON.parse errors", () => {
sandbox.stub(global.console, "error");
ASRouterPreferences._transformPersonalizedCfrScores("");
assert.calledOnce(global.console.error);
});
it("should return an object parsed from a string", () => {
const scores = { FOO: 3000, BAR: 4000 };
assert.deepEqual(
ASRouterPreferences._transformPersonalizedCfrScores(
JSON.stringify(scores)
),
scores
);
});
});
});
|