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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const TESTCASES = [
{
description: "Clear populated address form with text inputs",
document: `<form>
<input id="given-name">
<input id="family-name">
<input id="street-addr">
<input id="city">
</form>`,
focusedInputId: "given-name",
profileData: {
"given-name": "John",
"family-name": "Doe",
"street-addr": "1000 Main Street",
city: "Nowhere",
},
expectedResult: {
"given-name": "",
"family-name": "",
"street-addr": "",
city: "",
},
},
{
description: "Clear populated address form with select and text inputs",
document: `<form>
<input id="given-name">
<input id="family-name">
<input id="street-addr">
<select id="state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="OH">Ohio</option>
</select>
</form>`,
focusedInputId: "given-name",
profileData: {
"given-name": "John",
"family-name": "Doe",
"street-addr": "1000 Main Street",
state: "OH",
},
expectedResult: {
"given-name": "",
"family-name": "",
"street-addr": "",
state: "AL",
},
},
{
description:
"Clear populated address form with select element with selected attribute and text inputs",
document: `<form>
<input id="given-name">
<input id="family-name">
<input id="street-addr">
<select id="state">
<option value="AL">Alabama</option>
<option selected value="AK">Alaska</option>
<option value="OH">Ohio</option>
</select>
</form>`,
focusedInputId: "given-name",
profileData: {
"given-name": "John",
"family-name": "Doe",
"street-addr": "1000 Main Street",
state: "OH",
},
expectedResult: {
"given-name": "",
"family-name": "",
"street-addr": "",
state: "AK",
},
},
];
add_task(async function do_test() {
let { FormAutofillHandler } = ChromeUtils.importESModule(
"resource://gre/modules/shared/FormAutofillHandler.sys.mjs"
);
for (let test of TESTCASES) {
info("Test case: " + test.description);
let testDoc = MockDocument.createTestDocument(
"http://localhost:8080/test",
test.document
);
let form = testDoc.querySelector("form");
let formLike = FormLikeFactory.createFromForm(form);
let handler = new FormAutofillHandler(formLike);
handler.collectFormFields();
let focusedInput = testDoc.getElementById(test.focusedInputId);
handler.focusedInput = focusedInput;
let [adaptedProfile] = handler.activeSection.getAdaptedProfiles([
test.profileData,
]);
await handler.autofillFormFields(adaptedProfile, focusedInput);
handler.activeSection.clearPopulatedForm();
handler.activeSection.fieldDetails.forEach(detail => {
let element = detail.elementWeakRef.get();
let id = element.id;
Assert.equal(
element.value,
test.expectedResult[id],
`Check the ${id} field was restored to the correct value`
);
});
}
});
|