1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/format_macros.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/common/metrics/entropy_provider.h"
14 #include "chrome/test/base/chrome_render_view_test.h"
15 #include "components/autofill/content/renderer/form_autofill_util.h"
16 #include "components/autofill/content/renderer/form_cache.h"
17 #include "components/autofill/core/common/form_data.h"
18 #include "components/autofill/core/common/web_element_descriptor.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebVector.h"
22 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebElement.h"
24 #include "third_party/WebKit/public/web/WebFormControlElement.h"
25 #include "third_party/WebKit/public/web/WebFormElement.h"
26 #include "third_party/WebKit/public/web/WebInputElement.h"
27 #include "third_party/WebKit/public/web/WebNode.h"
28 #include "third_party/WebKit/public/web/WebSelectElement.h"
30 using WebKit::WebDocument
;
31 using WebKit::WebElement
;
32 using WebKit::WebFormControlElement
;
33 using WebKit::WebFormElement
;
34 using WebKit::WebFrame
;
35 using WebKit::WebInputElement
;
36 using WebKit::WebSelectElement
;
37 using WebKit::WebNode
;
38 using WebKit::WebString
;
39 using WebKit::WebVector
;
43 struct AutofillFieldCase
{
44 const char* const name
;
45 const char* const initial_value
;
46 const char* const autocomplete_attribute
; // The autocomplete attribute of
48 bool should_be_autofilled
; // Whether the filed should be autofilled.
49 const char* const autofill_value
; // The value being used to fill the field.
50 const char* const expected_value
; // The expected value after Autofill
54 static const char kFormHtml
[] =
55 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
56 " <INPUT type=\"text\" id=\"firstname\"/>"
57 " <INPUT type=\"text\" id=\"lastname\"/>"
58 " <INPUT type=\"hidden\" id=\"imhidden\"/>"
59 " <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>"
60 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>"
61 " <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>"
62 " <INPUT type=\"text\" readonly id=\"readonly\"/>"
63 " <INPUT type=\"text\" style=\"visibility: hidden\""
65 " <INPUT type=\"text\" style=\"display: none\" id=\"displaynone\"/>"
66 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
73 class FormAutofillTest
: public ChromeRenderViewTest
{
75 FormAutofillTest() : ChromeRenderViewTest() {}
76 virtual ~FormAutofillTest() {}
78 void ExpectLabels(const char* html
,
79 const std::vector
<string16
>& labels
,
80 const std::vector
<string16
>& names
,
81 const std::vector
<string16
>& values
) {
82 std::vector
<std::string
> control_types(labels
.size(), "text");
83 ExpectLabelsAndTypes(html
, labels
, names
, values
, control_types
);
86 void ExpectLabelsAndTypes(const char* html
,
87 const std::vector
<string16
>& labels
,
88 const std::vector
<string16
>& names
,
89 const std::vector
<string16
>& values
,
90 const std::vector
<std::string
>& control_types
) {
91 ASSERT_EQ(labels
.size(), names
.size());
92 ASSERT_EQ(labels
.size(), values
.size());
93 ASSERT_EQ(labels
.size(), control_types
.size());
97 WebFrame
* web_frame
= GetMainFrame();
98 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
100 FormCache form_cache
;
101 std::vector
<FormData
> forms
;
102 form_cache
.ExtractForms(*web_frame
, &forms
);
103 ASSERT_EQ(1U, forms
.size());
105 const FormData
& form
= forms
[0];
106 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
107 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
108 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
110 const std::vector
<FormFieldData
>& fields
= form
.fields
;
111 ASSERT_EQ(labels
.size(), fields
.size());
112 for (size_t i
= 0; i
< labels
.size(); ++i
) {
113 int max_length
= control_types
[i
] == "text" ?
114 WebInputElement::defaultMaxLength() : 0;
115 FormFieldData expected
;
116 expected
.label
= labels
[i
];
117 expected
.name
= names
[i
];
118 expected
.value
= values
[i
];
119 expected
.form_control_type
= control_types
[i
];
120 expected
.max_length
= max_length
;
121 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS
, i
));
122 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[i
]);
126 void ExpectJohnSmithLabels(const char* html
) {
127 std::vector
<string16
> labels
, names
, values
;
129 labels
.push_back(ASCIIToUTF16("First name:"));
130 names
.push_back(ASCIIToUTF16("firstname"));
131 values
.push_back(ASCIIToUTF16("John"));
133 labels
.push_back(ASCIIToUTF16("Last name:"));
134 names
.push_back(ASCIIToUTF16("lastname"));
135 values
.push_back(ASCIIToUTF16("Smith"));
137 labels
.push_back(ASCIIToUTF16("Email:"));
138 names
.push_back(ASCIIToUTF16("email"));
139 values
.push_back(ASCIIToUTF16("john@example.com"));
141 ExpectLabels(html
, labels
, names
, values
);
144 typedef void (*FillFormFunction
)(const FormData
& form
,
145 const WebInputElement
& element
);
147 typedef WebString (WebInputElement::*GetValueFunction
)(void) const;
149 // Test FormFillxxx functions.
150 void TestFormFillFunctions(const char* html
,
151 const AutofillFieldCase
* field_cases
,
152 size_t number_of_field_cases
,
153 FillFormFunction fill_form_function
,
154 GetValueFunction get_value_function
) {
157 WebFrame
* web_frame
= GetMainFrame();
158 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
160 FormCache form_cache
;
161 std::vector
<FormData
> forms
;
162 form_cache
.ExtractForms(*web_frame
, &forms
);
163 ASSERT_EQ(1U, forms
.size());
165 // Get the input element we want to find.
166 WebElement element
= web_frame
->document().getElementById("firstname");
167 WebInputElement input_element
= element
.to
<WebInputElement
>();
169 // Find the form that contains the input element.
173 FindFormAndFieldForInputElement(input_element
,
176 autofill::REQUIRE_AUTOCOMPLETE
));
177 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data
.name
);
178 EXPECT_EQ(GURL(web_frame
->document().url()), form_data
.origin
);
179 EXPECT_EQ(GURL("http://buh.com"), form_data
.action
);
181 const std::vector
<FormFieldData
>& fields
= form_data
.fields
;
182 ASSERT_EQ(number_of_field_cases
, fields
.size());
184 FormFieldData expected
;
185 expected
.form_control_type
= "text";
186 expected
.max_length
= WebInputElement::defaultMaxLength();
188 // Verify field's initial value.
189 for (size_t i
= 0; i
< number_of_field_cases
; ++i
) {
190 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
191 field_cases
[i
].name
));
192 expected
.name
= ASCIIToUTF16(field_cases
[i
].name
);
193 expected
.value
= ASCIIToUTF16(field_cases
[i
].initial_value
);
194 expected
.autocomplete_attribute
= field_cases
[i
].autocomplete_attribute
;
195 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[i
]);
196 // Fill the form_data for the field.
197 form_data
.fields
[i
].value
= ASCIIToUTF16(field_cases
[i
].autofill_value
);
200 // Autofill the form using the given fill form function.
201 fill_form_function(form_data
, input_element
);
203 // Validate Autofill or Preview results.
204 for (size_t i
= 0; i
< number_of_field_cases
; ++i
) {
205 ValidteFilledField(field_cases
[i
], get_value_function
);
209 // Validate an Autofilled field.
210 void ValidteFilledField(const AutofillFieldCase
& field_case
,
211 GetValueFunction get_value_function
) {
212 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
214 WebInputElement input_element
= GetMainFrame()->document().getElementById(
215 ASCIIToUTF16(field_case
.name
)).to
<WebInputElement
>();
216 EXPECT_EQ(field_case
.should_be_autofilled
, input_element
.isAutofilled());
217 if (field_case
.should_be_autofilled
) {
218 EXPECT_EQ(ASCIIToUTF16(field_case
.expected_value
),
219 (input_element
.*get_value_function
)());
221 WebString expected_value
= ASCIIToUTF16(field_case
.expected_value
);
222 if (expected_value
.isEmpty())
223 EXPECT_TRUE((input_element
.*get_value_function
)().isEmpty());
225 EXPECT_EQ(expected_value
, (input_element
.*get_value_function
)());
229 static void FillFormForAllFieldsWrapper(const FormData
& form
,
230 const WebInputElement
& element
) {
231 FillFormForAllElements(form
, element
.form());
234 static void FillFormIncludingNonFocusableElementsWrapper(
235 const FormData
& form
,
236 const WebInputElement
& element
) {
237 FillFormIncludingNonFocusableElements(form
, element
.form());
241 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest
);
244 // We should be able to extract a normal text field.
245 TEST_F(FormAutofillTest
, WebFormControlElementToFormField
) {
246 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>");
248 WebFrame
* frame
= GetMainFrame();
249 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
251 WebElement web_element
= frame
->document().getElementById("element");
252 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
253 FormFieldData result1
;
254 WebFormControlElementToFormField(element
, autofill::EXTRACT_NONE
, &result1
);
256 FormFieldData expected
;
257 expected
.form_control_type
= "text";
258 expected
.max_length
= WebInputElement::defaultMaxLength();
260 expected
.name
= ASCIIToUTF16("element");
261 expected
.value
= string16();
262 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result1
);
264 FormFieldData result2
;
265 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result2
);
267 expected
.name
= ASCIIToUTF16("element");
268 expected
.value
= ASCIIToUTF16("value");
269 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result2
);
272 // We should be able to extract a text field with autocomplete="off".
273 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutocompleteOff
) {
274 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\""
275 " autocomplete=\"off\"/>");
277 WebFrame
* frame
= GetMainFrame();
278 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
280 WebElement web_element
= frame
->document().getElementById("element");
281 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
282 FormFieldData result
;
283 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
285 FormFieldData expected
;
286 expected
.name
= ASCIIToUTF16("element");
287 expected
.value
= ASCIIToUTF16("value");
288 expected
.form_control_type
= "text";
289 expected
.autocomplete_attribute
= "off";
290 expected
.max_length
= WebInputElement::defaultMaxLength();
291 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
294 // We should be able to extract a text field with maxlength specified.
295 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldMaxLength
) {
296 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\""
297 " maxlength=\"5\"/>");
299 WebFrame
* frame
= GetMainFrame();
300 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
302 WebElement web_element
= frame
->document().getElementById("element");
303 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
304 FormFieldData result
;
305 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
307 FormFieldData expected
;
308 expected
.name
= ASCIIToUTF16("element");
309 expected
.value
= ASCIIToUTF16("value");
310 expected
.form_control_type
= "text";
311 expected
.max_length
= 5;
312 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
315 // We should be able to extract a text field that has been autofilled.
316 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutofilled
) {
317 LoadHTML("<INPUT type=\"text\" id=\"element\" value=\"value\"/>");
319 WebFrame
* frame
= GetMainFrame();
320 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
322 WebElement web_element
= frame
->document().getElementById("element");
323 WebInputElement element
= web_element
.to
<WebInputElement
>();
324 element
.setAutofilled(true);
325 FormFieldData result
;
326 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
328 FormFieldData expected
;
329 expected
.name
= ASCIIToUTF16("element");
330 expected
.value
= ASCIIToUTF16("value");
331 expected
.form_control_type
= "text";
332 expected
.max_length
= WebInputElement::defaultMaxLength();
333 expected
.is_autofilled
= true;
334 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
337 // We should be able to extract a radio or a checkbox field that has been
339 TEST_F(FormAutofillTest
, WebFormControlElementToClickableFormField
) {
340 LoadHTML("<INPUT type=\"checkbox\" id=\"checkbox\" value=\"mail\" checked/>"
341 "<INPUT type=\"radio\" id=\"radio\" value=\"male\"/>");
343 WebFrame
* frame
= GetMainFrame();
344 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
346 WebElement web_element
= frame
->document().getElementById("checkbox");
347 WebInputElement element
= web_element
.to
<WebInputElement
>();
348 element
.setAutofilled(true);
349 FormFieldData result
;
350 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
352 FormFieldData expected
;
353 expected
.name
= ASCIIToUTF16("checkbox");
354 expected
.value
= ASCIIToUTF16("mail");
355 expected
.form_control_type
= "checkbox";
356 expected
.is_autofilled
= true;
357 expected
.is_checkable
= true;
358 expected
.is_checked
= true;
359 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
361 web_element
= frame
->document().getElementById("radio");
362 element
= web_element
.to
<WebInputElement
>();
363 element
.setAutofilled(true);
364 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
365 expected
.name
= ASCIIToUTF16("radio");
366 expected
.value
= ASCIIToUTF16("male");
367 expected
.form_control_type
= "radio";
368 expected
.is_autofilled
= true;
369 expected
.is_checkable
= true;
370 expected
.is_checked
= false;
371 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
374 // We should be able to extract a <select> field.
375 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldSelect
) {
376 LoadHTML("<SELECT id=\"element\"/>"
377 " <OPTION value=\"CA\">California</OPTION>"
378 " <OPTION value=\"TX\">Texas</OPTION>"
381 WebFrame
* frame
= GetMainFrame();
382 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
384 WebElement web_element
= frame
->document().getElementById("element");
385 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
386 FormFieldData result1
;
387 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result1
);
389 FormFieldData expected
;
390 expected
.name
= ASCIIToUTF16("element");
391 expected
.max_length
= 0;
392 expected
.form_control_type
= "select-one";
394 expected
.value
= ASCIIToUTF16("CA");
395 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result1
);
397 FormFieldData result2
;
398 WebFormControlElementToFormField(
400 static_cast<autofill::ExtractMask
>(autofill::EXTRACT_VALUE
|
401 autofill::EXTRACT_OPTION_TEXT
),
403 expected
.value
= ASCIIToUTF16("California");
404 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result2
);
406 FormFieldData result3
;
407 WebFormControlElementToFormField(element
, autofill::EXTRACT_OPTIONS
,
409 expected
.value
= string16();
410 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result3
);
412 ASSERT_EQ(2U, result3
.option_values
.size());
413 ASSERT_EQ(2U, result3
.option_contents
.size());
414 EXPECT_EQ(ASCIIToUTF16("CA"), result3
.option_values
[0]);
415 EXPECT_EQ(ASCIIToUTF16("California"), result3
.option_contents
[0]);
416 EXPECT_EQ(ASCIIToUTF16("TX"), result3
.option_values
[1]);
417 EXPECT_EQ(ASCIIToUTF16("Texas"), result3
.option_contents
[1]);
420 // We should not extract the value for non-text and non-select fields.
421 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldInvalidType
) {
422 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
423 " <INPUT type=\"hidden\" id=\"hidden\" value=\"apple\"/>"
424 " <INPUT type=\"submit\" id=\"submit\" value=\"Send\"/>"
427 WebFrame
* frame
= GetMainFrame();
428 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
430 WebElement web_element
= frame
->document().getElementById("hidden");
431 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
432 FormFieldData result
;
433 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
435 FormFieldData expected
;
436 expected
.max_length
= 0;
438 expected
.name
= ASCIIToUTF16("hidden");
439 expected
.form_control_type
= "hidden";
440 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
442 web_element
= frame
->document().getElementById("submit");
443 element
= web_element
.to
<WebFormControlElement
>();
444 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
445 expected
.name
= ASCIIToUTF16("submit");
446 expected
.form_control_type
= "submit";
447 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
450 // We should be able to extract password fields.
451 TEST_F(FormAutofillTest
, WebFormControlElementToPasswordFormField
) {
452 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
453 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>"
456 WebFrame
* frame
= GetMainFrame();
457 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
459 WebElement web_element
= frame
->document().getElementById("password");
460 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
461 FormFieldData result
;
462 WebFormControlElementToFormField(element
, autofill::EXTRACT_VALUE
, &result
);
464 FormFieldData expected
;
465 expected
.max_length
= WebInputElement::defaultMaxLength();
466 expected
.name
= ASCIIToUTF16("password");
467 expected
.form_control_type
= "password";
468 expected
.value
= ASCIIToUTF16("secret");
469 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
472 // We should be able to extract the autocompletetype attribute.
473 TEST_F(FormAutofillTest
, WebFormControlElementToFormFieldAutocompletetype
) {
475 "<INPUT type=\"text\" id=\"absent\"/>"
476 "<INPUT type=\"text\" id=\"empty\" autocomplete=\"\"/>"
477 "<INPUT type=\"text\" id=\"off\" autocomplete=\"off\"/>"
478 "<INPUT type=\"text\" id=\"regular\" autocomplete=\"email\"/>"
479 "<INPUT type=\"text\" id=\"multi-valued\" "
480 " autocomplete=\"billing email\"/>"
481 "<INPUT type=\"text\" id=\"experimental\" x-autocompletetype=\"email\"/>"
482 "<SELECT id=\"select\" autocomplete=\"state\"/>"
483 " <OPTION value=\"CA\">California</OPTION>"
484 " <OPTION value=\"TX\">Texas</OPTION>"
487 "<INPUT type=\"text\" id=\"malicious\" autocomplete=\"" +
488 std::string(10000, 'x') + "\"/>";
489 LoadHTML(html
.c_str());
491 WebFrame
* frame
= GetMainFrame();
492 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
495 const std::string element_id
;
496 const std::string form_control_type
;
497 const std::string autocomplete_attribute
;
499 TestCase test_cases
[] = {
500 // An absent attribute is equivalent to an empty one.
501 { "absent", "text", "" },
502 // Make sure there are no issues parsing an empty attribute.
503 { "empty", "text", "" },
504 // Make sure there are no issues parsing an attribute value that isn't a
506 { "off", "text", "off" },
507 // Common case: exactly one type specified.
508 { "regular", "text", "email" },
509 // Verify that we correctly extract multiple tokens as well.
510 { "multi-valued", "text", "billing email" },
511 // We previously extracted this data from the experimental
512 // 'x-autocompletetype' attribute. Now that the field type hints are part
513 // of the spec under the autocomplete attribute, we no longer support the
514 // experimental version.
515 { "experimental", "text", "" },
516 // <select> elements should behave no differently from text fields here.
517 { "select", "select-one", "state" },
518 // Very long attribute values should be replaced by a default string, to
519 // prevent malicious websites from DOSing the browser process.
520 { "malicious", "text", "x-max-data-length-exceeded" },
523 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_cases
); ++i
) {
524 WebElement web_element
= frame
->document().getElementById(
525 ASCIIToUTF16(test_cases
[i
].element_id
));
526 WebFormControlElement element
= web_element
.to
<WebFormControlElement
>();
527 FormFieldData result
;
528 WebFormControlElementToFormField(element
, autofill::EXTRACT_NONE
, &result
);
530 FormFieldData expected
;
531 expected
.name
= ASCIIToUTF16(test_cases
[i
].element_id
);
532 expected
.form_control_type
= test_cases
[i
].form_control_type
;
533 expected
.autocomplete_attribute
= test_cases
[i
].autocomplete_attribute
;
534 if (test_cases
[i
].form_control_type
== "text")
535 expected
.max_length
= WebInputElement::defaultMaxLength();
537 expected
.max_length
= 0;
539 SCOPED_TRACE(test_cases
[i
].element_id
);
540 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, result
);
544 TEST_F(FormAutofillTest
, WebFormElementToFormData
) {
545 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
546 " <LABEL for=\"firstname\">First name:</LABEL>"
547 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
548 " <LABEL for=\"lastname\">Last name:</LABEL>"
549 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
550 " <LABEL for=\"state\">State:</LABEL>"
551 " <SELECT id=\"state\"/>"
552 " <OPTION value=\"CA\">California</OPTION>"
553 " <OPTION value=\"TX\">Texas</OPTION>"
555 " <LABEL for=\"password\">Password:</LABEL>"
556 " <INPUT type=\"password\" id=\"password\" value=\"secret\"/>"
557 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
558 // The below inputs should be ignored
559 " <LABEL for=\"notvisible\">Hidden:</LABEL>"
560 " <INPUT type=\"hidden\" id=\"notvisible\" value=\"apple\"/>"
563 WebFrame
* frame
= GetMainFrame();
564 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
566 WebVector
<WebFormElement
> forms
;
567 frame
->document().forms(forms
);
568 ASSERT_EQ(1U, forms
.size());
570 WebElement element
= frame
->document().getElementById("firstname");
571 WebInputElement input_element
= element
.to
<WebInputElement
>();
575 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
577 autofill::REQUIRE_NONE
,
578 autofill::EXTRACT_VALUE
,
581 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
582 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
583 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
585 const std::vector
<FormFieldData
>& fields
= form
.fields
;
586 ASSERT_EQ(4U, fields
.size());
588 FormFieldData expected
;
589 expected
.name
= ASCIIToUTF16("firstname");
590 expected
.value
= ASCIIToUTF16("John");
591 expected
.label
= ASCIIToUTF16("First name:");
592 expected
.form_control_type
= "text";
593 expected
.max_length
= WebInputElement::defaultMaxLength();
594 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
596 expected
.name
= ASCIIToUTF16("lastname");
597 expected
.value
= ASCIIToUTF16("Smith");
598 expected
.label
= ASCIIToUTF16("Last name:");
599 expected
.form_control_type
= "text";
600 expected
.max_length
= WebInputElement::defaultMaxLength();
601 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
603 expected
.name
= ASCIIToUTF16("state");
604 expected
.value
= ASCIIToUTF16("CA");
605 expected
.label
= ASCIIToUTF16("State:");
606 expected
.form_control_type
= "select-one";
607 expected
.max_length
= 0;
608 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
610 expected
.name
= ASCIIToUTF16("password");
611 expected
.value
= ASCIIToUTF16("secret");
612 expected
.label
= ASCIIToUTF16("Password:");
613 expected
.form_control_type
= "password";
614 expected
.max_length
= WebInputElement::defaultMaxLength();
615 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
618 // We should not be able to serialize a form with too many fillable fields.
619 TEST_F(FormAutofillTest
, WebFormElementToFormDataTooManyFields
) {
621 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">";
622 for (size_t i
= 0; i
< (autofill::kMaxParseableFields
+ 1); ++i
) {
623 html
+= "<INPUT type=\"text\"/>";
626 LoadHTML(html
.c_str());
628 WebFrame
* frame
= GetMainFrame();
629 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
631 WebVector
<WebFormElement
> forms
;
632 frame
->document().forms(forms
);
633 ASSERT_EQ(1U, forms
.size());
635 WebElement element
= frame
->document().getElementById("firstname");
636 WebInputElement input_element
= element
.to
<WebInputElement
>();
640 EXPECT_FALSE(WebFormElementToFormData(forms
[0],
642 autofill::REQUIRE_NONE
,
643 autofill::EXTRACT_VALUE
,
648 TEST_F(FormAutofillTest
, ExtractForms
) {
649 ExpectJohnSmithLabels(
650 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
651 " First name: <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
652 " Last name: <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
653 " Email: <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
654 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
658 TEST_F(FormAutofillTest
, ExtractMultipleForms
) {
659 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
660 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
661 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
662 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
663 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
665 "<FORM name=\"TestForm2\" action=\"http://zoo.com\" method=\"post\">"
666 " <INPUT type=\"text\" id=\"firstname\" value=\"Jack\"/>"
667 " <INPUT type=\"text\" id=\"lastname\" value=\"Adams\"/>"
668 " <INPUT type=\"text\" id=\"email\" value=\"jack@example.com\"/>"
669 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
672 WebFrame
* web_frame
= GetMainFrame();
673 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
675 FormCache form_cache
;
676 std::vector
<FormData
> forms
;
677 form_cache
.ExtractForms(*web_frame
, &forms
);
678 ASSERT_EQ(2U, forms
.size());
681 const FormData
& form
= forms
[0];
682 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
683 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
684 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
686 const std::vector
<FormFieldData
>& fields
= form
.fields
;
687 ASSERT_EQ(3U, fields
.size());
689 FormFieldData expected
;
690 expected
.form_control_type
= "text";
691 expected
.max_length
= WebInputElement::defaultMaxLength();
693 expected
.name
= ASCIIToUTF16("firstname");
694 expected
.value
= ASCIIToUTF16("John");
695 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
697 expected
.name
= ASCIIToUTF16("lastname");
698 expected
.value
= ASCIIToUTF16("Smith");
699 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
701 expected
.name
= ASCIIToUTF16("email");
702 expected
.value
= ASCIIToUTF16("john@example.com");
703 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
706 const FormData
& form2
= forms
[1];
707 EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2
.name
);
708 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
709 EXPECT_EQ(GURL("http://zoo.com"), form2
.action
);
711 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
712 ASSERT_EQ(3U, fields2
.size());
714 expected
.name
= ASCIIToUTF16("firstname");
715 expected
.value
= ASCIIToUTF16("Jack");
716 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
718 expected
.name
= ASCIIToUTF16("lastname");
719 expected
.value
= ASCIIToUTF16("Adams");
720 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
722 expected
.name
= ASCIIToUTF16("email");
723 expected
.value
= ASCIIToUTF16("jack@example.com");
724 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
727 // We should not extract a form if it has too few fillable fields.
728 TEST_F(FormAutofillTest
, ExtractFormsTooFewFields
) {
729 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
730 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
731 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
732 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
735 WebFrame
* web_frame
= GetMainFrame();
736 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
738 FormCache form_cache
;
739 std::vector
<FormData
> forms
;
740 form_cache
.ExtractForms(*web_frame
, &forms
);
741 EXPECT_EQ(0U, forms
.size());
744 // We should not report additional forms for empty forms.
745 TEST_F(FormAutofillTest
, ExtractFormsSkippedForms
) {
746 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
747 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
748 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
751 WebFrame
* web_frame
= GetMainFrame();
752 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
754 FormCache form_cache
;
755 std::vector
<FormData
> forms
;
756 bool has_skipped_forms
= form_cache
.ExtractFormsAndFormElements(*web_frame
,
760 EXPECT_EQ(0U, forms
.size());
761 EXPECT_TRUE(has_skipped_forms
);
764 // We should not report additional forms for empty forms.
765 TEST_F(FormAutofillTest
, ExtractFormsNoFields
) {
766 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
769 WebFrame
* web_frame
= GetMainFrame();
770 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
772 FormCache form_cache
;
773 std::vector
<FormData
> forms
;
774 bool has_skipped_forms
= form_cache
.ExtractFormsAndFormElements(*web_frame
,
778 EXPECT_EQ(0U, forms
.size());
779 EXPECT_FALSE(has_skipped_forms
);
782 // We should not extract a form if it has too few fillable fields.
783 // Make sure radio and checkbox fields don't count.
784 TEST_F(FormAutofillTest
, ExtractFormsTooFewFieldsSkipsCheckable
) {
785 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
786 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
787 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
788 " <INPUT type=\"radio\" id=\"a_radio\" value=\"0\"/>"
789 " <INPUT type=\"checkbox\" id=\"a_check\" value=\"1\"/>"
790 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
793 WebFrame
* web_frame
= GetMainFrame();
794 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
796 FormCache form_cache
;
797 std::vector
<FormData
> forms
;
798 form_cache
.ExtractForms(*web_frame
, &forms
);
799 EXPECT_EQ(0U, forms
.size());
802 TEST_F(FormAutofillTest
, WebFormElementToFormDataAutocomplete
) {
804 // Form is not auto-completable due to autocomplete=off.
805 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\""
807 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
808 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
809 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
810 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
813 WebFrame
* web_frame
= GetMainFrame();
814 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
816 WebVector
<WebFormElement
> web_forms
;
817 web_frame
->document().forms(web_forms
);
818 ASSERT_EQ(1U, web_forms
.size());
819 WebFormElement web_form
= web_forms
[0];
822 EXPECT_TRUE(WebFormElementToFormData(
823 web_form
, WebFormControlElement(), autofill::REQUIRE_NONE
,
824 autofill::EXTRACT_NONE
, &form
, NULL
));
825 EXPECT_FALSE(WebFormElementToFormData(
826 web_form
, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE
,
827 autofill::EXTRACT_NONE
, &form
, NULL
));
831 // The firstname element is not auto-completable due to autocomplete=off.
832 LoadHTML("<FORM name=\"TestForm\" action=\"http://abc.com\" "
834 " <INPUT type=\"text\" id=\"firstname\" value=\"John\""
836 " <INPUT type=\"text\" id=\"middlename\" value=\"Jack\"/>"
837 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
838 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
839 " <INPUT type=\"submit\" name=\"reply\" value=\"Send\"/>"
842 WebFrame
* web_frame
= GetMainFrame();
843 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
845 WebVector
<WebFormElement
> web_forms
;
846 web_frame
->document().forms(web_forms
);
847 ASSERT_EQ(1U, web_forms
.size());
848 WebFormElement web_form
= web_forms
[0];
851 EXPECT_TRUE(WebFormElementToFormData(
852 web_form
, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE
,
853 autofill::EXTRACT_VALUE
, &form
, NULL
));
855 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
856 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
857 EXPECT_EQ(GURL("http://abc.com"), form
.action
);
859 const std::vector
<FormFieldData
>& fields
= form
.fields
;
860 ASSERT_EQ(3U, fields
.size());
862 FormFieldData expected
;
863 expected
.form_control_type
= "text";
864 expected
.max_length
= WebInputElement::defaultMaxLength();
866 expected
.name
= ASCIIToUTF16("middlename");
867 expected
.value
= ASCIIToUTF16("Jack");
868 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
870 expected
.name
= ASCIIToUTF16("lastname");
871 expected
.value
= ASCIIToUTF16("Smith");
872 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
874 expected
.name
= ASCIIToUTF16("email");
875 expected
.value
= ASCIIToUTF16("john@example.com");
876 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
880 TEST_F(FormAutofillTest
, FindForm
) {
881 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
882 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
883 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
884 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\""
885 "autocomplete=\"off\" />"
886 " <INPUT type=\"text\" id=\"phone\" value=\"1.800.555.1234\"/>"
887 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
890 WebFrame
* web_frame
= GetMainFrame();
891 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
893 FormCache form_cache
;
894 std::vector
<FormData
> forms
;
895 form_cache
.ExtractForms(*web_frame
, &forms
);
896 ASSERT_EQ(1U, forms
.size());
898 // Get the input element we want to find.
899 WebElement element
= web_frame
->document().getElementById("firstname");
900 WebInputElement input_element
= element
.to
<WebInputElement
>();
902 // Find the form and verify it's the correct form.
905 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form
, &field
,
906 autofill::REQUIRE_NONE
));
907 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
908 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
909 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
911 const std::vector
<FormFieldData
>& fields
= form
.fields
;
912 ASSERT_EQ(4U, fields
.size());
914 FormFieldData expected
;
915 expected
.form_control_type
= "text";
916 expected
.max_length
= WebInputElement::defaultMaxLength();
918 expected
.name
= ASCIIToUTF16("firstname");
919 expected
.value
= ASCIIToUTF16("John");
920 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
921 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
923 expected
.name
= ASCIIToUTF16("lastname");
924 expected
.value
= ASCIIToUTF16("Smith");
925 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
927 expected
.name
= ASCIIToUTF16("email");
928 expected
.value
= ASCIIToUTF16("john@example.com");
929 expected
.autocomplete_attribute
= "off";
930 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
931 expected
.autocomplete_attribute
= std::string(); // reset
933 expected
.name
= ASCIIToUTF16("phone");
934 expected
.value
= ASCIIToUTF16("1.800.555.1234");
935 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
937 // Try again, but require autocomplete.
939 FormFieldData field2
;
940 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form2
, &field2
,
941 autofill::REQUIRE_AUTOCOMPLETE
));
942 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
943 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
944 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
946 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
947 ASSERT_EQ(3U, fields2
.size());
949 expected
.form_control_type
= "text";
950 expected
.max_length
= WebInputElement::defaultMaxLength();
952 expected
.name
= ASCIIToUTF16("firstname");
953 expected
.value
= ASCIIToUTF16("John");
954 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
955 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, field
);
957 expected
.name
= ASCIIToUTF16("lastname");
958 expected
.value
= ASCIIToUTF16("Smith");
959 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
961 expected
.name
= ASCIIToUTF16("phone");
962 expected
.value
= ASCIIToUTF16("1.800.555.1234");
963 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
966 // Test regular FillForm function.
967 TEST_F(FormAutofillTest
, FillForm
) {
968 static const AutofillFieldCase field_cases
[] = {
969 // fields: name, initial_value, autocomplete_attribute,
970 // should_be_autofilled, autofill_value, expected_value
972 // Regular empty fields (firstname & lastname) should be autofilled.
973 {"firstname", "", "", true, "filled firstname", "filled firstname"},
974 {"lastname", "", "", true, "filled lastname", "filled lastname"},
975 // hidden fields should not be extracted to form_data.
976 // Non empty fields should not be autofilled.
977 {"notempty", "Hi", "", false, "filled notempty", "Hi"},
978 // "noautocomplete" should not be extracted to form_data.
979 // Disabled fields should not be autofilled.
980 {"notenabled", "", "", false, "filled notenabled", ""},
981 // Readonly fields should not be autofilled.
982 {"readonly", "", "", false, "filled readonly", ""},
983 // Fields with "visibility: hidden" should not be autofilled.
984 {"invisible", "", "", false, "filled invisible", ""},
985 // Fields with "display:none" should not be autofilled.
986 {"displaynone", "", "", false, "filled displaynone", ""},
988 TestFormFillFunctions(kFormHtml
, field_cases
, arraysize(field_cases
),
989 FillForm
, &WebInputElement::value
);
990 // Verify preview selection.
991 WebInputElement firstname
= GetMainFrame()->document().
992 getElementById("firstname").to
<WebInputElement
>();
993 EXPECT_EQ(16, firstname
.selectionStart());
994 EXPECT_EQ(16, firstname
.selectionEnd());
997 TEST_F(FormAutofillTest
, FillFormIncludingNonFocusableElements
) {
998 static const AutofillFieldCase field_cases
[] = {
999 // fields: name, initial_value, autocomplete_attribute,
1000 // should_be_autofilled, autofill_value, expected_value
1002 // Regular empty fields (firstname & lastname) should be autofilled.
1003 {"firstname", "", "", true, "filled firstname", "filled firstname"},
1004 {"lastname", "", "", true, "filled lastname", "filled lastname"},
1005 // hidden fields should not be extracted to form_data.
1006 // Non empty fields should be overrided.
1007 {"notempty", "Hi", "", true, "filled notempty", "filled notempty"},
1008 // "noautocomplete" should not be extracted to form_data.
1009 // Disabled fields should not be autofilled.
1010 {"notenabled", "", "", false, "filled notenabled", ""},
1011 // Readonly fields should not be autofilled.
1012 {"readonly", "", "", false, "filled readonly", ""},
1013 // Fields with "visibility: hidden" should also be autofilled.
1014 {"invisible", "", "", true, "filled invisible", "filled invisible"},
1015 // Fields with "display:none" should also be autofilled.
1016 {"displaynone", "", "", true, "filled displaynone", "filled displaynone"},
1018 TestFormFillFunctions(kFormHtml
, field_cases
, arraysize(field_cases
),
1019 &FillFormIncludingNonFocusableElementsWrapper
,
1020 &WebInputElement::value
);
1023 TEST_F(FormAutofillTest
, FillFormForAllElements
) {
1024 static const AutofillFieldCase field_cases
[] = {
1025 // fields: name, initial_value, autocomplete_attribute,
1026 // should_be_autofilled, autofill_value, expected_value
1028 // All fields except hidden fields (type="hidden") should be Autofilled.
1029 {"firstname", "", "", true, "filled firstname", "filled firstname"},
1030 {"lastname", "", "", true, "filled lastname", "filled lastname"},
1031 // hidden fields should not be extracted to form_data.
1032 {"notempty", "Hi", "", true, "filled notempty", "filled notempty"},
1033 {"noautocomplete", "", "off", true, "filled noautocomplete",
1034 "filled noautocomplete"},
1035 {"notenabled", "", "", true, "filled notenabled", "filled notenabled"},
1036 {"readonly", "", "", true, "filled readonly", "filled readonly"},
1037 {"invisible", "", "", true, "filled invisible", "filled invisible"},
1038 {"displaynone", "", "", true, "filled displaynone", "filled displaynone"},
1040 // Enable Autocheckout because |FillFormForAllElements| is only used by
1042 base::FieldTrialList
field_trial_list(
1043 new metrics::SHA1EntropyProvider("foo"));
1044 base::FieldTrialList::CreateFieldTrial("Autocheckout", "Yes");
1045 TestFormFillFunctions(kFormHtml
, field_cases
, arraysize(field_cases
),
1046 &FillFormForAllFieldsWrapper
, &WebInputElement::value
);
1049 TEST_F(FormAutofillTest
, PreviewForm
) {
1050 static const char* html
=
1051 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
1052 " <INPUT type=\"text\" id=\"firstname\"/>"
1053 " <INPUT type=\"text\" id=\"lastname\"/>"
1054 " <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>"
1055 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>"
1056 " <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>"
1057 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1060 static const AutofillFieldCase field_cases
[] = {
1061 // Normal empty fields should be previewed.
1062 {"firstname", "", "", true, "suggested firstname", "suggested firstname"},
1063 {"lastname", "", "", true, "suggested lastname", "suggested lastname"},
1064 // Non empty fields should not be previewed.
1065 {"notempty", "Hi", "", false, "filled notempty", ""},
1066 // "noautocomplete" should not be extracted to form_data.
1067 // Disabled fields should not be previewed.
1068 {"notenabled", "", "", false, "filled notenabled", ""},
1070 TestFormFillFunctions(html
, field_cases
, arraysize(field_cases
), &PreviewForm
,
1071 &WebInputElement::suggestedValue
);
1073 // Verify preview selection.
1074 WebInputElement firstname
= GetMainFrame()->document().
1075 getElementById("firstname").to
<WebInputElement
>();
1076 EXPECT_EQ(0, firstname
.selectionStart());
1077 EXPECT_EQ(19, firstname
.selectionEnd());
1080 TEST_F(FormAutofillTest
, Labels
) {
1081 ExpectJohnSmithLabels(
1082 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1083 " <LABEL for=\"firstname\"> First name: </LABEL>"
1084 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1085 " <LABEL for=\"lastname\"> Last name: </LABEL>"
1086 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1087 " <LABEL for=\"email\"> Email: </LABEL>"
1088 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1089 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1093 TEST_F(FormAutofillTest
, LabelsWithSpans
) {
1094 ExpectJohnSmithLabels(
1095 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1096 " <LABEL for=\"firstname\"><span>First name: </span></LABEL>"
1097 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1098 " <LABEL for=\"lastname\"><span>Last name: </span></LABEL>"
1099 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1100 " <LABEL for=\"email\"><span>Email: </span></LABEL>"
1101 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1102 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1106 // This test is different from FormAutofillTest.Labels in that the label
1107 // elements for= attribute is set to the name of the form control element it is
1108 // a label for instead of the id of the form control element. This is invalid
1109 // because the for= attribute must be set to the id of the form control element;
1110 // however, current label parsing code will extract the text from the previous
1111 // label element and apply it to the following input field.
1112 TEST_F(FormAutofillTest
, InvalidLabels
) {
1113 ExpectJohnSmithLabels(
1114 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1115 " <LABEL for=\"firstname\"> First name: </LABEL>"
1116 " <INPUT type=\"text\" name=\"firstname\" value=\"John\"/>"
1117 " <LABEL for=\"lastname\"> Last name: </LABEL>"
1118 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\"/>"
1119 " <LABEL for=\"email\"> Email: </LABEL>"
1120 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\"/>"
1121 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1125 // This test has three form control elements, only one of which has a label
1126 // element associated with it.
1127 TEST_F(FormAutofillTest
, OneLabelElement
) {
1128 ExpectJohnSmithLabels(
1129 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1131 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1132 " <LABEL for=\"lastname\">Last name: </LABEL>"
1133 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1135 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1136 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1140 TEST_F(FormAutofillTest
, LabelsInferredFromText
) {
1141 ExpectJohnSmithLabels(
1142 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1144 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1146 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1148 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1149 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1153 TEST_F(FormAutofillTest
, LabelsInferredFromParagraph
) {
1154 ExpectJohnSmithLabels(
1155 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1156 " <P>First name:</P><INPUT type=\"text\" "
1157 " id=\"firstname\" value=\"John\"/>"
1158 " <P>Last name:</P>"
1159 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1161 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1162 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1166 TEST_F(FormAutofillTest
, LabelsInferredFromBold
) {
1167 ExpectJohnSmithLabels(
1168 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1169 " <B>First name:</B><INPUT type=\"text\" "
1170 " id=\"firstname\" value=\"John\"/>"
1171 " <B>Last name:</B>"
1172 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1174 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1175 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1179 TEST_F(FormAutofillTest
, LabelsInferredPriorToImgOrBr
) {
1180 ExpectJohnSmithLabels(
1181 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1182 " First name:<IMG/><INPUT type=\"text\" "
1183 " id=\"firstname\" value=\"John\"/>"
1185 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1187 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1188 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1192 TEST_F(FormAutofillTest
, LabelsInferredFromTableCell
) {
1193 ExpectJohnSmithLabels(
1194 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1197 " <TD>First name:</TD>"
1198 " <TD><INPUT type=\"text\" id=\"firstname\" value=\"John\"/></TD>"
1201 " <TD>Last name:</TD>"
1202 " <TD><INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/></TD>"
1206 " <TD><INPUT type=\"text\" id=\"email\""
1207 " value=\"john@example.com\"/></TD>"
1212 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1219 TEST_F(FormAutofillTest
, LabelsInferredFromTableCellTH
) {
1220 ExpectJohnSmithLabels(
1221 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1224 " <TH>First name:</TH>"
1225 " <TD><INPUT type=\"text\" id=\"firstname\" value=\"John\"/></TD>"
1228 " <TH>Last name:</TH>"
1229 " <TD><INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/></TD>"
1233 " <TD><INPUT type=\"text\" id=\"email\""
1234 " value=\"john@example.com\"/></TD>"
1239 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1246 TEST_F(FormAutofillTest
, LabelsInferredFromTableCellNested
) {
1247 std::vector
<string16
> labels
, names
, values
;
1249 labels
.push_back(ASCIIToUTF16("First name: Bogus"));
1250 names
.push_back(ASCIIToUTF16("firstname"));
1251 values
.push_back(ASCIIToUTF16("John"));
1253 labels
.push_back(ASCIIToUTF16("Last name:"));
1254 names
.push_back(ASCIIToUTF16("lastname"));
1255 values
.push_back(ASCIIToUTF16("Smith"));
1257 labels
.push_back(ASCIIToUTF16("Email:"));
1258 names
.push_back(ASCIIToUTF16("email"));
1259 values
.push_back(ASCIIToUTF16("john@example.com"));
1262 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1275 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1287 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1299 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1306 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1311 labels
, names
, values
);
1314 TEST_F(FormAutofillTest
, LabelsInferredFromTableEmptyTDs
) {
1315 std::vector
<string16
> labels
, names
, values
;
1317 labels
.push_back(ASCIIToUTF16("* First Name"));
1318 names
.push_back(ASCIIToUTF16("firstname"));
1319 values
.push_back(ASCIIToUTF16("John"));
1321 labels
.push_back(ASCIIToUTF16("* Last Name"));
1322 names
.push_back(ASCIIToUTF16("lastname"));
1323 values
.push_back(ASCIIToUTF16("Smith"));
1325 labels
.push_back(ASCIIToUTF16("* Email"));
1326 names
.push_back(ASCIIToUTF16("email"));
1327 values
.push_back(ASCIIToUTF16("john@example.com"));
1330 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1335 " <B>First Name</B>"
1339 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1349 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1359 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1365 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1370 labels
, names
, values
);
1373 TEST_F(FormAutofillTest
, LabelsInferredFromPreviousTD
) {
1374 std::vector
<string16
> labels
, names
, values
;
1376 labels
.push_back(ASCIIToUTF16("* First Name"));
1377 names
.push_back(ASCIIToUTF16("firstname"));
1378 values
.push_back(ASCIIToUTF16("John"));
1380 labels
.push_back(ASCIIToUTF16("* Last Name"));
1381 names
.push_back(ASCIIToUTF16("lastname"));
1382 values
.push_back(ASCIIToUTF16("Smith"));
1384 labels
.push_back(ASCIIToUTF16("* Email"));
1385 names
.push_back(ASCIIToUTF16("email"));
1386 values
.push_back(ASCIIToUTF16("john@example.com"));
1389 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1392 " <TD>* First Name</TD>"
1395 " <INPUT type=\"hidden\"/>"
1396 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1400 " <TD>* Last Name</TD>"
1402 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1408 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1414 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1419 labels
, names
, values
);
1422 // <script>, <noscript> and <option> tags are excluded when the labels are
1424 // Also <!-- comment --> is excluded.
1425 TEST_F(FormAutofillTest
, LabelsInferredFromTableWithSpecialElements
) {
1426 std::vector
<string16
> labels
, names
, values
;
1427 std::vector
<std::string
> control_types
;
1429 labels
.push_back(ASCIIToUTF16("* First Name"));
1430 names
.push_back(ASCIIToUTF16("firstname"));
1431 values
.push_back(ASCIIToUTF16("John"));
1432 control_types
.push_back("text");
1434 labels
.push_back(ASCIIToUTF16("* Middle Name"));
1435 names
.push_back(ASCIIToUTF16("middlename"));
1436 values
.push_back(ASCIIToUTF16("Joe"));
1437 control_types
.push_back("text");
1439 labels
.push_back(ASCIIToUTF16("* Last Name"));
1440 names
.push_back(ASCIIToUTF16("lastname"));
1441 values
.push_back(ASCIIToUTF16("Smith"));
1442 control_types
.push_back("text");
1444 labels
.push_back(ASCIIToUTF16("* Country"));
1445 names
.push_back(ASCIIToUTF16("country"));
1446 values
.push_back(ASCIIToUTF16("US"));
1447 control_types
.push_back("select-one");
1449 labels
.push_back(ASCIIToUTF16("* Email"));
1450 names
.push_back(ASCIIToUTF16("email"));
1451 values
.push_back(ASCIIToUTF16("john@example.com"));
1452 control_types
.push_back("text");
1454 ExpectLabelsAndTypes(
1455 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1460 " <B>First Name</B>"
1463 " <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
1465 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1471 " <B>Middle Name</B>"
1477 " <INPUT type=\"text\" id=\"middlename\" value=\"Joe\"/>"
1486 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1495 " <SELECT id=\"country\">"
1496 " <OPTION VALUE=\"US\">The value should be ignored as label."
1498 " <OPTION VALUE=\"JP\">JAPAN</OPTION>"
1508 " <!-- This comment should be ignored as inferred label.-->"
1509 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1515 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1520 labels
, names
, values
, control_types
);
1523 TEST_F(FormAutofillTest
, LabelsInferredFromTableLabels
) {
1524 ExpectJohnSmithLabels(
1525 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1529 " <LABEL>First name:</LABEL>"
1530 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1535 " <LABEL>Last name:</LABEL>"
1536 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1541 " <LABEL>Email:</LABEL>"
1542 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1546 "<INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1550 TEST_F(FormAutofillTest
, LabelsInferredFromTableTDInterveningElements
) {
1551 ExpectJohnSmithLabels(
1552 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1558 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1565 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1572 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1576 "<INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1580 // Verify that we correctly infer labels when the label text spans multiple
1581 // adjacent HTML elements, not separated by whitespace.
1582 TEST_F(FormAutofillTest
, LabelsInferredFromTableAdjacentElements
) {
1583 std::vector
<string16
> labels
, names
, values
;
1585 labels
.push_back(ASCIIToUTF16("*First Name"));
1586 names
.push_back(ASCIIToUTF16("firstname"));
1587 values
.push_back(ASCIIToUTF16("John"));
1589 labels
.push_back(ASCIIToUTF16("*Last Name"));
1590 names
.push_back(ASCIIToUTF16("lastname"));
1591 values
.push_back(ASCIIToUTF16("Smith"));
1593 labels
.push_back(ASCIIToUTF16("*Email"));
1594 names
.push_back(ASCIIToUTF16("email"));
1595 values
.push_back(ASCIIToUTF16("john@example.com"));
1598 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1602 " <SPAN>*</SPAN><B>First Name</B>"
1605 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1610 " <SPAN>*</SPAN><B>Last Name</B>"
1613 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1618 " <SPAN>*</SPAN><B>Email</B>"
1621 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1626 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1631 labels
, names
, values
);
1634 // Verify that we correctly infer labels when the label text resides in the
1636 TEST_F(FormAutofillTest
, LabelsInferredFromTableRow
) {
1637 std::vector
<string16
> labels
, names
, values
;
1639 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1640 names
.push_back(ASCIIToUTF16("firstname"));
1641 values
.push_back(ASCIIToUTF16("John"));
1643 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1644 names
.push_back(ASCIIToUTF16("lastname"));
1645 values
.push_back(ASCIIToUTF16("Smith"));
1647 labels
.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
1648 names
.push_back(ASCIIToUTF16("email"));
1649 values
.push_back(ASCIIToUTF16("john@example.com"));
1652 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1655 " <TD>*First Name</TD>"
1656 " <TD>*Last Name</TD>"
1661 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1664 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1667 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1672 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1676 labels
, names
, values
);
1679 // Verify that we correctly infer labels when enclosed within a list item.
1680 TEST_F(FormAutofillTest
, LabelsInferredFromListItem
) {
1681 std::vector
<string16
> labels
, names
, values
;
1683 labels
.push_back(ASCIIToUTF16("* Home Phone"));
1684 names
.push_back(ASCIIToUTF16("areacode"));
1685 values
.push_back(ASCIIToUTF16("415"));
1687 labels
.push_back(ASCIIToUTF16("* Home Phone"));
1688 names
.push_back(ASCIIToUTF16("prefix"));
1689 values
.push_back(ASCIIToUTF16("555"));
1691 labels
.push_back(ASCIIToUTF16("* Home Phone"));
1692 names
.push_back(ASCIIToUTF16("suffix"));
1693 values
.push_back(ASCIIToUTF16("1212"));
1696 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1699 " <SPAN>Bogus</SPAN>"
1702 " <LABEL><EM>*</EM> Home Phone</LABEL>"
1703 " <INPUT type=\"text\" id=\"areacode\" value=\"415\"/>"
1704 " <INPUT type=\"text\" id=\"prefix\" value=\"555\"/>"
1705 " <INPUT type=\"text\" id=\"suffix\" value=\"1212\"/>"
1708 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1712 labels
, names
, values
);
1715 TEST_F(FormAutofillTest
, LabelsInferredFromDefinitionList
) {
1716 std::vector
<string16
> labels
, names
, values
;
1718 labels
.push_back(ASCIIToUTF16("* First name: Bogus"));
1719 names
.push_back(ASCIIToUTF16("firstname"));
1720 values
.push_back(ASCIIToUTF16("John"));
1722 labels
.push_back(ASCIIToUTF16("Last name:"));
1723 names
.push_back(ASCIIToUTF16("lastname"));
1724 values
.push_back(ASCIIToUTF16("Smith"));
1726 labels
.push_back(ASCIIToUTF16("Email:"));
1727 names
.push_back(ASCIIToUTF16("email"));
1728 values
.push_back(ASCIIToUTF16("john@example.com"));
1731 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1746 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1756 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1766 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1771 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1775 labels
, names
, values
);
1778 TEST_F(FormAutofillTest
, LabelsInferredWithSameName
) {
1779 std::vector
<string16
> labels
, names
, values
;
1781 labels
.push_back(ASCIIToUTF16("Address Line 1:"));
1782 names
.push_back(ASCIIToUTF16("Address"));
1783 values
.push_back(string16());
1785 labels
.push_back(ASCIIToUTF16("Address Line 2:"));
1786 names
.push_back(ASCIIToUTF16("Address"));
1787 values
.push_back(string16());
1789 labels
.push_back(ASCIIToUTF16("Address Line 3:"));
1790 names
.push_back(ASCIIToUTF16("Address"));
1791 values
.push_back(string16());
1794 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1796 " <INPUT type=\"text\" name=\"Address\"/>"
1798 " <INPUT type=\"text\" name=\"Address\"/>"
1800 " <INPUT type=\"text\" name=\"Address\"/>"
1801 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1803 labels
, names
, values
);
1806 TEST_F(FormAutofillTest
, LabelsInferredWithImageTags
) {
1807 std::vector
<string16
> labels
, names
, values
;
1809 labels
.push_back(ASCIIToUTF16("Phone:"));
1810 names
.push_back(ASCIIToUTF16("dayphone1"));
1811 values
.push_back(string16());
1813 labels
.push_back(ASCIIToUTF16("-"));
1814 names
.push_back(ASCIIToUTF16("dayphone2"));
1815 values
.push_back(string16());
1817 labels
.push_back(ASCIIToUTF16("-"));
1818 names
.push_back(ASCIIToUTF16("dayphone3"));
1819 values
.push_back(string16());
1821 labels
.push_back(ASCIIToUTF16("ext.:"));
1822 names
.push_back(ASCIIToUTF16("dayphone4"));
1823 values
.push_back(string16());
1825 labels
.push_back(string16());
1826 names
.push_back(ASCIIToUTF16("dummy"));
1827 values
.push_back(string16());
1830 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1832 " <input type=\"text\" name=\"dayphone1\">"
1836 " <input type=\"text\" name=\"dayphone2\">"
1840 " <input type=\"text\" name=\"dayphone3\">"
1842 " <input type=\"text\" name=\"dayphone4\">"
1843 " <input type=\"text\" name=\"dummy\">"
1844 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
1846 labels
, names
, values
);
1849 TEST_F(FormAutofillTest
, LabelsInferredFromDivTable
) {
1850 ExpectJohnSmithLabels(
1851 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1852 "<DIV>First name:<BR>"
1854 " <INPUT type=\"text\" name=\"firstname\" value=\"John\">"
1857 "<DIV>Last name:<BR>"
1859 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\">"
1864 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\">"
1867 "<input type=\"submit\" name=\"reply-send\" value=\"Send\">"
1871 TEST_F(FormAutofillTest
, LabelsInferredFromDivSiblingTable
) {
1872 ExpectJohnSmithLabels(
1873 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1874 "<DIV>First name:</DIV>"
1877 " <INPUT type=\"text\" name=\"firstname\" value=\"John\">"
1880 "<DIV>Last name:</DIV>"
1883 " <INPUT type=\"text\" name=\"lastname\" value=\"Smith\">"
1889 " <INPUT type=\"text\" name=\"email\" value=\"john@example.com\">"
1892 "<input type=\"submit\" name=\"reply-send\" value=\"Send\">"
1896 TEST_F(FormAutofillTest
, LabelsInferredFromDefinitionListRatherThanDivTable
) {
1897 ExpectJohnSmithLabels(
1898 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
1899 "<DIV>This is not a label.<BR>"
1908 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
1918 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
1928 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
1933 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1940 TEST_F(FormAutofillTest
, FillFormMaxLength
) {
1941 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
1942 " <INPUT type=\"text\" id=\"firstname\" maxlength=\"5\"/>"
1943 " <INPUT type=\"text\" id=\"lastname\" maxlength=\"7\"/>"
1944 " <INPUT type=\"text\" id=\"email\" maxlength=\"9\"/>"
1945 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
1948 WebFrame
* web_frame
= GetMainFrame();
1949 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
1951 FormCache form_cache
;
1952 std::vector
<FormData
> forms
;
1953 form_cache
.ExtractForms(*web_frame
, &forms
);
1954 ASSERT_EQ(1U, forms
.size());
1956 // Get the input element we want to find.
1957 WebElement element
= web_frame
->document().getElementById("firstname");
1958 WebInputElement input_element
= element
.to
<WebInputElement
>();
1960 // Find the form that contains the input element.
1962 FormFieldData field
;
1963 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form
, &field
,
1964 autofill::REQUIRE_NONE
));
1965 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
1966 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
1967 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
1969 const std::vector
<FormFieldData
>& fields
= form
.fields
;
1970 ASSERT_EQ(3U, fields
.size());
1972 FormFieldData expected
;
1973 expected
.form_control_type
= "text";
1975 expected
.name
= ASCIIToUTF16("firstname");
1976 expected
.max_length
= 5;
1977 expected
.is_autofilled
= false;
1978 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
1980 expected
.name
= ASCIIToUTF16("lastname");
1981 expected
.max_length
= 7;
1982 expected
.is_autofilled
= false;
1983 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
1985 expected
.name
= ASCIIToUTF16("email");
1986 expected
.max_length
= 9;
1987 expected
.is_autofilled
= false;
1988 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
1991 form
.fields
[0].value
= ASCIIToUTF16("Brother");
1992 form
.fields
[1].value
= ASCIIToUTF16("Jonathan");
1993 form
.fields
[2].value
= ASCIIToUTF16("brotherj@example.com");
1994 FillForm(form
, input_element
);
1996 // Find the newly-filled form that contains the input element.
1998 FormFieldData field2
;
1999 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form2
, &field2
,
2000 autofill::REQUIRE_NONE
));
2002 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2003 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2004 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2006 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2007 ASSERT_EQ(3U, fields2
.size());
2009 expected
.form_control_type
= "text";
2011 expected
.name
= ASCIIToUTF16("firstname");
2012 expected
.value
= ASCIIToUTF16("Broth");
2013 expected
.max_length
= 5;
2014 expected
.is_autofilled
= true;
2015 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2017 expected
.name
= ASCIIToUTF16("lastname");
2018 expected
.value
= ASCIIToUTF16("Jonatha");
2019 expected
.max_length
= 7;
2020 expected
.is_autofilled
= true;
2021 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2023 expected
.name
= ASCIIToUTF16("email");
2024 expected
.value
= ASCIIToUTF16("brotherj@");
2025 expected
.max_length
= 9;
2026 expected
.is_autofilled
= true;
2027 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2030 // This test uses negative values of the maxlength attribute for input elements.
2031 // In this case, the maxlength of the input elements is set to the default
2032 // maxlength (defined in WebKit.)
2033 TEST_F(FormAutofillTest
, FillFormNegativeMaxLength
) {
2034 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2035 " <INPUT type=\"text\" id=\"firstname\" maxlength=\"-1\"/>"
2036 " <INPUT type=\"text\" id=\"lastname\" maxlength=\"-10\"/>"
2037 " <INPUT type=\"text\" id=\"email\" maxlength=\"-13\"/>"
2038 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2041 WebFrame
* web_frame
= GetMainFrame();
2042 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2044 FormCache form_cache
;
2045 std::vector
<FormData
> forms
;
2046 form_cache
.ExtractForms(*web_frame
, &forms
);
2047 ASSERT_EQ(1U, forms
.size());
2049 // Get the input element we want to find.
2050 WebElement element
= web_frame
->document().getElementById("firstname");
2051 WebInputElement input_element
= element
.to
<WebInputElement
>();
2053 // Find the form that contains the input element.
2055 FormFieldData field
;
2056 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form
, &field
,
2057 autofill::REQUIRE_NONE
));
2058 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2059 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2060 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
2062 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2063 ASSERT_EQ(3U, fields
.size());
2065 FormFieldData expected
;
2066 expected
.form_control_type
= "text";
2067 expected
.max_length
= WebInputElement::defaultMaxLength();
2069 expected
.name
= ASCIIToUTF16("firstname");
2070 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2072 expected
.name
= ASCIIToUTF16("lastname");
2073 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2075 expected
.name
= ASCIIToUTF16("email");
2076 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2079 form
.fields
[0].value
= ASCIIToUTF16("Brother");
2080 form
.fields
[1].value
= ASCIIToUTF16("Jonathan");
2081 form
.fields
[2].value
= ASCIIToUTF16("brotherj@example.com");
2082 FillForm(form
, input_element
);
2084 // Find the newly-filled form that contains the input element.
2086 FormFieldData field2
;
2087 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form2
, &field2
,
2088 autofill::REQUIRE_NONE
));
2090 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2091 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2092 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2094 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2095 ASSERT_EQ(3U, fields2
.size());
2097 expected
.name
= ASCIIToUTF16("firstname");
2098 expected
.value
= ASCIIToUTF16("Brother");
2099 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2101 expected
.name
= ASCIIToUTF16("lastname");
2102 expected
.value
= ASCIIToUTF16("Jonathan");
2103 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2105 expected
.name
= ASCIIToUTF16("email");
2106 expected
.value
= ASCIIToUTF16("brotherj@example.com");
2107 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2110 TEST_F(FormAutofillTest
, FillFormEmptyName
) {
2111 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2112 " <INPUT type=\"text\" id=\"firstname\"/>"
2113 " <INPUT type=\"text\" id=\"lastname\"/>"
2114 " <INPUT type=\"text\" id=\"email\"/>"
2115 " <INPUT type=\"submit\" value=\"Send\"/>"
2118 WebFrame
* web_frame
= GetMainFrame();
2119 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2121 FormCache form_cache
;
2122 std::vector
<FormData
> forms
;
2123 form_cache
.ExtractForms(*web_frame
, &forms
);
2124 ASSERT_EQ(1U, forms
.size());
2126 // Get the input element we want to find.
2127 WebElement element
= web_frame
->document().getElementById("firstname");
2128 WebInputElement input_element
= element
.to
<WebInputElement
>();
2130 // Find the form that contains the input element.
2132 FormFieldData field
;
2133 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form
, &field
,
2134 autofill::REQUIRE_NONE
));
2135 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2136 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2137 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
2139 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2140 ASSERT_EQ(3U, fields
.size());
2142 FormFieldData expected
;
2143 expected
.form_control_type
= "text";
2144 expected
.max_length
= WebInputElement::defaultMaxLength();
2146 expected
.name
= ASCIIToUTF16("firstname");
2147 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2149 expected
.name
= ASCIIToUTF16("lastname");
2150 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2152 expected
.name
= ASCIIToUTF16("email");
2153 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2156 form
.fields
[0].value
= ASCIIToUTF16("Wyatt");
2157 form
.fields
[1].value
= ASCIIToUTF16("Earp");
2158 form
.fields
[2].value
= ASCIIToUTF16("wyatt@example.com");
2159 FillForm(form
, input_element
);
2161 // Find the newly-filled form that contains the input element.
2163 FormFieldData field2
;
2164 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form2
, &field2
,
2165 autofill::REQUIRE_NONE
));
2167 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2168 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2169 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2171 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2172 ASSERT_EQ(3U, fields2
.size());
2174 expected
.form_control_type
= "text";
2175 expected
.max_length
= WebInputElement::defaultMaxLength();
2177 expected
.name
= ASCIIToUTF16("firstname");
2178 expected
.value
= ASCIIToUTF16("Wyatt");
2179 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2181 expected
.name
= ASCIIToUTF16("lastname");
2182 expected
.value
= ASCIIToUTF16("Earp");
2183 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2185 expected
.name
= ASCIIToUTF16("email");
2186 expected
.value
= ASCIIToUTF16("wyatt@example.com");
2187 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2190 TEST_F(FormAutofillTest
, FillFormEmptyFormNames
) {
2191 LoadHTML("<FORM action=\"http://buh.com\" method=\"post\">"
2192 " <INPUT type=\"text\" id=\"firstname\"/>"
2193 " <INPUT type=\"text\" id=\"middlename\"/>"
2194 " <INPUT type=\"text\" id=\"lastname\"/>"
2195 " <INPUT type=\"submit\" value=\"Send\"/>"
2197 "<FORM action=\"http://abc.com\" method=\"post\">"
2198 " <INPUT type=\"text\" id=\"apple\"/>"
2199 " <INPUT type=\"text\" id=\"banana\"/>"
2200 " <INPUT type=\"text\" id=\"cantelope\"/>"
2201 " <INPUT type=\"submit\" value=\"Send\"/>"
2204 WebFrame
* web_frame
= GetMainFrame();
2205 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2207 FormCache form_cache
;
2208 std::vector
<FormData
> forms
;
2209 form_cache
.ExtractForms(*web_frame
, &forms
);
2210 ASSERT_EQ(2U, forms
.size());
2212 // Get the input element we want to find.
2213 WebElement element
= web_frame
->document().getElementById("apple");
2214 WebInputElement input_element
= element
.to
<WebInputElement
>();
2216 // Find the form that contains the input element.
2218 FormFieldData field
;
2219 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form
, &field
,
2220 autofill::REQUIRE_NONE
));
2221 EXPECT_EQ(string16(), form
.name
);
2222 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2223 EXPECT_EQ(GURL("http://abc.com"), form
.action
);
2225 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2226 ASSERT_EQ(3U, fields
.size());
2228 FormFieldData expected
;
2229 expected
.form_control_type
= "text";
2230 expected
.max_length
= WebInputElement::defaultMaxLength();
2232 expected
.name
= ASCIIToUTF16("apple");
2233 expected
.is_autofilled
= false;
2234 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2236 expected
.name
= ASCIIToUTF16("banana");
2237 expected
.is_autofilled
= false;
2238 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2240 expected
.name
= ASCIIToUTF16("cantelope");
2241 expected
.is_autofilled
= false;
2242 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2245 form
.fields
[0].value
= ASCIIToUTF16("Red");
2246 form
.fields
[1].value
= ASCIIToUTF16("Yellow");
2247 form
.fields
[2].value
= ASCIIToUTF16("Also Yellow");
2248 FillForm(form
, input_element
);
2250 // Find the newly-filled form that contains the input element.
2252 FormFieldData field2
;
2253 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form2
, &field2
,
2254 autofill::REQUIRE_NONE
));
2256 EXPECT_EQ(string16(), form2
.name
);
2257 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2258 EXPECT_EQ(GURL("http://abc.com"), form2
.action
);
2260 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2261 ASSERT_EQ(3U, fields2
.size());
2263 expected
.name
= ASCIIToUTF16("apple");
2264 expected
.value
= ASCIIToUTF16("Red");
2265 expected
.is_autofilled
= true;
2266 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2268 expected
.name
= ASCIIToUTF16("banana");
2269 expected
.value
= ASCIIToUTF16("Yellow");
2270 expected
.is_autofilled
= true;
2271 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2273 expected
.name
= ASCIIToUTF16("cantelope");
2274 expected
.value
= ASCIIToUTF16("Also Yellow");
2275 expected
.is_autofilled
= true;
2276 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2279 TEST_F(FormAutofillTest
, ThreePartPhone
) {
2280 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2282 " <input type=\"text\" name=\"dayphone1\">"
2284 " <input type=\"text\" name=\"dayphone2\">"
2286 " <input type=\"text\" name=\"dayphone3\">"
2288 " <input type=\"text\" name=\"dayphone4\">"
2289 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2293 WebFrame
* frame
= GetMainFrame();
2294 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
2296 WebVector
<WebFormElement
> forms
;
2297 frame
->document().forms(forms
);
2298 ASSERT_EQ(1U, forms
.size());
2301 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
2302 WebFormControlElement(),
2303 autofill::REQUIRE_NONE
,
2304 autofill::EXTRACT_VALUE
,
2307 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2308 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
2309 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
2311 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2312 ASSERT_EQ(4U, fields
.size());
2314 FormFieldData expected
;
2315 expected
.form_control_type
= "text";
2316 expected
.max_length
= WebInputElement::defaultMaxLength();
2318 expected
.label
= ASCIIToUTF16("Phone:");
2319 expected
.name
= ASCIIToUTF16("dayphone1");
2320 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2322 expected
.label
= ASCIIToUTF16("-");
2323 expected
.name
= ASCIIToUTF16("dayphone2");
2324 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2326 expected
.label
= ASCIIToUTF16("-");
2327 expected
.name
= ASCIIToUTF16("dayphone3");
2328 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2330 expected
.label
= ASCIIToUTF16("ext.:");
2331 expected
.name
= ASCIIToUTF16("dayphone4");
2332 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
2336 TEST_F(FormAutofillTest
, MaxLengthFields
) {
2337 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2339 " <input type=\"text\" maxlength=\"3\" name=\"dayphone1\">"
2341 " <input type=\"text\" maxlength=\"3\" name=\"dayphone2\">"
2343 " <input type=\"text\" maxlength=\"4\" size=\"5\""
2344 " name=\"dayphone3\">"
2346 " <input type=\"text\" maxlength=\"5\" name=\"dayphone4\">"
2347 " <input type=\"text\" name=\"default1\">"
2348 " <input type=\"text\" maxlength=\"-1\" name=\"invalid1\">"
2349 " <input type=\"submit\" name=\"reply-send\" value=\"Send\">"
2352 WebFrame
* frame
= GetMainFrame();
2353 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
2355 WebVector
<WebFormElement
> forms
;
2356 frame
->document().forms(forms
);
2357 ASSERT_EQ(1U, forms
.size());
2360 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
2361 WebFormControlElement(),
2362 autofill::REQUIRE_NONE
,
2363 autofill::EXTRACT_VALUE
,
2366 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2367 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
2368 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
2370 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2371 ASSERT_EQ(6U, fields
.size());
2373 FormFieldData expected
;
2374 expected
.form_control_type
= "text";
2376 expected
.label
= ASCIIToUTF16("Phone:");
2377 expected
.name
= ASCIIToUTF16("dayphone1");
2378 expected
.max_length
= 3;
2379 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2381 expected
.label
= ASCIIToUTF16("-");
2382 expected
.name
= ASCIIToUTF16("dayphone2");
2383 expected
.max_length
= 3;
2384 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2386 expected
.label
= ASCIIToUTF16("-");
2387 expected
.name
= ASCIIToUTF16("dayphone3");
2388 expected
.max_length
= 4;
2389 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2391 expected
.label
= ASCIIToUTF16("ext.:");
2392 expected
.name
= ASCIIToUTF16("dayphone4");
2393 expected
.max_length
= 5;
2394 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[3]);
2396 // When unspecified |size|, default is returned.
2397 expected
.label
= string16();
2398 expected
.name
= ASCIIToUTF16("default1");
2399 expected
.max_length
= WebInputElement::defaultMaxLength();
2400 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[4]);
2402 // When invalid |size|, default is returned.
2403 expected
.label
= string16();
2404 expected
.name
= ASCIIToUTF16("invalid1");
2405 expected
.max_length
= WebInputElement::defaultMaxLength();
2406 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[5]);
2409 // This test re-creates the experience of typing in a field then selecting a
2410 // profile from the Autofill suggestions popup. The field that is being typed
2411 // into should be filled even though it's not technically empty.
2412 TEST_F(FormAutofillTest
, FillFormNonEmptyField
) {
2413 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2414 " <INPUT type=\"text\" id=\"firstname\"/>"
2415 " <INPUT type=\"text\" id=\"lastname\"/>"
2416 " <INPUT type=\"text\" id=\"email\"/>"
2417 " <INPUT type=\"submit\" value=\"Send\"/>"
2420 WebFrame
* web_frame
= GetMainFrame();
2421 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2423 FormCache form_cache
;
2424 std::vector
<FormData
> forms
;
2425 form_cache
.ExtractForms(*web_frame
, &forms
);
2426 ASSERT_EQ(1U, forms
.size());
2428 // Get the input element we want to find.
2429 WebElement element
= web_frame
->document().getElementById("firstname");
2430 WebInputElement input_element
= element
.to
<WebInputElement
>();
2432 // Simulate typing by modifying the field value.
2433 input_element
.setValue(ASCIIToUTF16("Wy"));
2435 // Find the form that contains the input element.
2437 FormFieldData field
;
2438 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form
, &field
,
2439 autofill::REQUIRE_NONE
));
2440 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2441 EXPECT_EQ(GURL(web_frame
->document().url()), form
.origin
);
2442 EXPECT_EQ(GURL("http://buh.com"), form
.action
);
2444 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2445 ASSERT_EQ(3U, fields
.size());
2447 FormFieldData expected
;
2448 expected
.form_control_type
= "text";
2449 expected
.max_length
= WebInputElement::defaultMaxLength();
2451 expected
.name
= ASCIIToUTF16("firstname");
2452 expected
.value
= ASCIIToUTF16("Wy");
2453 expected
.is_autofilled
= false;
2454 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2456 expected
.name
= ASCIIToUTF16("lastname");
2457 expected
.value
= string16();
2458 expected
.is_autofilled
= false;
2459 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
2461 expected
.name
= ASCIIToUTF16("email");
2462 expected
.value
= string16();
2463 expected
.is_autofilled
= false;
2464 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
2466 // Preview the form and verify that the cursor position has been updated.
2467 form
.fields
[0].value
= ASCIIToUTF16("Wyatt");
2468 form
.fields
[1].value
= ASCIIToUTF16("Earp");
2469 form
.fields
[2].value
= ASCIIToUTF16("wyatt@example.com");
2470 PreviewForm(form
, input_element
);
2471 EXPECT_EQ(2, input_element
.selectionStart());
2472 EXPECT_EQ(5, input_element
.selectionEnd());
2475 FillForm(form
, input_element
);
2477 // Find the newly-filled form that contains the input element.
2479 FormFieldData field2
;
2480 EXPECT_TRUE(FindFormAndFieldForInputElement(input_element
, &form2
, &field2
,
2481 autofill::REQUIRE_NONE
));
2483 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2484 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2485 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2487 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2488 ASSERT_EQ(3U, fields2
.size());
2490 expected
.name
= ASCIIToUTF16("firstname");
2491 expected
.value
= ASCIIToUTF16("Wyatt");
2492 expected
.is_autofilled
= true;
2493 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2495 expected
.name
= ASCIIToUTF16("lastname");
2496 expected
.value
= ASCIIToUTF16("Earp");
2497 expected
.is_autofilled
= true;
2498 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2500 expected
.name
= ASCIIToUTF16("email");
2501 expected
.value
= ASCIIToUTF16("wyatt@example.com");
2502 expected
.is_autofilled
= true;
2503 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2505 // Verify that the cursor position has been updated.
2506 EXPECT_EQ(5, input_element
.selectionStart());
2507 EXPECT_EQ(5, input_element
.selectionEnd());
2510 TEST_F(FormAutofillTest
, ClearFormWithNode
) {
2512 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2513 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2514 " <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>"
2515 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noAC\" value=\"one\"/>"
2516 " <INPUT type=\"text\" id=\"notenabled\" disabled=\"disabled\">"
2517 " <INPUT type=\"submit\" value=\"Send\"/>"
2520 WebFrame
* web_frame
= GetMainFrame();
2521 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2523 FormCache form_cache
;
2524 std::vector
<FormData
> forms
;
2525 form_cache
.ExtractForms(*web_frame
, &forms
);
2526 ASSERT_EQ(1U, forms
.size());
2528 // Set the auto-filled attribute on the firstname element.
2529 WebInputElement firstname
=
2530 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2531 firstname
.setAutofilled(true);
2533 // Set the value of the disabled attribute.
2534 WebInputElement notenabled
=
2535 web_frame
->document().getElementById("notenabled").to
<WebInputElement
>();
2536 notenabled
.setValue(WebString::fromUTF8("no clear"));
2539 EXPECT_TRUE(form_cache
.ClearFormWithElement(firstname
));
2541 // Verify that the auto-filled attribute has been turned off.
2542 EXPECT_FALSE(firstname
.isAutofilled());
2544 // Verify the form is cleared.
2546 FormFieldData field2
;
2547 EXPECT_TRUE(FindFormAndFieldForInputElement(firstname
, &form2
, &field2
,
2548 autofill::REQUIRE_NONE
));
2549 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2550 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2551 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2553 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2554 ASSERT_EQ(4U, fields2
.size());
2556 FormFieldData expected
;
2557 expected
.form_control_type
= "text";
2558 expected
.max_length
= WebInputElement::defaultMaxLength();
2560 expected
.name
= ASCIIToUTF16("firstname");
2561 expected
.value
= string16();
2562 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2564 expected
.name
= ASCIIToUTF16("lastname");
2565 expected
.value
= string16();
2566 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2568 expected
.name
= ASCIIToUTF16("noAC");
2569 expected
.value
= string16();
2570 expected
.autocomplete_attribute
= "off";
2571 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2572 expected
.autocomplete_attribute
= std::string(); // reset
2574 expected
.name
= ASCIIToUTF16("notenabled");
2575 expected
.value
= ASCIIToUTF16("no clear");
2576 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[3]);
2578 // Verify that the cursor position has been updated.
2579 EXPECT_EQ(0, firstname
.selectionStart());
2580 EXPECT_EQ(0, firstname
.selectionEnd());
2583 TEST_F(FormAutofillTest
, ClearFormWithNodeContainingSelectOne
) {
2585 "<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2586 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2587 " <INPUT type=\"text\" id=\"lastname\" value=\"Earp\"/>"
2588 " <SELECT id=\"state\" name=\"state\">"
2589 " <OPTION selected>?</OPTION>"
2590 " <OPTION>AA</OPTION>"
2591 " <OPTION>AE</OPTION>"
2592 " <OPTION>AK</OPTION>"
2594 " <INPUT type=\"submit\" value=\"Send\"/>"
2597 WebFrame
* web_frame
= GetMainFrame();
2598 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2600 FormCache form_cache
;
2601 std::vector
<FormData
> forms
;
2602 form_cache
.ExtractForms(*web_frame
, &forms
);
2603 ASSERT_EQ(1U, forms
.size());
2605 // Set the auto-filled attribute on the firstname element.
2606 WebInputElement firstname
=
2607 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2608 firstname
.setAutofilled(true);
2610 // Set the value of the select-one.
2611 WebSelectElement select_element
=
2612 web_frame
->document().getElementById("state").to
<WebSelectElement
>();
2613 select_element
.setValue(WebString::fromUTF8("AK"));
2616 EXPECT_TRUE(form_cache
.ClearFormWithElement(firstname
));
2618 // Verify that the auto-filled attribute has been turned off.
2619 EXPECT_FALSE(firstname
.isAutofilled());
2621 // Verify the form is cleared.
2623 FormFieldData field2
;
2624 EXPECT_TRUE(FindFormAndFieldForInputElement(firstname
, &form2
, &field2
,
2625 autofill::REQUIRE_NONE
));
2626 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2
.name
);
2627 EXPECT_EQ(GURL(web_frame
->document().url()), form2
.origin
);
2628 EXPECT_EQ(GURL("http://buh.com"), form2
.action
);
2630 const std::vector
<FormFieldData
>& fields2
= form2
.fields
;
2631 ASSERT_EQ(3U, fields2
.size());
2633 FormFieldData expected
;
2635 expected
.name
= ASCIIToUTF16("firstname");
2636 expected
.value
= string16();
2637 expected
.form_control_type
= "text";
2638 expected
.max_length
= WebInputElement::defaultMaxLength();
2639 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[0]);
2641 expected
.name
= ASCIIToUTF16("lastname");
2642 expected
.value
= string16();
2643 expected
.form_control_type
= "text";
2644 expected
.max_length
= WebInputElement::defaultMaxLength();
2645 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[1]);
2647 expected
.name
= ASCIIToUTF16("state");
2648 expected
.value
= ASCIIToUTF16("?");
2649 expected
.form_control_type
= "select-one";
2650 expected
.max_length
= 0;
2651 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields2
[2]);
2653 // Verify that the cursor position has been updated.
2654 EXPECT_EQ(0, firstname
.selectionStart());
2655 EXPECT_EQ(0, firstname
.selectionEnd());
2658 TEST_F(FormAutofillTest
, ClearPreviewedFormWithElement
) {
2659 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2660 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2661 " <INPUT type=\"text\" id=\"lastname\"/>"
2662 " <INPUT type=\"text\" id=\"email\"/>"
2663 " <INPUT type=\"email\" id=\"email2\"/>"
2664 " <INPUT type=\"tel\" id=\"phone\"/>"
2665 " <INPUT type=\"submit\" value=\"Send\"/>"
2668 WebFrame
* web_frame
= GetMainFrame();
2669 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2671 FormCache form_cache
;
2672 std::vector
<FormData
> forms
;
2673 form_cache
.ExtractForms(*web_frame
, &forms
);
2674 ASSERT_EQ(1U, forms
.size());
2676 // Set the auto-filled attribute.
2677 WebInputElement firstname
=
2678 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2679 firstname
.setAutofilled(true);
2680 WebInputElement lastname
=
2681 web_frame
->document().getElementById("lastname").to
<WebInputElement
>();
2682 lastname
.setAutofilled(true);
2683 WebInputElement email
=
2684 web_frame
->document().getElementById("email").to
<WebInputElement
>();
2685 email
.setAutofilled(true);
2686 WebInputElement email2
=
2687 web_frame
->document().getElementById("email2").to
<WebInputElement
>();
2688 email2
.setAutofilled(true);
2689 WebInputElement phone
=
2690 web_frame
->document().getElementById("phone").to
<WebInputElement
>();
2691 phone
.setAutofilled(true);
2693 // Set the suggested values on two of the elements.
2694 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
2695 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2696 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2697 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2699 // Clear the previewed fields.
2700 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname
, false));
2702 // Fields with empty suggestions suggestions are not modified.
2703 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname
.value());
2704 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
2705 EXPECT_TRUE(firstname
.isAutofilled());
2707 // Verify the previewed fields are cleared.
2708 EXPECT_TRUE(lastname
.value().isEmpty());
2709 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
2710 EXPECT_FALSE(lastname
.isAutofilled());
2711 EXPECT_TRUE(email
.value().isEmpty());
2712 EXPECT_TRUE(email
.suggestedValue().isEmpty());
2713 EXPECT_FALSE(email
.isAutofilled());
2714 EXPECT_TRUE(email2
.value().isEmpty());
2715 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
2716 EXPECT_FALSE(email2
.isAutofilled());
2717 EXPECT_TRUE(phone
.value().isEmpty());
2718 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
2719 EXPECT_FALSE(phone
.isAutofilled());
2721 // Verify that the cursor position has been updated.
2722 EXPECT_EQ(0, lastname
.selectionStart());
2723 EXPECT_EQ(0, lastname
.selectionEnd());
2726 TEST_F(FormAutofillTest
, ClearPreviewedFormWithNonEmptyInitiatingNode
) {
2727 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2728 " <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
2729 " <INPUT type=\"text\" id=\"lastname\"/>"
2730 " <INPUT type=\"text\" id=\"email\"/>"
2731 " <INPUT type=\"email\" id=\"email2\"/>"
2732 " <INPUT type=\"tel\" id=\"phone\"/>"
2733 " <INPUT type=\"submit\" value=\"Send\"/>"
2736 WebFrame
* web_frame
= GetMainFrame();
2737 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2739 FormCache form_cache
;
2740 std::vector
<FormData
> forms
;
2741 form_cache
.ExtractForms(*web_frame
, &forms
);
2742 ASSERT_EQ(1U, forms
.size());
2744 // Set the auto-filled attribute.
2745 WebInputElement firstname
=
2746 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2747 firstname
.setAutofilled(true);
2748 WebInputElement lastname
=
2749 web_frame
->document().getElementById("lastname").to
<WebInputElement
>();
2750 lastname
.setAutofilled(true);
2751 WebInputElement email
=
2752 web_frame
->document().getElementById("email").to
<WebInputElement
>();
2753 email
.setAutofilled(true);
2754 WebInputElement email2
=
2755 web_frame
->document().getElementById("email2").to
<WebInputElement
>();
2756 email2
.setAutofilled(true);
2757 WebInputElement phone
=
2758 web_frame
->document().getElementById("phone").to
<WebInputElement
>();
2759 phone
.setAutofilled(true);
2762 // Set the suggested values on all of the elements.
2763 firstname
.setSuggestedValue(ASCIIToUTF16("Wyatt"));
2764 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
2765 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2766 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2767 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2769 // Clear the previewed fields.
2770 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname
, false));
2772 // Fields with non-empty values are restored.
2773 EXPECT_EQ(ASCIIToUTF16("W"), firstname
.value());
2774 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
2775 EXPECT_FALSE(firstname
.isAutofilled());
2776 EXPECT_EQ(1, firstname
.selectionStart());
2777 EXPECT_EQ(1, firstname
.selectionEnd());
2779 // Verify the previewed fields are cleared.
2780 EXPECT_TRUE(lastname
.value().isEmpty());
2781 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
2782 EXPECT_FALSE(lastname
.isAutofilled());
2783 EXPECT_TRUE(email
.value().isEmpty());
2784 EXPECT_TRUE(email
.suggestedValue().isEmpty());
2785 EXPECT_FALSE(email
.isAutofilled());
2786 EXPECT_TRUE(email2
.value().isEmpty());
2787 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
2788 EXPECT_FALSE(email2
.isAutofilled());
2789 EXPECT_TRUE(phone
.value().isEmpty());
2790 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
2791 EXPECT_FALSE(phone
.isAutofilled());
2794 TEST_F(FormAutofillTest
, ClearPreviewedFormWithAutofilledInitiatingNode
) {
2795 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2796 " <INPUT type=\"text\" id=\"firstname\" value=\"W\"/>"
2797 " <INPUT type=\"text\" id=\"lastname\"/>"
2798 " <INPUT type=\"text\" id=\"email\"/>"
2799 " <INPUT type=\"email\" id=\"email2\"/>"
2800 " <INPUT type=\"tel\" id=\"phone\"/>"
2801 " <INPUT type=\"submit\" value=\"Send\"/>"
2804 WebFrame
* web_frame
= GetMainFrame();
2805 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2807 FormCache form_cache
;
2808 std::vector
<FormData
> forms
;
2809 form_cache
.ExtractForms(*web_frame
, &forms
);
2810 ASSERT_EQ(1U, forms
.size());
2812 // Set the auto-filled attribute.
2813 WebInputElement firstname
=
2814 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2815 firstname
.setAutofilled(true);
2816 WebInputElement lastname
=
2817 web_frame
->document().getElementById("lastname").to
<WebInputElement
>();
2818 lastname
.setAutofilled(true);
2819 WebInputElement email
=
2820 web_frame
->document().getElementById("email").to
<WebInputElement
>();
2821 email
.setAutofilled(true);
2822 WebInputElement email2
=
2823 web_frame
->document().getElementById("email2").to
<WebInputElement
>();
2824 email2
.setAutofilled(true);
2825 WebInputElement phone
=
2826 web_frame
->document().getElementById("phone").to
<WebInputElement
>();
2827 phone
.setAutofilled(true);
2829 // Set the suggested values on all of the elements.
2830 firstname
.setSuggestedValue(ASCIIToUTF16("Wyatt"));
2831 lastname
.setSuggestedValue(ASCIIToUTF16("Earp"));
2832 email
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2833 email2
.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
2834 phone
.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
2836 // Clear the previewed fields.
2837 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname
, true));
2839 // Fields with non-empty values are restored.
2840 EXPECT_EQ(ASCIIToUTF16("W"), firstname
.value());
2841 EXPECT_TRUE(firstname
.suggestedValue().isEmpty());
2842 EXPECT_TRUE(firstname
.isAutofilled());
2843 EXPECT_EQ(1, firstname
.selectionStart());
2844 EXPECT_EQ(1, firstname
.selectionEnd());
2846 // Verify the previewed fields are cleared.
2847 EXPECT_TRUE(lastname
.value().isEmpty());
2848 EXPECT_TRUE(lastname
.suggestedValue().isEmpty());
2849 EXPECT_FALSE(lastname
.isAutofilled());
2850 EXPECT_TRUE(email
.value().isEmpty());
2851 EXPECT_TRUE(email
.suggestedValue().isEmpty());
2852 EXPECT_FALSE(email
.isAutofilled());
2853 EXPECT_TRUE(email2
.value().isEmpty());
2854 EXPECT_TRUE(email2
.suggestedValue().isEmpty());
2855 EXPECT_FALSE(email2
.isAutofilled());
2856 EXPECT_TRUE(phone
.value().isEmpty());
2857 EXPECT_TRUE(phone
.suggestedValue().isEmpty());
2858 EXPECT_FALSE(phone
.isAutofilled());
2861 TEST_F(FormAutofillTest
, FormWithNodeIsAutofilled
) {
2862 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">"
2863 " <INPUT type=\"text\" id=\"firstname\" value=\"Wyatt\"/>"
2864 " <INPUT type=\"text\" id=\"lastname\"/>"
2865 " <INPUT type=\"text\" id=\"email\"/>"
2866 " <INPUT type=\"email\" id=\"email2\"/>"
2867 " <INPUT type=\"tel\" id=\"phone\"/>"
2868 " <INPUT type=\"submit\" value=\"Send\"/>"
2871 WebFrame
* web_frame
= GetMainFrame();
2872 ASSERT_NE(static_cast<WebFrame
*>(NULL
), web_frame
);
2874 FormCache form_cache
;
2875 std::vector
<FormData
> forms
;
2876 form_cache
.ExtractForms(*web_frame
, &forms
);
2877 ASSERT_EQ(1U, forms
.size());
2879 WebInputElement firstname
=
2880 web_frame
->document().getElementById("firstname").to
<WebInputElement
>();
2882 // Auto-filled attribute not set yet.
2883 EXPECT_FALSE(FormWithElementIsAutofilled(firstname
));
2885 // Set the auto-filled attribute.
2886 firstname
.setAutofilled(true);
2888 EXPECT_TRUE(FormWithElementIsAutofilled(firstname
));
2891 // If we have multiple labels per id, the labels concatenated into label string.
2892 TEST_F(FormAutofillTest
, MultipleLabelsPerElement
) {
2893 std::vector
<string16
> labels
, names
, values
;
2895 labels
.push_back(ASCIIToUTF16("First Name:"));
2896 names
.push_back(ASCIIToUTF16("firstname"));
2897 values
.push_back(ASCIIToUTF16("John"));
2899 labels
.push_back(ASCIIToUTF16("Last Name:"));
2900 names
.push_back(ASCIIToUTF16("lastname"));
2901 values
.push_back(ASCIIToUTF16("Smith"));
2903 labels
.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
2904 names
.push_back(ASCIIToUTF16("email"));
2905 values
.push_back(ASCIIToUTF16("john@example.com"));
2908 "<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2909 " <LABEL for=\"firstname\"> First Name: </LABEL>"
2910 " <LABEL for=\"firstname\"></LABEL>"
2911 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
2912 " <LABEL for=\"lastname\"></LABEL>"
2913 " <LABEL for=\"lastname\"> Last Name: </LABEL>"
2914 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
2915 " <LABEL for=\"email\"> Email: </LABEL>"
2916 " <LABEL for=\"email\"> xxx@yyy.com </LABEL>"
2917 " <INPUT type=\"text\" id=\"email\" value=\"john@example.com\"/>"
2918 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2920 labels
, names
, values
);
2923 TEST_F(FormAutofillTest
, ClickElement
) {
2924 LoadHTML("<BUTTON id=\"link\">Button</BUTTON>"
2925 "<BUTTON name=\"button\">Button</BUTTON>");
2926 WebFrame
* frame
= GetMainFrame();
2927 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
2929 // Successful retrieval by id.
2930 autofill::WebElementDescriptor clicker
;
2931 clicker
.retrieval_method
= autofill::WebElementDescriptor::ID
;
2932 clicker
.descriptor
= "link";
2933 EXPECT_TRUE(ClickElement(frame
->document(), clicker
));
2935 // Successful retrieval by css selector.
2936 clicker
.retrieval_method
= autofill::WebElementDescriptor::CSS_SELECTOR
;
2937 clicker
.descriptor
= "button[name=\"button\"]";
2938 EXPECT_TRUE(ClickElement(frame
->document(), clicker
));
2940 // Unsuccessful retrieval due to invalid CSS selector.
2941 clicker
.descriptor
= "^*&";
2942 EXPECT_FALSE(ClickElement(frame
->document(), clicker
));
2944 // Unsuccessful retrieval because element does not exist.
2945 clicker
.descriptor
= "#junk";
2946 EXPECT_FALSE(ClickElement(frame
->document(), clicker
));
2949 TEST_F(FormAutofillTest
, SelectOneAsText
) {
2950 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">"
2951 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>"
2952 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>"
2953 " <SELECT id=\"country\">"
2954 " <OPTION value=\"AF\">Afghanistan</OPTION>"
2955 " <OPTION value=\"AL\">Albania</OPTION>"
2956 " <OPTION value=\"DZ\">Algeria</OPTION>"
2958 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>"
2961 WebFrame
* frame
= GetMainFrame();
2962 ASSERT_NE(static_cast<WebFrame
*>(NULL
), frame
);
2964 // Set the value of the select-one.
2965 WebSelectElement select_element
=
2966 frame
->document().getElementById("country").to
<WebSelectElement
>();
2967 select_element
.setValue(WebString::fromUTF8("AL"));
2969 WebVector
<WebFormElement
> forms
;
2970 frame
->document().forms(forms
);
2971 ASSERT_EQ(1U, forms
.size());
2975 // Extract the country select-one value as text.
2976 EXPECT_TRUE(WebFormElementToFormData(
2977 forms
[0], WebFormControlElement(), autofill::REQUIRE_NONE
,
2978 static_cast<autofill::ExtractMask
>(
2979 autofill::EXTRACT_VALUE
| autofill::EXTRACT_OPTION_TEXT
),
2981 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
2982 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
2983 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
2985 const std::vector
<FormFieldData
>& fields
= form
.fields
;
2986 ASSERT_EQ(3U, fields
.size());
2988 FormFieldData expected
;
2990 expected
.name
= ASCIIToUTF16("firstname");
2991 expected
.value
= ASCIIToUTF16("John");
2992 expected
.form_control_type
= "text";
2993 expected
.max_length
= WebInputElement::defaultMaxLength();
2994 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
2996 expected
.name
= ASCIIToUTF16("lastname");
2997 expected
.value
= ASCIIToUTF16("Smith");
2998 expected
.form_control_type
= "text";
2999 expected
.max_length
= WebInputElement::defaultMaxLength();
3000 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
3002 expected
.name
= ASCIIToUTF16("country");
3003 expected
.value
= ASCIIToUTF16("Albania");
3004 expected
.form_control_type
= "select-one";
3005 expected
.max_length
= 0;
3006 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
3008 form
.fields
.clear();
3009 // Extract the country select-one value as value.
3010 EXPECT_TRUE(WebFormElementToFormData(forms
[0],
3011 WebFormControlElement(),
3012 autofill::REQUIRE_NONE
,
3013 autofill::EXTRACT_VALUE
,
3016 EXPECT_EQ(ASCIIToUTF16("TestForm"), form
.name
);
3017 EXPECT_EQ(GURL(frame
->document().url()), form
.origin
);
3018 EXPECT_EQ(GURL("http://cnn.com"), form
.action
);
3020 ASSERT_EQ(3U, fields
.size());
3022 expected
.name
= ASCIIToUTF16("firstname");
3023 expected
.value
= ASCIIToUTF16("John");
3024 expected
.form_control_type
= "text";
3025 expected
.max_length
= WebInputElement::defaultMaxLength();
3026 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[0]);
3028 expected
.name
= ASCIIToUTF16("lastname");
3029 expected
.value
= ASCIIToUTF16("Smith");
3030 expected
.form_control_type
= "text";
3031 expected
.max_length
= WebInputElement::defaultMaxLength();
3032 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[1]);
3034 expected
.name
= ASCIIToUTF16("country");
3035 expected
.value
= ASCIIToUTF16("AL");
3036 expected
.form_control_type
= "select-one";
3037 expected
.max_length
= 0;
3038 EXPECT_FORM_FIELD_DATA_EQUALS(expected
, fields
[2]);
3041 } // namespace autofill