Backed out changeset 7b2ffe9a4d06 (bug 1869605) for causing bc failures on browser_no...
[gecko.git] / browser / components / firefoxview / tests / browser / browser_setup_primary_password.js
blob7f8722d808c40e5db826a41f68721a6b484c71fc
1 /* Any copyright is dedicated to the Public Domain.
2  * http://creativecommons.org/publicdomain/zero/1.0/ */
4 const { LoginTestUtils } = ChromeUtils.importESModule(
5   "resource://testing-common/LoginTestUtils.sys.mjs"
6 );
8 async function tearDown(sandbox) {
9   sandbox?.restore();
10   Services.prefs.clearUserPref("services.sync.lastTabFetch");
13 function setupMocks() {
14   const sandbox = (gSandbox = setupRecentDeviceListMocks());
15   return sandbox;
18 add_setup(async function () {
19   registerCleanupFunction(async () => {
20     // reset internal state so it doesn't affect the next tests
21     TabsSetupFlowManager.resetInternalState();
22     LoginTestUtils.primaryPassword.disable();
23     await tearDown(gSandbox);
24   });
25   await SpecialPowers.pushPrefEnv({
26     set: [["services.sync.username", "username@example.com"]],
27   });
28 });
30 add_task(async function test_primary_password_locked() {
31   LoginTestUtils.primaryPassword.enable();
32   const sandbox = setupMocks();
34   await withFirefoxView({}, async browser => {
35     sandbox.stub(TabsSetupFlowManager, "syncTabs").resolves(null);
36     const syncedTabsMock = sandbox.stub(SyncedTabs, "getRecentTabs");
37     syncedTabsMock.resolves(getMockTabData(syncedTabsData1));
39     const { document } = browser.contentWindow;
40     Services.obs.notifyObservers(null, UIState.ON_UPDATE);
42     info("waiting for the error setup step to be visible");
43     await waitForVisibleSetupStep(browser, {
44       expectedVisible: "#tabpickup-steps-view0",
45     });
47     const errorStateHeader = document.querySelector(
48       "#tabpickup-steps-view0-header"
49     );
50     await BrowserTestUtils.waitForMutationCondition(
51       errorStateHeader,
52       { childList: true },
53       () => errorStateHeader.textContent.includes("Enter your Primary Password")
54     );
56     ok(
57       errorStateHeader.getAttribute("data-l10n-id").includes("password-locked"),
58       "Correct error message is shown"
59     );
61     const errorLink = document.querySelector("#error-state-link");
62     ok(
63       errorLink && BrowserTestUtils.is_visible(errorLink),
64       "Error link is visible"
65     );
66     ok(
67       errorLink.getAttribute("data-l10n-id").includes("password-locked-link"),
68       "Correct link text is shown"
69     );
71     const primaryButton = document.querySelector("#error-state-button");
72     ok(
73       primaryButton && BrowserTestUtils.is_visible(primaryButton),
74       "Error primary button is visible"
75     );
77     const clearErrorStub = sandbox.stub(
78       TabsSetupFlowManager,
79       "tryToClearError"
80     );
81     info("Setup state:" + TabsSetupFlowManager.currentSetupState.name);
83     info("clicking the error panel button");
84     primaryButton.click();
85     ok(
86       clearErrorStub.called,
87       "tryToClearError was called when the try-again button was clicked"
88     );
89     TabsSetupFlowManager.tryToClearError.restore();
91     info("Clearing the primary password");
92     LoginTestUtils.primaryPassword.disable();
93     ok(
94       !TabsSetupFlowManager.isPrimaryPasswordLocked,
95       "primary password is unlocked"
96     );
98     info("notifying of the primary-password unlock");
99     const clearErrorSpy = sandbox.spy(TabsSetupFlowManager, "tryToClearError");
100     // we stubbed out sync, so pretend it ran.
101     info("notifying of sync:finish");
102     Services.obs.notifyObservers(null, "weave:service:sync:finish");
104     const setupContainer = document.querySelector(".sync-setup-container");
105     // wait until the setup container gets hidden before checking if the tabs container is visible
106     // as it may not exist until then
107     let setupHiddenPromise = BrowserTestUtils.waitForMutationCondition(
108       setupContainer,
109       {
110         attributeFilter: ["hidden"],
111       },
112       () => {
113         return BrowserTestUtils.is_hidden(setupContainer);
114       }
115     );
117     Services.obs.notifyObservers(null, "passwordmgr-crypto-login");
118     await setupHiddenPromise;
119     ok(
120       clearErrorSpy.called,
121       "tryToClearError was called when the primary-password unlock notification was received"
122     );
123     // We expect the waiting state until we get a sync update/finished
124     info("Setup state:" + TabsSetupFlowManager.currentSetupState.name);
126     ok(TabsSetupFlowManager.waitingForTabs, "Now waiting for tabs");
127     ok(
128       document
129         .querySelector("#tabpickup-tabs-container")
130         .classList.contains("loading"),
131       "Synced tabs container has loading class"
132     );
134     info("notifying of sync:finish");
135     Services.obs.notifyObservers(null, "weave:service:sync:finish");
136     await TestUtils.waitForTick();
137     ok(
138       !document
139         .querySelector("#tabpickup-tabs-container")
140         .classList.contains("loading"),
141       "Synced tabs isn't loading any more"
142     );
143   });
144   await tearDown(sandbox);