OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/autofill/form_structure.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/string_util.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/autofill/autofill_metrics.h" | |
11 #include "components/autofill/common/form_data.h" | |
12 #include "components/autofill/common/form_field_data.h" | |
13 #include "googleurl/src/gurl.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | |
16 | |
17 using WebKit::WebInputElement; | |
18 | |
19 namespace { | |
20 | |
21 // Unlike the base AutofillMetrics, exposes copy and assignment constructors, | |
22 // which are handy for briefer test code. The AutofillMetrics class is | |
23 // stateless, so this is safe. | |
24 class TestAutofillMetrics : public AutofillMetrics { | |
25 public: | |
26 TestAutofillMetrics() {} | |
27 virtual ~TestAutofillMetrics() {} | |
28 }; | |
29 | |
30 } // anonymous namespace | |
31 | |
32 | |
33 namespace content { | |
34 | |
35 std::ostream& operator<<(std::ostream& os, const FormData& form) { | |
36 os << UTF16ToUTF8(form.name) | |
37 << " " | |
38 << UTF16ToUTF8(form.method) | |
39 << " " | |
40 << form.origin.spec() | |
41 << " " | |
42 << form.action.spec() | |
43 << " "; | |
44 | |
45 for (std::vector<FormFieldData>::const_iterator iter = | |
46 form.fields.begin(); | |
47 iter != form.fields.end(); ++iter) { | |
48 os << *iter | |
49 << " "; | |
50 } | |
51 | |
52 return os; | |
53 } | |
54 | |
55 } // namespace content | |
56 | |
57 class FormStructureTest { | |
58 public: | |
59 static std::string Hash64Bit(const std::string& str) { | |
60 return FormStructure::Hash64Bit(str); | |
61 } | |
62 }; | |
63 | |
64 TEST(FormStructureTest, FieldCount) { | |
65 scoped_ptr<FormStructure> form_structure; | |
66 FormData form; | |
67 form.method = ASCIIToUTF16("post"); | |
68 | |
69 FormFieldData field; | |
70 field.label = ASCIIToUTF16("username"); | |
71 field.name = ASCIIToUTF16("username"); | |
72 field.form_control_type = "text"; | |
73 form.fields.push_back(field); | |
74 | |
75 field.label = ASCIIToUTF16("password"); | |
76 field.name = ASCIIToUTF16("password"); | |
77 field.form_control_type = "password"; | |
78 form.fields.push_back(field); | |
79 | |
80 field.label = string16(); | |
81 field.name = ASCIIToUTF16("Submit"); | |
82 field.form_control_type = "submit"; | |
83 form.fields.push_back(field); | |
84 | |
85 field.label = ASCIIToUTF16("address1"); | |
86 field.name = ASCIIToUTF16("address1"); | |
87 field.form_control_type = "text"; | |
88 field.should_autocomplete = false; | |
89 form.fields.push_back(field); | |
90 | |
91 // The render process sends all fields to browser including fields with | |
92 // autocomplete=off | |
93 form_structure.reset(new FormStructure(form, std::string())); | |
94 EXPECT_EQ(4U, form_structure->field_count()); | |
95 | |
96 // We expect the same count when autocheckout is enabled. | |
97 form_structure.reset(new FormStructure(form, "http://fake_url")); | |
98 EXPECT_EQ(4U, form_structure->field_count()); | |
99 } | |
100 | |
101 TEST(FormStructureTest, AutofillCount) { | |
102 scoped_ptr<FormStructure> form_structure; | |
103 FormData form; | |
104 form.method = ASCIIToUTF16("post"); | |
105 | |
106 FormFieldData field; | |
107 field.label = ASCIIToUTF16("username"); | |
108 field.name = ASCIIToUTF16("username"); | |
109 field.form_control_type = "text"; | |
110 form.fields.push_back(field); | |
111 | |
112 field.label = ASCIIToUTF16("password"); | |
113 field.name = ASCIIToUTF16("password"); | |
114 field.form_control_type = "password"; | |
115 form.fields.push_back(field); | |
116 | |
117 field.label = ASCIIToUTF16("state"); | |
118 field.name = ASCIIToUTF16("state"); | |
119 field.form_control_type = "select-one"; | |
120 form.fields.push_back(field); | |
121 | |
122 field.label = string16(); | |
123 field.name = ASCIIToUTF16("Submit"); | |
124 field.form_control_type = "submit"; | |
125 form.fields.push_back(field); | |
126 | |
127 // Only text and select fields that are heuristically matched are counted. | |
128 form_structure.reset(new FormStructure(form, std::string())); | |
129 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
130 EXPECT_EQ(1U, form_structure->autofill_count()); | |
131 | |
132 // Add a field with should_autocomplete=false. | |
133 field.label = ASCIIToUTF16("address1"); | |
134 field.name = ASCIIToUTF16("address1"); | |
135 field.form_control_type = "text"; | |
136 field.should_autocomplete = false; | |
137 form.fields.push_back(field); | |
138 | |
139 form_structure.reset(new FormStructure(form, std::string())); | |
140 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
141 // DetermineHeuristicTypes also assign field type for fields with | |
142 // autocomplete=off thus autofill_count includes them. This is a bug, | |
143 // and they should not be counted. See http://crbug.com/176432 for details. | |
144 // TODO(benquan): change it to EXPECT_EQ(1U, ... when the bug is fixed. | |
145 EXPECT_EQ(2U, form_structure->autofill_count()); | |
146 | |
147 // All fields should be counted when Autocheckout is enabled. | |
148 form_structure.reset(new FormStructure(form, "http://fake_url")); | |
149 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
150 EXPECT_EQ(2U, form_structure->autofill_count()); | |
151 } | |
152 | |
153 TEST(FormStructureTest, SourceURL) { | |
154 FormData form; | |
155 form.origin = GURL("http://www.foo.com/"); | |
156 form.method = ASCIIToUTF16("post"); | |
157 FormStructure form_structure(form, std::string()); | |
158 | |
159 EXPECT_EQ(form.origin, form_structure.source_url()); | |
160 } | |
161 | |
162 TEST(FormStructureTest, IsAutofillable) { | |
163 scoped_ptr<FormStructure> form_structure; | |
164 FormData form; | |
165 | |
166 // We need at least three text fields to be auto-fillable. | |
167 form.method = ASCIIToUTF16("post"); | |
168 | |
169 FormFieldData field; | |
170 // When autocheckout is enabled, we enable autofill even the form has | |
171 // no fields | |
172 form_structure.reset(new FormStructure(form, "http://fake_url")); | |
173 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
174 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
175 | |
176 field.label = ASCIIToUTF16("username"); | |
177 field.name = ASCIIToUTF16("username"); | |
178 field.form_control_type = "text"; | |
179 form.fields.push_back(field); | |
180 | |
181 field.label = ASCIIToUTF16("password"); | |
182 field.name = ASCIIToUTF16("password"); | |
183 field.form_control_type = "password"; | |
184 form.fields.push_back(field); | |
185 | |
186 field.label = string16(); | |
187 field.name = ASCIIToUTF16("Submit"); | |
188 field.form_control_type = "submit"; | |
189 form.fields.push_back(field); | |
190 | |
191 form_structure.reset(new FormStructure(form, std::string())); | |
192 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
193 EXPECT_FALSE(form_structure->IsAutofillable(true)); | |
194 | |
195 // We do not limit to three text fields when autocheckout is enabled. | |
196 form_structure.reset(new FormStructure(form, "http://fake_url")); | |
197 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
198 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
199 | |
200 // We now have three text fields, but only two auto-fillable fields. | |
201 field.label = ASCIIToUTF16("First Name"); | |
202 field.name = ASCIIToUTF16("firstname"); | |
203 field.form_control_type = "text"; | |
204 form.fields.push_back(field); | |
205 | |
206 field.label = ASCIIToUTF16("Last Name"); | |
207 field.name = ASCIIToUTF16("lastname"); | |
208 field.form_control_type = "text"; | |
209 form.fields.push_back(field); | |
210 | |
211 form_structure.reset(new FormStructure(form, std::string())); | |
212 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
213 EXPECT_FALSE(form_structure->IsAutofillable(true)); | |
214 | |
215 // We now have three auto-fillable fields. | |
216 field.label = ASCIIToUTF16("Email"); | |
217 field.name = ASCIIToUTF16("email"); | |
218 field.form_control_type = "email"; | |
219 form.fields.push_back(field); | |
220 | |
221 form_structure.reset(new FormStructure(form, std::string())); | |
222 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
223 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
224 | |
225 // The method must be 'post', though we can intentionally ignore this | |
226 // criterion for the sake of providing a helpful warning message to the user. | |
227 form.method = ASCIIToUTF16("get"); | |
228 form_structure.reset(new FormStructure(form, std::string())); | |
229 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
230 EXPECT_FALSE(form_structure->IsAutofillable(true)); | |
231 EXPECT_TRUE(form_structure->IsAutofillable(false)); | |
232 | |
233 // The target cannot include http(s)://*/search... | |
234 form.method = ASCIIToUTF16("post"); | |
235 form.action = GURL("http://google.com/search?q=hello"); | |
236 form_structure.reset(new FormStructure(form, std::string())); | |
237 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
238 EXPECT_FALSE(form_structure->IsAutofillable(true)); | |
239 | |
240 // But search can be in the URL. | |
241 form.action = GURL("http://search.com/?q=hello"); | |
242 form_structure.reset(new FormStructure(form, std::string())); | |
243 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
244 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
245 } | |
246 | |
247 TEST(FormStructureTest, ShouldBeParsed) { | |
248 scoped_ptr<FormStructure> form_structure; | |
249 FormData form; | |
250 | |
251 // We need at least three text fields to be parseable. | |
252 form.method = ASCIIToUTF16("post"); | |
253 | |
254 FormFieldData field; | |
255 field.label = ASCIIToUTF16("username"); | |
256 field.name = ASCIIToUTF16("username"); | |
257 field.form_control_type = "text"; | |
258 form.fields.push_back(field); | |
259 | |
260 FormFieldData checkable_field; | |
261 checkable_field.is_checkable = true; | |
262 checkable_field.name = ASCIIToUTF16("radiobtn"); | |
263 checkable_field.form_control_type = "radio"; | |
264 form.fields.push_back(checkable_field); | |
265 | |
266 checkable_field.name = ASCIIToUTF16("checkbox"); | |
267 checkable_field.form_control_type = "checkbox"; | |
268 form.fields.push_back(checkable_field); | |
269 | |
270 // We have only one text field, should not be parsed. | |
271 form_structure.reset(new FormStructure(form, std::string())); | |
272 EXPECT_FALSE(form_structure->ShouldBeParsed(true)); | |
273 | |
274 // The form should be parsed for autocheckout even it has less than three | |
275 // text fields. | |
276 form_structure.reset(new FormStructure(form, "http://fake_url")); | |
277 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); | |
278 | |
279 // We now have three text fields, though only two are auto-fillable. | |
280 field.label = ASCIIToUTF16("First Name"); | |
281 field.name = ASCIIToUTF16("firstname"); | |
282 field.form_control_type = "text"; | |
283 form.fields.push_back(field); | |
284 | |
285 field.label = ASCIIToUTF16("Last Name"); | |
286 field.name = ASCIIToUTF16("lastname"); | |
287 field.form_control_type = "text"; | |
288 form.fields.push_back(field); | |
289 | |
290 form_structure.reset(new FormStructure(form, std::string())); | |
291 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); | |
292 | |
293 // The method must be 'post', though we can intentionally ignore this | |
294 // criterion for the sake of providing a helpful warning message to the user. | |
295 form.method = ASCIIToUTF16("get"); | |
296 form_structure.reset(new FormStructure(form, std::string())); | |
297 EXPECT_FALSE(form_structure->IsAutofillable(true)); | |
298 EXPECT_TRUE(form_structure->ShouldBeParsed(false)); | |
299 | |
300 // The target cannot include http(s)://*/search... | |
301 form.method = ASCIIToUTF16("post"); | |
302 form.action = GURL("http://google.com/search?q=hello"); | |
303 form_structure.reset(new FormStructure(form, std::string())); | |
304 EXPECT_FALSE(form_structure->ShouldBeParsed(true)); | |
305 | |
306 // But search can be in the URL. | |
307 form.action = GURL("http://search.com/?q=hello"); | |
308 form_structure.reset(new FormStructure(form, std::string())); | |
309 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); | |
310 | |
311 // The form need only have three fields, but at least one must be a text | |
312 // field. | |
313 form.fields.clear(); | |
314 | |
315 field.label = ASCIIToUTF16("Email"); | |
316 field.name = ASCIIToUTF16("email"); | |
317 field.form_control_type = "email"; | |
318 form.fields.push_back(field); | |
319 | |
320 field.label = ASCIIToUTF16("State"); | |
321 field.name = ASCIIToUTF16("state"); | |
322 field.form_control_type = "select-one"; | |
323 form.fields.push_back(field); | |
324 | |
325 field.label = ASCIIToUTF16("Country"); | |
326 field.name = ASCIIToUTF16("country"); | |
327 field.form_control_type = "select-one"; | |
328 form.fields.push_back(field); | |
329 | |
330 form_structure.reset(new FormStructure(form, std::string())); | |
331 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); | |
332 | |
333 form.fields[0].form_control_type = "select-one"; | |
334 // Now, no text fields. | |
335 form_structure.reset(new FormStructure(form, std::string())); | |
336 EXPECT_FALSE(form_structure->ShouldBeParsed(true)); | |
337 | |
338 // It should be parsed when autocheckout is enabled. | |
339 form_structure.reset(new FormStructure(form, "http://fake_url")); | |
340 EXPECT_TRUE(form_structure->ShouldBeParsed(true)); | |
341 } | |
342 | |
343 TEST(FormStructureTest, HeuristicsContactInfo) { | |
344 scoped_ptr<FormStructure> form_structure; | |
345 FormData form; | |
346 form.method = ASCIIToUTF16("post"); | |
347 | |
348 FormFieldData field; | |
349 field.form_control_type = "text"; | |
350 | |
351 field.label = ASCIIToUTF16("First Name"); | |
352 field.name = ASCIIToUTF16("firstname"); | |
353 form.fields.push_back(field); | |
354 | |
355 field.label = ASCIIToUTF16("Last Name"); | |
356 field.name = ASCIIToUTF16("lastname"); | |
357 form.fields.push_back(field); | |
358 | |
359 field.label = ASCIIToUTF16("Email"); | |
360 field.name = ASCIIToUTF16("email"); | |
361 form.fields.push_back(field); | |
362 | |
363 field.label = ASCIIToUTF16("Phone"); | |
364 field.name = ASCIIToUTF16("phone"); | |
365 form.fields.push_back(field); | |
366 | |
367 field.label = ASCIIToUTF16("Address"); | |
368 field.name = ASCIIToUTF16("address"); | |
369 form.fields.push_back(field); | |
370 | |
371 field.label = ASCIIToUTF16("City"); | |
372 field.name = ASCIIToUTF16("city"); | |
373 form.fields.push_back(field); | |
374 | |
375 field.label = ASCIIToUTF16("Zip code"); | |
376 field.name = ASCIIToUTF16("zipcode"); | |
377 form.fields.push_back(field); | |
378 | |
379 field.label = string16(); | |
380 field.name = ASCIIToUTF16("Submit"); | |
381 field.form_control_type = "submit"; | |
382 form.fields.push_back(field); | |
383 | |
384 form_structure.reset(new FormStructure(form, std::string())); | |
385 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
386 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
387 | |
388 // Expect the correct number of fields. | |
389 ASSERT_EQ(8U, form_structure->field_count()); | |
390 ASSERT_EQ(7U, form_structure->autofill_count()); | |
391 | |
392 // First name. | |
393 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | |
394 // Last name. | |
395 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | |
396 // Email. | |
397 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); | |
398 // Phone. | |
399 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, | |
400 form_structure->field(3)->heuristic_type()); | |
401 // Address. | |
402 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type()); | |
403 // City. | |
404 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type()); | |
405 // Zip. | |
406 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type()); | |
407 // Submit. | |
408 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type()); | |
409 } | |
410 | |
411 // Verify that we can correctly process the |autocomplete| attribute. | |
412 TEST(FormStructureTest, HeuristicsAutocompleteAttribute) { | |
413 scoped_ptr<FormStructure> form_structure; | |
414 FormData form; | |
415 form.method = ASCIIToUTF16("post"); | |
416 | |
417 FormFieldData field; | |
418 field.form_control_type = "text"; | |
419 | |
420 field.label = string16(); | |
421 field.name = ASCIIToUTF16("field1"); | |
422 field.autocomplete_attribute = "given-name"; | |
423 form.fields.push_back(field); | |
424 | |
425 field.label = string16(); | |
426 field.name = ASCIIToUTF16("field2"); | |
427 field.autocomplete_attribute = "family-name"; | |
428 form.fields.push_back(field); | |
429 | |
430 field.label = string16(); | |
431 field.name = ASCIIToUTF16("field3"); | |
432 field.autocomplete_attribute = "email"; | |
433 form.fields.push_back(field); | |
434 | |
435 form_structure.reset(new FormStructure(form, std::string())); | |
436 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
437 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
438 | |
439 // Expect the correct number of fields. | |
440 ASSERT_EQ(3U, form_structure->field_count()); | |
441 ASSERT_EQ(3U, form_structure->autofill_count()); | |
442 | |
443 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | |
444 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | |
445 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); | |
446 } | |
447 | |
448 // Verify that we can correctly process the 'autocomplete' attribute for phone | |
449 // number types (especially phone prefixes and suffixes). | |
450 TEST(FormStructureTest, HeuristicsAutocompleteAttributePhoneTypes) { | |
451 scoped_ptr<FormStructure> form_structure; | |
452 FormData form; | |
453 form.method = ASCIIToUTF16("post"); | |
454 | |
455 FormFieldData field; | |
456 field.form_control_type = "text"; | |
457 | |
458 field.label = string16(); | |
459 field.name = ASCIIToUTF16("field1"); | |
460 field.autocomplete_attribute = "tel-local"; | |
461 form.fields.push_back(field); | |
462 | |
463 field.label = string16(); | |
464 field.name = ASCIIToUTF16("field2"); | |
465 field.autocomplete_attribute = "tel-local-prefix"; | |
466 form.fields.push_back(field); | |
467 | |
468 field.label = string16(); | |
469 field.name = ASCIIToUTF16("field3"); | |
470 field.autocomplete_attribute = "tel-local-suffix"; | |
471 form.fields.push_back(field); | |
472 | |
473 form_structure.reset(new FormStructure(form, std::string())); | |
474 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
475 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
476 | |
477 // Expect the correct number of fields. | |
478 ASSERT_EQ(3U, form_structure->field_count()); | |
479 EXPECT_EQ(3U, form_structure->autofill_count()); | |
480 | |
481 EXPECT_EQ(PHONE_HOME_NUMBER, form_structure->field(0)->heuristic_type()); | |
482 EXPECT_EQ(AutofillField::IGNORED, form_structure->field(0)->phone_part()); | |
483 EXPECT_EQ(PHONE_HOME_NUMBER, form_structure->field(1)->heuristic_type()); | |
484 EXPECT_EQ(AutofillField::PHONE_PREFIX, | |
485 form_structure->field(1)->phone_part()); | |
486 EXPECT_EQ(PHONE_HOME_NUMBER, form_structure->field(2)->heuristic_type()); | |
487 EXPECT_EQ(AutofillField::PHONE_SUFFIX, | |
488 form_structure->field(2)->phone_part()); | |
489 } | |
490 | |
491 // If at least one field includes type hints in the 'autocomplete' attribute, we | |
492 // should not try to apply any other heuristics. | |
493 TEST(FormStructureTest, AutocompleteAttributeOverridesOtherHeuristics) { | |
494 scoped_ptr<FormStructure> form_structure; | |
495 FormData form; | |
496 form.method = ASCIIToUTF16("post"); | |
497 | |
498 // Start with a regular contact form. | |
499 FormFieldData field; | |
500 field.form_control_type = "text"; | |
501 | |
502 field.label = ASCIIToUTF16("First Name"); | |
503 field.name = ASCIIToUTF16("firstname"); | |
504 form.fields.push_back(field); | |
505 | |
506 field.label = ASCIIToUTF16("Last Name"); | |
507 field.name = ASCIIToUTF16("lastname"); | |
508 form.fields.push_back(field); | |
509 | |
510 field.label = ASCIIToUTF16("Email"); | |
511 field.name = ASCIIToUTF16("email"); | |
512 form.fields.push_back(field); | |
513 | |
514 form_structure.reset(new FormStructure(form, std::string())); | |
515 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
516 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
517 EXPECT_TRUE(form_structure->ShouldBeCrowdsourced()); | |
518 | |
519 ASSERT_EQ(3U, form_structure->field_count()); | |
520 ASSERT_EQ(3U, form_structure->autofill_count()); | |
521 | |
522 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | |
523 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | |
524 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); | |
525 | |
526 // Now update the first form field to include an 'autocomplete' attribute. | |
527 form.fields.front().autocomplete_attribute = "x-other"; | |
528 form_structure.reset(new FormStructure(form, std::string())); | |
529 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
530 EXPECT_FALSE(form_structure->IsAutofillable(true)); | |
531 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced()); | |
532 | |
533 ASSERT_EQ(3U, form_structure->field_count()); | |
534 ASSERT_EQ(0U, form_structure->autofill_count()); | |
535 | |
536 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type()); | |
537 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); | |
538 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); | |
539 } | |
540 | |
541 // Verify that we can correctly process sections listed in the |autocomplete| | |
542 // attribute. | |
543 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSections) { | |
544 FormData form; | |
545 form.method = ASCIIToUTF16("post"); | |
546 | |
547 FormFieldData field; | |
548 field.form_control_type = "text"; | |
549 | |
550 // Some fields will have no section specified. These fall into the default | |
551 // section. | |
552 field.autocomplete_attribute = "email"; | |
553 form.fields.push_back(field); | |
554 | |
555 // We allow arbitrary section names. | |
556 field.autocomplete_attribute = "section-foo email"; | |
557 form.fields.push_back(field); | |
558 | |
559 // "shipping" and "billing" are special section tokens that don't require the | |
560 // "section-" prefix. | |
561 field.autocomplete_attribute = "shipping email"; | |
562 form.fields.push_back(field); | |
563 field.autocomplete_attribute = "billing email"; | |
564 form.fields.push_back(field); | |
565 | |
566 // "shipping" and "billing" can be combined with other section names. | |
567 field.autocomplete_attribute = "section-foo shipping email"; | |
568 form.fields.push_back(field); | |
569 field.autocomplete_attribute = "section-foo billing email"; | |
570 form.fields.push_back(field); | |
571 | |
572 // We don't do anything clever to try to coalesce sections; it's up to site | |
573 // authors to avoid typos. | |
574 field.autocomplete_attribute = "section--foo email"; | |
575 form.fields.push_back(field); | |
576 | |
577 // "shipping email" and "section--shipping" email should be parsed as | |
578 // different sections. This is only an interesting test due to how we | |
579 // implement implicit section names from attributes like "shipping email"; see | |
580 // the implementation for more details. | |
581 field.autocomplete_attribute = "section--shipping email"; | |
582 form.fields.push_back(field); | |
583 | |
584 // Credit card fields are implicitly in a separate section from other fields. | |
585 field.autocomplete_attribute = "section-foo cc-number"; | |
586 form.fields.push_back(field); | |
587 | |
588 FormStructure form_structure(form, std::string()); | |
589 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); | |
590 EXPECT_TRUE(form_structure.IsAutofillable(true)); | |
591 | |
592 // Expect the correct number of fields. | |
593 ASSERT_EQ(9U, form_structure.field_count()); | |
594 EXPECT_EQ(9U, form_structure.autofill_count()); | |
595 | |
596 // All of the fields in this form should be parsed as belonging to different | |
597 // sections. | |
598 std::set<std::string> section_names; | |
599 for (size_t i = 0; i < 9; ++i) { | |
600 section_names.insert(form_structure.field(i)->section()); | |
601 } | |
602 EXPECT_EQ(9U, section_names.size()); | |
603 } | |
604 | |
605 // Verify that we can correctly process a degenerate section listed in the | |
606 // |autocomplete| attribute. | |
607 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsDegenerate) { | |
608 FormData form; | |
609 form.method = ASCIIToUTF16("post"); | |
610 | |
611 FormFieldData field; | |
612 field.form_control_type = "text"; | |
613 | |
614 // Some fields will have no section specified. These fall into the default | |
615 // section. | |
616 field.autocomplete_attribute = "email"; | |
617 form.fields.push_back(field); | |
618 | |
619 // Specifying "section-" is equivalent to not specifying a section. | |
620 field.autocomplete_attribute = "section- email"; | |
621 form.fields.push_back(field); | |
622 | |
623 // Invalid tokens should prevent us from setting a section name. | |
624 field.autocomplete_attribute = "garbage section-foo email"; | |
625 form.fields.push_back(field); | |
626 field.autocomplete_attribute = "garbage section-bar email"; | |
627 form.fields.push_back(field); | |
628 field.autocomplete_attribute = "garbage shipping email"; | |
629 form.fields.push_back(field); | |
630 field.autocomplete_attribute = "garbage billing email"; | |
631 form.fields.push_back(field); | |
632 | |
633 FormStructure form_structure(form, std::string()); | |
634 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); | |
635 | |
636 // Expect the correct number of fields. | |
637 ASSERT_EQ(6U, form_structure.field_count()); | |
638 EXPECT_EQ(2U, form_structure.autofill_count()); | |
639 | |
640 // All of the fields in this form should be parsed as belonging to the same | |
641 // section. | |
642 std::set<std::string> section_names; | |
643 for (size_t i = 0; i < 6; ++i) { | |
644 section_names.insert(form_structure.field(i)->section()); | |
645 } | |
646 EXPECT_EQ(1U, section_names.size()); | |
647 } | |
648 | |
649 // Verify that we can correctly process repeated sections listed in the | |
650 // |autocomplete| attribute. | |
651 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsRepeated) { | |
652 FormData form; | |
653 form.method = ASCIIToUTF16("post"); | |
654 | |
655 FormFieldData field; | |
656 field.form_control_type = "text"; | |
657 | |
658 field.autocomplete_attribute = "section-foo email"; | |
659 form.fields.push_back(field); | |
660 field.autocomplete_attribute = "section-foo street-address"; | |
661 form.fields.push_back(field); | |
662 | |
663 FormStructure form_structure(form, std::string()); | |
664 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); | |
665 | |
666 // Expect the correct number of fields. | |
667 ASSERT_EQ(2U, form_structure.field_count()); | |
668 EXPECT_EQ(2U, form_structure.autofill_count()); | |
669 | |
670 // All of the fields in this form should be parsed as belonging to the same | |
671 // section. | |
672 std::set<std::string> section_names; | |
673 for (size_t i = 0; i < 2; ++i) { | |
674 section_names.insert(form_structure.field(i)->section()); | |
675 } | |
676 EXPECT_EQ(1U, section_names.size()); | |
677 } | |
678 | |
679 // Verify that we do not override the author-specified sections from a form with | |
680 // local heuristics. | |
681 TEST(FormStructureTest, HeuristicsDontOverrideAutocompleteAttributeSections) { | |
682 FormData form; | |
683 form.method = ASCIIToUTF16("post"); | |
684 | |
685 FormFieldData field; | |
686 field.form_control_type = "text"; | |
687 | |
688 field.name = ASCIIToUTF16("one"); | |
689 field.autocomplete_attribute = "street-address"; | |
690 form.fields.push_back(field); | |
691 field.name = string16(); | |
692 field.autocomplete_attribute = "section-foo email"; | |
693 form.fields.push_back(field); | |
694 field.name = string16(); | |
695 field.autocomplete_attribute = "name"; | |
696 form.fields.push_back(field); | |
697 field.name = ASCIIToUTF16("two"); | |
698 field.autocomplete_attribute = "street-address"; | |
699 form.fields.push_back(field); | |
700 | |
701 FormStructure form_structure(form, std::string()); | |
702 form_structure.DetermineHeuristicTypes(TestAutofillMetrics()); | |
703 | |
704 // Expect the correct number of fields. | |
705 ASSERT_EQ(4U, form_structure.field_count()); | |
706 EXPECT_EQ(4U, form_structure.autofill_count()); | |
707 | |
708 // Normally, the two separate address fields would cause us to detect two | |
709 // separate sections; but because there is an author-specified section in this | |
710 // form, we do not apply these usual heuristics. | |
711 EXPECT_EQ(ASCIIToUTF16("one"), form_structure.field(0)->name); | |
712 EXPECT_EQ(ASCIIToUTF16("two"), form_structure.field(3)->name); | |
713 EXPECT_EQ(form_structure.field(0)->section(), | |
714 form_structure.field(3)->section()); | |
715 } | |
716 | |
717 TEST(FormStructureTest, HeuristicsSample8) { | |
718 scoped_ptr<FormStructure> form_structure; | |
719 FormData form; | |
720 form.method = ASCIIToUTF16("post"); | |
721 | |
722 FormFieldData field; | |
723 field.form_control_type = "text"; | |
724 | |
725 field.label = ASCIIToUTF16("Your First Name:"); | |
726 field.name = ASCIIToUTF16("bill.first"); | |
727 form.fields.push_back(field); | |
728 | |
729 field.label = ASCIIToUTF16("Your Last Name:"); | |
730 field.name = ASCIIToUTF16("bill.last"); | |
731 form.fields.push_back(field); | |
732 | |
733 field.label = ASCIIToUTF16("Street Address Line 1:"); | |
734 field.name = ASCIIToUTF16("bill.street1"); | |
735 form.fields.push_back(field); | |
736 | |
737 field.label = ASCIIToUTF16("Street Address Line 2:"); | |
738 field.name = ASCIIToUTF16("bill.street2"); | |
739 form.fields.push_back(field); | |
740 | |
741 field.label = ASCIIToUTF16("City"); | |
742 field.name = ASCIIToUTF16("bill.city"); | |
743 form.fields.push_back(field); | |
744 | |
745 field.label = ASCIIToUTF16("State (U.S.):"); | |
746 field.name = ASCIIToUTF16("bill.state"); | |
747 form.fields.push_back(field); | |
748 | |
749 field.label = ASCIIToUTF16("Zip/Postal Code:"); | |
750 field.name = ASCIIToUTF16("BillTo.PostalCode"); | |
751 form.fields.push_back(field); | |
752 | |
753 field.label = ASCIIToUTF16("Country:"); | |
754 field.name = ASCIIToUTF16("bill.country"); | |
755 form.fields.push_back(field); | |
756 | |
757 field.label = ASCIIToUTF16("Phone Number:"); | |
758 field.name = ASCIIToUTF16("BillTo.Phone"); | |
759 form.fields.push_back(field); | |
760 | |
761 field.label = string16(); | |
762 field.name = ASCIIToUTF16("Submit"); | |
763 field.form_control_type = "submit"; | |
764 form.fields.push_back(field); | |
765 | |
766 form_structure.reset(new FormStructure(form, std::string())); | |
767 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
768 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
769 ASSERT_EQ(10U, form_structure->field_count()); | |
770 ASSERT_EQ(9U, form_structure->autofill_count()); | |
771 | |
772 // First name. | |
773 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | |
774 // Last name. | |
775 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | |
776 // Address. | |
777 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(2)->heuristic_type()); | |
778 // Address. | |
779 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(3)->heuristic_type()); | |
780 // City. | |
781 EXPECT_EQ(ADDRESS_BILLING_CITY, form_structure->field(4)->heuristic_type()); | |
782 // State. | |
783 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure->field(5)->heuristic_type()); | |
784 // Zip. | |
785 EXPECT_EQ(ADDRESS_BILLING_ZIP, form_structure->field(6)->heuristic_type()); | |
786 // Country. | |
787 EXPECT_EQ(ADDRESS_BILLING_COUNTRY, | |
788 form_structure->field(7)->heuristic_type()); | |
789 // Phone. | |
790 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, | |
791 form_structure->field(8)->heuristic_type()); | |
792 // Submit. | |
793 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(9)->heuristic_type()); | |
794 } | |
795 | |
796 TEST(FormStructureTest, HeuristicsSample6) { | |
797 scoped_ptr<FormStructure> form_structure; | |
798 FormData form; | |
799 form.method = ASCIIToUTF16("post"); | |
800 | |
801 FormFieldData field; | |
802 field.form_control_type = "text"; | |
803 | |
804 field.label = ASCIIToUTF16("E-mail address"); | |
805 field.name = ASCIIToUTF16("email"); | |
806 form.fields.push_back(field); | |
807 | |
808 field.label = ASCIIToUTF16("Full name"); | |
809 field.name = ASCIIToUTF16("name"); | |
810 form.fields.push_back(field); | |
811 | |
812 field.label = ASCIIToUTF16("Company"); | |
813 field.name = ASCIIToUTF16("company"); | |
814 form.fields.push_back(field); | |
815 | |
816 field.label = ASCIIToUTF16("Address"); | |
817 field.name = ASCIIToUTF16("address"); | |
818 form.fields.push_back(field); | |
819 | |
820 field.label = ASCIIToUTF16("City"); | |
821 field.name = ASCIIToUTF16("city"); | |
822 form.fields.push_back(field); | |
823 | |
824 field.label = ASCIIToUTF16("Zip Code"); | |
825 field.name = ASCIIToUTF16("Home.PostalCode"); | |
826 form.fields.push_back(field); | |
827 | |
828 field.label = string16(); | |
829 field.name = ASCIIToUTF16("Submit"); | |
830 field.value = ASCIIToUTF16("continue"); | |
831 field.form_control_type = "submit"; | |
832 form.fields.push_back(field); | |
833 | |
834 form_structure.reset(new FormStructure(form, std::string())); | |
835 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
836 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
837 ASSERT_EQ(7U, form_structure->field_count()); | |
838 ASSERT_EQ(6U, form_structure->autofill_count()); | |
839 | |
840 // Email. | |
841 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(0)->heuristic_type()); | |
842 // Full name. | |
843 EXPECT_EQ(NAME_FULL, form_structure->field(1)->heuristic_type()); | |
844 // Company | |
845 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type()); | |
846 // Address. | |
847 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type()); | |
848 // City. | |
849 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type()); | |
850 // Zip. | |
851 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(5)->heuristic_type()); | |
852 // Submit. | |
853 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type()); | |
854 } | |
855 | |
856 // Tests a sequence of FormFields where only labels are supplied to heuristics | |
857 // for matching. This works because FormFieldData labels are matched in the | |
858 // case that input element ids (or |name| fields) are missing. | |
859 TEST(FormStructureTest, HeuristicsLabelsOnly) { | |
860 scoped_ptr<FormStructure> form_structure; | |
861 FormData form; | |
862 form.method = ASCIIToUTF16("post"); | |
863 | |
864 FormFieldData field; | |
865 field.form_control_type = "text"; | |
866 | |
867 field.label = ASCIIToUTF16("First Name"); | |
868 field.name = string16(); | |
869 form.fields.push_back(field); | |
870 | |
871 field.label = ASCIIToUTF16("Last Name"); | |
872 field.name = string16(); | |
873 form.fields.push_back(field); | |
874 | |
875 field.label = ASCIIToUTF16("Email"); | |
876 field.name = string16(); | |
877 form.fields.push_back(field); | |
878 | |
879 field.label = ASCIIToUTF16("Phone"); | |
880 field.name = string16(); | |
881 form.fields.push_back(field); | |
882 | |
883 field.label = ASCIIToUTF16("Address"); | |
884 field.name = string16(); | |
885 form.fields.push_back(field); | |
886 | |
887 field.label = ASCIIToUTF16("Address"); | |
888 field.name = string16(); | |
889 form.fields.push_back(field); | |
890 | |
891 field.label = ASCIIToUTF16("Zip code"); | |
892 field.name = string16(); | |
893 form.fields.push_back(field); | |
894 | |
895 field.label = string16(); | |
896 field.name = ASCIIToUTF16("Submit"); | |
897 field.form_control_type = "submit"; | |
898 form.fields.push_back(field); | |
899 | |
900 form_structure.reset(new FormStructure(form, std::string())); | |
901 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
902 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
903 ASSERT_EQ(8U, form_structure->field_count()); | |
904 ASSERT_EQ(7U, form_structure->autofill_count()); | |
905 | |
906 // First name. | |
907 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | |
908 // Last name. | |
909 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | |
910 // Email. | |
911 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type()); | |
912 // Phone. | |
913 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, | |
914 form_structure->field(3)->heuristic_type()); | |
915 // Address. | |
916 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type()); | |
917 // Address Line 2. | |
918 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(5)->heuristic_type()); | |
919 // Zip. | |
920 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type()); | |
921 // Submit. | |
922 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type()); | |
923 } | |
924 | |
925 TEST(FormStructureTest, HeuristicsCreditCardInfo) { | |
926 scoped_ptr<FormStructure> form_structure; | |
927 FormData form; | |
928 form.method = ASCIIToUTF16("post"); | |
929 | |
930 FormFieldData field; | |
931 field.form_control_type = "text"; | |
932 | |
933 field.label = ASCIIToUTF16("Name on Card"); | |
934 field.name = ASCIIToUTF16("name_on_card"); | |
935 form.fields.push_back(field); | |
936 | |
937 field.label = ASCIIToUTF16("Card Number"); | |
938 field.name = ASCIIToUTF16("card_number"); | |
939 form.fields.push_back(field); | |
940 | |
941 field.label = ASCIIToUTF16("Exp Month"); | |
942 field.name = ASCIIToUTF16("ccmonth"); | |
943 form.fields.push_back(field); | |
944 | |
945 field.label = ASCIIToUTF16("Exp Year"); | |
946 field.name = ASCIIToUTF16("ccyear"); | |
947 form.fields.push_back(field); | |
948 | |
949 field.label = ASCIIToUTF16("Verification"); | |
950 field.name = ASCIIToUTF16("verification"); | |
951 form.fields.push_back(field); | |
952 | |
953 field.label = string16(); | |
954 field.name = ASCIIToUTF16("Submit"); | |
955 field.form_control_type = "submit"; | |
956 form.fields.push_back(field); | |
957 | |
958 form_structure.reset(new FormStructure(form, std::string())); | |
959 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
960 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
961 ASSERT_EQ(6U, form_structure->field_count()); | |
962 ASSERT_EQ(5U, form_structure->autofill_count()); | |
963 | |
964 // Credit card name. | |
965 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); | |
966 // Credit card number. | |
967 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(1)->heuristic_type()); | |
968 // Credit card expiration month. | |
969 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(2)->heuristic_type()); | |
970 // Credit card expiration year. | |
971 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, | |
972 form_structure->field(3)->heuristic_type()); | |
973 // CVV. | |
974 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, | |
975 form_structure->field(4)->heuristic_type()); | |
976 // Submit. | |
977 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(5)->heuristic_type()); | |
978 } | |
979 | |
980 TEST(FormStructureTest, HeuristicsCreditCardInfoWithUnknownCardField) { | |
981 scoped_ptr<FormStructure> form_structure; | |
982 FormData form; | |
983 form.method = ASCIIToUTF16("post"); | |
984 | |
985 FormFieldData field; | |
986 field.form_control_type = "text"; | |
987 | |
988 field.label = ASCIIToUTF16("Name on Card"); | |
989 field.name = ASCIIToUTF16("name_on_card"); | |
990 form.fields.push_back(field); | |
991 | |
992 // This is not a field we know how to process. But we should skip over it | |
993 // and process the other fields in the card block. | |
994 field.label = ASCIIToUTF16("Card image"); | |
995 field.name = ASCIIToUTF16("card_image"); | |
996 form.fields.push_back(field); | |
997 | |
998 field.label = ASCIIToUTF16("Card Number"); | |
999 field.name = ASCIIToUTF16("card_number"); | |
1000 form.fields.push_back(field); | |
1001 | |
1002 field.label = ASCIIToUTF16("Exp Month"); | |
1003 field.name = ASCIIToUTF16("ccmonth"); | |
1004 form.fields.push_back(field); | |
1005 | |
1006 field.label = ASCIIToUTF16("Exp Year"); | |
1007 field.name = ASCIIToUTF16("ccyear"); | |
1008 form.fields.push_back(field); | |
1009 | |
1010 field.label = ASCIIToUTF16("Verification"); | |
1011 field.name = ASCIIToUTF16("verification"); | |
1012 form.fields.push_back(field); | |
1013 | |
1014 field.label = string16(); | |
1015 field.name = ASCIIToUTF16("Submit"); | |
1016 field.form_control_type = "submit"; | |
1017 form.fields.push_back(field); | |
1018 | |
1019 form_structure.reset(new FormStructure(form, std::string())); | |
1020 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1021 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1022 ASSERT_EQ(7U, form_structure->field_count()); | |
1023 ASSERT_EQ(5U, form_structure->autofill_count()); | |
1024 | |
1025 // Credit card name. | |
1026 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); | |
1027 // Credit card type. This is an unknown type but related to the credit card. | |
1028 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type()); | |
1029 // Credit card number. | |
1030 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type()); | |
1031 // Credit card expiration month. | |
1032 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); | |
1033 // Credit card expiration year. | |
1034 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, | |
1035 form_structure->field(4)->heuristic_type()); | |
1036 // CVV. | |
1037 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, | |
1038 form_structure->field(5)->heuristic_type()); | |
1039 // Submit. | |
1040 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type()); | |
1041 } | |
1042 | |
1043 TEST(FormStructureTest, ThreeAddressLines) { | |
1044 scoped_ptr<FormStructure> form_structure; | |
1045 FormData form; | |
1046 form.method = ASCIIToUTF16("post"); | |
1047 | |
1048 FormFieldData field; | |
1049 field.form_control_type = "text"; | |
1050 | |
1051 field.label = ASCIIToUTF16("Address Line1"); | |
1052 field.name = ASCIIToUTF16("Address"); | |
1053 form.fields.push_back(field); | |
1054 | |
1055 field.label = ASCIIToUTF16("Address Line2"); | |
1056 field.name = ASCIIToUTF16("Address"); | |
1057 form.fields.push_back(field); | |
1058 | |
1059 field.label = ASCIIToUTF16("Address Line3"); | |
1060 field.name = ASCIIToUTF16("Address"); | |
1061 form.fields.push_back(field); | |
1062 | |
1063 field.label = ASCIIToUTF16("City"); | |
1064 field.name = ASCIIToUTF16("city"); | |
1065 form.fields.push_back(field); | |
1066 | |
1067 form_structure.reset(new FormStructure(form, std::string())); | |
1068 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1069 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1070 ASSERT_EQ(4U, form_structure->field_count()); | |
1071 ASSERT_EQ(3U, form_structure->autofill_count()); | |
1072 | |
1073 // Address Line 1. | |
1074 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); | |
1075 // Address Line 2. | |
1076 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); | |
1077 // Address Line 3. | |
1078 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); | |
1079 // City. | |
1080 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type()); | |
1081 } | |
1082 | |
1083 // This test verifies that "addressLine1" and "addressLine2" matches heuristics. | |
1084 // This occured in https://www.gorillaclothing.com/. http://crbug.com/52126. | |
1085 TEST(FormStructureTest, BillingAndShippingAddresses) { | |
1086 scoped_ptr<FormStructure> form_structure; | |
1087 FormData form; | |
1088 form.method = ASCIIToUTF16("post"); | |
1089 | |
1090 FormFieldData field; | |
1091 field.form_control_type = "text"; | |
1092 | |
1093 field.label = ASCIIToUTF16("Address Line1"); | |
1094 field.name = ASCIIToUTF16("shipping.address.addressLine1"); | |
1095 form.fields.push_back(field); | |
1096 | |
1097 field.label = ASCIIToUTF16("Address Line2"); | |
1098 field.name = ASCIIToUTF16("shipping.address.addressLine2"); | |
1099 form.fields.push_back(field); | |
1100 | |
1101 field.label = ASCIIToUTF16("Address Line1"); | |
1102 field.name = ASCIIToUTF16("billing.address.addressLine1"); | |
1103 form.fields.push_back(field); | |
1104 | |
1105 field.label = ASCIIToUTF16("Address Line2"); | |
1106 field.name = ASCIIToUTF16("billing.address.addressLine2"); | |
1107 form.fields.push_back(field); | |
1108 | |
1109 form_structure.reset(new FormStructure(form, std::string())); | |
1110 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1111 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1112 ASSERT_EQ(4U, form_structure->field_count()); | |
1113 ASSERT_EQ(4U, form_structure->autofill_count()); | |
1114 | |
1115 // Address Line 1. | |
1116 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); | |
1117 // Address Line 2. | |
1118 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); | |
1119 // Address Line 1. | |
1120 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(2)->heuristic_type()); | |
1121 // Address Line 2. | |
1122 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(3)->heuristic_type()); | |
1123 } | |
1124 | |
1125 | |
1126 // This example comes from expedia.com where they use a "Suite" label to | |
1127 // indicate a suite or apartment number. We interpret this as address line 2. | |
1128 // And the following "Street address second line" we interpret as address line | |
1129 // 3 and discard. | |
1130 // See http://crbug.com/48197 for details. | |
1131 TEST(FormStructureTest, ThreeAddressLinesExpedia) { | |
1132 scoped_ptr<FormStructure> form_structure; | |
1133 FormData form; | |
1134 form.method = ASCIIToUTF16("post"); | |
1135 | |
1136 FormFieldData field; | |
1137 field.form_control_type = "text"; | |
1138 | |
1139 field.label = ASCIIToUTF16("Street:"); | |
1140 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads1"); | |
1141 form.fields.push_back(field); | |
1142 | |
1143 field.label = ASCIIToUTF16("Suite or Apt:"); | |
1144 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adap"); | |
1145 form.fields.push_back(field); | |
1146 | |
1147 field.label = ASCIIToUTF16("Street address second line"); | |
1148 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads2"); | |
1149 form.fields.push_back(field); | |
1150 | |
1151 field.label = ASCIIToUTF16("City:"); | |
1152 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adct"); | |
1153 form.fields.push_back(field); | |
1154 | |
1155 form_structure.reset(new FormStructure(form, std::string())); | |
1156 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1157 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1158 ASSERT_EQ(4U, form_structure->field_count()); | |
1159 EXPECT_EQ(3U, form_structure->autofill_count()); | |
1160 | |
1161 // Address Line 1. | |
1162 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); | |
1163 // Suite / Apt. | |
1164 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); | |
1165 // Address Line 3. | |
1166 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); | |
1167 // City. | |
1168 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type()); | |
1169 } | |
1170 | |
1171 // This example comes from ebay.com where the word "suite" appears in the label | |
1172 // and the name "address2" clearly indicates that this is the address line 2. | |
1173 // See http://crbug.com/48197 for details. | |
1174 TEST(FormStructureTest, TwoAddressLinesEbay) { | |
1175 scoped_ptr<FormStructure> form_structure; | |
1176 FormData form; | |
1177 form.method = ASCIIToUTF16("post"); | |
1178 | |
1179 FormFieldData field; | |
1180 field.form_control_type = "text"; | |
1181 | |
1182 field.label = ASCIIToUTF16("Address Line1"); | |
1183 field.name = ASCIIToUTF16("address1"); | |
1184 form.fields.push_back(field); | |
1185 | |
1186 field.label = ASCIIToUTF16("Floor number, suite number, etc"); | |
1187 field.name = ASCIIToUTF16("address2"); | |
1188 form.fields.push_back(field); | |
1189 | |
1190 field.label = ASCIIToUTF16("City:"); | |
1191 field.name = ASCIIToUTF16("city"); | |
1192 form.fields.push_back(field); | |
1193 | |
1194 form_structure.reset(new FormStructure(form, std::string())); | |
1195 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1196 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1197 ASSERT_EQ(3U, form_structure->field_count()); | |
1198 ASSERT_EQ(3U, form_structure->autofill_count()); | |
1199 | |
1200 // Address Line 1. | |
1201 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); | |
1202 // Address Line 2. | |
1203 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); | |
1204 // City. | |
1205 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(2)->heuristic_type()); | |
1206 } | |
1207 | |
1208 TEST(FormStructureTest, HeuristicsStateWithProvince) { | |
1209 scoped_ptr<FormStructure> form_structure; | |
1210 FormData form; | |
1211 form.method = ASCIIToUTF16("post"); | |
1212 | |
1213 FormFieldData field; | |
1214 field.form_control_type = "text"; | |
1215 | |
1216 field.label = ASCIIToUTF16("Address Line1"); | |
1217 field.name = ASCIIToUTF16("Address"); | |
1218 form.fields.push_back(field); | |
1219 | |
1220 field.label = ASCIIToUTF16("Address Line2"); | |
1221 field.name = ASCIIToUTF16("Address"); | |
1222 form.fields.push_back(field); | |
1223 | |
1224 field.label = ASCIIToUTF16("State/Province/Region"); | |
1225 field.name = ASCIIToUTF16("State"); | |
1226 form.fields.push_back(field); | |
1227 | |
1228 form_structure.reset(new FormStructure(form, std::string())); | |
1229 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1230 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1231 ASSERT_EQ(3U, form_structure->field_count()); | |
1232 ASSERT_EQ(3U, form_structure->autofill_count()); | |
1233 | |
1234 // Address Line 1. | |
1235 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type()); | |
1236 // Address Line 2. | |
1237 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type()); | |
1238 // State. | |
1239 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(2)->heuristic_type()); | |
1240 } | |
1241 | |
1242 // This example comes from lego.com's checkout page. | |
1243 TEST(FormStructureTest, HeuristicsWithBilling) { | |
1244 scoped_ptr<FormStructure> form_structure; | |
1245 FormData form; | |
1246 form.method = ASCIIToUTF16("post"); | |
1247 | |
1248 FormFieldData field; | |
1249 field.form_control_type = "text"; | |
1250 | |
1251 field.label = ASCIIToUTF16("First Name*:"); | |
1252 field.name = ASCIIToUTF16("editBillingAddress$firstNameBox"); | |
1253 form.fields.push_back(field); | |
1254 | |
1255 field.label = ASCIIToUTF16("Last Name*:"); | |
1256 field.name = ASCIIToUTF16("editBillingAddress$lastNameBox"); | |
1257 form.fields.push_back(field); | |
1258 | |
1259 field.label = ASCIIToUTF16("Company Name:"); | |
1260 field.name = ASCIIToUTF16("editBillingAddress$companyBox"); | |
1261 form.fields.push_back(field); | |
1262 | |
1263 field.label = ASCIIToUTF16("Address*:"); | |
1264 field.name = ASCIIToUTF16("editBillingAddress$addressLine1Box"); | |
1265 form.fields.push_back(field); | |
1266 | |
1267 field.label = ASCIIToUTF16("Apt/Suite :"); | |
1268 field.name = ASCIIToUTF16("editBillingAddress$addressLine2Box"); | |
1269 form.fields.push_back(field); | |
1270 | |
1271 field.label = ASCIIToUTF16("City*:"); | |
1272 field.name = ASCIIToUTF16("editBillingAddress$cityBox"); | |
1273 form.fields.push_back(field); | |
1274 | |
1275 field.label = ASCIIToUTF16("State/Province*:"); | |
1276 field.name = ASCIIToUTF16("editBillingAddress$stateDropDown"); | |
1277 form.fields.push_back(field); | |
1278 | |
1279 field.label = ASCIIToUTF16("Country*:"); | |
1280 field.name = ASCIIToUTF16("editBillingAddress$countryDropDown"); | |
1281 form.fields.push_back(field); | |
1282 | |
1283 field.label = ASCIIToUTF16("Postal Code*:"); | |
1284 field.name = ASCIIToUTF16("editBillingAddress$zipCodeBox"); | |
1285 form.fields.push_back(field); | |
1286 | |
1287 field.label = ASCIIToUTF16("Phone*:"); | |
1288 field.name = ASCIIToUTF16("editBillingAddress$phoneBox"); | |
1289 form.fields.push_back(field); | |
1290 | |
1291 field.label = ASCIIToUTF16("Email Address*:"); | |
1292 field.name = ASCIIToUTF16("email$emailBox"); | |
1293 form.fields.push_back(field); | |
1294 | |
1295 form_structure.reset(new FormStructure(form, std::string())); | |
1296 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1297 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1298 ASSERT_EQ(11U, form_structure->field_count()); | |
1299 ASSERT_EQ(11U, form_structure->autofill_count()); | |
1300 | |
1301 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type()); | |
1302 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type()); | |
1303 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type()); | |
1304 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(3)->heuristic_type()); | |
1305 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(4)->heuristic_type()); | |
1306 EXPECT_EQ(ADDRESS_BILLING_CITY, form_structure->field(5)->heuristic_type()); | |
1307 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure->field(6)->heuristic_type()); | |
1308 EXPECT_EQ(ADDRESS_BILLING_COUNTRY, | |
1309 form_structure->field(7)->heuristic_type()); | |
1310 EXPECT_EQ(ADDRESS_BILLING_ZIP, form_structure->field(8)->heuristic_type()); | |
1311 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER, | |
1312 form_structure->field(9)->heuristic_type()); | |
1313 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(10)->heuristic_type()); | |
1314 } | |
1315 | |
1316 TEST(FormStructureTest, ThreePartPhoneNumber) { | |
1317 scoped_ptr<FormStructure> form_structure; | |
1318 FormData form; | |
1319 form.method = ASCIIToUTF16("post"); | |
1320 | |
1321 FormFieldData field; | |
1322 field.form_control_type = "text"; | |
1323 | |
1324 field.label = ASCIIToUTF16("Phone:"); | |
1325 field.name = ASCIIToUTF16("dayphone1"); | |
1326 field.max_length = 0; | |
1327 form.fields.push_back(field); | |
1328 | |
1329 field.label = ASCIIToUTF16("-"); | |
1330 field.name = ASCIIToUTF16("dayphone2"); | |
1331 field.max_length = 3; // Size of prefix is 3. | |
1332 form.fields.push_back(field); | |
1333 | |
1334 field.label = ASCIIToUTF16("-"); | |
1335 field.name = ASCIIToUTF16("dayphone3"); | |
1336 field.max_length = 4; // Size of suffix is 4. If unlimited size is | |
1337 // passed, phone will be parsed as | |
1338 // <country code> - <area code> - <phone>. | |
1339 form.fields.push_back(field); | |
1340 | |
1341 field.label = ASCIIToUTF16("ext.:"); | |
1342 field.name = ASCIIToUTF16("dayphone4"); | |
1343 field.max_length = 0; | |
1344 form.fields.push_back(field); | |
1345 | |
1346 form_structure.reset(new FormStructure(form, std::string())); | |
1347 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1348 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1349 ASSERT_EQ(4U, form_structure->field_count()); | |
1350 ASSERT_EQ(3U, form_structure->autofill_count()); | |
1351 | |
1352 // Area code. | |
1353 EXPECT_EQ(PHONE_HOME_CITY_CODE, form_structure->field(0)->heuristic_type()); | |
1354 // Phone number suffix. | |
1355 EXPECT_EQ(PHONE_HOME_NUMBER, | |
1356 form_structure->field(1)->heuristic_type()); | |
1357 // Phone number suffix. | |
1358 EXPECT_EQ(PHONE_HOME_NUMBER, | |
1359 form_structure->field(2)->heuristic_type()); | |
1360 // Unknown. | |
1361 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type()); | |
1362 } | |
1363 | |
1364 TEST(FormStructureTest, HeuristicsInfernoCC) { | |
1365 scoped_ptr<FormStructure> form_structure; | |
1366 FormData form; | |
1367 form.method = ASCIIToUTF16("post"); | |
1368 | |
1369 FormFieldData field; | |
1370 field.form_control_type = "text"; | |
1371 | |
1372 field.label = ASCIIToUTF16("Name on Card"); | |
1373 field.name = ASCIIToUTF16("name_on_card"); | |
1374 form.fields.push_back(field); | |
1375 | |
1376 field.label = ASCIIToUTF16("Address"); | |
1377 field.name = ASCIIToUTF16("billing_address"); | |
1378 form.fields.push_back(field); | |
1379 | |
1380 field.label = ASCIIToUTF16("Card Number"); | |
1381 field.name = ASCIIToUTF16("card_number"); | |
1382 form.fields.push_back(field); | |
1383 | |
1384 field.label = ASCIIToUTF16("Expiration Date"); | |
1385 field.name = ASCIIToUTF16("expiration_month"); | |
1386 form.fields.push_back(field); | |
1387 | |
1388 field.label = ASCIIToUTF16("Expiration Year"); | |
1389 field.name = ASCIIToUTF16("expiration_year"); | |
1390 form.fields.push_back(field); | |
1391 | |
1392 form_structure.reset(new FormStructure(form, std::string())); | |
1393 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1394 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1395 | |
1396 // Expect the correct number of fields. | |
1397 ASSERT_EQ(5U, form_structure->field_count()); | |
1398 EXPECT_EQ(5U, form_structure->autofill_count()); | |
1399 | |
1400 // Name on Card. | |
1401 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type()); | |
1402 // Address. | |
1403 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(1)->heuristic_type()); | |
1404 // Card Number. | |
1405 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type()); | |
1406 // Expiration Date. | |
1407 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); | |
1408 // Expiration Year. | |
1409 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, | |
1410 form_structure->field(4)->heuristic_type()); | |
1411 } | |
1412 | |
1413 TEST(FormStructureTest, CVCCodeClash) { | |
1414 scoped_ptr<FormStructure> form_structure; | |
1415 FormData form; | |
1416 form.method = ASCIIToUTF16("post"); | |
1417 | |
1418 FormFieldData field; | |
1419 field.form_control_type = "text"; | |
1420 | |
1421 field.label = ASCIIToUTF16("Card number"); | |
1422 field.name = ASCIIToUTF16("ccnumber"); | |
1423 form.fields.push_back(field); | |
1424 | |
1425 field.label = ASCIIToUTF16("First name"); | |
1426 field.name = ASCIIToUTF16("first_name"); | |
1427 form.fields.push_back(field); | |
1428 | |
1429 field.label = ASCIIToUTF16("Last name"); | |
1430 field.name = ASCIIToUTF16("last_name"); | |
1431 form.fields.push_back(field); | |
1432 | |
1433 field.label = ASCIIToUTF16("Expiration date"); | |
1434 field.name = ASCIIToUTF16("ccexpiresmonth"); | |
1435 form.fields.push_back(field); | |
1436 | |
1437 field.label = string16(); | |
1438 field.name = ASCIIToUTF16("ccexpiresyear"); | |
1439 form.fields.push_back(field); | |
1440 | |
1441 field.label = ASCIIToUTF16("cvc number"); | |
1442 field.name = ASCIIToUTF16("csc"); | |
1443 form.fields.push_back(field); | |
1444 | |
1445 form_structure.reset(new FormStructure(form, std::string())); | |
1446 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1447 EXPECT_TRUE(form_structure->IsAutofillable(true)); | |
1448 | |
1449 // Expect the correct number of fields. | |
1450 ASSERT_EQ(6U, form_structure->field_count()); | |
1451 ASSERT_EQ(5U, form_structure->autofill_count()); | |
1452 | |
1453 // Card Number. | |
1454 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(0)->heuristic_type()); | |
1455 // First name, taken as name on card. | |
1456 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(1)->heuristic_type()); | |
1457 // Last name is not merged. | |
1458 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type()); | |
1459 // Expiration Date. | |
1460 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type()); | |
1461 // Expiration Year. | |
1462 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR, | |
1463 form_structure->field(4)->heuristic_type()); | |
1464 // CVC code. | |
1465 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE, | |
1466 form_structure->field(5)->heuristic_type()); | |
1467 } | |
1468 | |
1469 TEST(FormStructureTest, EncodeQueryRequest) { | |
1470 FormData form; | |
1471 form.method = ASCIIToUTF16("post"); | |
1472 | |
1473 FormFieldData field; | |
1474 field.form_control_type = "text"; | |
1475 | |
1476 field.label = ASCIIToUTF16("Name on Card"); | |
1477 field.name = ASCIIToUTF16("name_on_card"); | |
1478 form.fields.push_back(field); | |
1479 | |
1480 field.label = ASCIIToUTF16("Address"); | |
1481 field.name = ASCIIToUTF16("billing_address"); | |
1482 form.fields.push_back(field); | |
1483 | |
1484 field.label = ASCIIToUTF16("Card Number"); | |
1485 field.name = ASCIIToUTF16("card_number"); | |
1486 form.fields.push_back(field); | |
1487 | |
1488 field.label = ASCIIToUTF16("Expiration Date"); | |
1489 field.name = ASCIIToUTF16("expiration_month"); | |
1490 form.fields.push_back(field); | |
1491 | |
1492 field.label = ASCIIToUTF16("Expiration Year"); | |
1493 field.name = ASCIIToUTF16("expiration_year"); | |
1494 form.fields.push_back(field); | |
1495 | |
1496 // Add checkable field. | |
1497 FormFieldData checkable_field; | |
1498 checkable_field.is_checkable = true; | |
1499 checkable_field.label = ASCIIToUTF16("Checkable1"); | |
1500 checkable_field.name = ASCIIToUTF16("Checkable1"); | |
1501 form.fields.push_back(checkable_field); | |
1502 | |
1503 ScopedVector<FormStructure> forms; | |
1504 forms.push_back(new FormStructure(form, std::string())); | |
1505 std::vector<std::string> encoded_signatures; | |
1506 std::string encoded_xml; | |
1507 const char * const kSignature1 = "11337937696949187602"; | |
1508 const char * const kResponse1 = | |
1509 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery " | |
1510 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form " | |
1511 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>" | |
1512 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" | |
1513 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>" | |
1514 "</autofillquery>"; | |
1515 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), | |
1516 &encoded_signatures, | |
1517 &encoded_xml)); | |
1518 ASSERT_EQ(1U, encoded_signatures.size()); | |
1519 EXPECT_EQ(kSignature1, encoded_signatures[0]); | |
1520 EXPECT_EQ(kResponse1, encoded_xml); | |
1521 | |
1522 // Add the same form, only one will be encoded, so EncodeQueryRequest() should | |
1523 // return the same data. | |
1524 forms.push_back(new FormStructure(form, std::string())); | |
1525 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), | |
1526 &encoded_signatures, | |
1527 &encoded_xml)); | |
1528 ASSERT_EQ(1U, encoded_signatures.size()); | |
1529 EXPECT_EQ(kSignature1, encoded_signatures[0]); | |
1530 EXPECT_EQ(kResponse1, encoded_xml); | |
1531 // Add 5 address fields - this should be still a valid form. | |
1532 for (size_t i = 0; i < 5; ++i) { | |
1533 field.label = ASCIIToUTF16("Address"); | |
1534 field.name = ASCIIToUTF16("address"); | |
1535 form.fields.push_back(field); | |
1536 } | |
1537 | |
1538 forms.push_back(new FormStructure(form, std::string())); | |
1539 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), | |
1540 &encoded_signatures, | |
1541 &encoded_xml)); | |
1542 ASSERT_EQ(2U, encoded_signatures.size()); | |
1543 EXPECT_EQ(kSignature1, encoded_signatures[0]); | |
1544 const char * const kSignature2 = "8308881815906226214"; | |
1545 EXPECT_EQ(kSignature2, encoded_signatures[1]); | |
1546 const char * const kResponse2 = | |
1547 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery " | |
1548 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form " | |
1549 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>" | |
1550 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" | |
1551 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>" | |
1552 "<form signature=\"8308881815906226214\"><field signature=\"412125936\"/>" | |
1553 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>" | |
1554 "<field signature=\"747221617\"/><field signature=\"4108155786\"/><field " | |
1555 "signature=\"509334676\"/><field signature=\"509334676\"/><field " | |
1556 "signature=\"509334676\"/><field signature=\"509334676\"/><field " | |
1557 "signature=\"509334676\"/></form></autofillquery>"; | |
1558 EXPECT_EQ(kResponse2, encoded_xml); | |
1559 | |
1560 FormData malformed_form(form); | |
1561 // Add 50 address fields - the form is not valid anymore, but previous ones | |
1562 // are. The result should be the same as in previous test. | |
1563 for (size_t i = 0; i < 50; ++i) { | |
1564 field.label = ASCIIToUTF16("Address"); | |
1565 field.name = ASCIIToUTF16("address"); | |
1566 malformed_form.fields.push_back(field); | |
1567 } | |
1568 | |
1569 forms.push_back(new FormStructure(malformed_form, std::string())); | |
1570 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(), | |
1571 &encoded_signatures, | |
1572 &encoded_xml)); | |
1573 ASSERT_EQ(2U, encoded_signatures.size()); | |
1574 EXPECT_EQ(kSignature1, encoded_signatures[0]); | |
1575 EXPECT_EQ(kSignature2, encoded_signatures[1]); | |
1576 EXPECT_EQ(kResponse2, encoded_xml); | |
1577 | |
1578 // Check that we fail if there are only bad form(s). | |
1579 ScopedVector<FormStructure> bad_forms; | |
1580 bad_forms.push_back(new FormStructure(malformed_form, std::string())); | |
1581 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(), | |
1582 &encoded_signatures, | |
1583 &encoded_xml)); | |
1584 EXPECT_EQ(0U, encoded_signatures.size()); | |
1585 EXPECT_EQ("", encoded_xml); | |
1586 | |
1587 // Check the behaviour with autocheckout enabled. | |
1588 ScopedVector<FormStructure> checkable_forms; | |
1589 checkable_forms.push_back( | |
1590 new FormStructure(form, "https://www.sample1.com/query/path")); | |
1591 | |
1592 ASSERT_TRUE(FormStructure::EncodeQueryRequest(checkable_forms.get(), | |
1593 &encoded_signatures, | |
1594 &encoded_xml)); | |
1595 const char * const kSignature3 = "7747357776717901584"; | |
1596 const char * const kResponse3 = | |
1597 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><autofillquery " | |
1598 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"a,e\" " | |
1599 "urlprefixsignature=\"7648393911063090788\">" | |
1600 "<form signature=\"7747357776717901584\">" | |
1601 "<field signature=\"412125936\"/>" | |
1602 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/><field" | |
1603 " signature=\"747221617\"/><field signature=\"4108155786\"/><field " | |
1604 "signature=\"3410250678\"/><field signature=\"509334676\"/><field " | |
1605 "signature=\"509334676\"/><field signature=\"509334676\"/><field " | |
1606 "signature=\"509334676\"/><field signature=\"509334676\"/></form>" | |
1607 "</autofillquery>"; | |
1608 ASSERT_EQ(1U, encoded_signatures.size()); | |
1609 EXPECT_EQ(kSignature3, encoded_signatures[0]); | |
1610 EXPECT_EQ(kResponse3, encoded_xml); | |
1611 } | |
1612 | |
1613 TEST(FormStructureTest, EncodeUploadRequest) { | |
1614 scoped_ptr<FormStructure> form_structure; | |
1615 std::vector<FieldTypeSet> possible_field_types; | |
1616 FormData form; | |
1617 form.method = ASCIIToUTF16("post"); | |
1618 form_structure.reset(new FormStructure(form, std::string())); | |
1619 form_structure->DetermineHeuristicTypes(TestAutofillMetrics()); | |
1620 | |
1621 FormFieldData field; | |
1622 field.form_control_type = "text"; | |
1623 | |
1624 field.label = ASCIIToUTF16("First Name"); | |
1625 field.name = ASCIIToUTF16("firstname"); | |
1626 form.fields.push_back(field); | |
1627 possible_field_types.push_back(FieldTypeSet()); | |
1628 possible_field_types.back().insert(NAME_FIRST); | |
1629 | |
1630 field.label = ASCIIToUTF16("Last Name"); | |
1631 field.name = ASCIIToUTF16("lastname"); | |
1632 form.fields.push_back(field); | |
1633 possible_field_types.push_back(FieldTypeSet()); | |
1634 possible_field_types.back().insert(NAME_LAST); | |
1635 | |
1636 field.label = ASCIIToUTF16("Email"); | |
1637 field.name = ASCIIToUTF16("email"); | |
1638 field.form_control_type = "email"; | |
1639 form.fields.push_back(field); | |
1640 possible_field_types.push_back(FieldTypeSet()); | |
1641 possible_field_types.back().insert(EMAIL_ADDRESS); | |
1642 | |
1643 field.label = ASCIIToUTF16("Phone"); | |
1644 field.name = ASCIIToUTF16("phone"); | |
1645 field.form_control_type = "number"; | |
1646 form.fields.push_back(field); | |
1647 possible_field_types.push_back(FieldTypeSet()); | |
1648 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER); | |
1649 | |
1650 field.label = ASCIIToUTF16("Country"); | |
1651 field.name = ASCIIToUTF16("country"); | |
1652 field.form_control_type = "select-one"; | |
1653 form.fields.push_back(field); | |
1654 possible_field_types.push_back(FieldTypeSet()); | |
1655 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); | |
1656 | |
1657 // Add checkable field. | |
1658 FormFieldData checkable_field; | |
1659 checkable_field.is_checkable = true; | |
1660 checkable_field.label = ASCIIToUTF16("Checkable1"); | |
1661 checkable_field.name = ASCIIToUTF16("Checkable1"); | |
1662 form.fields.push_back(checkable_field); | |
1663 possible_field_types.push_back(FieldTypeSet()); | |
1664 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY); | |
1665 | |
1666 form_structure.reset(new FormStructure(form, std::string())); | |
1667 | |
1668 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); | |
1669 for (size_t i = 0; i < form_structure->field_count(); ++i) | |
1670 form_structure->field(i)->set_possible_types(possible_field_types[i]); | |
1671 | |
1672 FieldTypeSet available_field_types; | |
1673 available_field_types.insert(NAME_FIRST); | |
1674 available_field_types.insert(NAME_LAST); | |
1675 available_field_types.insert(ADDRESS_HOME_LINE1); | |
1676 available_field_types.insert(ADDRESS_HOME_LINE2); | |
1677 available_field_types.insert(ADDRESS_HOME_COUNTRY); | |
1678 available_field_types.insert(ADDRESS_BILLING_LINE1); | |
1679 available_field_types.insert(ADDRESS_BILLING_LINE2); | |
1680 available_field_types.insert(EMAIL_ADDRESS); | |
1681 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); | |
1682 | |
1683 std::string encoded_xml; | |
1684 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, | |
1685 &encoded_xml)); | |
1686 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
1687 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " | |
1688 "formsignature=\"8736493185895608956\" autofillused=\"false\" " | |
1689 "datapresent=\"144200030e\">" | |
1690 "<field signature=\"3763331450\" autofilltype=\"3\"/>" | |
1691 "<field signature=\"3494530716\" autofilltype=\"5\"/>" | |
1692 "<field signature=\"1029417091\" autofilltype=\"9\"/>" | |
1693 "<field signature=\"466116101\" autofilltype=\"14\"/>" | |
1694 "<field signature=\"2799270304\" autofilltype=\"36\"/>" | |
1695 "</autofillupload>", | |
1696 encoded_xml); | |
1697 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true, | |
1698 &encoded_xml)); | |
1699 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
1700 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " | |
1701 "formsignature=\"8736493185895608956\" autofillused=\"true\" " | |
1702 "datapresent=\"144200030e\">" | |
1703 "<field signature=\"3763331450\" autofilltype=\"3\"/>" | |
1704 "<field signature=\"3494530716\" autofilltype=\"5\"/>" | |
1705 "<field signature=\"1029417091\" autofilltype=\"9\"/>" | |
1706 "<field signature=\"466116101\" autofilltype=\"14\"/>" | |
1707 "<field signature=\"2799270304\" autofilltype=\"36\"/>" | |
1708 "</autofillupload>", | |
1709 encoded_xml); | |
1710 | |
1711 // Add 2 address fields - this should be still a valid form. | |
1712 for (size_t i = 0; i < 2; ++i) { | |
1713 field.label = ASCIIToUTF16("Address"); | |
1714 field.name = ASCIIToUTF16("address"); | |
1715 field.form_control_type = "text"; | |
1716 form.fields.push_back(field); | |
1717 possible_field_types.push_back(FieldTypeSet()); | |
1718 possible_field_types.back().insert(ADDRESS_HOME_LINE1); | |
1719 possible_field_types.back().insert(ADDRESS_HOME_LINE2); | |
1720 possible_field_types.back().insert(ADDRESS_BILLING_LINE1); | |
1721 possible_field_types.back().insert(ADDRESS_BILLING_LINE2); | |
1722 } | |
1723 | |
1724 form_structure.reset(new FormStructure(form, std::string())); | |
1725 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); | |
1726 for (size_t i = 0; i < form_structure->field_count(); ++i) | |
1727 form_structure->field(i)->set_possible_types(possible_field_types[i]); | |
1728 | |
1729 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, | |
1730 &encoded_xml)); | |
1731 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
1732 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" " | |
1733 "formsignature=\"7816485729218079147\" autofillused=\"false\" " | |
1734 "datapresent=\"144200030e\">" | |
1735 "<field signature=\"3763331450\" autofilltype=\"3\"/>" | |
1736 "<field signature=\"3494530716\" autofilltype=\"5\"/>" | |
1737 "<field signature=\"1029417091\" autofilltype=\"9\"/>" | |
1738 "<field signature=\"466116101\" autofilltype=\"14\"/>" | |
1739 "<field signature=\"2799270304\" autofilltype=\"36\"/>" | |
1740 "<field signature=\"509334676\" autofilltype=\"30\"/>" | |
1741 "<field signature=\"509334676\" autofilltype=\"31\"/>" | |
1742 "<field signature=\"509334676\" autofilltype=\"37\"/>" | |
1743 "<field signature=\"509334676\" autofilltype=\"38\"/>" | |
1744 "<field signature=\"509334676\" autofilltype=\"30\"/>" | |
1745 "<field signature=\"509334676\" autofilltype=\"31\"/>" | |
1746 "<field signature=\"509334676\" autofilltype=\"37\"/>" | |
1747 "<field signature=\"509334676\" autofilltype=\"38\"/>" | |
1748 "</autofillupload>", | |
1749 encoded_xml); | |
1750 | |
1751 // Add 50 address fields - now the form is invalid, as it has too many fields. | |
1752 for (size_t i = 0; i < 50; ++i) { | |
1753 field.label = ASCIIToUTF16("Address"); | |
1754 field.name = ASCIIToUTF16("address"); | |
1755 field.form_control_type = "text"; | |
1756 form.fields.push_back(field); | |
1757 possible_field_types.push_back(FieldTypeSet()); | |
1758 possible_field_types.back().insert(ADDRESS_HOME_LINE1); | |
1759 possible_field_types.back().insert(ADDRESS_HOME_LINE2); | |
1760 possible_field_types.back().insert(ADDRESS_BILLING_LINE1); | |
1761 possible_field_types.back().insert(ADDRESS_BILLING_LINE2); | |
1762 } | |
1763 form_structure.reset(new FormStructure(form, std::string())); | |
1764 ASSERT_EQ(form_structure->field_count(), possible_field_types.size()); | |
1765 for (size_t i = 0; i < form_structure->field_count(); ++i) | |
1766 form_structure->field(i)->set_possible_types(possible_field_types[i]); | |
1767 EXPECT_FALSE(form_structure->EncodeUploadRequest(available_field_types, false, | |
1768 &encoded_xml)); | |
1769 } | |
1770 | |
1771 // Check that we compute the "datapresent" string correctly for the given | |
1772 // |available_types|. | |
1773 TEST(FormStructureTest, CheckDataPresence) { | |
1774 FormData form; | |
1775 form.method = ASCIIToUTF16("post"); | |
1776 | |
1777 FormFieldData field; | |
1778 field.form_control_type = "text"; | |
1779 | |
1780 field.label = ASCIIToUTF16("First Name"); | |
1781 field.name = ASCIIToUTF16("first"); | |
1782 form.fields.push_back(field); | |
1783 | |
1784 field.label = ASCIIToUTF16("Last Name"); | |
1785 field.name = ASCIIToUTF16("last"); | |
1786 form.fields.push_back(field); | |
1787 | |
1788 field.label = ASCIIToUTF16("Email"); | |
1789 field.name = ASCIIToUTF16("email"); | |
1790 form.fields.push_back(field); | |
1791 | |
1792 FormStructure form_structure(form, std::string()); | |
1793 | |
1794 FieldTypeSet unknown_type; | |
1795 unknown_type.insert(UNKNOWN_TYPE); | |
1796 for (size_t i = 0; i < form_structure.field_count(); ++i) | |
1797 form_structure.field(i)->set_possible_types(unknown_type); | |
1798 | |
1799 // No available types. | |
1800 // datapresent should be "" == trimmmed(0x0000000000000000) == | |
1801 // 0b0000000000000000000000000000000000000000000000000000000000000000 | |
1802 FieldTypeSet available_field_types; | |
1803 | |
1804 std::string encoded_xml; | |
1805 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, | |
1806 &encoded_xml)); | |
1807 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
1808 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
1809 " formsignature=\"6402244543831589061\" autofillused=\"false\"" | |
1810 " datapresent=\"\">" | |
1811 "<field signature=\"1089846351\" autofilltype=\"1\"/>" | |
1812 "<field signature=\"2404144663\" autofilltype=\"1\"/>" | |
1813 "<field signature=\"420638584\" autofilltype=\"1\"/>" | |
1814 "</autofillupload>", | |
1815 encoded_xml); | |
1816 | |
1817 // Only a few types available. | |
1818 // datapresent should be "1540000240" == trimmmed(0x1540000240000000) == | |
1819 // 0b0001010101000000000000000000001001000000000000000000000000000000 | |
1820 // The set bits are: | |
1821 // 3 == NAME_FIRST | |
1822 // 5 == NAME_LAST | |
1823 // 7 == NAME_FULL | |
1824 // 9 == EMAIL_ADDRESS | |
1825 // 30 == ADDRESS_HOME_LINE1 | |
1826 // 33 == ADDRESS_HOME_CITY | |
1827 available_field_types.clear(); | |
1828 available_field_types.insert(NAME_FIRST); | |
1829 available_field_types.insert(NAME_LAST); | |
1830 available_field_types.insert(NAME_FULL); | |
1831 available_field_types.insert(EMAIL_ADDRESS); | |
1832 available_field_types.insert(ADDRESS_HOME_LINE1); | |
1833 available_field_types.insert(ADDRESS_HOME_CITY); | |
1834 | |
1835 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, | |
1836 &encoded_xml)); | |
1837 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
1838 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
1839 " formsignature=\"6402244543831589061\" autofillused=\"false\"" | |
1840 " datapresent=\"1540000240\">" | |
1841 "<field signature=\"1089846351\" autofilltype=\"1\"/>" | |
1842 "<field signature=\"2404144663\" autofilltype=\"1\"/>" | |
1843 "<field signature=\"420638584\" autofilltype=\"1\"/>" | |
1844 "</autofillupload>", | |
1845 encoded_xml); | |
1846 | |
1847 // All supported non-credit card types available. | |
1848 // datapresent should be "1f7e000378000008" == trimmmed(0x1f7e000378000008) == | |
1849 // 0b0001111101111110000000000000001101111000000000000000000000001000 | |
1850 // The set bits are: | |
1851 // 3 == NAME_FIRST | |
1852 // 4 == NAME_MIDDLE | |
1853 // 5 == NAME_LAST | |
1854 // 6 == NAME_MIDDLE_INITIAL | |
1855 // 7 == NAME_FULL | |
1856 // 9 == EMAIL_ADDRESS | |
1857 // 10 == PHONE_HOME_NUMBER, | |
1858 // 11 == PHONE_HOME_CITY_CODE, | |
1859 // 12 == PHONE_HOME_COUNTRY_CODE, | |
1860 // 13 == PHONE_HOME_CITY_AND_NUMBER, | |
1861 // 14 == PHONE_HOME_WHOLE_NUMBER, | |
1862 // 30 == ADDRESS_HOME_LINE1 | |
1863 // 31 == ADDRESS_HOME_LINE2 | |
1864 // 33 == ADDRESS_HOME_CITY | |
1865 // 34 == ADDRESS_HOME_STATE | |
1866 // 35 == ADDRESS_HOME_ZIP | |
1867 // 36 == ADDRESS_HOME_COUNTRY | |
1868 // 60 == COMPANY_NAME | |
1869 available_field_types.clear(); | |
1870 available_field_types.insert(NAME_FIRST); | |
1871 available_field_types.insert(NAME_MIDDLE); | |
1872 available_field_types.insert(NAME_LAST); | |
1873 available_field_types.insert(NAME_MIDDLE_INITIAL); | |
1874 available_field_types.insert(NAME_FULL); | |
1875 available_field_types.insert(EMAIL_ADDRESS); | |
1876 available_field_types.insert(PHONE_HOME_NUMBER); | |
1877 available_field_types.insert(PHONE_HOME_CITY_CODE); | |
1878 available_field_types.insert(PHONE_HOME_COUNTRY_CODE); | |
1879 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER); | |
1880 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); | |
1881 available_field_types.insert(ADDRESS_HOME_LINE1); | |
1882 available_field_types.insert(ADDRESS_HOME_LINE2); | |
1883 available_field_types.insert(ADDRESS_HOME_CITY); | |
1884 available_field_types.insert(ADDRESS_HOME_STATE); | |
1885 available_field_types.insert(ADDRESS_HOME_ZIP); | |
1886 available_field_types.insert(ADDRESS_HOME_COUNTRY); | |
1887 available_field_types.insert(COMPANY_NAME); | |
1888 | |
1889 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, | |
1890 &encoded_xml)); | |
1891 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
1892 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
1893 " formsignature=\"6402244543831589061\" autofillused=\"false\"" | |
1894 " datapresent=\"1f7e000378000008\">" | |
1895 "<field signature=\"1089846351\" autofilltype=\"1\"/>" | |
1896 "<field signature=\"2404144663\" autofilltype=\"1\"/>" | |
1897 "<field signature=\"420638584\" autofilltype=\"1\"/>" | |
1898 "</autofillupload>", | |
1899 encoded_xml); | |
1900 | |
1901 // All supported credit card types available. | |
1902 // datapresent should be "0000000000001fc0" == trimmmed(0x0000000000001fc0) == | |
1903 // 0b0000000000000000000000000000000000000000000000000001111111000000 | |
1904 // The set bits are: | |
1905 // 51 == CREDIT_CARD_NAME | |
1906 // 52 == CREDIT_CARD_NUMBER | |
1907 // 53 == CREDIT_CARD_EXP_MONTH | |
1908 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR | |
1909 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR | |
1910 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR | |
1911 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR | |
1912 available_field_types.clear(); | |
1913 available_field_types.insert(CREDIT_CARD_NAME); | |
1914 available_field_types.insert(CREDIT_CARD_NUMBER); | |
1915 available_field_types.insert(CREDIT_CARD_EXP_MONTH); | |
1916 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); | |
1917 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); | |
1918 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); | |
1919 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); | |
1920 | |
1921 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, | |
1922 &encoded_xml)); | |
1923 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
1924 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
1925 " formsignature=\"6402244543831589061\" autofillused=\"false\"" | |
1926 " datapresent=\"0000000000001fc0\">" | |
1927 "<field signature=\"1089846351\" autofilltype=\"1\"/>" | |
1928 "<field signature=\"2404144663\" autofilltype=\"1\"/>" | |
1929 "<field signature=\"420638584\" autofilltype=\"1\"/>" | |
1930 "</autofillupload>", | |
1931 encoded_xml); | |
1932 | |
1933 // All supported types available. | |
1934 // datapresent should be "1f7e000378001fc8" == trimmmed(0x1f7e000378001fc8) == | |
1935 // 0b0001111101111110000000000000001101111000000000000001111111001000 | |
1936 // The set bits are: | |
1937 // 3 == NAME_FIRST | |
1938 // 4 == NAME_MIDDLE | |
1939 // 5 == NAME_LAST | |
1940 // 6 == NAME_MIDDLE_INITIAL | |
1941 // 7 == NAME_FULL | |
1942 // 9 == EMAIL_ADDRESS | |
1943 // 10 == PHONE_HOME_NUMBER, | |
1944 // 11 == PHONE_HOME_CITY_CODE, | |
1945 // 12 == PHONE_HOME_COUNTRY_CODE, | |
1946 // 13 == PHONE_HOME_CITY_AND_NUMBER, | |
1947 // 14 == PHONE_HOME_WHOLE_NUMBER, | |
1948 // 30 == ADDRESS_HOME_LINE1 | |
1949 // 31 == ADDRESS_HOME_LINE2 | |
1950 // 33 == ADDRESS_HOME_CITY | |
1951 // 34 == ADDRESS_HOME_STATE | |
1952 // 35 == ADDRESS_HOME_ZIP | |
1953 // 36 == ADDRESS_HOME_COUNTRY | |
1954 // 51 == CREDIT_CARD_NAME | |
1955 // 52 == CREDIT_CARD_NUMBER | |
1956 // 53 == CREDIT_CARD_EXP_MONTH | |
1957 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR | |
1958 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR | |
1959 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR | |
1960 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR | |
1961 // 60 == COMPANY_NAME | |
1962 available_field_types.clear(); | |
1963 available_field_types.insert(NAME_FIRST); | |
1964 available_field_types.insert(NAME_MIDDLE); | |
1965 available_field_types.insert(NAME_LAST); | |
1966 available_field_types.insert(NAME_MIDDLE_INITIAL); | |
1967 available_field_types.insert(NAME_FULL); | |
1968 available_field_types.insert(EMAIL_ADDRESS); | |
1969 available_field_types.insert(PHONE_HOME_NUMBER); | |
1970 available_field_types.insert(PHONE_HOME_CITY_CODE); | |
1971 available_field_types.insert(PHONE_HOME_COUNTRY_CODE); | |
1972 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER); | |
1973 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER); | |
1974 available_field_types.insert(ADDRESS_HOME_LINE1); | |
1975 available_field_types.insert(ADDRESS_HOME_LINE2); | |
1976 available_field_types.insert(ADDRESS_HOME_CITY); | |
1977 available_field_types.insert(ADDRESS_HOME_STATE); | |
1978 available_field_types.insert(ADDRESS_HOME_ZIP); | |
1979 available_field_types.insert(ADDRESS_HOME_COUNTRY); | |
1980 available_field_types.insert(CREDIT_CARD_NAME); | |
1981 available_field_types.insert(CREDIT_CARD_NUMBER); | |
1982 available_field_types.insert(CREDIT_CARD_EXP_MONTH); | |
1983 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR); | |
1984 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR); | |
1985 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR); | |
1986 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR); | |
1987 available_field_types.insert(COMPANY_NAME); | |
1988 | |
1989 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false, | |
1990 &encoded_xml)); | |
1991 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
1992 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
1993 " formsignature=\"6402244543831589061\" autofillused=\"false\"" | |
1994 " datapresent=\"1f7e000378001fc8\">" | |
1995 "<field signature=\"1089846351\" autofilltype=\"1\"/>" | |
1996 "<field signature=\"2404144663\" autofilltype=\"1\"/>" | |
1997 "<field signature=\"420638584\" autofilltype=\"1\"/>" | |
1998 "</autofillupload>", | |
1999 encoded_xml); | |
2000 } | |
2001 | |
2002 TEST(FormStructureTest, CheckMultipleTypes) { | |
2003 // Throughout this test, datapresent should be | |
2004 // 0x1440000360000008 == | |
2005 // 0b0001010001000000000000000000001101100000000000000000000000001000 | |
2006 // The set bits are: | |
2007 // 3 == NAME_FIRST | |
2008 // 5 == NAME_LAST | |
2009 // 9 == EMAIL_ADDRESS | |
2010 // 30 == ADDRESS_HOME_LINE1 | |
2011 // 31 == ADDRESS_HOME_LINE2 | |
2012 // 33 == ADDRESS_HOME_CITY | |
2013 // 34 == ADDRESS_HOME_STATE | |
2014 // 60 == COMPANY_NAME | |
2015 FieldTypeSet available_field_types; | |
2016 available_field_types.insert(NAME_FIRST); | |
2017 available_field_types.insert(NAME_LAST); | |
2018 available_field_types.insert(EMAIL_ADDRESS); | |
2019 available_field_types.insert(ADDRESS_HOME_LINE1); | |
2020 available_field_types.insert(ADDRESS_HOME_LINE2); | |
2021 available_field_types.insert(ADDRESS_HOME_CITY); | |
2022 available_field_types.insert(ADDRESS_HOME_STATE); | |
2023 available_field_types.insert(COMPANY_NAME); | |
2024 | |
2025 // Check that multiple types for the field are processed correctly. | |
2026 scoped_ptr<FormStructure> form_structure; | |
2027 std::vector<FieldTypeSet> possible_field_types; | |
2028 FormData form; | |
2029 form.method = ASCIIToUTF16("post"); | |
2030 | |
2031 FormFieldData field; | |
2032 field.form_control_type = "text"; | |
2033 | |
2034 field.label = ASCIIToUTF16("email"); | |
2035 field.name = ASCIIToUTF16("email"); | |
2036 form.fields.push_back(field); | |
2037 possible_field_types.push_back(FieldTypeSet()); | |
2038 possible_field_types.back().insert(EMAIL_ADDRESS); | |
2039 | |
2040 field.label = ASCIIToUTF16("First Name"); | |
2041 field.name = ASCIIToUTF16("first"); | |
2042 form.fields.push_back(field); | |
2043 possible_field_types.push_back(FieldTypeSet()); | |
2044 possible_field_types.back().insert(NAME_FIRST); | |
2045 | |
2046 field.label = ASCIIToUTF16("Last Name"); | |
2047 field.name = ASCIIToUTF16("last"); | |
2048 form.fields.push_back(field); | |
2049 possible_field_types.push_back(FieldTypeSet()); | |
2050 possible_field_types.back().insert(NAME_LAST); | |
2051 | |
2052 field.label = ASCIIToUTF16("Address"); | |
2053 field.name = ASCIIToUTF16("address"); | |
2054 form.fields.push_back(field); | |
2055 possible_field_types.push_back(FieldTypeSet()); | |
2056 possible_field_types.back().insert(ADDRESS_HOME_LINE1); | |
2057 | |
2058 form_structure.reset(new FormStructure(form, std::string())); | |
2059 | |
2060 for (size_t i = 0; i < form_structure->field_count(); ++i) | |
2061 form_structure->field(i)->set_possible_types(possible_field_types[i]); | |
2062 std::string encoded_xml; | |
2063 | |
2064 // Now we matched both fields singularly. | |
2065 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, | |
2066 &encoded_xml)); | |
2067 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
2068 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
2069 " formsignature=\"18062476096658145866\" autofillused=\"false\"" | |
2070 " datapresent=\"1440000360000008\">" | |
2071 "<field signature=\"420638584\" autofilltype=\"9\"/>" | |
2072 "<field signature=\"1089846351\" autofilltype=\"3\"/>" | |
2073 "<field signature=\"2404144663\" autofilltype=\"5\"/>" | |
2074 "<field signature=\"509334676\" autofilltype=\"30\"/>" | |
2075 "</autofillupload>", | |
2076 encoded_xml); | |
2077 // Match third field as both first and last. | |
2078 possible_field_types[2].insert(NAME_FIRST); | |
2079 form_structure->field(2)->set_possible_types(possible_field_types[2]); | |
2080 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, | |
2081 &encoded_xml)); | |
2082 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
2083 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
2084 " formsignature=\"18062476096658145866\" autofillused=\"false\"" | |
2085 " datapresent=\"1440000360000008\">" | |
2086 "<field signature=\"420638584\" autofilltype=\"9\"/>" | |
2087 "<field signature=\"1089846351\" autofilltype=\"3\"/>" | |
2088 "<field signature=\"2404144663\" autofilltype=\"3\"/>" | |
2089 "<field signature=\"2404144663\" autofilltype=\"5\"/>" | |
2090 "<field signature=\"509334676\" autofilltype=\"30\"/>" | |
2091 "</autofillupload>", | |
2092 encoded_xml); | |
2093 possible_field_types[3].insert(ADDRESS_HOME_LINE2); | |
2094 form_structure->field(form_structure->field_count() - 1)->set_possible_types( | |
2095 possible_field_types[form_structure->field_count() - 1]); | |
2096 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, | |
2097 &encoded_xml)); | |
2098 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
2099 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
2100 " formsignature=\"18062476096658145866\" autofillused=\"false\"" | |
2101 " datapresent=\"1440000360000008\">" | |
2102 "<field signature=\"420638584\" autofilltype=\"9\"/>" | |
2103 "<field signature=\"1089846351\" autofilltype=\"3\"/>" | |
2104 "<field signature=\"2404144663\" autofilltype=\"3\"/>" | |
2105 "<field signature=\"2404144663\" autofilltype=\"5\"/>" | |
2106 "<field signature=\"509334676\" autofilltype=\"30\"/>" | |
2107 "<field signature=\"509334676\" autofilltype=\"31\"/>" | |
2108 "</autofillupload>", | |
2109 encoded_xml); | |
2110 possible_field_types[3].clear(); | |
2111 possible_field_types[3].insert(ADDRESS_HOME_LINE1); | |
2112 possible_field_types[3].insert(COMPANY_NAME); | |
2113 form_structure->field(form_structure->field_count() - 1)->set_possible_types( | |
2114 possible_field_types[form_structure->field_count() - 1]); | |
2115 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false, | |
2116 &encoded_xml)); | |
2117 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>" | |
2118 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\"" | |
2119 " formsignature=\"18062476096658145866\" autofillused=\"false\"" | |
2120 " datapresent=\"1440000360000008\">" | |
2121 "<field signature=\"420638584\" autofilltype=\"9\"/>" | |
2122 "<field signature=\"1089846351\" autofilltype=\"3\"/>" | |
2123 "<field signature=\"2404144663\" autofilltype=\"3\"/>" | |
2124 "<field signature=\"2404144663\" autofilltype=\"5\"/>" | |
2125 "<field signature=\"509334676\" autofilltype=\"30\"/>" | |
2126 "<field signature=\"509334676\" autofilltype=\"60\"/>" | |
2127 "</autofillupload>", | |
2128 encoded_xml); | |
2129 } | |
2130 | |
2131 TEST(FormStructureTest, CheckFormSignature) { | |
2132 // Check that form signature is created correctly. | |
2133 scoped_ptr<FormStructure> form_structure; | |
2134 FormData form; | |
2135 form.method = ASCIIToUTF16("post"); | |
2136 | |
2137 FormFieldData field; | |
2138 field.form_control_type = "text"; | |
2139 | |
2140 field.label = ASCIIToUTF16("email"); | |
2141 field.name = ASCIIToUTF16("email"); | |
2142 form.fields.push_back(field); | |
2143 | |
2144 field.label = ASCIIToUTF16("First Name"); | |
2145 field.name = ASCIIToUTF16("first"); | |
2146 form.fields.push_back(field); | |
2147 | |
2148 form_structure.reset(new FormStructure(form, std::string())); | |
2149 | |
2150 EXPECT_EQ(FormStructureTest::Hash64Bit( | |
2151 std::string("://&&email&first")), | |
2152 form_structure->FormSignature()); | |
2153 | |
2154 form.origin = GURL(std::string("http://www.facebook.com")); | |
2155 form_structure.reset(new FormStructure(form, std::string())); | |
2156 EXPECT_EQ(FormStructureTest::Hash64Bit( | |
2157 std::string("http://www.facebook.com&&email&first")), | |
2158 form_structure->FormSignature()); | |
2159 | |
2160 form.action = GURL(std::string("https://login.facebook.com/path")); | |
2161 form_structure.reset(new FormStructure(form, std::string())); | |
2162 EXPECT_EQ(FormStructureTest::Hash64Bit( | |
2163 std::string("https://login.facebook.com&&email&first")), | |
2164 form_structure->FormSignature()); | |
2165 | |
2166 form.name = ASCIIToUTF16("login_form"); | |
2167 form_structure.reset(new FormStructure(form, std::string())); | |
2168 EXPECT_EQ(FormStructureTest::Hash64Bit( | |
2169 std::string("https://login.facebook.com&login_form&email&first")), | |
2170 form_structure->FormSignature()); | |
2171 } | |
2172 | |
2173 TEST(FormStructureTest, ToFormData) { | |
2174 FormData form; | |
2175 form.name = ASCIIToUTF16("the-name"); | |
2176 form.method = ASCIIToUTF16("POST"); | |
2177 form.origin = GURL("http://cool.com"); | |
2178 form.action = form.origin.Resolve("/login"); | |
2179 | |
2180 FormFieldData field; | |
2181 field.label = ASCIIToUTF16("username"); | |
2182 field.name = ASCIIToUTF16("username"); | |
2183 field.form_control_type = "text"; | |
2184 form.fields.push_back(field); | |
2185 | |
2186 field.label = ASCIIToUTF16("password"); | |
2187 field.name = ASCIIToUTF16("password"); | |
2188 field.form_control_type = "password"; | |
2189 form.fields.push_back(field); | |
2190 | |
2191 field.label = string16(); | |
2192 field.name = ASCIIToUTF16("Submit"); | |
2193 field.form_control_type = "submit"; | |
2194 form.fields.push_back(field); | |
2195 | |
2196 EXPECT_EQ(form, FormStructure(form, std::string()).ToFormData()); | |
2197 | |
2198 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always | |
2199 // false. This forces a future author that changes this to update this test. | |
2200 form.user_submitted = true; | |
2201 EXPECT_NE(form, FormStructure(form, std::string()).ToFormData()); | |
2202 } | |
OLD | NEW |