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