summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_autofill_tab_between_fields.html
blob: 53eb959d7ba4cd9f2be602890eac147bbb65e8d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test autocomplete behavior when tabbing between form fields</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="pwmgr_common.js"></script>
  <script type="text/javascript" src="../../../satchel/test/satchel_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script>
let nsLoginInfo = SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/login-manager/loginInfo;1",
                                                                           SpecialPowers.Ci.nsILoginInfo,
                                                                           "init");
let readyPromise = registerRunTests();
</script>
<p id="display"></p>

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

<pre id="test">
<script class="testbody" type="text/javascript">
let DEFAULT_ORIGIN = window.location.origin;
let win;
let html = `
  <form id="form1" action="https://autofill" onsubmit="return false;">
    <input type="text" name="uname">
    <input type="password" name="pword">
    <button type="submit">Submit</button>
  </form>`;

async function prepareLogins(logins = []) {
  await LoginManager.removeAllUserFacingLogins();

  for (let login of logins) {
    let storageAddPromise = promiseStorageChanged(["addLogin"]);
    await LoginManager.addLogin(login);
    await storageAddPromise;
  }
  let count = (await LoginManager.getAllLogins()).length;
  is(count, logins.length, "All logins were added");
}

const availableLogins = {
  "exampleUser1": new nsLoginInfo(DEFAULT_ORIGIN, "https://autofill", null,
                              "user1", "pass1", "uname", "pword"),
}

async function recreateTreeInWindow(formNum) {
  await SpecialPowers.spawn(win, [formNum], (formNumF) => {
    // eslint-disable-next-line no-unsanitized/property
    let form = this.content.document.querySelector(`#form${formNumF}`);
    // eslint-disable-next-line no-unsanitized/property, no-self-assign
    form.outerHTML = form.outerHTML;
  });
}

const tests = [
  {
    name: "autofill_disabled_exact_username",
    autofillEnabled: false,
    logins: ["exampleUser1"],
    expectedAutofillUsername: "",
    expectedAutofillPassword: "",
    typeUsername: "user1",
    expectedTabbedUsername: "user1",
    expectedTabbedPassword: "",
  },
  {
    name: "autofill_enabled_exact_username",
    autofillEnabled: true,
    logins: ["exampleUser1"],
    expectedAutofillUsername: "user1",
    expectedAutofillPassword: "pass1",
    typeUsername: "user1",
    expectedTabbedUsername: "user1",
    expectedTabbedPassword: "pass1",
  },
];

add_setup(async () => {
  ok(readyPromise, "check promise is available");
  await readyPromise;
  win = window.open("about:blank");
  SimpleTest.registerCleanupFunction(() => win.close());
  await loadFormIntoWindow(DEFAULT_ORIGIN, html, win);
});

async function testResultOfTabInteractions(testData) {
  await SpecialPowers.pushPrefEnv({"set": [
    ["signon.autofillForms", testData.autofillEnabled],
  ]});

  await SimpleTest.promiseFocus(win);
  let logins = testData.logins.map(name => availableLogins[name]);
  await prepareLogins(logins);

  info("recreating form");
  let processed = promiseFormsProcessed();
  await recreateTreeInWindow(1);
  info("waiting for form processed");
  await processed;
  // check autofill results
  await checkLoginFormInFrameWithElementValues(win, 1, testData.expectedAutofillUsername, testData.expectedAutofillPassword);

  await SpecialPowers.spawn(win, [testData.typeUsername], async (typeUsername) => {
    let doc = this.content.document;
    let pword = doc.querySelector("[name='pword']");
    let uname = doc.querySelector("[name='uname']");

    pword.setUserInput("");
    uname.setUserInput("");

    info("Placing focus in the username field");
    uname.focus();

    if (typeUsername) {
      info("Filling username field");
      EventUtils.sendString(typeUsername, this.content);
    }

    EventUtils.synthesizeKey("KEY_Tab", {}, this.content); // blur un, focus pw
    await new Promise(resolve => SpecialPowers.executeSoon(resolve));

    ok(pword.matches("input:focus"), "pword field is focused");
  });

  await checkLoginFormInFrameWithElementValues(win, 1, testData.expectedTabbedUsername, testData.expectedTabbedPassword);

  await recreateTreeInWindow(1);
  await promiseFormsProcessed();

  await SpecialPowers.spawn(win, [], () => {
    EventUtils.synthesizeKey("KEY_Escape", {}, this.content);
  });
}

for (let testData of tests) {
  let tmp = {
    async [testData.name]() {
      await testResultOfTabInteractions(testData);
    },
  };
  add_task(tmp[testData.name]);
}

</script>
</pre>
</body>
</html>