Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

/* Any copyright is dedicated to the Public Domain.
"use strict";
const { FxaSignedInWatcher } = ChromeUtils.importESModule(
"moz-src:///browser/components/aiwindow/models/auth/FxaSignedInWatcher.sys.mjs"
);
const { getFxAccountsSingleton } = ChromeUtils.importESModule(
"resource://gre/modules/FxAccounts.sys.mjs"
);
const { UIState } = ChromeUtils.importESModule(
);
const { sinon } = ChromeUtils.importESModule(
);
/**
* Simulates a UIState update with the given status.
*
* @param {object} uiState The stubbed UIState.get.
* @param {string} status One of the UIState.STATUS_* constants.
*/
function updateUIState(uiState, status) {
uiState.returns({ status });
Services.obs.notifyObservers(null, UIState.ON_UPDATE);
}
add_task(async function test_watcher() {
const sb = sinon.createSandbox();
sb.stub(getFxAccountsSingleton(), "getSignedInUser").resolves({
uid: "uid",
verified: false,
});
const uiState = sb.stub(UIState, "get");
let changes = 0;
FxaSignedInWatcher.addEventListener(
"FxaSignedInWatcher:StateChanged",
() => changes++
);
try {
Assert.ok(
!(await FxaSignedInWatcher.isFxaSignedIn()),
"isFxaSignedIn waits for init and reports an unverified account"
);
Assert.ok(
!FxaSignedInWatcher.signedIn,
"An unverified account is not signed in"
);
Assert.equal(changes, 0, "No change from the initial state");
updateUIState(uiState, UIState.STATUS_SIGNED_IN);
Assert.ok(FxaSignedInWatcher.signedIn, "Signed in after verification");
Assert.ok(
await FxaSignedInWatcher.isFxaSignedIn(),
"isFxaSignedIn reports the verified account"
);
Assert.equal(changes, 1, "The change is notified");
updateUIState(uiState, UIState.STATUS_SIGNED_IN);
Assert.equal(changes, 1, "An update without a change is not notified");
updateUIState(uiState, UIState.STATUS_LOGIN_FAILED);
Assert.ok(!FxaSignedInWatcher.signedIn, "Not signed in after a failure");
Assert.equal(changes, 2, "The change is notified");
updateUIState(uiState, UIState.STATUS_NOT_CONFIGURED);
Assert.equal(changes, 2, "An update without a change is not notified");
} finally {
sb.restore();
}
});