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