<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test password field autocomplete footer with and without logins</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"></div>

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

/** Test for Login Manager: Test password field autocomplete footer with and without logins **/

add_task(async function test_no_autofill() {
  await setStoredLoginsAsync(
    [location.origin, "", null, "user-1", "pass-1", "uname", "pword"],
    [location.origin, "", null, "user-2", "pass-2", "uname", "pword"]
  );
  const form = createLoginForm();
  await promiseFormsProcessedInSameProcess();

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

add_task(async function test_two_logins() {
  await setStoredLoginsAsync(
    [location.origin, "", null, "user-1", "pass-1", "uname", "pword"],
    [location.origin, "", null, "user-2", "pass-2", "uname", "pword"]
  );
  const form = createLoginForm();
  await promiseFormsProcessedInSameProcess();

  await popupBy(() => form.uname.focus());

  // popup on the password field should open upon focus
  let results = await popupBy(() => synthesizeKey("KEY_Tab"));

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

  let expectedMenuItems = [
    "user-1",
    "user-2",
  ];
  checkAutoCompleteResults(results, expectedMenuItems, window.location.host, "Check all menuitems are displayed correctly.");

  checkLoginForm(form.uname, "", form.pword, "");
});

add_task(async function test_zero_logins() {
  // no logins stored
  await setStoredLoginsAsync();
  const form = createLoginForm();
  await promiseFormsProcessedInSameProcess();

  form.uname.focus();

  let shownPromise = popupBy().then(() => ok(false, "Should not have shown"));
  // Popup on the password field should NOT automatically open upon focus when there are no saved logins.
  synthesizeKey("KEY_Tab"); // focus the password field
  SimpleTest.requestFlakyTimeout("Giving a chance for the unexpected popup to show");
  let autocompleteItems = await Promise.race([
    shownPromise,
    new Promise(resolve => setTimeout(resolve, 2000)), // Wait 2s for the popup to appear
  ]);

  let popupState = await getPopupState();
  is(popupState.open, false, "Check popup is still closed");

  checkLoginForm(form.uname, "", form.pword, "");
  info("arrow down should still open the popup");
  autocompleteItems = await popupByArrowDown();
  checkAutoCompleteResults(autocompleteItems, [], window.location.host, "Check only footer is displayed.");
  checkLoginForm(form.uname, "", form.pword, "");
});
</script>
</pre>
</body>
</html>
