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/metrics/field_trial.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/test/base/chrome_render_view_test.h"
14 #include "components/autofill/content/renderer/form_autofill_util.h"
15 #include "components/autofill/content/renderer/form_cache.h"
16 #include "components/autofill/core/common/form_data.h"
17 #include "components/autofill/core/common/web_element_descriptor.h"
18 #include "components/variations/entropy_provider.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebVector.h"
22 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebElement.h"
24 #include "third_party/WebKit/public/web/WebFormControlElement.h"
25 #include "third_party/WebKit/public/web/WebFormElement.h"
26 #include "third_party/WebKit/public/web/WebInputElement.h"
27 #include "third_party/WebKit/public/web/WebNode.h"
28 #include "third_party/WebKit/public/web/WebSelectElement.h"
29 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
31 using WebKit::WebDocument;
32 using WebKit::WebElement;
33 using WebKit::WebFormControlElement;
34 using WebKit::WebFormElement;
35 using WebKit::WebFrame;
36 using WebKit::WebInputElement;
37 using WebKit::WebNode;
38 using WebKit::WebSelectElement;
39 using WebKit::WebString;
40 using WebKit::WebTextAreaElement;
41 using WebKit::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 " <TEXTAREA id=\"textarea\"></TEXTAREA>"
81 " <TEXTAREA id=\"textarea-nonempty\">Go away!</TEXTAREA>"
82 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
89 class FormAutofillTest : public ChromeRenderViewTest {
91 FormAutofillTest() : ChromeRenderViewTest() {}
92 virtual ~FormAutofillTest() {}
94 void ExpectLabels(const char* html,
95 const std::vector<string16>& labels,
96 const std::vector<string16>& names,
97 const std::vector<string16>& values) {
98 std::vector<std::string> control_types(labels.size(), "text");
99 ExpectLabelsAndTypes(html, labels, names, values, control_types);
102 void ExpectLabelsAndTypes(const char* html,
103 const std::vector<string16>& labels,
104 const std::vector<string16>& names,
105 const std::vector<string16>& values,
106 const std::vector<std::string>& control_types) {
107 ASSERT_EQ(labels.size(), names.size());
108 ASSERT_EQ(labels.size(), values.size());
109 ASSERT_EQ(labels.size(), control_types.size());
113 WebFrame* web_frame = GetMainFrame();
114 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
116 FormCache form_cache;
117 std::vector<FormData> forms;
118 form_cache.ExtractForms(*web_frame, &forms);
119 ASSERT_EQ(1U, forms.size());
121 const FormData& form = forms[0];
122 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
123 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
124 EXPECT_EQ(GURL("http://cnn.com"), form.action);
126 const std::vector<FormFieldData>& fields = form.fields;
127 ASSERT_EQ(labels.size(), fields.size());
128 for (size_t i = 0; i < labels.size(); ++i) {
129 int max_length = control_types[i] == "text" ?
130 WebInputElement::defaultMaxLength() : 0;
131 FormFieldData expected;
132 expected.label = labels[i];
133 expected.name = names[i];
134 expected.value = values[i];
135 expected.form_control_type = control_types[i];
136 expected.max_length = max_length;
137 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
138 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
142 void ExpectJohnSmithLabels(const char* html) {
143 std::vector<string16> labels, names, values;
145 labels.push_back(ASCIIToUTF16("First name:"));
146 names.push_back(ASCIIToUTF16("firstname"));
147 values.push_back(ASCIIToUTF16("John"));
149 labels.push_back(ASCIIToUTF16("Last name:"));
150 names.push_back(ASCIIToUTF16("lastname"));
151 values.push_back(ASCIIToUTF16("Smith"));
153 labels.push_back(ASCIIToUTF16("Email:"));
154 names.push_back(ASCIIToUTF16("email"));
155 values.push_back(ASCIIToUTF16("john@example.com"));
157 ExpectLabels(html, labels, names, values);
160 typedef void (*FillFormFunction)(const FormData& form,
161 const WebInputElement& element);
163 typedef WebString (WebInputElement::*GetValueFunction)(void) const;
165 // Test FormFillxxx functions.
166 void TestFormFillFunctions(const char* html,
167 const AutofillFieldCase* field_cases,
168 size_t number_of_field_cases,
169 FillFormFunction fill_form_function,
170 GetValueFunction get_value_function) {
173 WebFrame* web_frame = GetMainFrame();
174 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
176 FormCache form_cache;
177 std::vector<FormData> forms;
178 form_cache.ExtractForms(*web_frame, &forms);
179 ASSERT_EQ(1U, forms.size());
181 // Get the input element we want to find.
182 WebElement element = web_frame->document().getElementById("firstname");
183 WebInputElement input_element = element.to<WebInputElement>();
185 // Find the form that contains the input element.
189 FindFormAndFieldForInputElement(input_element,
192 autofill::REQUIRE_AUTOCOMPLETE));
193 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name);
194 EXPECT_EQ(GURL(web_frame->document().url()), form_data.origin);
195 EXPECT_EQ(GURL("http://buh.com"), form_data.action);
197 const std::vector<FormFieldData>& fields = form_data.fields;
198 ASSERT_EQ(number_of_field_cases, fields.size());
200 FormFieldData expected;
201 // Verify field's initial value.
202 for (size_t i = 0; i < number_of_field_cases; ++i) {
203 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
204 field_cases[i].name));
205 expected.form_control_type = field_cases[i].form_control_type;
206 expected.max_length =
207 expected.form_control_type == "text" ?
208 WebInputElement::defaultMaxLength() : 0;
209 expected.name = ASCIIToUTF16(field_cases[i].name);
210 expected.value = ASCIIToUTF16(field_cases[i].initial_value);
211 expected.autocomplete_attribute = field_cases[i].autocomplete_attribute;
212 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
213 // Fill the form_data for the field.
214 form_data.fields[i].value = ASCIIToUTF16(field_cases[i].autofill_value);
217 // Autofill the form using the given fill form function.
218 fill_form_function(form_data, input_element);
220 // Validate Autofill or Preview results.
221 for (size_t i = 0; i < number_of_field_cases; ++i) {
222 ValidteFilledField(field_cases[i], get_value_function);
226 // Validate an Autofilled field.
227 void ValidteFilledField(const AutofillFieldCase& field_case,
228 GetValueFunction get_value_function) {
229 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
232 WebFormControlElement element = GetMainFrame()->document().getElementById(
233 ASCIIToUTF16(field_case.name)).to<WebFormControlElement>();
234 if (element.formControlType() == "select-one") {
235 value = element.to<WebSelectElement>().value();
236 } else if (element.formControlType() == "textarea") {
237 value = element.to<WebTextAreaElement>().value();
239 ASSERT_TRUE(element.formControlType() == "text" ||
240 element.formControlType() == "month");
241 WebInputElement input_element = GetMainFrame()->document().getElementById(
242 ASCIIToUTF16(field_case.name)).to<WebInputElement>();
243 value = (input_element.*get_value_function)();
246 const WebString expected_value = ASCIIToUTF16(field_case.expected_value);
247 if (expected_value.isEmpty())
248 EXPECT_TRUE(value.isEmpty());
250 EXPECT_EQ(expected_value, value);
252 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled());
255 static void FillFormForAllFieldsWrapper(const FormData& form,
256 const WebInputElement& element) {
257 FillFormForAllElements(form, element.form());
260 static void FillFormIncludingNonFocusableElementsWrapper(
261 const FormData& form,
262 const WebInputElement& element) {
263 FillFormIncludingNonFocusableElements(form, element.form());
267 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest);
270 // We should be able to extract a normal text field.
271 TEST_F(FormAutofillTest, WebFormControlElementToFormField) {
272 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>");
274 WebFrame* frame = GetMainFrame();
275 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
277 WebElement web_element = frame->document().getElementById("element");
278 WebFormControlElement element = web_element.to<WebFormControlElement>();
279 FormFieldData result1;
280 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result1);
282 FormFieldData expected;
283 expected.form_control_type = "text";
284 expected.max_length = WebInputElement::defaultMaxLength();
286 expected.name = ASCIIToUTF16("element");
287 expected.value = string16();
288 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
290 FormFieldData result2;
291 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result2);
293 expected.name = ASCIIToUTF16("element");
294 expected.value = ASCIIToUTF16("value");
295 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
298 // We should be able to extract a text field with autocomplete="off".
299 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompleteOff) {
300 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\""
301 " autocomplete=\"off\"/>");
303 WebFrame* frame = GetMainFrame();
304 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
306 WebElement web_element = frame->document().getElementById("element");
307 WebFormControlElement element = web_element.to<WebFormControlElement>();
308 FormFieldData result;
309 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
311 FormFieldData expected;
312 expected.name = ASCIIToUTF16("element");
313 expected.value = ASCIIToUTF16("value");
314 expected.form_control_type = "text";
315 expected.autocomplete_attribute = "off";
316 expected.max_length = WebInputElement::defaultMaxLength();
317 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
320 // We should be able to extract a text field with maxlength specified.
321 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMaxLength) {
322 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\""
323 " maxlength=\"5\"/>");
325 WebFrame* frame = GetMainFrame();
326 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
328 WebElement web_element = frame->document().getElementById("element");
329 WebFormControlElement element = web_element.to<WebFormControlElement>();
330 FormFieldData result;
331 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
333 FormFieldData expected;
334 expected.name = ASCIIToUTF16("element");
335 expected.value = ASCIIToUTF16("value");
336 expected.form_control_type = "text";
337 expected.max_length = 5;
338 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
341 // We should be able to extract a text field that has been autofilled.
342 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutofilled) {
343 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>");
345 WebFrame* frame = GetMainFrame();
346 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
348 WebElement web_element = frame->document().getElementById("element");
349 WebInputElement element = web_element.to<WebInputElement>();
350 element.setAutofilled(true);
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 = WebInputElement::defaultMaxLength();
359 expected.is_autofilled = true;
360 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
363 // We should be able to extract a radio or a checkbox field that has been
365 TEST_F(FormAutofillTest, WebFormControlElementToClickableFormField) {
366 LoadHTML("<INPUT type=\"checkbox\" id=\"checkbox\" value=\"mail\" checked/>"
367 "<INPUT type=\"radio\" id=\"radio\" value=\"male\"/>");
369 WebFrame* frame = GetMainFrame();
370 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
372 WebElement web_element = frame->document().getElementById("checkbox");
373 WebInputElement element = web_element.to<WebInputElement>();
374 element.setAutofilled(true);
375 FormFieldData result;
376 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
378 FormFieldData expected;
379 expected.name = ASCIIToUTF16("checkbox");
380 expected.value = ASCIIToUTF16("mail");
381 expected.form_control_type = "checkbox";
382 expected.is_autofilled = true;
383 expected.is_checkable = true;
384 expected.is_checked = true;
385 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
387 web_element = frame->document().getElementById("radio");
388 element = web_element.to<WebInputElement>();
389 element.setAutofilled(true);
390 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
391 expected.name = ASCIIToUTF16("radio");
392 expected.value = ASCIIToUTF16("male");
393 expected.form_control_type = "radio";
394 expected.is_autofilled = true;
395 expected.is_checkable = true;
396 expected.is_checked = false;
397 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
400 // We should be able to extract a <select> field.
401 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) {
402 LoadHTML("<SELECT id=\"element\"/>"
403 " <OPTION value=\"CA\">California</OPTION>"
404 " <OPTION value=\"TX\">Texas</OPTION>"
407 WebFrame* frame = GetMainFrame();
408 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
410 WebElement web_element = frame->document().getElementById("element");
411 WebFormControlElement element = web_element.to<WebFormControlElement>();
412 FormFieldData result1;
413 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result1);
415 FormFieldData expected;
416 expected.name = ASCIIToUTF16("element");
417 expected.max_length = 0;
418 expected.form_control_type = "select-one";
420 expected.value = ASCIIToUTF16("CA");
421 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
423 FormFieldData result2;
424 WebFormControlElementToFormField(
426 static_cast<autofill::ExtractMask>(autofill::EXTRACT_VALUE |
427 autofill::EXTRACT_OPTION_TEXT),
429 expected.value = ASCIIToUTF16("California");
430 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
432 FormFieldData result3;
433 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS,
435 expected.value = string16();
436 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result3);
438 ASSERT_EQ(2U, result3.option_values.size());
439 ASSERT_EQ(2U, result3.option_contents.size());
440 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]);
441 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]);
442 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]);
443 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]);
446 // We should be able to extract a <textarea> field.
447 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) {
448 LoadHTML("<TEXTAREA id=\"element\">"
449 "This element's value "
450 "spans multiple lines."
453 WebFrame* frame = GetMainFrame();
454 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
456 WebElement web_element = frame->document().getElementById("element");
457 WebFormControlElement element = web_element.to<WebFormControlElement>();
458 FormFieldData result_sans_value;
459 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
462 FormFieldData expected;
463 expected.name = ASCIIToUTF16("element");
464 expected.max_length = 0;
465 expected.form_control_type = "textarea";
466 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
468 FormFieldData result_with_value;
469 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
471 expected.value = ASCIIToUTF16("This element's value\n"
472 "spans multiple lines.");
473 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
476 // We should be able to extract an <input type="month"> field.
477 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMonthInput) {
478 LoadHTML("<INPUT type=\"month\" id=\"element\" value=\"2011-12\">");
480 WebFrame* frame = GetMainFrame();
481 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
483 WebElement web_element = frame->document().getElementById("element");
484 WebFormControlElement element = web_element.to<WebFormControlElement>();
485 FormFieldData result_sans_value;
486 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
489 FormFieldData expected;
490 expected.name = ASCIIToUTF16("element");
491 expected.max_length = 0;
492 expected.form_control_type = "month";
493 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
495 FormFieldData result_with_value;
496 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
498 expected.value = ASCIIToUTF16("2011-12");
499 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
502 // We should not extract the value for non-text and non-select fields.
503 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) {
504 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
505 " <INPUT type=\"hidden\" id=\"hidden\" value=\"apple\"/>"
506 " <INPUT type=\"submit\" id=\"submit\" value=\"Send\"/>"
509 WebFrame* frame = GetMainFrame();
510 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
512 WebElement web_element = frame->document().getElementById("hidden");
513 WebFormControlElement element = web_element.to<WebFormControlElement>();
514 FormFieldData result;
515 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
517 FormFieldData expected;
518 expected.max_length = 0;
520 expected.name = ASCIIToUTF16("hidden");
521 expected.form_control_type = "hidden";
522 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
524 web_element = frame->document().getElementById("submit");
525 element = web_element.to<WebFormControlElement>();
526 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
527 expected.name = ASCIIToUTF16("submit");
528 expected.form_control_type = "submit";
529 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
532 // We should be able to extract password fields.
533 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) {
534 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
535 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>"
538 WebFrame* frame = GetMainFrame();
539 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
541 WebElement web_element = frame->document().getElementById("password");
542 WebFormControlElement element = web_element.to<WebFormControlElement>();
543 FormFieldData result;
544 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
546 FormFieldData expected;
547 expected.max_length = WebInputElement::defaultMaxLength();
548 expected.name = ASCIIToUTF16("password");
549 expected.form_control_type = "password";
550 expected.value = ASCIIToUTF16("secret");
551 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
554 // We should be able to extract the autocompletetype attribute.
555 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) {
557 "<INPUT type=\"text\" id=\"absent\"/>"
558 "<INPUT type=\"text\" id=\"empty\" autocomplete=\"\"/>"
559 "<INPUT type=\"text\" id=\"off\" autocomplete=\"off\"/>"
560 "<INPUT type=\"text\" id=\"regular\" autocomplete=\"email\"/>"
561 "<INPUT type=\"text\" id=\"multi-valued\" "
562 " autocomplete=\"billing email\"/>"
563 "<INPUT type=\"text\" id=\"experimental\" x-autocompletetype=\"email\"/>"
564 "<INPUT type=\"month\" id=\"month\" autocomplete=\"cc-exp\"/>"
565 "<SELECT id=\"select\" autocomplete=\"state\"/>"
566 " <OPTION value=\"CA\">California</OPTION>"
567 " <OPTION value=\"TX\">Texas</OPTION>"
569 "<TEXTAREA id=\"textarea\" autocomplete=\"street-address\">"
574 "<INPUT type=\"text\" id=\"malicious\" autocomplete=\"" +
575 std::string(10000, 'x') + "\"/>";
576 LoadHTML(html.c_str());
578 WebFrame* frame = GetMainFrame();
579 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
582 const std::string element_id;
583 const std::string form_control_type;
584 const std::string autocomplete_attribute;
586 TestCase test_cases[] = {
587 // An absent attribute is equivalent to an empty one.
588 { "absent", "text", "" },
589 // Make sure there are no issues parsing an empty attribute.
590 { "empty", "text", "" },
591 // Make sure there are no issues parsing an attribute value that isn't a
593 { "off", "text", "off" },
594 // Common case: exactly one type specified.
595 { "regular", "text", "email" },
596 // Verify that we correctly extract multiple tokens as well.
597 { "multi-valued", "text", "billing email" },
598 // Verify that <input type="month"> fields are supported.
599 { "month", "month", "cc-exp" },
600 // We previously extracted this data from the experimental
601 // 'x-autocompletetype' attribute. Now that the field type hints are part
602 // of the spec under the autocomplete attribute, we no longer support the
603 // experimental version.
604 { "experimental", "text", "" },
605 // <select> elements should behave no differently from text fields here.
606 { "select", "select-one", "state" },
607 // <textarea> elements should also behave no differently from text fields.
608 { "textarea", "textarea", "street-address" },
609 // Very long attribute values should be replaced by a default string, to
610 // prevent malicious websites from DOSing the browser process.
611 { "malicious", "text", "x-max-data-length-exceeded" },
614 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
615 WebElement web_element = frame->document().getElementById(
616 ASCIIToUTF16(test_cases[i].element_id));
617 WebFormControlElement element = web_element.to<WebFormControlElement>();
618 FormFieldData result;
619 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result);
621 FormFieldData expected;
622 expected.name = ASCIIToUTF16(test_cases[i].element_id);
623 expected.form_control_type = test_cases[i].form_control_type;
624 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute;
625 if (test_cases[i].form_control_type == "text")
626 expected.max_length = WebInputElement::defaultMaxLength();
628 expected.max_length = 0;
630 SCOPED_TRACE(test_cases[i].element_id);
631 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
635 TEST_F(FormAutofillTest, WebFormElementToFormData) {
636 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
637 " <LABEL for=\"firstname\">First name:</LABEL>"
638 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
639 " <LABEL for=\"lastname\">Last name:</LABEL>"
640 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
641 " <LABEL for=\"street-address\">Address:</LABEL>"
642 " <TEXTAREA id=\"street-address\">"
643 "123 Fantasy Ln. "
646 " <LABEL for=\"state\">State:</LABEL>"
647 " <SELECT id=\"state\"/>"
648 " <OPTION value=\"CA\">California</OPTION>"
649 " <OPTION value=\"TX\">Texas</OPTION>"
651 " <LABEL for=\"password\">Password:</LABEL>"
652 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>"
653 " <LABEL for=\"month\">Card expiration:</LABEL>"
654 " <INPUT type=\"month\" id=\"month\" value=\"2011-12\"/>"
655 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
656 // The below inputs should be ignored
657 " <LABEL for=\"notvisible\">Hidden:</LABEL>"
658 " <INPUT type=\"hidden\" id=\"notvisible\" value=\"apple\"/>"
661 WebFrame* frame = GetMainFrame();
662 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
664 WebVector<WebFormElement> forms;
665 frame->document().forms(forms);
666 ASSERT_EQ(1U, forms.size());
668 WebElement element = frame->document().getElementById("firstname");
669 WebInputElement input_element = element.to<WebInputElement>();
673 EXPECT_TRUE(WebFormElementToFormData(forms[0],
675 autofill::REQUIRE_NONE,
676 autofill::EXTRACT_VALUE,
679 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
680 EXPECT_EQ(GURL(frame->document().url()), form.origin);
681 EXPECT_EQ(GURL("http://cnn.com"), form.action);
683 const std::vector<FormFieldData>& fields = form.fields;
684 ASSERT_EQ(6U, fields.size());
686 FormFieldData expected;
687 expected.name = ASCIIToUTF16("firstname");
688 expected.value = ASCIIToUTF16("John");
689 expected.label = ASCIIToUTF16("First name:");
690 expected.form_control_type = "text";
691 expected.max_length = WebInputElement::defaultMaxLength();
692 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
694 expected.name = ASCIIToUTF16("lastname");
695 expected.value = ASCIIToUTF16("Smith");
696 expected.label = ASCIIToUTF16("Last name:");
697 expected.form_control_type = "text";
698 expected.max_length = WebInputElement::defaultMaxLength();
699 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
701 expected.name = ASCIIToUTF16("street-address");
702 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
703 expected.label = ASCIIToUTF16("Address:");
704 expected.form_control_type = "textarea";
705 expected.max_length = 0;
706 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
708 expected.name = ASCIIToUTF16("state");
709 expected.value = ASCIIToUTF16("CA");
710 expected.label = ASCIIToUTF16("State:");
711 expected.form_control_type = "select-one";
712 expected.max_length = 0;
713 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
715 expected.name = ASCIIToUTF16("password");
716 expected.value = ASCIIToUTF16("secret");
717 expected.label = ASCIIToUTF16("Password:");
718 expected.form_control_type = "password";
719 expected.max_length = WebInputElement::defaultMaxLength();
720 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
722 expected.name = ASCIIToUTF16("month");
723 expected.value = ASCIIToUTF16("2011-12");
724 expected.label = ASCIIToUTF16("Card expiration:");
725 expected.form_control_type = "month";
726 expected.max_length = 0;
727 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
730 // We should not be able to serialize a form with too many fillable fields.
731 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) {
733 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">";
734 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) {
735 html += "<INPUT type=\"text\"/>";
738 LoadHTML(html.c_str());
740 WebFrame* frame = GetMainFrame();
741 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
743 WebVector<WebFormElement> forms;
744 frame->document().forms(forms);
745 ASSERT_EQ(1U, forms.size());
747 WebElement element = frame->document().getElementById("firstname");
748 WebInputElement input_element = element.to<WebInputElement>();
752 EXPECT_FALSE(WebFormElementToFormData(forms[0],
754 autofill::REQUIRE_NONE,
755 autofill::EXTRACT_VALUE,
760 TEST_F(FormAutofillTest, ExtractForms) {
761 ExpectJohnSmithLabels(
762 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
763 " First name: <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
764 " Last name: <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
765 " Email: <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
766 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
770 TEST_F(FormAutofillTest, ExtractMultipleForms) {
771 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
772 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
773 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
774 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
775 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
777 "<FORM name=\"TestForm2\" action=\"http://zoo.com\" method=\"post\">"
778 " <INPUT type=\"text\" id=\"firstname\" value=\"Jack\"/>"
779 " <INPUT type=\"text\" id=\"lastname\" value=\"Adams\"/>"
780 " <INPUT type=\"text\" id=\"email\" value=\"jack@example.com\"/>"
781 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
784 WebFrame* web_frame = GetMainFrame();
785 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
787 FormCache form_cache;
788 std::vector<FormData> forms;
789 form_cache.ExtractForms(*web_frame, &forms);
790 ASSERT_EQ(2U, forms.size());
793 const FormData& form = forms[0];
794 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
795 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
796 EXPECT_EQ(GURL("http://cnn.com"), form.action);
798 const std::vector<FormFieldData>& fields = form.fields;
799 ASSERT_EQ(3U, fields.size());
801 FormFieldData expected;
802 expected.form_control_type = "text";
803 expected.max_length = WebInputElement::defaultMaxLength();
805 expected.name = ASCIIToUTF16("firstname");
806 expected.value = ASCIIToUTF16("John");
807 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
809 expected.name = ASCIIToUTF16("lastname");
810 expected.value = ASCIIToUTF16("Smith");
811 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
813 expected.name = ASCIIToUTF16("email");
814 expected.value = ASCIIToUTF16("john@example.com");
815 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
818 const FormData& form2 = forms[1];
819 EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2.name);
820 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
821 EXPECT_EQ(GURL("http://zoo.com"), form2.action);
823 const std::vector<FormFieldData>& fields2 = form2.fields;
824 ASSERT_EQ(3U, fields2.size());
826 expected.name = ASCIIToUTF16("firstname");
827 expected.value = ASCIIToUTF16("Jack");
828 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
830 expected.name = ASCIIToUTF16("lastname");
831 expected.value = ASCIIToUTF16("Adams");
832 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
834 expected.name = ASCIIToUTF16("email");
835 expected.value = ASCIIToUTF16("jack@example.com");
836 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
839 // We should not extract a form if it has too few fillable fields.
840 TEST_F(FormAutofillTest, ExtractFormsTooFewFields) {
841 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
842 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
843 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
844 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
847 WebFrame* web_frame = GetMainFrame();
848 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
850 FormCache form_cache;
851 std::vector<FormData> forms;
852 form_cache.ExtractForms(*web_frame, &forms);
853 EXPECT_EQ(0U, forms.size());
856 // We should not report additional forms for empty forms.
857 TEST_F(FormAutofillTest, ExtractFormsSkippedForms) {
858 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
859 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
860 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
863 WebFrame* web_frame = GetMainFrame();
864 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
866 FormCache form_cache;
867 std::vector<FormData> forms;
868 bool has_skipped_forms = form_cache.ExtractFormsAndFormElements(*web_frame,
872 EXPECT_EQ(0U, forms.size());
873 EXPECT_TRUE(has_skipped_forms);
876 // We should not report additional forms for empty forms.
877 TEST_F(FormAutofillTest, ExtractFormsNoFields) {
878 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
881 WebFrame* web_frame = GetMainFrame();
882 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
884 FormCache form_cache;
885 std::vector<FormData> forms;
886 bool has_skipped_forms = form_cache.ExtractFormsAndFormElements(*web_frame,
890 EXPECT_EQ(0U, forms.size());
891 EXPECT_FALSE(has_skipped_forms);
894 // We should not extract a form if it has too few fillable fields.
895 // Make sure radio and checkbox fields don't count.
896 TEST_F(FormAutofillTest, ExtractFormsTooFewFieldsSkipsCheckable) {
897 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
898 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
899 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
900 " <INPUT type=\"radio\" id=\"a_radio\" value=\"0\"/>"
901 " <INPUT type=\"checkbox\" id=\"a_check\" value=\"1\"/>"
902 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
905 WebFrame* web_frame = GetMainFrame();
906 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
908 FormCache form_cache;
909 std::vector<FormData> forms;
910 form_cache.ExtractForms(*web_frame, &forms);
911 EXPECT_EQ(0U, forms.size());
914 TEST_F(FormAutofillTest, WebFormElementToFormDataAutocomplete) {
916 // Form is not auto-completable due to autocomplete=off.
917 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\""
919 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
920 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
921 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
922 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
925 WebFrame* web_frame = GetMainFrame();
926 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
928 WebVector<WebFormElement> web_forms;
929 web_frame->document().forms(web_forms);
930 ASSERT_EQ(1U, web_forms.size());
931 WebFormElement web_form = web_forms[0];
934 EXPECT_TRUE(WebFormElementToFormData(
935 web_form, WebFormControlElement(), autofill::REQUIRE_NONE,
936 autofill::EXTRACT_NONE, &form, NULL));
937 EXPECT_FALSE(WebFormElementToFormData(
938 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
939 autofill::EXTRACT_NONE, &form, NULL));
943 // The firstname element is not auto-completable due to autocomplete=off.
944 LoadHTML("<FORM name=\"TestForm\" action=\"http://abc.com\" "
946 " <INPUT type=\"text\" id=\"firstname\" value=\"John\""
948 " <INPUT type=\"text\" id=\"middlename\" value=\"Jack\"/>"
949 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
950 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
951 " <INPUT type=\"submit\" name=\"reply\" value=\"Send\"/>"
954 WebFrame* web_frame = GetMainFrame();
955 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
957 WebVector<WebFormElement> web_forms;
958 web_frame->document().forms(web_forms);
959 ASSERT_EQ(1U, web_forms.size());
960 WebFormElement web_form = web_forms[0];
963 EXPECT_TRUE(WebFormElementToFormData(
964 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
965 autofill::EXTRACT_VALUE, &form, NULL));
967 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
968 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
969 EXPECT_EQ(GURL("http://abc.com"), form.action);
971 const std::vector<FormFieldData>& fields = form.fields;
972 ASSERT_EQ(3U, fields.size());
974 FormFieldData expected;
975 expected.form_control_type = "text";
976 expected.max_length = WebInputElement::defaultMaxLength();
978 expected.name = ASCIIToUTF16("middlename");
979 expected.value = ASCIIToUTF16("Jack");
980 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
982 expected.name = ASCIIToUTF16("lastname");
983 expected.value = ASCIIToUTF16("Smith");
984 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
986 expected.name = ASCIIToUTF16("email");
987 expected.value = ASCIIToUTF16("john@example.com");
988 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
992 TEST_F(FormAutofillTest, FindForm) {
993 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
994 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
995 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
996 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\""
997 "autocomplete=\"off\" />"
998 " <INPUT type=\"text\" id=\"phone\" value=\"1.800.555.1234\"/>"
999 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1002 WebFrame* web_frame = GetMainFrame();
1003 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1005 FormCache form_cache;
1006 std::vector<FormData> forms;
1007 form_cache.ExtractForms(*web_frame, &forms);
1008 ASSERT_EQ(1U, forms.size());
1010 // Get the input element we want to find.
1011 WebElement element = web_frame->document().getElementById("firstname");
1012 WebInputElement input_element = element.to<WebInputElement>();
1014 // Find the form and verify it's the correct form.
1016 FormFieldData field;
1017 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
1018 autofill::REQUIRE_NONE));
1019 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1020 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1021 EXPECT_EQ(GURL("http://buh.com"), form.action);
1023 const std::vector<FormFieldData>& fields = form.fields;
1024 ASSERT_EQ(4U, fields.size());
1026 FormFieldData expected;
1027 expected.form_control_type = "text";
1028 expected.max_length = WebInputElement::defaultMaxLength();
1030 expected.name = ASCIIToUTF16("firstname");
1031 expected.value = ASCIIToUTF16("John");
1032 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1033 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1035 expected.name = ASCIIToUTF16("lastname");
1036 expected.value = ASCIIToUTF16("Smith");
1037 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1039 expected.name = ASCIIToUTF16("email");
1040 expected.value = ASCIIToUTF16("john@example.com");
1041 expected.autocomplete_attribute = "off";
1042 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1043 expected.autocomplete_attribute = std::string(); // reset
1045 expected.name = ASCIIToUTF16("phone");
1046 expected.value = ASCIIToUTF16("1.800.555.1234");
1047 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1049 // Try again, but require autocomplete.
1051 FormFieldData field2;
1052 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
1053 autofill::REQUIRE_AUTOCOMPLETE));
1054 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1055 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1056 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1058 const std::vector<FormFieldData>& fields2 = form2.fields;
1059 ASSERT_EQ(3U, fields2.size());
1061 expected.form_control_type = "text";
1062 expected.max_length = WebInputElement::defaultMaxLength();
1064 expected.name = ASCIIToUTF16("firstname");
1065 expected.value = ASCIIToUTF16("John");
1066 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1067 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1069 expected.name = ASCIIToUTF16("lastname");
1070 expected.value = ASCIIToUTF16("Smith");
1071 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1073 expected.name = ASCIIToUTF16("phone");
1074 expected.value = ASCIIToUTF16("1.800.555.1234");
1075 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1078 // Test regular FillForm function.
1079 TEST_F(FormAutofillTest, FillForm) {
1080 static const AutofillFieldCase field_cases[] = {
1081 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1082 // should_be_autofilled, autofill_value, expected_value
1084 // Regular empty fields (firstname & lastname) should be autofilled.
1085 {"text", "firstname", "", "", true, "filled firstname",
1086 "filled firstname"},
1087 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1088 // hidden fields should not be extracted to form_data.
1089 // Non empty fields should not be autofilled.
1090 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
1091 // "noautocomplete" should not be extracted to form_data.
1092 // Disabled fields should not be autofilled.
1093 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1094 // Readonly fields should not be autofilled.
1095 {"text", "readonly", "", "", false, "filled readonly", ""},
1096 // Fields with "visibility: hidden" should not be autofilled.
1097 {"text", "invisible", "", "", false, "filled invisible", ""},
1098 // Fields with "display:none" should not be autofilled.
1099 {"text", "displaynone", "", "", false, "filled displaynone", ""},
1100 // Regular <input type="month"> should be autofilled.
1101 {"month", "month", "", "", true, "2017-11", "2017-11"},
1102 // Non-empty <input type="month"> should not be autofilled.
1103 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
1104 // Regular select fields should be autofilled.
1105 {"select-one", "select", "", "", true, "TX", "TX"},
1106 // Select fields should be autofilled even if they already have a
1108 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1109 // Regular textarea elements should be autofilled.
1110 {"textarea", "textarea", "", "", true, "some multi-\nline value",
1111 "some multi-\nline value"},
1112 // Non-empty textarea elements should not be autofilled.
1113 {"textarea", "textarea-nonempty", "Go\naway!", "", false,
1114 "some multi-\nline value", "Go\naway!"},
1116 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1117 FillForm, &WebInputElement::value);
1118 // Verify preview selection.
1119 WebInputElement firstname = GetMainFrame()->document().
1120 getElementById("firstname").to<WebInputElement>();
1121 EXPECT_EQ(16, firstname.selectionStart());
1122 EXPECT_EQ(16, firstname.selectionEnd());
1125 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) {
1126 static const AutofillFieldCase field_cases[] = {
1127 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1128 // should_be_autofilled, autofill_value, expected_value
1130 // Regular empty fields (firstname & lastname) should be autofilled.
1131 {"text", "firstname", "", "", true, "filled firstname",
1132 "filled firstname"},
1133 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1134 // hidden fields should not be extracted to form_data.
1135 // Non empty fields should be overriden.
1136 {"text", "notempty", "Hi", "", true, "filled notempty",
1138 // "noautocomplete" should not be extracted to form_data.
1139 // Disabled fields should not be autofilled.
1140 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1141 // Readonly fields should not be autofilled.
1142 {"text", "readonly", "", "", false, "filled readonly", ""},
1143 // Fields with "visibility: hidden" should also be autofilled.
1144 {"text", "invisible", "", "", true, "filled invisible",
1145 "filled invisible"},
1146 // Fields with "display:none" should also be autofilled.
1147 {"text", "displaynone", "", "", true, "filled displaynone",
1148 "filled displaynone"},
1149 // Regular <input type="month"> should be autofilled.
1150 {"month", "month", "", "", true, "2017-11", "2017-11"},
1151 // Non-empty <input type="month"> should be overridden.
1152 {"month", "month-nonempty", "2011-12", "", true, "2017-11", "2017-11"},
1153 // Regular select fields should be autofilled.
1154 {"select-one", "select", "", "", true, "TX", "TX"},
1155 // Select fields should be autofilled even if they already have a
1157 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1158 // Regular textarea elements should be autofilled.
1159 {"textarea", "textarea", "", "", true, "some multi-\nline value",
1160 "some multi-\nline value"},
1161 // Nonempty textarea elements should be overridden.
1162 {"textarea", "textarea-nonempty", "Go\naway!", "", true,
1163 "some multi-\nline value", "some multi-\nline value"},
1165 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1166 &FillFormIncludingNonFocusableElementsWrapper,
1167 &WebInputElement::value);
1170 TEST_F(FormAutofillTest, PreviewForm) {
1171 static const char* html =
1172 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
1173 " <INPUT type=\"text\" id=\"firstname\"/>"
1174 " <INPUT type=\"text\" id=\"lastname\"/>"
1175 " <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>"
1176 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>"
1177 " <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>"
1178 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1181 static const AutofillFieldCase field_cases[] = {
1182 // Normal empty fields should be previewed.
1183 {"text", "firstname", "", "", true, "suggested firstname",
1184 "suggested firstname"},
1185 {"text", "lastname", "", "", true, "suggested lastname",
1186 "suggested lastname"},
1187 // Non empty fields should not be previewed.
1188 {"text", "notempty", "Hi", "", false, "filled notempty", ""},
1189 // "noautocomplete" should not be extracted to form_data.
1190 // Disabled fields should not be previewed.
1191 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1193 TestFormFillFunctions(html, field_cases, arraysize(field_cases), &PreviewForm,
1194 &WebInputElement::suggestedValue);
1196 // Verify preview selection.
1197 WebInputElement firstname = GetMainFrame()->document().
1198 getElementById("firstname").to<WebInputElement>();
1199 EXPECT_EQ(0, firstname.selectionStart());
1200 EXPECT_EQ(19, firstname.selectionEnd());
1203 TEST_F(FormAutofillTest, Labels) {
1204 ExpectJohnSmithLabels(
1205 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1206 " <LABEL for=\"firstname\"> First name: </LABEL>"
1207 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1208 " <LABEL for=\"lastname\"> Last name: </LABEL>"
1209 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1210 " <LABEL for=\"email\"> Email: </LABEL>"
1211 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1212 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1216 TEST_F(FormAutofillTest, LabelsWithSpans) {
1217 ExpectJohnSmithLabels(
1218 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1219 " <LABEL for=\"firstname\"><span>First name: </span></LABEL>"
1220 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1221 " <LABEL for=\"lastname\"><span>Last name: </span></LABEL>"
1222 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1223 " <LABEL for=\"email\"><span>Email: </span></LABEL>"
1224 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1225 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1229 // This test is different from FormAutofillTest.Labels in that the label
1230 // elements for= attribute is set to the name of the form control element it is
1231 // a label for instead of the id of the form control element. This is invalid
1232 // because the for= attribute must be set to the id of the form control element;
1233 // however, current label parsing code will extract the text from the previous
1234 // label element and apply it to the following input field.
1235 TEST_F(FormAutofillTest, InvalidLabels) {
1236 ExpectJohnSmithLabels(
1237 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1238 " <LABEL for=\"firstname\"> First name: </LABEL>"
1239 " <INPUT type=\"text\" name=\"firstname\" value=\"John\"/>"
1240 " <LABEL for=\"lastname\"> Last name: </LABEL>"
1241 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\"/>"
1242 " <LABEL for=\"email\"> Email: </LABEL>"
1243 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\"/>"
1244 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1248 // This test has three form control elements, only one of which has a label
1249 // element associated with it.
1250 TEST_F(FormAutofillTest, OneLabelElement) {
1251 ExpectJohnSmithLabels(
1252 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1254 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1255 " <LABEL for=\"lastname\">Last name: </LABEL>"
1256 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1258 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1259 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1263 TEST_F(FormAutofillTest, LabelsInferredFromText) {
1264 ExpectJohnSmithLabels(
1265 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1267 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1269 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1271 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1272 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1276 TEST_F(FormAutofillTest, LabelsInferredFromParagraph) {
1277 ExpectJohnSmithLabels(
1278 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1279 " <P>First name:</P><INPUT type=\"text\" "
1280 " id=\"firstname\" value=\"John\"/>"
1281 " <P>Last name:</P>"
1282 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1284 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1285 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1289 TEST_F(FormAutofillTest, LabelsInferredFromBold) {
1290 ExpectJohnSmithLabels(
1291 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1292 " <B>First name:</B><INPUT type=\"text\" "
1293 " id=\"firstname\" value=\"John\"/>"
1294 " <B>Last name:</B>"
1295 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1297 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1298 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1302 TEST_F(FormAutofillTest, LabelsInferredPriorToImgOrBr) {
1303 ExpectJohnSmithLabels(
1304 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1305 " First name:<IMG/><INPUT type=\"text\" "
1306 " id=\"firstname\" value=\"John\"/>"
1308 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1310 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1311 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1315 TEST_F(FormAutofillTest, LabelsInferredFromTableCell) {
1316 ExpectJohnSmithLabels(
1317 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1320 " <TD>First name:</TD>"
1321 " <TD><INPUT type=\"text\" id=\"firstname\" value=\"John\"/></TD>"
1324 " <TD>Last name:</TD>"
1325 " <TD><INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/></TD>"
1329 " <TD><INPUT type=\"text\" id=\"email\""
1330 " value=\"john@example.com\"/></TD>"
1335 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1342 TEST_F(FormAutofillTest, LabelsInferredFromTableCellTH) {
1343 ExpectJohnSmithLabels(
1344 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1347 " <TH>First name:</TH>"
1348 " <TD><INPUT type=\"text\" id=\"firstname\" value=\"John\"/></TD>"
1351 " <TH>Last name:</TH>"
1352 " <TD><INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/></TD>"
1356 " <TD><INPUT type=\"text\" id=\"email\""
1357 " value=\"john@example.com\"/></TD>"
1362 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1369 TEST_F(FormAutofillTest, LabelsInferredFromTableCellNested) {
1370 std::vector<string16> labels, names, values;
1372 labels.push_back(ASCIIToUTF16("First name: Bogus"));
1373 names.push_back(ASCIIToUTF16("firstname"));
1374 values.push_back(ASCIIToUTF16("John"));
1376 labels.push_back(ASCIIToUTF16("Last name:"));
1377 names.push_back(ASCIIToUTF16("lastname"));
1378 values.push_back(ASCIIToUTF16("Smith"));
1380 labels.push_back(ASCIIToUTF16("Email:"));
1381 names.push_back(ASCIIToUTF16("email"));
1382 values.push_back(ASCIIToUTF16("john@example.com"));
1385 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1398 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1410 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1422 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1429 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1434 labels, names, values);
1437 TEST_F(FormAutofillTest, LabelsInferredFromTableEmptyTDs) {
1438 std::vector<string16> labels, names, values;
1440 labels.push_back(ASCIIToUTF16("* First Name"));
1441 names.push_back(ASCIIToUTF16("firstname"));
1442 values.push_back(ASCIIToUTF16("John"));
1444 labels.push_back(ASCIIToUTF16("* Last Name"));
1445 names.push_back(ASCIIToUTF16("lastname"));
1446 values.push_back(ASCIIToUTF16("Smith"));
1448 labels.push_back(ASCIIToUTF16("* Email"));
1449 names.push_back(ASCIIToUTF16("email"));
1450 values.push_back(ASCIIToUTF16("john@example.com"));
1453 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1458 " <B>First Name</B>"
1462 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1472 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1482 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1488 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1493 labels, names, values);
1496 TEST_F(FormAutofillTest, LabelsInferredFromPreviousTD) {
1497 std::vector<string16> labels, names, values;
1499 labels.push_back(ASCIIToUTF16("* First Name"));
1500 names.push_back(ASCIIToUTF16("firstname"));
1501 values.push_back(ASCIIToUTF16("John"));
1503 labels.push_back(ASCIIToUTF16("* Last Name"));
1504 names.push_back(ASCIIToUTF16("lastname"));
1505 values.push_back(ASCIIToUTF16("Smith"));
1507 labels.push_back(ASCIIToUTF16("* Email"));
1508 names.push_back(ASCIIToUTF16("email"));
1509 values.push_back(ASCIIToUTF16("john@example.com"));
1512 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1515 " <TD>* First Name</TD>"
1518 " <INPUT type=\"hidden\"/>"
1519 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1523 " <TD>* Last Name</TD>"
1525 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1531 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1537 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1542 labels, names, values);
1545 // <script>, <noscript> and <option> tags are excluded when the labels are
1547 // Also <!-- comment --> is excluded.
1548 TEST_F(FormAutofillTest, LabelsInferredFromTableWithSpecialElements) {
1549 std::vector<string16> labels, names, values;
1550 std::vector<std::string> control_types;
1552 labels.push_back(ASCIIToUTF16("* First Name"));
1553 names.push_back(ASCIIToUTF16("firstname"));
1554 values.push_back(ASCIIToUTF16("John"));
1555 control_types.push_back("text");
1557 labels.push_back(ASCIIToUTF16("* Middle Name"));
1558 names.push_back(ASCIIToUTF16("middlename"));
1559 values.push_back(ASCIIToUTF16("Joe"));
1560 control_types.push_back("text");
1562 labels.push_back(ASCIIToUTF16("* Last Name"));
1563 names.push_back(ASCIIToUTF16("lastname"));
1564 values.push_back(ASCIIToUTF16("Smith"));
1565 control_types.push_back("text");
1567 labels.push_back(ASCIIToUTF16("* Country"));
1568 names.push_back(ASCIIToUTF16("country"));
1569 values.push_back(ASCIIToUTF16("US"));
1570 control_types.push_back("select-one");
1572 labels.push_back(ASCIIToUTF16("* Email"));
1573 names.push_back(ASCIIToUTF16("email"));
1574 values.push_back(ASCIIToUTF16("john@example.com"));
1575 control_types.push_back("text");
1577 ExpectLabelsAndTypes(
1578 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1583 " <B>First Name</B>"
1586 " <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
1588 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1594 " <B>Middle Name</B>"
1600 " <INPUT type=\"text\" id=\"middlename\" value=\"Joe\"/>"
1609 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1618 " <SELECT id=\"country\">"
1619 " <OPTION VALUE=\"US\">The value should be ignored as label."
1621 " <OPTION VALUE=\"JP\">JAPAN</OPTION>"
1631 " <!-- This comment should be ignored as inferred label.-->"
1632 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1638 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1643 labels, names, values, control_types);
1646 TEST_F(FormAutofillTest, LabelsInferredFromTableLabels) {
1647 ExpectJohnSmithLabels(
1648 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1652 " <LABEL>First name:</LABEL>"
1653 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1658 " <LABEL>Last name:</LABEL>"
1659 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1664 " <LABEL>Email:</LABEL>"
1665 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1669 "<INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1673 TEST_F(FormAutofillTest, LabelsInferredFromTableTDInterveningElements) {
1674 ExpectJohnSmithLabels(
1675 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1681 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1688 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1695 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1699 "<INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1703 // Verify that we correctly infer labels when the label text spans multiple
1704 // adjacent HTML elements, not separated by whitespace.
1705 TEST_F(FormAutofillTest, LabelsInferredFromTableAdjacentElements) {
1706 std::vector<string16> labels, names, values;
1708 labels.push_back(ASCIIToUTF16("*First Name"));
1709 names.push_back(ASCIIToUTF16("firstname"));
1710 values.push_back(ASCIIToUTF16("John"));
1712 labels.push_back(ASCIIToUTF16("*Last Name"));
1713 names.push_back(ASCIIToUTF16("lastname"));
1714 values.push_back(ASCIIToUTF16("Smith"));
1716 labels.push_back(ASCIIToUTF16("*Email"));
1717 names.push_back(ASCIIToUTF16("email"));
1718 values.push_back(ASCIIToUTF16("john@example.com"));
1721 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1725 " <SPAN>*</SPAN><B>First Name</B>"
1728 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1733 " <SPAN>*</SPAN><B>Last Name</B>"
1736 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1741 " <SPAN>*</SPAN><B>Email</B>"
1744 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1749 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1754 labels, names, values);
1757 // Verify that we correctly infer labels when the label text resides in the
1759 TEST_F(FormAutofillTest, LabelsInferredFromTableRow) {
1760 std::vector<string16> labels, names, values;
1762 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1763 names.push_back(ASCIIToUTF16("firstname"));
1764 values.push_back(ASCIIToUTF16("John"));
1766 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1767 names.push_back(ASCIIToUTF16("lastname"));
1768 values.push_back(ASCIIToUTF16("Smith"));
1770 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1771 names.push_back(ASCIIToUTF16("email"));
1772 values.push_back(ASCIIToUTF16("john@example.com"));
1775 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1778 " <TD>*First Name</TD>"
1779 " <TD>*Last Name</TD>"
1784 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1787 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1790 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1795 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1799 labels, names, values);
1802 // Verify that we correctly infer labels when enclosed within a list item.
1803 TEST_F(FormAutofillTest, LabelsInferredFromListItem) {
1804 std::vector<string16> labels, names, values;
1806 labels.push_back(ASCIIToUTF16("* Home Phone"));
1807 names.push_back(ASCIIToUTF16("areacode"));
1808 values.push_back(ASCIIToUTF16("415"));
1810 labels.push_back(ASCIIToUTF16("* Home Phone"));
1811 names.push_back(ASCIIToUTF16("prefix"));
1812 values.push_back(ASCIIToUTF16("555"));
1814 labels.push_back(ASCIIToUTF16("* Home Phone"));
1815 names.push_back(ASCIIToUTF16("suffix"));
1816 values.push_back(ASCIIToUTF16("1212"));
1819 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1822 " <SPAN>Bogus</SPAN>"
1825 " <LABEL><EM>*</EM> Home Phone</LABEL>"
1826 " <INPUT type=\"text\" id=\"areacode\" value=\"415\"/>"
1827 " <INPUT type=\"text\" id=\"prefix\" value=\"555\"/>"
1828 " <INPUT type=\"text\" id=\"suffix\" value=\"1212\"/>"
1831 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1835 labels, names, values);
1838 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionList) {
1839 std::vector<string16> labels, names, values;
1841 labels.push_back(ASCIIToUTF16("* First name: Bogus"));
1842 names.push_back(ASCIIToUTF16("firstname"));
1843 values.push_back(ASCIIToUTF16("John"));
1845 labels.push_back(ASCIIToUTF16("Last name:"));
1846 names.push_back(ASCIIToUTF16("lastname"));
1847 values.push_back(ASCIIToUTF16("Smith"));
1849 labels.push_back(ASCIIToUTF16("Email:"));
1850 names.push_back(ASCIIToUTF16("email"));
1851 values.push_back(ASCIIToUTF16("john@example.com"));
1854 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1869 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1879 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1889 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1894 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1898 labels, names, values);
1901 TEST_F(FormAutofillTest, LabelsInferredWithSameName) {
1902 std::vector<string16> labels, names, values;
1904 labels.push_back(ASCIIToUTF16("Address Line 1:"));
1905 names.push_back(ASCIIToUTF16("Address"));
1906 values.push_back(string16());
1908 labels.push_back(ASCIIToUTF16("Address Line 2:"));
1909 names.push_back(ASCIIToUTF16("Address"));
1910 values.push_back(string16());
1912 labels.push_back(ASCIIToUTF16("Address Line 3:"));
1913 names.push_back(ASCIIToUTF16("Address"));
1914 values.push_back(string16());
1917 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1919 " <INPUT type=\"text\" name=\"Address\"/>"
1921 " <INPUT type=\"text\" name=\"Address\"/>"
1923 " <INPUT type=\"text\" name=\"Address\"/>"
1924 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1926 labels, names, values);
1929 TEST_F(FormAutofillTest, LabelsInferredWithImageTags) {
1930 std::vector<string16> labels, names, values;
1932 labels.push_back(ASCIIToUTF16("Phone:"));
1933 names.push_back(ASCIIToUTF16("dayphone1"));
1934 values.push_back(string16());
1936 labels.push_back(ASCIIToUTF16("-"));
1937 names.push_back(ASCIIToUTF16("dayphone2"));
1938 values.push_back(string16());
1940 labels.push_back(ASCIIToUTF16("-"));
1941 names.push_back(ASCIIToUTF16("dayphone3"));
1942 values.push_back(string16());
1944 labels.push_back(ASCIIToUTF16("ext.:"));
1945 names.push_back(ASCIIToUTF16("dayphone4"));
1946 values.push_back(string16());
1948 labels.push_back(string16());
1949 names.push_back(ASCIIToUTF16("dummy"));
1950 values.push_back(string16());
1953 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1955 " <input type=\"text\" name=\"dayphone1\">"
1959 " <input type=\"text\" name=\"dayphone2\">"
1963 " <input type=\"text\" name=\"dayphone3\">"
1965 " <input type=\"text\" name=\"dayphone4\">"
1966 " <input type=\"text\" name=\"dummy\">"
1967 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
1969 labels, names, values);
1972 TEST_F(FormAutofillTest, LabelsInferredFromDivTable) {
1973 ExpectJohnSmithLabels(
1974 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1975 "<DIV>First name:<BR>"
1977 " <INPUT type=\"text\" name=\"firstname\" value=\"John\">"
1980 "<DIV>Last name:<BR>"
1982 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\">"
1987 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\">"
1990 "<input type=\"submit\" name=\"reply-send\" value=\"Send\">"
1994 TEST_F(FormAutofillTest, LabelsInferredFromDivSiblingTable) {
1995 ExpectJohnSmithLabels(
1996 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1997 "<DIV>First name:</DIV>"
2000 " <INPUT type=\"text\" name=\"firstname\" value=\"John\">"
2003 "<DIV>Last name:</DIV>"
2006 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\">"
2012 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\">"
2015 "<input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2019 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionListRatherThanDivTable) {
2020 ExpectJohnSmithLabels(
2021 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2022 "<DIV>This is not a label.<BR>"
2031 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
2041 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
2051 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
2056 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2063 TEST_F(FormAutofillTest, FillFormMaxLength) {
2064 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2065 " <INPUT type=\"text\" id=\"firstname\" maxlength=\"5\"/>"
2066 " <INPUT type=\"text\" id=\"lastname\" maxlength=\"7\"/>"
2067 " <INPUT type=\"text\" id=\"email\" maxlength=\"9\"/>"
2068 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2071 WebFrame* web_frame = GetMainFrame();
2072 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2074 FormCache form_cache;
2075 std::vector<FormData> forms;
2076 form_cache.ExtractForms(*web_frame, &forms);
2077 ASSERT_EQ(1U, forms.size());
2079 // Get the input element we want to find.
2080 WebElement element = web_frame->document().getElementById("firstname");
2081 WebInputElement input_element = element.to<WebInputElement>();
2083 // Find the form that contains the input element.
2085 FormFieldData field;
2086 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2087 autofill::REQUIRE_NONE));
2088 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2089 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2090 EXPECT_EQ(GURL("http://buh.com"), form.action);
2092 const std::vector<FormFieldData>& fields = form.fields;
2093 ASSERT_EQ(3U, fields.size());
2095 FormFieldData expected;
2096 expected.form_control_type = "text";
2098 expected.name = ASCIIToUTF16("firstname");
2099 expected.max_length = 5;
2100 expected.is_autofilled = false;
2101 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2103 expected.name = ASCIIToUTF16("lastname");
2104 expected.max_length = 7;
2105 expected.is_autofilled = false;
2106 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2108 expected.name = ASCIIToUTF16("email");
2109 expected.max_length = 9;
2110 expected.is_autofilled = false;
2111 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2114 form.fields[0].value = ASCIIToUTF16("Brother");
2115 form.fields[1].value = ASCIIToUTF16("Jonathan");
2116 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2117 FillForm(form, input_element);
2119 // Find the newly-filled form that contains the input element.
2121 FormFieldData field2;
2122 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2123 autofill::REQUIRE_NONE));
2125 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2126 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2127 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2129 const std::vector<FormFieldData>& fields2 = form2.fields;
2130 ASSERT_EQ(3U, fields2.size());
2132 expected.form_control_type = "text";
2134 expected.name = ASCIIToUTF16("firstname");
2135 expected.value = ASCIIToUTF16("Broth");
2136 expected.max_length = 5;
2137 expected.is_autofilled = true;
2138 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2140 expected.name = ASCIIToUTF16("lastname");
2141 expected.value = ASCIIToUTF16("Jonatha");
2142 expected.max_length = 7;
2143 expected.is_autofilled = true;
2144 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2146 expected.name = ASCIIToUTF16("email");
2147 expected.value = ASCIIToUTF16("brotherj@");
2148 expected.max_length = 9;
2149 expected.is_autofilled = true;
2150 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2153 // This test uses negative values of the maxlength attribute for input elements.
2154 // In this case, the maxlength of the input elements is set to the default
2155 // maxlength (defined in WebKit.)
2156 TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
2157 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2158 " <INPUT type=\"text\" id=\"firstname\" maxlength=\"-1\"/>"
2159 " <INPUT type=\"text\" id=\"lastname\" maxlength=\"-10\"/>"
2160 " <INPUT type=\"text\" id=\"email\" maxlength=\"-13\"/>"
2161 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2164 WebFrame* web_frame = GetMainFrame();
2165 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2167 FormCache form_cache;
2168 std::vector<FormData> forms;
2169 form_cache.ExtractForms(*web_frame, &forms);
2170 ASSERT_EQ(1U, forms.size());
2172 // Get the input element we want to find.
2173 WebElement element = web_frame->document().getElementById("firstname");
2174 WebInputElement input_element = element.to<WebInputElement>();
2176 // Find the form that contains the input element.
2178 FormFieldData field;
2179 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2180 autofill::REQUIRE_NONE));
2181 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2182 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2183 EXPECT_EQ(GURL("http://buh.com"), form.action);
2185 const std::vector<FormFieldData>& fields = form.fields;
2186 ASSERT_EQ(3U, fields.size());
2188 FormFieldData expected;
2189 expected.form_control_type = "text";
2190 expected.max_length = WebInputElement::defaultMaxLength();
2192 expected.name = ASCIIToUTF16("firstname");
2193 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2195 expected.name = ASCIIToUTF16("lastname");
2196 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2198 expected.name = ASCIIToUTF16("email");
2199 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2202 form.fields[0].value = ASCIIToUTF16("Brother");
2203 form.fields[1].value = ASCIIToUTF16("Jonathan");
2204 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2205 FillForm(form, input_element);
2207 // Find the newly-filled form that contains the input element.
2209 FormFieldData field2;
2210 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2211 autofill::REQUIRE_NONE));
2213 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2214 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2215 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2217 const std::vector<FormFieldData>& fields2 = form2.fields;
2218 ASSERT_EQ(3U, fields2.size());
2220 expected.name = ASCIIToUTF16("firstname");
2221 expected.value = ASCIIToUTF16("Brother");
2222 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2224 expected.name = ASCIIToUTF16("lastname");
2225 expected.value = ASCIIToUTF16("Jonathan");
2226 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2228 expected.name = ASCIIToUTF16("email");
2229 expected.value = ASCIIToUTF16("brotherj@example.com");
2230 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2233 TEST_F(FormAutofillTest, FillFormEmptyName) {
2234 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2235 " <INPUT type=\"text\" id=\"firstname\"/>"
2236 " <INPUT type=\"text\" id=\"lastname\"/>"
2237 " <INPUT type=\"text\" id=\"email\"/>"
2238 " <INPUT type=\"submit\" value=\"Send\"/>"
2241 WebFrame* web_frame = GetMainFrame();
2242 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2244 FormCache form_cache;
2245 std::vector<FormData> forms;
2246 form_cache.ExtractForms(*web_frame, &forms);
2247 ASSERT_EQ(1U, forms.size());
2249 // Get the input element we want to find.
2250 WebElement element = web_frame->document().getElementById("firstname");
2251 WebInputElement input_element = element.to<WebInputElement>();
2253 // Find the form that contains the input element.
2255 FormFieldData field;
2256 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2257 autofill::REQUIRE_NONE));
2258 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2259 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2260 EXPECT_EQ(GURL("http://buh.com"), form.action);
2262 const std::vector<FormFieldData>& fields = form.fields;
2263 ASSERT_EQ(3U, fields.size());
2265 FormFieldData expected;
2266 expected.form_control_type = "text";
2267 expected.max_length = WebInputElement::defaultMaxLength();
2269 expected.name = ASCIIToUTF16("firstname");
2270 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2272 expected.name = ASCIIToUTF16("lastname");
2273 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2275 expected.name = ASCIIToUTF16("email");
2276 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2279 form.fields[0].value = ASCIIToUTF16("Wyatt");
2280 form.fields[1].value = ASCIIToUTF16("Earp");
2281 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2282 FillForm(form, input_element);
2284 // Find the newly-filled form that contains the input element.
2286 FormFieldData field2;
2287 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2288 autofill::REQUIRE_NONE));
2290 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2291 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2292 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2294 const std::vector<FormFieldData>& fields2 = form2.fields;
2295 ASSERT_EQ(3U, fields2.size());
2297 expected.form_control_type = "text";
2298 expected.max_length = WebInputElement::defaultMaxLength();
2300 expected.name = ASCIIToUTF16("firstname");
2301 expected.value = ASCIIToUTF16("Wyatt");
2302 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2304 expected.name = ASCIIToUTF16("lastname");
2305 expected.value = ASCIIToUTF16("Earp");
2306 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2308 expected.name = ASCIIToUTF16("email");
2309 expected.value = ASCIIToUTF16("wyatt@example.com");
2310 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2313 TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
2314 LoadHTML("<FORM action=\"http://buh.com\" method=\"post\">"
2315 " <INPUT type=\"text\" id=\"firstname\"/>"
2316 " <INPUT type=\"text\" id=\"middlename\"/>"
2317 " <INPUT type=\"text\" id=\"lastname\"/>"
2318 " <INPUT type=\"submit\" value=\"Send\"/>"
2320 "<FORM action=\"http://abc.com\" method=\"post\">"
2321 " <INPUT type=\"text\" id=\"apple\"/>"
2322 " <INPUT type=\"text\" id=\"banana\"/>"
2323 " <INPUT type=\"text\" id=\"cantelope\"/>"
2324 " <INPUT type=\"submit\" value=\"Send\"/>"
2327 WebFrame* web_frame = GetMainFrame();
2328 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2330 FormCache form_cache;
2331 std::vector<FormData> forms;
2332 form_cache.ExtractForms(*web_frame, &forms);
2333 ASSERT_EQ(2U, forms.size());
2335 // Get the input element we want to find.
2336 WebElement element = web_frame->document().getElementById("apple");
2337 WebInputElement input_element = element.to<WebInputElement>();
2339 // Find the form that contains the input element.
2341 FormFieldData field;
2342 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2343 autofill::REQUIRE_NONE));
2344 EXPECT_EQ(string16(), form.name);
2345 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2346 EXPECT_EQ(GURL("http://abc.com"), form.action);
2348 const std::vector<FormFieldData>& fields = form.fields;
2349 ASSERT_EQ(3U, fields.size());
2351 FormFieldData expected;
2352 expected.form_control_type = "text";
2353 expected.max_length = WebInputElement::defaultMaxLength();
2355 expected.name = ASCIIToUTF16("apple");
2356 expected.is_autofilled = false;
2357 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2359 expected.name = ASCIIToUTF16("banana");
2360 expected.is_autofilled = false;
2361 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2363 expected.name = ASCIIToUTF16("cantelope");
2364 expected.is_autofilled = false;
2365 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2368 form.fields[0].value = ASCIIToUTF16("Red");
2369 form.fields[1].value = ASCIIToUTF16("Yellow");
2370 form.fields[2].value = ASCIIToUTF16("Also Yellow");
2371 FillForm(form, input_element);
2373 // Find the newly-filled form that contains the input element.
2375 FormFieldData field2;
2376 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2377 autofill::REQUIRE_NONE));
2379 EXPECT_EQ(string16(), form2.name);
2380 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2381 EXPECT_EQ(GURL("http://abc.com"), form2.action);
2383 const std::vector<FormFieldData>& fields2 = form2.fields;
2384 ASSERT_EQ(3U, fields2.size());
2386 expected.name = ASCIIToUTF16("apple");
2387 expected.value = ASCIIToUTF16("Red");
2388 expected.is_autofilled = true;
2389 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2391 expected.name = ASCIIToUTF16("banana");
2392 expected.value = ASCIIToUTF16("Yellow");
2393 expected.is_autofilled = true;
2394 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2396 expected.name = ASCIIToUTF16("cantelope");
2397 expected.value = ASCIIToUTF16("Also Yellow");
2398 expected.is_autofilled = true;
2399 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2402 TEST_F(FormAutofillTest, ThreePartPhone) {
2403 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2405 " <input type=\"text\" name=\"dayphone1\">"
2407 " <input type=\"text\" name=\"dayphone2\">"
2409 " <input type=\"text\" name=\"dayphone3\">"
2411 " <input type=\"text\" name=\"dayphone4\">"
2412 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2416 WebFrame* frame = GetMainFrame();
2417 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2419 WebVector<WebFormElement> forms;
2420 frame->document().forms(forms);
2421 ASSERT_EQ(1U, forms.size());
2424 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2425 WebFormControlElement(),
2426 autofill::REQUIRE_NONE,
2427 autofill::EXTRACT_VALUE,
2430 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2431 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2432 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2434 const std::vector<FormFieldData>& fields = form.fields;
2435 ASSERT_EQ(4U, fields.size());
2437 FormFieldData expected;
2438 expected.form_control_type = "text";
2439 expected.max_length = WebInputElement::defaultMaxLength();
2441 expected.label = ASCIIToUTF16("Phone:");
2442 expected.name = ASCIIToUTF16("dayphone1");
2443 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2445 expected.label = ASCIIToUTF16("-");
2446 expected.name = ASCIIToUTF16("dayphone2");
2447 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2449 expected.label = ASCIIToUTF16("-");
2450 expected.name = ASCIIToUTF16("dayphone3");
2451 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2453 expected.label = ASCIIToUTF16("ext.:");
2454 expected.name = ASCIIToUTF16("dayphone4");
2455 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2459 TEST_F(FormAutofillTest, MaxLengthFields) {
2460 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2462 " <input type=\"text\" maxlength=\"3\" name=\"dayphone1\">"
2464 " <input type=\"text\" maxlength=\"3\" name=\"dayphone2\">"
2466 " <input type=\"text\" maxlength=\"4\" size=\"5\""
2467 " name=\"dayphone3\">"
2469 " <input type=\"text\" maxlength=\"5\" name=\"dayphone4\">"
2470 " <input type=\"text\" name=\"default1\">"
2471 " <input type=\"text\" maxlength=\"-1\" name=\"invalid1\">"
2472 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2475 WebFrame* frame = GetMainFrame();
2476 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2478 WebVector<WebFormElement> forms;
2479 frame->document().forms(forms);
2480 ASSERT_EQ(1U, forms.size());
2483 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2484 WebFormControlElement(),
2485 autofill::REQUIRE_NONE,
2486 autofill::EXTRACT_VALUE,
2489 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2490 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2491 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2493 const std::vector<FormFieldData>& fields = form.fields;
2494 ASSERT_EQ(6U, fields.size());
2496 FormFieldData expected;
2497 expected.form_control_type = "text";
2499 expected.label = ASCIIToUTF16("Phone:");
2500 expected.name = ASCIIToUTF16("dayphone1");
2501 expected.max_length = 3;
2502 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2504 expected.label = ASCIIToUTF16("-");
2505 expected.name = ASCIIToUTF16("dayphone2");
2506 expected.max_length = 3;
2507 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2509 expected.label = ASCIIToUTF16("-");
2510 expected.name = ASCIIToUTF16("dayphone3");
2511 expected.max_length = 4;
2512 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2514 expected.label = ASCIIToUTF16("ext.:");
2515 expected.name = ASCIIToUTF16("dayphone4");
2516 expected.max_length = 5;
2517 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2519 // When unspecified |size|, default is returned.
2520 expected.label = string16();
2521 expected.name = ASCIIToUTF16("default1");
2522 expected.max_length = WebInputElement::defaultMaxLength();
2523 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
2525 // When invalid |size|, default is returned.
2526 expected.label = string16();
2527 expected.name = ASCIIToUTF16("invalid1");
2528 expected.max_length = WebInputElement::defaultMaxLength();
2529 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
2532 // This test re-creates the experience of typing in a field then selecting a
2533 // profile from the Autofill suggestions popup. The field that is being typed
2534 // into should be filled even though it's not technically empty.
2535 TEST_F(FormAutofillTest, FillFormNonEmptyField) {
2536 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2537 " <INPUT type=\"text\" id=\"firstname\"/>"
2538 " <INPUT type=\"text\" id=\"lastname\"/>"
2539 " <INPUT type=\"text\" id=\"email\"/>"
2540 " <INPUT type=\"submit\" value=\"Send\"/>"
2543 WebFrame* web_frame = GetMainFrame();
2544 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2546 FormCache form_cache;
2547 std::vector<FormData> forms;
2548 form_cache.ExtractForms(*web_frame, &forms);
2549 ASSERT_EQ(1U, forms.size());
2551 // Get the input element we want to find.
2552 WebElement element = web_frame->document().getElementById("firstname");
2553 WebInputElement input_element = element.to<WebInputElement>();
2555 // Simulate typing by modifying the field value.
2556 input_element.setValue(ASCIIToUTF16("Wy"));
2558 // Find the form that contains the input element.
2560 FormFieldData field;
2561 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form, &field,
2562 autofill::REQUIRE_NONE));
2563 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2564 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2565 EXPECT_EQ(GURL("http://buh.com"), form.action);
2567 const std::vector<FormFieldData>& fields = form.fields;
2568 ASSERT_EQ(3U, fields.size());
2570 FormFieldData expected;
2571 expected.form_control_type = "text";
2572 expected.max_length = WebInputElement::defaultMaxLength();
2574 expected.name = ASCIIToUTF16("firstname");
2575 expected.value = ASCIIToUTF16("Wy");
2576 expected.is_autofilled = false;
2577 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2579 expected.name = ASCIIToUTF16("lastname");
2580 expected.value = string16();
2581 expected.is_autofilled = false;
2582 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2584 expected.name = ASCIIToUTF16("email");
2585 expected.value = string16();
2586 expected.is_autofilled = false;
2587 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2589 // Preview the form and verify that the cursor position has been updated.
2590 form.fields[0].value = ASCIIToUTF16("Wyatt");
2591 form.fields[1].value = ASCIIToUTF16("Earp");
2592 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2593 PreviewForm(form, input_element);
2594 EXPECT_EQ(2, input_element.selectionStart());
2595 EXPECT_EQ(5, input_element.selectionEnd());
2598 FillForm(form, input_element);
2600 // Find the newly-filled form that contains the input element.
2602 FormFieldData field2;
2603 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element, &form2, &field2,
2604 autofill::REQUIRE_NONE));
2606 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2607 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2608 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2610 const std::vector<FormFieldData>& fields2 = form2.fields;
2611 ASSERT_EQ(3U, fields2.size());
2613 expected.name = ASCIIToUTF16("firstname");
2614 expected.value = ASCIIToUTF16("Wyatt");
2615 expected.is_autofilled = true;
2616 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2618 expected.name = ASCIIToUTF16("lastname");
2619 expected.value = ASCIIToUTF16("Earp");
2620 expected.is_autofilled = true;
2621 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2623 expected.name = ASCIIToUTF16("email");
2624 expected.value = ASCIIToUTF16("wyatt@example.com");
2625 expected.is_autofilled = true;
2626 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2628 // Verify that the cursor position has been updated.
2629 EXPECT_EQ(5, input_element.selectionStart());
2630 EXPECT_EQ(5, input_element.selectionEnd());
2633 TEST_F(FormAutofillTest, ClearFormWithNode) {
2635 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2636 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2637 " <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>"
2638 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noAC\" value=\"one\"/>"
2639 " <INPUT type=\"text\" id=\"notenabled\" disabled=\"disabled\">"
2640 " <INPUT type=\"month\" id=\"month\" value=\"2012-11\">"
2641 " <INPUT type=\"month\" id=\"month-disabled\" value=\"2012-11\""
2642 " disabled=\"disabled\">"
2643 " <TEXTAREA id=\"textarea\">Apple.</TEXTAREA>"
2644 " <TEXTAREA id=\"textarea-disabled\" disabled=\"disabled\">"
2647 " <TEXTAREA id=\"textarea-noAC\" autocomplete=\"off\">Carrot?</TEXTAREA>"
2648 " <INPUT type=\"submit\" value=\"Send\"/>"
2651 WebFrame* web_frame = GetMainFrame();
2652 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2654 FormCache form_cache;
2655 std::vector<FormData> forms;
2656 form_cache.ExtractForms(*web_frame, &forms);
2657 ASSERT_EQ(1U, forms.size());
2659 // Set the auto-filled attribute on the firstname element.
2660 WebInputElement firstname =
2661 web_frame->document().getElementById("firstname").to<WebInputElement>();
2662 firstname.setAutofilled(true);
2664 // Set the value of the disabled text input element.
2665 WebInputElement notenabled =
2666 web_frame->document().getElementById("notenabled").to<WebInputElement>();
2667 notenabled.setValue(WebString::fromUTF8("no clear"));
2670 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
2672 // Verify that the auto-filled attribute has been turned off.
2673 EXPECT_FALSE(firstname.isAutofilled());
2675 // Verify the form is cleared.
2677 FormFieldData field2;
2678 EXPECT_TRUE(FindFormAndFieldForInputElement(firstname, &form2, &field2,
2679 autofill::REQUIRE_NONE));
2680 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2681 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2682 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2684 const std::vector<FormFieldData>& fields2 = form2.fields;
2685 ASSERT_EQ(9U, fields2.size());
2687 FormFieldData expected;
2688 expected.form_control_type = "text";
2689 expected.max_length = WebInputElement::defaultMaxLength();
2691 expected.name = ASCIIToUTF16("firstname");
2692 expected.value = string16();
2693 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2695 expected.name = ASCIIToUTF16("lastname");
2696 expected.value = string16();
2697 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2699 expected.name = ASCIIToUTF16("noAC");
2700 expected.value = string16();
2701 expected.autocomplete_attribute = "off";
2702 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2703 expected.autocomplete_attribute = std::string(); // reset
2705 expected.name = ASCIIToUTF16("notenabled");
2706 expected.value = ASCIIToUTF16("no clear");
2707 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[3]);
2709 expected.form_control_type = "month";
2710 expected.max_length = 0;
2711 expected.name = ASCIIToUTF16("month");
2712 expected.value = string16();
2713 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[4]);
2715 expected.name = ASCIIToUTF16("month-disabled");
2716 expected.value = ASCIIToUTF16("2012-11");
2717 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[5]);
2719 expected.form_control_type = "textarea";
2720 expected.name = ASCIIToUTF16("textarea");
2721 expected.value = string16();
2722 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[6]);
2724 expected.name = ASCIIToUTF16("textarea-disabled");
2725 expected.value = ASCIIToUTF16(" Banana! ");
2726 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[7]);
2728 expected.name = ASCIIToUTF16("textarea-noAC");
2729 expected.value = string16();
2730 expected.autocomplete_attribute = "off";
2731 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[8]);
2732 expected.autocomplete_attribute = std::string(); // reset
2734 // Verify that the cursor position has been updated.
2735 EXPECT_EQ(0, firstname.selectionStart());
2736 EXPECT_EQ(0, firstname.selectionEnd());
2739 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) {
2741 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2742 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2743 " <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>"
2744 " <SELECT id=\"state\" name=\"state\">"
2745 " <OPTION selected>?</OPTION>"
2746 " <OPTION>AA</OPTION>"
2747 " <OPTION>AE</OPTION>"
2748 " <OPTION>AK</OPTION>"
2750 " <INPUT type=\"submit\" value=\"Send\"/>"
2753 WebFrame* web_frame = GetMainFrame();
2754 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2756 FormCache form_cache;
2757 std::vector<FormData> forms;
2758 form_cache.ExtractForms(*web_frame, &forms);
2759 ASSERT_EQ(1U, forms.size());
2761 // Set the auto-filled attribute on the firstname element.
2762 WebInputElement firstname =
2763 web_frame->document().getElementById("firstname").to<WebInputElement>();
2764 firstname.setAutofilled(true);
2766 // Set the value of the select-one.
2767 WebSelectElement select_element =
2768 web_frame->document().getElementById("state").to<WebSelectElement>();
2769 select_element.setValue(WebString::fromUTF8("AK"));
2772 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
2774 // Verify that the auto-filled attribute has been turned off.
2775 EXPECT_FALSE(firstname.isAutofilled());
2777 // Verify the form is cleared.
2779 FormFieldData field2;
2780 EXPECT_TRUE(FindFormAndFieldForInputElement(firstname, &form2, &field2,
2781 autofill::REQUIRE_NONE));
2782 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2783 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2784 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2786 const std::vector<FormFieldData>& fields2 = form2.fields;
2787 ASSERT_EQ(3U, fields2.size());
2789 FormFieldData expected;
2791 expected.name = ASCIIToUTF16("firstname");
2792 expected.value = string16();
2793 expected.form_control_type = "text";
2794 expected.max_length = WebInputElement::defaultMaxLength();
2795 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2797 expected.name = ASCIIToUTF16("lastname");
2798 expected.value = string16();
2799 expected.form_control_type = "text";
2800 expected.max_length = WebInputElement::defaultMaxLength();
2801 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2803 expected.name = ASCIIToUTF16("state");
2804 expected.value = ASCIIToUTF16("?");
2805 expected.form_control_type = "select-one";
2806 expected.max_length = 0;
2807 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2809 // Verify that the cursor position has been updated.
2810 EXPECT_EQ(0, firstname.selectionStart());
2811 EXPECT_EQ(0, firstname.selectionEnd());
2814 TEST_F(FormAutofillTest, ClearPreviewedFormWithElement) {
2815 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2816 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2817 " <INPUT type=\"text\" id=\"lastname\"/>"
2818 " <INPUT type=\"text\" id=\"email\"/>"
2819 " <INPUT type=\"email\" id=\"email2\"/>"
2820 " <INPUT type=\"tel\" id=\"phone\"/>"
2821 " <INPUT type=\"submit\" value=\"Send\"/>"
2824 WebFrame* web_frame = GetMainFrame();
2825 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2827 FormCache form_cache;
2828 std::vector<FormData> forms;
2829 form_cache.ExtractForms(*web_frame, &forms);
2830 ASSERT_EQ(1U, forms.size());
2832 // Set the auto-filled attribute.
2833 WebInputElement firstname =
2834 web_frame->document().getElementById("firstname").to<WebInputElement>();
2835 firstname.setAutofilled(true);
2836 WebInputElement lastname =
2837 web_frame->document().getElementById("lastname").to<WebInputElement>();
2838 lastname.setAutofilled(true);
2839 WebInputElement email =
2840 web_frame->document().getElementById("email").to<WebInputElement>();
2841 email.setAutofilled(true);
2842 WebInputElement email2 =
2843 web_frame->document().getElementById("email2").to<WebInputElement>();
2844 email2.setAutofilled(true);
2845 WebInputElement phone =
2846 web_frame->document().getElementById("phone").to<WebInputElement>();
2847 phone.setAutofilled(true);
2849 // Set the suggested values on two of the elements.
2850 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
2851 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2852 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2853 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2855 // Clear the previewed fields.
2856 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
2858 // Fields with empty suggestions suggestions are not modified.
2859 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
2860 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
2861 EXPECT_TRUE(firstname.isAutofilled());
2863 // Verify the previewed fields are cleared.
2864 EXPECT_TRUE(lastname.value().isEmpty());
2865 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
2866 EXPECT_FALSE(lastname.isAutofilled());
2867 EXPECT_TRUE(email.value().isEmpty());
2868 EXPECT_TRUE(email.suggestedValue().isEmpty());
2869 EXPECT_FALSE(email.isAutofilled());
2870 EXPECT_TRUE(email2.value().isEmpty());
2871 EXPECT_TRUE(email2.suggestedValue().isEmpty());
2872 EXPECT_FALSE(email2.isAutofilled());
2873 EXPECT_TRUE(phone.value().isEmpty());
2874 EXPECT_TRUE(phone.suggestedValue().isEmpty());
2875 EXPECT_FALSE(phone.isAutofilled());
2877 // Verify that the cursor position has been updated.
2878 EXPECT_EQ(0, lastname.selectionStart());
2879 EXPECT_EQ(0, lastname.selectionEnd());
2882 TEST_F(FormAutofillTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
2883 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2884 " <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
2885 " <INPUT type=\"text\" id=\"lastname\"/>"
2886 " <INPUT type=\"text\" id=\"email\"/>"
2887 " <INPUT type=\"email\" id=\"email2\"/>"
2888 " <INPUT type=\"tel\" id=\"phone\"/>"
2889 " <INPUT type=\"submit\" value=\"Send\"/>"
2892 WebFrame* web_frame = GetMainFrame();
2893 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2895 FormCache form_cache;
2896 std::vector<FormData> forms;
2897 form_cache.ExtractForms(*web_frame, &forms);
2898 ASSERT_EQ(1U, forms.size());
2900 // Set the auto-filled attribute.
2901 WebInputElement firstname =
2902 web_frame->document().getElementById("firstname").to<WebInputElement>();
2903 firstname.setAutofilled(true);
2904 WebInputElement lastname =
2905 web_frame->document().getElementById("lastname").to<WebInputElement>();
2906 lastname.setAutofilled(true);
2907 WebInputElement email =
2908 web_frame->document().getElementById("email").to<WebInputElement>();
2909 email.setAutofilled(true);
2910 WebInputElement email2 =
2911 web_frame->document().getElementById("email2").to<WebInputElement>();
2912 email2.setAutofilled(true);
2913 WebInputElement phone =
2914 web_frame->document().getElementById("phone").to<WebInputElement>();
2915 phone.setAutofilled(true);
2918 // Set the suggested values on all of the elements.
2919 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
2920 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
2921 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2922 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2923 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2925 // Clear the previewed fields.
2926 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
2928 // Fields with non-empty values are restored.
2929 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
2930 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
2931 EXPECT_FALSE(firstname.isAutofilled());
2932 EXPECT_EQ(1, firstname.selectionStart());
2933 EXPECT_EQ(1, firstname.selectionEnd());
2935 // Verify the previewed fields are cleared.
2936 EXPECT_TRUE(lastname.value().isEmpty());
2937 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
2938 EXPECT_FALSE(lastname.isAutofilled());
2939 EXPECT_TRUE(email.value().isEmpty());
2940 EXPECT_TRUE(email.suggestedValue().isEmpty());
2941 EXPECT_FALSE(email.isAutofilled());
2942 EXPECT_TRUE(email2.value().isEmpty());
2943 EXPECT_TRUE(email2.suggestedValue().isEmpty());
2944 EXPECT_FALSE(email2.isAutofilled());
2945 EXPECT_TRUE(phone.value().isEmpty());
2946 EXPECT_TRUE(phone.suggestedValue().isEmpty());
2947 EXPECT_FALSE(phone.isAutofilled());
2950 TEST_F(FormAutofillTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
2951 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2952 " <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
2953 " <INPUT type=\"text\" id=\"lastname\"/>"
2954 " <INPUT type=\"text\" id=\"email\"/>"
2955 " <INPUT type=\"email\" id=\"email2\"/>"
2956 " <INPUT type=\"tel\" id=\"phone\"/>"
2957 " <INPUT type=\"submit\" value=\"Send\"/>"
2960 WebFrame* web_frame = GetMainFrame();
2961 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2963 FormCache form_cache;
2964 std::vector<FormData> forms;
2965 form_cache.ExtractForms(*web_frame, &forms);
2966 ASSERT_EQ(1U, forms.size());
2968 // Set the auto-filled attribute.
2969 WebInputElement firstname =
2970 web_frame->document().getElementById("firstname").to<WebInputElement>();
2971 firstname.setAutofilled(true);
2972 WebInputElement lastname =
2973 web_frame->document().getElementById("lastname").to<WebInputElement>();
2974 lastname.setAutofilled(true);
2975 WebInputElement email =
2976 web_frame->document().getElementById("email").to<WebInputElement>();
2977 email.setAutofilled(true);
2978 WebInputElement email2 =
2979 web_frame->document().getElementById("email2").to<WebInputElement>();
2980 email2.setAutofilled(true);
2981 WebInputElement phone =
2982 web_frame->document().getElementById("phone").to<WebInputElement>();
2983 phone.setAutofilled(true);
2985 // Set the suggested values on all of the elements.
2986 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
2987 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
2988 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2989 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2990 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2992 // Clear the previewed fields.
2993 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
2995 // Fields with non-empty values are restored.
2996 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
2997 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
2998 EXPECT_TRUE(firstname.isAutofilled());
2999 EXPECT_EQ(1, firstname.selectionStart());
3000 EXPECT_EQ(1, firstname.selectionEnd());
3002 // Verify the previewed fields are cleared.
3003 EXPECT_TRUE(lastname.value().isEmpty());
3004 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3005 EXPECT_FALSE(lastname.isAutofilled());
3006 EXPECT_TRUE(email.value().isEmpty());
3007 EXPECT_TRUE(email.suggestedValue().isEmpty());
3008 EXPECT_FALSE(email.isAutofilled());
3009 EXPECT_TRUE(email2.value().isEmpty());
3010 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3011 EXPECT_FALSE(email2.isAutofilled());
3012 EXPECT_TRUE(phone.value().isEmpty());
3013 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3014 EXPECT_FALSE(phone.isAutofilled());
3017 TEST_F(FormAutofillTest, FormWithNodeIsAutofilled) {
3018 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
3019 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
3020 " <INPUT type=\"text\" id=\"lastname\"/>"
3021 " <INPUT type=\"text\" id=\"email\"/>"
3022 " <INPUT type=\"email\" id=\"email2\"/>"
3023 " <INPUT type=\"tel\" id=\"phone\"/>"
3024 " <INPUT type=\"submit\" value=\"Send\"/>"
3027 WebFrame* web_frame = GetMainFrame();
3028 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3030 FormCache form_cache;
3031 std::vector<FormData> forms;
3032 form_cache.ExtractForms(*web_frame, &forms);
3033 ASSERT_EQ(1U, forms.size());
3035 WebInputElement firstname =
3036 web_frame->document().getElementById("firstname").to<WebInputElement>();
3038 // Auto-filled attribute not set yet.
3039 EXPECT_FALSE(FormWithElementIsAutofilled(firstname));
3041 // Set the auto-filled attribute.
3042 firstname.setAutofilled(true);
3044 EXPECT_TRUE(FormWithElementIsAutofilled(firstname));
3047 // If we have multiple labels per id, the labels concatenated into label string.
3048 TEST_F(FormAutofillTest, MultipleLabelsPerElement) {
3049 std::vector<string16> labels, names, values;
3051 labels.push_back(ASCIIToUTF16("First Name:"));
3052 names.push_back(ASCIIToUTF16("firstname"));
3053 values.push_back(ASCIIToUTF16("John"));
3055 labels.push_back(ASCIIToUTF16("Last Name:"));
3056 names.push_back(ASCIIToUTF16("lastname"));
3057 values.push_back(ASCIIToUTF16("Smith"));
3059 labels.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
3060 names.push_back(ASCIIToUTF16("email"));
3061 values.push_back(ASCIIToUTF16("john@example.com"));
3064 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
3065 " <LABEL for=\"firstname\"> First Name: </LABEL>"
3066 " <LABEL for=\"firstname\"></LABEL>"
3067 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
3068 " <LABEL for=\"lastname\"></LABEL>"
3069 " <LABEL for=\"lastname\"> Last Name: </LABEL>"
3070 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
3071 " <LABEL for=\"email\"> Email: </LABEL>"
3072 " <LABEL for=\"email\"> xxx@yyy.com </LABEL>"
3073 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
3074 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
3076 labels, names, values);
3079 TEST_F(FormAutofillTest, ClickElement) {
3080 LoadHTML("<BUTTON id=\"link\">Button</BUTTON>"
3081 "<BUTTON name=\"button\">Button</BUTTON>");
3082 WebFrame* frame = GetMainFrame();
3083 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3085 // Successful retrieval by id.
3086 autofill::WebElementDescriptor clicker;
3087 clicker.retrieval_method = autofill::WebElementDescriptor::ID;
3088 clicker.descriptor = "link";
3089 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3091 // Successful retrieval by css selector.
3092 clicker.retrieval_method = autofill::WebElementDescriptor::CSS_SELECTOR;
3093 clicker.descriptor = "button[name=\"button\"]";
3094 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3096 // Unsuccessful retrieval due to invalid CSS selector.
3097 clicker.descriptor = "^*&";
3098 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3100 // Unsuccessful retrieval because element does not exist.
3101 clicker.descriptor = "#junk";
3102 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3105 TEST_F(FormAutofillTest, SelectOneAsText) {
3106 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
3107 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
3108 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
3109 " <SELECT id=\"country\">"
3110 " <OPTION value=\"AF\">Afghanistan</OPTION>"
3111 " <OPTION value=\"AL\">Albania</OPTION>"
3112 " <OPTION value=\"DZ\">Algeria</OPTION>"
3114 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
3117 WebFrame* frame = GetMainFrame();
3118 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3120 // Set the value of the select-one.
3121 WebSelectElement select_element =
3122 frame->document().getElementById("country").to<WebSelectElement>();
3123 select_element.setValue(WebString::fromUTF8("AL"));
3125 WebVector<WebFormElement> forms;
3126 frame->document().forms(forms);
3127 ASSERT_EQ(1U, forms.size());
3131 // Extract the country select-one value as text.
3132 EXPECT_TRUE(WebFormElementToFormData(
3133 forms[0], WebFormControlElement(), autofill::REQUIRE_NONE,
3134 static_cast<autofill::ExtractMask>(
3135 autofill::EXTRACT_VALUE | autofill::EXTRACT_OPTION_TEXT),
3137 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3138 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3139 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3141 const std::vector<FormFieldData>& fields = form.fields;
3142 ASSERT_EQ(3U, fields.size());
3144 FormFieldData expected;
3146 expected.name = ASCIIToUTF16("firstname");
3147 expected.value = ASCIIToUTF16("John");
3148 expected.form_control_type = "text";
3149 expected.max_length = WebInputElement::defaultMaxLength();
3150 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3152 expected.name = ASCIIToUTF16("lastname");
3153 expected.value = ASCIIToUTF16("Smith");
3154 expected.form_control_type = "text";
3155 expected.max_length = WebInputElement::defaultMaxLength();
3156 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3158 expected.name = ASCIIToUTF16("country");
3159 expected.value = ASCIIToUTF16("Albania");
3160 expected.form_control_type = "select-one";
3161 expected.max_length = 0;
3162 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3164 form.fields.clear();
3165 // Extract the country select-one value as value.
3166 EXPECT_TRUE(WebFormElementToFormData(forms[0],
3167 WebFormControlElement(),
3168 autofill::REQUIRE_NONE,
3169 autofill::EXTRACT_VALUE,
3172 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3173 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3174 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3176 ASSERT_EQ(3U, fields.size());
3178 expected.name = ASCIIToUTF16("firstname");
3179 expected.value = ASCIIToUTF16("John");
3180 expected.form_control_type = "text";
3181 expected.max_length = WebInputElement::defaultMaxLength();
3182 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3184 expected.name = ASCIIToUTF16("lastname");
3185 expected.value = ASCIIToUTF16("Smith");
3186 expected.form_control_type = "text";
3187 expected.max_length = WebInputElement::defaultMaxLength();
3188 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3190 expected.name = ASCIIToUTF16("country");
3191 expected.value = ASCIIToUTF16("AL");
3192 expected.form_control_type = "select-one";
3193 expected.max_length = 0;
3194 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3197 } // namespace autofill