summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutconfig/test/browser/browser_edit.js
blob: 24fb168e7642a57f3195474d84bb4ccc7e0c9ccc (plain)
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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const PREF_MODIFY_PREFIX = "test.aboutconfig.modify";
const PREF_MODIFY_BOOLEAN = "test.aboutconfig.modify.boolean";
const PREF_MODIFY_NUMBER = "test.aboutconfig.modify.number";
const PREF_MODIFY_STRING = "test.aboutconfig.modify.string";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      [PREF_MODIFY_BOOLEAN, true],
      [PREF_MODIFY_NUMBER, 1337],
      [
        PREF_MODIFY_STRING,
        "the answer to the life the universe and everything",
      ],
    ],
  });

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref(PREF_BOOLEAN_DEFAULT_TRUE);
    Services.prefs.clearUserPref(PREF_NUMBER_DEFAULT_ZERO);
    Services.prefs.clearUserPref(PREF_STRING_DEFAULT_EMPTY);
  });
});

add_task(async function test_add_user_pref() {
  Assert.equal(
    Services.prefs.getPrefType(PREF_NEW),
    Ci.nsIPrefBranch.PREF_INVALID
  );

  await AboutConfigTest.withNewTab(async function () {
    // The row for a new preference appears when searching for its name.
    Assert.ok(!this.getRow(PREF_NEW));

    for (let [radioIndex, expectedValue, expectedEditingMode] of [
      [0, true, false],
      [1, 0, true],
      [2, "", true],
    ]) {
      this.search(PREF_NEW);
      let row = this.getRow(PREF_NEW);
      Assert.ok(row.hasClass("deleted"));
      Assert.ok(row.hasClass("add"));

      // Adding the preference should set the default for the data type.
      row.element.querySelectorAll("input")[radioIndex].click();
      row.editColumnButton.click();
      Assert.ok(!row.hasClass("deleted"));
      Assert.ok(!row.hasClass("add"));
      Assert.ok(Preferences.get(PREF_NEW) === expectedValue);

      // Number and String preferences should be in edit mode.
      Assert.equal(!!row.valueInput, expectedEditingMode);

      // Repeat the search to verify that the preference remains.
      this.search(PREF_NEW);
      row = this.getRow(PREF_NEW);
      Assert.ok(!row.hasClass("deleted"));
      Assert.ok(!row.hasClass("add"));
      Assert.ok(!row.valueInput);

      // Reset the preference, then continue by adding a different type.
      row.resetColumnButton.click();
      Assert.equal(
        Services.prefs.getPrefType(PREF_NEW),
        Ci.nsIPrefBranch.PREF_INVALID
      );
    }
  });
});

add_task(async function test_delete_user_pref() {
  for (let [radioIndex, testValue] of [
    [0, false],
    [1, -1],
    [2, "value"],
  ]) {
    Preferences.set(PREF_NEW, testValue);
    await AboutConfigTest.withNewTab(async function () {
      // Deleting the preference should keep the row.
      let row = this.getRow(PREF_NEW);
      row.resetColumnButton.click();
      Assert.ok(row.hasClass("deleted"));
      Assert.equal(
        Services.prefs.getPrefType(PREF_NEW),
        Ci.nsIPrefBranch.PREF_INVALID
      );

      // Re-adding the preference should keep the same value.
      Assert.ok(row.element.querySelectorAll("input")[radioIndex].checked);
      row.editColumnButton.click();
      Assert.ok(!row.hasClass("deleted"));
      Assert.ok(Preferences.get(PREF_NEW) === testValue);

      // Filtering again after deleting should remove the row.
      row.resetColumnButton.click();
      this.showAll();
      Assert.ok(!this.getRow(PREF_NEW));
    });
  }
});

add_task(async function test_click_type_label_multiple_forms() {
  // This test displays the row to add a preference while other preferences are
  // also displayed, and tries to select the type of the new preference by
  // clicking the label next to the radio button. This should work even if the
  // user has deleted a different preference, and multiple forms are displayed.
  const PREF_TO_DELETE = "test.aboutconfig.modify.boolean";
  const PREF_NEW_WHILE_DELETED = "test.aboutconfig.modify.";

  await AboutConfigTest.withNewTab(async function () {
    this.search(PREF_NEW_WHILE_DELETED);

    // This preference will remain deleted during the test.
    let existingRow = this.getRow(PREF_TO_DELETE);
    existingRow.resetColumnButton.click();

    let newRow = this.getRow(PREF_NEW_WHILE_DELETED);

    for (let [radioIndex, expectedValue] of [
      [0, true],
      [1, 0],
      [2, ""],
    ]) {
      let radioLabels = newRow.element.querySelectorAll("label > span");
      await this.document.l10n.translateElements(radioLabels);

      // Even if this is the second form on the page, the click should select
      // the radio button next to the label, not the one on the first form.
      EventUtils.synthesizeMouseAtCenter(
        radioLabels[radioIndex],
        {},
        this.browser.contentWindow
      );

      // Adding the preference should set the default for the data type.
      newRow.editColumnButton.click();
      Assert.ok(Preferences.get(PREF_NEW_WHILE_DELETED) === expectedValue);

      // Reset the preference, then continue by adding a different type.
      newRow.resetColumnButton.click();
    }

    // Re-adding the deleted preference should restore the value.
    existingRow.editColumnButton.click();
    Assert.ok(Preferences.get(PREF_TO_DELETE) === true);
  });
});

add_task(async function test_reset_user_pref() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [PREF_BOOLEAN_DEFAULT_TRUE, false],
      [PREF_STRING_LOCALIZED_MISSING, "user-value"],
    ],
  });

  await AboutConfigTest.withNewTab(async function () {
    // Click reset.
    let row = this.getRow(PREF_BOOLEAN_DEFAULT_TRUE);
    row.resetColumnButton.click();
    // Check new layout and reset.
    Assert.ok(!row.hasClass("has-user-value"));
    Assert.ok(!row.resetColumnButton);
    Assert.ok(!Services.prefs.prefHasUserValue(PREF_BOOLEAN_DEFAULT_TRUE));
    Assert.equal(this.getRow(PREF_BOOLEAN_DEFAULT_TRUE).value, "true");

    // Filter again to test the preference cache.
    this.showAll();
    row = this.getRow(PREF_BOOLEAN_DEFAULT_TRUE);
    Assert.ok(!row.hasClass("has-user-value"));
    Assert.ok(!row.resetColumnButton);
    Assert.equal(this.getRow(PREF_BOOLEAN_DEFAULT_TRUE).value, "true");

    // Clicking reset on a localized preference without a corresponding value.
    row = this.getRow(PREF_STRING_LOCALIZED_MISSING);
    Assert.equal(row.value, "user-value");
    row.resetColumnButton.click();
    // Check new layout and reset.
    Assert.ok(!row.hasClass("has-user-value"));
    Assert.ok(!row.resetColumnButton);
    Assert.ok(!Services.prefs.prefHasUserValue(PREF_STRING_LOCALIZED_MISSING));
    Assert.equal(this.getRow(PREF_STRING_LOCALIZED_MISSING).value, "");
  });
});

add_task(async function test_modify() {
  await AboutConfigTest.withNewTab(async function () {
    // Test toggle for boolean prefs.
    for (let nameOfBoolPref of [
      PREF_MODIFY_BOOLEAN,
      PREF_BOOLEAN_DEFAULT_TRUE,
    ]) {
      let row = this.getRow(nameOfBoolPref);
      // Do this a two times to reset the pref.
      for (let i = 0; i < 2; i++) {
        row.editColumnButton.click();
        // Check new layout and saving in backend.
        Assert.equal(
          this.getRow(nameOfBoolPref).value,
          "" + Preferences.get(nameOfBoolPref)
        );
        let prefHasUserValue = Services.prefs.prefHasUserValue(nameOfBoolPref);
        Assert.equal(row.hasClass("has-user-value"), prefHasUserValue);
        Assert.equal(!!row.resetColumnButton, prefHasUserValue);
      }
    }

    // Test abort of edit by starting with string and continuing with editing Int pref.
    let row = this.getRow(PREF_MODIFY_STRING);
    row.editColumnButton.click();
    row.valueInput.value = "test";
    let intRow = this.getRow(PREF_MODIFY_NUMBER);
    intRow.editColumnButton.click();
    Assert.equal(intRow.valueInput.value, Preferences.get(PREF_MODIFY_NUMBER));
    Assert.ok(!row.valueInput);
    Assert.equal(row.value, Preferences.get(PREF_MODIFY_STRING));

    // Test validation of integer values.
    for (let invalidValue of [
      "",
      " ",
      "a",
      "1.5",
      "-2147483649",
      "2147483648",
    ]) {
      intRow.valueInput.value = invalidValue;
      intRow.editColumnButton.click();
      // We should still be in edit mode.
      Assert.ok(intRow.valueInput);
    }

    // Test correct saving and DOM-update.
    for (let [prefName, willDelete] of [
      [PREF_MODIFY_STRING, true],
      [PREF_MODIFY_NUMBER, true],
      [PREF_NUMBER_DEFAULT_ZERO, false],
      [PREF_STRING_DEFAULT_EMPTY, false],
    ]) {
      row = this.getRow(prefName);
      // Activate edit and check displaying.
      row.editColumnButton.click();
      Assert.equal(row.valueInput.value, Preferences.get(prefName));
      row.valueInput.value = "42";
      // Save and check saving.
      row.editColumnButton.click();
      Assert.equal(Preferences.get(prefName), "42");
      Assert.equal(row.value, "42");
      Assert.ok(row.hasClass("has-user-value"));
      // Reset or delete the preference while editing.
      row.editColumnButton.click();
      Assert.equal(row.valueInput.value, Preferences.get(prefName));
      row.resetColumnButton.click();
      Assert.ok(!row.hasClass("has-user-value"));
      Assert.equal(row.hasClass("deleted"), willDelete);
    }
  });

  // This test would have opened the invalid form popup, so just close it so as not to
  // affect later tests.
  let invalidFormPopup = window.document.getElementById("invalid-form-popup");
  invalidFormPopup.hidePopup();
  await BrowserTestUtils.waitForCondition(() => {
    return invalidFormPopup.state == "closed";
  }, "form validation popup closed");
});

add_task(async function test_edit_field_selected() {
  let prefsToCheck = [
    [PREF_MODIFY_STRING, "A string", "A new string"],
    [PREF_MODIFY_NUMBER, "100", "500"],
  ];
  await AboutConfigTest.withNewTab(async function () {
    for (let [prefName, startValue, endValue] of prefsToCheck) {
      Preferences.set(prefName, startValue);
      let row = this.getRow(prefName);

      Assert.equal(row.value, startValue);
      row.editColumnButton.click();
      Assert.equal(row.valueInput.value, startValue);
      Assert.equal(
        row.valueInput.getAttribute("aria-label"),
        prefName,
        "The input field is labeled from the pref name"
      );

      EventUtils.sendString(endValue, this.window);

      row.editColumnButton.click();
      Assert.equal(row.value, endValue);
      Assert.equal(Preferences.get(prefName), endValue);
    }
  });
});

add_task(async function test_escape_cancels_edit() {
  await AboutConfigTest.withNewTab(async function () {
    let row = this.getRow(PREF_MODIFY_STRING);
    Preferences.set(PREF_MODIFY_STRING, "Edit me, maybe");

    for (let blurInput of [false, true]) {
      Assert.ok(!row.valueInput);
      row.editColumnButton.click();

      Assert.ok(row.valueInput);

      Assert.equal(row.valueInput.value, "Edit me, maybe");
      row.valueInput.value = "Edited";

      // Test both cases of the input being focused and not being focused.
      if (blurInput) {
        row.valueInput.blur();
        Assert.notEqual(this.document.activeElement, row.valueInput);
      } else {
        Assert.equal(this.document.activeElement, row.valueInput);
      }

      EventUtils.synthesizeKey("KEY_Escape", {}, this.window);

      Assert.ok(!row.valueInput);
      Assert.equal(row.value, "Edit me, maybe");
      Assert.equal(row.value, Preferences.get(PREF_MODIFY_STRING));
    }
  });
});

add_task(async function test_double_click_modify() {
  Preferences.set(PREF_MODIFY_BOOLEAN, true);
  Preferences.set(PREF_MODIFY_NUMBER, 10);
  Preferences.set(PREF_MODIFY_STRING, "Hello!");

  await AboutConfigTest.withNewTab(async function () {
    this.search(PREF_MODIFY_PREFIX);

    let click = (target, opts) =>
      EventUtils.synthesizeMouseAtCenter(target, opts, this.window);
    let doubleClick = target => {
      // We intentionally turn off this a11y check, because the following series
      // of clicks (in these test cases) is either performing an activation of
      // the edit mode for prefs or selecting a text in focused inputs. The
      // edit mode can be activated with a separate "Edit" or "Toggle" button
      // provided for each pref, and the text selection can be performed with
      // caret browsing (when supported). Thus, this rule check can be ignored
      // by a11y_checks suite.
      AccessibilityUtils.setEnv({ mustHaveAccessibleRule: false });
      // Trigger two mouse events to simulate the first then second click.
      click(target, { clickCount: 1 });
      click(target, { clickCount: 2 });
      AccessibilityUtils.resetEnv();
    };
    let tripleClick = target => {
      // We intentionally turn off this a11y check, because the following series
      // of clicks is purposefully targeting a non - interactive text content.
      // This action does not require the element to have an interactive
      // accessible to be done by assistive technology with caret browsing
      // (when supported), this rule check shall be ignored by a11y_checks suite.
      AccessibilityUtils.setEnv({ mustHaveAccessibleRule: false });
      // Trigger all 3 mouse events to simulate the three mouse events we'd see.
      click(target, { clickCount: 1 });
      click(target, { clickCount: 2 });
      click(target, { clickCount: 3 });
      AccessibilityUtils.resetEnv();
    };

    // Check double-click to edit a boolean.
    let boolRow = this.getRow(PREF_MODIFY_BOOLEAN);
    Assert.equal(boolRow.value, "true");
    doubleClick(boolRow.valueCell);
    Assert.equal(boolRow.value, "false");
    doubleClick(boolRow.nameCell);
    Assert.equal(boolRow.value, "true");

    // Check double-click to edit a number.
    let intRow = this.getRow(PREF_MODIFY_NUMBER);
    Assert.equal(intRow.value, 10);
    doubleClick(intRow.valueCell);
    Assert.equal(this.document.activeElement, intRow.valueInput);
    EventUtils.sendString("75");
    EventUtils.synthesizeKey("KEY_Enter");
    Assert.equal(intRow.value, 75);

    // Check double-click focuses input when already editing.
    Assert.equal(intRow.value, 75);
    doubleClick(intRow.nameCell);
    Assert.equal(this.document.activeElement, intRow.valueInput);
    intRow.valueInput.blur();
    Assert.notEqual(this.document.activeElement, intRow.valueInput);
    doubleClick(intRow.nameCell);
    Assert.equal(this.document.activeElement, intRow.valueInput);
    EventUtils.sendString("20");
    EventUtils.synthesizeKey("KEY_Enter");
    Assert.equal(intRow.value, 20);

    // Check double-click to edit a string.
    let stringRow = this.getRow(PREF_MODIFY_STRING);
    Assert.equal(stringRow.value, "Hello!");
    doubleClick(stringRow.valueCell);
    Assert.equal(
      this.document.activeElement,
      stringRow.valueInput,
      "The input is focused"
    );
    EventUtils.sendString("New String!");
    EventUtils.synthesizeKey("KEY_Enter");
    Assert.equal(stringRow.value, "New String!");

    // Check triple-click also edits the pref and selects the text inside.
    tripleClick(stringRow.nameCell);
    Assert.equal(
      this.document.activeElement,
      stringRow.valueInput,
      "The input is focused"
    );

    // Check double-click inside input selects a word.
    let newString = "Another string...";
    EventUtils.sendString(newString);
    Assert.equal(this.window.getSelection().toString(), "");
    let stringInput = stringRow.valueInput;
    doubleClick(stringInput);
    let selectionLength = stringInput.selectionEnd - stringInput.selectionStart;
    Assert.greater(selectionLength, 0);
    Assert.less(selectionLength, newString.length);
    EventUtils.synthesizeKey("KEY_Enter");
    Assert.equal(stringRow.value, newString);

    // Check that double/triple-click on the add row selects text as usual.
    let addRow = this.getRow(PREF_MODIFY_PREFIX);
    Assert.ok(addRow.hasClass("deleted"));
    doubleClick(addRow.nameCell);
    Assert.ok(PREF_MODIFY_PREFIX.includes(this.window.getSelection()));
    tripleClick(addRow.nameCell);
    Assert.equal(this.window.getSelection().toString(), PREF_MODIFY_PREFIX);
    // Make sure the localized text is set in the value cell.
    let labels = Array.from(addRow.valueCell.querySelectorAll("label > span"));
    await this.document.l10n.translateElements(labels);
    Assert.ok(labels.every(label => !!label.textContent));
    // Double-click the first input label text.
    doubleClick(labels[0]);
    Assert.equal(this.window.getSelection().toString(), labels[0].textContent);
    tripleClick(addRow.valueCell.querySelector("label > span"));
    Assert.equal(
      this.window.getSelection().toString(),
      labels.map(l => l.textContent).join("")
    );
  });
});