toolkit/components/passwordmgr/test/mochitest/test_autofocus_js.html
author Masatoshi Kimura <VYV03354@nifty.ne.jp>
Fri, 22 May 2020 21:46:25 +0000
changeset 531728 a845717e4d10ad7cb65ca64b08666c373db6e1de
parent 519297 b3503cdeb434acb64d893b73f388d71cae19e4ed
child 531733 d5c77659d412d28268519c1add754776fa566c1c
permissions -rw-r--r--
Bug 1482279 - Stop using Cu.forcePermissiveCOWs() in SpecialPowers. r=kmag Differential Revision: https://phabricator.services.mozilla.com/D74641

<!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>
<script>

SpecialPowers.pushPrefEnv({"set": [["security.allow_eval_with_system_principal",
                                    true]]});

const chromeScript = runChecksAfterCommonInit(false);

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

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

  let login0 = new nsLoginInfo("https://example.com", "https://example.com", null,
                               "name", "pass");

  let login1 = new nsLoginInfo("https://example.com", "https://example.com", null,
                               "name1", "pass1");

  try {
    Services.logins.addLogin(login0);
    Services.logins.addLogin(login1);
  } catch (e) {
    assert.ok(false, "addLogin threw: " + e);
  }
});
</script>
<p id="display"></p>

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

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

let iframe = document.getElementsByTagName("iframe")[0];
let iframeDoc, hostname;

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

  await promiseFormsProcessed();

  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 promiseFormsProcessed();
  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";
  });
  let shownPromise = promiseACShown();
  await SpecialPowers.spawn(getIframeBrowsingContext(window), [], function() {
    this.content.document.getElementById("form-basic-username").focus();
  });
  await shownPromise;
  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>