<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test form field autocomplete 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="../../../satchel/test/satchel_common.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>

<!-- we presumably can't hide the content for this test. -->
<div id="content">
  <iframe id="sandboxed"
          sandbox=""
          src="form_basic.html"></iframe>
</div>

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

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

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

add_setup(async () => {
  await setStoredLoginsAsync(
    [window.location.origin, "", null, "tempuser1", "temppass1", "uname", "pword"]
  );
  let frameWindow = SpecialPowers.wrap(sandboxed).contentWindow;
  // Can't use SimpleTest.promiseFocus as it doesn't work with the sandbox.
  await SimpleTest.promiseWaitForCondition(() => {
    return frameWindow.document.readyState == "complete" && frameWindow.location.href != "about:blank";
  }, "Check frame is loaded");
  let frameDoc = SpecialPowers.wrap(sandboxed).contentDocument;
  uname = frameDoc.getElementById("form-basic-username");
  pword = frameDoc.getElementById("form-basic-password");
  hostname = frameDoc.documentURIObject.host;
});

add_task(async function test_no_autofill() {
  // Make sure initial form is empty as autofill shouldn't happen in the sandboxed frame.
  checkLoginForm(uname, "", pword, "");
  let popupState = await getPopupState();
  is(popupState.open, false, "Check popup is initially closed");
});

add_task(async function test_autocomplete_warning_no_logins() {
  const { items } = await openPopupOn(pword);

  let popupState = await getPopupState();
  is(popupState.selectedIndex, -1, "Check no entries are selected upon opening");

  let expectedMenuItems = [
    "This connection is not secure. Logins entered here could be compromised. Learn More",
  ];
  checkAutoCompleteResults(items, expectedMenuItems, hostname, "Check all menuitems are displayed correctly.");

  checkLoginForm(uname, "", pword, "");
});
</script>
</pre>
</body>
</html>
