<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test autofill for forms with 2 password fields</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 2 password fields
<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
        <!-- simple form, fill in first pw -->
        <input type='password' name='pname' value=''>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form2' action='formtest.js'> 2
        <!-- same but reverse pname and qname, field names are ignored. -->
        <input type='password' name='qname' value=''>
        <input type='password' name='pname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form3' action='formtest.js'> 3
        <!-- text field after password fields should be ignored, no username. -->
        <input type='password' name='pname' value=''>
        <input type='password' name='qname' value=''>
        <input type='text'     name='uname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form4' action='formtest.js'> 4
        <!-- nothing to do, password already present -->
        <input type='password' name='pname' value='testpass'>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form5' action='formtest.js'> 5
        <!-- don't clobber an existing unrecognized password -->
        <input type='password' name='pname' value='xxxxxxxx'>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form6' action='formtest.js'> 6
        <!-- fill in first field, 2nd field shouldn't be touched anyway. -->
        <input type='password' name='pname' value=''>
        <input type='password' name='qname' value='xxxxxxxx'>
        <button type='submit'>Submit</button>
    </form>



    <!-- with username fields -->



    <form id='form7' action='formtest.js'> 7
        <!-- simple form, should fill in username and first pw -->
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value=''>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form8' action='formtest.js'> 8
        <!-- reverse pname and qname, field names are ignored. -->
        <input type='text'     name='uname' value=''>
        <input type='password' name='qname' value=''>
        <input type='password' name='pname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form9' action='formtest.js'> 9
        <!-- username already filled, so just fill first password -->
        <input type='text'     name='uname' value='testuser'>
        <input type='password' name='pname' value=''>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>

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

    <form id='form11' action='formtest.js'> 11
        <!-- don't clobber unknown password -->
        <input type='text'     name='uname' value='testuser'>
        <input type='password' name='pname' value='xxxxxxxx'>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form12' action='formtest.js'> 12
        <!-- fill in 1st pass, don't clobber 2nd pass -->
        <input type='text'     name='uname' value='testuser'>
        <input type='password' name='pname' value=''>
        <input type='password' name='qname' value='xxxxxxxx'>
        <button type='submit'>Submit</button>
    </form>

    <form id='form13' action='formtest.js'> 13
        <!-- nothing to do, user and pass prefilled. life is easy. -->
        <input type='text'     name='uname' value='testuser'>
        <input type='password' name='pname' value='testpass'>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form14' action='formtest.js'> 14
        <!-- shouldn't fill in username because 1st pw field is unknown. -->
        <input type='text'     name='uname' value=''>
        <input type='password' name='pname' value='xxxxxxxx'>
        <input type='password' name='qname' value='testpass'>
        <button type='submit'>Submit</button>
    </form>

    <form id='form15' action='formtest.js'> 15
        <!-- textfield in the middle of pw fields should be ignored -->
        <input type='password' name='pname' value=''>
        <input type='text'     name='uname' value=''>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>

    <form id='form16' action='formtest.js'> 16
        <!-- same, and don't clobber existing unknown password -->
        <input type='password' name='pname' value='xxxxxxxx'>
        <input type='text'     name='uname' value=''>
        <input type='password' name='qname' value=''>
        <button type='submit'>Submit</button>
    </form>`, win, 16);

  var f = 1;

  // 1-6 no username
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass", "", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "xxxxxxxx", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass", "xxxxxxxx");

  // 7-15 with username
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "xxxxxxxx", "", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "xxxxxxxx", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass", "xxxxxxxx");
  await checkLoginFormInFrameWithElementValues(win, f++, "testuser", "testpass", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "", "xxxxxxxx", "testpass");
  await checkLoginFormInFrameWithElementValues(win, f++, "testpass", "", "");
  await checkLoginFormInFrameWithElementValues(win, f++, "xxxxxxxx", "", "");

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