<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test autofill for forms with 1 password field</title>
  <script src="/tests/SimpleTest/SimpleTest.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: forms with 1 password field
<script>
gTestDependsOnDeprecatedLogin = true;
runChecksAfterCommonInit(() => startTest());

let DEFAULT_ORIGIN = window.location.origin;
</script>
<p id="display"></p>

<div id="content" style="display: none">
</div>

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

/** Test for Login Manager: simple form fill **/

async function startTest() {
  let win = window.open("about:blank");
  SimpleTest.registerCleanupFunction(() => win.close());
  await loadFormIntoWindow(DEFAULT_ORIGIN, `
    <!-- no username fields -->

    <form id='form1' action='formtest.js'> 1
        <!-- Blank, so fill in the password -->
        <input type='password' name='pname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form2' action='formtest.js'> 2
        <!-- Already contains the password, so nothing to do. -->
        <input type='password' name='pname' value='testpass'>
        <button type='submit'>Submit</button>
    </form>

    <form id='form3' action='formtest.js'> 3
        <!-- Contains unknown password, so don't change it -->
        <input type='password' name='pname' value='xxxxxxxx'>
        <button type='submit'>Submit</button>
    </form>


    <!-- username fields -->

    <form id='form4' action='formtest.js'> 4
        <!-- Blanks, so fill in login -->
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form5' action='formtest.js'> 5
        <!-- Username already set, so fill in password -->
        <input type='text'     name='uname' value='testuser'>
        <input type='password' name='pname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form6' action='formtest.js'> 6
        <!-- Unknown username, so don't fill in password -->
        <input type='text'     name='uname' value='xxxxxxxx'>
        <input type='password' name='pname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form7' action='formtest.js'> 7
        <!-- Password already set, could fill in username but that's weird so we don't -->
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value='testpass'>
        <button type='submit'>Submit</button>
    </form>

    <form id='form8' action='formtest.js'> 8
        <!-- Unknown password, so don't fill in a username -->
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value='xxxxxxxx'>
        <button type='submit'>Submit</button>
    </form>



    <!-- extra text fields -->

    <form id='form9' action='formtest.js'> 9
        <!-- text field _after_ password should never be treated as a username field -->
        <input type='password' name='pname' value=''>
        <input type='text'     name='uname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form10' action='formtest.js'> 10
        <!-- only the first text field before the password should be for username -->
        <input type='text'     name='other' value=''>
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form11' action='formtest.js'> 11
        <!-- variation just to make sure extra text field is still ignored -->
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value=''>
        <input type='text'     name='other' value=''>
        <button type='submit'>Submit</button>
    </form>



    <!-- same as last bunch, but with xxxx in the extra field. -->

    <form id='form12' action='formtest.js'> 12
        <!-- text field _after_ password should never be treated as a username field -->
        <input type='password' name='pname' value=''>
        <input type='text'     name='uname' value='xxxxxxxx'>
        <button type='submit'>Submit</button>
    </form>

    <form id='form13' action='formtest.js'> 13
        <!-- only the first text field before the password should be for username -->
        <input type='text'     name='other' value='xxxxxxxx'>
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form14' action='formtest.js'> 14
        <!-- variation just to make sure extra text field is still ignored -->
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value=''>
        <input type='text'     name='other' value='xxxxxxxx'>
        <button type='submit'>Submit</button>
    </form>`, win, 14);

  var f = 1;

  // 1-3
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass");
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass");
  await checkLoginFormInFrameWithElementValues(win, f++, "xxxxxxxx");

  // 4-8
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass");
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass");
  await checkLoginFormInFrameWithElementValues(win, f++, "xxxxxxxx", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "", "testpass");
  await checkLoginFormInFrameWithElementValues(win, f++, "", "xxxxxxxx");

  // 9-14
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "", "testuser", "testpass");
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass", "xxxxxxxx");
  await checkLoginFormInFrameWithElementValues(win, f++, "xxxxxxxx", "testuser", "testpass");
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass", "xxxxxxxx");

  SimpleTest.finish();
}
</script>
</pre>
</body>
</html>
