tor-browser

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

test_nimbus_newtabTrainhopAddon_onBrowserReady.js (3216B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /* import-globals-from ../../../../extensions/newtab/test/xpcshell/head.js */
      7 
      8 /* import-globals-from head_nimbus_trainhop.js */
      9 
     10 add_task(
     11  {
     12    pref_set: [
     13      [TRAINHOP_SCHEDULED_UPDATE_STATE_TIMEOUT_PREF, 100],
     14      [TRAINHOP_SCHEDULED_UPDATE_STATE_DELAY_PREF, 100],
     15      // Allow this test to call `AsyncShutdown.appShutdownConfirmed._trigger`.
     16      ["toolkit.asyncshutdown.testing", true],
     17    ],
     18  },
     19  async function test_scheduled_updateAddonState_onBrowserReady() {
     20    const { sinon } = ChromeUtils.importESModule(
     21      "resource://testing-common/Sinon.sys.mjs"
     22    );
     23    const sandbox = sinon.createSandbox();
     24    const asyncAssertNoPendingInstalls = async () => {
     25      Assert.deepEqual(
     26        await AddonManager.getAllInstalls(),
     27        [],
     28        "Expect no pending install to be found"
     29      );
     30    };
     31 
     32    const promiseInstallPostponed =
     33      AddonTestUtils.promiseInstallEvent("onInstallPostponed");
     34    const updateAddonVersion = `${BUILTIN_ADDON_VERSION}.123`;
     35    const { nimbusFeatureCleanup } = await setupNimbusTrainhopAddon({
     36      updateAddonVersion,
     37    });
     38    AboutNewTab.onBrowserReady();
     39    await promiseInstallPostponed;
     40    const { pendingInstall } = await asyncAssertNimbusTrainhopAddonStaged({
     41      updateAddonVersion,
     42    });
     43    await cancelPendingInstall(pendingInstall);
     44    // Sanity check.
     45    await asyncAssertNoPendingInstalls();
     46 
     47    // Verify that calls to updateTrainhopAddonState triggered while the client isn't
     48    // enrolled in the newtabTrainhopAddon Nimbus feature don't log any warning for
     49    // incomplete Nimbus feature variables, and that we do not call _installTrainhopAddon.
     50    const loggerWarnSpy = sandbox.spy(
     51      AboutNewTabResourceMapping.logger,
     52      "warn"
     53    );
     54    // The nimbusFeatureCleanup helper function is expected to trigger the Nimbus feature update
     55    // event like when a Nimbus featture is being unenrolled from the client, and so we await
     56    // for the deferred task to be completed.
     57    await nimbusFeatureCleanup();
     58    await AboutNewTabResourceMapping._updateAddonStateDeferredTask
     59      ?._runningPromise;
     60    await asyncAssertNoPendingInstalls();
     61    Assert.deepEqual(
     62      loggerWarnSpy.getCalls().map(spyCall => spyCall.args),
     63      [],
     64      "Expect no warning to be logged by updateTrainhopAddonState when not enrolled"
     65    );
     66    sandbox.restore();
     67 
     68    // NOTE: prevents AboutNewTab.uninit to hit unexpected failures while the test harness
     69    // is shutting down (side-effect of calling AboutNewTab onBrowserReady and the simulated
     70    // application restarts).
     71    AboutNewTab.activityStream = null;
     72 
     73    // NOTE: ensures the _updateAddonStateDeferredTask to return right away if triggered
     74    // after this test is completed while the xpcshell test harness is shutting down
     75    // (similarly to what would happen if it would be getting triggered too late on
     76    // the actual application shutdown).
     77    const { AsyncShutdown } = ChromeUtils.importESModule(
     78      "resource://gre/modules/AsyncShutdown.sys.mjs"
     79    );
     80    AsyncShutdown.appShutdownConfirmed._trigger();
     81  }
     82 );