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
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { FeatureGate } = ChromeUtils.importESModule(
"resource://featuregates/FeatureGate.sys.mjs"
);
const { HttpServer } = ChromeUtils.importESModule(
"resource://testing-common/httpd.sys.mjs"
);
const { AppConstants } = ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
const kDefinitionDefaults = {
id: "test-feature",
title: "Test Feature",
description: "A feature for testing",
restartRequired: false,
type: "boolean",
preference: "test.feature",
defaultValue: false,
isPublic: false,
};
function definitionFactory(override = {}) {
return Object.assign({}, kDefinitionDefaults, override);
}
class DefinitionServer {
constructor(definitionOverrides = []) {
this.server = new HttpServer();
this.server.registerPathHandler("/definitions.json", this);
this.definitions = {};
for (const override of definitionOverrides) {
this.addDefinition(override);
}
this.server.start();
registerCleanupFunction(
() => new Promise(resolve => this.server.stop(resolve))
);
}
// for nsIHttpRequestHandler
handle(request, response) {
// response.setHeader("Content-Type", "application/json");
response.write(JSON.stringify(this.definitions));
}
get definitionsUrl() {
const { primaryScheme, primaryHost, primaryPort } = this.server.identity;
return `${primaryScheme}://${primaryHost}:${primaryPort}/definitions.json`;
}
addDefinition(overrides = {}) {
const definition = definitionFactory(overrides);
// convert targeted values, used by fromId
definition.isPublic = {
default: definition.isPublic,
"test-fact": !definition.isPublic,
};
definition.defaultValue = {
default: definition.defaultValue,
"test-fact": !definition.defaultValue,
};
this.definitions[definition.id] = definition;
return definition;
}
}
// ============================================================================
add_task(async function testReadAll() {
const server = new DefinitionServer();
let ids = ["test-featureA", "test-featureB", "test-featureC"];
for (let id of ids) {
server.addDefinition({ id });
}
let sortedIds = ids.sort();
const features = await FeatureGate.all(server.definitionsUrl);
for (let feature of features) {
equal(
feature.id,
sortedIds.shift(),
"Features are returned in order of definition"
);
}
equal(sortedIds.length, 0, "All features are returned when calling all()");
});
// The getters and setters should read correctly from the definition
add_task(async function testReadFromDefinition() {
const server = new DefinitionServer();
const definition = server.addDefinition({ id: "test-feature" });
const feature = await FeatureGate.fromId(
"test-feature",
server.definitionsUrl
);
// simple fields
equal(feature.id, definition.id, "id should be read from definition");
equal(
feature.title,
definition.title,
"title should be read from definition"
);
equal(
feature.description,
definition.description,
"description should be read from definition"
);
equal(
feature.restartRequired,
definition.restartRequired,
"restartRequired should be read from definition"
);
equal(feature.type, definition.type, "type should be read from definition");
equal(
feature.preference,
definition.preference,
"preference should be read from definition"
);
// targeted fields
equal(
feature.defaultValue,
definition.defaultValue.default,
"defaultValue should be processed as a targeted value"
);
equal(
feature.defaultValueWith(new Map()),
definition.defaultValue.default,
"An empty set of extra facts results in the same value"
);
equal(
feature.defaultValueWith(new Map([["test-fact", true]])),
!definition.defaultValue.default,
"Including an extra fact can change the value"
);
equal(
feature.isPublic,
definition.isPublic.default,
"isPublic should be processed as a targeted value"
);
equal(
feature.isPublicWith(new Map()),
definition.isPublic.default,
"An empty set of extra facts results in the same value"
);
equal(
feature.isPublicWith(new Map([["test-fact", true]])),
!definition.isPublic.default,
"Including an extra fact can change the value"
);
// cleanup
Services.prefs.getDefaultBranch("").deleteBranch("test.feature");
});
// Targeted values should return the correct value
add_task(async function testTargetedValues() {
const targetingFacts = new Map(
Object.entries({ true1: true, true2: true, false1: false, false2: false })
);
Assert.equal(
FeatureGate.evaluateTargetedValue({ default: "foo" }, targetingFacts),
"foo",
"A lone default value should be returned"
);
Assert.equal(
FeatureGate.evaluateTargetedValue(
{ default: "foo", true1: "bar" },
targetingFacts
),
"bar",
"A true target should override the default"
);
Assert.equal(
FeatureGate.evaluateTargetedValue(
{ default: "foo", false1: "bar" },
targetingFacts
),
"foo",
"A false target should not overrides the default"
);
Assert.equal(
FeatureGate.evaluateTargetedValue(
{ default: "foo", "true1,true2": "bar" },
targetingFacts
),
"bar",
"A compound target of two true targets should override the default"
);
Assert.equal(
FeatureGate.evaluateTargetedValue(
{ default: "foo", "true1,false1": "bar" },
targetingFacts
),
"foo",
"A compound target of a true target and a false target should not override the default"
);
Assert.equal(
FeatureGate.evaluateTargetedValue(
{ default: "foo", "false1,false2": "bar" },
targetingFacts
),
"foo",
"A compound target of two false targets should not override the default"
);
Assert.equal(
FeatureGate.evaluateTargetedValue(
{ default: "foo", false1: "bar", true1: "baz" },
targetingFacts
),
"baz",
"A true target should override the default when a false target is also present"
);
});
// getValue should work
add_task(async function testGetValue() {
equal(
Services.prefs.getPrefType("test.feature.1"),
Services.prefs.PREF_INVALID,
"Before creating the feature gate, the preference should not exist"
);
const server = new DefinitionServer([
{ id: "test-feature-1", defaultValue: false, preference: "test.feature.1" },
{ id: "test-feature-2", defaultValue: true, preference: "test.feature.2" },
]);
equal(
await FeatureGate.getValue("test-feature-1", server.definitionsUrl),
false,
"getValue() starts by returning the default value"
);
equal(
await FeatureGate.getValue("test-feature-2", server.definitionsUrl),
true,
"getValue() starts by returning the default value"
);
Services.prefs.setBoolPref("test.feature.1", true);
equal(
await FeatureGate.getValue("test-feature-1", server.definitionsUrl),
true,
"getValue() return the new value"
);
Services.prefs.setBoolPref("test.feature.1", false);
equal(
await FeatureGate.getValue("test-feature-1", server.definitionsUrl),
false,
"getValue() should return the second value"
);
// cleanup
Services.prefs.getDefaultBranch("").deleteBranch("test.feature.");
});
// getValue should work
add_task(async function testGetValue() {
const server = new DefinitionServer([
{ id: "test-feature-1", defaultValue: false, preference: "test.feature.1" },
{ id: "test-feature-2", defaultValue: true, preference: "test.feature.2" },
]);
equal(
Services.prefs.getPrefType("test.feature.1"),
Services.prefs.PREF_INVALID,
"Before creating the feature gate, the first preference should not exist"
);
equal(
Services.prefs.getPrefType("test.feature.2"),
Services.prefs.PREF_INVALID,
"Before creating the feature gate, the second preference should not exist"
);
equal(
await FeatureGate.isEnabled("test-feature-1", server.definitionsUrl),
false,
"isEnabled() starts by returning the default value"
);
equal(
await FeatureGate.isEnabled("test-feature-2", server.definitionsUrl),
true,
"isEnabled() starts by returning the default value"
);
Services.prefs.setBoolPref("test.feature.1", true);
equal(
await FeatureGate.isEnabled("test-feature-1", server.definitionsUrl),
true,
"isEnabled() return the new value"
);
Services.prefs.setBoolPref("test.feature.1", false);
equal(
await FeatureGate.isEnabled("test-feature-1", server.definitionsUrl),
false,
"isEnabled() should return the second value"
);
// cleanup
Services.prefs.getDefaultBranch("").deleteBranch("test.feature.");
});
// adding and removing event observers should work
add_task(async function testGetValue() {
const preference = "test.pref";
const server = new DefinitionServer([
{ id: "test-feature", defaultValue: false, preference },
]);
const observer = {
onChange: sinon.stub(),
onEnable: sinon.stub(),
onDisable: sinon.stub(),
};
let rv = await FeatureGate.addObserver(
"test-feature",
observer,
server.definitionsUrl
);
equal(rv, false, "addObserver returns the current value");
Assert.deepEqual(observer.onChange.args, [], "onChange should not be called");
Assert.deepEqual(observer.onEnable.args, [], "onEnable should not be called");
Assert.deepEqual(
observer.onDisable.args,
[],
"onDisable should not be called"
);
Services.prefs.setBoolPref(preference, true);
await Promise.resolve(); // Allow events to be called async
Assert.deepEqual(
observer.onChange.args,
[[true]],
"onChange should be called with the new value"
);
Assert.deepEqual(observer.onEnable.args, [[]], "onEnable should be called");
Assert.deepEqual(
observer.onDisable.args,
[],
"onDisable should not be called"
);
Services.prefs.setBoolPref(preference, false);
await Promise.resolve(); // Allow events to be called async
Assert.deepEqual(
observer.onChange.args,
[[true], [false]],
"onChange should be called again with the new value"
);
Assert.deepEqual(
observer.onEnable.args,
[[]],
"onEnable should not be called a second time"
);
Assert.deepEqual(
observer.onDisable.args,
[[]],
"onDisable should be called for the first time"
);
Services.prefs.setBoolPref(preference, false);
await Promise.resolve(); // Allow events to be called async
Assert.deepEqual(
observer.onChange.args,
[[true], [false]],
"onChange should not be called if the value did not change"
);
Assert.deepEqual(
observer.onEnable.args,
[[]],
"onEnable should not be called again if the value did not change"
);
Assert.deepEqual(
observer.onDisable.args,
[[]],
"onDisable should not be called if the value did not change"
);
// remove the listener and make sure the observer isn't called again
FeatureGate.removeObserver("test-feature", observer);
await Promise.resolve(); // Allow events to be called async
Services.prefs.setBoolPref(preference, true);
await Promise.resolve(); // Allow events to be called async
Assert.deepEqual(
observer.onChange.args,
[[true], [false]],
"onChange should not be called after observer was removed"
);
Assert.deepEqual(
observer.onEnable.args,
[[]],
"onEnable should not be called after observer was removed"
);
Assert.deepEqual(
observer.onDisable.args,
[[]],
"onDisable should not be called after observer was removed"
);
// cleanup
Services.prefs.getDefaultBranch("").deleteBranch(preference);
});
if (AppConstants.platform != "android") {
// All preferences should have default values.
add_task(async function testAllHaveDefault() {
const featuresList = await FeatureGate.all();
for (let feature of featuresList) {
notEqual(
typeof feature.defaultValue,
"undefined",
`Feature ${feature.id} should have a defined default value!`
);
notEqual(
feature.defaultValue,
null,
`Feature ${feature.id} should have a non-null default value!`
);
}
});
// All preference defaults should match service pref defaults
add_task(async function testAllDefaultsMatchSettings() {
const featuresList = await FeatureGate.all();
for (let feature of featuresList) {
let value = Services.prefs
.getDefaultBranch("")
.getBoolPref(feature.preference);
equal(
feature.defaultValue,
value,
`Feature ${feature.preference} should match runtime value.`
);
}
});
}
|