summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_customizemode_uidensity.js
blob: 9f22341f5661edf24b731eb2cdc1371e3d44e08b (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
/* 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/. */

"use strict";

const PREF_UI_DENSITY = "browser.uidensity";
const PREF_AUTO_TOUCH_MODE = "browser.touchmode.auto";

async function testModeMenuitem(mode, modePref) {
  await startCustomizing();

  let win = document.getElementById("main-window");
  let popupButton = document.getElementById("customization-uidensity-button");
  let popup = document.getElementById("customization-uidensity-menu");

  // Show the popup.
  let popupShownPromise = popupShown(popup);
  EventUtils.synthesizeMouseAtCenter(popupButton, {});
  await popupShownPromise;

  let item = document.getElementById(
    "customization-uidensity-menuitem-" + mode
  );
  let normalItem = document.getElementById(
    "customization-uidensity-menuitem-normal"
  );

  is(
    normalItem.getAttribute("active"),
    "true",
    "Normal mode menuitem should be active by default"
  );

  // Hover over the mode menuitem and wait for the event that updates the UI
  // density.
  let mouseoverPromise = BrowserTestUtils.waitForEvent(item, "mouseover");
  EventUtils.synthesizeMouseAtCenter(item, { type: "mouseover" });
  await mouseoverPromise;

  is(
    win.getAttribute("uidensity"),
    mode,
    `UI Density should be set to ${mode} on ${mode} menuitem hover`
  );

  is(
    Services.prefs.getIntPref(PREF_UI_DENSITY),
    window.gUIDensity.MODE_NORMAL,
    `UI Density pref should still be set to normal on ${mode} menuitem hover`
  );

  // Hover the normal menuitem again and check that the UI density reset to normal.
  EventUtils.synthesizeMouseAtCenter(normalItem, { type: "mouseover" });
  await BrowserTestUtils.waitForCondition(() => !win.hasAttribute("uidensity"));

  ok(
    !win.hasAttribute("uidensity"),
    `UI Density should be reset when no longer hovering the ${mode} menuitem`
  );

  // Select the custom UI density and wait for the popup to be hidden.
  let popupHiddenPromise = popupHidden(popup);
  EventUtils.synthesizeMouseAtCenter(item, {});
  await popupHiddenPromise;

  // Check that the click permanently changed the UI density.
  is(
    win.getAttribute("uidensity"),
    mode,
    `UI Density should be set to ${mode} on ${mode} menuitem click`
  );
  is(
    Services.prefs.getIntPref(PREF_UI_DENSITY),
    modePref,
    `UI Density pref should be set to ${mode} when clicking the ${mode} menuitem`
  );

  // Open the popup again.
  popupShownPromise = popupShown(popup);
  EventUtils.synthesizeMouseAtCenter(popupButton, {});
  await popupShownPromise;

  // Check that the menuitem is still active after opening and closing the popup.
  is(
    item.getAttribute("active"),
    "true",
    `${mode} mode menuitem should be active`
  );

  // Hide the popup again.
  popupHiddenPromise = popupHidden(popup);
  EventUtils.synthesizeMouseAtCenter(popupButton, {});
  await popupHiddenPromise;

  // Check that the menuitem is still active after re-opening customize mode.
  await endCustomizing();
  await startCustomizing();

  popupShownPromise = popupShown(popup);
  EventUtils.synthesizeMouseAtCenter(popupButton, {});
  await popupShownPromise;

  is(
    item.getAttribute("active"),
    "true",
    `${mode} mode menuitem should be active after entering and exiting customize mode`
  );

  // Click the normal menuitem and check that the density is reset.
  popupHiddenPromise = popupHidden(popup);
  EventUtils.synthesizeMouseAtCenter(normalItem, {});
  await popupHiddenPromise;

  ok(
    !win.hasAttribute("uidensity"),
    "UI Density should be reset when clicking the normal menuitem"
  );

  is(
    Services.prefs.getIntPref(PREF_UI_DENSITY),
    window.gUIDensity.MODE_NORMAL,
    "UI Density pref should be set to normal."
  );

  // Show the popup and click on the mode menuitem again to test the
  // reset default feature.
  popupShownPromise = popupShown(popup);
  EventUtils.synthesizeMouseAtCenter(popupButton, {});
  await popupShownPromise;

  popupHiddenPromise = popupHidden(popup);
  EventUtils.synthesizeMouseAtCenter(item, {});
  await popupHiddenPromise;

  is(
    win.getAttribute("uidensity"),
    mode,
    `UI Density should be set to ${mode} on ${mode} menuitem click`
  );

  is(
    Services.prefs.getIntPref(PREF_UI_DENSITY),
    modePref,
    `UI Density pref should be set to ${mode} when clicking the ${mode} menuitem`
  );

  await gCustomizeMode.reset();

  ok(
    !win.hasAttribute("uidensity"),
    "UI Density should be reset when clicking the normal menuitem"
  );

  is(
    Services.prefs.getIntPref(PREF_UI_DENSITY),
    window.gUIDensity.MODE_NORMAL,
    "UI Density pref should be set to normal."
  );

  await endCustomizing();
}

add_task(async function test_touch_mode_menuitem() {
  // OSX doesn't get touch mode for now.
  if (AppConstants.platform == "macosx") {
    is(
      document.getElementById("customization-uidensity-menuitem-touch"),
      null,
      "There's no touch option on Mac OSX"
    );
    return;
  }

  await testModeMenuitem("touch", window.gUIDensity.MODE_TOUCH);

  // Test the checkbox for automatic Touch Mode transition
  // in Windows Tablet Mode.
  if (AppConstants.platform == "win") {
    await startCustomizing();

    let popupButton = document.getElementById("customization-uidensity-button");
    let popup = document.getElementById("customization-uidensity-menu");
    let popupShownPromise = popupShown(popup);
    EventUtils.synthesizeMouseAtCenter(popupButton, {});
    await popupShownPromise;

    let checkbox = document.getElementById(
      "customization-uidensity-autotouchmode-checkbox"
    );
    ok(checkbox.checked, "Checkbox should be checked by default");

    // Test toggling the checkbox.
    EventUtils.synthesizeMouseAtCenter(checkbox, {});
    is(
      Services.prefs.getBoolPref(PREF_AUTO_TOUCH_MODE),
      false,
      "Automatic Touch Mode is off when the checkbox is unchecked."
    );

    EventUtils.synthesizeMouseAtCenter(checkbox, {});
    is(
      Services.prefs.getBoolPref(PREF_AUTO_TOUCH_MODE),
      true,
      "Automatic Touch Mode is on when the checkbox is checked."
    );

    // Test reset to defaults.
    EventUtils.synthesizeMouseAtCenter(checkbox, {});
    is(
      Services.prefs.getBoolPref(PREF_AUTO_TOUCH_MODE),
      false,
      "Automatic Touch Mode is off when the checkbox is unchecked."
    );

    await gCustomizeMode.reset();
    is(
      Services.prefs.getBoolPref(PREF_AUTO_TOUCH_MODE),
      true,
      "Automatic Touch Mode is on when the checkbox is checked."
    );
  }
});

add_task(async function cleanup() {
  await endCustomizing();

  Services.prefs.clearUserPref(PREF_UI_DENSITY);
  Services.prefs.clearUserPref(PREF_AUTO_TOUCH_MODE);
});