summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/preferences/test/browser/head.js
blob: 12cbdb17f129783b6c1d26356474983215e837f7 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* import-globals-from ../../../../base/content/utilityOverlay.js */

async function openNewPrefsTab(paneID, scrollPaneTo, otherArgs) {
  let tabmail = document.getElementById("tabmail");
  let prefsTabMode = tabmail.tabModes.preferencesTab;

  is(prefsTabMode.tabs.length, 0, "Prefs tab is not open");

  let prefsDocument = await new Promise(resolve => {
    Services.obs.addObserver(function documentLoaded(subject) {
      if (subject.URL.startsWith("about:preferences")) {
        Services.obs.removeObserver(documentLoaded, "chrome-document-loaded");
        subject.ownerGlobal.setTimeout(() => resolve(subject));
      }
    }, "chrome-document-loaded");
    openPreferencesTab(paneID, scrollPaneTo, otherArgs);
  });
  ok(prefsDocument.URL.startsWith("about:preferences"), "Prefs tab is open");

  prefsDocument = prefsTabMode.tabs[0].browser.contentDocument;
  let prefsWindow = prefsDocument.ownerGlobal;
  prefsWindow.resizeTo(screen.availWidth, screen.availHeight);

  if (paneID) {
    await new Promise(resolve => prefsWindow.setTimeout(resolve));
    is(
      prefsWindow.gLastCategory.category,
      paneID,
      `Selected pane is ${paneID}`
    );
  } else {
    // If we don't wait here for other scripts to run, they
    // could be in a bad state if our test closes the tab.
    await new Promise(resolve => prefsWindow.setTimeout(resolve));
  }

  registerCleanupOnce();

  await new Promise(resolve => prefsWindow.setTimeout(resolve));
  let container = prefsDocument.getElementById("preferencesContainer");
  if (scrollPaneTo && container.scrollHeight > container.clientHeight) {
    Assert.greater(
      container.scrollTop,
      0,
      "Prefs page did scroll when it was supposed to"
    );
  }
  return { prefsDocument, prefsWindow };
}

async function openExistingPrefsTab(paneID, scrollPaneTo, otherArgs) {
  let tabmail = document.getElementById("tabmail");
  let prefsTabMode = tabmail.tabModes.preferencesTab;

  is(prefsTabMode.tabs.length, 1, "Prefs tab is open");

  let prefsDocument = prefsTabMode.tabs[0].browser.contentDocument;
  let prefsWindow = prefsDocument.ownerGlobal;
  prefsWindow.resizeTo(screen.availWidth, screen.availHeight);

  if (paneID && prefsWindow.gLastCategory.category != paneID) {
    openPreferencesTab(paneID, scrollPaneTo, otherArgs);
  }

  await new Promise(resolve => prefsWindow.setTimeout(resolve));
  is(prefsWindow.gLastCategory.category, paneID, `Selected pane is ${paneID}`);

  if (scrollPaneTo) {
    Assert.greater(
      prefsDocument.getElementById("preferencesContainer").scrollTop,
      0,
      "Prefs page did scroll when it was supposed to"
    );
  }

  registerCleanupOnce();
  return { prefsDocument, prefsWindow };
}

function registerCleanupOnce() {
  if (registerCleanupOnce.alreadyRegistered) {
    return;
  }
  registerCleanupFunction(closePrefsTab);
  registerCleanupOnce.alreadyRegistered = true;
}

async function closePrefsTab() {
  info("Closing prefs tab");
  let tabmail = document.getElementById("tabmail");
  let prefsTab = tabmail.tabModes.preferencesTab.tabs[0];
  if (prefsTab) {
    tabmail.closeTab(prefsTab);
  }
}

/**
 * Tests a checkbox sets the preference is set in the right state when the preferences tab opens,
 * that the preference it relates to is set properly, and any UI elements that should be disabled
 * by it are disabled.
 *
 * Each of the tests arguments is an object describing a test, containing:
 *   checkboxID - the ID of the checkbox to test
 *   pref - the name of a preference,
 *   prefValues - an array of two values: pref value when not checked, pref value when checked
 *                (optional, defaults to [false, true])
 *   enabledElements - an array of CSS selectors (optional)
 *   enabledInverted - if the elements should be disabled when the checkbox is checked (optional)
 *   unaffectedElements - array of CSS selectors that should not be affected by
 *                        the toggling of the checkbox.
 */
async function testCheckboxes(paneID, scrollPaneTo, ...tests) {
  for (let initiallyChecked of [true, false]) {
    info(`Opening ${paneID} with prefs set to ${initiallyChecked}`);

    for (let test of tests) {
      let wantedValue = initiallyChecked;
      if (test.prefValues) {
        wantedValue = wantedValue ? test.prefValues[1] : test.prefValues[0];
      }
      if (typeof wantedValue == "number") {
        Services.prefs.setIntPref(test.pref, wantedValue);
      } else {
        Services.prefs.setBoolPref(test.pref, wantedValue);
      }
    }

    let { prefsDocument, prefsWindow } = await openNewPrefsTab(
      paneID,
      scrollPaneTo
    );

    let testUIState = function (test, checked) {
      let wantedValue = checked;
      if (test.prefValues) {
        wantedValue = wantedValue ? test.prefValues[1] : test.prefValues[0];
      }
      let checkbox = prefsDocument.getElementById(test.checkboxID);
      is(
        checkbox.checked,
        checked,
        wantedValue,
        "Checkbox " + (checked ? "is" : "isn't") + " checked"
      );
      if (typeof wantedValue == "number") {
        is(
          Services.prefs.getIntPref(test.pref, -999),
          wantedValue,
          `Pref is ${wantedValue}`
        );
      } else {
        is(
          Services.prefs.getBoolPref(test.pref),
          wantedValue,
          `Pref is ${wantedValue}`
        );
      }

      if (test.enabledElements) {
        let disabled = checked;
        if (test.enabledInverted) {
          disabled = !disabled;
        }
        for (let selector of test.enabledElements) {
          let elements = prefsDocument.querySelectorAll(selector);
          ok(
            elements.length >= 1,
            `At least one element matched '${selector}'`
          );
          for (let element of elements) {
            is(
              element.disabled,
              !disabled,
              "Element " + (disabled ? "isn't" : "is") + " disabled"
            );
          }
        }
      }
    };

    let testUnaffected = function (ids, states) {
      ids.forEach((sel, index) => {
        let isOk = prefsDocument.querySelector(sel).disabled === states[index];
        is(isOk, true, `Element "${sel}" is unaffected`);
      });
    };

    for (let test of tests) {
      info(`Checking ${test.checkboxID}`);

      let unaffectedSelectors = test.unaffectedElements || [];
      let unaffectedStates = unaffectedSelectors.map(
        sel => prefsDocument.querySelector(sel).disabled
      );

      let checkbox = prefsDocument.getElementById(test.checkboxID);
      checkbox.scrollIntoView(false);
      testUIState(test, initiallyChecked);

      EventUtils.synthesizeMouseAtCenter(checkbox, {}, prefsWindow);
      testUIState(test, !initiallyChecked);
      testUnaffected(unaffectedSelectors, unaffectedStates);

      EventUtils.synthesizeMouseAtCenter(checkbox, {}, prefsWindow);
      testUIState(test, initiallyChecked);
      testUnaffected(unaffectedSelectors, unaffectedStates);
    }

    await closePrefsTab();
  }
}

/**
 * Tests a set of radio buttons is in the right state when the preferences tab opens, and when
 * the selected button changes that the preference it relates to is set properly, and any related
 * UI elements that should be disabled are disabled.
 *
 * Each of the tests arguments is an object describing a test, containing:
 *   pref - the name of an integer preference,
 *   states - an array with each element describing a radio button:
 *     id - the ID of the button to test,
 *     prefValue - the value the pref should be set to
 *     enabledElements - an array of CSS selectors to elements that should be enabled when this
 *                       radio button is selected (optional)
 */
async function testRadioButtons(paneID, scrollPaneTo, ...tests) {
  for (let { pref, states } of tests) {
    for (let initialState of states) {
      info(`Opening ${paneID} with ${pref} set to ${initialState.prefValue}`);

      if (typeof initialState.prefValue == "number") {
        Services.prefs.setIntPref(pref, initialState.prefValue);
      } else if (typeof initialState.prefValue == "boolean") {
        Services.prefs.setBoolPref(pref, initialState.prefValue);
      } else {
        Services.prefs.setCharPref(pref, initialState.prefValue);
      }

      let { prefsDocument, prefsWindow } = await openNewPrefsTab(
        paneID,
        scrollPaneTo
      );

      let testUIState = function (currentState) {
        info(`Testing with ${pref} set to ${currentState.prefValue}`);
        for (let state of states) {
          let isCurrentState = state == currentState;
          let radio = prefsDocument.getElementById(state.id);
          is(radio.selected, isCurrentState, `${state.id}.selected`);

          if (state.enabledElements) {
            for (let selector of state.enabledElements) {
              let elements = prefsDocument.querySelectorAll(selector);
              ok(
                elements.length >= 1,
                `At least one element matched '${selector}'`
              );
              for (let element of elements) {
                is(
                  element.disabled,
                  !isCurrentState,
                  "Element " + (isCurrentState ? "isn't" : "is") + " disabled"
                );
              }
            }
          }
        }
        if (typeof initialState.prefValue == "number") {
          is(
            Services.prefs.getIntPref(pref, -999),
            currentState.prefValue,
            `Pref is ${currentState.prefValue}`
          );
        } else if (typeof initialState.prefValue == "boolean") {
          is(
            Services.prefs.getBoolPref(pref),
            currentState.prefValue,
            `Pref is ${currentState.prefValue}`
          );
        } else {
          is(
            Services.prefs.getCharPref(pref, "FAKE VALUE"),
            currentState.prefValue,
            `Pref is ${currentState.prefValue}`
          );
        }
      };

      // Check the initial setup is correct.
      testUIState(initialState);
      // Cycle through possible values, checking each one.
      for (let state of states) {
        if (state == initialState) {
          continue;
        }
        let radio = prefsDocument.getElementById(state.id);
        radio.scrollIntoView(false);
        EventUtils.synthesizeMouseAtCenter(radio, {}, prefsWindow);
        testUIState(state);
      }
      // Go back to the initial value.
      let initialRadio = prefsDocument.getElementById(initialState.id);
      initialRadio.scrollIntoView(false);
      EventUtils.synthesizeMouseAtCenter(initialRadio, {}, prefsWindow);
      testUIState(initialState);

      await closePrefsTab();
    }
  }
}