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