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

"use strict";

const { Preferences } = ChromeUtils.importESModule(
  "resource://gre/modules/Preferences.sys.mjs"
);

// List of default preferences that can be used for tests, chosen because they
// have little or no side-effects when they are modified for a brief time. If
// any of these preferences are removed or their default state changes, just
// update the constant to point to a different preference with the same default.
const PREF_BOOLEAN_DEFAULT_TRUE = "accessibility.typeaheadfind.manual";
const PREF_BOOLEAN_USERVALUE_TRUE = "browser.dom.window.dump.enabled";
const PREF_NUMBER_DEFAULT_ZERO = "accessibility.typeaheadfind.casesensitive";
const PREF_STRING_DEFAULT_EMPTY = "browser.helperApps.neverAsk.openFile";
const PREF_STRING_DEFAULT_NOTEMPTY = "accessibility.typeaheadfind.soundURL";
const PREF_STRING_DEFAULT_NOTEMPTY_VALUE = "beep";
const PREF_STRING_LOCALIZED_MISSING = "intl.menuitems.alwaysappendaccesskeys";

// Other preference names used in tests.
const PREF_NEW = "test.aboutconfig.new";

// These tests can be slow to execute because they show all the preferences
// several times, and each time can require a second on some virtual machines.
requestLongerTimeout(2);

class AboutConfigRowTest {
  constructor(element) {
    this.element = element;
  }

  querySelector(selector) {
    return this.element.querySelector(selector);
  }

  get nameCell() {
    return this.querySelector("th");
  }

  get name() {
    return this.nameCell.textContent;
  }

  get valueCell() {
    return this.querySelector("td.cell-value");
  }

  get value() {
    return this.valueCell.textContent;
  }

  /**
   * Text input field when the row is in edit mode.
   */
  get valueInput() {
    return this.valueCell.querySelector("input");
  }

  /**
   * This is normally "edit" or "toggle" based on the preference type, "save"
   * when the row is in edit mode, or "add" when the preference does not exist.
   */
  get editColumnButton() {
    return this.querySelector("td.cell-edit > button");
  }

  /**
   * This can be "reset" or "delete" based on whether a default exists.
   */
  get resetColumnButton() {
    return this.querySelector("td:last-child > button");
  }

  hasClass(className) {
    return this.element.classList.contains(className);
  }
}

class AboutConfigTest {
  static withNewTab(testFn, options = {}) {
    return BrowserTestUtils.withNewTab(
      {
        gBrowser,
        url: "chrome://global/content/aboutconfig/aboutconfig.html",
      },
      async browser => {
        let scope = new this(browser);
        await scope.setupNewTab(options);
        await testFn.call(scope);
      }
    );
  }

  constructor(browser) {
    this.browser = browser;
    this.document = browser.contentDocument;
    this.window = browser.contentWindow;
  }

  async setupNewTab(options) {
    await this.document.l10n.ready;
    if (!options.dontBypassWarning) {
      this.bypassWarningButton.click();
      this.showAll();
    }
  }

  get showWarningNextTimeInput() {
    return this.document.getElementById("showWarningNextTime");
  }

  get bypassWarningButton() {
    return this.document.getElementById("warningButton");
  }

  get searchInput() {
    return this.document.getElementById("about-config-search");
  }

  get showOnlyModifiedCheckbox() {
    return this.document.getElementById("about-config-show-only-modified");
  }

  get prefsTable() {
    return this.document.getElementById("prefs");
  }

  /**
   * Array of AboutConfigRowTest objects, one for each row in the main table.
   */
  get rows() {
    let elements = this.prefsTable.querySelectorAll("tr:not(.hidden)");
    return Array.from(elements, element => new AboutConfigRowTest(element));
  }

  /**
   * Returns the AboutConfigRowTest object for the row in the main table which
   * corresponds to the given preference name, or undefined if none is present.
   */
  getRow(name) {
    return this.rows.find(row => row.name == name);
  }

  /**
   * Shows all preferences using the dedicated button.
   */
  showAll() {
    this.search("");
    this.document.getElementById("show-all").click();
  }

  /**
   * Performs a new search using the dedicated textbox. This also makes sure
   * that the list of preferences displayed is up to date.
   */
  search(value) {
    this.searchInput.value = value;
    this.searchInput.focus();
    EventUtils.sendKey("return");
  }

  /**
   * Checks whether or not the initial warning page is displayed.
   */
  assertWarningPage(expected) {
    Assert.equal(!!this.showWarningNextTimeInput, expected);
    Assert.equal(!!this.bypassWarningButton, expected);
    Assert.equal(!this.searchInput, expected);
    Assert.equal(!this.prefsTable, expected);
  }
}