Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

/* Any copyright is dedicated to the Public Domain.
"use strict";
ChromeUtils.defineESModuleGetters(this, {
});
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",
{
faviconDataURI: "faviconDataURI1",
hostname: "hostname1sub",
redirectURL: "https://redirectURL1.com",
title: "title1",
},
],
]);
sandbox.stub(feed, "_frecencyBoostedSponsors").value(frecencyBoostedSponsors);
return feed;
}
// eslint-disable-next-line max-statements
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: [
{
frecency: 1234,
},
],
});
const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
Assert.equal(frecencyBoostedSpocs.length, 1);
Assert.deepEqual(frecencyBoostedSpocs[0], {
hostname: "hostname1",
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: [
{
frecency: 1234,
},
{
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: [
{
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: [
{
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: [
{
frecency: 1234,
},
{
frecency: 1234,
},
{
frecency: 1234,
},
],
linksWithDefaults: [
{
url: "",
hostname: "hostname1",
label: "",
},
{
hostname: "",
label: "",
},
],
});
const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
Assert.equal(frecencyBoostedSpocs.length, 1);
Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname3");
sandbox.restore();
}
{
info(
"TopSitesFeed.fetchFrecencyBoostedSpocs - " +
"Should not return a match with a different subdomain"
);
const feed = getTopSitesFeedForTest(sandbox, {
frecent: [
{
frecency: 1234,
},
],
});
const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
Assert.equal(frecencyBoostedSpocs.length, 0);
sandbox.restore();
}
{
info(
"TopSitesFeed.fetchFrecencyBoostedSpocs - " +
"Should return a match with the same subdomain"
);
const feed = getTopSitesFeedForTest(sandbox, {
frecent: [
{
frecency: 1234,
},
],
});
const frecencyBoostedSpocs = await feed.fetchFrecencyBoostedSpocs();
Assert.equal(frecencyBoostedSpocs.length, 1);
Assert.equal(frecencyBoostedSpocs[0].hostname, "hostname1sub");
sandbox.restore();
}
});