Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / components / autofill / core / browser / form_structure_unittest.cc
blob73f146f9492136fc94bb14d06db76ddf00f2259c
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/metrics/field_trial.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.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 "components/rappor/test_rappor_service.h"
16 #include "components/variations/entropy_provider.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
20 using base::ASCIIToUTF16;
21 using rappor::TestRapporService;
23 namespace autofill {
25 namespace content {
27 std::ostream& operator<<(std::ostream& os, const FormData& form) {
28 os << base::UTF16ToUTF8(form.name)
29 << " "
30 << form.origin.spec()
31 << " "
32 << form.action.spec()
33 << " ";
35 for (std::vector<FormFieldData>::const_iterator iter =
36 form.fields.begin();
37 iter != form.fields.end(); ++iter) {
38 os << *iter
39 << " ";
42 return os;
45 } // namespace content
47 class FormStructureTest : public testing::Test {
48 public:
49 static std::string Hash64Bit(const std::string& str) {
50 return FormStructure::Hash64Bit(str);
53 void SetUp() override {
54 // By default this trial is enabled on tests.
55 EnableAutofillMetadataFieldTrial();
58 protected:
59 void DisableAutofillMetadataFieldTrial() {
60 field_trial_list_.reset(NULL);
63 private:
64 void EnableAutofillMetadataFieldTrial() {
65 // Clear the existing |field_trial_list_| to avoid firing a DCHECK.
66 field_trial_list_.reset(NULL);
67 field_trial_list_.reset(
68 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo")));
69 field_trial_ = base::FieldTrialList::CreateFieldTrial(
70 "AutofillFieldMetadata", "Enabled");
71 field_trial_->group();
74 scoped_ptr<base::FieldTrialList> field_trial_list_;
75 scoped_refptr<base::FieldTrial> field_trial_;
78 TEST_F(FormStructureTest, FieldCount) {
79 scoped_ptr<FormStructure> form_structure;
80 FormData form;
82 FormFieldData field;
83 field.label = ASCIIToUTF16("username");
84 field.name = ASCIIToUTF16("username");
85 field.form_control_type = "text";
86 form.fields.push_back(field);
88 field.label = ASCIIToUTF16("password");
89 field.name = ASCIIToUTF16("password");
90 field.form_control_type = "password";
91 form.fields.push_back(field);
93 field.label = base::string16();
94 field.name = ASCIIToUTF16("Submit");
95 field.form_control_type = "submit";
96 form.fields.push_back(field);
98 field.label = ASCIIToUTF16("address1");
99 field.name = ASCIIToUTF16("address1");
100 field.form_control_type = "text";
101 field.should_autocomplete = false;
102 form.fields.push_back(field);
104 // The render process sends all fields to browser including fields with
105 // autocomplete=off
106 form_structure.reset(new FormStructure(form));
107 EXPECT_EQ(4U, form_structure->field_count());
110 TEST_F(FormStructureTest, AutofillCount) {
111 scoped_ptr<FormStructure> form_structure;
112 FormData form;
114 FormFieldData field;
115 field.label = ASCIIToUTF16("username");
116 field.name = ASCIIToUTF16("username");
117 field.form_control_type = "text";
118 form.fields.push_back(field);
120 field.label = ASCIIToUTF16("password");
121 field.name = ASCIIToUTF16("password");
122 field.form_control_type = "password";
123 form.fields.push_back(field);
125 field.label = ASCIIToUTF16("email");
126 field.name = ASCIIToUTF16("email");
127 field.form_control_type = "text";
128 form.fields.push_back(field);
130 field.label = ASCIIToUTF16("city");
131 field.name = ASCIIToUTF16("city");
132 field.form_control_type = "text";
133 form.fields.push_back(field);
135 field.label = ASCIIToUTF16("state");
136 field.name = ASCIIToUTF16("state");
137 field.form_control_type = "select-one";
138 form.fields.push_back(field);
140 field.label = base::string16();
141 field.name = ASCIIToUTF16("Submit");
142 field.form_control_type = "submit";
143 form.fields.push_back(field);
145 // Only text and select fields that are heuristically matched are counted.
146 form_structure.reset(new FormStructure(form));
147 form_structure->DetermineHeuristicTypes();
148 EXPECT_EQ(3U, form_structure->autofill_count());
150 // Add a field with should_autocomplete=false. This should not be considered a
151 // fillable field.
152 field.label = ASCIIToUTF16("address1");
153 field.name = ASCIIToUTF16("address1");
154 field.form_control_type = "text";
155 field.should_autocomplete = false;
156 form.fields.push_back(field);
158 form_structure.reset(new FormStructure(form));
159 form_structure->DetermineHeuristicTypes();
160 EXPECT_EQ(4U, form_structure->autofill_count());
163 TEST_F(FormStructureTest, SourceURL) {
164 FormData form;
165 form.origin = GURL("http://www.foo.com/");
166 FormStructure form_structure(form);
168 EXPECT_EQ(form.origin, form_structure.source_url());
171 TEST_F(FormStructureTest, IsAutofillable) {
172 scoped_ptr<FormStructure> form_structure;
173 FormData form;
175 // We need at least three text fields to be auto-fillable.
176 FormFieldData field;
178 field.label = ASCIIToUTF16("username");
179 field.name = ASCIIToUTF16("username");
180 field.form_control_type = "text";
181 form.fields.push_back(field);
183 field.label = ASCIIToUTF16("password");
184 field.name = ASCIIToUTF16("password");
185 field.form_control_type = "password";
186 form.fields.push_back(field);
188 field.label = base::string16();
189 field.name = ASCIIToUTF16("Submit");
190 field.form_control_type = "submit";
191 form.fields.push_back(field);
193 form_structure.reset(new FormStructure(form));
194 form_structure->DetermineHeuristicTypes();
195 EXPECT_FALSE(form_structure->IsAutofillable());
197 // We now have three text fields, but only two auto-fillable fields.
198 field.label = ASCIIToUTF16("First Name");
199 field.name = ASCIIToUTF16("firstname");
200 field.form_control_type = "text";
201 form.fields.push_back(field);
203 field.label = ASCIIToUTF16("Last Name");
204 field.name = ASCIIToUTF16("lastname");
205 field.form_control_type = "text";
206 form.fields.push_back(field);
208 form_structure.reset(new FormStructure(form));
209 form_structure->DetermineHeuristicTypes();
210 EXPECT_FALSE(form_structure->IsAutofillable());
212 // We now have three auto-fillable fields.
213 field.label = ASCIIToUTF16("Email");
214 field.name = ASCIIToUTF16("email");
215 field.form_control_type = "email";
216 form.fields.push_back(field);
218 form_structure.reset(new FormStructure(form));
219 form_structure->DetermineHeuristicTypes();
220 EXPECT_TRUE(form_structure->IsAutofillable());
222 // The target cannot include http(s)://*/search...
223 form.action = GURL("http://google.com/search?q=hello");
224 form_structure.reset(new FormStructure(form));
225 form_structure->DetermineHeuristicTypes();
226 EXPECT_FALSE(form_structure->IsAutofillable());
228 // But search can be in the URL.
229 form.action = GURL("http://search.com/?q=hello");
230 form_structure.reset(new FormStructure(form));
231 form_structure->DetermineHeuristicTypes();
232 EXPECT_TRUE(form_structure->IsAutofillable());
235 TEST_F(FormStructureTest, ShouldBeParsed) {
236 scoped_ptr<FormStructure> form_structure;
237 FormData form;
239 // We need at least three text fields to be parseable.
240 FormFieldData field;
241 field.label = ASCIIToUTF16("username");
242 field.name = ASCIIToUTF16("username");
243 field.form_control_type = "text";
244 form.fields.push_back(field);
246 FormFieldData checkable_field;
247 checkable_field.is_checkable = true;
248 checkable_field.name = ASCIIToUTF16("radiobtn");
249 checkable_field.form_control_type = "radio";
250 form.fields.push_back(checkable_field);
252 checkable_field.name = ASCIIToUTF16("checkbox");
253 checkable_field.form_control_type = "checkbox";
254 form.fields.push_back(checkable_field);
256 // We have only one text field, should not be parsed.
257 form_structure.reset(new FormStructure(form));
258 EXPECT_FALSE(form_structure->ShouldBeParsed());
260 // We now have three text fields, though only two are auto-fillable.
261 field.label = ASCIIToUTF16("First Name");
262 field.name = ASCIIToUTF16("firstname");
263 field.form_control_type = "text";
264 form.fields.push_back(field);
266 field.label = ASCIIToUTF16("Last Name");
267 field.name = ASCIIToUTF16("lastname");
268 field.form_control_type = "text";
269 form.fields.push_back(field);
271 form_structure.reset(new FormStructure(form));
272 EXPECT_TRUE(form_structure->ShouldBeParsed());
274 form_structure.reset(new FormStructure(form));
275 EXPECT_FALSE(form_structure->IsAutofillable());
276 EXPECT_TRUE(form_structure->ShouldBeParsed());
278 // The target cannot include http(s)://*/search...
279 form.action = GURL("http://google.com/search?q=hello");
280 form_structure.reset(new FormStructure(form));
281 EXPECT_FALSE(form_structure->ShouldBeParsed());
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());
288 // The form need only have three fields, but at least one must be a text
289 // field.
290 form.fields.clear();
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());
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());
316 TEST_F(FormStructureTest, HeuristicsContactInfo) {
317 scoped_ptr<FormStructure> form_structure;
318 FormData form;
320 FormFieldData field;
321 field.form_control_type = "text";
323 field.label = ASCIIToUTF16("First Name");
324 field.name = ASCIIToUTF16("firstname");
325 form.fields.push_back(field);
327 field.label = ASCIIToUTF16("Last Name");
328 field.name = ASCIIToUTF16("lastname");
329 form.fields.push_back(field);
331 field.label = ASCIIToUTF16("Email");
332 field.name = ASCIIToUTF16("email");
333 form.fields.push_back(field);
335 field.label = ASCIIToUTF16("Phone");
336 field.name = ASCIIToUTF16("phone");
337 form.fields.push_back(field);
339 field.label = ASCIIToUTF16("Address");
340 field.name = ASCIIToUTF16("address");
341 form.fields.push_back(field);
343 field.label = ASCIIToUTF16("City");
344 field.name = ASCIIToUTF16("city");
345 form.fields.push_back(field);
347 field.label = ASCIIToUTF16("Zip code");
348 field.name = ASCIIToUTF16("zipcode");
349 form.fields.push_back(field);
351 field.label = base::string16();
352 field.name = ASCIIToUTF16("Submit");
353 field.form_control_type = "submit";
354 form.fields.push_back(field);
356 form_structure.reset(new FormStructure(form));
357 form_structure->DetermineHeuristicTypes();
358 EXPECT_TRUE(form_structure->IsAutofillable());
360 // Expect the correct number of fields.
361 ASSERT_EQ(8U, form_structure->field_count());
362 ASSERT_EQ(7U, form_structure->autofill_count());
364 // First name.
365 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
366 // Last name.
367 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
368 // Email.
369 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
370 // Phone.
371 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
372 form_structure->field(3)->heuristic_type());
373 // Address.
374 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type());
375 // City.
376 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type());
377 // Zip.
378 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type());
379 // Submit.
380 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type());
383 // Verify that we can correctly process the |autocomplete| attribute.
384 TEST_F(FormStructureTest, HeuristicsAutocompleteAttribute) {
385 scoped_ptr<FormStructure> form_structure;
386 FormData form;
388 FormFieldData field;
389 field.form_control_type = "text";
391 field.label = base::string16();
392 field.name = ASCIIToUTF16("field1");
393 field.autocomplete_attribute = "given-name";
394 form.fields.push_back(field);
396 field.label = base::string16();
397 field.name = ASCIIToUTF16("field2");
398 field.autocomplete_attribute = "family-name";
399 form.fields.push_back(field);
401 field.label = base::string16();
402 field.name = ASCIIToUTF16("field3");
403 field.autocomplete_attribute = "email";
404 form.fields.push_back(field);
406 form_structure.reset(new FormStructure(form));
407 form_structure->DetermineHeuristicTypes();
408 EXPECT_TRUE(form_structure->IsAutofillable());
410 // Expect the correct number of fields.
411 ASSERT_EQ(3U, form_structure->field_count());
412 ASSERT_EQ(3U, form_structure->autofill_count());
414 EXPECT_EQ(HTML_TYPE_GIVEN_NAME, form_structure->field(0)->html_type());
415 EXPECT_EQ(HTML_TYPE_FAMILY_NAME, form_structure->field(1)->html_type());
416 EXPECT_EQ(HTML_TYPE_EMAIL, form_structure->field(2)->html_type());
417 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type());
418 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
419 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
422 // Verify that we can correctly process the 'autocomplete' attribute for phone
423 // number types (especially phone prefixes and suffixes).
424 TEST_F(FormStructureTest, HeuristicsAutocompleteAttributePhoneTypes) {
425 scoped_ptr<FormStructure> form_structure;
426 FormData form;
428 FormFieldData field;
429 field.form_control_type = "text";
431 field.label = base::string16();
432 field.name = ASCIIToUTF16("field1");
433 field.autocomplete_attribute = "tel-local";
434 form.fields.push_back(field);
436 field.label = base::string16();
437 field.name = ASCIIToUTF16("field2");
438 field.autocomplete_attribute = "tel-local-prefix";
439 form.fields.push_back(field);
441 field.label = base::string16();
442 field.name = ASCIIToUTF16("field3");
443 field.autocomplete_attribute = "tel-local-suffix";
444 form.fields.push_back(field);
446 form_structure.reset(new FormStructure(form));
447 form_structure->DetermineHeuristicTypes();
448 EXPECT_TRUE(form_structure->IsAutofillable());
450 // Expect the correct number of fields.
451 ASSERT_EQ(3U, form_structure->field_count());
452 EXPECT_EQ(3U, form_structure->autofill_count());
454 EXPECT_EQ(HTML_TYPE_TEL_LOCAL, form_structure->field(0)->html_type());
455 EXPECT_EQ(AutofillField::IGNORED, form_structure->field(0)->phone_part());
456 EXPECT_EQ(HTML_TYPE_TEL_LOCAL_PREFIX, form_structure->field(1)->html_type());
457 EXPECT_EQ(AutofillField::PHONE_PREFIX,
458 form_structure->field(1)->phone_part());
459 EXPECT_EQ(HTML_TYPE_TEL_LOCAL_SUFFIX, form_structure->field(2)->html_type());
460 EXPECT_EQ(AutofillField::PHONE_SUFFIX,
461 form_structure->field(2)->phone_part());
464 // If at least one field includes type hints in the 'autocomplete' attribute, we
465 // should not try to apply any other heuristics.
466 TEST_F(FormStructureTest, AutocompleteAttributeOverridesOtherHeuristics) {
467 scoped_ptr<FormStructure> form_structure;
468 FormData form;
470 // Start with a regular contact form.
471 FormFieldData field;
472 field.form_control_type = "text";
474 field.label = ASCIIToUTF16("First Name");
475 field.name = ASCIIToUTF16("firstname");
476 form.fields.push_back(field);
478 field.label = ASCIIToUTF16("Last Name");
479 field.name = ASCIIToUTF16("lastname");
480 form.fields.push_back(field);
482 field.label = ASCIIToUTF16("Email");
483 field.name = ASCIIToUTF16("email");
484 form.fields.push_back(field);
486 form_structure.reset(new FormStructure(form));
487 form_structure->DetermineHeuristicTypes();
488 EXPECT_TRUE(form_structure->IsAutofillable());
489 EXPECT_TRUE(form_structure->ShouldBeCrowdsourced());
491 ASSERT_EQ(3U, form_structure->field_count());
492 ASSERT_EQ(3U, form_structure->autofill_count());
494 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
495 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
496 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
498 // Now update the first form field to include an 'autocomplete' attribute.
499 form.fields.front().autocomplete_attribute = "x-other";
500 form_structure.reset(new FormStructure(form));
501 form_structure->DetermineHeuristicTypes();
502 EXPECT_FALSE(form_structure->IsAutofillable());
503 EXPECT_FALSE(form_structure->ShouldBeCrowdsourced());
505 ASSERT_EQ(3U, form_structure->field_count());
506 ASSERT_EQ(0U, form_structure->autofill_count());
508 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(0)->heuristic_type());
509 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
510 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
513 // Even with an 'autocomplete' attribute set, ShouldBeCrowdsourced() should
514 // return true if the structure contains a password field, since there are
515 // no local heuristics to depend upon in this case. Fields will still not be
516 // considered autofillable though.
517 TEST_F(FormStructureTest, PasswordFormShouldBeCrowdsourced) {
518 FormData form;
520 // Start with a regular contact form.
521 FormFieldData field;
522 field.form_control_type = "text";
524 field.label = ASCIIToUTF16("First Name");
525 field.name = ASCIIToUTF16("firstname");
526 form.fields.push_back(field);
528 field.label = ASCIIToUTF16("Last Name");
529 field.name = ASCIIToUTF16("lastname");
530 form.fields.push_back(field);
532 field.label = ASCIIToUTF16("Email");
533 field.name = ASCIIToUTF16("email");
534 field.autocomplete_attribute = "username";
535 form.fields.push_back(field);
537 field.label = ASCIIToUTF16("Password");
538 field.name = ASCIIToUTF16("Password");
539 field.form_control_type = "password";
540 form.fields.push_back(field);
542 FormStructure form_structure(form);
543 form_structure.DetermineHeuristicTypes();
544 EXPECT_TRUE(form_structure.ShouldBeCrowdsourced());
547 // Verify that we can correctly process sections listed in the |autocomplete|
548 // attribute.
549 TEST_F(FormStructureTest, HeuristicsAutocompleteAttributeWithSections) {
550 FormData form;
552 FormFieldData field;
553 field.form_control_type = "text";
555 // Some fields will have no section specified. These fall into the default
556 // section.
557 field.autocomplete_attribute = "email";
558 form.fields.push_back(field);
560 // We allow arbitrary section names.
561 field.autocomplete_attribute = "section-foo email";
562 form.fields.push_back(field);
564 // "shipping" and "billing" are special section tokens that don't require the
565 // "section-" prefix.
566 field.autocomplete_attribute = "shipping email";
567 form.fields.push_back(field);
568 field.autocomplete_attribute = "billing email";
569 form.fields.push_back(field);
571 // "shipping" and "billing" can be combined with other section names.
572 field.autocomplete_attribute = "section-foo shipping email";
573 form.fields.push_back(field);
574 field.autocomplete_attribute = "section-foo billing email";
575 form.fields.push_back(field);
577 // We don't do anything clever to try to coalesce sections; it's up to site
578 // authors to avoid typos.
579 field.autocomplete_attribute = "section--foo email";
580 form.fields.push_back(field);
582 // "shipping email" and "section--shipping" email should be parsed as
583 // different sections. This is only an interesting test due to how we
584 // implement implicit section names from attributes like "shipping email"; see
585 // the implementation for more details.
586 field.autocomplete_attribute = "section--shipping email";
587 form.fields.push_back(field);
589 // Credit card fields are implicitly in a separate section from other fields.
590 field.autocomplete_attribute = "section-foo cc-number";
591 form.fields.push_back(field);
593 FormStructure form_structure(form);
594 form_structure.DetermineHeuristicTypes();
595 EXPECT_TRUE(form_structure.IsAutofillable());
597 // Expect the correct number of fields.
598 ASSERT_EQ(9U, form_structure.field_count());
599 EXPECT_EQ(9U, form_structure.autofill_count());
601 // All of the fields in this form should be parsed as belonging to different
602 // sections.
603 std::set<std::string> section_names;
604 for (size_t i = 0; i < 9; ++i) {
605 section_names.insert(form_structure.field(i)->section());
607 EXPECT_EQ(9U, section_names.size());
610 // Verify that we can correctly process a degenerate section listed in the
611 // |autocomplete| attribute.
612 TEST_F(FormStructureTest,
613 HeuristicsAutocompleteAttributeWithSectionsDegenerate) {
614 FormData form;
616 FormFieldData field;
617 field.form_control_type = "text";
619 // Some fields will have no section specified. These fall into the default
620 // section.
621 field.autocomplete_attribute = "email";
622 form.fields.push_back(field);
624 // Specifying "section-" is equivalent to not specifying a section.
625 field.autocomplete_attribute = "section- email";
626 form.fields.push_back(field);
628 // Invalid tokens should prevent us from setting a section name.
629 field.autocomplete_attribute = "garbage section-foo email";
630 form.fields.push_back(field);
631 field.autocomplete_attribute = "garbage section-bar email";
632 form.fields.push_back(field);
633 field.autocomplete_attribute = "garbage shipping email";
634 form.fields.push_back(field);
635 field.autocomplete_attribute = "garbage billing email";
636 form.fields.push_back(field);
638 FormStructure form_structure(form);
639 form_structure.DetermineHeuristicTypes();
641 // Expect the correct number of fields.
642 ASSERT_EQ(6U, form_structure.field_count());
643 EXPECT_EQ(2U, form_structure.autofill_count());
645 // All of the fields in this form should be parsed as belonging to the same
646 // section.
647 std::set<std::string> section_names;
648 for (size_t i = 0; i < 6; ++i) {
649 section_names.insert(form_structure.field(i)->section());
651 EXPECT_EQ(1U, section_names.size());
654 // Verify that we can correctly process repeated sections listed in the
655 // |autocomplete| attribute.
656 TEST_F(FormStructureTest, HeuristicsAutocompleteAttributeWithSectionsRepeated) {
657 FormData form;
659 FormFieldData field;
660 field.form_control_type = "text";
662 field.autocomplete_attribute = "section-foo email";
663 form.fields.push_back(field);
664 field.autocomplete_attribute = "section-foo address-line1";
665 form.fields.push_back(field);
667 FormStructure form_structure(form);
668 form_structure.DetermineHeuristicTypes();
670 // Expect the correct number of fields.
671 ASSERT_EQ(2U, form_structure.field_count());
672 EXPECT_EQ(2U, form_structure.autofill_count());
674 // All of the fields in this form should be parsed as belonging to the same
675 // section.
676 std::set<std::string> section_names;
677 for (size_t i = 0; i < 2; ++i) {
678 section_names.insert(form_structure.field(i)->section());
680 EXPECT_EQ(1U, section_names.size());
683 // Verify that we do not override the author-specified sections from a form with
684 // local heuristics.
685 TEST_F(FormStructureTest, HeuristicsDontOverrideAutocompleteAttributeSections) {
686 FormData form;
688 FormFieldData field;
689 field.form_control_type = "text";
691 field.name = ASCIIToUTF16("one");
692 field.autocomplete_attribute = "address-line1";
693 form.fields.push_back(field);
694 field.name = base::string16();
695 field.autocomplete_attribute = "section-foo email";
696 form.fields.push_back(field);
697 field.name = base::string16();
698 field.autocomplete_attribute = "name";
699 form.fields.push_back(field);
700 field.name = ASCIIToUTF16("two");
701 field.autocomplete_attribute = "address-line1";
702 form.fields.push_back(field);
704 FormStructure form_structure(form);
705 form_structure.DetermineHeuristicTypes();
707 // Expect the correct number of fields.
708 ASSERT_EQ(4U, form_structure.field_count());
709 EXPECT_EQ(4U, form_structure.autofill_count());
711 // Normally, the two separate address fields would cause us to detect two
712 // separate sections; but because there is an author-specified section in this
713 // form, we do not apply these usual heuristics.
714 EXPECT_EQ(ASCIIToUTF16("one"), form_structure.field(0)->name);
715 EXPECT_EQ(ASCIIToUTF16("two"), form_structure.field(3)->name);
716 EXPECT_EQ(form_structure.field(0)->section(),
717 form_structure.field(3)->section());
720 TEST_F(FormStructureTest, HeuristicsSample8) {
721 scoped_ptr<FormStructure> form_structure;
722 FormData form;
724 FormFieldData field;
725 field.form_control_type = "text";
727 field.label = ASCIIToUTF16("Your First Name:");
728 field.name = ASCIIToUTF16("bill.first");
729 form.fields.push_back(field);
731 field.label = ASCIIToUTF16("Your Last Name:");
732 field.name = ASCIIToUTF16("bill.last");
733 form.fields.push_back(field);
735 field.label = ASCIIToUTF16("Street Address Line 1:");
736 field.name = ASCIIToUTF16("bill.street1");
737 form.fields.push_back(field);
739 field.label = ASCIIToUTF16("Street Address Line 2:");
740 field.name = ASCIIToUTF16("bill.street2");
741 form.fields.push_back(field);
743 field.label = ASCIIToUTF16("City");
744 field.name = ASCIIToUTF16("bill.city");
745 form.fields.push_back(field);
747 field.label = ASCIIToUTF16("State (U.S.):");
748 field.name = ASCIIToUTF16("bill.state");
749 form.fields.push_back(field);
751 field.label = ASCIIToUTF16("Zip/Postal Code:");
752 field.name = ASCIIToUTF16("BillTo.PostalCode");
753 form.fields.push_back(field);
755 field.label = ASCIIToUTF16("Country:");
756 field.name = ASCIIToUTF16("bill.country");
757 form.fields.push_back(field);
759 field.label = ASCIIToUTF16("Phone Number:");
760 field.name = ASCIIToUTF16("BillTo.Phone");
761 form.fields.push_back(field);
763 field.label = base::string16();
764 field.name = ASCIIToUTF16("Submit");
765 field.form_control_type = "submit";
766 form.fields.push_back(field);
768 form_structure.reset(new FormStructure(form));
769 form_structure->DetermineHeuristicTypes();
770 EXPECT_TRUE(form_structure->IsAutofillable());
771 ASSERT_EQ(10U, form_structure->field_count());
772 ASSERT_EQ(9U, form_structure->autofill_count());
774 // First name.
775 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
776 // Last name.
777 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
778 // Address.
779 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(2)->heuristic_type());
780 // Address.
781 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(3)->heuristic_type());
782 // City.
783 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type());
784 // State.
785 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(5)->heuristic_type());
786 // Zip.
787 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type());
788 // Country.
789 EXPECT_EQ(ADDRESS_HOME_COUNTRY, form_structure->field(7)->heuristic_type());
790 // Phone.
791 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
792 form_structure->field(8)->heuristic_type());
793 // Submit.
794 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(9)->heuristic_type());
797 TEST_F(FormStructureTest, HeuristicsSample6) {
798 scoped_ptr<FormStructure> form_structure;
799 FormData form;
801 FormFieldData field;
802 field.form_control_type = "text";
804 field.label = ASCIIToUTF16("E-mail address");
805 field.name = ASCIIToUTF16("email");
806 form.fields.push_back(field);
808 field.label = ASCIIToUTF16("Full name");
809 field.name = ASCIIToUTF16("name");
810 form.fields.push_back(field);
812 field.label = ASCIIToUTF16("Company");
813 field.name = ASCIIToUTF16("company");
814 form.fields.push_back(field);
816 field.label = ASCIIToUTF16("Address");
817 field.name = ASCIIToUTF16("address");
818 form.fields.push_back(field);
820 field.label = ASCIIToUTF16("City");
821 field.name = ASCIIToUTF16("city");
822 form.fields.push_back(field);
824 field.label = ASCIIToUTF16("Zip Code");
825 field.name = ASCIIToUTF16("Home.PostalCode");
826 form.fields.push_back(field);
828 field.label = base::string16();
829 field.name = ASCIIToUTF16("Submit");
830 field.value = ASCIIToUTF16("continue");
831 field.form_control_type = "submit";
832 form.fields.push_back(field);
834 form_structure.reset(new FormStructure(form));
835 form_structure->DetermineHeuristicTypes();
836 EXPECT_TRUE(form_structure->IsAutofillable());
837 ASSERT_EQ(7U, form_structure->field_count());
838 ASSERT_EQ(6U, form_structure->autofill_count());
840 // Email.
841 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(0)->heuristic_type());
842 // Full name.
843 EXPECT_EQ(NAME_FULL, form_structure->field(1)->heuristic_type());
844 // Company
845 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type());
846 // Address.
847 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type());
848 // City.
849 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(4)->heuristic_type());
850 // Zip.
851 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(5)->heuristic_type());
852 // Submit.
853 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type());
856 // Tests a sequence of FormFields where only labels are supplied to heuristics
857 // for matching. This works because FormFieldData labels are matched in the
858 // case that input element ids (or |name| fields) are missing.
859 TEST_F(FormStructureTest, HeuristicsLabelsOnly) {
860 scoped_ptr<FormStructure> form_structure;
861 FormData form;
863 FormFieldData field;
864 field.form_control_type = "text";
866 field.label = ASCIIToUTF16("First Name");
867 field.name = base::string16();
868 form.fields.push_back(field);
870 field.label = ASCIIToUTF16("Last Name");
871 field.name = base::string16();
872 form.fields.push_back(field);
874 field.label = ASCIIToUTF16("Email");
875 field.name = base::string16();
876 form.fields.push_back(field);
878 field.label = ASCIIToUTF16("Phone");
879 field.name = base::string16();
880 form.fields.push_back(field);
882 field.label = ASCIIToUTF16("Address");
883 field.name = base::string16();
884 form.fields.push_back(field);
886 field.label = ASCIIToUTF16("Address");
887 field.name = base::string16();
888 form.fields.push_back(field);
890 field.label = ASCIIToUTF16("Zip code");
891 field.name = base::string16();
892 form.fields.push_back(field);
894 field.label = base::string16();
895 field.name = ASCIIToUTF16("Submit");
896 field.form_control_type = "submit";
897 form.fields.push_back(field);
899 form_structure.reset(new FormStructure(form));
900 form_structure->DetermineHeuristicTypes();
901 EXPECT_TRUE(form_structure->IsAutofillable());
902 ASSERT_EQ(8U, form_structure->field_count());
903 ASSERT_EQ(7U, form_structure->autofill_count());
905 // First name.
906 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
907 // Last name.
908 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
909 // Email.
910 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(2)->heuristic_type());
911 // Phone.
912 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
913 form_structure->field(3)->heuristic_type());
914 // Address.
915 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(4)->heuristic_type());
916 // Address Line 2.
917 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(5)->heuristic_type());
918 // Zip.
919 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(6)->heuristic_type());
920 // Submit.
921 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(7)->heuristic_type());
924 TEST_F(FormStructureTest, HeuristicsCreditCardInfo) {
925 scoped_ptr<FormStructure> form_structure;
926 FormData form;
928 FormFieldData field;
929 field.form_control_type = "text";
931 field.label = ASCIIToUTF16("Name on Card");
932 field.name = ASCIIToUTF16("name_on_card");
933 form.fields.push_back(field);
935 field.label = ASCIIToUTF16("Card Number");
936 field.name = ASCIIToUTF16("card_number");
937 form.fields.push_back(field);
939 field.label = ASCIIToUTF16("Exp Month");
940 field.name = ASCIIToUTF16("ccmonth");
941 form.fields.push_back(field);
943 field.label = ASCIIToUTF16("Exp Year");
944 field.name = ASCIIToUTF16("ccyear");
945 form.fields.push_back(field);
947 field.label = ASCIIToUTF16("Verification");
948 field.name = ASCIIToUTF16("verification");
949 form.fields.push_back(field);
951 field.label = base::string16();
952 field.name = ASCIIToUTF16("Submit");
953 field.form_control_type = "submit";
954 form.fields.push_back(field);
956 form_structure.reset(new FormStructure(form));
957 form_structure->DetermineHeuristicTypes();
958 EXPECT_TRUE(form_structure->IsAutofillable());
959 ASSERT_EQ(6U, form_structure->field_count());
960 ASSERT_EQ(5U, form_structure->autofill_count());
962 // Credit card name.
963 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
964 // Credit card number.
965 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(1)->heuristic_type());
966 // Credit card expiration month.
967 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(2)->heuristic_type());
968 // Credit card expiration year.
969 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
970 form_structure->field(3)->heuristic_type());
971 // CVV.
972 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
973 form_structure->field(4)->heuristic_type());
974 // Submit.
975 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(5)->heuristic_type());
978 TEST_F(FormStructureTest, HeuristicsCreditCardInfoWithUnknownCardField) {
979 scoped_ptr<FormStructure> form_structure;
980 FormData form;
982 FormFieldData field;
983 field.form_control_type = "text";
985 field.label = ASCIIToUTF16("Name on Card");
986 field.name = ASCIIToUTF16("name_on_card");
987 form.fields.push_back(field);
989 // This is not a field we know how to process. But we should skip over it
990 // and process the other fields in the card block.
991 field.label = ASCIIToUTF16("Card image");
992 field.name = ASCIIToUTF16("card_image");
993 form.fields.push_back(field);
995 field.label = ASCIIToUTF16("Card Number");
996 field.name = ASCIIToUTF16("card_number");
997 form.fields.push_back(field);
999 field.label = ASCIIToUTF16("Exp Month");
1000 field.name = ASCIIToUTF16("ccmonth");
1001 form.fields.push_back(field);
1003 field.label = ASCIIToUTF16("Exp Year");
1004 field.name = ASCIIToUTF16("ccyear");
1005 form.fields.push_back(field);
1007 field.label = ASCIIToUTF16("Verification");
1008 field.name = ASCIIToUTF16("verification");
1009 form.fields.push_back(field);
1011 field.label = base::string16();
1012 field.name = ASCIIToUTF16("Submit");
1013 field.form_control_type = "submit";
1014 form.fields.push_back(field);
1016 form_structure.reset(new FormStructure(form));
1017 form_structure->DetermineHeuristicTypes();
1018 EXPECT_TRUE(form_structure->IsAutofillable());
1019 ASSERT_EQ(7U, form_structure->field_count());
1020 ASSERT_EQ(5U, form_structure->autofill_count());
1022 // Credit card name.
1023 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
1024 // Credit card type. This is an unknown type but related to the credit card.
1025 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(1)->heuristic_type());
1026 // Credit card number.
1027 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type());
1028 // Credit card expiration month.
1029 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1030 // Credit card expiration year.
1031 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1032 form_structure->field(4)->heuristic_type());
1033 // CVV.
1034 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
1035 form_structure->field(5)->heuristic_type());
1036 // Submit.
1037 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(6)->heuristic_type());
1040 TEST_F(FormStructureTest, ThreeAddressLines) {
1041 scoped_ptr<FormStructure> form_structure;
1042 FormData form;
1044 FormFieldData field;
1045 field.form_control_type = "text";
1047 field.label = ASCIIToUTF16("Address Line1");
1048 field.name = ASCIIToUTF16("Address");
1049 form.fields.push_back(field);
1051 field.label = ASCIIToUTF16("Address Line2");
1052 field.name = ASCIIToUTF16("Address");
1053 form.fields.push_back(field);
1055 field.label = ASCIIToUTF16("Address Line3");
1056 field.name = ASCIIToUTF16("Address");
1057 form.fields.push_back(field);
1059 field.label = ASCIIToUTF16("City");
1060 field.name = ASCIIToUTF16("city");
1061 form.fields.push_back(field);
1063 form_structure.reset(new FormStructure(form));
1064 form_structure->DetermineHeuristicTypes();
1065 EXPECT_TRUE(form_structure->IsAutofillable());
1066 ASSERT_EQ(4U, form_structure->field_count());
1067 ASSERT_EQ(4U, form_structure->autofill_count());
1069 // Address Line 1.
1070 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1071 // Address Line 2.
1072 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1073 // Address Line 3.
1074 EXPECT_EQ(ADDRESS_HOME_LINE3, form_structure->field(2)->heuristic_type());
1075 // City.
1076 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type());
1079 // Numbered address lines after line two are ignored.
1080 TEST_F(FormStructureTest, SurplusAddressLinesIgnored) {
1081 scoped_ptr<FormStructure> form_structure;
1082 FormData form;
1084 FormFieldData field;
1085 field.form_control_type = "text";
1087 field.label = ASCIIToUTF16("Address Line1");
1088 field.name = ASCIIToUTF16("shipping.address.addressLine1");
1089 form.fields.push_back(field);
1091 field.label = ASCIIToUTF16("Address Line2");
1092 field.name = ASCIIToUTF16("shipping.address.addressLine2");
1093 form.fields.push_back(field);
1095 field.label = ASCIIToUTF16("Address Line3");
1096 field.name = ASCIIToUTF16("billing.address.addressLine3");
1097 form.fields.push_back(field);
1099 field.label = ASCIIToUTF16("Address Line4");
1100 field.name = ASCIIToUTF16("billing.address.addressLine4");
1101 form.fields.push_back(field);
1103 form_structure.reset(new FormStructure(form));
1104 form_structure->DetermineHeuristicTypes();
1105 ASSERT_EQ(4U, form_structure->field_count());
1106 ASSERT_EQ(3U, form_structure->autofill_count());
1108 // Address Line 1.
1109 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1110 // Address Line 2.
1111 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1112 // Address Line 3.
1113 EXPECT_EQ(ADDRESS_HOME_LINE3, form_structure->field(2)->heuristic_type());
1114 // Address Line 4 (ignored).
1115 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type());
1118 // This example comes from expedia.com where they used to use a "Suite" label
1119 // to indicate a suite or apartment number (the form has changed since this
1120 // test was written). We interpret this as address line 2. And the following
1121 // "Street address second line" we interpret as address line 3.
1122 // See http://crbug.com/48197 for details.
1123 TEST_F(FormStructureTest, ThreeAddressLinesExpedia) {
1124 scoped_ptr<FormStructure> form_structure;
1125 FormData form;
1127 FormFieldData field;
1128 field.form_control_type = "text";
1130 field.label = ASCIIToUTF16("Street:");
1131 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads1");
1132 form.fields.push_back(field);
1134 field.label = ASCIIToUTF16("Suite or Apt:");
1135 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adap");
1136 form.fields.push_back(field);
1138 field.label = ASCIIToUTF16("Street address second line");
1139 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_ads2");
1140 form.fields.push_back(field);
1142 field.label = ASCIIToUTF16("City:");
1143 field.name = ASCIIToUTF16("FOPIH_RgWebCC_0_IHAddress_adct");
1144 form.fields.push_back(field);
1146 form_structure.reset(new FormStructure(form));
1147 form_structure->DetermineHeuristicTypes();
1148 EXPECT_TRUE(form_structure->IsAutofillable());
1149 ASSERT_EQ(4U, form_structure->field_count());
1150 EXPECT_EQ(4U, form_structure->autofill_count());
1152 // Address Line 1.
1153 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1154 // Suite / Apt.
1155 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1156 // Address Line 3.
1157 EXPECT_EQ(ADDRESS_HOME_LINE3, form_structure->field(2)->heuristic_type());
1158 // City.
1159 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(3)->heuristic_type());
1162 // This example comes from ebay.com where the word "suite" appears in the label
1163 // and the name "address2" clearly indicates that this is the address line 2.
1164 // See http://crbug.com/48197 for details.
1165 TEST_F(FormStructureTest, TwoAddressLinesEbay) {
1166 scoped_ptr<FormStructure> form_structure;
1167 FormData form;
1169 FormFieldData field;
1170 field.form_control_type = "text";
1172 field.label = ASCIIToUTF16("Address Line1");
1173 field.name = ASCIIToUTF16("address1");
1174 form.fields.push_back(field);
1176 field.label = ASCIIToUTF16("Floor number, suite number, etc");
1177 field.name = ASCIIToUTF16("address2");
1178 form.fields.push_back(field);
1180 field.label = ASCIIToUTF16("City:");
1181 field.name = ASCIIToUTF16("city");
1182 form.fields.push_back(field);
1184 form_structure.reset(new FormStructure(form));
1185 form_structure->DetermineHeuristicTypes();
1186 EXPECT_TRUE(form_structure->IsAutofillable());
1187 ASSERT_EQ(3U, form_structure->field_count());
1188 ASSERT_EQ(3U, form_structure->autofill_count());
1190 // Address Line 1.
1191 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1192 // Address Line 2.
1193 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1194 // City.
1195 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(2)->heuristic_type());
1198 TEST_F(FormStructureTest, HeuristicsStateWithProvince) {
1199 scoped_ptr<FormStructure> form_structure;
1200 FormData form;
1202 FormFieldData field;
1203 field.form_control_type = "text";
1205 field.label = ASCIIToUTF16("Address Line1");
1206 field.name = ASCIIToUTF16("Address");
1207 form.fields.push_back(field);
1209 field.label = ASCIIToUTF16("Address Line2");
1210 field.name = ASCIIToUTF16("Address");
1211 form.fields.push_back(field);
1213 field.label = ASCIIToUTF16("State/Province/Region");
1214 field.name = ASCIIToUTF16("State");
1215 form.fields.push_back(field);
1217 form_structure.reset(new FormStructure(form));
1218 form_structure->DetermineHeuristicTypes();
1219 EXPECT_TRUE(form_structure->IsAutofillable());
1220 ASSERT_EQ(3U, form_structure->field_count());
1221 ASSERT_EQ(3U, form_structure->autofill_count());
1223 // Address Line 1.
1224 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(0)->heuristic_type());
1225 // Address Line 2.
1226 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(1)->heuristic_type());
1227 // State.
1228 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(2)->heuristic_type());
1231 // This example comes from lego.com's checkout page.
1232 TEST_F(FormStructureTest, HeuristicsWithBilling) {
1233 scoped_ptr<FormStructure> form_structure;
1234 FormData form;
1236 FormFieldData field;
1237 field.form_control_type = "text";
1239 field.label = ASCIIToUTF16("First Name*:");
1240 field.name = ASCIIToUTF16("editBillingAddress$firstNameBox");
1241 form.fields.push_back(field);
1243 field.label = ASCIIToUTF16("Last Name*:");
1244 field.name = ASCIIToUTF16("editBillingAddress$lastNameBox");
1245 form.fields.push_back(field);
1247 field.label = ASCIIToUTF16("Company Name:");
1248 field.name = ASCIIToUTF16("editBillingAddress$companyBox");
1249 form.fields.push_back(field);
1251 field.label = ASCIIToUTF16("Address*:");
1252 field.name = ASCIIToUTF16("editBillingAddress$addressLine1Box");
1253 form.fields.push_back(field);
1255 field.label = ASCIIToUTF16("Apt/Suite :");
1256 field.name = ASCIIToUTF16("editBillingAddress$addressLine2Box");
1257 form.fields.push_back(field);
1259 field.label = ASCIIToUTF16("City*:");
1260 field.name = ASCIIToUTF16("editBillingAddress$cityBox");
1261 form.fields.push_back(field);
1263 field.label = ASCIIToUTF16("State/Province*:");
1264 field.name = ASCIIToUTF16("editBillingAddress$stateDropDown");
1265 form.fields.push_back(field);
1267 field.label = ASCIIToUTF16("Country*:");
1268 field.name = ASCIIToUTF16("editBillingAddress$countryDropDown");
1269 form.fields.push_back(field);
1271 field.label = ASCIIToUTF16("Postal Code*:");
1272 field.name = ASCIIToUTF16("editBillingAddress$zipCodeBox");
1273 form.fields.push_back(field);
1275 field.label = ASCIIToUTF16("Phone*:");
1276 field.name = ASCIIToUTF16("editBillingAddress$phoneBox");
1277 form.fields.push_back(field);
1279 field.label = ASCIIToUTF16("Email Address*:");
1280 field.name = ASCIIToUTF16("email$emailBox");
1281 form.fields.push_back(field);
1283 form_structure.reset(new FormStructure(form));
1284 form_structure->DetermineHeuristicTypes();
1285 EXPECT_TRUE(form_structure->IsAutofillable());
1286 ASSERT_EQ(11U, form_structure->field_count());
1287 ASSERT_EQ(11U, form_structure->autofill_count());
1289 EXPECT_EQ(NAME_FIRST, form_structure->field(0)->heuristic_type());
1290 EXPECT_EQ(NAME_LAST, form_structure->field(1)->heuristic_type());
1291 EXPECT_EQ(COMPANY_NAME, form_structure->field(2)->heuristic_type());
1292 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(3)->heuristic_type());
1293 EXPECT_EQ(ADDRESS_HOME_LINE2, form_structure->field(4)->heuristic_type());
1294 EXPECT_EQ(ADDRESS_HOME_CITY, form_structure->field(5)->heuristic_type());
1295 EXPECT_EQ(ADDRESS_HOME_STATE, form_structure->field(6)->heuristic_type());
1296 EXPECT_EQ(ADDRESS_HOME_COUNTRY, form_structure->field(7)->heuristic_type());
1297 EXPECT_EQ(ADDRESS_HOME_ZIP, form_structure->field(8)->heuristic_type());
1298 EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
1299 form_structure->field(9)->heuristic_type());
1300 EXPECT_EQ(EMAIL_ADDRESS, form_structure->field(10)->heuristic_type());
1303 TEST_F(FormStructureTest, ThreePartPhoneNumber) {
1304 scoped_ptr<FormStructure> form_structure;
1305 FormData form;
1307 FormFieldData field;
1308 field.form_control_type = "text";
1310 field.label = ASCIIToUTF16("Phone:");
1311 field.name = ASCIIToUTF16("dayphone1");
1312 field.max_length = 0;
1313 form.fields.push_back(field);
1315 field.label = ASCIIToUTF16("-");
1316 field.name = ASCIIToUTF16("dayphone2");
1317 field.max_length = 3; // Size of prefix is 3.
1318 form.fields.push_back(field);
1320 field.label = ASCIIToUTF16("-");
1321 field.name = ASCIIToUTF16("dayphone3");
1322 field.max_length = 4; // Size of suffix is 4. If unlimited size is
1323 // passed, phone will be parsed as
1324 // <country code> - <area code> - <phone>.
1325 form.fields.push_back(field);
1327 field.label = ASCIIToUTF16("ext.:");
1328 field.name = ASCIIToUTF16("dayphone4");
1329 field.max_length = 0;
1330 form.fields.push_back(field);
1332 form_structure.reset(new FormStructure(form));
1333 form_structure->DetermineHeuristicTypes();
1334 EXPECT_TRUE(form_structure->IsAutofillable());
1335 ASSERT_EQ(4U, form_structure->field_count());
1336 ASSERT_EQ(3U, form_structure->autofill_count());
1338 // Area code.
1339 EXPECT_EQ(PHONE_HOME_CITY_CODE, form_structure->field(0)->heuristic_type());
1340 // Phone number suffix.
1341 EXPECT_EQ(PHONE_HOME_NUMBER,
1342 form_structure->field(1)->heuristic_type());
1343 // Phone number suffix.
1344 EXPECT_EQ(PHONE_HOME_NUMBER,
1345 form_structure->field(2)->heuristic_type());
1346 // Unknown.
1347 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(3)->heuristic_type());
1350 TEST_F(FormStructureTest, HeuristicsInfernoCC) {
1351 scoped_ptr<FormStructure> form_structure;
1352 FormData form;
1354 FormFieldData field;
1355 field.form_control_type = "text";
1357 field.label = ASCIIToUTF16("Name on Card");
1358 field.name = ASCIIToUTF16("name_on_card");
1359 form.fields.push_back(field);
1361 field.label = ASCIIToUTF16("Address");
1362 field.name = ASCIIToUTF16("billing_address");
1363 form.fields.push_back(field);
1365 field.label = ASCIIToUTF16("Card Number");
1366 field.name = ASCIIToUTF16("card_number");
1367 form.fields.push_back(field);
1369 field.label = ASCIIToUTF16("Expiration Date");
1370 field.name = ASCIIToUTF16("expiration_month");
1371 form.fields.push_back(field);
1373 field.label = ASCIIToUTF16("Expiration Year");
1374 field.name = ASCIIToUTF16("expiration_year");
1375 form.fields.push_back(field);
1377 form_structure.reset(new FormStructure(form));
1378 form_structure->DetermineHeuristicTypes();
1379 EXPECT_TRUE(form_structure->IsAutofillable());
1381 // Expect the correct number of fields.
1382 ASSERT_EQ(5U, form_structure->field_count());
1383 EXPECT_EQ(5U, form_structure->autofill_count());
1385 // Name on Card.
1386 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(0)->heuristic_type());
1387 // Address.
1388 EXPECT_EQ(ADDRESS_HOME_LINE1, form_structure->field(1)->heuristic_type());
1389 // Card Number.
1390 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(2)->heuristic_type());
1391 // Expiration Date.
1392 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1393 // Expiration Year.
1394 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1395 form_structure->field(4)->heuristic_type());
1398 TEST_F(FormStructureTest, CVCCodeClash) {
1399 scoped_ptr<FormStructure> form_structure;
1400 FormData form;
1402 FormFieldData field;
1403 field.form_control_type = "text";
1405 field.label = ASCIIToUTF16("Card number");
1406 field.name = ASCIIToUTF16("ccnumber");
1407 form.fields.push_back(field);
1409 field.label = ASCIIToUTF16("First name");
1410 field.name = ASCIIToUTF16("first_name");
1411 form.fields.push_back(field);
1413 field.label = ASCIIToUTF16("Last name");
1414 field.name = ASCIIToUTF16("last_name");
1415 form.fields.push_back(field);
1417 field.label = ASCIIToUTF16("Expiration date");
1418 field.name = ASCIIToUTF16("ccexpiresmonth");
1419 form.fields.push_back(field);
1421 field.label = base::string16();
1422 field.name = ASCIIToUTF16("ccexpiresyear");
1423 form.fields.push_back(field);
1425 field.label = ASCIIToUTF16("cvc number");
1426 field.name = ASCIIToUTF16("csc");
1427 form.fields.push_back(field);
1429 form_structure.reset(new FormStructure(form));
1430 form_structure->DetermineHeuristicTypes();
1431 EXPECT_TRUE(form_structure->IsAutofillable());
1433 // Expect the correct number of fields.
1434 ASSERT_EQ(6U, form_structure->field_count());
1435 ASSERT_EQ(5U, form_structure->autofill_count());
1437 // Card Number.
1438 EXPECT_EQ(CREDIT_CARD_NUMBER, form_structure->field(0)->heuristic_type());
1439 // First name, taken as name on card.
1440 EXPECT_EQ(CREDIT_CARD_NAME, form_structure->field(1)->heuristic_type());
1441 // Last name is not merged.
1442 EXPECT_EQ(UNKNOWN_TYPE, form_structure->field(2)->heuristic_type());
1443 // Expiration Date.
1444 EXPECT_EQ(CREDIT_CARD_EXP_MONTH, form_structure->field(3)->heuristic_type());
1445 // Expiration Year.
1446 EXPECT_EQ(CREDIT_CARD_EXP_4_DIGIT_YEAR,
1447 form_structure->field(4)->heuristic_type());
1448 // CVC code.
1449 EXPECT_EQ(CREDIT_CARD_VERIFICATION_CODE,
1450 form_structure->field(5)->heuristic_type());
1453 TEST_F(FormStructureTest, EncodeQueryRequest) {
1454 FormData form;
1456 FormFieldData field;
1457 field.form_control_type = "text";
1459 field.label = ASCIIToUTF16("Name on Card");
1460 field.name = ASCIIToUTF16("name_on_card");
1461 form.fields.push_back(field);
1463 field.label = ASCIIToUTF16("Address");
1464 field.name = ASCIIToUTF16("billing_address");
1465 form.fields.push_back(field);
1467 field.label = ASCIIToUTF16("Card Number");
1468 field.name = ASCIIToUTF16("card_number");
1469 form.fields.push_back(field);
1471 field.label = ASCIIToUTF16("Expiration Date");
1472 field.name = ASCIIToUTF16("expiration_month");
1473 form.fields.push_back(field);
1475 field.label = ASCIIToUTF16("Expiration Year");
1476 field.name = ASCIIToUTF16("expiration_year");
1477 form.fields.push_back(field);
1479 // Add checkable field.
1480 FormFieldData checkable_field;
1481 checkable_field.is_checkable = true;
1482 checkable_field.label = ASCIIToUTF16("Checkable1");
1483 checkable_field.name = ASCIIToUTF16("Checkable1");
1484 form.fields.push_back(checkable_field);
1486 ScopedVector<FormStructure> forms;
1487 forms.push_back(new FormStructure(form));
1488 std::vector<std::string> encoded_signatures;
1489 std::string encoded_xml;
1490 const char kSignature1[] = "11337937696949187602";
1491 const char kResponse1[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1492 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
1493 "<form signature=\"11337937696949187602\">"
1494 "<field signature=\"412125936\" name=\"name_on_card\" type=\"text\"/>"
1495 "<field signature=\"1917667676\" name=\"billing_address\" type=\"text\"/>"
1496 "<field signature=\"2226358947\" name=\"card_number\" type=\"text\"/>"
1497 "<field signature=\"747221617\" name=\"expiration_month\" type=\"text\"/>"
1498 "<field signature=\"4108155786\" name=\"expiration_year\" type=\"text\"/>"
1499 "</form></autofillquery>";
1500 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1501 &encoded_signatures,
1502 &encoded_xml));
1503 ASSERT_EQ(1U, encoded_signatures.size());
1504 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1505 EXPECT_EQ(kResponse1, encoded_xml);
1507 // Add the same form, only one will be encoded, so EncodeQueryRequest() should
1508 // return the same data.
1509 forms.push_back(new FormStructure(form));
1510 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1511 &encoded_signatures,
1512 &encoded_xml));
1513 ASSERT_EQ(1U, encoded_signatures.size());
1514 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1515 EXPECT_EQ(kResponse1, encoded_xml);
1516 // Add 5 address fields - this should be still a valid form.
1517 for (size_t i = 0; i < 5; ++i) {
1518 field.label = ASCIIToUTF16("Address");
1519 field.name = ASCIIToUTF16("address");
1520 form.fields.push_back(field);
1523 forms.push_back(new FormStructure(form));
1524 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1525 &encoded_signatures,
1526 &encoded_xml));
1527 ASSERT_EQ(2U, encoded_signatures.size());
1528 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1529 const char kSignature2[] = "8308881815906226214";
1530 EXPECT_EQ(kSignature2, encoded_signatures[1]);
1531 const char kResponse2[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1532 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
1533 "<form signature=\"11337937696949187602\">"
1534 "<field signature=\"412125936\" name=\"name_on_card\" type=\"text\"/>"
1535 "<field signature=\"1917667676\" name=\"billing_address\" type=\"text\"/>"
1536 "<field signature=\"2226358947\" name=\"card_number\" type=\"text\"/>"
1537 "<field signature=\"747221617\" name=\"expiration_month\" type=\"text\"/>"
1538 "<field signature=\"4108155786\" name=\"expiration_year\" type=\"text\"/>"
1539 "</form><form signature=\"8308881815906226214\">"
1540 "<field signature=\"412125936\" name=\"name_on_card\" type=\"text\"/>"
1541 "<field signature=\"1917667676\" name=\"billing_address\" type=\"text\"/>"
1542 "<field signature=\"2226358947\" name=\"card_number\" type=\"text\"/>"
1543 "<field signature=\"747221617\" name=\"expiration_month\" type=\"text\"/>"
1544 "<field signature=\"4108155786\" name=\"expiration_year\" type=\"text\"/>"
1545 "<field signature=\"509334676\" name=\"address\" type=\"text\"/>"
1546 "<field signature=\"509334676\" name=\"address\" type=\"text\"/>"
1547 "<field signature=\"509334676\" name=\"address\" type=\"text\"/>"
1548 "<field signature=\"509334676\" name=\"address\" type=\"text\"/>"
1549 "<field signature=\"509334676\" name=\"address\" type=\"text\"/>"
1550 "</form></autofillquery>";
1551 EXPECT_EQ(kResponse2, encoded_xml);
1553 FormData malformed_form(form);
1554 // Add 50 address fields - the form is not valid anymore, but previous ones
1555 // are. The result should be the same as in previous test.
1556 for (size_t i = 0; i < 50; ++i) {
1557 field.label = ASCIIToUTF16("Address");
1558 field.name = ASCIIToUTF16("address");
1559 malformed_form.fields.push_back(field);
1562 forms.push_back(new FormStructure(malformed_form));
1563 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
1564 &encoded_signatures,
1565 &encoded_xml));
1566 ASSERT_EQ(2U, encoded_signatures.size());
1567 EXPECT_EQ(kSignature1, encoded_signatures[0]);
1568 EXPECT_EQ(kSignature2, encoded_signatures[1]);
1569 EXPECT_EQ(kResponse2, encoded_xml);
1571 // Check that we fail if there are only bad form(s).
1572 ScopedVector<FormStructure> bad_forms;
1573 bad_forms.push_back(new FormStructure(malformed_form));
1574 EXPECT_FALSE(FormStructure::EncodeQueryRequest(bad_forms.get(),
1575 &encoded_signatures,
1576 &encoded_xml));
1577 EXPECT_EQ(0U, encoded_signatures.size());
1578 EXPECT_EQ("", encoded_xml);
1581 TEST_F(FormStructureTest, EncodeUploadRequest) {
1582 scoped_ptr<FormStructure> form_structure;
1583 std::vector<ServerFieldTypeSet> possible_field_types;
1584 FormData form;
1585 form_structure.reset(new FormStructure(form));
1586 form_structure->DetermineHeuristicTypes();
1588 FormFieldData field;
1589 field.form_control_type = "text";
1591 field.label = ASCIIToUTF16("First Name");
1592 field.name = ASCIIToUTF16("firstname");
1593 form.fields.push_back(field);
1594 possible_field_types.push_back(ServerFieldTypeSet());
1595 possible_field_types.back().insert(NAME_FIRST);
1597 field.label = ASCIIToUTF16("Last Name");
1598 field.name = ASCIIToUTF16("lastname");
1599 form.fields.push_back(field);
1600 possible_field_types.push_back(ServerFieldTypeSet());
1601 possible_field_types.back().insert(NAME_LAST);
1603 field.label = ASCIIToUTF16("Email");
1604 field.name = ASCIIToUTF16("email");
1605 field.form_control_type = "email";
1606 form.fields.push_back(field);
1607 possible_field_types.push_back(ServerFieldTypeSet());
1608 possible_field_types.back().insert(EMAIL_ADDRESS);
1610 field.label = ASCIIToUTF16("Phone");
1611 field.name = ASCIIToUTF16("phone");
1612 field.form_control_type = "number";
1613 form.fields.push_back(field);
1614 possible_field_types.push_back(ServerFieldTypeSet());
1615 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER);
1617 field.label = ASCIIToUTF16("Country");
1618 field.name = ASCIIToUTF16("country");
1619 field.form_control_type = "select-one";
1620 form.fields.push_back(field);
1621 possible_field_types.push_back(ServerFieldTypeSet());
1622 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1624 // Add checkable field.
1625 FormFieldData checkable_field;
1626 checkable_field.is_checkable = true;
1627 checkable_field.label = ASCIIToUTF16("Checkable1");
1628 checkable_field.name = ASCIIToUTF16("Checkable1");
1629 form.fields.push_back(checkable_field);
1630 possible_field_types.push_back(ServerFieldTypeSet());
1631 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1633 form_structure.reset(new FormStructure(form));
1635 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1636 for (size_t i = 0; i < form_structure->field_count(); ++i)
1637 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1639 ServerFieldTypeSet available_field_types;
1640 available_field_types.insert(NAME_FIRST);
1641 available_field_types.insert(NAME_LAST);
1642 available_field_types.insert(ADDRESS_HOME_LINE1);
1643 available_field_types.insert(ADDRESS_HOME_LINE2);
1644 available_field_types.insert(ADDRESS_HOME_COUNTRY);
1645 available_field_types.insert(ADDRESS_BILLING_LINE1);
1646 available_field_types.insert(ADDRESS_BILLING_LINE2);
1647 available_field_types.insert(EMAIL_ADDRESS);
1648 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
1650 std::string encoded_xml;
1651 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
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=\"false\""
1656 " datapresent=\"144200030e\">"
1657 "<field signature=\"3763331450\" name=\"firstname\" type=\"text\""
1658 " autofilltype=\"3\"/>"
1659 "<field signature=\"3494530716\" name=\"lastname\" type=\"text\""
1660 " autofilltype=\"5\"/>"
1661 "<field signature=\"1029417091\" name=\"email\" type=\"email\""
1662 " autofilltype=\"9\"/>"
1663 "<field signature=\"466116101\" name=\"phone\" type=\"number\""
1664 " autofilltype=\"14\"/>"
1665 "<field signature=\"2799270304\" name=\"country\""
1666 " type=\"select-one\" autofilltype=\"36\"/></autofillupload>",
1667 encoded_xml);
1668 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true,
1669 &encoded_xml));
1670 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1671 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1672 " formsignature=\"8736493185895608956\" autofillused=\"true\""
1673 " datapresent=\"144200030e\">"
1674 "<field signature=\"3763331450\" name=\"firstname\" type=\"text\""
1675 " autofilltype=\"3\"/>"
1676 "<field signature=\"3494530716\" name=\"lastname\" type=\"text\""
1677 " autofilltype=\"5\"/>"
1678 "<field signature=\"1029417091\" name=\"email\" type=\"email\""
1679 " autofilltype=\"9\"/>"
1680 "<field signature=\"466116101\" name=\"phone\" type=\"number\""
1681 " autofilltype=\"14\"/>"
1682 "<field signature=\"2799270304\" name=\"country\""
1683 " type=\"select-one\" autofilltype=\"36\"/></autofillupload>",
1684 encoded_xml);
1686 // Add 2 address fields - this should be still a valid form.
1687 for (size_t i = 0; i < 2; ++i) {
1688 field.label = ASCIIToUTF16("Address");
1689 field.name = ASCIIToUTF16("address");
1690 field.form_control_type = "text";
1691 form.fields.push_back(field);
1692 possible_field_types.push_back(ServerFieldTypeSet());
1693 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
1694 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
1695 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
1696 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
1699 form_structure.reset(new FormStructure(form));
1700 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1701 for (size_t i = 0; i < form_structure->field_count(); ++i)
1702 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1704 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
1705 &encoded_xml));
1706 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1707 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1708 " formsignature=\"7816485729218079147\" autofillused=\"false\""
1709 " datapresent=\"144200030e\">"
1710 "<field signature=\"3763331450\" name=\"firstname\" type=\"text\""
1711 " autofilltype=\"3\"/>"
1712 "<field signature=\"3494530716\" name=\"lastname\" type=\"text\""
1713 " autofilltype=\"5\"/>"
1714 "<field signature=\"1029417091\" name=\"email\" type=\"email\""
1715 " autofilltype=\"9\"/>"
1716 "<field signature=\"466116101\" name=\"phone\" type=\"number\""
1717 " autofilltype=\"14\"/>"
1718 "<field signature=\"2799270304\" name=\"country\""
1719 " type=\"select-one\" autofilltype=\"36\"/>"
1720 "<field signature=\"509334676\" name=\"address\" type=\"text\""
1721 " autofilltype=\"30\"/>"
1722 "<field signature=\"509334676\" name=\"address\" type=\"text\""
1723 " autofilltype=\"31\"/>"
1724 "<field signature=\"509334676\" name=\"address\" type=\"text\""
1725 " autofilltype=\"37\"/>"
1726 "<field signature=\"509334676\" name=\"address\" type=\"text\""
1727 " autofilltype=\"38\"/>"
1728 "<field signature=\"509334676\" name=\"address\" type=\"text\""
1729 " autofilltype=\"30\"/>"
1730 "<field signature=\"509334676\" name=\"address\" type=\"text\""
1731 " autofilltype=\"31\"/>"
1732 "<field signature=\"509334676\" name=\"address\" type=\"text\""
1733 " autofilltype=\"37\"/>"
1734 "<field signature=\"509334676\" name=\"address\" type=\"text\""
1735 " autofilltype=\"38\"/></autofillupload>",
1736 encoded_xml);
1738 // Add 50 address fields - now the form is invalid, as it has too many fields.
1739 for (size_t i = 0; i < 50; ++i) {
1740 field.label = ASCIIToUTF16("Address");
1741 field.name = ASCIIToUTF16("address");
1742 field.form_control_type = "text";
1743 form.fields.push_back(field);
1744 possible_field_types.push_back(ServerFieldTypeSet());
1745 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
1746 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
1747 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
1748 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
1750 form_structure.reset(new FormStructure(form));
1751 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1752 for (size_t i = 0; i < form_structure->field_count(); ++i)
1753 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1754 EXPECT_FALSE(form_structure->EncodeUploadRequest(available_field_types, false,
1755 &encoded_xml));
1758 TEST_F(FormStructureTest, EncodeUploadRequestWithAutocomplete) {
1759 scoped_ptr<FormStructure> form_structure;
1760 std::vector<ServerFieldTypeSet> possible_field_types;
1761 FormData form;
1762 form_structure.reset(new FormStructure(form));
1763 form_structure->DetermineHeuristicTypes();
1765 FormFieldData field;
1766 field.form_control_type = "text";
1768 field.label = ASCIIToUTF16("First Name");
1769 field.name = ASCIIToUTF16("firstname");
1770 field.autocomplete_attribute = "given-name";
1771 form.fields.push_back(field);
1772 possible_field_types.push_back(ServerFieldTypeSet());
1773 possible_field_types.back().insert(NAME_FIRST);
1775 field.label = ASCIIToUTF16("Last Name");
1776 field.name = ASCIIToUTF16("lastname");
1777 field.autocomplete_attribute = "family-name";
1778 form.fields.push_back(field);
1779 possible_field_types.push_back(ServerFieldTypeSet());
1780 possible_field_types.back().insert(NAME_LAST);
1782 field.label = ASCIIToUTF16("Email");
1783 field.name = ASCIIToUTF16("email");
1784 field.form_control_type = "email";
1785 field.autocomplete_attribute = "email";
1786 form.fields.push_back(field);
1787 possible_field_types.push_back(ServerFieldTypeSet());
1788 possible_field_types.back().insert(EMAIL_ADDRESS);
1790 form_structure.reset(new FormStructure(form));
1792 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1793 for (size_t i = 0; i < form_structure->field_count(); ++i)
1794 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1796 ServerFieldTypeSet available_field_types;
1797 available_field_types.insert(NAME_FIRST);
1798 available_field_types.insert(NAME_LAST);
1799 available_field_types.insert(EMAIL_ADDRESS);
1801 std::string encoded_xml;
1802 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true,
1803 &encoded_xml));
1804 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1805 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1806 " formsignature=\"14746822798145140279\" autofillused=\"true\""
1807 " datapresent=\"1440\">"
1808 "<field signature=\"3763331450\" name=\"firstname\" type=\"text\""
1809 " autocomplete=\"given-name\" autofilltype=\"3\"/>"
1810 "<field signature=\"3494530716\" name=\"lastname\" type=\"text\""
1811 " autocomplete=\"family-name\" autofilltype=\"5\"/>"
1812 "<field signature=\"1029417091\" name=\"email\" type=\"email\""
1813 " autocomplete=\"email\" autofilltype=\"9\"/></autofillupload>",
1814 encoded_xml);
1817 TEST_F(FormStructureTest, EncodeUploadRequestPartialMetadata) {
1818 scoped_ptr<FormStructure> form_structure;
1819 std::vector<ServerFieldTypeSet> possible_field_types;
1820 FormData form;
1821 form_structure.reset(new FormStructure(form));
1822 form_structure->DetermineHeuristicTypes();
1824 FormFieldData field;
1825 field.form_control_type = "text";
1827 // Some fields don't have "name" or "autocomplete" attributes, and some have
1828 // neither.
1829 field.label = ASCIIToUTF16("First Name");
1830 form.fields.push_back(field);
1831 possible_field_types.push_back(ServerFieldTypeSet());
1832 possible_field_types.back().insert(NAME_FIRST);
1834 field.label = ASCIIToUTF16("Last Name");
1835 field.name = ASCIIToUTF16("lastname");
1836 field.autocomplete_attribute = "family-name";
1837 form.fields.push_back(field);
1838 possible_field_types.push_back(ServerFieldTypeSet());
1839 possible_field_types.back().insert(NAME_LAST);
1841 field.label = ASCIIToUTF16("Email");
1842 field.form_control_type = "email";
1843 field.autocomplete_attribute = "email";
1844 form.fields.push_back(field);
1845 possible_field_types.push_back(ServerFieldTypeSet());
1846 possible_field_types.back().insert(EMAIL_ADDRESS);
1848 form_structure.reset(new FormStructure(form));
1850 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1851 for (size_t i = 0; i < form_structure->field_count(); ++i)
1852 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1854 ServerFieldTypeSet available_field_types;
1855 available_field_types.insert(NAME_FIRST);
1856 available_field_types.insert(NAME_LAST);
1857 available_field_types.insert(EMAIL_ADDRESS);
1859 std::string encoded_xml;
1860 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true,
1861 &encoded_xml));
1862 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1863 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1864 " formsignature=\"13043654279838250996\" autofillused=\"true\""
1865 " datapresent=\"1440\">"
1866 "<field signature=\"1318412689\" type=\"text\" autofilltype=\"3\"/>"
1867 "<field signature=\"3494530716\" name=\"lastname\" type=\"text\""
1868 " autocomplete=\"family-name\" autofilltype=\"5\"/>"
1869 "<field signature=\"1545468175\" name=\"lastname\" type=\"email\""
1870 " autocomplete=\"email\" autofilltype=\"9\"/></autofillupload>",
1871 encoded_xml);
1874 // Sending field metadata to the server is disabled.
1875 TEST_F(FormStructureTest, EncodeUploadRequest_DisabledMetadataTrial) {
1876 DisableAutofillMetadataFieldTrial();
1878 scoped_ptr<FormStructure> form_structure;
1879 std::vector<ServerFieldTypeSet> possible_field_types;
1880 FormData form;
1881 form_structure.reset(new FormStructure(form));
1882 form_structure->DetermineHeuristicTypes();
1884 FormFieldData field;
1885 field.form_control_type = "text";
1887 field.label = ASCIIToUTF16("First Name");
1888 field.name = ASCIIToUTF16("firstname");
1889 field.autocomplete_attribute = "given-name";
1890 form.fields.push_back(field);
1891 possible_field_types.push_back(ServerFieldTypeSet());
1892 possible_field_types.back().insert(NAME_FIRST);
1894 field.label = ASCIIToUTF16("Last Name");
1895 field.name = ASCIIToUTF16("lastname");
1896 field.autocomplete_attribute = "family-name";
1897 form.fields.push_back(field);
1898 possible_field_types.push_back(ServerFieldTypeSet());
1899 possible_field_types.back().insert(NAME_LAST);
1901 field.label = ASCIIToUTF16("Email");
1902 field.name = ASCIIToUTF16("email");
1903 field.form_control_type = "email";
1904 field.autocomplete_attribute = "email";
1905 form.fields.push_back(field);
1906 possible_field_types.push_back(ServerFieldTypeSet());
1907 possible_field_types.back().insert(EMAIL_ADDRESS);
1909 form_structure.reset(new FormStructure(form));
1911 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1912 for (size_t i = 0; i < form_structure->field_count(); ++i)
1913 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1915 ServerFieldTypeSet available_field_types;
1916 available_field_types.insert(NAME_FIRST);
1917 available_field_types.insert(NAME_LAST);
1918 available_field_types.insert(EMAIL_ADDRESS);
1920 std::string encoded_xml;
1921 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, true,
1922 &encoded_xml));
1923 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1924 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
1925 " formsignature=\"14746822798145140279\" autofillused=\"true\""
1926 " datapresent=\"1440\">"
1927 "<field signature=\"3763331450\"/>"
1928 "<field signature=\"3494530716\"/>"
1929 "<field signature=\"1029417091\"/></autofillupload>",
1930 encoded_xml);
1933 TEST_F(FormStructureTest, EncodeFieldAssignments) {
1934 scoped_ptr<FormStructure> form_structure;
1935 std::vector<ServerFieldTypeSet> possible_field_types;
1936 FormData form;
1937 form_structure.reset(new FormStructure(form));
1938 form_structure->DetermineHeuristicTypes();
1940 FormFieldData field;
1941 field.form_control_type = "text";
1943 field.label = ASCIIToUTF16("First Name");
1944 field.name = ASCIIToUTF16("firstname");
1945 form.fields.push_back(field);
1946 possible_field_types.push_back(ServerFieldTypeSet());
1947 possible_field_types.back().insert(NAME_FIRST);
1949 field.label = ASCIIToUTF16("Last Name");
1950 field.name = ASCIIToUTF16("lastname");
1951 form.fields.push_back(field);
1952 possible_field_types.push_back(ServerFieldTypeSet());
1953 possible_field_types.back().insert(NAME_LAST);
1955 field.label = ASCIIToUTF16("Email");
1956 field.name = ASCIIToUTF16("email");
1957 field.form_control_type = "email";
1958 form.fields.push_back(field);
1959 possible_field_types.push_back(ServerFieldTypeSet());
1960 possible_field_types.back().insert(EMAIL_ADDRESS);
1962 field.label = ASCIIToUTF16("Phone");
1963 field.name = ASCIIToUTF16("phone");
1964 field.form_control_type = "number";
1965 form.fields.push_back(field);
1966 possible_field_types.push_back(ServerFieldTypeSet());
1967 possible_field_types.back().insert(PHONE_HOME_WHOLE_NUMBER);
1969 field.label = ASCIIToUTF16("Country");
1970 field.name = ASCIIToUTF16("country");
1971 field.form_control_type = "select-one";
1972 form.fields.push_back(field);
1973 possible_field_types.push_back(ServerFieldTypeSet());
1974 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1976 // Add checkable field.
1977 FormFieldData checkable_field;
1978 checkable_field.is_checkable = true;
1979 checkable_field.label = ASCIIToUTF16("Checkable1");
1980 checkable_field.name = ASCIIToUTF16("Checkable1");
1981 form.fields.push_back(checkable_field);
1982 possible_field_types.push_back(ServerFieldTypeSet());
1983 possible_field_types.back().insert(ADDRESS_HOME_COUNTRY);
1985 form_structure.reset(new FormStructure(form));
1987 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
1988 for (size_t i = 0; i < form_structure->field_count(); ++i)
1989 form_structure->field(i)->set_possible_types(possible_field_types[i]);
1991 ServerFieldTypeSet available_field_types;
1992 available_field_types.insert(NAME_FIRST);
1993 available_field_types.insert(NAME_LAST);
1994 available_field_types.insert(ADDRESS_HOME_LINE1);
1995 available_field_types.insert(ADDRESS_HOME_LINE2);
1996 available_field_types.insert(ADDRESS_HOME_COUNTRY);
1997 available_field_types.insert(ADDRESS_BILLING_LINE1);
1998 available_field_types.insert(ADDRESS_BILLING_LINE2);
1999 available_field_types.insert(EMAIL_ADDRESS);
2000 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
2002 std::string encoded_xml;
2003 EXPECT_TRUE(form_structure->EncodeFieldAssignments(
2004 available_field_types, &encoded_xml));
2005 EXPECT_EQ(
2006 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2007 "<fieldassignments formsignature=\"8736493185895608956\">"
2008 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>"
2009 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>"
2010 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>"
2011 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>"
2012 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>"
2013 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>"
2014 "</fieldassignments>",
2015 encoded_xml);
2017 // Add 2 address fields - this should be still a valid form.
2018 for (size_t i = 0; i < 2; ++i) {
2019 field.label = ASCIIToUTF16("Address");
2020 field.name = ASCIIToUTF16("address");
2021 field.form_control_type = "text";
2022 form.fields.push_back(field);
2023 possible_field_types.push_back(ServerFieldTypeSet());
2024 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
2025 possible_field_types.back().insert(ADDRESS_HOME_LINE2);
2026 possible_field_types.back().insert(ADDRESS_BILLING_LINE1);
2027 possible_field_types.back().insert(ADDRESS_BILLING_LINE2);
2030 form_structure.reset(new FormStructure(form));
2031 ASSERT_EQ(form_structure->field_count(), possible_field_types.size());
2032 for (size_t i = 0; i < form_structure->field_count(); ++i)
2033 form_structure->field(i)->set_possible_types(possible_field_types[i]);
2035 EXPECT_TRUE(form_structure->EncodeFieldAssignments(
2036 available_field_types, &encoded_xml));
2037 EXPECT_EQ(
2038 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2039 "<fieldassignments formsignature=\"7816485729218079147\">"
2040 "<fields fieldid=\"3763331450\" fieldtype=\"3\" name=\"firstname\"/>"
2041 "<fields fieldid=\"3494530716\" fieldtype=\"5\" name=\"lastname\"/>"
2042 "<fields fieldid=\"1029417091\" fieldtype=\"9\" name=\"email\"/>"
2043 "<fields fieldid=\"466116101\" fieldtype=\"14\" name=\"phone\"/>"
2044 "<fields fieldid=\"2799270304\" fieldtype=\"36\" name=\"country\"/>"
2045 "<fields fieldid=\"3410250678\" fieldtype=\"36\" name=\"Checkable1\"/>"
2046 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>"
2047 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>"
2048 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>"
2049 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>"
2050 "<fields fieldid=\"509334676\" fieldtype=\"30\" name=\"address\"/>"
2051 "<fields fieldid=\"509334676\" fieldtype=\"31\" name=\"address\"/>"
2052 "<fields fieldid=\"509334676\" fieldtype=\"37\" name=\"address\"/>"
2053 "<fields fieldid=\"509334676\" fieldtype=\"38\" name=\"address\"/>"
2054 "</fieldassignments>",
2055 encoded_xml);
2058 // Check that we compute the "datapresent" string correctly for the given
2059 // |available_types|.
2060 TEST_F(FormStructureTest, CheckDataPresence) {
2061 FormData form;
2063 FormFieldData field;
2064 field.form_control_type = "text";
2066 field.label = ASCIIToUTF16("First Name");
2067 field.name = ASCIIToUTF16("first");
2068 form.fields.push_back(field);
2070 field.label = ASCIIToUTF16("Last Name");
2071 field.name = ASCIIToUTF16("last");
2072 form.fields.push_back(field);
2074 field.label = ASCIIToUTF16("Email");
2075 field.name = ASCIIToUTF16("email");
2076 form.fields.push_back(field);
2078 FormStructure form_structure(form);
2080 ServerFieldTypeSet unknown_type;
2081 unknown_type.insert(UNKNOWN_TYPE);
2082 for (size_t i = 0; i < form_structure.field_count(); ++i)
2083 form_structure.field(i)->set_possible_types(unknown_type);
2085 // No available types.
2086 // datapresent should be "" == trimmmed(0x0000000000000000) ==
2087 // 0b0000000000000000000000000000000000000000000000000000000000000000
2088 ServerFieldTypeSet available_field_types;
2090 std::string encoded_xml;
2091 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2092 &encoded_xml));
2093 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2094 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2095 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2096 " datapresent=\"\">"
2097 "<field signature=\"1089846351\" name=\"first\" type=\"text\""
2098 " autofilltype=\"1\"/>"
2099 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2100 " autofilltype=\"1\"/>"
2101 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2102 " autofilltype=\"1\"/></autofillupload>",
2103 encoded_xml);
2105 // Only a few types available.
2106 // datapresent should be "1540000240" == trimmmed(0x1540000240000000) ==
2107 // 0b0001010101000000000000000000001001000000000000000000000000000000
2108 // The set bits are:
2109 // 3 == NAME_FIRST
2110 // 5 == NAME_LAST
2111 // 7 == NAME_FULL
2112 // 9 == EMAIL_ADDRESS
2113 // 30 == ADDRESS_HOME_LINE1
2114 // 33 == ADDRESS_HOME_CITY
2115 available_field_types.clear();
2116 available_field_types.insert(NAME_FIRST);
2117 available_field_types.insert(NAME_LAST);
2118 available_field_types.insert(NAME_FULL);
2119 available_field_types.insert(EMAIL_ADDRESS);
2120 available_field_types.insert(ADDRESS_HOME_LINE1);
2121 available_field_types.insert(ADDRESS_HOME_CITY);
2123 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2124 &encoded_xml));
2125 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2126 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2127 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2128 " datapresent=\"1540000240\">"
2129 "<field signature=\"1089846351\" name=\"first\" type=\"text\""
2130 " autofilltype=\"1\"/>"
2131 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2132 " autofilltype=\"1\"/>"
2133 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2134 " autofilltype=\"1\"/></autofillupload>",
2135 encoded_xml);
2137 // All supported non-credit card types available.
2138 // datapresent should be "1f7e000378000008" == trimmmed(0x1f7e000378000008) ==
2139 // 0b0001111101111110000000000000001101111000000000000000000000001000
2140 // The set bits are:
2141 // 3 == NAME_FIRST
2142 // 4 == NAME_MIDDLE
2143 // 5 == NAME_LAST
2144 // 6 == NAME_MIDDLE_INITIAL
2145 // 7 == NAME_FULL
2146 // 9 == EMAIL_ADDRESS
2147 // 10 == PHONE_HOME_NUMBER,
2148 // 11 == PHONE_HOME_CITY_CODE,
2149 // 12 == PHONE_HOME_COUNTRY_CODE,
2150 // 13 == PHONE_HOME_CITY_AND_NUMBER,
2151 // 14 == PHONE_HOME_WHOLE_NUMBER,
2152 // 30 == ADDRESS_HOME_LINE1
2153 // 31 == ADDRESS_HOME_LINE2
2154 // 33 == ADDRESS_HOME_CITY
2155 // 34 == ADDRESS_HOME_STATE
2156 // 35 == ADDRESS_HOME_ZIP
2157 // 36 == ADDRESS_HOME_COUNTRY
2158 // 60 == COMPANY_NAME
2159 available_field_types.clear();
2160 available_field_types.insert(NAME_FIRST);
2161 available_field_types.insert(NAME_MIDDLE);
2162 available_field_types.insert(NAME_LAST);
2163 available_field_types.insert(NAME_MIDDLE_INITIAL);
2164 available_field_types.insert(NAME_FULL);
2165 available_field_types.insert(EMAIL_ADDRESS);
2166 available_field_types.insert(PHONE_HOME_NUMBER);
2167 available_field_types.insert(PHONE_HOME_CITY_CODE);
2168 available_field_types.insert(PHONE_HOME_COUNTRY_CODE);
2169 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER);
2170 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
2171 available_field_types.insert(ADDRESS_HOME_LINE1);
2172 available_field_types.insert(ADDRESS_HOME_LINE2);
2173 available_field_types.insert(ADDRESS_HOME_CITY);
2174 available_field_types.insert(ADDRESS_HOME_STATE);
2175 available_field_types.insert(ADDRESS_HOME_ZIP);
2176 available_field_types.insert(ADDRESS_HOME_COUNTRY);
2177 available_field_types.insert(COMPANY_NAME);
2179 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2180 &encoded_xml));
2181 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2182 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2183 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2184 " datapresent=\"1f7e000378000008\">"
2185 "<field signature=\"1089846351\" name=\"first\" type=\"text\""
2186 " autofilltype=\"1\"/>"
2187 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2188 " autofilltype=\"1\"/>"
2189 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2190 " autofilltype=\"1\"/></autofillupload>",
2191 encoded_xml);
2193 // All supported credit card types available.
2194 // datapresent should be "0000000000001fc0" == trimmmed(0x0000000000001fc0) ==
2195 // 0b0000000000000000000000000000000000000000000000000001111111000000
2196 // The set bits are:
2197 // 51 == CREDIT_CARD_NAME
2198 // 52 == CREDIT_CARD_NUMBER
2199 // 53 == CREDIT_CARD_EXP_MONTH
2200 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR
2201 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR
2202 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
2203 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR
2204 available_field_types.clear();
2205 available_field_types.insert(CREDIT_CARD_NAME);
2206 available_field_types.insert(CREDIT_CARD_NUMBER);
2207 available_field_types.insert(CREDIT_CARD_EXP_MONTH);
2208 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2209 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2210 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR);
2211 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2213 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2214 &encoded_xml));
2215 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2216 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2217 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2218 " datapresent=\"0000000000001fc0\">"
2219 "<field signature=\"1089846351\" name=\"first\" type=\"text\""
2220 " autofilltype=\"1\"/><field signature=\"2404144663\" name=\"last\""
2221 " type=\"text\" autofilltype=\"1\"/>"
2222 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2223 " autofilltype=\"1\"/></autofillupload>",
2224 encoded_xml);
2226 // All supported types available.
2227 // datapresent should be "1f7e000378001fc8" == trimmmed(0x1f7e000378001fc8) ==
2228 // 0b0001111101111110000000000000001101111000000000000001111111001000
2229 // The set bits are:
2230 // 3 == NAME_FIRST
2231 // 4 == NAME_MIDDLE
2232 // 5 == NAME_LAST
2233 // 6 == NAME_MIDDLE_INITIAL
2234 // 7 == NAME_FULL
2235 // 9 == EMAIL_ADDRESS
2236 // 10 == PHONE_HOME_NUMBER,
2237 // 11 == PHONE_HOME_CITY_CODE,
2238 // 12 == PHONE_HOME_COUNTRY_CODE,
2239 // 13 == PHONE_HOME_CITY_AND_NUMBER,
2240 // 14 == PHONE_HOME_WHOLE_NUMBER,
2241 // 30 == ADDRESS_HOME_LINE1
2242 // 31 == ADDRESS_HOME_LINE2
2243 // 33 == ADDRESS_HOME_CITY
2244 // 34 == ADDRESS_HOME_STATE
2245 // 35 == ADDRESS_HOME_ZIP
2246 // 36 == ADDRESS_HOME_COUNTRY
2247 // 51 == CREDIT_CARD_NAME
2248 // 52 == CREDIT_CARD_NUMBER
2249 // 53 == CREDIT_CARD_EXP_MONTH
2250 // 54 == CREDIT_CARD_EXP_2_DIGIT_YEAR
2251 // 55 == CREDIT_CARD_EXP_4_DIGIT_YEAR
2252 // 56 == CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR
2253 // 57 == CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR
2254 // 60 == COMPANY_NAME
2255 available_field_types.clear();
2256 available_field_types.insert(NAME_FIRST);
2257 available_field_types.insert(NAME_MIDDLE);
2258 available_field_types.insert(NAME_LAST);
2259 available_field_types.insert(NAME_MIDDLE_INITIAL);
2260 available_field_types.insert(NAME_FULL);
2261 available_field_types.insert(EMAIL_ADDRESS);
2262 available_field_types.insert(PHONE_HOME_NUMBER);
2263 available_field_types.insert(PHONE_HOME_CITY_CODE);
2264 available_field_types.insert(PHONE_HOME_COUNTRY_CODE);
2265 available_field_types.insert(PHONE_HOME_CITY_AND_NUMBER);
2266 available_field_types.insert(PHONE_HOME_WHOLE_NUMBER);
2267 available_field_types.insert(ADDRESS_HOME_LINE1);
2268 available_field_types.insert(ADDRESS_HOME_LINE2);
2269 available_field_types.insert(ADDRESS_HOME_CITY);
2270 available_field_types.insert(ADDRESS_HOME_STATE);
2271 available_field_types.insert(ADDRESS_HOME_ZIP);
2272 available_field_types.insert(ADDRESS_HOME_COUNTRY);
2273 available_field_types.insert(CREDIT_CARD_NAME);
2274 available_field_types.insert(CREDIT_CARD_NUMBER);
2275 available_field_types.insert(CREDIT_CARD_EXP_MONTH);
2276 available_field_types.insert(CREDIT_CARD_EXP_2_DIGIT_YEAR);
2277 available_field_types.insert(CREDIT_CARD_EXP_4_DIGIT_YEAR);
2278 available_field_types.insert(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR);
2279 available_field_types.insert(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR);
2280 available_field_types.insert(COMPANY_NAME);
2282 EXPECT_TRUE(form_structure.EncodeUploadRequest(available_field_types, false,
2283 &encoded_xml));
2284 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2285 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2286 " formsignature=\"6402244543831589061\" autofillused=\"false\""
2287 " datapresent=\"1f7e000378001fc8\">"
2288 "<field signature=\"1089846351\" name=\"first\" type=\"text\""
2289 " autofilltype=\"1\"/>"
2290 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2291 " autofilltype=\"1\"/>"
2292 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2293 " autofilltype=\"1\"/></autofillupload>",
2294 encoded_xml);
2297 TEST_F(FormStructureTest, CheckMultipleTypes) {
2298 // Throughout this test, datapresent should be
2299 // 0x1440000360000008 ==
2300 // 0b0001010001000000000000000000001101100000000000000000000000001000
2301 // The set bits are:
2302 // 3 == NAME_FIRST
2303 // 5 == NAME_LAST
2304 // 9 == EMAIL_ADDRESS
2305 // 30 == ADDRESS_HOME_LINE1
2306 // 31 == ADDRESS_HOME_LINE2
2307 // 33 == ADDRESS_HOME_CITY
2308 // 34 == ADDRESS_HOME_STATE
2309 // 60 == COMPANY_NAME
2310 ServerFieldTypeSet available_field_types;
2311 available_field_types.insert(NAME_FIRST);
2312 available_field_types.insert(NAME_LAST);
2313 available_field_types.insert(EMAIL_ADDRESS);
2314 available_field_types.insert(ADDRESS_HOME_LINE1);
2315 available_field_types.insert(ADDRESS_HOME_LINE2);
2316 available_field_types.insert(ADDRESS_HOME_CITY);
2317 available_field_types.insert(ADDRESS_HOME_STATE);
2318 available_field_types.insert(COMPANY_NAME);
2320 // Check that multiple types for the field are processed correctly.
2321 scoped_ptr<FormStructure> form_structure;
2322 std::vector<ServerFieldTypeSet> possible_field_types;
2323 FormData form;
2325 FormFieldData field;
2326 field.form_control_type = "text";
2328 field.label = ASCIIToUTF16("email");
2329 field.name = ASCIIToUTF16("email");
2330 form.fields.push_back(field);
2331 possible_field_types.push_back(ServerFieldTypeSet());
2332 possible_field_types.back().insert(EMAIL_ADDRESS);
2334 field.label = ASCIIToUTF16("First Name");
2335 field.name = ASCIIToUTF16("first");
2336 form.fields.push_back(field);
2337 possible_field_types.push_back(ServerFieldTypeSet());
2338 possible_field_types.back().insert(NAME_FIRST);
2340 field.label = ASCIIToUTF16("Last Name");
2341 field.name = ASCIIToUTF16("last");
2342 form.fields.push_back(field);
2343 possible_field_types.push_back(ServerFieldTypeSet());
2344 possible_field_types.back().insert(NAME_LAST);
2346 field.label = ASCIIToUTF16("Address");
2347 field.name = ASCIIToUTF16("address");
2348 form.fields.push_back(field);
2349 possible_field_types.push_back(ServerFieldTypeSet());
2350 possible_field_types.back().insert(ADDRESS_HOME_LINE1);
2352 form_structure.reset(new FormStructure(form));
2354 for (size_t i = 0; i < form_structure->field_count(); ++i)
2355 form_structure->field(i)->set_possible_types(possible_field_types[i]);
2356 std::string encoded_xml;
2358 // Now we matched both fields singularly.
2359 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2360 &encoded_xml));
2361 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2362 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2363 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2364 " datapresent=\"1440000360000008\">"
2365 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2366 " autofilltype=\"9\"/><field signature=\"1089846351\""
2367 " name=\"first\" type=\"text\" autofilltype=\"3\"/>"
2368 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2369 " autofilltype=\"5\"/>"
2370 "<field signature=\"509334676\" name=\"address\" type=\"text\""
2371 " autofilltype=\"30\"/></autofillupload>",
2372 encoded_xml);
2373 // Match third field as both first and last.
2374 possible_field_types[2].insert(NAME_FIRST);
2375 form_structure->field(2)->set_possible_types(possible_field_types[2]);
2376 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2377 &encoded_xml));
2378 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2379 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2380 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2381 " datapresent=\"1440000360000008\">"
2382 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2383 " autofilltype=\"9\"/>"
2384 "<field signature=\"1089846351\" name=\"first\" type=\"text\""
2385 " autofilltype=\"3\"/>"
2386 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2387 " autofilltype=\"3\"/>"
2388 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2389 " autofilltype=\"5\"/>"
2390 "<field signature=\"509334676\" name=\"address\" type=\"text\""
2391 " autofilltype=\"30\"/></autofillupload>",
2392 encoded_xml);
2393 possible_field_types[3].insert(ADDRESS_HOME_LINE2);
2394 form_structure->field(form_structure->field_count() - 1)->set_possible_types(
2395 possible_field_types[form_structure->field_count() - 1]);
2396 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2397 &encoded_xml));
2398 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2399 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2400 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2401 " datapresent=\"1440000360000008\">"
2402 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2403 " autofilltype=\"9\"/>"
2404 "<field signature=\"1089846351\" name=\"first\" type=\"text\""
2405 " autofilltype=\"3\"/>"
2406 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2407 " autofilltype=\"3\"/>"
2408 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2409 " autofilltype=\"5\"/>"
2410 "<field signature=\"509334676\" name=\"address\" type=\"text\""
2411 " autofilltype=\"30\"/>"
2412 "<field signature=\"509334676\" name=\"address\" type=\"text\""
2413 " autofilltype=\"31\"/></autofillupload>",
2414 encoded_xml);
2415 possible_field_types[3].clear();
2416 possible_field_types[3].insert(ADDRESS_HOME_LINE1);
2417 possible_field_types[3].insert(COMPANY_NAME);
2418 form_structure->field(form_structure->field_count() - 1)->set_possible_types(
2419 possible_field_types[form_structure->field_count() - 1]);
2420 EXPECT_TRUE(form_structure->EncodeUploadRequest(available_field_types, false,
2421 &encoded_xml));
2422 EXPECT_EQ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2423 "<autofillupload clientversion=\"6.1.1715.1442/en (GGLL)\""
2424 " formsignature=\"18062476096658145866\" autofillused=\"false\""
2425 " datapresent=\"1440000360000008\">"
2426 "<field signature=\"420638584\" name=\"email\" type=\"text\""
2427 " autofilltype=\"9\"/>"
2428 "<field signature=\"1089846351\" name=\"first\" type=\"text\""
2429 " autofilltype=\"3\"/>"
2430 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2431 " autofilltype=\"3\"/>"
2432 "<field signature=\"2404144663\" name=\"last\" type=\"text\""
2433 " autofilltype=\"5\"/>"
2434 "<field signature=\"509334676\" name=\"address\" type=\"text\""
2435 " autofilltype=\"30\"/>"
2436 "<field signature=\"509334676\" name=\"address\" type=\"text\""
2437 " autofilltype=\"60\"/></autofillupload>",
2438 encoded_xml);
2441 TEST_F(FormStructureTest, CheckFormSignature) {
2442 // Check that form signature is created correctly.
2443 scoped_ptr<FormStructure> form_structure;
2444 FormData form;
2446 FormFieldData field;
2447 field.form_control_type = "text";
2449 field.label = ASCIIToUTF16("email");
2450 field.name = ASCIIToUTF16("email");
2451 form.fields.push_back(field);
2453 field.label = ASCIIToUTF16("First Name");
2454 field.name = ASCIIToUTF16("first");
2455 form.fields.push_back(field);
2457 // Checkable fields shouldn't affect the signature.
2458 field.label = ASCIIToUTF16("Select");
2459 field.name = ASCIIToUTF16("Select");
2460 field.form_control_type = "checkbox";
2461 field.is_checkable = true;
2462 form.fields.push_back(field);
2464 form_structure.reset(new FormStructure(form));
2466 EXPECT_EQ(FormStructureTest::Hash64Bit(
2467 std::string("://&&email&first")),
2468 form_structure->FormSignature());
2470 form.origin = GURL(std::string("http://www.facebook.com"));
2471 form_structure.reset(new FormStructure(form));
2472 EXPECT_EQ(FormStructureTest::Hash64Bit(
2473 std::string("http://www.facebook.com&&email&first")),
2474 form_structure->FormSignature());
2476 form.action = GURL(std::string("https://login.facebook.com/path"));
2477 form_structure.reset(new FormStructure(form));
2478 EXPECT_EQ(FormStructureTest::Hash64Bit(
2479 std::string("https://login.facebook.com&&email&first")),
2480 form_structure->FormSignature());
2482 form.name = ASCIIToUTF16("login_form");
2483 form_structure.reset(new FormStructure(form));
2484 EXPECT_EQ(FormStructureTest::Hash64Bit(
2485 std::string("https://login.facebook.com&login_form&email&first")),
2486 form_structure->FormSignature());
2488 field.is_checkable = false;
2489 field.label = ASCIIToUTF16("Random Field label");
2490 field.name = ASCIIToUTF16("random1234");
2491 field.form_control_type = "text";
2492 form.fields.push_back(field);
2493 field.label = ASCIIToUTF16("Random Field label2");
2494 field.name = ASCIIToUTF16("random12345");
2495 form.fields.push_back(field);
2496 field.label = ASCIIToUTF16("Random Field label3");
2497 field.name = ASCIIToUTF16("1random12345678");
2498 form.fields.push_back(field);
2499 field.label = ASCIIToUTF16("Random Field label3");
2500 field.name = ASCIIToUTF16("12345random");
2501 form.fields.push_back(field);
2502 form_structure.reset(new FormStructure(form));
2503 EXPECT_EQ(FormStructureTest::Hash64Bit(
2504 std::string("https://login.facebook.com&login_form&email&first&"
2505 "random1234&random&1random&random")),
2506 form_structure->FormSignature());
2509 TEST_F(FormStructureTest, ToFormData) {
2510 FormData form;
2511 form.name = ASCIIToUTF16("the-name");
2512 form.origin = GURL("http://cool.com");
2513 form.action = form.origin.Resolve("/login");
2515 FormFieldData field;
2516 field.label = ASCIIToUTF16("username");
2517 field.name = ASCIIToUTF16("username");
2518 field.form_control_type = "text";
2519 form.fields.push_back(field);
2521 field.label = ASCIIToUTF16("password");
2522 field.name = ASCIIToUTF16("password");
2523 field.form_control_type = "password";
2524 form.fields.push_back(field);
2526 field.label = base::string16();
2527 field.name = ASCIIToUTF16("Submit");
2528 field.form_control_type = "submit";
2529 form.fields.push_back(field);
2531 EXPECT_TRUE(form.SameFormAs(FormStructure(form).ToFormData()));
2533 // Currently |FormStructure(form_data)ToFormData().user_submitted| is always
2534 // false. This forces a future author that changes this to update this test.
2535 form.user_submitted = true;
2536 EXPECT_FALSE(form.SameFormAs(FormStructure(form).ToFormData()));
2539 TEST_F(FormStructureTest, SkipFieldTest) {
2540 FormData form;
2541 form.name = ASCIIToUTF16("the-name");
2542 form.origin = GURL("http://cool.com");
2543 form.action = form.origin.Resolve("/login");
2545 FormFieldData field;
2546 field.label = ASCIIToUTF16("username");
2547 field.name = ASCIIToUTF16("username");
2548 field.form_control_type = "text";
2549 form.fields.push_back(field);
2551 field.label = ASCIIToUTF16("select");
2552 field.name = ASCIIToUTF16("select");
2553 field.form_control_type = "checkbox";
2554 field.is_checkable = true;
2555 form.fields.push_back(field);
2557 field.label = base::string16();
2558 field.name = ASCIIToUTF16("email");
2559 field.form_control_type = "text";
2560 field.is_checkable = false;
2561 form.fields.push_back(field);
2563 ScopedVector<FormStructure> forms;
2564 forms.push_back(new FormStructure(form));
2565 std::vector<std::string> encoded_signatures;
2566 std::string encoded_xml;
2568 const char kSignature[] = "18006745212084723782";
2569 const char kResponse[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2570 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
2571 "<form signature=\"18006745212084723782\">"
2572 "<field signature=\"239111655\" name=\"username\" type=\"text\"/>"
2573 "<field signature=\"420638584\" name=\"email\" type=\"text\"/></form>"
2574 "</autofillquery>";
2575 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
2576 &encoded_signatures,
2577 &encoded_xml));
2578 ASSERT_EQ(1U, encoded_signatures.size());
2579 EXPECT_EQ(kSignature, encoded_signatures[0]);
2580 EXPECT_EQ(kResponse, encoded_xml);
2583 // One name is missing from one field.
2584 TEST_F(FormStructureTest, EncodeQueryRequest_MissingNames) {
2585 FormData form;
2586 // No name set for the form.
2587 form.origin = GURL("http://cool.com");
2588 form.action = form.origin.Resolve("/login");
2590 FormFieldData field;
2591 field.label = ASCIIToUTF16("username");
2592 field.name = ASCIIToUTF16("username");
2593 field.form_control_type = "text";
2594 form.fields.push_back(field);
2596 field.label = base::string16();
2597 // No name set for this field.
2598 field.name = ASCIIToUTF16("");
2599 field.form_control_type = "text";
2600 field.is_checkable = false;
2601 form.fields.push_back(field);
2603 ScopedVector<FormStructure> forms;
2604 forms.push_back(new FormStructure(form));
2605 std::vector<std::string> encoded_signatures;
2606 std::string encoded_xml;
2608 const char kSignature[] = "16416961345885087496";
2609 const char kResponse[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2610 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
2611 "<form signature=\"16416961345885087496\">"
2612 "<field signature=\"239111655\" name=\"username\" type=\"text\"/>"
2613 "<field signature=\"1318412689\" type=\"text\"/></form></autofillquery>";
2614 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
2615 &encoded_signatures,
2616 &encoded_xml));
2617 ASSERT_EQ(1U, encoded_signatures.size());
2618 EXPECT_EQ(kSignature, encoded_signatures[0]);
2619 EXPECT_EQ(kResponse, encoded_xml);
2622 // Sending field metadata to the server is disabled.
2623 TEST_F(FormStructureTest, EncodeQueryRequest_DisabledMetadataTrial) {
2624 DisableAutofillMetadataFieldTrial();
2626 FormData form;
2627 // No name set for the form.
2628 form.origin = GURL("http://cool.com");
2629 form.action = form.origin.Resolve("/login");
2631 FormFieldData field;
2632 field.label = ASCIIToUTF16("username");
2633 field.name = ASCIIToUTF16("username");
2634 field.form_control_type = "text";
2635 form.fields.push_back(field);
2637 field.label = base::string16();
2638 field.name = ASCIIToUTF16("country");
2639 field.form_control_type = "text";
2640 field.is_checkable = false;
2641 form.fields.push_back(field);
2643 ScopedVector<FormStructure> forms;
2644 forms.push_back(new FormStructure(form));
2645 std::vector<std::string> encoded_signatures;
2646 std::string encoded_xml;
2648 const char kSignature[] = "7635954436925888745";
2649 const char kResponse[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
2650 "<autofillquery clientversion=\"6.1.1715.1442/en (GGLL)\">"
2651 "<form signature=\"7635954436925888745\">"
2652 "<field signature=\"239111655\"/>"
2653 "<field signature=\"3654076265\"/>"
2654 "</form></autofillquery>";
2655 ASSERT_TRUE(FormStructure::EncodeQueryRequest(forms.get(),
2656 &encoded_signatures,
2657 &encoded_xml));
2658 ASSERT_EQ(1U, encoded_signatures.size());
2659 EXPECT_EQ(kSignature, encoded_signatures[0]);
2660 EXPECT_EQ(kResponse, encoded_xml);
2663 TEST_F(FormStructureTest, PossibleValues) {
2664 FormData form_data;
2665 FormFieldData field;
2666 field.autocomplete_attribute = "billing country";
2667 field.option_contents.push_back(ASCIIToUTF16("Down Under"));
2668 field.option_values.push_back(ASCIIToUTF16("AU"));
2669 field.option_contents.push_back(ASCIIToUTF16("Fr"));
2670 field.option_values.push_back(ASCIIToUTF16(""));
2671 field.option_contents.push_back(ASCIIToUTF16("Germany"));
2672 field.option_values.push_back(ASCIIToUTF16("GRMNY"));
2673 form_data.fields.push_back(field);
2674 FormStructure form_structure(form_data);
2676 bool unused;
2677 form_structure.ParseFieldTypesFromAutocompleteAttributes(&unused, &unused);
2679 // All values in <option> value= or contents are returned, set to upper case.
2680 std::set<base::string16> possible_values =
2681 form_structure.PossibleValues(ADDRESS_BILLING_COUNTRY);
2682 EXPECT_EQ(5U, possible_values.size());
2683 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("AU")));
2684 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("FR")));
2685 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("DOWN UNDER")));
2686 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("GERMANY")));
2687 EXPECT_EQ(1U, possible_values.count(ASCIIToUTF16("GRMNY")));
2688 EXPECT_EQ(0U, possible_values.count(ASCIIToUTF16("Fr")));
2689 EXPECT_EQ(0U, possible_values.count(ASCIIToUTF16("DE")));
2691 // No field for the given type; empty value set.
2692 EXPECT_EQ(0U, form_structure.PossibleValues(ADDRESS_HOME_COUNTRY).size());
2694 // A freeform input (<input>) allows any value (overriding other <select>s).
2695 FormFieldData freeform_field;
2696 freeform_field.autocomplete_attribute = "billing country";
2697 form_data.fields.push_back(freeform_field);
2698 FormStructure form_structure2(form_data);
2699 form_structure2.ParseFieldTypesFromAutocompleteAttributes(&unused, &unused);
2700 EXPECT_EQ(0U, form_structure2.PossibleValues(ADDRESS_BILLING_COUNTRY).size());
2703 TEST_F(FormStructureTest, ParseQueryResponse) {
2704 TestRapporService rappor_service;
2705 FormData form;
2706 form.origin = GURL("http://foo.com");
2707 FormFieldData field;
2708 field.form_control_type = "text";
2710 field.label = ASCIIToUTF16("fullname");
2711 field.name = ASCIIToUTF16("fullname");
2712 form.fields.push_back(field);
2714 field.label = ASCIIToUTF16("address");
2715 field.name = ASCIIToUTF16("address");
2716 form.fields.push_back(field);
2718 // Checkable fields should be ignored in parsing
2719 FormFieldData checkable_field;
2720 checkable_field.label = ASCIIToUTF16("radio_button");
2721 checkable_field.form_control_type = "radio";
2722 checkable_field.is_checkable = true;
2723 form.fields.push_back(checkable_field);
2725 ScopedVector<FormStructure> forms;
2726 forms.push_back(new FormStructure(form));
2728 field.label = ASCIIToUTF16("email");
2729 field.name = ASCIIToUTF16("email");
2730 form.fields.push_back(field);
2732 field.label = ASCIIToUTF16("password");
2733 field.name = ASCIIToUTF16("password");
2734 field.form_control_type = "password";
2735 form.fields.push_back(field);
2737 forms.push_back(new FormStructure(form));
2739 std::string response =
2740 "<autofillqueryresponse>"
2741 "<field autofilltype=\"7\" />"
2742 "<field autofilltype=\"30\" />"
2743 "<field autofilltype=\"9\" />"
2744 "<field autofilltype=\"0\" />"
2745 "</autofillqueryresponse>";
2747 FormStructure::ParseQueryResponse(response, forms.get(), &rappor_service);
2749 ASSERT_GE(forms[0]->field_count(), 2U);
2750 ASSERT_GE(forms[1]->field_count(), 2U);
2751 EXPECT_EQ(7, forms[0]->field(0)->server_type());
2752 EXPECT_EQ(30, forms[0]->field(1)->server_type());
2753 EXPECT_EQ(9, forms[1]->field(0)->server_type());
2754 EXPECT_EQ(0, forms[1]->field(1)->server_type());
2756 // No RAPPOR metrics are logged in the case there is server data available for
2757 // all forms.
2758 EXPECT_EQ(0, rappor_service.GetReportsCount());
2761 // If user defined types are present, only parse password fields.
2762 TEST_F(FormStructureTest, ParseQueryResponseAuthorDefinedTypes) {
2763 TestRapporService rappor_service;
2764 FormData form;
2765 form.origin = GURL("http://foo.com");
2766 FormFieldData field;
2768 field.label = ASCIIToUTF16("email");
2769 field.name = ASCIIToUTF16("email");
2770 field.form_control_type = "text";
2771 field.autocomplete_attribute = "email";
2772 form.fields.push_back(field);
2774 field.label = ASCIIToUTF16("password");
2775 field.name = ASCIIToUTF16("password");
2776 field.form_control_type = "password";
2777 field.autocomplete_attribute = "new-password";
2778 form.fields.push_back(field);
2780 ScopedVector<FormStructure> forms;
2781 forms.push_back(new FormStructure(form));
2782 forms.front()->DetermineHeuristicTypes();
2784 std::string response =
2785 "<autofillqueryresponse>"
2786 "<field autofilltype=\"9\" />"
2787 "<field autofilltype=\"76\" />"
2788 "</autofillqueryresponse>";
2790 FormStructure::ParseQueryResponse(response, forms.get(), &rappor_service);
2792 ASSERT_GE(forms[0]->field_count(), 2U);
2793 EXPECT_EQ(NO_SERVER_DATA, forms[0]->field(0)->server_type());
2794 EXPECT_EQ(76, forms[0]->field(1)->server_type());
2797 // If the server returns NO_SERVER_DATA for one of the forms, expect RAPPOR
2798 // logging.
2799 TEST_F(FormStructureTest,
2800 ParseQueryResponse_RapporLogging_OneFormNoServerData) {
2801 TestRapporService rappor_service;
2802 FormData form;
2803 form.origin = GURL("http://foo.com");
2804 FormFieldData field;
2805 field.form_control_type = "text";
2807 field.label = ASCIIToUTF16("fullname");
2808 field.name = ASCIIToUTF16("fullname");
2809 form.fields.push_back(field);
2811 field.label = ASCIIToUTF16("address");
2812 field.name = ASCIIToUTF16("address");
2813 form.fields.push_back(field);
2815 ScopedVector<FormStructure> forms;
2816 forms.push_back(new FormStructure(form));
2818 field.label = ASCIIToUTF16("email");
2819 field.name = ASCIIToUTF16("email");
2820 form.fields.push_back(field);
2822 field.label = ASCIIToUTF16("password");
2823 field.name = ASCIIToUTF16("password");
2824 field.form_control_type = "password";
2825 form.fields.push_back(field);
2827 forms.push_back(new FormStructure(form));
2829 std::string response =
2830 "<autofillqueryresponse>"
2831 "<field autofilltype=\"0\" />"
2832 "<field autofilltype=\"0\" />"
2833 "<field autofilltype=\"9\" />"
2834 "<field autofilltype=\"0\" />"
2835 "</autofillqueryresponse>";
2837 FormStructure::ParseQueryResponse(response, forms.get(), &rappor_service);
2839 EXPECT_EQ(1, rappor_service.GetReportsCount());
2840 std::string sample;
2841 rappor::RapporType type;
2842 EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric(
2843 "Autofill.QueryResponseHasNoServerDataForForm", &sample, &type));
2844 EXPECT_EQ("foo.com", sample);
2845 EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type);
2848 // If the server returns NO_SERVER_DATA for both of the forms, expect RAPPOR
2849 // logging.
2850 TEST_F(FormStructureTest,
2851 ParseQueryResponse_RapporLogging_AllFormsNoServerData) {
2852 TestRapporService rappor_service;
2853 FormData form;
2854 form.origin = GURL("http://foo.com");
2855 FormFieldData field;
2856 field.form_control_type = "text";
2858 field.label = ASCIIToUTF16("fullname");
2859 field.name = ASCIIToUTF16("fullname");
2860 form.fields.push_back(field);
2862 field.label = ASCIIToUTF16("address");
2863 field.name = ASCIIToUTF16("address");
2864 form.fields.push_back(field);
2866 ScopedVector<FormStructure> forms;
2867 forms.push_back(new FormStructure(form));
2869 field.label = ASCIIToUTF16("email");
2870 field.name = ASCIIToUTF16("email");
2871 form.fields.push_back(field);
2873 field.label = ASCIIToUTF16("password");
2874 field.name = ASCIIToUTF16("password");
2875 field.form_control_type = "password";
2876 form.fields.push_back(field);
2878 forms.push_back(new FormStructure(form));
2880 std::string response =
2881 "<autofillqueryresponse>"
2882 "<field autofilltype=\"0\" />"
2883 "<field autofilltype=\"0\" />"
2884 "<field autofilltype=\"0\" />"
2885 "<field autofilltype=\"0\" />"
2886 "</autofillqueryresponse>";
2888 FormStructure::ParseQueryResponse(response, forms.get(), &rappor_service);
2890 // Even though both forms are logging to RAPPOR, there is only one sample for
2891 // a given eTLD+1.
2892 EXPECT_EQ(1, rappor_service.GetReportsCount());
2893 std::string sample;
2894 rappor::RapporType type;
2895 EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric(
2896 "Autofill.QueryResponseHasNoServerDataForForm", &sample, &type));
2897 EXPECT_EQ("foo.com", sample);
2898 EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type);
2901 // If the server returns NO_SERVER_DATA for only some of the fields, expect no
2902 // RAPPOR logging.
2903 TEST_F(FormStructureTest,
2904 ParseQueryResponse_RapporLogging_PartialNoServerData) {
2905 TestRapporService rappor_service;
2906 FormData form;
2907 form.origin = GURL("http://foo.com");
2908 FormFieldData field;
2909 field.form_control_type = "text";
2911 field.label = ASCIIToUTF16("fullname");
2912 field.name = ASCIIToUTF16("fullname");
2913 form.fields.push_back(field);
2915 field.label = ASCIIToUTF16("address");
2916 field.name = ASCIIToUTF16("address");
2917 form.fields.push_back(field);
2919 ScopedVector<FormStructure> forms;
2920 forms.push_back(new FormStructure(form));
2922 field.label = ASCIIToUTF16("email");
2923 field.name = ASCIIToUTF16("email");
2924 form.fields.push_back(field);
2926 field.label = ASCIIToUTF16("password");
2927 field.name = ASCIIToUTF16("password");
2928 field.form_control_type = "password";
2929 form.fields.push_back(field);
2931 forms.push_back(new FormStructure(form));
2933 std::string response =
2934 "<autofillqueryresponse>"
2935 "<field autofilltype=\"0\" />"
2936 "<field autofilltype=\"10\" />"
2937 "<field autofilltype=\"0\" />"
2938 "<field autofilltype=\"11\" />"
2939 "</autofillqueryresponse>";
2941 FormStructure::ParseQueryResponse(response, forms.get(), &rappor_service);
2943 // No RAPPOR metrics are logged in the case there is at least some server data
2944 // available for all forms.
2945 EXPECT_EQ(0, rappor_service.GetReportsCount());
2948 } // namespace autofill