tor-browser

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

browser_appmenu_signin_cta_variants.js (4015B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { NimbusTestUtils } = ChromeUtils.importESModule(
      7  "resource://testing-common/NimbusTestUtils.sys.mjs"
      8 );
      9 
     10 /**
     11 * Tests that we can change the CTA for the FxA sign-in button in the AppMenu
     12 * using Nimbus.
     13 */
     14 
     15 add_setup(function () {
     16  registerCleanupFunction(() => {
     17    PanelUI.hide();
     18  });
     19 });
     20 
     21 /**
     22 * Closes and re-opens the AppMenu for the current browser window, and returns a
     23 * Promise once the main view fires the "ViewShown" event. The Promise resolves
     24 * to the element in the AppMenu that holds the FxA sign-in CTA.
     25 *
     26 * If the panel is not already open, this just opens it.
     27 *
     28 * @returns {Promise<Element>}
     29 */
     30 async function reopenAppMenu() {
     31  PanelUI.hide();
     32  let promiseViewShown = BrowserTestUtils.waitForEvent(
     33    PanelUI.panel,
     34    "ViewShown"
     35  );
     36  PanelUI.show();
     37  await promiseViewShown;
     38  return PanelMultiView.getViewNode(document, "appMenu-fxa-text");
     39 }
     40 
     41 /**
     42 * Tests that we use the default CTA when not enrolled in an experiment.
     43 */
     44 add_task(async function test_default() {
     45  let sandbox = sinon.createSandbox();
     46  sandbox.spy(NimbusFeatures.fxaAppMenuItem, "recordExposureEvent");
     47  Assert.equal(
     48    NimbusFeatures.fxaAppMenuItem.getVariable("ctaCopyVariant"),
     49    undefined,
     50    "Should not start with a NimbusFeature set for the CTA copy."
     51  );
     52  let ctaEl = await reopenAppMenu();
     53  Assert.equal(
     54    ctaEl.dataset.l10nId,
     55    "appmenu-fxa-sync-and-save-data2",
     56    "Should have the default CTA text."
     57  );
     58  Assert.ok(
     59    NimbusFeatures.fxaAppMenuItem.recordExposureEvent.notCalled,
     60    "Did not record any exposure."
     61  );
     62  sandbox.restore();
     63 });
     64 
     65 /**
     66 * Tests that the control variant uses the default CTA, but also records an
     67 * impression.
     68 */
     69 add_task(async function test_control() {
     70  let doCleanup = await NimbusTestUtils.enrollWithFeatureConfig(
     71    {
     72      featureId: NimbusFeatures.fxaAppMenuItem.featureId,
     73      value: {
     74        ctaCopyVariant: "control",
     75      },
     76    },
     77    { isRollout: true }
     78  );
     79  let sandbox = sinon.createSandbox();
     80  sandbox.spy(NimbusFeatures.fxaAppMenuItem, "recordExposureEvent");
     81  let ctaEl = await reopenAppMenu();
     82  Assert.equal(
     83    ctaEl.dataset.l10nId,
     84    "appmenu-fxa-sync-and-save-data2",
     85    "Should have the default CTA text."
     86  );
     87 
     88  Assert.ok(
     89    NimbusFeatures.fxaAppMenuItem.recordExposureEvent.calledOnce,
     90    "Recorded exposure."
     91  );
     92  await doCleanup();
     93  sandbox.restore();
     94 });
     95 
     96 /**
     97 * Tests all variants , and that when we stop experimenting with that variant,
     98 * we revert back to the default CTA.
     99 */
    100 add_task(async function test_variants() {
    101  let variants = ["sync-devices", "backup-data", "backup-sync", "mobile"];
    102 
    103  for (let variant of variants) {
    104    let sandbox = sinon.createSandbox();
    105    sandbox.spy(NimbusFeatures.fxaAppMenuItem, "recordExposureEvent");
    106 
    107    let expectedL10nID = `fxa-menu-message-${variant}-collapsed-text`;
    108    // Ensure that a string actually exists for that ID.
    109    try {
    110      Assert.ok(
    111        await document.l10n.formatValue(expectedL10nID),
    112        `Found a string for ${expectedL10nID}`
    113      );
    114    } catch (e) {
    115      Assert.ok(false, `Missing string for ID: ${expectedL10nID}`);
    116    }
    117    let doCleanup = await NimbusTestUtils.enrollWithFeatureConfig(
    118      {
    119        featureId: NimbusFeatures.fxaAppMenuItem.featureId,
    120        value: {
    121          ctaCopyVariant: variant,
    122        },
    123      },
    124      { isRollout: true }
    125    );
    126 
    127    let ctaEl = await reopenAppMenu();
    128    Assert.equal(
    129      ctaEl.dataset.l10nId,
    130      expectedL10nID,
    131      "Got the expected string for the variant."
    132    );
    133 
    134    await doCleanup();
    135 
    136    ctaEl = await reopenAppMenu();
    137    Assert.equal(
    138      ctaEl.dataset.l10nId,
    139      "appmenu-fxa-sync-and-save-data2",
    140      "Should have the default CTA text."
    141    );
    142    Assert.ok(
    143      NimbusFeatures.fxaAppMenuItem.recordExposureEvent.calledOnce,
    144      "Recorded exposure."
    145    );
    146    sandbox.restore();
    147  }
    148 });