tor-browser

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

browser_setDefaultBrowser.js (6562B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 ChromeUtils.defineESModuleGetters(this, {
      5  ASRouter: "resource:///modules/asrouter/ASRouter.sys.mjs",
      6  ExperimentAPI: "resource://nimbus/ExperimentAPI.sys.mjs",
      7  NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs",
      8  NimbusTestUtils: "resource://testing-common/NimbusTestUtils.sys.mjs",
      9  sinon: "resource://testing-common/Sinon.sys.mjs",
     10 });
     11 
     12 const setDefaultBrowserUserChoiceStub = async () => {
     13  throw Components.Exception("", Cr.NS_ERROR_WDBA_NO_PROGID);
     14 };
     15 
     16 const defaultAgentStub = sinon
     17  .stub(ShellService, "defaultAgent")
     18  .value({ setDefaultBrowserUserChoiceAsync: setDefaultBrowserUserChoiceStub });
     19 
     20 const _userChoiceImpossibleTelemetryResultStub = sinon
     21  .stub(ShellService, "_userChoiceImpossibleTelemetryResult")
     22  .callsFake(() => null);
     23 
     24 const userChoiceStub = sinon
     25  .stub(ShellService, "setAsDefaultUserChoice")
     26  .resolves();
     27 const setDefaultStub = sinon.stub();
     28 const shellStub = sinon
     29  .stub(ShellService, "shellService")
     30  .value({ setDefaultBrowser: setDefaultStub });
     31 
     32 const sendTriggerStub = sinon.stub(ASRouter, "sendTriggerMessage");
     33 
     34 registerCleanupFunction(() => {
     35  sinon.restore();
     36 });
     37 
     38 let defaultUserChoice;
     39 add_task(async function need_user_choice() {
     40  await ShellService.setDefaultBrowser();
     41  defaultUserChoice = userChoiceStub.called;
     42 
     43  Assert.notStrictEqual(
     44    defaultUserChoice,
     45    undefined,
     46    "Decided which default browser method to use"
     47  );
     48  Assert.equal(
     49    setDefaultStub.notCalled,
     50    defaultUserChoice,
     51    "Only one default behavior was used"
     52  );
     53 });
     54 
     55 add_task(async function remote_disable() {
     56  if (defaultUserChoice === false) {
     57    info("Default behavior already not user choice, so nothing to test");
     58    return;
     59  }
     60 
     61  userChoiceStub.resetHistory();
     62  setDefaultStub.resetHistory();
     63  let doCleanup = await NimbusTestUtils.enrollWithFeatureConfig(
     64    {
     65      featureId: NimbusFeatures.shellService.featureId,
     66      value: {
     67        setDefaultBrowserUserChoice: false,
     68        enabled: true,
     69      },
     70    },
     71    { isRollout: true }
     72  );
     73 
     74  await ShellService.setDefaultBrowser();
     75 
     76  Assert.ok(
     77    userChoiceStub.notCalled,
     78    "Set default with user choice disabled via nimbus"
     79  );
     80  Assert.ok(setDefaultStub.called, "Used plain set default instead");
     81 
     82  await doCleanup();
     83 });
     84 
     85 add_task(async function restore_default() {
     86  if (defaultUserChoice === undefined) {
     87    info("No default user choice behavior set, so nothing to test");
     88    return;
     89  }
     90 
     91  userChoiceStub.resetHistory();
     92  setDefaultStub.resetHistory();
     93 
     94  await ShellService.setDefaultBrowser();
     95 
     96  Assert.equal(
     97    userChoiceStub.called,
     98    defaultUserChoice,
     99    "Set default with user choice restored to original"
    100  );
    101  Assert.equal(
    102    setDefaultStub.notCalled,
    103    defaultUserChoice,
    104    "Plain set default behavior restored to original"
    105  );
    106 });
    107 
    108 add_task(async function ensure_fallback() {
    109  if (AppConstants.platform != "win") {
    110    info("Nothing to test on non-Windows");
    111    return;
    112  }
    113 
    114  let userChoicePromise = Promise.resolve();
    115  userChoiceStub.callsFake(function (...args) {
    116    return (userChoicePromise = userChoiceStub.wrappedMethod.apply(this, args));
    117  });
    118  userChoiceStub.resetHistory();
    119  setDefaultStub.resetHistory();
    120  let doCleanup = await NimbusTestUtils.enrollWithFeatureConfig(
    121    {
    122      featureId: NimbusFeatures.shellService.featureId,
    123      value: {
    124        setDefaultBrowserUserChoice: true,
    125        setDefaultPDFHandler: false,
    126        enabled: true,
    127      },
    128    },
    129    { isRollout: true }
    130  );
    131 
    132  await ShellService.setDefaultBrowser();
    133 
    134  Assert.ok(userChoiceStub.called, "Set default with user choice called");
    135 
    136  let message = "";
    137  await userChoicePromise.catch(err => (message = err.message || ""));
    138 
    139  Assert.ok(
    140    message.includes("ErrExeProgID"),
    141    "Set default with user choice threw an expected error"
    142  );
    143  Assert.ok(setDefaultStub.called, "Fallbacked to plain set default");
    144 
    145  await doCleanup();
    146 });
    147 
    148 async function setUpNotificationTests(guidanceEnabled, oneClick) {
    149  sinon.reset();
    150  const experimentCleanup = await NimbusTestUtils.enrollWithFeatureConfig(
    151    {
    152      featureId: NimbusFeatures.shellService.featureId,
    153      value: {
    154        setDefaultGuidanceNotifications: guidanceEnabled,
    155        setDefaultBrowserUserChoice: oneClick,
    156        setDefaultBrowserUserChoiceRegRename: oneClick,
    157        enabled: true,
    158      },
    159    },
    160    { isRollout: true }
    161  );
    162 
    163  const doCleanup = async () => {
    164    await experimentCleanup();
    165    sinon.reset();
    166  };
    167 
    168  await ShellService.setDefaultBrowser();
    169  return doCleanup;
    170 }
    171 
    172 add_task(
    173  async function show_notification_when_set_to_default_guidance_enabled_and_one_click_disabled() {
    174    if (!AppConstants.isPlatformAndVersionAtLeast("win", 10)) {
    175      info("Nothing to test on non-Windows or older Windows versions");
    176      return;
    177    }
    178    const doCleanup = await setUpNotificationTests(
    179      true, // guidance enabled
    180      false // one-click disabled
    181    );
    182 
    183    Assert.ok(setDefaultStub.called, "Fallback method used to set default");
    184 
    185    Assert.equal(
    186      sendTriggerStub.firstCall.args[0].id,
    187      "deeplinkedToWindowsSettingsUI",
    188      `Set to default guidance message trigger was sent`
    189    );
    190 
    191    await doCleanup();
    192  }
    193 );
    194 
    195 add_task(
    196  async function do_not_show_notification_when_set_to_default_guidance_disabled_and_one_click_enabled() {
    197    if (!AppConstants.isPlatformAndVersionAtLeast("win", 10)) {
    198      info("Nothing to test on non-Windows or older Windows versions");
    199      return;
    200    }
    201 
    202    const doCleanup = await setUpNotificationTests(
    203      false, // guidance disabled
    204      true // one-click enabled
    205    );
    206 
    207    Assert.ok(setDefaultStub.notCalled, "Fallback method not called");
    208 
    209    Assert.equal(
    210      sendTriggerStub.callCount,
    211      0,
    212      `Set to default guidance message trigger was not sent`
    213    );
    214 
    215    await doCleanup();
    216  }
    217 );
    218 
    219 add_task(
    220  async function do_not_show_notification_when_set_to_default_guidance_enabled_and_one_click_enabled() {
    221    if (!AppConstants.isPlatformAndVersionAtLeast("win", 10)) {
    222      info("Nothing to test on non-Windows or older Windows versions");
    223      return;
    224    }
    225 
    226    const doCleanup = await setUpNotificationTests(
    227      true, // guidance enabled
    228      true // one-click enabled
    229    );
    230 
    231    Assert.ok(setDefaultStub.notCalled, "Fallback method not called");
    232    Assert.equal(
    233      sendTriggerStub.callCount,
    234      0,
    235      `Set to default guidance message trigger was not sent`
    236    );
    237    await doCleanup();
    238  }
    239 );