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