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