tor-browser

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

browser_proton_toolbar_hide_toolbarbuttons.js (8744B)


      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 ChromeUtils.defineESModuleGetters(this, {
      8  HomePage: "resource:///modules/HomePage.sys.mjs",
      9 });
     10 
     11 const kPrefProtonToolbarVersion = "browser.proton.toolbar.version";
     12 const kPrefHomeButtonUsed = "browser.engagement.home-button.has-used";
     13 const kPrefLibraryButtonUsed = "browser.engagement.library-button.has-used";
     14 const kPrefSidebarButtonUsed = "browser.engagement.sidebar-button.has-used";
     15 
     16 async function testToolbarButtons(aActions) {
     17  let {
     18    shouldRemoveHomeButton,
     19    shouldRemoveLibraryButton,
     20    shouldRemoveSidebarButton,
     21    shouldUpdateVersion,
     22  } = aActions;
     23  const defaultPlacements = [
     24    "back-button",
     25    "forward-button",
     26    "stop-reload-button",
     27    "home-button",
     28    "customizableui-special-spring1",
     29    "urlbar-container",
     30    "customizableui-special-spring2",
     31    "downloads-button",
     32    "library-button",
     33    "sidebar-button",
     34    "fxa-toolbar-menu-button",
     35  ];
     36  let oldState = CustomizableUI.getTestOnlyInternalProp("gSavedState");
     37 
     38  Assert.equal(
     39    Services.prefs.getIntPref(kPrefProtonToolbarVersion),
     40    0,
     41    "Toolbar proton version is 0"
     42  );
     43 
     44  let CustomizableUIInternal = CustomizableUI.getTestOnlyInternalProp(
     45    "CustomizableUIInternal"
     46  );
     47 
     48  CustomizableUI.setTestOnlyInternalProp("gSavedState", {
     49    placements: {
     50      "nav-bar": defaultPlacements,
     51    },
     52  });
     53  CustomizableUIInternal.updateForNewProtonVersion();
     54 
     55  let navbarPlacements =
     56    CustomizableUI.getTestOnlyInternalProp("gSavedState").placements["nav-bar"];
     57  let includesHomeButton = navbarPlacements.includes("home-button");
     58  let includesLibraryButton = navbarPlacements.includes("library-button");
     59  let includesSidebarButton = navbarPlacements.includes("sidebar-button");
     60 
     61  Assert.equal(
     62    !includesHomeButton,
     63    shouldRemoveHomeButton,
     64    "Correctly handles home button"
     65  );
     66  Assert.equal(
     67    !includesLibraryButton,
     68    shouldRemoveLibraryButton,
     69    "Correctly handles library button"
     70  );
     71  Assert.equal(
     72    !includesSidebarButton,
     73    shouldRemoveSidebarButton,
     74    "Correctly handles sidebar button"
     75  );
     76 
     77  let toolbarVersion = Services.prefs.getIntPref(kPrefProtonToolbarVersion);
     78  if (shouldUpdateVersion) {
     79    Assert.greaterOrEqual(toolbarVersion, 1, "Toolbar proton version updated");
     80  } else {
     81    Assert.equal(toolbarVersion, 0, "Toolbar proton version not updated");
     82  }
     83 
     84  // Cleanup
     85  CustomizableUI.setTestOnlyInternalProp("gSavedState", oldState);
     86 }
     87 
     88 /**
     89 * Checks that the home button is removed from the nav-bar under
     90 * these conditions: proton must be enabled, the toolbar engagement
     91 * pref is false, and the homepage is about:home or about:blank.
     92 * Otherwise, the home button should remain if it was previously
     93 * in the navbar.
     94 * Also checks that the library button is removed from the nav-bar
     95 * if proton is enabled and the toolbar engagement pref is false.
     96 */
     97 add_task(async function testButtonRemoval() {
     98  // Ensure the engagement prefs are set to their default values
     99  await SpecialPowers.pushPrefEnv({
    100    set: [
    101      [kPrefHomeButtonUsed, false],
    102      [kPrefLibraryButtonUsed, false],
    103      [kPrefSidebarButtonUsed, false],
    104    ],
    105  });
    106 
    107  let tests = [
    108    // Proton enabled without home and library engagement
    109    {
    110      prefs: [],
    111      actions: {
    112        shouldRemoveHomeButton: true,
    113        shouldRemoveLibraryButton: true,
    114        shouldRemoveSidebarButton: true,
    115        shouldUpdateVersion: true,
    116      },
    117    },
    118    // Proton enabled with home engagement
    119    {
    120      prefs: [[kPrefHomeButtonUsed, true]],
    121      actions: {
    122        shouldRemoveHomeButton: false,
    123        shouldRemoveLibraryButton: true,
    124        shouldRemoveSidebarButton: true,
    125        shouldUpdateVersion: true,
    126      },
    127    },
    128    // Proton enabled with custom homepage
    129    {
    130      prefs: [],
    131      actions: {
    132        shouldRemoveHomeButton: false,
    133        shouldRemoveLibraryButton: true,
    134        shouldRemoveSidebarButton: true,
    135        shouldUpdateVersion: true,
    136      },
    137      async fn() {
    138        HomePage.safeSet("https://example.com");
    139      },
    140    },
    141    // Proton enabled with library engagement
    142    {
    143      prefs: [[kPrefLibraryButtonUsed, true]],
    144      actions: {
    145        shouldRemoveHomeButton: true,
    146        shouldRemoveLibraryButton: false,
    147        shouldRemoveSidebarButton: true,
    148        shouldUpdateVersion: true,
    149      },
    150    },
    151    // Proton enabled with sidebar engagement
    152    {
    153      prefs: [[kPrefSidebarButtonUsed, true]],
    154      actions: {
    155        shouldRemoveHomeButton: true,
    156        shouldRemoveLibraryButton: true,
    157        shouldRemoveSidebarButton: false,
    158        shouldUpdateVersion: true,
    159      },
    160    },
    161  ];
    162 
    163  for (let test of tests) {
    164    await SpecialPowers.pushPrefEnv({
    165      set: [[kPrefProtonToolbarVersion, 0], ...test.prefs],
    166    });
    167    if (test.fn) {
    168      await test.fn();
    169    }
    170    testToolbarButtons(test.actions);
    171    HomePage.reset();
    172    await SpecialPowers.popPrefEnv();
    173  }
    174 });
    175 
    176 /**
    177 * Checks that a null saved state (new profile) does not prevent migration.
    178 */
    179 add_task(async function testNullSavedState() {
    180  await SpecialPowers.pushPrefEnv({
    181    set: [[kPrefProtonToolbarVersion, 0]],
    182  });
    183  let oldState = CustomizableUI.getTestOnlyInternalProp("gSavedState");
    184 
    185  Assert.equal(
    186    Services.prefs.getIntPref(kPrefProtonToolbarVersion),
    187    0,
    188    "Toolbar proton version is 0"
    189  );
    190 
    191  let CustomizableUIInternal = CustomizableUI.getTestOnlyInternalProp(
    192    "CustomizableUIInternal"
    193  );
    194  // Calling initialize() wants to add this observer again.
    195  // TODO: Having a test-only testReset() method could avoid this hack.
    196  Services.obs.removeObserver(
    197    CustomizableUIInternal,
    198    "browser-set-toolbar-visibility"
    199  );
    200  CustomizableUIInternal.initialize();
    201 
    202  Assert.greaterOrEqual(
    203    Services.prefs.getIntPref(kPrefProtonToolbarVersion),
    204    1,
    205    "Toolbar proton version updated"
    206  );
    207  let navbarPlacements = CustomizableUI.getTestOnlyInternalProp("gAreas")
    208    .get("nav-bar")
    209    .get("defaultPlacements");
    210  Assert.ok(
    211    !navbarPlacements.includes("home-button"),
    212    "Home button isn't included by default"
    213  );
    214  Assert.ok(
    215    !navbarPlacements.includes("library-button"),
    216    "Library button isn't included by default"
    217  );
    218  if (!Services.prefs.getBoolPref("sidebar.revamp", false)) {
    219    Assert.ok(
    220      !navbarPlacements.includes("sidebar-button"),
    221      "Sidebar button isn't included by default"
    222    );
    223  } else {
    224    Assert.ok(
    225      navbarPlacements.includes("sidebar-button"),
    226      "Sidebar button is included by default"
    227    );
    228  }
    229 
    230  // Cleanup
    231  CustomizableUI.setTestOnlyInternalProp("gSavedState", oldState);
    232  await SpecialPowers.popPrefEnv();
    233 
    234  // Re-initialize to prevent future test failures
    235  Services.obs.removeObserver(
    236    CustomizableUIInternal,
    237    "browser-set-toolbar-visibility"
    238  );
    239  CustomizableUIInternal.initialize();
    240 });
    241 
    242 /**
    243 * Checks that a saved state that is missing nav-bar placements does not prevent migration.
    244 */
    245 add_task(async function testNoNavbarPlacements() {
    246  await SpecialPowers.pushPrefEnv({
    247    set: [[kPrefProtonToolbarVersion, 0]],
    248  });
    249 
    250  let oldState = CustomizableUI.getTestOnlyInternalProp("gSavedState");
    251 
    252  Assert.equal(
    253    Services.prefs.getIntPref(kPrefProtonToolbarVersion),
    254    0,
    255    "Toolbar proton version is 0"
    256  );
    257 
    258  let CustomizableUIInternal = CustomizableUI.getTestOnlyInternalProp(
    259    "CustomizableUIInternal"
    260  );
    261 
    262  CustomizableUI.setTestOnlyInternalProp("gSavedState", {
    263    placements: { "widget-overflow-fixed-list": [] },
    264  });
    265  CustomizableUIInternal.updateForNewProtonVersion();
    266 
    267  Assert.ok(true, "_updateForNewProtonVersion didn't throw");
    268 
    269  // Cleanup
    270  CustomizableUI.setTestOnlyInternalProp("gSavedState", oldState);
    271 
    272  await SpecialPowers.popPrefEnv();
    273 });
    274 
    275 /**
    276 * Checks that a saved state that is missing the placements value does not prevent migration.
    277 */
    278 add_task(async function testNullPlacements() {
    279  await SpecialPowers.pushPrefEnv({
    280    set: [[kPrefProtonToolbarVersion, 0]],
    281  });
    282 
    283  let oldState = CustomizableUI.getTestOnlyInternalProp("gSavedState");
    284 
    285  Assert.equal(
    286    Services.prefs.getIntPref(kPrefProtonToolbarVersion),
    287    0,
    288    "Toolbar proton version is 0"
    289  );
    290 
    291  let CustomizableUIInternal = CustomizableUI.getTestOnlyInternalProp(
    292    "CustomizableUIInternal"
    293  );
    294 
    295  CustomizableUI.setTestOnlyInternalProp("gSavedState", {});
    296  CustomizableUIInternal.updateForNewProtonVersion();
    297 
    298  Assert.ok(true, "_updateForNewProtonVersion didn't throw");
    299 
    300  // Cleanup
    301  CustomizableUI.setTestOnlyInternalProp("gSavedState", oldState);
    302 
    303  await SpecialPowers.popPrefEnv();
    304 });