From b0de56f2a093f4834c64b9b1f33031b93acf059a Mon Sep 17 00:00:00 2001 From: emcminn Date: Tue, 28 Apr 2020 21:12:08 +0000 Subject: [PATCH] Bug 1631921 - Mochitests for AboutWelcomeActors & AboutWelcome Simplified r=pdahiya Differential Revision: https://phabricator.services.mozilla.com/D71824 --- .../newtab/test/browser/browser.ini | 1 + .../browser/browser_aboutwelcome_actors.js | 132 ++++++++++++++++++ .../browser_aboutwelcome_simplified.js | 73 ---------- 3 files changed, 133 insertions(+), 73 deletions(-) create mode 100644 browser/components/newtab/test/browser/browser_aboutwelcome_actors.js diff --git a/browser/components/newtab/test/browser/browser.ini b/browser/components/newtab/test/browser/browser.ini index 94b94034c1d7..c5a90993f302 100644 --- a/browser/components/newtab/test/browser/browser.ini +++ b/browser/components/newtab/test/browser/browser.ini @@ -14,6 +14,7 @@ prefs = browser.newtabpage.activity-stream.feeds.section.topstories.options={"provider_name":""} [browser_aboutwelcome.js] +[browser_aboutwelcome_actors.js] [browser_aboutwelcome_simplified.js] [browser_aboutwelcome_observer.js] [browser_as_load_location.js] diff --git a/browser/components/newtab/test/browser/browser_aboutwelcome_actors.js b/browser/components/newtab/test/browser/browser_aboutwelcome_actors.js new file mode 100644 index 000000000000..93b558f09561 --- /dev/null +++ b/browser/components/newtab/test/browser/browser_aboutwelcome_actors.js @@ -0,0 +1,132 @@ +"use strict"; + +const SEPARATE_ABOUT_WELCOME_PREF = "browser.aboutwelcome.enabled"; +const DID_SEE_ABOUT_WELCOME_PREF = "trailhead.firstrun.didSeeAboutWelcome"; + +const { PrivateBrowsingUtils } = ChromeUtils.import( + "resource://gre/modules/PrivateBrowsingUtils.jsm" +); +const { FxAccounts } = ChromeUtils.import( + "resource://gre/modules/FxAccounts.jsm" +); + +/** + * Sets the aboutwelcome pref to enabled simplified welcome UI + */ +async function setAboutWelcomePref(value) { + return pushPrefs([SEPARATE_ABOUT_WELCOME_PREF, value]); +} + +async function openAboutWelcome() { + let tab = await BrowserTestUtils.openNewForegroundTab( + gBrowser, + "about:welcome", + true + ); + registerCleanupFunction(() => { + BrowserTestUtils.removeTab(tab); + }); + return tab.linkedBrowser; +} + +// Test events from AboutWelcomeUtils +async function test_set_message() { + Services.prefs.setBoolPref(DID_SEE_ABOUT_WELCOME_PREF, false); + await openAboutWelcome(); + Assert.equal( + Services.prefs.getBoolPref(DID_SEE_ABOUT_WELCOME_PREF, false), + true, + "Pref was set" + ); +} + +async function test_open_awesome_bar(browser, message) { + await ContentTask.spawn(browser, message, async () => { + await ContentTaskUtils.waitForCondition( + () => + content.document.querySelector( + "button[data-l10n-id=onboarding-start-browsing-button-label]" + ), + "Wait for start browsing button to load" + ); + let button = content.document.querySelector( + "button[data-l10n-id=onboarding-start-browsing-button-label]" + ); + button.click(); + }); + Assert.ok(gURLBar.focused, "Focus should be on awesome bar"); +} + +async function test_open_private_browser(browser, message) { + let newWindowPromise = BrowserTestUtils.waitForNewWindow(); + await ContentTask.spawn(browser, message, async () => { + await ContentTaskUtils.waitForCondition( + () => + content.document.querySelector( + "button[data-l10n-id=onboarding-browse-privately-button]" + ), + "Wait for private browsing button to load" + ); + let button = content.document.querySelector( + "button[data-l10n-id=onboarding-browse-privately-button]" + ); + button.click(); + }); + let win = await newWindowPromise; + Assert.ok(PrivateBrowsingUtils.isWindowPrivate(win)); + await BrowserTestUtils.closeWindow(win); +} + +// Test Fxaccounts MetricsFlowURI + +add_task(function setup() { + const sandbox = sinon.createSandbox(); + sandbox.stub(FxAccounts.config, "promiseMetricsFlowURI").resolves(""); + + registerCleanupFunction(() => { + sandbox.restore(); + }); +}); + +test_newtab( + { + async before({ pushPrefs }) { + await pushPrefs(["browser.aboutwelcome.enabled", true]); + }, + test: async function test_startBrowsing() { + await ContentTaskUtils.waitForCondition( + () => + content.document.querySelector( + "button[data-l10n-id=onboarding-start-browsing-button-label]" + ), + "Wait for start browsing button to load" + ); + }, + after() { + ok( + FxAccounts.config.promiseMetricsFlowURI.callCount === 1, + "Stub was called" + ); + Assert.equal( + FxAccounts.config.promiseMetricsFlowURI.firstCall.args[0], + "aboutwelcome", + "Called by AboutWelcomeParent" + ); + }, + }, + "about:welcome" +); + +add_task(async function test_onContentMessage() { + await setAboutWelcomePref(true); + let browser = await openAboutWelcome(); + + //case "SET_WELCOME_MESSAGE_SEEN" + await test_set_message(); + + // //case "OPEN_AWESOME_BAR" + await test_open_awesome_bar(browser, "Open awesome bar"); + + //case "OPEN_PRIVATE_BROWSER_WINDOW" + await test_open_private_browser(browser, "Open private window"); +}); diff --git a/browser/components/newtab/test/browser/browser_aboutwelcome_simplified.js b/browser/components/newtab/test/browser/browser_aboutwelcome_simplified.js index 81a3df58116c..7d2e8167ae0b 100644 --- a/browser/components/newtab/test/browser/browser_aboutwelcome_simplified.js +++ b/browser/components/newtab/test/browser/browser_aboutwelcome_simplified.js @@ -2,10 +2,6 @@ const SEPARATE_ABOUT_WELCOME_PREF = "browser.aboutwelcome.enabled"; -const { FxAccounts } = ChromeUtils.import( - "resource://gre/modules/FxAccounts.jsm" -); - /** * Sets the aboutwelcome pref to enabled simplified welcome UI */ @@ -86,72 +82,3 @@ add_task(async function test_Separate_About_Welcome_branches() { ] ); }); - -/** - * Test click of StartBrowsing button on simplified about:welcome - * page changes focus to location bar - */ -test_newtab( - { - async before({ pushPrefs }) { - await pushPrefs(["browser.aboutwelcome.enabled", true]); - }, - test: async function test_startBrowsing() { - await ContentTaskUtils.waitForCondition( - () => - content.document.querySelector( - "button[data-l10n-id=onboarding-start-browsing-button-label]" - ), - "Wait for start browsing button to load" - ); - - const startBrowsingButton = content.document.querySelector( - "button[data-l10n-id=onboarding-start-browsing-button-label]" - ); - - startBrowsingButton.click(); - }, - after() { - ok( - gURLBar.focused, - "Start Browsing click should move focus to awesome bar" - ); - }, - }, - "about:welcome" -); - -add_task(function setup() { - const sandbox = sinon.createSandbox(); - sandbox.stub(FxAccounts.config, "promiseMetricsFlowURI").resolves(""); - - registerCleanupFunction(() => { - sandbox.restore(); - }); -}); - -test_newtab( - { - async before({ pushPrefs }) { - await pushPrefs(["browser.aboutwelcome.enabled", true]); - }, - test: async function test_startBrowsing() { - await ContentTaskUtils.waitForCondition( - () => content.document.querySelector(".start-button"), - "Wait for start browsing button to load" - ); - }, - after() { - ok( - FxAccounts.config.promiseMetricsFlowURI.callCount === 1, - "Stub was called" - ); - Assert.equal( - FxAccounts.config.promiseMetricsFlowURI.firstCall.args[0], - "aboutwelcome", - "Called by AboutWelcomeParent" - ); - }, - }, - "about:welcome" -);