<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test that logins with non-exact match origin appear in autocomplete dropdown</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" 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>
Login Manager test: logins with non-exact match origin appear in autocomplete dropdown
<p id="display"></p>

<!-- we presumably can't hide the content for this test. -->
<div id="content"></div>
<pre id="test">
<script class="testbody" type="text/javascript">
const setupScript = runInParent(function setup() {
  addMessageListener("getDateString", () => {
    const dateAndTimeFormatter = new Services.intl.DateTimeFormat(undefined, { dateStyle: "medium" });
    return dateAndTimeFormatter.format(new Date());
  });
});

add_setup(async () => {
  const origin = window.location.origin;
  const lastDot = origin.lastIndexOf(".");
  const suffix = origin.slice(lastDot);

  const baseHost = "http://example" + suffix;
  const baseSecureHost = "https://example" + suffix;
  const oldHost = "http://old.example" + suffix;
  const oldSecureHost = "https://old.example" + suffix;
  const newHost = "https://new.example" + suffix;

  await setStoredLoginsAsync(
    // The first two logins should never be visible on https: versions of
    // *.example.com since the login is for http: and an https: login exists for this username.
    [oldHost, oldSecureHost, null, "dsdu1", "dsdp1new", "uname", "pword"],
    [baseHost, baseSecureHost, null, "dsdu1", "dsdp1", "uname", "pword"],
    [oldSecureHost, oldSecureHost, null, "dsdu1", "dsdp1", "uname", "pword"],
    [baseSecureHost, baseSecureHost, null, "dsdu1", "dsdp1", "uname", "pword"],
    [newHost, newHost, null, "dsdu1", "dsdp1prime", "uname", "pword"]
  );

  await SpecialPowers.pushPrefEnv({
    set: [
      ["signon.includeOtherSubdomainsInLookup", true],
    ],
  });
  listenForUnexpectedPopupShown();
});

add_task(async function test_form1_initial_empty() {
  const form = createLoginForm({
    action: "https://otherexample.com/formtest.js"
  });

  // Make sure initial form is empty.
  checkLoginForm(form.uname, "", form.pword, "");
  const popupState = await getPopupState();
  is(popupState.open, false, "Check popup is initially closed");
});

/* For this testcase, there exists two logins for this origin
 * on different subdomains but with different passwords. Both logins
 * should appear in the autocomplete popup.
 */
add_task(async function test_form1_menu_shows_two_logins_same_usernames_for_different_subdomain() {
  const form = createLoginForm({
    action: "https://otherexample.com/formtest.js"
  });

  // Trigger autocomplete popup
  form.uname.focus();

  const autocompleteItems = await popupByArrowDown();

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

  // The logins are added "today" and since they are duplicates, the date that they were last
  // changed will be appended.
  const dateString = await setupScript.sendQuery("getDateString");
  const username = `dsdu1 (${dateString})`;

  checkAutoCompleteResults(autocompleteItems, [username, username], window.location.host, "Check all menuitems are displayed correctly.");

  synthesizeKey("KEY_ArrowDown"); // first item
  checkLoginForm(form.uname, "", form.pword, ""); // value shouldn't update just by selecting

  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  is(form.pword.value, "dsdp1", "password should match the login that was selected");
  checkLoginForm(form.uname, "dsdu1", form.pword, "dsdp1");

  form.uname.value = "";
  form.pword.value = "";
  form.uname.focus();

  await popupByArrowDown();

  synthesizeKey("KEY_ArrowDown"); // first item
  synthesizeKey("KEY_ArrowDown"); // second item
  checkLoginForm(form.uname, "", form.pword, ""); // value shouldn't update just by selecting

  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  is(form.pword.value, "dsdp1prime", "Password should match the login that was selected");
  checkLoginForm(form.uname, "dsdu1", form.pword, "dsdp1prime");
});
</script>
</pre>
</body>
</html>
