summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_DOMInputPasswordAdded_fired_between_DOMContentLoaded_and_load_events.html
blob: 3ab40e92af5d3aa497deb7d9a754fded70a94af1 (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
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test the password manager code called on DOMInputPasswordAdded runs when it occurs between DOMContentLoaded and load events</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="pwmgr_common.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css" />
</head>
<body>
  <!-- In cases where the "DOMContentLoaded" event for a page has occured but not the "load" event when
  "DOMInputPasswordAdded" fires, we want to make sure that the Password Manager code (i.e.
  _fetchLoginsFromParentAndFillForm) still runs on the page.
  This scenario can happen for example when a page has very little initial HTML, but extensive JS that
  adds Custom Elements or other HTML later, or when other subresources, like images, take a while to load.
  In this test, we delay the page load with a delayed response for an image source. -->
<script type="application/javascript">

let DEFAULT_ORIGIN = window.location.origin;
let FILE_PATH = "/tests/toolkit/components/passwordmgr/test/mochitest/slow_image.html";

async function openDocumentInWindow(win) {
  let DOMContentLoadedPromise = new Promise((resolve) => {
    win.addEventListener("DOMContentLoaded", function() {
      resolve();
    }, {once: true});
  });
  win.location = DEFAULT_ORIGIN + FILE_PATH;
  await DOMContentLoadedPromise;
}

add_setup(async () => {
  await setStoredLoginsAsync([DEFAULT_ORIGIN, DEFAULT_ORIGIN, null, "user", "omgsecret!"]);
});

add_task(async function test_password_autofilled() {
  let numLogins = await LoginManager.countLogins(DEFAULT_ORIGIN, DEFAULT_ORIGIN, null);
  is(numLogins, 1, "Correct number of logins");

  let win = window.open("about:blank");
  SimpleTest.registerCleanupFunction(() => win.close());
  await openDocumentInWindow(win);
  let processedPromise = promiseFormsProcessed();
  await SpecialPowers.spawn(win, [], function() {
    let doc = this.content.document;
    info("Adding password input field to the page to trigger DOMInputPasswordAdded");
    let passwordField = doc.createElement("input");
    passwordField.type = "password";
    is(doc.readyState, "interactive", "Make sure 'DOMContentLoaded' has fired but not 'load'");
    doc.body.append(passwordField);
  });
  info("Waiting for the password field to be autofilled");
  await processedPromise;
  let expectedValue = "omgsecret!";
  await SpecialPowers.spawn(win, [expectedValue], expectedValueF => {
    is(this.content.document.querySelector("input[type='password']").value, expectedValueF, "Ensure the password field is autofilled");
  });
});
</script>
</body>
</html>