tor-browser

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

commit 2be29d8370b79f15355c84519e34b6bcfbcbffc0
parent 34dced049f1fb06f781eaf454349081b859b060d
Author: scottdowne <sdowne@mozilla.com>
Date:   Mon,  8 Dec 2025 22:01:56 +0000

Bug 2004619 - Newtab frecency-based sorting unit tests r=home-newtab-reviewers,nbarrett

Differential Revision: https://phabricator.services.mozilla.com/D275405

Diffstat:
Mbrowser/extensions/newtab/lib/TopSitesFeed.sys.mjs | 1-
Abrowser/extensions/newtab/test/xpcshell/test_TopSitesFeed_frecencyRanking.js | 269+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mbrowser/extensions/newtab/test/xpcshell/xpcshell.toml | 2++
3 files changed, 271 insertions(+), 1 deletion(-)

diff --git a/browser/extensions/newtab/lib/TopSitesFeed.sys.mjs b/browser/extensions/newtab/lib/TopSitesFeed.sys.mjs @@ -1497,7 +1497,6 @@ export class TopSitesFeed { hostname: domainObj.hostname, url: domainObj.redirectURL, label: domainObj.title, - sponsored_position: 3, partner: SPONSORED_TILE_PARTNER_FREC_BOOST, type: "frecency-boost", frecency: site.frecency, diff --git a/browser/extensions/newtab/test/xpcshell/test_TopSitesFeed_frecencyRanking.js b/browser/extensions/newtab/test/xpcshell/test_TopSitesFeed_frecencyRanking.js @@ -0,0 +1,269 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +ChromeUtils.defineESModuleGetters(this, { + actionCreators: "resource://newtab/common/Actions.mjs", + actionTypes: "resource://newtab/common/Actions.mjs", + 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 = [ + { + url: "url", + hostname: "hostname", + label: "label", + }, + ], + } = {} +) { + let feed = new TopSitesFeed(); + + feed.store = { + getState() { + return this.state; + }, + state: { + Prefs: { + values: { + [PREF_SOV_ENABLED]: true, + [SHOW_SPONSORED_PREF]: true, + [ROWS_PREF]: 1, + }, + }, + }, + }; + feed.frecentCache = { + request() { + return this.cache; + }, + cache: frecent, + }; + feed._linksWithDefaults = linksWithDefaults; + 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", + }, + ], + [ + "hostname3", + { + domain: "https://domain3.com", + faviconDataURI: "faviconDataURI3", + hostname: "hostname3", + redirectURL: "https://redirectURL3.com", + title: "title3", + }, + ], + [ + "hostname4", + { + domain: "https://domain4.com", + faviconDataURI: "faviconDataURI4", + hostname: "hostname4", + redirectURL: "https://redirectURL4.com", + title: "title4", + }, + ], + [ + "hostname1sub", + { + domain: "https://sub.domain1.com", + faviconDataURI: "faviconDataURI1", + hostname: "hostname1sub", + redirectURL: "https://redirectURL1.com", + title: "title1", + }, + ], + ]); + + sandbox.stub(feed, "_frecencyBoostedSponsors").value(frecencyBoostedSponsors); + + return feed; +} + +add_task(async function test_frecency_sponsored_topsites() { + let sandbox = sinon.createSandbox(); + { + info( + "TopSitesFeed.fetchFrecencyBoostedSpocs - " + + "Should return an empty array with no history" + ); + const feed = getTopSitesFeedForTest(sandbox); + + const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs(); + Assert.equal(frecencyBoostedSpocs.length, 0); + + sandbox.restore(); + } + { + info( + "TopSitesFeed.fetchFrecencyBoostedSpocs - " + + "Should return a single match with the right format" + ); + const feed = getTopSitesFeedForTest(sandbox, { + frecent: [ + { + url: "https://domain1.com", + frecency: 1234, + }, + ], + }); + + const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs(); + Assert.equal(frecencyBoostedSpocs.length, 1); + Assert.deepEqual(frecencyBoostedSpocs[0], { + hostname: "hostname1", + url: "https://redirectURL1.com", + label: "title1", + partner: "frec-boost", + type: "frecency-boost", + frecency: 1234, + show_sponsored_label: true, + favicon: "faviconDataURI1", + faviconSize: 96, + }); + + sandbox.restore(); + } + { + info( + "TopSitesFeed.fetchFrecencyBoostedSpocs - " + + "Should return multiple matches" + ); + const feed = getTopSitesFeedForTest(sandbox, { + frecent: [ + { + url: "https://domain1.com", + frecency: 1234, + }, + { + url: "https://domain3.com", + frecency: 1234, + }, + ], + }); + + const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs(); + Assert.equal(frecencyBoostedSpocs.length, 2); + Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname1"); + Assert.equal(frecencyBoostedSpocs[1].hostname, "hostname3"); + + sandbox.restore(); + } + { + info( + "TopSitesFeed.fetchFrecencyBoostedSpocs - " + + "Should return a single match with partial url" + ); + const feed = getTopSitesFeedForTest(sandbox, { + frecent: [ + { + url: "https://domain1.com/path", + frecency: 1234, + }, + ], + }); + + const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs(); + Assert.equal(frecencyBoostedSpocs.length, 1); + Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname1"); + + sandbox.restore(); + } + /*{ + info( + "TopSitesFeed.fetchFrecencyBoostedSpocs - " + + "Should return a single match with a subdomain" + ); + const feed = getTopSitesFeedForTest(sandbox, { + frecent: [ + { + url: "https://www.domain1.com", + frecency: 1234, + }, + ], + }); + + const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs(); + Assert.equal(frecencyBoostedSpocs.length, 1); + Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname1"); + + sandbox.restore(); + }*/ + { + info( + "TopSitesFeed.fetchFrecencyBoostedSpocs - " + + "Should return a single match with a subdomain" + ); + const feed = getTopSitesFeedForTest(sandbox, { + frecent: [ + { + url: "https://domain1.com", + frecency: 1234, + }, + { + url: "https://domain2.com", + frecency: 1234, + }, + { + url: "https://domain3.com", + frecency: 1234, + }, + { + url: "https://domain4.com", + frecency: 1234, + }, + ], + linksWithDefaults: [ + { + url: "", + hostname: "hostname1", + label: "", + }, + { + url: "", + hostname: "", + label: "hostname2", + }, + { + url: "https://hostname3.com", + hostname: "", + label: "", + }, + ], + }); + + const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs(); + Assert.equal(frecencyBoostedSpocs.length, 1); + Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname4"); + + 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_frecencyRanking.js"] + ["test_TopSitesFeed_glean.js"] ["test_WallpaperFeed.js"]