Commit fb135b95 authored by Lina Butler's avatar Lina Butler
Browse files

Bug 1825157 - Unify the fake pref implementations used in tests. r=nanj

Before this commit, the New Tab tests had two fake pref
implementations:

* The `Services.prefs` mock in `TEST_GLOBAL`, which stubbed out a
  handful of `nsIPrefBranch` methods.
* `FakensIPrefBranch`, which mocked a different handful of
  `nsIPrefBranch` methods.

This commit:

* Consolidates the two fake implementations into the mock
  `FakensIPrefBranch` class.
* Adds a `FakensIPrefService` subclass of `FakensIPrefBranch`, with
  additional methods specific to `nsIPrefService`.
* Replaces `FakensIPrefBranch.prototype.prefs` with a new
  `FAKE_GLOBAL_PREFS` map. The new approach still lets tests change and
  clear prefs on existing instances of `FakensIPrefBranch` and its
  subclasses, but makes the shared state explicit. The old approach of
  mutating `FakensIPrefBranch.prototype.prefs` relied on a subtle quirk
  of `prefs` being shared by all instances of `FakensIPrefBranch` and
  its subclasses, and broke when setting `prefs` to a new, empty
  object.
* Moves `{observe, ignore}Branch` into `FakePrefs`, since these are
  not part of the `nsIPrefBranch` interface.

Differential Revision: https://phabricator.services.mozilla.com/D173897
parent e418d8bb
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ describe("ActivityStreamPrefs", () => {
        assert.calledWith(defaultPrefs.set, "foo", true);
      });
      it("should not initialize a pref if a default exists", () => {
        defaultPrefs.prefs.foo = false;
        defaultPrefs.prefs.set("foo", false);

        defaultPrefs.init();

+1 −0
Original line number Diff line number Diff line
@@ -281,6 +281,7 @@ describe("DiscoveryStreamFeed", () => {
      assert.equal(feed._impressionId, FAKE_UUID);
    });
    it("should create impression id if none exists", async () => {
      sandbox.stub(global.Services.prefs, "getCharPref").returns("");
      sandbox.stub(global.Services.prefs, "setCharPref").returns();

      const result = feed.getOrCreateImpressionId();
+43 −28
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ import {
  SessionPing,
  UserEventPing,
} from "test/schemas/pings";
import { FakePrefs, GlobalOverrider } from "test/unit/utils";
import { FAKE_GLOBAL_PREFS, GlobalOverrider } from "test/unit/utils";
import { ASRouterPreferences } from "lib/ASRouterPreferences.jsm";
import injector from "inject!lib/TelemetryFeed.jsm";
import { MESSAGE_TYPE_HASH as msg } from "common/ActorConstants.sys.mjs";
@@ -47,6 +47,28 @@ describe("TelemetryFeed", () => {
    uninit() {}
  }

  // Reset the global prefs before importing the `TelemetryFeed` module, to
  // avoid a coverage miss caused by preference pollution when this test and
  // `ActivityStream.test.js` are run together.
  //
  // The `TelemetryFeed` module defines a lazy `contextId` getter, which the
  // `XPCOMUtils.defineLazyGetter` mock (defined in `unit-entry.js`) executes
  // immediately, as soon as the module is imported.
  //
  // If this test runs first, there's no coverage miss: this test will load
  // the `TelemetryFeed` module and run the lazy `contextId` getter, which will
  // generate a fake context ID and store it in `FAKE_GLOBAL_PREFS`, covering
  // all branches in the module. When `ActivityStream.test.js` runs, it'll load
  // `TelemetryFeed` and run the lazy getter a second time, which will use the
  // existing fake context ID from `FAKE_GLOBAL_PREFS` instead of generating a
  // new one.
  //
  // But, if `ActivityStream.test.js` runs first, then loading `TelemetryFeed` a
  // second time as part of this test will use the existing fake context ID from
  // `FAKE_GLOBAL_PREFS`, missing coverage for the branch to generate a new
  // context ID.
  FAKE_GLOBAL_PREFS.clear();

  const {
    TelemetryFeed,
    USER_PREFS_ENCODING,
@@ -99,7 +121,7 @@ describe("TelemetryFeed", () => {
  afterEach(() => {
    clock.restore();
    globals.restore();
    FakePrefs.prototype.prefs = {};
    FAKE_GLOBAL_PREFS.clear();
    ASRouterPreferences.uninit();
  });
  describe("#init", () => {
@@ -162,8 +184,7 @@ describe("TelemetryFeed", () => {
      assert.equal(instance._impressionId, FAKE_UUID);
    });
    it("should set impression id if it exists", () => {
      FakePrefs.prototype.prefs = {};
      FakePrefs.prototype.prefs[PREF_IMPRESSION_ID] = "fakeImpressionId";
      FAKE_GLOBAL_PREFS.set(PREF_IMPRESSION_ID, "fakeImpressionId");
      assert.equal(new TelemetryFeed()._impressionId, "fakeImpressionId");
    });
    it("should register listeners on existing windows", () => {
@@ -183,8 +204,7 @@ describe("TelemetryFeed", () => {
    });
    describe("telemetry pref changes from false to true", () => {
      beforeEach(() => {
        FakePrefs.prototype.prefs = {};
        FakePrefs.prototype.prefs[TELEMETRY_PREF] = false;
        FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, false);
        instance = new TelemetryFeed();

        assert.propertyVal(instance, "telemetryEnabled", false);
@@ -198,8 +218,7 @@ describe("TelemetryFeed", () => {
    });
    describe("events telemetry pref changes from false to true", () => {
      beforeEach(() => {
        FakePrefs.prototype.prefs = {};
        FakePrefs.prototype.prefs[EVENTS_TELEMETRY_PREF] = false;
        FAKE_GLOBAL_PREFS.set(EVENTS_TELEMETRY_PREF, false);
        instance = new TelemetryFeed();

        assert.propertyVal(instance, "eventTelemetryEnabled", false);
@@ -505,8 +524,8 @@ describe("TelemetryFeed", () => {
      assert.isFalse(instance.sessions.has("foo"));
    });
    it("should call createSessionSendEvent and sendEvent with the sesssion", () => {
      FakePrefs.prototype.prefs[TELEMETRY_PREF] = true;
      FakePrefs.prototype.prefs[EVENTS_TELEMETRY_PREF] = true;
      FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, true);
      FAKE_GLOBAL_PREFS.set(EVENTS_TELEMETRY_PREF, true);
      instance = new TelemetryFeed();

      sandbox.stub(instance, "sendEvent");
@@ -528,9 +547,8 @@ describe("TelemetryFeed", () => {
  });
  describe("ping creators", () => {
    beforeEach(() => {
      FakePrefs.prototype.prefs = {};
      for (const pref of Object.keys(USER_PREFS_ENCODING)) {
        FakePrefs.prototype.prefs[pref] = true;
        FAKE_GLOBAL_PREFS.set(pref, true);
        expectedUserPrefs |= USER_PREFS_ENCODING[pref];
      }
      instance.init();
@@ -1192,7 +1210,7 @@ describe("TelemetryFeed", () => {
  });
  describe("#sendEvent", () => {
    it("should call sendEventPing on activity_stream_user_event", () => {
      FakePrefs.prototype.prefs.telemetry = true;
      FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, true);
      const event = { action: "activity_stream_user_event" };
      instance = new TelemetryFeed();
      sandbox.spy(instance, "sendEventPing");
@@ -1202,7 +1220,7 @@ describe("TelemetryFeed", () => {
      assert.calledOnce(instance.sendEventPing);
    });
    it("should call sendSessionPing on activity_stream_session", () => {
      FakePrefs.prototype.prefs.telemetry = true;
      FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, true);
      const event = { action: "activity_stream_session" };
      instance = new TelemetryFeed();
      sandbox.spy(instance, "sendSessionPing");
@@ -1214,8 +1232,8 @@ describe("TelemetryFeed", () => {
  });
  describe("#sendUTEvent", () => {
    it("should call the UT event function passed in", async () => {
      FakePrefs.prototype.prefs[TELEMETRY_PREF] = true;
      FakePrefs.prototype.prefs[EVENTS_TELEMETRY_PREF] = true;
      FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, true);
      FAKE_GLOBAL_PREFS.set(EVENTS_TELEMETRY_PREF, true);
      const event = {};
      instance = new TelemetryFeed();
      sandbox.stub(instance.utEvents, "sendUserEvent");
@@ -1227,7 +1245,7 @@ describe("TelemetryFeed", () => {
  });
  describe("#sendStructuredIngestionEvent", () => {
    it("should call PingCentre sendStructuredIngestionPing", async () => {
      FakePrefs.prototype.prefs[TELEMETRY_PREF] = true;
      FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, true);
      const event = {};
      instance = new TelemetryFeed();
      sandbox.stub(instance.pingCentre, "sendStructuredIngestionPing");
@@ -1382,7 +1400,7 @@ describe("TelemetryFeed", () => {
  });
  describe("#onAction", () => {
    beforeEach(() => {
      FakePrefs.prototype.prefs = {};
      FAKE_GLOBAL_PREFS.clear();
    });
    it("should call .init() on an INIT action", () => {
      const init = sandbox.stub(instance, "init");
@@ -1449,8 +1467,8 @@ describe("TelemetryFeed", () => {
      assert.calledWith(stub, "port123", data);
    });
    it("should send an event on a TELEMETRY_USER_EVENT action", () => {
      FakePrefs.prototype.prefs[TELEMETRY_PREF] = true;
      FakePrefs.prototype.prefs[EVENTS_TELEMETRY_PREF] = true;
      FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, true);
      FAKE_GLOBAL_PREFS.set(EVENTS_TELEMETRY_PREF, true);
      instance = new TelemetryFeed();

      const sendEvent = sandbox.stub(instance, "sendEvent");
@@ -1465,8 +1483,8 @@ describe("TelemetryFeed", () => {
      assert.calledWith(utSendUserEvent, eventCreator.returnValue);
    });
    it("should send an event on a DISCOVERY_STREAM_USER_EVENT action", () => {
      FakePrefs.prototype.prefs[TELEMETRY_PREF] = true;
      FakePrefs.prototype.prefs[EVENTS_TELEMETRY_PREF] = true;
      FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, true);
      FAKE_GLOBAL_PREFS.set(EVENTS_TELEMETRY_PREF, true);
      instance = new TelemetryFeed();

      const sendEvent = sandbox.stub(instance, "sendEvent");
@@ -1497,8 +1515,8 @@ describe("TelemetryFeed", () => {
      ];
      actions.forEach(type => {
        it(`${type} action`, () => {
          FakePrefs.prototype.prefs[TELEMETRY_PREF] = true;
          FakePrefs.prototype.prefs[EVENTS_TELEMETRY_PREF] = true;
          FAKE_GLOBAL_PREFS.set(TELEMETRY_PREF, true);
          FAKE_GLOBAL_PREFS.set(EVENTS_TELEMETRY_PREF, true);
          instance = new TelemetryFeed();

          const eventHandler = sandbox.spy(instance, "handleASRouterUserEvent");
@@ -1952,10 +1970,7 @@ describe("TelemetryFeed", () => {
      const fakeEndpoint = "http://fakeendpoint.com/base/";
      const fakeUUID = "{34f24486-f01a-9749-9c5b-21476af1fa77}";
      const fakeUUIDWithoutBraces = fakeUUID.substring(1, fakeUUID.length - 1);
      FakePrefs.prototype.prefs = {};
      FakePrefs.prototype.prefs[
        STRUCTURED_INGESTION_ENDPOINT_PREF
      ] = fakeEndpoint;
      FAKE_GLOBAL_PREFS.set(STRUCTURED_INGESTION_ENDPOINT_PREF, fakeEndpoint);
      sandbox.stub(Services.uuid, "generateUUID").returns(fakeUUID);
      const feed = new TelemetryFeed();
      const url = feed._generateStructuredIngestionEndpoint(
+2 −2
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import {
  actionCreators as ac,
  actionTypes as at,
} from "common/Actions.sys.mjs";
import { FakePrefs, GlobalOverrider } from "test/unit/utils";
import { FAKE_GLOBAL_PREFS, FakePrefs, GlobalOverrider } from "test/unit/utils";
import {
  insertPinned,
  TOP_SITES_DEFAULT_ROWS,
@@ -129,7 +129,7 @@ describe("Top Sites Feed", () => {
      Screenshots: fakeScreenshot,
    });
    sandbox.spy(global.XPCOMUtils, "defineLazyGetter");
    FakePrefs.prototype.prefs["default.sites"] = "https://foo.com/";
    FAKE_GLOBAL_PREFS.set("default.sites", "https://foo.com/");
    ({ TopSitesFeed, DEFAULT_TOP_SITES } = injector({
      "lib/ActivityStreamPrefs.jsm": { Prefs: FakePrefs },
      "common/Dedupe.jsm": { Dedupe: fakeDedupe },
+11 −8
Original line number Diff line number Diff line
import { FakePrefs, GlobalOverrider } from "test/unit/utils";
import { FAKE_GLOBAL_PREFS, FakePrefs, GlobalOverrider } from "test/unit/utils";
import { actionTypes as at } from "common/Actions.sys.mjs";
import injector from "inject!lib/TopStoriesFeed.jsm";

@@ -28,13 +28,16 @@ describe("Top Stories Feed", () => {
  };

  beforeEach(() => {
    FakePrefs.prototype.prefs.apiKeyPref = "test-api-key";
    FakePrefs.prototype.prefs.pocketCta = JSON.stringify({
    FAKE_GLOBAL_PREFS.set("apiKeyPref", "test-api-key");
    FAKE_GLOBAL_PREFS.set(
      "pocketCta",
      JSON.stringify({
        cta_button: "",
        cta_text: "",
        cta_url: "",
        use_cta: false,
    });
      })
    );

    globals = new GlobalOverrider();
    globals.set("PlacesUtils", { history: {} });
Loading