tor-browser

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

browser_defaultbrowser_alwayscheck.js (7141B)


      1 "use strict";
      2 
      3 /**
      4 * Sets up initial prefs and opens about:preferences page.
      5 *
      6 * @returns {Promise<void>}
      7 */
      8 async function setup() {
      9  await SpecialPowers.pushPrefEnv({
     10    set: [["browser.shell.checkDefaultBrowser", false]],
     11  });
     12  await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:preferences");
     13 }
     14 
     15 /**
     16 * Closes out the about:preferences tab and clears out
     17 * any prefs that could have potentially been manipulated.
     18 *
     19 * @returns {void}
     20 */
     21 function teardown() {
     22  Services.prefs.unlockPref("browser.shell.checkDefaultBrowser");
     23  gBrowser.removeCurrentTab();
     24 }
     25 
     26 /**
     27 * Sets up the 'Make default' mock service to mimic
     28 * whether the user has set the browser as default already or not.
     29 *
     30 * @param {{isDefault: boolean}} options
     31 */
     32 async function setupInitialBrowserDefaultSetting(options) {
     33  const win = gBrowser.selectedBrowser.contentWindow;
     34  win.oldShellService = win.getShellService();
     35  const { isDefault } = options;
     36 
     37  const mockShellService = {
     38    _isDefault: isDefault,
     39    isDefaultBrowser() {
     40      return this._isDefault;
     41    },
     42    async setDefaultBrowser() {
     43      this._isDefault = true;
     44    },
     45  };
     46  win.getShellService = function () {
     47    return mockShellService;
     48  };
     49 }
     50 
     51 add_task(
     52  /**
     53   * Tests when clicking 'Make default' button, setting browser
     54   * to default, and the side effects of the 'Always check' checkbox.
     55   */
     56  async function clicking_make_default_checks_alwaysCheck_checkbox() {
     57    await setup();
     58    await setupInitialBrowserDefaultSetting({ isDefault: false });
     59 
     60    let checkDefaultBrowserState = isDefault => {
     61      let isDefaultPane = content.document.getElementById("isDefaultPane");
     62      let isNotDefaultPane =
     63        content.document.getElementById("isNotDefaultPane");
     64 
     65      Assert.equal(
     66        BrowserTestUtils.isHidden(isDefaultPane.control),
     67        !isDefault,
     68        "The 'browser is default' pane should be hidden when browser is not default"
     69      );
     70      Assert.equal(
     71        BrowserTestUtils.isHidden(isNotDefaultPane.control),
     72        isDefault,
     73        "The 'make default' pane should be hidden when browser is default"
     74      );
     75    };
     76 
     77    checkDefaultBrowserState(false);
     78 
     79    const alwaysCheck = content.document.getElementById("alwaysCheckDefault");
     80 
     81    Assert.ok(!alwaysCheck.checked, "Always Check is unchecked by default");
     82 
     83    Assert.ok(
     84      !Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
     85      "alwaysCheck pref should be false by default in test runs"
     86    );
     87 
     88    const setDefaultButton =
     89      content.document.getElementById("setDefaultButton");
     90    /**
     91     * Click 'Make default' button to trigger shell service that sets the browser to default.
     92     */
     93    setDefaultButton.click();
     94    /**
     95     * Deem complete when 'Always Check' checkbox is checked.
     96     */
     97    await TestUtils.waitForCondition(
     98      () => alwaysCheck.checked,
     99      "'Always Check' checkbox should get checked after clicking the 'Set Default' button"
    100    );
    101 
    102    Assert.ok(
    103      alwaysCheck.checked,
    104      "Clicking 'Make Default' checks the 'Always Check' checkbox"
    105    );
    106    Assert.ok(
    107      Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
    108      "Checking the checkbox should set the pref to true"
    109    );
    110    Assert.ok(
    111      alwaysCheck.disabled,
    112      "'Always Check' checkbox is locked with default browser and alwaysCheck=true"
    113    );
    114    checkDefaultBrowserState(true);
    115    Assert.ok(
    116      Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
    117      "checkDefaultBrowser pref is now enabled"
    118    );
    119    teardown();
    120  }
    121 );
    122 
    123 add_task(
    124  /**
    125   * Tests when clicking 'Make default' button, setting browser
    126   * to default, and the side effects of the 'Always check' checkbox
    127   * when browser.shell.checkDefaultBrowser pref is locked
    128   */
    129  async function clicking_make_default_checks_alwaysCheck_checkbox_when_locked() {
    130    await setup();
    131 
    132    Services.prefs.lockPref("browser.shell.checkDefaultBrowser");
    133 
    134    const isDefault = false;
    135    await setupInitialBrowserDefaultSetting({ isDefault });
    136 
    137    let isDefaultPane = content.document.getElementById("isDefaultPane");
    138    let isNotDefaultPane = content.document.getElementById("isNotDefaultPane");
    139 
    140    is(isDefaultPane.localName, "moz-promo", "Pane is a moz-promo");
    141 
    142    Assert.ok(
    143      BrowserTestUtils.isHidden(isDefaultPane.control),
    144      "The 'browser is default' pane should be hidden when not default"
    145    );
    146 
    147    Assert.ok(
    148      !BrowserTestUtils.isHidden(isNotDefaultPane.control),
    149      "The 'is not default' pane should be visible when not default"
    150    );
    151 
    152    let alwaysCheck = content.document.getElementById("alwaysCheckDefault");
    153 
    154    Assert.ok(alwaysCheck.disabled, "Always Check is disabled when locked");
    155 
    156    Assert.ok(
    157      alwaysCheck.checked,
    158      "Always Check is checked because defaultPref is true and pref is locked"
    159    );
    160    Assert.ok(
    161      Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
    162      "alwaysCheck pref should ship with 'true' by default"
    163    );
    164    const setDefaultButton =
    165      content.document.getElementById("setDefaultButton");
    166    /**
    167     * Click 'Make default' button to trigger shell service that sets the browser to default.
    168     */
    169    setDefaultButton.click();
    170 
    171    const { TelemetryTestUtils } = ChromeUtils.importESModule(
    172      "resource://testing-common/TelemetryTestUtils.sys.mjs"
    173    );
    174    let snapshot = TelemetryTestUtils.getProcessScalars("parent", true, true);
    175    TelemetryTestUtils.assertKeyedScalar(
    176      snapshot,
    177      "browser.ui.interaction.preferences_paneGeneral",
    178      "setDefaultButton",
    179      2 // button clicked
    180    );
    181 
    182    Assert.ok(
    183      !BrowserTestUtils.isHidden(isNotDefaultPane.control),
    184      "Browser default pane still shows after click because pref is locked"
    185    );
    186 
    187    Assert.ok(
    188      BrowserTestUtils.isHidden(isDefaultPane.control),
    189      "Browser is not default pane is NOT showing"
    190    );
    191 
    192    Assert.ok(
    193      alwaysCheck.checked,
    194      "'Always Check' is still checked because it's locked"
    195    );
    196    Assert.ok(
    197      alwaysCheck.disabled,
    198      "'Always Check is disabled because it's locked"
    199    );
    200    Assert.ok(
    201      Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
    202      "The pref is locked and so doesn't get changed"
    203    );
    204    teardown();
    205  }
    206 );
    207 
    208 add_task(
    209  /**
    210   * Testcase with Firefox initially set as the default browser
    211   */
    212  async function make_default_after_browser_set_as_default() {
    213    await setup();
    214 
    215    await setupInitialBrowserDefaultSetting({ isDefault: true });
    216 
    217    const alwaysCheck = content.document.getElementById("alwaysCheckDefault");
    218 
    219    is(alwaysCheck.localName, "moz-checkbox", "Checkbox is a moz-checkbox.");
    220 
    221    Assert.ok(
    222      !BrowserTestUtils.isHidden(alwaysCheck.control),
    223      "Control element is visible by default"
    224    );
    225 
    226    Assert.ok(!alwaysCheck.disabled, "'Always Check' is NOT disabled");
    227 
    228    Assert.ok(!alwaysCheck.checked, "Checkbox is NOT checked initially");
    229 
    230    is(
    231      content.document.l10n.getAttributes(alwaysCheck).id,
    232      "always-check-default",
    233      `Checkbox has the correct data-l10n-id attribute`
    234    );
    235    teardown();
    236  }
    237 );