<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test login autocomplete with related realms</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" href="/tests/SimpleTest/test.css"/>
</head>
<body>
Login Manager test: related realms autocomplete
<p id="display"></p>
<div id="content"></div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Login Manager: related realms autocomplete. **/

function sendFakeAutocompleteEvent(element) {
  var acEvent = document.createEvent("HTMLEvents");
  acEvent.initEvent("DOMAutoComplete", true, false);
  element.dispatchEvent(acEvent);
}

add_setup(async () => {
  await setStoredLoginsAsync(
    // Simple related domain relationship where example.com and other-example.com are in the related domains list
    ["https://other-example.com", "https://other-example.com", null, "relatedUser1", "relatedPass1", "uname", "pword"],

    // Example.com and example.co.uk are related, so sub.example.co.uk should appear on example.com's autocomplete dropdown
    // The intent is to cover the ebay.com/ebay.co.uk and all other country TLD cases
    // where the sign in page is actually signin.ebay.com/signin.ebay.co.uk but credentials could have manually been entered
    // for ebay.com/ebay.co.uk or automatically stored as signin.ebay.com/sigin.ebay.co.uk
    ["https://sub.example.co.uk", "https://sub.example.co.uk", null, "subUser1", "subPass1", "uname", "pword"],
  );
  listenForUnexpectedPopupShown();
});

add_task(async function test_form1_initial_empty() {
  const form = createLoginForm();
  await promiseFormsProcessedInSameProcess();

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

add_task(async function test_form_related_domain_menuitems() {
  const form = createLoginForm();
  await promiseFormsProcessedInSameProcess();

  form.uname.focus();

  const autocompleteItems = await popupByArrowDown();
  const popupState = await getPopupState();

  is(popupState.selectedIndex, -1, "Check no entires are selected upon opening");

  const expectedMenuItems = ["relatedUser1", "subUser1"];
  checkAutoCompleteResults(autocompleteItems, expectedMenuItems, window.location.host, "Check all menuitems are displayed correctly");

  const acEvents = await getTelemetryEvents({ process: "parent", filterProps: TelemetryFilterPropsAC, clear: true });
  is(acEvents.length, 1, "One autocomplete event");
  checkACTelemetryEvent(acEvents[0], form.uname, {
    "hadPrevious": "0",
    "login": expectedMenuItems.length + "",
    "loginsFooter": "1"
  });
  checkLoginForm(form.uname, "", form.pword, ""); // value shouldn't update just by opening

  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, "relatedPass1", "password should match the login that was selected");
  checkLoginForm(form.uname, "relatedUser1", form.pword, "relatedPass1");

  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, "subPass1", "password should match the login that was selected");
  checkLoginForm(form.uname, "subUser1", form.pword, "subPass1");
});
</script>
</pre>
</body>
</html>
