toolkit/components/passwordmgr/test/mochitest/test_basic_form_autocomplete_formSubmitURL.html
author Andreea Pavel <apavel@mozilla.com>
Thu, 11 Apr 2019 15:16:03 +0300
changeset 468987 5d5e4df2976a4649e0a717f75c5f311ea1385046
parent 468986 03ed53e600d458cef860664cbee5d5fdaf8e33f3
child 469279 cbae72da0dc4b0ab70cb29dbec66b460c83c9d7f
permissions -rw-r--r--
Backed out changeset 03ed53e600d4 (bug 1540828) for failing eslint on a CLOSED TREE

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test that logins with non-matching formSubmitURL 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="/tests/SimpleTest/AddTask.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-matching formSubmitURL appear in autocomplete dropdown

<script>
var chromeScript = runChecksAfterCommonInit();

runInParent(function setup() {
  const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

  // Create some logins just for this form, since we'll be deleting them.
  var nsLoginInfo = Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
                                           Ci.nsILoginInfo, "init");
  assert.ok(nsLoginInfo != null, "nsLoginInfo constructor");

  var login1 = new nsLoginInfo("https://example.com", "https://differentFormSubmitURL", null,
                               "dfsu1", "dfsp1", "uname", "pword");

  Services.logins.addLogin(login1);
});
</script>
<p id="display"></p>

<!-- we presumably can't hide the content for this test. -->
<div id="content">

  <!-- form1 tests multiple matching logins -->
  <form id="form1" action="https://autocomplete:8888/formtest.js" onsubmit="return false;">
    <input  type="text"       name="uname">
    <input  type="password"   name="pword">
    <button type="submit">Submit</button>
  </form>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Login Manager: multiple login autocomplete. **/

var uname = $_(1, "uname");
var pword = $_(1, "pword");

// Restore the form to the default state.
function restoreForm() {
  uname.value = "";
  pword.value = "";
  uname.focus();
}

// Check for expected username/password in form.
function checkACForm(expectedUsername, expectedPassword) {
  var formID = uname.parentNode.id;
  is(uname.value, expectedUsername, "Checking " + formID + " username is: " + expectedUsername);
  is(pword.value, expectedPassword, "Checking " + formID + " password is: " + expectedPassword);
}

function spinEventLoop() {
  return Promise.resolve();
}

add_task(async function setup() {
  listenForUnexpectedPopupShown();
});

add_task(async function test_form1_initial_empty() {
  await SimpleTest.promiseFocus(window);

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

/* For this testcase, the only login that exists for this origin
 * is one with a different formSubmitURL, so the login will appear
 * in the autocomplete popup.
 */
add_task(async function test_form1_menu_shows_logins_for_different_formSubmitURL() {
  await SimpleTest.promiseFocus(window);
  // Trigger autocomplete popup
  restoreForm();
  let shownPromise = promiseACShown();
  synthesizeKey("KEY_ArrowDown"); // open
  let results = await shownPromise;

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

  let expectedMenuItems = ["dfsu1"];
  checkAutoCompleteResults(results, expectedMenuItems, "example.com", "Check all menuitems are displayed correctly.");

  synthesizeKey("KEY_ArrowDown"); // first item
  checkACForm("", ""); // value shouldn't update just by selecting

  synthesizeKey("KEY_Enter");
  await promiseFormsProcessed();
  checkACForm("dfsu1", "dfsp1");
});
</script>
</pre>
</body>
</html>