summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_autocomplete_generated_password_private_window.js
blob: f28a7a1d526c49a2aae740c815574108d14e5b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// The origin for the test URIs.
const TEST_ORIGIN = "https://example.com";
const FORM_PAGE_PATH =
  "/browser/toolkit/components/passwordmgr/test/browser/form_basic.html";
const passwordInputSelector = "#form-basic-password";

add_setup(async function () {
  Services.telemetry.clearEvents();
  TelemetryTestUtils.assertEvents([], {
    category: "pwmgr",
    method: "autocomplete_shown",
  });
});

add_task(async function test_autocomplete_new_password_popup_item_visible() {
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: TEST_ORIGIN,
    },
    async function (browser) {
      info("Generate and cache a password for a non-private context");
      let lmp =
        browser.browsingContext.currentWindowGlobal.getActor("LoginManager");
      await lmp.getGeneratedPassword();
      Assert.equal(
        LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin().size,
        1,
        "Non-Private password should be cached"
      );
    }
  );

  await LoginTestUtils.addLogin({ username: "username", password: "pass1" });
  const win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
  const doc = win.document;
  await BrowserTestUtils.withNewTab(
    {
      gBrowser: win.gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser.ownerGlobal);
      await ContentTask.spawn(
        browser,
        [passwordInputSelector],
        function openAutocomplete(sel) {
          content.document.querySelector(sel).autocomplete = "new-password";
        }
      );

      let popup = doc.getElementById("PopupAutoComplete");
      Assert.ok(popup, "Got popup");
      await openACPopup(popup, browser, passwordInputSelector);

      let item = popup.querySelector(`[originaltype="generatedPassword"]`);
      Assert.ok(item, "Should get 'Generate password' richlistitem");

      let onPopupClosed = BrowserTestUtils.waitForCondition(
        () => !popup.popupOpen,
        "Popup should get closed"
      );

      await TestUtils.waitForTick();

      TelemetryTestUtils.assertEvents(
        [["pwmgr", "autocomplete_shown", "generatedpassword"]],
        { category: "pwmgr", method: "autocomplete_shown" }
      );

      await closePopup(popup);
      await onPopupClosed;
    }
  );

  let lastPBContextExitedPromise = TestUtils.topicObserved(
    "last-pb-context-exited"
  ).then(() => TestUtils.waitForTick());
  await BrowserTestUtils.closeWindow(win);
  await lastPBContextExitedPromise;
  Assert.equal(
    LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin().size,
    1,
    "Only private-context passwords should be cleared"
  );
});

add_task(async function test_autocomplete_menu_item_enabled() {
  const win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
  const doc = win.document;
  await BrowserTestUtils.withNewTab(
    {
      gBrowser: win.gBrowser,
      url: TEST_ORIGIN + FORM_PAGE_PATH,
    },
    async function (browser) {
      await SimpleTest.promiseFocus(browser);
      await openPasswordContextMenu(browser, passwordInputSelector);
      let generatedPasswordItem = doc.getElementById(
        "fill-login-generated-password"
      );
      Assert.equal(
        generatedPasswordItem.disabled,
        false,
        "Generate password context menu item should be enabled in PB mode"
      );
      await closePopup(document.getElementById("contentAreaContextMenu"));
    }
  );
  await BrowserTestUtils.closeWindow(win);
});