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.
7 #include "base/format_macros.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/test/base/chrome_render_view_test.h"
12 #include "components/autofill/content/renderer/form_autofill_util.h"
13 #include "components/autofill/content/renderer/form_cache.h"
14 #include "components/autofill/core/common/autofill_data_validation.h"
15 #include "components/autofill/core/common/form_data.h"
16 #include "components/autofill/core/common/web_element_descriptor.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/platform/WebVector.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebFormControlElement.h"
23 #include "third_party/WebKit/public/web/WebFormElement.h"
24 #include "third_party/WebKit/public/web/WebInputElement.h"
25 #include "third_party/WebKit/public/web/WebLocalFrame.h"
26 #include "third_party/WebKit/public/web/WebNode.h"
27 #include "third_party/WebKit/public/web/WebSelectElement.h"
28 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
30 using base::ASCIIToUTF16;
31 using blink::WebDocument;
32 using blink::WebElement;
33 using blink::WebFormControlElement;
34 using blink::WebFormElement;
35 using blink::WebFrame;
36 using blink::WebInputElement;
38 using blink::WebSelectElement;
39 using blink::WebString;
40 using blink::WebTextAreaElement;
41 using blink::WebVector;
45 struct AutofillFieldCase {
46 const char* const form_control_type;
47 const char* const name;
48 const char* const initial_value;
49 const char* const autocomplete_attribute; // The autocomplete attribute of
51 bool should_be_autofilled; // Whether the filed should be autofilled.
52 const char* const autofill_value; // The value being used to fill the field.
53 const char* const expected_value; // The expected value after Autofill
57 static const char kFormHtml[] =
58 "<FORM name='TestForm' action='http://buh.com' method='post'>"
59 " <INPUT type='text' id='firstname'/>"
60 " <INPUT type='text' id='lastname'/>"
61 " <INPUT type='hidden' id='imhidden'/>"
62 " <INPUT type='text' id='notempty' value='Hi'/>"
63 " <INPUT type='text' autocomplete='off' id='noautocomplete'/>"
64 " <INPUT type='text' disabled='disabled' id='notenabled'/>"
65 " <INPUT type='text' readonly id='readonly'/>"
66 " <INPUT type='text' style='visibility: hidden'"
68 " <INPUT type='text' style='display: none' id='displaynone'/>"
69 " <INPUT type='month' id='month'/>"
70 " <INPUT type='month' id='month-nonempty' value='2011-12'/>"
71 " <SELECT id='select'>"
73 " <OPTION value='CA'>California</OPTION>"
74 " <OPTION value='TX'>Texas</OPTION>"
76 " <SELECT id='select-nonempty'>"
77 " <OPTION value='CA' selected>California</OPTION>"
78 " <OPTION value='TX'>Texas</OPTION>"
80 " <SELECT id='select-unchanged'>"
81 " <OPTION value='CA' selected>California</OPTION>"
82 " <OPTION value='TX'>Texas</OPTION>"
84 " <TEXTAREA id='textarea'></TEXTAREA>"
85 " <TEXTAREA id='textarea-nonempty'>Go away!</TEXTAREA>"
86 " <INPUT type='submit' name='reply-send' value='Send'/>"
93 class FormAutofillTest : public ChromeRenderViewTest {
95 FormAutofillTest() : ChromeRenderViewTest() {}
96 ~FormAutofillTest() override {}
98 void ExpectLabels(const char* html,
99 const std::vector<base::string16>& labels,
100 const std::vector<base::string16>& names,
101 const std::vector<base::string16>& values) {
102 std::vector<std::string> control_types(labels.size(), "text");
103 ExpectLabelsAndTypes(html, labels, names, values, control_types);
106 void ExpectLabelsAndTypes(const char* html,
107 const std::vector<base::string16>& labels,
108 const std::vector<base::string16>& names,
109 const std::vector<base::string16>& values,
110 const std::vector<std::string>& control_types) {
111 ASSERT_EQ(labels.size(), names.size());
112 ASSERT_EQ(labels.size(), values.size());
113 ASSERT_EQ(labels.size(), control_types.size());
117 WebFrame* web_frame = GetMainFrame();
118 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
120 FormCache form_cache;
121 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
122 ASSERT_EQ(1U, forms.size());
124 const FormData& form = forms[0];
125 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
126 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
127 EXPECT_EQ(GURL("http://cnn.com"), form.action);
129 const std::vector<FormFieldData>& fields = form.fields;
130 ASSERT_EQ(labels.size(), fields.size());
131 for (size_t i = 0; i < labels.size(); ++i) {
132 int max_length = control_types[i] == "text" ?
133 WebInputElement::defaultMaxLength() : 0;
134 FormFieldData expected;
135 expected.label = labels[i];
136 expected.name = names[i];
137 expected.value = values[i];
138 expected.form_control_type = control_types[i];
139 expected.max_length = max_length;
140 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
141 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
145 void ExpectJohnSmithLabels(const char* html) {
146 std::vector<base::string16> labels, names, values;
148 labels.push_back(ASCIIToUTF16("First name:"));
149 names.push_back(ASCIIToUTF16("firstname"));
150 values.push_back(ASCIIToUTF16("John"));
152 labels.push_back(ASCIIToUTF16("Last name:"));
153 names.push_back(ASCIIToUTF16("lastname"));
154 values.push_back(ASCIIToUTF16("Smith"));
156 labels.push_back(ASCIIToUTF16("Email:"));
157 names.push_back(ASCIIToUTF16("email"));
158 values.push_back(ASCIIToUTF16("john@example.com"));
160 ExpectLabels(html, labels, names, values);
163 typedef void (*FillFormFunction)(const FormData& form,
164 const WebFormControlElement& element);
166 typedef WebString (*GetValueFunction)(WebFormControlElement element);
168 // Test FormFillxxx functions.
169 void TestFormFillFunctions(const char* html,
170 const AutofillFieldCase* field_cases,
171 size_t number_of_field_cases,
172 FillFormFunction fill_form_function,
173 GetValueFunction get_value_function) {
176 WebFrame* web_frame = GetMainFrame();
177 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
179 FormCache form_cache;
180 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
181 ASSERT_EQ(1U, forms.size());
183 // Get the input element we want to find.
184 WebElement element = web_frame->document().getElementById("firstname");
185 WebInputElement input_element = element.to<WebInputElement>();
187 // Find the form that contains the input element.
191 FindFormAndFieldForFormControlElement(input_element,
194 autofill::REQUIRE_AUTOCOMPLETE));
195 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name);
196 EXPECT_EQ(GURL(web_frame->document().url()), form_data.origin);
197 EXPECT_EQ(GURL("http://buh.com"), form_data.action);
199 const std::vector<FormFieldData>& fields = form_data.fields;
200 ASSERT_EQ(number_of_field_cases, fields.size());
202 FormFieldData expected;
203 // Verify field's initial value.
204 for (size_t i = 0; i < number_of_field_cases; ++i) {
205 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
206 field_cases[i].name));
207 expected.form_control_type = field_cases[i].form_control_type;
208 expected.max_length =
209 expected.form_control_type == "text" ?
210 WebInputElement::defaultMaxLength() : 0;
211 expected.name = ASCIIToUTF16(field_cases[i].name);
212 expected.value = ASCIIToUTF16(field_cases[i].initial_value);
213 expected.autocomplete_attribute = field_cases[i].autocomplete_attribute;
214 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
215 // Fill the form_data for the field.
216 form_data.fields[i].value = ASCIIToUTF16(field_cases[i].autofill_value);
217 // Set the is_autofilled property for the field.
218 form_data.fields[i].is_autofilled = field_cases[i].should_be_autofilled;
221 // Autofill the form using the given fill form function.
222 fill_form_function(form_data, input_element);
224 // Validate Autofill or Preview results.
225 for (size_t i = 0; i < number_of_field_cases; ++i) {
226 ValidteFilledField(field_cases[i], get_value_function);
230 // Validate an Autofilled field.
231 void ValidteFilledField(const AutofillFieldCase& field_case,
232 GetValueFunction get_value_function) {
233 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
236 WebFormControlElement element = GetMainFrame()->document().getElementById(
237 ASCIIToUTF16(field_case.name)).to<WebFormControlElement>();
238 if ((element.formControlType() == "select-one") ||
239 (element.formControlType() == "textarea")) {
240 value = get_value_function(element);
242 ASSERT_TRUE(element.formControlType() == "text" ||
243 element.formControlType() == "month");
244 value = get_value_function(element);
247 const WebString expected_value = ASCIIToUTF16(field_case.expected_value);
248 if (expected_value.isEmpty())
249 EXPECT_TRUE(value.isEmpty());
251 EXPECT_EQ(expected_value, value);
253 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled());
256 static void FillFormForAllFieldsWrapper(const FormData& form,
257 const WebInputElement& element) {
258 FillFormForAllElements(form, element.form());
261 static void FillFormIncludingNonFocusableElementsWrapper(
262 const FormData& form,
263 const WebFormControlElement& element) {
264 FillFormIncludingNonFocusableElements(form, element.form());
267 static WebString GetValueWrapper(WebFormControlElement element) {
268 if (element.formControlType() == "textarea")
269 return element.to<WebTextAreaElement>().value();
271 if (element.formControlType() == "select-one")
272 return element.to<WebSelectElement>().value();
274 return element.to<WebInputElement>().value();
277 static WebString GetSuggestedValueWrapper(WebFormControlElement element) {
278 if (element.formControlType() == "textarea")
279 return element.to<WebTextAreaElement>().suggestedValue();
281 if (element.formControlType() == "select-one")
282 return element.to<WebSelectElement>().suggestedValue();
284 return element.to<WebInputElement>().suggestedValue();
288 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest);
291 // We should be able to extract a normal text field.
292 TEST_F(FormAutofillTest, WebFormControlElementToFormField) {
293 LoadHTML("<INPUT type='text' id='element' value='value'/>");
295 WebFrame* frame = GetMainFrame();
296 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
298 WebElement web_element = frame->document().getElementById("element");
299 WebFormControlElement element = web_element.to<WebFormControlElement>();
300 FormFieldData result1;
301 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result1);
303 FormFieldData expected;
304 expected.form_control_type = "text";
305 expected.max_length = WebInputElement::defaultMaxLength();
307 expected.name = ASCIIToUTF16("element");
308 expected.value = base::string16();
309 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
311 FormFieldData result2;
312 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result2);
314 expected.name = ASCIIToUTF16("element");
315 expected.value = ASCIIToUTF16("value");
316 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
319 // We should be able to extract a text field with autocomplete="off".
320 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompleteOff) {
321 LoadHTML("<INPUT type='text' id='element' value='value'"
322 " autocomplete='off'/>");
324 WebFrame* frame = GetMainFrame();
325 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
327 WebElement web_element = frame->document().getElementById("element");
328 WebFormControlElement element = web_element.to<WebFormControlElement>();
329 FormFieldData result;
330 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
332 FormFieldData expected;
333 expected.name = ASCIIToUTF16("element");
334 expected.value = ASCIIToUTF16("value");
335 expected.form_control_type = "text";
336 expected.autocomplete_attribute = "off";
337 expected.max_length = WebInputElement::defaultMaxLength();
338 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
341 // We should be able to extract a text field with maxlength specified.
342 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMaxLength) {
343 LoadHTML("<INPUT type='text' id='element' value='value'"
346 WebFrame* frame = GetMainFrame();
347 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
349 WebElement web_element = frame->document().getElementById("element");
350 WebFormControlElement element = web_element.to<WebFormControlElement>();
351 FormFieldData result;
352 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
354 FormFieldData expected;
355 expected.name = ASCIIToUTF16("element");
356 expected.value = ASCIIToUTF16("value");
357 expected.form_control_type = "text";
358 expected.max_length = 5;
359 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
362 // We should be able to extract a text field that has been autofilled.
363 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutofilled) {
364 LoadHTML("<INPUT type='text' id='element' value='value'/>");
366 WebFrame* frame = GetMainFrame();
367 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
369 WebElement web_element = frame->document().getElementById("element");
370 WebInputElement element = web_element.to<WebInputElement>();
371 element.setAutofilled(true);
372 FormFieldData result;
373 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
375 FormFieldData expected;
376 expected.name = ASCIIToUTF16("element");
377 expected.value = ASCIIToUTF16("value");
378 expected.form_control_type = "text";
379 expected.max_length = WebInputElement::defaultMaxLength();
380 expected.is_autofilled = true;
381 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
384 // We should be able to extract a radio or a checkbox field that has been
386 TEST_F(FormAutofillTest, WebFormControlElementToClickableFormField) {
387 LoadHTML("<INPUT type='checkbox' id='checkbox' value='mail' checked/>"
388 "<INPUT type='radio' id='radio' value='male'/>");
390 WebFrame* frame = GetMainFrame();
391 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
393 WebElement web_element = frame->document().getElementById("checkbox");
394 WebInputElement element = web_element.to<WebInputElement>();
395 element.setAutofilled(true);
396 FormFieldData result;
397 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
399 FormFieldData expected;
400 expected.name = ASCIIToUTF16("checkbox");
401 expected.value = ASCIIToUTF16("mail");
402 expected.form_control_type = "checkbox";
403 expected.is_autofilled = true;
404 expected.is_checkable = true;
405 expected.is_checked = true;
406 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
408 web_element = frame->document().getElementById("radio");
409 element = web_element.to<WebInputElement>();
410 element.setAutofilled(true);
411 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
412 expected.name = ASCIIToUTF16("radio");
413 expected.value = ASCIIToUTF16("male");
414 expected.form_control_type = "radio";
415 expected.is_autofilled = true;
416 expected.is_checkable = true;
417 expected.is_checked = false;
418 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
421 // We should be able to extract a <select> field.
422 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) {
423 LoadHTML("<SELECT id='element'/>"
424 " <OPTION value='CA'>California</OPTION>"
425 " <OPTION value='TX'>Texas</OPTION>"
428 WebFrame* frame = GetMainFrame();
429 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
431 WebElement web_element = frame->document().getElementById("element");
432 WebFormControlElement element = web_element.to<WebFormControlElement>();
433 FormFieldData result1;
434 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result1);
436 FormFieldData expected;
437 expected.name = ASCIIToUTF16("element");
438 expected.max_length = 0;
439 expected.form_control_type = "select-one";
441 expected.value = ASCIIToUTF16("CA");
442 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
444 FormFieldData result2;
445 WebFormControlElementToFormField(
447 static_cast<autofill::ExtractMask>(autofill::EXTRACT_VALUE |
448 autofill::EXTRACT_OPTION_TEXT),
450 expected.value = ASCIIToUTF16("California");
451 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
453 FormFieldData result3;
454 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS,
456 expected.value = base::string16();
457 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result3);
459 ASSERT_EQ(2U, result3.option_values.size());
460 ASSERT_EQ(2U, result3.option_contents.size());
461 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]);
462 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]);
463 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]);
464 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]);
467 // When faced with <select> field with *many* options, we should trim them to a
468 // reasonable number.
469 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldLongSelect) {
470 std::string html = "<SELECT id='element'/>";
471 for (size_t i = 0; i < 2 * kMaxListSize; ++i) {
472 html += base::StringPrintf("<OPTION value='%" PRIuS "'>"
473 "%" PRIuS "</OPTION>", i, i);
476 LoadHTML(html.c_str());
478 WebFrame* frame = GetMainFrame();
481 WebElement web_element = frame->document().getElementById("element");
482 WebFormControlElement element = web_element.to<WebFormControlElement>();
483 FormFieldData result;
484 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS, &result);
486 EXPECT_TRUE(result.option_values.empty());
487 EXPECT_TRUE(result.option_contents.empty());
490 // We should be able to extract a <textarea> field.
491 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) {
492 LoadHTML("<TEXTAREA id='element'>"
493 "This element's value "
494 "spans multiple lines."
497 WebFrame* frame = GetMainFrame();
498 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
500 WebElement web_element = frame->document().getElementById("element");
501 WebFormControlElement element = web_element.to<WebFormControlElement>();
502 FormFieldData result_sans_value;
503 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
506 FormFieldData expected;
507 expected.name = ASCIIToUTF16("element");
508 expected.max_length = 0;
509 expected.form_control_type = "textarea";
510 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
512 FormFieldData result_with_value;
513 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
515 expected.value = ASCIIToUTF16("This element's value\n"
516 "spans multiple lines.");
517 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
520 // We should be able to extract an <input type="month"> field.
521 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMonthInput) {
522 LoadHTML("<INPUT type='month' id='element' value='2011-12'>");
524 WebFrame* frame = GetMainFrame();
525 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
527 WebElement web_element = frame->document().getElementById("element");
528 WebFormControlElement element = web_element.to<WebFormControlElement>();
529 FormFieldData result_sans_value;
530 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
533 FormFieldData expected;
534 expected.name = ASCIIToUTF16("element");
535 expected.max_length = 0;
536 expected.form_control_type = "month";
537 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
539 FormFieldData result_with_value;
540 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
542 expected.value = ASCIIToUTF16("2011-12");
543 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
546 // We should not extract the value for non-text and non-select fields.
547 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) {
548 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
549 " <INPUT type='hidden' id='hidden' value='apple'/>"
550 " <INPUT type='submit' id='submit' value='Send'/>"
553 WebFrame* frame = GetMainFrame();
554 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
556 WebElement web_element = frame->document().getElementById("hidden");
557 WebFormControlElement element = web_element.to<WebFormControlElement>();
558 FormFieldData result;
559 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
561 FormFieldData expected;
562 expected.max_length = 0;
564 expected.name = ASCIIToUTF16("hidden");
565 expected.form_control_type = "hidden";
566 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
568 web_element = frame->document().getElementById("submit");
569 element = web_element.to<WebFormControlElement>();
570 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
571 expected.name = ASCIIToUTF16("submit");
572 expected.form_control_type = "submit";
573 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
576 // We should be able to extract password fields.
577 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) {
578 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
579 " <INPUT type='password' id='password' value='secret'/>"
582 WebFrame* frame = GetMainFrame();
583 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
585 WebElement web_element = frame->document().getElementById("password");
586 WebFormControlElement element = web_element.to<WebFormControlElement>();
587 FormFieldData result;
588 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
590 FormFieldData expected;
591 expected.max_length = WebInputElement::defaultMaxLength();
592 expected.name = ASCIIToUTF16("password");
593 expected.form_control_type = "password";
594 expected.value = ASCIIToUTF16("secret");
595 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
598 // We should be able to extract the autocompletetype attribute.
599 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) {
601 "<INPUT type='text' id='absent'/>"
602 "<INPUT type='text' id='empty' autocomplete=''/>"
603 "<INPUT type='text' id='off' autocomplete='off'/>"
604 "<INPUT type='text' id='regular' autocomplete='email'/>"
605 "<INPUT type='text' id='multi-valued' "
606 " autocomplete='billing email'/>"
607 "<INPUT type='text' id='experimental' x-autocompletetype='email'/>"
608 "<INPUT type='month' id='month' autocomplete='cc-exp'/>"
609 "<SELECT id='select' autocomplete='state'/>"
610 " <OPTION value='CA'>California</OPTION>"
611 " <OPTION value='TX'>Texas</OPTION>"
613 "<TEXTAREA id='textarea' autocomplete='street-address'>"
618 "<INPUT type='text' id='malicious' autocomplete='" +
619 std::string(10000, 'x') + "'/>";
620 LoadHTML(html.c_str());
622 WebFrame* frame = GetMainFrame();
623 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
626 const std::string element_id;
627 const std::string form_control_type;
628 const std::string autocomplete_attribute;
630 TestCase test_cases[] = {
631 // An absent attribute is equivalent to an empty one.
632 { "absent", "text", "" },
633 // Make sure there are no issues parsing an empty attribute.
634 { "empty", "text", "" },
635 // Make sure there are no issues parsing an attribute value that isn't a
637 { "off", "text", "off" },
638 // Common case: exactly one type specified.
639 { "regular", "text", "email" },
640 // Verify that we correctly extract multiple tokens as well.
641 { "multi-valued", "text", "billing email" },
642 // Verify that <input type="month"> fields are supported.
643 { "month", "month", "cc-exp" },
644 // We previously extracted this data from the experimental
645 // 'x-autocompletetype' attribute. Now that the field type hints are part
646 // of the spec under the autocomplete attribute, we no longer support the
647 // experimental version.
648 { "experimental", "text", "" },
649 // <select> elements should behave no differently from text fields here.
650 { "select", "select-one", "state" },
651 // <textarea> elements should also behave no differently from text fields.
652 { "textarea", "textarea", "street-address" },
653 // Very long attribute values should be replaced by a default string, to
654 // prevent malicious websites from DOSing the browser process.
655 { "malicious", "text", "x-max-data-length-exceeded" },
658 for (size_t i = 0; i < arraysize(test_cases); ++i) {
659 WebElement web_element = frame->document().getElementById(
660 ASCIIToUTF16(test_cases[i].element_id));
661 WebFormControlElement element = web_element.to<WebFormControlElement>();
662 FormFieldData result;
663 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result);
665 FormFieldData expected;
666 expected.name = ASCIIToUTF16(test_cases[i].element_id);
667 expected.form_control_type = test_cases[i].form_control_type;
668 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute;
669 if (test_cases[i].form_control_type == "text")
670 expected.max_length = WebInputElement::defaultMaxLength();
672 expected.max_length = 0;
674 SCOPED_TRACE(test_cases[i].element_id);
675 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
679 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectStyle) {
680 LoadHTML("<STYLE>input{direction:rtl}</STYLE>"
682 " <INPUT type='text' id='element'>"
685 WebFrame* frame = GetMainFrame();
686 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
688 WebElement web_element = frame->document().getElementById("element");
689 WebFormControlElement element = web_element.to<WebFormControlElement>();
691 FormFieldData result;
692 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
693 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
696 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectDIRAttribute) {
698 " <INPUT dir='rtl' type='text' id='element'/>"
701 WebFrame* frame = GetMainFrame();
702 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
704 WebElement web_element = frame->document().getElementById("element");
705 WebFormControlElement element = web_element.to<WebFormControlElement>();
707 FormFieldData result;
708 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
709 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
712 TEST_F(FormAutofillTest, DetectTextDirectionFromParentStyle) {
713 LoadHTML("<STYLE>form{direction:rtl}</STYLE>"
715 " <INPUT type='text' id='element'/>"
718 WebFrame* frame = GetMainFrame();
719 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
721 WebElement web_element = frame->document().getElementById("element");
722 WebFormControlElement element = web_element.to<WebFormControlElement>();
724 FormFieldData result;
725 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
726 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
729 TEST_F(FormAutofillTest, DetectTextDirectionFromParentDIRAttribute) {
730 LoadHTML("<FORM dir='rtl'>"
731 " <INPUT type='text' id='element'/>"
734 WebFrame* frame = GetMainFrame();
735 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
737 WebElement web_element = frame->document().getElementById("element");
738 WebFormControlElement element = web_element.to<WebFormControlElement>();
740 FormFieldData result;
741 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
742 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
745 TEST_F(FormAutofillTest, DetectTextDirectionWhenStyleAndDIRAttributMixed) {
746 LoadHTML("<STYLE>input{direction:ltr}</STYLE>"
748 " <INPUT type='text' id='element'/>"
751 WebFrame* frame = GetMainFrame();
752 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
754 WebElement web_element = frame->document().getElementById("element");
755 WebFormControlElement element = web_element.to<WebFormControlElement>();
757 FormFieldData result;
758 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
759 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
762 TEST_F(FormAutofillTest,
763 DetectTextDirectionWhenParentHasBothDIRAttributeAndStyle) {
764 LoadHTML("<STYLE>form{direction:ltr}</STYLE>"
766 " <INPUT type='text' id='element'/>"
769 WebFrame* frame = GetMainFrame();
770 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
772 WebElement web_element = frame->document().getElementById("element");
773 WebFormControlElement element = web_element.to<WebFormControlElement>();
775 FormFieldData result;
776 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
777 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
780 TEST_F(FormAutofillTest, DetectTextDirectionWhenAncestorHasInlineStyle) {
781 LoadHTML("<FORM style='direction:ltr'>"
783 " <INPUT type='text' id='element'/>"
787 WebFrame* frame = GetMainFrame();
788 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
790 WebElement web_element = frame->document().getElementById("element");
791 WebFormControlElement element = web_element.to<WebFormControlElement>();
793 FormFieldData result;
794 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
795 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
798 TEST_F(FormAutofillTest, WebFormElementToFormData) {
799 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
800 " <LABEL for='firstname'>First name:</LABEL>"
801 " <INPUT type='text' id='firstname' value='John'/>"
802 " <LABEL for='lastname'>Last name:</LABEL>"
803 " <INPUT type='text' id='lastname' value='Smith'/>"
804 " <LABEL for='street-address'>Address:</LABEL>"
805 " <TEXTAREA id='street-address'>"
806 "123 Fantasy Ln. "
809 " <LABEL for='state'>State:</LABEL>"
810 " <SELECT id='state'/>"
811 " <OPTION value='CA'>California</OPTION>"
812 " <OPTION value='TX'>Texas</OPTION>"
814 " <LABEL for='password'>Password:</LABEL>"
815 " <INPUT type='password' id='password' value='secret'/>"
816 " <LABEL for='month'>Card expiration:</LABEL>"
817 " <INPUT type='month' id='month' value='2011-12'/>"
818 " <INPUT type='submit' name='reply-send' value='Send'/>"
819 // The below inputs should be ignored
820 " <LABEL for='notvisible'>Hidden:</LABEL>"
821 " <INPUT type='hidden' id='notvisible' value='apple'/>"
824 WebFrame* frame = GetMainFrame();
825 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
827 WebVector<WebFormElement> forms;
828 frame->document().forms(forms);
829 ASSERT_EQ(1U, forms.size());
831 WebElement element = frame->document().getElementById("firstname");
832 WebInputElement input_element = element.to<WebInputElement>();
836 EXPECT_TRUE(WebFormElementToFormData(forms[0],
838 autofill::REQUIRE_NONE,
839 autofill::EXTRACT_VALUE,
842 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
843 EXPECT_EQ(GURL(frame->document().url()), form.origin);
844 EXPECT_EQ(GURL("http://cnn.com"), form.action);
846 const std::vector<FormFieldData>& fields = form.fields;
847 ASSERT_EQ(6U, fields.size());
849 FormFieldData expected;
850 expected.name = ASCIIToUTF16("firstname");
851 expected.value = ASCIIToUTF16("John");
852 expected.label = ASCIIToUTF16("First name:");
853 expected.form_control_type = "text";
854 expected.max_length = WebInputElement::defaultMaxLength();
855 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
857 expected.name = ASCIIToUTF16("lastname");
858 expected.value = ASCIIToUTF16("Smith");
859 expected.label = ASCIIToUTF16("Last name:");
860 expected.form_control_type = "text";
861 expected.max_length = WebInputElement::defaultMaxLength();
862 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
864 expected.name = ASCIIToUTF16("street-address");
865 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
866 expected.label = ASCIIToUTF16("Address:");
867 expected.form_control_type = "textarea";
868 expected.max_length = 0;
869 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
871 expected.name = ASCIIToUTF16("state");
872 expected.value = ASCIIToUTF16("CA");
873 expected.label = ASCIIToUTF16("State:");
874 expected.form_control_type = "select-one";
875 expected.max_length = 0;
876 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
878 expected.name = ASCIIToUTF16("password");
879 expected.value = ASCIIToUTF16("secret");
880 expected.label = ASCIIToUTF16("Password:");
881 expected.form_control_type = "password";
882 expected.max_length = WebInputElement::defaultMaxLength();
883 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
885 expected.name = ASCIIToUTF16("month");
886 expected.value = ASCIIToUTF16("2011-12");
887 expected.label = ASCIIToUTF16("Card expiration:");
888 expected.form_control_type = "month";
889 expected.max_length = 0;
890 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
893 // We should not be able to serialize a form with too many fillable fields.
894 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) {
896 "<FORM name='TestForm' action='http://cnn.com' method='post'>";
897 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) {
898 html += "<INPUT type='text'/>";
901 LoadHTML(html.c_str());
903 WebFrame* frame = GetMainFrame();
904 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
906 WebVector<WebFormElement> forms;
907 frame->document().forms(forms);
908 ASSERT_EQ(1U, forms.size());
910 WebElement element = frame->document().getElementById("firstname");
911 WebInputElement input_element = element.to<WebInputElement>();
915 EXPECT_FALSE(WebFormElementToFormData(forms[0],
917 autofill::REQUIRE_NONE,
918 autofill::EXTRACT_VALUE,
923 TEST_F(FormAutofillTest, ExtractForms) {
924 ExpectJohnSmithLabels(
925 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
926 " First name: <INPUT type='text' id='firstname' value='John'/>"
927 " Last name: <INPUT type='text' id='lastname' value='Smith'/>"
928 " Email: <INPUT type='text' id='email' value='john@example.com'/>"
929 " <INPUT type='submit' name='reply-send' value='Send'/>"
933 TEST_F(FormAutofillTest, ExtractMultipleForms) {
934 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
935 " <INPUT type='text' id='firstname' value='John'/>"
936 " <INPUT type='text' id='lastname' value='Smith'/>"
937 " <INPUT type='text' id='email' value='john@example.com'/>"
938 " <INPUT type='submit' name='reply-send' value='Send'/>"
940 "<FORM name='TestForm2' action='http://zoo.com' method='post'>"
941 " <INPUT type='text' id='firstname' value='Jack'/>"
942 " <INPUT type='text' id='lastname' value='Adams'/>"
943 " <INPUT type='text' id='email' value='jack@example.com'/>"
944 " <INPUT type='submit' name='reply-send' value='Send'/>"
947 WebFrame* web_frame = GetMainFrame();
948 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
950 FormCache form_cache;
951 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
952 ASSERT_EQ(2U, forms.size());
955 const FormData& form = forms[0];
956 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
957 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
958 EXPECT_EQ(GURL("http://cnn.com"), form.action);
960 const std::vector<FormFieldData>& fields = form.fields;
961 ASSERT_EQ(3U, fields.size());
963 FormFieldData expected;
964 expected.form_control_type = "text";
965 expected.max_length = WebInputElement::defaultMaxLength();
967 expected.name = ASCIIToUTF16("firstname");
968 expected.value = ASCIIToUTF16("John");
969 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
971 expected.name = ASCIIToUTF16("lastname");
972 expected.value = ASCIIToUTF16("Smith");
973 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
975 expected.name = ASCIIToUTF16("email");
976 expected.value = ASCIIToUTF16("john@example.com");
977 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
980 const FormData& form2 = forms[1];
981 EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2.name);
982 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
983 EXPECT_EQ(GURL("http://zoo.com"), form2.action);
985 const std::vector<FormFieldData>& fields2 = form2.fields;
986 ASSERT_EQ(3U, fields2.size());
988 expected.name = ASCIIToUTF16("firstname");
989 expected.value = ASCIIToUTF16("Jack");
990 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
992 expected.name = ASCIIToUTF16("lastname");
993 expected.value = ASCIIToUTF16("Adams");
994 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
996 expected.name = ASCIIToUTF16("email");
997 expected.value = ASCIIToUTF16("jack@example.com");
998 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1001 TEST_F(FormAutofillTest, OnlyExtractNewForms) {
1003 "<FORM id='testform' action='http://cnn.com' method='post'>"
1004 " <INPUT type='text' id='firstname' value='John'/>"
1005 " <INPUT type='text' id='lastname' value='Smith'/>"
1006 " <INPUT type='text' id='email' value='john@example.com'/>"
1007 " <INPUT type='submit' name='reply-send' value='Send'/>"
1010 WebFrame* web_frame = GetMainFrame();
1011 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1013 FormCache form_cache;
1014 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1015 ASSERT_EQ(1U, forms.size());
1017 // Second call should give nothing as there are no new forms.
1018 forms = form_cache.ExtractNewForms(*web_frame);
1019 ASSERT_TRUE(forms.empty());
1021 // Append to the current form will re-extract.
1023 "var newInput = document.createElement('input');"
1024 "newInput.setAttribute('type', 'text');"
1025 "newInput.setAttribute('id', 'telephone');"
1026 "newInput.value = '12345';"
1027 "document.getElementById('testform').appendChild(newInput);");
1028 msg_loop_.RunUntilIdle();
1030 forms = form_cache.ExtractNewForms(*web_frame);
1031 ASSERT_EQ(1U, forms.size());
1033 const std::vector<FormFieldData>& fields = forms[0].fields;
1034 ASSERT_EQ(4U, fields.size());
1036 FormFieldData expected;
1037 expected.form_control_type = "text";
1038 expected.max_length = WebInputElement::defaultMaxLength();
1040 expected.name = ASCIIToUTF16("firstname");
1041 expected.value = ASCIIToUTF16("John");
1042 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1044 expected.name = ASCIIToUTF16("lastname");
1045 expected.value = ASCIIToUTF16("Smith");
1046 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1048 expected.name = ASCIIToUTF16("email");
1049 expected.value = ASCIIToUTF16("john@example.com");
1050 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1052 expected.name = ASCIIToUTF16("telephone");
1053 expected.value = ASCIIToUTF16("12345");
1054 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1058 // Completely new form will also be extracted.
1060 "var newForm=document.createElement('form');"
1061 "newForm.id='new_testform';"
1062 "newForm.action='http://google.com';"
1063 "newForm.method='post';"
1064 "var newFirstname=document.createElement('input');"
1065 "newFirstname.setAttribute('type', 'text');"
1066 "newFirstname.setAttribute('id', 'second_firstname');"
1067 "newFirstname.value = 'Bob';"
1068 "var newLastname=document.createElement('input');"
1069 "newLastname.setAttribute('type', 'text');"
1070 "newLastname.setAttribute('id', 'second_lastname');"
1071 "newLastname.value = 'Hope';"
1072 "var newEmail=document.createElement('input');"
1073 "newEmail.setAttribute('type', 'text');"
1074 "newEmail.setAttribute('id', 'second_email');"
1075 "newEmail.value = 'bobhope@example.com';"
1076 "newForm.appendChild(newFirstname);"
1077 "newForm.appendChild(newLastname);"
1078 "newForm.appendChild(newEmail);"
1079 "document.body.appendChild(newForm);");
1080 msg_loop_.RunUntilIdle();
1082 web_frame = GetMainFrame();
1083 forms = form_cache.ExtractNewForms(*web_frame);
1084 ASSERT_EQ(1U, forms.size());
1086 const std::vector<FormFieldData>& fields2 = forms[0].fields;
1087 ASSERT_EQ(3U, fields2.size());
1089 expected.name = ASCIIToUTF16("second_firstname");
1090 expected.value = ASCIIToUTF16("Bob");
1091 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1093 expected.name = ASCIIToUTF16("second_lastname");
1094 expected.value = ASCIIToUTF16("Hope");
1095 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1097 expected.name = ASCIIToUTF16("second_email");
1098 expected.value = ASCIIToUTF16("bobhope@example.com");
1099 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1102 // We should not extract a form if it has too few fillable fields.
1103 TEST_F(FormAutofillTest, ExtractFormsTooFewFields) {
1104 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1105 " <INPUT type='text' id='firstname' value='John'/>"
1106 " <INPUT type='text' id='lastname' value='Smith'/>"
1107 " <INPUT type='submit' name='reply-send' value='Send'/>"
1110 WebFrame* web_frame = GetMainFrame();
1111 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1113 FormCache form_cache;
1114 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1115 ASSERT_TRUE(forms.empty());
1118 // We should not report additional forms for empty forms.
1119 TEST_F(FormAutofillTest, ExtractFormsSkippedForms) {
1120 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1121 " <INPUT type='text' id='firstname' value='John'/>"
1122 " <INPUT type='text' id='lastname' value='Smith'/>"
1125 WebFrame* web_frame = GetMainFrame();
1126 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1128 FormCache form_cache;
1129 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1130 ASSERT_TRUE(forms.empty());
1133 // We should not report additional forms for empty forms.
1134 TEST_F(FormAutofillTest, ExtractFormsNoFields) {
1135 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1138 WebFrame* web_frame = GetMainFrame();
1139 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1141 FormCache form_cache;
1142 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1143 ASSERT_TRUE(forms.empty());
1146 // We should not extract a form if it has too few fillable fields.
1147 // Make sure radio and checkbox fields don't count.
1148 TEST_F(FormAutofillTest, ExtractFormsTooFewFieldsSkipsCheckable) {
1149 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1150 " <INPUT type='text' id='firstname' value='John'/>"
1151 " <INPUT type='text' id='lastname' value='Smith'/>"
1152 " <INPUT type='radio' id='a_radio' value='0'/>"
1153 " <INPUT type='checkbox' id='a_check' value='1'/>"
1154 " <INPUT type='submit' name='reply-send' value='Send'/>"
1157 WebFrame* web_frame = GetMainFrame();
1158 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1160 FormCache form_cache;
1161 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1162 ASSERT_TRUE(forms.empty());
1165 TEST_F(FormAutofillTest, WebFormElementToFormDataAutocomplete) {
1167 // Form is not auto-completable due to autocomplete=off.
1168 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'"
1169 " autocomplete=off>"
1170 " <INPUT type='text' id='firstname' value='John'/>"
1171 " <INPUT type='text' id='lastname' value='Smith'/>"
1172 " <INPUT type='text' id='email' value='john@example.com'/>"
1173 " <INPUT type='submit' name='reply-send' value='Send'/>"
1176 WebFrame* web_frame = GetMainFrame();
1177 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1179 WebVector<WebFormElement> web_forms;
1180 web_frame->document().forms(web_forms);
1181 ASSERT_EQ(1U, web_forms.size());
1182 WebFormElement web_form = web_forms[0];
1185 EXPECT_TRUE(WebFormElementToFormData(
1186 web_form, WebFormControlElement(), autofill::REQUIRE_NONE,
1187 autofill::EXTRACT_NONE, &form, NULL));
1188 EXPECT_FALSE(WebFormElementToFormData(
1189 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
1190 autofill::EXTRACT_NONE, &form, NULL));
1194 // The firstname element is not auto-completable due to autocomplete=off.
1195 LoadHTML("<FORM name='TestForm' action='http://abc.com' "
1197 " <INPUT type='text' id='firstname' value='John'"
1198 " autocomplete=off>"
1199 " <INPUT type='text' id='middlename' value='Jack'/>"
1200 " <INPUT type='text' id='lastname' value='Smith'/>"
1201 " <INPUT type='text' id='email' value='john@example.com'/>"
1202 " <INPUT type='submit' name='reply' value='Send'/>"
1205 WebFrame* web_frame = GetMainFrame();
1206 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1208 WebVector<WebFormElement> web_forms;
1209 web_frame->document().forms(web_forms);
1210 ASSERT_EQ(1U, web_forms.size());
1211 WebFormElement web_form = web_forms[0];
1214 EXPECT_TRUE(WebFormElementToFormData(
1215 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
1216 autofill::EXTRACT_VALUE, &form, NULL));
1218 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1219 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1220 EXPECT_EQ(GURL("http://abc.com"), form.action);
1222 const std::vector<FormFieldData>& fields = form.fields;
1223 ASSERT_EQ(3U, fields.size());
1225 FormFieldData expected;
1226 expected.form_control_type = "text";
1227 expected.max_length = WebInputElement::defaultMaxLength();
1229 expected.name = ASCIIToUTF16("middlename");
1230 expected.value = ASCIIToUTF16("Jack");
1231 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1233 expected.name = ASCIIToUTF16("lastname");
1234 expected.value = ASCIIToUTF16("Smith");
1235 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1237 expected.name = ASCIIToUTF16("email");
1238 expected.value = ASCIIToUTF16("john@example.com");
1239 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1243 TEST_F(FormAutofillTest, FindFormForInputElement) {
1244 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
1245 " <INPUT type='text' id='firstname' value='John'/>"
1246 " <INPUT type='text' id='lastname' value='Smith'/>"
1247 " <INPUT type='text' id='email' value='john@example.com'"
1248 "autocomplete='off' />"
1249 " <INPUT type='text' id='phone' value='1.800.555.1234'/>"
1250 " <INPUT type='submit' name='reply-send' value='Send'/>"
1253 WebFrame* web_frame = GetMainFrame();
1254 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1256 FormCache form_cache;
1257 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1258 ASSERT_EQ(1U, forms.size());
1260 // Get the input element we want to find.
1261 WebElement element = web_frame->document().getElementById("firstname");
1262 WebInputElement input_element = element.to<WebInputElement>();
1264 // Find the form and verify it's the correct form.
1266 FormFieldData field;
1267 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
1270 autofill::REQUIRE_NONE));
1271 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1272 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1273 EXPECT_EQ(GURL("http://buh.com"), form.action);
1275 const std::vector<FormFieldData>& fields = form.fields;
1276 ASSERT_EQ(4U, fields.size());
1278 FormFieldData expected;
1279 expected.form_control_type = "text";
1280 expected.max_length = WebInputElement::defaultMaxLength();
1282 expected.name = ASCIIToUTF16("firstname");
1283 expected.value = ASCIIToUTF16("John");
1284 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1285 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1287 expected.name = ASCIIToUTF16("lastname");
1288 expected.value = ASCIIToUTF16("Smith");
1289 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1291 expected.name = ASCIIToUTF16("email");
1292 expected.value = ASCIIToUTF16("john@example.com");
1293 expected.autocomplete_attribute = "off";
1294 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1295 expected.autocomplete_attribute = std::string(); // reset
1297 expected.name = ASCIIToUTF16("phone");
1298 expected.value = ASCIIToUTF16("1.800.555.1234");
1299 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1301 // Try again, but require autocomplete.
1303 FormFieldData field2;
1304 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1308 autofill::REQUIRE_AUTOCOMPLETE));
1309 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1310 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1311 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1313 const std::vector<FormFieldData>& fields2 = form2.fields;
1314 ASSERT_EQ(3U, fields2.size());
1316 expected.form_control_type = "text";
1317 expected.max_length = WebInputElement::defaultMaxLength();
1319 expected.name = ASCIIToUTF16("firstname");
1320 expected.value = ASCIIToUTF16("John");
1321 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1322 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1324 expected.name = ASCIIToUTF16("lastname");
1325 expected.value = ASCIIToUTF16("Smith");
1326 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1328 expected.name = ASCIIToUTF16("phone");
1329 expected.value = ASCIIToUTF16("1.800.555.1234");
1330 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1333 TEST_F(FormAutofillTest, FindFormForTextAreaElement) {
1334 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
1335 " <INPUT type='text' id='firstname' value='John'/>"
1336 " <INPUT type='text' id='lastname' value='Smith'/>"
1337 " <INPUT type='text' id='email' value='john@example.com'"
1338 "autocomplete='off' />"
1339 " <TEXTAREA id='street-address'>"
1340 "123 Fantasy Ln. "
1343 " <INPUT type='submit' name='reply-send' value='Send'/>"
1346 WebFrame* web_frame = GetMainFrame();
1347 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1349 FormCache form_cache;
1350 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1351 ASSERT_EQ(1U, forms.size());
1353 // Get the textarea element we want to find.
1354 WebElement element = web_frame->document().getElementById("street-address");
1355 WebTextAreaElement textarea_element = element.to<WebTextAreaElement>();
1357 // Find the form and verify it's the correct form.
1359 FormFieldData field;
1360 EXPECT_TRUE(FindFormAndFieldForFormControlElement(textarea_element,
1363 autofill::REQUIRE_NONE));
1364 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1365 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1366 EXPECT_EQ(GURL("http://buh.com"), form.action);
1368 const std::vector<FormFieldData>& fields = form.fields;
1369 ASSERT_EQ(4U, fields.size());
1371 FormFieldData expected;
1373 expected.name = ASCIIToUTF16("firstname");
1374 expected.value = ASCIIToUTF16("John");
1375 expected.form_control_type = "text";
1376 expected.max_length = WebInputElement::defaultMaxLength();
1377 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1379 expected.name = ASCIIToUTF16("lastname");
1380 expected.value = ASCIIToUTF16("Smith");
1381 expected.form_control_type = "text";
1382 expected.max_length = WebInputElement::defaultMaxLength();
1383 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1385 expected.name = ASCIIToUTF16("email");
1386 expected.value = ASCIIToUTF16("john@example.com");
1387 expected.autocomplete_attribute = "off";
1388 expected.form_control_type = "text";
1389 expected.max_length = WebInputElement::defaultMaxLength();
1390 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1391 expected.autocomplete_attribute = std::string(); // reset
1393 expected.name = ASCIIToUTF16("street-address");
1394 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1395 expected.form_control_type = "textarea";
1396 expected.max_length = 0;
1397 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1398 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1400 // Try again, but require autocomplete.
1402 FormFieldData field2;
1403 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1407 autofill::REQUIRE_AUTOCOMPLETE));
1408 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1409 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1410 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1412 const std::vector<FormFieldData>& fields2 = form2.fields;
1413 ASSERT_EQ(3U, fields2.size());
1415 expected.name = ASCIIToUTF16("firstname");
1416 expected.value = ASCIIToUTF16("John");
1417 expected.form_control_type = "text";
1418 expected.max_length = WebInputElement::defaultMaxLength();
1419 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1421 expected.name = ASCIIToUTF16("lastname");
1422 expected.value = ASCIIToUTF16("Smith");
1423 expected.form_control_type = "text";
1424 expected.max_length = WebInputElement::defaultMaxLength();
1425 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1427 expected.name = ASCIIToUTF16("street-address");
1428 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1429 expected.form_control_type = "textarea";
1430 expected.max_length = 0;
1431 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1432 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1435 // Test regular FillForm function.
1436 TEST_F(FormAutofillTest, FillForm) {
1437 static const AutofillFieldCase field_cases[] = {
1438 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1439 // should_be_autofilled, autofill_value, expected_value
1441 // Regular empty fields (firstname & lastname) should be autofilled.
1442 {"text", "firstname", "", "", true, "filled firstname",
1443 "filled firstname"},
1444 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1445 // hidden fields should not be extracted to form_data.
1446 // Non empty fields should not be autofilled.
1447 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
1448 // "noautocomplete" should not be extracted to form_data.
1449 // Disabled fields should not be autofilled.
1450 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1451 // Readonly fields should not be autofilled.
1452 {"text", "readonly", "", "", false, "filled readonly", ""},
1453 // Fields with "visibility: hidden" should not be autofilled.
1454 {"text", "invisible", "", "", false, "filled invisible", ""},
1455 // Fields with "display:none" should not be autofilled.
1456 {"text", "displaynone", "", "", false, "filled displaynone", ""},
1457 // Regular <input type="month"> should be autofilled.
1458 {"month", "month", "", "", true, "2017-11", "2017-11"},
1459 // Non-empty <input type="month"> should not be autofilled.
1460 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
1461 // Regular select fields should be autofilled.
1462 {"select-one", "select", "", "", true, "TX", "TX"},
1463 // Select fields should be autofilled even if they already have a
1465 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1466 // Select fields should not be autofilled if no new value is passed from
1467 // autofill profile. The existing value should not be overriden.
1468 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
1469 // Regular textarea elements should be autofilled.
1470 {"textarea", "textarea", "", "", true, "some multi-\nline value",
1471 "some multi-\nline value"},
1472 // Non-empty textarea elements should not be autofilled.
1473 {"textarea", "textarea-nonempty", "Go\naway!", "", false,
1474 "some multi-\nline value", "Go\naway!"},
1476 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1477 FillForm, &GetValueWrapper);
1478 // Verify preview selection.
1479 WebInputElement firstname = GetMainFrame()->document().
1480 getElementById("firstname").to<WebInputElement>();
1481 EXPECT_EQ(16, firstname.selectionStart());
1482 EXPECT_EQ(16, firstname.selectionEnd());
1485 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) {
1486 static const AutofillFieldCase field_cases[] = {
1487 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1488 // should_be_autofilled, autofill_value, expected_value
1490 // Regular empty fields (firstname & lastname) should be autofilled.
1491 {"text", "firstname", "", "", true, "filled firstname",
1492 "filled firstname"},
1493 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1494 // hidden fields should not be extracted to form_data.
1495 // Non empty fields should be overriden.
1496 {"text", "notempty", "Hi", "", true, "filled notempty",
1498 // "noautocomplete" should not be extracted to form_data.
1499 // Disabled fields should not be autofilled.
1500 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1501 // Readonly fields should not be autofilled.
1502 {"text", "readonly", "", "", false, "filled readonly", ""},
1503 // Fields with "visibility: hidden" should also be autofilled.
1504 {"text", "invisible", "", "", true, "filled invisible",
1505 "filled invisible"},
1506 // Fields with "display:none" should also be autofilled.
1507 {"text", "displaynone", "", "", true, "filled displaynone",
1508 "filled displaynone"},
1509 // Regular <input type="month"> should be autofilled.
1510 {"month", "month", "", "", true, "2017-11", "2017-11"},
1511 // Non-empty <input type="month"> should be overridden.
1512 {"month", "month-nonempty", "2011-12", "", true, "2017-11", "2017-11"},
1513 // Regular select fields should be autofilled.
1514 {"select-one", "select", "", "", true, "TX", "TX"},
1515 // Select fields should be autofilled even if they already have a
1517 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1518 // Select fields should not be autofilled if no new value is passed from
1519 // autofill profile. The existing value should not be overriden.
1520 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
1521 // Regular textarea elements should be autofilled.
1522 {"textarea", "textarea", "", "", true, "some multi-\nline value",
1523 "some multi-\nline value"},
1524 // Nonempty textarea elements should be overridden.
1525 {"textarea", "textarea-nonempty", "Go\naway!", "", true,
1526 "some multi-\nline value", "some multi-\nline value"},
1528 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1529 &FillFormIncludingNonFocusableElementsWrapper,
1533 TEST_F(FormAutofillTest, PreviewForm) {
1534 static const AutofillFieldCase field_cases[] = {
1535 // Normal empty fields should be previewed.
1536 {"text", "firstname", "", "", true, "suggested firstname",
1537 "suggested firstname"},
1538 {"text", "lastname", "", "", true, "suggested lastname",
1539 "suggested lastname"},
1540 // Hidden fields should not be extracted to form_data.
1541 // Non empty fields should not be previewed.
1542 {"text", "notempty", "Hi", "", false, "suggested notempty", ""},
1543 // "noautocomplete" should not be extracted to form_data.
1544 // Disabled fields should not be previewed.
1545 {"text", "notenabled", "", "", false, "suggested notenabled", ""},
1546 // Readonly fields should not be previewed.
1547 {"text", "readonly", "", "", false, "suggested readonly", ""},
1548 // Fields with "visibility: hidden" should not be previewed.
1549 {"text", "invisible", "", "", false, "suggested invisible",
1551 // Fields with "display:none" should not previewed.
1552 {"text", "displaynone", "", "", false, "suggested displaynone",
1554 // Regular <input type="month"> should be previewed.
1555 {"month", "month", "", "", true, "2017-11", "2017-11"},
1556 // Non-empty <input type="month"> should not be previewed.
1557 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
1558 // Regular select fields should be previewed.
1559 {"select-one", "select", "", "", true, "TX", "TX"},
1560 // Select fields should be previewed even if they already have a
1562 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1563 // Select fields should not be previewed if no suggestion is passed from
1564 // autofill profile.
1565 {"select-one", "select-unchanged", "CA", "", false, "", ""},
1566 // Normal textarea elements should be previewed.
1567 {"textarea", "textarea", "", "", true, "suggested multi-\nline value",
1568 "suggested multi-\nline value"},
1569 // Nonempty textarea elements should not be previewed.
1570 {"textarea", "textarea-nonempty", "Go\naway!", "", false,
1571 "suggested multi-\nline value", ""},
1573 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1574 &PreviewForm, &GetSuggestedValueWrapper);
1576 // Verify preview selection.
1577 WebInputElement firstname = GetMainFrame()->document().
1578 getElementById("firstname").to<WebInputElement>();
1579 EXPECT_EQ(0, firstname.selectionStart());
1580 EXPECT_EQ(19, firstname.selectionEnd());
1583 TEST_F(FormAutofillTest, Labels) {
1584 ExpectJohnSmithLabels(
1585 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1586 " <LABEL for='firstname'> First name: </LABEL>"
1587 " <INPUT type='text' id='firstname' value='John'/>"
1588 " <LABEL for='lastname'> Last name: </LABEL>"
1589 " <INPUT type='text' id='lastname' value='Smith'/>"
1590 " <LABEL for='email'> Email: </LABEL>"
1591 " <INPUT type='text' id='email' value='john@example.com'/>"
1592 " <INPUT type='submit' name='reply-send' value='Send'/>"
1596 TEST_F(FormAutofillTest, LabelsWithSpans) {
1597 ExpectJohnSmithLabels(
1598 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1599 " <LABEL for='firstname'><span>First name: </span></LABEL>"
1600 " <INPUT type='text' id='firstname' value='John'/>"
1601 " <LABEL for='lastname'><span>Last name: </span></LABEL>"
1602 " <INPUT type='text' id='lastname' value='Smith'/>"
1603 " <LABEL for='email'><span>Email: </span></LABEL>"
1604 " <INPUT type='text' id='email' value='john@example.com'/>"
1605 " <INPUT type='submit' name='reply-send' value='Send'/>"
1609 // This test is different from FormAutofillTest.Labels in that the label
1610 // elements for= attribute is set to the name of the form control element it is
1611 // a label for instead of the id of the form control element. This is invalid
1612 // because the for= attribute must be set to the id of the form control element;
1613 // however, current label parsing code will extract the text from the previous
1614 // label element and apply it to the following input field.
1615 TEST_F(FormAutofillTest, InvalidLabels) {
1616 ExpectJohnSmithLabels(
1617 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1618 " <LABEL for='firstname'> First name: </LABEL>"
1619 " <INPUT type='text' name='firstname' value='John'/>"
1620 " <LABEL for='lastname'> Last name: </LABEL>"
1621 " <INPUT type='text' name='lastname' value='Smith'/>"
1622 " <LABEL for='email'> Email: </LABEL>"
1623 " <INPUT type='text' name='email' value='john@example.com'/>"
1624 " <INPUT type='submit' name='reply-send' value='Send'/>"
1628 // This test has three form control elements, only one of which has a label
1629 // element associated with it.
1630 TEST_F(FormAutofillTest, OneLabelElement) {
1631 ExpectJohnSmithLabels(
1632 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1634 " <INPUT type='text' id='firstname' value='John'/>"
1635 " <LABEL for='lastname'>Last name: </LABEL>"
1636 " <INPUT type='text' id='lastname' value='Smith'/>"
1638 " <INPUT type='text' id='email' value='john@example.com'/>"
1639 " <INPUT type='submit' name='reply-send' value='Send'/>"
1643 TEST_F(FormAutofillTest, LabelsInferredFromText) {
1644 ExpectJohnSmithLabels(
1645 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1647 " <INPUT type='text' id='firstname' value='John'/>"
1649 " <INPUT type='text' id='lastname' value='Smith'/>"
1651 " <INPUT type='text' id='email' value='john@example.com'/>"
1652 " <INPUT type='submit' name='reply-send' value='Send'/>"
1656 TEST_F(FormAutofillTest, LabelsInferredFromParagraph) {
1657 ExpectJohnSmithLabels(
1658 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1659 " <P>First name:</P><INPUT type='text' "
1660 " id='firstname' value='John'/>"
1661 " <P>Last name:</P>"
1662 " <INPUT type='text' id='lastname' value='Smith'/>"
1664 " <INPUT type='text' id='email' value='john@example.com'/>"
1665 " <INPUT type='submit' name='reply-send' value='Send'/>"
1669 TEST_F(FormAutofillTest, LabelsInferredFromBold) {
1670 ExpectJohnSmithLabels(
1671 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1672 " <B>First name:</B><INPUT type='text' "
1673 " id='firstname' value='John'/>"
1674 " <B>Last name:</B>"
1675 " <INPUT type='text' id='lastname' value='Smith'/>"
1677 " <INPUT type='text' id='email' value='john@example.com'/>"
1678 " <INPUT type='submit' name='reply-send' value='Send'/>"
1682 TEST_F(FormAutofillTest, LabelsInferredPriorToImgOrBr) {
1683 ExpectJohnSmithLabels(
1684 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1685 " First name:<IMG/><INPUT type='text' "
1686 " id='firstname' value='John'/>"
1688 " <INPUT type='text' id='lastname' value='Smith'/>"
1690 " <INPUT type='text' id='email' value='john@example.com'/>"
1691 " <INPUT type='submit' name='reply-send' value='Send'/>"
1695 TEST_F(FormAutofillTest, LabelsInferredFromTableCell) {
1696 ExpectJohnSmithLabels(
1697 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1700 " <TD>First name:</TD>"
1701 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
1704 " <TD>Last name:</TD>"
1705 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
1709 " <TD><INPUT type='text' id='email'"
1710 " value='john@example.com'/></TD>"
1715 " <INPUT type='submit' name='reply-send' value='Send'/>"
1722 TEST_F(FormAutofillTest, LabelsInferredFromTableCellTH) {
1723 ExpectJohnSmithLabels(
1724 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1727 " <TH>First name:</TH>"
1728 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
1731 " <TH>Last name:</TH>"
1732 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
1736 " <TD><INPUT type='text' id='email'"
1737 " value='john@example.com'/></TD>"
1742 " <INPUT type='submit' name='reply-send' value='Send'/>"
1749 TEST_F(FormAutofillTest, LabelsInferredFromTableCellNested) {
1750 std::vector<base::string16> labels, names, values;
1752 labels.push_back(ASCIIToUTF16("First name: Bogus"));
1753 names.push_back(ASCIIToUTF16("firstname"));
1754 values.push_back(ASCIIToUTF16("John"));
1756 labels.push_back(ASCIIToUTF16("Last name:"));
1757 names.push_back(ASCIIToUTF16("lastname"));
1758 values.push_back(ASCIIToUTF16("Smith"));
1760 labels.push_back(ASCIIToUTF16("Email:"));
1761 names.push_back(ASCIIToUTF16("email"));
1762 values.push_back(ASCIIToUTF16("john@example.com"));
1765 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1778 " <INPUT type='text' id='firstname' value='John'/>"
1790 " <INPUT type='text' id='lastname' value='Smith'/>"
1802 " <INPUT type='text' id='email' value='john@example.com'/>"
1809 " <INPUT type='submit' name='reply-send' value='Send'/>"
1814 labels, names, values);
1817 TEST_F(FormAutofillTest, LabelsInferredFromTableEmptyTDs) {
1818 std::vector<base::string16> labels, names, values;
1820 labels.push_back(ASCIIToUTF16("* First Name"));
1821 names.push_back(ASCIIToUTF16("firstname"));
1822 values.push_back(ASCIIToUTF16("John"));
1824 labels.push_back(ASCIIToUTF16("* Last Name"));
1825 names.push_back(ASCIIToUTF16("lastname"));
1826 values.push_back(ASCIIToUTF16("Smith"));
1828 labels.push_back(ASCIIToUTF16("* Email"));
1829 names.push_back(ASCIIToUTF16("email"));
1830 values.push_back(ASCIIToUTF16("john@example.com"));
1833 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1838 " <B>First Name</B>"
1842 " <INPUT type='text' id='firstname' value='John'/>"
1852 " <INPUT type='text' id='lastname' value='Smith'/>"
1862 " <INPUT type='text' id='email' value='john@example.com'/>"
1868 " <INPUT type='submit' name='reply-send' value='Send'/>"
1873 labels, names, values);
1876 TEST_F(FormAutofillTest, LabelsInferredFromPreviousTD) {
1877 std::vector<base::string16> labels, names, values;
1879 labels.push_back(ASCIIToUTF16("* First Name"));
1880 names.push_back(ASCIIToUTF16("firstname"));
1881 values.push_back(ASCIIToUTF16("John"));
1883 labels.push_back(ASCIIToUTF16("* Last Name"));
1884 names.push_back(ASCIIToUTF16("lastname"));
1885 values.push_back(ASCIIToUTF16("Smith"));
1887 labels.push_back(ASCIIToUTF16("* Email"));
1888 names.push_back(ASCIIToUTF16("email"));
1889 values.push_back(ASCIIToUTF16("john@example.com"));
1892 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1895 " <TD>* First Name</TD>"
1898 " <INPUT type='hidden'/>"
1899 " <INPUT type='text' id='firstname' value='John'/>"
1903 " <TD>* Last Name</TD>"
1905 " <INPUT type='text' id='lastname' value='Smith'/>"
1911 " <INPUT type='text' id='email' value='john@example.com'/>"
1917 " <INPUT type='submit' name='reply-send' value='Send'/>"
1922 labels, names, values);
1925 // <script>, <noscript> and <option> tags are excluded when the labels are
1927 // Also <!-- comment --> is excluded.
1928 TEST_F(FormAutofillTest, LabelsInferredFromTableWithSpecialElements) {
1929 std::vector<base::string16> labels, names, values;
1930 std::vector<std::string> control_types;
1932 labels.push_back(ASCIIToUTF16("* First Name"));
1933 names.push_back(ASCIIToUTF16("firstname"));
1934 values.push_back(ASCIIToUTF16("John"));
1935 control_types.push_back("text");
1937 labels.push_back(ASCIIToUTF16("* Middle Name"));
1938 names.push_back(ASCIIToUTF16("middlename"));
1939 values.push_back(ASCIIToUTF16("Joe"));
1940 control_types.push_back("text");
1942 labels.push_back(ASCIIToUTF16("* Last Name"));
1943 names.push_back(ASCIIToUTF16("lastname"));
1944 values.push_back(ASCIIToUTF16("Smith"));
1945 control_types.push_back("text");
1947 labels.push_back(ASCIIToUTF16("* Country"));
1948 names.push_back(ASCIIToUTF16("country"));
1949 values.push_back(ASCIIToUTF16("US"));
1950 control_types.push_back("select-one");
1952 labels.push_back(ASCIIToUTF16("* Email"));
1953 names.push_back(ASCIIToUTF16("email"));
1954 values.push_back(ASCIIToUTF16("john@example.com"));
1955 control_types.push_back("text");
1957 ExpectLabelsAndTypes(
1958 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1963 " <B>First Name</B>"
1966 " <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
1968 " <INPUT type='text' id='firstname' value='John'/>"
1974 " <B>Middle Name</B>"
1980 " <INPUT type='text' id='middlename' value='Joe'/>"
1989 " <INPUT type='text' id='lastname' value='Smith'/>"
1998 " <SELECT id='country'>"
1999 " <OPTION VALUE='US'>The value should be ignored as label."
2001 " <OPTION VALUE='JP'>JAPAN</OPTION>"
2011 " <!-- This comment should be ignored as inferred label.-->"
2012 " <INPUT type='text' id='email' value='john@example.com'/>"
2018 " <INPUT type='submit' name='reply-send' value='Send'/>"
2023 labels, names, values, control_types);
2026 TEST_F(FormAutofillTest, LabelsInferredFromTableLabels) {
2027 ExpectJohnSmithLabels(
2028 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2032 " <LABEL>First name:</LABEL>"
2033 " <INPUT type='text' id='firstname' value='John'/>"
2038 " <LABEL>Last name:</LABEL>"
2039 " <INPUT type='text' id='lastname' value='Smith'/>"
2044 " <LABEL>Email:</LABEL>"
2045 " <INPUT type='text' id='email' value='john@example.com'/>"
2049 "<INPUT type='submit' name='reply-send' value='Send'/>"
2053 TEST_F(FormAutofillTest, LabelsInferredFromTableTDInterveningElements) {
2054 ExpectJohnSmithLabels(
2055 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2061 " <INPUT type='text' id='firstname' value='John'/>"
2068 " <INPUT type='text' id='lastname' value='Smith'/>"
2075 " <INPUT type='text' id='email' value='john@example.com'/>"
2079 "<INPUT type='submit' name='reply-send' value='Send'/>"
2083 // Verify that we correctly infer labels when the label text spans multiple
2084 // adjacent HTML elements, not separated by whitespace.
2085 TEST_F(FormAutofillTest, LabelsInferredFromTableAdjacentElements) {
2086 std::vector<base::string16> labels, names, values;
2088 labels.push_back(ASCIIToUTF16("*First Name"));
2089 names.push_back(ASCIIToUTF16("firstname"));
2090 values.push_back(ASCIIToUTF16("John"));
2092 labels.push_back(ASCIIToUTF16("*Last Name"));
2093 names.push_back(ASCIIToUTF16("lastname"));
2094 values.push_back(ASCIIToUTF16("Smith"));
2096 labels.push_back(ASCIIToUTF16("*Email"));
2097 names.push_back(ASCIIToUTF16("email"));
2098 values.push_back(ASCIIToUTF16("john@example.com"));
2101 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2105 " <SPAN>*</SPAN><B>First Name</B>"
2108 " <INPUT type='text' id='firstname' value='John'/>"
2113 " <SPAN>*</SPAN><B>Last Name</B>"
2116 " <INPUT type='text' id='lastname' value='Smith'/>"
2121 " <SPAN>*</SPAN><B>Email</B>"
2124 " <INPUT type='text' id='email' value='john@example.com'/>"
2129 " <INPUT type='submit' name='reply-send' value='Send'/>"
2134 labels, names, values);
2137 // Verify that we correctly infer labels when the label text resides in the
2139 TEST_F(FormAutofillTest, LabelsInferredFromTableRow) {
2140 std::vector<base::string16> labels, names, values;
2142 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2143 names.push_back(ASCIIToUTF16("firstname"));
2144 values.push_back(ASCIIToUTF16("John"));
2146 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2147 names.push_back(ASCIIToUTF16("lastname"));
2148 values.push_back(ASCIIToUTF16("Smith"));
2150 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2151 names.push_back(ASCIIToUTF16("email"));
2152 values.push_back(ASCIIToUTF16("john@example.com"));
2155 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2158 " <TD>*First Name</TD>"
2159 " <TD>*Last Name</TD>"
2164 " <INPUT type='text' id='firstname' value='John'/>"
2167 " <INPUT type='text' id='lastname' value='Smith'/>"
2170 " <INPUT type='text' id='email' value='john@example.com'/>"
2175 " <INPUT type='submit' name='reply-send' value='Send'/>"
2179 labels, names, values);
2182 // Verify that we correctly infer labels when enclosed within a list item.
2183 TEST_F(FormAutofillTest, LabelsInferredFromListItem) {
2184 std::vector<base::string16> labels, names, values;
2186 labels.push_back(ASCIIToUTF16("* Home Phone"));
2187 names.push_back(ASCIIToUTF16("areacode"));
2188 values.push_back(ASCIIToUTF16("415"));
2190 labels.push_back(ASCIIToUTF16("* Home Phone"));
2191 names.push_back(ASCIIToUTF16("prefix"));
2192 values.push_back(ASCIIToUTF16("555"));
2194 labels.push_back(ASCIIToUTF16("* Home Phone"));
2195 names.push_back(ASCIIToUTF16("suffix"));
2196 values.push_back(ASCIIToUTF16("1212"));
2199 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2202 " <SPAN>Bogus</SPAN>"
2205 " <LABEL><EM>*</EM> Home Phone</LABEL>"
2206 " <INPUT type='text' id='areacode' value='415'/>"
2207 " <INPUT type='text' id='prefix' value='555'/>"
2208 " <INPUT type='text' id='suffix' value='1212'/>"
2211 " <INPUT type='submit' name='reply-send' value='Send'/>"
2215 labels, names, values);
2218 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionList) {
2219 std::vector<base::string16> labels, names, values;
2221 labels.push_back(ASCIIToUTF16("* First name: Bogus"));
2222 names.push_back(ASCIIToUTF16("firstname"));
2223 values.push_back(ASCIIToUTF16("John"));
2225 labels.push_back(ASCIIToUTF16("Last name:"));
2226 names.push_back(ASCIIToUTF16("lastname"));
2227 values.push_back(ASCIIToUTF16("Smith"));
2229 labels.push_back(ASCIIToUTF16("Email:"));
2230 names.push_back(ASCIIToUTF16("email"));
2231 values.push_back(ASCIIToUTF16("john@example.com"));
2234 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2249 " <INPUT type='text' id='firstname' value='John'/>"
2259 " <INPUT type='text' id='lastname' value='Smith'/>"
2269 " <INPUT type='text' id='email' value='john@example.com'/>"
2274 " <INPUT type='submit' name='reply-send' value='Send'/>"
2278 labels, names, values);
2281 TEST_F(FormAutofillTest, LabelsInferredWithSameName) {
2282 std::vector<base::string16> labels, names, values;
2284 labels.push_back(ASCIIToUTF16("Address Line 1:"));
2285 names.push_back(ASCIIToUTF16("Address"));
2286 values.push_back(base::string16());
2288 labels.push_back(ASCIIToUTF16("Address Line 2:"));
2289 names.push_back(ASCIIToUTF16("Address"));
2290 values.push_back(base::string16());
2292 labels.push_back(ASCIIToUTF16("Address Line 3:"));
2293 names.push_back(ASCIIToUTF16("Address"));
2294 values.push_back(base::string16());
2297 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2299 " <INPUT type='text' name='Address'/>"
2301 " <INPUT type='text' name='Address'/>"
2303 " <INPUT type='text' name='Address'/>"
2304 " <INPUT type='submit' name='reply-send' value='Send'/>"
2306 labels, names, values);
2309 TEST_F(FormAutofillTest, LabelsInferredWithImageTags) {
2310 std::vector<base::string16> labels, names, values;
2312 labels.push_back(ASCIIToUTF16("Phone:"));
2313 names.push_back(ASCIIToUTF16("dayphone1"));
2314 values.push_back(base::string16());
2316 labels.push_back(ASCIIToUTF16("-"));
2317 names.push_back(ASCIIToUTF16("dayphone2"));
2318 values.push_back(base::string16());
2320 labels.push_back(ASCIIToUTF16("-"));
2321 names.push_back(ASCIIToUTF16("dayphone3"));
2322 values.push_back(base::string16());
2324 labels.push_back(ASCIIToUTF16("ext.:"));
2325 names.push_back(ASCIIToUTF16("dayphone4"));
2326 values.push_back(base::string16());
2328 labels.push_back(base::string16());
2329 names.push_back(ASCIIToUTF16("dummy"));
2330 values.push_back(base::string16());
2333 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2335 " <input type='text' name='dayphone1'>"
2339 " <input type='text' name='dayphone2'>"
2343 " <input type='text' name='dayphone3'>"
2345 " <input type='text' name='dayphone4'>"
2346 " <input type='text' name='dummy'>"
2347 " <input type='submit' name='reply-send' value='Send'>"
2349 labels, names, values);
2352 TEST_F(FormAutofillTest, LabelsInferredFromDivTable) {
2353 ExpectJohnSmithLabels(
2354 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2355 "<DIV>First name:<BR>"
2357 " <INPUT type='text' name='firstname' value='John'>"
2360 "<DIV>Last name:<BR>"
2362 " <INPUT type='text' name='lastname' value='Smith'>"
2367 " <INPUT type='text' name='email' value='john@example.com'>"
2370 "<input type='submit' name='reply-send' value='Send'>"
2374 TEST_F(FormAutofillTest, LabelsInferredFromDivSiblingTable) {
2375 ExpectJohnSmithLabels(
2376 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2377 "<DIV>First name:</DIV>"
2380 " <INPUT type='text' name='firstname' value='John'>"
2383 "<DIV>Last name:</DIV>"
2386 " <INPUT type='text' name='lastname' value='Smith'>"
2392 " <INPUT type='text' name='email' value='john@example.com'>"
2395 "<input type='submit' name='reply-send' value='Send'>"
2399 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionListRatherThanDivTable) {
2400 ExpectJohnSmithLabels(
2401 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2402 "<DIV>This is not a label.<BR>"
2411 " <INPUT type='text' id='firstname' value='John'/>"
2421 " <INPUT type='text' id='lastname' value='Smith'/>"
2431 " <INPUT type='text' id='email' value='john@example.com'/>"
2436 " <INPUT type='submit' name='reply-send' value='Send'/>"
2443 TEST_F(FormAutofillTest, FillFormMaxLength) {
2444 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2445 " <INPUT type='text' id='firstname' maxlength='5'/>"
2446 " <INPUT type='text' id='lastname' maxlength='7'/>"
2447 " <INPUT type='text' id='email' maxlength='9'/>"
2448 " <INPUT type='submit' name='reply-send' value='Send'/>"
2451 WebFrame* web_frame = GetMainFrame();
2452 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2454 FormCache form_cache;
2455 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2456 ASSERT_EQ(1U, forms.size());
2458 // Get the input element we want to find.
2459 WebElement element = web_frame->document().getElementById("firstname");
2460 WebInputElement input_element = element.to<WebInputElement>();
2462 // Find the form that contains the input element.
2464 FormFieldData field;
2465 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2468 autofill::REQUIRE_NONE));
2469 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2470 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2471 EXPECT_EQ(GURL("http://buh.com"), form.action);
2473 const std::vector<FormFieldData>& fields = form.fields;
2474 ASSERT_EQ(3U, fields.size());
2476 FormFieldData expected;
2477 expected.form_control_type = "text";
2479 expected.name = ASCIIToUTF16("firstname");
2480 expected.max_length = 5;
2481 expected.is_autofilled = false;
2482 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2484 expected.name = ASCIIToUTF16("lastname");
2485 expected.max_length = 7;
2486 expected.is_autofilled = false;
2487 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2489 expected.name = ASCIIToUTF16("email");
2490 expected.max_length = 9;
2491 expected.is_autofilled = false;
2492 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2495 form.fields[0].value = ASCIIToUTF16("Brother");
2496 form.fields[1].value = ASCIIToUTF16("Jonathan");
2497 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2498 form.fields[0].is_autofilled = true;
2499 form.fields[1].is_autofilled = true;
2500 form.fields[2].is_autofilled = true;
2501 FillForm(form, input_element);
2503 // Find the newly-filled form that contains the input element.
2505 FormFieldData field2;
2506 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2509 autofill::REQUIRE_NONE));
2511 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2512 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2513 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2515 const std::vector<FormFieldData>& fields2 = form2.fields;
2516 ASSERT_EQ(3U, fields2.size());
2518 expected.form_control_type = "text";
2520 expected.name = ASCIIToUTF16("firstname");
2521 expected.value = ASCIIToUTF16("Broth");
2522 expected.max_length = 5;
2523 expected.is_autofilled = true;
2524 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2526 expected.name = ASCIIToUTF16("lastname");
2527 expected.value = ASCIIToUTF16("Jonatha");
2528 expected.max_length = 7;
2529 expected.is_autofilled = true;
2530 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2532 expected.name = ASCIIToUTF16("email");
2533 expected.value = ASCIIToUTF16("brotherj@");
2534 expected.max_length = 9;
2535 expected.is_autofilled = true;
2536 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2539 // This test uses negative values of the maxlength attribute for input elements.
2540 // In this case, the maxlength of the input elements is set to the default
2541 // maxlength (defined in WebKit.)
2542 TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
2543 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2544 " <INPUT type='text' id='firstname' maxlength='-1'/>"
2545 " <INPUT type='text' id='lastname' maxlength='-10'/>"
2546 " <INPUT type='text' id='email' maxlength='-13'/>"
2547 " <INPUT type='submit' name='reply-send' value='Send'/>"
2550 WebFrame* web_frame = GetMainFrame();
2551 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2553 FormCache form_cache;
2554 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2555 ASSERT_EQ(1U, forms.size());
2557 // Get the input element we want to find.
2558 WebElement element = web_frame->document().getElementById("firstname");
2559 WebInputElement input_element = element.to<WebInputElement>();
2561 // Find the form that contains the input element.
2563 FormFieldData field;
2564 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2567 autofill::REQUIRE_NONE));
2568 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2569 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2570 EXPECT_EQ(GURL("http://buh.com"), form.action);
2572 const std::vector<FormFieldData>& fields = form.fields;
2573 ASSERT_EQ(3U, fields.size());
2575 FormFieldData expected;
2576 expected.form_control_type = "text";
2577 expected.max_length = WebInputElement::defaultMaxLength();
2579 expected.name = ASCIIToUTF16("firstname");
2580 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2582 expected.name = ASCIIToUTF16("lastname");
2583 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2585 expected.name = ASCIIToUTF16("email");
2586 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2589 form.fields[0].value = ASCIIToUTF16("Brother");
2590 form.fields[1].value = ASCIIToUTF16("Jonathan");
2591 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2592 FillForm(form, input_element);
2594 // Find the newly-filled form that contains the input element.
2596 FormFieldData field2;
2597 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2600 autofill::REQUIRE_NONE));
2602 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2603 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2604 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2606 const std::vector<FormFieldData>& fields2 = form2.fields;
2607 ASSERT_EQ(3U, fields2.size());
2609 expected.name = ASCIIToUTF16("firstname");
2610 expected.value = ASCIIToUTF16("Brother");
2611 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2613 expected.name = ASCIIToUTF16("lastname");
2614 expected.value = ASCIIToUTF16("Jonathan");
2615 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2617 expected.name = ASCIIToUTF16("email");
2618 expected.value = ASCIIToUTF16("brotherj@example.com");
2619 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2622 TEST_F(FormAutofillTest, FillFormEmptyName) {
2623 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2624 " <INPUT type='text' id='firstname'/>"
2625 " <INPUT type='text' id='lastname'/>"
2626 " <INPUT type='text' id='email'/>"
2627 " <INPUT type='submit' value='Send'/>"
2630 WebFrame* web_frame = GetMainFrame();
2631 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2633 FormCache form_cache;
2634 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2635 ASSERT_EQ(1U, forms.size());
2637 // Get the input element we want to find.
2638 WebElement element = web_frame->document().getElementById("firstname");
2639 WebInputElement input_element = element.to<WebInputElement>();
2641 // Find the form that contains the input element.
2643 FormFieldData field;
2644 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2647 autofill::REQUIRE_NONE));
2648 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2649 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2650 EXPECT_EQ(GURL("http://buh.com"), form.action);
2652 const std::vector<FormFieldData>& fields = form.fields;
2653 ASSERT_EQ(3U, fields.size());
2655 FormFieldData expected;
2656 expected.form_control_type = "text";
2657 expected.max_length = WebInputElement::defaultMaxLength();
2659 expected.name = ASCIIToUTF16("firstname");
2660 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2662 expected.name = ASCIIToUTF16("lastname");
2663 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2665 expected.name = ASCIIToUTF16("email");
2666 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2669 form.fields[0].value = ASCIIToUTF16("Wyatt");
2670 form.fields[1].value = ASCIIToUTF16("Earp");
2671 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2672 FillForm(form, input_element);
2674 // Find the newly-filled form that contains the input element.
2676 FormFieldData field2;
2677 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2680 autofill::REQUIRE_NONE));
2682 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2683 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2684 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2686 const std::vector<FormFieldData>& fields2 = form2.fields;
2687 ASSERT_EQ(3U, fields2.size());
2689 expected.form_control_type = "text";
2690 expected.max_length = WebInputElement::defaultMaxLength();
2692 expected.name = ASCIIToUTF16("firstname");
2693 expected.value = ASCIIToUTF16("Wyatt");
2694 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2696 expected.name = ASCIIToUTF16("lastname");
2697 expected.value = ASCIIToUTF16("Earp");
2698 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2700 expected.name = ASCIIToUTF16("email");
2701 expected.value = ASCIIToUTF16("wyatt@example.com");
2702 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2705 TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
2706 LoadHTML("<FORM action='http://buh.com' method='post'>"
2707 " <INPUT type='text' id='firstname'/>"
2708 " <INPUT type='text' id='middlename'/>"
2709 " <INPUT type='text' id='lastname'/>"
2710 " <INPUT type='submit' value='Send'/>"
2712 "<FORM action='http://abc.com' method='post'>"
2713 " <INPUT type='text' id='apple'/>"
2714 " <INPUT type='text' id='banana'/>"
2715 " <INPUT type='text' id='cantelope'/>"
2716 " <INPUT type='submit' value='Send'/>"
2719 WebFrame* web_frame = GetMainFrame();
2720 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2722 FormCache form_cache;
2723 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2724 ASSERT_EQ(2U, forms.size());
2726 // Get the input element we want to find.
2727 WebElement element = web_frame->document().getElementById("apple");
2728 WebInputElement input_element = element.to<WebInputElement>();
2730 // Find the form that contains the input element.
2732 FormFieldData field;
2733 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2736 autofill::REQUIRE_NONE));
2737 EXPECT_EQ(base::string16(), form.name);
2738 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2739 EXPECT_EQ(GURL("http://abc.com"), form.action);
2741 const std::vector<FormFieldData>& fields = form.fields;
2742 ASSERT_EQ(3U, fields.size());
2744 FormFieldData expected;
2745 expected.form_control_type = "text";
2746 expected.max_length = WebInputElement::defaultMaxLength();
2748 expected.name = ASCIIToUTF16("apple");
2749 expected.is_autofilled = false;
2750 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2752 expected.name = ASCIIToUTF16("banana");
2753 expected.is_autofilled = false;
2754 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2756 expected.name = ASCIIToUTF16("cantelope");
2757 expected.is_autofilled = false;
2758 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2761 form.fields[0].value = ASCIIToUTF16("Red");
2762 form.fields[1].value = ASCIIToUTF16("Yellow");
2763 form.fields[2].value = ASCIIToUTF16("Also Yellow");
2764 form.fields[0].is_autofilled = true;
2765 form.fields[1].is_autofilled = true;
2766 form.fields[2].is_autofilled = true;
2767 FillForm(form, input_element);
2769 // Find the newly-filled form that contains the input element.
2771 FormFieldData field2;
2772 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2775 autofill::REQUIRE_NONE));
2777 EXPECT_EQ(base::string16(), form2.name);
2778 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2779 EXPECT_EQ(GURL("http://abc.com"), form2.action);
2781 const std::vector<FormFieldData>& fields2 = form2.fields;
2782 ASSERT_EQ(3U, fields2.size());
2784 expected.name = ASCIIToUTF16("apple");
2785 expected.value = ASCIIToUTF16("Red");
2786 expected.is_autofilled = true;
2787 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2789 expected.name = ASCIIToUTF16("banana");
2790 expected.value = ASCIIToUTF16("Yellow");
2791 expected.is_autofilled = true;
2792 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2794 expected.name = ASCIIToUTF16("cantelope");
2795 expected.value = ASCIIToUTF16("Also Yellow");
2796 expected.is_autofilled = true;
2797 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2800 TEST_F(FormAutofillTest, ThreePartPhone) {
2801 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2803 " <input type='text' name='dayphone1'>"
2805 " <input type='text' name='dayphone2'>"
2807 " <input type='text' name='dayphone3'>"
2809 " <input type='text' name='dayphone4'>"
2810 " <input type='submit' name='reply-send' value='Send'>"
2814 WebFrame* frame = GetMainFrame();
2815 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2817 WebVector<WebFormElement> forms;
2818 frame->document().forms(forms);
2819 ASSERT_EQ(1U, forms.size());
2822 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2823 WebFormControlElement(),
2824 autofill::REQUIRE_NONE,
2825 autofill::EXTRACT_VALUE,
2828 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2829 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2830 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2832 const std::vector<FormFieldData>& fields = form.fields;
2833 ASSERT_EQ(4U, fields.size());
2835 FormFieldData expected;
2836 expected.form_control_type = "text";
2837 expected.max_length = WebInputElement::defaultMaxLength();
2839 expected.label = ASCIIToUTF16("Phone:");
2840 expected.name = ASCIIToUTF16("dayphone1");
2841 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2843 expected.label = ASCIIToUTF16("-");
2844 expected.name = ASCIIToUTF16("dayphone2");
2845 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2847 expected.label = ASCIIToUTF16("-");
2848 expected.name = ASCIIToUTF16("dayphone3");
2849 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2851 expected.label = ASCIIToUTF16("ext.:");
2852 expected.name = ASCIIToUTF16("dayphone4");
2853 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2857 TEST_F(FormAutofillTest, MaxLengthFields) {
2858 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2860 " <input type='text' maxlength='3' name='dayphone1'>"
2862 " <input type='text' maxlength='3' name='dayphone2'>"
2864 " <input type='text' maxlength='4' size='5'"
2865 " name='dayphone3'>"
2867 " <input type='text' maxlength='5' name='dayphone4'>"
2868 " <input type='text' name='default1'>"
2869 " <input type='text' maxlength='-1' name='invalid1'>"
2870 " <input type='submit' name='reply-send' value='Send'>"
2873 WebFrame* frame = GetMainFrame();
2874 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2876 WebVector<WebFormElement> forms;
2877 frame->document().forms(forms);
2878 ASSERT_EQ(1U, forms.size());
2881 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2882 WebFormControlElement(),
2883 autofill::REQUIRE_NONE,
2884 autofill::EXTRACT_VALUE,
2887 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2888 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2889 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2891 const std::vector<FormFieldData>& fields = form.fields;
2892 ASSERT_EQ(6U, fields.size());
2894 FormFieldData expected;
2895 expected.form_control_type = "text";
2897 expected.label = ASCIIToUTF16("Phone:");
2898 expected.name = ASCIIToUTF16("dayphone1");
2899 expected.max_length = 3;
2900 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2902 expected.label = ASCIIToUTF16("-");
2903 expected.name = ASCIIToUTF16("dayphone2");
2904 expected.max_length = 3;
2905 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2907 expected.label = ASCIIToUTF16("-");
2908 expected.name = ASCIIToUTF16("dayphone3");
2909 expected.max_length = 4;
2910 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2912 expected.label = ASCIIToUTF16("ext.:");
2913 expected.name = ASCIIToUTF16("dayphone4");
2914 expected.max_length = 5;
2915 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2917 // When unspecified |size|, default is returned.
2918 expected.label = base::string16();
2919 expected.name = ASCIIToUTF16("default1");
2920 expected.max_length = WebInputElement::defaultMaxLength();
2921 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
2923 // When invalid |size|, default is returned.
2924 expected.label = base::string16();
2925 expected.name = ASCIIToUTF16("invalid1");
2926 expected.max_length = WebInputElement::defaultMaxLength();
2927 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
2930 // This test re-creates the experience of typing in a field then selecting a
2931 // profile from the Autofill suggestions popup. The field that is being typed
2932 // into should be filled even though it's not technically empty.
2933 TEST_F(FormAutofillTest, FillFormNonEmptyField) {
2934 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2935 " <INPUT type='text' id='firstname'/>"
2936 " <INPUT type='text' id='lastname'/>"
2937 " <INPUT type='text' id='email'/>"
2938 " <INPUT type='submit' value='Send'/>"
2941 WebFrame* web_frame = GetMainFrame();
2942 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2944 FormCache form_cache;
2945 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2946 ASSERT_EQ(1U, forms.size());
2948 // Get the input element we want to find.
2949 WebElement element = web_frame->document().getElementById("firstname");
2950 WebInputElement input_element = element.to<WebInputElement>();
2952 // Simulate typing by modifying the field value.
2953 input_element.setValue(ASCIIToUTF16("Wy"));
2955 // Find the form that contains the input element.
2957 FormFieldData field;
2958 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2961 autofill::REQUIRE_NONE));
2962 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2963 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2964 EXPECT_EQ(GURL("http://buh.com"), form.action);
2966 const std::vector<FormFieldData>& fields = form.fields;
2967 ASSERT_EQ(3U, fields.size());
2969 FormFieldData expected;
2970 expected.form_control_type = "text";
2971 expected.max_length = WebInputElement::defaultMaxLength();
2973 expected.name = ASCIIToUTF16("firstname");
2974 expected.value = ASCIIToUTF16("Wy");
2975 expected.is_autofilled = false;
2976 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2978 expected.name = ASCIIToUTF16("lastname");
2979 expected.value = base::string16();
2980 expected.is_autofilled = false;
2981 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2983 expected.name = ASCIIToUTF16("email");
2984 expected.value = base::string16();
2985 expected.is_autofilled = false;
2986 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2988 // Preview the form and verify that the cursor position has been updated.
2989 form.fields[0].value = ASCIIToUTF16("Wyatt");
2990 form.fields[1].value = ASCIIToUTF16("Earp");
2991 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2992 form.fields[0].is_autofilled = true;
2993 form.fields[1].is_autofilled = true;
2994 form.fields[2].is_autofilled = true;
2995 PreviewForm(form, input_element);
2996 EXPECT_EQ(2, input_element.selectionStart());
2997 EXPECT_EQ(5, input_element.selectionEnd());
3000 FillForm(form, input_element);
3002 // Find the newly-filled form that contains the input element.
3004 FormFieldData field2;
3005 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
3008 autofill::REQUIRE_NONE));
3010 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3011 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3012 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3014 const std::vector<FormFieldData>& fields2 = form2.fields;
3015 ASSERT_EQ(3U, fields2.size());
3017 expected.name = ASCIIToUTF16("firstname");
3018 expected.value = ASCIIToUTF16("Wyatt");
3019 expected.is_autofilled = true;
3020 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3022 expected.name = ASCIIToUTF16("lastname");
3023 expected.value = ASCIIToUTF16("Earp");
3024 expected.is_autofilled = true;
3025 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3027 expected.name = ASCIIToUTF16("email");
3028 expected.value = ASCIIToUTF16("wyatt@example.com");
3029 expected.is_autofilled = true;
3030 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3032 // Verify that the cursor position has been updated.
3033 EXPECT_EQ(5, input_element.selectionStart());
3034 EXPECT_EQ(5, input_element.selectionEnd());
3037 TEST_F(FormAutofillTest, ClearFormWithNode) {
3039 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3040 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3041 " <INPUT type='text' id='lastname' value='Earp'/>"
3042 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3043 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3044 " <INPUT type='month' id='month' value='2012-11'>"
3045 " <INPUT type='month' id='month-disabled' value='2012-11'"
3046 " disabled='disabled'>"
3047 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3048 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3051 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3052 " <INPUT type='submit' value='Send'/>"
3055 WebFrame* web_frame = GetMainFrame();
3056 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3058 FormCache form_cache;
3059 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3060 ASSERT_EQ(1U, forms.size());
3062 // Set the auto-filled attribute.
3063 WebInputElement firstname =
3064 web_frame->document().getElementById("firstname").to<WebInputElement>();
3065 firstname.setAutofilled(true);
3066 WebInputElement lastname =
3067 web_frame->document().getElementById("lastname").to<WebInputElement>();
3068 lastname.setAutofilled(true);
3069 WebInputElement month =
3070 web_frame->document().getElementById("month").to<WebInputElement>();
3071 month.setAutofilled(true);
3072 WebInputElement textarea =
3073 web_frame->document().getElementById("textarea").to<WebInputElement>();
3074 textarea.setAutofilled(true);
3076 // Set the value of the disabled text input element.
3077 WebInputElement notenabled =
3078 web_frame->document().getElementById("notenabled").to<WebInputElement>();
3079 notenabled.setValue(WebString::fromUTF8("no clear"));
3082 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3084 // Verify that the auto-filled attribute has been turned off.
3085 EXPECT_FALSE(firstname.isAutofilled());
3087 // Verify the form is cleared.
3089 FormFieldData field2;
3090 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
3093 autofill::REQUIRE_NONE));
3094 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3095 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3096 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3098 const std::vector<FormFieldData>& fields2 = form2.fields;
3099 ASSERT_EQ(9U, fields2.size());
3101 FormFieldData expected;
3102 expected.form_control_type = "text";
3103 expected.max_length = WebInputElement::defaultMaxLength();
3105 expected.name = ASCIIToUTF16("firstname");
3106 expected.value = base::string16();
3107 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3109 expected.name = ASCIIToUTF16("lastname");
3110 expected.value = base::string16();
3111 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3113 expected.name = ASCIIToUTF16("noAC");
3114 expected.value = ASCIIToUTF16("one");
3115 expected.autocomplete_attribute = "off";
3116 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3117 expected.autocomplete_attribute = std::string(); // reset
3119 expected.name = ASCIIToUTF16("notenabled");
3120 expected.value = ASCIIToUTF16("no clear");
3121 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[3]);
3123 expected.form_control_type = "month";
3124 expected.max_length = 0;
3125 expected.name = ASCIIToUTF16("month");
3126 expected.value = base::string16();
3127 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[4]);
3129 expected.name = ASCIIToUTF16("month-disabled");
3130 expected.value = ASCIIToUTF16("2012-11");
3131 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[5]);
3133 expected.form_control_type = "textarea";
3134 expected.name = ASCIIToUTF16("textarea");
3135 expected.value = base::string16();
3136 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[6]);
3138 expected.name = ASCIIToUTF16("textarea-disabled");
3139 expected.value = ASCIIToUTF16(" Banana! ");
3140 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[7]);
3142 expected.name = ASCIIToUTF16("textarea-noAC");
3143 expected.value = ASCIIToUTF16("Carrot?");
3144 expected.autocomplete_attribute = "off";
3145 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[8]);
3146 expected.autocomplete_attribute = std::string(); // reset
3148 // Verify that the cursor position has been updated.
3149 EXPECT_EQ(0, firstname.selectionStart());
3150 EXPECT_EQ(0, firstname.selectionEnd());
3153 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) {
3155 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3156 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3157 " <INPUT type='text' id='lastname' value='Earp'/>"
3158 " <SELECT id='state' name='state'>"
3159 " <OPTION selected>?</OPTION>"
3160 " <OPTION>AA</OPTION>"
3161 " <OPTION>AE</OPTION>"
3162 " <OPTION>AK</OPTION>"
3164 " <INPUT type='submit' value='Send'/>"
3167 WebFrame* web_frame = GetMainFrame();
3168 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3170 FormCache form_cache;
3171 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3172 ASSERT_EQ(1U, forms.size());
3174 // Set the auto-filled attribute.
3175 WebInputElement firstname =
3176 web_frame->document().getElementById("firstname").to<WebInputElement>();
3177 firstname.setAutofilled(true);
3178 WebInputElement lastname =
3179 web_frame->document().getElementById("lastname").to<WebInputElement>();
3180 lastname.setAutofilled(true);
3182 // Set the value and auto-filled attribute of the state element.
3183 WebSelectElement state =
3184 web_frame->document().getElementById("state").to<WebSelectElement>();
3185 state.setValue(WebString::fromUTF8("AK"));
3186 state.setAutofilled(true);
3189 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3191 // Verify that the auto-filled attribute has been turned off.
3192 EXPECT_FALSE(firstname.isAutofilled());
3194 // Verify the form is cleared.
3196 FormFieldData field2;
3197 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
3200 autofill::REQUIRE_NONE));
3201 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3202 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3203 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3205 const std::vector<FormFieldData>& fields2 = form2.fields;
3206 ASSERT_EQ(3U, fields2.size());
3208 FormFieldData expected;
3210 expected.name = ASCIIToUTF16("firstname");
3211 expected.value = base::string16();
3212 expected.form_control_type = "text";
3213 expected.max_length = WebInputElement::defaultMaxLength();
3214 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3216 expected.name = ASCIIToUTF16("lastname");
3217 expected.value = base::string16();
3218 expected.form_control_type = "text";
3219 expected.max_length = WebInputElement::defaultMaxLength();
3220 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3222 expected.name = ASCIIToUTF16("state");
3223 expected.value = ASCIIToUTF16("?");
3224 expected.form_control_type = "select-one";
3225 expected.max_length = 0;
3226 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3228 // Verify that the cursor position has been updated.
3229 EXPECT_EQ(0, firstname.selectionStart());
3230 EXPECT_EQ(0, firstname.selectionEnd());
3233 TEST_F(FormAutofillTest, ClearPreviewedFormWithElement) {
3234 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3235 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3236 " <INPUT type='text' id='lastname'/>"
3237 " <INPUT type='text' id='email'/>"
3238 " <INPUT type='email' id='email2'/>"
3239 " <INPUT type='tel' id='phone'/>"
3240 " <INPUT type='submit' value='Send'/>"
3243 WebFrame* web_frame = GetMainFrame();
3244 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3246 FormCache form_cache;
3247 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3248 ASSERT_EQ(1U, forms.size());
3250 // Set the auto-filled attribute.
3251 WebInputElement firstname =
3252 web_frame->document().getElementById("firstname").to<WebInputElement>();
3253 firstname.setAutofilled(true);
3254 WebInputElement lastname =
3255 web_frame->document().getElementById("lastname").to<WebInputElement>();
3256 lastname.setAutofilled(true);
3257 WebInputElement email =
3258 web_frame->document().getElementById("email").to<WebInputElement>();
3259 email.setAutofilled(true);
3260 WebInputElement email2 =
3261 web_frame->document().getElementById("email2").to<WebInputElement>();
3262 email2.setAutofilled(true);
3263 WebInputElement phone =
3264 web_frame->document().getElementById("phone").to<WebInputElement>();
3265 phone.setAutofilled(true);
3267 // Set the suggested values on two of the elements.
3268 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3269 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3270 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3271 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3273 // Clear the previewed fields.
3274 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
3276 // Fields with empty suggestions suggestions are not modified.
3277 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
3278 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3279 EXPECT_TRUE(firstname.isAutofilled());
3281 // Verify the previewed fields are cleared.
3282 EXPECT_TRUE(lastname.value().isEmpty());
3283 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3284 EXPECT_FALSE(lastname.isAutofilled());
3285 EXPECT_TRUE(email.value().isEmpty());
3286 EXPECT_TRUE(email.suggestedValue().isEmpty());
3287 EXPECT_FALSE(email.isAutofilled());
3288 EXPECT_TRUE(email2.value().isEmpty());
3289 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3290 EXPECT_FALSE(email2.isAutofilled());
3291 EXPECT_TRUE(phone.value().isEmpty());
3292 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3293 EXPECT_FALSE(phone.isAutofilled());
3295 // Verify that the cursor position has been updated.
3296 EXPECT_EQ(0, lastname.selectionStart());
3297 EXPECT_EQ(0, lastname.selectionEnd());
3300 TEST_F(FormAutofillTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
3301 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3302 " <INPUT type='text' id='firstname' value='W'/>"
3303 " <INPUT type='text' id='lastname'/>"
3304 " <INPUT type='text' id='email'/>"
3305 " <INPUT type='email' id='email2'/>"
3306 " <INPUT type='tel' id='phone'/>"
3307 " <INPUT type='submit' value='Send'/>"
3310 WebFrame* web_frame = GetMainFrame();
3311 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3313 FormCache form_cache;
3314 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3315 ASSERT_EQ(1U, forms.size());
3317 // Set the auto-filled attribute.
3318 WebInputElement firstname =
3319 web_frame->document().getElementById("firstname").to<WebInputElement>();
3320 firstname.setAutofilled(true);
3321 WebInputElement lastname =
3322 web_frame->document().getElementById("lastname").to<WebInputElement>();
3323 lastname.setAutofilled(true);
3324 WebInputElement email =
3325 web_frame->document().getElementById("email").to<WebInputElement>();
3326 email.setAutofilled(true);
3327 WebInputElement email2 =
3328 web_frame->document().getElementById("email2").to<WebInputElement>();
3329 email2.setAutofilled(true);
3330 WebInputElement phone =
3331 web_frame->document().getElementById("phone").to<WebInputElement>();
3332 phone.setAutofilled(true);
3335 // Set the suggested values on all of the elements.
3336 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3337 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3338 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3339 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3340 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3342 // Clear the previewed fields.
3343 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
3345 // Fields with non-empty values are restored.
3346 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
3347 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3348 EXPECT_FALSE(firstname.isAutofilled());
3349 EXPECT_EQ(1, firstname.selectionStart());
3350 EXPECT_EQ(1, firstname.selectionEnd());
3352 // Verify the previewed fields are cleared.
3353 EXPECT_TRUE(lastname.value().isEmpty());
3354 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3355 EXPECT_FALSE(lastname.isAutofilled());
3356 EXPECT_TRUE(email.value().isEmpty());
3357 EXPECT_TRUE(email.suggestedValue().isEmpty());
3358 EXPECT_FALSE(email.isAutofilled());
3359 EXPECT_TRUE(email2.value().isEmpty());
3360 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3361 EXPECT_FALSE(email2.isAutofilled());
3362 EXPECT_TRUE(phone.value().isEmpty());
3363 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3364 EXPECT_FALSE(phone.isAutofilled());
3367 TEST_F(FormAutofillTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
3368 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3369 " <INPUT type='text' id='firstname' value='W'/>"
3370 " <INPUT type='text' id='lastname'/>"
3371 " <INPUT type='text' id='email'/>"
3372 " <INPUT type='email' id='email2'/>"
3373 " <INPUT type='tel' id='phone'/>"
3374 " <INPUT type='submit' value='Send'/>"
3377 WebFrame* web_frame = GetMainFrame();
3378 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3380 FormCache form_cache;
3381 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3382 ASSERT_EQ(1U, forms.size());
3384 // Set the auto-filled attribute.
3385 WebInputElement firstname =
3386 web_frame->document().getElementById("firstname").to<WebInputElement>();
3387 firstname.setAutofilled(true);
3388 WebInputElement lastname =
3389 web_frame->document().getElementById("lastname").to<WebInputElement>();
3390 lastname.setAutofilled(true);
3391 WebInputElement email =
3392 web_frame->document().getElementById("email").to<WebInputElement>();
3393 email.setAutofilled(true);
3394 WebInputElement email2 =
3395 web_frame->document().getElementById("email2").to<WebInputElement>();
3396 email2.setAutofilled(true);
3397 WebInputElement phone =
3398 web_frame->document().getElementById("phone").to<WebInputElement>();
3399 phone.setAutofilled(true);
3401 // Set the suggested values on all of the elements.
3402 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3403 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3404 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3405 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3406 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3408 // Clear the previewed fields.
3409 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
3411 // Fields with non-empty values are restored.
3412 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
3413 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3414 EXPECT_TRUE(firstname.isAutofilled());
3415 EXPECT_EQ(1, firstname.selectionStart());
3416 EXPECT_EQ(1, firstname.selectionEnd());
3418 // Verify the previewed fields are cleared.
3419 EXPECT_TRUE(lastname.value().isEmpty());
3420 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3421 EXPECT_FALSE(lastname.isAutofilled());
3422 EXPECT_TRUE(email.value().isEmpty());
3423 EXPECT_TRUE(email.suggestedValue().isEmpty());
3424 EXPECT_FALSE(email.isAutofilled());
3425 EXPECT_TRUE(email2.value().isEmpty());
3426 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3427 EXPECT_FALSE(email2.isAutofilled());
3428 EXPECT_TRUE(phone.value().isEmpty());
3429 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3430 EXPECT_FALSE(phone.isAutofilled());
3433 // Autofill's "Clear Form" should clear only autofilled fields
3434 TEST_F(FormAutofillTest, ClearOnlyAutofilledFields) {
3437 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3438 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3439 " <INPUT type='text' id='lastname' value='Earp'/>"
3440 " <INPUT type='email' id='email' value='wyatt@earp.com'/>"
3441 " <INPUT type='tel' id='phone' value='650-777-9999'/>"
3442 " <INPUT type='submit' value='Send'/>"
3445 WebFrame* web_frame = GetMainFrame();
3446 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3448 FormCache form_cache;
3449 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3450 ASSERT_EQ(1U, forms.size());
3452 // Set the autofilled attribute.
3453 WebInputElement firstname =
3454 web_frame->document().getElementById("firstname").to<WebInputElement>();
3455 firstname.setAutofilled(false);
3456 WebInputElement lastname =
3457 web_frame->document().getElementById("lastname").to<WebInputElement>();
3458 lastname.setAutofilled(true);
3459 WebInputElement email =
3460 web_frame->document().getElementById("email").to<WebInputElement>();
3461 email.setAutofilled(true);
3462 WebInputElement phone =
3463 web_frame->document().getElementById("phone").to<WebInputElement>();
3464 phone.setAutofilled(true);
3466 // Clear the fields.
3467 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3469 // Verify only autofilled fields are cleared.
3470 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
3471 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3472 EXPECT_FALSE(firstname.isAutofilled());
3473 EXPECT_TRUE(lastname.value().isEmpty());
3474 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3475 EXPECT_FALSE(lastname.isAutofilled());
3476 EXPECT_TRUE(email.value().isEmpty());
3477 EXPECT_TRUE(email.suggestedValue().isEmpty());
3478 EXPECT_FALSE(email.isAutofilled());
3479 EXPECT_TRUE(phone.value().isEmpty());
3480 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3481 EXPECT_FALSE(phone.isAutofilled());
3484 TEST_F(FormAutofillTest, FormWithNodeIsAutofilled) {
3485 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3486 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3487 " <INPUT type='text' id='lastname'/>"
3488 " <INPUT type='text' id='email'/>"
3489 " <INPUT type='email' id='email2'/>"
3490 " <INPUT type='tel' id='phone'/>"
3491 " <INPUT type='submit' value='Send'/>"
3494 WebFrame* web_frame = GetMainFrame();
3495 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3497 FormCache form_cache;
3498 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3499 ASSERT_EQ(1U, forms.size());
3501 WebInputElement firstname =
3502 web_frame->document().getElementById("firstname").to<WebInputElement>();
3504 // Auto-filled attribute not set yet.
3505 EXPECT_FALSE(FormWithElementIsAutofilled(firstname));
3507 // Set the auto-filled attribute.
3508 firstname.setAutofilled(true);
3510 EXPECT_TRUE(FormWithElementIsAutofilled(firstname));
3513 // If we have multiple labels per id, the labels concatenated into label string.
3514 TEST_F(FormAutofillTest, MultipleLabelsPerElement) {
3515 std::vector<base::string16> labels, names, values;
3517 labels.push_back(ASCIIToUTF16("First Name:"));
3518 names.push_back(ASCIIToUTF16("firstname"));
3519 values.push_back(ASCIIToUTF16("John"));
3521 labels.push_back(ASCIIToUTF16("Last Name:"));
3522 names.push_back(ASCIIToUTF16("lastname"));
3523 values.push_back(ASCIIToUTF16("Smith"));
3525 labels.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
3526 names.push_back(ASCIIToUTF16("email"));
3527 values.push_back(ASCIIToUTF16("john@example.com"));
3530 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3531 " <LABEL for='firstname'> First Name: </LABEL>"
3532 " <LABEL for='firstname'></LABEL>"
3533 " <INPUT type='text' id='firstname' value='John'/>"
3534 " <LABEL for='lastname'></LABEL>"
3535 " <LABEL for='lastname'> Last Name: </LABEL>"
3536 " <INPUT type='text' id='lastname' value='Smith'/>"
3537 " <LABEL for='email'> Email: </LABEL>"
3538 " <LABEL for='email'> xxx@yyy.com </LABEL>"
3539 " <INPUT type='text' id='email' value='john@example.com'/>"
3540 " <INPUT type='submit' name='reply-send' value='Send'/>"
3542 labels, names, values);
3545 TEST_F(FormAutofillTest, ClickElement) {
3546 LoadHTML("<BUTTON id='link'>Button</BUTTON>"
3547 "<BUTTON name='button'>Button</BUTTON>");
3548 WebFrame* frame = GetMainFrame();
3549 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3551 // Successful retrieval by id.
3552 autofill::WebElementDescriptor clicker;
3553 clicker.retrieval_method = autofill::WebElementDescriptor::ID;
3554 clicker.descriptor = "link";
3555 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3557 // Successful retrieval by css selector.
3558 clicker.retrieval_method = autofill::WebElementDescriptor::CSS_SELECTOR;
3559 clicker.descriptor = "button[name='button']";
3560 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3562 // Unsuccessful retrieval due to invalid CSS selector.
3563 clicker.descriptor = "^*&";
3564 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3566 // Unsuccessful retrieval because element does not exist.
3567 clicker.descriptor = "#junk";
3568 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3571 TEST_F(FormAutofillTest, SelectOneAsText) {
3572 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3573 " <INPUT type='text' id='firstname' value='John'/>"
3574 " <INPUT type='text' id='lastname' value='Smith'/>"
3575 " <SELECT id='country'>"
3576 " <OPTION value='AF'>Afghanistan</OPTION>"
3577 " <OPTION value='AL'>Albania</OPTION>"
3578 " <OPTION value='DZ'>Algeria</OPTION>"
3580 " <INPUT type='submit' name='reply-send' value='Send'/>"
3583 WebFrame* frame = GetMainFrame();
3584 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3586 // Set the value of the select-one.
3587 WebSelectElement select_element =
3588 frame->document().getElementById("country").to<WebSelectElement>();
3589 select_element.setValue(WebString::fromUTF8("AL"));
3591 WebVector<WebFormElement> forms;
3592 frame->document().forms(forms);
3593 ASSERT_EQ(1U, forms.size());
3597 // Extract the country select-one value as text.
3598 EXPECT_TRUE(WebFormElementToFormData(
3599 forms[0], WebFormControlElement(), autofill::REQUIRE_NONE,
3600 static_cast<autofill::ExtractMask>(
3601 autofill::EXTRACT_VALUE | autofill::EXTRACT_OPTION_TEXT),
3603 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3604 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3605 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3607 const std::vector<FormFieldData>& fields = form.fields;
3608 ASSERT_EQ(3U, fields.size());
3610 FormFieldData expected;
3612 expected.name = ASCIIToUTF16("firstname");
3613 expected.value = ASCIIToUTF16("John");
3614 expected.form_control_type = "text";
3615 expected.max_length = WebInputElement::defaultMaxLength();
3616 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3618 expected.name = ASCIIToUTF16("lastname");
3619 expected.value = ASCIIToUTF16("Smith");
3620 expected.form_control_type = "text";
3621 expected.max_length = WebInputElement::defaultMaxLength();
3622 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3624 expected.name = ASCIIToUTF16("country");
3625 expected.value = ASCIIToUTF16("Albania");
3626 expected.form_control_type = "select-one";
3627 expected.max_length = 0;
3628 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3630 form.fields.clear();
3631 // Extract the country select-one value as value.
3632 EXPECT_TRUE(WebFormElementToFormData(forms[0],
3633 WebFormControlElement(),
3634 autofill::REQUIRE_NONE,
3635 autofill::EXTRACT_VALUE,
3638 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3639 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3640 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3642 ASSERT_EQ(3U, fields.size());
3644 expected.name = ASCIIToUTF16("firstname");
3645 expected.value = ASCIIToUTF16("John");
3646 expected.form_control_type = "text";
3647 expected.max_length = WebInputElement::defaultMaxLength();
3648 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3650 expected.name = ASCIIToUTF16("lastname");
3651 expected.value = ASCIIToUTF16("Smith");
3652 expected.form_control_type = "text";
3653 expected.max_length = WebInputElement::defaultMaxLength();
3654 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3656 expected.name = ASCIIToUTF16("country");
3657 expected.value = ASCIIToUTF16("AL");
3658 expected.form_control_type = "select-one";
3659 expected.max_length = 0;
3660 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3663 } // namespace autofill