tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_customizemode_uidensity.js (6827B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const PREF_UI_DENSITY = "browser.uidensity";
      8 const PREF_AUTO_TOUCH_MODE = "browser.touchmode.auto";
      9 
     10 async function testModeMenuitem(mode, modePref) {
     11  await startCustomizing();
     12 
     13  let win = document.getElementById("main-window");
     14  let popupButton = document.getElementById("customization-uidensity-button");
     15  let popup = document.getElementById("customization-uidensity-menu");
     16 
     17  // Show the popup.
     18  let popupShownPromise = popupShown(popup);
     19  EventUtils.synthesizeMouseAtCenter(popupButton, {});
     20  await popupShownPromise;
     21 
     22  let item = document.getElementById(
     23    "customization-uidensity-menuitem-" + mode
     24  );
     25  let normalItem = document.getElementById(
     26    "customization-uidensity-menuitem-normal"
     27  );
     28 
     29  is(
     30    normalItem.getAttribute("active"),
     31    "true",
     32    "Normal mode menuitem should be active by default"
     33  );
     34 
     35  // Hover over the mode menuitem and wait for the event that updates the UI
     36  // density.
     37  let mouseoverPromise = BrowserTestUtils.waitForEvent(item, "mouseover");
     38  EventUtils.synthesizeMouseAtCenter(item, { type: "mouseover" });
     39  await mouseoverPromise;
     40 
     41  is(
     42    win.getAttribute("uidensity"),
     43    mode,
     44    `UI Density should be set to ${mode} on ${mode} menuitem hover`
     45  );
     46 
     47  is(
     48    Services.prefs.getIntPref(PREF_UI_DENSITY),
     49    window.gUIDensity.MODE_NORMAL,
     50    `UI Density pref should still be set to normal on ${mode} menuitem hover`
     51  );
     52 
     53  // Hover the normal menuitem again and check that the UI density reset to normal.
     54  EventUtils.synthesizeMouseAtCenter(normalItem, { type: "mouseover" });
     55  await BrowserTestUtils.waitForCondition(() => !win.hasAttribute("uidensity"));
     56 
     57  ok(
     58    !win.hasAttribute("uidensity"),
     59    `UI Density should be reset when no longer hovering the ${mode} menuitem`
     60  );
     61 
     62  // Select the custom UI density and wait for the popup to be hidden.
     63  let popupHiddenPromise = popupHidden(popup);
     64  EventUtils.synthesizeMouseAtCenter(item, {});
     65  await popupHiddenPromise;
     66 
     67  // Check that the click permanently changed the UI density.
     68  is(
     69    win.getAttribute("uidensity"),
     70    mode,
     71    `UI Density should be set to ${mode} on ${mode} menuitem click`
     72  );
     73  is(
     74    Services.prefs.getIntPref(PREF_UI_DENSITY),
     75    modePref,
     76    `UI Density pref should be set to ${mode} when clicking the ${mode} menuitem`
     77  );
     78 
     79  // Open the popup again.
     80  popupShownPromise = popupShown(popup);
     81  EventUtils.synthesizeMouseAtCenter(popupButton, {});
     82  await popupShownPromise;
     83 
     84  // Check that the menuitem is still active after opening and closing the popup.
     85  is(
     86    item.getAttribute("active"),
     87    "true",
     88    `${mode} mode menuitem should be active`
     89  );
     90 
     91  // Hide the popup again.
     92  popupHiddenPromise = popupHidden(popup);
     93  EventUtils.synthesizeMouseAtCenter(popupButton, {});
     94  await popupHiddenPromise;
     95 
     96  // Check that the menuitem is still active after re-opening customize mode.
     97  await endCustomizing();
     98  await startCustomizing();
     99 
    100  popupShownPromise = popupShown(popup);
    101  EventUtils.synthesizeMouseAtCenter(popupButton, {});
    102  await popupShownPromise;
    103 
    104  is(
    105    item.getAttribute("active"),
    106    "true",
    107    `${mode} mode menuitem should be active after entering and exiting customize mode`
    108  );
    109 
    110  // Click the normal menuitem and check that the density is reset.
    111  popupHiddenPromise = popupHidden(popup);
    112  EventUtils.synthesizeMouseAtCenter(normalItem, {});
    113  await popupHiddenPromise;
    114 
    115  ok(
    116    !win.hasAttribute("uidensity"),
    117    "UI Density should be reset when clicking the normal menuitem"
    118  );
    119 
    120  is(
    121    Services.prefs.getIntPref(PREF_UI_DENSITY),
    122    window.gUIDensity.MODE_NORMAL,
    123    "UI Density pref should be set to normal."
    124  );
    125 
    126  // Show the popup and click on the mode menuitem again to test the
    127  // reset default feature.
    128  popupShownPromise = popupShown(popup);
    129  EventUtils.synthesizeMouseAtCenter(popupButton, {});
    130  await popupShownPromise;
    131 
    132  popupHiddenPromise = popupHidden(popup);
    133  EventUtils.synthesizeMouseAtCenter(item, {});
    134  await popupHiddenPromise;
    135 
    136  is(
    137    win.getAttribute("uidensity"),
    138    mode,
    139    `UI Density should be set to ${mode} on ${mode} menuitem click`
    140  );
    141 
    142  is(
    143    Services.prefs.getIntPref(PREF_UI_DENSITY),
    144    modePref,
    145    `UI Density pref should be set to ${mode} when clicking the ${mode} menuitem`
    146  );
    147 
    148  await gCustomizeMode.reset();
    149 
    150  ok(
    151    !win.hasAttribute("uidensity"),
    152    "UI Density should be reset when clicking the normal menuitem"
    153  );
    154 
    155  is(
    156    Services.prefs.getIntPref(PREF_UI_DENSITY),
    157    window.gUIDensity.MODE_NORMAL,
    158    "UI Density pref should be set to normal."
    159  );
    160 
    161  await endCustomizing();
    162 }
    163 
    164 add_task(async function test_touch_mode_menuitem() {
    165  // OSX doesn't get touch mode for now.
    166  if (AppConstants.platform == "macosx") {
    167    is(
    168      document.getElementById("customization-uidensity-menuitem-touch"),
    169      null,
    170      "There's no touch option on Mac OSX"
    171    );
    172    return;
    173  }
    174 
    175  await testModeMenuitem("touch", window.gUIDensity.MODE_TOUCH);
    176 
    177  // Test the checkbox for automatic Touch Mode transition
    178  // in Windows Tablet Mode.
    179  if (AppConstants.platform == "win") {
    180    await startCustomizing();
    181 
    182    let popupButton = document.getElementById("customization-uidensity-button");
    183    let popup = document.getElementById("customization-uidensity-menu");
    184    let popupShownPromise = popupShown(popup);
    185    EventUtils.synthesizeMouseAtCenter(popupButton, {});
    186    await popupShownPromise;
    187 
    188    let checkbox = document.getElementById(
    189      "customization-uidensity-autotouchmode-checkbox"
    190    );
    191    ok(checkbox.checked, "Checkbox should be checked by default");
    192 
    193    // Test toggling the checkbox.
    194    EventUtils.synthesizeMouseAtCenter(checkbox, {});
    195    is(
    196      Services.prefs.getBoolPref(PREF_AUTO_TOUCH_MODE),
    197      false,
    198      "Automatic Touch Mode is off when the checkbox is unchecked."
    199    );
    200 
    201    EventUtils.synthesizeMouseAtCenter(checkbox, {});
    202    is(
    203      Services.prefs.getBoolPref(PREF_AUTO_TOUCH_MODE),
    204      true,
    205      "Automatic Touch Mode is on when the checkbox is checked."
    206    );
    207 
    208    // Test reset to defaults.
    209    EventUtils.synthesizeMouseAtCenter(checkbox, {});
    210    is(
    211      Services.prefs.getBoolPref(PREF_AUTO_TOUCH_MODE),
    212      false,
    213      "Automatic Touch Mode is off when the checkbox is unchecked."
    214    );
    215 
    216    await gCustomizeMode.reset();
    217    is(
    218      Services.prefs.getBoolPref(PREF_AUTO_TOUCH_MODE),
    219      true,
    220      "Automatic Touch Mode is on when the checkbox is checked."
    221    );
    222  }
    223 });
    224 
    225 add_task(async function cleanup() {
    226  await endCustomizing();
    227 
    228  Services.prefs.clearUserPref(PREF_UI_DENSITY);
    229  Services.prefs.clearUserPref(PREF_AUTO_TOUCH_MODE);
    230 });