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