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/memory/scoped_ptr.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/browser/autofill_metrics.h"
11 #include "components/autofill/core/common/form_data.h"
12 #include "components/autofill/core/common/form_field_data.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/web/WebInputElement.h"
17 using WebKit::WebInputElement;
22 // Unlike the base AutofillMetrics, exposes copy and assignment constructors,
23 // which are handy for briefer test code. The AutofillMetrics class is
24 // stateless, so this is safe.
25 class TestAutofillMetrics : public AutofillMetrics {
27 TestAutofillMetrics() {}
28 virtual ~TestAutofillMetrics() {}
31 } // anonymous namespace
36 std::ostream& operator<<(std::ostream& os, const FormData& form) {
37 os << UTF16ToUTF8(form.name)
39 << UTF16ToUTF8(form.method)
46 for (std::vector<FormFieldData>::const_iterator iter =
48 iter != form.fields.end(); ++iter) {
56 } // namespace content
58 class FormStructureTest {
60 static std::string Hash64Bit(const std::string& str) {
61 return FormStructure::Hash64Bit(str);
65 TEST(FormStructureTest, FieldCount) {
66 scoped_ptr<FormStructure> form_structure;
68 form.method = ASCIIToUTF16("post");
71 field.label = ASCIIToUTF16("username");
72 field.name = ASCIIToUTF16("username");
73 field.form_control_type = "text";
74 form.fields.push_back(field);
76 field.label = ASCIIToUTF16("password");
77 field.name = ASCIIToUTF16("password");
78 field.form_control_type = "password";
79 form.fields.push_back(field);
81 field.label = base::string16();
82 field.name = ASCIIToUTF16("Submit");
83 field.form_control_type = "submit";
84 form.fields.push_back(field);
86 field.label = ASCIIToUTF16("address1");
87 field.name = ASCIIToUTF16("address1");
88 field.form_control_type = "text";
89 field.should_autocomplete = false;
90 form.fields.push_back(field);
92 // The render process sends all fields to browser including fields with
94 form_structure.reset(new FormStructure(form));
95 EXPECT_EQ(4U, form_structure->field_count());
98 TEST(FormStructureTest, AutofillCount) {
99 scoped_ptr<FormStructure> form_structure;
101 form.method = ASCIIToUTF16("post");
104 field.label = ASCIIToUTF16("username");
105 field.name = ASCIIToUTF16("username");
106 field.form_control_type = "text";
107 form.fields.push_back(field);
109 field.label = ASCIIToUTF16("password");
110 field.name = ASCIIToUTF16("password");
111 field.form_control_type = "password";
112 form.fields.push_back(field);
114 field.label = ASCIIToUTF16("state");
115 field.name = ASCIIToUTF16("state");
116 field.form_control_type = "select-one";
117 form.fields.push_back(field);
119 field.label = base::string16();
120 field.name = ASCIIToUTF16("Submit");
121 field.form_control_type = "submit";
122 form.fields.push_back(field);
124 // Only text and select fields that are heuristically matched are counted.
125 form_structure.reset(new FormStructure(form));
126 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
127 EXPECT_EQ(1U, form_structure->autofill_count());
129 // Add a field with should_autocomplete=false.
130 field.label = ASCIIToUTF16("address1");
131 field.name = ASCIIToUTF16("address1");
132 field.form_control_type = "text";
133 field.should_autocomplete = false;
134 form.fields.push_back(field);
136 form_structure.reset(new FormStructure(form));
137 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
138 // DetermineHeuristicTypes also assign field type for fields with
139 // autocomplete=off thus autofill_count includes them. This is a bug,
140 // and they should not be counted. See http://crbug.com/176432 for details.
141 // TODO(benquan): change it to EXPECT_EQ(1U, ... when the bug is fixed.
142 EXPECT_EQ(2U, form_structure->autofill_count());
145 TEST(FormStructureTest, SourceURL) {
147 form.origin = GURL("http://www.foo.com/");
148 form.method = ASCIIToUTF16("post");
149 FormStructure form_structure(form);
151 EXPECT_EQ(form.origin, form_structure.source_url());
154 TEST(FormStructureTest, IsAutofillable) {
155 scoped_ptr<FormStructure> form_structure;
158 // We need at least three text fields to be auto-fillable.
159 form.method = ASCIIToUTF16("post");
163 field.label = ASCIIToUTF16("username");
164 field.name = ASCIIToUTF16("username");
165 field.form_control_type = "text";
166 form.fields.push_back(field);
168 field.label = ASCIIToUTF16("password");
169 field.name = ASCIIToUTF16("password");
170 field.form_control_type = "password";
171 form.fields.push_back(field);
173 field.label = base::string16();
174 field.name = ASCIIToUTF16("Submit");
175 field.form_control_type = "submit";
176 form.fields.push_back(field);
178 form_structure.reset(new FormStructure(form));
179 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
180 EXPECT_FALSE(form_structure->IsAutofillable(true));
182 // We now have three text fields, but only two auto-fillable fields.
183 field.label = ASCIIToUTF16("First Name");
184 field.name = ASCIIToUTF16("firstname");
185 field.form_control_type = "text";
186 form.fields.push_back(field);
188 field.label = ASCIIToUTF16("Last Name");
189 field.name = ASCIIToUTF16("lastname");
190 field.form_control_type = "text";
191 form.fields.push_back(field);
193 form_structure.reset(new FormStructure(form));
194 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
195 EXPECT_FALSE(form_structure->IsAutofillable(true));
197 // We now have three auto-fillable fields.
198 field.label = ASCIIToUTF16("Email");
199 field.name = ASCIIToUTF16("email");
200 field.form_control_type = "email";
201 form.fields.push_back(field);
203 form_structure.reset(new FormStructure(form));
204 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
205 EXPECT_TRUE(form_structure->IsAutofillable(true));
207 // The method must be 'post', though we can intentionally ignore this
208 // criterion for the sake of providing a helpful warning message to the user.
209 form.method = ASCIIToUTF16("get");
210 form_structure.reset(new FormStructure(form));
211 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
212 EXPECT_FALSE(form_structure->IsAutofillable(true));
213 EXPECT_TRUE(form_structure->IsAutofillable(false));
215 // The target cannot include http(s)://*/search...
216 form.method = ASCIIToUTF16("post");
217 form.action = GURL("http://google.com/search?q=hello");
218 form_structure.reset(new FormStructure(form));
219 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
220 EXPECT_FALSE(form_structure->IsAutofillable(true));
222 // But search can be in the URL.
223 form.action = GURL("http://search.com/?q=hello");
224 form_structure.reset(new FormStructure(form));
225 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
226 EXPECT_TRUE(form_structure->IsAutofillable(true));
229 TEST(FormStructureTest, ShouldBeParsed) {
230 scoped_ptr<FormStructure> form_structure;
233 // We need at least three text fields to be parseable.
234 form.method = ASCIIToUTF16("post");
237 field.label = ASCIIToUTF16("username");
238 field.name = ASCIIToUTF16("username");
239 field.form_control_type = "text";
240 form.fields.push_back(field);
242 FormFieldData checkable_field;
243 checkable_field.is_checkable = true;
244 checkable_field.name = ASCIIToUTF16("radiobtn");
245 checkable_field.form_control_type = "radio";
246 form.fields.push_back(checkable_field);
248 checkable_field.name = ASCIIToUTF16("checkbox");
249 checkable_field.form_control_type = "checkbox";
250 form.fields.push_back(checkable_field);
252 // We have only one text field, should not be parsed.
253 form_structure.reset(new FormStructure(form));
254 EXPECT_FALSE(form_structure->ShouldBeParsed(true));
256 // We now have three text fields, though only two are auto-fillable.
257 field.label = ASCIIToUTF16("First Name");
258 field.name = ASCIIToUTF16("firstname");
259 field.form_control_type = "text";
260 form.fields.push_back(field);
262 field.label = ASCIIToUTF16("Last Name");
263 field.name = ASCIIToUTF16("lastname");
264 field.form_control_type = "text";
265 form.fields.push_back(field);
267 form_structure.reset(new FormStructure(form));
268 EXPECT_TRUE(form_structure->ShouldBeParsed(true));
270 // The method must be 'post', though we can intentionally ignore this
271 // criterion for the sake of providing a helpful warning message to the user.
272 form.method = ASCIIToUTF16("get");
273 form_structure.reset(new FormStructure(form));
274 EXPECT_FALSE(form_structure->IsAutofillable(true));
275 EXPECT_TRUE(form_structure->ShouldBeParsed(false));
277 // The target cannot include http(s)://*/search...
278 form.method = ASCIIToUTF16("post");
279 form.action = GURL("http://google.com/search?q=hello");
280 form_structure.reset(new FormStructure(form));
281 EXPECT_FALSE(form_structure->ShouldBeParsed(true));
283 // But search can be in the URL.
284 form.action = GURL("http://search.com/?q=hello");
285 form_structure.reset(new FormStructure(form));
286 EXPECT_TRUE(form_structure->ShouldBeParsed(true));
288 // The form need only have three fields, but at least one must be a text
292 field.label = ASCIIToUTF16("Email");
293 field.name = ASCIIToUTF16("email");
294 field.form_control_type = "email";
295 form.fields.push_back(field);
297 field.label = ASCIIToUTF16("State");
298 field.name = ASCIIToUTF16("state");
299 field.form_control_type = "select-one";
300 form.fields.push_back(field);
302 field.label = ASCIIToUTF16("Country");
303 field.name = ASCIIToUTF16("country");
304 field.form_control_type = "select-one";
305 form.fields.push_back(field);
307 form_structure.reset(new FormStructure(form));
308 EXPECT_TRUE(form_structure->ShouldBeParsed(true));
310 form.fields[0].form_control_type = "select-one";
311 // Now, no text fields.
312 form_structure.reset(new FormStructure(form));
313 EXPECT_FALSE(form_structure->ShouldBeParsed(true));
316 TEST(FormStructureTest, HeuristicsContactInfo) {
317 scoped_ptr<FormStructure> form_structure;
319 form.method = ASCIIToUTF16("post");
322 field.form_control_type = "text";
324 field.label = ASCIIToUTF16("First Name");
325 field.name = ASCIIToUTF16("firstname");
326 form.fields.push_back(field);
328 field.label = ASCIIToUTF16("Last Name");
329 field.name = ASCIIToUTF16("lastname");
330 form.fields.push_back(field);
332 field.label = ASCIIToUTF16("Email");
333 field.name = ASCIIToUTF16("email");
334 form.fields.push_back(field);
336 field.label = ASCIIToUTF16("Phone");
337 field.name = ASCIIToUTF16("phone");
338 form.fields.push_back(field);
340 field.label = ASCIIToUTF16("Address");
341 field.name = ASCIIToUTF16("address");
342 form.fields.push_back(field);
344 field.label = ASCIIToUTF16("City");
345 field.name = ASCIIToUTF16("city");
346 form.fields.push_back(field);
348 field.label = ASCIIToUTF16("Zip code");
349 field.name = ASCIIToUTF16("zipcode");
350 form.fields.push_back(field);
352 field.label = base::string16();
353 field.name = ASCIIToUTF16("Submit");
354 field.form_control_type = "submit";
355 form.fields.push_back(field);
357 form_structure.reset(new FormStructure(form));
358 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
359 EXPECT_TRUE(form_structure->IsAutofillable(true));
361 // Expect the correct number of fields.
362 ASSERT_EQ(8U, form_structure->field_count());
363 ASSERT_EQ(7U, form_structure->autofill_count());
366 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
368 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
370 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
372 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
373 form_structure->field(3)->heuristic_type());
375 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type());
377 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type());
379 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type());
381 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type());
384 // Verify that we can correctly process the |autocomplete| attribute.
385 TEST(FormStructureTest, HeuristicsAutocompleteAttribute) {
386 scoped_ptr<FormStructure> form_structure;
388 form.method = ASCIIToUTF16("post");
391 field.form_control_type = "text";
393 field.label = base::string16();
394 field.name = ASCIIToUTF16("field1");
395 field.autocomplete_attribute = "given-name";
396 form.fields.push_back(field);
398 field.label = base::string16();
399 field.name = ASCIIToUTF16("field2");
400 field.autocomplete_attribute = "family-name";
401 form.fields.push_back(field);
403 field.label = base::string16();
404 field.name = ASCIIToUTF16("field3");
405 field.autocomplete_attribute = "email";
406 form.fields.push_back(field);
408 form_structure.reset(new FormStructure(form));
409 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
410 EXPECT_TRUE(form_structure->IsAutofillable(true));
412 // Expect the correct number of fields.
413 ASSERT_EQ(3U, form_structure->field_count());
414 ASSERT_EQ(3U, form_structure->autofill_count());
416 EXPECT_EQ(HTML_TYPE_GIVEN_NAME, form_structure->field(0)->html_type());
417 EXPECT_EQ(HTML_TYPE_FAMILY_NAME, form_structure->field(1)->html_type());
418 EXPECT_EQ(HTML_TYPE_EMAIL, form_structure->field(2)->html_type());
419 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type());
420 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
421 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
424 // Verify that we can correctly process the 'autocomplete' attribute for phone
425 // number types (especially phone prefixes and suffixes).
426 TEST(FormStructureTest, HeuristicsAutocompleteAttributePhoneTypes) {
427 scoped_ptr<FormStructure> form_structure;
429 form.method = ASCIIToUTF16("post");
432 field.form_control_type = "text";
434 field.label = base::string16();
435 field.name = ASCIIToUTF16("field1");
436 field.autocomplete_attribute = "tel-local";
437 form.fields.push_back(field);
439 field.label = base::string16();
440 field.name = ASCIIToUTF16("field2");
441 field.autocomplete_attribute = "tel-local-prefix";
442 form.fields.push_back(field);
444 field.label = base::string16();
445 field.name = ASCIIToUTF16("field3");
446 field.autocomplete_attribute = "tel-local-suffix";
447 form.fields.push_back(field);
449 form_structure.reset(new FormStructure(form));
450 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
451 EXPECT_TRUE(form_structure->IsAutofillable(true));
453 // Expect the correct number of fields.
454 ASSERT_EQ(3U, form_structure->field_count());
455 EXPECT_EQ(3U, form_structure->autofill_count());
457 EXPECT_EQ(HTML_TYPE_TEL_LOCAL, form_structure->field(0)->html_type());
458 EXPECT_EQ(AutofillField::IGNORED, form_structure->field(0)->phone_part());
459 EXPECT_EQ(HTML_TYPE_TEL_LOCAL_PREFIX, form_structure->field(1)->html_type());
460 EXPECT_EQ(AutofillField::PHONE_PREFIX,
461 form_structure->field(1)->phone_part());
462 EXPECT_EQ(HTML_TYPE_TEL_LOCAL_SUFFIX, form_structure->field(2)->html_type());
463 EXPECT_EQ(AutofillField::PHONE_SUFFIX,
464 form_structure->field(2)->phone_part());
467 // If at least one field includes type hints in the 'autocomplete' attribute, we
468 // should not try to apply any other heuristics.
469 TEST(FormStructureTest, AutocompleteAttributeOverridesOtherHeuristics) {
470 scoped_ptr<FormStructure> form_structure;
472 form.method = ASCIIToUTF16("post");
474 // Start with a regular contact form.
476 field.form_control_type = "text";
478 field.label = ASCIIToUTF16("First Name");
479 field.name = ASCIIToUTF16("firstname");
480 form.fields.push_back(field);
482 field.label = ASCIIToUTF16("Last Name");
483 field.name = ASCIIToUTF16("lastname");
484 form.fields.push_back(field);
486 field.label = ASCIIToUTF16("Email");
487 field.name = ASCIIToUTF16("email");
488 form.fields.push_back(field);
490 form_structure.reset(new FormStructure(form));
491 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
492 EXPECT_TRUE(form_structure->IsAutofillable(true));
493 EXPECT_TRUE(form_structure->ShouldBeCrowdsourced());
495 ASSERT_EQ(3U, form_structure->field_count());
496 ASSERT_EQ(3U, form_structure->autofill_count());
498 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
499 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
500 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
502 // Now update the first form field to include an 'autocomplete' attribute.
503 form.fields.front().autocomplete_attribute = "x-other";
504 form_structure.reset(new FormStructure(form));
505 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
506 EXPECT_FALSE(form_structure->IsAutofillable(true));
507 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced());
509 ASSERT_EQ(3U, form_structure->field_count());
510 ASSERT_EQ(0U, form_structure->autofill_count());
512 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type());
513 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
514 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
517 // Verify that we can correctly process sections listed in the |autocomplete|
519 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSections) {
521 form.method = ASCIIToUTF16("post");
524 field.form_control_type = "text";
526 // Some fields will have no section specified. These fall into the default
528 field.autocomplete_attribute = "email";
529 form.fields.push_back(field);
531 // We allow arbitrary section names.
532 field.autocomplete_attribute = "section-foo email";
533 form.fields.push_back(field);
535 // "shipping" and "billing" are special section tokens that don't require the
536 // "section-" prefix.
537 field.autocomplete_attribute = "shipping email";
538 form.fields.push_back(field);
539 field.autocomplete_attribute = "billing email";
540 form.fields.push_back(field);
542 // "shipping" and "billing" can be combined with other section names.
543 field.autocomplete_attribute = "section-foo shipping email";
544 form.fields.push_back(field);
545 field.autocomplete_attribute = "section-foo billing email";
546 form.fields.push_back(field);
548 // We don't do anything clever to try to coalesce sections; it's up to site
549 // authors to avoid typos.
550 field.autocomplete_attribute = "section--foo email";
551 form.fields.push_back(field);
553 // "shipping email" and "section--shipping" email should be parsed as
554 // different sections. This is only an interesting test due to how we
555 // implement implicit section names from attributes like "shipping email"; see
556 // the implementation for more details.
557 field.autocomplete_attribute = "section--shipping email";
558 form.fields.push_back(field);
560 // Credit card fields are implicitly in a separate section from other fields.
561 field.autocomplete_attribute = "section-foo cc-number";
562 form.fields.push_back(field);
564 FormStructure form_structure(form);
565 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
566 EXPECT_TRUE(form_structure.IsAutofillable(true));
568 // Expect the correct number of fields.
569 ASSERT_EQ(9U, form_structure.field_count());
570 EXPECT_EQ(9U, form_structure.autofill_count());
572 // All of the fields in this form should be parsed as belonging to different
574 std::set<std::string> section_names;
575 for (size_t i = 0; i < 9; ++i) {
576 section_names.insert(form_structure.field(i)->section());
578 EXPECT_EQ(9U, section_names.size());
581 // Verify that we can correctly process a degenerate section listed in the
582 // |autocomplete| attribute.
583 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsDegenerate) {
585 form.method = ASCIIToUTF16("post");
588 field.form_control_type = "text";
590 // Some fields will have no section specified. These fall into the default
592 field.autocomplete_attribute = "email";
593 form.fields.push_back(field);
595 // Specifying "section-" is equivalent to not specifying a section.
596 field.autocomplete_attribute = "section- email";
597 form.fields.push_back(field);
599 // Invalid tokens should prevent us from setting a section name.
600 field.autocomplete_attribute = "garbage section-foo email";
601 form.fields.push_back(field);
602 field.autocomplete_attribute = "garbage section-bar email";
603 form.fields.push_back(field);
604 field.autocomplete_attribute = "garbage shipping email";
605 form.fields.push_back(field);
606 field.autocomplete_attribute = "garbage billing email";
607 form.fields.push_back(field);
609 FormStructure form_structure(form);
610 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
612 // Expect the correct number of fields.
613 ASSERT_EQ(6U, form_structure.field_count());
614 EXPECT_EQ(2U, form_structure.autofill_count());
616 // All of the fields in this form should be parsed as belonging to the same
618 std::set<std::string> section_names;
619 for (size_t i = 0; i < 6; ++i) {
620 section_names.insert(form_structure.field(i)->section());
622 EXPECT_EQ(1U, section_names.size());
625 // Verify that we can correctly process repeated sections listed in the
626 // |autocomplete| attribute.
627 TEST(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsRepeated) {
629 form.method = ASCIIToUTF16("post");
632 field.form_control_type = "text";
634 field.autocomplete_attribute = "section-foo email";
635 form.fields.push_back(field);
636 field.autocomplete_attribute = "section-foo address-line1";
637 form.fields.push_back(field);
639 FormStructure form_structure(form);
640 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
642 // Expect the correct number of fields.
643 ASSERT_EQ(2U, form_structure.field_count());
644 EXPECT_EQ(2U, form_structure.autofill_count());
646 // All of the fields in this form should be parsed as belonging to the same
648 std::set<std::string> section_names;
649 for (size_t i = 0; i < 2; ++i) {
650 section_names.insert(form_structure.field(i)->section());
652 EXPECT_EQ(1U, section_names.size());
655 // Verify that we do not override the author-specified sections from a form with
657 TEST(FormStructureTest, HeuristicsDontOverrideAutocompleteAttributeSections) {
659 form.method = ASCIIToUTF16("post");
662 field.form_control_type = "text";
664 field.name = ASCIIToUTF16("one");
665 field.autocomplete_attribute = "address-line1";
666 form.fields.push_back(field);
667 field.name = base::string16();
668 field.autocomplete_attribute = "section-foo email";
669 form.fields.push_back(field);
670 field.name = base::string16();
671 field.autocomplete_attribute = "name";
672 form.fields.push_back(field);
673 field.name = ASCIIToUTF16("two");
674 field.autocomplete_attribute = "address-line1";
675 form.fields.push_back(field);
677 FormStructure form_structure(form);
678 form_structure.DetermineHeuristicTypes(TestAutofillMetrics());
680 // Expect the correct number of fields.
681 ASSERT_EQ(4U, form_structure.field_count());
682 EXPECT_EQ(4U, form_structure.autofill_count());
684 // Normally, the two separate address fields would cause us to detect two
685 // separate sections; but because there is an author-specified section in this
686 // form, we do not apply these usual heuristics.
687 EXPECT_EQ(ASCIIToUTF16("one"), form_structure.field(0)->name);
688 EXPECT_EQ(ASCIIToUTF16("two"), form_structure.field(3)->name);
689 EXPECT_EQ(form_structure.field(0)->section(),
690 form_structure.field(3)->section());
693 TEST(FormStructureTest, HeuristicsSample8) {
694 scoped_ptr<FormStructure> form_structure;
696 form.method = ASCIIToUTF16("post");
699 field.form_control_type = "text";
701 field.label = ASCIIToUTF16("Your First Name:");
702 field.name = ASCIIToUTF16("bill.first");
703 form.fields.push_back(field);
705 field.label = ASCIIToUTF16("Your Last Name:");
706 field.name = ASCIIToUTF16("bill.last");
707 form.fields.push_back(field);
709 field.label = ASCIIToUTF16("Street Address Line 1:");
710 field.name = ASCIIToUTF16("bill.street1");
711 form.fields.push_back(field);
713 field.label = ASCIIToUTF16("Street Address Line 2:");
714 field.name = ASCIIToUTF16("bill.street2");
715 form.fields.push_back(field);
717 field.label = ASCIIToUTF16("City");
718 field.name = ASCIIToUTF16("bill.city");
719 form.fields.push_back(field);
721 field.label = ASCIIToUTF16("State (U.S.):");
722 field.name = ASCIIToUTF16("bill.state");
723 form.fields.push_back(field);
725 field.label = ASCIIToUTF16("Zip/Postal Code:");
726 field.name = ASCIIToUTF16("BillTo.PostalCode");
727 form.fields.push_back(field);
729 field.label = ASCIIToUTF16("Country:");
730 field.name = ASCIIToUTF16("bill.country");
731 form.fields.push_back(field);
733 field.label = ASCIIToUTF16("Phone Number:");
734 field.name = ASCIIToUTF16("BillTo.Phone");
735 form.fields.push_back(field);
737 field.label = base::string16();
738 field.name = ASCIIToUTF16("Submit");
739 field.form_control_type = "submit";
740 form.fields.push_back(field);
742 form_structure.reset(new FormStructure(form));
743 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
744 EXPECT_TRUE(form_structure->IsAutofillable(true));
745 ASSERT_EQ(10U, form_structure->field_count());
746 ASSERT_EQ(9U, form_structure->autofill_count());
749 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
751 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
753 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(2)->heuristic_type());
755 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(3)->heuristic_type());
757 EXPECT_EQ(ADDRESS_BILLING_CITY, form_structure->field(4)->heuristic_type());
759 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure->field(5)->heuristic_type());
761 EXPECT_EQ(ADDRESS_BILLING_ZIP, form_structure->field(6)->heuristic_type());
763 EXPECT_EQ(ADDRESS_BILLING_COUNTRY,
764 form_structure->field(7)->heuristic_type());
766 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
767 form_structure->field(8)->heuristic_type());
769 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(9)->heuristic_type());
772 TEST(FormStructureTest, HeuristicsSample6) {
773 scoped_ptr<FormStructure> form_structure;
775 form.method = ASCIIToUTF16("post");
778 field.form_control_type = "text";
780 field.label = ASCIIToUTF16("E-mail address");
781 field.name = ASCIIToUTF16("email");
782 form.fields.push_back(field);
784 field.label = ASCIIToUTF16("Full name");
785 field.name = ASCIIToUTF16("name");
786 form.fields.push_back(field);
788 field.label = ASCIIToUTF16("Company");
789 field.name = ASCIIToUTF16("company");
790 form.fields.push_back(field);
792 field.label = ASCIIToUTF16("Address");
793 field.name = ASCIIToUTF16("address");
794 form.fields.push_back(field);
796 field.label = ASCIIToUTF16("City");
797 field.name = ASCIIToUTF16("city");
798 form.fields.push_back(field);
800 field.label = ASCIIToUTF16("Zip Code");
801 field.name = ASCIIToUTF16("Home.PostalCode");
802 form.fields.push_back(field);
804 field.label = base::string16();
805 field.name = ASCIIToUTF16("Submit");
806 field.value = ASCIIToUTF16("continue");
807 field.form_control_type = "submit";
808 form.fields.push_back(field);
810 form_structure.reset(new FormStructure(form));
811 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
812 EXPECT_TRUE(form_structure->IsAutofillable(true));
813 ASSERT_EQ(7U, form_structure->field_count());
814 ASSERT_EQ(6U, form_structure->autofill_count());
817 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(0)->heuristic_type());
819 EXPECT_EQ(NAME_FULL, form_structure->field(1)->heuristic_type());
821 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type());
823 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type());
825 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type());
827 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(5)->heuristic_type());
829 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type());
832 // Tests a sequence of FormFields where only labels are supplied to heuristics
833 // for matching. This works because FormFieldData labels are matched in the
834 // case that input element ids (or |name| fields) are missing.
835 TEST(FormStructureTest, HeuristicsLabelsOnly) {
836 scoped_ptr<FormStructure> form_structure;
838 form.method = ASCIIToUTF16("post");
841 field.form_control_type = "text";
843 field.label = ASCIIToUTF16("First Name");
844 field.name = base::string16();
845 form.fields.push_back(field);
847 field.label = ASCIIToUTF16("Last Name");
848 field.name = base::string16();
849 form.fields.push_back(field);
851 field.label = ASCIIToUTF16("Email");
852 field.name = base::string16();
853 form.fields.push_back(field);
855 field.label = ASCIIToUTF16("Phone");
856 field.name = base::string16();
857 form.fields.push_back(field);
859 field.label = ASCIIToUTF16("Address");
860 field.name = base::string16();
861 form.fields.push_back(field);
863 field.label = ASCIIToUTF16("Address");
864 field.name = base::string16();
865 form.fields.push_back(field);
867 field.label = ASCIIToUTF16("Zip code");
868 field.name = base::string16();
869 form.fields.push_back(field);
871 field.label = base::string16();
872 field.name = ASCIIToUTF16("Submit");
873 field.form_control_type = "submit";
874 form.fields.push_back(field);
876 form_structure.reset(new FormStructure(form));
877 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
878 EXPECT_TRUE(form_structure->IsAutofillable(true));
879 ASSERT_EQ(8U, form_structure->field_count());
880 ASSERT_EQ(7U, form_structure->autofill_count());
883 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
885 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
887 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
889 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
890 form_structure->field(3)->heuristic_type());
892 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type());
894 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(5)->heuristic_type());
896 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type());
898 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type());
901 TEST(FormStructureTest, HeuristicsCreditCardInfo) {
902 scoped_ptr<FormStructure> form_structure;
904 form.method = ASCIIToUTF16("post");
907 field.form_control_type = "text";
909 field.label = ASCIIToUTF16("Name on Card");
910 field.name = ASCIIToUTF16("name_on_card");
911 form.fields.push_back(field);
913 field.label = ASCIIToUTF16("Card Number");
914 field.name = ASCIIToUTF16("card_number");
915 form.fields.push_back(field);
917 field.label = ASCIIToUTF16("Exp Month");
918 field.name = ASCIIToUTF16("ccmonth");
919 form.fields.push_back(field);
921 field.label = ASCIIToUTF16("Exp Year");
922 field.name = ASCIIToUTF16("ccyear");
923 form.fields.push_back(field);
925 field.label = ASCIIToUTF16("Verification");
926 field.name = ASCIIToUTF16("verification");
927 form.fields.push_back(field);
929 field.label = base::string16();
930 field.name = ASCIIToUTF16("Submit");
931 field.form_control_type = "submit";
932 form.fields.push_back(field);
934 form_structure.reset(new FormStructure(form));
935 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
936 EXPECT_TRUE(form_structure->IsAutofillable(true));
937 ASSERT_EQ(6U, form_structure->field_count());
938 ASSERT_EQ(5U, form_structure->autofill_count());
941 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
942 // Credit card number.
943 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(1)->heuristic_type());
944 // Credit card expiration month.
945 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(2)->heuristic_type());
946 // Credit card expiration year.
947 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
948 form_structure->field(3)->heuristic_type());
950 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
951 form_structure->field(4)->heuristic_type());
953 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(5)->heuristic_type());
956 TEST(FormStructureTest, HeuristicsCreditCardInfoWithUnknownCardField) {
957 scoped_ptr<FormStructure> form_structure;
959 form.method = ASCIIToUTF16("post");
962 field.form_control_type = "text";
964 field.label = ASCIIToUTF16("Name on Card");
965 field.name = ASCIIToUTF16("name_on_card");
966 form.fields.push_back(field);
968 // This is not a field we know how to process. But we should skip over it
969 // and process the other fields in the card block.
970 field.label = ASCIIToUTF16("Card image");
971 field.name = ASCIIToUTF16("card_image");
972 form.fields.push_back(field);
974 field.label = ASCIIToUTF16("Card Number");
975 field.name = ASCIIToUTF16("card_number");
976 form.fields.push_back(field);
978 field.label = ASCIIToUTF16("Exp Month");
979 field.name = ASCIIToUTF16("ccmonth");
980 form.fields.push_back(field);
982 field.label = ASCIIToUTF16("Exp Year");
983 field.name = ASCIIToUTF16("ccyear");
984 form.fields.push_back(field);
986 field.label = ASCIIToUTF16("Verification");
987 field.name = ASCIIToUTF16("verification");
988 form.fields.push_back(field);
990 field.label = base::string16();
991 field.name = ASCIIToUTF16("Submit");
992 field.form_control_type = "submit";
993 form.fields.push_back(field);
995 form_structure.reset(new FormStructure(form));
996 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
997 EXPECT_TRUE(form_structure->IsAutofillable(true));
998 ASSERT_EQ(7U, form_structure->field_count());
999 ASSERT_EQ(5U, form_structure->autofill_count());
1001 // Credit card name.
1002 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
1003 // Credit card type. This is an unknown type but related to the credit card.
1004 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
1005 // Credit card number.
1006 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type());
1007 // Credit card expiration month.
1008 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1009 // Credit card expiration year.
1010 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1011 form_structure->field(4)->heuristic_type());
1013 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
1014 form_structure->field(5)->heuristic_type());
1016 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type());
1019 TEST(FormStructureTest, ThreeAddressLines) {
1020 scoped_ptr<FormStructure> form_structure;
1022 form.method = ASCIIToUTF16("post");
1024 FormFieldData field;
1025 field.form_control_type = "text";
1027 field.label = ASCIIToUTF16("Address Line1");
1028 field.name = ASCIIToUTF16("Address");
1029 form.fields.push_back(field);
1031 field.label = ASCIIToUTF16("Address Line2");
1032 field.name = ASCIIToUTF16("Address");
1033 form.fields.push_back(field);
1035 field.label = ASCIIToUTF16("Address Line3");
1036 field.name = ASCIIToUTF16("Address");
1037 form.fields.push_back(field);
1039 field.label = ASCIIToUTF16("City");
1040 field.name = ASCIIToUTF16("city");
1041 form.fields.push_back(field);
1043 form_structure.reset(new FormStructure(form));
1044 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1045 EXPECT_TRUE(form_structure->IsAutofillable(true));
1046 ASSERT_EQ(4U, form_structure->field_count());
1047 ASSERT_EQ(3U, form_structure->autofill_count());
1050 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1052 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1054 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1056 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type());
1059 // This test verifies that "addressLine1" and "addressLine2" matches heuristics.
1060 // This occured in https://www.gorillaclothing.com/. http://crbug.com/52126.
1061 TEST(FormStructureTest, BillingAndShippingAddresses) {
1062 scoped_ptr<FormStructure> form_structure;
1064 form.method = ASCIIToUTF16("post");
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 Line1");
1078 field.name = ASCIIToUTF16("billing.address.addressLine1");
1079 form.fields.push_back(field);
1081 field.label = ASCIIToUTF16("Address Line2");
1082 field.name = ASCIIToUTF16("billing.address.addressLine2");
1083 form.fields.push_back(field);
1085 form_structure.reset(new FormStructure(form));
1086 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1087 EXPECT_TRUE(form_structure->IsAutofillable(true));
1088 ASSERT_EQ(4U, form_structure->field_count());
1089 ASSERT_EQ(4U, form_structure->autofill_count());
1092 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1094 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1096 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(2)->heuristic_type());
1098 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(3)->heuristic_type());
1101 // Numbered address lines after line two are ignored.
1102 TEST(FormStructureTest, SurplusAddressLinesIgnored) {
1103 scoped_ptr<FormStructure> form_structure;
1105 form.method = ASCIIToUTF16("post");
1107 FormFieldData field;
1108 field.form_control_type = "text";
1110 field.label = ASCIIToUTF16("Address Line1");
1111 field.name = ASCIIToUTF16("shipping.address.addressLine1");
1112 form.fields.push_back(field);
1114 field.label = ASCIIToUTF16("Address Line2");
1115 field.name = ASCIIToUTF16("shipping.address.addressLine2");
1116 form.fields.push_back(field);
1118 field.label = ASCIIToUTF16("Address Line3");
1119 field.name = ASCIIToUTF16("billing.address.addressLine3");
1120 form.fields.push_back(field);
1122 field.label = ASCIIToUTF16("Address Line4");
1123 field.name = ASCIIToUTF16("billing.address.addressLine4");
1124 form.fields.push_back(field);
1126 form_structure.reset(new FormStructure(form));
1127 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1128 ASSERT_EQ(4U, form_structure->field_count());
1129 ASSERT_EQ(2U, form_structure->autofill_count());
1132 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1134 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1135 // Address Line 3 (ignored).
1136 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1137 // Address Line 4 (ignored).
1138 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type());
1141 // This example comes from expedia.com where they use a "Suite" label to
1142 // indicate a suite or apartment number. We interpret this as address line 2.
1143 // And the following "Street address second line" we interpret as address line
1145 // See http://crbug.com/48197 for details.
1146 TEST(FormStructureTest, ThreeAddressLinesExpedia) {
1147 scoped_ptr<FormStructure> form_structure;
1149 form.method = ASCIIToUTF16("post");
1151 FormFieldData field;
1152 field.form_control_type = "text";
1154 field.label = ASCIIToUTF16("Street:");
1155 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads1");
1156 form.fields.push_back(field);
1158 field.label = ASCIIToUTF16("Suite or Apt:");
1159 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adap");
1160 form.fields.push_back(field);
1162 field.label = ASCIIToUTF16("Street address second line");
1163 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads2");
1164 form.fields.push_back(field);
1166 field.label = ASCIIToUTF16("City:");
1167 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adct");
1168 form.fields.push_back(field);
1170 form_structure.reset(new FormStructure(form));
1171 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1172 EXPECT_TRUE(form_structure->IsAutofillable(true));
1173 ASSERT_EQ(4U, form_structure->field_count());
1174 EXPECT_EQ(3U, form_structure->autofill_count());
1177 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1179 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1181 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1183 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type());
1186 // This example comes from ebay.com where the word "suite" appears in the label
1187 // and the name "address2" clearly indicates that this is the address line 2.
1188 // See http://crbug.com/48197 for details.
1189 TEST(FormStructureTest, TwoAddressLinesEbay) {
1190 scoped_ptr<FormStructure> form_structure;
1192 form.method = ASCIIToUTF16("post");
1194 FormFieldData field;
1195 field.form_control_type = "text";
1197 field.label = ASCIIToUTF16("Address Line1");
1198 field.name = ASCIIToUTF16("address1");
1199 form.fields.push_back(field);
1201 field.label = ASCIIToUTF16("Floor number, suite number, etc");
1202 field.name = ASCIIToUTF16("address2");
1203 form.fields.push_back(field);
1205 field.label = ASCIIToUTF16("City:");
1206 field.name = ASCIIToUTF16("city");
1207 form.fields.push_back(field);
1209 form_structure.reset(new FormStructure(form));
1210 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1211 EXPECT_TRUE(form_structure->IsAutofillable(true));
1212 ASSERT_EQ(3U, form_structure->field_count());
1213 ASSERT_EQ(3U, form_structure->autofill_count());
1216 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1218 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1220 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(2)->heuristic_type());
1223 TEST(FormStructureTest, HeuristicsStateWithProvince) {
1224 scoped_ptr<FormStructure> form_structure;
1226 form.method = ASCIIToUTF16("post");
1228 FormFieldData field;
1229 field.form_control_type = "text";
1231 field.label = ASCIIToUTF16("Address Line1");
1232 field.name = ASCIIToUTF16("Address");
1233 form.fields.push_back(field);
1235 field.label = ASCIIToUTF16("Address Line2");
1236 field.name = ASCIIToUTF16("Address");
1237 form.fields.push_back(field);
1239 field.label = ASCIIToUTF16("State/Province/Region");
1240 field.name = ASCIIToUTF16("State");
1241 form.fields.push_back(field);
1243 form_structure.reset(new FormStructure(form));
1244 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1245 EXPECT_TRUE(form_structure->IsAutofillable(true));
1246 ASSERT_EQ(3U, form_structure->field_count());
1247 ASSERT_EQ(3U, form_structure->autofill_count());
1250 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1252 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1254 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(2)->heuristic_type());
1257 // This example comes from lego.com's checkout page.
1258 TEST(FormStructureTest, HeuristicsWithBilling) {
1259 scoped_ptr<FormStructure> form_structure;
1261 form.method = ASCIIToUTF16("post");
1263 FormFieldData field;
1264 field.form_control_type = "text";
1266 field.label = ASCIIToUTF16("First Name*:");
1267 field.name = ASCIIToUTF16("editBillingAddress$firstNameBox");
1268 form.fields.push_back(field);
1270 field.label = ASCIIToUTF16("Last Name*:");
1271 field.name = ASCIIToUTF16("editBillingAddress$lastNameBox");
1272 form.fields.push_back(field);
1274 field.label = ASCIIToUTF16("Company Name:");
1275 field.name = ASCIIToUTF16("editBillingAddress$companyBox");
1276 form.fields.push_back(field);
1278 field.label = ASCIIToUTF16("Address*:");
1279 field.name = ASCIIToUTF16("editBillingAddress$addressLine1Box");
1280 form.fields.push_back(field);
1282 field.label = ASCIIToUTF16("Apt/Suite :");
1283 field.name = ASCIIToUTF16("editBillingAddress$addressLine2Box");
1284 form.fields.push_back(field);
1286 field.label = ASCIIToUTF16("City*:");
1287 field.name = ASCIIToUTF16("editBillingAddress$cityBox");
1288 form.fields.push_back(field);
1290 field.label = ASCIIToUTF16("State/Province*:");
1291 field.name = ASCIIToUTF16("editBillingAddress$stateDropDown");
1292 form.fields.push_back(field);
1294 field.label = ASCIIToUTF16("Country*:");
1295 field.name = ASCIIToUTF16("editBillingAddress$countryDropDown");
1296 form.fields.push_back(field);
1298 field.label = ASCIIToUTF16("Postal Code*:");
1299 field.name = ASCIIToUTF16("editBillingAddress$zipCodeBox");
1300 form.fields.push_back(field);
1302 field.label = ASCIIToUTF16("Phone*:");
1303 field.name = ASCIIToUTF16("editBillingAddress$phoneBox");
1304 form.fields.push_back(field);
1306 field.label = ASCIIToUTF16("Email Address*:");
1307 field.name = ASCIIToUTF16("email$emailBox");
1308 form.fields.push_back(field);
1310 form_structure.reset(new FormStructure(form));
1311 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1312 EXPECT_TRUE(form_structure->IsAutofillable(true));
1313 ASSERT_EQ(11U, form_structure->field_count());
1314 ASSERT_EQ(11U, form_structure->autofill_count());
1316 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
1317 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
1318 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type());
1319 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(3)->heuristic_type());
1320 EXPECT_EQ(ADDRESS_BILLING_LINE2, form_structure->field(4)->heuristic_type());
1321 EXPECT_EQ(ADDRESS_BILLING_CITY, form_structure->field(5)->heuristic_type());
1322 EXPECT_EQ(ADDRESS_BILLING_STATE, form_structure->field(6)->heuristic_type());
1323 EXPECT_EQ(ADDRESS_BILLING_COUNTRY,
1324 form_structure->field(7)->heuristic_type());
1325 EXPECT_EQ(ADDRESS_BILLING_ZIP, form_structure->field(8)->heuristic_type());
1326 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1327 form_structure->field(9)->heuristic_type());
1328 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(10)->heuristic_type());
1331 TEST(FormStructureTest, ThreePartPhoneNumber) {
1332 scoped_ptr<FormStructure> form_structure;
1334 form.method = ASCIIToUTF16("post");
1336 FormFieldData field;
1337 field.form_control_type = "text";
1339 field.label = ASCIIToUTF16("Phone:");
1340 field.name = ASCIIToUTF16("dayphone1");
1341 field.max_length = 0;
1342 form.fields.push_back(field);
1344 field.label = ASCIIToUTF16("-");
1345 field.name = ASCIIToUTF16("dayphone2");
1346 field.max_length = 3; // Size of prefix is 3.
1347 form.fields.push_back(field);
1349 field.label = ASCIIToUTF16("-");
1350 field.name = ASCIIToUTF16("dayphone3");
1351 field.max_length = 4; // Size of suffix is 4. If unlimited size is
1352 // passed, phone will be parsed as
1353 // <country code> - <area code> - <phone>.
1354 form.fields.push_back(field);
1356 field.label = ASCIIToUTF16("ext.:");
1357 field.name = ASCIIToUTF16("dayphone4");
1358 field.max_length = 0;
1359 form.fields.push_back(field);
1361 form_structure.reset(new FormStructure(form));
1362 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1363 EXPECT_TRUE(form_structure->IsAutofillable(true));
1364 ASSERT_EQ(4U, form_structure->field_count());
1365 ASSERT_EQ(3U, form_structure->autofill_count());
1368 EXPECT_EQ(PHONE_HOME_CITY_CODE, form_structure->field(0)->heuristic_type());
1369 // Phone number suffix.
1370 EXPECT_EQ(PHONE_HOME_NUMBER,
1371 form_structure->field(1)->heuristic_type());
1372 // Phone number suffix.
1373 EXPECT_EQ(PHONE_HOME_NUMBER,
1374 form_structure->field(2)->heuristic_type());
1376 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type());
1379 TEST(FormStructureTest, HeuristicsInfernoCC) {
1380 scoped_ptr<FormStructure> form_structure;
1382 form.method = ASCIIToUTF16("post");
1384 FormFieldData field;
1385 field.form_control_type = "text";
1387 field.label = ASCIIToUTF16("Name on Card");
1388 field.name = ASCIIToUTF16("name_on_card");
1389 form.fields.push_back(field);
1391 field.label = ASCIIToUTF16("Address");
1392 field.name = ASCIIToUTF16("billing_address");
1393 form.fields.push_back(field);
1395 field.label = ASCIIToUTF16("Card Number");
1396 field.name = ASCIIToUTF16("card_number");
1397 form.fields.push_back(field);
1399 field.label = ASCIIToUTF16("Expiration Date");
1400 field.name = ASCIIToUTF16("expiration_month");
1401 form.fields.push_back(field);
1403 field.label = ASCIIToUTF16("Expiration Year");
1404 field.name = ASCIIToUTF16("expiration_year");
1405 form.fields.push_back(field);
1407 form_structure.reset(new FormStructure(form));
1408 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1409 EXPECT_TRUE(form_structure->IsAutofillable(true));
1411 // Expect the correct number of fields.
1412 ASSERT_EQ(5U, form_structure->field_count());
1413 EXPECT_EQ(5U, form_structure->autofill_count());
1416 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
1418 EXPECT_EQ(ADDRESS_BILLING_LINE1, form_structure->field(1)->heuristic_type());
1420 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type());
1422 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1424 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1425 form_structure->field(4)->heuristic_type());
1428 TEST(FormStructureTest, CVCCodeClash) {
1429 scoped_ptr<FormStructure> form_structure;
1431 form.method = ASCIIToUTF16("post");
1433 FormFieldData field;
1434 field.form_control_type = "text";
1436 field.label = ASCIIToUTF16("Card number");
1437 field.name = ASCIIToUTF16("ccnumber");
1438 form.fields.push_back(field);
1440 field.label = ASCIIToUTF16("First name");
1441 field.name = ASCIIToUTF16("first_name");
1442 form.fields.push_back(field);
1444 field.label = ASCIIToUTF16("Last name");
1445 field.name = ASCIIToUTF16("last_name");
1446 form.fields.push_back(field);
1448 field.label = ASCIIToUTF16("Expiration date");
1449 field.name = ASCIIToUTF16("ccexpiresmonth");
1450 form.fields.push_back(field);
1452 field.label = base::string16();
1453 field.name = ASCIIToUTF16("ccexpiresyear");
1454 form.fields.push_back(field);
1456 field.label = ASCIIToUTF16("cvc number");
1457 field.name = ASCIIToUTF16("csc");
1458 form.fields.push_back(field);
1460 form_structure.reset(new FormStructure(form));
1461 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1462 EXPECT_TRUE(form_structure->IsAutofillable(true));
1464 // Expect the correct number of fields.
1465 ASSERT_EQ(6U, form_structure->field_count());
1466 ASSERT_EQ(5U, form_structure->autofill_count());
1469 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(0)->heuristic_type());
1470 // First name, taken as name on card.
1471 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(1)->heuristic_type());
1472 // Last name is not merged.
1473 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1475 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1477 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1478 form_structure->field(4)->heuristic_type());
1480 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
1481 form_structure->field(5)->heuristic_type());
1484 TEST(FormStructureTest, EncodeQueryRequest) {
1486 form.method = ASCIIToUTF16("post");
1488 FormFieldData field;
1489 field.form_control_type = "text";
1491 field.label = ASCIIToUTF16("Name on Card");
1492 field.name = ASCIIToUTF16("name_on_card");
1493 form.fields.push_back(field);
1495 field.label = ASCIIToUTF16("Address");
1496 field.name = ASCIIToUTF16("billing_address");
1497 form.fields.push_back(field);
1499 field.label = ASCIIToUTF16("Card Number");
1500 field.name = ASCIIToUTF16("card_number");
1501 form.fields.push_back(field);
1503 field.label = ASCIIToUTF16("Expiration Date");
1504 field.name = ASCIIToUTF16("expiration_month");
1505 form.fields.push_back(field);
1507 field.label = ASCIIToUTF16("Expiration Year");
1508 field.name = ASCIIToUTF16("expiration_year");
1509 form.fields.push_back(field);
1511 // Add checkable field.
1512 FormFieldData checkable_field;
1513 checkable_field.is_checkable = true;
1514 checkable_field.label = ASCIIToUTF16("Checkable1");
1515 checkable_field.name = ASCIIToUTF16("Checkable1");
1516 form.fields.push_back(checkable_field);
1518 ScopedVector<FormStructure> forms;
1519 forms.push_back(new FormStructure(form));
1520 std::vector<std::string> encoded_signatures;
1521 std::string encoded_xml;
1522 const char * const kSignature1 = "11337937696949187602";
1523 const char * const kResponse1 =
1524 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery "
1525 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form "
1526 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>"
1527 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>"
1528 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>"
1530 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1531 &encoded_signatures,
1533 ASSERT_EQ(1U, encoded_signatures.size());
1534 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1535 EXPECT_EQ(kResponse1, encoded_xml);
1537 // Add the same form, only one will be encoded, so EncodeQueryRequest() should
1538 // return the same data.
1539 forms.push_back(new FormStructure(form));
1540 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1541 &encoded_signatures,
1543 ASSERT_EQ(1U, encoded_signatures.size());
1544 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1545 EXPECT_EQ(kResponse1, encoded_xml);
1546 // Add 5 address fields - this should be still a valid form.
1547 for (size_t i = 0; i < 5; ++i) {
1548 field.label = ASCIIToUTF16("Address");
1549 field.name = ASCIIToUTF16("address");
1550 form.fields.push_back(field);
1553 forms.push_back(new FormStructure(form));
1554 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1555 &encoded_signatures,
1557 ASSERT_EQ(2U, encoded_signatures.size());
1558 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1559 const char * const kSignature2 = "8308881815906226214";
1560 EXPECT_EQ(kSignature2, encoded_signatures[1]);
1561 const char * const kResponse2 =
1562 "<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><autofillquery "
1563 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form "
1564 "signature=\"11337937696949187602\"><field signature=\"412125936\"/>"
1565 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>"
1566 "<field signature=\"747221617\"/><field signature=\"4108155786\"/></form>"
1567 "<form signature=\"8308881815906226214\"><field signature=\"412125936\"/>"
1568 "<field signature=\"1917667676\"/><field signature=\"2226358947\"/>"
1569 "<field signature=\"747221617\"/><field signature=\"4108155786\"/><field "
1570 "signature=\"509334676\"/><field signature=\"509334676\"/><field "
1571 "signature=\"509334676\"/><field signature=\"509334676\"/><field "
1572 "signature=\"509334676\"/></form></autofillquery>";
1573 EXPECT_EQ(kResponse2, encoded_xml);
1575 FormData malformed_form(form);
1576 // Add 50 address fields - the form is not valid anymore, but previous ones
1577 // are. The result should be the same as in previous test.
1578 for (size_t i = 0; i < 50; ++i) {
1579 field.label = ASCIIToUTF16("Address");
1580 field.name = ASCIIToUTF16("address");
1581 malformed_form.fields.push_back(field);
1584 forms.push_back(new FormStructure(malformed_form));
1585 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1586 &encoded_signatures,
1588 ASSERT_EQ(2U, encoded_signatures.size());
1589 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1590 EXPECT_EQ(kSignature2, encoded_signatures[1]);
1591 EXPECT_EQ(kResponse2, encoded_xml);
1593 // Check that we fail if there are only bad form(s).
1594 ScopedVector<FormStructure> bad_forms;
1595 bad_forms.push_back(new FormStructure(malformed_form));
1596 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(),
1597 &encoded_signatures,
1599 EXPECT_EQ(0U, encoded_signatures.size());
1600 EXPECT_EQ("", encoded_xml);
1603 TEST(FormStructureTest, EncodeUploadRequest) {
1604 scoped_ptr<FormStructure> form_structure;
1605 std::vector<ServerFieldTypeSet> possible_field_types;
1607 form.method = ASCIIToUTF16("post");
1608 form_structure.reset(new FormStructure(form));
1609 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1611 FormFieldData field;
1612 field.form_control_type = "text";
1614 field.label = ASCIIToUTF16("First Name");
1615 field.name = ASCIIToUTF16("firstname");
1616 form.fields.push_back(field);
1617 possible_field_types.push_back(ServerFieldTypeSet());
1618 possible_field_types.back().insert(NAME_FIRST);
1620 field.label = ASCIIToUTF16("Last Name");
1621 field.name = ASCIIToUTF16("lastname");
1622 form.fields.push_back(field);
1623 possible_field_types.push_back(ServerFieldTypeSet());
1624 possible_field_types.back().insert(NAME_LAST);
1626 field.label = ASCIIToUTF16("Email");
1627 field.name = ASCIIToUTF16("email");
1628 field.form_control_type = "email";
1629 form.fields.push_back(field);
1630 possible_field_types.push_back(ServerFieldTypeSet());
1631 possible_field_types.back().insert(EMAIL_ADDRESS);
1633 field.label = ASCIIToUTF16("Phone");
1634 field.name = ASCIIToUTF16("phone");
1635 field.form_control_type = "number";
1636 form.fields.push_back(field);
1637 possible_field_types.push_back(ServerFieldTypeSet());
1638 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER);
1640 field.label = ASCIIToUTF16("Country");
1641 field.name = ASCIIToUTF16("country");
1642 field.form_control_type = "select-one";
1643 form.fields.push_back(field);
1644 possible_field_types.push_back(ServerFieldTypeSet());
1645 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1647 // Add checkable field.
1648 FormFieldData checkable_field;
1649 checkable_field.is_checkable = true;
1650 checkable_field.label = ASCIIToUTF16("Checkable1");
1651 checkable_field.name = ASCIIToUTF16("Checkable1");
1652 form.fields.push_back(checkable_field);
1653 possible_field_types.push_back(ServerFieldTypeSet());
1654 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1656 form_structure.reset(new FormStructure(form));
1658 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1659 for (size_t i = 0; i < form_structure->field_count(); ++i)
1660 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1662 ServerFieldTypeSet available_field_types;
1663 available_field_types.insert(NAME_FIRST);
1664 available_field_types.insert(NAME_LAST);
1665 available_field_types.insert(ADDRESS_HOME_LINE1);
1666 available_field_types.insert(ADDRESS_HOME_LINE2);
1667 available_field_types.insert(ADDRESS_HOME_COUNTRY);
1668 available_field_types.insert(ADDRESS_BILLING_LINE1);
1669 available_field_types.insert(ADDRESS_BILLING_LINE2);
1670 available_field_types.insert(EMAIL_ADDRESS);
1671 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
1673 std::string encoded_xml;
1674 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
1676 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1677 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" "
1678 "formsignature=\"8736493185895608956\" autofillused=\"false\" "
1679 "datapresent=\"144200030e\">"
1680 "<field signature=\"3763331450\" autofilltype=\"3\"/>"
1681 "<field signature=\"3494530716\" autofilltype=\"5\"/>"
1682 "<field signature=\"1029417091\" autofilltype=\"9\"/>"
1683 "<field signature=\"466116101\" autofilltype=\"14\"/>"
1684 "<field signature=\"2799270304\" autofilltype=\"36\"/>"
1685 "</autofillupload>",
1687 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true,
1689 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1690 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" "
1691 "formsignature=\"8736493185895608956\" autofillused=\"true\" "
1692 "datapresent=\"144200030e\">"
1693 "<field signature=\"3763331450\" autofilltype=\"3\"/>"
1694 "<field signature=\"3494530716\" autofilltype=\"5\"/>"
1695 "<field signature=\"1029417091\" autofilltype=\"9\"/>"
1696 "<field signature=\"466116101\" autofilltype=\"14\"/>"
1697 "<field signature=\"2799270304\" autofilltype=\"36\"/>"
1698 "</autofillupload>",
1701 // Add 2 address fields - this should be still a valid form.
1702 for (size_t i = 0; i < 2; ++i) {
1703 field.label = ASCIIToUTF16("Address");
1704 field.name = ASCIIToUTF16("address");
1705 field.form_control_type = "text";
1706 form.fields.push_back(field);
1707 possible_field_types.push_back(ServerFieldTypeSet());
1708 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
1709 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
1710 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
1711 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
1714 form_structure.reset(new FormStructure(form));
1715 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1716 for (size_t i = 0; i < form_structure->field_count(); ++i)
1717 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1719 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
1721 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1722 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\" "
1723 "formsignature=\"7816485729218079147\" autofillused=\"false\" "
1724 "datapresent=\"144200030e\">"
1725 "<field signature=\"3763331450\" autofilltype=\"3\"/>"
1726 "<field signature=\"3494530716\" autofilltype=\"5\"/>"
1727 "<field signature=\"1029417091\" autofilltype=\"9\"/>"
1728 "<field signature=\"466116101\" autofilltype=\"14\"/>"
1729 "<field signature=\"2799270304\" autofilltype=\"36\"/>"
1730 "<field signature=\"509334676\" autofilltype=\"30\"/>"
1731 "<field signature=\"509334676\" autofilltype=\"31\"/>"
1732 "<field signature=\"509334676\" autofilltype=\"37\"/>"
1733 "<field signature=\"509334676\" autofilltype=\"38\"/>"
1734 "<field signature=\"509334676\" autofilltype=\"30\"/>"
1735 "<field signature=\"509334676\" autofilltype=\"31\"/>"
1736 "<field signature=\"509334676\" autofilltype=\"37\"/>"
1737 "<field signature=\"509334676\" autofilltype=\"38\"/>"
1738 "</autofillupload>",
1741 // Add 50 address fields - now the form is invalid, as it has too many fields.
1742 for (size_t i = 0; i < 50; ++i) {
1743 field.label = ASCIIToUTF16("Address");
1744 field.name = ASCIIToUTF16("address");
1745 field.form_control_type = "text";
1746 form.fields.push_back(field);
1747 possible_field_types.push_back(ServerFieldTypeSet());
1748 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
1749 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
1750 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
1751 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
1753 form_structure.reset(new FormStructure(form));
1754 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1755 for (size_t i = 0; i < form_structure->field_count(); ++i)
1756 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1757 EXPECT_FALSE(form_structure->EncodeUploadRequest(available_field_types, false,
1761 TEST(FormStructureTest, EncodeFieldAssignments) {
1762 scoped_ptr<FormStructure> form_structure;
1763 std::vector<ServerFieldTypeSet> possible_field_types;
1765 form.method = ASCIIToUTF16("post");
1766 form_structure.reset(new FormStructure(form));
1767 form_structure->DetermineHeuristicTypes(TestAutofillMetrics());
1769 FormFieldData field;
1770 field.form_control_type = "text";
1772 field.label = ASCIIToUTF16("First Name");
1773 field.name = ASCIIToUTF16("firstname");
1774 form.fields.push_back(field);
1775 possible_field_types.push_back(ServerFieldTypeSet());
1776 possible_field_types.back().insert(NAME_FIRST);
1778 field.label = ASCIIToUTF16("Last Name");
1779 field.name = ASCIIToUTF16("lastname");
1780 form.fields.push_back(field);
1781 possible_field_types.push_back(ServerFieldTypeSet());
1782 possible_field_types.back().insert(NAME_LAST);
1784 field.label = ASCIIToUTF16("Email");
1785 field.name = ASCIIToUTF16("email");
1786 field.form_control_type = "email";
1787 form.fields.push_back(field);
1788 possible_field_types.push_back(ServerFieldTypeSet());
1789 possible_field_types.back().insert(EMAIL_ADDRESS);
1791 field.label = ASCIIToUTF16("Phone");
1792 field.name = ASCIIToUTF16("phone");
1793 field.form_control_type = "number";
1794 form.fields.push_back(field);
1795 possible_field_types.push_back(ServerFieldTypeSet());
1796 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER);
1798 field.label = ASCIIToUTF16("Country");
1799 field.name = ASCIIToUTF16("country");
1800 field.form_control_type = "select-one";
1801 form.fields.push_back(field);
1802 possible_field_types.push_back(ServerFieldTypeSet());
1803 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1805 // Add checkable field.
1806 FormFieldData checkable_field;
1807 checkable_field.is_checkable = true;
1808 checkable_field.label = ASCIIToUTF16("Checkable1");
1809 checkable_field.name = ASCIIToUTF16("Checkable1");
1810 form.fields.push_back(checkable_field);
1811 possible_field_types.push_back(ServerFieldTypeSet());
1812 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1814 form_structure.reset(new FormStructure(form));
1816 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1817 for (size_t i = 0; i < form_structure->field_count(); ++i)
1818 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1820 ServerFieldTypeSet available_field_types;
1821 available_field_types.insert(NAME_FIRST);
1822 available_field_types.insert(NAME_LAST);
1823 available_field_types.insert(ADDRESS_HOME_LINE1);
1824 available_field_types.insert(ADDRESS_HOME_LINE2);
1825 available_field_types.insert(ADDRESS_HOME_COUNTRY);
1826 available_field_types.insert(ADDRESS_BILLING_LINE1);
1827 available_field_types.insert(ADDRESS_BILLING_LINE2);
1828 available_field_types.insert(EMAIL_ADDRESS);
1829 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
1831 std::string encoded_xml;
1832 EXPECT_TRUE(form_structure->EncodeFieldAssignments(
1833 available_field_types, &encoded_xml));
1835 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1836 "<fieldassignments formsignature=\"8736493185895608956\">"
1837 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>"
1838 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>"
1839 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>"
1840 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>"
1841 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>"
1842 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>"
1843 "</fieldassignments>",
1846 // Add 2 address fields - this should be still a valid form.
1847 for (size_t i = 0; i < 2; ++i) {
1848 field.label = ASCIIToUTF16("Address");
1849 field.name = ASCIIToUTF16("address");
1850 field.form_control_type = "text";
1851 form.fields.push_back(field);
1852 possible_field_types.push_back(ServerFieldTypeSet());
1853 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
1854 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
1855 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
1856 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
1859 form_structure.reset(new FormStructure(form));
1860 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1861 for (size_t i = 0; i < form_structure->field_count(); ++i)
1862 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1864 EXPECT_TRUE(form_structure->EncodeFieldAssignments(
1865 available_field_types, &encoded_xml));
1867 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1868 "<fieldassignments formsignature=\"7816485729218079147\">"
1869 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>"
1870 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>"
1871 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>"
1872 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>"
1873 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>"
1874 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>"
1875 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>"
1876 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>"
1877 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>"
1878 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>"
1879 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>"
1880 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>"
1881 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>"
1882 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>"
1883 "</fieldassignments>",
1887 // Check that we compute the "datapresent" string correctly for the given
1888 // |available_types|.
1889 TEST(FormStructureTest, CheckDataPresence) {
1891 form.method = ASCIIToUTF16("post");
1893 FormFieldData field;
1894 field.form_control_type = "text";
1896 field.label = ASCIIToUTF16("First Name");
1897 field.name = ASCIIToUTF16("first");
1898 form.fields.push_back(field);
1900 field.label = ASCIIToUTF16("Last Name");
1901 field.name = ASCIIToUTF16("last");
1902 form.fields.push_back(field);
1904 field.label = ASCIIToUTF16("Email");
1905 field.name = ASCIIToUTF16("email");
1906 form.fields.push_back(field);
1908 FormStructure form_structure(form);
1910 ServerFieldTypeSet unknown_type;
1911 unknown_type.insert(UNKNOWN_TYPE);
1912 for (size_t i = 0; i < form_structure.field_count(); ++i)
1913 form_structure.field(i)->set_possible_types(unknown_type);
1915 // No available types.
1916 // datapresent should be "" == trimmmed(0x0000000000000000) ==
1917 // 0b0000000000000000000000000000000000000000000000000000000000000000
1918 ServerFieldTypeSet available_field_types;
1920 std::string encoded_xml;
1921 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
1923 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1924 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1925 " formsignature=\"6402244543831589061\" autofillused=\"false\""
1926 " datapresent=\"\">"
1927 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
1928 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
1929 "<field signature=\"420638584\" autofilltype=\"1\"/>"
1930 "</autofillupload>",
1933 // Only a few types available.
1934 // datapresent should be "1540000240" == trimmmed(0x1540000240000000) ==
1935 // 0b0001010101000000000000000000001001000000000000000000000000000000
1936 // The set bits are:
1940 // 9 == EMAIL_ADDRESS
1941 // 30 == ADDRESS_HOME_LINE1
1942 // 33 == ADDRESS_HOME_CITY
1943 available_field_types.clear();
1944 available_field_types.insert(NAME_FIRST);
1945 available_field_types.insert(NAME_LAST);
1946 available_field_types.insert(NAME_FULL);
1947 available_field_types.insert(EMAIL_ADDRESS);
1948 available_field_types.insert(ADDRESS_HOME_LINE1);
1949 available_field_types.insert(ADDRESS_HOME_CITY);
1951 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
1953 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
1954 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1955 " formsignature=\"6402244543831589061\" autofillused=\"false\""
1956 " datapresent=\"1540000240\">"
1957 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
1958 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
1959 "<field signature=\"420638584\" autofilltype=\"1\"/>"
1960 "</autofillupload>",
1963 // All supported non-credit card types available.
1964 // datapresent should be "1f7e000378000008" == trimmmed(0x1f7e000378000008) ==
1965 // 0b0001111101111110000000000000001101111000000000000000000000001000
1966 // The set bits are:
1970 // 6 == NAME_MIDDLE_INITIAL
1972 // 9 == EMAIL_ADDRESS
1973 // 10 == PHONE_HOME_NUMBER,
1974 // 11 == PHONE_HOME_CITY_CODE,
1975 // 12 == PHONE_HOME_COUNTRY_CODE,
1976 // 13 == PHONE_HOME_CITY_AND_NUMBER,
1977 // 14 == PHONE_HOME_WHOLE_NUMBER,
1978 // 30 == ADDRESS_HOME_LINE1
1979 // 31 == ADDRESS_HOME_LINE2
1980 // 33 == ADDRESS_HOME_CITY
1981 // 34 == ADDRESS_HOME_STATE
1982 // 35 == ADDRESS_HOME_ZIP
1983 // 36 == ADDRESS_HOME_COUNTRY
1984 // 60 == COMPANY_NAME
1985 available_field_types.clear();
1986 available_field_types.insert(NAME_FIRST);
1987 available_field_types.insert(NAME_MIDDLE);
1988 available_field_types.insert(NAME_LAST);
1989 available_field_types.insert(NAME_MIDDLE_INITIAL);
1990 available_field_types.insert(NAME_FULL);
1991 available_field_types.insert(EMAIL_ADDRESS);
1992 available_field_types.insert(PHONE_HOME_NUMBER);
1993 available_field_types.insert(PHONE_HOME_CITY_CODE);
1994 available_field_types.insert(PHONE_HOME_COUNTRY_CODE);
1995 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER);
1996 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
1997 available_field_types.insert(ADDRESS_HOME_LINE1);
1998 available_field_types.insert(ADDRESS_HOME_LINE2);
1999 available_field_types.insert(ADDRESS_HOME_CITY);
2000 available_field_types.insert(ADDRESS_HOME_STATE);
2001 available_field_types.insert(ADDRESS_HOME_ZIP);
2002 available_field_types.insert(ADDRESS_HOME_COUNTRY);
2003 available_field_types.insert(COMPANY_NAME);
2005 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2007 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2008 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2009 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2010 " datapresent=\"1f7e000378000008\">"
2011 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
2012 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
2013 "<field signature=\"420638584\" autofilltype=\"1\"/>"
2014 "</autofillupload>",
2017 // All supported credit card types available.
2018 // datapresent should be "0000000000001fc0" == trimmmed(0x0000000000001fc0) ==
2019 // 0b0000000000000000000000000000000000000000000000000001111111000000
2020 // The set bits are:
2021 // 51 == CREDIT_CARD_NAME
2022 // 52 == CREDIT_CARD_NUMBER
2023 // 53 == CREDIT_CARD_EXP_MONTH
2024 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR
2025 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR
2026 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
2027 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR
2028 available_field_types.clear();
2029 available_field_types.insert(CREDIT_CARD_NAME);
2030 available_field_types.insert(CREDIT_CARD_NUMBER);
2031 available_field_types.insert(CREDIT_CARD_EXP_MONTH);
2032 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2033 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2034 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR);
2035 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2037 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2039 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2040 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2041 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2042 " datapresent=\"0000000000001fc0\">"
2043 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
2044 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
2045 "<field signature=\"420638584\" autofilltype=\"1\"/>"
2046 "</autofillupload>",
2049 // All supported types available.
2050 // datapresent should be "1f7e000378001fc8" == trimmmed(0x1f7e000378001fc8) ==
2051 // 0b0001111101111110000000000000001101111000000000000001111111001000
2052 // The set bits are:
2056 // 6 == NAME_MIDDLE_INITIAL
2058 // 9 == EMAIL_ADDRESS
2059 // 10 == PHONE_HOME_NUMBER,
2060 // 11 == PHONE_HOME_CITY_CODE,
2061 // 12 == PHONE_HOME_COUNTRY_CODE,
2062 // 13 == PHONE_HOME_CITY_AND_NUMBER,
2063 // 14 == PHONE_HOME_WHOLE_NUMBER,
2064 // 30 == ADDRESS_HOME_LINE1
2065 // 31 == ADDRESS_HOME_LINE2
2066 // 33 == ADDRESS_HOME_CITY
2067 // 34 == ADDRESS_HOME_STATE
2068 // 35 == ADDRESS_HOME_ZIP
2069 // 36 == ADDRESS_HOME_COUNTRY
2070 // 51 == CREDIT_CARD_NAME
2071 // 52 == CREDIT_CARD_NUMBER
2072 // 53 == CREDIT_CARD_EXP_MONTH
2073 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR
2074 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR
2075 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
2076 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR
2077 // 60 == COMPANY_NAME
2078 available_field_types.clear();
2079 available_field_types.insert(NAME_FIRST);
2080 available_field_types.insert(NAME_MIDDLE);
2081 available_field_types.insert(NAME_LAST);
2082 available_field_types.insert(NAME_MIDDLE_INITIAL);
2083 available_field_types.insert(NAME_FULL);
2084 available_field_types.insert(EMAIL_ADDRESS);
2085 available_field_types.insert(PHONE_HOME_NUMBER);
2086 available_field_types.insert(PHONE_HOME_CITY_CODE);
2087 available_field_types.insert(PHONE_HOME_COUNTRY_CODE);
2088 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER);
2089 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
2090 available_field_types.insert(ADDRESS_HOME_LINE1);
2091 available_field_types.insert(ADDRESS_HOME_LINE2);
2092 available_field_types.insert(ADDRESS_HOME_CITY);
2093 available_field_types.insert(ADDRESS_HOME_STATE);
2094 available_field_types.insert(ADDRESS_HOME_ZIP);
2095 available_field_types.insert(ADDRESS_HOME_COUNTRY);
2096 available_field_types.insert(CREDIT_CARD_NAME);
2097 available_field_types.insert(CREDIT_CARD_NUMBER);
2098 available_field_types.insert(CREDIT_CARD_EXP_MONTH);
2099 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2100 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2101 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR);
2102 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2103 available_field_types.insert(COMPANY_NAME);
2105 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2107 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2108 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2109 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2110 " datapresent=\"1f7e000378001fc8\">"
2111 "<field signature=\"1089846351\" autofilltype=\"1\"/>"
2112 "<field signature=\"2404144663\" autofilltype=\"1\"/>"
2113 "<field signature=\"420638584\" autofilltype=\"1\"/>"
2114 "</autofillupload>",
2118 TEST(FormStructureTest, CheckMultipleTypes) {
2119 // Throughout this test, datapresent should be
2120 // 0x1440000360000008 ==
2121 // 0b0001010001000000000000000000001101100000000000000000000000001000
2122 // The set bits are:
2125 // 9 == EMAIL_ADDRESS
2126 // 30 == ADDRESS_HOME_LINE1
2127 // 31 == ADDRESS_HOME_LINE2
2128 // 33 == ADDRESS_HOME_CITY
2129 // 34 == ADDRESS_HOME_STATE
2130 // 60 == COMPANY_NAME
2131 ServerFieldTypeSet available_field_types;
2132 available_field_types.insert(NAME_FIRST);
2133 available_field_types.insert(NAME_LAST);
2134 available_field_types.insert(EMAIL_ADDRESS);
2135 available_field_types.insert(ADDRESS_HOME_LINE1);
2136 available_field_types.insert(ADDRESS_HOME_LINE2);
2137 available_field_types.insert(ADDRESS_HOME_CITY);
2138 available_field_types.insert(ADDRESS_HOME_STATE);
2139 available_field_types.insert(COMPANY_NAME);
2141 // Check that multiple types for the field are processed correctly.
2142 scoped_ptr<FormStructure> form_structure;
2143 std::vector<ServerFieldTypeSet> possible_field_types;
2145 form.method = ASCIIToUTF16("post");
2147 FormFieldData field;
2148 field.form_control_type = "text";
2150 field.label = ASCIIToUTF16("email");
2151 field.name = ASCIIToUTF16("email");
2152 form.fields.push_back(field);
2153 possible_field_types.push_back(ServerFieldTypeSet());
2154 possible_field_types.back().insert(EMAIL_ADDRESS);
2156 field.label = ASCIIToUTF16("First Name");
2157 field.name = ASCIIToUTF16("first");
2158 form.fields.push_back(field);
2159 possible_field_types.push_back(ServerFieldTypeSet());
2160 possible_field_types.back().insert(NAME_FIRST);
2162 field.label = ASCIIToUTF16("Last Name");
2163 field.name = ASCIIToUTF16("last");
2164 form.fields.push_back(field);
2165 possible_field_types.push_back(ServerFieldTypeSet());
2166 possible_field_types.back().insert(NAME_LAST);
2168 field.label = ASCIIToUTF16("Address");
2169 field.name = ASCIIToUTF16("address");
2170 form.fields.push_back(field);
2171 possible_field_types.push_back(ServerFieldTypeSet());
2172 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
2174 form_structure.reset(new FormStructure(form));
2176 for (size_t i = 0; i < form_structure->field_count(); ++i)
2177 form_structure->field(i)->set_possible_types(possible_field_types[i]);
2178 std::string encoded_xml;
2180 // Now we matched both fields singularly.
2181 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2183 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2184 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2185 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2186 " datapresent=\"1440000360000008\">"
2187 "<field signature=\"420638584\" autofilltype=\"9\"/>"
2188 "<field signature=\"1089846351\" autofilltype=\"3\"/>"
2189 "<field signature=\"2404144663\" autofilltype=\"5\"/>"
2190 "<field signature=\"509334676\" autofilltype=\"30\"/>"
2191 "</autofillupload>",
2193 // Match third field as both first and last.
2194 possible_field_types[2].insert(NAME_FIRST);
2195 form_structure->field(2)->set_possible_types(possible_field_types[2]);
2196 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2198 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2199 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2200 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2201 " datapresent=\"1440000360000008\">"
2202 "<field signature=\"420638584\" autofilltype=\"9\"/>"
2203 "<field signature=\"1089846351\" autofilltype=\"3\"/>"
2204 "<field signature=\"2404144663\" autofilltype=\"3\"/>"
2205 "<field signature=\"2404144663\" autofilltype=\"5\"/>"
2206 "<field signature=\"509334676\" autofilltype=\"30\"/>"
2207 "</autofillupload>",
2209 possible_field_types[3].insert(ADDRESS_HOME_LINE2);
2210 form_structure->field(form_structure->field_count() - 1)->set_possible_types(
2211 possible_field_types[form_structure->field_count() - 1]);
2212 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2214 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2215 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2216 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2217 " datapresent=\"1440000360000008\">"
2218 "<field signature=\"420638584\" autofilltype=\"9\"/>"
2219 "<field signature=\"1089846351\" autofilltype=\"3\"/>"
2220 "<field signature=\"2404144663\" autofilltype=\"3\"/>"
2221 "<field signature=\"2404144663\" autofilltype=\"5\"/>"
2222 "<field signature=\"509334676\" autofilltype=\"30\"/>"
2223 "<field signature=\"509334676\" autofilltype=\"31\"/>"
2224 "</autofillupload>",
2226 possible_field_types[3].clear();
2227 possible_field_types[3].insert(ADDRESS_HOME_LINE1);
2228 possible_field_types[3].insert(COMPANY_NAME);
2229 form_structure->field(form_structure->field_count() - 1)->set_possible_types(
2230 possible_field_types[form_structure->field_count() - 1]);
2231 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2233 EXPECT_EQ("<\?xml version=\"1.0\" encoding=\"UTF-8\"\?>"
2234 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2235 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2236 " datapresent=\"1440000360000008\">"
2237 "<field signature=\"420638584\" autofilltype=\"9\"/>"
2238 "<field signature=\"1089846351\" autofilltype=\"3\"/>"
2239 "<field signature=\"2404144663\" autofilltype=\"3\"/>"
2240 "<field signature=\"2404144663\" autofilltype=\"5\"/>"
2241 "<field signature=\"509334676\" autofilltype=\"30\"/>"
2242 "<field signature=\"509334676\" autofilltype=\"60\"/>"
2243 "</autofillupload>",
2247 TEST(FormStructureTest, CheckFormSignature) {
2248 // Check that form signature is created correctly.
2249 scoped_ptr<FormStructure> form_structure;
2251 form.method = ASCIIToUTF16("post");
2253 FormFieldData field;
2254 field.form_control_type = "text";
2256 field.label = ASCIIToUTF16("email");
2257 field.name = ASCIIToUTF16("email");
2258 form.fields.push_back(field);
2260 field.label = ASCIIToUTF16("First Name");
2261 field.name = ASCIIToUTF16("first");
2262 form.fields.push_back(field);
2264 // Checkable fields shouldn't affect the signature.
2265 field.label = ASCIIToUTF16("Select");
2266 field.name = ASCIIToUTF16("Select");
2267 field.form_control_type = "checkbox";
2268 field.is_checkable = true;
2269 form.fields.push_back(field);
2271 form_structure.reset(new FormStructure(form));
2273 EXPECT_EQ(FormStructureTest::Hash64Bit(
2274 std::string("://&&email&first")),
2275 form_structure->FormSignature());
2277 form.origin = GURL(std::string("http://www.facebook.com"));
2278 form_structure.reset(new FormStructure(form));
2279 EXPECT_EQ(FormStructureTest::Hash64Bit(
2280 std::string("http://www.facebook.com&&email&first")),
2281 form_structure->FormSignature());
2283 form.action = GURL(std::string("https://login.facebook.com/path"));
2284 form_structure.reset(new FormStructure(form));
2285 EXPECT_EQ(FormStructureTest::Hash64Bit(
2286 std::string("https://login.facebook.com&&email&first")),
2287 form_structure->FormSignature());
2289 form.name = ASCIIToUTF16("login_form");
2290 form_structure.reset(new FormStructure(form));
2291 EXPECT_EQ(FormStructureTest::Hash64Bit(
2292 std::string("https://login.facebook.com&login_form&email&first")),
2293 form_structure->FormSignature());
2295 field.is_checkable = false;
2296 field.label = ASCIIToUTF16("Random Field label");
2297 field.name = ASCIIToUTF16("random1234");
2298 field.form_control_type = "text";
2299 form.fields.push_back(field);
2300 field.label = ASCIIToUTF16("Random Field label2");
2301 field.name = ASCIIToUTF16("random12345");
2302 form.fields.push_back(field);
2303 field.label = ASCIIToUTF16("Random Field label3");
2304 field.name = ASCIIToUTF16("1random12345678");
2305 form.fields.push_back(field);
2306 field.label = ASCIIToUTF16("Random Field label3");
2307 field.name = ASCIIToUTF16("12345random");
2308 form.fields.push_back(field);
2309 form_structure.reset(new FormStructure(form));
2310 EXPECT_EQ(FormStructureTest::Hash64Bit(
2311 std::string("https://login.facebook.com&login_form&email&first&"
2312 "random1234&random&1random&random")),
2313 form_structure->FormSignature());
2317 TEST(FormStructureTest, ToFormData) {
2319 form.name = ASCIIToUTF16("the-name");
2320 form.method = ASCIIToUTF16("POST");
2321 form.origin = GURL("http://cool.com");
2322 form.action = form.origin.Resolve("/login");
2324 FormFieldData field;
2325 field.label = ASCIIToUTF16("username");
2326 field.name = ASCIIToUTF16("username");
2327 field.form_control_type = "text";
2328 form.fields.push_back(field);
2330 field.label = ASCIIToUTF16("password");
2331 field.name = ASCIIToUTF16("password");
2332 field.form_control_type = "password";
2333 form.fields.push_back(field);
2335 field.label = base::string16();
2336 field.name = ASCIIToUTF16("Submit");
2337 field.form_control_type = "submit";
2338 form.fields.push_back(field);
2340 EXPECT_EQ(form, FormStructure(form).ToFormData());
2342 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always
2343 // false. This forces a future author that changes this to update this test.
2344 form.user_submitted = true;
2345 EXPECT_NE(form, FormStructure(form).ToFormData());
2348 TEST(FormStructureTest, SkipFieldTest) {
2350 form.name = ASCIIToUTF16("the-name");
2351 form.method = ASCIIToUTF16("POST");
2352 form.origin = GURL("http://cool.com");
2353 form.action = form.origin.Resolve("/login");
2355 FormFieldData field;
2356 field.label = ASCIIToUTF16("username");
2357 field.name = ASCIIToUTF16("username");
2358 field.form_control_type = "text";
2359 form.fields.push_back(field);
2361 field.label = ASCIIToUTF16("select");
2362 field.name = ASCIIToUTF16("select");
2363 field.form_control_type = "checkbox";
2364 field.is_checkable = true;
2365 form.fields.push_back(field);
2367 field.label = base::string16();
2368 field.name = ASCIIToUTF16("email");
2369 field.form_control_type = "text";
2370 field.is_checkable = false;
2371 form.fields.push_back(field);
2373 ScopedVector<FormStructure> forms;
2374 forms.push_back(new FormStructure(form));
2375 std::vector<std::string> encoded_signatures;
2376 std::string encoded_xml;
2378 const char * const kSignature = "18006745212084723782";
2379 const char * const kResponse =
2380 "<\?xml version=\"1.0\" encoding=\"UTF-8\"?><autofillquery "
2381 "clientversion=\"6.1.1715.1442/en (GGLL)\" accepts=\"e\"><form "
2382 "signature=\"18006745212084723782\"><field signature=\"239111655\"/>"
2383 "<field signature=\"420638584\"/></form></autofillquery>";
2384 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
2385 &encoded_signatures,
2387 ASSERT_EQ(1U, encoded_signatures.size());
2388 EXPECT_EQ(kSignature, encoded_signatures[0]);
2389 EXPECT_EQ(kResponse, encoded_xml);
2392 } // namespace autofill