Revert of Add address3 autofill heuristics. (patchset #6 id:100001 of https://coderev...
[chromium-blink-merge.git] / components / autofill / core / browser / form_structure_unittest.cc
blob6ff05f0eca2eea16580db21c0e87430fee8fae7d
1 // Copyright 2013 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.
5 #include "components/autofill/core/browser/form_structure.h"
7 #include "base/command_line.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/browser/autofill_metrics.h"
12 #include "components/autofill/core/common/autofill_switches.h"
13 #include "components/autofill/core/common/form_data.h"
14 #include "components/autofill/core/common/form_field_data.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h"
18 using base::ASCIIToUTF16;
20 namespace autofill {
21 namespace {
23 // Unlike the base AutofillMetrics, exposes copy and assignment constructors,
24 // which are handy for briefer test code. The AutofillMetrics class is
25 // stateless, so this is safe.
26 class TestAutofillMetrics : public AutofillMetrics {
27 public:
28 TestAutofillMetrics() {}
29 ~TestAutofillMetrics() override {}
32 } // anonymous namespace
35 namespace content {
37 std::ostream& operator<<(std::ostream& os, const FormData& form) {
38 os << base::UTF16ToUTF8(form.name)
39 << " "
40 << form.origin.spec()
41 << " "
42 << form.action.spec()
43 << " ";
45 for (std::vector<FormFieldData>::const_iterator iter =
46 form.fields.begin();
47 iter != form.fields.end(); ++iter) {
48 os << *iter
49 << " ";
52 return os;
55 } // namespace content
57 class FormStructureTest {
58 public:
59 static std::string Hash64Bit(const std::string& str) {
60 return FormStructure::Hash64Bit(str);
64 TEST(FormStructureTest, FieldCount) {
65 scoped_ptr<FormStructure> form_structure;
66 FormData form;
68 FormFieldData field;
69 field.label = ASCIIToUTF16("username");
70 field.name = ASCIIToUTF16("username");
71 field.form_control_type = "text";
72 form.fields.push_back(field);
74 field.label = ASCIIToUTF16("password");
75 field.name = ASCIIToUTF16("password");
76 field.form_control_type = "password";
77 form.fields.push_back(field);
79 field.label = base::string16();
80 field.name = ASCIIToUTF16("Submit");
81 field.form_control_type = "submit";
82 form.fields.push_back(field);
84 field.label = ASCIIToUTF16("address1");
85 field.name = ASCIIToUTF16("address1");
86 field.form_control_type = "text";
87 field.should_autocomplete = false;
88 form.fields.push_back(field);
90 // The render process sends all fields to browser including fields with
91 // autocomplete=off
92 form_structure.reset(new FormStructure(form));
93 EXPECT_EQ(4U, form_structure->field_count());
96 TEST(FormStructureTest, AutofillCount) {
97 scoped_ptr<FormStructure> form_structure;
98 FormData form;
100 FormFieldData field;
101 field.label = ASCIIToUTF16("username");
102 field.name = ASCIIToUTF16("username");
103 field.form_control_type = "text";
104 form.fields.push_back(field);
106 field.label = ASCIIToUTF16("password");
107 field.name = ASCIIToUTF16("password");
108 field.form_control_type = "password";
109 form.fields.push_back(field);
111 field.label = ASCIIToUTF16("state");
112 field.name = ASCIIToUTF16("state");
113 field.form_control_type = "select-one";
114 form.fields.push_back(field);
116 field.label = base::string16();
117 field.name = ASCIIToUTF16("Submit");
118 field.form_control_type = "submit";
119 form.fields.push_back(field);
121 // Only text and select fields that are heuristically matched are counted.
122 form_structure.reset(new FormStructure(form));
123 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
124 EXPECT_EQ(1U, form_structure->autofill_count());
126 // Add a field with should_autocomplete=false. This should not be considered a
127 // fillable field.
128 field.label = ASCIIToUTF16("address1");
129 field.name = ASCIIToUTF16("address1");
130 field.form_control_type = "text";
131 field.should_autocomplete = false;
132 form.fields.push_back(field);
134 form_structure.reset(new FormStructure(form));
135 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
136 EXPECT_EQ(2U, form_structure->autofill_count());
138 CommandLine::ForCurrentProcess()->AppendSwitch(
139 switches::kRespectAutocompleteOffForAutofill);
141 form_structure.reset(new FormStructure(form));
142 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
143 EXPECT_EQ(1U, form_structure->autofill_count());
146 TEST(FormStructureTest, SourceURL) {
147 FormData form;
148 form.origin = GURL("http://www.foo.com/");
149 FormStructure form_structure(form);
151 EXPECT_EQ(form.origin, form_structure.source_url());
154 TEST(FormStructureTest, IsAutofillable) {
155 scoped_ptr<FormStructure> form_structure;
156 FormData form;
158 // We need at least three text fields to be auto-fillable.
159 FormFieldData field;
161 field.label = ASCIIToUTF16("username");
162 field.name = ASCIIToUTF16("username");
163 field.form_control_type = "text";
164 form.fields.push_back(field);
166 field.label = ASCIIToUTF16("password");
167 field.name = ASCIIToUTF16("password");
168 field.form_control_type = "password";
169 form.fields.push_back(field);
171 field.label = base::string16();
172 field.name = ASCIIToUTF16("Submit");
173 field.form_control_type = "submit";
174 form.fields.push_back(field);
176 form_structure.reset(new FormStructure(form));
177 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
178 EXPECT_FALSE(form_structure->IsAutofillable());
180 // We now have three text fields, but only two auto-fillable fields.
181 field.label = ASCIIToUTF16("First Name");
182 field.name = ASCIIToUTF16("firstname");
183 field.form_control_type = "text";
184 form.fields.push_back(field);
186 field.label = ASCIIToUTF16("Last Name");
187 field.name = ASCIIToUTF16("lastname");
188 field.form_control_type = "text";
189 form.fields.push_back(field);
191 form_structure.reset(new FormStructure(form));
192 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
193 EXPECT_FALSE(form_structure->IsAutofillable());
195 // We now have three auto-fillable fields.
196 field.label = ASCIIToUTF16("Email");
197 field.name = ASCIIToUTF16("email");
198 field.form_control_type = "email";
199 form.fields.push_back(field);
201 form_structure.reset(new FormStructure(form));
202 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
203 EXPECT_TRUE(form_structure->IsAutofillable());
205 // The target cannot include http(s)://*/search...
206 form.action = GURL("http://google.com/search?q=hello");
207 form_structure.reset(new FormStructure(form));
208 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
209 EXPECT_FALSE(form_structure->IsAutofillable());
211 // But search can be in the URL.
212 form.action = GURL("http://search.com/?q=hello");
213 form_structure.reset(new FormStructure(form));
214 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
215 EXPECT_TRUE(form_structure->IsAutofillable());
218 TEST(FormStructureTest, ShouldBeParsed) {
219 scoped_ptr<FormStructure> form_structure;
220 FormData form;
222 // We need at least three text fields to be parseable.
223 FormFieldData field;
224 field.label = ASCIIToUTF16("username");
225 field.name = ASCIIToUTF16("username");
226 field.form_control_type = "text";
227 form.fields.push_back(field);
229 FormFieldData checkable_field;
230 checkable_field.is_checkable = true;
231 checkable_field.name = ASCIIToUTF16("radiobtn");
232 checkable_field.form_control_type = "radio";
233 form.fields.push_back(checkable_field);
235 checkable_field.name = ASCIIToUTF16("checkbox");
236 checkable_field.form_control_type = "checkbox";
237 form.fields.push_back(checkable_field);
239 // We have only one text field, should not be parsed.
240 form_structure.reset(new FormStructure(form));
241 EXPECT_FALSE(form_structure->ShouldBeParsed());
243 // We now have three text fields, though only two are auto-fillable.
244 field.label = ASCIIToUTF16("First Name");
245 field.name = ASCIIToUTF16("firstname");
246 field.form_control_type = "text";
247 form.fields.push_back(field);
249 field.label = ASCIIToUTF16("Last Name");
250 field.name = ASCIIToUTF16("lastname");
251 field.form_control_type = "text";
252 form.fields.push_back(field);
254 form_structure.reset(new FormStructure(form));
255 EXPECT_TRUE(form_structure->ShouldBeParsed());
257 form_structure.reset(new FormStructure(form));
258 EXPECT_FALSE(form_structure->IsAutofillable());
259 EXPECT_TRUE(form_structure->ShouldBeParsed());
261 // The target cannot include http(s)://*/search...
262 form.action = GURL("http://google.com/search?q=hello");
263 form_structure.reset(new FormStructure(form));
264 EXPECT_FALSE(form_structure->ShouldBeParsed());
266 // But search can be in the URL.
267 form.action = GURL("http://search.com/?q=hello");
268 form_structure.reset(new FormStructure(form));
269 EXPECT_TRUE(form_structure->ShouldBeParsed());
271 // The form need only have three fields, but at least one must be a text
272 // field.
273 form.fields.clear();
275 field.label = ASCIIToUTF16("Email");
276 field.name = ASCIIToUTF16("email");
277 field.form_control_type = "email";
278 form.fields.push_back(field);
280 field.label = ASCIIToUTF16("State");
281 field.name = ASCIIToUTF16("state");
282 field.form_control_type = "select-one";
283 form.fields.push_back(field);
285 field.label = ASCIIToUTF16("Country");
286 field.name = ASCIIToUTF16("country");
287 field.form_control_type = "select-one";
288 form.fields.push_back(field);
290 form_structure.reset(new FormStructure(form));
291 EXPECT_TRUE(form_structure->ShouldBeParsed());
293 form.fields[0].form_control_type = "select-one";
294 // Now, no text fields.
295 form_structure.reset(new FormStructure(form));
296 EXPECT_FALSE(form_structure->ShouldBeParsed());
299 TEST(FormStructureTest, HeuristicsContactInfo) {
300 scoped_ptr<FormStructure> form_structure;
301 FormData form;
303 FormFieldData field;
304 field.form_control_type = "text";
306 field.label = ASCIIToUTF16("First Name");
307 field.name = ASCIIToUTF16("firstname");
308 form.fields.push_back(field);
310 field.label = ASCIIToUTF16("Last Name");
311 field.name = ASCIIToUTF16("lastname");
312 form.fields.push_back(field);
314 field.label = ASCIIToUTF16("Email");
315 field.name = ASCIIToUTF16("email");
316 form.fields.push_back(field);
318 field.label = ASCIIToUTF16("Phone");
319 field.name = ASCIIToUTF16("phone");
320 form.fields.push_back(field);
322 field.label = ASCIIToUTF16("Address");
323 field.name = ASCIIToUTF16("address");
324 form.fields.push_back(field);
326 field.label = ASCIIToUTF16("City");
327 field.name = ASCIIToUTF16("city");
328 form.fields.push_back(field);
330 field.label = ASCIIToUTF16("Zip code");
331 field.name = ASCIIToUTF16("zipcode");
332 form.fields.push_back(field);
334 field.label = base::string16();
335 field.name = ASCIIToUTF16("Submit");
336 field.form_control_type = "submit";
337 form.fields.push_back(field);
339 form_structure.reset(new FormStructure(form));
340 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
341 EXPECT_TRUE(form_structure->IsAutofillable());
343 // Expect the correct number of fields.
344 ASSERT_EQ(8U, form_structure->field_count());
345 ASSERT_EQ(7U, form_structure->autofill_count());
347 // First name.
348 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
349 // Last name.
350 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
351 // Email.
352 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
353 // Phone.
354 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
355 form_structure->field(3)->heuristic_type());
356 // Address.
357 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type());
358 // City.
359 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type());
360 // Zip.
361 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type());
362 // Submit.
363 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type());
366 // Verify that we can correctly process the |autocomplete| attribute.
367 TEST(FormStructureTest, HeuristicsAutocompleteAttribute) {
368 scoped_ptr<FormStructure> form_structure;
369 FormData form;
371 FormFieldData field;
372 field.form_control_type = "text";
374 field.label = base::string16();
375 field.name = ASCIIToUTF16("field1");
376 field.autocomplete_attribute = "given-name";
377 form.fields.push_back(field);
379 field.label = base::string16();
380 field.name = ASCIIToUTF16("field2");
381 field.autocomplete_attribute = "family-name";
382 form.fields.push_back(field);
384 field.label = base::string16();
385 field.name = ASCIIToUTF16("field3");
386 field.autocomplete_attribute = "email";
387 form.fields.push_back(field);
389 form_structure.reset(new FormStructure(form));
390 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
391 EXPECT_TRUE(form_structure->IsAutofillable());
393 // Expect the correct number of fields.
394 ASSERT_EQ(3U, form_structure->field_count());
395 ASSERT_EQ(3U, form_structure->autofill_count());
397 EXPECT_EQ(HTML_TYPE_GIVEN_NAME, form_structure->field(0)->html_type());
398 EXPECT_EQ(HTML_TYPE_FAMILY_NAME, form_structure->field(1)->html_type());
399 EXPECT_EQ(HTML_TYPE_EMAIL, form_structure->field(2)->html_type());
400 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type());
401 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
402 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
405 // Verify that we can correctly process the 'autocomplete' attribute for phone
406 // number types (especially phone prefixes and suffixes).
407 TEST(FormStructureTest, HeuristicsAutocompleteAttributePhoneTypes) {
408 scoped_ptr<FormStructure> form_structure;
409 FormData form;
411 FormFieldData field;
412 field.form_control_type = "text";
414 field.label = base::string16();
415 field.name = ASCIIToUTF16("field1");
416 field.autocomplete_attribute = "tel-local";
417 form.fields.push_back(field);
419 field.label = base::string16();
420 field.name = ASCIIToUTF16("field2");
421 field.autocomplete_attribute = "tel-local-prefix";
422 form.fields.push_back(field);
424 field.label = base::string16();
425 field.name = ASCIIToUTF16("field3");
426 field.autocomplete_attribute = "tel-local-suffix";
427 form.fields.push_back(field);
429 form_structure.reset(new FormStructure(form));
430 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
431 EXPECT_TRUE(form_structure->IsAutofillable());
433 // Expect the correct number of fields.
434 ASSERT_EQ(3U, form_structure->field_count());
435 EXPECT_EQ(3U, form_structure->autofill_count());
437 EXPECT_EQ(HTML_TYPE_TEL_LOCAL, form_structure->field(0)->html_type());
438 EXPECT_EQ(AutofillField::IGNORED, form_structure->field(0)->phone_part());
439 EXPECT_EQ(HTML_TYPE_TEL_LOCAL_PREFIX, form_structure->field(1)->html_type());
440 EXPECT_EQ(AutofillField::PHONE_PREFIX,
441 form_structure->field(1)->phone_part());
442 EXPECT_EQ(HTML_TYPE_TEL_LOCAL_SUFFIX, form_structure->field(2)->html_type());
443 EXPECT_EQ(AutofillField::PHONE_SUFFIX,
444 form_structure->field(2)->phone_part());
447 // If at least one field includes type hints in the 'autocomplete' attribute, we
448 // should not try to apply any other heuristics.
449 TEST(FormStructureTest, AutocompleteAttributeOverridesOtherHeuristics) {
450 scoped_ptr<FormStructure> form_structure;
451 FormData form;
453 // Start with a regular contact form.
454 FormFieldData field;
455 field.form_control_type = "text";
457 field.label = ASCIIToUTF16("First Name");
458 field.name = ASCIIToUTF16("firstname");
459 form.fields.push_back(field);
461 field.label = ASCIIToUTF16("Last Name");
462 field.name = ASCIIToUTF16("lastname");
463 form.fields.push_back(field);
465 field.label = ASCIIToUTF16("Email");
466 field.name = ASCIIToUTF16("email");
467 form.fields.push_back(field);
469 form_structure.reset(new FormStructure(form));
470 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
471 EXPECT_TRUE(form_structure->IsAutofillable());
472 EXPECT_TRUE(form_structure->ShouldBeCrowdsourced());
474 ASSERT_EQ(3U, form_structure->field_count());
475 ASSERT_EQ(3U, form_structure->autofill_count());
477 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
478 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
479 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
481 // Now update the first form field to include an 'autocomplete' attribute.
482 form.fields.front().autocomplete_attribute = "x-other";
483 form_structure.reset(new FormStructure(form));
484 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
485 EXPECT_FALSE(form_structure->IsAutofillable());
486 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced());
488 ASSERT_EQ(3U, form_structure->field_count());
489 ASSERT_EQ(0U, form_structure->autofill_count());
491 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type());
492 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
493 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
496 // Even with an 'autocomplete' attribute set, ShouldBeCrowdsourced() should
497 // return true if the structure contains a password field, since there are
498 // no local heuristics to depend upon in this case. Fields will still not be
499 // considered autofillable though.
500 TEST(FormStructureTest, PasswordFormShouldBeCrowdsourced) {
501 FormData form;
503 // Start with a regular contact form.
504 FormFieldData field;
505 field.form_control_type = "text";
507 field.label = ASCIIToUTF16("First Name");
508 field.name = ASCIIToUTF16("firstname");
509 form.fields.push_back(field);
511 field.label = ASCIIToUTF16("Last Name");
512 field.name = ASCIIToUTF16("lastname");
513 form.fields.push_back(field);
515 field.label = ASCIIToUTF16("Email");
516 field.name = ASCIIToUTF16("email");
517 field.autocomplete_attribute = "username";
518 form.fields.push_back(field);
520 field.label = ASCIIToUTF16("Password");
521 field.name = ASCIIToUTF16("Password");
522 field.form_control_type = "password";
523 form.fields.push_back(field);
525 FormStructure form_structure(form);
526 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
527 EXPECT_TRUE(form_structure.ShouldBeCrowdsourced());
530 // Verify that we can correctly process sections listed in the |autocomplete|
531 // attribute.
532 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSections) {
533 FormData form;
535 FormFieldData field;
536 field.form_control_type = "text";
538 // Some fields will have no section specified. These fall into the default
539 // section.
540 field.autocomplete_attribute = "email";
541 form.fields.push_back(field);
543 // We allow arbitrary section names.
544 field.autocomplete_attribute = "section-foo email";
545 form.fields.push_back(field);
547 // "shipping" and "billing" are special section tokens that don't require the
548 // "section-" prefix.
549 field.autocomplete_attribute = "shipping email";
550 form.fields.push_back(field);
551 field.autocomplete_attribute = "billing email";
552 form.fields.push_back(field);
554 // "shipping" and "billing" can be combined with other section names.
555 field.autocomplete_attribute = "section-foo shipping email";
556 form.fields.push_back(field);
557 field.autocomplete_attribute = "section-foo billing email";
558 form.fields.push_back(field);
560 // We don't do anything clever to try to coalesce sections; it's up to site
561 // authors to avoid typos.
562 field.autocomplete_attribute = "section--foo email";
563 form.fields.push_back(field);
565 // "shipping email" and "section--shipping" email should be parsed as
566 // different sections. This is only an interesting test due to how we
567 // implement implicit section names from attributes like "shipping email"; see
568 // the implementation for more details.
569 field.autocomplete_attribute = "section--shipping email";
570 form.fields.push_back(field);
572 // Credit card fields are implicitly in a separate section from other fields.
573 field.autocomplete_attribute = "section-foo cc-number";
574 form.fields.push_back(field);
576 FormStructure form_structure(form);
577 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
578 EXPECT_TRUE(form_structure.IsAutofillable());
580 // Expect the correct number of fields.
581 ASSERT_EQ(9U, form_structure.field_count());
582 EXPECT_EQ(9U, form_structure.autofill_count());
584 // All of the fields in this form should be parsed as belonging to different
585 // sections.
586 std::set<std::string> section_names;
587 for (size_t i = 0; i < 9; ++i) {
588 section_names.insert(form_structure.field(i)->section());
590 EXPECT_EQ(9U, section_names.size());
593 // Verify that we can correctly process a degenerate section listed in the
594 // |autocomplete| attribute.
595 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsDegenerate) {
596 FormData form;
598 FormFieldData field;
599 field.form_control_type = "text";
601 // Some fields will have no section specified. These fall into the default
602 // section.
603 field.autocomplete_attribute = "email";
604 form.fields.push_back(field);
606 // Specifying "section-" is equivalent to not specifying a section.
607 field.autocomplete_attribute = "section- email";
608 form.fields.push_back(field);
610 // Invalid tokens should prevent us from setting a section name.
611 field.autocomplete_attribute = "garbage section-foo email";
612 form.fields.push_back(field);
613 field.autocomplete_attribute = "garbage section-bar email";
614 form.fields.push_back(field);
615 field.autocomplete_attribute = "garbage shipping email";
616 form.fields.push_back(field);
617 field.autocomplete_attribute = "garbage billing email";
618 form.fields.push_back(field);
620 FormStructure form_structure(form);
621 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
623 // Expect the correct number of fields.
624 ASSERT_EQ(6U, form_structure.field_count());
625 EXPECT_EQ(2U, form_structure.autofill_count());
627 // All of the fields in this form should be parsed as belonging to the same
628 // section.
629 std::set<std::string> section_names;
630 for (size_t i = 0; i < 6; ++i) {
631 section_names.insert(form_structure.field(i)->section());
633 EXPECT_EQ(1U, section_names.size());
636 // Verify that we can correctly process repeated sections listed in the
637 // |autocomplete| attribute.
638 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsRepeated) {
639 FormData form;
641 FormFieldData field;
642 field.form_control_type = "text";
644 field.autocomplete_attribute = "section-foo email";
645 form.fields.push_back(field);
646 field.autocomplete_attribute = "section-foo address-line1";
647 form.fields.push_back(field);
649 FormStructure form_structure(form);
650 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
652 // Expect the correct number of fields.
653 ASSERT_EQ(2U, form_structure.field_count());
654 EXPECT_EQ(2U, form_structure.autofill_count());
656 // All of the fields in this form should be parsed as belonging to the same
657 // section.
658 std::set<std::string> section_names;
659 for (size_t i = 0; i < 2; ++i) {
660 section_names.insert(form_structure.field(i)->section());
662 EXPECT_EQ(1U, section_names.size());
665 // Verify that we do not override the author-specified sections from a form with
666 // local heuristics.
667 TEST(FormStructureTest, HeuristicsDontOverrideAutocompleteAttributeSections) {
668 FormData form;
670 FormFieldData field;
671 field.form_control_type = "text";
673 field.name = ASCIIToUTF16("one");
674 field.autocomplete_attribute = "address-line1";
675 form.fields.push_back(field);
676 field.name = base::string16();
677 field.autocomplete_attribute = "section-foo email";
678 form.fields.push_back(field);
679 field.name = base::string16();
680 field.autocomplete_attribute = "name";
681 form.fields.push_back(field);
682 field.name = ASCIIToUTF16("two");
683 field.autocomplete_attribute = "address-line1";
684 form.fields.push_back(field);
686 FormStructure form_structure(form);
687 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
689 // Expect the correct number of fields.
690 ASSERT_EQ(4U, form_structure.field_count());
691 EXPECT_EQ(4U, form_structure.autofill_count());
693 // Normally, the two separate address fields would cause us to detect two
694 // separate sections; but because there is an author-specified section in this
695 // form, we do not apply these usual heuristics.
696 EXPECT_EQ(ASCIIToUTF16("one"), form_structure.field(0)->name);
697 EXPECT_EQ(ASCIIToUTF16("two"), form_structure.field(3)->name);
698 EXPECT_EQ(form_structure.field(0)->section(),
699 form_structure.field(3)->section());
702 TEST(FormStructureTest, HeuristicsSample8) {
703 scoped_ptr<FormStructure> form_structure;
704 FormData form;
706 FormFieldData field;
707 field.form_control_type = "text";
709 field.label = ASCIIToUTF16("Your First Name:");
710 field.name = ASCIIToUTF16("bill.first");
711 form.fields.push_back(field);
713 field.label = ASCIIToUTF16("Your Last Name:");
714 field.name = ASCIIToUTF16("bill.last");
715 form.fields.push_back(field);
717 field.label = ASCIIToUTF16("Street Address Line 1:");
718 field.name = ASCIIToUTF16("bill.street1");
719 form.fields.push_back(field);
721 field.label = ASCIIToUTF16("Street Address Line 2:");
722 field.name = ASCIIToUTF16("bill.street2");
723 form.fields.push_back(field);
725 field.label = ASCIIToUTF16("City");
726 field.name = ASCIIToUTF16("bill.city");
727 form.fields.push_back(field);
729 field.label = ASCIIToUTF16("State (U.S.):");
730 field.name = ASCIIToUTF16("bill.state");
731 form.fields.push_back(field);
733 field.label = ASCIIToUTF16("Zip/Postal Code:");
734 field.name = ASCIIToUTF16("BillTo.PostalCode");
735 form.fields.push_back(field);
737 field.label = ASCIIToUTF16("Country:");
738 field.name = ASCIIToUTF16("bill.country");
739 form.fields.push_back(field);
741 field.label = ASCIIToUTF16("Phone Number:");
742 field.name = ASCIIToUTF16("BillTo.Phone");
743 form.fields.push_back(field);
745 field.label = base::string16();
746 field.name = ASCIIToUTF16("Submit");
747 field.form_control_type = "submit";
748 form.fields.push_back(field);
750 form_structure.reset(new FormStructure(form));
751 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
752 EXPECT_TRUE(form_structure->IsAutofillable());
753 ASSERT_EQ(10U, form_structure->field_count());
754 ASSERT_EQ(9U, form_structure->autofill_count());
756 // First name.
757 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
758 // Last name.
759 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
760 // Address.
761 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(2)->heuristic_type());
762 // Address.
763 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(3)->heuristic_type());
764 // City.
765 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type());
766 // State.
767 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(5)->heuristic_type());
768 // Zip.
769 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type());
770 // Country.
771 EXPECT_EQ(ADDRESS_HOME_COUNTRY, form_structure->field(7)->heuristic_type());
772 // Phone.
773 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
774 form_structure->field(8)->heuristic_type());
775 // Submit.
776 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(9)->heuristic_type());
779 TEST(FormStructureTest, HeuristicsSample6) {
780 scoped_ptr<FormStructure> form_structure;
781 FormData form;
783 FormFieldData field;
784 field.form_control_type = "text";
786 field.label = ASCIIToUTF16("E-mail address");
787 field.name = ASCIIToUTF16("email");
788 form.fields.push_back(field);
790 field.label = ASCIIToUTF16("Full name");
791 field.name = ASCIIToUTF16("name");
792 form.fields.push_back(field);
794 field.label = ASCIIToUTF16("Company");
795 field.name = ASCIIToUTF16("company");
796 form.fields.push_back(field);
798 field.label = ASCIIToUTF16("Address");
799 field.name = ASCIIToUTF16("address");
800 form.fields.push_back(field);
802 field.label = ASCIIToUTF16("City");
803 field.name = ASCIIToUTF16("city");
804 form.fields.push_back(field);
806 field.label = ASCIIToUTF16("Zip Code");
807 field.name = ASCIIToUTF16("Home.PostalCode");
808 form.fields.push_back(field);
810 field.label = base::string16();
811 field.name = ASCIIToUTF16("Submit");
812 field.value = ASCIIToUTF16("continue");
813 field.form_control_type = "submit";
814 form.fields.push_back(field);
816 form_structure.reset(new FormStructure(form));
817 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
818 EXPECT_TRUE(form_structure->IsAutofillable());
819 ASSERT_EQ(7U, form_structure->field_count());
820 ASSERT_EQ(6U, form_structure->autofill_count());
822 // Email.
823 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(0)->heuristic_type());
824 // Full name.
825 EXPECT_EQ(NAME_FULL, form_structure->field(1)->heuristic_type());
826 // Company
827 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type());
828 // Address.
829 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type());
830 // City.
831 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type());
832 // Zip.
833 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(5)->heuristic_type());
834 // Submit.
835 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type());
838 // Tests a sequence of FormFields where only labels are supplied to heuristics
839 // for matching. This works because FormFieldData labels are matched in the
840 // case that input element ids (or |name| fields) are missing.
841 TEST(FormStructureTest, HeuristicsLabelsOnly) {
842 scoped_ptr<FormStructure> form_structure;
843 FormData form;
845 FormFieldData field;
846 field.form_control_type = "text";
848 field.label = ASCIIToUTF16("First Name");
849 field.name = base::string16();
850 form.fields.push_back(field);
852 field.label = ASCIIToUTF16("Last Name");
853 field.name = base::string16();
854 form.fields.push_back(field);
856 field.label = ASCIIToUTF16("Email");
857 field.name = base::string16();
858 form.fields.push_back(field);
860 field.label = ASCIIToUTF16("Phone");
861 field.name = base::string16();
862 form.fields.push_back(field);
864 field.label = ASCIIToUTF16("Address");
865 field.name = base::string16();
866 form.fields.push_back(field);
868 field.label = ASCIIToUTF16("Address");
869 field.name = base::string16();
870 form.fields.push_back(field);
872 field.label = ASCIIToUTF16("Zip code");
873 field.name = base::string16();
874 form.fields.push_back(field);
876 field.label = base::string16();
877 field.name = ASCIIToUTF16("Submit");
878 field.form_control_type = "submit";
879 form.fields.push_back(field);
881 form_structure.reset(new FormStructure(form));
882 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
883 EXPECT_TRUE(form_structure->IsAutofillable());
884 ASSERT_EQ(8U, form_structure->field_count());
885 ASSERT_EQ(7U, form_structure->autofill_count());
887 // First name.
888 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
889 // Last name.
890 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
891 // Email.
892 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
893 // Phone.
894 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
895 form_structure->field(3)->heuristic_type());
896 // Address.
897 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type());
898 // Address Line 2.
899 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(5)->heuristic_type());
900 // Zip.
901 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type());
902 // Submit.
903 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type());
906 TEST(FormStructureTest, HeuristicsCreditCardInfo) {
907 scoped_ptr<FormStructure> form_structure;
908 FormData form;
910 FormFieldData field;
911 field.form_control_type = "text";
913 field.label = ASCIIToUTF16("Name on Card");
914 field.name = ASCIIToUTF16("name_on_card");
915 form.fields.push_back(field);
917 field.label = ASCIIToUTF16("Card Number");
918 field.name = ASCIIToUTF16("card_number");
919 form.fields.push_back(field);
921 field.label = ASCIIToUTF16("Exp Month");
922 field.name = ASCIIToUTF16("ccmonth");
923 form.fields.push_back(field);
925 field.label = ASCIIToUTF16("Exp Year");
926 field.name = ASCIIToUTF16("ccyear");
927 form.fields.push_back(field);
929 field.label = ASCIIToUTF16("Verification");
930 field.name = ASCIIToUTF16("verification");
931 form.fields.push_back(field);
933 field.label = base::string16();
934 field.name = ASCIIToUTF16("Submit");
935 field.form_control_type = "submit";
936 form.fields.push_back(field);
938 form_structure.reset(new FormStructure(form));
939 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
940 EXPECT_TRUE(form_structure->IsAutofillable());
941 ASSERT_EQ(6U, form_structure->field_count());
942 ASSERT_EQ(5U, form_structure->autofill_count());
944 // Credit card name.
945 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
946 // Credit card number.
947 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(1)->heuristic_type());
948 // Credit card expiration month.
949 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(2)->heuristic_type());
950 // Credit card expiration year.
951 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
952 form_structure->field(3)->heuristic_type());
953 // CVV.
954 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
955 form_structure->field(4)->heuristic_type());
956 // Submit.
957 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(5)->heuristic_type());
960 TEST(FormStructureTest, HeuristicsCreditCardInfoWithUnknownCardField) {
961 scoped_ptr<FormStructure> form_structure;
962 FormData form;
964 FormFieldData field;
965 field.form_control_type = "text";
967 field.label = ASCIIToUTF16("Name on Card");
968 field.name = ASCIIToUTF16("name_on_card");
969 form.fields.push_back(field);
971 // This is not a field we know how to process. But we should skip over it
972 // and process the other fields in the card block.
973 field.label = ASCIIToUTF16("Card image");
974 field.name = ASCIIToUTF16("card_image");
975 form.fields.push_back(field);
977 field.label = ASCIIToUTF16("Card Number");
978 field.name = ASCIIToUTF16("card_number");
979 form.fields.push_back(field);
981 field.label = ASCIIToUTF16("Exp Month");
982 field.name = ASCIIToUTF16("ccmonth");
983 form.fields.push_back(field);
985 field.label = ASCIIToUTF16("Exp Year");
986 field.name = ASCIIToUTF16("ccyear");
987 form.fields.push_back(field);
989 field.label = ASCIIToUTF16("Verification");
990 field.name = ASCIIToUTF16("verification");
991 form.fields.push_back(field);
993 field.label = base::string16();
994 field.name = ASCIIToUTF16("Submit");
995 field.form_control_type = "submit";
996 form.fields.push_back(field);
998 form_structure.reset(new FormStructure(form));
999 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1000 EXPECT_TRUE(form_structure->IsAutofillable());
1001 ASSERT_EQ(7U, form_structure->field_count());
1002 ASSERT_EQ(5U, form_structure->autofill_count());
1004 // Credit card name.
1005 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
1006 // Credit card type. This is an unknown type but related to the credit card.
1007 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
1008 // Credit card number.
1009 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type());
1010 // Credit card expiration month.
1011 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1012 // Credit card expiration year.
1013 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1014 form_structure->field(4)->heuristic_type());
1015 // CVV.
1016 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
1017 form_structure->field(5)->heuristic_type());
1018 // Submit.
1019 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type());
1022 TEST(FormStructureTest, ThreeAddressLines) {
1023 scoped_ptr<FormStructure> form_structure;
1024 FormData form;
1026 FormFieldData field;
1027 field.form_control_type = "text";
1029 field.label = ASCIIToUTF16("Address Line1");
1030 field.name = ASCIIToUTF16("Address");
1031 form.fields.push_back(field);
1033 field.label = ASCIIToUTF16("Address Line2");
1034 field.name = ASCIIToUTF16("Address");
1035 form.fields.push_back(field);
1037 field.label = ASCIIToUTF16("Address Line3");
1038 field.name = ASCIIToUTF16("Address");
1039 form.fields.push_back(field);
1041 field.label = ASCIIToUTF16("City");
1042 field.name = ASCIIToUTF16("city");
1043 form.fields.push_back(field);
1045 form_structure.reset(new FormStructure(form));
1046 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1047 EXPECT_TRUE(form_structure->IsAutofillable());
1048 ASSERT_EQ(4U, form_structure->field_count());
1049 ASSERT_EQ(3U, form_structure->autofill_count());
1051 // Address Line 1.
1052 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1053 // Address Line 2.
1054 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1055 // Address Line 3.
1056 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1057 // City.
1058 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type());
1061 // Numbered address lines after line two are ignored.
1062 TEST(FormStructureTest, SurplusAddressLinesIgnored) {
1063 scoped_ptr<FormStructure> form_structure;
1064 FormData form;
1066 FormFieldData field;
1067 field.form_control_type = "text";
1069 field.label = ASCIIToUTF16("Address Line1");
1070 field.name = ASCIIToUTF16("shipping.address.addressLine1");
1071 form.fields.push_back(field);
1073 field.label = ASCIIToUTF16("Address Line2");
1074 field.name = ASCIIToUTF16("shipping.address.addressLine2");
1075 form.fields.push_back(field);
1077 field.label = ASCIIToUTF16("Address Line3");
1078 field.name = ASCIIToUTF16("billing.address.addressLine3");
1079 form.fields.push_back(field);
1081 field.label = ASCIIToUTF16("Address Line4");
1082 field.name = ASCIIToUTF16("billing.address.addressLine4");
1083 form.fields.push_back(field);
1085 form_structure.reset(new FormStructure(form));
1086 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1087 ASSERT_EQ(4U, form_structure->field_count());
1088 ASSERT_EQ(2U, form_structure->autofill_count());
1090 // Address Line 1.
1091 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1092 // Address Line 2.
1093 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1094 // Address Line 3 (ignored).
1095 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1096 // Address Line 4 (ignored).
1097 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type());
1100 // This example comes from expedia.com where they use a "Suite" label to
1101 // indicate a suite or apartment number. We interpret this as address line 2.
1102 // And the following "Street address second line" we interpret as address line
1103 // 3 and discard.
1104 // See http://crbug.com/48197 for details.
1105 TEST(FormStructureTest, ThreeAddressLinesExpedia) {
1106 scoped_ptr<FormStructure> form_structure;
1107 FormData form;
1109 FormFieldData field;
1110 field.form_control_type = "text";
1112 field.label = ASCIIToUTF16("Street:");
1113 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads1");
1114 form.fields.push_back(field);
1116 field.label = ASCIIToUTF16("Suite or Apt:");
1117 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adap");
1118 form.fields.push_back(field);
1120 field.label = ASCIIToUTF16("Street address second line");
1121 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads2");
1122 form.fields.push_back(field);
1124 field.label = ASCIIToUTF16("City:");
1125 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adct");
1126 form.fields.push_back(field);
1128 form_structure.reset(new FormStructure(form));
1129 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1130 EXPECT_TRUE(form_structure->IsAutofillable());
1131 ASSERT_EQ(4U, form_structure->field_count());
1132 EXPECT_EQ(3U, form_structure->autofill_count());
1134 // Address Line 1.
1135 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1136 // Suite / Apt.
1137 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1138 // Address Line 3.
1139 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1140 // City.
1141 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type());
1144 // This example comes from ebay.com where the word "suite" appears in the label
1145 // and the name "address2" clearly indicates that this is the address line 2.
1146 // See http://crbug.com/48197 for details.
1147 TEST(FormStructureTest, TwoAddressLinesEbay) {
1148 scoped_ptr<FormStructure> form_structure;
1149 FormData form;
1151 FormFieldData field;
1152 field.form_control_type = "text";
1154 field.label = ASCIIToUTF16("Address Line1");
1155 field.name = ASCIIToUTF16("address1");
1156 form.fields.push_back(field);
1158 field.label = ASCIIToUTF16("Floor number, suite number, etc");
1159 field.name = ASCIIToUTF16("address2");
1160 form.fields.push_back(field);
1162 field.label = ASCIIToUTF16("City:");
1163 field.name = ASCIIToUTF16("city");
1164 form.fields.push_back(field);
1166 form_structure.reset(new FormStructure(form));
1167 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1168 EXPECT_TRUE(form_structure->IsAutofillable());
1169 ASSERT_EQ(3U, form_structure->field_count());
1170 ASSERT_EQ(3U, form_structure->autofill_count());
1172 // Address Line 1.
1173 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1174 // Address Line 2.
1175 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1176 // City.
1177 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(2)->heuristic_type());
1180 TEST(FormStructureTest, HeuristicsStateWithProvince) {
1181 scoped_ptr<FormStructure> form_structure;
1182 FormData form;
1184 FormFieldData field;
1185 field.form_control_type = "text";
1187 field.label = ASCIIToUTF16("Address Line1");
1188 field.name = ASCIIToUTF16("Address");
1189 form.fields.push_back(field);
1191 field.label = ASCIIToUTF16("Address Line2");
1192 field.name = ASCIIToUTF16("Address");
1193 form.fields.push_back(field);
1195 field.label = ASCIIToUTF16("State/Province/Region");
1196 field.name = ASCIIToUTF16("State");
1197 form.fields.push_back(field);
1199 form_structure.reset(new FormStructure(form));
1200 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1201 EXPECT_TRUE(form_structure->IsAutofillable());
1202 ASSERT_EQ(3U, form_structure->field_count());
1203 ASSERT_EQ(3U, form_structure->autofill_count());
1205 // Address Line 1.
1206 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1207 // Address Line 2.
1208 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1209 // State.
1210 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(2)->heuristic_type());
1213 // This example comes from lego.com's checkout page.
1214 TEST(FormStructureTest, HeuristicsWithBilling) {
1215 scoped_ptr<FormStructure> form_structure;
1216 FormData form;
1218 FormFieldData field;
1219 field.form_control_type = "text";
1221 field.label = ASCIIToUTF16("First Name*:");
1222 field.name = ASCIIToUTF16("editBillingAddress$firstNameBox");
1223 form.fields.push_back(field);
1225 field.label = ASCIIToUTF16("Last Name*:");
1226 field.name = ASCIIToUTF16("editBillingAddress$lastNameBox");
1227 form.fields.push_back(field);
1229 field.label = ASCIIToUTF16("Company Name:");
1230 field.name = ASCIIToUTF16("editBillingAddress$companyBox");
1231 form.fields.push_back(field);
1233 field.label = ASCIIToUTF16("Address*:");
1234 field.name = ASCIIToUTF16("editBillingAddress$addressLine1Box");
1235 form.fields.push_back(field);
1237 field.label = ASCIIToUTF16("Apt/Suite :");
1238 field.name = ASCIIToUTF16("editBillingAddress$addressLine2Box");
1239 form.fields.push_back(field);
1241 field.label = ASCIIToUTF16("City*:");
1242 field.name = ASCIIToUTF16("editBillingAddress$cityBox");
1243 form.fields.push_back(field);
1245 field.label = ASCIIToUTF16("State/Province*:");
1246 field.name = ASCIIToUTF16("editBillingAddress$stateDropDown");
1247 form.fields.push_back(field);
1249 field.label = ASCIIToUTF16("Country*:");
1250 field.name = ASCIIToUTF16("editBillingAddress$countryDropDown");
1251 form.fields.push_back(field);
1253 field.label = ASCIIToUTF16("Postal Code*:");
1254 field.name = ASCIIToUTF16("editBillingAddress$zipCodeBox");
1255 form.fields.push_back(field);
1257 field.label = ASCIIToUTF16("Phone*:");
1258 field.name = ASCIIToUTF16("editBillingAddress$phoneBox");
1259 form.fields.push_back(field);
1261 field.label = ASCIIToUTF16("Email Address*:");
1262 field.name = ASCIIToUTF16("email$emailBox");
1263 form.fields.push_back(field);
1265 form_structure.reset(new FormStructure(form));
1266 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1267 EXPECT_TRUE(form_structure->IsAutofillable());
1268 ASSERT_EQ(11U, form_structure->field_count());
1269 ASSERT_EQ(11U, form_structure->autofill_count());
1271 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
1272 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
1273 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type());
1274 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type());
1275 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(4)->heuristic_type());
1276 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type());
1277 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(6)->heuristic_type());
1278 EXPECT_EQ(ADDRESS_HOME_COUNTRY, form_structure->field(7)->heuristic_type());
1279 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(8)->heuristic_type());
1280 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1281 form_structure->field(9)->heuristic_type());
1282 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(10)->heuristic_type());
1285 TEST(FormStructureTest, ThreePartPhoneNumber) {
1286 scoped_ptr<FormStructure> form_structure;
1287 FormData form;
1289 FormFieldData field;
1290 field.form_control_type = "text";
1292 field.label = ASCIIToUTF16("Phone:");
1293 field.name = ASCIIToUTF16("dayphone1");
1294 field.max_length = 0;
1295 form.fields.push_back(field);
1297 field.label = ASCIIToUTF16("-");
1298 field.name = ASCIIToUTF16("dayphone2");
1299 field.max_length = 3; // Size of prefix is 3.
1300 form.fields.push_back(field);
1302 field.label = ASCIIToUTF16("-");
1303 field.name = ASCIIToUTF16("dayphone3");
1304 field.max_length = 4; // Size of suffix is 4. If unlimited size is
1305 // passed, phone will be parsed as
1306 // <country code> - <area code> - <phone>.
1307 form.fields.push_back(field);
1309 field.label = ASCIIToUTF16("ext.:");
1310 field.name = ASCIIToUTF16("dayphone4");
1311 field.max_length = 0;
1312 form.fields.push_back(field);
1314 form_structure.reset(new FormStructure(form));
1315 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1316 EXPECT_TRUE(form_structure->IsAutofillable());
1317 ASSERT_EQ(4U, form_structure->field_count());
1318 ASSERT_EQ(3U, form_structure->autofill_count());
1320 // Area code.
1321 EXPECT_EQ(PHONE_HOME_CITY_CODE, form_structure->field(0)->heuristic_type());
1322 // Phone number suffix.
1323 EXPECT_EQ(PHONE_HOME_NUMBER,
1324 form_structure->field(1)->heuristic_type());
1325 // Phone number suffix.
1326 EXPECT_EQ(PHONE_HOME_NUMBER,
1327 form_structure->field(2)->heuristic_type());
1328 // Unknown.
1329 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type());
1332 TEST(FormStructureTest, HeuristicsInfernoCC) {
1333 scoped_ptr<FormStructure> form_structure;
1334 FormData form;
1336 FormFieldData field;
1337 field.form_control_type = "text";
1339 field.label = ASCIIToUTF16("Name on Card");
1340 field.name = ASCIIToUTF16("name_on_card");
1341 form.fields.push_back(field);
1343 field.label = ASCIIToUTF16("Address");
1344 field.name = ASCIIToUTF16("billing_address");
1345 form.fields.push_back(field);
1347 field.label = ASCIIToUTF16("Card Number");
1348 field.name = ASCIIToUTF16("card_number");
1349 form.fields.push_back(field);
1351 field.label = ASCIIToUTF16("Expiration Date");
1352 field.name = ASCIIToUTF16("expiration_month");
1353 form.fields.push_back(field);
1355 field.label = ASCIIToUTF16("Expiration Year");
1356 field.name = ASCIIToUTF16("expiration_year");
1357 form.fields.push_back(field);
1359 form_structure.reset(new FormStructure(form));
1360 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1361 EXPECT_TRUE(form_structure->IsAutofillable());
1363 // Expect the correct number of fields.
1364 ASSERT_EQ(5U, form_structure->field_count());
1365 EXPECT_EQ(5U, form_structure->autofill_count());
1367 // Name on Card.
1368 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
1369 // Address.
1370 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(1)->heuristic_type());
1371 // Card Number.
1372 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type());
1373 // Expiration Date.
1374 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1375 // Expiration Year.
1376 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1377 form_structure->field(4)->heuristic_type());
1380 TEST(FormStructureTest, CVCCodeClash) {
1381 scoped_ptr<FormStructure> form_structure;
1382 FormData form;
1384 FormFieldData field;
1385 field.form_control_type = "text";
1387 field.label = ASCIIToUTF16("Card number");
1388 field.name = ASCIIToUTF16("ccnumber");
1389 form.fields.push_back(field);
1391 field.label = ASCIIToUTF16("First name");
1392 field.name = ASCIIToUTF16("first_name");
1393 form.fields.push_back(field);
1395 field.label = ASCIIToUTF16("Last name");
1396 field.name = ASCIIToUTF16("last_name");
1397 form.fields.push_back(field);
1399 field.label = ASCIIToUTF16("Expiration date");
1400 field.name = ASCIIToUTF16("ccexpiresmonth");
1401 form.fields.push_back(field);
1403 field.label = base::string16();
1404 field.name = ASCIIToUTF16("ccexpiresyear");
1405 form.fields.push_back(field);
1407 field.label = ASCIIToUTF16("cvc number");
1408 field.name = ASCIIToUTF16("csc");
1409 form.fields.push_back(field);
1411 form_structure.reset(new FormStructure(form));
1412 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1413 EXPECT_TRUE(form_structure->IsAutofillable());
1415 // Expect the correct number of fields.
1416 ASSERT_EQ(6U, form_structure->field_count());
1417 ASSERT_EQ(5U, form_structure->autofill_count());
1419 // Card Number.
1420 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(0)->heuristic_type());
1421 // First name, taken as name on card.
1422 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(1)->heuristic_type());
1423 // Last name is not merged.
1424 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1425 // Expiration Date.
1426 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1427 // Expiration Year.
1428 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1429 form_structure->field(4)->heuristic_type());
1430 // CVC code.
1431 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
1432 form_structure->field(5)->heuristic_type());
1435 TEST(FormStructureTest, EncodeQueryRequest) {
1436 FormData form;
1438 FormFieldData field;
1439 field.form_control_type = "text";
1441 field.label = ASCIIToUTF16("Name on Card");
1442 field.name = ASCIIToUTF16("name_on_card");
1443 form.fields.push_back(field);
1445 field.label = ASCIIToUTF16("Address");
1446 field.name = ASCIIToUTF16("billing_address");
1447 form.fields.push_back(field);
1449 field.label = ASCIIToUTF16("Card Number");
1450 field.name = ASCIIToUTF16("card_number");
1451 form.fields.push_back(field);
1453 field.label = ASCIIToUTF16("Expiration Date");
1454 field.name = ASCIIToUTF16("expiration_month");
1455 form.fields.push_back(field);
1457 field.label = ASCIIToUTF16("Expiration Year");
1458 field.name = ASCIIToUTF16("expiration_year");
1459 form.fields.push_back(field);
1461 // Add checkable field.
1462 FormFieldData checkable_field;
1463 checkable_field.is_checkable = true;
1464 checkable_field.label = ASCIIToUTF16("Checkable1");
1465 checkable_field.name = ASCIIToUTF16("Checkable1");
1466 form.fields.push_back(checkable_field);
1468 ScopedVector<FormStructure> forms;
1469 forms.push_back(new FormStructure(form));
1470 std::vector<std::string> encoded_signatures;
1471 std::string encoded_xml;
1472 const char kSignature1[] = "11337937696949187602";
1473 const char kResponse1[] =
1474 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1475 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
1476 "<form signature=\"11337937696949187602\">"
1477 "<field signature=\"412125936\"/>"
1478 "<field signature=\"1917667676\"/>"
1479 "<field signature=\"2226358947\"/>"
1480 "<field signature=\"747221617\"/>"
1481 "<field signature=\"4108155786\"/>"
1482 "</form>"
1483 "</autofillquery>";
1484 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1485 &encoded_signatures,
1486 &encoded_xml));
1487 ASSERT_EQ(1U, encoded_signatures.size());
1488 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1489 EXPECT_EQ(kResponse1, encoded_xml);
1491 // Add the same form, only one will be encoded, so EncodeQueryRequest() should
1492 // return the same data.
1493 forms.push_back(new FormStructure(form));
1494 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1495 &encoded_signatures,
1496 &encoded_xml));
1497 ASSERT_EQ(1U, encoded_signatures.size());
1498 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1499 EXPECT_EQ(kResponse1, encoded_xml);
1500 // Add 5 address fields - this should be still a valid form.
1501 for (size_t i = 0; i < 5; ++i) {
1502 field.label = ASCIIToUTF16("Address");
1503 field.name = ASCIIToUTF16("address");
1504 form.fields.push_back(field);
1507 forms.push_back(new FormStructure(form));
1508 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1509 &encoded_signatures,
1510 &encoded_xml));
1511 ASSERT_EQ(2U, encoded_signatures.size());
1512 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1513 const char kSignature2[] = "8308881815906226214";
1514 EXPECT_EQ(kSignature2, encoded_signatures[1]);
1515 const char kResponse2[] =
1516 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1517 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
1518 "<form signature=\"11337937696949187602\">"
1519 "<field signature=\"412125936\"/>"
1520 "<field signature=\"1917667676\"/>"
1521 "<field signature=\"2226358947\"/>"
1522 "<field signature=\"747221617\"/>"
1523 "<field signature=\"4108155786\"/>"
1524 "</form>"
1525 "<form signature=\"8308881815906226214\">"
1526 "<field signature=\"412125936\"/>"
1527 "<field signature=\"1917667676\"/>"
1528 "<field signature=\"2226358947\"/>"
1529 "<field signature=\"747221617\"/>"
1530 "<field signature=\"4108155786\"/>"
1531 "<field signature=\"509334676\"/>"
1532 "<field signature=\"509334676\"/>"
1533 "<field signature=\"509334676\"/>"
1534 "<field signature=\"509334676\"/>"
1535 "<field signature=\"509334676\"/>"
1536 "</form>"
1537 "</autofillquery>";
1538 EXPECT_EQ(kResponse2, encoded_xml);
1540 FormData malformed_form(form);
1541 // Add 50 address fields - the form is not valid anymore, but previous ones
1542 // are. The result should be the same as in previous test.
1543 for (size_t i = 0; i < 50; ++i) {
1544 field.label = ASCIIToUTF16("Address");
1545 field.name = ASCIIToUTF16("address");
1546 malformed_form.fields.push_back(field);
1549 forms.push_back(new FormStructure(malformed_form));
1550 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1551 &encoded_signatures,
1552 &encoded_xml));
1553 ASSERT_EQ(2U, encoded_signatures.size());
1554 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1555 EXPECT_EQ(kSignature2, encoded_signatures[1]);
1556 EXPECT_EQ(kResponse2, encoded_xml);
1558 // Check that we fail if there are only bad form(s).
1559 ScopedVector<FormStructure> bad_forms;
1560 bad_forms.push_back(new FormStructure(malformed_form));
1561 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(),
1562 &encoded_signatures,
1563 &encoded_xml));
1564 EXPECT_EQ(0U, encoded_signatures.size());
1565 EXPECT_EQ("", encoded_xml);
1568 TEST(FormStructureTest, EncodeUploadRequest) {
1569 scoped_ptr<FormStructure> form_structure;
1570 std::vector<ServerFieldTypeSet> possible_field_types;
1571 FormData form;
1572 form_structure.reset(new FormStructure(form));
1573 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1575 FormFieldData field;
1576 field.form_control_type = "text";
1578 field.label = ASCIIToUTF16("First Name");
1579 field.name = ASCIIToUTF16("firstname");
1580 form.fields.push_back(field);
1581 possible_field_types.push_back(ServerFieldTypeSet());
1582 possible_field_types.back().insert(NAME_FIRST);
1584 field.label = ASCIIToUTF16("Last Name");
1585 field.name = ASCIIToUTF16("lastname");
1586 form.fields.push_back(field);
1587 possible_field_types.push_back(ServerFieldTypeSet());
1588 possible_field_types.back().insert(NAME_LAST);
1590 field.label = ASCIIToUTF16("Email");
1591 field.name = ASCIIToUTF16("email");
1592 field.form_control_type = "email";
1593 form.fields.push_back(field);
1594 possible_field_types.push_back(ServerFieldTypeSet());
1595 possible_field_types.back().insert(EMAIL_ADDRESS);
1597 field.label = ASCIIToUTF16("Phone");
1598 field.name = ASCIIToUTF16("phone");
1599 field.form_control_type = "number";
1600 form.fields.push_back(field);
1601 possible_field_types.push_back(ServerFieldTypeSet());
1602 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER);
1604 field.label = ASCIIToUTF16("Country");
1605 field.name = ASCIIToUTF16("country");
1606 field.form_control_type = "select-one";
1607 form.fields.push_back(field);
1608 possible_field_types.push_back(ServerFieldTypeSet());
1609 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1611 // Add checkable field.
1612 FormFieldData checkable_field;
1613 checkable_field.is_checkable = true;
1614 checkable_field.label = ASCIIToUTF16("Checkable1");
1615 checkable_field.name = ASCIIToUTF16("Checkable1");
1616 form.fields.push_back(checkable_field);
1617 possible_field_types.push_back(ServerFieldTypeSet());
1618 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1620 form_structure.reset(new FormStructure(form));
1622 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1623 for (size_t i = 0; i < form_structure->field_count(); ++i)
1624 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1626 ServerFieldTypeSet available_field_types;
1627 available_field_types.insert(NAME_FIRST);
1628 available_field_types.insert(NAME_LAST);
1629 available_field_types.insert(ADDRESS_HOME_LINE1);
1630 available_field_types.insert(ADDRESS_HOME_LINE2);
1631 available_field_types.insert(ADDRESS_HOME_COUNTRY);
1632 available_field_types.insert(ADDRESS_BILLING_LINE1);
1633 available_field_types.insert(ADDRESS_BILLING_LINE2);
1634 available_field_types.insert(EMAIL_ADDRESS);
1635 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
1637 std::string encoded_xml;
1638 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
1639 &encoded_xml));
1640 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1641 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" "
1642 "formsignature=\"8736493185895608956\" autofillused=\"false\" "
1643 "datapresent=\"144200030e\">"
1644 "<field signature=\"3763331450\" autofilltype=\"3\"/>"
1645 "<field signature=\"3494530716\" autofilltype=\"5\"/>"
1646 "<field signature=\"1029417091\" autofilltype=\"9\"/>"
1647 "<field signature=\"466116101\" autofilltype=\"14\"/>"
1648 "<field signature=\"2799270304\" autofilltype=\"36\"/>"
1649 "</autofillupload>",
1650 encoded_xml);
1651 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true,
1652 &encoded_xml));
1653 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1654 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" "
1655 "formsignature=\"8736493185895608956\" autofillused=\"true\" "
1656 "datapresent=\"144200030e\">"
1657 "<field signature=\"3763331450\" autofilltype=\"3\"/>"
1658 "<field signature=\"3494530716\" autofilltype=\"5\"/>"
1659 "<field signature=\"1029417091\" autofilltype=\"9\"/>"
1660 "<field signature=\"466116101\" autofilltype=\"14\"/>"
1661 "<field signature=\"2799270304\" autofilltype=\"36\"/>"
1662 "</autofillupload>",
1663 encoded_xml);
1665 // Add 2 address fields - this should be still a valid form.
1666 for (size_t i = 0; i < 2; ++i) {
1667 field.label = ASCIIToUTF16("Address");
1668 field.name = ASCIIToUTF16("address");
1669 field.form_control_type = "text";
1670 form.fields.push_back(field);
1671 possible_field_types.push_back(ServerFieldTypeSet());
1672 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
1673 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
1674 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
1675 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
1678 form_structure.reset(new FormStructure(form));
1679 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1680 for (size_t i = 0; i < form_structure->field_count(); ++i)
1681 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1683 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
1684 &encoded_xml));
1685 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1686 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" "
1687 "formsignature=\"7816485729218079147\" autofillused=\"false\" "
1688 "datapresent=\"144200030e\">"
1689 "<field signature=\"3763331450\" autofilltype=\"3\"/>"
1690 "<field signature=\"3494530716\" autofilltype=\"5\"/>"
1691 "<field signature=\"1029417091\" autofilltype=\"9\"/>"
1692 "<field signature=\"466116101\" autofilltype=\"14\"/>"
1693 "<field signature=\"2799270304\" autofilltype=\"36\"/>"
1694 "<field signature=\"509334676\" autofilltype=\"30\"/>"
1695 "<field signature=\"509334676\" autofilltype=\"31\"/>"
1696 "<field signature=\"509334676\" autofilltype=\"37\"/>"
1697 "<field signature=\"509334676\" autofilltype=\"38\"/>"
1698 "<field signature=\"509334676\" autofilltype=\"30\"/>"
1699 "<field signature=\"509334676\" autofilltype=\"31\"/>"
1700 "<field signature=\"509334676\" autofilltype=\"37\"/>"
1701 "<field signature=\"509334676\" autofilltype=\"38\"/>"
1702 "</autofillupload>",
1703 encoded_xml);
1705 // Add 50 address fields - now the form is invalid, as it has too many fields.
1706 for (size_t i = 0; i < 50; ++i) {
1707 field.label = ASCIIToUTF16("Address");
1708 field.name = ASCIIToUTF16("address");
1709 field.form_control_type = "text";
1710 form.fields.push_back(field);
1711 possible_field_types.push_back(ServerFieldTypeSet());
1712 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
1713 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
1714 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
1715 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
1717 form_structure.reset(new FormStructure(form));
1718 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1719 for (size_t i = 0; i < form_structure->field_count(); ++i)
1720 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1721 EXPECT_FALSE(form_structure->EncodeUploadRequest(available_field_types, false,
1722 &encoded_xml));
1725 TEST(FormStructureTest, EncodeFieldAssignments) {
1726 scoped_ptr<FormStructure> form_structure;
1727 std::vector<ServerFieldTypeSet> possible_field_types;
1728 FormData form;
1729 form_structure.reset(new FormStructure(form));
1730 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1732 FormFieldData field;
1733 field.form_control_type = "text";
1735 field.label = ASCIIToUTF16("First Name");
1736 field.name = ASCIIToUTF16("firstname");
1737 form.fields.push_back(field);
1738 possible_field_types.push_back(ServerFieldTypeSet());
1739 possible_field_types.back().insert(NAME_FIRST);
1741 field.label = ASCIIToUTF16("Last Name");
1742 field.name = ASCIIToUTF16("lastname");
1743 form.fields.push_back(field);
1744 possible_field_types.push_back(ServerFieldTypeSet());
1745 possible_field_types.back().insert(NAME_LAST);
1747 field.label = ASCIIToUTF16("Email");
1748 field.name = ASCIIToUTF16("email");
1749 field.form_control_type = "email";
1750 form.fields.push_back(field);
1751 possible_field_types.push_back(ServerFieldTypeSet());
1752 possible_field_types.back().insert(EMAIL_ADDRESS);
1754 field.label = ASCIIToUTF16("Phone");
1755 field.name = ASCIIToUTF16("phone");
1756 field.form_control_type = "number";
1757 form.fields.push_back(field);
1758 possible_field_types.push_back(ServerFieldTypeSet());
1759 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER);
1761 field.label = ASCIIToUTF16("Country");
1762 field.name = ASCIIToUTF16("country");
1763 field.form_control_type = "select-one";
1764 form.fields.push_back(field);
1765 possible_field_types.push_back(ServerFieldTypeSet());
1766 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1768 // Add checkable field.
1769 FormFieldData checkable_field;
1770 checkable_field.is_checkable = true;
1771 checkable_field.label = ASCIIToUTF16("Checkable1");
1772 checkable_field.name = ASCIIToUTF16("Checkable1");
1773 form.fields.push_back(checkable_field);
1774 possible_field_types.push_back(ServerFieldTypeSet());
1775 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1777 form_structure.reset(new FormStructure(form));
1779 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1780 for (size_t i = 0; i < form_structure->field_count(); ++i)
1781 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1783 ServerFieldTypeSet available_field_types;
1784 available_field_types.insert(NAME_FIRST);
1785 available_field_types.insert(NAME_LAST);
1786 available_field_types.insert(ADDRESS_HOME_LINE1);
1787 available_field_types.insert(ADDRESS_HOME_LINE2);
1788 available_field_types.insert(ADDRESS_HOME_COUNTRY);
1789 available_field_types.insert(ADDRESS_BILLING_LINE1);
1790 available_field_types.insert(ADDRESS_BILLING_LINE2);
1791 available_field_types.insert(EMAIL_ADDRESS);
1792 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
1794 std::string encoded_xml;
1795 EXPECT_TRUE(form_structure->EncodeFieldAssignments(
1796 available_field_types, &encoded_xml));
1797 EXPECT_EQ(
1798 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1799 "<fieldassignments formsignature=\"8736493185895608956\">"
1800 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>"
1801 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>"
1802 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>"
1803 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>"
1804 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>"
1805 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>"
1806 "</fieldassignments>",
1807 encoded_xml);
1809 // Add 2 address fields - this should be still a valid form.
1810 for (size_t i = 0; i < 2; ++i) {
1811 field.label = ASCIIToUTF16("Address");
1812 field.name = ASCIIToUTF16("address");
1813 field.form_control_type = "text";
1814 form.fields.push_back(field);
1815 possible_field_types.push_back(ServerFieldTypeSet());
1816 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
1817 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
1818 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
1819 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
1822 form_structure.reset(new FormStructure(form));
1823 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1824 for (size_t i = 0; i < form_structure->field_count(); ++i)
1825 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1827 EXPECT_TRUE(form_structure->EncodeFieldAssignments(
1828 available_field_types, &encoded_xml));
1829 EXPECT_EQ(
1830 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1831 "<fieldassignments formsignature=\"7816485729218079147\">"
1832 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>"
1833 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>"
1834 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>"
1835 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>"
1836 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>"
1837 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>"
1838 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>"
1839 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>"
1840 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>"
1841 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>"
1842 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>"
1843 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>"
1844 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>"
1845 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>"
1846 "</fieldassignments>",
1847 encoded_xml);
1850 // Check that we compute the "datapresent" string correctly for the given
1851 // |available_types|.
1852 TEST(FormStructureTest, CheckDataPresence) {
1853 FormData form;
1855 FormFieldData field;
1856 field.form_control_type = "text";
1858 field.label = ASCIIToUTF16("First Name");
1859 field.name = ASCIIToUTF16("first");
1860 form.fields.push_back(field);
1862 field.label = ASCIIToUTF16("Last Name");
1863 field.name = ASCIIToUTF16("last");
1864 form.fields.push_back(field);
1866 field.label = ASCIIToUTF16("Email");
1867 field.name = ASCIIToUTF16("email");
1868 form.fields.push_back(field);
1870 FormStructure form_structure(form);
1872 ServerFieldTypeSet unknown_type;
1873 unknown_type.insert(UNKNOWN_TYPE);
1874 for (size_t i = 0; i < form_structure.field_count(); ++i)
1875 form_structure.field(i)->set_possible_types(unknown_type);
1877 // No available types.
1878 // datapresent should be "" == trimmmed(0x0000000000000000) ==
1879 // 0b0000000000000000000000000000000000000000000000000000000000000000
1880 ServerFieldTypeSet available_field_types;
1882 std::string encoded_xml;
1883 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
1884 &encoded_xml));
1885 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1886 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1887 " formsignature=\"6402244543831589061\" autofillused=\"false\""
1888 " datapresent=\"\">"
1889 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
1890 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
1891 "<field signature=\"420638584\" autofilltype=\"1\"/>"
1892 "</autofillupload>",
1893 encoded_xml);
1895 // Only a few types available.
1896 // datapresent should be "1540000240" == trimmmed(0x1540000240000000) ==
1897 // 0b0001010101000000000000000000001001000000000000000000000000000000
1898 // The set bits are:
1899 // 3 == NAME_FIRST
1900 // 5 == NAME_LAST
1901 // 7 == NAME_FULL
1902 // 9 == EMAIL_ADDRESS
1903 // 30 == ADDRESS_HOME_LINE1
1904 // 33 == ADDRESS_HOME_CITY
1905 available_field_types.clear();
1906 available_field_types.insert(NAME_FIRST);
1907 available_field_types.insert(NAME_LAST);
1908 available_field_types.insert(NAME_FULL);
1909 available_field_types.insert(EMAIL_ADDRESS);
1910 available_field_types.insert(ADDRESS_HOME_LINE1);
1911 available_field_types.insert(ADDRESS_HOME_CITY);
1913 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
1914 &encoded_xml));
1915 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1916 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1917 " formsignature=\"6402244543831589061\" autofillused=\"false\""
1918 " datapresent=\"1540000240\">"
1919 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
1920 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
1921 "<field signature=\"420638584\" autofilltype=\"1\"/>"
1922 "</autofillupload>",
1923 encoded_xml);
1925 // All supported non-credit card types available.
1926 // datapresent should be "1f7e000378000008" == trimmmed(0x1f7e000378000008) ==
1927 // 0b0001111101111110000000000000001101111000000000000000000000001000
1928 // The set bits are:
1929 // 3 == NAME_FIRST
1930 // 4 == NAME_MIDDLE
1931 // 5 == NAME_LAST
1932 // 6 == NAME_MIDDLE_INITIAL
1933 // 7 == NAME_FULL
1934 // 9 == EMAIL_ADDRESS
1935 // 10 == PHONE_HOME_NUMBER,
1936 // 11 == PHONE_HOME_CITY_CODE,
1937 // 12 == PHONE_HOME_COUNTRY_CODE,
1938 // 13 == PHONE_HOME_CITY_AND_NUMBER,
1939 // 14 == PHONE_HOME_WHOLE_NUMBER,
1940 // 30 == ADDRESS_HOME_LINE1
1941 // 31 == ADDRESS_HOME_LINE2
1942 // 33 == ADDRESS_HOME_CITY
1943 // 34 == ADDRESS_HOME_STATE
1944 // 35 == ADDRESS_HOME_ZIP
1945 // 36 == ADDRESS_HOME_COUNTRY
1946 // 60 == COMPANY_NAME
1947 available_field_types.clear();
1948 available_field_types.insert(NAME_FIRST);
1949 available_field_types.insert(NAME_MIDDLE);
1950 available_field_types.insert(NAME_LAST);
1951 available_field_types.insert(NAME_MIDDLE_INITIAL);
1952 available_field_types.insert(NAME_FULL);
1953 available_field_types.insert(EMAIL_ADDRESS);
1954 available_field_types.insert(PHONE_HOME_NUMBER);
1955 available_field_types.insert(PHONE_HOME_CITY_CODE);
1956 available_field_types.insert(PHONE_HOME_COUNTRY_CODE);
1957 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER);
1958 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
1959 available_field_types.insert(ADDRESS_HOME_LINE1);
1960 available_field_types.insert(ADDRESS_HOME_LINE2);
1961 available_field_types.insert(ADDRESS_HOME_CITY);
1962 available_field_types.insert(ADDRESS_HOME_STATE);
1963 available_field_types.insert(ADDRESS_HOME_ZIP);
1964 available_field_types.insert(ADDRESS_HOME_COUNTRY);
1965 available_field_types.insert(COMPANY_NAME);
1967 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
1968 &encoded_xml));
1969 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1970 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1971 " formsignature=\"6402244543831589061\" autofillused=\"false\""
1972 " datapresent=\"1f7e000378000008\">"
1973 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
1974 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
1975 "<field signature=\"420638584\" autofilltype=\"1\"/>"
1976 "</autofillupload>",
1977 encoded_xml);
1979 // All supported credit card types available.
1980 // datapresent should be "0000000000001fc0" == trimmmed(0x0000000000001fc0) ==
1981 // 0b0000000000000000000000000000000000000000000000000001111111000000
1982 // The set bits are:
1983 // 51 == CREDIT_CARD_NAME
1984 // 52 == CREDIT_CARD_NUMBER
1985 // 53 == CREDIT_CARD_EXP_MONTH
1986 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR
1987 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR
1988 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
1989 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR
1990 available_field_types.clear();
1991 available_field_types.insert(CREDIT_CARD_NAME);
1992 available_field_types.insert(CREDIT_CARD_NUMBER);
1993 available_field_types.insert(CREDIT_CARD_EXP_MONTH);
1994 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
1995 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
1996 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR);
1997 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
1999 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2000 &encoded_xml));
2001 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2002 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2003 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2004 " datapresent=\"0000000000001fc0\">"
2005 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
2006 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
2007 "<field signature=\"420638584\" autofilltype=\"1\"/>"
2008 "</autofillupload>",
2009 encoded_xml);
2011 // All supported types available.
2012 // datapresent should be "1f7e000378001fc8" == trimmmed(0x1f7e000378001fc8) ==
2013 // 0b0001111101111110000000000000001101111000000000000001111111001000
2014 // The set bits are:
2015 // 3 == NAME_FIRST
2016 // 4 == NAME_MIDDLE
2017 // 5 == NAME_LAST
2018 // 6 == NAME_MIDDLE_INITIAL
2019 // 7 == NAME_FULL
2020 // 9 == EMAIL_ADDRESS
2021 // 10 == PHONE_HOME_NUMBER,
2022 // 11 == PHONE_HOME_CITY_CODE,
2023 // 12 == PHONE_HOME_COUNTRY_CODE,
2024 // 13 == PHONE_HOME_CITY_AND_NUMBER,
2025 // 14 == PHONE_HOME_WHOLE_NUMBER,
2026 // 30 == ADDRESS_HOME_LINE1
2027 // 31 == ADDRESS_HOME_LINE2
2028 // 33 == ADDRESS_HOME_CITY
2029 // 34 == ADDRESS_HOME_STATE
2030 // 35 == ADDRESS_HOME_ZIP
2031 // 36 == ADDRESS_HOME_COUNTRY
2032 // 51 == CREDIT_CARD_NAME
2033 // 52 == CREDIT_CARD_NUMBER
2034 // 53 == CREDIT_CARD_EXP_MONTH
2035 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR
2036 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR
2037 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
2038 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR
2039 // 60 == COMPANY_NAME
2040 available_field_types.clear();
2041 available_field_types.insert(NAME_FIRST);
2042 available_field_types.insert(NAME_MIDDLE);
2043 available_field_types.insert(NAME_LAST);
2044 available_field_types.insert(NAME_MIDDLE_INITIAL);
2045 available_field_types.insert(NAME_FULL);
2046 available_field_types.insert(EMAIL_ADDRESS);
2047 available_field_types.insert(PHONE_HOME_NUMBER);
2048 available_field_types.insert(PHONE_HOME_CITY_CODE);
2049 available_field_types.insert(PHONE_HOME_COUNTRY_CODE);
2050 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER);
2051 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
2052 available_field_types.insert(ADDRESS_HOME_LINE1);
2053 available_field_types.insert(ADDRESS_HOME_LINE2);
2054 available_field_types.insert(ADDRESS_HOME_CITY);
2055 available_field_types.insert(ADDRESS_HOME_STATE);
2056 available_field_types.insert(ADDRESS_HOME_ZIP);
2057 available_field_types.insert(ADDRESS_HOME_COUNTRY);
2058 available_field_types.insert(CREDIT_CARD_NAME);
2059 available_field_types.insert(CREDIT_CARD_NUMBER);
2060 available_field_types.insert(CREDIT_CARD_EXP_MONTH);
2061 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2062 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2063 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR);
2064 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2065 available_field_types.insert(COMPANY_NAME);
2067 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2068 &encoded_xml));
2069 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2070 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2071 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2072 " datapresent=\"1f7e000378001fc8\">"
2073 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
2074 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
2075 "<field signature=\"420638584\" autofilltype=\"1\"/>"
2076 "</autofillupload>",
2077 encoded_xml);
2080 TEST(FormStructureTest, CheckMultipleTypes) {
2081 // Throughout this test, datapresent should be
2082 // 0x1440000360000008 ==
2083 // 0b0001010001000000000000000000001101100000000000000000000000001000
2084 // The set bits are:
2085 // 3 == NAME_FIRST
2086 // 5 == NAME_LAST
2087 // 9 == EMAIL_ADDRESS
2088 // 30 == ADDRESS_HOME_LINE1
2089 // 31 == ADDRESS_HOME_LINE2
2090 // 33 == ADDRESS_HOME_CITY
2091 // 34 == ADDRESS_HOME_STATE
2092 // 60 == COMPANY_NAME
2093 ServerFieldTypeSet available_field_types;
2094 available_field_types.insert(NAME_FIRST);
2095 available_field_types.insert(NAME_LAST);
2096 available_field_types.insert(EMAIL_ADDRESS);
2097 available_field_types.insert(ADDRESS_HOME_LINE1);
2098 available_field_types.insert(ADDRESS_HOME_LINE2);
2099 available_field_types.insert(ADDRESS_HOME_CITY);
2100 available_field_types.insert(ADDRESS_HOME_STATE);
2101 available_field_types.insert(COMPANY_NAME);
2103 // Check that multiple types for the field are processed correctly.
2104 scoped_ptr<FormStructure> form_structure;
2105 std::vector<ServerFieldTypeSet> possible_field_types;
2106 FormData form;
2108 FormFieldData field;
2109 field.form_control_type = "text";
2111 field.label = ASCIIToUTF16("email");
2112 field.name = ASCIIToUTF16("email");
2113 form.fields.push_back(field);
2114 possible_field_types.push_back(ServerFieldTypeSet());
2115 possible_field_types.back().insert(EMAIL_ADDRESS);
2117 field.label = ASCIIToUTF16("First Name");
2118 field.name = ASCIIToUTF16("first");
2119 form.fields.push_back(field);
2120 possible_field_types.push_back(ServerFieldTypeSet());
2121 possible_field_types.back().insert(NAME_FIRST);
2123 field.label = ASCIIToUTF16("Last Name");
2124 field.name = ASCIIToUTF16("last");
2125 form.fields.push_back(field);
2126 possible_field_types.push_back(ServerFieldTypeSet());
2127 possible_field_types.back().insert(NAME_LAST);
2129 field.label = ASCIIToUTF16("Address");
2130 field.name = ASCIIToUTF16("address");
2131 form.fields.push_back(field);
2132 possible_field_types.push_back(ServerFieldTypeSet());
2133 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
2135 form_structure.reset(new FormStructure(form));
2137 for (size_t i = 0; i < form_structure->field_count(); ++i)
2138 form_structure->field(i)->set_possible_types(possible_field_types[i]);
2139 std::string encoded_xml;
2141 // Now we matched both fields singularly.
2142 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2143 &encoded_xml));
2144 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2145 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2146 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2147 " datapresent=\"1440000360000008\">"
2148 "<field signature=\"420638584\" autofilltype=\"9\"/>"
2149 "<field signature=\"1089846351\" autofilltype=\"3\"/>"
2150 "<field signature=\"2404144663\" autofilltype=\"5\"/>"
2151 "<field signature=\"509334676\" autofilltype=\"30\"/>"
2152 "</autofillupload>",
2153 encoded_xml);
2154 // Match third field as both first and last.
2155 possible_field_types[2].insert(NAME_FIRST);
2156 form_structure->field(2)->set_possible_types(possible_field_types[2]);
2157 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2158 &encoded_xml));
2159 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2160 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2161 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2162 " datapresent=\"1440000360000008\">"
2163 "<field signature=\"420638584\" autofilltype=\"9\"/>"
2164 "<field signature=\"1089846351\" autofilltype=\"3\"/>"
2165 "<field signature=\"2404144663\" autofilltype=\"3\"/>"
2166 "<field signature=\"2404144663\" autofilltype=\"5\"/>"
2167 "<field signature=\"509334676\" autofilltype=\"30\"/>"
2168 "</autofillupload>",
2169 encoded_xml);
2170 possible_field_types[3].insert(ADDRESS_HOME_LINE2);
2171 form_structure->field(form_structure->field_count() - 1)->set_possible_types(
2172 possible_field_types[form_structure->field_count() - 1]);
2173 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2174 &encoded_xml));
2175 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2176 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2177 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2178 " datapresent=\"1440000360000008\">"
2179 "<field signature=\"420638584\" autofilltype=\"9\"/>"
2180 "<field signature=\"1089846351\" autofilltype=\"3\"/>"
2181 "<field signature=\"2404144663\" autofilltype=\"3\"/>"
2182 "<field signature=\"2404144663\" autofilltype=\"5\"/>"
2183 "<field signature=\"509334676\" autofilltype=\"30\"/>"
2184 "<field signature=\"509334676\" autofilltype=\"31\"/>"
2185 "</autofillupload>",
2186 encoded_xml);
2187 possible_field_types[3].clear();
2188 possible_field_types[3].insert(ADDRESS_HOME_LINE1);
2189 possible_field_types[3].insert(COMPANY_NAME);
2190 form_structure->field(form_structure->field_count() - 1)->set_possible_types(
2191 possible_field_types[form_structure->field_count() - 1]);
2192 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2193 &encoded_xml));
2194 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2195 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2196 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2197 " datapresent=\"1440000360000008\">"
2198 "<field signature=\"420638584\" autofilltype=\"9\"/>"
2199 "<field signature=\"1089846351\" autofilltype=\"3\"/>"
2200 "<field signature=\"2404144663\" autofilltype=\"3\"/>"
2201 "<field signature=\"2404144663\" autofilltype=\"5\"/>"
2202 "<field signature=\"509334676\" autofilltype=\"30\"/>"
2203 "<field signature=\"509334676\" autofilltype=\"60\"/>"
2204 "</autofillupload>",
2205 encoded_xml);
2208 TEST(FormStructureTest, CheckFormSignature) {
2209 // Check that form signature is created correctly.
2210 scoped_ptr<FormStructure> form_structure;
2211 FormData form;
2213 FormFieldData field;
2214 field.form_control_type = "text";
2216 field.label = ASCIIToUTF16("email");
2217 field.name = ASCIIToUTF16("email");
2218 form.fields.push_back(field);
2220 field.label = ASCIIToUTF16("First Name");
2221 field.name = ASCIIToUTF16("first");
2222 form.fields.push_back(field);
2224 // Checkable fields shouldn't affect the signature.
2225 field.label = ASCIIToUTF16("Select");
2226 field.name = ASCIIToUTF16("Select");
2227 field.form_control_type = "checkbox";
2228 field.is_checkable = true;
2229 form.fields.push_back(field);
2231 form_structure.reset(new FormStructure(form));
2233 EXPECT_EQ(FormStructureTest::Hash64Bit(
2234 std::string("://&&email&first")),
2235 form_structure->FormSignature());
2237 form.origin = GURL(std::string("http://www.facebook.com"));
2238 form_structure.reset(new FormStructure(form));
2239 EXPECT_EQ(FormStructureTest::Hash64Bit(
2240 std::string("http://www.facebook.com&&email&first")),
2241 form_structure->FormSignature());
2243 form.action = GURL(std::string("https://login.facebook.com/path"));
2244 form_structure.reset(new FormStructure(form));
2245 EXPECT_EQ(FormStructureTest::Hash64Bit(
2246 std::string("https://login.facebook.com&&email&first")),
2247 form_structure->FormSignature());
2249 form.name = ASCIIToUTF16("login_form");
2250 form_structure.reset(new FormStructure(form));
2251 EXPECT_EQ(FormStructureTest::Hash64Bit(
2252 std::string("https://login.facebook.com&login_form&email&first")),
2253 form_structure->FormSignature());
2255 field.is_checkable = false;
2256 field.label = ASCIIToUTF16("Random Field label");
2257 field.name = ASCIIToUTF16("random1234");
2258 field.form_control_type = "text";
2259 form.fields.push_back(field);
2260 field.label = ASCIIToUTF16("Random Field label2");
2261 field.name = ASCIIToUTF16("random12345");
2262 form.fields.push_back(field);
2263 field.label = ASCIIToUTF16("Random Field label3");
2264 field.name = ASCIIToUTF16("1random12345678");
2265 form.fields.push_back(field);
2266 field.label = ASCIIToUTF16("Random Field label3");
2267 field.name = ASCIIToUTF16("12345random");
2268 form.fields.push_back(field);
2269 form_structure.reset(new FormStructure(form));
2270 EXPECT_EQ(FormStructureTest::Hash64Bit(
2271 std::string("https://login.facebook.com&login_form&email&first&"
2272 "random1234&random&1random&random")),
2273 form_structure->FormSignature());
2277 TEST(FormStructureTest, ToFormData) {
2278 FormData form;
2279 form.name = ASCIIToUTF16("the-name");
2280 form.origin = GURL("http://cool.com");
2281 form.action = form.origin.Resolve("/login");
2283 FormFieldData field;
2284 field.label = ASCIIToUTF16("username");
2285 field.name = ASCIIToUTF16("username");
2286 field.form_control_type = "text";
2287 form.fields.push_back(field);
2289 field.label = ASCIIToUTF16("password");
2290 field.name = ASCIIToUTF16("password");
2291 field.form_control_type = "password";
2292 form.fields.push_back(field);
2294 field.label = base::string16();
2295 field.name = ASCIIToUTF16("Submit");
2296 field.form_control_type = "submit";
2297 form.fields.push_back(field);
2299 EXPECT_TRUE(form.SameFormAs(FormStructure(form).ToFormData()));
2301 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always
2302 // false. This forces a future author that changes this to update this test.
2303 form.user_submitted = true;
2304 EXPECT_FALSE(form.SameFormAs(FormStructure(form).ToFormData()));
2307 TEST(FormStructureTest, SkipFieldTest) {
2308 FormData form;
2309 form.name = ASCIIToUTF16("the-name");
2310 form.origin = GURL("http://cool.com");
2311 form.action = form.origin.Resolve("/login");
2313 FormFieldData field;
2314 field.label = ASCIIToUTF16("username");
2315 field.name = ASCIIToUTF16("username");
2316 field.form_control_type = "text";
2317 form.fields.push_back(field);
2319 field.label = ASCIIToUTF16("select");
2320 field.name = ASCIIToUTF16("select");
2321 field.form_control_type = "checkbox";
2322 field.is_checkable = true;
2323 form.fields.push_back(field);
2325 field.label = base::string16();
2326 field.name = ASCIIToUTF16("email");
2327 field.form_control_type = "text";
2328 field.is_checkable = false;
2329 form.fields.push_back(field);
2331 ScopedVector<FormStructure> forms;
2332 forms.push_back(new FormStructure(form));
2333 std::vector<std::string> encoded_signatures;
2334 std::string encoded_xml;
2336 const char kSignature[] = "18006745212084723782";
2337 const char kResponse[] =
2338 "<\?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2339 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
2340 "<form signature=\"18006745212084723782\">"
2341 "<field signature=\"239111655\"/>"
2342 "<field signature=\"420638584\"/>"
2343 "</form>"
2344 "</autofillquery>";
2345 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
2346 &encoded_signatures,
2347 &encoded_xml));
2348 ASSERT_EQ(1U, encoded_signatures.size());
2349 EXPECT_EQ(kSignature, encoded_signatures[0]);
2350 EXPECT_EQ(kResponse, encoded_xml);
2353 TEST(FormStructureTest, PossibleValues) {
2354 FormData form_data;
2355 FormFieldData field;
2356 field.autocomplete_attribute = "billing country";
2357 field.option_contents.push_back(ASCIIToUTF16("Down Under"));
2358 field.option_values.push_back(ASCIIToUTF16("AU"));
2359 field.option_contents.push_back(ASCIIToUTF16("Fr"));
2360 field.option_values.push_back(ASCIIToUTF16(""));
2361 field.option_contents.push_back(ASCIIToUTF16("Germany"));
2362 field.option_values.push_back(ASCIIToUTF16("GRMNY"));
2363 form_data.fields.push_back(field);
2364 FormStructure form_structure(form_data);
2366 bool unused;
2367 form_structure.ParseFieldTypesFromAutocompleteAttributes(&unused, &unused);
2369 // All values in <option> value= or contents are returned, set to upper case.
2370 std::set<base::string16> possible_values =
2371 form_structure.PossibleValues(ADDRESS_BILLING_COUNTRY);
2372 EXPECT_EQ(5U, possible_values.size());
2373 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("AU")));
2374 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("FR")));
2375 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("DOWN UNDER")));
2376 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("GERMANY")));
2377 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("GRMNY")));
2378 EXPECT_EQ(0U, possible_values.count(ASCIIToUTF16("Fr")));
2379 EXPECT_EQ(0U, possible_values.count(ASCIIToUTF16("DE")));
2381 // No field for the given type; empty value set.
2382 EXPECT_EQ(0U, form_structure.PossibleValues(ADDRESS_HOME_COUNTRY).size());
2384 // A freeform input (<input>) allows any value (overriding other <select>s).
2385 FormFieldData freeform_field;
2386 freeform_field.autocomplete_attribute = "billing country";
2387 form_data.fields.push_back(freeform_field);
2388 FormStructure form_structure2(form_data);
2389 form_structure2.ParseFieldTypesFromAutocompleteAttributes(&unused, &unused);
2390 EXPECT_EQ(0U, form_structure2.PossibleValues(ADDRESS_BILLING_COUNTRY).size());
2393 TEST(FormStructureTest, ParseQueryResponse) {
2394 FormData form;
2395 FormFieldData field;
2396 field.form_control_type = "text";
2398 field.label = ASCIIToUTF16("fullname");
2399 field.name = ASCIIToUTF16("fullname");
2400 form.fields.push_back(field);
2402 field.label = ASCIIToUTF16("address");
2403 field.name = ASCIIToUTF16("address");
2404 form.fields.push_back(field);
2406 // Checkable fields should be ignored in parsing
2407 FormFieldData checkable_field;
2408 checkable_field.label = ASCIIToUTF16("radio_button");
2409 checkable_field.form_control_type = "radio";
2410 checkable_field.is_checkable = true;
2411 form.fields.push_back(checkable_field);
2413 ScopedVector<FormStructure> forms;
2414 forms.push_back(new FormStructure(form));
2416 field.label = ASCIIToUTF16("email");
2417 field.name = ASCIIToUTF16("email");
2418 form.fields.push_back(field);
2420 field.label = ASCIIToUTF16("password");
2421 field.name = ASCIIToUTF16("password");
2422 field.form_control_type = "password";
2423 form.fields.push_back(field);
2425 forms.push_back(new FormStructure(form));
2427 std::string response =
2428 "<autofillqueryresponse>"
2429 "<field autofilltype=\"7\" />"
2430 "<field autofilltype=\"30\" />"
2431 "<field autofilltype=\"9\" />"
2432 "<field autofilltype=\"0\" />"
2433 "</autofillqueryresponse>";
2435 FormStructure::ParseQueryResponse(response,
2436 forms.get(),
2437 TestAutofillMetrics());
2439 EXPECT_EQ(7, forms[0]->field(0)->server_type());
2440 EXPECT_EQ(30, forms[0]->field(1)->server_type());
2441 EXPECT_EQ(9, forms[1]->field(0)->server_type());
2442 EXPECT_EQ(0, forms[1]->field(1)->server_type());
2445 // If user defined types are present, only parse password fields.
2446 TEST(FormStructureTest, ParseQueryResponseAuthorDefinedTypes) {
2447 FormData form;
2448 FormFieldData field;
2450 field.label = ASCIIToUTF16("email");
2451 field.name = ASCIIToUTF16("email");
2452 field.form_control_type = "text";
2453 field.autocomplete_attribute = "email";
2454 form.fields.push_back(field);
2456 field.label = ASCIIToUTF16("password");
2457 field.name = ASCIIToUTF16("password");
2458 field.form_control_type = "password";
2459 field.autocomplete_attribute = "new-password";
2460 form.fields.push_back(field);
2462 ScopedVector<FormStructure> forms;
2463 forms.push_back(new FormStructure(form));
2464 forms.front()->DetermineHeuristicTypes(TestAutofillMetrics());
2466 std::string response =
2467 "<autofillqueryresponse>"
2468 "<field autofilltype=\"9\" />"
2469 "<field autofilltype=\"76\" />"
2470 "</autofillqueryresponse>";
2472 FormStructure::ParseQueryResponse(response,
2473 forms.get(),
2474 TestAutofillMetrics());
2476 EXPECT_EQ(NO_SERVER_DATA, forms[0]->field(0)->server_type());
2477 EXPECT_EQ(76, forms[0]->field(1)->server_type());
2480 } // namespace autofill