diff options
Diffstat (limited to 'browser/components/newtab/test')
15 files changed, 510 insertions, 45 deletions
diff --git a/browser/components/newtab/test/browser/abouthomecache/browser_experiments_api_control.js b/browser/components/newtab/test/browser/abouthomecache/browser_experiments_api_control.js index a94f1fe055..0b49b3eb69 100644 --- a/browser/components/newtab/test/browser/abouthomecache/browser_experiments_api_control.js +++ b/browser/components/newtab/test/browser/abouthomecache/browser_experiments_api_control.js @@ -28,7 +28,9 @@ add_task(async function test_experiments_api_control() { }); Assert.ok( - !NimbusFeatures.abouthomecache.getVariable("enabled"), + !Services.prefs.getBoolPref( + "browser.startup.homepage.abouthome_cache.enabled" + ), "NimbusFeatures should tell us that the about:home startup cache " + "is disabled" ); @@ -51,7 +53,9 @@ add_task(async function test_experiments_api_control() { }); Assert.ok( - NimbusFeatures.abouthomecache.getVariable("enabled"), + Services.prefs.getBoolPref( + "browser.startup.homepage.abouthome_cache.enabled" + ), "NimbusFeatures should tell us that the about:home startup cache " + "is enabled" ); diff --git a/browser/components/newtab/test/browser/browser_customize_menu_content.js b/browser/components/newtab/test/browser/browser_customize_menu_content.js index ba83f1ff0a..fab032937a 100644 --- a/browser/components/newtab/test/browser/browser_customize_menu_content.js +++ b/browser/components/newtab/test/browser/browser_customize_menu_content.js @@ -1,5 +1,15 @@ "use strict"; +const { WeatherFeed } = ChromeUtils.importESModule( + "resource://activity-stream/lib/WeatherFeed.sys.mjs" +); + +ChromeUtils.defineESModuleGetters(this, { + MerinoTestUtils: "resource://testing-common/MerinoTestUtils.sys.mjs", +}); + +const { WEATHER_SUGGESTION } = MerinoTestUtils; + test_newtab({ async before({ pushPrefs }) { await pushPrefs( @@ -118,6 +128,79 @@ test_newtab({ }); test_newtab({ + async before({ pushPrefs }) { + sinon.stub(WeatherFeed.prototype, "MerinoClient").returns({ + fetch: () => [WEATHER_SUGGESTION], + }); + await pushPrefs( + ["browser.newtabpage.activity-stream.system.showWeather", true], + ["browser.newtabpage.activity-stream.showWeather", false] + ); + }, + test: async function test_render_customizeMenuWeather() { + // Weather Widget Fecthing + function getWeatherWidget() { + return content.document.querySelector(`.weather`); + } + + function promiseWeatherShown() { + return ContentTaskUtils.waitForMutationCondition( + content.document.querySelector("aside"), + { childList: true, subtree: true }, + () => getWeatherWidget() + ); + } + + const WEATHER_PREF = "browser.newtabpage.activity-stream.showWeather"; + + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector(".personalize-button"), + "Wait for prefs button to load on the newtab page" + ); + + let customizeButton = content.document.querySelector(".personalize-button"); + customizeButton.click(); + + let defaultPos = "matrix(1, 0, 0, 1, 0, 0)"; + await ContentTaskUtils.waitForCondition( + () => + content.getComputedStyle( + content.document.querySelector(".customize-menu") + ).transform === defaultPos, + "Customize Menu should be visible on screen" + ); + + // Test that clicking the weather toggle will make the + // weather widget appear on the newtab page. + // + // We waive XRay wrappers because we want to call the click() + // method defined on the toggle from this context. + let weatherSwitch = Cu.waiveXrays( + content.document.querySelector("#weather-section moz-toggle") + ); + Assert.ok( + !Services.prefs.getBoolPref(WEATHER_PREF), + "Weather pref is turned off" + ); + Assert.ok(!getWeatherWidget(), "Weather widget is not rendered"); + + let sectionShownPromise = promiseWeatherShown(); + weatherSwitch.click(); + await sectionShownPromise; + + Assert.ok(getWeatherWidget(), "Weather widget is rendered"); + }, + async after() { + Services.prefs.clearUserPref( + "browser.newtabpage.activity-stream.showWeather" + ); + Services.prefs.clearUserPref( + "browser.newtabpage.activity-stream.system.showWeather" + ); + }, +}); + +test_newtab({ test: async function test_open_close_customizeMenu() { const EventUtils = ContentTaskUtils.getEventUtils(content); await ContentTaskUtils.waitForCondition( diff --git a/browser/components/newtab/test/unit/content-src/components/CustomiseMenu.test.jsx b/browser/components/newtab/test/unit/content-src/components/CustomiseMenu.test.jsx index 0407622cf9..6186ca71fe 100644 --- a/browser/components/newtab/test/unit/content-src/components/CustomiseMenu.test.jsx +++ b/browser/components/newtab/test/unit/content-src/components/CustomiseMenu.test.jsx @@ -10,6 +10,7 @@ const DEFAULT_PROPS = { }, mayHaveSponsoredTopSites: true, mayHaveSponsoredStories: true, + mayHaveWeather: true, pocketRegion: true, dispatch: sinon.stub(), setPref: sinon.stub(), @@ -68,5 +69,9 @@ describe("ContentSection", () => { wrapper.find("#highlights-toggle").prop("data-eventSource"), "HIGHLIGHTS" ); + assert.equal( + wrapper.find("#weather-toggle").prop("data-eventSource"), + "WEATHER" + ); }); }); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamAdmin.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamAdmin.test.jsx index 7f40b66200..006e83e663 100644 --- a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamAdmin.test.jsx +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamAdmin.test.jsx @@ -67,6 +67,9 @@ describe("DiscoveryStreamAdmin", () => { otherPrefs={{}} state={{ DiscoveryStream: state, + Weather: { + suggestions: [], + }, }} /> ); @@ -90,7 +93,12 @@ describe("DiscoveryStreamAdmin", () => { wrapper = shallow( <DiscoveryStreamAdminUI otherPrefs={{}} - state={{ DiscoveryStream: state }} + state={{ + DiscoveryStream: state, + Weather: { + suggestions: [], + }, + }} /> ); wrapper.instance().onStoryToggle({ id: 12345 }); diff --git a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSCard.test.jsx b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSCard.test.jsx index afb6d6dcd2..796f805444 100644 --- a/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSCard.test.jsx +++ b/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/DSCard.test.jsx @@ -70,7 +70,9 @@ describe("<DSCard>", () => { }); it("should render DSLinkMenu", () => { - assert.equal(wrapper.children().at(3).type(), DSLinkMenu); + // Note: <DSLinkMenu> component moved from a direct child element of `.ds-card`. See Bug 1893936 + const default_link_menu = wrapper.find(DSLinkMenu); + assert.ok(default_link_menu.exists()); }); it("should start with no .active class", () => { diff --git a/browser/components/newtab/test/unit/lib/AboutPreferences.test.js b/browser/components/newtab/test/unit/lib/AboutPreferences.test.js index a19bf698d9..6555a1b77e 100644 --- a/browser/components/newtab/test/unit/lib/AboutPreferences.test.js +++ b/browser/components/newtab/test/unit/lib/AboutPreferences.test.js @@ -54,17 +54,21 @@ describe("AboutPreferences Feed", () => { instance.onAction(action); assert.calledOnce(action._target.browser.ownerGlobal.openPreferences); }); - it("should call .BrowserOpenAddonsMgr with the extension id on OPEN_WEBEXT_SETTINGS", () => { + it("should call .BrowserAddonUI.openAddonsMgr with the extension id on OPEN_WEBEXT_SETTINGS", () => { const action = { type: at.OPEN_WEBEXT_SETTINGS, data: "foo", _target: { - browser: { ownerGlobal: { BrowserOpenAddonsMgr: sinon.spy() } }, + browser: { + ownerGlobal: { + BrowserAddonUI: { openAddonsMgr: sinon.spy() }, + }, + }, }, }; instance.onAction(action); assert.calledWith( - action._target.browser.ownerGlobal.BrowserOpenAddonsMgr, + action._target.browser.ownerGlobal.BrowserAddonUI.openAddonsMgr, "addons://detail/foo" ); }); @@ -122,8 +126,9 @@ describe("AboutPreferences Feed", () => { const [, structure] = stub.firstCall.args; assert.equal(structure[0].id, "search"); assert.equal(structure[1].id, "topsites"); - assert.equal(structure[2].id, "topstories"); - assert.isEmpty(structure[2].rowsPref); + assert.equal(structure[2].id, "weather"); + assert.equal(structure[3].id, "topstories"); + assert.isEmpty(structure[3].rowsPref); }); }); describe("#renderPreferences", () => { diff --git a/browser/components/newtab/test/unit/lib/ActivityStream.test.js b/browser/components/newtab/test/unit/lib/ActivityStream.test.js index 7921ae2c91..ed00eb8202 100644 --- a/browser/components/newtab/test/unit/lib/ActivityStream.test.js +++ b/browser/components/newtab/test/unit/lib/ActivityStream.test.js @@ -311,6 +311,38 @@ describe("ActivityStream", () => { ); }); }); + describe("discoverystream.region-weather-config", () => { + let getVariableStub; + beforeEach(() => { + getVariableStub = sandbox.stub( + global.NimbusFeatures.pocketNewtab, + "getVariable" + ); + sandbox.stub(global.Region, "home").get(() => "CA"); + }); + it("should turn off weather system pref if no region weather config is set and no geo is set", () => { + getVariableStub.withArgs("regionWeatherConfig").returns(""); + sandbox.stub(global.Region, "home").get(() => ""); + + as._updateDynamicPrefs(); + + assert.isFalse(PREFS_CONFIG.get("system.showWeather").value); + }); + it("should turn on weather system pref based on region weather config pref", () => { + getVariableStub.withArgs("regionWeatherConfig").returns("CA"); + + as._updateDynamicPrefs(); + + assert.isTrue(PREFS_CONFIG.get("system.showWeather").value); + }); + it("should turn off weather system pref if no region weather config is set", () => { + getVariableStub.withArgs("regionWeatherConfig").returns(""); + + as._updateDynamicPrefs(); + + assert.isFalse(PREFS_CONFIG.get("system.showWeather").value); + }); + }); describe("_updateDynamicPrefs topstories default value", () => { let getVariableStub; let getBoolPrefStub; diff --git a/browser/components/newtab/test/unit/lib/ActivityStreamStorage.test.js b/browser/components/newtab/test/unit/lib/ActivityStreamStorage.test.js index 0b8baef762..fd56a3e185 100644 --- a/browser/components/newtab/test/unit/lib/ActivityStreamStorage.test.js +++ b/browser/components/newtab/test/unit/lib/ActivityStreamStorage.test.js @@ -47,6 +47,7 @@ describe("ActivityStreamStorage", () => { beforeEach(() => { storeStub = { getAll: sandbox.stub().resolves(), + getAllKeys: sandbox.stub().resolves(), get: sandbox.stub().resolves(), put: sandbox.stub().resolves(), }; @@ -75,6 +76,14 @@ describe("ActivityStreamStorage", () => { assert.calledOnce(storeStub.getAll); assert.deepEqual(result, ["bar"]); }); + it("should return the correct value for getAllKeys", async () => { + storeStub.getAllKeys.resolves(["key1", "key2", "key3"]); + + const result = await testStorage.getAllKeys(); + + assert.calledOnce(storeStub.getAllKeys); + assert.deepEqual(result, ["key1", "key2", "key3"]); + }); it("should query the correct object store", async () => { await testStorage.get(); diff --git a/browser/components/newtab/test/unit/lib/DiscoveryStreamFeed.test.js b/browser/components/newtab/test/unit/lib/DiscoveryStreamFeed.test.js index e10a4cbc04..72fc6bd0b8 100644 --- a/browser/components/newtab/test/unit/lib/DiscoveryStreamFeed.test.js +++ b/browser/components/newtab/test/unit/lib/DiscoveryStreamFeed.test.js @@ -3467,6 +3467,22 @@ describe("DiscoveryStreamFeed", () => { "https://bffApi/desktop/v1/recommendations?locale=$locale®ion=$region&count=30" ); }); + it("should update the new feed url with pocketFeedParameters", async () => { + globals.set("NimbusFeatures", { + pocketNewtab: { + getVariable: sandbox.stub(), + }, + }); + global.NimbusFeatures.pocketNewtab.getVariable + .withArgs("pocketFeedParameters") + .returns("&enableRankingByRegion=1"); + await feed.loadLayout(feed.store.dispatch); + const { layout } = feed.store.getState().DiscoveryStream; + assert.equal( + layout[0].components[2].feed.url, + "https://bffApi/desktop/v1/recommendations?locale=$locale®ion=$region&count=30&enableRankingByRegion=1" + ); + }); it("should fetch proper data from getComponentFeed", async () => { const fakeCache = {}; sandbox.stub(feed.cache, "get").returns(Promise.resolve(fakeCache)); diff --git a/browser/components/newtab/test/unit/lib/DownloadsManager.test.js b/browser/components/newtab/test/unit/lib/DownloadsManager.test.js index 5e2979893d..23ee9ffa34 100644 --- a/browser/components/newtab/test/unit/lib/DownloadsManager.test.js +++ b/browser/components/newtab/test/unit/lib/DownloadsManager.test.js @@ -29,6 +29,10 @@ describe("Downloads Manager", () => { showDownloadedFile: sinon.stub(), }); + globals.set("BrowserUtils", { + whereToOpenLink: sinon.stub().returns("current"), + }); + downloadsManager = new DownloadsManager(); downloadsManager.init({ dispatch() {} }); downloadsManager.onDownloadAdded({ diff --git a/browser/components/newtab/test/xpcshell/test_PlacesFeed.js b/browser/components/newtab/test/xpcshell/test_PlacesFeed.js index 78dda7818e..d6f7079d77 100644 --- a/browser/components/newtab/test/xpcshell/test_PlacesFeed.js +++ b/browser/components/newtab/test/xpcshell/test_PlacesFeed.js @@ -424,7 +424,7 @@ add_task(async function test_onAction_OPEN_LINK() { data: { url: "https://foo.com" }, _target: { browser: { - ownerGlobal: { openTrustedLinkIn, whereToOpenLink: () => "current" }, + ownerGlobal: { openTrustedLinkIn }, }, }, }; @@ -524,7 +524,7 @@ add_task(async function test_onAction_OPEN_LINK_pocket() { }, _target: { browser: { - ownerGlobal: { openTrustedLinkIn, whereToOpenLink: () => "current" }, + ownerGlobal: { openTrustedLinkIn }, }, }, }; @@ -551,7 +551,7 @@ add_task(async function test_onAction_OPEN_LINK_not_http() { data: { url: "file:///foo.com" }, _target: { browser: { - ownerGlobal: { openTrustedLinkIn, whereToOpenLink: () => "current" }, + ownerGlobal: { openTrustedLinkIn }, }, }, }; diff --git a/browser/components/newtab/test/xpcshell/test_TopSitesFeed.js b/browser/components/newtab/test/xpcshell/test_TopSitesFeed.js index 4be520fcca..247e08b333 100644 --- a/browser/components/newtab/test/xpcshell/test_TopSitesFeed.js +++ b/browser/components/newtab/test/xpcshell/test_TopSitesFeed.js @@ -2970,9 +2970,10 @@ add_task(async function test_ContileIntegration() { Assert.ok(fetched); // Both "foo" and "bar" should be filtered - Assert.equal(feed._contile.sites.length, 2); + Assert.equal(feed._contile.sites.length, 3); Assert.equal(feed._contile.sites[0].url, "https://www.test.com"); Assert.equal(feed._contile.sites[1].url, "https://test1.com"); + Assert.equal(feed._contile.sites[2].url, "https://test2.com"); } { diff --git a/browser/components/newtab/test/xpcshell/test_TopSitesFeed_glean.js b/browser/components/newtab/test/xpcshell/test_TopSitesFeed_glean.js index 5d13df0eb0..04501dbe53 100644 --- a/browser/components/newtab/test/xpcshell/test_TopSitesFeed_glean.js +++ b/browser/components/newtab/test/xpcshell/test_TopSitesFeed_glean.js @@ -47,6 +47,15 @@ let contileTile3 = { image_size: 200, impression_url: "https://impression_url.com", }; +let contileTile4 = { + id: 75899, + name: "Brand4", + url: "https://www.brand4.com", + click_url: "https://click_url.com", + image_url: "https://contile-images.jpg", + image_size: 200, + impression_url: "https://impression_url.com", +}; let mozSalesTile = [ { label: "MozSales Title", @@ -155,7 +164,12 @@ add_task(async function test_set_contile_tile_to_oversold() { let feed = getTopSitesFeedForTest(sandbox); feed._telemetryUtility.setSponsoredTilesConfigured(); - feed._telemetryUtility.setTiles([contileTile1, contileTile2, contileTile3]); + feed._telemetryUtility.setTiles([ + contileTile1, + contileTile2, + contileTile3, + contileTile4, + ]); let mergedTiles = [ { @@ -170,12 +184,18 @@ add_task(async function test_set_contile_tile_to_oversold() { sponsored_position: 2, partner: "amp", }, + { + url: "https://www.brand3.com", + label: "brand3", + sponsored_position: 3, + partner: "amp", + }, ]; feed._telemetryUtility.determineFilteredTilesAndSetToOversold(mergedTiles); feed._telemetryUtility.finalizeNewtabPingFields(mergedTiles); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -194,6 +214,12 @@ add_task(async function test_set_contile_tile_to_oversold() { { advertiser: "brand3", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand4", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -477,7 +503,12 @@ add_task(async function test_set_tiles_to_dismissed_then_updated() { feed._telemetryUtility.setSponsoredTilesConfigured(); // Step 1: Set initial tiles - feed._telemetryUtility.setTiles([contileTile1, contileTile2, contileTile3]); + feed._telemetryUtility.setTiles([ + contileTile1, + contileTile2, + contileTile3, + contileTile4, + ]); // Step 2: Set all tiles to dismissed feed._telemetryUtility.determineFilteredTilesAndSetToDismissed([]); @@ -495,12 +526,18 @@ add_task(async function test_set_tiles_to_dismissed_then_updated() { sponsored_position: 2, partner: "amp", }, + { + url: "https://www.brand3.com", + label: "brand3", + sponsored_position: 3, + partner: "amp", + }, ]; // Step 3: Finalize with the updated list of tiles. feed._telemetryUtility.finalizeNewtabPingFields(updatedTiles); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -522,6 +559,12 @@ add_task(async function test_set_tiles_to_dismissed_then_updated() { display_position: null, display_fail_reason: "dismissed", }, + { + advertiser: "brand4", + provider: "amp", + display_position: null, + display_fail_reason: "dismissed", + }, ], }; Assert.equal( @@ -537,7 +580,12 @@ add_task(async function test_set_tile_positions_after_updated_list() { feed._telemetryUtility.setSponsoredTilesConfigured(); // Step 1: Set initial tiles - feed._telemetryUtility.setTiles([contileTile1, contileTile2, contileTile3]); + feed._telemetryUtility.setTiles([ + contileTile1, + contileTile2, + contileTile3, + contileTile4, + ]); // Step 2: Set 1 tile to oversold (brand3) let mergedTiles = [ @@ -553,6 +601,12 @@ add_task(async function test_set_tile_positions_after_updated_list() { sponsored_position: 2, partner: "amp", }, + { + url: "https://www.brand3.com", + label: "brand3", + sponsored_position: 3, + partner: "amp", + }, ]; feed._telemetryUtility.determineFilteredTilesAndSetToOversold(mergedTiles); @@ -570,10 +624,16 @@ add_task(async function test_set_tile_positions_after_updated_list() { sponsored_position: 2, partner: "amp", }, + { + url: "https://www.brand3.com", + label: "brand3", + sponsored_position: 3, + partner: "amp", + }, ]; feed._telemetryUtility.finalizeNewtabPingFields(updatedTiles); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -592,6 +652,12 @@ add_task(async function test_set_tile_positions_after_updated_list() { { advertiser: "brand3", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand4", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -610,7 +676,12 @@ add_task(async function test_set_tile_positions_after_updated_list_all_tiles() { feed._telemetryUtility.setSponsoredTilesConfigured(); // Step 1: Set initial tiles - feed._telemetryUtility.setTiles([contileTile1, contileTile2, contileTile3]); + feed._telemetryUtility.setTiles([ + contileTile1, + contileTile2, + contileTile3, + contileTile4, + ]); // Step 2: Set 1 tile to oversold (brand3) let mergedTiles = [ @@ -626,6 +697,12 @@ add_task(async function test_set_tile_positions_after_updated_list_all_tiles() { sponsored_position: 2, partner: "amp", }, + { + url: "https://www.brand3.com", + label: "brand3", + sponsored_position: 3, + partner: "amp", + }, ]; feed._telemetryUtility.determineFilteredTilesAndSetToOversold(mergedTiles); @@ -643,10 +720,16 @@ add_task(async function test_set_tile_positions_after_updated_list_all_tiles() { sponsored_position: 2, partner: "amp", }, + { + url: "https://www.replacement3.com", + label: "replacement3", + sponsored_position: 3, + partner: "amp", + }, ]; feed._telemetryUtility.finalizeNewtabPingFields(updatedTiles); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -665,6 +748,12 @@ add_task(async function test_set_tile_positions_after_updated_list_all_tiles() { { advertiser: "brand3", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand4", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -684,7 +773,12 @@ add_task( feed._telemetryUtility.setSponsoredTilesConfigured(); // Step 1: Set initial tiles - feed._telemetryUtility.setTiles([contileTile1, contileTile2, contileTile3]); + feed._telemetryUtility.setTiles([ + contileTile1, + contileTile2, + contileTile3, + contileTile4, + ]); // Step 2: Set 1 tile to oversold (brand3) let mergedTiles = [ @@ -700,6 +794,12 @@ add_task( sponsored_position: 2, partner: "amp", }, + { + url: "https://www.brand3.com", + label: "brand3", + sponsored_position: 3, + partner: "amp", + }, ]; feed._telemetryUtility.determineFilteredTilesAndSetToOversold(mergedTiles); @@ -717,10 +817,16 @@ add_task( sponsored_position: 2, partner: "amp", }, + { + url: "https://www.brand3.com", + label: "brand3", + sponsored_position: 3, + partner: "amp", + }, ]; feed._telemetryUtility.finalizeNewtabPingFields(updatedTiles); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -739,6 +845,12 @@ add_task( { advertiser: "brand3", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand4", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -901,7 +1013,7 @@ add_task(async function test_all_tiles_displayed() { await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -961,18 +1073,25 @@ add_task(async function test_set_one_tile_display_fail_reason_to_oversold() { impression_url: "https://www.brand3-impression.com", name: "brand3", }, + { + url: "https://www.brand4.com", + image_url: "images/brnad4-com.png", + click_url: "https://www.brand4-click.com", + impression_url: "https://www.brand4-impression.com", + name: "brand4", + }, ], }), }); const fetched = await feed._contile._fetchSites(); Assert.ok(fetched); - Assert.equal(feed._contile.sites.length, 2); + Assert.equal(feed._contile.sites.length, 3); await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ { @@ -990,6 +1109,12 @@ add_task(async function test_set_one_tile_display_fail_reason_to_oversold() { { advertiser: "brand3", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand4", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -1052,7 +1177,7 @@ add_task(async function test_set_one_tile_display_fail_reason_to_dismissed() { await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -1129,6 +1254,13 @@ add_task( impression_url: "https://www.brand4-impression.com", name: "brand4", }, + { + url: "https://www.brand5.com", + image_url: "images/brand5-com.png", + click_url: "https://www.brand5-click.com", + impression_url: "https://www.brand5-impression.com", + name: "brand5", + }, ], }), }); @@ -1140,12 +1272,12 @@ add_task( const fetched = await feed._contile._fetchSites(); Assert.ok(fetched); - Assert.equal(feed._contile.sites.length, 2); + Assert.equal(feed._contile.sites.length, 3); await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -1170,6 +1302,12 @@ add_task( { advertiser: "brand4", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand5", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -1233,7 +1371,7 @@ add_task( await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -1297,6 +1435,13 @@ add_task(async function test_update_tile_count() { impression_url: "https://www.brand3-impression.com", name: "brand3", }, + { + url: "https://www.brand4.com", + image_url: "images/brand4-com.png", + click_url: "https://www.brand4-click.com", + impression_url: "https://www.brand4-impression.com", + name: "brand4", + }, ], }), }); @@ -1304,11 +1449,11 @@ add_task(async function test_update_tile_count() { // 1. Initially the Nimbus pref is set to 2 tiles let fetched = await feed._contile._fetchSites(); Assert.ok(fetched); - Assert.equal(feed._contile.sites.length, 2); + Assert.equal(feed._contile.sites.length, 3); await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -1327,6 +1472,12 @@ add_task(async function test_update_tile_count() { { advertiser: "brand3", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand4", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -1344,7 +1495,7 @@ add_task(async function test_update_tile_count() { ); setNimbusVariablesForNumTiles(nimbusPocketStub, 3); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); expectedResult = { sponsoredTilesReceived: [ @@ -1363,6 +1514,12 @@ add_task(async function test_update_tile_count() { { advertiser: "brand3", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand4", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -1402,6 +1559,12 @@ add_task(async function test_update_tile_count() { display_position: 3, display_fail_reason: null, }, + { + advertiser: "brand4", + provider: "amp", + display_position: null, + display_fail_reason: "oversold", + }, ], }; Assert.equal( @@ -1442,6 +1605,13 @@ add_task(async function test_update_tile_count_sourced_from_cache() { impression_url: "https://www.brand3-impression.com", name: "brand3", }, + { + url: "https://www.brand4.com", + image_url: "images/brand4-com.png", + click_url: "https://www.brand4-click.com", + impression_url: "https://www.brand4-impression.com", + name: "brand4", + }, ]; Services.prefs.setStringPref(CONTILE_CACHE_PREF, JSON.stringify(tiles)); @@ -1459,11 +1629,11 @@ add_task(async function test_update_tile_count_sourced_from_cache() { // Ensure ContileIntegration._fetchSites is working populate _sites and initilize TelemetryUtility let fetched = await feed._contile._fetchSites(); Assert.ok(fetched); - Assert.equal(feed._contile.sites.length, 3); + Assert.equal(feed._contile.sites.length, 4); await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -1482,6 +1652,12 @@ add_task(async function test_update_tile_count_sourced_from_cache() { { advertiser: "brand3", provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { + advertiser: "brand4", + provider: "amp", display_position: null, display_fail_reason: "oversold", }, @@ -1499,12 +1675,12 @@ add_task(async function test_update_tile_count_sourced_from_cache() { ); setNimbusVariablesForNumTiles(nimbusPocketStub, 3); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); // 3. Confirm the new count is applied when data pulled from Contile, 3 tiles displayed fetched = await feed._contile._fetchSites(); Assert.ok(fetched); - Assert.equal(feed._contile.sites.length, 3); + Assert.equal(feed._contile.sites.length, 4); await feed._readDefaults(); await feed.getLinksWithDefaults(false); @@ -1530,6 +1706,12 @@ add_task(async function test_update_tile_count_sourced_from_cache() { display_position: 3, display_fail_reason: null, }, + { + advertiser: "brand4", + provider: "amp", + display_position: null, + display_fail_reason: "oversold", + }, ], }; Assert.equal( @@ -1595,7 +1777,7 @@ add_task( await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -1636,7 +1818,7 @@ add_task( await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); expectedResult = { sponsoredTilesReceived: [ @@ -1684,7 +1866,7 @@ add_task(async function test_sponsoredTilesReceived_not_set() { await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [] }; Assert.equal( @@ -1744,7 +1926,7 @@ add_task(async function test_telemetry_data_updates() { await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -1896,7 +2078,7 @@ add_task(async function test_reset_telemetry_data() { await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ @@ -1934,7 +2116,7 @@ add_task(async function test_reset_telemetry_data() { await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); expectedResult = { sponsoredTilesReceived: [] }; Assert.equal( @@ -1982,17 +2164,24 @@ add_task(async function test_set_telemetry_for_moz_sales_tiles() { impression_url: "https://www.brand2-impression.com", name: "brand2", }, + { + url: "https://www.brand3.com", + image_url: "images/brand3-com.png", + click_url: "https://www.brand3-click.com", + impression_url: "https://www.brand3-impression.com", + name: "brand2", + }, ], }), }); const fetched = await feed._contile._fetchSites(); Assert.ok(fetched); - Assert.equal(feed._contile.sites.length, 2); + Assert.equal(feed._contile.sites.length, 3); await feed._readDefaults(); await feed.getLinksWithDefaults(false); - Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 2); + Assert.equal(Glean.topsites.sponsoredTilesConfigured.testGetValue(), 3); let expectedResult = { sponsoredTilesReceived: [ { @@ -2008,6 +2197,12 @@ add_task(async function test_set_telemetry_for_moz_sales_tiles() { display_fail_reason: null, }, { + advertiser: "brand3", + provider: "amp", + display_position: 3, + display_fail_reason: null, + }, + { advertiser: "mozsales title", provider: "moz-sales", display_position: null, diff --git a/browser/components/newtab/test/xpcshell/test_WeatherFeed.js b/browser/components/newtab/test/xpcshell/test_WeatherFeed.js new file mode 100644 index 0000000000..2821f4b7d0 --- /dev/null +++ b/browser/components/newtab/test/xpcshell/test_WeatherFeed.js @@ -0,0 +1,99 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const { WeatherFeed } = ChromeUtils.importESModule( + "resource://activity-stream/lib/WeatherFeed.sys.mjs" +); + +const { actionCreators: ac, actionTypes: at } = ChromeUtils.importESModule( + "resource://activity-stream/common/Actions.mjs" +); + +ChromeUtils.defineESModuleGetters(this, { + sinon: "resource://testing-common/Sinon.sys.mjs", + MerinoTestUtils: "resource://testing-common/MerinoTestUtils.sys.mjs", +}); + +const { WEATHER_SUGGESTION } = MerinoTestUtils; + +const WEATHER_ENABLED = "browser.newtabpage.activity-stream.showWeather"; +const SYS_WEATHER_ENABLED = + "browser.newtabpage.activity-stream.system.showWeather"; + +add_task(async function test_construction() { + let sandbox = sinon.createSandbox(); + sandbox.stub(WeatherFeed.prototype, "PersistentCache").returns({ + set: () => {}, + get: () => {}, + }); + + let feed = new WeatherFeed(); + + info("WeatherFeed constructor should create initial values"); + + Assert.ok(feed, "Could construct a WeatherFeed"); + Assert.ok(feed.loaded === false, "WeatherFeed is not loaded"); + Assert.ok(feed.merino === null, "merino is initialized as null"); + Assert.ok( + feed.suggestions.length === 0, + "suggestions is initialized as a array with length of 0" + ); + Assert.ok(feed.fetchTimer === null, "fetchTimer is initialized as null"); + sandbox.restore(); +}); + +add_task(async function test_onAction_INIT() { + let sandbox = sinon.createSandbox(); + sandbox.stub(WeatherFeed.prototype, "MerinoClient").returns({ + get: () => [WEATHER_SUGGESTION], + on: () => {}, + }); + sandbox.stub(WeatherFeed.prototype, "PersistentCache").returns({ + set: () => {}, + get: () => {}, + }); + const dateNowTestValue = 1; + sandbox.stub(WeatherFeed.prototype, "Date").returns({ + now: () => dateNowTestValue, + }); + + let feed = new WeatherFeed(); + + Services.prefs.setBoolPref(WEATHER_ENABLED, true); + Services.prefs.setBoolPref(SYS_WEATHER_ENABLED, true); + + sandbox.stub(feed, "isEnabled").returns(true); + + sandbox.stub(feed, "fetchHelper"); + feed.suggestions = [WEATHER_SUGGESTION]; + + feed.store = { + dispatch: sinon.spy(), + }; + + info("WeatherFeed.onAction INIT should initialize Weather"); + + await feed.onAction({ + type: at.INIT, + }); + + Assert.ok(feed.store.dispatch.calledOnce); + Assert.ok( + feed.store.dispatch.calledWith( + ac.BroadcastToContent({ + type: at.WEATHER_UPDATE, + data: { + suggestions: [WEATHER_SUGGESTION], + lastUpdated: dateNowTestValue, + }, + meta: { + isStartup: true, + }, + }) + ) + ); + Services.prefs.clearUserPref(WEATHER_ENABLED); + sandbox.restore(); +}); diff --git a/browser/components/newtab/test/xpcshell/xpcshell.toml b/browser/components/newtab/test/xpcshell/xpcshell.toml index 13c11b0541..567927c31c 100644 --- a/browser/components/newtab/test/xpcshell/xpcshell.toml +++ b/browser/components/newtab/test/xpcshell/xpcshell.toml @@ -28,3 +28,5 @@ support-files = ["../schemas/*.schema.json"] ["test_TopSitesFeed_glean.js"] ["test_WallpaperFeed.js"] + +["test_WeatherFeed.js"] |