Bug 1899500 - Implement explicit resource management in Baseline compiler. r=arai
[gecko.git] / browser / components / preferences / tests / browser_primaryPassword.js
blob2c42bd8d918a4465ab7e0d9f81d4fdfc2b5aa714
1 const { OSKeyStoreTestUtils } = ChromeUtils.importESModule(
2   "resource://testing-common/OSKeyStoreTestUtils.sys.mjs"
3 );
4 const { OSKeyStore } = ChromeUtils.importESModule(
5   "resource://gre/modules/OSKeyStore.sys.mjs"
6 );
8 add_task(async function () {
9   let prefs = await openPreferencesViaOpenPreferencesAPI("panePrivacy", {
10     leaveOpen: true,
11   });
12   is(prefs.selectedPane, "panePrivacy", "Privacy pane was selected");
14   let doc = gBrowser.contentDocument;
15   // Fake the subdialog and LoginHelper
16   let win = doc.defaultView;
17   let dialogURL = "";
18   let dialogOpened = false;
19   ChromeUtils.defineLazyGetter(win, "gSubDialog", () => ({
20     open(aDialogURL, { closingCallback: aCallback }) {
21       dialogOpened = true;
22       dialogURL = aDialogURL;
23       primaryPasswordSet = primaryPasswordNextState;
24       aCallback();
25     },
26   }));
28   let primaryPasswordSet = false;
29   win.LoginHelper = {
30     isPrimaryPasswordSet() {
31       return primaryPasswordSet;
32     },
33     getOSAuthEnabled() {
34       return true; // Since enabled by default.
35     },
36   };
38   let checkbox = doc.querySelector("#useMasterPassword");
39   checkbox.scrollIntoView();
40   ok(
41     !checkbox.checked,
42     "primary password checkbox should be unchecked by default"
43   );
44   let button = doc.getElementById("changeMasterPassword");
45   ok(button.disabled, "primary password button should be disabled by default");
47   let primaryPasswordNextState = false;
48   if (OSKeyStoreTestUtils.canTestOSKeyStoreLogin() && OSKeyStore.canReauth()) {
49     let osAuthDialogShown = OSKeyStoreTestUtils.waitForOSKeyStoreLogin(false);
50     checkbox.click();
51     info("waiting for os auth dialog to appear and get canceled");
52     await osAuthDialogShown;
53     await TestUtils.waitForCondition(
54       () => !checkbox.checked,
55       "wait for checkbox to get unchecked"
56     );
57     ok(!dialogOpened, "the dialog should not have opened");
58     ok(
59       !dialogURL,
60       "the changemp dialog should not have been opened when the os auth dialog is canceled"
61     );
62     ok(
63       !checkbox.checked,
64       "primary password checkbox should be unchecked after canceling os auth dialog"
65     );
66     ok(button.disabled, "button should be disabled after canceling os auth");
67   }
69   primaryPasswordNextState = true;
70   if (OSKeyStoreTestUtils.canTestOSKeyStoreLogin() && OSKeyStore.canReauth()) {
71     let osAuthDialogShown = OSKeyStoreTestUtils.waitForOSKeyStoreLogin(true);
72     checkbox.click();
73     info("waiting for os auth dialog to appear");
74     await osAuthDialogShown;
75     info("waiting for dialogURL to get set");
76     await TestUtils.waitForCondition(
77       () => dialogURL,
78       "wait for open to get called asynchronously"
79     );
80     is(
81       dialogURL,
82       "chrome://mozapps/content/preferences/changemp.xhtml",
83       "clicking on the checkbox should open the primary password dialog"
84     );
85   } else {
86     primaryPasswordSet = true;
87     doc.defaultView.gPrivacyPane._initMasterPasswordUI();
88     await TestUtils.waitForCondition(
89       () => !button.disabled,
90       "waiting for primary password button to get enabled"
91     );
92   }
93   ok(!button.disabled, "primary password button should now be enabled");
94   ok(checkbox.checked, "primary password checkbox should be checked now");
96   dialogURL = "";
97   button.doCommand();
98   await TestUtils.waitForCondition(
99     () => dialogURL,
100     "wait for open to get called asynchronously"
101   );
102   is(
103     dialogURL,
104     "chrome://mozapps/content/preferences/changemp.xhtml",
105     "clicking on the button should open the primary password dialog"
106   );
107   ok(!button.disabled, "primary password button should still be enabled");
108   ok(checkbox.checked, "primary password checkbox should be checked still");
110   // Confirm that we won't automatically respond to the dialog,
111   // since we don't expect a dialog here, we want the test to fail if one appears.
112   is(
113     Services.prefs.getStringPref(
114       "toolkit.osKeyStore.unofficialBuildOnlyLogin",
115       ""
116     ),
117     "",
118     "Pref should be set to an empty string"
119   );
121   primaryPasswordNextState = false;
122   dialogURL = "";
123   checkbox.click();
124   is(
125     dialogURL,
126     "chrome://mozapps/content/preferences/removemp.xhtml",
127     "clicking on the checkbox to uncheck primary password should show the removal dialog"
128   );
129   ok(button.disabled, "primary password button should now be disabled");
130   ok(!checkbox.checked, "primary password checkbox should now be unchecked");
132   BrowserTestUtils.removeTab(gBrowser.selectedTab);