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 base::ASCIIToUTF16
;
32 using blink::WebDocument
;
33 using blink::WebElement
;
34 using blink::WebFormControlElement
;
35 using blink::WebFormElement
;
36 using blink::WebFrame
;
37 using blink::WebInputElement
;
39 using blink::WebSelectElement
;
40 using blink::WebString
;
41 using blink::WebTextAreaElement
;
42 using blink::WebVector
;
46 struct AutofillFieldCase
{
47 const char* const form_control_type
;
48 const char* const name
;
49 const char* const initial_value
;
50 const char* const autocomplete_attribute
; // The autocomplete attribute of
52 bool should_be_autofilled
; // Whether the filed should be autofilled.
53 const char* const autofill_value
; // The value being used to fill the field.
54 const char* const expected_value
; // The expected value after Autofill
58 static const char kFormHtml
[] =
59 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
60 " <INPUT type=\"text\" id=\"firstname\"/>"
61 " <INPUT type=\"text\" id=\"lastname\"/>"
62 " <INPUT type=\"hidden\" id=\"imhidden\"/>"
63 " <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>"
64 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>"
65 " <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>"
66 " <INPUT type=\"text\" readonly id=\"readonly\"/>"
67 " <INPUT type=\"text\" style=\"visibility: hidden\""
69 " <INPUT type=\"text\" style=\"display: none\" id=\"displaynone\"/>"
70 " <INPUT type=\"month\" id=\"month\"/>"
71 " <INPUT type=\"month\" id=\"month-nonempty\" value=\"2011-12\"/>"
72 " <SELECT id=\"select\">"
74 " <OPTION value=\"CA\">California</OPTION>"
75 " <OPTION value=\"TX\">Texas</OPTION>"
77 " <SELECT id=\"select-nonempty\">"
78 " <OPTION value=\"CA\" selected>California</OPTION>"
79 " <OPTION value=\"TX\">Texas</OPTION>"
81 " <TEXTAREA id=\"textarea\"></TEXTAREA>"
82 " <TEXTAREA id=\"textarea-nonempty\">Go away!</TEXTAREA>"
83 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
90 class FormAutofillTest
: public ChromeRenderViewTest
{
92 FormAutofillTest() : ChromeRenderViewTest() {}
93 virtual ~FormAutofillTest() {}
95 void ExpectLabels(const char* html
,
96 const std::vector
<base::string16
>& labels
,
97 const std::vector
<base::string16
>& names
,
98 const std::vector
<base::string16
>& values
) {
99 std::vector
<std::string
> control_types(labels
.size(), "text");
100 ExpectLabelsAndTypes(html
, labels
, names
, values
, control_types
);
103 void ExpectLabelsAndTypes(const char* html
,
104 const std::vector
<base::string16
>& labels
,
105 const std::vector
<base::string16
>& names
,
106 const std::vector
<base::string16
>& values
,
107 const std::vector
<std::string
>& control_types
) {
108 ASSERT_EQ(labels
.size(), names
.size());
109 ASSERT_EQ(labels
.size(), values
.size());
110 ASSERT_EQ(labels
.size(), control_types
.size());
114 WebFrame
* web_frame
= GetMainFrame();
115 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
117 FormCache form_cache
;
118 std::vector
<FormData
> forms
;
119 form_cache
.ExtractForms(*web_frame
, &forms
);
120 ASSERT_EQ(1U, forms
.size());
122 const FormData
& form
= forms
[0];
123 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
124 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
125 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
127 const std::vector
<FormFieldData
>& fields
= form
.fields
;
128 ASSERT_EQ(labels
.size(), fields
.size());
129 for (size_t i
= 0; i
< labels
.size(); ++i
) {
130 int max_length
= control_types
[i
] == "text" ?
131 WebInputElement::defaultMaxLength() : 0;
132 FormFieldData expected
;
133 expected
.label
= labels
[i
];
134 expected
.name
= names
[i
];
135 expected
.value
= values
[i
];
136 expected
.form_control_type
= control_types
[i
];
137 expected
.max_length
= max_length
;
138 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS
, i
));
139 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[i
]);
143 void ExpectJohnSmithLabels(const char* html
) {
144 std::vector
<base::string16
> labels
, names
, values
;
146 labels
.push_back(ASCIIToUTF16("First name:"));
147 names
.push_back(ASCIIToUTF16("firstname"));
148 values
.push_back(ASCIIToUTF16("John"));
150 labels
.push_back(ASCIIToUTF16("Last name:"));
151 names
.push_back(ASCIIToUTF16("lastname"));
152 values
.push_back(ASCIIToUTF16("Smith"));
154 labels
.push_back(ASCIIToUTF16("Email:"));
155 names
.push_back(ASCIIToUTF16("email"));
156 values
.push_back(ASCIIToUTF16("john@example.com"));
158 ExpectLabels(html
, labels
, names
, values
);
161 typedef void (*FillFormFunction
)(const FormData
& form
,
162 const WebFormControlElement
& element
);
164 typedef WebString (*GetValueFunction
)(WebFormControlElement element
);
166 // Test FormFillxxx functions.
167 void TestFormFillFunctions(const char* html
,
168 const AutofillFieldCase
* field_cases
,
169 size_t number_of_field_cases
,
170 FillFormFunction fill_form_function
,
171 GetValueFunction get_value_function
) {
174 WebFrame
* web_frame
= GetMainFrame();
175 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
177 FormCache form_cache
;
178 std::vector
<FormData
> forms
;
179 form_cache
.ExtractForms(*web_frame
, &forms
);
180 ASSERT_EQ(1U, forms
.size());
182 // Get the input element we want to find.
183 WebElement element
= web_frame
->document().getElementById("firstname");
184 WebInputElement input_element
= element
.to
<WebInputElement
>();
186 // Find the form that contains the input element.
190 FindFormAndFieldForFormControlElement(input_element
,
193 autofill::REQUIRE_AUTOCOMPLETE
));
194 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data
.name
);
195 EXPECT_EQ(GURL(web_frame
->document().url()), form_data
.origin
);
196 EXPECT_EQ(GURL("http://buh.com"), form_data
.action
);
198 const std::vector
<FormFieldData
>& fields
= form_data
.fields
;
199 ASSERT_EQ(number_of_field_cases
, fields
.size());
201 FormFieldData expected
;
202 // Verify field's initial value.
203 for (size_t i
= 0; i
< number_of_field_cases
; ++i
) {
204 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
205 field_cases
[i
].name
));
206 expected
.form_control_type
= field_cases
[i
].form_control_type
;
207 expected
.max_length
=
208 expected
.form_control_type
== "text" ?
209 WebInputElement::defaultMaxLength() : 0;
210 expected
.name
= ASCIIToUTF16(field_cases
[i
].name
);
211 expected
.value
= ASCIIToUTF16(field_cases
[i
].initial_value
);
212 expected
.autocomplete_attribute
= field_cases
[i
].autocomplete_attribute
;
213 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[i
]);
214 // Fill the form_data for the field.
215 form_data
.fields
[i
].value
= ASCIIToUTF16(field_cases
[i
].autofill_value
);
218 // Autofill the form using the given fill form function.
219 fill_form_function(form_data
, input_element
);
221 // Validate Autofill or Preview results.
222 for (size_t i
= 0; i
< number_of_field_cases
; ++i
) {
223 ValidteFilledField(field_cases
[i
], get_value_function
);
227 // Validate an Autofilled field.
228 void ValidteFilledField(const AutofillFieldCase
& field_case
,
229 GetValueFunction get_value_function
) {
230 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
233 WebFormControlElement element
= GetMainFrame()->document().getElementById(
234 ASCIIToUTF16(field_case
.name
)).to
<WebFormControlElement
>();
235 if ((element
.formControlType() == "select-one") ||
236 (element
.formControlType() == "textarea")) {
237 value
= get_value_function(element
);
239 ASSERT_TRUE(element
.formControlType() == "text" ||
240 element
.formControlType() == "month");
241 value
= get_value_function(element
);
244 const WebString expected_value
= ASCIIToUTF16(field_case
.expected_value
);
245 if (expected_value
.isEmpty())
246 EXPECT_TRUE(value
.isEmpty());
248 EXPECT_EQ(expected_value
, value
);
250 EXPECT_EQ(field_case
.should_be_autofilled
, element
.isAutofilled());
253 static void FillFormForAllFieldsWrapper(const FormData
& form
,
254 const WebInputElement
& element
) {
255 FillFormForAllElements(form
, element
.form());
258 static void FillFormIncludingNonFocusableElementsWrapper(
259 const FormData
& form
,
260 const WebFormControlElement
& element
) {
261 FillFormIncludingNonFocusableElements(form
, element
.form());
264 static WebString
GetValueWrapper(WebFormControlElement element
) {
265 if (element
.formControlType() == "textarea")
266 return element
.to
<WebTextAreaElement
>().value();
268 if (element
.formControlType() == "select-one")
269 return element
.to
<WebSelectElement
>().value();
271 return element
.to
<WebInputElement
>().value();
274 static WebString
GetSuggestedValueWrapper(WebFormControlElement element
) {
275 if (element
.formControlType() == "textarea")
276 return element
.to
<WebTextAreaElement
>().suggestedValue();
278 if (element
.formControlType() == "select-one")
279 return element
.to
<WebSelectElement
>().suggestedValue();
281 return element
.to
<WebInputElement
>().suggestedValue();
285 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest
);
288 // We should be able to extract a normal text field.
289 TEST_F(FormAutofillTest
, WebFormControlElementToFormField
) {
290 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>");
292 WebFrame
* frame
= GetMainFrame();
293 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
295 WebElement web_element
= frame
->document().getElementById("element");
296 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
297 FormFieldData result1
;
298 WebFormControlElementToFormField(element
, autofill::EXTRACT_NONE
, &result1
);
300 FormFieldData expected
;
301 expected
.form_control_type
= "text";
302 expected
.max_length
= WebInputElement::defaultMaxLength();
304 expected
.name
= ASCIIToUTF16("element");
305 expected
.value
= base::string16();
306 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result1
);
308 FormFieldData result2
;
309 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result2
);
311 expected
.name
= ASCIIToUTF16("element");
312 expected
.value
= ASCIIToUTF16("value");
313 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result2
);
316 // We should be able to extract a text field with autocomplete="off".
317 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutocompleteOff
) {
318 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\""
319 " autocomplete=\"off\"/>");
321 WebFrame
* frame
= GetMainFrame();
322 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
324 WebElement web_element
= frame
->document().getElementById("element");
325 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
326 FormFieldData result
;
327 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
329 FormFieldData expected
;
330 expected
.name
= ASCIIToUTF16("element");
331 expected
.value
= ASCIIToUTF16("value");
332 expected
.form_control_type
= "text";
333 expected
.autocomplete_attribute
= "off";
334 expected
.max_length
= WebInputElement::defaultMaxLength();
335 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
338 // We should be able to extract a text field with maxlength specified.
339 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldMaxLength
) {
340 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\""
341 " maxlength=\"5\"/>");
343 WebFrame
* frame
= GetMainFrame();
344 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
346 WebElement web_element
= frame
->document().getElementById("element");
347 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
348 FormFieldData result
;
349 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
351 FormFieldData expected
;
352 expected
.name
= ASCIIToUTF16("element");
353 expected
.value
= ASCIIToUTF16("value");
354 expected
.form_control_type
= "text";
355 expected
.max_length
= 5;
356 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
359 // We should be able to extract a text field that has been autofilled.
360 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutofilled
) {
361 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>");
363 WebFrame
* frame
= GetMainFrame();
364 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
366 WebElement web_element
= frame
->document().getElementById("element");
367 WebInputElement element
= web_element
.to
<WebInputElement
>();
368 element
.setAutofilled(true);
369 FormFieldData result
;
370 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
372 FormFieldData expected
;
373 expected
.name
= ASCIIToUTF16("element");
374 expected
.value
= ASCIIToUTF16("value");
375 expected
.form_control_type
= "text";
376 expected
.max_length
= WebInputElement::defaultMaxLength();
377 expected
.is_autofilled
= true;
378 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
381 // We should be able to extract a radio or a checkbox field that has been
383 TEST_F(FormAutofillTest
, WebFormControlElementToClickableFormField
) {
384 LoadHTML("<INPUT type=\"checkbox\" id=\"checkbox\" value=\"mail\" checked/>"
385 "<INPUT type=\"radio\" id=\"radio\" value=\"male\"/>");
387 WebFrame
* frame
= GetMainFrame();
388 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
390 WebElement web_element
= frame
->document().getElementById("checkbox");
391 WebInputElement element
= web_element
.to
<WebInputElement
>();
392 element
.setAutofilled(true);
393 FormFieldData result
;
394 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
396 FormFieldData expected
;
397 expected
.name
= ASCIIToUTF16("checkbox");
398 expected
.value
= ASCIIToUTF16("mail");
399 expected
.form_control_type
= "checkbox";
400 expected
.is_autofilled
= true;
401 expected
.is_checkable
= true;
402 expected
.is_checked
= true;
403 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
405 web_element
= frame
->document().getElementById("radio");
406 element
= web_element
.to
<WebInputElement
>();
407 element
.setAutofilled(true);
408 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
409 expected
.name
= ASCIIToUTF16("radio");
410 expected
.value
= ASCIIToUTF16("male");
411 expected
.form_control_type
= "radio";
412 expected
.is_autofilled
= true;
413 expected
.is_checkable
= true;
414 expected
.is_checked
= false;
415 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
418 // We should be able to extract a <select> field.
419 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldSelect
) {
420 LoadHTML("<SELECT id=\"element\"/>"
421 " <OPTION value=\"CA\">California</OPTION>"
422 " <OPTION value=\"TX\">Texas</OPTION>"
425 WebFrame
* frame
= GetMainFrame();
426 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
428 WebElement web_element
= frame
->document().getElementById("element");
429 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
430 FormFieldData result1
;
431 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result1
);
433 FormFieldData expected
;
434 expected
.name
= ASCIIToUTF16("element");
435 expected
.max_length
= 0;
436 expected
.form_control_type
= "select-one";
438 expected
.value
= ASCIIToUTF16("CA");
439 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result1
);
441 FormFieldData result2
;
442 WebFormControlElementToFormField(
444 static_cast<autofill::ExtractMask
>(autofill::EXTRACT_VALUE
|
445 autofill::EXTRACT_OPTION_TEXT
),
447 expected
.value
= ASCIIToUTF16("California");
448 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result2
);
450 FormFieldData result3
;
451 WebFormControlElementToFormField(element
, autofill::EXTRACT_OPTIONS
,
453 expected
.value
= base::string16();
454 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result3
);
456 ASSERT_EQ(2U, result3
.option_values
.size());
457 ASSERT_EQ(2U, result3
.option_contents
.size());
458 EXPECT_EQ(ASCIIToUTF16("CA"), result3
.option_values
[0]);
459 EXPECT_EQ(ASCIIToUTF16("California"), result3
.option_contents
[0]);
460 EXPECT_EQ(ASCIIToUTF16("TX"), result3
.option_values
[1]);
461 EXPECT_EQ(ASCIIToUTF16("Texas"), result3
.option_contents
[1]);
464 // We should be able to extract a <textarea> field.
465 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldTextArea
) {
466 LoadHTML("<TEXTAREA id=\"element\">"
467 "This element's value "
468 "spans multiple lines."
471 WebFrame
* frame
= GetMainFrame();
472 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
474 WebElement web_element
= frame
->document().getElementById("element");
475 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
476 FormFieldData result_sans_value
;
477 WebFormControlElementToFormField(element
, autofill::EXTRACT_NONE
,
480 FormFieldData expected
;
481 expected
.name
= ASCIIToUTF16("element");
482 expected
.max_length
= 0;
483 expected
.form_control_type
= "textarea";
484 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result_sans_value
);
486 FormFieldData result_with_value
;
487 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
,
489 expected
.value
= ASCIIToUTF16("This element's value\n"
490 "spans multiple lines.");
491 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result_with_value
);
494 // We should be able to extract an <input type="month"> field.
495 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldMonthInput
) {
496 LoadHTML("<INPUT type=\"month\" id=\"element\" value=\"2011-12\">");
498 WebFrame
* frame
= GetMainFrame();
499 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
501 WebElement web_element
= frame
->document().getElementById("element");
502 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
503 FormFieldData result_sans_value
;
504 WebFormControlElementToFormField(element
, autofill::EXTRACT_NONE
,
507 FormFieldData expected
;
508 expected
.name
= ASCIIToUTF16("element");
509 expected
.max_length
= 0;
510 expected
.form_control_type
= "month";
511 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result_sans_value
);
513 FormFieldData result_with_value
;
514 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
,
516 expected
.value
= ASCIIToUTF16("2011-12");
517 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result_with_value
);
520 // We should not extract the value for non-text and non-select fields.
521 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldInvalidType
) {
522 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
523 " <INPUT type=\"hidden\" id=\"hidden\" value=\"apple\"/>"
524 " <INPUT type=\"submit\" id=\"submit\" value=\"Send\"/>"
527 WebFrame
* frame
= GetMainFrame();
528 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
530 WebElement web_element
= frame
->document().getElementById("hidden");
531 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
532 FormFieldData result
;
533 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
535 FormFieldData expected
;
536 expected
.max_length
= 0;
538 expected
.name
= ASCIIToUTF16("hidden");
539 expected
.form_control_type
= "hidden";
540 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
542 web_element
= frame
->document().getElementById("submit");
543 element
= web_element
.to
<WebFormControlElement
>();
544 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
545 expected
.name
= ASCIIToUTF16("submit");
546 expected
.form_control_type
= "submit";
547 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
550 // We should be able to extract password fields.
551 TEST_F(FormAutofillTest
, WebFormControlElementToPasswordFormField
) {
552 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
553 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>"
556 WebFrame
* frame
= GetMainFrame();
557 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
559 WebElement web_element
= frame
->document().getElementById("password");
560 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
561 FormFieldData result
;
562 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
564 FormFieldData expected
;
565 expected
.max_length
= WebInputElement::defaultMaxLength();
566 expected
.name
= ASCIIToUTF16("password");
567 expected
.form_control_type
= "password";
568 expected
.value
= ASCIIToUTF16("secret");
569 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
572 // We should be able to extract the autocompletetype attribute.
573 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutocompletetype
) {
575 "<INPUT type=\"text\" id=\"absent\"/>"
576 "<INPUT type=\"text\" id=\"empty\" autocomplete=\"\"/>"
577 "<INPUT type=\"text\" id=\"off\" autocomplete=\"off\"/>"
578 "<INPUT type=\"text\" id=\"regular\" autocomplete=\"email\"/>"
579 "<INPUT type=\"text\" id=\"multi-valued\" "
580 " autocomplete=\"billing email\"/>"
581 "<INPUT type=\"text\" id=\"experimental\" x-autocompletetype=\"email\"/>"
582 "<INPUT type=\"month\" id=\"month\" autocomplete=\"cc-exp\"/>"
583 "<SELECT id=\"select\" autocomplete=\"state\"/>"
584 " <OPTION value=\"CA\">California</OPTION>"
585 " <OPTION value=\"TX\">Texas</OPTION>"
587 "<TEXTAREA id=\"textarea\" autocomplete=\"street-address\">"
592 "<INPUT type=\"text\" id=\"malicious\" autocomplete=\"" +
593 std::string(10000, 'x') + "\"/>";
594 LoadHTML(html
.c_str());
596 WebFrame
* frame
= GetMainFrame();
597 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
600 const std::string element_id
;
601 const std::string form_control_type
;
602 const std::string autocomplete_attribute
;
604 TestCase test_cases
[] = {
605 // An absent attribute is equivalent to an empty one.
606 { "absent", "text", "" },
607 // Make sure there are no issues parsing an empty attribute.
608 { "empty", "text", "" },
609 // Make sure there are no issues parsing an attribute value that isn't a
611 { "off", "text", "off" },
612 // Common case: exactly one type specified.
613 { "regular", "text", "email" },
614 // Verify that we correctly extract multiple tokens as well.
615 { "multi-valued", "text", "billing email" },
616 // Verify that <input type="month"> fields are supported.
617 { "month", "month", "cc-exp" },
618 // We previously extracted this data from the experimental
619 // 'x-autocompletetype' attribute. Now that the field type hints are part
620 // of the spec under the autocomplete attribute, we no longer support the
621 // experimental version.
622 { "experimental", "text", "" },
623 // <select> elements should behave no differently from text fields here.
624 { "select", "select-one", "state" },
625 // <textarea> elements should also behave no differently from text fields.
626 { "textarea", "textarea", "street-address" },
627 // Very long attribute values should be replaced by a default string, to
628 // prevent malicious websites from DOSing the browser process.
629 { "malicious", "text", "x-max-data-length-exceeded" },
632 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_cases
); ++i
) {
633 WebElement web_element
= frame
->document().getElementById(
634 ASCIIToUTF16(test_cases
[i
].element_id
));
635 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
636 FormFieldData result
;
637 WebFormControlElementToFormField(element
, autofill::EXTRACT_NONE
, &result
);
639 FormFieldData expected
;
640 expected
.name
= ASCIIToUTF16(test_cases
[i
].element_id
);
641 expected
.form_control_type
= test_cases
[i
].form_control_type
;
642 expected
.autocomplete_attribute
= test_cases
[i
].autocomplete_attribute
;
643 if (test_cases
[i
].form_control_type
== "text")
644 expected
.max_length
= WebInputElement::defaultMaxLength();
646 expected
.max_length
= 0;
648 SCOPED_TRACE(test_cases
[i
].element_id
);
649 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
653 TEST_F(FormAutofillTest
, WebFormElementToFormData
) {
654 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
655 " <LABEL for=\"firstname\">First name:</LABEL>"
656 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
657 " <LABEL for=\"lastname\">Last name:</LABEL>"
658 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
659 " <LABEL for=\"street-address\">Address:</LABEL>"
660 " <TEXTAREA id=\"street-address\">"
661 "123 Fantasy Ln. "
664 " <LABEL for=\"state\">State:</LABEL>"
665 " <SELECT id=\"state\"/>"
666 " <OPTION value=\"CA\">California</OPTION>"
667 " <OPTION value=\"TX\">Texas</OPTION>"
669 " <LABEL for=\"password\">Password:</LABEL>"
670 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>"
671 " <LABEL for=\"month\">Card expiration:</LABEL>"
672 " <INPUT type=\"month\" id=\"month\" value=\"2011-12\"/>"
673 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
674 // The below inputs should be ignored
675 " <LABEL for=\"notvisible\">Hidden:</LABEL>"
676 " <INPUT type=\"hidden\" id=\"notvisible\" value=\"apple\"/>"
679 WebFrame
* frame
= GetMainFrame();
680 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
682 WebVector
<WebFormElement
> forms
;
683 frame
->document().forms(forms
);
684 ASSERT_EQ(1U, forms
.size());
686 WebElement element
= frame
->document().getElementById("firstname");
687 WebInputElement input_element
= element
.to
<WebInputElement
>();
691 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
693 autofill::REQUIRE_NONE
,
694 autofill::EXTRACT_VALUE
,
697 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
698 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
699 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
701 const std::vector
<FormFieldData
>& fields
= form
.fields
;
702 ASSERT_EQ(6U, fields
.size());
704 FormFieldData expected
;
705 expected
.name
= ASCIIToUTF16("firstname");
706 expected
.value
= ASCIIToUTF16("John");
707 expected
.label
= ASCIIToUTF16("First name:");
708 expected
.form_control_type
= "text";
709 expected
.max_length
= WebInputElement::defaultMaxLength();
710 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
712 expected
.name
= ASCIIToUTF16("lastname");
713 expected
.value
= ASCIIToUTF16("Smith");
714 expected
.label
= ASCIIToUTF16("Last name:");
715 expected
.form_control_type
= "text";
716 expected
.max_length
= WebInputElement::defaultMaxLength();
717 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
719 expected
.name
= ASCIIToUTF16("street-address");
720 expected
.value
= ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
721 expected
.label
= ASCIIToUTF16("Address:");
722 expected
.form_control_type
= "textarea";
723 expected
.max_length
= 0;
724 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
726 expected
.name
= ASCIIToUTF16("state");
727 expected
.value
= ASCIIToUTF16("CA");
728 expected
.label
= ASCIIToUTF16("State:");
729 expected
.form_control_type
= "select-one";
730 expected
.max_length
= 0;
731 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
733 expected
.name
= ASCIIToUTF16("password");
734 expected
.value
= ASCIIToUTF16("secret");
735 expected
.label
= ASCIIToUTF16("Password:");
736 expected
.form_control_type
= "password";
737 expected
.max_length
= WebInputElement::defaultMaxLength();
738 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[4]);
740 expected
.name
= ASCIIToUTF16("month");
741 expected
.value
= ASCIIToUTF16("2011-12");
742 expected
.label
= ASCIIToUTF16("Card expiration:");
743 expected
.form_control_type
= "month";
744 expected
.max_length
= 0;
745 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[5]);
748 // We should not be able to serialize a form with too many fillable fields.
749 TEST_F(FormAutofillTest
, WebFormElementToFormDataTooManyFields
) {
751 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">";
752 for (size_t i
= 0; i
< (autofill::kMaxParseableFields
+ 1); ++i
) {
753 html
+= "<INPUT type=\"text\"/>";
756 LoadHTML(html
.c_str());
758 WebFrame
* frame
= GetMainFrame();
759 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
761 WebVector
<WebFormElement
> forms
;
762 frame
->document().forms(forms
);
763 ASSERT_EQ(1U, forms
.size());
765 WebElement element
= frame
->document().getElementById("firstname");
766 WebInputElement input_element
= element
.to
<WebInputElement
>();
770 EXPECT_FALSE(WebFormElementToFormData(forms
[0],
772 autofill::REQUIRE_NONE
,
773 autofill::EXTRACT_VALUE
,
778 TEST_F(FormAutofillTest
, ExtractForms
) {
779 ExpectJohnSmithLabels(
780 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
781 " First name: <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
782 " Last name: <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
783 " Email: <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
784 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
788 TEST_F(FormAutofillTest
, ExtractMultipleForms
) {
789 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
790 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
791 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
792 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
793 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
795 "<FORM name=\"TestForm2\" action=\"http://zoo.com\" method=\"post\">"
796 " <INPUT type=\"text\" id=\"firstname\" value=\"Jack\"/>"
797 " <INPUT type=\"text\" id=\"lastname\" value=\"Adams\"/>"
798 " <INPUT type=\"text\" id=\"email\" value=\"jack@example.com\"/>"
799 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
802 WebFrame
* web_frame
= GetMainFrame();
803 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
805 FormCache form_cache
;
806 std::vector
<FormData
> forms
;
807 form_cache
.ExtractForms(*web_frame
, &forms
);
808 ASSERT_EQ(2U, forms
.size());
811 const FormData
& form
= forms
[0];
812 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
813 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
814 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
816 const std::vector
<FormFieldData
>& fields
= form
.fields
;
817 ASSERT_EQ(3U, fields
.size());
819 FormFieldData expected
;
820 expected
.form_control_type
= "text";
821 expected
.max_length
= WebInputElement::defaultMaxLength();
823 expected
.name
= ASCIIToUTF16("firstname");
824 expected
.value
= ASCIIToUTF16("John");
825 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
827 expected
.name
= ASCIIToUTF16("lastname");
828 expected
.value
= ASCIIToUTF16("Smith");
829 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
831 expected
.name
= ASCIIToUTF16("email");
832 expected
.value
= ASCIIToUTF16("john@example.com");
833 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
836 const FormData
& form2
= forms
[1];
837 EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2
.name
);
838 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
839 EXPECT_EQ(GURL("http://zoo.com"), form2
.action
);
841 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
842 ASSERT_EQ(3U, fields2
.size());
844 expected
.name
= ASCIIToUTF16("firstname");
845 expected
.value
= ASCIIToUTF16("Jack");
846 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
848 expected
.name
= ASCIIToUTF16("lastname");
849 expected
.value
= ASCIIToUTF16("Adams");
850 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
852 expected
.name
= ASCIIToUTF16("email");
853 expected
.value
= ASCIIToUTF16("jack@example.com");
854 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
857 // We should not extract a form if it has too few fillable fields.
858 TEST_F(FormAutofillTest
, ExtractFormsTooFewFields
) {
859 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
860 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
861 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
862 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
865 WebFrame
* web_frame
= GetMainFrame();
866 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
868 FormCache form_cache
;
869 std::vector
<FormData
> forms
;
870 form_cache
.ExtractForms(*web_frame
, &forms
);
871 EXPECT_EQ(0U, forms
.size());
874 // We should not report additional forms for empty forms.
875 TEST_F(FormAutofillTest
, ExtractFormsSkippedForms
) {
876 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
877 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
878 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
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_TRUE(has_skipped_forms
);
894 // We should not report additional forms for empty forms.
895 TEST_F(FormAutofillTest
, ExtractFormsNoFields
) {
896 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
899 WebFrame
* web_frame
= GetMainFrame();
900 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
902 FormCache form_cache
;
903 std::vector
<FormData
> forms
;
904 bool has_skipped_forms
= form_cache
.ExtractFormsAndFormElements(*web_frame
,
908 EXPECT_EQ(0U, forms
.size());
909 EXPECT_FALSE(has_skipped_forms
);
912 // We should not extract a form if it has too few fillable fields.
913 // Make sure radio and checkbox fields don't count.
914 TEST_F(FormAutofillTest
, ExtractFormsTooFewFieldsSkipsCheckable
) {
915 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
916 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
917 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
918 " <INPUT type=\"radio\" id=\"a_radio\" value=\"0\"/>"
919 " <INPUT type=\"checkbox\" id=\"a_check\" value=\"1\"/>"
920 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
923 WebFrame
* web_frame
= GetMainFrame();
924 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
926 FormCache form_cache
;
927 std::vector
<FormData
> forms
;
928 form_cache
.ExtractForms(*web_frame
, &forms
);
929 EXPECT_EQ(0U, forms
.size());
932 TEST_F(FormAutofillTest
, WebFormElementToFormDataAutocomplete
) {
934 // Form is not auto-completable due to autocomplete=off.
935 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\""
937 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
938 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
939 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
940 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
943 WebFrame
* web_frame
= GetMainFrame();
944 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
946 WebVector
<WebFormElement
> web_forms
;
947 web_frame
->document().forms(web_forms
);
948 ASSERT_EQ(1U, web_forms
.size());
949 WebFormElement web_form
= web_forms
[0];
952 EXPECT_TRUE(WebFormElementToFormData(
953 web_form
, WebFormControlElement(), autofill::REQUIRE_NONE
,
954 autofill::EXTRACT_NONE
, &form
, NULL
));
955 EXPECT_FALSE(WebFormElementToFormData(
956 web_form
, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE
,
957 autofill::EXTRACT_NONE
, &form
, NULL
));
961 // The firstname element is not auto-completable due to autocomplete=off.
962 LoadHTML("<FORM name=\"TestForm\" action=\"http://abc.com\" "
964 " <INPUT type=\"text\" id=\"firstname\" value=\"John\""
966 " <INPUT type=\"text\" id=\"middlename\" value=\"Jack\"/>"
967 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
968 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
969 " <INPUT type=\"submit\" name=\"reply\" value=\"Send\"/>"
972 WebFrame
* web_frame
= GetMainFrame();
973 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
975 WebVector
<WebFormElement
> web_forms
;
976 web_frame
->document().forms(web_forms
);
977 ASSERT_EQ(1U, web_forms
.size());
978 WebFormElement web_form
= web_forms
[0];
981 EXPECT_TRUE(WebFormElementToFormData(
982 web_form
, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE
,
983 autofill::EXTRACT_VALUE
, &form
, NULL
));
985 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
986 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
987 EXPECT_EQ(GURL("http://abc.com"), form
.action
);
989 const std::vector
<FormFieldData
>& fields
= form
.fields
;
990 ASSERT_EQ(3U, fields
.size());
992 FormFieldData expected
;
993 expected
.form_control_type
= "text";
994 expected
.max_length
= WebInputElement::defaultMaxLength();
996 expected
.name
= ASCIIToUTF16("middlename");
997 expected
.value
= ASCIIToUTF16("Jack");
998 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
1000 expected
.name
= ASCIIToUTF16("lastname");
1001 expected
.value
= ASCIIToUTF16("Smith");
1002 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
1004 expected
.name
= ASCIIToUTF16("email");
1005 expected
.value
= ASCIIToUTF16("john@example.com");
1006 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
1010 TEST_F(FormAutofillTest
, FindFormForInputElement
) {
1011 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
1012 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1013 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1014 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\""
1015 "autocomplete=\"off\" />"
1016 " <INPUT type=\"text\" id=\"phone\" value=\"1.800.555.1234\"/>"
1017 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1020 WebFrame
* web_frame
= GetMainFrame();
1021 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
1023 FormCache form_cache
;
1024 std::vector
<FormData
> forms
;
1025 form_cache
.ExtractForms(*web_frame
, &forms
);
1026 ASSERT_EQ(1U, forms
.size());
1028 // Get the input element we want to find.
1029 WebElement element
= web_frame
->document().getElementById("firstname");
1030 WebInputElement input_element
= element
.to
<WebInputElement
>();
1032 // Find the form and verify it's the correct form.
1034 FormFieldData field
;
1035 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
1038 autofill::REQUIRE_NONE
));
1039 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
1040 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
1041 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
1043 const std::vector
<FormFieldData
>& fields
= form
.fields
;
1044 ASSERT_EQ(4U, fields
.size());
1046 FormFieldData expected
;
1047 expected
.form_control_type
= "text";
1048 expected
.max_length
= WebInputElement::defaultMaxLength();
1050 expected
.name
= ASCIIToUTF16("firstname");
1051 expected
.value
= ASCIIToUTF16("John");
1052 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
1053 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
1055 expected
.name
= ASCIIToUTF16("lastname");
1056 expected
.value
= ASCIIToUTF16("Smith");
1057 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
1059 expected
.name
= ASCIIToUTF16("email");
1060 expected
.value
= ASCIIToUTF16("john@example.com");
1061 expected
.autocomplete_attribute
= "off";
1062 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
1063 expected
.autocomplete_attribute
= std::string(); // reset
1065 expected
.name
= ASCIIToUTF16("phone");
1066 expected
.value
= ASCIIToUTF16("1.800.555.1234");
1067 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
1069 // Try again, but require autocomplete.
1071 FormFieldData field2
;
1072 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1076 autofill::REQUIRE_AUTOCOMPLETE
));
1077 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
1078 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
1079 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
1081 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
1082 ASSERT_EQ(3U, fields2
.size());
1084 expected
.form_control_type
= "text";
1085 expected
.max_length
= WebInputElement::defaultMaxLength();
1087 expected
.name
= ASCIIToUTF16("firstname");
1088 expected
.value
= ASCIIToUTF16("John");
1089 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
1090 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
1092 expected
.name
= ASCIIToUTF16("lastname");
1093 expected
.value
= ASCIIToUTF16("Smith");
1094 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
1096 expected
.name
= ASCIIToUTF16("phone");
1097 expected
.value
= ASCIIToUTF16("1.800.555.1234");
1098 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
1101 TEST_F(FormAutofillTest
, FindFormForTextAreaElement
) {
1102 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
1103 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1104 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1105 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\""
1106 "autocomplete=\"off\" />"
1107 " <TEXTAREA id=\"street-address\">"
1108 "123 Fantasy Ln. "
1111 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1114 WebFrame
* web_frame
= GetMainFrame();
1115 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
1117 FormCache form_cache
;
1118 std::vector
<FormData
> forms
;
1119 form_cache
.ExtractForms(*web_frame
, &forms
);
1120 ASSERT_EQ(1U, forms
.size());
1122 // Get the textarea element we want to find.
1123 WebElement element
= web_frame
->document().getElementById("street-address");
1124 WebTextAreaElement textarea_element
= element
.to
<WebTextAreaElement
>();
1126 // Find the form and verify it's the correct form.
1128 FormFieldData field
;
1129 EXPECT_TRUE(FindFormAndFieldForFormControlElement(textarea_element
,
1132 autofill::REQUIRE_NONE
));
1133 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
1134 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
1135 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
1137 const std::vector
<FormFieldData
>& fields
= form
.fields
;
1138 ASSERT_EQ(4U, fields
.size());
1140 FormFieldData expected
;
1142 expected
.name
= ASCIIToUTF16("firstname");
1143 expected
.value
= ASCIIToUTF16("John");
1144 expected
.form_control_type
= "text";
1145 expected
.max_length
= WebInputElement::defaultMaxLength();
1146 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
1148 expected
.name
= ASCIIToUTF16("lastname");
1149 expected
.value
= ASCIIToUTF16("Smith");
1150 expected
.form_control_type
= "text";
1151 expected
.max_length
= WebInputElement::defaultMaxLength();
1152 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
1154 expected
.name
= ASCIIToUTF16("email");
1155 expected
.value
= ASCIIToUTF16("john@example.com");
1156 expected
.autocomplete_attribute
= "off";
1157 expected
.form_control_type
= "text";
1158 expected
.max_length
= WebInputElement::defaultMaxLength();
1159 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
1160 expected
.autocomplete_attribute
= std::string(); // reset
1162 expected
.name
= ASCIIToUTF16("street-address");
1163 expected
.value
= ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1164 expected
.form_control_type
= "textarea";
1165 expected
.max_length
= 0;
1166 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
1167 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
1169 // Try again, but require autocomplete.
1171 FormFieldData field2
;
1172 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1176 autofill::REQUIRE_AUTOCOMPLETE
));
1177 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
1178 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
1179 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
1181 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
1182 ASSERT_EQ(3U, fields2
.size());
1184 expected
.name
= ASCIIToUTF16("firstname");
1185 expected
.value
= ASCIIToUTF16("John");
1186 expected
.form_control_type
= "text";
1187 expected
.max_length
= WebInputElement::defaultMaxLength();
1188 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
1190 expected
.name
= ASCIIToUTF16("lastname");
1191 expected
.value
= ASCIIToUTF16("Smith");
1192 expected
.form_control_type
= "text";
1193 expected
.max_length
= WebInputElement::defaultMaxLength();
1194 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
1196 expected
.name
= ASCIIToUTF16("street-address");
1197 expected
.value
= ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1198 expected
.form_control_type
= "textarea";
1199 expected
.max_length
= 0;
1200 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
1201 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
1204 // Test regular FillForm function.
1205 TEST_F(FormAutofillTest
, FillForm
) {
1206 static const AutofillFieldCase field_cases
[] = {
1207 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1208 // should_be_autofilled, autofill_value, expected_value
1210 // Regular empty fields (firstname & lastname) should be autofilled.
1211 {"text", "firstname", "", "", true, "filled firstname",
1212 "filled firstname"},
1213 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1214 // hidden fields should not be extracted to form_data.
1215 // Non empty fields should not be autofilled.
1216 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
1217 // "noautocomplete" should not be extracted to form_data.
1218 // Disabled fields should not be autofilled.
1219 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1220 // Readonly fields should not be autofilled.
1221 {"text", "readonly", "", "", false, "filled readonly", ""},
1222 // Fields with "visibility: hidden" should not be autofilled.
1223 {"text", "invisible", "", "", false, "filled invisible", ""},
1224 // Fields with "display:none" should not be autofilled.
1225 {"text", "displaynone", "", "", false, "filled displaynone", ""},
1226 // Regular <input type="month"> should be autofilled.
1227 {"month", "month", "", "", true, "2017-11", "2017-11"},
1228 // Non-empty <input type="month"> should not be autofilled.
1229 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
1230 // Regular select fields should be autofilled.
1231 {"select-one", "select", "", "", true, "TX", "TX"},
1232 // Select fields should be autofilled even if they already have a
1234 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1235 // Regular textarea elements should be autofilled.
1236 {"textarea", "textarea", "", "", true, "some multi-\nline value",
1237 "some multi-\nline value"},
1238 // Non-empty textarea elements should not be autofilled.
1239 {"textarea", "textarea-nonempty", "Go\naway!", "", false,
1240 "some multi-\nline value", "Go\naway!"},
1242 TestFormFillFunctions(kFormHtml
, field_cases
, arraysize(field_cases
),
1243 FillForm
, &GetValueWrapper
);
1244 // Verify preview selection.
1245 WebInputElement firstname
= GetMainFrame()->document().
1246 getElementById("firstname").to
<WebInputElement
>();
1247 EXPECT_EQ(16, firstname
.selectionStart());
1248 EXPECT_EQ(16, firstname
.selectionEnd());
1251 TEST_F(FormAutofillTest
, FillFormIncludingNonFocusableElements
) {
1252 static const AutofillFieldCase field_cases
[] = {
1253 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1254 // should_be_autofilled, autofill_value, expected_value
1256 // Regular empty fields (firstname & lastname) should be autofilled.
1257 {"text", "firstname", "", "", true, "filled firstname",
1258 "filled firstname"},
1259 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1260 // hidden fields should not be extracted to form_data.
1261 // Non empty fields should be overriden.
1262 {"text", "notempty", "Hi", "", true, "filled notempty",
1264 // "noautocomplete" should not be extracted to form_data.
1265 // Disabled fields should not be autofilled.
1266 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1267 // Readonly fields should not be autofilled.
1268 {"text", "readonly", "", "", false, "filled readonly", ""},
1269 // Fields with "visibility: hidden" should also be autofilled.
1270 {"text", "invisible", "", "", true, "filled invisible",
1271 "filled invisible"},
1272 // Fields with "display:none" should also be autofilled.
1273 {"text", "displaynone", "", "", true, "filled displaynone",
1274 "filled displaynone"},
1275 // Regular <input type="month"> should be autofilled.
1276 {"month", "month", "", "", true, "2017-11", "2017-11"},
1277 // Non-empty <input type="month"> should be overridden.
1278 {"month", "month-nonempty", "2011-12", "", true, "2017-11", "2017-11"},
1279 // Regular select fields should be autofilled.
1280 {"select-one", "select", "", "", true, "TX", "TX"},
1281 // Select fields should be autofilled even if they already have a
1283 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1284 // Regular textarea elements should be autofilled.
1285 {"textarea", "textarea", "", "", true, "some multi-\nline value",
1286 "some multi-\nline value"},
1287 // Nonempty textarea elements should be overridden.
1288 {"textarea", "textarea-nonempty", "Go\naway!", "", true,
1289 "some multi-\nline value", "some multi-\nline value"},
1291 TestFormFillFunctions(kFormHtml
, field_cases
, arraysize(field_cases
),
1292 &FillFormIncludingNonFocusableElementsWrapper
,
1296 TEST_F(FormAutofillTest
, PreviewForm
) {
1297 static const AutofillFieldCase field_cases
[] = {
1298 // Normal empty fields should be previewed.
1299 {"text", "firstname", "", "", true, "suggested firstname",
1300 "suggested firstname"},
1301 {"text", "lastname", "", "", true, "suggested lastname",
1302 "suggested lastname"},
1303 // Hidden fields should not be extracted to form_data.
1304 // Non empty fields should not be previewed.
1305 {"text", "notempty", "Hi", "", false, "suggested notempty", ""},
1306 // "noautocomplete" should not be extracted to form_data.
1307 // Disabled fields should not be previewed.
1308 {"text", "notenabled", "", "", false, "suggested notenabled", ""},
1309 // Readonly fields should not be previewed.
1310 {"text", "readonly", "", "", false, "suggested readonly", ""},
1311 // Fields with "visibility: hidden" should not be previewed.
1312 {"text", "invisible", "", "", false, "suggested invisible",
1314 // Fields with "display:none" should not previewed.
1315 {"text", "displaynone", "", "", false, "suggested displaynone",
1317 // Regular <input type="month"> should be previewed.
1318 {"month", "month", "", "", true, "2017-11", "2017-11"},
1319 // Non-empty <input type="month"> should not be previewed.
1320 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
1321 // Regular select fields should be previewed.
1322 {"select-one", "select", "", "", true, "TX", "TX"},
1323 // Select fields should be previewed even if they already have a
1325 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1326 // Normal textarea elements should be previewed.
1327 {"textarea", "textarea", "", "", true, "suggested multi-\nline value",
1328 "suggested multi-\nline value"},
1329 // Nonempty textarea elements should not be previewed.
1330 {"textarea", "textarea-nonempty", "Go\naway!", "", false,
1331 "suggested multi-\nline value", ""},
1333 TestFormFillFunctions(kFormHtml
, field_cases
, arraysize(field_cases
),
1334 &PreviewForm
, &GetSuggestedValueWrapper
);
1336 // Verify preview selection.
1337 WebInputElement firstname
= GetMainFrame()->document().
1338 getElementById("firstname").to
<WebInputElement
>();
1339 EXPECT_EQ(0, firstname
.selectionStart());
1340 EXPECT_EQ(19, firstname
.selectionEnd());
1343 TEST_F(FormAutofillTest
, Labels
) {
1344 ExpectJohnSmithLabels(
1345 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1346 " <LABEL for=\"firstname\"> First name: </LABEL>"
1347 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1348 " <LABEL for=\"lastname\"> Last name: </LABEL>"
1349 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1350 " <LABEL for=\"email\"> Email: </LABEL>"
1351 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1352 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1356 TEST_F(FormAutofillTest
, LabelsWithSpans
) {
1357 ExpectJohnSmithLabels(
1358 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1359 " <LABEL for=\"firstname\"><span>First name: </span></LABEL>"
1360 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1361 " <LABEL for=\"lastname\"><span>Last name: </span></LABEL>"
1362 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1363 " <LABEL for=\"email\"><span>Email: </span></LABEL>"
1364 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1365 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1369 // This test is different from FormAutofillTest.Labels in that the label
1370 // elements for= attribute is set to the name of the form control element it is
1371 // a label for instead of the id of the form control element. This is invalid
1372 // because the for= attribute must be set to the id of the form control element;
1373 // however, current label parsing code will extract the text from the previous
1374 // label element and apply it to the following input field.
1375 TEST_F(FormAutofillTest
, InvalidLabels
) {
1376 ExpectJohnSmithLabels(
1377 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1378 " <LABEL for=\"firstname\"> First name: </LABEL>"
1379 " <INPUT type=\"text\" name=\"firstname\" value=\"John\"/>"
1380 " <LABEL for=\"lastname\"> Last name: </LABEL>"
1381 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\"/>"
1382 " <LABEL for=\"email\"> Email: </LABEL>"
1383 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\"/>"
1384 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1388 // This test has three form control elements, only one of which has a label
1389 // element associated with it.
1390 TEST_F(FormAutofillTest
, OneLabelElement
) {
1391 ExpectJohnSmithLabels(
1392 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1394 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1395 " <LABEL for=\"lastname\">Last name: </LABEL>"
1396 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1398 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1399 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1403 TEST_F(FormAutofillTest
, LabelsInferredFromText
) {
1404 ExpectJohnSmithLabels(
1405 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1407 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1409 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1411 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1412 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1416 TEST_F(FormAutofillTest
, LabelsInferredFromParagraph
) {
1417 ExpectJohnSmithLabels(
1418 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1419 " <P>First name:</P><INPUT type=\"text\" "
1420 " id=\"firstname\" value=\"John\"/>"
1421 " <P>Last name:</P>"
1422 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1424 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1425 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1429 TEST_F(FormAutofillTest
, LabelsInferredFromBold
) {
1430 ExpectJohnSmithLabels(
1431 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1432 " <B>First name:</B><INPUT type=\"text\" "
1433 " id=\"firstname\" value=\"John\"/>"
1434 " <B>Last name:</B>"
1435 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1437 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1438 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1442 TEST_F(FormAutofillTest
, LabelsInferredPriorToImgOrBr
) {
1443 ExpectJohnSmithLabels(
1444 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1445 " First name:<IMG/><INPUT type=\"text\" "
1446 " id=\"firstname\" value=\"John\"/>"
1448 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1450 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1451 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1455 TEST_F(FormAutofillTest
, LabelsInferredFromTableCell
) {
1456 ExpectJohnSmithLabels(
1457 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1460 " <TD>First name:</TD>"
1461 " <TD><INPUT type=\"text\" id=\"firstname\" value=\"John\"/></TD>"
1464 " <TD>Last name:</TD>"
1465 " <TD><INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/></TD>"
1469 " <TD><INPUT type=\"text\" id=\"email\""
1470 " value=\"john@example.com\"/></TD>"
1475 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1482 TEST_F(FormAutofillTest
, LabelsInferredFromTableCellTH
) {
1483 ExpectJohnSmithLabels(
1484 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1487 " <TH>First name:</TH>"
1488 " <TD><INPUT type=\"text\" id=\"firstname\" value=\"John\"/></TD>"
1491 " <TH>Last name:</TH>"
1492 " <TD><INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/></TD>"
1496 " <TD><INPUT type=\"text\" id=\"email\""
1497 " value=\"john@example.com\"/></TD>"
1502 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1509 TEST_F(FormAutofillTest
, LabelsInferredFromTableCellNested
) {
1510 std::vector
<base::string16
> labels
, names
, values
;
1512 labels
.push_back(ASCIIToUTF16("First name: Bogus"));
1513 names
.push_back(ASCIIToUTF16("firstname"));
1514 values
.push_back(ASCIIToUTF16("John"));
1516 labels
.push_back(ASCIIToUTF16("Last name:"));
1517 names
.push_back(ASCIIToUTF16("lastname"));
1518 values
.push_back(ASCIIToUTF16("Smith"));
1520 labels
.push_back(ASCIIToUTF16("Email:"));
1521 names
.push_back(ASCIIToUTF16("email"));
1522 values
.push_back(ASCIIToUTF16("john@example.com"));
1525 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1538 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1550 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1562 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1569 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1574 labels
, names
, values
);
1577 TEST_F(FormAutofillTest
, LabelsInferredFromTableEmptyTDs
) {
1578 std::vector
<base::string16
> labels
, names
, values
;
1580 labels
.push_back(ASCIIToUTF16("* First Name"));
1581 names
.push_back(ASCIIToUTF16("firstname"));
1582 values
.push_back(ASCIIToUTF16("John"));
1584 labels
.push_back(ASCIIToUTF16("* Last Name"));
1585 names
.push_back(ASCIIToUTF16("lastname"));
1586 values
.push_back(ASCIIToUTF16("Smith"));
1588 labels
.push_back(ASCIIToUTF16("* Email"));
1589 names
.push_back(ASCIIToUTF16("email"));
1590 values
.push_back(ASCIIToUTF16("john@example.com"));
1593 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1598 " <B>First Name</B>"
1602 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1612 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1622 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1628 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1633 labels
, names
, values
);
1636 TEST_F(FormAutofillTest
, LabelsInferredFromPreviousTD
) {
1637 std::vector
<base::string16
> labels
, names
, values
;
1639 labels
.push_back(ASCIIToUTF16("* First Name"));
1640 names
.push_back(ASCIIToUTF16("firstname"));
1641 values
.push_back(ASCIIToUTF16("John"));
1643 labels
.push_back(ASCIIToUTF16("* Last Name"));
1644 names
.push_back(ASCIIToUTF16("lastname"));
1645 values
.push_back(ASCIIToUTF16("Smith"));
1647 labels
.push_back(ASCIIToUTF16("* Email"));
1648 names
.push_back(ASCIIToUTF16("email"));
1649 values
.push_back(ASCIIToUTF16("john@example.com"));
1652 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1655 " <TD>* First Name</TD>"
1658 " <INPUT type=\"hidden\"/>"
1659 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1663 " <TD>* Last Name</TD>"
1665 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1671 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1677 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1682 labels
, names
, values
);
1685 // <script>, <noscript> and <option> tags are excluded when the labels are
1687 // Also <!-- comment --> is excluded.
1688 TEST_F(FormAutofillTest
, LabelsInferredFromTableWithSpecialElements
) {
1689 std::vector
<base::string16
> labels
, names
, values
;
1690 std::vector
<std::string
> control_types
;
1692 labels
.push_back(ASCIIToUTF16("* First Name"));
1693 names
.push_back(ASCIIToUTF16("firstname"));
1694 values
.push_back(ASCIIToUTF16("John"));
1695 control_types
.push_back("text");
1697 labels
.push_back(ASCIIToUTF16("* Middle Name"));
1698 names
.push_back(ASCIIToUTF16("middlename"));
1699 values
.push_back(ASCIIToUTF16("Joe"));
1700 control_types
.push_back("text");
1702 labels
.push_back(ASCIIToUTF16("* Last Name"));
1703 names
.push_back(ASCIIToUTF16("lastname"));
1704 values
.push_back(ASCIIToUTF16("Smith"));
1705 control_types
.push_back("text");
1707 labels
.push_back(ASCIIToUTF16("* Country"));
1708 names
.push_back(ASCIIToUTF16("country"));
1709 values
.push_back(ASCIIToUTF16("US"));
1710 control_types
.push_back("select-one");
1712 labels
.push_back(ASCIIToUTF16("* Email"));
1713 names
.push_back(ASCIIToUTF16("email"));
1714 values
.push_back(ASCIIToUTF16("john@example.com"));
1715 control_types
.push_back("text");
1717 ExpectLabelsAndTypes(
1718 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1723 " <B>First Name</B>"
1726 " <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
1728 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1734 " <B>Middle Name</B>"
1740 " <INPUT type=\"text\" id=\"middlename\" value=\"Joe\"/>"
1749 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1758 " <SELECT id=\"country\">"
1759 " <OPTION VALUE=\"US\">The value should be ignored as label."
1761 " <OPTION VALUE=\"JP\">JAPAN</OPTION>"
1771 " <!-- This comment should be ignored as inferred label.-->"
1772 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1778 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1783 labels
, names
, values
, control_types
);
1786 TEST_F(FormAutofillTest
, LabelsInferredFromTableLabels
) {
1787 ExpectJohnSmithLabels(
1788 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1792 " <LABEL>First name:</LABEL>"
1793 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1798 " <LABEL>Last name:</LABEL>"
1799 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1804 " <LABEL>Email:</LABEL>"
1805 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1809 "<INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1813 TEST_F(FormAutofillTest
, LabelsInferredFromTableTDInterveningElements
) {
1814 ExpectJohnSmithLabels(
1815 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1821 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1828 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1835 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1839 "<INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1843 // Verify that we correctly infer labels when the label text spans multiple
1844 // adjacent HTML elements, not separated by whitespace.
1845 TEST_F(FormAutofillTest
, LabelsInferredFromTableAdjacentElements
) {
1846 std::vector
<base::string16
> labels
, names
, values
;
1848 labels
.push_back(ASCIIToUTF16("*First Name"));
1849 names
.push_back(ASCIIToUTF16("firstname"));
1850 values
.push_back(ASCIIToUTF16("John"));
1852 labels
.push_back(ASCIIToUTF16("*Last Name"));
1853 names
.push_back(ASCIIToUTF16("lastname"));
1854 values
.push_back(ASCIIToUTF16("Smith"));
1856 labels
.push_back(ASCIIToUTF16("*Email"));
1857 names
.push_back(ASCIIToUTF16("email"));
1858 values
.push_back(ASCIIToUTF16("john@example.com"));
1861 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1865 " <SPAN>*</SPAN><B>First Name</B>"
1868 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1873 " <SPAN>*</SPAN><B>Last Name</B>"
1876 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1881 " <SPAN>*</SPAN><B>Email</B>"
1884 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1889 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1894 labels
, names
, values
);
1897 // Verify that we correctly infer labels when the label text resides in the
1899 TEST_F(FormAutofillTest
, LabelsInferredFromTableRow
) {
1900 std::vector
<base::string16
> labels
, names
, values
;
1902 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1903 names
.push_back(ASCIIToUTF16("firstname"));
1904 values
.push_back(ASCIIToUTF16("John"));
1906 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1907 names
.push_back(ASCIIToUTF16("lastname"));
1908 values
.push_back(ASCIIToUTF16("Smith"));
1910 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1911 names
.push_back(ASCIIToUTF16("email"));
1912 values
.push_back(ASCIIToUTF16("john@example.com"));
1915 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1918 " <TD>*First Name</TD>"
1919 " <TD>*Last Name</TD>"
1924 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1927 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1930 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1935 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1939 labels
, names
, values
);
1942 // Verify that we correctly infer labels when enclosed within a list item.
1943 TEST_F(FormAutofillTest
, LabelsInferredFromListItem
) {
1944 std::vector
<base::string16
> labels
, names
, values
;
1946 labels
.push_back(ASCIIToUTF16("* Home Phone"));
1947 names
.push_back(ASCIIToUTF16("areacode"));
1948 values
.push_back(ASCIIToUTF16("415"));
1950 labels
.push_back(ASCIIToUTF16("* Home Phone"));
1951 names
.push_back(ASCIIToUTF16("prefix"));
1952 values
.push_back(ASCIIToUTF16("555"));
1954 labels
.push_back(ASCIIToUTF16("* Home Phone"));
1955 names
.push_back(ASCIIToUTF16("suffix"));
1956 values
.push_back(ASCIIToUTF16("1212"));
1959 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1962 " <SPAN>Bogus</SPAN>"
1965 " <LABEL><EM>*</EM> Home Phone</LABEL>"
1966 " <INPUT type=\"text\" id=\"areacode\" value=\"415\"/>"
1967 " <INPUT type=\"text\" id=\"prefix\" value=\"555\"/>"
1968 " <INPUT type=\"text\" id=\"suffix\" value=\"1212\"/>"
1971 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1975 labels
, names
, values
);
1978 TEST_F(FormAutofillTest
, LabelsInferredFromDefinitionList
) {
1979 std::vector
<base::string16
> labels
, names
, values
;
1981 labels
.push_back(ASCIIToUTF16("* First name: Bogus"));
1982 names
.push_back(ASCIIToUTF16("firstname"));
1983 values
.push_back(ASCIIToUTF16("John"));
1985 labels
.push_back(ASCIIToUTF16("Last name:"));
1986 names
.push_back(ASCIIToUTF16("lastname"));
1987 values
.push_back(ASCIIToUTF16("Smith"));
1989 labels
.push_back(ASCIIToUTF16("Email:"));
1990 names
.push_back(ASCIIToUTF16("email"));
1991 values
.push_back(ASCIIToUTF16("john@example.com"));
1994 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2009 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
2019 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
2029 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
2034 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2038 labels
, names
, values
);
2041 TEST_F(FormAutofillTest
, LabelsInferredWithSameName
) {
2042 std::vector
<base::string16
> labels
, names
, values
;
2044 labels
.push_back(ASCIIToUTF16("Address Line 1:"));
2045 names
.push_back(ASCIIToUTF16("Address"));
2046 values
.push_back(base::string16());
2048 labels
.push_back(ASCIIToUTF16("Address Line 2:"));
2049 names
.push_back(ASCIIToUTF16("Address"));
2050 values
.push_back(base::string16());
2052 labels
.push_back(ASCIIToUTF16("Address Line 3:"));
2053 names
.push_back(ASCIIToUTF16("Address"));
2054 values
.push_back(base::string16());
2057 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2059 " <INPUT type=\"text\" name=\"Address\"/>"
2061 " <INPUT type=\"text\" name=\"Address\"/>"
2063 " <INPUT type=\"text\" name=\"Address\"/>"
2064 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2066 labels
, names
, values
);
2069 TEST_F(FormAutofillTest
, LabelsInferredWithImageTags
) {
2070 std::vector
<base::string16
> labels
, names
, values
;
2072 labels
.push_back(ASCIIToUTF16("Phone:"));
2073 names
.push_back(ASCIIToUTF16("dayphone1"));
2074 values
.push_back(base::string16());
2076 labels
.push_back(ASCIIToUTF16("-"));
2077 names
.push_back(ASCIIToUTF16("dayphone2"));
2078 values
.push_back(base::string16());
2080 labels
.push_back(ASCIIToUTF16("-"));
2081 names
.push_back(ASCIIToUTF16("dayphone3"));
2082 values
.push_back(base::string16());
2084 labels
.push_back(ASCIIToUTF16("ext.:"));
2085 names
.push_back(ASCIIToUTF16("dayphone4"));
2086 values
.push_back(base::string16());
2088 labels
.push_back(base::string16());
2089 names
.push_back(ASCIIToUTF16("dummy"));
2090 values
.push_back(base::string16());
2093 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2095 " <input type=\"text\" name=\"dayphone1\">"
2099 " <input type=\"text\" name=\"dayphone2\">"
2103 " <input type=\"text\" name=\"dayphone3\">"
2105 " <input type=\"text\" name=\"dayphone4\">"
2106 " <input type=\"text\" name=\"dummy\">"
2107 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2109 labels
, names
, values
);
2112 TEST_F(FormAutofillTest
, LabelsInferredFromDivTable
) {
2113 ExpectJohnSmithLabels(
2114 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2115 "<DIV>First name:<BR>"
2117 " <INPUT type=\"text\" name=\"firstname\" value=\"John\">"
2120 "<DIV>Last name:<BR>"
2122 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\">"
2127 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\">"
2130 "<input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2134 TEST_F(FormAutofillTest
, LabelsInferredFromDivSiblingTable
) {
2135 ExpectJohnSmithLabels(
2136 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2137 "<DIV>First name:</DIV>"
2140 " <INPUT type=\"text\" name=\"firstname\" value=\"John\">"
2143 "<DIV>Last name:</DIV>"
2146 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\">"
2152 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\">"
2155 "<input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2159 TEST_F(FormAutofillTest
, LabelsInferredFromDefinitionListRatherThanDivTable
) {
2160 ExpectJohnSmithLabels(
2161 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2162 "<DIV>This is not a label.<BR>"
2171 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
2181 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
2191 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
2196 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2203 TEST_F(FormAutofillTest
, FillFormMaxLength
) {
2204 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2205 " <INPUT type=\"text\" id=\"firstname\" maxlength=\"5\"/>"
2206 " <INPUT type=\"text\" id=\"lastname\" maxlength=\"7\"/>"
2207 " <INPUT type=\"text\" id=\"email\" maxlength=\"9\"/>"
2208 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2211 WebFrame
* web_frame
= GetMainFrame();
2212 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2214 FormCache form_cache
;
2215 std::vector
<FormData
> forms
;
2216 form_cache
.ExtractForms(*web_frame
, &forms
);
2217 ASSERT_EQ(1U, forms
.size());
2219 // Get the input element we want to find.
2220 WebElement element
= web_frame
->document().getElementById("firstname");
2221 WebInputElement input_element
= element
.to
<WebInputElement
>();
2223 // Find the form that contains the input element.
2225 FormFieldData field
;
2226 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2229 autofill::REQUIRE_NONE
));
2230 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2231 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2232 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
2234 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2235 ASSERT_EQ(3U, fields
.size());
2237 FormFieldData expected
;
2238 expected
.form_control_type
= "text";
2240 expected
.name
= ASCIIToUTF16("firstname");
2241 expected
.max_length
= 5;
2242 expected
.is_autofilled
= false;
2243 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2245 expected
.name
= ASCIIToUTF16("lastname");
2246 expected
.max_length
= 7;
2247 expected
.is_autofilled
= false;
2248 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2250 expected
.name
= ASCIIToUTF16("email");
2251 expected
.max_length
= 9;
2252 expected
.is_autofilled
= false;
2253 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2256 form
.fields
[0].value
= ASCIIToUTF16("Brother");
2257 form
.fields
[1].value
= ASCIIToUTF16("Jonathan");
2258 form
.fields
[2].value
= ASCIIToUTF16("brotherj@example.com");
2259 FillForm(form
, input_element
);
2261 // Find the newly-filled form that contains the input element.
2263 FormFieldData field2
;
2264 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2267 autofill::REQUIRE_NONE
));
2269 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2270 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2271 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2273 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2274 ASSERT_EQ(3U, fields2
.size());
2276 expected
.form_control_type
= "text";
2278 expected
.name
= ASCIIToUTF16("firstname");
2279 expected
.value
= ASCIIToUTF16("Broth");
2280 expected
.max_length
= 5;
2281 expected
.is_autofilled
= true;
2282 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2284 expected
.name
= ASCIIToUTF16("lastname");
2285 expected
.value
= ASCIIToUTF16("Jonatha");
2286 expected
.max_length
= 7;
2287 expected
.is_autofilled
= true;
2288 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2290 expected
.name
= ASCIIToUTF16("email");
2291 expected
.value
= ASCIIToUTF16("brotherj@");
2292 expected
.max_length
= 9;
2293 expected
.is_autofilled
= true;
2294 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2297 // This test uses negative values of the maxlength attribute for input elements.
2298 // In this case, the maxlength of the input elements is set to the default
2299 // maxlength (defined in WebKit.)
2300 TEST_F(FormAutofillTest
, FillFormNegativeMaxLength
) {
2301 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2302 " <INPUT type=\"text\" id=\"firstname\" maxlength=\"-1\"/>"
2303 " <INPUT type=\"text\" id=\"lastname\" maxlength=\"-10\"/>"
2304 " <INPUT type=\"text\" id=\"email\" maxlength=\"-13\"/>"
2305 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2308 WebFrame
* web_frame
= GetMainFrame();
2309 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2311 FormCache form_cache
;
2312 std::vector
<FormData
> forms
;
2313 form_cache
.ExtractForms(*web_frame
, &forms
);
2314 ASSERT_EQ(1U, forms
.size());
2316 // Get the input element we want to find.
2317 WebElement element
= web_frame
->document().getElementById("firstname");
2318 WebInputElement input_element
= element
.to
<WebInputElement
>();
2320 // Find the form that contains the input element.
2322 FormFieldData field
;
2323 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2326 autofill::REQUIRE_NONE
));
2327 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2328 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2329 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
2331 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2332 ASSERT_EQ(3U, fields
.size());
2334 FormFieldData expected
;
2335 expected
.form_control_type
= "text";
2336 expected
.max_length
= WebInputElement::defaultMaxLength();
2338 expected
.name
= ASCIIToUTF16("firstname");
2339 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2341 expected
.name
= ASCIIToUTF16("lastname");
2342 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2344 expected
.name
= ASCIIToUTF16("email");
2345 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2348 form
.fields
[0].value
= ASCIIToUTF16("Brother");
2349 form
.fields
[1].value
= ASCIIToUTF16("Jonathan");
2350 form
.fields
[2].value
= ASCIIToUTF16("brotherj@example.com");
2351 FillForm(form
, input_element
);
2353 // Find the newly-filled form that contains the input element.
2355 FormFieldData field2
;
2356 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2359 autofill::REQUIRE_NONE
));
2361 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2362 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2363 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2365 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2366 ASSERT_EQ(3U, fields2
.size());
2368 expected
.name
= ASCIIToUTF16("firstname");
2369 expected
.value
= ASCIIToUTF16("Brother");
2370 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2372 expected
.name
= ASCIIToUTF16("lastname");
2373 expected
.value
= ASCIIToUTF16("Jonathan");
2374 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2376 expected
.name
= ASCIIToUTF16("email");
2377 expected
.value
= ASCIIToUTF16("brotherj@example.com");
2378 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2381 TEST_F(FormAutofillTest
, FillFormEmptyName
) {
2382 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2383 " <INPUT type=\"text\" id=\"firstname\"/>"
2384 " <INPUT type=\"text\" id=\"lastname\"/>"
2385 " <INPUT type=\"text\" id=\"email\"/>"
2386 " <INPUT type=\"submit\" value=\"Send\"/>"
2389 WebFrame
* web_frame
= GetMainFrame();
2390 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2392 FormCache form_cache
;
2393 std::vector
<FormData
> forms
;
2394 form_cache
.ExtractForms(*web_frame
, &forms
);
2395 ASSERT_EQ(1U, forms
.size());
2397 // Get the input element we want to find.
2398 WebElement element
= web_frame
->document().getElementById("firstname");
2399 WebInputElement input_element
= element
.to
<WebInputElement
>();
2401 // Find the form that contains the input element.
2403 FormFieldData field
;
2404 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2407 autofill::REQUIRE_NONE
));
2408 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2409 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2410 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
2412 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2413 ASSERT_EQ(3U, fields
.size());
2415 FormFieldData expected
;
2416 expected
.form_control_type
= "text";
2417 expected
.max_length
= WebInputElement::defaultMaxLength();
2419 expected
.name
= ASCIIToUTF16("firstname");
2420 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2422 expected
.name
= ASCIIToUTF16("lastname");
2423 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2425 expected
.name
= ASCIIToUTF16("email");
2426 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2429 form
.fields
[0].value
= ASCIIToUTF16("Wyatt");
2430 form
.fields
[1].value
= ASCIIToUTF16("Earp");
2431 form
.fields
[2].value
= ASCIIToUTF16("wyatt@example.com");
2432 FillForm(form
, input_element
);
2434 // Find the newly-filled form that contains the input element.
2436 FormFieldData field2
;
2437 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2440 autofill::REQUIRE_NONE
));
2442 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2443 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2444 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2446 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2447 ASSERT_EQ(3U, fields2
.size());
2449 expected
.form_control_type
= "text";
2450 expected
.max_length
= WebInputElement::defaultMaxLength();
2452 expected
.name
= ASCIIToUTF16("firstname");
2453 expected
.value
= ASCIIToUTF16("Wyatt");
2454 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2456 expected
.name
= ASCIIToUTF16("lastname");
2457 expected
.value
= ASCIIToUTF16("Earp");
2458 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2460 expected
.name
= ASCIIToUTF16("email");
2461 expected
.value
= ASCIIToUTF16("wyatt@example.com");
2462 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2465 TEST_F(FormAutofillTest
, FillFormEmptyFormNames
) {
2466 LoadHTML("<FORM action=\"http://buh.com\" method=\"post\">"
2467 " <INPUT type=\"text\" id=\"firstname\"/>"
2468 " <INPUT type=\"text\" id=\"middlename\"/>"
2469 " <INPUT type=\"text\" id=\"lastname\"/>"
2470 " <INPUT type=\"submit\" value=\"Send\"/>"
2472 "<FORM action=\"http://abc.com\" method=\"post\">"
2473 " <INPUT type=\"text\" id=\"apple\"/>"
2474 " <INPUT type=\"text\" id=\"banana\"/>"
2475 " <INPUT type=\"text\" id=\"cantelope\"/>"
2476 " <INPUT type=\"submit\" value=\"Send\"/>"
2479 WebFrame
* web_frame
= GetMainFrame();
2480 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2482 FormCache form_cache
;
2483 std::vector
<FormData
> forms
;
2484 form_cache
.ExtractForms(*web_frame
, &forms
);
2485 ASSERT_EQ(2U, forms
.size());
2487 // Get the input element we want to find.
2488 WebElement element
= web_frame
->document().getElementById("apple");
2489 WebInputElement input_element
= element
.to
<WebInputElement
>();
2491 // Find the form that contains the input element.
2493 FormFieldData field
;
2494 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2497 autofill::REQUIRE_NONE
));
2498 EXPECT_EQ(base::string16(), form
.name
);
2499 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2500 EXPECT_EQ(GURL("http://abc.com"), form
.action
);
2502 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2503 ASSERT_EQ(3U, fields
.size());
2505 FormFieldData expected
;
2506 expected
.form_control_type
= "text";
2507 expected
.max_length
= WebInputElement::defaultMaxLength();
2509 expected
.name
= ASCIIToUTF16("apple");
2510 expected
.is_autofilled
= false;
2511 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2513 expected
.name
= ASCIIToUTF16("banana");
2514 expected
.is_autofilled
= false;
2515 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2517 expected
.name
= ASCIIToUTF16("cantelope");
2518 expected
.is_autofilled
= false;
2519 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2522 form
.fields
[0].value
= ASCIIToUTF16("Red");
2523 form
.fields
[1].value
= ASCIIToUTF16("Yellow");
2524 form
.fields
[2].value
= ASCIIToUTF16("Also Yellow");
2525 FillForm(form
, input_element
);
2527 // Find the newly-filled form that contains the input element.
2529 FormFieldData field2
;
2530 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2533 autofill::REQUIRE_NONE
));
2535 EXPECT_EQ(base::string16(), form2
.name
);
2536 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2537 EXPECT_EQ(GURL("http://abc.com"), form2
.action
);
2539 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2540 ASSERT_EQ(3U, fields2
.size());
2542 expected
.name
= ASCIIToUTF16("apple");
2543 expected
.value
= ASCIIToUTF16("Red");
2544 expected
.is_autofilled
= true;
2545 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2547 expected
.name
= ASCIIToUTF16("banana");
2548 expected
.value
= ASCIIToUTF16("Yellow");
2549 expected
.is_autofilled
= true;
2550 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2552 expected
.name
= ASCIIToUTF16("cantelope");
2553 expected
.value
= ASCIIToUTF16("Also Yellow");
2554 expected
.is_autofilled
= true;
2555 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2558 TEST_F(FormAutofillTest
, ThreePartPhone
) {
2559 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2561 " <input type=\"text\" name=\"dayphone1\">"
2563 " <input type=\"text\" name=\"dayphone2\">"
2565 " <input type=\"text\" name=\"dayphone3\">"
2567 " <input type=\"text\" name=\"dayphone4\">"
2568 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2572 WebFrame
* frame
= GetMainFrame();
2573 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
2575 WebVector
<WebFormElement
> forms
;
2576 frame
->document().forms(forms
);
2577 ASSERT_EQ(1U, forms
.size());
2580 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
2581 WebFormControlElement(),
2582 autofill::REQUIRE_NONE
,
2583 autofill::EXTRACT_VALUE
,
2586 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2587 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
2588 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
2590 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2591 ASSERT_EQ(4U, fields
.size());
2593 FormFieldData expected
;
2594 expected
.form_control_type
= "text";
2595 expected
.max_length
= WebInputElement::defaultMaxLength();
2597 expected
.label
= ASCIIToUTF16("Phone:");
2598 expected
.name
= ASCIIToUTF16("dayphone1");
2599 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2601 expected
.label
= ASCIIToUTF16("-");
2602 expected
.name
= ASCIIToUTF16("dayphone2");
2603 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2605 expected
.label
= ASCIIToUTF16("-");
2606 expected
.name
= ASCIIToUTF16("dayphone3");
2607 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2609 expected
.label
= ASCIIToUTF16("ext.:");
2610 expected
.name
= ASCIIToUTF16("dayphone4");
2611 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
2615 TEST_F(FormAutofillTest
, MaxLengthFields
) {
2616 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2618 " <input type=\"text\" maxlength=\"3\" name=\"dayphone1\">"
2620 " <input type=\"text\" maxlength=\"3\" name=\"dayphone2\">"
2622 " <input type=\"text\" maxlength=\"4\" size=\"5\""
2623 " name=\"dayphone3\">"
2625 " <input type=\"text\" maxlength=\"5\" name=\"dayphone4\">"
2626 " <input type=\"text\" name=\"default1\">"
2627 " <input type=\"text\" maxlength=\"-1\" name=\"invalid1\">"
2628 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2631 WebFrame
* frame
= GetMainFrame();
2632 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
2634 WebVector
<WebFormElement
> forms
;
2635 frame
->document().forms(forms
);
2636 ASSERT_EQ(1U, forms
.size());
2639 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
2640 WebFormControlElement(),
2641 autofill::REQUIRE_NONE
,
2642 autofill::EXTRACT_VALUE
,
2645 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2646 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
2647 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
2649 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2650 ASSERT_EQ(6U, fields
.size());
2652 FormFieldData expected
;
2653 expected
.form_control_type
= "text";
2655 expected
.label
= ASCIIToUTF16("Phone:");
2656 expected
.name
= ASCIIToUTF16("dayphone1");
2657 expected
.max_length
= 3;
2658 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2660 expected
.label
= ASCIIToUTF16("-");
2661 expected
.name
= ASCIIToUTF16("dayphone2");
2662 expected
.max_length
= 3;
2663 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2665 expected
.label
= ASCIIToUTF16("-");
2666 expected
.name
= ASCIIToUTF16("dayphone3");
2667 expected
.max_length
= 4;
2668 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2670 expected
.label
= ASCIIToUTF16("ext.:");
2671 expected
.name
= ASCIIToUTF16("dayphone4");
2672 expected
.max_length
= 5;
2673 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
2675 // When unspecified |size|, default is returned.
2676 expected
.label
= base::string16();
2677 expected
.name
= ASCIIToUTF16("default1");
2678 expected
.max_length
= WebInputElement::defaultMaxLength();
2679 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[4]);
2681 // When invalid |size|, default is returned.
2682 expected
.label
= base::string16();
2683 expected
.name
= ASCIIToUTF16("invalid1");
2684 expected
.max_length
= WebInputElement::defaultMaxLength();
2685 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[5]);
2688 // This test re-creates the experience of typing in a field then selecting a
2689 // profile from the Autofill suggestions popup. The field that is being typed
2690 // into should be filled even though it's not technically empty.
2691 TEST_F(FormAutofillTest
, FillFormNonEmptyField
) {
2692 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2693 " <INPUT type=\"text\" id=\"firstname\"/>"
2694 " <INPUT type=\"text\" id=\"lastname\"/>"
2695 " <INPUT type=\"text\" id=\"email\"/>"
2696 " <INPUT type=\"submit\" value=\"Send\"/>"
2699 WebFrame
* web_frame
= GetMainFrame();
2700 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2702 FormCache form_cache
;
2703 std::vector
<FormData
> forms
;
2704 form_cache
.ExtractForms(*web_frame
, &forms
);
2705 ASSERT_EQ(1U, forms
.size());
2707 // Get the input element we want to find.
2708 WebElement element
= web_frame
->document().getElementById("firstname");
2709 WebInputElement input_element
= element
.to
<WebInputElement
>();
2711 // Simulate typing by modifying the field value.
2712 input_element
.setValue(ASCIIToUTF16("Wy"));
2714 // Find the form that contains the input element.
2716 FormFieldData field
;
2717 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2720 autofill::REQUIRE_NONE
));
2721 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2722 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2723 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
2725 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2726 ASSERT_EQ(3U, fields
.size());
2728 FormFieldData expected
;
2729 expected
.form_control_type
= "text";
2730 expected
.max_length
= WebInputElement::defaultMaxLength();
2732 expected
.name
= ASCIIToUTF16("firstname");
2733 expected
.value
= ASCIIToUTF16("Wy");
2734 expected
.is_autofilled
= false;
2735 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2737 expected
.name
= ASCIIToUTF16("lastname");
2738 expected
.value
= base::string16();
2739 expected
.is_autofilled
= false;
2740 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2742 expected
.name
= ASCIIToUTF16("email");
2743 expected
.value
= base::string16();
2744 expected
.is_autofilled
= false;
2745 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2747 // Preview the form and verify that the cursor position has been updated.
2748 form
.fields
[0].value
= ASCIIToUTF16("Wyatt");
2749 form
.fields
[1].value
= ASCIIToUTF16("Earp");
2750 form
.fields
[2].value
= ASCIIToUTF16("wyatt@example.com");
2751 PreviewForm(form
, input_element
);
2752 EXPECT_EQ(2, input_element
.selectionStart());
2753 EXPECT_EQ(5, input_element
.selectionEnd());
2756 FillForm(form
, input_element
);
2758 // Find the newly-filled form that contains the input element.
2760 FormFieldData field2
;
2761 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element
,
2764 autofill::REQUIRE_NONE
));
2766 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2767 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2768 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2770 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2771 ASSERT_EQ(3U, fields2
.size());
2773 expected
.name
= ASCIIToUTF16("firstname");
2774 expected
.value
= ASCIIToUTF16("Wyatt");
2775 expected
.is_autofilled
= true;
2776 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2778 expected
.name
= ASCIIToUTF16("lastname");
2779 expected
.value
= ASCIIToUTF16("Earp");
2780 expected
.is_autofilled
= true;
2781 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2783 expected
.name
= ASCIIToUTF16("email");
2784 expected
.value
= ASCIIToUTF16("wyatt@example.com");
2785 expected
.is_autofilled
= true;
2786 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2788 // Verify that the cursor position has been updated.
2789 EXPECT_EQ(5, input_element
.selectionStart());
2790 EXPECT_EQ(5, input_element
.selectionEnd());
2793 TEST_F(FormAutofillTest
, ClearFormWithNode
) {
2795 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2796 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2797 " <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>"
2798 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noAC\" value=\"one\"/>"
2799 " <INPUT type=\"text\" id=\"notenabled\" disabled=\"disabled\">"
2800 " <INPUT type=\"month\" id=\"month\" value=\"2012-11\">"
2801 " <INPUT type=\"month\" id=\"month-disabled\" value=\"2012-11\""
2802 " disabled=\"disabled\">"
2803 " <TEXTAREA id=\"textarea\">Apple.</TEXTAREA>"
2804 " <TEXTAREA id=\"textarea-disabled\" disabled=\"disabled\">"
2807 " <TEXTAREA id=\"textarea-noAC\" autocomplete=\"off\">Carrot?</TEXTAREA>"
2808 " <INPUT type=\"submit\" value=\"Send\"/>"
2811 WebFrame
* web_frame
= GetMainFrame();
2812 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2814 FormCache form_cache
;
2815 std::vector
<FormData
> forms
;
2816 form_cache
.ExtractForms(*web_frame
, &forms
);
2817 ASSERT_EQ(1U, forms
.size());
2819 // Set the auto-filled attribute on the firstname element.
2820 WebInputElement firstname
=
2821 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2822 firstname
.setAutofilled(true);
2824 // Set the value of the disabled text input element.
2825 WebInputElement notenabled
=
2826 web_frame
->document().getElementById("notenabled").to
<WebInputElement
>();
2827 notenabled
.setValue(WebString::fromUTF8("no clear"));
2830 EXPECT_TRUE(form_cache
.ClearFormWithElement(firstname
));
2832 // Verify that the auto-filled attribute has been turned off.
2833 EXPECT_FALSE(firstname
.isAutofilled());
2835 // Verify the form is cleared.
2837 FormFieldData field2
;
2838 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname
,
2841 autofill::REQUIRE_NONE
));
2842 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2843 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2844 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2846 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2847 ASSERT_EQ(9U, fields2
.size());
2849 FormFieldData expected
;
2850 expected
.form_control_type
= "text";
2851 expected
.max_length
= WebInputElement::defaultMaxLength();
2853 expected
.name
= ASCIIToUTF16("firstname");
2854 expected
.value
= base::string16();
2855 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2857 expected
.name
= ASCIIToUTF16("lastname");
2858 expected
.value
= base::string16();
2859 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2861 expected
.name
= ASCIIToUTF16("noAC");
2862 expected
.value
= base::string16();
2863 expected
.autocomplete_attribute
= "off";
2864 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2865 expected
.autocomplete_attribute
= std::string(); // reset
2867 expected
.name
= ASCIIToUTF16("notenabled");
2868 expected
.value
= ASCIIToUTF16("no clear");
2869 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[3]);
2871 expected
.form_control_type
= "month";
2872 expected
.max_length
= 0;
2873 expected
.name
= ASCIIToUTF16("month");
2874 expected
.value
= base::string16();
2875 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[4]);
2877 expected
.name
= ASCIIToUTF16("month-disabled");
2878 expected
.value
= ASCIIToUTF16("2012-11");
2879 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[5]);
2881 expected
.form_control_type
= "textarea";
2882 expected
.name
= ASCIIToUTF16("textarea");
2883 expected
.value
= base::string16();
2884 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[6]);
2886 expected
.name
= ASCIIToUTF16("textarea-disabled");
2887 expected
.value
= ASCIIToUTF16(" Banana! ");
2888 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[7]);
2890 expected
.name
= ASCIIToUTF16("textarea-noAC");
2891 expected
.value
= base::string16();
2892 expected
.autocomplete_attribute
= "off";
2893 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[8]);
2894 expected
.autocomplete_attribute
= std::string(); // reset
2896 // Verify that the cursor position has been updated.
2897 EXPECT_EQ(0, firstname
.selectionStart());
2898 EXPECT_EQ(0, firstname
.selectionEnd());
2901 TEST_F(FormAutofillTest
, ClearFormWithNodeContainingSelectOne
) {
2903 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2904 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2905 " <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>"
2906 " <SELECT id=\"state\" name=\"state\">"
2907 " <OPTION selected>?</OPTION>"
2908 " <OPTION>AA</OPTION>"
2909 " <OPTION>AE</OPTION>"
2910 " <OPTION>AK</OPTION>"
2912 " <INPUT type=\"submit\" value=\"Send\"/>"
2915 WebFrame
* web_frame
= GetMainFrame();
2916 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2918 FormCache form_cache
;
2919 std::vector
<FormData
> forms
;
2920 form_cache
.ExtractForms(*web_frame
, &forms
);
2921 ASSERT_EQ(1U, forms
.size());
2923 // Set the auto-filled attribute on the firstname element.
2924 WebInputElement firstname
=
2925 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2926 firstname
.setAutofilled(true);
2928 // Set the value of the select-one.
2929 WebSelectElement select_element
=
2930 web_frame
->document().getElementById("state").to
<WebSelectElement
>();
2931 select_element
.setValue(WebString::fromUTF8("AK"));
2934 EXPECT_TRUE(form_cache
.ClearFormWithElement(firstname
));
2936 // Verify that the auto-filled attribute has been turned off.
2937 EXPECT_FALSE(firstname
.isAutofilled());
2939 // Verify the form is cleared.
2941 FormFieldData field2
;
2942 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname
,
2945 autofill::REQUIRE_NONE
));
2946 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2947 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2948 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2950 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2951 ASSERT_EQ(3U, fields2
.size());
2953 FormFieldData expected
;
2955 expected
.name
= ASCIIToUTF16("firstname");
2956 expected
.value
= base::string16();
2957 expected
.form_control_type
= "text";
2958 expected
.max_length
= WebInputElement::defaultMaxLength();
2959 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2961 expected
.name
= ASCIIToUTF16("lastname");
2962 expected
.value
= base::string16();
2963 expected
.form_control_type
= "text";
2964 expected
.max_length
= WebInputElement::defaultMaxLength();
2965 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2967 expected
.name
= ASCIIToUTF16("state");
2968 expected
.value
= ASCIIToUTF16("?");
2969 expected
.form_control_type
= "select-one";
2970 expected
.max_length
= 0;
2971 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2973 // Verify that the cursor position has been updated.
2974 EXPECT_EQ(0, firstname
.selectionStart());
2975 EXPECT_EQ(0, firstname
.selectionEnd());
2978 TEST_F(FormAutofillTest
, ClearPreviewedFormWithElement
) {
2979 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2980 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2981 " <INPUT type=\"text\" id=\"lastname\"/>"
2982 " <INPUT type=\"text\" id=\"email\"/>"
2983 " <INPUT type=\"email\" id=\"email2\"/>"
2984 " <INPUT type=\"tel\" id=\"phone\"/>"
2985 " <INPUT type=\"submit\" value=\"Send\"/>"
2988 WebFrame
* web_frame
= GetMainFrame();
2989 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2991 FormCache form_cache
;
2992 std::vector
<FormData
> forms
;
2993 form_cache
.ExtractForms(*web_frame
, &forms
);
2994 ASSERT_EQ(1U, forms
.size());
2996 // Set the auto-filled attribute.
2997 WebInputElement firstname
=
2998 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2999 firstname
.setAutofilled(true);
3000 WebInputElement lastname
=
3001 web_frame
->document().getElementById("lastname").to
<WebInputElement
>();
3002 lastname
.setAutofilled(true);
3003 WebInputElement email
=
3004 web_frame
->document().getElementById("email").to
<WebInputElement
>();
3005 email
.setAutofilled(true);
3006 WebInputElement email2
=
3007 web_frame
->document().getElementById("email2").to
<WebInputElement
>();
3008 email2
.setAutofilled(true);
3009 WebInputElement phone
=
3010 web_frame
->document().getElementById("phone").to
<WebInputElement
>();
3011 phone
.setAutofilled(true);
3013 // Set the suggested values on two of the elements.
3014 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
3015 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3016 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3017 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3019 // Clear the previewed fields.
3020 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname
, false));
3022 // Fields with empty suggestions suggestions are not modified.
3023 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname
.value());
3024 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
3025 EXPECT_TRUE(firstname
.isAutofilled());
3027 // Verify the previewed fields are cleared.
3028 EXPECT_TRUE(lastname
.value().isEmpty());
3029 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
3030 EXPECT_FALSE(lastname
.isAutofilled());
3031 EXPECT_TRUE(email
.value().isEmpty());
3032 EXPECT_TRUE(email
.suggestedValue().isEmpty());
3033 EXPECT_FALSE(email
.isAutofilled());
3034 EXPECT_TRUE(email2
.value().isEmpty());
3035 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
3036 EXPECT_FALSE(email2
.isAutofilled());
3037 EXPECT_TRUE(phone
.value().isEmpty());
3038 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
3039 EXPECT_FALSE(phone
.isAutofilled());
3041 // Verify that the cursor position has been updated.
3042 EXPECT_EQ(0, lastname
.selectionStart());
3043 EXPECT_EQ(0, lastname
.selectionEnd());
3046 TEST_F(FormAutofillTest
, ClearPreviewedFormWithNonEmptyInitiatingNode
) {
3047 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
3048 " <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
3049 " <INPUT type=\"text\" id=\"lastname\"/>"
3050 " <INPUT type=\"text\" id=\"email\"/>"
3051 " <INPUT type=\"email\" id=\"email2\"/>"
3052 " <INPUT type=\"tel\" id=\"phone\"/>"
3053 " <INPUT type=\"submit\" value=\"Send\"/>"
3056 WebFrame
* web_frame
= GetMainFrame();
3057 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
3059 FormCache form_cache
;
3060 std::vector
<FormData
> forms
;
3061 form_cache
.ExtractForms(*web_frame
, &forms
);
3062 ASSERT_EQ(1U, forms
.size());
3064 // Set the auto-filled attribute.
3065 WebInputElement firstname
=
3066 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
3067 firstname
.setAutofilled(true);
3068 WebInputElement lastname
=
3069 web_frame
->document().getElementById("lastname").to
<WebInputElement
>();
3070 lastname
.setAutofilled(true);
3071 WebInputElement email
=
3072 web_frame
->document().getElementById("email").to
<WebInputElement
>();
3073 email
.setAutofilled(true);
3074 WebInputElement email2
=
3075 web_frame
->document().getElementById("email2").to
<WebInputElement
>();
3076 email2
.setAutofilled(true);
3077 WebInputElement phone
=
3078 web_frame
->document().getElementById("phone").to
<WebInputElement
>();
3079 phone
.setAutofilled(true);
3082 // Set the suggested values on all of the elements.
3083 firstname
.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3084 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
3085 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3086 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3087 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3089 // Clear the previewed fields.
3090 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname
, false));
3092 // Fields with non-empty values are restored.
3093 EXPECT_EQ(ASCIIToUTF16("W"), firstname
.value());
3094 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
3095 EXPECT_FALSE(firstname
.isAutofilled());
3096 EXPECT_EQ(1, firstname
.selectionStart());
3097 EXPECT_EQ(1, firstname
.selectionEnd());
3099 // Verify the previewed fields are cleared.
3100 EXPECT_TRUE(lastname
.value().isEmpty());
3101 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
3102 EXPECT_FALSE(lastname
.isAutofilled());
3103 EXPECT_TRUE(email
.value().isEmpty());
3104 EXPECT_TRUE(email
.suggestedValue().isEmpty());
3105 EXPECT_FALSE(email
.isAutofilled());
3106 EXPECT_TRUE(email2
.value().isEmpty());
3107 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
3108 EXPECT_FALSE(email2
.isAutofilled());
3109 EXPECT_TRUE(phone
.value().isEmpty());
3110 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
3111 EXPECT_FALSE(phone
.isAutofilled());
3114 TEST_F(FormAutofillTest
, ClearPreviewedFormWithAutofilledInitiatingNode
) {
3115 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
3116 " <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
3117 " <INPUT type=\"text\" id=\"lastname\"/>"
3118 " <INPUT type=\"text\" id=\"email\"/>"
3119 " <INPUT type=\"email\" id=\"email2\"/>"
3120 " <INPUT type=\"tel\" id=\"phone\"/>"
3121 " <INPUT type=\"submit\" value=\"Send\"/>"
3124 WebFrame
* web_frame
= GetMainFrame();
3125 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
3127 FormCache form_cache
;
3128 std::vector
<FormData
> forms
;
3129 form_cache
.ExtractForms(*web_frame
, &forms
);
3130 ASSERT_EQ(1U, forms
.size());
3132 // Set the auto-filled attribute.
3133 WebInputElement firstname
=
3134 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
3135 firstname
.setAutofilled(true);
3136 WebInputElement lastname
=
3137 web_frame
->document().getElementById("lastname").to
<WebInputElement
>();
3138 lastname
.setAutofilled(true);
3139 WebInputElement email
=
3140 web_frame
->document().getElementById("email").to
<WebInputElement
>();
3141 email
.setAutofilled(true);
3142 WebInputElement email2
=
3143 web_frame
->document().getElementById("email2").to
<WebInputElement
>();
3144 email2
.setAutofilled(true);
3145 WebInputElement phone
=
3146 web_frame
->document().getElementById("phone").to
<WebInputElement
>();
3147 phone
.setAutofilled(true);
3149 // Set the suggested values on all of the elements.
3150 firstname
.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3151 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
3152 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3153 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3154 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3156 // Clear the previewed fields.
3157 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname
, true));
3159 // Fields with non-empty values are restored.
3160 EXPECT_EQ(ASCIIToUTF16("W"), firstname
.value());
3161 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
3162 EXPECT_TRUE(firstname
.isAutofilled());
3163 EXPECT_EQ(1, firstname
.selectionStart());
3164 EXPECT_EQ(1, firstname
.selectionEnd());
3166 // Verify the previewed fields are cleared.
3167 EXPECT_TRUE(lastname
.value().isEmpty());
3168 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
3169 EXPECT_FALSE(lastname
.isAutofilled());
3170 EXPECT_TRUE(email
.value().isEmpty());
3171 EXPECT_TRUE(email
.suggestedValue().isEmpty());
3172 EXPECT_FALSE(email
.isAutofilled());
3173 EXPECT_TRUE(email2
.value().isEmpty());
3174 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
3175 EXPECT_FALSE(email2
.isAutofilled());
3176 EXPECT_TRUE(phone
.value().isEmpty());
3177 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
3178 EXPECT_FALSE(phone
.isAutofilled());
3181 TEST_F(FormAutofillTest
, FormWithNodeIsAutofilled
) {
3182 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
3183 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
3184 " <INPUT type=\"text\" id=\"lastname\"/>"
3185 " <INPUT type=\"text\" id=\"email\"/>"
3186 " <INPUT type=\"email\" id=\"email2\"/>"
3187 " <INPUT type=\"tel\" id=\"phone\"/>"
3188 " <INPUT type=\"submit\" value=\"Send\"/>"
3191 WebFrame
* web_frame
= GetMainFrame();
3192 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
3194 FormCache form_cache
;
3195 std::vector
<FormData
> forms
;
3196 form_cache
.ExtractForms(*web_frame
, &forms
);
3197 ASSERT_EQ(1U, forms
.size());
3199 WebInputElement firstname
=
3200 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
3202 // Auto-filled attribute not set yet.
3203 EXPECT_FALSE(FormWithElementIsAutofilled(firstname
));
3205 // Set the auto-filled attribute.
3206 firstname
.setAutofilled(true);
3208 EXPECT_TRUE(FormWithElementIsAutofilled(firstname
));
3211 // If we have multiple labels per id, the labels concatenated into label string.
3212 TEST_F(FormAutofillTest
, MultipleLabelsPerElement
) {
3213 std::vector
<base::string16
> labels
, names
, values
;
3215 labels
.push_back(ASCIIToUTF16("First Name:"));
3216 names
.push_back(ASCIIToUTF16("firstname"));
3217 values
.push_back(ASCIIToUTF16("John"));
3219 labels
.push_back(ASCIIToUTF16("Last Name:"));
3220 names
.push_back(ASCIIToUTF16("lastname"));
3221 values
.push_back(ASCIIToUTF16("Smith"));
3223 labels
.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
3224 names
.push_back(ASCIIToUTF16("email"));
3225 values
.push_back(ASCIIToUTF16("john@example.com"));
3228 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
3229 " <LABEL for=\"firstname\"> First Name: </LABEL>"
3230 " <LABEL for=\"firstname\"></LABEL>"
3231 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
3232 " <LABEL for=\"lastname\"></LABEL>"
3233 " <LABEL for=\"lastname\"> Last Name: </LABEL>"
3234 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
3235 " <LABEL for=\"email\"> Email: </LABEL>"
3236 " <LABEL for=\"email\"> xxx@yyy.com </LABEL>"
3237 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
3238 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
3240 labels
, names
, values
);
3243 TEST_F(FormAutofillTest
, ClickElement
) {
3244 LoadHTML("<BUTTON id=\"link\">Button</BUTTON>"
3245 "<BUTTON name=\"button\">Button</BUTTON>");
3246 WebFrame
* frame
= GetMainFrame();
3247 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
3249 // Successful retrieval by id.
3250 autofill::WebElementDescriptor clicker
;
3251 clicker
.retrieval_method
= autofill::WebElementDescriptor::ID
;
3252 clicker
.descriptor
= "link";
3253 EXPECT_TRUE(ClickElement(frame
->document(), clicker
));
3255 // Successful retrieval by css selector.
3256 clicker
.retrieval_method
= autofill::WebElementDescriptor::CSS_SELECTOR
;
3257 clicker
.descriptor
= "button[name=\"button\"]";
3258 EXPECT_TRUE(ClickElement(frame
->document(), clicker
));
3260 // Unsuccessful retrieval due to invalid CSS selector.
3261 clicker
.descriptor
= "^*&";
3262 EXPECT_FALSE(ClickElement(frame
->document(), clicker
));
3264 // Unsuccessful retrieval because element does not exist.
3265 clicker
.descriptor
= "#junk";
3266 EXPECT_FALSE(ClickElement(frame
->document(), clicker
));
3269 TEST_F(FormAutofillTest
, SelectOneAsText
) {
3270 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
3271 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
3272 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
3273 " <SELECT id=\"country\">"
3274 " <OPTION value=\"AF\">Afghanistan</OPTION>"
3275 " <OPTION value=\"AL\">Albania</OPTION>"
3276 " <OPTION value=\"DZ\">Algeria</OPTION>"
3278 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
3281 WebFrame
* frame
= GetMainFrame();
3282 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
3284 // Set the value of the select-one.
3285 WebSelectElement select_element
=
3286 frame
->document().getElementById("country").to
<WebSelectElement
>();
3287 select_element
.setValue(WebString::fromUTF8("AL"));
3289 WebVector
<WebFormElement
> forms
;
3290 frame
->document().forms(forms
);
3291 ASSERT_EQ(1U, forms
.size());
3295 // Extract the country select-one value as text.
3296 EXPECT_TRUE(WebFormElementToFormData(
3297 forms
[0], WebFormControlElement(), autofill::REQUIRE_NONE
,
3298 static_cast<autofill::ExtractMask
>(
3299 autofill::EXTRACT_VALUE
| autofill::EXTRACT_OPTION_TEXT
),
3301 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
3302 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
3303 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
3305 const std::vector
<FormFieldData
>& fields
= form
.fields
;
3306 ASSERT_EQ(3U, fields
.size());
3308 FormFieldData expected
;
3310 expected
.name
= ASCIIToUTF16("firstname");
3311 expected
.value
= ASCIIToUTF16("John");
3312 expected
.form_control_type
= "text";
3313 expected
.max_length
= WebInputElement::defaultMaxLength();
3314 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
3316 expected
.name
= ASCIIToUTF16("lastname");
3317 expected
.value
= ASCIIToUTF16("Smith");
3318 expected
.form_control_type
= "text";
3319 expected
.max_length
= WebInputElement::defaultMaxLength();
3320 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
3322 expected
.name
= ASCIIToUTF16("country");
3323 expected
.value
= ASCIIToUTF16("Albania");
3324 expected
.form_control_type
= "select-one";
3325 expected
.max_length
= 0;
3326 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
3328 form
.fields
.clear();
3329 // Extract the country select-one value as value.
3330 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
3331 WebFormControlElement(),
3332 autofill::REQUIRE_NONE
,
3333 autofill::EXTRACT_VALUE
,
3336 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
3337 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
3338 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
3340 ASSERT_EQ(3U, fields
.size());
3342 expected
.name
= ASCIIToUTF16("firstname");
3343 expected
.value
= ASCIIToUTF16("John");
3344 expected
.form_control_type
= "text";
3345 expected
.max_length
= WebInputElement::defaultMaxLength();
3346 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
3348 expected
.name
= ASCIIToUTF16("lastname");
3349 expected
.value
= ASCIIToUTF16("Smith");
3350 expected
.form_control_type
= "text";
3351 expected
.max_length
= WebInputElement::defaultMaxLength();
3352 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
3354 expected
.name
= ASCIIToUTF16("country");
3355 expected
.value
= ASCIIToUTF16("AL");
3356 expected
.form_control_type
= "select-one";
3357 expected
.max_length
= 0;
3358 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
3361 } // namespace autofill