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