<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test login autocomplete is activated when focused by js on load</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>

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

<pre id="test">
<script class="testbody" type="text/javascript">
const iframe = document.getElementsByTagName("iframe")[0];
let iframeDoc, hostname;

add_setup(async () => {
  const origin = window.location.origin;
  await setStoredLoginsAsync(
    [origin, origin, null, "name", "pass"],
    [origin, origin, null, "name1", "pass1"]
  );

  const processedPromise = promiseFormsProcessed();
  iframe.src = "/tests/toolkit/components/passwordmgr/test/mochitest/form_autofocus_js.html";
  await new Promise(resolve => {
    iframe.addEventListener("load", function() {
      resolve();
    }, {once: true});
  });

  await processedPromise;

  hostname = await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    return this.content.document.documentURIObject.host;
  });

  SimpleTest.requestFlakyTimeout("Giving a chance for the unexpected popupshown to occur");
});

add_task(async function test_initial_focus() {
  let results = await notifyMenuChanged(3, "name");
  checkAutoCompleteResults(results, ["name", "name1"], hostname, "Two login results");
  synthesizeKey("KEY_ArrowDown");
  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    Assert.equal(this.content.document.getElementById("form-basic-password").value, "pass", "Check first password filled");
  });
  let popupState = await getPopupState();
  is(popupState.open, false, "Check popup is now closed");
});

// This depends on the filling from the previous test.
add_task(async function test_not_reopened_if_filled() {
  listenForUnexpectedPopupShown();
  await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    this.content.document.getElementById("form-basic-username").focus();
  });
  info("Waiting to see if a popupshown occurs");
  await new Promise(resolve => setTimeout(resolve, 1000));

  // cleanup
  gPopupShownExpected = true;
  await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    this.content.document.getElementById("form-basic-submit").focus();
  });
});

add_task(async function test_reopened_after_edit_not_matching_saved() {
  await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    this.content.document.getElementById("form-basic-username").value = "nam";
  });
  await popupBy(async () => {
    await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
      this.content.document.getElementById("form-basic-username").focus();
    });
  });
  await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    this.content.document.getElementById("form-basic-submit").focus();
  });
});

add_task(async function test_not_reopened_after_selecting() {
  await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    this.content.document.getElementById("form-basic-username").value = "";
    this.content.document.getElementById("form-basic-password").value = "";
  });
  listenForUnexpectedPopupShown();

  await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    let formFillController = SpecialPowers.getFormFillController();
    let usernameField = this.content.document.getElementById("form-basic-username");
    formFillController.markAsLoginManagerField(usernameField);
  });

  info("Waiting to see if a popupshown occurs");
  await new Promise(resolve => setTimeout(resolve, 1000));

  // Cleanup
  gPopupShownExpected = true;
});

</script>
</pre>
</body>
</html>
