commit faaa95ad17de048438a166d0411026b6c45f96f1
parent 9e80ac1401c44849d5ecf1c40e8dd5cb40fcf13b
Author: Irene Ni <ini@mozilla.com>
Date: Fri, 19 Dec 2025 21:46:16 +0000
Bug 2006972 - Add deduping unit tests for New Tab frecency-based sorting. r=home-newtab-reviewers,thecount
Differential Revision: https://phabricator.services.mozilla.com/D277050
Diffstat:
3 files changed, 226 insertions(+), 11 deletions(-)
diff --git a/browser/extensions/newtab/test/xpcshell/test_TopSitesFeed_frecencyDeduping.js b/browser/extensions/newtab/test/xpcshell/test_TopSitesFeed_frecencyDeduping.js
@@ -0,0 +1,222 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+ChromeUtils.defineESModuleGetters(this, {
+ sinon: "resource://testing-common/Sinon.sys.mjs",
+ TopSitesFeed: "resource://newtab/lib/TopSitesFeed.sys.mjs",
+});
+
+const PREF_SOV_ENABLED = "sov.enabled";
+const SHOW_SPONSORED_PREF = "showSponsoredTopSites";
+const ROWS_PREF = "topSitesRows";
+
+function getTopSitesFeedForTest(
+ sandbox,
+ { frecent = [], linksWithDefaults = [] } = {}
+) {
+ let feed = new TopSitesFeed();
+
+ feed.store = {
+ getState() {
+ return this.state;
+ },
+ state: {
+ Prefs: {
+ values: {
+ [PREF_SOV_ENABLED]: true,
+ [SHOW_SPONSORED_PREF]: true,
+ [ROWS_PREF]: 1,
+ },
+ },
+ TopSites: {
+ sov: { ready: true },
+ },
+ },
+ };
+
+ feed.frecentCache = {
+ request() {
+ return this.cache;
+ },
+ cache: frecent,
+ };
+
+ feed._linksWithDefaults = linksWithDefaults;
+
+ feed._contile = {
+ sov: true,
+ sovEnabled: () => true,
+ };
+
+ const frecencyBoostedSponsors = new Map([
+ [
+ "hostname1",
+ {
+ domain: "https://domain1.com",
+ faviconDataURI: "faviconDataURI1",
+ hostname: "hostname1",
+ redirectURL: "https://redirectURL1.com",
+ title: "title1",
+ },
+ ],
+ [
+ "hostname2",
+ {
+ domain: "https://domain2.com",
+ faviconDataURI: "faviconDataURI2",
+ hostname: "hostname2",
+ redirectURL: "https://redirectURL2.com",
+ title: "title2",
+ },
+ ],
+ ]);
+
+ sandbox.stub(feed, "_frecencyBoostedSponsors").value(frecencyBoostedSponsors);
+
+ return feed;
+}
+
+add_task(async function test_dedupeSponsorsAgainstTopsites() {
+ let sandbox = sinon.createSandbox();
+ {
+ info(
+ "TopSitesFeed.fetchFrecencyBoostedSpocs - " +
+ "Should return a single match with the right format"
+ );
+ const feed = getTopSitesFeedForTest(sandbox, {
+ frecent: [{ url: "https://domain1.com", frecency: 1000 }],
+ linksWithDefaults: [
+ { url: "https://otherdomain.com", hostname: "otherdomain" },
+ ],
+ });
+
+ const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
+ Assert.equal(frecencyBoostedSpocs.length, 1);
+ Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname1");
+
+ sandbox.restore();
+ }
+ {
+ info(
+ "TopSitesFeed.fetchFrecencyBoostedSpocs - " +
+ "Should dedupe against matching topsite"
+ );
+ const feed = getTopSitesFeedForTest(sandbox, {
+ frecent: [{ url: "https://domain1.com", frecency: 1000 }],
+ linksWithDefaults: [
+ {
+ url: "https://domain1.com",
+ hostname: "hostname1",
+ label: "Domain 1",
+ },
+ ],
+ });
+
+ const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
+ Assert.equal(frecencyBoostedSpocs.length, 0);
+
+ sandbox.restore();
+ }
+ {
+ info(
+ "TopSitesFeed.fetchFrecencyBoostedSpocs - " +
+ "Should dedupe against matching topsite with path"
+ );
+ const feed = getTopSitesFeedForTest(sandbox, {
+ frecent: [{ url: "https://domain1.com", frecency: 1000 }],
+ linksWithDefaults: [
+ {
+ url: "https://domain1.com/page",
+ hostname: "hostname1",
+ label: "Domain 1",
+ },
+ ],
+ });
+
+ const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
+ Assert.equal(frecencyBoostedSpocs.length, 0);
+
+ sandbox.restore();
+ }
+});
+
+add_task(async function test_mergeSponsoredLinks_deduplication() {
+ {
+ let sandbox = sinon.createSandbox();
+ info("Two sponsored shortcuts with same hostname should dedupe");
+ const feed = getTopSitesFeedForTest(sandbox);
+
+ feed.store.state.TopSites.sov = {
+ ready: true,
+ positions: [
+ { position: 1, assignedPartner: "amp" },
+ { position: 2, assignedPartner: "moz-sales" },
+ ],
+ };
+
+ const sponsoredLinks = {
+ amp: [
+ {
+ url: "https://sponsor1.com",
+ hostname: "sponsor1",
+ label: "Sponsor 1",
+ },
+ ],
+ "moz-sales": [
+ {
+ url: "https://sponsor1.com",
+ hostname: "sponsor1",
+ label: "Different Label",
+ },
+ ],
+ "frec-boost": [],
+ };
+
+ const result = feed._mergeSponsoredLinks(sponsoredLinks);
+ Assert.equal(result.length, 1);
+ Assert.equal(result[0].label, "Sponsor 1");
+
+ sandbox.restore();
+ }
+ {
+ let sandbox = sinon.createSandbox();
+ info(
+ "Two sponsored shortcuts with same label but different hostname should dedupe"
+ );
+ const feed = getTopSitesFeedForTest(sandbox);
+
+ feed.store.state.TopSites.sov = {
+ ready: true,
+ positions: [
+ { position: 1, assignedPartner: "amp" },
+ { position: 2, assignedPartner: "moz-sales" },
+ ],
+ };
+
+ const sponsoredLinks = {
+ amp: [
+ {
+ url: "https://sponsor1.com",
+ hostname: "sponsor1",
+ label: "Brand Name",
+ },
+ ],
+ "moz-sales": [
+ {
+ url: "https://affiliate.com",
+ hostname: "affiliate",
+ label: "Brand Name",
+ },
+ ],
+ "frec-boost": [],
+ };
+
+ const result = feed._mergeSponsoredLinks(sponsoredLinks);
+ Assert.equal(result.length, 1);
+ Assert.equal(result[0].hostname, "sponsor1");
+
+ sandbox.restore();
+ }
+});
diff --git a/browser/extensions/newtab/test/xpcshell/test_TopSitesFeed_frecencyRanking.js b/browser/extensions/newtab/test/xpcshell/test_TopSitesFeed_frecencyRanking.js
@@ -237,10 +237,6 @@ add_task(async function test_frecency_sponsored_topsites() {
url: "https://domain3.com",
frecency: 1234,
},
- {
- url: "https://domain4.com",
- frecency: 1234,
- },
],
linksWithDefaults: [
{
@@ -249,12 +245,7 @@ add_task(async function test_frecency_sponsored_topsites() {
label: "",
},
{
- url: "",
- hostname: "",
- label: "hostname2",
- },
- {
- url: "https://hostname3.com",
+ url: "https://hostname2.com",
hostname: "",
label: "",
},
@@ -263,7 +254,7 @@ add_task(async function test_frecency_sponsored_topsites() {
const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
Assert.equal(frecencyBoostedSpocs.length, 1);
- Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname4");
+ Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname3");
sandbox.restore();
}
diff --git a/browser/extensions/newtab/test/xpcshell/xpcshell.toml b/browser/extensions/newtab/test/xpcshell/xpcshell.toml
@@ -52,6 +52,8 @@ support-files = ["../schemas/*.schema.json"]
["test_TopSitesFeed.js"]
+["test_TopSitesFeed_frecencyDeduping.js"]
+
["test_TopSitesFeed_frecencyRanking.js"]
["test_TopSitesFeed_glean.js"]