Commit 5c94e5d7 authored by Tif Tran's avatar Tif Tran
Browse files

Bug 1826403 - Fill blocked sponsored tiles with additional ones r=nanj

parent 193d8a5b
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -95,6 +95,10 @@ const MAX_NUM_SPONSORED = 2;
// both Contile and Pocket sources.
// The default will be `MAX_NUM_SPONSORED` if this variable is unspecified.
const NIMBUS_VARIABLE_MAX_SPONSORED = "topSitesMaxSponsored";
// Nimbus variable to allow more than two sponsored tiles from Contile to be
//considered for Top Sites.
const NIMBUS_VARIABLE_ADDITIONAL_TILES =
  "topSitesUseAdditionalTilesFromContile";

// Search experiment stuff
const FILTER_DEFAULT_SEARCH_PREF = "improvesearch.noDefaultSearchTile";
@@ -198,11 +202,16 @@ class ContileIntegration {
      const body = await response.json();
      if (body?.tiles && Array.isArray(body.tiles)) {
        let { tiles } = body;
        if (
          !lazy.NimbusFeatures.newtab.getVariable(
            NIMBUS_VARIABLE_ADDITIONAL_TILES
          )
        ) {
          tiles.length = CONTILE_MAX_NUM_SPONSORED;
        }
        tiles = this._filterBlockedSponsors(tiles);
        if (tiles.length > CONTILE_MAX_NUM_SPONSORED) {
          lazy.log.warn(
            `Contile provided more links than permitted. (${tiles.length} received, limit is ${CONTILE_MAX_NUM_SPONSORED})`
          );
          lazy.log.info("Remove unused links from Contile");
          tiles.length = CONTILE_MAX_NUM_SPONSORED;
        }
        this._sites = tiles;
+102 −0
Original line number Diff line number Diff line
@@ -2160,6 +2160,108 @@ describe("Top Sites Feed", () => {
      assert.equal(feed._contile.sites.length, 0);
    });

    it("should still return two tiles when Contile provides more than 2 tiles and filtering results in more than 2 tiles", async () => {
      fakeNimbusFeatures.newtab.getVariable.reset();
      fakeNimbusFeatures.newtab.getVariable.onCall(0).returns(true);
      fakeNimbusFeatures.newtab.getVariable.onCall(1).returns(true);

      fetchStub.resolves({
        ok: true,
        status: 200,
        json: () =>
          Promise.resolve({
            tiles: [
              {
                url: "https://www.test.com",
                image_url: "images/test-com.png",
                click_url: "https://www.test-click.com",
                impression_url: "https://www.test-impression.com",
                name: "test",
              },
              {
                url: "https://foo.com",
                image_url: "images/foo-com.png",
                click_url: "https://www.foo-click.com",
                impression_url: "https://www.foo-impression.com",
                name: "foo",
              },
              {
                url: "https://bar.com",
                image_url: "images/bar-com.png",
                click_url: "https://www.bar-click.com",
                impression_url: "https://www.bar-impression.com",
                name: "bar",
              },
              {
                url: "https://test1.com",
                image_url: "images/test1-com.png",
                click_url: "https://www.test1-click.com",
                impression_url: "https://www.test1-impression.com",
                name: "test1",
              },
              {
                url: "https://test2.com",
                image_url: "images/test2-com.png",
                click_url: "https://www.test2-click.com",
                impression_url: "https://www.test2-impression.com",
                name: "test2",
              },
            ],
          }),
      });

      const fetched = await feed._contile._fetchSites();

      assert.ok(fetched);
      // Both "foo" and "bar" should be filtered
      assert.equal(feed._contile.sites.length, 2);
      assert.equal(feed._contile.sites[0].url, "https://www.test.com");
      assert.equal(feed._contile.sites[1].url, "https://test1.com");
    });

    it("should still return two tiles when Contile provides more than 2 tiles", async () => {
      fakeNimbusFeatures.newtab.getVariable.reset();
      fakeNimbusFeatures.newtab.getVariable.onCall(0).returns(true);
      fakeNimbusFeatures.newtab.getVariable.onCall(1).returns(null);
      fetchStub.resolves({
        ok: true,
        status: 200,
        json: () =>
          Promise.resolve({
            tiles: [
              {
                url: "https://www.test.com",
                image_url: "images/test-com.png",
                click_url: "https://www.test-click.com",
                impression_url: "https://www.test-impression.com",
                name: "test",
              },
              {
                url: "https://test1.com",
                image_url: "images/test1-com.png",
                click_url: "https://www.test1-click.com",
                impression_url: "https://www.test1-impression.com",
                name: "test1",
              },
              {
                url: "https://test2.com",
                image_url: "images/test2-com.png",
                click_url: "https://www.test2-click.com",
                impression_url: "https://www.test2-impression.com",
                name: "test2",
              },
            ],
          }),
      });

      const fetched = await feed._contile._fetchSites();

      assert.ok(fetched);
      assert.equal(feed._contile.sites.length, 2);
      assert.equal(feed._contile.sites[0].url, "https://www.test.com");
      assert.equal(feed._contile.sites[1].url, "https://test1.com");
    });

    it("should filter the blocked sponsors", async () => {
      fetchStub.resolves({
        ok: true,
+3 −0
Original line number Diff line number Diff line
@@ -364,6 +364,9 @@ newtab:
      type: boolean
      fallbackPref: browser.topsites.contile.enabled
      description: Enable the Contile integration for Sponsored Top Sites
    topSitesUseAdditionalTilesFromContile:
      type: boolean
      description: Allow Contile to use additonal sponsored top sites

pocketNewtab:
  description: The Pocket section in newtab