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