File: test_preferencesBindings_setting.html

package info (click to toggle)
firefox 147.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,532 kB
  • sloc: cpp: 7,607,356; javascript: 6,533,348; ansic: 3,775,236; python: 1,415,508; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,760; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (462 lines) | stat: -rw-r--r-- 15,330 bytes parent folder | download | duplicates (2)
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
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>PreferencesBindings Setting Tests</title>
    <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    <link
      rel="stylesheet"
      href="chrome://mochikit/content/tests/SimpleTest/test.css"
    />
    <script
      type="application/javascript"
      src="chrome://global/content/preferencesBindings.js"
    ></script>
    <script>
      /* import-globals-from /toolkit/content/preferencesBindings.js */
      function waitForSettingChange(setting) {
        return new Promise(resolve => {
          setting.on("change", function handler() {
            setting.off("change", handler);
            resolve();
          });
        });
      }

      add_task(async function testSimplePrefSetting() {
        const PREF_ID = "test.setting.bool";
        const SETTING_ID = "testSettingBool";
        await SpecialPowers.pushPrefEnv({
          set: [[PREF_ID, false]],
        });
        Preferences.add({
          id: PREF_ID,
          type: "bool",
        });
        Preferences.addSetting({
          id: SETTING_ID,
          pref: PREF_ID,
        });

        let setting = Preferences.getSetting(SETTING_ID);
        ok(setting, "Got a setting object");

        is(Services.prefs.getBoolPref(PREF_ID), false, "Pref is false");
        is(setting.value, false, "Setting read from prefs");

        let settingChanged = waitForSettingChange(setting);
        setting.userChange(true);
        await settingChanged;

        is(Services.prefs.getBoolPref(PREF_ID), true, "Pref is true");
        is(setting.value, true, "Setting updated");

        is(setting.visible, true, "Setting is visible by default");
      });

      add_task(async function testPrefSettingCustomValue() {
        const PREF_ID = "test.setting.int";
        const SETTING_ID = "testSettingInt";
        await SpecialPowers.pushPrefEnv({
          set: [[PREF_ID, 7]],
        });
        Preferences.add({
          id: PREF_ID,
          type: "int",
        });
        Preferences.addSetting({
          id: SETTING_ID,
          pref: PREF_ID,
          get(prefVal) {
            return prefVal == 3;
          },
          set(val) {
            return val ? 3 : 7;
          },
        });

        let setting = Preferences.getSetting(SETTING_ID);
        ok(setting, "Got a setting object");

        is(Services.prefs.getIntPref(PREF_ID), 7, "Pref is 7");
        is(setting.value, false, "Setting read from prefs 7 is false");

        let settingChanged = waitForSettingChange(setting);
        setting.userChange(true);
        await settingChanged;

        is(setting.value, true, "Setting is now true");
        is(Services.prefs.getIntPref(PREF_ID), 3, "Pref is 3 now");
      });

      add_task(async function testPrefSettingOnUserChange() {
        const PREF_ID = "test.setting.int2";
        const SETTING_ID = "testSettingOnUserChange";
        await SpecialPowers.pushPrefEnv({
          set: [[PREF_ID, 7]],
        });
        let inputValue;
        Preferences.add({
          id: PREF_ID,
          type: "int",
        });
        Preferences.addSetting({
          id: SETTING_ID,
          pref: PREF_ID,
          onUserChange: val => {
            inputValue = val;
          },
        });

        let setting = Preferences.getSetting(SETTING_ID);
        ok(setting, "Got a setting object");

        is(Services.prefs.getIntPref(PREF_ID), 7, "Pref is 7");
        is(setting.value, 7, "Setting read from prefs is 7");
        ok(!inputValue, "Callback has not fired");

        let settingChanged = waitForSettingChange(setting);
        setting.userChange(9);
        await settingChanged;

        is(setting.value, 9, "Setting is now 9");
        is(Services.prefs.getIntPref(PREF_ID), 9, "Pref is 9 now");
        is(inputValue, 9, "Callback set inputValue to 9");
      });

      add_task(async function testPrefSettingOnUserClick() {
        const PREF_ID = "test.setting.click";
        const SETTING_ID = "testSettingOnUserClick";
        await SpecialPowers.pushPrefEnv({
          set: [[PREF_ID, false]],
        });

        let clickValue;
        Preferences.add({
          id: PREF_ID,
          type: "bool",
        });
        Preferences.addSetting({
          id: SETTING_ID,
          pref: PREF_ID,
          onUserClick: event => {
            clickValue = event.detail.value;
          },
        });

        let setting = Preferences.getSetting(SETTING_ID);
        ok(setting, "Got a setting object");

        ok(!clickValue, "onUserClick callback has not fired.");

        let testEvent = new CustomEvent("click", { detail: { value: true } });
        setting.userClick(testEvent);

        is(clickValue, true, "Callback set clickValue to true.");
      });

      add_task(async function testPrefSettingVisible() {
        const PREF_ID = "test.setting.visible";
        const SETTING_ID = "testSettingVisible";
        await SpecialPowers.pushPrefEnv({
          set: [[PREF_ID, 5]],
        });
        Preferences.add({
          id: PREF_ID,
          type: "int",
        });
        Preferences.addSetting({
          id: SETTING_ID,
          pref: PREF_ID,
          visible: () => false,
        });

        let setting = Preferences.getSetting(SETTING_ID);
        ok(setting, "Got a setting object");

        is(setting.visible, false, "Setting is not visible");
      });

      add_task(async function testPrefSettingDisabled() {
        const DEFAULT_SETTING_ID = "testSettingDisabledDefault";
        const DISABLED_SETTING_ID = "testSettingDisabled";
        Preferences.addSetting({
          id: DEFAULT_SETTING_ID,
        });
        Preferences.addSetting({
          id: DISABLED_SETTING_ID,
          disabled: () => true,
        });

        let defaultSetting = Preferences.getSetting(DEFAULT_SETTING_ID);
        ok(!defaultSetting.disabled, "Setting is not disabled by default");

        let disabledSetting = Preferences.getSetting(DISABLED_SETTING_ID);
        ok(disabledSetting.disabled, "Setting disabled is called");
      });

      add_task(async function testPrefIsLocked() {
        const PREF_ID = "test.setting.locked";
        const SETTING_ID = "testSettingLocked";
        const NO_PREF_ID = "noSettingLocked";
        await SpecialPowers.pushPrefEnv({
          set: [[PREF_ID, false]],
        });
        Preferences.add({
          id: PREF_ID,
          type: "bool",
        });
        Preferences.addSetting({
          id: SETTING_ID,
          pref: PREF_ID,
        });
        Preferences.addSetting({
          id: NO_PREF_ID,
          get: () => true,
        });

        let setting = Preferences.getSetting(SETTING_ID);
        ok(setting, "Got a setting object");

        is(setting.locked, false, "Setting is not locked");

        Services.prefs.lockPref(PREF_ID);
        is(setting.locked, true, "Setting is locked");

        Services.prefs.unlockPref(PREF_ID);
        is(setting.locked, false, "Setting is not unlocked");

        let noPrefSetting = Preferences.getSetting(NO_PREF_ID);
        ok(noPrefSetting, "Got a setting not backed by prefs");
        is(noPrefSetting.locked, false, "Non-pref setting is not locked");
      });

      add_task(async function testGetControlConfig() {
        const SETTING_ID = "getControlConfigSetting";
        const NO_CONFIG_ID = "noGetControlConfigSetting";
        const BASE_CONFIG = { original: "settings" };

        Preferences.addSetting({
          id: SETTING_ID,
          getControlConfig: config => ({ ...config, added: "config" }),
        });
        Preferences.addSetting({ id: NO_CONFIG_ID });

        let setting = Preferences.getSetting(SETTING_ID);
        let controlConfig = setting.getControlConfig(BASE_CONFIG);
        is(Object.keys(controlConfig).length, 2, "There are now 2 config keys");
        is(
          controlConfig.original,
          "settings",
          "The original config is passed in"
        );
        is(controlConfig.added, "config", "The config was updated");

        let noConfigSetting = Preferences.getSetting(NO_CONFIG_ID);
        let noConfigControlConfig =
          noConfigSetting.getControlConfig(BASE_CONFIG);
        is(
          noConfigControlConfig,
          BASE_CONFIG,
          "There's a default implementation"
        );
      });

      add_task(async function testDepsChange() {
        const DEP_PREF_ID = "test.depsChange.dep";
        const DEP_SETTING_ID = "testDepsChangeDep";
        await SpecialPowers.pushPrefEnv({
          set: [[DEP_PREF_ID, true]],
        });
        Preferences.add({ id: DEP_PREF_ID, type: "bool" });
        Preferences.addSetting({
          id: DEP_SETTING_ID,
          pref: DEP_PREF_ID,
        });

        const PARENT_SETTING_ID = "testDepsChangeParent";
        Preferences.addSetting({
          id: PARENT_SETTING_ID,
          deps: [DEP_SETTING_ID],
          visible: deps => deps[DEP_SETTING_ID].value,
        });

        const parentSetting = Preferences.getSetting(PARENT_SETTING_ID);

        ok(parentSetting.visible, "Parent setting is visible initially");

        let settingChanged = waitForSettingChange(parentSetting);
        Services.prefs.setBoolPref(DEP_PREF_ID, false);
        await settingChanged;

        ok(
          !parentSetting.visible,
          "Parent setting is not visible based on dep"
        );
      });

      add_task(async function testDepsToCallbacks() {
        const SETTING_ID = "testDepsToCallbacks";
        const DEP_ID = "testDepsToCallbacksDep";
        const DEP_VALUE = Symbol("DEP_VALUE");
        const INITIAL_VALUE = Symbol("INITIAL_VALUE");

        Preferences.addSetting({
          id: DEP_ID,
          get: () => DEP_VALUE,
        });

        let setupResult;
        let _value = INITIAL_VALUE;
        let _valueWasDepsValue = false;
        let _onUserChangeValue = null;
        Preferences.addSetting({
          id: SETTING_ID,
          deps: [DEP_ID],
          get: (_, deps) =>
            _value == INITIAL_VALUE
              ? INITIAL_VALUE
              : deps[DEP_ID].value == _value,
          set: (val, deps) => {
            _value = val;
            _valueWasDepsValue = val == deps[DEP_ID].value;
          },
          visible: deps => deps[DEP_ID].value,
          disabled: deps => deps[DEP_ID].value,
          getControlConfig: (_, deps) => deps[DEP_ID].value,
          onUserChange: (_, deps) => {
            _onUserChangeValue = deps[DEP_ID].value;
          },
          setup: (_, deps, setting) => {
            setupResult = [deps[DEP_ID].value, setting.value];
          },
        });

        let setting = Preferences.getSetting(SETTING_ID);

        // Check setup gets real Setting objects
        is(setupResult.length, 2, "setup was called");
        is(setupResult[0], DEP_VALUE, "setup gets the deps as a real Setting");
        is(
          setupResult[1],
          INITIAL_VALUE,
          "setup gets the setting as a real Setting"
        );

        // Check the straightforward callbacks.
        is(setting.value, INITIAL_VALUE, "initial value is INITIAL_VALUE");
        is(setting.visible, DEP_VALUE, "visible is passed deps");
        is(setting.disabled, DEP_VALUE, "disabled is passed deps");
        is(
          setting.getControlConfig(),
          DEP_VALUE,
          "getControlConfig() is passed deps"
        );

        // Check set and get
        setting.value = "foo";
        is(_value, "foo", "Value was set to foo");
        is(_valueWasDepsValue, false, "Value was not set to DEP_VALUE");
        is(setting.value, false, "Value changed to false (value != DEP_VALUE)");
        setting.value = DEP_VALUE;
        is(_value, DEP_VALUE, "Value was set to DEP_VALUE");
        is(_valueWasDepsValue, true, "set is passed deps (value == DEP_VALUE)");
        is(setting.value, true, "get is passed deps (value == DEP_VALUE)");

        // Check onUserChange
        is(_onUserChangeValue, null, "_onUserChangeValue is unset");
        setting.userChange("foo");
        is(_onUserChangeValue, DEP_VALUE, "onUserChange() is passed deps");
      });

      // Validate that settings can depend on one another, and that the order
      // they are added in doesn't matter for defining dependencies.
      add_task(async function testCircularDeps() {
        const FIRST_PREF = "test.settings-group.first";
        const FIRST_SETTING = "first-setting";

        const SECOND_PREF = "test.settings-group.second";
        const SECOND_SETTING = "second-setting";

        await SpecialPowers.pushPrefEnv({
          set: [
            [FIRST_PREF, true],
            [SECOND_PREF, true],
          ],
        });
        Preferences.addAll([
          { id: FIRST_PREF, type: "bool" },
          { id: SECOND_PREF, type: "bool" },
        ]);
        Preferences.addSetting({
          id: FIRST_SETTING,
          pref: FIRST_PREF,
          deps: [SECOND_SETTING],
          visible: deps => deps[SECOND_SETTING].value,
        });
        Preferences.addSetting({
          id: SECOND_SETTING,
          pref: SECOND_PREF,
          deps: [FIRST_SETTING],
          visible: deps => deps[FIRST_SETTING].value,
        });

        let firstSetting = Preferences.getSetting(FIRST_SETTING);
        let secondSetting = Preferences.getSetting(SECOND_SETTING);

        ok(firstSetting.visible, "First setting is visible");
        ok(secondSetting.visible, "Second setting is visible");

        let settingChanged = waitForSettingChange(secondSetting);
        Services.prefs.setBoolPref(SECOND_PREF, false);
        await settingChanged;

        ok(!firstSetting.visible, "First setting is no longer visible.");

        settingChanged = waitForSettingChange(secondSetting);
        Services.prefs.setBoolPref(FIRST_PREF, false);
        await settingChanged;

        ok(!secondSetting.visible, "Second setting is no longer visible.");

        // Validate the console output to ensure we haven't cause infinite recursion.
        let consoleOutput = Services.console.getMessageArray();
        let recursionErrors = consoleOutput.filter(
          item =>
            item instanceof Ci.nsIScriptError &&
            item.message.includes("InternalError: too much recursion")
        );
        ok(
          !recursionErrors.length,
          "Circular dependencies don't produce any console errors."
        );
      });

      // Verify that a PreferenceNotAddedError is thrown if adding a
      // Setting with a preference that has not been registered
      // through Preferences.add/addAll.
      add_task(async function testPrefNotRegistered() {
        const PREF_ID = "non_existent_pref";
        const SETTING_ID = "testSettingNoPref";
        try {
          Preferences.addSetting({
            id: SETTING_ID,
            pref: PREF_ID,
          });
        } catch (e) {
          let errorName = e.name;
          is(
            errorName,
            "PreferenceNotAddedError",
            "Adding a Setting without registering the Preference should throw a PreferenceNotAddedError"
          );
        }
      });
    </script>
  </head>
  <body>
    <p id="display"></p>
    <div id="content" style="display: none"></div>
    <pre id="test"></pre>
  </body>
</html>