test_createRecords.js (10553B)
1 /* 2 * Test for the normalization of records created by FormAutofillHandler. 3 */ 4 5 "use strict"; 6 7 const { FormAutofillSection } = ChromeUtils.importESModule( 8 "resource://gre/modules/shared/FormAutofillSection.sys.mjs" 9 ); 10 11 const TESTCASES = [ 12 { 13 description: 14 "Don't contain a field whose length of value is greater than 200", 15 document: `<form> 16 <input id="given-name" autocomplete="given-name"> 17 <input id="organization" autocomplete="organization"> 18 <input id="address-level1" autocomplete="address-level1"> 19 <input id="country" autocomplete="country"> 20 <input id="cc-number" autocomplete="cc-number"> 21 <input id="cc-name" autocomplete="cc-name"> 22 </form>`, 23 formValue: { 24 "given-name": "John", 25 organization: "*".repeat(200), 26 "address-level1": "*".repeat(201), 27 country: "US", 28 "cc-number": "1111222233334444", 29 "cc-name": "*".repeat(201), 30 }, 31 expectedRecord: [ 32 { 33 "given-name": "John", 34 organization: "*".repeat(200), 35 "address-level1": "", 36 country: "US", 37 }, 38 { 39 "cc-number": "1111222233334444", 40 "cc-name": "", 41 }, 42 ], 43 }, 44 { 45 description: "Don't create address record if filled data is less than 3", 46 document: `<form> 47 <input id="given-name" autocomplete="given-name"> 48 <input id="organization" autocomplete="organization"> 49 <input id="country" autocomplete="country"> 50 </form>`, 51 formValue: { 52 "given-name": "John", 53 organization: "Mozilla", 54 }, 55 expectedRecord: [], 56 }, 57 { 58 description: "All name related fields should be counted as 1 field only.", 59 document: `<form> 60 <input id="given-name" autocomplete="given-name"> 61 <input id="family-name" autocomplete="family-name"> 62 <input id="organization" autocomplete="organization"> 63 </form>`, 64 formValue: { 65 "given-name": "John", 66 "family-name": "Doe", 67 organization: "Mozilla", 68 }, 69 expectedRecord: [], 70 }, 71 { 72 description: 73 "All telephone related fields should be counted as 1 field only.", 74 document: `<form> 75 <input id="tel-country-code" autocomplete="tel-country-code"> 76 <input id="tel-area-code" autocomplete="tel-area-code"> 77 <input id="tel-local" autocomplete="tel-local"> 78 <input id="organization" autocomplete="organization"> 79 </form>`, 80 formValue: { 81 "tel-country-code": "+1", 82 "tel-area-code": "123", 83 "tel-local": "4567890", 84 organization: "Mozilla", 85 }, 86 expectedRecord: [], 87 }, 88 { 89 description: 90 "A credit card form with the value of cc-number, cc-exp, cc-name and cc-type.", 91 document: `<form> 92 <input id="cc-number" autocomplete="cc-number"> 93 <input id="cc-name" autocomplete="cc-name"> 94 <input id="cc-exp" autocomplete="cc-exp"> 95 <input id="cc-type" autocomplete="cc-type"> 96 </form>`, 97 formValue: { 98 "cc-number": "5105105105105100", 99 "cc-name": "Foo Bar", 100 "cc-exp": "2022-06", 101 "cc-type": "Visa", 102 }, 103 expectedRecord: [ 104 { 105 "cc-number": "5105105105105100", 106 "cc-name": "Foo Bar", 107 "cc-exp": "2022-06", 108 "cc-type": "Visa", 109 }, 110 ], 111 }, 112 { 113 description: "A credit card form with cc-number value only.", 114 document: `<form> 115 <input id="cc-number" autocomplete="cc-number"> 116 </form>`, 117 formValue: { 118 "cc-number": "4111111111111111", 119 }, 120 expectedRecord: [ 121 { 122 "cc-number": "4111111111111111", 123 }, 124 ], 125 }, 126 { 127 description: "A credit card form must have cc-number value.", 128 document: `<form> 129 <input id="cc-number" autocomplete="cc-number"> 130 <input id="cc-name" autocomplete="cc-name"> 131 <input id="cc-exp" autocomplete="cc-exp"> 132 </form>`, 133 formValue: { 134 "cc-number": "", 135 "cc-name": "Foo Bar", 136 "cc-exp": "2022-06", 137 }, 138 expectedRecord: [], 139 }, 140 { 141 description: "A credit card form must have cc-number field.", 142 document: `<form> 143 <input id="cc-name" autocomplete="cc-name"> 144 <input id="cc-exp" autocomplete="cc-exp"> 145 </form>`, 146 formValue: { 147 "cc-name": "Foo Bar", 148 "cc-exp": "2022-06", 149 }, 150 expectedRecord: [], 151 }, 152 { 153 description: "A form with multiple sections", 154 document: `<form> 155 <input id="given-name" autocomplete="given-name"> 156 <input id="organization" autocomplete="organization"> 157 <input id="country" autocomplete="country"> 158 159 <input id="given-name-shipping" autocomplete="shipping given-name"> 160 <input id="family-name-shipping" autocomplete="shipping family-name"> 161 <input id="organization-shipping" autocomplete="shipping organization"> 162 <input id="country-shipping" autocomplete="shipping country"> 163 164 <input id="given-name-billing" autocomplete="billing given-name"> 165 <input id="organization-billing" autocomplete="billing organization"> 166 <input id="country-billing" autocomplete="billing country"> 167 168 <input id="cc-number-section-one" autocomplete="section-one cc-number"> 169 <input id="cc-name-section-one" autocomplete="section-one cc-name"> 170 171 <input id="cc-number-section-two" autocomplete="section-two cc-number"> 172 <input id="cc-name-section-two" autocomplete="section-two cc-name"> 173 <input id="cc-exp-section-two" autocomplete="section-two cc-exp"> 174 </form>`, 175 formValue: { 176 "given-name": "Bar", 177 organization: "Foo", 178 country: "US", 179 180 "given-name-shipping": "John", 181 "family-name-shipping": "Doe", 182 "organization-shipping": "Mozilla", 183 "country-shipping": "US", 184 185 "given-name-billing": "Foo", 186 "organization-billing": "Bar", 187 "country-billing": "US", 188 189 "cc-number-section-one": "4111111111111111", 190 "cc-name-section-one": "John", 191 192 "cc-number-section-two": "5105105105105100", 193 "cc-name-section-two": "Foo Bar", 194 "cc-exp-section-two": "2026-26", 195 }, 196 expectedRecord: [ 197 { 198 "given-name": "Bar", 199 organization: "Foo", 200 country: "US", 201 }, 202 { 203 "given-name": "John", 204 "family-name": "Doe", 205 organization: "Mozilla", 206 country: "US", 207 }, 208 { 209 "given-name": "Foo", 210 organization: "Bar", 211 country: "US", 212 }, 213 { 214 "cc-number": "4111111111111111", 215 "cc-name": "John", 216 }, 217 { 218 "cc-number": "5105105105105100", 219 "cc-name": "Foo Bar", 220 "cc-exp": "2026-26", 221 }, 222 ], 223 }, 224 { 225 description: "A credit card form with a cc-type select.", 226 document: `<form> 227 <input id="cc-number" autocomplete="cc-number"> 228 <label for="field1">Card Type:</label> 229 <select id="field1"> 230 <option value="visa" selected>Visa</option> 231 </select> 232 </form>`, 233 formValue: { 234 "cc-number": "5105105105105100", 235 }, 236 expectedRecord: [ 237 { 238 "cc-number": "5105105105105100", 239 "cc-type": "visa", 240 }, 241 ], 242 }, 243 { 244 description: "A credit card form with a cc-type select from label.", 245 document: `<form> 246 <input id="cc-number" autocomplete="cc-number"> 247 <label for="cc-type">Card Type:</label> 248 <select id="cc-type"> 249 <option value="V" selected>Visa</option> 250 <option value="A">American Express</option> 251 </select> 252 </form>`, 253 formValue: { 254 "cc-number": "5105105105105100", 255 "cc-type": "A", 256 }, 257 expectedRecord: [ 258 { 259 "cc-number": "5105105105105100", 260 "cc-type": "amex", 261 }, 262 ], 263 }, 264 { 265 description: 266 "A credit card form with separate expiry fields should have normalized expiry data.", 267 document: `<form> 268 <input id="cc-number" autocomplete="cc-number"> 269 <input id="cc-exp-month" autocomplete="cc-exp-month"> 270 <input id="cc-exp-year" autocomplete="cc-exp-year"> 271 </form>`, 272 formValue: { 273 "cc-number": "5105105105105100", 274 "cc-exp-month": "05", 275 "cc-exp-year": "26", 276 }, 277 expectedRecord: [ 278 { 279 "cc-number": "5105105105105100", 280 "cc-exp-month": "05", 281 "cc-exp-year": "26", 282 }, 283 ], 284 }, 285 { 286 description: 287 "A credit card form with combined expiry fields should have normalized expiry data.", 288 document: `<form> 289 <input id="cc-number" autocomplete="cc-number"> 290 <input id="cc-exp" autocomplete="cc-exp"> 291 </form>`, 292 formValue: { 293 "cc-number": "5105105105105100", 294 "cc-exp": "07/27", 295 }, 296 expectedRecord: [ 297 { 298 "cc-number": "5105105105105100", 299 "cc-exp": "07/27", 300 }, 301 ], 302 }, 303 ]; 304 305 for (let testcase of TESTCASES) { 306 add_task(async function () { 307 info("Starting testcase: " + testcase.description); 308 309 const doc = MockDocument.createTestDocument( 310 "http://localhost:8080/test/", 311 testcase.document 312 ); 313 314 for (const id in testcase.formValue) { 315 doc.getElementById(id).value = testcase.formValue[id]; 316 } 317 318 const form = doc.querySelector("form"); 319 const formLike = FormLikeFactory.createFromForm(form); 320 321 // Child process 322 const handler = new FormAutofillHandler(formLike); 323 const fields = FormAutofillHandler.collectFormFieldDetails(handler.form); 324 handler.setIdentifiedFieldDetails(fields); 325 const filledData = handler.collectFormFilledData(); 326 327 // Parent process 328 const sections = FormAutofillSection.classifySections(fields); 329 330 const actualRecords = sections 331 .map(section => section.createRecord(filledData)?.record) 332 .filter(s => !!s); 333 334 const expectedRecords = testcase.expectedRecord; 335 Assert.equal( 336 actualRecords.length, 337 expectedRecords.length, 338 "Check the number of record" 339 ); 340 for (const idx in expectedRecords) { 341 Assert.deepEqual(actualRecords[idx], expectedRecords[idx]); 342 } 343 }); 344 }