1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/format_macros.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/test/base/chrome_render_view_test.h"
12 #include "components/autofill/content/renderer/form_autofill_util.h"
13 #include "components/autofill/content/renderer/form_cache.h"
14 #include "components/autofill/core/common/autofill_data_validation.h"
15 #include "components/autofill/core/common/form_data.h"
16 #include "components/autofill/core/common/web_element_descriptor.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/platform/WebVector.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebElementCollection.h"
23 #include "third_party/WebKit/public/web/WebExceptionCode.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/WebLocalFrame.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::WebExceptionCode
;
35 using blink::WebFormControlElement
;
36 using blink::WebFormElement
;
37 using blink::WebFrame
;
38 using blink::WebInputElement
;
39 using blink::WebSelectElement
;
40 using blink::WebString
;
41 using blink::WebTextAreaElement
;
42 using blink::WebVector
;
48 struct AutofillFieldCase
{
49 const char* const form_control_type
;
50 const char* const name
;
51 const char* const initial_value
;
52 const char* const autocomplete_attribute
; // The autocomplete attribute of
54 bool should_be_autofilled
; // Whether the filed should be autofilled.
55 const char* const autofill_value
; // The value being used to fill the field.
56 const char* const expected_value
; // The expected value after Autofill
60 const char kFormHtml
[] =
61 "<FORM name='TestForm' action='http://buh.com' method='post'>"
62 " <INPUT type='text' id='firstname'/>"
63 " <INPUT type='text' id='lastname'/>"
64 " <INPUT type='hidden' id='imhidden'/>"
65 " <INPUT type='text' id='notempty' value='Hi'/>"
66 " <INPUT type='text' autocomplete='off' id='noautocomplete'/>"
67 " <INPUT type='text' disabled='disabled' id='notenabled'/>"
68 " <INPUT type='text' readonly id='readonly'/>"
69 " <INPUT type='text' style='visibility: hidden'"
71 " <INPUT type='text' style='display: none' id='displaynone'/>"
72 " <INPUT type='month' id='month'/>"
73 " <INPUT type='month' id='month-nonempty' value='2011-12'/>"
74 " <SELECT id='select'>"
76 " <OPTION value='CA'>California</OPTION>"
77 " <OPTION value='TX'>Texas</OPTION>"
79 " <SELECT id='select-nonempty'>"
80 " <OPTION value='CA' selected>California</OPTION>"
81 " <OPTION value='TX'>Texas</OPTION>"
83 " <SELECT id='select-unchanged'>"
84 " <OPTION value='CA' selected>California</OPTION>"
85 " <OPTION value='TX'>Texas</OPTION>"
87 " <TEXTAREA id='textarea'></TEXTAREA>"
88 " <TEXTAREA id='textarea-nonempty'>Go away!</TEXTAREA>"
89 " <INPUT type='submit' name='reply-send' value='Send'/>"
92 const char kUnownedFormHtml
[] =
93 "<INPUT type='text' id='firstname'/>"
94 "<INPUT type='text' id='lastname'/>"
95 "<INPUT type='hidden' id='imhidden'/>"
96 "<INPUT type='text' id='notempty' value='Hi'/>"
97 "<INPUT type='text' autocomplete='off' id='noautocomplete'/>"
98 "<INPUT type='text' disabled='disabled' id='notenabled'/>"
99 "<INPUT type='text' readonly id='readonly'/>"
100 "<INPUT type='text' style='visibility: hidden'"
102 "<INPUT type='text' style='display: none' id='displaynone'/>"
103 "<INPUT type='month' id='month'/>"
104 "<INPUT type='month' id='month-nonempty' value='2011-12'/>"
105 "<SELECT id='select'>"
107 " <OPTION value='CA'>California</OPTION>"
108 " <OPTION value='TX'>Texas</OPTION>"
110 "<SELECT id='select-nonempty'>"
111 " <OPTION value='CA' selected>California</OPTION>"
112 " <OPTION value='TX'>Texas</OPTION>"
114 "<SELECT id='select-unchanged'>"
115 " <OPTION value='CA' selected>California</OPTION>"
116 " <OPTION value='TX'>Texas</OPTION>"
118 "<TEXTAREA id='textarea'></TEXTAREA>"
119 "<TEXTAREA id='textarea-nonempty'>Go away!</TEXTAREA>"
120 "<INPUT type='submit' name='reply-send' value='Send'/>";
122 std::string
RetrievalMethodToString(
123 const WebElementDescriptor::RetrievalMethod
& method
) {
125 case WebElementDescriptor::CSS_SELECTOR
:
126 return "CSS_SELECTOR";
127 case WebElementDescriptor::ID
:
129 case WebElementDescriptor::NONE
:
136 bool ClickElement(const WebDocument
& document
,
137 const WebElementDescriptor
& element_descriptor
) {
138 WebString web_descriptor
= WebString::fromUTF8(element_descriptor
.descriptor
);
139 blink::WebElement element
;
141 switch (element_descriptor
.retrieval_method
) {
142 case WebElementDescriptor::CSS_SELECTOR
: {
143 WebExceptionCode ec
= 0;
144 element
= document
.querySelector(web_descriptor
, ec
);
146 DVLOG(1) << "Query selector failed. Error code: " << ec
<< ".";
149 case WebElementDescriptor::ID
:
150 element
= document
.getElementById(web_descriptor
);
152 case WebElementDescriptor::NONE
:
156 if (element
.isNull()) {
157 DVLOG(1) << "Could not find "
158 << element_descriptor
.descriptor
160 << RetrievalMethodToString(element_descriptor
.retrieval_method
)
165 element
.simulateClick();
171 class FormAutofillTest
: public ChromeRenderViewTest
{
173 FormAutofillTest() : ChromeRenderViewTest() {}
174 ~FormAutofillTest() override
{}
176 void ExpectLabels(const char* html
,
177 const std::vector
<base::string16
>& labels
,
178 const std::vector
<base::string16
>& names
,
179 const std::vector
<base::string16
>& values
) {
180 std::vector
<std::string
> control_types(labels
.size(), "text");
181 ExpectLabelsAndTypes(html
, labels
, names
, values
, control_types
);
184 void ExpectLabelsAndTypes(const char* html
,
185 const std::vector
<base::string16
>& labels
,
186 const std::vector
<base::string16
>& names
,
187 const std::vector
<base::string16
>& values
,
188 const std::vector
<std::string
>& control_types
) {
189 ASSERT_EQ(labels
.size(), names
.size());
190 ASSERT_EQ(labels
.size(), values
.size());
191 ASSERT_EQ(labels
.size(), control_types
.size());
195 WebFrame
* web_frame
= GetMainFrame();
196 ASSERT_NE(nullptr, web_frame
);
198 FormCache
form_cache(*web_frame
);
199 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
200 ASSERT_EQ(1U, forms
.size());
202 const FormData
& form
= forms
[0];
203 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
204 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
205 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
207 const std::vector
<FormFieldData
>& fields
= form
.fields
;
208 ASSERT_EQ(labels
.size(), fields
.size());
209 for (size_t i
= 0; i
< labels
.size(); ++i
) {
210 int max_length
= control_types
[i
] == "text" ?
211 WebInputElement::defaultMaxLength() : 0;
212 FormFieldData expected
;
213 expected
.label
= labels
[i
];
214 expected
.name
= names
[i
];
215 expected
.value
= values
[i
];
216 expected
.form_control_type
= control_types
[i
];
217 expected
.max_length
= max_length
;
218 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS
, i
));
219 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[i
]);
223 void ExpectJohnSmithLabels(const char* html
) {
224 std::vector
<base::string16
> labels
, names
, values
;
226 labels
.push_back(ASCIIToUTF16("First name:"));
227 names
.push_back(ASCIIToUTF16("firstname"));
228 values
.push_back(ASCIIToUTF16("John"));
230 labels
.push_back(ASCIIToUTF16("Last name:"));
231 names
.push_back(ASCIIToUTF16("lastname"));
232 values
.push_back(ASCIIToUTF16("Smith"));
234 labels
.push_back(ASCIIToUTF16("Email:"));
235 names
.push_back(ASCIIToUTF16("email"));
236 values
.push_back(ASCIIToUTF16("john@example.com"));
238 ExpectLabels(html
, labels
, names
, values
);
241 typedef void (*FillFormFunction
)(const FormData
& form
,
242 const WebFormControlElement
& element
);
244 typedef WebString (*GetValueFunction
)(WebFormControlElement element
);
246 // Test FormFillxxx functions.
247 void TestFormFillFunctions(const char* html
,
249 const AutofillFieldCase
* field_cases
,
250 size_t number_of_field_cases
,
251 FillFormFunction fill_form_function
,
252 GetValueFunction get_value_function
) {
255 WebFrame
* web_frame
= GetMainFrame();
256 ASSERT_NE(nullptr, web_frame
);
258 FormCache
form_cache(*web_frame
);
259 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
260 ASSERT_EQ(1U, forms
.size());
262 // Get the input element we want to find.
263 WebInputElement input_element
= GetInputElementById("firstname");
265 // Find the form that contains the input element.
268 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
269 input_element
, &form_data
, &field
, REQUIRE_NONE
));
271 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data
.name
);
272 EXPECT_EQ(GURL("http://buh.com"), form_data
.action
);
275 const std::vector
<FormFieldData
>& fields
= form_data
.fields
;
276 ASSERT_EQ(number_of_field_cases
, fields
.size());
278 FormFieldData expected
;
279 // Verify field's initial value.
280 for (size_t i
= 0; i
< number_of_field_cases
; ++i
) {
281 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
282 field_cases
[i
].name
));
283 expected
.form_control_type
= field_cases
[i
].form_control_type
;
284 expected
.max_length
=
285 expected
.form_control_type
== "text" ?
286 WebInputElement::defaultMaxLength() : 0;
287 expected
.name
= ASCIIToUTF16(field_cases
[i
].name
);
288 expected
.value
= ASCIIToUTF16(field_cases
[i
].initial_value
);
289 expected
.autocomplete_attribute
= field_cases
[i
].autocomplete_attribute
;
290 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[i
]);
291 // Fill the form_data for the field.
292 form_data
.fields
[i
].value
= ASCIIToUTF16(field_cases
[i
].autofill_value
);
293 // Set the is_autofilled property for the field.
294 form_data
.fields
[i
].is_autofilled
= field_cases
[i
].should_be_autofilled
;
297 // Autofill the form using the given fill form function.
298 fill_form_function(form_data
, input_element
);
300 // Validate Autofill or Preview results.
301 for (size_t i
= 0; i
< number_of_field_cases
; ++i
) {
302 ValidateFilledField(field_cases
[i
], get_value_function
);
306 // Validate an Autofilled field.
307 void ValidateFilledField(const AutofillFieldCase
& field_case
,
308 GetValueFunction get_value_function
) {
309 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
312 WebFormControlElement element
=
313 GetFormControlElementById(ASCIIToUTF16(field_case
.name
));
314 if ((element
.formControlType() == "select-one") ||
315 (element
.formControlType() == "textarea")) {
316 value
= get_value_function(element
);
318 ASSERT_TRUE(element
.formControlType() == "text" ||
319 element
.formControlType() == "month");
320 value
= get_value_function(element
);
323 const WebString expected_value
= ASCIIToUTF16(field_case
.expected_value
);
324 if (expected_value
.isEmpty())
325 EXPECT_TRUE(value
.isEmpty());
327 EXPECT_EQ(expected_value
.utf8(), value
.utf8());
329 EXPECT_EQ(field_case
.should_be_autofilled
, element
.isAutofilled());
332 WebFormControlElement
GetFormControlElementById(const WebString
& id
) {
333 return GetMainFrame()->document().getElementById(
334 id
).to
<WebFormControlElement
>();
337 WebInputElement
GetInputElementById(const WebString
& id
) {
338 return GetMainFrame()->document().getElementById(
339 id
).to
<WebInputElement
>();
342 void TestFillForm(const char* html
, bool unowned
) {
343 static const AutofillFieldCase field_cases
[] = {
344 // fields: form_control_type, name, initial_value, autocomplete_attribute,
345 // should_be_autofilled, autofill_value, expected_value
347 // Regular empty fields (firstname & lastname) should be autofilled.
355 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
356 // hidden fields should not be extracted to form_data.
357 // Non empty fields should not be autofilled.
358 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
364 "filled noautocomplete",
365 "filled noautocomplete"},
366 // Disabled fields should not be autofilled.
367 {"text", "notenabled", "", "", false, "filled notenabled", ""},
368 // Readonly fields should not be autofilled.
369 {"text", "readonly", "", "", false, "filled readonly", ""},
370 // Fields with "visibility: hidden" should not be autofilled.
371 {"text", "invisible", "", "", false, "filled invisible", ""},
372 // Fields with "display:none" should not be autofilled.
373 {"text", "displaynone", "", "", false, "filled displaynone", ""},
374 // Regular <input type="month"> should be autofilled.
375 {"month", "month", "", "", true, "2017-11", "2017-11"},
376 // Non-empty <input type="month"> should not be autofilled.
377 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
378 // Regular select fields should be autofilled.
379 {"select-one", "select", "", "", true, "TX", "TX"},
380 // Select fields should be autofilled even if they already have a
382 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
383 // Select fields should not be autofilled if no new value is passed from
384 // autofill profile. The existing value should not be overriden.
385 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
386 // Regular textarea elements should be autofilled.
392 "some multi-\nline value",
393 "some multi-\nline value"},
394 // Non-empty textarea elements should not be autofilled.
400 "some multi-\nline value",
403 TestFormFillFunctions(html
, unowned
, field_cases
, arraysize(field_cases
),
404 FillForm
, &GetValueWrapper
);
405 // Verify preview selection.
406 WebInputElement firstname
= GetInputElementById("firstname");
407 EXPECT_EQ(16, firstname
.selectionStart());
408 EXPECT_EQ(16, firstname
.selectionEnd());
411 void TestPreviewForm(const char* html
, bool unowned
) {
412 static const AutofillFieldCase field_cases
[] = {
413 // Normal empty fields should be previewed.
419 "suggested firstname",
420 "suggested firstname"},
426 "suggested lastname",
427 "suggested lastname"},
428 // Hidden fields should not be extracted to form_data.
429 // Non empty fields should not be previewed.
430 {"text", "notempty", "Hi", "", false, "suggested notempty", ""},
436 "filled noautocomplete",
437 "filled noautocomplete"},
438 // Disabled fields should not be previewed.
439 {"text", "notenabled", "", "", false, "suggested notenabled", ""},
440 // Readonly fields should not be previewed.
441 {"text", "readonly", "", "", false, "suggested readonly", ""},
442 // Fields with "visibility: hidden" should not be previewed.
443 {"text", "invisible", "", "", false, "suggested invisible", ""},
444 // Fields with "display:none" should not previewed.
445 {"text", "displaynone", "", "", false, "suggested displaynone", ""},
446 // Regular <input type="month"> should be previewed.
447 {"month", "month", "", "", true, "2017-11", "2017-11"},
448 // Non-empty <input type="month"> should not be previewed.
449 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
450 // Regular select fields should be previewed.
451 {"select-one", "select", "", "", true, "TX", "TX"},
452 // Select fields should be previewed even if they already have a
454 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
455 // Select fields should not be previewed if no suggestion is passed from
457 {"select-one", "select-unchanged", "CA", "", false, "", ""},
458 // Normal textarea elements should be previewed.
464 "suggested multi-\nline value",
465 "suggested multi-\nline value"},
466 // Nonempty textarea elements should not be previewed.
472 "suggested multi-\nline value",
475 TestFormFillFunctions(html
, unowned
, field_cases
, arraysize(field_cases
),
476 &PreviewForm
, &GetSuggestedValueWrapper
);
478 // Verify preview selection.
479 WebInputElement firstname
= GetInputElementById("firstname");
480 EXPECT_EQ(0, firstname
.selectionStart());
481 EXPECT_EQ(19, firstname
.selectionEnd());
484 void TestFindFormForInputElement(const char* html
, bool unowned
) {
486 WebFrame
* web_frame
= GetMainFrame();
487 ASSERT_NE(nullptr, web_frame
);
489 FormCache
form_cache(*web_frame
);
490 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
491 ASSERT_EQ(1U, forms
.size());
493 // Get the input element we want to find.
494 WebInputElement input_element
= GetInputElementById("firstname");
496 // Find the form and verify it's the correct form.
499 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
500 input_element
, &form
, &field
, REQUIRE_NONE
));
501 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
503 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
504 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
507 const std::vector
<FormFieldData
>& fields
= form
.fields
;
508 ASSERT_EQ(4U, fields
.size());
510 FormFieldData expected
;
511 expected
.form_control_type
= "text";
512 expected
.max_length
= WebInputElement::defaultMaxLength();
514 expected
.name
= ASCIIToUTF16("firstname");
515 expected
.value
= ASCIIToUTF16("John");
516 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
517 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
519 expected
.name
= ASCIIToUTF16("lastname");
520 expected
.value
= ASCIIToUTF16("Smith");
521 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
523 expected
.name
= ASCIIToUTF16("email");
524 expected
.value
= ASCIIToUTF16("john@example.com");
525 expected
.autocomplete_attribute
= "off";
526 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
527 expected
.autocomplete_attribute
.clear();
529 expected
.name
= ASCIIToUTF16("phone");
530 expected
.value
= ASCIIToUTF16("1.800.555.1234");
531 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
533 // Try again, but require autocomplete.
535 FormFieldData field2
;
536 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
537 input_element
, &form2
, &field2
, REQUIRE_AUTOCOMPLETE
));
538 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
540 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
541 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
544 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
545 ASSERT_EQ(3U, fields2
.size());
547 expected
.form_control_type
= "text";
548 expected
.max_length
= WebInputElement::defaultMaxLength();
550 expected
.name
= ASCIIToUTF16("firstname");
551 expected
.value
= ASCIIToUTF16("John");
552 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
553 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
555 expected
.name
= ASCIIToUTF16("lastname");
556 expected
.value
= ASCIIToUTF16("Smith");
557 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
559 expected
.name
= ASCIIToUTF16("phone");
560 expected
.value
= ASCIIToUTF16("1.800.555.1234");
561 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
564 void TestFindFormForTextAreaElement(const char* html
, bool unowned
) {
566 WebFrame
* web_frame
= GetMainFrame();
567 ASSERT_NE(nullptr, web_frame
);
569 FormCache
form_cache(*web_frame
);
570 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
571 ASSERT_EQ(1U, forms
.size());
573 // Get the textarea element we want to find.
574 WebElement element
= web_frame
->document().getElementById("street-address");
575 WebTextAreaElement textarea_element
= element
.to
<WebTextAreaElement
>();
577 // Find the form and verify it's the correct form.
580 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
581 textarea_element
, &form
, &field
, REQUIRE_NONE
));
582 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
584 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
585 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
588 const std::vector
<FormFieldData
>& fields
= form
.fields
;
589 ASSERT_EQ(4U, fields
.size());
591 FormFieldData expected
;
593 expected
.name
= ASCIIToUTF16("firstname");
594 expected
.value
= ASCIIToUTF16("John");
595 expected
.form_control_type
= "text";
596 expected
.max_length
= WebInputElement::defaultMaxLength();
597 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
599 expected
.name
= ASCIIToUTF16("lastname");
600 expected
.value
= ASCIIToUTF16("Smith");
601 expected
.form_control_type
= "text";
602 expected
.max_length
= WebInputElement::defaultMaxLength();
603 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
605 expected
.name
= ASCIIToUTF16("email");
606 expected
.value
= ASCIIToUTF16("john@example.com");
607 expected
.autocomplete_attribute
= "off";
608 expected
.form_control_type
= "text";
609 expected
.max_length
= WebInputElement::defaultMaxLength();
610 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
611 expected
.autocomplete_attribute
.clear();
613 expected
.name
= ASCIIToUTF16("street-address");
614 expected
.value
= ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
615 expected
.form_control_type
= "textarea";
616 expected
.max_length
= 0;
617 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
618 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
620 // Try again, but require autocomplete.
622 FormFieldData field2
;
623 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
624 textarea_element
, &form2
, &field2
, REQUIRE_AUTOCOMPLETE
));
625 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
627 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
628 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
631 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
632 ASSERT_EQ(3U, fields2
.size());
634 expected
.name
= ASCIIToUTF16("firstname");
635 expected
.value
= ASCIIToUTF16("John");
636 expected
.form_control_type
= "text";
637 expected
.max_length
= WebInputElement::defaultMaxLength();
638 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
640 expected
.name
= ASCIIToUTF16("lastname");
641 expected
.value
= ASCIIToUTF16("Smith");
642 expected
.form_control_type
= "text";
643 expected
.max_length
= WebInputElement::defaultMaxLength();
644 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
646 expected
.name
= ASCIIToUTF16("street-address");
647 expected
.value
= ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
648 expected
.form_control_type
= "textarea";
649 expected
.max_length
= 0;
650 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
651 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
654 void TestFillFormMaxLength(const char* html
, bool unowned
) {
656 WebFrame
* web_frame
= GetMainFrame();
657 ASSERT_NE(nullptr, web_frame
);
659 FormCache
form_cache(*web_frame
);
660 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
661 ASSERT_EQ(1U, forms
.size());
663 // Get the input element we want to find.
664 WebInputElement input_element
= GetInputElementById("firstname");
666 // Find the form that contains the input element.
669 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
670 input_element
, &form
, &field
, REQUIRE_NONE
));
671 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
673 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
674 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
677 const std::vector
<FormFieldData
>& fields
= form
.fields
;
678 ASSERT_EQ(3U, fields
.size());
680 FormFieldData expected
;
681 expected
.form_control_type
= "text";
683 expected
.name
= ASCIIToUTF16("firstname");
684 expected
.max_length
= 5;
685 expected
.is_autofilled
= false;
686 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
688 expected
.name
= ASCIIToUTF16("lastname");
689 expected
.max_length
= 7;
690 expected
.is_autofilled
= false;
691 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
693 expected
.name
= ASCIIToUTF16("email");
694 expected
.max_length
= 9;
695 expected
.is_autofilled
= false;
696 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
699 form
.fields
[0].value
= ASCIIToUTF16("Brother");
700 form
.fields
[1].value
= ASCIIToUTF16("Jonathan");
701 form
.fields
[2].value
= ASCIIToUTF16("brotherj@example.com");
702 form
.fields
[0].is_autofilled
= true;
703 form
.fields
[1].is_autofilled
= true;
704 form
.fields
[2].is_autofilled
= true;
705 FillForm(form
, input_element
);
707 // Find the newly-filled form that contains the input element.
709 FormFieldData field2
;
710 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
711 input_element
, &form2
, &field2
, REQUIRE_NONE
));
712 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
714 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
715 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
718 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
719 ASSERT_EQ(3U, fields2
.size());
721 expected
.form_control_type
= "text";
723 expected
.name
= ASCIIToUTF16("firstname");
724 expected
.value
= ASCIIToUTF16("Broth");
725 expected
.max_length
= 5;
726 expected
.is_autofilled
= true;
727 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
729 expected
.name
= ASCIIToUTF16("lastname");
730 expected
.value
= ASCIIToUTF16("Jonatha");
731 expected
.max_length
= 7;
732 expected
.is_autofilled
= true;
733 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
735 expected
.name
= ASCIIToUTF16("email");
736 expected
.value
= ASCIIToUTF16("brotherj@");
737 expected
.max_length
= 9;
738 expected
.is_autofilled
= true;
739 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
742 void TestFillFormNegativeMaxLength(const char* html
, bool unowned
) {
744 WebFrame
* web_frame
= GetMainFrame();
745 ASSERT_NE(nullptr, web_frame
);
747 FormCache
form_cache(*web_frame
);
748 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
749 ASSERT_EQ(1U, forms
.size());
751 // Get the input element we want to find.
752 WebInputElement input_element
= GetInputElementById("firstname");
754 // Find the form that contains the input element.
757 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
758 input_element
, &form
, &field
, REQUIRE_NONE
));
759 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
761 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
762 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
765 const std::vector
<FormFieldData
>& fields
= form
.fields
;
766 ASSERT_EQ(3U, fields
.size());
768 FormFieldData expected
;
769 expected
.form_control_type
= "text";
770 expected
.max_length
= WebInputElement::defaultMaxLength();
772 expected
.name
= ASCIIToUTF16("firstname");
773 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
775 expected
.name
= ASCIIToUTF16("lastname");
776 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
778 expected
.name
= ASCIIToUTF16("email");
779 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
782 form
.fields
[0].value
= ASCIIToUTF16("Brother");
783 form
.fields
[1].value
= ASCIIToUTF16("Jonathan");
784 form
.fields
[2].value
= ASCIIToUTF16("brotherj@example.com");
785 FillForm(form
, input_element
);
787 // Find the newly-filled form that contains the input element.
789 FormFieldData field2
;
790 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
791 input_element
, &form2
, &field2
, REQUIRE_NONE
));
792 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
794 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
795 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
798 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
799 ASSERT_EQ(3U, fields2
.size());
801 expected
.name
= ASCIIToUTF16("firstname");
802 expected
.value
= ASCIIToUTF16("Brother");
803 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
805 expected
.name
= ASCIIToUTF16("lastname");
806 expected
.value
= ASCIIToUTF16("Jonathan");
807 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
809 expected
.name
= ASCIIToUTF16("email");
810 expected
.value
= ASCIIToUTF16("brotherj@example.com");
811 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
814 void TestFillFormEmptyName(const char* html
, bool unowned
) {
816 WebFrame
* web_frame
= GetMainFrame();
817 ASSERT_NE(nullptr, web_frame
);
819 FormCache
form_cache(*web_frame
);
820 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
821 ASSERT_EQ(1U, forms
.size());
823 // Get the input element we want to find.
824 WebInputElement input_element
= GetInputElementById("firstname");
826 // Find the form that contains the input element.
829 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
830 input_element
, &form
, &field
, REQUIRE_NONE
));
831 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
833 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
834 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
837 const std::vector
<FormFieldData
>& fields
= form
.fields
;
838 ASSERT_EQ(3U, fields
.size());
840 FormFieldData expected
;
841 expected
.form_control_type
= "text";
842 expected
.max_length
= WebInputElement::defaultMaxLength();
844 expected
.name
= ASCIIToUTF16("firstname");
845 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
847 expected
.name
= ASCIIToUTF16("lastname");
848 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
850 expected
.name
= ASCIIToUTF16("email");
851 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
854 form
.fields
[0].value
= ASCIIToUTF16("Wyatt");
855 form
.fields
[1].value
= ASCIIToUTF16("Earp");
856 form
.fields
[2].value
= ASCIIToUTF16("wyatt@example.com");
857 FillForm(form
, input_element
);
859 // Find the newly-filled form that contains the input element.
861 FormFieldData field2
;
862 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
863 input_element
, &form2
, &field2
, REQUIRE_NONE
));
864 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
866 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
867 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
870 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
871 ASSERT_EQ(3U, fields2
.size());
873 expected
.form_control_type
= "text";
874 expected
.max_length
= WebInputElement::defaultMaxLength();
876 expected
.name
= ASCIIToUTF16("firstname");
877 expected
.value
= ASCIIToUTF16("Wyatt");
878 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
880 expected
.name
= ASCIIToUTF16("lastname");
881 expected
.value
= ASCIIToUTF16("Earp");
882 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
884 expected
.name
= ASCIIToUTF16("email");
885 expected
.value
= ASCIIToUTF16("wyatt@example.com");
886 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
889 void TestFillFormEmptyFormNames(const char* html
, bool unowned
) {
891 WebFrame
* web_frame
= GetMainFrame();
892 ASSERT_NE(nullptr, web_frame
);
894 FormCache
form_cache(*web_frame
);
895 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
896 const size_t expected_size
= unowned
? 1 : 2;
897 ASSERT_EQ(expected_size
, forms
.size());
899 // Get the input element we want to find.
900 WebInputElement input_element
= GetInputElementById("apple");
902 // Find the form that contains the input element.
905 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
906 input_element
, &form
, &field
, REQUIRE_NONE
));
907 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
909 EXPECT_TRUE(form
.name
.empty());
910 EXPECT_EQ(GURL("http://abc.com"), form
.action
);
913 const std::vector
<FormFieldData
>& fields
= form
.fields
;
914 const size_t unowned_offset
= unowned
? 3 : 0;
915 ASSERT_EQ(unowned_offset
+ 3, fields
.size());
917 FormFieldData expected
;
918 expected
.form_control_type
= "text";
919 expected
.max_length
= WebInputElement::defaultMaxLength();
921 expected
.name
= ASCIIToUTF16("apple");
922 expected
.is_autofilled
= false;
923 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[unowned_offset
]);
925 expected
.name
= ASCIIToUTF16("banana");
926 expected
.is_autofilled
= false;
927 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[unowned_offset
+ 1]);
929 expected
.name
= ASCIIToUTF16("cantelope");
930 expected
.is_autofilled
= false;
931 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[unowned_offset
+ 2]);
934 form
.fields
[unowned_offset
+ 0].value
= ASCIIToUTF16("Red");
935 form
.fields
[unowned_offset
+ 1].value
= ASCIIToUTF16("Yellow");
936 form
.fields
[unowned_offset
+ 2].value
= ASCIIToUTF16("Also Yellow");
937 form
.fields
[unowned_offset
+ 0].is_autofilled
= true;
938 form
.fields
[unowned_offset
+ 1].is_autofilled
= true;
939 form
.fields
[unowned_offset
+ 2].is_autofilled
= true;
940 FillForm(form
, input_element
);
942 // Find the newly-filled form that contains the input element.
944 FormFieldData field2
;
945 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
946 input_element
, &form2
, &field2
, REQUIRE_NONE
));
947 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
949 EXPECT_TRUE(form2
.name
.empty());
950 EXPECT_EQ(GURL("http://abc.com"), form2
.action
);
953 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
954 ASSERT_EQ(unowned_offset
+ 3, fields2
.size());
956 expected
.name
= ASCIIToUTF16("apple");
957 expected
.value
= ASCIIToUTF16("Red");
958 expected
.is_autofilled
= true;
959 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[unowned_offset
+ 0]);
961 expected
.name
= ASCIIToUTF16("banana");
962 expected
.value
= ASCIIToUTF16("Yellow");
963 expected
.is_autofilled
= true;
964 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[unowned_offset
+ 1]);
966 expected
.name
= ASCIIToUTF16("cantelope");
967 expected
.value
= ASCIIToUTF16("Also Yellow");
968 expected
.is_autofilled
= true;
969 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[unowned_offset
+ 2]);
972 void TestFillFormNonEmptyField(const char* html
, bool unowned
) {
974 WebFrame
* web_frame
= GetMainFrame();
975 ASSERT_NE(nullptr, web_frame
);
977 FormCache
form_cache(*web_frame
);
978 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
979 ASSERT_EQ(1U, forms
.size());
981 // Get the input element we want to find.
982 WebInputElement input_element
= GetInputElementById("firstname");
984 // Simulate typing by modifying the field value.
985 input_element
.setValue(ASCIIToUTF16("Wy"));
987 // Find the form that contains the input element.
990 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
991 input_element
, &form
, &field
, REQUIRE_NONE
));
992 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
994 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
995 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
998 const std::vector
<FormFieldData
>& fields
= form
.fields
;
999 ASSERT_EQ(3U, fields
.size());
1001 FormFieldData expected
;
1002 expected
.form_control_type
= "text";
1003 expected
.max_length
= WebInputElement::defaultMaxLength();
1005 expected
.name
= ASCIIToUTF16("firstname");
1006 expected
.value
= ASCIIToUTF16("Wy");
1007 expected
.is_autofilled
= false;
1008 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
1010 expected
.name
= ASCIIToUTF16("lastname");
1011 expected
.value
.clear();
1012 expected
.is_autofilled
= false;
1013 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
1015 expected
.name
= ASCIIToUTF16("email");
1016 expected
.value
.clear();
1017 expected
.is_autofilled
= false;
1018 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
1020 // Preview the form and verify that the cursor position has been updated.
1021 form
.fields
[0].value
= ASCIIToUTF16("Wyatt");
1022 form
.fields
[1].value
= ASCIIToUTF16("Earp");
1023 form
.fields
[2].value
= ASCIIToUTF16("wyatt@example.com");
1024 form
.fields
[0].is_autofilled
= true;
1025 form
.fields
[1].is_autofilled
= true;
1026 form
.fields
[2].is_autofilled
= true;
1027 PreviewForm(form
, input_element
);
1028 EXPECT_EQ(2, input_element
.selectionStart());
1029 EXPECT_EQ(5, input_element
.selectionEnd());
1032 FillForm(form
, input_element
);
1034 // Find the newly-filled form that contains the input element.
1036 FormFieldData field2
;
1037 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1038 input_element
, &form2
, &field2
, REQUIRE_NONE
));
1039 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
1041 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
1042 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
1045 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
1046 ASSERT_EQ(3U, fields2
.size());
1048 expected
.name
= ASCIIToUTF16("firstname");
1049 expected
.value
= ASCIIToUTF16("Wyatt");
1050 expected
.is_autofilled
= true;
1051 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
1053 expected
.name
= ASCIIToUTF16("lastname");
1054 expected
.value
= ASCIIToUTF16("Earp");
1055 expected
.is_autofilled
= true;
1056 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
1058 expected
.name
= ASCIIToUTF16("email");
1059 expected
.value
= ASCIIToUTF16("wyatt@example.com");
1060 expected
.is_autofilled
= true;
1061 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
1063 // Verify that the cursor position has been updated.
1064 EXPECT_EQ(5, input_element
.selectionStart());
1065 EXPECT_EQ(5, input_element
.selectionEnd());
1068 void TestClearFormWithNode(const char* html
, bool unowned
) {
1070 WebFrame
* web_frame
= GetMainFrame();
1071 ASSERT_NE(nullptr, web_frame
);
1073 FormCache
form_cache(*web_frame
);
1074 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
1075 ASSERT_EQ(1U, forms
.size());
1077 // Set the auto-filled attribute.
1078 WebInputElement firstname
= GetInputElementById("firstname");
1079 firstname
.setAutofilled(true);
1080 WebInputElement lastname
= GetInputElementById("lastname");
1081 lastname
.setAutofilled(true);
1082 WebInputElement month
= GetInputElementById("month");
1083 month
.setAutofilled(true);
1084 WebInputElement textarea
= GetInputElementById("textarea");
1085 textarea
.setAutofilled(true);
1087 // Set the value of the disabled text input element.
1088 WebInputElement notenabled
= GetInputElementById("notenabled");
1089 notenabled
.setValue(WebString::fromUTF8("no clear"));
1092 EXPECT_TRUE(form_cache
.ClearFormWithElement(firstname
));
1094 // Verify that the auto-filled attribute has been turned off.
1095 EXPECT_FALSE(firstname
.isAutofilled());
1097 // Verify the form is cleared.
1099 FormFieldData field
;
1100 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1101 firstname
, &form
, &field
, REQUIRE_NONE
));
1102 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
1104 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
1105 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
1108 const std::vector
<FormFieldData
>& fields
= form
.fields
;
1109 ASSERT_EQ(9U, fields
.size());
1111 FormFieldData expected
;
1112 expected
.form_control_type
= "text";
1113 expected
.max_length
= WebInputElement::defaultMaxLength();
1115 expected
.name
= ASCIIToUTF16("firstname");
1116 expected
.value
.clear();
1117 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
1119 expected
.name
= ASCIIToUTF16("lastname");
1120 expected
.value
.clear();
1121 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
1123 expected
.name
= ASCIIToUTF16("noAC");
1124 expected
.value
= ASCIIToUTF16("one");
1125 expected
.autocomplete_attribute
= "off";
1126 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
1127 expected
.autocomplete_attribute
.clear();
1129 expected
.name
= ASCIIToUTF16("notenabled");
1130 expected
.value
= ASCIIToUTF16("no clear");
1131 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
1133 expected
.form_control_type
= "month";
1134 expected
.max_length
= 0;
1135 expected
.name
= ASCIIToUTF16("month");
1136 expected
.value
.clear();
1137 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[4]);
1139 expected
.name
= ASCIIToUTF16("month-disabled");
1140 expected
.value
= ASCIIToUTF16("2012-11");
1141 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[5]);
1143 expected
.form_control_type
= "textarea";
1144 expected
.name
= ASCIIToUTF16("textarea");
1145 expected
.value
.clear();
1146 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[6]);
1148 expected
.name
= ASCIIToUTF16("textarea-disabled");
1149 expected
.value
= ASCIIToUTF16(" Banana! ");
1150 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[7]);
1152 expected
.name
= ASCIIToUTF16("textarea-noAC");
1153 expected
.value
= ASCIIToUTF16("Carrot?");
1154 expected
.autocomplete_attribute
= "off";
1155 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[8]);
1156 expected
.autocomplete_attribute
.clear();
1158 // Verify that the cursor position has been updated.
1159 EXPECT_EQ(0, firstname
.selectionStart());
1160 EXPECT_EQ(0, firstname
.selectionEnd());
1163 void TestClearFormWithNodeContainingSelectOne(const char* html
,
1166 WebFrame
* web_frame
= GetMainFrame();
1167 ASSERT_NE(nullptr, web_frame
);
1169 FormCache
form_cache(*web_frame
);
1170 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
1171 ASSERT_EQ(1U, forms
.size());
1173 // Set the auto-filled attribute.
1174 WebInputElement firstname
= GetInputElementById("firstname");
1175 firstname
.setAutofilled(true);
1176 WebInputElement lastname
= GetInputElementById("lastname");
1177 lastname
.setAutofilled(true);
1179 // Set the value and auto-filled attribute of the state element.
1180 WebSelectElement state
=
1181 web_frame
->document().getElementById("state").to
<WebSelectElement
>();
1182 state
.setValue(WebString::fromUTF8("AK"));
1183 state
.setAutofilled(true);
1186 EXPECT_TRUE(form_cache
.ClearFormWithElement(firstname
));
1188 // Verify that the auto-filled attribute has been turned off.
1189 EXPECT_FALSE(firstname
.isAutofilled());
1191 // Verify the form is cleared.
1193 FormFieldData field
;
1194 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1195 firstname
, &form
, &field
, REQUIRE_NONE
));
1196 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
1198 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
1199 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
1202 const std::vector
<FormFieldData
>& fields
= form
.fields
;
1203 ASSERT_EQ(3U, fields
.size());
1205 FormFieldData expected
;
1207 expected
.name
= ASCIIToUTF16("firstname");
1208 expected
.value
.clear();
1209 expected
.form_control_type
= "text";
1210 expected
.max_length
= WebInputElement::defaultMaxLength();
1211 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
1213 expected
.name
= ASCIIToUTF16("lastname");
1214 expected
.value
.clear();
1215 expected
.form_control_type
= "text";
1216 expected
.max_length
= WebInputElement::defaultMaxLength();
1217 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
1219 expected
.name
= ASCIIToUTF16("state");
1220 expected
.value
= ASCIIToUTF16("?");
1221 expected
.form_control_type
= "select-one";
1222 expected
.max_length
= 0;
1223 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
1225 // Verify that the cursor position has been updated.
1226 EXPECT_EQ(0, firstname
.selectionStart());
1227 EXPECT_EQ(0, firstname
.selectionEnd());
1230 void TestClearPreviewedFormWithElement(const char* html
) {
1232 WebFrame
* web_frame
= GetMainFrame();
1233 ASSERT_NE(nullptr, web_frame
);
1235 FormCache
form_cache(*web_frame
);
1236 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
1237 ASSERT_EQ(1U, forms
.size());
1239 // Set the auto-filled attribute.
1240 WebInputElement firstname
= GetInputElementById("firstname");
1241 firstname
.setAutofilled(true);
1242 WebInputElement lastname
= GetInputElementById("lastname");
1243 lastname
.setAutofilled(true);
1244 WebInputElement email
= GetInputElementById("email");
1245 email
.setAutofilled(true);
1246 WebInputElement email2
= GetInputElementById("email2");
1247 email2
.setAutofilled(true);
1248 WebInputElement phone
= GetInputElementById("phone");
1249 phone
.setAutofilled(true);
1251 // Set the suggested values on two of the elements.
1252 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
1253 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1254 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1255 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1257 // Clear the previewed fields.
1258 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname
, false));
1260 // Fields with empty suggestions suggestions are not modified.
1261 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname
.value());
1262 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
1263 EXPECT_TRUE(firstname
.isAutofilled());
1265 // Verify the previewed fields are cleared.
1266 EXPECT_TRUE(lastname
.value().isEmpty());
1267 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
1268 EXPECT_FALSE(lastname
.isAutofilled());
1269 EXPECT_TRUE(email
.value().isEmpty());
1270 EXPECT_TRUE(email
.suggestedValue().isEmpty());
1271 EXPECT_FALSE(email
.isAutofilled());
1272 EXPECT_TRUE(email2
.value().isEmpty());
1273 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
1274 EXPECT_FALSE(email2
.isAutofilled());
1275 EXPECT_TRUE(phone
.value().isEmpty());
1276 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
1277 EXPECT_FALSE(phone
.isAutofilled());
1279 // Verify that the cursor position has been updated.
1280 EXPECT_EQ(0, lastname
.selectionStart());
1281 EXPECT_EQ(0, lastname
.selectionEnd());
1284 void TestClearPreviewedFormWithNonEmptyInitiatingNode(const char* html
) {
1286 WebFrame
* web_frame
= GetMainFrame();
1287 ASSERT_NE(nullptr, web_frame
);
1289 FormCache
form_cache(*web_frame
);
1290 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
1291 ASSERT_EQ(1U, forms
.size());
1293 // Set the auto-filled attribute.
1294 WebInputElement firstname
= GetInputElementById("firstname");
1295 firstname
.setAutofilled(true);
1296 WebInputElement lastname
= GetInputElementById("lastname");
1297 lastname
.setAutofilled(true);
1298 WebInputElement email
= GetInputElementById("email");
1299 email
.setAutofilled(true);
1300 WebInputElement email2
= GetInputElementById("email2");
1301 email2
.setAutofilled(true);
1302 WebInputElement phone
= GetInputElementById("phone");
1303 phone
.setAutofilled(true);
1306 // Set the suggested values on all of the elements.
1307 firstname
.setSuggestedValue(ASCIIToUTF16("Wyatt"));
1308 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
1309 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1310 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1311 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1313 // Clear the previewed fields.
1314 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname
, false));
1316 // Fields with non-empty values are restored.
1317 EXPECT_EQ(ASCIIToUTF16("W"), firstname
.value());
1318 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
1319 EXPECT_FALSE(firstname
.isAutofilled());
1320 EXPECT_EQ(1, firstname
.selectionStart());
1321 EXPECT_EQ(1, firstname
.selectionEnd());
1323 // Verify the previewed fields are cleared.
1324 EXPECT_TRUE(lastname
.value().isEmpty());
1325 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
1326 EXPECT_FALSE(lastname
.isAutofilled());
1327 EXPECT_TRUE(email
.value().isEmpty());
1328 EXPECT_TRUE(email
.suggestedValue().isEmpty());
1329 EXPECT_FALSE(email
.isAutofilled());
1330 EXPECT_TRUE(email2
.value().isEmpty());
1331 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
1332 EXPECT_FALSE(email2
.isAutofilled());
1333 EXPECT_TRUE(phone
.value().isEmpty());
1334 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
1335 EXPECT_FALSE(phone
.isAutofilled());
1338 void TestClearPreviewedFormWithAutofilledInitiatingNode(const char* html
) {
1340 WebFrame
* web_frame
= GetMainFrame();
1341 ASSERT_NE(nullptr, web_frame
);
1343 FormCache
form_cache(*web_frame
);
1344 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
1345 ASSERT_EQ(1U, forms
.size());
1347 // Set the auto-filled attribute.
1348 WebInputElement firstname
= GetInputElementById("firstname");
1349 firstname
.setAutofilled(true);
1350 WebInputElement lastname
= GetInputElementById("lastname");
1351 lastname
.setAutofilled(true);
1352 WebInputElement email
= GetInputElementById("email");
1353 email
.setAutofilled(true);
1354 WebInputElement email2
= GetInputElementById("email2");
1355 email2
.setAutofilled(true);
1356 WebInputElement phone
= GetInputElementById("phone");
1357 phone
.setAutofilled(true);
1359 // Set the suggested values on all of the elements.
1360 firstname
.setSuggestedValue(ASCIIToUTF16("Wyatt"));
1361 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
1362 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1363 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1364 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1366 // Clear the previewed fields.
1367 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname
, true));
1369 // Fields with non-empty values are restored.
1370 EXPECT_EQ(ASCIIToUTF16("W"), firstname
.value());
1371 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
1372 EXPECT_TRUE(firstname
.isAutofilled());
1373 EXPECT_EQ(1, firstname
.selectionStart());
1374 EXPECT_EQ(1, firstname
.selectionEnd());
1376 // Verify the previewed fields are cleared.
1377 EXPECT_TRUE(lastname
.value().isEmpty());
1378 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
1379 EXPECT_FALSE(lastname
.isAutofilled());
1380 EXPECT_TRUE(email
.value().isEmpty());
1381 EXPECT_TRUE(email
.suggestedValue().isEmpty());
1382 EXPECT_FALSE(email
.isAutofilled());
1383 EXPECT_TRUE(email2
.value().isEmpty());
1384 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
1385 EXPECT_FALSE(email2
.isAutofilled());
1386 EXPECT_TRUE(phone
.value().isEmpty());
1387 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
1388 EXPECT_FALSE(phone
.isAutofilled());
1391 void TestClearOnlyAutofilledFields(const char* html
) {
1394 WebFrame
* web_frame
= GetMainFrame();
1395 ASSERT_NE(nullptr, web_frame
);
1397 FormCache
form_cache(*web_frame
);
1398 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
1399 ASSERT_EQ(1U, forms
.size());
1401 // Set the autofilled attribute.
1402 WebInputElement firstname
= GetInputElementById("firstname");
1403 firstname
.setAutofilled(false);
1404 WebInputElement lastname
= GetInputElementById("lastname");
1405 lastname
.setAutofilled(true);
1406 WebInputElement email
= GetInputElementById("email");
1407 email
.setAutofilled(true);
1408 WebInputElement phone
= GetInputElementById("phone");
1409 phone
.setAutofilled(true);
1411 // Clear the fields.
1412 EXPECT_TRUE(form_cache
.ClearFormWithElement(firstname
));
1414 // Verify only autofilled fields are cleared.
1415 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname
.value());
1416 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
1417 EXPECT_FALSE(firstname
.isAutofilled());
1418 EXPECT_TRUE(lastname
.value().isEmpty());
1419 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
1420 EXPECT_FALSE(lastname
.isAutofilled());
1421 EXPECT_TRUE(email
.value().isEmpty());
1422 EXPECT_TRUE(email
.suggestedValue().isEmpty());
1423 EXPECT_FALSE(email
.isAutofilled());
1424 EXPECT_TRUE(phone
.value().isEmpty());
1425 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
1426 EXPECT_FALSE(phone
.isAutofilled());
1429 static void FillFormIncludingNonFocusableElementsWrapper(
1430 const FormData
& form
,
1431 const WebFormControlElement
& element
) {
1432 FillFormIncludingNonFocusableElements(form
, element
.form());
1435 static WebString
GetValueWrapper(WebFormControlElement element
) {
1436 if (element
.formControlType() == "textarea")
1437 return element
.to
<WebTextAreaElement
>().value();
1439 if (element
.formControlType() == "select-one")
1440 return element
.to
<WebSelectElement
>().value();
1442 return element
.to
<WebInputElement
>().value();
1445 static WebString
GetSuggestedValueWrapper(WebFormControlElement element
) {
1446 if (element
.formControlType() == "textarea")
1447 return element
.to
<WebTextAreaElement
>().suggestedValue();
1449 if (element
.formControlType() == "select-one")
1450 return element
.to
<WebSelectElement
>().suggestedValue();
1452 return element
.to
<WebInputElement
>().suggestedValue();
1456 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest
);
1459 // We should be able to extract a normal text field.
1460 TEST_F(FormAutofillTest
, WebFormControlElementToFormField
) {
1461 LoadHTML("<INPUT type='text' id='element' value='value'/>");
1463 WebFrame
* frame
= GetMainFrame();
1464 ASSERT_NE(nullptr, frame
);
1466 WebFormControlElement element
= GetFormControlElementById("element");
1467 FormFieldData result1
;
1468 WebFormControlElementToFormField(element
, EXTRACT_NONE
, &result1
);
1470 FormFieldData expected
;
1471 expected
.form_control_type
= "text";
1472 expected
.max_length
= WebInputElement::defaultMaxLength();
1474 expected
.name
= ASCIIToUTF16("element");
1475 expected
.value
.clear();
1476 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result1
);
1478 FormFieldData result2
;
1479 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result2
);
1481 expected
.name
= ASCIIToUTF16("element");
1482 expected
.value
= ASCIIToUTF16("value");
1483 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result2
);
1486 // We should be able to extract a text field with autocomplete="off".
1487 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutocompleteOff
) {
1488 LoadHTML("<INPUT type='text' id='element' value='value'"
1489 " autocomplete='off'/>");
1491 WebFrame
* frame
= GetMainFrame();
1492 ASSERT_NE(nullptr, frame
);
1494 WebFormControlElement element
= GetFormControlElementById("element");
1495 FormFieldData result
;
1496 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1498 FormFieldData expected
;
1499 expected
.name
= ASCIIToUTF16("element");
1500 expected
.value
= ASCIIToUTF16("value");
1501 expected
.form_control_type
= "text";
1502 expected
.autocomplete_attribute
= "off";
1503 expected
.max_length
= WebInputElement::defaultMaxLength();
1504 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1507 // We should be able to extract a text field with maxlength specified.
1508 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldMaxLength
) {
1509 LoadHTML("<INPUT type='text' id='element' value='value'"
1510 " maxlength='5'/>");
1512 WebFrame
* frame
= GetMainFrame();
1513 ASSERT_NE(nullptr, frame
);
1515 WebFormControlElement element
= GetFormControlElementById("element");
1516 FormFieldData result
;
1517 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1519 FormFieldData expected
;
1520 expected
.name
= ASCIIToUTF16("element");
1521 expected
.value
= ASCIIToUTF16("value");
1522 expected
.form_control_type
= "text";
1523 expected
.max_length
= 5;
1524 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1527 // We should be able to extract a text field that has been autofilled.
1528 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutofilled
) {
1529 LoadHTML("<INPUT type='text' id='element' value='value'/>");
1531 WebFrame
* frame
= GetMainFrame();
1532 ASSERT_NE(nullptr, frame
);
1534 WebInputElement element
= GetInputElementById("element");
1535 element
.setAutofilled(true);
1536 FormFieldData result
;
1537 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1539 FormFieldData expected
;
1540 expected
.name
= ASCIIToUTF16("element");
1541 expected
.value
= ASCIIToUTF16("value");
1542 expected
.form_control_type
= "text";
1543 expected
.max_length
= WebInputElement::defaultMaxLength();
1544 expected
.is_autofilled
= true;
1545 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1548 // We should be able to extract a radio or a checkbox field that has been
1550 TEST_F(FormAutofillTest
, WebFormControlElementToClickableFormField
) {
1551 LoadHTML("<INPUT type='checkbox' id='checkbox' value='mail' checked/>"
1552 "<INPUT type='radio' id='radio' value='male'/>");
1554 WebFrame
* frame
= GetMainFrame();
1555 ASSERT_NE(nullptr, frame
);
1557 WebInputElement element
= GetInputElementById("checkbox");
1558 element
.setAutofilled(true);
1559 FormFieldData result
;
1560 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1562 FormFieldData expected
;
1563 expected
.name
= ASCIIToUTF16("checkbox");
1564 expected
.value
= ASCIIToUTF16("mail");
1565 expected
.form_control_type
= "checkbox";
1566 expected
.is_autofilled
= true;
1567 expected
.is_checkable
= true;
1568 expected
.is_checked
= true;
1569 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1571 element
= GetInputElementById("radio");
1572 element
.setAutofilled(true);
1573 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1574 expected
.name
= ASCIIToUTF16("radio");
1575 expected
.value
= ASCIIToUTF16("male");
1576 expected
.form_control_type
= "radio";
1577 expected
.is_autofilled
= true;
1578 expected
.is_checkable
= true;
1579 expected
.is_checked
= false;
1580 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1583 // We should be able to extract a <select> field.
1584 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldSelect
) {
1585 LoadHTML("<SELECT id='element'/>"
1586 " <OPTION value='CA'>California</OPTION>"
1587 " <OPTION value='TX'>Texas</OPTION>"
1590 WebFrame
* frame
= GetMainFrame();
1591 ASSERT_NE(nullptr, frame
);
1593 WebFormControlElement element
= GetFormControlElementById("element");
1594 FormFieldData result1
;
1595 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result1
);
1597 FormFieldData expected
;
1598 expected
.name
= ASCIIToUTF16("element");
1599 expected
.max_length
= 0;
1600 expected
.form_control_type
= "select-one";
1602 expected
.value
= ASCIIToUTF16("CA");
1603 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result1
);
1605 FormFieldData result2
;
1606 WebFormControlElementToFormField(
1608 static_cast<ExtractMask
>(EXTRACT_VALUE
| EXTRACT_OPTION_TEXT
),
1610 expected
.value
= ASCIIToUTF16("California");
1611 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result2
);
1613 FormFieldData result3
;
1614 WebFormControlElementToFormField(element
, EXTRACT_OPTIONS
, &result3
);
1615 expected
.value
.clear();
1616 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result3
);
1618 ASSERT_EQ(2U, result3
.option_values
.size());
1619 ASSERT_EQ(2U, result3
.option_contents
.size());
1620 EXPECT_EQ(ASCIIToUTF16("CA"), result3
.option_values
[0]);
1621 EXPECT_EQ(ASCIIToUTF16("California"), result3
.option_contents
[0]);
1622 EXPECT_EQ(ASCIIToUTF16("TX"), result3
.option_values
[1]);
1623 EXPECT_EQ(ASCIIToUTF16("Texas"), result3
.option_contents
[1]);
1626 // When faced with <select> field with *many* options, we should trim them to a
1627 // reasonable number.
1628 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldLongSelect
) {
1629 std::string html
= "<SELECT id='element'/>";
1630 for (size_t i
= 0; i
< 2 * kMaxListSize
; ++i
) {
1631 html
+= base::StringPrintf("<OPTION value='%" PRIuS
"'>"
1632 "%" PRIuS
"</OPTION>", i
, i
);
1634 html
+= "</SELECT>";
1635 LoadHTML(html
.c_str());
1637 WebFrame
* frame
= GetMainFrame();
1640 WebFormControlElement element
= GetFormControlElementById("element");
1641 FormFieldData result
;
1642 WebFormControlElementToFormField(element
, EXTRACT_OPTIONS
, &result
);
1644 EXPECT_TRUE(result
.option_values
.empty());
1645 EXPECT_TRUE(result
.option_contents
.empty());
1648 // We should be able to extract a <textarea> field.
1649 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldTextArea
) {
1650 LoadHTML("<TEXTAREA id='element'>"
1651 "This element's value "
1652 "spans multiple lines."
1655 WebFrame
* frame
= GetMainFrame();
1656 ASSERT_NE(nullptr, frame
);
1658 WebFormControlElement element
= GetFormControlElementById("element");
1659 FormFieldData result_sans_value
;
1660 WebFormControlElementToFormField(element
, EXTRACT_NONE
, &result_sans_value
);
1662 FormFieldData expected
;
1663 expected
.name
= ASCIIToUTF16("element");
1664 expected
.max_length
= 0;
1665 expected
.form_control_type
= "textarea";
1666 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result_sans_value
);
1668 FormFieldData result_with_value
;
1669 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result_with_value
);
1670 expected
.value
= ASCIIToUTF16("This element's value\n"
1671 "spans multiple lines.");
1672 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result_with_value
);
1675 // We should be able to extract an <input type="month"> field.
1676 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldMonthInput
) {
1677 LoadHTML("<INPUT type='month' id='element' value='2011-12'>");
1679 WebFrame
* frame
= GetMainFrame();
1680 ASSERT_NE(nullptr, frame
);
1682 WebFormControlElement element
= GetFormControlElementById("element");
1683 FormFieldData result_sans_value
;
1684 WebFormControlElementToFormField(element
, EXTRACT_NONE
, &result_sans_value
);
1686 FormFieldData expected
;
1687 expected
.name
= ASCIIToUTF16("element");
1688 expected
.max_length
= 0;
1689 expected
.form_control_type
= "month";
1690 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result_sans_value
);
1692 FormFieldData result_with_value
;
1693 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result_with_value
);
1694 expected
.value
= ASCIIToUTF16("2011-12");
1695 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result_with_value
);
1698 // We should not extract the value for non-text and non-select fields.
1699 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldInvalidType
) {
1700 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1701 " <INPUT type='hidden' id='hidden' value='apple'/>"
1702 " <INPUT type='submit' id='submit' value='Send'/>"
1705 WebFrame
* frame
= GetMainFrame();
1706 ASSERT_NE(nullptr, frame
);
1708 WebFormControlElement element
= GetFormControlElementById("hidden");
1709 FormFieldData result
;
1710 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1712 FormFieldData expected
;
1713 expected
.max_length
= 0;
1715 expected
.name
= ASCIIToUTF16("hidden");
1716 expected
.form_control_type
= "hidden";
1717 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1719 element
= GetFormControlElementById("submit");
1720 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1721 expected
.name
= ASCIIToUTF16("submit");
1722 expected
.form_control_type
= "submit";
1723 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1726 // We should be able to extract password fields.
1727 TEST_F(FormAutofillTest
, WebFormControlElementToPasswordFormField
) {
1728 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1729 " <INPUT type='password' id='password' value='secret'/>"
1732 WebFrame
* frame
= GetMainFrame();
1733 ASSERT_NE(nullptr, frame
);
1735 WebFormControlElement element
= GetFormControlElementById("password");
1736 FormFieldData result
;
1737 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1739 FormFieldData expected
;
1740 expected
.max_length
= WebInputElement::defaultMaxLength();
1741 expected
.name
= ASCIIToUTF16("password");
1742 expected
.form_control_type
= "password";
1743 expected
.value
= ASCIIToUTF16("secret");
1744 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1747 // We should be able to extract the autocompletetype attribute.
1748 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutocompletetype
) {
1750 "<INPUT type='text' id='absent'/>"
1751 "<INPUT type='text' id='empty' autocomplete=''/>"
1752 "<INPUT type='text' id='off' autocomplete='off'/>"
1753 "<INPUT type='text' id='regular' autocomplete='email'/>"
1754 "<INPUT type='text' id='multi-valued' "
1755 " autocomplete='billing email'/>"
1756 "<INPUT type='text' id='experimental' x-autocompletetype='email'/>"
1757 "<INPUT type='month' id='month' autocomplete='cc-exp'/>"
1758 "<SELECT id='select' autocomplete='state'/>"
1759 " <OPTION value='CA'>California</OPTION>"
1760 " <OPTION value='TX'>Texas</OPTION>"
1762 "<TEXTAREA id='textarea' autocomplete='street-address'>"
1767 "<INPUT type='text' id='malicious' autocomplete='" +
1768 std::string(10000, 'x') + "'/>";
1769 LoadHTML(html
.c_str());
1771 WebFrame
* frame
= GetMainFrame();
1772 ASSERT_NE(nullptr, frame
);
1775 const std::string element_id
;
1776 const std::string form_control_type
;
1777 const std::string autocomplete_attribute
;
1779 TestCase test_cases
[] = {
1780 // An absent attribute is equivalent to an empty one.
1781 { "absent", "text", "" },
1782 // Make sure there are no issues parsing an empty attribute.
1783 { "empty", "text", "" },
1784 // Make sure there are no issues parsing an attribute value that isn't a
1786 { "off", "text", "off" },
1787 // Common case: exactly one type specified.
1788 { "regular", "text", "email" },
1789 // Verify that we correctly extract multiple tokens as well.
1790 { "multi-valued", "text", "billing email" },
1791 // Verify that <input type="month"> fields are supported.
1792 { "month", "month", "cc-exp" },
1793 // We previously extracted this data from the experimental
1794 // 'x-autocompletetype' attribute. Now that the field type hints are part
1795 // of the spec under the autocomplete attribute, we no longer support the
1796 // experimental version.
1797 { "experimental", "text", "" },
1798 // <select> elements should behave no differently from text fields here.
1799 { "select", "select-one", "state" },
1800 // <textarea> elements should also behave no differently from text fields.
1801 { "textarea", "textarea", "street-address" },
1802 // Very long attribute values should be replaced by a default string, to
1803 // prevent malicious websites from DOSing the browser process.
1804 { "malicious", "text", "x-max-data-length-exceeded" },
1807 WebDocument document
= frame
->document();
1808 for (size_t i
= 0; i
< arraysize(test_cases
); ++i
) {
1809 WebFormControlElement element
=
1810 GetFormControlElementById(ASCIIToUTF16(test_cases
[i
].element_id
));
1811 FormFieldData result
;
1812 WebFormControlElementToFormField(element
, EXTRACT_NONE
, &result
);
1814 FormFieldData expected
;
1815 expected
.name
= ASCIIToUTF16(test_cases
[i
].element_id
);
1816 expected
.form_control_type
= test_cases
[i
].form_control_type
;
1817 expected
.autocomplete_attribute
= test_cases
[i
].autocomplete_attribute
;
1818 if (test_cases
[i
].form_control_type
== "text")
1819 expected
.max_length
= WebInputElement::defaultMaxLength();
1821 expected
.max_length
= 0;
1823 SCOPED_TRACE(test_cases
[i
].element_id
);
1824 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
1828 TEST_F(FormAutofillTest
, DetectTextDirectionFromDirectStyle
) {
1829 LoadHTML("<STYLE>input{direction:rtl}</STYLE>"
1831 " <INPUT type='text' id='element'>"
1834 WebFrame
* frame
= GetMainFrame();
1835 ASSERT_NE(nullptr, frame
);
1837 WebFormControlElement element
= GetFormControlElementById("element");
1839 FormFieldData result
;
1840 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1841 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT
, result
.text_direction
);
1844 TEST_F(FormAutofillTest
, DetectTextDirectionFromDirectDIRAttribute
) {
1846 " <INPUT dir='rtl' type='text' id='element'/>"
1849 WebFrame
* frame
= GetMainFrame();
1850 ASSERT_NE(nullptr, frame
);
1852 WebFormControlElement element
= GetFormControlElementById("element");
1854 FormFieldData result
;
1855 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1856 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT
, result
.text_direction
);
1859 TEST_F(FormAutofillTest
, DetectTextDirectionFromParentStyle
) {
1860 LoadHTML("<STYLE>form{direction:rtl}</STYLE>"
1862 " <INPUT type='text' id='element'/>"
1865 WebFrame
* frame
= GetMainFrame();
1866 ASSERT_NE(nullptr, frame
);
1868 WebFormControlElement element
= GetFormControlElementById("element");
1870 FormFieldData result
;
1871 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1872 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT
, result
.text_direction
);
1875 TEST_F(FormAutofillTest
, DetectTextDirectionFromParentDIRAttribute
) {
1876 LoadHTML("<FORM dir='rtl'>"
1877 " <INPUT type='text' id='element'/>"
1880 WebFrame
* frame
= GetMainFrame();
1881 ASSERT_NE(nullptr, frame
);
1883 WebFormControlElement element
= GetFormControlElementById("element");
1885 FormFieldData result
;
1886 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1887 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT
, result
.text_direction
);
1890 TEST_F(FormAutofillTest
, DetectTextDirectionWhenStyleAndDIRAttributMixed
) {
1891 LoadHTML("<STYLE>input{direction:ltr}</STYLE>"
1893 " <INPUT type='text' id='element'/>"
1896 WebFrame
* frame
= GetMainFrame();
1897 ASSERT_NE(nullptr, frame
);
1899 WebFormControlElement element
= GetFormControlElementById("element");
1901 FormFieldData result
;
1902 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1903 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT
, result
.text_direction
);
1906 TEST_F(FormAutofillTest
,
1907 DetectTextDirectionWhenParentHasBothDIRAttributeAndStyle
) {
1908 LoadHTML("<STYLE>form{direction:ltr}</STYLE>"
1910 " <INPUT type='text' id='element'/>"
1913 WebFrame
* frame
= GetMainFrame();
1914 ASSERT_NE(nullptr, frame
);
1916 WebFormControlElement element
= GetFormControlElementById("element");
1918 FormFieldData result
;
1919 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1920 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT
, result
.text_direction
);
1923 TEST_F(FormAutofillTest
, DetectTextDirectionWhenAncestorHasInlineStyle
) {
1924 LoadHTML("<FORM style='direction:ltr'>"
1926 " <INPUT type='text' id='element'/>"
1930 WebFrame
* frame
= GetMainFrame();
1931 ASSERT_NE(nullptr, frame
);
1933 WebFormControlElement element
= GetFormControlElementById("element");
1935 FormFieldData result
;
1936 WebFormControlElementToFormField(element
, EXTRACT_VALUE
, &result
);
1937 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT
, result
.text_direction
);
1940 TEST_F(FormAutofillTest
, WebFormElementToFormData
) {
1941 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1942 " <LABEL for='firstname'>First name:</LABEL>"
1943 " <INPUT type='text' id='firstname' value='John'/>"
1944 " <LABEL for='lastname'>Last name:</LABEL>"
1945 " <INPUT type='text' id='lastname' value='Smith'/>"
1946 " <LABEL for='street-address'>Address:</LABEL>"
1947 " <TEXTAREA id='street-address'>"
1948 "123 Fantasy Ln. "
1951 " <LABEL for='state'>State:</LABEL>"
1952 " <SELECT id='state'/>"
1953 " <OPTION value='CA'>California</OPTION>"
1954 " <OPTION value='TX'>Texas</OPTION>"
1956 " <LABEL for='password'>Password:</LABEL>"
1957 " <INPUT type='password' id='password' value='secret'/>"
1958 " <LABEL for='month'>Card expiration:</LABEL>"
1959 " <INPUT type='month' id='month' value='2011-12'/>"
1960 " <INPUT type='submit' name='reply-send' value='Send'/>"
1961 // The below inputs should be ignored
1962 " <LABEL for='notvisible'>Hidden:</LABEL>"
1963 " <INPUT type='hidden' id='notvisible' value='apple'/>"
1966 WebFrame
* frame
= GetMainFrame();
1967 ASSERT_NE(nullptr, frame
);
1969 WebVector
<WebFormElement
> forms
;
1970 frame
->document().forms(forms
);
1971 ASSERT_EQ(1U, forms
.size());
1973 WebInputElement input_element
= GetInputElementById("firstname");
1976 FormFieldData field
;
1977 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
1983 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
1984 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
1985 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
1987 const std::vector
<FormFieldData
>& fields
= form
.fields
;
1988 ASSERT_EQ(6U, fields
.size());
1990 FormFieldData expected
;
1991 expected
.name
= ASCIIToUTF16("firstname");
1992 expected
.value
= ASCIIToUTF16("John");
1993 expected
.label
= ASCIIToUTF16("First name:");
1994 expected
.form_control_type
= "text";
1995 expected
.max_length
= WebInputElement::defaultMaxLength();
1996 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
1998 expected
.name
= ASCIIToUTF16("lastname");
1999 expected
.value
= ASCIIToUTF16("Smith");
2000 expected
.label
= ASCIIToUTF16("Last name:");
2001 expected
.form_control_type
= "text";
2002 expected
.max_length
= WebInputElement::defaultMaxLength();
2003 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2005 expected
.name
= ASCIIToUTF16("street-address");
2006 expected
.value
= ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
2007 expected
.label
= ASCIIToUTF16("Address:");
2008 expected
.form_control_type
= "textarea";
2009 expected
.max_length
= 0;
2010 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2012 expected
.name
= ASCIIToUTF16("state");
2013 expected
.value
= ASCIIToUTF16("CA");
2014 expected
.label
= ASCIIToUTF16("State:");
2015 expected
.form_control_type
= "select-one";
2016 expected
.max_length
= 0;
2017 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
2019 expected
.name
= ASCIIToUTF16("password");
2020 expected
.value
= ASCIIToUTF16("secret");
2021 expected
.label
= ASCIIToUTF16("Password:");
2022 expected
.form_control_type
= "password";
2023 expected
.max_length
= WebInputElement::defaultMaxLength();
2024 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[4]);
2026 expected
.name
= ASCIIToUTF16("month");
2027 expected
.value
= ASCIIToUTF16("2011-12");
2028 expected
.label
= ASCIIToUTF16("Card expiration:");
2029 expected
.form_control_type
= "month";
2030 expected
.max_length
= 0;
2031 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[5]);
2034 // We should not be able to serialize a form with too many fillable fields.
2035 TEST_F(FormAutofillTest
, WebFormElementToFormDataTooManyFields
) {
2037 "<FORM name='TestForm' action='http://cnn.com' method='post'>";
2038 for (size_t i
= 0; i
< (kMaxParseableFields
+ 1); ++i
) {
2039 html
+= "<INPUT type='text'/>";
2042 LoadHTML(html
.c_str());
2044 WebFrame
* frame
= GetMainFrame();
2045 ASSERT_NE(nullptr, frame
);
2047 WebVector
<WebFormElement
> forms
;
2048 frame
->document().forms(forms
);
2049 ASSERT_EQ(1U, forms
.size());
2051 WebInputElement input_element
= GetInputElementById("firstname");
2054 FormFieldData field
;
2055 EXPECT_FALSE(WebFormElementToFormData(forms
[0],
2063 TEST_F(FormAutofillTest
, ExtractForms
) {
2064 ExpectJohnSmithLabels(
2065 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2066 " First name: <INPUT type='text' id='firstname' value='John'/>"
2067 " Last name: <INPUT type='text' id='lastname' value='Smith'/>"
2068 " Email: <INPUT type='text' id='email' value='john@example.com'/>"
2069 " <INPUT type='submit' name='reply-send' value='Send'/>"
2073 TEST_F(FormAutofillTest
, ExtractMultipleForms
) {
2074 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2075 " <INPUT type='text' id='firstname' value='John'/>"
2076 " <INPUT type='text' id='lastname' value='Smith'/>"
2077 " <INPUT type='text' id='email' value='john@example.com'/>"
2078 " <INPUT type='submit' name='reply-send' value='Send'/>"
2080 "<FORM name='TestForm2' action='http://zoo.com' method='post'>"
2081 " <INPUT type='text' id='firstname' value='Jack'/>"
2082 " <INPUT type='text' id='lastname' value='Adams'/>"
2083 " <INPUT type='text' id='email' value='jack@example.com'/>"
2084 " <INPUT type='submit' name='reply-send' value='Send'/>"
2087 WebFrame
* web_frame
= GetMainFrame();
2088 ASSERT_NE(nullptr, web_frame
);
2090 FormCache
form_cache(*web_frame
);
2091 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
2092 ASSERT_EQ(2U, forms
.size());
2095 const FormData
& form
= forms
[0];
2096 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2097 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2098 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
2100 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2101 ASSERT_EQ(3U, fields
.size());
2103 FormFieldData expected
;
2104 expected
.form_control_type
= "text";
2105 expected
.max_length
= WebInputElement::defaultMaxLength();
2107 expected
.name
= ASCIIToUTF16("firstname");
2108 expected
.value
= ASCIIToUTF16("John");
2109 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2111 expected
.name
= ASCIIToUTF16("lastname");
2112 expected
.value
= ASCIIToUTF16("Smith");
2113 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2115 expected
.name
= ASCIIToUTF16("email");
2116 expected
.value
= ASCIIToUTF16("john@example.com");
2117 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2120 const FormData
& form2
= forms
[1];
2121 EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2
.name
);
2122 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2123 EXPECT_EQ(GURL("http://zoo.com"), form2
.action
);
2125 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2126 ASSERT_EQ(3U, fields2
.size());
2128 expected
.name
= ASCIIToUTF16("firstname");
2129 expected
.value
= ASCIIToUTF16("Jack");
2130 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2132 expected
.name
= ASCIIToUTF16("lastname");
2133 expected
.value
= ASCIIToUTF16("Adams");
2134 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2136 expected
.name
= ASCIIToUTF16("email");
2137 expected
.value
= ASCIIToUTF16("jack@example.com");
2138 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2141 TEST_F(FormAutofillTest
, OnlyExtractNewForms
) {
2143 "<FORM id='testform' action='http://cnn.com' method='post'>"
2144 " <INPUT type='text' id='firstname' value='John'/>"
2145 " <INPUT type='text' id='lastname' value='Smith'/>"
2146 " <INPUT type='text' id='email' value='john@example.com'/>"
2147 " <INPUT type='submit' name='reply-send' value='Send'/>"
2150 WebFrame
* web_frame
= GetMainFrame();
2151 ASSERT_NE(nullptr, web_frame
);
2153 FormCache
form_cache(*web_frame
);
2154 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
2155 ASSERT_EQ(1U, forms
.size());
2157 // Second call should give nothing as there are no new forms.
2158 forms
= form_cache
.ExtractNewForms();
2159 ASSERT_TRUE(forms
.empty());
2161 // Append to the current form will re-extract.
2163 "var newInput = document.createElement('input');"
2164 "newInput.setAttribute('type', 'text');"
2165 "newInput.setAttribute('id', 'telephone');"
2166 "newInput.value = '12345';"
2167 "document.getElementById('testform').appendChild(newInput);");
2168 msg_loop_
.RunUntilIdle();
2170 forms
= form_cache
.ExtractNewForms();
2171 ASSERT_EQ(1U, forms
.size());
2173 const std::vector
<FormFieldData
>& fields
= forms
[0].fields
;
2174 ASSERT_EQ(4U, fields
.size());
2176 FormFieldData expected
;
2177 expected
.form_control_type
= "text";
2178 expected
.max_length
= WebInputElement::defaultMaxLength();
2180 expected
.name
= ASCIIToUTF16("firstname");
2181 expected
.value
= ASCIIToUTF16("John");
2182 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2184 expected
.name
= ASCIIToUTF16("lastname");
2185 expected
.value
= ASCIIToUTF16("Smith");
2186 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2188 expected
.name
= ASCIIToUTF16("email");
2189 expected
.value
= ASCIIToUTF16("john@example.com");
2190 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2192 expected
.name
= ASCIIToUTF16("telephone");
2193 expected
.value
= ASCIIToUTF16("12345");
2194 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
2198 // Completely new form will also be extracted.
2200 "var newForm=document.createElement('form');"
2201 "newForm.id='new_testform';"
2202 "newForm.action='http://google.com';"
2203 "newForm.method='post';"
2204 "var newFirstname=document.createElement('input');"
2205 "newFirstname.setAttribute('type', 'text');"
2206 "newFirstname.setAttribute('id', 'second_firstname');"
2207 "newFirstname.value = 'Bob';"
2208 "var newLastname=document.createElement('input');"
2209 "newLastname.setAttribute('type', 'text');"
2210 "newLastname.setAttribute('id', 'second_lastname');"
2211 "newLastname.value = 'Hope';"
2212 "var newEmail=document.createElement('input');"
2213 "newEmail.setAttribute('type', 'text');"
2214 "newEmail.setAttribute('id', 'second_email');"
2215 "newEmail.value = 'bobhope@example.com';"
2216 "newForm.appendChild(newFirstname);"
2217 "newForm.appendChild(newLastname);"
2218 "newForm.appendChild(newEmail);"
2219 "document.body.appendChild(newForm);");
2220 msg_loop_
.RunUntilIdle();
2222 web_frame
= GetMainFrame();
2223 forms
= form_cache
.ExtractNewForms();
2224 ASSERT_EQ(1U, forms
.size());
2226 const std::vector
<FormFieldData
>& fields2
= forms
[0].fields
;
2227 ASSERT_EQ(3U, fields2
.size());
2229 expected
.name
= ASCIIToUTF16("second_firstname");
2230 expected
.value
= ASCIIToUTF16("Bob");
2231 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2233 expected
.name
= ASCIIToUTF16("second_lastname");
2234 expected
.value
= ASCIIToUTF16("Hope");
2235 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2237 expected
.name
= ASCIIToUTF16("second_email");
2238 expected
.value
= ASCIIToUTF16("bobhope@example.com");
2239 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2242 // We should not extract a form if it has too few fillable fields.
2243 TEST_F(FormAutofillTest
, ExtractFormsTooFewFields
) {
2244 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2245 " <INPUT type='text' id='firstname' value='John'/>"
2246 " <INPUT type='text' id='lastname' value='Smith'/>"
2247 " <INPUT type='submit' name='reply-send' value='Send'/>"
2250 WebFrame
* web_frame
= GetMainFrame();
2251 ASSERT_NE(nullptr, web_frame
);
2253 FormCache
form_cache(*web_frame
);
2254 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
2255 ASSERT_TRUE(forms
.empty());
2258 // We should not report additional forms for empty forms.
2259 TEST_F(FormAutofillTest
, ExtractFormsSkippedForms
) {
2260 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2261 " <INPUT type='text' id='firstname' value='John'/>"
2262 " <INPUT type='text' id='lastname' value='Smith'/>"
2265 WebFrame
* web_frame
= GetMainFrame();
2266 ASSERT_NE(nullptr, web_frame
);
2268 FormCache
form_cache(*web_frame
);
2269 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
2270 ASSERT_TRUE(forms
.empty());
2273 // We should not report additional forms for empty forms.
2274 TEST_F(FormAutofillTest
, ExtractFormsNoFields
) {
2275 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2278 WebFrame
* web_frame
= GetMainFrame();
2279 ASSERT_NE(nullptr, web_frame
);
2281 FormCache
form_cache(*web_frame
);
2282 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
2283 ASSERT_TRUE(forms
.empty());
2286 // We should not extract a form if it has too few fillable fields.
2287 // Make sure radio and checkbox fields don't count.
2288 TEST_F(FormAutofillTest
, ExtractFormsTooFewFieldsSkipsCheckable
) {
2289 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2290 " <INPUT type='text' id='firstname' value='John'/>"
2291 " <INPUT type='text' id='lastname' value='Smith'/>"
2292 " <INPUT type='radio' id='a_radio' value='0'/>"
2293 " <INPUT type='checkbox' id='a_check' value='1'/>"
2294 " <INPUT type='submit' name='reply-send' value='Send'/>"
2297 WebFrame
* web_frame
= GetMainFrame();
2298 ASSERT_NE(nullptr, web_frame
);
2300 FormCache
form_cache(*web_frame
);
2301 std::vector
<FormData
> forms
= form_cache
.ExtractNewForms();
2302 ASSERT_TRUE(forms
.empty());
2305 TEST_F(FormAutofillTest
, WebFormElementToFormDataAutocomplete
) {
2307 // Form is not auto-completable due to autocomplete=off.
2308 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'"
2309 " autocomplete=off>"
2310 " <INPUT type='text' id='firstname' value='John'/>"
2311 " <INPUT type='text' id='lastname' value='Smith'/>"
2312 " <INPUT type='text' id='email' value='john@example.com'/>"
2313 " <INPUT type='submit' name='reply-send' value='Send'/>"
2316 WebFrame
* web_frame
= GetMainFrame();
2317 ASSERT_NE(nullptr, web_frame
);
2319 WebVector
<WebFormElement
> web_forms
;
2320 web_frame
->document().forms(web_forms
);
2321 ASSERT_EQ(1U, web_forms
.size());
2322 WebFormElement web_form
= web_forms
[0];
2325 EXPECT_TRUE(WebFormElementToFormData(
2326 web_form
, WebFormControlElement(), REQUIRE_NONE
, EXTRACT_NONE
, &form
,
2328 EXPECT_FALSE(WebFormElementToFormData(
2329 web_form
, WebFormControlElement(), REQUIRE_AUTOCOMPLETE
, EXTRACT_NONE
,
2334 // The firstname element is not auto-completable due to autocomplete=off.
2335 LoadHTML("<FORM name='TestForm' action='http://abc.com' "
2337 " <INPUT type='text' id='firstname' value='John'"
2338 " autocomplete=off>"
2339 " <INPUT type='text' id='middlename' value='Jack'/>"
2340 " <INPUT type='text' id='lastname' value='Smith'/>"
2341 " <INPUT type='text' id='email' value='john@example.com'/>"
2342 " <INPUT type='submit' name='reply' value='Send'/>"
2345 WebFrame
* web_frame
= GetMainFrame();
2346 ASSERT_NE(nullptr, web_frame
);
2348 WebVector
<WebFormElement
> web_forms
;
2349 web_frame
->document().forms(web_forms
);
2350 ASSERT_EQ(1U, web_forms
.size());
2351 WebFormElement web_form
= web_forms
[0];
2354 EXPECT_TRUE(WebFormElementToFormData(
2355 web_form
, WebFormControlElement(), REQUIRE_AUTOCOMPLETE
, EXTRACT_VALUE
,
2358 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2359 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2360 EXPECT_EQ(GURL("http://abc.com"), form
.action
);
2362 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2363 ASSERT_EQ(3U, fields
.size());
2365 FormFieldData expected
;
2366 expected
.form_control_type
= "text";
2367 expected
.max_length
= WebInputElement::defaultMaxLength();
2369 expected
.name
= ASCIIToUTF16("middlename");
2370 expected
.value
= ASCIIToUTF16("Jack");
2371 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2373 expected
.name
= ASCIIToUTF16("lastname");
2374 expected
.value
= ASCIIToUTF16("Smith");
2375 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2377 expected
.name
= ASCIIToUTF16("email");
2378 expected
.value
= ASCIIToUTF16("john@example.com");
2379 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2383 TEST_F(FormAutofillTest
, FindFormForInputElement
) {
2384 TestFindFormForInputElement(
2385 "<FORM name='TestForm' action='http://buh.com' method='post'>"
2386 " <INPUT type='text' id='firstname' value='John'/>"
2387 " <INPUT type='text' id='lastname' value='Smith'/>"
2388 " <INPUT type='text' id='email' value='john@example.com'"
2389 "autocomplete='off' />"
2390 " <INPUT type='text' id='phone' value='1.800.555.1234'/>"
2391 " <INPUT type='submit' name='reply-send' value='Send'/>"
2396 TEST_F(FormAutofillTest
, FindFormForInputElementForUnownedForm
) {
2397 TestFindFormForInputElement(
2398 "<INPUT type='text' id='firstname' value='John'/>"
2399 "<INPUT type='text' id='lastname' value='Smith'/>"
2400 "<INPUT type='text' id='email' value='john@example.com'"
2401 "autocomplete='off' />"
2402 "<INPUT type='text' id='phone' value='1.800.555.1234'/>"
2403 "<INPUT type='submit' name='reply-send' value='Send'/>",
2407 TEST_F(FormAutofillTest
, FindFormForTextAreaElement
) {
2408 TestFindFormForTextAreaElement(
2409 "<FORM name='TestForm' action='http://buh.com' method='post'>"
2410 " <INPUT type='text' id='firstname' value='John'/>"
2411 " <INPUT type='text' id='lastname' value='Smith'/>"
2412 " <INPUT type='text' id='email' value='john@example.com'"
2413 "autocomplete='off' />"
2414 " <TEXTAREA id='street-address'>"
2415 "123 Fantasy Ln. "
2418 " <INPUT type='submit' name='reply-send' value='Send'/>"
2423 TEST_F(FormAutofillTest
, FindFormForTextAreaElementForUnownedForm
) {
2424 TestFindFormForTextAreaElement(
2425 "<INPUT type='text' id='firstname' value='John'/>"
2426 "<INPUT type='text' id='lastname' value='Smith'/>"
2427 "<INPUT type='text' id='email' value='john@example.com'"
2428 "autocomplete='off' />"
2429 "<TEXTAREA id='street-address'>"
2430 "123 Fantasy Ln. "
2433 "<INPUT type='submit' name='reply-send' value='Send'/>",
2437 // Test regular FillForm function.
2438 TEST_F(FormAutofillTest
, FillForm
) {
2439 TestFillForm(kFormHtml
, false);
2442 TEST_F(FormAutofillTest
, FillFormForUnownedForm
) {
2443 TestFillForm(kUnownedFormHtml
, true);
2446 TEST_F(FormAutofillTest
, FillFormIncludingNonFocusableElements
) {
2447 static const AutofillFieldCase field_cases
[] = {
2448 // fields: form_control_type, name, initial_value, autocomplete_attribute,
2449 // should_be_autofilled, autofill_value, expected_value
2451 // Regular empty fields (firstname & lastname) should be autofilled.
2458 "filled firstname"},
2459 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
2460 // hidden fields should not be extracted to form_data.
2461 // Non empty fields should be overriden.
2474 "filled noautocomplete",
2475 "filled noautocomplete"},
2476 // Disabled fields should not be autofilled.
2477 {"text", "notenabled", "", "", false, "filled notenabled", ""},
2478 // Readonly fields should not be autofilled.
2479 {"text", "readonly", "", "", false, "filled readonly", ""},
2480 // Fields with "visibility: hidden" should also be autofilled.
2487 "filled invisible"},
2488 // Fields with "display:none" should also be autofilled.
2494 "filled displaynone",
2495 "filled displaynone"},
2496 // Regular <input type="month"> should be autofilled.
2497 {"month", "month", "", "", true, "2017-11", "2017-11"},
2498 // Non-empty <input type="month"> should be overridden.
2499 {"month", "month-nonempty", "2011-12", "", true, "2017-11", "2017-11"},
2500 // Regular select fields should be autofilled.
2501 {"select-one", "select", "", "", true, "TX", "TX"},
2502 // Select fields should be autofilled even if they already have a
2504 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
2505 // Select fields should not be autofilled if no new value is passed from
2506 // autofill profile. The existing value should not be overriden.
2507 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
2508 // Regular textarea elements should be autofilled.
2514 "some multi-\nline value",
2515 "some multi-\nline value"},
2516 // Nonempty textarea elements should be overridden.
2518 "textarea-nonempty",
2522 "some multi-\nline value",
2523 "some multi-\nline value"},
2525 TestFormFillFunctions(kFormHtml
, false, field_cases
, arraysize(field_cases
),
2526 &FillFormIncludingNonFocusableElementsWrapper
,
2530 TEST_F(FormAutofillTest
, PreviewForm
) {
2531 TestPreviewForm(kFormHtml
, false);
2534 TEST_F(FormAutofillTest
, PreviewFormForUnownedForm
) {
2535 TestPreviewForm(kUnownedFormHtml
, true);
2538 TEST_F(FormAutofillTest
, Labels
) {
2539 ExpectJohnSmithLabels(
2540 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2541 " <LABEL for='firstname'> First name: </LABEL>"
2542 " <INPUT type='text' id='firstname' value='John'/>"
2543 " <LABEL for='lastname'> Last name: </LABEL>"
2544 " <INPUT type='text' id='lastname' value='Smith'/>"
2545 " <LABEL for='email'> Email: </LABEL>"
2546 " <INPUT type='text' id='email' value='john@example.com'/>"
2547 " <INPUT type='submit' name='reply-send' value='Send'/>"
2551 TEST_F(FormAutofillTest
, LabelsWithSpans
) {
2552 ExpectJohnSmithLabels(
2553 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2554 " <LABEL for='firstname'><span>First name: </span></LABEL>"
2555 " <INPUT type='text' id='firstname' value='John'/>"
2556 " <LABEL for='lastname'><span>Last name: </span></LABEL>"
2557 " <INPUT type='text' id='lastname' value='Smith'/>"
2558 " <LABEL for='email'><span>Email: </span></LABEL>"
2559 " <INPUT type='text' id='email' value='john@example.com'/>"
2560 " <INPUT type='submit' name='reply-send' value='Send'/>"
2564 // This test is different from FormAutofillTest.Labels in that the label
2565 // elements for= attribute is set to the name of the form control element it is
2566 // a label for instead of the id of the form control element. This is invalid
2567 // because the for= attribute must be set to the id of the form control element;
2568 // however, current label parsing code will extract the text from the previous
2569 // label element and apply it to the following input field.
2570 TEST_F(FormAutofillTest
, InvalidLabels
) {
2571 ExpectJohnSmithLabels(
2572 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2573 " <LABEL for='firstname'> First name: </LABEL>"
2574 " <INPUT type='text' name='firstname' value='John'/>"
2575 " <LABEL for='lastname'> Last name: </LABEL>"
2576 " <INPUT type='text' name='lastname' value='Smith'/>"
2577 " <LABEL for='email'> Email: </LABEL>"
2578 " <INPUT type='text' name='email' value='john@example.com'/>"
2579 " <INPUT type='submit' name='reply-send' value='Send'/>"
2583 // This test has three form control elements, only one of which has a label
2584 // element associated with it.
2585 TEST_F(FormAutofillTest
, OneLabelElement
) {
2586 ExpectJohnSmithLabels(
2587 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2589 " <INPUT type='text' id='firstname' value='John'/>"
2590 " <LABEL for='lastname'>Last name: </LABEL>"
2591 " <INPUT type='text' id='lastname' value='Smith'/>"
2593 " <INPUT type='text' id='email' value='john@example.com'/>"
2594 " <INPUT type='submit' name='reply-send' value='Send'/>"
2598 TEST_F(FormAutofillTest
, LabelsInferredFromText
) {
2599 ExpectJohnSmithLabels(
2600 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2602 " <INPUT type='text' id='firstname' value='John'/>"
2604 " <INPUT type='text' id='lastname' value='Smith'/>"
2606 " <INPUT type='text' id='email' value='john@example.com'/>"
2607 " <INPUT type='submit' name='reply-send' value='Send'/>"
2611 TEST_F(FormAutofillTest
, LabelsInferredFromParagraph
) {
2612 ExpectJohnSmithLabels(
2613 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2614 " <P>First name:</P><INPUT type='text' "
2615 " id='firstname' value='John'/>"
2616 " <P>Last name:</P>"
2617 " <INPUT type='text' id='lastname' value='Smith'/>"
2619 " <INPUT type='text' id='email' value='john@example.com'/>"
2620 " <INPUT type='submit' name='reply-send' value='Send'/>"
2624 TEST_F(FormAutofillTest
, LabelsInferredFromBold
) {
2625 ExpectJohnSmithLabels(
2626 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2627 " <B>First name:</B><INPUT type='text' "
2628 " id='firstname' value='John'/>"
2629 " <B>Last name:</B>"
2630 " <INPUT type='text' id='lastname' value='Smith'/>"
2632 " <INPUT type='text' id='email' value='john@example.com'/>"
2633 " <INPUT type='submit' name='reply-send' value='Send'/>"
2637 TEST_F(FormAutofillTest
, LabelsInferredPriorToImgOrBr
) {
2638 ExpectJohnSmithLabels(
2639 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2640 " First name:<IMG/><INPUT type='text' "
2641 " id='firstname' value='John'/>"
2643 " <INPUT type='text' id='lastname' value='Smith'/>"
2645 " <INPUT type='text' id='email' value='john@example.com'/>"
2646 " <INPUT type='submit' name='reply-send' value='Send'/>"
2650 TEST_F(FormAutofillTest
, LabelsInferredFromTableCell
) {
2651 ExpectJohnSmithLabels(
2652 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2655 " <TD>First name:</TD>"
2656 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
2659 " <TD>Last name:</TD>"
2660 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
2664 " <TD><INPUT type='text' id='email'"
2665 " value='john@example.com'/></TD>"
2670 " <INPUT type='submit' name='reply-send' value='Send'/>"
2677 TEST_F(FormAutofillTest
, LabelsInferredFromTableCellTH
) {
2678 ExpectJohnSmithLabels(
2679 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2682 " <TH>First name:</TH>"
2683 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
2686 " <TH>Last name:</TH>"
2687 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
2691 " <TD><INPUT type='text' id='email'"
2692 " value='john@example.com'/></TD>"
2697 " <INPUT type='submit' name='reply-send' value='Send'/>"
2704 TEST_F(FormAutofillTest
, LabelsInferredFromTableCellNested
) {
2705 std::vector
<base::string16
> labels
, names
, values
;
2707 labels
.push_back(ASCIIToUTF16("First name: Bogus"));
2708 names
.push_back(ASCIIToUTF16("firstname"));
2709 values
.push_back(ASCIIToUTF16("John"));
2711 labels
.push_back(ASCIIToUTF16("Last name:"));
2712 names
.push_back(ASCIIToUTF16("lastname"));
2713 values
.push_back(ASCIIToUTF16("Smith"));
2715 labels
.push_back(ASCIIToUTF16("Email:"));
2716 names
.push_back(ASCIIToUTF16("email"));
2717 values
.push_back(ASCIIToUTF16("john@example.com"));
2720 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2733 " <INPUT type='text' id='firstname' value='John'/>"
2745 " <INPUT type='text' id='lastname' value='Smith'/>"
2757 " <INPUT type='text' id='email' value='john@example.com'/>"
2764 " <INPUT type='submit' name='reply-send' value='Send'/>"
2769 labels
, names
, values
);
2772 TEST_F(FormAutofillTest
, LabelsInferredFromTableEmptyTDs
) {
2773 std::vector
<base::string16
> labels
, names
, values
;
2775 labels
.push_back(ASCIIToUTF16("* First Name"));
2776 names
.push_back(ASCIIToUTF16("firstname"));
2777 values
.push_back(ASCIIToUTF16("John"));
2779 labels
.push_back(ASCIIToUTF16("* Last Name"));
2780 names
.push_back(ASCIIToUTF16("lastname"));
2781 values
.push_back(ASCIIToUTF16("Smith"));
2783 labels
.push_back(ASCIIToUTF16("* Email"));
2784 names
.push_back(ASCIIToUTF16("email"));
2785 values
.push_back(ASCIIToUTF16("john@example.com"));
2788 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2793 " <B>First Name</B>"
2797 " <INPUT type='text' id='firstname' value='John'/>"
2807 " <INPUT type='text' id='lastname' value='Smith'/>"
2817 " <INPUT type='text' id='email' value='john@example.com'/>"
2823 " <INPUT type='submit' name='reply-send' value='Send'/>"
2828 labels
, names
, values
);
2831 TEST_F(FormAutofillTest
, LabelsInferredFromPreviousTD
) {
2832 std::vector
<base::string16
> labels
, names
, values
;
2834 labels
.push_back(ASCIIToUTF16("* First Name"));
2835 names
.push_back(ASCIIToUTF16("firstname"));
2836 values
.push_back(ASCIIToUTF16("John"));
2838 labels
.push_back(ASCIIToUTF16("* Last Name"));
2839 names
.push_back(ASCIIToUTF16("lastname"));
2840 values
.push_back(ASCIIToUTF16("Smith"));
2842 labels
.push_back(ASCIIToUTF16("* Email"));
2843 names
.push_back(ASCIIToUTF16("email"));
2844 values
.push_back(ASCIIToUTF16("john@example.com"));
2847 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2850 " <TD>* First Name</TD>"
2853 " <INPUT type='hidden'/>"
2854 " <INPUT type='text' id='firstname' value='John'/>"
2858 " <TD>* Last Name</TD>"
2860 " <INPUT type='text' id='lastname' value='Smith'/>"
2866 " <INPUT type='text' id='email' value='john@example.com'/>"
2872 " <INPUT type='submit' name='reply-send' value='Send'/>"
2877 labels
, names
, values
);
2880 // <script>, <noscript> and <option> tags are excluded when the labels are
2882 // Also <!-- comment --> is excluded.
2883 TEST_F(FormAutofillTest
, LabelsInferredFromTableWithSpecialElements
) {
2884 std::vector
<base::string16
> labels
, names
, values
;
2885 std::vector
<std::string
> control_types
;
2887 labels
.push_back(ASCIIToUTF16("* First Name"));
2888 names
.push_back(ASCIIToUTF16("firstname"));
2889 values
.push_back(ASCIIToUTF16("John"));
2890 control_types
.push_back("text");
2892 labels
.push_back(ASCIIToUTF16("* Middle Name"));
2893 names
.push_back(ASCIIToUTF16("middlename"));
2894 values
.push_back(ASCIIToUTF16("Joe"));
2895 control_types
.push_back("text");
2897 labels
.push_back(ASCIIToUTF16("* Last Name"));
2898 names
.push_back(ASCIIToUTF16("lastname"));
2899 values
.push_back(ASCIIToUTF16("Smith"));
2900 control_types
.push_back("text");
2902 labels
.push_back(ASCIIToUTF16("* Country"));
2903 names
.push_back(ASCIIToUTF16("country"));
2904 values
.push_back(ASCIIToUTF16("US"));
2905 control_types
.push_back("select-one");
2907 labels
.push_back(ASCIIToUTF16("* Email"));
2908 names
.push_back(ASCIIToUTF16("email"));
2909 values
.push_back(ASCIIToUTF16("john@example.com"));
2910 control_types
.push_back("text");
2912 ExpectLabelsAndTypes(
2913 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2918 " <B>First Name</B>"
2921 " <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
2923 " <INPUT type='text' id='firstname' value='John'/>"
2929 " <B>Middle Name</B>"
2935 " <INPUT type='text' id='middlename' value='Joe'/>"
2944 " <INPUT type='text' id='lastname' value='Smith'/>"
2953 " <SELECT id='country'>"
2954 " <OPTION VALUE='US'>The value should be ignored as label."
2956 " <OPTION VALUE='JP'>JAPAN</OPTION>"
2966 " <!-- This comment should be ignored as inferred label.-->"
2967 " <INPUT type='text' id='email' value='john@example.com'/>"
2973 " <INPUT type='submit' name='reply-send' value='Send'/>"
2978 labels
, names
, values
, control_types
);
2981 TEST_F(FormAutofillTest
, LabelsInferredFromTableLabels
) {
2982 ExpectJohnSmithLabels(
2983 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2987 " <LABEL>First name:</LABEL>"
2988 " <INPUT type='text' id='firstname' value='John'/>"
2993 " <LABEL>Last name:</LABEL>"
2994 " <INPUT type='text' id='lastname' value='Smith'/>"
2999 " <LABEL>Email:</LABEL>"
3000 " <INPUT type='text' id='email' value='john@example.com'/>"
3004 "<INPUT type='submit' name='reply-send' value='Send'/>"
3008 TEST_F(FormAutofillTest
, LabelsInferredFromTableTDInterveningElements
) {
3009 ExpectJohnSmithLabels(
3010 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3016 " <INPUT type='text' id='firstname' value='John'/>"
3023 " <INPUT type='text' id='lastname' value='Smith'/>"
3030 " <INPUT type='text' id='email' value='john@example.com'/>"
3034 "<INPUT type='submit' name='reply-send' value='Send'/>"
3038 // Verify that we correctly infer labels when the label text spans multiple
3039 // adjacent HTML elements, not separated by whitespace.
3040 TEST_F(FormAutofillTest
, LabelsInferredFromTableAdjacentElements
) {
3041 std::vector
<base::string16
> labels
, names
, values
;
3043 labels
.push_back(ASCIIToUTF16("*First Name"));
3044 names
.push_back(ASCIIToUTF16("firstname"));
3045 values
.push_back(ASCIIToUTF16("John"));
3047 labels
.push_back(ASCIIToUTF16("*Last Name"));
3048 names
.push_back(ASCIIToUTF16("lastname"));
3049 values
.push_back(ASCIIToUTF16("Smith"));
3051 labels
.push_back(ASCIIToUTF16("*Email"));
3052 names
.push_back(ASCIIToUTF16("email"));
3053 values
.push_back(ASCIIToUTF16("john@example.com"));
3056 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3060 " <SPAN>*</SPAN><B>First Name</B>"
3063 " <INPUT type='text' id='firstname' value='John'/>"
3068 " <SPAN>*</SPAN><B>Last Name</B>"
3071 " <INPUT type='text' id='lastname' value='Smith'/>"
3076 " <SPAN>*</SPAN><B>Email</B>"
3079 " <INPUT type='text' id='email' value='john@example.com'/>"
3084 " <INPUT type='submit' name='reply-send' value='Send'/>"
3089 labels
, names
, values
);
3092 // Verify that we correctly infer labels when the label text resides in the
3094 TEST_F(FormAutofillTest
, LabelsInferredFromTableRow
) {
3095 std::vector
<base::string16
> labels
, names
, values
;
3097 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
3098 names
.push_back(ASCIIToUTF16("firstname"));
3099 values
.push_back(ASCIIToUTF16("John"));
3101 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
3102 names
.push_back(ASCIIToUTF16("lastname"));
3103 values
.push_back(ASCIIToUTF16("Smith"));
3105 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
3106 names
.push_back(ASCIIToUTF16("email"));
3107 values
.push_back(ASCIIToUTF16("john@example.com"));
3110 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3113 " <TD>*First Name</TD>"
3114 " <TD>*Last Name</TD>"
3119 " <INPUT type='text' id='firstname' value='John'/>"
3122 " <INPUT type='text' id='lastname' value='Smith'/>"
3125 " <INPUT type='text' id='email' value='john@example.com'/>"
3130 " <INPUT type='submit' name='reply-send' value='Send'/>"
3134 labels
, names
, values
);
3137 // Verify that we correctly infer labels when enclosed within a list item.
3138 TEST_F(FormAutofillTest
, LabelsInferredFromListItem
) {
3139 std::vector
<base::string16
> labels
, names
, values
;
3141 labels
.push_back(ASCIIToUTF16("* Home Phone"));
3142 names
.push_back(ASCIIToUTF16("areacode"));
3143 values
.push_back(ASCIIToUTF16("415"));
3145 labels
.push_back(ASCIIToUTF16("* Home Phone"));
3146 names
.push_back(ASCIIToUTF16("prefix"));
3147 values
.push_back(ASCIIToUTF16("555"));
3149 labels
.push_back(ASCIIToUTF16("* Home Phone"));
3150 names
.push_back(ASCIIToUTF16("suffix"));
3151 values
.push_back(ASCIIToUTF16("1212"));
3154 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3157 " <SPAN>Bogus</SPAN>"
3160 " <LABEL><EM>*</EM> Home Phone</LABEL>"
3161 " <INPUT type='text' id='areacode' value='415'/>"
3162 " <INPUT type='text' id='prefix' value='555'/>"
3163 " <INPUT type='text' id='suffix' value='1212'/>"
3166 " <INPUT type='submit' name='reply-send' value='Send'/>"
3170 labels
, names
, values
);
3173 TEST_F(FormAutofillTest
, LabelsInferredFromDefinitionList
) {
3174 std::vector
<base::string16
> labels
, names
, values
;
3176 labels
.push_back(ASCIIToUTF16("* First name: Bogus"));
3177 names
.push_back(ASCIIToUTF16("firstname"));
3178 values
.push_back(ASCIIToUTF16("John"));
3180 labels
.push_back(ASCIIToUTF16("Last name:"));
3181 names
.push_back(ASCIIToUTF16("lastname"));
3182 values
.push_back(ASCIIToUTF16("Smith"));
3184 labels
.push_back(ASCIIToUTF16("Email:"));
3185 names
.push_back(ASCIIToUTF16("email"));
3186 values
.push_back(ASCIIToUTF16("john@example.com"));
3189 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3204 " <INPUT type='text' id='firstname' value='John'/>"
3214 " <INPUT type='text' id='lastname' value='Smith'/>"
3224 " <INPUT type='text' id='email' value='john@example.com'/>"
3229 " <INPUT type='submit' name='reply-send' value='Send'/>"
3233 labels
, names
, values
);
3236 TEST_F(FormAutofillTest
, LabelsInferredWithSameName
) {
3237 std::vector
<base::string16
> labels
, names
, values
;
3239 labels
.push_back(ASCIIToUTF16("Address Line 1:"));
3240 names
.push_back(ASCIIToUTF16("Address"));
3241 values
.push_back(base::string16());
3243 labels
.push_back(ASCIIToUTF16("Address Line 2:"));
3244 names
.push_back(ASCIIToUTF16("Address"));
3245 values
.push_back(base::string16());
3247 labels
.push_back(ASCIIToUTF16("Address Line 3:"));
3248 names
.push_back(ASCIIToUTF16("Address"));
3249 values
.push_back(base::string16());
3252 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3254 " <INPUT type='text' name='Address'/>"
3256 " <INPUT type='text' name='Address'/>"
3258 " <INPUT type='text' name='Address'/>"
3259 " <INPUT type='submit' name='reply-send' value='Send'/>"
3261 labels
, names
, values
);
3264 TEST_F(FormAutofillTest
, LabelsInferredWithImageTags
) {
3265 std::vector
<base::string16
> labels
, names
, values
;
3267 labels
.push_back(ASCIIToUTF16("Phone:"));
3268 names
.push_back(ASCIIToUTF16("dayphone1"));
3269 values
.push_back(base::string16());
3271 labels
.push_back(ASCIIToUTF16("-"));
3272 names
.push_back(ASCIIToUTF16("dayphone2"));
3273 values
.push_back(base::string16());
3275 labels
.push_back(ASCIIToUTF16("-"));
3276 names
.push_back(ASCIIToUTF16("dayphone3"));
3277 values
.push_back(base::string16());
3279 labels
.push_back(ASCIIToUTF16("ext.:"));
3280 names
.push_back(ASCIIToUTF16("dayphone4"));
3281 values
.push_back(base::string16());
3283 labels
.push_back(base::string16());
3284 names
.push_back(ASCIIToUTF16("dummy"));
3285 values
.push_back(base::string16());
3288 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3290 " <input type='text' name='dayphone1'>"
3294 " <input type='text' name='dayphone2'>"
3298 " <input type='text' name='dayphone3'>"
3300 " <input type='text' name='dayphone4'>"
3301 " <input type='text' name='dummy'>"
3302 " <input type='submit' name='reply-send' value='Send'>"
3304 labels
, names
, values
);
3307 TEST_F(FormAutofillTest
, LabelsInferredFromDivTable
) {
3308 ExpectJohnSmithLabels(
3309 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3310 "<DIV>First name:<BR>"
3312 " <INPUT type='text' name='firstname' value='John'>"
3315 "<DIV>Last name:<BR>"
3317 " <INPUT type='text' name='lastname' value='Smith'>"
3322 " <INPUT type='text' name='email' value='john@example.com'>"
3325 "<input type='submit' name='reply-send' value='Send'>"
3329 TEST_F(FormAutofillTest
, LabelsInferredFromDivSiblingTable
) {
3330 ExpectJohnSmithLabels(
3331 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3332 "<DIV>First name:</DIV>"
3335 " <INPUT type='text' name='firstname' value='John'>"
3338 "<DIV>Last name:</DIV>"
3341 " <INPUT type='text' name='lastname' value='Smith'>"
3347 " <INPUT type='text' name='email' value='john@example.com'>"
3350 "<input type='submit' name='reply-send' value='Send'>"
3354 TEST_F(FormAutofillTest
, LabelsInferredFromDefinitionListRatherThanDivTable
) {
3355 ExpectJohnSmithLabels(
3356 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3357 "<DIV>This is not a label.<BR>"
3366 " <INPUT type='text' id='firstname' value='John'/>"
3376 " <INPUT type='text' id='lastname' value='Smith'/>"
3386 " <INPUT type='text' id='email' value='john@example.com'/>"
3391 " <INPUT type='submit' name='reply-send' value='Send'/>"
3398 TEST_F(FormAutofillTest
, FillFormMaxLength
) {
3399 TestFillFormMaxLength(
3400 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3401 " <INPUT type='text' id='firstname' maxlength='5'/>"
3402 " <INPUT type='text' id='lastname' maxlength='7'/>"
3403 " <INPUT type='text' id='email' maxlength='9'/>"
3404 " <INPUT type='submit' name='reply-send' value='Send'/>"
3409 TEST_F(FormAutofillTest
, FillFormMaxLengthForUnownedForm
) {
3410 TestFillFormMaxLength(
3411 "<INPUT type='text' id='firstname' maxlength='5'/>"
3412 "<INPUT type='text' id='lastname' maxlength='7'/>"
3413 "<INPUT type='text' id='email' maxlength='9'/>"
3414 "<INPUT type='submit' name='reply-send' value='Send'/>",
3418 // This test uses negative values of the maxlength attribute for input elements.
3419 // In this case, the maxlength of the input elements is set to the default
3420 // maxlength (defined in WebKit.)
3421 TEST_F(FormAutofillTest
, FillFormNegativeMaxLength
) {
3422 TestFillFormNegativeMaxLength(
3423 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3424 " <INPUT type='text' id='firstname' maxlength='-1'/>"
3425 " <INPUT type='text' id='lastname' maxlength='-10'/>"
3426 " <INPUT type='text' id='email' maxlength='-13'/>"
3427 " <INPUT type='submit' name='reply-send' value='Send'/>"
3432 TEST_F(FormAutofillTest
, FillFormNegativeMaxLengthForUnownedForm
) {
3433 TestFillFormNegativeMaxLength(
3434 "<INPUT type='text' id='firstname' maxlength='-1'/>"
3435 "<INPUT type='text' id='lastname' maxlength='-10'/>"
3436 "<INPUT type='text' id='email' maxlength='-13'/>"
3437 "<INPUT type='submit' name='reply-send' value='Send'/>",
3441 TEST_F(FormAutofillTest
, FillFormEmptyName
) {
3442 TestFillFormEmptyName(
3443 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3444 " <INPUT type='text' id='firstname'/>"
3445 " <INPUT type='text' id='lastname'/>"
3446 " <INPUT type='text' id='email'/>"
3447 " <INPUT type='submit' value='Send'/>"
3452 TEST_F(FormAutofillTest
, FillFormEmptyNameForUnownedForm
) {
3453 TestFillFormEmptyName(
3454 "<INPUT type='text' id='firstname'/>"
3455 "<INPUT type='text' id='lastname'/>"
3456 "<INPUT type='text' id='email'/>"
3457 "<INPUT type='submit' value='Send'/>",
3461 TEST_F(FormAutofillTest
, FillFormEmptyFormNames
) {
3462 TestFillFormEmptyFormNames(
3463 "<FORM action='http://buh.com' method='post'>"
3464 " <INPUT type='text' id='firstname'/>"
3465 " <INPUT type='text' id='middlename'/>"
3466 " <INPUT type='text' id='lastname'/>"
3467 " <INPUT type='submit' value='Send'/>"
3469 "<FORM action='http://abc.com' method='post'>"
3470 " <INPUT type='text' id='apple'/>"
3471 " <INPUT type='text' id='banana'/>"
3472 " <INPUT type='text' id='cantelope'/>"
3473 " <INPUT type='submit' value='Send'/>"
3478 TEST_F(FormAutofillTest
, FillFormEmptyFormNamesForUnownedForm
) {
3479 TestFillFormEmptyFormNames(
3480 "<INPUT type='text' id='firstname'/>"
3481 "<INPUT type='text' id='middlename'/>"
3482 "<INPUT type='text' id='lastname'/>"
3483 "<INPUT type='text' id='apple'/>"
3484 "<INPUT type='text' id='banana'/>"
3485 "<INPUT type='text' id='cantelope'/>"
3486 "<INPUT type='submit' value='Send'/>",
3490 TEST_F(FormAutofillTest
, ThreePartPhone
) {
3491 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3493 " <input type='text' name='dayphone1'>"
3495 " <input type='text' name='dayphone2'>"
3497 " <input type='text' name='dayphone3'>"
3499 " <input type='text' name='dayphone4'>"
3500 " <input type='submit' name='reply-send' value='Send'>"
3504 WebFrame
* frame
= GetMainFrame();
3505 ASSERT_NE(nullptr, frame
);
3507 WebVector
<WebFormElement
> forms
;
3508 frame
->document().forms(forms
);
3509 ASSERT_EQ(1U, forms
.size());
3512 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
3513 WebFormControlElement(),
3518 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
3519 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
3520 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
3522 const std::vector
<FormFieldData
>& fields
= form
.fields
;
3523 ASSERT_EQ(4U, fields
.size());
3525 FormFieldData expected
;
3526 expected
.form_control_type
= "text";
3527 expected
.max_length
= WebInputElement::defaultMaxLength();
3529 expected
.label
= ASCIIToUTF16("Phone:");
3530 expected
.name
= ASCIIToUTF16("dayphone1");
3531 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
3533 expected
.label
= ASCIIToUTF16("-");
3534 expected
.name
= ASCIIToUTF16("dayphone2");
3535 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
3537 expected
.label
= ASCIIToUTF16("-");
3538 expected
.name
= ASCIIToUTF16("dayphone3");
3539 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
3541 expected
.label
= ASCIIToUTF16("ext.:");
3542 expected
.name
= ASCIIToUTF16("dayphone4");
3543 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
3547 TEST_F(FormAutofillTest
, MaxLengthFields
) {
3548 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3550 " <input type='text' maxlength='3' name='dayphone1'>"
3552 " <input type='text' maxlength='3' name='dayphone2'>"
3554 " <input type='text' maxlength='4' size='5'"
3555 " name='dayphone3'>"
3557 " <input type='text' maxlength='5' name='dayphone4'>"
3558 " <input type='text' name='default1'>"
3559 " <input type='text' maxlength='-1' name='invalid1'>"
3560 " <input type='submit' name='reply-send' value='Send'>"
3563 WebFrame
* frame
= GetMainFrame();
3564 ASSERT_NE(nullptr, frame
);
3566 WebVector
<WebFormElement
> forms
;
3567 frame
->document().forms(forms
);
3568 ASSERT_EQ(1U, forms
.size());
3571 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
3572 WebFormControlElement(),
3577 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
3578 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
3579 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
3581 const std::vector
<FormFieldData
>& fields
= form
.fields
;
3582 ASSERT_EQ(6U, fields
.size());
3584 FormFieldData expected
;
3585 expected
.form_control_type
= "text";
3587 expected
.label
= ASCIIToUTF16("Phone:");
3588 expected
.name
= ASCIIToUTF16("dayphone1");
3589 expected
.max_length
= 3;
3590 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
3592 expected
.label
= ASCIIToUTF16("-");
3593 expected
.name
= ASCIIToUTF16("dayphone2");
3594 expected
.max_length
= 3;
3595 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
3597 expected
.label
= ASCIIToUTF16("-");
3598 expected
.name
= ASCIIToUTF16("dayphone3");
3599 expected
.max_length
= 4;
3600 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
3602 expected
.label
= ASCIIToUTF16("ext.:");
3603 expected
.name
= ASCIIToUTF16("dayphone4");
3604 expected
.max_length
= 5;
3605 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
3607 // When unspecified |size|, default is returned.
3608 expected
.label
.clear();
3609 expected
.name
= ASCIIToUTF16("default1");
3610 expected
.max_length
= WebInputElement::defaultMaxLength();
3611 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[4]);
3613 // When invalid |size|, default is returned.
3614 expected
.label
.clear();
3615 expected
.name
= ASCIIToUTF16("invalid1");
3616 expected
.max_length
= WebInputElement::defaultMaxLength();
3617 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[5]);
3620 // This test re-creates the experience of typing in a field then selecting a
3621 // profile from the Autofill suggestions popup. The field that is being typed
3622 // into should be filled even though it's not technically empty.
3623 TEST_F(FormAutofillTest
, FillFormNonEmptyField
) {
3624 TestFillFormNonEmptyField(
3625 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3626 " <INPUT type='text' id='firstname'/>"
3627 " <INPUT type='text' id='lastname'/>"
3628 " <INPUT type='text' id='email'/>"
3629 " <INPUT type='submit' value='Send'/>"
3634 TEST_F(FormAutofillTest
, FillFormNonEmptyFieldForUnownedForm
) {
3635 TestFillFormNonEmptyField("<INPUT type='text' id='firstname'/>"
3636 "<INPUT type='text' id='lastname'/>"
3637 "<INPUT type='text' id='email'/>"
3638 "<INPUT type='submit' value='Send'/>",
3642 TEST_F(FormAutofillTest
, ClearFormWithNode
) {
3643 TestClearFormWithNode(
3644 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3645 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3646 " <INPUT type='text' id='lastname' value='Earp'/>"
3647 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3648 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3649 " <INPUT type='month' id='month' value='2012-11'>"
3650 " <INPUT type='month' id='month-disabled' value='2012-11'"
3651 " disabled='disabled'>"
3652 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3653 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3656 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3657 " <INPUT type='submit' value='Send'/>"
3662 TEST_F(FormAutofillTest
, ClearFormWithNodeForUnownedForm
) {
3663 TestClearFormWithNode(
3664 " <!-- Indented on purpose //-->"
3665 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3666 " <INPUT type='text' id='lastname' value='Earp'/>"
3667 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3668 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3669 " <INPUT type='month' id='month' value='2012-11'>"
3670 " <INPUT type='month' id='month-disabled' value='2012-11'"
3671 " disabled='disabled'>"
3672 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3673 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3676 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3677 " <INPUT type='submit' value='Send'/>",
3681 TEST_F(FormAutofillTest
, ClearFormWithNodeContainingSelectOne
) {
3682 TestClearFormWithNodeContainingSelectOne(
3683 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3684 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3685 " <INPUT type='text' id='lastname' value='Earp'/>"
3686 " <SELECT id='state' name='state'>"
3687 " <OPTION selected>?</OPTION>"
3688 " <OPTION>AA</OPTION>"
3689 " <OPTION>AE</OPTION>"
3690 " <OPTION>AK</OPTION>"
3692 " <INPUT type='submit' value='Send'/>"
3697 TEST_F(FormAutofillTest
, ClearFormWithNodeContainingSelectOneForUnownedForm
) {
3698 TestClearFormWithNodeContainingSelectOne(
3699 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3700 "<INPUT type='text' id='lastname' value='Earp'/>"
3701 "<SELECT id='state' name='state'>"
3702 " <OPTION selected>?</OPTION>"
3703 " <OPTION>AA</OPTION>"
3704 " <OPTION>AE</OPTION>"
3705 " <OPTION>AK</OPTION>"
3707 "<INPUT type='submit' value='Send'/>",
3711 TEST_F(FormAutofillTest
, ClearPreviewedFormWithElement
) {
3712 TestClearPreviewedFormWithElement(
3713 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3714 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3715 " <INPUT type='text' id='lastname'/>"
3716 " <INPUT type='text' id='email'/>"
3717 " <INPUT type='email' id='email2'/>"
3718 " <INPUT type='tel' id='phone'/>"
3719 " <INPUT type='submit' value='Send'/>"
3723 TEST_F(FormAutofillTest
, ClearPreviewedFormWithElementForUnownedForm
) {
3724 TestClearPreviewedFormWithElement(
3725 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3726 "<INPUT type='text' id='lastname'/>"
3727 "<INPUT type='text' id='email'/>"
3728 "<INPUT type='email' id='email2'/>"
3729 "<INPUT type='tel' id='phone'/>"
3730 "<INPUT type='submit' value='Send'/>");
3733 TEST_F(FormAutofillTest
, ClearPreviewedFormWithNonEmptyInitiatingNode
) {
3734 TestClearPreviewedFormWithNonEmptyInitiatingNode(
3735 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3736 " <INPUT type='text' id='firstname' value='W'/>"
3737 " <INPUT type='text' id='lastname'/>"
3738 " <INPUT type='text' id='email'/>"
3739 " <INPUT type='email' id='email2'/>"
3740 " <INPUT type='tel' id='phone'/>"
3741 " <INPUT type='submit' value='Send'/>"
3745 TEST_F(FormAutofillTest
,
3746 ClearPreviewedFormWithNonEmptyInitiatingNodeForUnownedForm
) {
3747 TestClearPreviewedFormWithNonEmptyInitiatingNode(
3748 "<INPUT type='text' id='firstname' value='W'/>"
3749 "<INPUT type='text' id='lastname'/>"
3750 "<INPUT type='text' id='email'/>"
3751 "<INPUT type='email' id='email2'/>"
3752 "<INPUT type='tel' id='phone'/>"
3753 "<INPUT type='submit' value='Send'/>");
3756 TEST_F(FormAutofillTest
, ClearPreviewedFormWithAutofilledInitiatingNode
) {
3757 TestClearPreviewedFormWithAutofilledInitiatingNode(
3758 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3759 " <INPUT type='text' id='firstname' value='W'/>"
3760 " <INPUT type='text' id='lastname'/>"
3761 " <INPUT type='text' id='email'/>"
3762 " <INPUT type='email' id='email2'/>"
3763 " <INPUT type='tel' id='phone'/>"
3764 " <INPUT type='submit' value='Send'/>"
3768 TEST_F(FormAutofillTest
,
3769 ClearPreviewedFormWithAutofilledInitiatingNodeForUnownedForm
) {
3770 TestClearPreviewedFormWithAutofilledInitiatingNode(
3771 "<INPUT type='text' id='firstname' value='W'/>"
3772 "<INPUT type='text' id='lastname'/>"
3773 "<INPUT type='text' id='email'/>"
3774 "<INPUT type='email' id='email2'/>"
3775 "<INPUT type='tel' id='phone'/>"
3776 "<INPUT type='submit' value='Send'/>");
3779 // Autofill's "Clear Form" should clear only autofilled fields
3780 TEST_F(FormAutofillTest
, ClearOnlyAutofilledFields
) {
3781 TestClearOnlyAutofilledFields(
3782 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3783 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3784 " <INPUT type='text' id='lastname' value='Earp'/>"
3785 " <INPUT type='email' id='email' value='wyatt@earp.com'/>"
3786 " <INPUT type='tel' id='phone' value='650-777-9999'/>"
3787 " <INPUT type='submit' value='Send'/>"
3791 TEST_F(FormAutofillTest
, ClearOnlyAutofilledFieldsForUnownedForm
) {
3792 TestClearOnlyAutofilledFields(
3793 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3794 "<INPUT type='text' id='lastname' value='Earp'/>"
3795 "<INPUT type='email' id='email' value='wyatt@earp.com'/>"
3796 "<INPUT type='tel' id='phone' value='650-777-9999'/>"
3797 "<INPUT type='submit' value='Send'/>");
3800 // If we have multiple labels per id, the labels concatenated into label string.
3801 TEST_F(FormAutofillTest
, MultipleLabelsPerElement
) {
3802 std::vector
<base::string16
> labels
, names
, values
;
3804 labels
.push_back(ASCIIToUTF16("First Name:"));
3805 names
.push_back(ASCIIToUTF16("firstname"));
3806 values
.push_back(ASCIIToUTF16("John"));
3808 labels
.push_back(ASCIIToUTF16("Last Name:"));
3809 names
.push_back(ASCIIToUTF16("lastname"));
3810 values
.push_back(ASCIIToUTF16("Smith"));
3812 labels
.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
3813 names
.push_back(ASCIIToUTF16("email"));
3814 values
.push_back(ASCIIToUTF16("john@example.com"));
3817 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3818 " <LABEL for='firstname'> First Name: </LABEL>"
3819 " <LABEL for='firstname'></LABEL>"
3820 " <INPUT type='text' id='firstname' value='John'/>"
3821 " <LABEL for='lastname'></LABEL>"
3822 " <LABEL for='lastname'> Last Name: </LABEL>"
3823 " <INPUT type='text' id='lastname' value='Smith'/>"
3824 " <LABEL for='email'> Email: </LABEL>"
3825 " <LABEL for='email'> xxx@yyy.com </LABEL>"
3826 " <INPUT type='text' id='email' value='john@example.com'/>"
3827 " <INPUT type='submit' name='reply-send' value='Send'/>"
3829 labels
, names
, values
);
3832 TEST_F(FormAutofillTest
, ClickElement
) {
3833 LoadHTML("<BUTTON id='link'>Button</BUTTON>"
3834 "<BUTTON name='button'>Button</BUTTON>");
3835 WebFrame
* frame
= GetMainFrame();
3836 ASSERT_NE(nullptr, frame
);
3838 // Successful retrieval by id.
3839 WebElementDescriptor clicker
;
3840 clicker
.retrieval_method
= WebElementDescriptor::ID
;
3841 clicker
.descriptor
= "link";
3842 EXPECT_TRUE(ClickElement(frame
->document(), clicker
));
3844 // Successful retrieval by css selector.
3845 clicker
.retrieval_method
= WebElementDescriptor::CSS_SELECTOR
;
3846 clicker
.descriptor
= "button[name='button']";
3847 EXPECT_TRUE(ClickElement(frame
->document(), clicker
));
3849 // Unsuccessful retrieval due to invalid CSS selector.
3850 clicker
.descriptor
= "^*&";
3851 EXPECT_FALSE(ClickElement(frame
->document(), clicker
));
3853 // Unsuccessful retrieval because element does not exist.
3854 clicker
.descriptor
= "#junk";
3855 EXPECT_FALSE(ClickElement(frame
->document(), clicker
));
3858 TEST_F(FormAutofillTest
, SelectOneAsText
) {
3859 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3860 " <INPUT type='text' id='firstname' value='John'/>"
3861 " <INPUT type='text' id='lastname' value='Smith'/>"
3862 " <SELECT id='country'>"
3863 " <OPTION value='AF'>Afghanistan</OPTION>"
3864 " <OPTION value='AL'>Albania</OPTION>"
3865 " <OPTION value='DZ'>Algeria</OPTION>"
3867 " <INPUT type='submit' name='reply-send' value='Send'/>"
3870 WebFrame
* frame
= GetMainFrame();
3871 ASSERT_NE(nullptr, frame
);
3873 // Set the value of the select-one.
3874 WebSelectElement select_element
=
3875 frame
->document().getElementById("country").to
<WebSelectElement
>();
3876 select_element
.setValue(WebString::fromUTF8("AL"));
3878 WebVector
<WebFormElement
> forms
;
3879 frame
->document().forms(forms
);
3880 ASSERT_EQ(1U, forms
.size());
3884 // Extract the country select-one value as text.
3885 EXPECT_TRUE(WebFormElementToFormData(
3886 forms
[0], WebFormControlElement(), REQUIRE_NONE
,
3887 static_cast<ExtractMask
>(EXTRACT_VALUE
| EXTRACT_OPTION_TEXT
),
3889 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
3890 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
3891 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
3893 const std::vector
<FormFieldData
>& fields
= form
.fields
;
3894 ASSERT_EQ(3U, fields
.size());
3896 FormFieldData expected
;
3898 expected
.name
= ASCIIToUTF16("firstname");
3899 expected
.value
= ASCIIToUTF16("John");
3900 expected
.form_control_type
= "text";
3901 expected
.max_length
= WebInputElement::defaultMaxLength();
3902 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
3904 expected
.name
= ASCIIToUTF16("lastname");
3905 expected
.value
= ASCIIToUTF16("Smith");
3906 expected
.form_control_type
= "text";
3907 expected
.max_length
= WebInputElement::defaultMaxLength();
3908 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
3910 expected
.name
= ASCIIToUTF16("country");
3911 expected
.value
= ASCIIToUTF16("Albania");
3912 expected
.form_control_type
= "select-one";
3913 expected
.max_length
= 0;
3914 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
3916 form
.fields
.clear();
3917 // Extract the country select-one value as value.
3918 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
3919 WebFormControlElement(),
3924 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
3925 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
3926 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
3928 ASSERT_EQ(3U, fields
.size());
3930 expected
.name
= ASCIIToUTF16("firstname");
3931 expected
.value
= ASCIIToUTF16("John");
3932 expected
.form_control_type
= "text";
3933 expected
.max_length
= WebInputElement::defaultMaxLength();
3934 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
3936 expected
.name
= ASCIIToUTF16("lastname");
3937 expected
.value
= ASCIIToUTF16("Smith");
3938 expected
.form_control_type
= "text";
3939 expected
.max_length
= WebInputElement::defaultMaxLength();
3940 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
3942 expected
.name
= ASCIIToUTF16("country");
3943 expected
.value
= ASCIIToUTF16("AL");
3944 expected
.form_control_type
= "select-one";
3945 expected
.max_length
= 0;
3946 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
3949 TEST_F(FormAutofillTest
,
3950 UnownedFormElementsAndFieldSetsToFormDataFieldsets
) {
3951 std::vector
<WebElement
> fieldsets
;
3952 std::vector
<WebFormControlElement
> control_elements
;
3954 const ExtractMask extract_mask
=
3955 static_cast<ExtractMask
>(EXTRACT_VALUE
| EXTRACT_OPTIONS
);
3956 const GURL
dummy_origin("http://www.example.com");
3960 " <LABEL for='firstname'>First name:</LABEL>"
3961 " <LABEL for='lastname'>Last name:</LABEL>"
3962 " <INPUT type='text' id='firstname' value='John'/>"
3963 " <INPUT type='text' id='lastname' value='Smith'/>"
3966 " <LABEL for='email'>Email:</LABEL>"
3967 " <INPUT type='text' id='email' value='john@example.com'/>"
3971 WebFrame
* frame
= GetMainFrame();
3972 ASSERT_NE(nullptr, frame
);
3974 control_elements
= GetUnownedAutofillableFormFieldElements(
3975 frame
->document().all(), &fieldsets
);
3976 ASSERT_EQ(3U, control_elements
.size());
3977 ASSERT_EQ(2U, fieldsets
.size());
3980 EXPECT_TRUE(UnownedFormElementsAndFieldSetsToFormData(
3981 fieldsets
, control_elements
, nullptr, dummy_origin
, REQUIRE_NONE
,
3982 extract_mask
, &form
, nullptr));
3984 EXPECT_TRUE(form
.name
.empty());
3985 EXPECT_EQ(dummy_origin
, form
.origin
);
3986 EXPECT_FALSE(form
.action
.is_valid());
3988 const std::vector
<FormFieldData
>& fields
= form
.fields
;
3989 ASSERT_EQ(3U, fields
.size());
3991 FormFieldData expected
;
3992 expected
.form_control_type
= "text";
3993 expected
.max_length
= WebInputElement::defaultMaxLength();
3995 expected
.name
= ASCIIToUTF16("firstname");
3996 expected
.value
= ASCIIToUTF16("John");
3997 expected
.label
= ASCIIToUTF16("First name:");
3998 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
4000 expected
.name
= ASCIIToUTF16("lastname");
4001 expected
.value
= ASCIIToUTF16("Smith");
4002 expected
.label
= ASCIIToUTF16("Last name:");
4003 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
4005 expected
.name
= ASCIIToUTF16("email");
4006 expected
.value
= ASCIIToUTF16("john@example.com");
4007 expected
.label
= ASCIIToUTF16("Email:");
4008 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
4011 TEST_F(FormAutofillTest
,
4012 UnownedFormElementsAndFieldSetsToFormDataControlOutsideOfFieldset
) {
4013 std::vector
<WebElement
> fieldsets
;
4014 std::vector
<WebFormControlElement
> control_elements
;
4016 const ExtractMask extract_mask
=
4017 static_cast<ExtractMask
>(EXTRACT_VALUE
| EXTRACT_OPTIONS
);
4018 const GURL
dummy_origin("http://www.example.com");
4022 " <LABEL for='firstname'>First name:</LABEL>"
4023 " <LABEL for='lastname'>Last name:</LABEL>"
4024 " <INPUT type='text' id='firstname' value='John'/>"
4025 " <INPUT type='text' id='lastname' value='Smith'/>"
4026 " <LABEL for='email'>Email:</LABEL>"
4028 " <INPUT type='text' id='email' value='john@example.com'/>"
4031 WebFrame
* frame
= GetMainFrame();
4032 ASSERT_NE(nullptr, frame
);
4034 control_elements
= GetUnownedAutofillableFormFieldElements(
4035 frame
->document().all(), &fieldsets
);
4036 ASSERT_EQ(3U, control_elements
.size());
4037 ASSERT_EQ(1U, fieldsets
.size());
4040 EXPECT_TRUE(UnownedFormElementsAndFieldSetsToFormData(
4041 fieldsets
, control_elements
, nullptr, dummy_origin
, REQUIRE_NONE
,
4042 extract_mask
, &form
, nullptr));
4044 EXPECT_TRUE(form
.name
.empty());
4045 EXPECT_EQ(dummy_origin
, form
.origin
);
4046 EXPECT_FALSE(form
.action
.is_valid());
4048 const std::vector
<FormFieldData
>& fields
= form
.fields
;
4049 ASSERT_EQ(3U, fields
.size());
4051 FormFieldData expected
;
4052 expected
.form_control_type
= "text";
4053 expected
.max_length
= WebInputElement::defaultMaxLength();
4055 expected
.name
= ASCIIToUTF16("firstname");
4056 expected
.value
= ASCIIToUTF16("John");
4057 expected
.label
= ASCIIToUTF16("First name:");
4058 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
4060 expected
.name
= ASCIIToUTF16("lastname");
4061 expected
.value
= ASCIIToUTF16("Smith");
4062 expected
.label
= ASCIIToUTF16("Last name:");
4063 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
4065 expected
.name
= ASCIIToUTF16("email");
4066 expected
.value
= ASCIIToUTF16("john@example.com");
4067 expected
.label
= ASCIIToUTF16("Email:");
4068 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
4071 TEST_F(FormAutofillTest
, UnownedFormElementsAndFieldSetsToFormDataWithForm
) {
4072 std::vector
<WebElement
> fieldsets
;
4073 std::vector
<WebFormControlElement
> control_elements
;
4075 const ExtractMask extract_mask
=
4076 static_cast<ExtractMask
>(EXTRACT_VALUE
| EXTRACT_OPTIONS
);
4077 const GURL
dummy_origin("http://www.example.com");
4079 LoadHTML(kFormHtml
);
4081 WebFrame
* frame
= GetMainFrame();
4082 ASSERT_NE(nullptr, frame
);
4084 control_elements
= GetUnownedAutofillableFormFieldElements(
4085 frame
->document().all(), &fieldsets
);
4086 ASSERT_TRUE(control_elements
.empty());
4087 ASSERT_TRUE(fieldsets
.empty());
4090 EXPECT_FALSE(UnownedFormElementsAndFieldSetsToFormData(
4091 fieldsets
, control_elements
, nullptr, dummy_origin
, REQUIRE_NONE
,
4092 extract_mask
, &form
, nullptr));
4095 } // namespace autofill