summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_autofill_sandboxed.html
blob: 8fd6debd5cd0660740e5911b407c0bf9fa8dd6ef (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test form field autofill in sandboxed documents (null principal)</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>

  <script type="text/javascript" src="pwmgr_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>

<div id="content">
  <iframe id="sandboxed"
          sandbox=""></iframe>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">

const { TestUtils } = SpecialPowers.ChromeUtils.import(
  "resource://testing-common/TestUtils.jsm"
);

/** Test for Login Manager: form field autofill in sandboxed documents (null principal) **/

const sandboxed = document.getElementById("sandboxed");
let uname;
let pword;

add_setup(async () => {
  await setStoredLoginsAsync(["https://example.com", "", null, "tempuser1", "temppass1", "uname", "pword"]);
});

add_task(async function test_no_autofill_in_form() {
  sandboxed.src = "form_basic.html";
  const frameWindow = SpecialPowers.wrap(sandboxed).contentWindow;
  const DOMFormHasPasswordPromise = new Promise(resolve => {
    SpecialPowers.addChromeEventListener("DOMFormHasPassword", function onDFHP() {
      SpecialPowers.removeChromeEventListener("DOMFormHasPassword", onDFHP);
      resolve();
    });
  });
  // Can't use SimpleTest.promiseFocus as it doesn't work with the sandbox.
  await SimpleTest.promiseWaitForCondition(() => {
    return frameWindow.document.readyState == "complete" &&
             frameWindow.location.href.endsWith("form_basic.html");
  }, "Check frame is loaded");
  info("frame loaded");
  await DOMFormHasPasswordPromise;
  const frameDoc = SpecialPowers.wrap(sandboxed).contentDocument;

  uname = frameDoc.getElementById("form-basic-username");
  pword = frameDoc.getElementById("form-basic-password");

  // Autofill shouldn't happen in the sandboxed frame but would have happened by
  // now since DOMFormHasPassword was observed above.
  await ensureLoginFormStaysFilledWith(uname, "", pword, "");

  info("blurring the username field after typing the username");
  uname.focus();
  uname.setUserInput("tempuser1");
  synthesizeKey("VK_TAB", {}, frameWindow);

  await TestUtils.waitForCondition(() => {
    return uname.value === "tempuser1" & pword.value === "";
  }, "Username and password field should be filled");
});

add_task(async function test_no_autofill_outside_form() {
  sandboxed.src = "formless_basic.html";
  const frameWindow = SpecialPowers.wrap(sandboxed).contentWindow;
  const DOMInputPasswordAddedPromise = new Promise(resolve => {
    SpecialPowers.addChromeEventListener("DOMInputPasswordAdded", function onDIPA() {
      SpecialPowers.removeChromeEventListener("DOMInputPasswordAdded", onDIPA);
      resolve();
    });
  });
  // Can't use SimpleTest.promiseFocus as it doesn't work with the sandbox.
  await SimpleTest.promiseWaitForCondition(() => {
    return frameWindow.document.readyState == "complete" &&
             frameWindow.location.href.endsWith("formless_basic.html");
  }, "Check frame is loaded");
  info("frame loaded");
  await DOMInputPasswordAddedPromise;
  const frameDoc = SpecialPowers.wrap(sandboxed).contentDocument;

  uname = frameDoc.getElementById("form-basic-username");
  pword = frameDoc.getElementById("form-basic-password");

  // Autofill shouldn't happen in the sandboxed frame but would have happened by
  // now since DOMInputPasswordAdded was observed above.
  await ensureLoginFormStaysFilledWith(uname, "", pword, "");
});
</script>
</pre>
</body>
</html>