summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_formless_autofill.html
blob: 3422fa893a792e51faefa0d3ff211675e962daf8 (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
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test autofilling of fields outside of a form</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="pwmgr_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="application/javascript">
document.addEventListener("DOMContentLoaded", () => {
  document.getElementById("loginFrame").addEventListener("load", (evt) => {
    // Tell the parent to setup test logins.
    PWMGR_COMMON_PARENT.sendAsyncMessage("setupParent", { selfFilling: true, testDependsOnDeprecatedLogin: true });
  });
});

let doneSetupPromise = new Promise(resolve => {
  // When the setup is done, load a recipe for this test.
  PWMGR_COMMON_PARENT.addMessageListener("doneSetup", function doneSetup() {
    resolve();
  });
});

add_setup(async () => {
  info("Waiting for loads and setup");
  await doneSetupPromise;

  await loadRecipes({
    siteRecipes: [{
      hosts: ["mochi.test:8888"],
      usernameSelector: "input[name='recipeuname']",
      passwordSelector: "input[name='recipepword']",
    }],
  });
});


const DEFAULT_ORIGIN = "http://mochi.test:8888";
const TESTCASES = [
  {
    // Inputs
    document: `<input type=password>`,

    // Expected outputs
    expectedInputValues: ["testpass"],
  },
  {
    document: `<input>
      <input type=password>`,
    expectedInputValues: ["testuser", "testpass"],
  },
  {
    document: `<input>
      <input type=password>
      <input type=password>`,
    expectedInputValues: ["testuser", "testpass", ""],
  },
  {
    document: `<input>
      <input type=password>
      <input type=password>
      <input type=password>`,
    expectedInputValues: ["testuser", "testpass", "", ""],
  },
  {
    document: `<input>
      <input type=password form="form1">
      <input type=password>
      <form id="form1">
        <input>
        <input type=password>
      </form>`,
    expectedFormCount: 2,
    expectedInputValues: ["testuser", "testpass", "testpass", "", ""],
  },
  {
    document: `<!-- formless password field selector recipe test -->
      <input>
      <input type=password>
      <input>
      <input type=password name="recipepword">`,
    expectedInputValues: ["", "", "testuser", "testpass"],
  },
  {
    document: `<!-- formless username and password field selector recipe test -->
      <input name="recipeuname">
      <input>
      <input type=password>
      <input type=password name="recipepword">`,
    expectedInputValues: ["testuser", "", "", "testpass"],
  },
  {
    document: `<!-- form and formless recipe field selector test -->
      <input name="recipeuname">
      <input>
      <input type=password form="form1"> <!-- not filled since recipe affects both FormLikes -->
      <input type=password>
      <input type=password name="recipepword">
      <form id="form1">
        <input>
        <input type=password>
      </form>`,
    expectedFormCount: 2,
    expectedInputValues: ["testuser", "", "", "", "testpass", "", ""],
  },
];

add_task(async function test() {
  let loginFrame = document.getElementById("loginFrame");
  let frameDoc = loginFrame.contentWindow.document;

  for (let tc of TESTCASES) {
    info("Starting testcase: " + JSON.stringify(tc));

    let numFormLikesExpected = tc.expectedFormCount || 1;

    let processedFormPromise = promiseFormsProcessedInSameProcess(numFormLikesExpected);

    // eslint-disable-next-line no-unsanitized/property
    frameDoc.documentElement.innerHTML = tc.document;
    info("waiting for " + numFormLikesExpected + " processed form(s)");
    await processedFormPromise;

    let testInputs = frameDoc.documentElement.querySelectorAll("input");
    is(testInputs.length, tc.expectedInputValues.length, "Check number of inputs");
    for (let i = 0; i < tc.expectedInputValues.length; i++) {
      let expectedValue = tc.expectedInputValues[i];
      is(testInputs[i].value, expectedValue, `Check expected input value ${i} : ${expectedValue}`);
    }
  }
});

</script>

<p id="display"></p>

<div id="content">
  <iframe id="loginFrame" src="http://mochi.test:8888/tests/toolkit/components/passwordmgr/test/blank.html"></iframe>
</div>
<pre id="test"></pre>
</body>
</html>