- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / autofill / form_autofill_browsertest.cc
1 // Copyright (c) 2012 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 <vector>
6
7 #include "base/format_macros.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/test/base/chrome_render_view_test.h"
14 #include "components/autofill/content/renderer/form_autofill_util.h"
15 #include "components/autofill/content/renderer/form_cache.h"
16 #include "components/autofill/core/common/form_data.h"
17 #include "components/autofill/core/common/web_element_descriptor.h"
18 #include "components/variations/entropy_provider.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebVector.h"
22 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebElement.h"
24 #include "third_party/WebKit/public/web/WebFormControlElement.h"
25 #include "third_party/WebKit/public/web/WebFormElement.h"
26 #include "third_party/WebKit/public/web/WebInputElement.h"
27 #include "third_party/WebKit/public/web/WebNode.h"
28 #include "third_party/WebKit/public/web/WebSelectElement.h"
29 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
30
31 using WebKit::WebDocument;
32 using WebKit::WebElement;
33 using WebKit::WebFormControlElement;
34 using WebKit::WebFormElement;
35 using WebKit::WebFrame;
36 using WebKit::WebInputElement;
37 using WebKit::WebNode;
38 using WebKit::WebSelectElement;
39 using WebKit::WebString;
40 using WebKit::WebTextAreaElement;
41 using WebKit::WebVector;
42
43 namespace {
44
45 struct AutofillFieldCase {
46   const char* const form_control_type;
47   const char* const name;
48   const char* const initial_value;
49   const char* const autocomplete_attribute;  // The autocomplete attribute of
50                                              // the element.
51   bool should_be_autofilled;   // Whether the filed should be autofilled.
52   const char* const autofill_value;  // The value being used to fill the field.
53   const char* const expected_value;  // The expected value after Autofill
54                                      // or Preview.
55 };
56
57 static const char kFormHtml[] =
58     "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
59     "  <INPUT type=\"text\" id=\"firstname\"/>"
60     "  <INPUT type=\"text\" id=\"lastname\"/>"
61     "  <INPUT type=\"hidden\" id=\"imhidden\"/>"
62     "  <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>"
63     "  <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>"
64     "  <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>"
65     "  <INPUT type=\"text\" readonly id=\"readonly\"/>"
66     "  <INPUT type=\"text\" style=\"visibility: hidden\""
67     "         id=\"invisible\"/>"
68     "  <INPUT type=\"text\" style=\"display: none\" id=\"displaynone\"/>"
69     "  <INPUT type=\"month\" id=\"month\"/>"
70     "  <INPUT type=\"month\" id=\"month-nonempty\" value=\"2011-12\"/>"
71     "  <SELECT id=\"select\">"
72     "    <OPTION></OPTION>"
73     "    <OPTION value=\"CA\">California</OPTION>"
74     "    <OPTION value=\"TX\">Texas</OPTION>"
75     "  </SELECT>"
76     "  <SELECT id=\"select-nonempty\">"
77     "    <OPTION value=\"CA\" selected>California</OPTION>"
78     "    <OPTION value=\"TX\">Texas</OPTION>"
79     "  </SELECT>"
80     "  <TEXTAREA id=\"textarea\"></TEXTAREA>"
81     "  <TEXTAREA id=\"textarea-nonempty\">Go&#10;away!</TEXTAREA>"
82     "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
83     "</FORM>";
84
85 }  // namespace
86
87 namespace autofill {
88
89 class FormAutofillTest : public ChromeRenderViewTest {
90  public:
91   FormAutofillTest() : ChromeRenderViewTest() {}
92   virtual ~FormAutofillTest() {}
93
94   void ExpectLabels(const char* html,
95                     const std::vector<string16>& labels,
96                     const std::vector<string16>& names,
97                     const std::vector<string16>& values) {
98     std::vector<std::string> control_types(labels.size(), "text");
99     ExpectLabelsAndTypes(html, labels, names, values, control_types);
100   }
101
102   void ExpectLabelsAndTypes(const char* html,
103                             const std::vector<string16>& labels,
104                             const std::vector<string16>& names,
105                             const std::vector<string16>& values,
106                             const std::vector<std::string>& control_types) {
107     ASSERT_EQ(labels.size(), names.size());
108     ASSERT_EQ(labels.size(), values.size());
109     ASSERT_EQ(labels.size(), control_types.size());
110
111     LoadHTML(html);
112
113     WebFrame* web_frame = GetMainFrame();
114     ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
115
116     FormCache form_cache;
117     std::vector<FormData> forms;
118     form_cache.ExtractForms(*web_frame, &forms);
119     ASSERT_EQ(1U, forms.size());
120
121     const FormData& form = forms[0];
122     EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
123     EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
124     EXPECT_EQ(GURL("http://cnn.com"), form.action);
125
126     const std::vector<FormFieldData>& fields = form.fields;
127     ASSERT_EQ(labels.size(), fields.size());
128     for (size_t i = 0; i < labels.size(); ++i) {
129       int max_length = control_types[i] == "text" ?
130                        WebInputElement::defaultMaxLength() : 0;
131       FormFieldData expected;
132       expected.label = labels[i];
133       expected.name = names[i];
134       expected.value = values[i];
135       expected.form_control_type = control_types[i];
136       expected.max_length = max_length;
137       SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
138       EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
139     }
140   }
141
142   void ExpectJohnSmithLabels(const char* html) {
143     std::vector<string16> labels, names, values;
144
145     labels.push_back(ASCIIToUTF16("First name:"));
146     names.push_back(ASCIIToUTF16("firstname"));
147     values.push_back(ASCIIToUTF16("John"));
148
149     labels.push_back(ASCIIToUTF16("Last name:"));
150     names.push_back(ASCIIToUTF16("lastname"));
151     values.push_back(ASCIIToUTF16("Smith"));
152
153     labels.push_back(ASCIIToUTF16("Email:"));
154     names.push_back(ASCIIToUTF16("email"));
155     values.push_back(ASCIIToUTF16("john@example.com"));
156
157     ExpectLabels(html, labels, names, values);
158   }
159
160   typedef void (*FillFormFunction)(const FormData& form,
161                                    const WebInputElement& element);
162
163   typedef WebString (WebInputElement::*GetValueFunction)(void) const;
164
165   // Test FormFillxxx functions.
166   void TestFormFillFunctions(const char* html,
167                              const AutofillFieldCase* field_cases,
168                              size_t number_of_field_cases,
169                              FillFormFunction fill_form_function,
170                              GetValueFunction get_value_function) {
171     LoadHTML(html);
172
173     WebFrame* web_frame = GetMainFrame();
174     ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
175
176     FormCache form_cache;
177     std::vector<FormData> forms;
178     form_cache.ExtractForms(*web_frame, &forms);
179     ASSERT_EQ(1U, forms.size());
180
181     // Get the input element we want to find.
182     WebElement element = web_frame->document().getElementById("firstname");
183     WebInputElement input_element = element.to<WebInputElement>();
184
185     // Find the form that contains the input element.
186     FormData form_data;
187     FormFieldData field;
188     EXPECT_TRUE(
189         FindFormAndFieldForInputElement(input_element,
190                                         &form_data,
191                                         &field,
192                                         autofill::REQUIRE_AUTOCOMPLETE));
193     EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name);
194     EXPECT_EQ(GURL(web_frame->document().url()), form_data.origin);
195     EXPECT_EQ(GURL("http://buh.com"), form_data.action);
196
197     const std::vector<FormFieldData>& fields = form_data.fields;
198     ASSERT_EQ(number_of_field_cases, fields.size());
199
200     FormFieldData expected;
201     // Verify field's initial value.
202     for (size_t i = 0; i < number_of_field_cases; ++i) {
203       SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
204                                       field_cases[i].name));
205       expected.form_control_type = field_cases[i].form_control_type;
206       expected.max_length =
207           expected.form_control_type == "text" ?
208           WebInputElement::defaultMaxLength() : 0;
209       expected.name = ASCIIToUTF16(field_cases[i].name);
210       expected.value = ASCIIToUTF16(field_cases[i].initial_value);
211       expected.autocomplete_attribute = field_cases[i].autocomplete_attribute;
212       EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
213       // Fill the form_data for the field.
214       form_data.fields[i].value = ASCIIToUTF16(field_cases[i].autofill_value);
215     }
216
217     // Autofill the form using the given fill form function.
218     fill_form_function(form_data, input_element);
219
220     // Validate Autofill or Preview results.
221     for (size_t i = 0; i < number_of_field_cases; ++i) {
222       ValidteFilledField(field_cases[i], get_value_function);
223     }
224   }
225
226   // Validate an Autofilled field.
227   void ValidteFilledField(const AutofillFieldCase& field_case,
228                           GetValueFunction get_value_function) {
229     SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
230                                     field_case.name));
231     WebString value;
232     WebFormControlElement element = GetMainFrame()->document().getElementById(
233         ASCIIToUTF16(field_case.name)).to<WebFormControlElement>();
234     if (element.formControlType() == "select-one") {
235       value = element.to<WebSelectElement>().value();
236     } else if (element.formControlType() == "textarea") {
237       value = element.to<WebTextAreaElement>().value();
238     } else {
239       ASSERT_TRUE(element.formControlType() == "text" ||
240                   element.formControlType() == "month");
241       WebInputElement input_element = GetMainFrame()->document().getElementById(
242           ASCIIToUTF16(field_case.name)).to<WebInputElement>();
243       value = (input_element.*get_value_function)();
244     }
245
246     const WebString expected_value = ASCIIToUTF16(field_case.expected_value);
247     if (expected_value.isEmpty())
248       EXPECT_TRUE(value.isEmpty());
249     else
250       EXPECT_EQ(expected_value, value);
251
252     EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled());
253   }
254
255   static void FillFormForAllFieldsWrapper(const FormData& form,
256                                        const WebInputElement& element) {
257     FillFormForAllElements(form, element.form());
258   }
259
260   static void FillFormIncludingNonFocusableElementsWrapper(
261       const FormData& form,
262       const WebInputElement& element) {
263     FillFormIncludingNonFocusableElements(form, element.form());
264   }
265
266  private:
267   DISALLOW_COPY_AND_ASSIGN(FormAutofillTest);
268 };
269
270 // We should be able to extract a normal text field.
271 TEST_F(FormAutofillTest, WebFormControlElementToFormField) {
272   LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>");
273
274   WebFrame* frame = GetMainFrame();
275   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
276
277   WebElement web_element = frame->document().getElementById("element");
278   WebFormControlElement element = web_element.to<WebFormControlElement>();
279   FormFieldData result1;
280   WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result1);
281
282   FormFieldData expected;
283   expected.form_control_type = "text";
284   expected.max_length = WebInputElement::defaultMaxLength();
285
286   expected.name = ASCIIToUTF16("element");
287   expected.value = string16();
288   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
289
290   FormFieldData result2;
291   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result2);
292
293   expected.name = ASCIIToUTF16("element");
294   expected.value = ASCIIToUTF16("value");
295   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
296 }
297
298 // We should be able to extract a text field with autocomplete="off".
299 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompleteOff) {
300   LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\""
301            "       autocomplete=\"off\"/>");
302
303   WebFrame* frame = GetMainFrame();
304   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
305
306   WebElement web_element = frame->document().getElementById("element");
307   WebFormControlElement element = web_element.to<WebFormControlElement>();
308   FormFieldData result;
309   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
310
311   FormFieldData expected;
312   expected.name = ASCIIToUTF16("element");
313   expected.value = ASCIIToUTF16("value");
314   expected.form_control_type = "text";
315   expected.autocomplete_attribute = "off";
316   expected.max_length = WebInputElement::defaultMaxLength();
317   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
318 }
319
320 // We should be able to extract a text field with maxlength specified.
321 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMaxLength) {
322   LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\""
323            "       maxlength=\"5\"/>");
324
325   WebFrame* frame = GetMainFrame();
326   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
327
328   WebElement web_element = frame->document().getElementById("element");
329   WebFormControlElement element = web_element.to<WebFormControlElement>();
330   FormFieldData result;
331   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
332
333   FormFieldData expected;
334   expected.name = ASCIIToUTF16("element");
335   expected.value = ASCIIToUTF16("value");
336   expected.form_control_type = "text";
337   expected.max_length = 5;
338   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
339 }
340
341 // We should be able to extract a text field that has been autofilled.
342 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutofilled) {
343   LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>");
344
345   WebFrame* frame = GetMainFrame();
346   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
347
348   WebElement web_element = frame->document().getElementById("element");
349   WebInputElement element = web_element.to<WebInputElement>();
350   element.setAutofilled(true);
351   FormFieldData result;
352   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
353
354   FormFieldData expected;
355   expected.name = ASCIIToUTF16("element");
356   expected.value = ASCIIToUTF16("value");
357   expected.form_control_type = "text";
358   expected.max_length = WebInputElement::defaultMaxLength();
359   expected.is_autofilled = true;
360   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
361 }
362
363 // We should be able to extract a radio or a checkbox field that has been
364 // autofilled.
365 TEST_F(FormAutofillTest, WebFormControlElementToClickableFormField) {
366   LoadHTML("<INPUT type=\"checkbox\" id=\"checkbox\" value=\"mail\" checked/>"
367            "<INPUT type=\"radio\" id=\"radio\" value=\"male\"/>");
368
369   WebFrame* frame = GetMainFrame();
370   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
371
372   WebElement web_element = frame->document().getElementById("checkbox");
373   WebInputElement element = web_element.to<WebInputElement>();
374   element.setAutofilled(true);
375   FormFieldData result;
376   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
377
378   FormFieldData expected;
379   expected.name = ASCIIToUTF16("checkbox");
380   expected.value = ASCIIToUTF16("mail");
381   expected.form_control_type = "checkbox";
382   expected.is_autofilled = true;
383   expected.is_checkable = true;
384   expected.is_checked = true;
385   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
386
387   web_element = frame->document().getElementById("radio");
388   element = web_element.to<WebInputElement>();
389   element.setAutofilled(true);
390   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
391   expected.name = ASCIIToUTF16("radio");
392   expected.value = ASCIIToUTF16("male");
393   expected.form_control_type = "radio";
394   expected.is_autofilled = true;
395   expected.is_checkable = true;
396   expected.is_checked = false;
397   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
398 }
399
400 // We should be able to extract a <select> field.
401 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) {
402   LoadHTML("<SELECT id=\"element\"/>"
403            "  <OPTION value=\"CA\">California</OPTION>"
404            "  <OPTION value=\"TX\">Texas</OPTION>"
405            "</SELECT>");
406
407   WebFrame* frame = GetMainFrame();
408   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
409
410   WebElement web_element = frame->document().getElementById("element");
411   WebFormControlElement element = web_element.to<WebFormControlElement>();
412   FormFieldData result1;
413   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result1);
414
415   FormFieldData expected;
416   expected.name = ASCIIToUTF16("element");
417   expected.max_length = 0;
418   expected.form_control_type = "select-one";
419
420   expected.value = ASCIIToUTF16("CA");
421   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
422
423   FormFieldData result2;
424   WebFormControlElementToFormField(
425       element,
426       static_cast<autofill::ExtractMask>(autofill::EXTRACT_VALUE |
427                                          autofill::EXTRACT_OPTION_TEXT),
428       &result2);
429   expected.value = ASCIIToUTF16("California");
430   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
431
432   FormFieldData result3;
433   WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS,
434                                    &result3);
435   expected.value = string16();
436   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result3);
437
438   ASSERT_EQ(2U, result3.option_values.size());
439   ASSERT_EQ(2U, result3.option_contents.size());
440   EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]);
441   EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]);
442   EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]);
443   EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]);
444 }
445
446 // We should be able to extract a <textarea> field.
447 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) {
448   LoadHTML("<TEXTAREA id=\"element\">"
449              "This element's value&#10;"
450              "spans multiple lines."
451            "</TEXTAREA>");
452
453   WebFrame* frame = GetMainFrame();
454   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
455
456   WebElement web_element = frame->document().getElementById("element");
457   WebFormControlElement element = web_element.to<WebFormControlElement>();
458   FormFieldData result_sans_value;
459   WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
460                                    &result_sans_value);
461
462   FormFieldData expected;
463   expected.name = ASCIIToUTF16("element");
464   expected.max_length = 0;
465   expected.form_control_type = "textarea";
466   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
467
468   FormFieldData result_with_value;
469   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
470                                    &result_with_value);
471   expected.value = ASCIIToUTF16("This element's value\n"
472                                 "spans multiple lines.");
473   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
474 }
475
476 // We should be able to extract an <input type="month"> field.
477 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMonthInput) {
478   LoadHTML("<INPUT type=\"month\" id=\"element\" value=\"2011-12\">");
479
480   WebFrame* frame = GetMainFrame();
481   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
482
483   WebElement web_element = frame->document().getElementById("element");
484   WebFormControlElement element = web_element.to<WebFormControlElement>();
485   FormFieldData result_sans_value;
486   WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
487                                    &result_sans_value);
488
489   FormFieldData expected;
490   expected.name = ASCIIToUTF16("element");
491   expected.max_length = 0;
492   expected.form_control_type = "month";
493   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
494
495   FormFieldData result_with_value;
496   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
497                                    &result_with_value);
498   expected.value = ASCIIToUTF16("2011-12");
499   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
500 }
501
502 // We should not extract the value for non-text and non-select fields.
503 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) {
504   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
505            "  <INPUT type=\"hidden\" id=\"hidden\" value=\"apple\"/>"
506            "  <INPUT type=\"submit\" id=\"submit\" value=\"Send\"/>"
507            "</FORM>");
508
509   WebFrame* frame = GetMainFrame();
510   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
511
512   WebElement web_element = frame->document().getElementById("hidden");
513   WebFormControlElement element = web_element.to<WebFormControlElement>();
514   FormFieldData result;
515   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
516
517   FormFieldData expected;
518   expected.max_length = 0;
519
520   expected.name = ASCIIToUTF16("hidden");
521   expected.form_control_type = "hidden";
522   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
523
524   web_element = frame->document().getElementById("submit");
525   element = web_element.to<WebFormControlElement>();
526   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
527   expected.name = ASCIIToUTF16("submit");
528   expected.form_control_type = "submit";
529   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
530 }
531
532 // We should be able to extract password fields.
533 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) {
534   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
535            "  <INPUT type=\"password\" id=\"password\" value=\"secret\"/>"
536            "</FORM>");
537
538   WebFrame* frame = GetMainFrame();
539   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
540
541   WebElement web_element = frame->document().getElementById("password");
542   WebFormControlElement element = web_element.to<WebFormControlElement>();
543   FormFieldData result;
544   WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
545
546   FormFieldData expected;
547   expected.max_length = WebInputElement::defaultMaxLength();
548   expected.name = ASCIIToUTF16("password");
549   expected.form_control_type = "password";
550   expected.value = ASCIIToUTF16("secret");
551   EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
552 }
553
554 // We should be able to extract the autocompletetype attribute.
555 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) {
556   std::string html =
557       "<INPUT type=\"text\" id=\"absent\"/>"
558       "<INPUT type=\"text\" id=\"empty\" autocomplete=\"\"/>"
559       "<INPUT type=\"text\" id=\"off\" autocomplete=\"off\"/>"
560       "<INPUT type=\"text\" id=\"regular\" autocomplete=\"email\"/>"
561       "<INPUT type=\"text\" id=\"multi-valued\" "
562       "       autocomplete=\"billing email\"/>"
563       "<INPUT type=\"text\" id=\"experimental\" x-autocompletetype=\"email\"/>"
564       "<INPUT type=\"month\" id=\"month\" autocomplete=\"cc-exp\"/>"
565       "<SELECT id=\"select\" autocomplete=\"state\"/>"
566       "  <OPTION value=\"CA\">California</OPTION>"
567       "  <OPTION value=\"TX\">Texas</OPTION>"
568       "</SELECT>"
569       "<TEXTAREA id=\"textarea\" autocomplete=\"street-address\">"
570       "  Some multi-"
571       "  lined value"
572       "</TEXTAREA>";
573   html +=
574       "<INPUT type=\"text\" id=\"malicious\" autocomplete=\"" +
575       std::string(10000, 'x') + "\"/>";
576   LoadHTML(html.c_str());
577
578   WebFrame* frame = GetMainFrame();
579   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
580
581   struct TestCase {
582     const std::string element_id;
583     const std::string form_control_type;
584     const std::string autocomplete_attribute;
585   };
586   TestCase test_cases[] = {
587     // An absent attribute is equivalent to an empty one.
588     { "absent", "text", "" },
589     // Make sure there are no issues parsing an empty attribute.
590     { "empty", "text", "" },
591     // Make sure there are no issues parsing an attribute value that isn't a
592     // type hint.
593     { "off", "text", "off" },
594     // Common case: exactly one type specified.
595     { "regular", "text", "email" },
596     // Verify that we correctly extract multiple tokens as well.
597     { "multi-valued", "text", "billing email" },
598     // Verify that <input type="month"> fields are supported.
599     { "month", "month", "cc-exp" },
600     // We previously extracted this data from the experimental
601     // 'x-autocompletetype' attribute.  Now that the field type hints are part
602     // of the spec under the autocomplete attribute, we no longer support the
603     // experimental version.
604     { "experimental", "text", "" },
605     // <select> elements should behave no differently from text fields here.
606     { "select", "select-one", "state" },
607     // <textarea> elements should also behave no differently from text fields.
608     { "textarea", "textarea", "street-address" },
609     // Very long attribute values should be replaced by a default string, to
610     // prevent malicious websites from DOSing the browser process.
611     { "malicious", "text", "x-max-data-length-exceeded" },
612   };
613
614   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
615     WebElement web_element = frame->document().getElementById(
616         ASCIIToUTF16(test_cases[i].element_id));
617     WebFormControlElement element = web_element.to<WebFormControlElement>();
618     FormFieldData result;
619     WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result);
620
621     FormFieldData expected;
622     expected.name = ASCIIToUTF16(test_cases[i].element_id);
623     expected.form_control_type = test_cases[i].form_control_type;
624     expected.autocomplete_attribute = test_cases[i].autocomplete_attribute;
625     if (test_cases[i].form_control_type == "text")
626       expected.max_length = WebInputElement::defaultMaxLength();
627     else
628       expected.max_length = 0;
629
630     SCOPED_TRACE(test_cases[i].element_id);
631     EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
632   }
633 }
634
635 TEST_F(FormAutofillTest, WebFormElementToFormData) {
636   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
637            " <LABEL for=\"firstname\">First name:</LABEL>"
638            "  <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
639            " <LABEL for=\"lastname\">Last name:</LABEL>"
640            "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
641            " <LABEL for=\"street-address\">Address:</LABEL>"
642            "  <TEXTAREA id=\"street-address\">"
643                "123 Fantasy Ln.&#10;"
644                "Apt. 42"
645              "</TEXTAREA>"
646            " <LABEL for=\"state\">State:</LABEL>"
647            "  <SELECT id=\"state\"/>"
648            "    <OPTION value=\"CA\">California</OPTION>"
649            "    <OPTION value=\"TX\">Texas</OPTION>"
650            "  </SELECT>"
651            " <LABEL for=\"password\">Password:</LABEL>"
652            "  <INPUT type=\"password\" id=\"password\" value=\"secret\"/>"
653            " <LABEL for=\"month\">Card expiration:</LABEL>"
654            "  <INPUT type=\"month\" id=\"month\" value=\"2011-12\"/>"
655            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
656            // The below inputs should be ignored
657            " <LABEL for=\"notvisible\">Hidden:</LABEL>"
658            "  <INPUT type=\"hidden\" id=\"notvisible\" value=\"apple\"/>"
659            "</FORM>");
660
661   WebFrame* frame = GetMainFrame();
662   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
663
664   WebVector<WebFormElement> forms;
665   frame->document().forms(forms);
666   ASSERT_EQ(1U, forms.size());
667
668   WebElement element = frame->document().getElementById("firstname");
669   WebInputElement input_element = element.to<WebInputElement>();
670
671   FormData form;
672   FormFieldData field;
673   EXPECT_TRUE(WebFormElementToFormData(forms[0],
674                                        input_element,
675                                        autofill::REQUIRE_NONE,
676                                        autofill::EXTRACT_VALUE,
677                                        &form,
678                                        &field));
679   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
680   EXPECT_EQ(GURL(frame->document().url()), form.origin);
681   EXPECT_EQ(GURL("http://cnn.com"), form.action);
682
683   const std::vector<FormFieldData>& fields = form.fields;
684   ASSERT_EQ(6U, fields.size());
685
686   FormFieldData expected;
687   expected.name = ASCIIToUTF16("firstname");
688   expected.value = ASCIIToUTF16("John");
689   expected.label = ASCIIToUTF16("First name:");
690   expected.form_control_type = "text";
691   expected.max_length = WebInputElement::defaultMaxLength();
692   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
693
694   expected.name = ASCIIToUTF16("lastname");
695   expected.value = ASCIIToUTF16("Smith");
696   expected.label = ASCIIToUTF16("Last name:");
697   expected.form_control_type = "text";
698   expected.max_length = WebInputElement::defaultMaxLength();
699   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
700
701   expected.name = ASCIIToUTF16("street-address");
702   expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
703   expected.label = ASCIIToUTF16("Address:");
704   expected.form_control_type = "textarea";
705   expected.max_length = 0;
706   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
707
708   expected.name = ASCIIToUTF16("state");
709   expected.value = ASCIIToUTF16("CA");
710   expected.label = ASCIIToUTF16("State:");
711   expected.form_control_type = "select-one";
712   expected.max_length = 0;
713   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
714
715   expected.name = ASCIIToUTF16("password");
716   expected.value = ASCIIToUTF16("secret");
717   expected.label = ASCIIToUTF16("Password:");
718   expected.form_control_type = "password";
719   expected.max_length = WebInputElement::defaultMaxLength();
720   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
721
722   expected.name = ASCIIToUTF16("month");
723   expected.value = ASCIIToUTF16("2011-12");
724   expected.label = ASCIIToUTF16("Card expiration:");
725   expected.form_control_type = "month";
726   expected.max_length = 0;
727   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
728 }
729
730 // We should not be able to serialize a form with too many fillable fields.
731 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) {
732   std::string html =
733       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">";
734   for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) {
735     html += "<INPUT type=\"text\"/>";
736   }
737   html += "</FORM>";
738   LoadHTML(html.c_str());
739
740   WebFrame* frame = GetMainFrame();
741   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
742
743   WebVector<WebFormElement> forms;
744   frame->document().forms(forms);
745   ASSERT_EQ(1U, forms.size());
746
747   WebElement element = frame->document().getElementById("firstname");
748   WebInputElement input_element = element.to<WebInputElement>();
749
750   FormData form;
751   FormFieldData field;
752   EXPECT_FALSE(WebFormElementToFormData(forms[0],
753                                         input_element,
754                                         autofill::REQUIRE_NONE,
755                                         autofill::EXTRACT_VALUE,
756                                         &form,
757                                         &field));
758 }
759
760 TEST_F(FormAutofillTest, ExtractForms) {
761   ExpectJohnSmithLabels(
762       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
763       "  First name: <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
764       "  Last name: <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
765       "  Email: <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
766       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
767       "</FORM>");
768 }
769
770 TEST_F(FormAutofillTest, ExtractMultipleForms) {
771   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
772            "  <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
773            "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
774            "  <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
775            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
776            "</FORM>"
777            "<FORM name=\"TestForm2\" action=\"http://zoo.com\" method=\"post\">"
778            "  <INPUT type=\"text\" id=\"firstname\" value=\"Jack\"/>"
779            "  <INPUT type=\"text\" id=\"lastname\" value=\"Adams\"/>"
780            "  <INPUT type=\"text\" id=\"email\" value=\"jack@example.com\"/>"
781            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
782            "</FORM>");
783
784   WebFrame* web_frame = GetMainFrame();
785   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
786
787   FormCache form_cache;
788   std::vector<FormData> forms;
789   form_cache.ExtractForms(*web_frame, &forms);
790   ASSERT_EQ(2U, forms.size());
791
792   // First form.
793   const FormData& form = forms[0];
794   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
795   EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
796   EXPECT_EQ(GURL("http://cnn.com"), form.action);
797
798   const std::vector<FormFieldData>& fields = form.fields;
799   ASSERT_EQ(3U, fields.size());
800
801   FormFieldData expected;
802   expected.form_control_type = "text";
803   expected.max_length = WebInputElement::defaultMaxLength();
804
805   expected.name = ASCIIToUTF16("firstname");
806   expected.value = ASCIIToUTF16("John");
807   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
808
809   expected.name = ASCIIToUTF16("lastname");
810   expected.value = ASCIIToUTF16("Smith");
811   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
812
813   expected.name = ASCIIToUTF16("email");
814   expected.value = ASCIIToUTF16("john@example.com");
815   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
816
817   // Second form.
818   const FormData& form2 = forms[1];
819   EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2.name);
820   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
821   EXPECT_EQ(GURL("http://zoo.com"), form2.action);
822
823   const std::vector<FormFieldData>& fields2 = form2.fields;
824   ASSERT_EQ(3U, fields2.size());
825
826   expected.name = ASCIIToUTF16("firstname");
827   expected.value = ASCIIToUTF16("Jack");
828   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
829
830   expected.name = ASCIIToUTF16("lastname");
831   expected.value = ASCIIToUTF16("Adams");
832   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
833
834   expected.name = ASCIIToUTF16("email");
835   expected.value = ASCIIToUTF16("jack@example.com");
836   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
837 }
838
839 // We should not extract a form if it has too few fillable fields.
840 TEST_F(FormAutofillTest, ExtractFormsTooFewFields) {
841   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
842            "  <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
843            "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
844            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
845            "</FORM>");
846
847   WebFrame* web_frame = GetMainFrame();
848   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
849
850   FormCache form_cache;
851   std::vector<FormData> forms;
852   form_cache.ExtractForms(*web_frame, &forms);
853   EXPECT_EQ(0U, forms.size());
854 }
855
856 // We should not report additional forms for empty forms.
857 TEST_F(FormAutofillTest, ExtractFormsSkippedForms) {
858   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
859            "  <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
860            "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
861            "</FORM>");
862
863   WebFrame* web_frame = GetMainFrame();
864   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
865
866   FormCache form_cache;
867   std::vector<FormData> forms;
868   bool has_skipped_forms = form_cache.ExtractFormsAndFormElements(*web_frame,
869                                                                3,
870                                                                &forms,
871                                                                NULL);
872   EXPECT_EQ(0U, forms.size());
873   EXPECT_TRUE(has_skipped_forms);
874 }
875
876 // We should not report additional forms for empty forms.
877 TEST_F(FormAutofillTest, ExtractFormsNoFields) {
878   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
879            "</FORM>");
880
881   WebFrame* web_frame = GetMainFrame();
882   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
883
884   FormCache form_cache;
885   std::vector<FormData> forms;
886   bool has_skipped_forms = form_cache.ExtractFormsAndFormElements(*web_frame,
887                                                                3,
888                                                                &forms,
889                                                                NULL);
890   EXPECT_EQ(0U, forms.size());
891   EXPECT_FALSE(has_skipped_forms);
892 }
893
894 // We should not extract a form if it has too few fillable fields.
895 // Make sure radio and checkbox fields don't count.
896 TEST_F(FormAutofillTest, ExtractFormsTooFewFieldsSkipsCheckable) {
897   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
898            "  <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
899            "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
900            "  <INPUT type=\"radio\" id=\"a_radio\" value=\"0\"/>"
901            "  <INPUT type=\"checkbox\" id=\"a_check\" value=\"1\"/>"
902            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
903            "</FORM>");
904
905   WebFrame* web_frame = GetMainFrame();
906   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
907
908   FormCache form_cache;
909   std::vector<FormData> forms;
910   form_cache.ExtractForms(*web_frame, &forms);
911   EXPECT_EQ(0U, forms.size());
912 }
913
914 TEST_F(FormAutofillTest, WebFormElementToFormDataAutocomplete) {
915   {
916     // Form is not auto-completable due to autocomplete=off.
917     LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\""
918              " autocomplete=off>"
919              "  <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
920              "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
921              "  <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
922              "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
923              "</FORM>");
924
925     WebFrame* web_frame = GetMainFrame();
926     ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
927
928     WebVector<WebFormElement> web_forms;
929     web_frame->document().forms(web_forms);
930     ASSERT_EQ(1U, web_forms.size());
931     WebFormElement web_form = web_forms[0];
932
933     FormData form;
934     EXPECT_TRUE(WebFormElementToFormData(
935         web_form, WebFormControlElement(), autofill::REQUIRE_NONE,
936         autofill::EXTRACT_NONE, &form, NULL));
937     EXPECT_FALSE(WebFormElementToFormData(
938         web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
939         autofill::EXTRACT_NONE, &form, NULL));
940   }
941
942   {
943     // The firstname element is not auto-completable due to autocomplete=off.
944     LoadHTML("<FORM name=\"TestForm\" action=\"http://abc.com\" "
945              "      method=\"post\">"
946              "  <INPUT type=\"text\" id=\"firstname\" value=\"John\""
947              "   autocomplete=off>"
948              "  <INPUT type=\"text\" id=\"middlename\" value=\"Jack\"/>"
949              "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
950              "  <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
951              "  <INPUT type=\"submit\" name=\"reply\" value=\"Send\"/>"
952              "</FORM>");
953
954     WebFrame* web_frame = GetMainFrame();
955     ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
956
957     WebVector<WebFormElement> web_forms;
958     web_frame->document().forms(web_forms);
959     ASSERT_EQ(1U, web_forms.size());
960     WebFormElement web_form = web_forms[0];
961
962     FormData form;
963     EXPECT_TRUE(WebFormElementToFormData(
964         web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
965         autofill::EXTRACT_VALUE, &form, NULL));
966
967     EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
968     EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
969     EXPECT_EQ(GURL("http://abc.com"), form.action);
970
971     const std::vector<FormFieldData>& fields = form.fields;
972     ASSERT_EQ(3U, fields.size());
973
974     FormFieldData expected;
975     expected.form_control_type = "text";
976     expected.max_length = WebInputElement::defaultMaxLength();
977
978     expected.name = ASCIIToUTF16("middlename");
979     expected.value = ASCIIToUTF16("Jack");
980     EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
981
982     expected.name = ASCIIToUTF16("lastname");
983     expected.value = ASCIIToUTF16("Smith");
984     EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
985
986     expected.name = ASCIIToUTF16("email");
987     expected.value = ASCIIToUTF16("john@example.com");
988     EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
989   }
990 }
991
992 TEST_F(FormAutofillTest, FindForm) {
993   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
994            "  <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
995            "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
996            "  <INPUT type=\"text\" id=\"email\" value=\"john@example.com\""
997                      "autocomplete=\"off\" />"
998            "  <INPUT type=\"text\" id=\"phone\" value=\"1.800.555.1234\"/>"
999            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1000            "</FORM>");
1001
1002   WebFrame* web_frame = GetMainFrame();
1003   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1004
1005   FormCache form_cache;
1006   std::vector<FormData> forms;
1007   form_cache.ExtractForms(*web_frame, &forms);
1008   ASSERT_EQ(1U, forms.size());
1009
1010   // Get the input element we want to find.
1011   WebElement element = web_frame->document().getElementById("firstname");
1012   WebInputElement input_element = element.to<WebInputElement>();
1013
1014   // Find the form and verify it's the correct form.
1015   FormData form;
1016   FormFieldData field;
1017   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
1018                                               autofill::REQUIRE_NONE));
1019   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1020   EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1021   EXPECT_EQ(GURL("http://buh.com"), form.action);
1022
1023   const std::vector<FormFieldData>& fields = form.fields;
1024   ASSERT_EQ(4U, fields.size());
1025
1026   FormFieldData expected;
1027   expected.form_control_type = "text";
1028   expected.max_length = WebInputElement::defaultMaxLength();
1029
1030   expected.name = ASCIIToUTF16("firstname");
1031   expected.value = ASCIIToUTF16("John");
1032   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1033   EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1034
1035   expected.name = ASCIIToUTF16("lastname");
1036   expected.value = ASCIIToUTF16("Smith");
1037   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1038
1039   expected.name = ASCIIToUTF16("email");
1040   expected.value = ASCIIToUTF16("john@example.com");
1041   expected.autocomplete_attribute = "off";
1042   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1043   expected.autocomplete_attribute = std::string();  // reset
1044
1045   expected.name = ASCIIToUTF16("phone");
1046   expected.value = ASCIIToUTF16("1.800.555.1234");
1047   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1048
1049   // Try again, but require autocomplete.
1050   FormData form2;
1051   FormFieldData field2;
1052   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
1053                                               autofill::REQUIRE_AUTOCOMPLETE));
1054   EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1055   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1056   EXPECT_EQ(GURL("http://buh.com"), form2.action);
1057
1058   const std::vector<FormFieldData>& fields2 = form2.fields;
1059   ASSERT_EQ(3U, fields2.size());
1060
1061   expected.form_control_type = "text";
1062   expected.max_length = WebInputElement::defaultMaxLength();
1063
1064   expected.name = ASCIIToUTF16("firstname");
1065   expected.value = ASCIIToUTF16("John");
1066   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1067   EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1068
1069   expected.name = ASCIIToUTF16("lastname");
1070   expected.value = ASCIIToUTF16("Smith");
1071   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1072
1073   expected.name = ASCIIToUTF16("phone");
1074   expected.value = ASCIIToUTF16("1.800.555.1234");
1075   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1076 }
1077
1078 // Test regular FillForm function.
1079 TEST_F(FormAutofillTest, FillForm) {
1080   static const AutofillFieldCase field_cases[] = {
1081       // fields: form_control_type, name, initial_value, autocomplete_attribute,
1082       //         should_be_autofilled, autofill_value, expected_value
1083
1084       // Regular empty fields (firstname & lastname) should be autofilled.
1085       {"text", "firstname", "", "", true, "filled firstname",
1086        "filled firstname"},
1087       {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1088       // hidden fields should not be extracted to form_data.
1089       // Non empty fields should not be autofilled.
1090       {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
1091       // "noautocomplete" should not be extracted to form_data.
1092       // Disabled fields should not be autofilled.
1093       {"text", "notenabled", "", "", false, "filled notenabled", ""},
1094       // Readonly fields should not be autofilled.
1095       {"text", "readonly", "", "", false, "filled readonly", ""},
1096       // Fields with "visibility: hidden" should not be autofilled.
1097       {"text", "invisible", "", "", false, "filled invisible", ""},
1098       // Fields with "display:none" should not be autofilled.
1099       {"text", "displaynone", "", "", false, "filled displaynone", ""},
1100       // Regular <input type="month"> should be autofilled.
1101       {"month", "month", "", "", true, "2017-11", "2017-11"},
1102       // Non-empty <input type="month"> should not be autofilled.
1103       {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
1104       // Regular select fields should be autofilled.
1105       {"select-one", "select", "", "", true, "TX", "TX"},
1106       // Select fields should be autofilled even if they already have a
1107       // non-empty value.
1108       {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1109       // Regular textarea elements should be autofilled.
1110       {"textarea", "textarea", "", "", true, "some multi-\nline value",
1111        "some multi-\nline value"},
1112       // Non-empty textarea elements should not be autofilled.
1113       {"textarea", "textarea-nonempty", "Go\naway!", "", false,
1114        "some multi-\nline value", "Go\naway!"},
1115   };
1116   TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1117                         FillForm, &WebInputElement::value);
1118   // Verify preview selection.
1119   WebInputElement firstname = GetMainFrame()->document().
1120       getElementById("firstname").to<WebInputElement>();
1121   EXPECT_EQ(16, firstname.selectionStart());
1122   EXPECT_EQ(16, firstname.selectionEnd());
1123 }
1124
1125 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) {
1126   static const AutofillFieldCase field_cases[] = {
1127       // fields: form_control_type, name, initial_value, autocomplete_attribute,
1128       //         should_be_autofilled, autofill_value, expected_value
1129
1130       // Regular empty fields (firstname & lastname) should be autofilled.
1131       {"text", "firstname", "", "", true, "filled firstname",
1132        "filled firstname"},
1133       {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1134       // hidden fields should not be extracted to form_data.
1135       // Non empty fields should be overriden.
1136       {"text", "notempty", "Hi", "", true, "filled notempty",
1137        "filled notempty"},
1138       // "noautocomplete" should not be extracted to form_data.
1139       // Disabled fields should not be autofilled.
1140       {"text", "notenabled", "", "", false, "filled notenabled", ""},
1141       // Readonly fields should not be autofilled.
1142       {"text", "readonly", "", "", false, "filled readonly", ""},
1143       // Fields with "visibility: hidden" should also be autofilled.
1144       {"text", "invisible", "", "", true, "filled invisible",
1145        "filled invisible"},
1146       // Fields with "display:none" should also be autofilled.
1147       {"text", "displaynone", "", "", true, "filled displaynone",
1148        "filled displaynone"},
1149       // Regular <input type="month"> should be autofilled.
1150       {"month", "month", "", "", true, "2017-11", "2017-11"},
1151       // Non-empty <input type="month"> should be overridden.
1152       {"month", "month-nonempty", "2011-12", "", true, "2017-11", "2017-11"},
1153       // Regular select fields should be autofilled.
1154       {"select-one", "select", "", "", true, "TX", "TX"},
1155       // Select fields should be autofilled even if they already have a
1156       // non-empty value.
1157       {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1158       // Regular textarea elements should be autofilled.
1159       {"textarea", "textarea", "", "", true, "some multi-\nline value",
1160        "some multi-\nline value"},
1161       // Nonempty textarea elements should be overridden.
1162       {"textarea", "textarea-nonempty", "Go\naway!", "", true,
1163        "some multi-\nline value", "some multi-\nline value"},
1164   };
1165   TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1166                         &FillFormIncludingNonFocusableElementsWrapper,
1167                         &WebInputElement::value);
1168 }
1169
1170 TEST_F(FormAutofillTest, PreviewForm) {
1171   static const char* html =
1172       "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
1173       "  <INPUT type=\"text\" id=\"firstname\"/>"
1174       "  <INPUT type=\"text\" id=\"lastname\"/>"
1175       "  <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>"
1176       "  <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>"
1177       "  <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>"
1178       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1179       "</FORM>";
1180
1181   static const AutofillFieldCase field_cases[] = {
1182       // Normal empty fields should be previewed.
1183       {"text", "firstname", "", "", true, "suggested firstname",
1184        "suggested firstname"},
1185       {"text", "lastname", "", "", true, "suggested lastname",
1186        "suggested lastname"},
1187       // Non empty fields should not be previewed.
1188       {"text", "notempty", "Hi", "", false, "filled notempty", ""},
1189       // "noautocomplete" should not be extracted to form_data.
1190       // Disabled fields should not be previewed.
1191       {"text", "notenabled", "", "", false, "filled notenabled", ""},
1192   };
1193   TestFormFillFunctions(html, field_cases, arraysize(field_cases), &PreviewForm,
1194                         &WebInputElement::suggestedValue);
1195
1196   // Verify preview selection.
1197   WebInputElement firstname = GetMainFrame()->document().
1198       getElementById("firstname").to<WebInputElement>();
1199   EXPECT_EQ(0, firstname.selectionStart());
1200   EXPECT_EQ(19, firstname.selectionEnd());
1201 }
1202
1203 TEST_F(FormAutofillTest, Labels) {
1204   ExpectJohnSmithLabels(
1205       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1206       "  <LABEL for=\"firstname\"> First name: </LABEL>"
1207       "    <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1208       "  <LABEL for=\"lastname\"> Last name: </LABEL>"
1209       "    <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1210       "  <LABEL for=\"email\"> Email: </LABEL>"
1211       "    <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1212       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1213       "</FORM>");
1214 }
1215
1216 TEST_F(FormAutofillTest, LabelsWithSpans) {
1217   ExpectJohnSmithLabels(
1218       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1219       "  <LABEL for=\"firstname\"><span>First name: </span></LABEL>"
1220       "    <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1221       "  <LABEL for=\"lastname\"><span>Last name: </span></LABEL>"
1222       "    <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1223       "  <LABEL for=\"email\"><span>Email: </span></LABEL>"
1224       "    <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1225       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1226       "</FORM>");
1227 }
1228
1229 // This test is different from FormAutofillTest.Labels in that the label
1230 // elements for= attribute is set to the name of the form control element it is
1231 // a label for instead of the id of the form control element.  This is invalid
1232 // because the for= attribute must be set to the id of the form control element;
1233 // however, current label parsing code will extract the text from the previous
1234 // label element and apply it to the following input field.
1235 TEST_F(FormAutofillTest, InvalidLabels) {
1236   ExpectJohnSmithLabels(
1237       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1238       "  <LABEL for=\"firstname\"> First name: </LABEL>"
1239       "    <INPUT type=\"text\" name=\"firstname\" value=\"John\"/>"
1240       "  <LABEL for=\"lastname\"> Last name: </LABEL>"
1241       "    <INPUT type=\"text\" name=\"lastname\" value=\"Smith\"/>"
1242       "  <LABEL for=\"email\"> Email: </LABEL>"
1243       "    <INPUT type=\"text\" name=\"email\" value=\"john@example.com\"/>"
1244       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1245       "</FORM>");
1246 }
1247
1248 // This test has three form control elements, only one of which has a label
1249 // element associated with it.
1250 TEST_F(FormAutofillTest, OneLabelElement) {
1251   ExpectJohnSmithLabels(
1252            "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1253            "  First name:"
1254            "    <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1255            "  <LABEL for=\"lastname\">Last name: </LABEL>"
1256            "    <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1257            "  Email:"
1258            "    <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1259            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1260            "</FORM>");
1261 }
1262
1263 TEST_F(FormAutofillTest, LabelsInferredFromText) {
1264   ExpectJohnSmithLabels(
1265       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1266       "  First name:"
1267       "    <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1268       "  Last name:"
1269       "    <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1270       "  Email:"
1271       "    <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1272       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1273       "</FORM>");
1274 }
1275
1276 TEST_F(FormAutofillTest, LabelsInferredFromParagraph) {
1277   ExpectJohnSmithLabels(
1278       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1279       "  <P>First name:</P><INPUT type=\"text\" "
1280       "                           id=\"firstname\" value=\"John\"/>"
1281       "  <P>Last name:</P>"
1282       "    <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1283       "  <P>Email:</P>"
1284       "    <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1285       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1286       "</FORM>");
1287 }
1288
1289 TEST_F(FormAutofillTest, LabelsInferredFromBold) {
1290   ExpectJohnSmithLabels(
1291       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1292       "  <B>First name:</B><INPUT type=\"text\" "
1293       "                           id=\"firstname\" value=\"John\"/>"
1294       "  <B>Last name:</B>"
1295       "    <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1296       "  <B>Email:</B>"
1297       "    <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1298       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1299       "</FORM>");
1300 }
1301
1302 TEST_F(FormAutofillTest, LabelsInferredPriorToImgOrBr) {
1303   ExpectJohnSmithLabels(
1304       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1305       "  First name:<IMG/><INPUT type=\"text\" "
1306       "                          id=\"firstname\" value=\"John\"/>"
1307       "  Last name:<IMG/>"
1308       "    <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1309       "  Email:<BR/>"
1310       "    <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1311       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1312       "</FORM>");
1313 }
1314
1315 TEST_F(FormAutofillTest, LabelsInferredFromTableCell) {
1316   ExpectJohnSmithLabels(
1317       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1318       "<TABLE>"
1319       "  <TR>"
1320       "    <TD>First name:</TD>"
1321       "    <TD><INPUT type=\"text\" id=\"firstname\" value=\"John\"/></TD>"
1322       "  </TR>"
1323       "  <TR>"
1324       "    <TD>Last name:</TD>"
1325       "    <TD><INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/></TD>"
1326       "  </TR>"
1327       "  <TR>"
1328       "    <TD>Email:</TD>"
1329       "    <TD><INPUT type=\"text\" id=\"email\""
1330       "               value=\"john@example.com\"/></TD>"
1331       "  </TR>"
1332       "  <TR>"
1333       "    <TD></TD>"
1334       "    <TD>"
1335       "      <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1336       "    </TD>"
1337       "  </TR>"
1338       "</TABLE>"
1339       "</FORM>");
1340 }
1341
1342 TEST_F(FormAutofillTest, LabelsInferredFromTableCellTH) {
1343   ExpectJohnSmithLabels(
1344       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1345       "<TABLE>"
1346       "  <TR>"
1347       "    <TH>First name:</TH>"
1348       "    <TD><INPUT type=\"text\" id=\"firstname\" value=\"John\"/></TD>"
1349       "  </TR>"
1350       "  <TR>"
1351       "    <TH>Last name:</TH>"
1352       "    <TD><INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/></TD>"
1353       "  </TR>"
1354       "  <TR>"
1355       "    <TH>Email:</TH>"
1356       "    <TD><INPUT type=\"text\" id=\"email\""
1357       "               value=\"john@example.com\"/></TD>"
1358       "  </TR>"
1359       "  <TR>"
1360       "    <TD></TD>"
1361       "    <TD>"
1362       "      <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1363       "    </TD>"
1364       "  </TR>"
1365       "</TABLE>"
1366       "</FORM>");
1367 }
1368
1369 TEST_F(FormAutofillTest, LabelsInferredFromTableCellNested) {
1370   std::vector<string16> labels, names, values;
1371
1372   labels.push_back(ASCIIToUTF16("First name: Bogus"));
1373   names.push_back(ASCIIToUTF16("firstname"));
1374   values.push_back(ASCIIToUTF16("John"));
1375
1376   labels.push_back(ASCIIToUTF16("Last name:"));
1377   names.push_back(ASCIIToUTF16("lastname"));
1378   values.push_back(ASCIIToUTF16("Smith"));
1379
1380   labels.push_back(ASCIIToUTF16("Email:"));
1381   names.push_back(ASCIIToUTF16("email"));
1382   values.push_back(ASCIIToUTF16("john@example.com"));
1383
1384   ExpectLabels(
1385       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1386       "<TABLE>"
1387       "  <TR>"
1388       "    <TD>"
1389       "      <FONT>"
1390       "        First name:"
1391       "      </FONT>"
1392       "      <FONT>"
1393       "        Bogus"
1394       "      </FONT>"
1395       "    </TD>"
1396       "    <TD>"
1397       "      <FONT>"
1398       "        <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1399       "      </FONT>"
1400       "    </TD>"
1401       "  </TR>"
1402       "  <TR>"
1403       "    <TD>"
1404       "      <FONT>"
1405       "        Last name:"
1406       "      </FONT>"
1407       "    </TD>"
1408       "    <TD>"
1409       "      <FONT>"
1410       "        <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1411       "      </FONT>"
1412       "    </TD>"
1413       "  </TR>"
1414       "  <TR>"
1415       "    <TD>"
1416       "      <FONT>"
1417       "        Email:"
1418       "      </FONT>"
1419       "    </TD>"
1420       "    <TD>"
1421       "      <FONT>"
1422       "        <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1423       "      </FONT>"
1424       "    </TD>"
1425       "  </TR>"
1426       "  <TR>"
1427       "    <TD></TD>"
1428       "    <TD>"
1429       "      <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1430       "    </TD>"
1431       "  </TR>"
1432       "</TABLE>"
1433       "</FORM>",
1434       labels, names, values);
1435 }
1436
1437 TEST_F(FormAutofillTest, LabelsInferredFromTableEmptyTDs) {
1438   std::vector<string16> labels, names, values;
1439
1440   labels.push_back(ASCIIToUTF16("* First Name"));
1441   names.push_back(ASCIIToUTF16("firstname"));
1442   values.push_back(ASCIIToUTF16("John"));
1443
1444   labels.push_back(ASCIIToUTF16("* Last Name"));
1445   names.push_back(ASCIIToUTF16("lastname"));
1446   values.push_back(ASCIIToUTF16("Smith"));
1447
1448   labels.push_back(ASCIIToUTF16("* Email"));
1449   names.push_back(ASCIIToUTF16("email"));
1450   values.push_back(ASCIIToUTF16("john@example.com"));
1451
1452   ExpectLabels(
1453       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1454       "<TABLE>"
1455       "  <TR>"
1456       "    <TD>"
1457       "      <SPAN>*</SPAN>"
1458       "      <B>First Name</B>"
1459       "    </TD>"
1460       "    <TD></TD>"
1461       "    <TD>"
1462       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1463       "    </TD>"
1464       "  </TR>"
1465       "  <TR>"
1466       "    <TD>"
1467       "      <SPAN>*</SPAN>"
1468       "      <B>Last Name</B>"
1469       "    </TD>"
1470       "    <TD></TD>"
1471       "    <TD>"
1472       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1473       "    </TD>"
1474       "  </TR>"
1475       "  <TR>"
1476       "    <TD>"
1477       "      <SPAN>*</SPAN>"
1478       "      <B>Email</B>"
1479       "    </TD>"
1480       "    <TD></TD>"
1481       "    <TD>"
1482       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1483       "    </TD>"
1484       "  </TR>"
1485       "  <TR>"
1486       "    <TD></TD>"
1487       "    <TD>"
1488       "      <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1489       "    </TD>"
1490       "  </TR>"
1491       "</TABLE>"
1492       "</FORM>",
1493       labels, names, values);
1494 }
1495
1496 TEST_F(FormAutofillTest, LabelsInferredFromPreviousTD) {
1497   std::vector<string16> labels, names, values;
1498
1499   labels.push_back(ASCIIToUTF16("* First Name"));
1500   names.push_back(ASCIIToUTF16("firstname"));
1501   values.push_back(ASCIIToUTF16("John"));
1502
1503   labels.push_back(ASCIIToUTF16("* Last Name"));
1504   names.push_back(ASCIIToUTF16("lastname"));
1505   values.push_back(ASCIIToUTF16("Smith"));
1506
1507   labels.push_back(ASCIIToUTF16("* Email"));
1508   names.push_back(ASCIIToUTF16("email"));
1509   values.push_back(ASCIIToUTF16("john@example.com"));
1510
1511   ExpectLabels(
1512       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1513       "<TABLE>"
1514       "  <TR>"
1515       "    <TD>* First Name</TD>"
1516       "    <TD>"
1517       "      Bogus"
1518       "      <INPUT type=\"hidden\"/>"
1519       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1520       "    </TD>"
1521       "  </TR>"
1522       "  <TR>"
1523       "    <TD>* Last Name</TD>"
1524       "    <TD>"
1525       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1526       "    </TD>"
1527       "  </TR>"
1528       "  <TR>"
1529       "    <TD>* Email</TD>"
1530       "    <TD>"
1531       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1532       "    </TD>"
1533       "  </TR>"
1534       "  <TR>"
1535       "    <TD></TD>"
1536       "    <TD>"
1537       "      <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1538       "    </TD>"
1539       "  </TR>"
1540       "</TABLE>"
1541       "</FORM>",
1542       labels, names, values);
1543 }
1544
1545 // <script>, <noscript> and <option> tags are excluded when the labels are
1546 // inferred.
1547 // Also <!-- comment --> is excluded.
1548 TEST_F(FormAutofillTest, LabelsInferredFromTableWithSpecialElements) {
1549   std::vector<string16> labels, names, values;
1550   std::vector<std::string> control_types;
1551
1552   labels.push_back(ASCIIToUTF16("* First Name"));
1553   names.push_back(ASCIIToUTF16("firstname"));
1554   values.push_back(ASCIIToUTF16("John"));
1555   control_types.push_back("text");
1556
1557   labels.push_back(ASCIIToUTF16("* Middle Name"));
1558   names.push_back(ASCIIToUTF16("middlename"));
1559   values.push_back(ASCIIToUTF16("Joe"));
1560   control_types.push_back("text");
1561
1562   labels.push_back(ASCIIToUTF16("* Last Name"));
1563   names.push_back(ASCIIToUTF16("lastname"));
1564   values.push_back(ASCIIToUTF16("Smith"));
1565   control_types.push_back("text");
1566
1567   labels.push_back(ASCIIToUTF16("* Country"));
1568   names.push_back(ASCIIToUTF16("country"));
1569   values.push_back(ASCIIToUTF16("US"));
1570   control_types.push_back("select-one");
1571
1572   labels.push_back(ASCIIToUTF16("* Email"));
1573   names.push_back(ASCIIToUTF16("email"));
1574   values.push_back(ASCIIToUTF16("john@example.com"));
1575   control_types.push_back("text");
1576
1577   ExpectLabelsAndTypes(
1578       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1579       "<TABLE>"
1580       "  <TR>"
1581       "    <TD>"
1582       "      <SPAN>*</SPAN>"
1583       "      <B>First Name</B>"
1584       "    </TD>"
1585       "    <TD>"
1586       "      <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
1587       "      </SCRIPT>"
1588       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1589       "    </TD>"
1590       "  </TR>"
1591       "  <TR>"
1592       "    <TD>"
1593       "      <SPAN>*</SPAN>"
1594       "      <B>Middle Name</B>"
1595       "    </TD>"
1596       "    <TD>"
1597       "      <NOSCRIPT>"
1598       "        <P>Bad</P>"
1599       "      </NOSCRIPT>"
1600       "      <INPUT type=\"text\" id=\"middlename\" value=\"Joe\"/>"
1601       "    </TD>"
1602       "  </TR>"
1603       "  <TR>"
1604       "    <TD>"
1605       "      <SPAN>*</SPAN>"
1606       "      <B>Last Name</B>"
1607       "    </TD>"
1608       "    <TD>"
1609       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1610       "    </TD>"
1611       "  </TR>"
1612       "  <TR>"
1613       "    <TD>"
1614       "      <SPAN>*</SPAN>"
1615       "      <B>Country</B>"
1616       "    </TD>"
1617       "    <TD>"
1618       "      <SELECT id=\"country\">"
1619       "        <OPTION VALUE=\"US\">The value should be ignored as label."
1620       "        </OPTION>"
1621       "        <OPTION VALUE=\"JP\">JAPAN</OPTION>"
1622       "      </SELECT>"
1623       "    </TD>"
1624       "  </TR>"
1625       "  <TR>"
1626       "    <TD>"
1627       "      <SPAN>*</SPAN>"
1628       "      <B>Email</B>"
1629       "    </TD>"
1630       "    <TD>"
1631       "      <!-- This comment should be ignored as inferred label.-->"
1632       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1633       "    </TD>"
1634       "  </TR>"
1635       "  <TR>"
1636       "    <TD></TD>"
1637       "    <TD>"
1638       "      <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1639       "    </TD>"
1640       "  </TR>"
1641       "</TABLE>"
1642       "</FORM>",
1643       labels, names, values, control_types);
1644 }
1645
1646 TEST_F(FormAutofillTest, LabelsInferredFromTableLabels) {
1647   ExpectJohnSmithLabels(
1648       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1649       "<TABLE>"
1650       "  <TR>"
1651       "    <TD>"
1652       "      <LABEL>First name:</LABEL>"
1653       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1654       "    </TD>"
1655       "  </TR>"
1656       "  <TR>"
1657       "    <TD>"
1658       "      <LABEL>Last name:</LABEL>"
1659       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1660       "    </TD>"
1661       "  </TR>"
1662       "  <TR>"
1663       "    <TD>"
1664       "      <LABEL>Email:</LABEL>"
1665       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1666       "    </TD>"
1667       "  </TR>"
1668       "</TABLE>"
1669       "<INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1670       "</FORM>");
1671 }
1672
1673 TEST_F(FormAutofillTest, LabelsInferredFromTableTDInterveningElements) {
1674   ExpectJohnSmithLabels(
1675       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1676       "<TABLE>"
1677       "  <TR>"
1678       "    <TD>"
1679       "      First name:"
1680       "      <BR>"
1681       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1682       "    </TD>"
1683       "  </TR>"
1684       "  <TR>"
1685       "    <TD>"
1686       "      Last name:"
1687       "      <BR>"
1688       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1689       "    </TD>"
1690       "  </TR>"
1691       "  <TR>"
1692       "    <TD>"
1693       "      Email:"
1694       "      <BR>"
1695       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1696       "    </TD>"
1697       "  </TR>"
1698       "</TABLE>"
1699       "<INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1700       "</FORM>");
1701 }
1702
1703 // Verify that we correctly infer labels when the label text spans multiple
1704 // adjacent HTML elements, not separated by whitespace.
1705 TEST_F(FormAutofillTest, LabelsInferredFromTableAdjacentElements) {
1706   std::vector<string16> labels, names, values;
1707
1708   labels.push_back(ASCIIToUTF16("*First Name"));
1709   names.push_back(ASCIIToUTF16("firstname"));
1710   values.push_back(ASCIIToUTF16("John"));
1711
1712   labels.push_back(ASCIIToUTF16("*Last Name"));
1713   names.push_back(ASCIIToUTF16("lastname"));
1714   values.push_back(ASCIIToUTF16("Smith"));
1715
1716   labels.push_back(ASCIIToUTF16("*Email"));
1717   names.push_back(ASCIIToUTF16("email"));
1718   values.push_back(ASCIIToUTF16("john@example.com"));
1719
1720   ExpectLabels(
1721       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1722       "<TABLE>"
1723       "  <TR>"
1724       "    <TD>"
1725       "      <SPAN>*</SPAN><B>First Name</B>"
1726       "    </TD>"
1727       "    <TD>"
1728       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1729       "    </TD>"
1730       "  </TR>"
1731       "  <TR>"
1732       "    <TD>"
1733       "      <SPAN>*</SPAN><B>Last Name</B>"
1734       "    </TD>"
1735       "    <TD>"
1736       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1737       "    </TD>"
1738       "  </TR>"
1739       "  <TR>"
1740       "    <TD>"
1741       "      <SPAN>*</SPAN><B>Email</B>"
1742       "    </TD>"
1743       "    <TD>"
1744       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1745       "    </TD>"
1746       "  </TR>"
1747       "  <TR>"
1748       "    <TD>"
1749       "      <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1750       "    </TD>"
1751       "  </TR>"
1752       "</TABLE>"
1753       "</FORM>",
1754       labels, names, values);
1755 }
1756
1757 // Verify that we correctly infer labels when the label text resides in the
1758 // previous row.
1759 TEST_F(FormAutofillTest, LabelsInferredFromTableRow) {
1760   std::vector<string16> labels, names, values;
1761
1762   labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1763   names.push_back(ASCIIToUTF16("firstname"));
1764   values.push_back(ASCIIToUTF16("John"));
1765
1766   labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1767   names.push_back(ASCIIToUTF16("lastname"));
1768   values.push_back(ASCIIToUTF16("Smith"));
1769
1770   labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1771   names.push_back(ASCIIToUTF16("email"));
1772   values.push_back(ASCIIToUTF16("john@example.com"));
1773
1774   ExpectLabels(
1775       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1776       "<TABLE>"
1777       "  <TR>"
1778       "    <TD>*First Name</TD>"
1779       "    <TD>*Last Name</TD>"
1780       "    <TD>*Email</TD>"
1781       "  </TR>"
1782       "  <TR>"
1783       "    <TD>"
1784       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1785       "    </TD>"
1786       "    <TD>"
1787       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1788       "    </TD>"
1789       "    <TD>"
1790       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1791       "    </TD>"
1792       "  </TR>"
1793       "  <TR>"
1794       "    <TD>"
1795       "      <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1796       "    </TD>"
1797       "  </TR>"
1798       "</TABLE>",
1799       labels, names, values);
1800 }
1801
1802 // Verify that we correctly infer labels when enclosed within a list item.
1803 TEST_F(FormAutofillTest, LabelsInferredFromListItem) {
1804   std::vector<string16> labels, names, values;
1805
1806   labels.push_back(ASCIIToUTF16("* Home Phone"));
1807   names.push_back(ASCIIToUTF16("areacode"));
1808   values.push_back(ASCIIToUTF16("415"));
1809
1810   labels.push_back(ASCIIToUTF16("* Home Phone"));
1811   names.push_back(ASCIIToUTF16("prefix"));
1812   values.push_back(ASCIIToUTF16("555"));
1813
1814   labels.push_back(ASCIIToUTF16("* Home Phone"));
1815   names.push_back(ASCIIToUTF16("suffix"));
1816   values.push_back(ASCIIToUTF16("1212"));
1817
1818   ExpectLabels(
1819       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1820       "<DIV>"
1821       "  <LI>"
1822       "    <SPAN>Bogus</SPAN>"
1823       "  </LI>"
1824       "  <LI>"
1825       "    <LABEL><EM>*</EM> Home Phone</LABEL>"
1826       "    <INPUT type=\"text\" id=\"areacode\" value=\"415\"/>"
1827       "    <INPUT type=\"text\" id=\"prefix\" value=\"555\"/>"
1828       "    <INPUT type=\"text\" id=\"suffix\" value=\"1212\"/>"
1829       "  </LI>"
1830       "  <LI>"
1831       "    <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1832       "  </LI>"
1833       "</DIV>"
1834       "</FORM>",
1835       labels, names, values);
1836 }
1837
1838 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionList) {
1839   std::vector<string16> labels, names, values;
1840
1841   labels.push_back(ASCIIToUTF16("* First name: Bogus"));
1842   names.push_back(ASCIIToUTF16("firstname"));
1843   values.push_back(ASCIIToUTF16("John"));
1844
1845   labels.push_back(ASCIIToUTF16("Last name:"));
1846   names.push_back(ASCIIToUTF16("lastname"));
1847   values.push_back(ASCIIToUTF16("Smith"));
1848
1849   labels.push_back(ASCIIToUTF16("Email:"));
1850   names.push_back(ASCIIToUTF16("email"));
1851   values.push_back(ASCIIToUTF16("john@example.com"));
1852
1853   ExpectLabels(
1854       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1855       "<DL>"
1856       "  <DT>"
1857       "    <SPAN>"
1858       "      *"
1859       "    </SPAN>"
1860       "    <SPAN>"
1861       "      First name:"
1862       "    </SPAN>"
1863       "    <SPAN>"
1864       "      Bogus"
1865       "    </SPAN>"
1866       "  </DT>"
1867       "  <DD>"
1868       "    <FONT>"
1869       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1870       "    </FONT>"
1871       "  </DD>"
1872       "  <DT>"
1873       "    <SPAN>"
1874       "      Last name:"
1875       "    </SPAN>"
1876       "  </DT>"
1877       "  <DD>"
1878       "    <FONT>"
1879       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1880       "    </FONT>"
1881       "  </DD>"
1882       "  <DT>"
1883       "    <SPAN>"
1884       "      Email:"
1885       "    </SPAN>"
1886       "  </DT>"
1887       "  <DD>"
1888       "    <FONT>"
1889       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1890       "    </FONT>"
1891       "  </DD>"
1892       "  <DT></DT>"
1893       "  <DD>"
1894       "    <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1895       "  </DD>"
1896       "</DL>"
1897       "</FORM>",
1898       labels, names, values);
1899 }
1900
1901 TEST_F(FormAutofillTest, LabelsInferredWithSameName) {
1902   std::vector<string16> labels, names, values;
1903
1904   labels.push_back(ASCIIToUTF16("Address Line 1:"));
1905   names.push_back(ASCIIToUTF16("Address"));
1906   values.push_back(string16());
1907
1908   labels.push_back(ASCIIToUTF16("Address Line 2:"));
1909   names.push_back(ASCIIToUTF16("Address"));
1910   values.push_back(string16());
1911
1912   labels.push_back(ASCIIToUTF16("Address Line 3:"));
1913   names.push_back(ASCIIToUTF16("Address"));
1914   values.push_back(string16());
1915
1916   ExpectLabels(
1917       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1918       "  Address Line 1:"
1919       "    <INPUT type=\"text\" name=\"Address\"/>"
1920       "  Address Line 2:"
1921       "    <INPUT type=\"text\" name=\"Address\"/>"
1922       "  Address Line 3:"
1923       "    <INPUT type=\"text\" name=\"Address\"/>"
1924       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1925       "</FORM>",
1926       labels, names, values);
1927 }
1928
1929 TEST_F(FormAutofillTest, LabelsInferredWithImageTags) {
1930   std::vector<string16> labels, names, values;
1931
1932   labels.push_back(ASCIIToUTF16("Phone:"));
1933   names.push_back(ASCIIToUTF16("dayphone1"));
1934   values.push_back(string16());
1935
1936   labels.push_back(ASCIIToUTF16("-"));
1937   names.push_back(ASCIIToUTF16("dayphone2"));
1938   values.push_back(string16());
1939
1940   labels.push_back(ASCIIToUTF16("-"));
1941   names.push_back(ASCIIToUTF16("dayphone3"));
1942   values.push_back(string16());
1943
1944   labels.push_back(ASCIIToUTF16("ext.:"));
1945   names.push_back(ASCIIToUTF16("dayphone4"));
1946   values.push_back(string16());
1947
1948   labels.push_back(string16());
1949   names.push_back(ASCIIToUTF16("dummy"));
1950   values.push_back(string16());
1951
1952   ExpectLabels(
1953       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1954       "  Phone:"
1955       "  <input type=\"text\" name=\"dayphone1\">"
1956       "  <img/>"
1957       "  -"
1958       "  <img/>"
1959       "  <input type=\"text\" name=\"dayphone2\">"
1960       "  <img/>"
1961       "  -"
1962       "  <img/>"
1963       "  <input type=\"text\" name=\"dayphone3\">"
1964       "  ext.:"
1965       "  <input type=\"text\" name=\"dayphone4\">"
1966       "  <input type=\"text\" name=\"dummy\">"
1967       "  <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
1968       "</FORM>",
1969       labels, names, values);
1970 }
1971
1972 TEST_F(FormAutofillTest, LabelsInferredFromDivTable) {
1973   ExpectJohnSmithLabels(
1974       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1975       "<DIV>First name:<BR>"
1976       "  <SPAN>"
1977       "    <INPUT type=\"text\" name=\"firstname\" value=\"John\">"
1978       "  </SPAN>"
1979       "</DIV>"
1980       "<DIV>Last name:<BR>"
1981       "  <SPAN>"
1982       "    <INPUT type=\"text\" name=\"lastname\" value=\"Smith\">"
1983       "  </SPAN>"
1984       "</DIV>"
1985       "<DIV>Email:<BR>"
1986       "  <SPAN>"
1987       "    <INPUT type=\"text\" name=\"email\" value=\"john@example.com\">"
1988       "  </SPAN>"
1989       "</DIV>"
1990       "<input type=\"submit\" name=\"reply-send\" value=\"Send\">"
1991       "</FORM>");
1992 }
1993
1994 TEST_F(FormAutofillTest, LabelsInferredFromDivSiblingTable) {
1995   ExpectJohnSmithLabels(
1996       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1997       "<DIV>First name:</DIV>"
1998       "<DIV>"
1999       "  <SPAN>"
2000       "    <INPUT type=\"text\" name=\"firstname\" value=\"John\">"
2001       "  </SPAN>"
2002       "</DIV>"
2003       "<DIV>Last name:</DIV>"
2004       "<DIV>"
2005       "  <SPAN>"
2006       "    <INPUT type=\"text\" name=\"lastname\" value=\"Smith\">"
2007       "  </SPAN>"
2008       "</DIV>"
2009       "<DIV>Email:</DIV>"
2010       "<DIV>"
2011       "  <SPAN>"
2012       "    <INPUT type=\"text\" name=\"email\" value=\"john@example.com\">"
2013       "  </SPAN>"
2014       "</DIV>"
2015       "<input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2016       "</FORM>");
2017 }
2018
2019 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionListRatherThanDivTable) {
2020   ExpectJohnSmithLabels(
2021       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2022       "<DIV>This is not a label.<BR>"
2023       "<DL>"
2024       "  <DT>"
2025       "    <SPAN>"
2026       "      First name:"
2027       "    </SPAN>"
2028       "  </DT>"
2029       "  <DD>"
2030       "    <FONT>"
2031       "      <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
2032       "    </FONT>"
2033       "  </DD>"
2034       "  <DT>"
2035       "    <SPAN>"
2036       "      Last name:"
2037       "    </SPAN>"
2038       "  </DT>"
2039       "  <DD>"
2040       "    <FONT>"
2041       "      <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
2042       "    </FONT>"
2043       "  </DD>"
2044       "  <DT>"
2045       "    <SPAN>"
2046       "      Email:"
2047       "    </SPAN>"
2048       "  </DT>"
2049       "  <DD>"
2050       "    <FONT>"
2051       "      <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
2052       "    </FONT>"
2053       "  </DD>"
2054       "  <DT></DT>"
2055       "  <DD>"
2056       "    <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2057       "  </DD>"
2058       "</DL>"
2059       "</DIV>"
2060       "</FORM>");
2061 }
2062
2063 TEST_F(FormAutofillTest, FillFormMaxLength) {
2064   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2065            "  <INPUT type=\"text\" id=\"firstname\" maxlength=\"5\"/>"
2066            "  <INPUT type=\"text\" id=\"lastname\" maxlength=\"7\"/>"
2067            "  <INPUT type=\"text\" id=\"email\" maxlength=\"9\"/>"
2068            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2069            "</FORM>");
2070
2071   WebFrame* web_frame = GetMainFrame();
2072   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2073
2074   FormCache form_cache;
2075   std::vector<FormData> forms;
2076   form_cache.ExtractForms(*web_frame, &forms);
2077   ASSERT_EQ(1U, forms.size());
2078
2079   // Get the input element we want to find.
2080   WebElement element = web_frame->document().getElementById("firstname");
2081   WebInputElement input_element = element.to<WebInputElement>();
2082
2083   // Find the form that contains the input element.
2084   FormData form;
2085   FormFieldData field;
2086   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2087                                               autofill::REQUIRE_NONE));
2088   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2089   EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2090   EXPECT_EQ(GURL("http://buh.com"), form.action);
2091
2092   const std::vector<FormFieldData>& fields = form.fields;
2093   ASSERT_EQ(3U, fields.size());
2094
2095   FormFieldData expected;
2096   expected.form_control_type = "text";
2097
2098   expected.name = ASCIIToUTF16("firstname");
2099   expected.max_length = 5;
2100   expected.is_autofilled = false;
2101   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2102
2103   expected.name = ASCIIToUTF16("lastname");
2104   expected.max_length = 7;
2105   expected.is_autofilled = false;
2106   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2107
2108   expected.name = ASCIIToUTF16("email");
2109   expected.max_length = 9;
2110   expected.is_autofilled = false;
2111   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2112
2113   // Fill the form.
2114   form.fields[0].value = ASCIIToUTF16("Brother");
2115   form.fields[1].value = ASCIIToUTF16("Jonathan");
2116   form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2117   FillForm(form, input_element);
2118
2119   // Find the newly-filled form that contains the input element.
2120   FormData form2;
2121   FormFieldData field2;
2122   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2123                                               autofill::REQUIRE_NONE));
2124
2125   EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2126   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2127   EXPECT_EQ(GURL("http://buh.com"), form2.action);
2128
2129   const std::vector<FormFieldData>& fields2 = form2.fields;
2130   ASSERT_EQ(3U, fields2.size());
2131
2132   expected.form_control_type = "text";
2133
2134   expected.name = ASCIIToUTF16("firstname");
2135   expected.value = ASCIIToUTF16("Broth");
2136   expected.max_length = 5;
2137   expected.is_autofilled = true;
2138   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2139
2140   expected.name = ASCIIToUTF16("lastname");
2141   expected.value = ASCIIToUTF16("Jonatha");
2142   expected.max_length = 7;
2143   expected.is_autofilled = true;
2144   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2145
2146   expected.name = ASCIIToUTF16("email");
2147   expected.value = ASCIIToUTF16("brotherj@");
2148   expected.max_length = 9;
2149   expected.is_autofilled = true;
2150   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2151 }
2152
2153 // This test uses negative values of the maxlength attribute for input elements.
2154 // In this case, the maxlength of the input elements is set to the default
2155 // maxlength (defined in WebKit.)
2156 TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
2157   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2158            "  <INPUT type=\"text\" id=\"firstname\" maxlength=\"-1\"/>"
2159            "  <INPUT type=\"text\" id=\"lastname\" maxlength=\"-10\"/>"
2160            "  <INPUT type=\"text\" id=\"email\" maxlength=\"-13\"/>"
2161            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2162            "</FORM>");
2163
2164   WebFrame* web_frame = GetMainFrame();
2165   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2166
2167   FormCache form_cache;
2168   std::vector<FormData> forms;
2169   form_cache.ExtractForms(*web_frame, &forms);
2170   ASSERT_EQ(1U, forms.size());
2171
2172   // Get the input element we want to find.
2173   WebElement element = web_frame->document().getElementById("firstname");
2174   WebInputElement input_element = element.to<WebInputElement>();
2175
2176   // Find the form that contains the input element.
2177   FormData form;
2178   FormFieldData field;
2179   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2180                                               autofill::REQUIRE_NONE));
2181   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2182   EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2183   EXPECT_EQ(GURL("http://buh.com"), form.action);
2184
2185   const std::vector<FormFieldData>& fields = form.fields;
2186   ASSERT_EQ(3U, fields.size());
2187
2188   FormFieldData expected;
2189   expected.form_control_type = "text";
2190   expected.max_length = WebInputElement::defaultMaxLength();
2191
2192   expected.name = ASCIIToUTF16("firstname");
2193   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2194
2195   expected.name = ASCIIToUTF16("lastname");
2196   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2197
2198   expected.name = ASCIIToUTF16("email");
2199   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2200
2201   // Fill the form.
2202   form.fields[0].value = ASCIIToUTF16("Brother");
2203   form.fields[1].value = ASCIIToUTF16("Jonathan");
2204   form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2205   FillForm(form, input_element);
2206
2207   // Find the newly-filled form that contains the input element.
2208   FormData form2;
2209   FormFieldData field2;
2210   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2211                                               autofill::REQUIRE_NONE));
2212
2213   EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2214   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2215   EXPECT_EQ(GURL("http://buh.com"), form2.action);
2216
2217   const std::vector<FormFieldData>& fields2 = form2.fields;
2218   ASSERT_EQ(3U, fields2.size());
2219
2220   expected.name = ASCIIToUTF16("firstname");
2221   expected.value = ASCIIToUTF16("Brother");
2222   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2223
2224   expected.name = ASCIIToUTF16("lastname");
2225   expected.value = ASCIIToUTF16("Jonathan");
2226   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2227
2228   expected.name = ASCIIToUTF16("email");
2229   expected.value = ASCIIToUTF16("brotherj@example.com");
2230   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2231 }
2232
2233 TEST_F(FormAutofillTest, FillFormEmptyName) {
2234   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2235            "  <INPUT type=\"text\" id=\"firstname\"/>"
2236            "  <INPUT type=\"text\" id=\"lastname\"/>"
2237            "  <INPUT type=\"text\" id=\"email\"/>"
2238            "  <INPUT type=\"submit\" value=\"Send\"/>"
2239            "</FORM>");
2240
2241   WebFrame* web_frame = GetMainFrame();
2242   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2243
2244   FormCache form_cache;
2245   std::vector<FormData> forms;
2246   form_cache.ExtractForms(*web_frame, &forms);
2247   ASSERT_EQ(1U, forms.size());
2248
2249   // Get the input element we want to find.
2250   WebElement element = web_frame->document().getElementById("firstname");
2251   WebInputElement input_element = element.to<WebInputElement>();
2252
2253   // Find the form that contains the input element.
2254   FormData form;
2255   FormFieldData field;
2256   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2257                                               autofill::REQUIRE_NONE));
2258   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2259   EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2260   EXPECT_EQ(GURL("http://buh.com"), form.action);
2261
2262   const std::vector<FormFieldData>& fields = form.fields;
2263   ASSERT_EQ(3U, fields.size());
2264
2265   FormFieldData expected;
2266   expected.form_control_type = "text";
2267   expected.max_length = WebInputElement::defaultMaxLength();
2268
2269   expected.name = ASCIIToUTF16("firstname");
2270   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2271
2272   expected.name = ASCIIToUTF16("lastname");
2273   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2274
2275   expected.name = ASCIIToUTF16("email");
2276   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2277
2278   // Fill the form.
2279   form.fields[0].value = ASCIIToUTF16("Wyatt");
2280   form.fields[1].value = ASCIIToUTF16("Earp");
2281   form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2282   FillForm(form, input_element);
2283
2284   // Find the newly-filled form that contains the input element.
2285   FormData form2;
2286   FormFieldData field2;
2287   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2288                                               autofill::REQUIRE_NONE));
2289
2290   EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2291   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2292   EXPECT_EQ(GURL("http://buh.com"), form2.action);
2293
2294   const std::vector<FormFieldData>& fields2 = form2.fields;
2295   ASSERT_EQ(3U, fields2.size());
2296
2297   expected.form_control_type = "text";
2298   expected.max_length = WebInputElement::defaultMaxLength();
2299
2300   expected.name = ASCIIToUTF16("firstname");
2301   expected.value = ASCIIToUTF16("Wyatt");
2302   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2303
2304   expected.name = ASCIIToUTF16("lastname");
2305   expected.value = ASCIIToUTF16("Earp");
2306   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2307
2308   expected.name = ASCIIToUTF16("email");
2309   expected.value = ASCIIToUTF16("wyatt@example.com");
2310   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2311 }
2312
2313 TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
2314   LoadHTML("<FORM action=\"http://buh.com\" method=\"post\">"
2315            "  <INPUT type=\"text\" id=\"firstname\"/>"
2316            "  <INPUT type=\"text\" id=\"middlename\"/>"
2317            "  <INPUT type=\"text\" id=\"lastname\"/>"
2318            "  <INPUT type=\"submit\" value=\"Send\"/>"
2319            "</FORM>"
2320            "<FORM action=\"http://abc.com\" method=\"post\">"
2321            "  <INPUT type=\"text\" id=\"apple\"/>"
2322            "  <INPUT type=\"text\" id=\"banana\"/>"
2323            "  <INPUT type=\"text\" id=\"cantelope\"/>"
2324            "  <INPUT type=\"submit\" value=\"Send\"/>"
2325            "</FORM>");
2326
2327   WebFrame* web_frame = GetMainFrame();
2328   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2329
2330   FormCache form_cache;
2331   std::vector<FormData> forms;
2332   form_cache.ExtractForms(*web_frame, &forms);
2333   ASSERT_EQ(2U, forms.size());
2334
2335   // Get the input element we want to find.
2336   WebElement element = web_frame->document().getElementById("apple");
2337   WebInputElement input_element = element.to<WebInputElement>();
2338
2339   // Find the form that contains the input element.
2340   FormData form;
2341   FormFieldData field;
2342   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2343                                               autofill::REQUIRE_NONE));
2344   EXPECT_EQ(string16(), form.name);
2345   EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2346   EXPECT_EQ(GURL("http://abc.com"), form.action);
2347
2348   const std::vector<FormFieldData>& fields = form.fields;
2349   ASSERT_EQ(3U, fields.size());
2350
2351   FormFieldData expected;
2352   expected.form_control_type = "text";
2353   expected.max_length = WebInputElement::defaultMaxLength();
2354
2355   expected.name = ASCIIToUTF16("apple");
2356   expected.is_autofilled = false;
2357   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2358
2359   expected.name = ASCIIToUTF16("banana");
2360   expected.is_autofilled = false;
2361   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2362
2363   expected.name = ASCIIToUTF16("cantelope");
2364   expected.is_autofilled = false;
2365   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2366
2367   // Fill the form.
2368   form.fields[0].value = ASCIIToUTF16("Red");
2369   form.fields[1].value = ASCIIToUTF16("Yellow");
2370   form.fields[2].value = ASCIIToUTF16("Also Yellow");
2371   FillForm(form, input_element);
2372
2373   // Find the newly-filled form that contains the input element.
2374   FormData form2;
2375   FormFieldData field2;
2376   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2377                                               autofill::REQUIRE_NONE));
2378
2379   EXPECT_EQ(string16(), form2.name);
2380   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2381   EXPECT_EQ(GURL("http://abc.com"), form2.action);
2382
2383   const std::vector<FormFieldData>& fields2 = form2.fields;
2384   ASSERT_EQ(3U, fields2.size());
2385
2386   expected.name = ASCIIToUTF16("apple");
2387   expected.value = ASCIIToUTF16("Red");
2388   expected.is_autofilled = true;
2389   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2390
2391   expected.name = ASCIIToUTF16("banana");
2392   expected.value = ASCIIToUTF16("Yellow");
2393   expected.is_autofilled = true;
2394   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2395
2396   expected.name = ASCIIToUTF16("cantelope");
2397   expected.value = ASCIIToUTF16("Also Yellow");
2398   expected.is_autofilled = true;
2399   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2400 }
2401
2402 TEST_F(FormAutofillTest, ThreePartPhone) {
2403   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2404            "  Phone:"
2405            "  <input type=\"text\" name=\"dayphone1\">"
2406            "  -"
2407            "  <input type=\"text\" name=\"dayphone2\">"
2408            "  -"
2409            "  <input type=\"text\" name=\"dayphone3\">"
2410            "  ext.:"
2411            "  <input type=\"text\" name=\"dayphone4\">"
2412            "  <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2413            "</FORM>");
2414
2415
2416   WebFrame* frame = GetMainFrame();
2417   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2418
2419   WebVector<WebFormElement> forms;
2420   frame->document().forms(forms);
2421   ASSERT_EQ(1U, forms.size());
2422
2423   FormData form;
2424   EXPECT_TRUE(WebFormElementToFormData(forms[0],
2425                                        WebFormControlElement(),
2426                                        autofill::REQUIRE_NONE,
2427                                        autofill::EXTRACT_VALUE,
2428                                        &form,
2429                                        NULL));
2430   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2431   EXPECT_EQ(GURL(frame->document().url()), form.origin);
2432   EXPECT_EQ(GURL("http://cnn.com"), form.action);
2433
2434   const std::vector<FormFieldData>& fields = form.fields;
2435   ASSERT_EQ(4U, fields.size());
2436
2437   FormFieldData expected;
2438   expected.form_control_type = "text";
2439   expected.max_length = WebInputElement::defaultMaxLength();
2440
2441   expected.label = ASCIIToUTF16("Phone:");
2442   expected.name = ASCIIToUTF16("dayphone1");
2443   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2444
2445   expected.label = ASCIIToUTF16("-");
2446   expected.name = ASCIIToUTF16("dayphone2");
2447   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2448
2449   expected.label = ASCIIToUTF16("-");
2450   expected.name = ASCIIToUTF16("dayphone3");
2451   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2452
2453   expected.label = ASCIIToUTF16("ext.:");
2454   expected.name = ASCIIToUTF16("dayphone4");
2455   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2456 }
2457
2458
2459 TEST_F(FormAutofillTest, MaxLengthFields) {
2460   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2461            "  Phone:"
2462            "  <input type=\"text\" maxlength=\"3\" name=\"dayphone1\">"
2463            "  -"
2464            "  <input type=\"text\" maxlength=\"3\" name=\"dayphone2\">"
2465            "  -"
2466            "  <input type=\"text\" maxlength=\"4\" size=\"5\""
2467            "         name=\"dayphone3\">"
2468            "  ext.:"
2469            "  <input type=\"text\" maxlength=\"5\" name=\"dayphone4\">"
2470            "  <input type=\"text\" name=\"default1\">"
2471            "  <input type=\"text\" maxlength=\"-1\" name=\"invalid1\">"
2472            "  <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2473            "</FORM>");
2474
2475   WebFrame* frame = GetMainFrame();
2476   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2477
2478   WebVector<WebFormElement> forms;
2479   frame->document().forms(forms);
2480   ASSERT_EQ(1U, forms.size());
2481
2482   FormData form;
2483   EXPECT_TRUE(WebFormElementToFormData(forms[0],
2484                                        WebFormControlElement(),
2485                                        autofill::REQUIRE_NONE,
2486                                        autofill::EXTRACT_VALUE,
2487                                        &form,
2488                                        NULL));
2489   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2490   EXPECT_EQ(GURL(frame->document().url()), form.origin);
2491   EXPECT_EQ(GURL("http://cnn.com"), form.action);
2492
2493   const std::vector<FormFieldData>& fields = form.fields;
2494   ASSERT_EQ(6U, fields.size());
2495
2496   FormFieldData expected;
2497   expected.form_control_type = "text";
2498
2499   expected.label = ASCIIToUTF16("Phone:");
2500   expected.name = ASCIIToUTF16("dayphone1");
2501   expected.max_length = 3;
2502   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2503
2504   expected.label = ASCIIToUTF16("-");
2505   expected.name = ASCIIToUTF16("dayphone2");
2506   expected.max_length = 3;
2507   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2508
2509   expected.label = ASCIIToUTF16("-");
2510   expected.name = ASCIIToUTF16("dayphone3");
2511   expected.max_length = 4;
2512   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2513
2514   expected.label = ASCIIToUTF16("ext.:");
2515   expected.name = ASCIIToUTF16("dayphone4");
2516   expected.max_length = 5;
2517   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2518
2519   // When unspecified |size|, default is returned.
2520   expected.label = string16();
2521   expected.name = ASCIIToUTF16("default1");
2522   expected.max_length = WebInputElement::defaultMaxLength();
2523   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
2524
2525   // When invalid |size|, default is returned.
2526   expected.label = string16();
2527   expected.name = ASCIIToUTF16("invalid1");
2528   expected.max_length = WebInputElement::defaultMaxLength();
2529   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
2530 }
2531
2532 // This test re-creates the experience of typing in a field then selecting a
2533 // profile from the Autofill suggestions popup.  The field that is being typed
2534 // into should be filled even though it's not technically empty.
2535 TEST_F(FormAutofillTest, FillFormNonEmptyField) {
2536   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2537            "  <INPUT type=\"text\" id=\"firstname\"/>"
2538            "  <INPUT type=\"text\" id=\"lastname\"/>"
2539            "  <INPUT type=\"text\" id=\"email\"/>"
2540            "  <INPUT type=\"submit\" value=\"Send\"/>"
2541            "</FORM>");
2542
2543   WebFrame* web_frame = GetMainFrame();
2544   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2545
2546   FormCache form_cache;
2547   std::vector<FormData> forms;
2548   form_cache.ExtractForms(*web_frame, &forms);
2549   ASSERT_EQ(1U, forms.size());
2550
2551   // Get the input element we want to find.
2552   WebElement element = web_frame->document().getElementById("firstname");
2553   WebInputElement input_element = element.to<WebInputElement>();
2554
2555   // Simulate typing by modifying the field value.
2556   input_element.setValue(ASCIIToUTF16("Wy"));
2557
2558   // Find the form that contains the input element.
2559   FormData form;
2560   FormFieldData field;
2561   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2562                                               autofill::REQUIRE_NONE));
2563   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2564   EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2565   EXPECT_EQ(GURL("http://buh.com"), form.action);
2566
2567   const std::vector<FormFieldData>& fields = form.fields;
2568   ASSERT_EQ(3U, fields.size());
2569
2570   FormFieldData expected;
2571   expected.form_control_type = "text";
2572   expected.max_length = WebInputElement::defaultMaxLength();
2573
2574   expected.name = ASCIIToUTF16("firstname");
2575   expected.value = ASCIIToUTF16("Wy");
2576   expected.is_autofilled = false;
2577   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2578
2579   expected.name = ASCIIToUTF16("lastname");
2580   expected.value = string16();
2581   expected.is_autofilled = false;
2582   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2583
2584   expected.name = ASCIIToUTF16("email");
2585   expected.value = string16();
2586   expected.is_autofilled = false;
2587   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2588
2589   // Preview the form and verify that the cursor position has been updated.
2590   form.fields[0].value = ASCIIToUTF16("Wyatt");
2591   form.fields[1].value = ASCIIToUTF16("Earp");
2592   form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2593   PreviewForm(form, input_element);
2594   EXPECT_EQ(2, input_element.selectionStart());
2595   EXPECT_EQ(5, input_element.selectionEnd());
2596
2597   // Fill the form.
2598   FillForm(form, input_element);
2599
2600   // Find the newly-filled form that contains the input element.
2601   FormData form2;
2602   FormFieldData field2;
2603   EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2604                                               autofill::REQUIRE_NONE));
2605
2606   EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2607   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2608   EXPECT_EQ(GURL("http://buh.com"), form2.action);
2609
2610   const std::vector<FormFieldData>& fields2 = form2.fields;
2611   ASSERT_EQ(3U, fields2.size());
2612
2613   expected.name = ASCIIToUTF16("firstname");
2614   expected.value = ASCIIToUTF16("Wyatt");
2615   expected.is_autofilled = true;
2616   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2617
2618   expected.name = ASCIIToUTF16("lastname");
2619   expected.value = ASCIIToUTF16("Earp");
2620   expected.is_autofilled = true;
2621   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2622
2623   expected.name = ASCIIToUTF16("email");
2624   expected.value = ASCIIToUTF16("wyatt@example.com");
2625   expected.is_autofilled = true;
2626   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2627
2628   // Verify that the cursor position has been updated.
2629   EXPECT_EQ(5, input_element.selectionStart());
2630   EXPECT_EQ(5, input_element.selectionEnd());
2631 }
2632
2633 TEST_F(FormAutofillTest, ClearFormWithNode) {
2634   LoadHTML(
2635       "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2636       "  <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2637       "  <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>"
2638       "  <INPUT type=\"text\" autocomplete=\"off\" id=\"noAC\" value=\"one\"/>"
2639       "  <INPUT type=\"text\" id=\"notenabled\" disabled=\"disabled\">"
2640       "  <INPUT type=\"month\" id=\"month\" value=\"2012-11\">"
2641       "  <INPUT type=\"month\" id=\"month-disabled\" value=\"2012-11\""
2642       "         disabled=\"disabled\">"
2643       "  <TEXTAREA id=\"textarea\">Apple.</TEXTAREA>"
2644       "  <TEXTAREA id=\"textarea-disabled\" disabled=\"disabled\">"
2645       "    Banana!"
2646       "  </TEXTAREA>"
2647       "  <TEXTAREA id=\"textarea-noAC\" autocomplete=\"off\">Carrot?</TEXTAREA>"
2648       "  <INPUT type=\"submit\" value=\"Send\"/>"
2649       "</FORM>");
2650
2651   WebFrame* web_frame = GetMainFrame();
2652   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2653
2654   FormCache form_cache;
2655   std::vector<FormData> forms;
2656   form_cache.ExtractForms(*web_frame, &forms);
2657   ASSERT_EQ(1U, forms.size());
2658
2659   // Set the auto-filled attribute on the firstname element.
2660   WebInputElement firstname =
2661       web_frame->document().getElementById("firstname").to<WebInputElement>();
2662   firstname.setAutofilled(true);
2663
2664   // Set the value of the disabled text input element.
2665   WebInputElement notenabled =
2666       web_frame->document().getElementById("notenabled").to<WebInputElement>();
2667   notenabled.setValue(WebString::fromUTF8("no clear"));
2668
2669   // Clear the form.
2670   EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
2671
2672   // Verify that the auto-filled attribute has been turned off.
2673   EXPECT_FALSE(firstname.isAutofilled());
2674
2675   // Verify the form is cleared.
2676   FormData form2;
2677   FormFieldData field2;
2678   EXPECT_TRUE(FindFormAndFieldForInputElement(firstname, &form2, &field2,
2679                                               autofill::REQUIRE_NONE));
2680   EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2681   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2682   EXPECT_EQ(GURL("http://buh.com"), form2.action);
2683
2684   const std::vector<FormFieldData>& fields2 = form2.fields;
2685   ASSERT_EQ(9U, fields2.size());
2686
2687   FormFieldData expected;
2688   expected.form_control_type = "text";
2689   expected.max_length = WebInputElement::defaultMaxLength();
2690
2691   expected.name = ASCIIToUTF16("firstname");
2692   expected.value = string16();
2693   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2694
2695   expected.name = ASCIIToUTF16("lastname");
2696   expected.value = string16();
2697   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2698
2699   expected.name = ASCIIToUTF16("noAC");
2700   expected.value = string16();
2701   expected.autocomplete_attribute = "off";
2702   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2703   expected.autocomplete_attribute = std::string();  // reset
2704
2705   expected.name = ASCIIToUTF16("notenabled");
2706   expected.value = ASCIIToUTF16("no clear");
2707   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[3]);
2708
2709   expected.form_control_type = "month";
2710   expected.max_length = 0;
2711   expected.name = ASCIIToUTF16("month");
2712   expected.value = string16();
2713   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[4]);
2714
2715   expected.name = ASCIIToUTF16("month-disabled");
2716   expected.value = ASCIIToUTF16("2012-11");
2717   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[5]);
2718
2719   expected.form_control_type = "textarea";
2720   expected.name = ASCIIToUTF16("textarea");
2721   expected.value = string16();
2722   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[6]);
2723
2724   expected.name = ASCIIToUTF16("textarea-disabled");
2725   expected.value = ASCIIToUTF16("    Banana!  ");
2726   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[7]);
2727
2728   expected.name = ASCIIToUTF16("textarea-noAC");
2729   expected.value = string16();
2730   expected.autocomplete_attribute = "off";
2731   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[8]);
2732   expected.autocomplete_attribute = std::string();  // reset
2733
2734   // Verify that the cursor position has been updated.
2735   EXPECT_EQ(0, firstname.selectionStart());
2736   EXPECT_EQ(0, firstname.selectionEnd());
2737 }
2738
2739 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) {
2740   LoadHTML(
2741       "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2742       "  <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2743       "  <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>"
2744       "  <SELECT id=\"state\" name=\"state\">"
2745       "    <OPTION selected>?</OPTION>"
2746       "    <OPTION>AA</OPTION>"
2747       "    <OPTION>AE</OPTION>"
2748       "    <OPTION>AK</OPTION>"
2749       "  </SELECT>"
2750       "  <INPUT type=\"submit\" value=\"Send\"/>"
2751       "</FORM>");
2752
2753   WebFrame* web_frame = GetMainFrame();
2754   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2755
2756   FormCache form_cache;
2757   std::vector<FormData> forms;
2758   form_cache.ExtractForms(*web_frame, &forms);
2759   ASSERT_EQ(1U, forms.size());
2760
2761   // Set the auto-filled attribute on the firstname element.
2762   WebInputElement firstname =
2763       web_frame->document().getElementById("firstname").to<WebInputElement>();
2764   firstname.setAutofilled(true);
2765
2766   // Set the value of the select-one.
2767   WebSelectElement select_element =
2768       web_frame->document().getElementById("state").to<WebSelectElement>();
2769   select_element.setValue(WebString::fromUTF8("AK"));
2770
2771   // Clear the form.
2772   EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
2773
2774   // Verify that the auto-filled attribute has been turned off.
2775   EXPECT_FALSE(firstname.isAutofilled());
2776
2777   // Verify the form is cleared.
2778   FormData form2;
2779   FormFieldData field2;
2780   EXPECT_TRUE(FindFormAndFieldForInputElement(firstname, &form2, &field2,
2781                                               autofill::REQUIRE_NONE));
2782   EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2783   EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2784   EXPECT_EQ(GURL("http://buh.com"), form2.action);
2785
2786   const std::vector<FormFieldData>& fields2 = form2.fields;
2787   ASSERT_EQ(3U, fields2.size());
2788
2789   FormFieldData expected;
2790
2791   expected.name = ASCIIToUTF16("firstname");
2792   expected.value = string16();
2793   expected.form_control_type = "text";
2794   expected.max_length = WebInputElement::defaultMaxLength();
2795   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2796
2797   expected.name = ASCIIToUTF16("lastname");
2798   expected.value = string16();
2799   expected.form_control_type = "text";
2800   expected.max_length = WebInputElement::defaultMaxLength();
2801   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2802
2803   expected.name = ASCIIToUTF16("state");
2804   expected.value = ASCIIToUTF16("?");
2805   expected.form_control_type = "select-one";
2806   expected.max_length = 0;
2807   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2808
2809   // Verify that the cursor position has been updated.
2810   EXPECT_EQ(0, firstname.selectionStart());
2811   EXPECT_EQ(0, firstname.selectionEnd());
2812 }
2813
2814 TEST_F(FormAutofillTest, ClearPreviewedFormWithElement) {
2815   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2816            "  <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2817            "  <INPUT type=\"text\" id=\"lastname\"/>"
2818            "  <INPUT type=\"text\" id=\"email\"/>"
2819            "  <INPUT type=\"email\" id=\"email2\"/>"
2820            "  <INPUT type=\"tel\" id=\"phone\"/>"
2821            "  <INPUT type=\"submit\" value=\"Send\"/>"
2822            "</FORM>");
2823
2824   WebFrame* web_frame = GetMainFrame();
2825   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2826
2827   FormCache form_cache;
2828   std::vector<FormData> forms;
2829   form_cache.ExtractForms(*web_frame, &forms);
2830   ASSERT_EQ(1U, forms.size());
2831
2832   // Set the auto-filled attribute.
2833   WebInputElement firstname =
2834       web_frame->document().getElementById("firstname").to<WebInputElement>();
2835   firstname.setAutofilled(true);
2836   WebInputElement lastname =
2837       web_frame->document().getElementById("lastname").to<WebInputElement>();
2838   lastname.setAutofilled(true);
2839   WebInputElement email =
2840       web_frame->document().getElementById("email").to<WebInputElement>();
2841   email.setAutofilled(true);
2842   WebInputElement email2 =
2843       web_frame->document().getElementById("email2").to<WebInputElement>();
2844   email2.setAutofilled(true);
2845   WebInputElement phone =
2846       web_frame->document().getElementById("phone").to<WebInputElement>();
2847   phone.setAutofilled(true);
2848
2849   // Set the suggested values on two of the elements.
2850   lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
2851   email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2852   email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2853   phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2854
2855   // Clear the previewed fields.
2856   EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
2857
2858   // Fields with empty suggestions suggestions are not modified.
2859   EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
2860   EXPECT_TRUE(firstname.suggestedValue().isEmpty());
2861   EXPECT_TRUE(firstname.isAutofilled());
2862
2863   // Verify the previewed fields are cleared.
2864   EXPECT_TRUE(lastname.value().isEmpty());
2865   EXPECT_TRUE(lastname.suggestedValue().isEmpty());
2866   EXPECT_FALSE(lastname.isAutofilled());
2867   EXPECT_TRUE(email.value().isEmpty());
2868   EXPECT_TRUE(email.suggestedValue().isEmpty());
2869   EXPECT_FALSE(email.isAutofilled());
2870   EXPECT_TRUE(email2.value().isEmpty());
2871   EXPECT_TRUE(email2.suggestedValue().isEmpty());
2872   EXPECT_FALSE(email2.isAutofilled());
2873   EXPECT_TRUE(phone.value().isEmpty());
2874   EXPECT_TRUE(phone.suggestedValue().isEmpty());
2875   EXPECT_FALSE(phone.isAutofilled());
2876
2877   // Verify that the cursor position has been updated.
2878   EXPECT_EQ(0, lastname.selectionStart());
2879   EXPECT_EQ(0, lastname.selectionEnd());
2880 }
2881
2882 TEST_F(FormAutofillTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
2883   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2884            "  <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
2885            "  <INPUT type=\"text\" id=\"lastname\"/>"
2886            "  <INPUT type=\"text\" id=\"email\"/>"
2887            "  <INPUT type=\"email\" id=\"email2\"/>"
2888            "  <INPUT type=\"tel\" id=\"phone\"/>"
2889            "  <INPUT type=\"submit\" value=\"Send\"/>"
2890            "</FORM>");
2891
2892   WebFrame* web_frame = GetMainFrame();
2893   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2894
2895   FormCache form_cache;
2896   std::vector<FormData> forms;
2897   form_cache.ExtractForms(*web_frame, &forms);
2898   ASSERT_EQ(1U, forms.size());
2899
2900   // Set the auto-filled attribute.
2901   WebInputElement firstname =
2902       web_frame->document().getElementById("firstname").to<WebInputElement>();
2903   firstname.setAutofilled(true);
2904   WebInputElement lastname =
2905       web_frame->document().getElementById("lastname").to<WebInputElement>();
2906   lastname.setAutofilled(true);
2907   WebInputElement email =
2908       web_frame->document().getElementById("email").to<WebInputElement>();
2909   email.setAutofilled(true);
2910   WebInputElement email2 =
2911       web_frame->document().getElementById("email2").to<WebInputElement>();
2912   email2.setAutofilled(true);
2913   WebInputElement phone =
2914       web_frame->document().getElementById("phone").to<WebInputElement>();
2915   phone.setAutofilled(true);
2916
2917
2918   // Set the suggested values on all of the elements.
2919   firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
2920   lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
2921   email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2922   email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2923   phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2924
2925   // Clear the previewed fields.
2926   EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
2927
2928   // Fields with non-empty values are restored.
2929   EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
2930   EXPECT_TRUE(firstname.suggestedValue().isEmpty());
2931   EXPECT_FALSE(firstname.isAutofilled());
2932   EXPECT_EQ(1, firstname.selectionStart());
2933   EXPECT_EQ(1, firstname.selectionEnd());
2934
2935   // Verify the previewed fields are cleared.
2936   EXPECT_TRUE(lastname.value().isEmpty());
2937   EXPECT_TRUE(lastname.suggestedValue().isEmpty());
2938   EXPECT_FALSE(lastname.isAutofilled());
2939   EXPECT_TRUE(email.value().isEmpty());
2940   EXPECT_TRUE(email.suggestedValue().isEmpty());
2941   EXPECT_FALSE(email.isAutofilled());
2942   EXPECT_TRUE(email2.value().isEmpty());
2943   EXPECT_TRUE(email2.suggestedValue().isEmpty());
2944   EXPECT_FALSE(email2.isAutofilled());
2945   EXPECT_TRUE(phone.value().isEmpty());
2946   EXPECT_TRUE(phone.suggestedValue().isEmpty());
2947   EXPECT_FALSE(phone.isAutofilled());
2948 }
2949
2950 TEST_F(FormAutofillTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
2951   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2952            "  <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
2953            "  <INPUT type=\"text\" id=\"lastname\"/>"
2954            "  <INPUT type=\"text\" id=\"email\"/>"
2955            "  <INPUT type=\"email\" id=\"email2\"/>"
2956            "  <INPUT type=\"tel\" id=\"phone\"/>"
2957            "  <INPUT type=\"submit\" value=\"Send\"/>"
2958            "</FORM>");
2959
2960   WebFrame* web_frame = GetMainFrame();
2961   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2962
2963   FormCache form_cache;
2964   std::vector<FormData> forms;
2965   form_cache.ExtractForms(*web_frame, &forms);
2966   ASSERT_EQ(1U, forms.size());
2967
2968   // Set the auto-filled attribute.
2969   WebInputElement firstname =
2970       web_frame->document().getElementById("firstname").to<WebInputElement>();
2971   firstname.setAutofilled(true);
2972   WebInputElement lastname =
2973       web_frame->document().getElementById("lastname").to<WebInputElement>();
2974   lastname.setAutofilled(true);
2975   WebInputElement email =
2976       web_frame->document().getElementById("email").to<WebInputElement>();
2977   email.setAutofilled(true);
2978   WebInputElement email2 =
2979       web_frame->document().getElementById("email2").to<WebInputElement>();
2980   email2.setAutofilled(true);
2981   WebInputElement phone =
2982       web_frame->document().getElementById("phone").to<WebInputElement>();
2983   phone.setAutofilled(true);
2984
2985   // Set the suggested values on all of the elements.
2986   firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
2987   lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
2988   email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2989   email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2990   phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2991
2992   // Clear the previewed fields.
2993   EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
2994
2995   // Fields with non-empty values are restored.
2996   EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
2997   EXPECT_TRUE(firstname.suggestedValue().isEmpty());
2998   EXPECT_TRUE(firstname.isAutofilled());
2999   EXPECT_EQ(1, firstname.selectionStart());
3000   EXPECT_EQ(1, firstname.selectionEnd());
3001
3002   // Verify the previewed fields are cleared.
3003   EXPECT_TRUE(lastname.value().isEmpty());
3004   EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3005   EXPECT_FALSE(lastname.isAutofilled());
3006   EXPECT_TRUE(email.value().isEmpty());
3007   EXPECT_TRUE(email.suggestedValue().isEmpty());
3008   EXPECT_FALSE(email.isAutofilled());
3009   EXPECT_TRUE(email2.value().isEmpty());
3010   EXPECT_TRUE(email2.suggestedValue().isEmpty());
3011   EXPECT_FALSE(email2.isAutofilled());
3012   EXPECT_TRUE(phone.value().isEmpty());
3013   EXPECT_TRUE(phone.suggestedValue().isEmpty());
3014   EXPECT_FALSE(phone.isAutofilled());
3015 }
3016
3017 TEST_F(FormAutofillTest, FormWithNodeIsAutofilled) {
3018   LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
3019            "  <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
3020            "  <INPUT type=\"text\" id=\"lastname\"/>"
3021            "  <INPUT type=\"text\" id=\"email\"/>"
3022            "  <INPUT type=\"email\" id=\"email2\"/>"
3023            "  <INPUT type=\"tel\" id=\"phone\"/>"
3024            "  <INPUT type=\"submit\" value=\"Send\"/>"
3025            "</FORM>");
3026
3027   WebFrame* web_frame = GetMainFrame();
3028   ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3029
3030   FormCache form_cache;
3031   std::vector<FormData> forms;
3032   form_cache.ExtractForms(*web_frame, &forms);
3033   ASSERT_EQ(1U, forms.size());
3034
3035   WebInputElement firstname =
3036       web_frame->document().getElementById("firstname").to<WebInputElement>();
3037
3038   // Auto-filled attribute not set yet.
3039   EXPECT_FALSE(FormWithElementIsAutofilled(firstname));
3040
3041   // Set the auto-filled attribute.
3042   firstname.setAutofilled(true);
3043
3044   EXPECT_TRUE(FormWithElementIsAutofilled(firstname));
3045 }
3046
3047 // If we have multiple labels per id, the labels concatenated into label string.
3048 TEST_F(FormAutofillTest, MultipleLabelsPerElement) {
3049   std::vector<string16> labels, names, values;
3050
3051   labels.push_back(ASCIIToUTF16("First Name:"));
3052   names.push_back(ASCIIToUTF16("firstname"));
3053   values.push_back(ASCIIToUTF16("John"));
3054
3055   labels.push_back(ASCIIToUTF16("Last Name:"));
3056   names.push_back(ASCIIToUTF16("lastname"));
3057   values.push_back(ASCIIToUTF16("Smith"));
3058
3059   labels.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
3060   names.push_back(ASCIIToUTF16("email"));
3061   values.push_back(ASCIIToUTF16("john@example.com"));
3062
3063   ExpectLabels(
3064       "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
3065       "  <LABEL for=\"firstname\"> First Name: </LABEL>"
3066       "  <LABEL for=\"firstname\"></LABEL>"
3067       "    <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
3068       "  <LABEL for=\"lastname\"></LABEL>"
3069       "  <LABEL for=\"lastname\"> Last Name: </LABEL>"
3070       "    <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
3071       "  <LABEL for=\"email\"> Email: </LABEL>"
3072       "  <LABEL for=\"email\"> xxx@yyy.com </LABEL>"
3073       "    <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
3074       "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
3075       "</FORM>",
3076       labels, names, values);
3077 }
3078
3079 TEST_F(FormAutofillTest, ClickElement) {
3080   LoadHTML("<BUTTON id=\"link\">Button</BUTTON>"
3081            "<BUTTON name=\"button\">Button</BUTTON>");
3082   WebFrame* frame = GetMainFrame();
3083   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3084
3085   // Successful retrieval by id.
3086   autofill::WebElementDescriptor clicker;
3087   clicker.retrieval_method = autofill::WebElementDescriptor::ID;
3088   clicker.descriptor = "link";
3089   EXPECT_TRUE(ClickElement(frame->document(), clicker));
3090
3091   // Successful retrieval by css selector.
3092   clicker.retrieval_method = autofill::WebElementDescriptor::CSS_SELECTOR;
3093   clicker.descriptor = "button[name=\"button\"]";
3094   EXPECT_TRUE(ClickElement(frame->document(), clicker));
3095
3096   // Unsuccessful retrieval due to invalid CSS selector.
3097   clicker.descriptor = "^*&";
3098   EXPECT_FALSE(ClickElement(frame->document(), clicker));
3099
3100   // Unsuccessful retrieval because element does not exist.
3101   clicker.descriptor = "#junk";
3102   EXPECT_FALSE(ClickElement(frame->document(), clicker));
3103 }
3104
3105 TEST_F(FormAutofillTest, SelectOneAsText) {
3106   LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
3107            "  <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
3108            "  <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
3109            "  <SELECT id=\"country\">"
3110            "    <OPTION value=\"AF\">Afghanistan</OPTION>"
3111            "    <OPTION value=\"AL\">Albania</OPTION>"
3112            "    <OPTION value=\"DZ\">Algeria</OPTION>"
3113            "  </SELECT>"
3114            "  <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
3115            "</FORM>");
3116
3117   WebFrame* frame = GetMainFrame();
3118   ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3119
3120   // Set the value of the select-one.
3121   WebSelectElement select_element =
3122       frame->document().getElementById("country").to<WebSelectElement>();
3123   select_element.setValue(WebString::fromUTF8("AL"));
3124
3125   WebVector<WebFormElement> forms;
3126   frame->document().forms(forms);
3127   ASSERT_EQ(1U, forms.size());
3128
3129   FormData form;
3130
3131   // Extract the country select-one value as text.
3132   EXPECT_TRUE(WebFormElementToFormData(
3133       forms[0], WebFormControlElement(), autofill::REQUIRE_NONE,
3134       static_cast<autofill::ExtractMask>(
3135           autofill::EXTRACT_VALUE | autofill::EXTRACT_OPTION_TEXT),
3136       &form, NULL));
3137   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3138   EXPECT_EQ(GURL(frame->document().url()), form.origin);
3139   EXPECT_EQ(GURL("http://cnn.com"), form.action);
3140
3141   const std::vector<FormFieldData>& fields = form.fields;
3142   ASSERT_EQ(3U, fields.size());
3143
3144   FormFieldData expected;
3145
3146   expected.name = ASCIIToUTF16("firstname");
3147   expected.value = ASCIIToUTF16("John");
3148   expected.form_control_type = "text";
3149   expected.max_length = WebInputElement::defaultMaxLength();
3150   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3151
3152   expected.name = ASCIIToUTF16("lastname");
3153   expected.value = ASCIIToUTF16("Smith");
3154   expected.form_control_type = "text";
3155   expected.max_length = WebInputElement::defaultMaxLength();
3156   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3157
3158   expected.name = ASCIIToUTF16("country");
3159   expected.value = ASCIIToUTF16("Albania");
3160   expected.form_control_type = "select-one";
3161   expected.max_length = 0;
3162   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3163
3164   form.fields.clear();
3165   // Extract the country select-one value as value.
3166   EXPECT_TRUE(WebFormElementToFormData(forms[0],
3167                                        WebFormControlElement(),
3168                                        autofill::REQUIRE_NONE,
3169                                        autofill::EXTRACT_VALUE,
3170                                        &form,
3171                                        NULL));
3172   EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3173   EXPECT_EQ(GURL(frame->document().url()), form.origin);
3174   EXPECT_EQ(GURL("http://cnn.com"), form.action);
3175
3176   ASSERT_EQ(3U, fields.size());
3177
3178   expected.name = ASCIIToUTF16("firstname");
3179   expected.value = ASCIIToUTF16("John");
3180   expected.form_control_type = "text";
3181   expected.max_length = WebInputElement::defaultMaxLength();
3182   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3183
3184   expected.name = ASCIIToUTF16("lastname");
3185   expected.value = ASCIIToUTF16("Smith");
3186   expected.form_control_type = "text";
3187   expected.max_length = WebInputElement::defaultMaxLength();
3188   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3189
3190   expected.name = ASCIIToUTF16("country");
3191   expected.value = ASCIIToUTF16("AL");
3192   expected.form_control_type = "select-one";
3193   expected.max_length = 0;
3194   EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3195 }
3196
3197 }  // namespace autofill