Add testing/scripts/OWNERS
[chromium-blink-merge.git] / chrome / renderer / autofill / form_autofill_browsertest.cc
blobf1a742585ec80079107d5441e8126ce87c076ce9
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/autofill_data_validation.h"
17 #include "components/autofill/core/common/form_data.h"
18 #include "components/autofill/core/common/web_element_descriptor.h"
19 #include "components/variations/entropy_provider.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "third_party/WebKit/public/platform/WebString.h"
22 #include "third_party/WebKit/public/platform/WebVector.h"
23 #include "third_party/WebKit/public/web/WebDocument.h"
24 #include "third_party/WebKit/public/web/WebElement.h"
25 #include "third_party/WebKit/public/web/WebFormControlElement.h"
26 #include "third_party/WebKit/public/web/WebFormElement.h"
27 #include "third_party/WebKit/public/web/WebInputElement.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h"
29 #include "third_party/WebKit/public/web/WebNode.h"
30 #include "third_party/WebKit/public/web/WebSelectElement.h"
31 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
33 using base::ASCIIToUTF16;
34 using blink::WebDocument;
35 using blink::WebElement;
36 using blink::WebFormControlElement;
37 using blink::WebFormElement;
38 using blink::WebFrame;
39 using blink::WebInputElement;
40 using blink::WebNode;
41 using blink::WebSelectElement;
42 using blink::WebString;
43 using blink::WebTextAreaElement;
44 using blink::WebVector;
46 namespace {
48 struct AutofillFieldCase {
49 const char* const form_control_type;
50 const char* const name;
51 const char* const initial_value;
52 const char* const autocomplete_attribute; // The autocomplete attribute of
53 // the element.
54 bool should_be_autofilled; // Whether the filed should be autofilled.
55 const char* const autofill_value; // The value being used to fill the field.
56 const char* const expected_value; // The expected value after Autofill
57 // or Preview.
60 static const char kFormHtml[] =
61 "<FORM name='TestForm' action='http://buh.com' method='post'>"
62 " <INPUT type='text' id='firstname'/>"
63 " <INPUT type='text' id='lastname'/>"
64 " <INPUT type='hidden' id='imhidden'/>"
65 " <INPUT type='text' id='notempty' value='Hi'/>"
66 " <INPUT type='text' autocomplete='off' id='noautocomplete'/>"
67 " <INPUT type='text' disabled='disabled' id='notenabled'/>"
68 " <INPUT type='text' readonly id='readonly'/>"
69 " <INPUT type='text' style='visibility: hidden'"
70 " id='invisible'/>"
71 " <INPUT type='text' style='display: none' id='displaynone'/>"
72 " <INPUT type='month' id='month'/>"
73 " <INPUT type='month' id='month-nonempty' value='2011-12'/>"
74 " <SELECT id='select'>"
75 " <OPTION></OPTION>"
76 " <OPTION value='CA'>California</OPTION>"
77 " <OPTION value='TX'>Texas</OPTION>"
78 " </SELECT>"
79 " <SELECT id='select-nonempty'>"
80 " <OPTION value='CA' selected>California</OPTION>"
81 " <OPTION value='TX'>Texas</OPTION>"
82 " </SELECT>"
83 " <SELECT id='select-unchanged'>"
84 " <OPTION value='CA' selected>California</OPTION>"
85 " <OPTION value='TX'>Texas</OPTION>"
86 " </SELECT>"
87 " <TEXTAREA id='textarea'></TEXTAREA>"
88 " <TEXTAREA id='textarea-nonempty'>Go&#10;away!</TEXTAREA>"
89 " <INPUT type='submit' name='reply-send' value='Send'/>"
90 "</FORM>";
92 } // namespace
94 namespace autofill {
96 class FormAutofillTest : public ChromeRenderViewTest {
97 public:
98 FormAutofillTest() : ChromeRenderViewTest() {}
99 ~FormAutofillTest() override {}
101 void ExpectLabels(const char* html,
102 const std::vector<base::string16>& labels,
103 const std::vector<base::string16>& names,
104 const std::vector<base::string16>& values) {
105 std::vector<std::string> control_types(labels.size(), "text");
106 ExpectLabelsAndTypes(html, labels, names, values, control_types);
109 void ExpectLabelsAndTypes(const char* html,
110 const std::vector<base::string16>& labels,
111 const std::vector<base::string16>& names,
112 const std::vector<base::string16>& values,
113 const std::vector<std::string>& control_types) {
114 ASSERT_EQ(labels.size(), names.size());
115 ASSERT_EQ(labels.size(), values.size());
116 ASSERT_EQ(labels.size(), control_types.size());
118 LoadHTML(html);
120 WebFrame* web_frame = GetMainFrame();
121 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
123 FormCache form_cache;
124 std::vector<FormData> forms;
125 form_cache.ExtractNewForms(*web_frame, &forms);
126 ASSERT_EQ(1U, forms.size());
128 const FormData& form = forms[0];
129 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
130 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
131 EXPECT_EQ(GURL("http://cnn.com"), form.action);
133 const std::vector<FormFieldData>& fields = form.fields;
134 ASSERT_EQ(labels.size(), fields.size());
135 for (size_t i = 0; i < labels.size(); ++i) {
136 int max_length = control_types[i] == "text" ?
137 WebInputElement::defaultMaxLength() : 0;
138 FormFieldData expected;
139 expected.label = labels[i];
140 expected.name = names[i];
141 expected.value = values[i];
142 expected.form_control_type = control_types[i];
143 expected.max_length = max_length;
144 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
145 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
149 void ExpectJohnSmithLabels(const char* html) {
150 std::vector<base::string16> labels, names, values;
152 labels.push_back(ASCIIToUTF16("First name:"));
153 names.push_back(ASCIIToUTF16("firstname"));
154 values.push_back(ASCIIToUTF16("John"));
156 labels.push_back(ASCIIToUTF16("Last name:"));
157 names.push_back(ASCIIToUTF16("lastname"));
158 values.push_back(ASCIIToUTF16("Smith"));
160 labels.push_back(ASCIIToUTF16("Email:"));
161 names.push_back(ASCIIToUTF16("email"));
162 values.push_back(ASCIIToUTF16("john@example.com"));
164 ExpectLabels(html, labels, names, values);
167 typedef void (*FillFormFunction)(const FormData& form,
168 const WebFormControlElement& element);
170 typedef WebString (*GetValueFunction)(WebFormControlElement element);
172 // Test FormFillxxx functions.
173 void TestFormFillFunctions(const char* html,
174 const AutofillFieldCase* field_cases,
175 size_t number_of_field_cases,
176 FillFormFunction fill_form_function,
177 GetValueFunction get_value_function) {
178 LoadHTML(html);
180 WebFrame* web_frame = GetMainFrame();
181 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
183 FormCache form_cache;
184 std::vector<FormData> forms;
185 form_cache.ExtractNewForms(*web_frame, &forms);
186 ASSERT_EQ(1U, forms.size());
188 // Get the input element we want to find.
189 WebElement element = web_frame->document().getElementById("firstname");
190 WebInputElement input_element = element.to<WebInputElement>();
192 // Find the form that contains the input element.
193 FormData form_data;
194 FormFieldData field;
195 EXPECT_TRUE(
196 FindFormAndFieldForFormControlElement(input_element,
197 &form_data,
198 &field,
199 autofill::REQUIRE_AUTOCOMPLETE));
200 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name);
201 EXPECT_EQ(GURL(web_frame->document().url()), form_data.origin);
202 EXPECT_EQ(GURL("http://buh.com"), form_data.action);
204 const std::vector<FormFieldData>& fields = form_data.fields;
205 ASSERT_EQ(number_of_field_cases, fields.size());
207 FormFieldData expected;
208 // Verify field's initial value.
209 for (size_t i = 0; i < number_of_field_cases; ++i) {
210 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
211 field_cases[i].name));
212 expected.form_control_type = field_cases[i].form_control_type;
213 expected.max_length =
214 expected.form_control_type == "text" ?
215 WebInputElement::defaultMaxLength() : 0;
216 expected.name = ASCIIToUTF16(field_cases[i].name);
217 expected.value = ASCIIToUTF16(field_cases[i].initial_value);
218 expected.autocomplete_attribute = field_cases[i].autocomplete_attribute;
219 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
220 // Fill the form_data for the field.
221 form_data.fields[i].value = ASCIIToUTF16(field_cases[i].autofill_value);
222 // Set the is_autofilled property for the field.
223 form_data.fields[i].is_autofilled = field_cases[i].should_be_autofilled;
226 // Autofill the form using the given fill form function.
227 fill_form_function(form_data, input_element);
229 // Validate Autofill or Preview results.
230 for (size_t i = 0; i < number_of_field_cases; ++i) {
231 ValidteFilledField(field_cases[i], get_value_function);
235 // Validate an Autofilled field.
236 void ValidteFilledField(const AutofillFieldCase& field_case,
237 GetValueFunction get_value_function) {
238 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
239 field_case.name));
240 WebString value;
241 WebFormControlElement element = GetMainFrame()->document().getElementById(
242 ASCIIToUTF16(field_case.name)).to<WebFormControlElement>();
243 if ((element.formControlType() == "select-one") ||
244 (element.formControlType() == "textarea")) {
245 value = get_value_function(element);
246 } else {
247 ASSERT_TRUE(element.formControlType() == "text" ||
248 element.formControlType() == "month");
249 value = get_value_function(element);
252 const WebString expected_value = ASCIIToUTF16(field_case.expected_value);
253 if (expected_value.isEmpty())
254 EXPECT_TRUE(value.isEmpty());
255 else
256 EXPECT_EQ(expected_value, value);
258 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled());
261 static void FillFormForAllFieldsWrapper(const FormData& form,
262 const WebInputElement& element) {
263 FillFormForAllElements(form, element.form());
266 static void FillFormIncludingNonFocusableElementsWrapper(
267 const FormData& form,
268 const WebFormControlElement& element) {
269 FillFormIncludingNonFocusableElements(form, element.form());
272 static WebString GetValueWrapper(WebFormControlElement element) {
273 if (element.formControlType() == "textarea")
274 return element.to<WebTextAreaElement>().value();
276 if (element.formControlType() == "select-one")
277 return element.to<WebSelectElement>().value();
279 return element.to<WebInputElement>().value();
282 static WebString GetSuggestedValueWrapper(WebFormControlElement element) {
283 if (element.formControlType() == "textarea")
284 return element.to<WebTextAreaElement>().suggestedValue();
286 if (element.formControlType() == "select-one")
287 return element.to<WebSelectElement>().suggestedValue();
289 return element.to<WebInputElement>().suggestedValue();
292 private:
293 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest);
296 // We should be able to extract a normal text field.
297 TEST_F(FormAutofillTest, WebFormControlElementToFormField) {
298 LoadHTML("<INPUT type='text' id='element' value='value'/>");
300 WebFrame* frame = GetMainFrame();
301 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
303 WebElement web_element = frame->document().getElementById("element");
304 WebFormControlElement element = web_element.to<WebFormControlElement>();
305 FormFieldData result1;
306 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result1);
308 FormFieldData expected;
309 expected.form_control_type = "text";
310 expected.max_length = WebInputElement::defaultMaxLength();
312 expected.name = ASCIIToUTF16("element");
313 expected.value = base::string16();
314 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
316 FormFieldData result2;
317 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result2);
319 expected.name = ASCIIToUTF16("element");
320 expected.value = ASCIIToUTF16("value");
321 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
324 // We should be able to extract a text field with autocomplete="off".
325 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompleteOff) {
326 LoadHTML("<INPUT type='text' id='element' value='value'"
327 " autocomplete='off'/>");
329 WebFrame* frame = GetMainFrame();
330 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
332 WebElement web_element = frame->document().getElementById("element");
333 WebFormControlElement element = web_element.to<WebFormControlElement>();
334 FormFieldData result;
335 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
337 FormFieldData expected;
338 expected.name = ASCIIToUTF16("element");
339 expected.value = ASCIIToUTF16("value");
340 expected.form_control_type = "text";
341 expected.autocomplete_attribute = "off";
342 expected.max_length = WebInputElement::defaultMaxLength();
343 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
346 // We should be able to extract a text field with maxlength specified.
347 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMaxLength) {
348 LoadHTML("<INPUT type='text' id='element' value='value'"
349 " maxlength='5'/>");
351 WebFrame* frame = GetMainFrame();
352 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
354 WebElement web_element = frame->document().getElementById("element");
355 WebFormControlElement element = web_element.to<WebFormControlElement>();
356 FormFieldData result;
357 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
359 FormFieldData expected;
360 expected.name = ASCIIToUTF16("element");
361 expected.value = ASCIIToUTF16("value");
362 expected.form_control_type = "text";
363 expected.max_length = 5;
364 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
367 // We should be able to extract a text field that has been autofilled.
368 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutofilled) {
369 LoadHTML("<INPUT type='text' id='element' value='value'/>");
371 WebFrame* frame = GetMainFrame();
372 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
374 WebElement web_element = frame->document().getElementById("element");
375 WebInputElement element = web_element.to<WebInputElement>();
376 element.setAutofilled(true);
377 FormFieldData result;
378 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
380 FormFieldData expected;
381 expected.name = ASCIIToUTF16("element");
382 expected.value = ASCIIToUTF16("value");
383 expected.form_control_type = "text";
384 expected.max_length = WebInputElement::defaultMaxLength();
385 expected.is_autofilled = true;
386 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
389 // We should be able to extract a radio or a checkbox field that has been
390 // autofilled.
391 TEST_F(FormAutofillTest, WebFormControlElementToClickableFormField) {
392 LoadHTML("<INPUT type='checkbox' id='checkbox' value='mail' checked/>"
393 "<INPUT type='radio' id='radio' value='male'/>");
395 WebFrame* frame = GetMainFrame();
396 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
398 WebElement web_element = frame->document().getElementById("checkbox");
399 WebInputElement element = web_element.to<WebInputElement>();
400 element.setAutofilled(true);
401 FormFieldData result;
402 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
404 FormFieldData expected;
405 expected.name = ASCIIToUTF16("checkbox");
406 expected.value = ASCIIToUTF16("mail");
407 expected.form_control_type = "checkbox";
408 expected.is_autofilled = true;
409 expected.is_checkable = true;
410 expected.is_checked = true;
411 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
413 web_element = frame->document().getElementById("radio");
414 element = web_element.to<WebInputElement>();
415 element.setAutofilled(true);
416 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
417 expected.name = ASCIIToUTF16("radio");
418 expected.value = ASCIIToUTF16("male");
419 expected.form_control_type = "radio";
420 expected.is_autofilled = true;
421 expected.is_checkable = true;
422 expected.is_checked = false;
423 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
426 // We should be able to extract a <select> field.
427 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) {
428 LoadHTML("<SELECT id='element'/>"
429 " <OPTION value='CA'>California</OPTION>"
430 " <OPTION value='TX'>Texas</OPTION>"
431 "</SELECT>");
433 WebFrame* frame = GetMainFrame();
434 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
436 WebElement web_element = frame->document().getElementById("element");
437 WebFormControlElement element = web_element.to<WebFormControlElement>();
438 FormFieldData result1;
439 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result1);
441 FormFieldData expected;
442 expected.name = ASCIIToUTF16("element");
443 expected.max_length = 0;
444 expected.form_control_type = "select-one";
446 expected.value = ASCIIToUTF16("CA");
447 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
449 FormFieldData result2;
450 WebFormControlElementToFormField(
451 element,
452 static_cast<autofill::ExtractMask>(autofill::EXTRACT_VALUE |
453 autofill::EXTRACT_OPTION_TEXT),
454 &result2);
455 expected.value = ASCIIToUTF16("California");
456 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
458 FormFieldData result3;
459 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS,
460 &result3);
461 expected.value = base::string16();
462 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result3);
464 ASSERT_EQ(2U, result3.option_values.size());
465 ASSERT_EQ(2U, result3.option_contents.size());
466 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]);
467 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]);
468 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]);
469 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]);
472 // When faced with <select> field with *many* options, we should trim them to a
473 // reasonable number.
474 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldLongSelect) {
475 std::string html = "<SELECT id='element'/>";
476 for (size_t i = 0; i < 2 * kMaxListSize; ++i) {
477 html += base::StringPrintf("<OPTION value='%" PRIuS "'>"
478 "%" PRIuS "</OPTION>", i, i);
480 html += "</SELECT>";
481 LoadHTML(html.c_str());
483 WebFrame* frame = GetMainFrame();
484 ASSERT_TRUE(frame);
486 WebElement web_element = frame->document().getElementById("element");
487 WebFormControlElement element = web_element.to<WebFormControlElement>();
488 FormFieldData result;
489 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS, &result);
491 EXPECT_EQ(0U, result.option_values.size());
492 EXPECT_EQ(0U, result.option_contents.size());
495 // We should be able to extract a <textarea> field.
496 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) {
497 LoadHTML("<TEXTAREA id='element'>"
498 "This element's value&#10;"
499 "spans multiple lines."
500 "</TEXTAREA>");
502 WebFrame* frame = GetMainFrame();
503 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
505 WebElement web_element = frame->document().getElementById("element");
506 WebFormControlElement element = web_element.to<WebFormControlElement>();
507 FormFieldData result_sans_value;
508 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
509 &result_sans_value);
511 FormFieldData expected;
512 expected.name = ASCIIToUTF16("element");
513 expected.max_length = 0;
514 expected.form_control_type = "textarea";
515 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
517 FormFieldData result_with_value;
518 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
519 &result_with_value);
520 expected.value = ASCIIToUTF16("This element's value\n"
521 "spans multiple lines.");
522 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
525 // We should be able to extract an <input type="month"> field.
526 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMonthInput) {
527 LoadHTML("<INPUT type='month' id='element' value='2011-12'>");
529 WebFrame* frame = GetMainFrame();
530 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
532 WebElement web_element = frame->document().getElementById("element");
533 WebFormControlElement element = web_element.to<WebFormControlElement>();
534 FormFieldData result_sans_value;
535 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
536 &result_sans_value);
538 FormFieldData expected;
539 expected.name = ASCIIToUTF16("element");
540 expected.max_length = 0;
541 expected.form_control_type = "month";
542 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
544 FormFieldData result_with_value;
545 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
546 &result_with_value);
547 expected.value = ASCIIToUTF16("2011-12");
548 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
551 // We should not extract the value for non-text and non-select fields.
552 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) {
553 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
554 " <INPUT type='hidden' id='hidden' value='apple'/>"
555 " <INPUT type='submit' id='submit' value='Send'/>"
556 "</FORM>");
558 WebFrame* frame = GetMainFrame();
559 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
561 WebElement web_element = frame->document().getElementById("hidden");
562 WebFormControlElement element = web_element.to<WebFormControlElement>();
563 FormFieldData result;
564 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
566 FormFieldData expected;
567 expected.max_length = 0;
569 expected.name = ASCIIToUTF16("hidden");
570 expected.form_control_type = "hidden";
571 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
573 web_element = frame->document().getElementById("submit");
574 element = web_element.to<WebFormControlElement>();
575 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
576 expected.name = ASCIIToUTF16("submit");
577 expected.form_control_type = "submit";
578 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
581 // We should be able to extract password fields.
582 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) {
583 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
584 " <INPUT type='password' id='password' value='secret'/>"
585 "</FORM>");
587 WebFrame* frame = GetMainFrame();
588 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
590 WebElement web_element = frame->document().getElementById("password");
591 WebFormControlElement element = web_element.to<WebFormControlElement>();
592 FormFieldData result;
593 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
595 FormFieldData expected;
596 expected.max_length = WebInputElement::defaultMaxLength();
597 expected.name = ASCIIToUTF16("password");
598 expected.form_control_type = "password";
599 expected.value = ASCIIToUTF16("secret");
600 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
603 // We should be able to extract the autocompletetype attribute.
604 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) {
605 std::string html =
606 "<INPUT type='text' id='absent'/>"
607 "<INPUT type='text' id='empty' autocomplete=''/>"
608 "<INPUT type='text' id='off' autocomplete='off'/>"
609 "<INPUT type='text' id='regular' autocomplete='email'/>"
610 "<INPUT type='text' id='multi-valued' "
611 " autocomplete='billing email'/>"
612 "<INPUT type='text' id='experimental' x-autocompletetype='email'/>"
613 "<INPUT type='month' id='month' autocomplete='cc-exp'/>"
614 "<SELECT id='select' autocomplete='state'/>"
615 " <OPTION value='CA'>California</OPTION>"
616 " <OPTION value='TX'>Texas</OPTION>"
617 "</SELECT>"
618 "<TEXTAREA id='textarea' autocomplete='street-address'>"
619 " Some multi-"
620 " lined value"
621 "</TEXTAREA>";
622 html +=
623 "<INPUT type='text' id='malicious' autocomplete='" +
624 std::string(10000, 'x') + "'/>";
625 LoadHTML(html.c_str());
627 WebFrame* frame = GetMainFrame();
628 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
630 struct TestCase {
631 const std::string element_id;
632 const std::string form_control_type;
633 const std::string autocomplete_attribute;
635 TestCase test_cases[] = {
636 // An absent attribute is equivalent to an empty one.
637 { "absent", "text", "" },
638 // Make sure there are no issues parsing an empty attribute.
639 { "empty", "text", "" },
640 // Make sure there are no issues parsing an attribute value that isn't a
641 // type hint.
642 { "off", "text", "off" },
643 // Common case: exactly one type specified.
644 { "regular", "text", "email" },
645 // Verify that we correctly extract multiple tokens as well.
646 { "multi-valued", "text", "billing email" },
647 // Verify that <input type="month"> fields are supported.
648 { "month", "month", "cc-exp" },
649 // We previously extracted this data from the experimental
650 // 'x-autocompletetype' attribute. Now that the field type hints are part
651 // of the spec under the autocomplete attribute, we no longer support the
652 // experimental version.
653 { "experimental", "text", "" },
654 // <select> elements should behave no differently from text fields here.
655 { "select", "select-one", "state" },
656 // <textarea> elements should also behave no differently from text fields.
657 { "textarea", "textarea", "street-address" },
658 // Very long attribute values should be replaced by a default string, to
659 // prevent malicious websites from DOSing the browser process.
660 { "malicious", "text", "x-max-data-length-exceeded" },
663 for (size_t i = 0; i < arraysize(test_cases); ++i) {
664 WebElement web_element = frame->document().getElementById(
665 ASCIIToUTF16(test_cases[i].element_id));
666 WebFormControlElement element = web_element.to<WebFormControlElement>();
667 FormFieldData result;
668 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result);
670 FormFieldData expected;
671 expected.name = ASCIIToUTF16(test_cases[i].element_id);
672 expected.form_control_type = test_cases[i].form_control_type;
673 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute;
674 if (test_cases[i].form_control_type == "text")
675 expected.max_length = WebInputElement::defaultMaxLength();
676 else
677 expected.max_length = 0;
679 SCOPED_TRACE(test_cases[i].element_id);
680 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
684 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectStyle) {
685 LoadHTML("<STYLE>input{direction:rtl}</STYLE>"
686 "<FORM>"
687 " <INPUT type='text' id='element'>"
688 "</FORM>");
690 WebFrame* frame = GetMainFrame();
691 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
693 WebElement web_element = frame->document().getElementById("element");
694 WebFormControlElement element = web_element.to<WebFormControlElement>();
696 FormFieldData result;
697 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
698 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
701 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectDIRAttribute) {
702 LoadHTML("<FORM>"
703 " <INPUT dir='rtl' type='text' id='element'/>"
704 "</FORM>");
706 WebFrame* frame = GetMainFrame();
707 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
709 WebElement web_element = frame->document().getElementById("element");
710 WebFormControlElement element = web_element.to<WebFormControlElement>();
712 FormFieldData result;
713 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
714 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
717 TEST_F(FormAutofillTest, DetectTextDirectionFromParentStyle) {
718 LoadHTML("<STYLE>form{direction:rtl}</STYLE>"
719 "<FORM>"
720 " <INPUT type='text' id='element'/>"
721 "</FORM>");
723 WebFrame* frame = GetMainFrame();
724 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
726 WebElement web_element = frame->document().getElementById("element");
727 WebFormControlElement element = web_element.to<WebFormControlElement>();
729 FormFieldData result;
730 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
731 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
734 TEST_F(FormAutofillTest, DetectTextDirectionFromParentDIRAttribute) {
735 LoadHTML("<FORM dir='rtl'>"
736 " <INPUT type='text' id='element'/>"
737 "</FORM>");
739 WebFrame* frame = GetMainFrame();
740 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
742 WebElement web_element = frame->document().getElementById("element");
743 WebFormControlElement element = web_element.to<WebFormControlElement>();
745 FormFieldData result;
746 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
747 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
750 TEST_F(FormAutofillTest, DetectTextDirectionWhenStyleAndDIRAttributMixed) {
751 LoadHTML("<STYLE>input{direction:ltr}</STYLE>"
752 "<FORM dir='rtl'>"
753 " <INPUT type='text' id='element'/>"
754 "</FORM>");
756 WebFrame* frame = GetMainFrame();
757 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
759 WebElement web_element = frame->document().getElementById("element");
760 WebFormControlElement element = web_element.to<WebFormControlElement>();
762 FormFieldData result;
763 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
764 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
767 TEST_F(FormAutofillTest,
768 DetectTextDirectionWhenParentHasBothDIRAttributeAndStyle) {
769 LoadHTML("<STYLE>form{direction:ltr}</STYLE>"
770 "<FORM dir='rtl'>"
771 " <INPUT type='text' id='element'/>"
772 "</FORM>");
774 WebFrame* frame = GetMainFrame();
775 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
777 WebElement web_element = frame->document().getElementById("element");
778 WebFormControlElement element = web_element.to<WebFormControlElement>();
780 FormFieldData result;
781 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
782 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
785 TEST_F(FormAutofillTest, DetectTextDirectionWhenAncestorHasInlineStyle) {
786 LoadHTML("<FORM style='direction:ltr'>"
787 " <SPAN dir='rtl'>"
788 " <INPUT type='text' id='element'/>"
789 " </SPAN>"
790 "</FORM>");
792 WebFrame* frame = GetMainFrame();
793 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
795 WebElement web_element = frame->document().getElementById("element");
796 WebFormControlElement element = web_element.to<WebFormControlElement>();
798 FormFieldData result;
799 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
800 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
803 TEST_F(FormAutofillTest, WebFormElementToFormData) {
804 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
805 " <LABEL for='firstname'>First name:</LABEL>"
806 " <INPUT type='text' id='firstname' value='John'/>"
807 " <LABEL for='lastname'>Last name:</LABEL>"
808 " <INPUT type='text' id='lastname' value='Smith'/>"
809 " <LABEL for='street-address'>Address:</LABEL>"
810 " <TEXTAREA id='street-address'>"
811 "123 Fantasy Ln.&#10;"
812 "Apt. 42"
813 "</TEXTAREA>"
814 " <LABEL for='state'>State:</LABEL>"
815 " <SELECT id='state'/>"
816 " <OPTION value='CA'>California</OPTION>"
817 " <OPTION value='TX'>Texas</OPTION>"
818 " </SELECT>"
819 " <LABEL for='password'>Password:</LABEL>"
820 " <INPUT type='password' id='password' value='secret'/>"
821 " <LABEL for='month'>Card expiration:</LABEL>"
822 " <INPUT type='month' id='month' value='2011-12'/>"
823 " <INPUT type='submit' name='reply-send' value='Send'/>"
824 // The below inputs should be ignored
825 " <LABEL for='notvisible'>Hidden:</LABEL>"
826 " <INPUT type='hidden' id='notvisible' value='apple'/>"
827 "</FORM>");
829 WebFrame* frame = GetMainFrame();
830 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
832 WebVector<WebFormElement> forms;
833 frame->document().forms(forms);
834 ASSERT_EQ(1U, forms.size());
836 WebElement element = frame->document().getElementById("firstname");
837 WebInputElement input_element = element.to<WebInputElement>();
839 FormData form;
840 FormFieldData field;
841 EXPECT_TRUE(WebFormElementToFormData(forms[0],
842 input_element,
843 autofill::REQUIRE_NONE,
844 autofill::EXTRACT_VALUE,
845 &form,
846 &field));
847 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
848 EXPECT_EQ(GURL(frame->document().url()), form.origin);
849 EXPECT_EQ(GURL("http://cnn.com"), form.action);
851 const std::vector<FormFieldData>& fields = form.fields;
852 ASSERT_EQ(6U, fields.size());
854 FormFieldData expected;
855 expected.name = ASCIIToUTF16("firstname");
856 expected.value = ASCIIToUTF16("John");
857 expected.label = ASCIIToUTF16("First name:");
858 expected.form_control_type = "text";
859 expected.max_length = WebInputElement::defaultMaxLength();
860 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
862 expected.name = ASCIIToUTF16("lastname");
863 expected.value = ASCIIToUTF16("Smith");
864 expected.label = ASCIIToUTF16("Last name:");
865 expected.form_control_type = "text";
866 expected.max_length = WebInputElement::defaultMaxLength();
867 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
869 expected.name = ASCIIToUTF16("street-address");
870 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
871 expected.label = ASCIIToUTF16("Address:");
872 expected.form_control_type = "textarea";
873 expected.max_length = 0;
874 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
876 expected.name = ASCIIToUTF16("state");
877 expected.value = ASCIIToUTF16("CA");
878 expected.label = ASCIIToUTF16("State:");
879 expected.form_control_type = "select-one";
880 expected.max_length = 0;
881 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
883 expected.name = ASCIIToUTF16("password");
884 expected.value = ASCIIToUTF16("secret");
885 expected.label = ASCIIToUTF16("Password:");
886 expected.form_control_type = "password";
887 expected.max_length = WebInputElement::defaultMaxLength();
888 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
890 expected.name = ASCIIToUTF16("month");
891 expected.value = ASCIIToUTF16("2011-12");
892 expected.label = ASCIIToUTF16("Card expiration:");
893 expected.form_control_type = "month";
894 expected.max_length = 0;
895 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
898 // We should not be able to serialize a form with too many fillable fields.
899 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) {
900 std::string html =
901 "<FORM name='TestForm' action='http://cnn.com' method='post'>";
902 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) {
903 html += "<INPUT type='text'/>";
905 html += "</FORM>";
906 LoadHTML(html.c_str());
908 WebFrame* frame = GetMainFrame();
909 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
911 WebVector<WebFormElement> forms;
912 frame->document().forms(forms);
913 ASSERT_EQ(1U, forms.size());
915 WebElement element = frame->document().getElementById("firstname");
916 WebInputElement input_element = element.to<WebInputElement>();
918 FormData form;
919 FormFieldData field;
920 EXPECT_FALSE(WebFormElementToFormData(forms[0],
921 input_element,
922 autofill::REQUIRE_NONE,
923 autofill::EXTRACT_VALUE,
924 &form,
925 &field));
928 TEST_F(FormAutofillTest, ExtractForms) {
929 ExpectJohnSmithLabels(
930 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
931 " First name: <INPUT type='text' id='firstname' value='John'/>"
932 " Last name: <INPUT type='text' id='lastname' value='Smith'/>"
933 " Email: <INPUT type='text' id='email' value='john@example.com'/>"
934 " <INPUT type='submit' name='reply-send' value='Send'/>"
935 "</FORM>");
938 TEST_F(FormAutofillTest, ExtractMultipleForms) {
939 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
940 " <INPUT type='text' id='firstname' value='John'/>"
941 " <INPUT type='text' id='lastname' value='Smith'/>"
942 " <INPUT type='text' id='email' value='john@example.com'/>"
943 " <INPUT type='submit' name='reply-send' value='Send'/>"
944 "</FORM>"
945 "<FORM name='TestForm2' action='http://zoo.com' method='post'>"
946 " <INPUT type='text' id='firstname' value='Jack'/>"
947 " <INPUT type='text' id='lastname' value='Adams'/>"
948 " <INPUT type='text' id='email' value='jack@example.com'/>"
949 " <INPUT type='submit' name='reply-send' value='Send'/>"
950 "</FORM>");
952 WebFrame* web_frame = GetMainFrame();
953 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
955 FormCache form_cache;
956 std::vector<FormData> forms;
957 form_cache.ExtractNewForms(*web_frame, &forms);
958 ASSERT_EQ(2U, forms.size());
960 // First form.
961 const FormData& form = forms[0];
962 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
963 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
964 EXPECT_EQ(GURL("http://cnn.com"), form.action);
966 const std::vector<FormFieldData>& fields = form.fields;
967 ASSERT_EQ(3U, fields.size());
969 FormFieldData expected;
970 expected.form_control_type = "text";
971 expected.max_length = WebInputElement::defaultMaxLength();
973 expected.name = ASCIIToUTF16("firstname");
974 expected.value = ASCIIToUTF16("John");
975 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
977 expected.name = ASCIIToUTF16("lastname");
978 expected.value = ASCIIToUTF16("Smith");
979 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
981 expected.name = ASCIIToUTF16("email");
982 expected.value = ASCIIToUTF16("john@example.com");
983 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
985 // Second form.
986 const FormData& form2 = forms[1];
987 EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2.name);
988 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
989 EXPECT_EQ(GURL("http://zoo.com"), form2.action);
991 const std::vector<FormFieldData>& fields2 = form2.fields;
992 ASSERT_EQ(3U, fields2.size());
994 expected.name = ASCIIToUTF16("firstname");
995 expected.value = ASCIIToUTF16("Jack");
996 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
998 expected.name = ASCIIToUTF16("lastname");
999 expected.value = ASCIIToUTF16("Adams");
1000 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1002 expected.name = ASCIIToUTF16("email");
1003 expected.value = ASCIIToUTF16("jack@example.com");
1004 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1007 TEST_F(FormAutofillTest, OnlyExtractNewForms) {
1008 LoadHTML(
1009 "<FORM id='testform' action='http://cnn.com' method='post'>"
1010 " <INPUT type='text' id='firstname' value='John'/>"
1011 " <INPUT type='text' id='lastname' value='Smith'/>"
1012 " <INPUT type='text' id='email' value='john@example.com'/>"
1013 " <INPUT type='submit' name='reply-send' value='Send'/>"
1014 "</FORM>");
1016 WebFrame* web_frame = GetMainFrame();
1017 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1019 FormCache form_cache;
1020 std::vector<FormData> forms;
1021 form_cache.ExtractNewForms(*web_frame, &forms);
1022 ASSERT_EQ(1U, forms.size());
1023 forms.clear();
1025 // Second call should give nothing as there are no new forms.
1026 form_cache.ExtractNewForms(*web_frame, &forms);
1027 ASSERT_EQ(0U, forms.size());
1029 // Append to the current form will re-extract.
1030 ExecuteJavaScript(
1031 "var newInput = document.createElement('input');"
1032 "newInput.setAttribute('type', 'text');"
1033 "newInput.setAttribute('id', 'telephone');"
1034 "newInput.value = '12345';"
1035 "document.getElementById('testform').appendChild(newInput);");
1036 msg_loop_.RunUntilIdle();
1038 form_cache.ExtractNewForms(*web_frame, &forms);
1039 ASSERT_EQ(1U, forms.size());
1041 const std::vector<FormFieldData>& fields = forms[0].fields;
1042 ASSERT_EQ(4U, fields.size());
1044 FormFieldData expected;
1045 expected.form_control_type = "text";
1046 expected.max_length = WebInputElement::defaultMaxLength();
1048 expected.name = ASCIIToUTF16("firstname");
1049 expected.value = ASCIIToUTF16("John");
1050 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1052 expected.name = ASCIIToUTF16("lastname");
1053 expected.value = ASCIIToUTF16("Smith");
1054 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1056 expected.name = ASCIIToUTF16("email");
1057 expected.value = ASCIIToUTF16("john@example.com");
1058 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1060 expected.name = ASCIIToUTF16("telephone");
1061 expected.value = ASCIIToUTF16("12345");
1062 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1064 forms.clear();
1066 // Completely new form will also be extracted.
1067 ExecuteJavaScript(
1068 "var newForm=document.createElement('form');"
1069 "newForm.id='new_testform';"
1070 "newForm.action='http://google.com';"
1071 "newForm.method='post';"
1072 "var newFirstname=document.createElement('input');"
1073 "newFirstname.setAttribute('type', 'text');"
1074 "newFirstname.setAttribute('id', 'second_firstname');"
1075 "newFirstname.value = 'Bob';"
1076 "var newLastname=document.createElement('input');"
1077 "newLastname.setAttribute('type', 'text');"
1078 "newLastname.setAttribute('id', 'second_lastname');"
1079 "newLastname.value = 'Hope';"
1080 "var newEmail=document.createElement('input');"
1081 "newEmail.setAttribute('type', 'text');"
1082 "newEmail.setAttribute('id', 'second_email');"
1083 "newEmail.value = 'bobhope@example.com';"
1084 "newForm.appendChild(newFirstname);"
1085 "newForm.appendChild(newLastname);"
1086 "newForm.appendChild(newEmail);"
1087 "document.body.appendChild(newForm);");
1088 msg_loop_.RunUntilIdle();
1090 web_frame = GetMainFrame();
1091 form_cache.ExtractNewForms(*web_frame, &forms);
1092 ASSERT_EQ(1U, forms.size());
1094 const std::vector<FormFieldData>& fields2 = forms[0].fields;
1095 ASSERT_EQ(3U, fields2.size());
1097 expected.name = ASCIIToUTF16("second_firstname");
1098 expected.value = ASCIIToUTF16("Bob");
1099 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1101 expected.name = ASCIIToUTF16("second_lastname");
1102 expected.value = ASCIIToUTF16("Hope");
1103 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1105 expected.name = ASCIIToUTF16("second_email");
1106 expected.value = ASCIIToUTF16("bobhope@example.com");
1107 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1110 // We should not extract a form if it has too few fillable fields.
1111 TEST_F(FormAutofillTest, ExtractFormsTooFewFields) {
1112 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1113 " <INPUT type='text' id='firstname' value='John'/>"
1114 " <INPUT type='text' id='lastname' value='Smith'/>"
1115 " <INPUT type='submit' name='reply-send' value='Send'/>"
1116 "</FORM>");
1118 WebFrame* web_frame = GetMainFrame();
1119 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1121 FormCache form_cache;
1122 std::vector<FormData> forms;
1123 form_cache.ExtractNewForms(*web_frame, &forms);
1124 EXPECT_EQ(0U, forms.size());
1127 // We should not report additional forms for empty forms.
1128 TEST_F(FormAutofillTest, ExtractFormsSkippedForms) {
1129 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1130 " <INPUT type='text' id='firstname' value='John'/>"
1131 " <INPUT type='text' id='lastname' value='Smith'/>"
1132 "</FORM>");
1134 WebFrame* web_frame = GetMainFrame();
1135 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1137 FormCache form_cache;
1138 std::vector<FormData> forms;
1139 form_cache.ExtractNewForms(*web_frame, &forms);
1140 EXPECT_EQ(0U, forms.size());
1143 // We should not report additional forms for empty forms.
1144 TEST_F(FormAutofillTest, ExtractFormsNoFields) {
1145 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1146 "</FORM>");
1148 WebFrame* web_frame = GetMainFrame();
1149 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1151 FormCache form_cache;
1152 std::vector<FormData> forms;
1153 form_cache.ExtractNewForms(*web_frame, &forms);
1154 EXPECT_EQ(0U, forms.size());
1157 // We should not extract a form if it has too few fillable fields.
1158 // Make sure radio and checkbox fields don't count.
1159 TEST_F(FormAutofillTest, ExtractFormsTooFewFieldsSkipsCheckable) {
1160 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1161 " <INPUT type='text' id='firstname' value='John'/>"
1162 " <INPUT type='text' id='lastname' value='Smith'/>"
1163 " <INPUT type='radio' id='a_radio' value='0'/>"
1164 " <INPUT type='checkbox' id='a_check' value='1'/>"
1165 " <INPUT type='submit' name='reply-send' value='Send'/>"
1166 "</FORM>");
1168 WebFrame* web_frame = GetMainFrame();
1169 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1171 FormCache form_cache;
1172 std::vector<FormData> forms;
1173 form_cache.ExtractNewForms(*web_frame, &forms);
1174 EXPECT_EQ(0U, forms.size());
1177 TEST_F(FormAutofillTest, WebFormElementToFormDataAutocomplete) {
1179 // Form is not auto-completable due to autocomplete=off.
1180 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'"
1181 " autocomplete=off>"
1182 " <INPUT type='text' id='firstname' value='John'/>"
1183 " <INPUT type='text' id='lastname' value='Smith'/>"
1184 " <INPUT type='text' id='email' value='john@example.com'/>"
1185 " <INPUT type='submit' name='reply-send' value='Send'/>"
1186 "</FORM>");
1188 WebFrame* web_frame = GetMainFrame();
1189 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1191 WebVector<WebFormElement> web_forms;
1192 web_frame->document().forms(web_forms);
1193 ASSERT_EQ(1U, web_forms.size());
1194 WebFormElement web_form = web_forms[0];
1196 FormData form;
1197 EXPECT_TRUE(WebFormElementToFormData(
1198 web_form, WebFormControlElement(), autofill::REQUIRE_NONE,
1199 autofill::EXTRACT_NONE, &form, NULL));
1200 EXPECT_FALSE(WebFormElementToFormData(
1201 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
1202 autofill::EXTRACT_NONE, &form, NULL));
1206 // The firstname element is not auto-completable due to autocomplete=off.
1207 LoadHTML("<FORM name='TestForm' action='http://abc.com' "
1208 " method='post'>"
1209 " <INPUT type='text' id='firstname' value='John'"
1210 " autocomplete=off>"
1211 " <INPUT type='text' id='middlename' value='Jack'/>"
1212 " <INPUT type='text' id='lastname' value='Smith'/>"
1213 " <INPUT type='text' id='email' value='john@example.com'/>"
1214 " <INPUT type='submit' name='reply' value='Send'/>"
1215 "</FORM>");
1217 WebFrame* web_frame = GetMainFrame();
1218 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1220 WebVector<WebFormElement> web_forms;
1221 web_frame->document().forms(web_forms);
1222 ASSERT_EQ(1U, web_forms.size());
1223 WebFormElement web_form = web_forms[0];
1225 FormData form;
1226 EXPECT_TRUE(WebFormElementToFormData(
1227 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
1228 autofill::EXTRACT_VALUE, &form, NULL));
1230 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1231 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1232 EXPECT_EQ(GURL("http://abc.com"), form.action);
1234 const std::vector<FormFieldData>& fields = form.fields;
1235 ASSERT_EQ(3U, fields.size());
1237 FormFieldData expected;
1238 expected.form_control_type = "text";
1239 expected.max_length = WebInputElement::defaultMaxLength();
1241 expected.name = ASCIIToUTF16("middlename");
1242 expected.value = ASCIIToUTF16("Jack");
1243 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1245 expected.name = ASCIIToUTF16("lastname");
1246 expected.value = ASCIIToUTF16("Smith");
1247 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1249 expected.name = ASCIIToUTF16("email");
1250 expected.value = ASCIIToUTF16("john@example.com");
1251 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1255 TEST_F(FormAutofillTest, FindFormForInputElement) {
1256 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
1257 " <INPUT type='text' id='firstname' value='John'/>"
1258 " <INPUT type='text' id='lastname' value='Smith'/>"
1259 " <INPUT type='text' id='email' value='john@example.com'"
1260 "autocomplete='off' />"
1261 " <INPUT type='text' id='phone' value='1.800.555.1234'/>"
1262 " <INPUT type='submit' name='reply-send' value='Send'/>"
1263 "</FORM>");
1265 WebFrame* web_frame = GetMainFrame();
1266 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1268 FormCache form_cache;
1269 std::vector<FormData> forms;
1270 form_cache.ExtractNewForms(*web_frame, &forms);
1271 ASSERT_EQ(1U, forms.size());
1273 // Get the input element we want to find.
1274 WebElement element = web_frame->document().getElementById("firstname");
1275 WebInputElement input_element = element.to<WebInputElement>();
1277 // Find the form and verify it's the correct form.
1278 FormData form;
1279 FormFieldData field;
1280 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
1281 &form,
1282 &field,
1283 autofill::REQUIRE_NONE));
1284 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1285 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1286 EXPECT_EQ(GURL("http://buh.com"), form.action);
1288 const std::vector<FormFieldData>& fields = form.fields;
1289 ASSERT_EQ(4U, fields.size());
1291 FormFieldData expected;
1292 expected.form_control_type = "text";
1293 expected.max_length = WebInputElement::defaultMaxLength();
1295 expected.name = ASCIIToUTF16("firstname");
1296 expected.value = ASCIIToUTF16("John");
1297 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1298 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1300 expected.name = ASCIIToUTF16("lastname");
1301 expected.value = ASCIIToUTF16("Smith");
1302 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1304 expected.name = ASCIIToUTF16("email");
1305 expected.value = ASCIIToUTF16("john@example.com");
1306 expected.autocomplete_attribute = "off";
1307 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1308 expected.autocomplete_attribute = std::string(); // reset
1310 expected.name = ASCIIToUTF16("phone");
1311 expected.value = ASCIIToUTF16("1.800.555.1234");
1312 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1314 // Try again, but require autocomplete.
1315 FormData form2;
1316 FormFieldData field2;
1317 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1318 input_element,
1319 &form2,
1320 &field2,
1321 autofill::REQUIRE_AUTOCOMPLETE));
1322 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1323 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1324 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1326 const std::vector<FormFieldData>& fields2 = form2.fields;
1327 ASSERT_EQ(3U, fields2.size());
1329 expected.form_control_type = "text";
1330 expected.max_length = WebInputElement::defaultMaxLength();
1332 expected.name = ASCIIToUTF16("firstname");
1333 expected.value = ASCIIToUTF16("John");
1334 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1335 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1337 expected.name = ASCIIToUTF16("lastname");
1338 expected.value = ASCIIToUTF16("Smith");
1339 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1341 expected.name = ASCIIToUTF16("phone");
1342 expected.value = ASCIIToUTF16("1.800.555.1234");
1343 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1346 TEST_F(FormAutofillTest, FindFormForTextAreaElement) {
1347 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
1348 " <INPUT type='text' id='firstname' value='John'/>"
1349 " <INPUT type='text' id='lastname' value='Smith'/>"
1350 " <INPUT type='text' id='email' value='john@example.com'"
1351 "autocomplete='off' />"
1352 " <TEXTAREA id='street-address'>"
1353 "123 Fantasy Ln.&#10;"
1354 "Apt. 42"
1355 "</TEXTAREA>"
1356 " <INPUT type='submit' name='reply-send' value='Send'/>"
1357 "</FORM>");
1359 WebFrame* web_frame = GetMainFrame();
1360 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1362 FormCache form_cache;
1363 std::vector<FormData> forms;
1364 form_cache.ExtractNewForms(*web_frame, &forms);
1365 ASSERT_EQ(1U, forms.size());
1367 // Get the textarea element we want to find.
1368 WebElement element = web_frame->document().getElementById("street-address");
1369 WebTextAreaElement textarea_element = element.to<WebTextAreaElement>();
1371 // Find the form and verify it's the correct form.
1372 FormData form;
1373 FormFieldData field;
1374 EXPECT_TRUE(FindFormAndFieldForFormControlElement(textarea_element,
1375 &form,
1376 &field,
1377 autofill::REQUIRE_NONE));
1378 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1379 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1380 EXPECT_EQ(GURL("http://buh.com"), form.action);
1382 const std::vector<FormFieldData>& fields = form.fields;
1383 ASSERT_EQ(4U, fields.size());
1385 FormFieldData expected;
1387 expected.name = ASCIIToUTF16("firstname");
1388 expected.value = ASCIIToUTF16("John");
1389 expected.form_control_type = "text";
1390 expected.max_length = WebInputElement::defaultMaxLength();
1391 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1393 expected.name = ASCIIToUTF16("lastname");
1394 expected.value = ASCIIToUTF16("Smith");
1395 expected.form_control_type = "text";
1396 expected.max_length = WebInputElement::defaultMaxLength();
1397 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1399 expected.name = ASCIIToUTF16("email");
1400 expected.value = ASCIIToUTF16("john@example.com");
1401 expected.autocomplete_attribute = "off";
1402 expected.form_control_type = "text";
1403 expected.max_length = WebInputElement::defaultMaxLength();
1404 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1405 expected.autocomplete_attribute = std::string(); // reset
1407 expected.name = ASCIIToUTF16("street-address");
1408 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1409 expected.form_control_type = "textarea";
1410 expected.max_length = 0;
1411 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1412 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1414 // Try again, but require autocomplete.
1415 FormData form2;
1416 FormFieldData field2;
1417 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1418 textarea_element,
1419 &form2,
1420 &field2,
1421 autofill::REQUIRE_AUTOCOMPLETE));
1422 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1423 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1424 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1426 const std::vector<FormFieldData>& fields2 = form2.fields;
1427 ASSERT_EQ(3U, fields2.size());
1429 expected.name = ASCIIToUTF16("firstname");
1430 expected.value = ASCIIToUTF16("John");
1431 expected.form_control_type = "text";
1432 expected.max_length = WebInputElement::defaultMaxLength();
1433 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1435 expected.name = ASCIIToUTF16("lastname");
1436 expected.value = ASCIIToUTF16("Smith");
1437 expected.form_control_type = "text";
1438 expected.max_length = WebInputElement::defaultMaxLength();
1439 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1441 expected.name = ASCIIToUTF16("street-address");
1442 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1443 expected.form_control_type = "textarea";
1444 expected.max_length = 0;
1445 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1446 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1449 // Test regular FillForm function.
1450 TEST_F(FormAutofillTest, FillForm) {
1451 static const AutofillFieldCase field_cases[] = {
1452 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1453 // should_be_autofilled, autofill_value, expected_value
1455 // Regular empty fields (firstname & lastname) should be autofilled.
1456 {"text", "firstname", "", "", true, "filled firstname",
1457 "filled firstname"},
1458 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1459 // hidden fields should not be extracted to form_data.
1460 // Non empty fields should not be autofilled.
1461 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
1462 // "noautocomplete" should not be extracted to form_data.
1463 // Disabled fields should not be autofilled.
1464 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1465 // Readonly fields should not be autofilled.
1466 {"text", "readonly", "", "", false, "filled readonly", ""},
1467 // Fields with "visibility: hidden" should not be autofilled.
1468 {"text", "invisible", "", "", false, "filled invisible", ""},
1469 // Fields with "display:none" should not be autofilled.
1470 {"text", "displaynone", "", "", false, "filled displaynone", ""},
1471 // Regular <input type="month"> should be autofilled.
1472 {"month", "month", "", "", true, "2017-11", "2017-11"},
1473 // Non-empty <input type="month"> should not be autofilled.
1474 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
1475 // Regular select fields should be autofilled.
1476 {"select-one", "select", "", "", true, "TX", "TX"},
1477 // Select fields should be autofilled even if they already have a
1478 // non-empty value.
1479 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1480 // Select fields should not be autofilled if no new value is passed from
1481 // autofill profile. The existing value should not be overriden.
1482 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
1483 // Regular textarea elements should be autofilled.
1484 {"textarea", "textarea", "", "", true, "some multi-\nline value",
1485 "some multi-\nline value"},
1486 // Non-empty textarea elements should not be autofilled.
1487 {"textarea", "textarea-nonempty", "Go\naway!", "", false,
1488 "some multi-\nline value", "Go\naway!"},
1490 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1491 FillForm, &GetValueWrapper);
1492 // Verify preview selection.
1493 WebInputElement firstname = GetMainFrame()->document().
1494 getElementById("firstname").to<WebInputElement>();
1495 EXPECT_EQ(16, firstname.selectionStart());
1496 EXPECT_EQ(16, firstname.selectionEnd());
1499 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) {
1500 static const AutofillFieldCase field_cases[] = {
1501 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1502 // should_be_autofilled, autofill_value, expected_value
1504 // Regular empty fields (firstname & lastname) should be autofilled.
1505 {"text", "firstname", "", "", true, "filled firstname",
1506 "filled firstname"},
1507 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1508 // hidden fields should not be extracted to form_data.
1509 // Non empty fields should be overriden.
1510 {"text", "notempty", "Hi", "", true, "filled notempty",
1511 "filled notempty"},
1512 // "noautocomplete" should not be extracted to form_data.
1513 // Disabled fields should not be autofilled.
1514 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1515 // Readonly fields should not be autofilled.
1516 {"text", "readonly", "", "", false, "filled readonly", ""},
1517 // Fields with "visibility: hidden" should also be autofilled.
1518 {"text", "invisible", "", "", true, "filled invisible",
1519 "filled invisible"},
1520 // Fields with "display:none" should also be autofilled.
1521 {"text", "displaynone", "", "", true, "filled displaynone",
1522 "filled displaynone"},
1523 // Regular <input type="month"> should be autofilled.
1524 {"month", "month", "", "", true, "2017-11", "2017-11"},
1525 // Non-empty <input type="month"> should be overridden.
1526 {"month", "month-nonempty", "2011-12", "", true, "2017-11", "2017-11"},
1527 // Regular select fields should be autofilled.
1528 {"select-one", "select", "", "", true, "TX", "TX"},
1529 // Select fields should be autofilled even if they already have a
1530 // non-empty value.
1531 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1532 // Select fields should not be autofilled if no new value is passed from
1533 // autofill profile. The existing value should not be overriden.
1534 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
1535 // Regular textarea elements should be autofilled.
1536 {"textarea", "textarea", "", "", true, "some multi-\nline value",
1537 "some multi-\nline value"},
1538 // Nonempty textarea elements should be overridden.
1539 {"textarea", "textarea-nonempty", "Go\naway!", "", true,
1540 "some multi-\nline value", "some multi-\nline value"},
1542 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1543 &FillFormIncludingNonFocusableElementsWrapper,
1544 &GetValueWrapper);
1547 TEST_F(FormAutofillTest, PreviewForm) {
1548 static const AutofillFieldCase field_cases[] = {
1549 // Normal empty fields should be previewed.
1550 {"text", "firstname", "", "", true, "suggested firstname",
1551 "suggested firstname"},
1552 {"text", "lastname", "", "", true, "suggested lastname",
1553 "suggested lastname"},
1554 // Hidden fields should not be extracted to form_data.
1555 // Non empty fields should not be previewed.
1556 {"text", "notempty", "Hi", "", false, "suggested notempty", ""},
1557 // "noautocomplete" should not be extracted to form_data.
1558 // Disabled fields should not be previewed.
1559 {"text", "notenabled", "", "", false, "suggested notenabled", ""},
1560 // Readonly fields should not be previewed.
1561 {"text", "readonly", "", "", false, "suggested readonly", ""},
1562 // Fields with "visibility: hidden" should not be previewed.
1563 {"text", "invisible", "", "", false, "suggested invisible",
1564 ""},
1565 // Fields with "display:none" should not previewed.
1566 {"text", "displaynone", "", "", false, "suggested displaynone",
1567 ""},
1568 // Regular <input type="month"> should be previewed.
1569 {"month", "month", "", "", true, "2017-11", "2017-11"},
1570 // Non-empty <input type="month"> should not be previewed.
1571 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
1572 // Regular select fields should be previewed.
1573 {"select-one", "select", "", "", true, "TX", "TX"},
1574 // Select fields should be previewed even if they already have a
1575 // non-empty value.
1576 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1577 // Select fields should not be previewed if no suggestion is passed from
1578 // autofill profile.
1579 {"select-one", "select-unchanged", "CA", "", false, "", ""},
1580 // Normal textarea elements should be previewed.
1581 {"textarea", "textarea", "", "", true, "suggested multi-\nline value",
1582 "suggested multi-\nline value"},
1583 // Nonempty textarea elements should not be previewed.
1584 {"textarea", "textarea-nonempty", "Go\naway!", "", false,
1585 "suggested multi-\nline value", ""},
1587 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1588 &PreviewForm, &GetSuggestedValueWrapper);
1590 // Verify preview selection.
1591 WebInputElement firstname = GetMainFrame()->document().
1592 getElementById("firstname").to<WebInputElement>();
1593 EXPECT_EQ(0, firstname.selectionStart());
1594 EXPECT_EQ(19, firstname.selectionEnd());
1597 TEST_F(FormAutofillTest, Labels) {
1598 ExpectJohnSmithLabels(
1599 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1600 " <LABEL for='firstname'> First name: </LABEL>"
1601 " <INPUT type='text' id='firstname' value='John'/>"
1602 " <LABEL for='lastname'> Last name: </LABEL>"
1603 " <INPUT type='text' id='lastname' value='Smith'/>"
1604 " <LABEL for='email'> Email: </LABEL>"
1605 " <INPUT type='text' id='email' value='john@example.com'/>"
1606 " <INPUT type='submit' name='reply-send' value='Send'/>"
1607 "</FORM>");
1610 TEST_F(FormAutofillTest, LabelsWithSpans) {
1611 ExpectJohnSmithLabels(
1612 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1613 " <LABEL for='firstname'><span>First name: </span></LABEL>"
1614 " <INPUT type='text' id='firstname' value='John'/>"
1615 " <LABEL for='lastname'><span>Last name: </span></LABEL>"
1616 " <INPUT type='text' id='lastname' value='Smith'/>"
1617 " <LABEL for='email'><span>Email: </span></LABEL>"
1618 " <INPUT type='text' id='email' value='john@example.com'/>"
1619 " <INPUT type='submit' name='reply-send' value='Send'/>"
1620 "</FORM>");
1623 // This test is different from FormAutofillTest.Labels in that the label
1624 // elements for= attribute is set to the name of the form control element it is
1625 // a label for instead of the id of the form control element. This is invalid
1626 // because the for= attribute must be set to the id of the form control element;
1627 // however, current label parsing code will extract the text from the previous
1628 // label element and apply it to the following input field.
1629 TEST_F(FormAutofillTest, InvalidLabels) {
1630 ExpectJohnSmithLabels(
1631 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1632 " <LABEL for='firstname'> First name: </LABEL>"
1633 " <INPUT type='text' name='firstname' value='John'/>"
1634 " <LABEL for='lastname'> Last name: </LABEL>"
1635 " <INPUT type='text' name='lastname' value='Smith'/>"
1636 " <LABEL for='email'> Email: </LABEL>"
1637 " <INPUT type='text' name='email' value='john@example.com'/>"
1638 " <INPUT type='submit' name='reply-send' value='Send'/>"
1639 "</FORM>");
1642 // This test has three form control elements, only one of which has a label
1643 // element associated with it.
1644 TEST_F(FormAutofillTest, OneLabelElement) {
1645 ExpectJohnSmithLabels(
1646 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1647 " First name:"
1648 " <INPUT type='text' id='firstname' value='John'/>"
1649 " <LABEL for='lastname'>Last name: </LABEL>"
1650 " <INPUT type='text' id='lastname' value='Smith'/>"
1651 " Email:"
1652 " <INPUT type='text' id='email' value='john@example.com'/>"
1653 " <INPUT type='submit' name='reply-send' value='Send'/>"
1654 "</FORM>");
1657 TEST_F(FormAutofillTest, LabelsInferredFromText) {
1658 ExpectJohnSmithLabels(
1659 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1660 " First name:"
1661 " <INPUT type='text' id='firstname' value='John'/>"
1662 " Last name:"
1663 " <INPUT type='text' id='lastname' value='Smith'/>"
1664 " Email:"
1665 " <INPUT type='text' id='email' value='john@example.com'/>"
1666 " <INPUT type='submit' name='reply-send' value='Send'/>"
1667 "</FORM>");
1670 TEST_F(FormAutofillTest, LabelsInferredFromParagraph) {
1671 ExpectJohnSmithLabels(
1672 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1673 " <P>First name:</P><INPUT type='text' "
1674 " id='firstname' value='John'/>"
1675 " <P>Last name:</P>"
1676 " <INPUT type='text' id='lastname' value='Smith'/>"
1677 " <P>Email:</P>"
1678 " <INPUT type='text' id='email' value='john@example.com'/>"
1679 " <INPUT type='submit' name='reply-send' value='Send'/>"
1680 "</FORM>");
1683 TEST_F(FormAutofillTest, LabelsInferredFromBold) {
1684 ExpectJohnSmithLabels(
1685 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1686 " <B>First name:</B><INPUT type='text' "
1687 " id='firstname' value='John'/>"
1688 " <B>Last name:</B>"
1689 " <INPUT type='text' id='lastname' value='Smith'/>"
1690 " <B>Email:</B>"
1691 " <INPUT type='text' id='email' value='john@example.com'/>"
1692 " <INPUT type='submit' name='reply-send' value='Send'/>"
1693 "</FORM>");
1696 TEST_F(FormAutofillTest, LabelsInferredPriorToImgOrBr) {
1697 ExpectJohnSmithLabels(
1698 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1699 " First name:<IMG/><INPUT type='text' "
1700 " id='firstname' value='John'/>"
1701 " Last name:<IMG/>"
1702 " <INPUT type='text' id='lastname' value='Smith'/>"
1703 " Email:<BR/>"
1704 " <INPUT type='text' id='email' value='john@example.com'/>"
1705 " <INPUT type='submit' name='reply-send' value='Send'/>"
1706 "</FORM>");
1709 TEST_F(FormAutofillTest, LabelsInferredFromTableCell) {
1710 ExpectJohnSmithLabels(
1711 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1712 "<TABLE>"
1713 " <TR>"
1714 " <TD>First name:</TD>"
1715 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
1716 " </TR>"
1717 " <TR>"
1718 " <TD>Last name:</TD>"
1719 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
1720 " </TR>"
1721 " <TR>"
1722 " <TD>Email:</TD>"
1723 " <TD><INPUT type='text' id='email'"
1724 " value='john@example.com'/></TD>"
1725 " </TR>"
1726 " <TR>"
1727 " <TD></TD>"
1728 " <TD>"
1729 " <INPUT type='submit' name='reply-send' value='Send'/>"
1730 " </TD>"
1731 " </TR>"
1732 "</TABLE>"
1733 "</FORM>");
1736 TEST_F(FormAutofillTest, LabelsInferredFromTableCellTH) {
1737 ExpectJohnSmithLabels(
1738 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1739 "<TABLE>"
1740 " <TR>"
1741 " <TH>First name:</TH>"
1742 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
1743 " </TR>"
1744 " <TR>"
1745 " <TH>Last name:</TH>"
1746 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
1747 " </TR>"
1748 " <TR>"
1749 " <TH>Email:</TH>"
1750 " <TD><INPUT type='text' id='email'"
1751 " value='john@example.com'/></TD>"
1752 " </TR>"
1753 " <TR>"
1754 " <TD></TD>"
1755 " <TD>"
1756 " <INPUT type='submit' name='reply-send' value='Send'/>"
1757 " </TD>"
1758 " </TR>"
1759 "</TABLE>"
1760 "</FORM>");
1763 TEST_F(FormAutofillTest, LabelsInferredFromTableCellNested) {
1764 std::vector<base::string16> labels, names, values;
1766 labels.push_back(ASCIIToUTF16("First name: Bogus"));
1767 names.push_back(ASCIIToUTF16("firstname"));
1768 values.push_back(ASCIIToUTF16("John"));
1770 labels.push_back(ASCIIToUTF16("Last name:"));
1771 names.push_back(ASCIIToUTF16("lastname"));
1772 values.push_back(ASCIIToUTF16("Smith"));
1774 labels.push_back(ASCIIToUTF16("Email:"));
1775 names.push_back(ASCIIToUTF16("email"));
1776 values.push_back(ASCIIToUTF16("john@example.com"));
1778 ExpectLabels(
1779 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1780 "<TABLE>"
1781 " <TR>"
1782 " <TD>"
1783 " <FONT>"
1784 " First name:"
1785 " </FONT>"
1786 " <FONT>"
1787 " Bogus"
1788 " </FONT>"
1789 " </TD>"
1790 " <TD>"
1791 " <FONT>"
1792 " <INPUT type='text' id='firstname' value='John'/>"
1793 " </FONT>"
1794 " </TD>"
1795 " </TR>"
1796 " <TR>"
1797 " <TD>"
1798 " <FONT>"
1799 " Last name:"
1800 " </FONT>"
1801 " </TD>"
1802 " <TD>"
1803 " <FONT>"
1804 " <INPUT type='text' id='lastname' value='Smith'/>"
1805 " </FONT>"
1806 " </TD>"
1807 " </TR>"
1808 " <TR>"
1809 " <TD>"
1810 " <FONT>"
1811 " Email:"
1812 " </FONT>"
1813 " </TD>"
1814 " <TD>"
1815 " <FONT>"
1816 " <INPUT type='text' id='email' value='john@example.com'/>"
1817 " </FONT>"
1818 " </TD>"
1819 " </TR>"
1820 " <TR>"
1821 " <TD></TD>"
1822 " <TD>"
1823 " <INPUT type='submit' name='reply-send' value='Send'/>"
1824 " </TD>"
1825 " </TR>"
1826 "</TABLE>"
1827 "</FORM>",
1828 labels, names, values);
1831 TEST_F(FormAutofillTest, LabelsInferredFromTableEmptyTDs) {
1832 std::vector<base::string16> labels, names, values;
1834 labels.push_back(ASCIIToUTF16("* First Name"));
1835 names.push_back(ASCIIToUTF16("firstname"));
1836 values.push_back(ASCIIToUTF16("John"));
1838 labels.push_back(ASCIIToUTF16("* Last Name"));
1839 names.push_back(ASCIIToUTF16("lastname"));
1840 values.push_back(ASCIIToUTF16("Smith"));
1842 labels.push_back(ASCIIToUTF16("* Email"));
1843 names.push_back(ASCIIToUTF16("email"));
1844 values.push_back(ASCIIToUTF16("john@example.com"));
1846 ExpectLabels(
1847 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1848 "<TABLE>"
1849 " <TR>"
1850 " <TD>"
1851 " <SPAN>*</SPAN>"
1852 " <B>First Name</B>"
1853 " </TD>"
1854 " <TD></TD>"
1855 " <TD>"
1856 " <INPUT type='text' id='firstname' value='John'/>"
1857 " </TD>"
1858 " </TR>"
1859 " <TR>"
1860 " <TD>"
1861 " <SPAN>*</SPAN>"
1862 " <B>Last Name</B>"
1863 " </TD>"
1864 " <TD></TD>"
1865 " <TD>"
1866 " <INPUT type='text' id='lastname' value='Smith'/>"
1867 " </TD>"
1868 " </TR>"
1869 " <TR>"
1870 " <TD>"
1871 " <SPAN>*</SPAN>"
1872 " <B>Email</B>"
1873 " </TD>"
1874 " <TD></TD>"
1875 " <TD>"
1876 " <INPUT type='text' id='email' value='john@example.com'/>"
1877 " </TD>"
1878 " </TR>"
1879 " <TR>"
1880 " <TD></TD>"
1881 " <TD>"
1882 " <INPUT type='submit' name='reply-send' value='Send'/>"
1883 " </TD>"
1884 " </TR>"
1885 "</TABLE>"
1886 "</FORM>",
1887 labels, names, values);
1890 TEST_F(FormAutofillTest, LabelsInferredFromPreviousTD) {
1891 std::vector<base::string16> labels, names, values;
1893 labels.push_back(ASCIIToUTF16("* First Name"));
1894 names.push_back(ASCIIToUTF16("firstname"));
1895 values.push_back(ASCIIToUTF16("John"));
1897 labels.push_back(ASCIIToUTF16("* Last Name"));
1898 names.push_back(ASCIIToUTF16("lastname"));
1899 values.push_back(ASCIIToUTF16("Smith"));
1901 labels.push_back(ASCIIToUTF16("* Email"));
1902 names.push_back(ASCIIToUTF16("email"));
1903 values.push_back(ASCIIToUTF16("john@example.com"));
1905 ExpectLabels(
1906 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1907 "<TABLE>"
1908 " <TR>"
1909 " <TD>* First Name</TD>"
1910 " <TD>"
1911 " Bogus"
1912 " <INPUT type='hidden'/>"
1913 " <INPUT type='text' id='firstname' value='John'/>"
1914 " </TD>"
1915 " </TR>"
1916 " <TR>"
1917 " <TD>* Last Name</TD>"
1918 " <TD>"
1919 " <INPUT type='text' id='lastname' value='Smith'/>"
1920 " </TD>"
1921 " </TR>"
1922 " <TR>"
1923 " <TD>* Email</TD>"
1924 " <TD>"
1925 " <INPUT type='text' id='email' value='john@example.com'/>"
1926 " </TD>"
1927 " </TR>"
1928 " <TR>"
1929 " <TD></TD>"
1930 " <TD>"
1931 " <INPUT type='submit' name='reply-send' value='Send'/>"
1932 " </TD>"
1933 " </TR>"
1934 "</TABLE>"
1935 "</FORM>",
1936 labels, names, values);
1939 // <script>, <noscript> and <option> tags are excluded when the labels are
1940 // inferred.
1941 // Also <!-- comment --> is excluded.
1942 TEST_F(FormAutofillTest, LabelsInferredFromTableWithSpecialElements) {
1943 std::vector<base::string16> labels, names, values;
1944 std::vector<std::string> control_types;
1946 labels.push_back(ASCIIToUTF16("* First Name"));
1947 names.push_back(ASCIIToUTF16("firstname"));
1948 values.push_back(ASCIIToUTF16("John"));
1949 control_types.push_back("text");
1951 labels.push_back(ASCIIToUTF16("* Middle Name"));
1952 names.push_back(ASCIIToUTF16("middlename"));
1953 values.push_back(ASCIIToUTF16("Joe"));
1954 control_types.push_back("text");
1956 labels.push_back(ASCIIToUTF16("* Last Name"));
1957 names.push_back(ASCIIToUTF16("lastname"));
1958 values.push_back(ASCIIToUTF16("Smith"));
1959 control_types.push_back("text");
1961 labels.push_back(ASCIIToUTF16("* Country"));
1962 names.push_back(ASCIIToUTF16("country"));
1963 values.push_back(ASCIIToUTF16("US"));
1964 control_types.push_back("select-one");
1966 labels.push_back(ASCIIToUTF16("* Email"));
1967 names.push_back(ASCIIToUTF16("email"));
1968 values.push_back(ASCIIToUTF16("john@example.com"));
1969 control_types.push_back("text");
1971 ExpectLabelsAndTypes(
1972 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1973 "<TABLE>"
1974 " <TR>"
1975 " <TD>"
1976 " <SPAN>*</SPAN>"
1977 " <B>First Name</B>"
1978 " </TD>"
1979 " <TD>"
1980 " <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
1981 " </SCRIPT>"
1982 " <INPUT type='text' id='firstname' value='John'/>"
1983 " </TD>"
1984 " </TR>"
1985 " <TR>"
1986 " <TD>"
1987 " <SPAN>*</SPAN>"
1988 " <B>Middle Name</B>"
1989 " </TD>"
1990 " <TD>"
1991 " <NOSCRIPT>"
1992 " <P>Bad</P>"
1993 " </NOSCRIPT>"
1994 " <INPUT type='text' id='middlename' value='Joe'/>"
1995 " </TD>"
1996 " </TR>"
1997 " <TR>"
1998 " <TD>"
1999 " <SPAN>*</SPAN>"
2000 " <B>Last Name</B>"
2001 " </TD>"
2002 " <TD>"
2003 " <INPUT type='text' id='lastname' value='Smith'/>"
2004 " </TD>"
2005 " </TR>"
2006 " <TR>"
2007 " <TD>"
2008 " <SPAN>*</SPAN>"
2009 " <B>Country</B>"
2010 " </TD>"
2011 " <TD>"
2012 " <SELECT id='country'>"
2013 " <OPTION VALUE='US'>The value should be ignored as label."
2014 " </OPTION>"
2015 " <OPTION VALUE='JP'>JAPAN</OPTION>"
2016 " </SELECT>"
2017 " </TD>"
2018 " </TR>"
2019 " <TR>"
2020 " <TD>"
2021 " <SPAN>*</SPAN>"
2022 " <B>Email</B>"
2023 " </TD>"
2024 " <TD>"
2025 " <!-- This comment should be ignored as inferred label.-->"
2026 " <INPUT type='text' id='email' value='john@example.com'/>"
2027 " </TD>"
2028 " </TR>"
2029 " <TR>"
2030 " <TD></TD>"
2031 " <TD>"
2032 " <INPUT type='submit' name='reply-send' value='Send'/>"
2033 " </TD>"
2034 " </TR>"
2035 "</TABLE>"
2036 "</FORM>",
2037 labels, names, values, control_types);
2040 TEST_F(FormAutofillTest, LabelsInferredFromTableLabels) {
2041 ExpectJohnSmithLabels(
2042 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2043 "<TABLE>"
2044 " <TR>"
2045 " <TD>"
2046 " <LABEL>First name:</LABEL>"
2047 " <INPUT type='text' id='firstname' value='John'/>"
2048 " </TD>"
2049 " </TR>"
2050 " <TR>"
2051 " <TD>"
2052 " <LABEL>Last name:</LABEL>"
2053 " <INPUT type='text' id='lastname' value='Smith'/>"
2054 " </TD>"
2055 " </TR>"
2056 " <TR>"
2057 " <TD>"
2058 " <LABEL>Email:</LABEL>"
2059 " <INPUT type='text' id='email' value='john@example.com'/>"
2060 " </TD>"
2061 " </TR>"
2062 "</TABLE>"
2063 "<INPUT type='submit' name='reply-send' value='Send'/>"
2064 "</FORM>");
2067 TEST_F(FormAutofillTest, LabelsInferredFromTableTDInterveningElements) {
2068 ExpectJohnSmithLabels(
2069 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2070 "<TABLE>"
2071 " <TR>"
2072 " <TD>"
2073 " First name:"
2074 " <BR>"
2075 " <INPUT type='text' id='firstname' value='John'/>"
2076 " </TD>"
2077 " </TR>"
2078 " <TR>"
2079 " <TD>"
2080 " Last name:"
2081 " <BR>"
2082 " <INPUT type='text' id='lastname' value='Smith'/>"
2083 " </TD>"
2084 " </TR>"
2085 " <TR>"
2086 " <TD>"
2087 " Email:"
2088 " <BR>"
2089 " <INPUT type='text' id='email' value='john@example.com'/>"
2090 " </TD>"
2091 " </TR>"
2092 "</TABLE>"
2093 "<INPUT type='submit' name='reply-send' value='Send'/>"
2094 "</FORM>");
2097 // Verify that we correctly infer labels when the label text spans multiple
2098 // adjacent HTML elements, not separated by whitespace.
2099 TEST_F(FormAutofillTest, LabelsInferredFromTableAdjacentElements) {
2100 std::vector<base::string16> labels, names, values;
2102 labels.push_back(ASCIIToUTF16("*First Name"));
2103 names.push_back(ASCIIToUTF16("firstname"));
2104 values.push_back(ASCIIToUTF16("John"));
2106 labels.push_back(ASCIIToUTF16("*Last Name"));
2107 names.push_back(ASCIIToUTF16("lastname"));
2108 values.push_back(ASCIIToUTF16("Smith"));
2110 labels.push_back(ASCIIToUTF16("*Email"));
2111 names.push_back(ASCIIToUTF16("email"));
2112 values.push_back(ASCIIToUTF16("john@example.com"));
2114 ExpectLabels(
2115 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2116 "<TABLE>"
2117 " <TR>"
2118 " <TD>"
2119 " <SPAN>*</SPAN><B>First Name</B>"
2120 " </TD>"
2121 " <TD>"
2122 " <INPUT type='text' id='firstname' value='John'/>"
2123 " </TD>"
2124 " </TR>"
2125 " <TR>"
2126 " <TD>"
2127 " <SPAN>*</SPAN><B>Last Name</B>"
2128 " </TD>"
2129 " <TD>"
2130 " <INPUT type='text' id='lastname' value='Smith'/>"
2131 " </TD>"
2132 " </TR>"
2133 " <TR>"
2134 " <TD>"
2135 " <SPAN>*</SPAN><B>Email</B>"
2136 " </TD>"
2137 " <TD>"
2138 " <INPUT type='text' id='email' value='john@example.com'/>"
2139 " </TD>"
2140 " </TR>"
2141 " <TR>"
2142 " <TD>"
2143 " <INPUT type='submit' name='reply-send' value='Send'/>"
2144 " </TD>"
2145 " </TR>"
2146 "</TABLE>"
2147 "</FORM>",
2148 labels, names, values);
2151 // Verify that we correctly infer labels when the label text resides in the
2152 // previous row.
2153 TEST_F(FormAutofillTest, LabelsInferredFromTableRow) {
2154 std::vector<base::string16> labels, names, values;
2156 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2157 names.push_back(ASCIIToUTF16("firstname"));
2158 values.push_back(ASCIIToUTF16("John"));
2160 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2161 names.push_back(ASCIIToUTF16("lastname"));
2162 values.push_back(ASCIIToUTF16("Smith"));
2164 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2165 names.push_back(ASCIIToUTF16("email"));
2166 values.push_back(ASCIIToUTF16("john@example.com"));
2168 ExpectLabels(
2169 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2170 "<TABLE>"
2171 " <TR>"
2172 " <TD>*First Name</TD>"
2173 " <TD>*Last Name</TD>"
2174 " <TD>*Email</TD>"
2175 " </TR>"
2176 " <TR>"
2177 " <TD>"
2178 " <INPUT type='text' id='firstname' value='John'/>"
2179 " </TD>"
2180 " <TD>"
2181 " <INPUT type='text' id='lastname' value='Smith'/>"
2182 " </TD>"
2183 " <TD>"
2184 " <INPUT type='text' id='email' value='john@example.com'/>"
2185 " </TD>"
2186 " </TR>"
2187 " <TR>"
2188 " <TD>"
2189 " <INPUT type='submit' name='reply-send' value='Send'/>"
2190 " </TD>"
2191 " </TR>"
2192 "</TABLE>",
2193 labels, names, values);
2196 // Verify that we correctly infer labels when enclosed within a list item.
2197 TEST_F(FormAutofillTest, LabelsInferredFromListItem) {
2198 std::vector<base::string16> labels, names, values;
2200 labels.push_back(ASCIIToUTF16("* Home Phone"));
2201 names.push_back(ASCIIToUTF16("areacode"));
2202 values.push_back(ASCIIToUTF16("415"));
2204 labels.push_back(ASCIIToUTF16("* Home Phone"));
2205 names.push_back(ASCIIToUTF16("prefix"));
2206 values.push_back(ASCIIToUTF16("555"));
2208 labels.push_back(ASCIIToUTF16("* Home Phone"));
2209 names.push_back(ASCIIToUTF16("suffix"));
2210 values.push_back(ASCIIToUTF16("1212"));
2212 ExpectLabels(
2213 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2214 "<DIV>"
2215 " <LI>"
2216 " <SPAN>Bogus</SPAN>"
2217 " </LI>"
2218 " <LI>"
2219 " <LABEL><EM>*</EM> Home Phone</LABEL>"
2220 " <INPUT type='text' id='areacode' value='415'/>"
2221 " <INPUT type='text' id='prefix' value='555'/>"
2222 " <INPUT type='text' id='suffix' value='1212'/>"
2223 " </LI>"
2224 " <LI>"
2225 " <INPUT type='submit' name='reply-send' value='Send'/>"
2226 " </LI>"
2227 "</DIV>"
2228 "</FORM>",
2229 labels, names, values);
2232 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionList) {
2233 std::vector<base::string16> labels, names, values;
2235 labels.push_back(ASCIIToUTF16("* First name: Bogus"));
2236 names.push_back(ASCIIToUTF16("firstname"));
2237 values.push_back(ASCIIToUTF16("John"));
2239 labels.push_back(ASCIIToUTF16("Last name:"));
2240 names.push_back(ASCIIToUTF16("lastname"));
2241 values.push_back(ASCIIToUTF16("Smith"));
2243 labels.push_back(ASCIIToUTF16("Email:"));
2244 names.push_back(ASCIIToUTF16("email"));
2245 values.push_back(ASCIIToUTF16("john@example.com"));
2247 ExpectLabels(
2248 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2249 "<DL>"
2250 " <DT>"
2251 " <SPAN>"
2252 " *"
2253 " </SPAN>"
2254 " <SPAN>"
2255 " First name:"
2256 " </SPAN>"
2257 " <SPAN>"
2258 " Bogus"
2259 " </SPAN>"
2260 " </DT>"
2261 " <DD>"
2262 " <FONT>"
2263 " <INPUT type='text' id='firstname' value='John'/>"
2264 " </FONT>"
2265 " </DD>"
2266 " <DT>"
2267 " <SPAN>"
2268 " Last name:"
2269 " </SPAN>"
2270 " </DT>"
2271 " <DD>"
2272 " <FONT>"
2273 " <INPUT type='text' id='lastname' value='Smith'/>"
2274 " </FONT>"
2275 " </DD>"
2276 " <DT>"
2277 " <SPAN>"
2278 " Email:"
2279 " </SPAN>"
2280 " </DT>"
2281 " <DD>"
2282 " <FONT>"
2283 " <INPUT type='text' id='email' value='john@example.com'/>"
2284 " </FONT>"
2285 " </DD>"
2286 " <DT></DT>"
2287 " <DD>"
2288 " <INPUT type='submit' name='reply-send' value='Send'/>"
2289 " </DD>"
2290 "</DL>"
2291 "</FORM>",
2292 labels, names, values);
2295 TEST_F(FormAutofillTest, LabelsInferredWithSameName) {
2296 std::vector<base::string16> labels, names, values;
2298 labels.push_back(ASCIIToUTF16("Address Line 1:"));
2299 names.push_back(ASCIIToUTF16("Address"));
2300 values.push_back(base::string16());
2302 labels.push_back(ASCIIToUTF16("Address Line 2:"));
2303 names.push_back(ASCIIToUTF16("Address"));
2304 values.push_back(base::string16());
2306 labels.push_back(ASCIIToUTF16("Address Line 3:"));
2307 names.push_back(ASCIIToUTF16("Address"));
2308 values.push_back(base::string16());
2310 ExpectLabels(
2311 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2312 " Address Line 1:"
2313 " <INPUT type='text' name='Address'/>"
2314 " Address Line 2:"
2315 " <INPUT type='text' name='Address'/>"
2316 " Address Line 3:"
2317 " <INPUT type='text' name='Address'/>"
2318 " <INPUT type='submit' name='reply-send' value='Send'/>"
2319 "</FORM>",
2320 labels, names, values);
2323 TEST_F(FormAutofillTest, LabelsInferredWithImageTags) {
2324 std::vector<base::string16> labels, names, values;
2326 labels.push_back(ASCIIToUTF16("Phone:"));
2327 names.push_back(ASCIIToUTF16("dayphone1"));
2328 values.push_back(base::string16());
2330 labels.push_back(ASCIIToUTF16("-"));
2331 names.push_back(ASCIIToUTF16("dayphone2"));
2332 values.push_back(base::string16());
2334 labels.push_back(ASCIIToUTF16("-"));
2335 names.push_back(ASCIIToUTF16("dayphone3"));
2336 values.push_back(base::string16());
2338 labels.push_back(ASCIIToUTF16("ext.:"));
2339 names.push_back(ASCIIToUTF16("dayphone4"));
2340 values.push_back(base::string16());
2342 labels.push_back(base::string16());
2343 names.push_back(ASCIIToUTF16("dummy"));
2344 values.push_back(base::string16());
2346 ExpectLabels(
2347 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2348 " Phone:"
2349 " <input type='text' name='dayphone1'>"
2350 " <img/>"
2351 " -"
2352 " <img/>"
2353 " <input type='text' name='dayphone2'>"
2354 " <img/>"
2355 " -"
2356 " <img/>"
2357 " <input type='text' name='dayphone3'>"
2358 " ext.:"
2359 " <input type='text' name='dayphone4'>"
2360 " <input type='text' name='dummy'>"
2361 " <input type='submit' name='reply-send' value='Send'>"
2362 "</FORM>",
2363 labels, names, values);
2366 TEST_F(FormAutofillTest, LabelsInferredFromDivTable) {
2367 ExpectJohnSmithLabels(
2368 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2369 "<DIV>First name:<BR>"
2370 " <SPAN>"
2371 " <INPUT type='text' name='firstname' value='John'>"
2372 " </SPAN>"
2373 "</DIV>"
2374 "<DIV>Last name:<BR>"
2375 " <SPAN>"
2376 " <INPUT type='text' name='lastname' value='Smith'>"
2377 " </SPAN>"
2378 "</DIV>"
2379 "<DIV>Email:<BR>"
2380 " <SPAN>"
2381 " <INPUT type='text' name='email' value='john@example.com'>"
2382 " </SPAN>"
2383 "</DIV>"
2384 "<input type='submit' name='reply-send' value='Send'>"
2385 "</FORM>");
2388 TEST_F(FormAutofillTest, LabelsInferredFromDivSiblingTable) {
2389 ExpectJohnSmithLabels(
2390 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2391 "<DIV>First name:</DIV>"
2392 "<DIV>"
2393 " <SPAN>"
2394 " <INPUT type='text' name='firstname' value='John'>"
2395 " </SPAN>"
2396 "</DIV>"
2397 "<DIV>Last name:</DIV>"
2398 "<DIV>"
2399 " <SPAN>"
2400 " <INPUT type='text' name='lastname' value='Smith'>"
2401 " </SPAN>"
2402 "</DIV>"
2403 "<DIV>Email:</DIV>"
2404 "<DIV>"
2405 " <SPAN>"
2406 " <INPUT type='text' name='email' value='john@example.com'>"
2407 " </SPAN>"
2408 "</DIV>"
2409 "<input type='submit' name='reply-send' value='Send'>"
2410 "</FORM>");
2413 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionListRatherThanDivTable) {
2414 ExpectJohnSmithLabels(
2415 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2416 "<DIV>This is not a label.<BR>"
2417 "<DL>"
2418 " <DT>"
2419 " <SPAN>"
2420 " First name:"
2421 " </SPAN>"
2422 " </DT>"
2423 " <DD>"
2424 " <FONT>"
2425 " <INPUT type='text' id='firstname' value='John'/>"
2426 " </FONT>"
2427 " </DD>"
2428 " <DT>"
2429 " <SPAN>"
2430 " Last name:"
2431 " </SPAN>"
2432 " </DT>"
2433 " <DD>"
2434 " <FONT>"
2435 " <INPUT type='text' id='lastname' value='Smith'/>"
2436 " </FONT>"
2437 " </DD>"
2438 " <DT>"
2439 " <SPAN>"
2440 " Email:"
2441 " </SPAN>"
2442 " </DT>"
2443 " <DD>"
2444 " <FONT>"
2445 " <INPUT type='text' id='email' value='john@example.com'/>"
2446 " </FONT>"
2447 " </DD>"
2448 " <DT></DT>"
2449 " <DD>"
2450 " <INPUT type='submit' name='reply-send' value='Send'/>"
2451 " </DD>"
2452 "</DL>"
2453 "</DIV>"
2454 "</FORM>");
2457 TEST_F(FormAutofillTest, FillFormMaxLength) {
2458 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2459 " <INPUT type='text' id='firstname' maxlength='5'/>"
2460 " <INPUT type='text' id='lastname' maxlength='7'/>"
2461 " <INPUT type='text' id='email' maxlength='9'/>"
2462 " <INPUT type='submit' name='reply-send' value='Send'/>"
2463 "</FORM>");
2465 WebFrame* web_frame = GetMainFrame();
2466 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2468 FormCache form_cache;
2469 std::vector<FormData> forms;
2470 form_cache.ExtractNewForms(*web_frame, &forms);
2471 ASSERT_EQ(1U, forms.size());
2473 // Get the input element we want to find.
2474 WebElement element = web_frame->document().getElementById("firstname");
2475 WebInputElement input_element = element.to<WebInputElement>();
2477 // Find the form that contains the input element.
2478 FormData form;
2479 FormFieldData field;
2480 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2481 &form,
2482 &field,
2483 autofill::REQUIRE_NONE));
2484 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2485 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2486 EXPECT_EQ(GURL("http://buh.com"), form.action);
2488 const std::vector<FormFieldData>& fields = form.fields;
2489 ASSERT_EQ(3U, fields.size());
2491 FormFieldData expected;
2492 expected.form_control_type = "text";
2494 expected.name = ASCIIToUTF16("firstname");
2495 expected.max_length = 5;
2496 expected.is_autofilled = false;
2497 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2499 expected.name = ASCIIToUTF16("lastname");
2500 expected.max_length = 7;
2501 expected.is_autofilled = false;
2502 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2504 expected.name = ASCIIToUTF16("email");
2505 expected.max_length = 9;
2506 expected.is_autofilled = false;
2507 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2509 // Fill the form.
2510 form.fields[0].value = ASCIIToUTF16("Brother");
2511 form.fields[1].value = ASCIIToUTF16("Jonathan");
2512 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2513 form.fields[0].is_autofilled = true;
2514 form.fields[1].is_autofilled = true;
2515 form.fields[2].is_autofilled = true;
2516 FillForm(form, input_element);
2518 // Find the newly-filled form that contains the input element.
2519 FormData form2;
2520 FormFieldData field2;
2521 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2522 &form2,
2523 &field2,
2524 autofill::REQUIRE_NONE));
2526 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2527 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2528 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2530 const std::vector<FormFieldData>& fields2 = form2.fields;
2531 ASSERT_EQ(3U, fields2.size());
2533 expected.form_control_type = "text";
2535 expected.name = ASCIIToUTF16("firstname");
2536 expected.value = ASCIIToUTF16("Broth");
2537 expected.max_length = 5;
2538 expected.is_autofilled = true;
2539 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2541 expected.name = ASCIIToUTF16("lastname");
2542 expected.value = ASCIIToUTF16("Jonatha");
2543 expected.max_length = 7;
2544 expected.is_autofilled = true;
2545 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2547 expected.name = ASCIIToUTF16("email");
2548 expected.value = ASCIIToUTF16("brotherj@");
2549 expected.max_length = 9;
2550 expected.is_autofilled = true;
2551 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2554 // This test uses negative values of the maxlength attribute for input elements.
2555 // In this case, the maxlength of the input elements is set to the default
2556 // maxlength (defined in WebKit.)
2557 TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
2558 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2559 " <INPUT type='text' id='firstname' maxlength='-1'/>"
2560 " <INPUT type='text' id='lastname' maxlength='-10'/>"
2561 " <INPUT type='text' id='email' maxlength='-13'/>"
2562 " <INPUT type='submit' name='reply-send' value='Send'/>"
2563 "</FORM>");
2565 WebFrame* web_frame = GetMainFrame();
2566 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2568 FormCache form_cache;
2569 std::vector<FormData> forms;
2570 form_cache.ExtractNewForms(*web_frame, &forms);
2571 ASSERT_EQ(1U, forms.size());
2573 // Get the input element we want to find.
2574 WebElement element = web_frame->document().getElementById("firstname");
2575 WebInputElement input_element = element.to<WebInputElement>();
2577 // Find the form that contains the input element.
2578 FormData form;
2579 FormFieldData field;
2580 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2581 &form,
2582 &field,
2583 autofill::REQUIRE_NONE));
2584 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2585 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2586 EXPECT_EQ(GURL("http://buh.com"), form.action);
2588 const std::vector<FormFieldData>& fields = form.fields;
2589 ASSERT_EQ(3U, fields.size());
2591 FormFieldData expected;
2592 expected.form_control_type = "text";
2593 expected.max_length = WebInputElement::defaultMaxLength();
2595 expected.name = ASCIIToUTF16("firstname");
2596 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2598 expected.name = ASCIIToUTF16("lastname");
2599 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2601 expected.name = ASCIIToUTF16("email");
2602 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2604 // Fill the form.
2605 form.fields[0].value = ASCIIToUTF16("Brother");
2606 form.fields[1].value = ASCIIToUTF16("Jonathan");
2607 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2608 FillForm(form, input_element);
2610 // Find the newly-filled form that contains the input element.
2611 FormData form2;
2612 FormFieldData field2;
2613 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2614 &form2,
2615 &field2,
2616 autofill::REQUIRE_NONE));
2618 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2619 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2620 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2622 const std::vector<FormFieldData>& fields2 = form2.fields;
2623 ASSERT_EQ(3U, fields2.size());
2625 expected.name = ASCIIToUTF16("firstname");
2626 expected.value = ASCIIToUTF16("Brother");
2627 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2629 expected.name = ASCIIToUTF16("lastname");
2630 expected.value = ASCIIToUTF16("Jonathan");
2631 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2633 expected.name = ASCIIToUTF16("email");
2634 expected.value = ASCIIToUTF16("brotherj@example.com");
2635 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2638 TEST_F(FormAutofillTest, FillFormEmptyName) {
2639 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2640 " <INPUT type='text' id='firstname'/>"
2641 " <INPUT type='text' id='lastname'/>"
2642 " <INPUT type='text' id='email'/>"
2643 " <INPUT type='submit' value='Send'/>"
2644 "</FORM>");
2646 WebFrame* web_frame = GetMainFrame();
2647 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2649 FormCache form_cache;
2650 std::vector<FormData> forms;
2651 form_cache.ExtractNewForms(*web_frame, &forms);
2652 ASSERT_EQ(1U, forms.size());
2654 // Get the input element we want to find.
2655 WebElement element = web_frame->document().getElementById("firstname");
2656 WebInputElement input_element = element.to<WebInputElement>();
2658 // Find the form that contains the input element.
2659 FormData form;
2660 FormFieldData field;
2661 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2662 &form,
2663 &field,
2664 autofill::REQUIRE_NONE));
2665 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2666 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2667 EXPECT_EQ(GURL("http://buh.com"), form.action);
2669 const std::vector<FormFieldData>& fields = form.fields;
2670 ASSERT_EQ(3U, fields.size());
2672 FormFieldData expected;
2673 expected.form_control_type = "text";
2674 expected.max_length = WebInputElement::defaultMaxLength();
2676 expected.name = ASCIIToUTF16("firstname");
2677 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2679 expected.name = ASCIIToUTF16("lastname");
2680 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2682 expected.name = ASCIIToUTF16("email");
2683 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2685 // Fill the form.
2686 form.fields[0].value = ASCIIToUTF16("Wyatt");
2687 form.fields[1].value = ASCIIToUTF16("Earp");
2688 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2689 FillForm(form, input_element);
2691 // Find the newly-filled form that contains the input element.
2692 FormData form2;
2693 FormFieldData field2;
2694 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2695 &form2,
2696 &field2,
2697 autofill::REQUIRE_NONE));
2699 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2700 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2701 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2703 const std::vector<FormFieldData>& fields2 = form2.fields;
2704 ASSERT_EQ(3U, fields2.size());
2706 expected.form_control_type = "text";
2707 expected.max_length = WebInputElement::defaultMaxLength();
2709 expected.name = ASCIIToUTF16("firstname");
2710 expected.value = ASCIIToUTF16("Wyatt");
2711 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2713 expected.name = ASCIIToUTF16("lastname");
2714 expected.value = ASCIIToUTF16("Earp");
2715 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2717 expected.name = ASCIIToUTF16("email");
2718 expected.value = ASCIIToUTF16("wyatt@example.com");
2719 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2722 TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
2723 LoadHTML("<FORM action='http://buh.com' method='post'>"
2724 " <INPUT type='text' id='firstname'/>"
2725 " <INPUT type='text' id='middlename'/>"
2726 " <INPUT type='text' id='lastname'/>"
2727 " <INPUT type='submit' value='Send'/>"
2728 "</FORM>"
2729 "<FORM action='http://abc.com' method='post'>"
2730 " <INPUT type='text' id='apple'/>"
2731 " <INPUT type='text' id='banana'/>"
2732 " <INPUT type='text' id='cantelope'/>"
2733 " <INPUT type='submit' value='Send'/>"
2734 "</FORM>");
2736 WebFrame* web_frame = GetMainFrame();
2737 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2739 FormCache form_cache;
2740 std::vector<FormData> forms;
2741 form_cache.ExtractNewForms(*web_frame, &forms);
2742 ASSERT_EQ(2U, forms.size());
2744 // Get the input element we want to find.
2745 WebElement element = web_frame->document().getElementById("apple");
2746 WebInputElement input_element = element.to<WebInputElement>();
2748 // Find the form that contains the input element.
2749 FormData form;
2750 FormFieldData field;
2751 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2752 &form,
2753 &field,
2754 autofill::REQUIRE_NONE));
2755 EXPECT_EQ(base::string16(), form.name);
2756 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2757 EXPECT_EQ(GURL("http://abc.com"), form.action);
2759 const std::vector<FormFieldData>& fields = form.fields;
2760 ASSERT_EQ(3U, fields.size());
2762 FormFieldData expected;
2763 expected.form_control_type = "text";
2764 expected.max_length = WebInputElement::defaultMaxLength();
2766 expected.name = ASCIIToUTF16("apple");
2767 expected.is_autofilled = false;
2768 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2770 expected.name = ASCIIToUTF16("banana");
2771 expected.is_autofilled = false;
2772 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2774 expected.name = ASCIIToUTF16("cantelope");
2775 expected.is_autofilled = false;
2776 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2778 // Fill the form.
2779 form.fields[0].value = ASCIIToUTF16("Red");
2780 form.fields[1].value = ASCIIToUTF16("Yellow");
2781 form.fields[2].value = ASCIIToUTF16("Also Yellow");
2782 form.fields[0].is_autofilled = true;
2783 form.fields[1].is_autofilled = true;
2784 form.fields[2].is_autofilled = true;
2785 FillForm(form, input_element);
2787 // Find the newly-filled form that contains the input element.
2788 FormData form2;
2789 FormFieldData field2;
2790 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2791 &form2,
2792 &field2,
2793 autofill::REQUIRE_NONE));
2795 EXPECT_EQ(base::string16(), form2.name);
2796 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2797 EXPECT_EQ(GURL("http://abc.com"), form2.action);
2799 const std::vector<FormFieldData>& fields2 = form2.fields;
2800 ASSERT_EQ(3U, fields2.size());
2802 expected.name = ASCIIToUTF16("apple");
2803 expected.value = ASCIIToUTF16("Red");
2804 expected.is_autofilled = true;
2805 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2807 expected.name = ASCIIToUTF16("banana");
2808 expected.value = ASCIIToUTF16("Yellow");
2809 expected.is_autofilled = true;
2810 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2812 expected.name = ASCIIToUTF16("cantelope");
2813 expected.value = ASCIIToUTF16("Also Yellow");
2814 expected.is_autofilled = true;
2815 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2818 TEST_F(FormAutofillTest, ThreePartPhone) {
2819 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2820 " Phone:"
2821 " <input type='text' name='dayphone1'>"
2822 " -"
2823 " <input type='text' name='dayphone2'>"
2824 " -"
2825 " <input type='text' name='dayphone3'>"
2826 " ext.:"
2827 " <input type='text' name='dayphone4'>"
2828 " <input type='submit' name='reply-send' value='Send'>"
2829 "</FORM>");
2832 WebFrame* frame = GetMainFrame();
2833 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2835 WebVector<WebFormElement> forms;
2836 frame->document().forms(forms);
2837 ASSERT_EQ(1U, forms.size());
2839 FormData form;
2840 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2841 WebFormControlElement(),
2842 autofill::REQUIRE_NONE,
2843 autofill::EXTRACT_VALUE,
2844 &form,
2845 NULL));
2846 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2847 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2848 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2850 const std::vector<FormFieldData>& fields = form.fields;
2851 ASSERT_EQ(4U, fields.size());
2853 FormFieldData expected;
2854 expected.form_control_type = "text";
2855 expected.max_length = WebInputElement::defaultMaxLength();
2857 expected.label = ASCIIToUTF16("Phone:");
2858 expected.name = ASCIIToUTF16("dayphone1");
2859 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2861 expected.label = ASCIIToUTF16("-");
2862 expected.name = ASCIIToUTF16("dayphone2");
2863 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2865 expected.label = ASCIIToUTF16("-");
2866 expected.name = ASCIIToUTF16("dayphone3");
2867 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2869 expected.label = ASCIIToUTF16("ext.:");
2870 expected.name = ASCIIToUTF16("dayphone4");
2871 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2875 TEST_F(FormAutofillTest, MaxLengthFields) {
2876 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2877 " Phone:"
2878 " <input type='text' maxlength='3' name='dayphone1'>"
2879 " -"
2880 " <input type='text' maxlength='3' name='dayphone2'>"
2881 " -"
2882 " <input type='text' maxlength='4' size='5'"
2883 " name='dayphone3'>"
2884 " ext.:"
2885 " <input type='text' maxlength='5' name='dayphone4'>"
2886 " <input type='text' name='default1'>"
2887 " <input type='text' maxlength='-1' name='invalid1'>"
2888 " <input type='submit' name='reply-send' value='Send'>"
2889 "</FORM>");
2891 WebFrame* frame = GetMainFrame();
2892 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2894 WebVector<WebFormElement> forms;
2895 frame->document().forms(forms);
2896 ASSERT_EQ(1U, forms.size());
2898 FormData form;
2899 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2900 WebFormControlElement(),
2901 autofill::REQUIRE_NONE,
2902 autofill::EXTRACT_VALUE,
2903 &form,
2904 NULL));
2905 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2906 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2907 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2909 const std::vector<FormFieldData>& fields = form.fields;
2910 ASSERT_EQ(6U, fields.size());
2912 FormFieldData expected;
2913 expected.form_control_type = "text";
2915 expected.label = ASCIIToUTF16("Phone:");
2916 expected.name = ASCIIToUTF16("dayphone1");
2917 expected.max_length = 3;
2918 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2920 expected.label = ASCIIToUTF16("-");
2921 expected.name = ASCIIToUTF16("dayphone2");
2922 expected.max_length = 3;
2923 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2925 expected.label = ASCIIToUTF16("-");
2926 expected.name = ASCIIToUTF16("dayphone3");
2927 expected.max_length = 4;
2928 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2930 expected.label = ASCIIToUTF16("ext.:");
2931 expected.name = ASCIIToUTF16("dayphone4");
2932 expected.max_length = 5;
2933 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2935 // When unspecified |size|, default is returned.
2936 expected.label = base::string16();
2937 expected.name = ASCIIToUTF16("default1");
2938 expected.max_length = WebInputElement::defaultMaxLength();
2939 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
2941 // When invalid |size|, default is returned.
2942 expected.label = base::string16();
2943 expected.name = ASCIIToUTF16("invalid1");
2944 expected.max_length = WebInputElement::defaultMaxLength();
2945 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
2948 // This test re-creates the experience of typing in a field then selecting a
2949 // profile from the Autofill suggestions popup. The field that is being typed
2950 // into should be filled even though it's not technically empty.
2951 TEST_F(FormAutofillTest, FillFormNonEmptyField) {
2952 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2953 " <INPUT type='text' id='firstname'/>"
2954 " <INPUT type='text' id='lastname'/>"
2955 " <INPUT type='text' id='email'/>"
2956 " <INPUT type='submit' value='Send'/>"
2957 "</FORM>");
2959 WebFrame* web_frame = GetMainFrame();
2960 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2962 FormCache form_cache;
2963 std::vector<FormData> forms;
2964 form_cache.ExtractNewForms(*web_frame, &forms);
2965 ASSERT_EQ(1U, forms.size());
2967 // Get the input element we want to find.
2968 WebElement element = web_frame->document().getElementById("firstname");
2969 WebInputElement input_element = element.to<WebInputElement>();
2971 // Simulate typing by modifying the field value.
2972 input_element.setValue(ASCIIToUTF16("Wy"));
2974 // Find the form that contains the input element.
2975 FormData form;
2976 FormFieldData field;
2977 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2978 &form,
2979 &field,
2980 autofill::REQUIRE_NONE));
2981 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2982 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2983 EXPECT_EQ(GURL("http://buh.com"), form.action);
2985 const std::vector<FormFieldData>& fields = form.fields;
2986 ASSERT_EQ(3U, fields.size());
2988 FormFieldData expected;
2989 expected.form_control_type = "text";
2990 expected.max_length = WebInputElement::defaultMaxLength();
2992 expected.name = ASCIIToUTF16("firstname");
2993 expected.value = ASCIIToUTF16("Wy");
2994 expected.is_autofilled = false;
2995 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2997 expected.name = ASCIIToUTF16("lastname");
2998 expected.value = base::string16();
2999 expected.is_autofilled = false;
3000 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3002 expected.name = ASCIIToUTF16("email");
3003 expected.value = base::string16();
3004 expected.is_autofilled = false;
3005 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3007 // Preview the form and verify that the cursor position has been updated.
3008 form.fields[0].value = ASCIIToUTF16("Wyatt");
3009 form.fields[1].value = ASCIIToUTF16("Earp");
3010 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
3011 form.fields[0].is_autofilled = true;
3012 form.fields[1].is_autofilled = true;
3013 form.fields[2].is_autofilled = true;
3014 PreviewForm(form, input_element);
3015 EXPECT_EQ(2, input_element.selectionStart());
3016 EXPECT_EQ(5, input_element.selectionEnd());
3018 // Fill the form.
3019 FillForm(form, input_element);
3021 // Find the newly-filled form that contains the input element.
3022 FormData form2;
3023 FormFieldData field2;
3024 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
3025 &form2,
3026 &field2,
3027 autofill::REQUIRE_NONE));
3029 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3030 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3031 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3033 const std::vector<FormFieldData>& fields2 = form2.fields;
3034 ASSERT_EQ(3U, fields2.size());
3036 expected.name = ASCIIToUTF16("firstname");
3037 expected.value = ASCIIToUTF16("Wyatt");
3038 expected.is_autofilled = true;
3039 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3041 expected.name = ASCIIToUTF16("lastname");
3042 expected.value = ASCIIToUTF16("Earp");
3043 expected.is_autofilled = true;
3044 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3046 expected.name = ASCIIToUTF16("email");
3047 expected.value = ASCIIToUTF16("wyatt@example.com");
3048 expected.is_autofilled = true;
3049 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3051 // Verify that the cursor position has been updated.
3052 EXPECT_EQ(5, input_element.selectionStart());
3053 EXPECT_EQ(5, input_element.selectionEnd());
3056 TEST_F(FormAutofillTest, ClearFormWithNode) {
3057 LoadHTML(
3058 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3059 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3060 " <INPUT type='text' id='lastname' value='Earp'/>"
3061 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3062 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3063 " <INPUT type='month' id='month' value='2012-11'>"
3064 " <INPUT type='month' id='month-disabled' value='2012-11'"
3065 " disabled='disabled'>"
3066 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3067 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3068 " Banana!"
3069 " </TEXTAREA>"
3070 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3071 " <INPUT type='submit' value='Send'/>"
3072 "</FORM>");
3074 WebFrame* web_frame = GetMainFrame();
3075 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3077 FormCache form_cache;
3078 std::vector<FormData> forms;
3079 form_cache.ExtractNewForms(*web_frame, &forms);
3080 ASSERT_EQ(1U, forms.size());
3082 // Set the auto-filled attribute.
3083 WebInputElement firstname =
3084 web_frame->document().getElementById("firstname").to<WebInputElement>();
3085 firstname.setAutofilled(true);
3086 WebInputElement lastname =
3087 web_frame->document().getElementById("lastname").to<WebInputElement>();
3088 lastname.setAutofilled(true);
3089 WebInputElement month =
3090 web_frame->document().getElementById("month").to<WebInputElement>();
3091 month.setAutofilled(true);
3092 WebInputElement textarea =
3093 web_frame->document().getElementById("textarea").to<WebInputElement>();
3094 textarea.setAutofilled(true);
3096 // Set the value of the disabled text input element.
3097 WebInputElement notenabled =
3098 web_frame->document().getElementById("notenabled").to<WebInputElement>();
3099 notenabled.setValue(WebString::fromUTF8("no clear"));
3101 // Clear the form.
3102 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3104 // Verify that the auto-filled attribute has been turned off.
3105 EXPECT_FALSE(firstname.isAutofilled());
3107 // Verify the form is cleared.
3108 FormData form2;
3109 FormFieldData field2;
3110 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
3111 &form2,
3112 &field2,
3113 autofill::REQUIRE_NONE));
3114 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3115 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3116 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3118 const std::vector<FormFieldData>& fields2 = form2.fields;
3119 ASSERT_EQ(9U, fields2.size());
3121 FormFieldData expected;
3122 expected.form_control_type = "text";
3123 expected.max_length = WebInputElement::defaultMaxLength();
3125 expected.name = ASCIIToUTF16("firstname");
3126 expected.value = base::string16();
3127 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3129 expected.name = ASCIIToUTF16("lastname");
3130 expected.value = base::string16();
3131 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3133 expected.name = ASCIIToUTF16("noAC");
3134 expected.value = ASCIIToUTF16("one");
3135 expected.autocomplete_attribute = "off";
3136 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3137 expected.autocomplete_attribute = std::string(); // reset
3139 expected.name = ASCIIToUTF16("notenabled");
3140 expected.value = ASCIIToUTF16("no clear");
3141 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[3]);
3143 expected.form_control_type = "month";
3144 expected.max_length = 0;
3145 expected.name = ASCIIToUTF16("month");
3146 expected.value = base::string16();
3147 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[4]);
3149 expected.name = ASCIIToUTF16("month-disabled");
3150 expected.value = ASCIIToUTF16("2012-11");
3151 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[5]);
3153 expected.form_control_type = "textarea";
3154 expected.name = ASCIIToUTF16("textarea");
3155 expected.value = base::string16();
3156 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[6]);
3158 expected.name = ASCIIToUTF16("textarea-disabled");
3159 expected.value = ASCIIToUTF16(" Banana! ");
3160 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[7]);
3162 expected.name = ASCIIToUTF16("textarea-noAC");
3163 expected.value = ASCIIToUTF16("Carrot?");
3164 expected.autocomplete_attribute = "off";
3165 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[8]);
3166 expected.autocomplete_attribute = std::string(); // reset
3168 // Verify that the cursor position has been updated.
3169 EXPECT_EQ(0, firstname.selectionStart());
3170 EXPECT_EQ(0, firstname.selectionEnd());
3173 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) {
3174 LoadHTML(
3175 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3176 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3177 " <INPUT type='text' id='lastname' value='Earp'/>"
3178 " <SELECT id='state' name='state'>"
3179 " <OPTION selected>?</OPTION>"
3180 " <OPTION>AA</OPTION>"
3181 " <OPTION>AE</OPTION>"
3182 " <OPTION>AK</OPTION>"
3183 " </SELECT>"
3184 " <INPUT type='submit' value='Send'/>"
3185 "</FORM>");
3187 WebFrame* web_frame = GetMainFrame();
3188 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3190 FormCache form_cache;
3191 std::vector<FormData> forms;
3192 form_cache.ExtractNewForms(*web_frame, &forms);
3193 ASSERT_EQ(1U, forms.size());
3195 // Set the auto-filled attribute.
3196 WebInputElement firstname =
3197 web_frame->document().getElementById("firstname").to<WebInputElement>();
3198 firstname.setAutofilled(true);
3199 WebInputElement lastname =
3200 web_frame->document().getElementById("lastname").to<WebInputElement>();
3201 lastname.setAutofilled(true);
3203 // Set the value and auto-filled attribute of the state element.
3204 WebSelectElement state =
3205 web_frame->document().getElementById("state").to<WebSelectElement>();
3206 state.setValue(WebString::fromUTF8("AK"));
3207 state.setAutofilled(true);
3209 // Clear the form.
3210 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3212 // Verify that the auto-filled attribute has been turned off.
3213 EXPECT_FALSE(firstname.isAutofilled());
3215 // Verify the form is cleared.
3216 FormData form2;
3217 FormFieldData field2;
3218 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
3219 &form2,
3220 &field2,
3221 autofill::REQUIRE_NONE));
3222 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3223 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3224 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3226 const std::vector<FormFieldData>& fields2 = form2.fields;
3227 ASSERT_EQ(3U, fields2.size());
3229 FormFieldData expected;
3231 expected.name = ASCIIToUTF16("firstname");
3232 expected.value = base::string16();
3233 expected.form_control_type = "text";
3234 expected.max_length = WebInputElement::defaultMaxLength();
3235 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3237 expected.name = ASCIIToUTF16("lastname");
3238 expected.value = base::string16();
3239 expected.form_control_type = "text";
3240 expected.max_length = WebInputElement::defaultMaxLength();
3241 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3243 expected.name = ASCIIToUTF16("state");
3244 expected.value = ASCIIToUTF16("?");
3245 expected.form_control_type = "select-one";
3246 expected.max_length = 0;
3247 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3249 // Verify that the cursor position has been updated.
3250 EXPECT_EQ(0, firstname.selectionStart());
3251 EXPECT_EQ(0, firstname.selectionEnd());
3254 TEST_F(FormAutofillTest, ClearPreviewedFormWithElement) {
3255 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3256 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3257 " <INPUT type='text' id='lastname'/>"
3258 " <INPUT type='text' id='email'/>"
3259 " <INPUT type='email' id='email2'/>"
3260 " <INPUT type='tel' id='phone'/>"
3261 " <INPUT type='submit' value='Send'/>"
3262 "</FORM>");
3264 WebFrame* web_frame = GetMainFrame();
3265 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3267 FormCache form_cache;
3268 std::vector<FormData> forms;
3269 form_cache.ExtractNewForms(*web_frame, &forms);
3270 ASSERT_EQ(1U, forms.size());
3272 // Set the auto-filled attribute.
3273 WebInputElement firstname =
3274 web_frame->document().getElementById("firstname").to<WebInputElement>();
3275 firstname.setAutofilled(true);
3276 WebInputElement lastname =
3277 web_frame->document().getElementById("lastname").to<WebInputElement>();
3278 lastname.setAutofilled(true);
3279 WebInputElement email =
3280 web_frame->document().getElementById("email").to<WebInputElement>();
3281 email.setAutofilled(true);
3282 WebInputElement email2 =
3283 web_frame->document().getElementById("email2").to<WebInputElement>();
3284 email2.setAutofilled(true);
3285 WebInputElement phone =
3286 web_frame->document().getElementById("phone").to<WebInputElement>();
3287 phone.setAutofilled(true);
3289 // Set the suggested values on two of the elements.
3290 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3291 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3292 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3293 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3295 // Clear the previewed fields.
3296 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
3298 // Fields with empty suggestions suggestions are not modified.
3299 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
3300 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3301 EXPECT_TRUE(firstname.isAutofilled());
3303 // Verify the previewed fields are cleared.
3304 EXPECT_TRUE(lastname.value().isEmpty());
3305 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3306 EXPECT_FALSE(lastname.isAutofilled());
3307 EXPECT_TRUE(email.value().isEmpty());
3308 EXPECT_TRUE(email.suggestedValue().isEmpty());
3309 EXPECT_FALSE(email.isAutofilled());
3310 EXPECT_TRUE(email2.value().isEmpty());
3311 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3312 EXPECT_FALSE(email2.isAutofilled());
3313 EXPECT_TRUE(phone.value().isEmpty());
3314 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3315 EXPECT_FALSE(phone.isAutofilled());
3317 // Verify that the cursor position has been updated.
3318 EXPECT_EQ(0, lastname.selectionStart());
3319 EXPECT_EQ(0, lastname.selectionEnd());
3322 TEST_F(FormAutofillTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
3323 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3324 " <INPUT type='text' id='firstname' value='W'/>"
3325 " <INPUT type='text' id='lastname'/>"
3326 " <INPUT type='text' id='email'/>"
3327 " <INPUT type='email' id='email2'/>"
3328 " <INPUT type='tel' id='phone'/>"
3329 " <INPUT type='submit' value='Send'/>"
3330 "</FORM>");
3332 WebFrame* web_frame = GetMainFrame();
3333 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3335 FormCache form_cache;
3336 std::vector<FormData> forms;
3337 form_cache.ExtractNewForms(*web_frame, &forms);
3338 ASSERT_EQ(1U, forms.size());
3340 // Set the auto-filled attribute.
3341 WebInputElement firstname =
3342 web_frame->document().getElementById("firstname").to<WebInputElement>();
3343 firstname.setAutofilled(true);
3344 WebInputElement lastname =
3345 web_frame->document().getElementById("lastname").to<WebInputElement>();
3346 lastname.setAutofilled(true);
3347 WebInputElement email =
3348 web_frame->document().getElementById("email").to<WebInputElement>();
3349 email.setAutofilled(true);
3350 WebInputElement email2 =
3351 web_frame->document().getElementById("email2").to<WebInputElement>();
3352 email2.setAutofilled(true);
3353 WebInputElement phone =
3354 web_frame->document().getElementById("phone").to<WebInputElement>();
3355 phone.setAutofilled(true);
3358 // Set the suggested values on all of the elements.
3359 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3360 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3361 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3362 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3363 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3365 // Clear the previewed fields.
3366 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
3368 // Fields with non-empty values are restored.
3369 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
3370 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3371 EXPECT_FALSE(firstname.isAutofilled());
3372 EXPECT_EQ(1, firstname.selectionStart());
3373 EXPECT_EQ(1, firstname.selectionEnd());
3375 // Verify the previewed fields are cleared.
3376 EXPECT_TRUE(lastname.value().isEmpty());
3377 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3378 EXPECT_FALSE(lastname.isAutofilled());
3379 EXPECT_TRUE(email.value().isEmpty());
3380 EXPECT_TRUE(email.suggestedValue().isEmpty());
3381 EXPECT_FALSE(email.isAutofilled());
3382 EXPECT_TRUE(email2.value().isEmpty());
3383 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3384 EXPECT_FALSE(email2.isAutofilled());
3385 EXPECT_TRUE(phone.value().isEmpty());
3386 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3387 EXPECT_FALSE(phone.isAutofilled());
3390 TEST_F(FormAutofillTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
3391 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3392 " <INPUT type='text' id='firstname' value='W'/>"
3393 " <INPUT type='text' id='lastname'/>"
3394 " <INPUT type='text' id='email'/>"
3395 " <INPUT type='email' id='email2'/>"
3396 " <INPUT type='tel' id='phone'/>"
3397 " <INPUT type='submit' value='Send'/>"
3398 "</FORM>");
3400 WebFrame* web_frame = GetMainFrame();
3401 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3403 FormCache form_cache;
3404 std::vector<FormData> forms;
3405 form_cache.ExtractNewForms(*web_frame, &forms);
3406 ASSERT_EQ(1U, forms.size());
3408 // Set the auto-filled attribute.
3409 WebInputElement firstname =
3410 web_frame->document().getElementById("firstname").to<WebInputElement>();
3411 firstname.setAutofilled(true);
3412 WebInputElement lastname =
3413 web_frame->document().getElementById("lastname").to<WebInputElement>();
3414 lastname.setAutofilled(true);
3415 WebInputElement email =
3416 web_frame->document().getElementById("email").to<WebInputElement>();
3417 email.setAutofilled(true);
3418 WebInputElement email2 =
3419 web_frame->document().getElementById("email2").to<WebInputElement>();
3420 email2.setAutofilled(true);
3421 WebInputElement phone =
3422 web_frame->document().getElementById("phone").to<WebInputElement>();
3423 phone.setAutofilled(true);
3425 // Set the suggested values on all of the elements.
3426 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3427 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3428 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3429 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3430 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3432 // Clear the previewed fields.
3433 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
3435 // Fields with non-empty values are restored.
3436 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
3437 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3438 EXPECT_TRUE(firstname.isAutofilled());
3439 EXPECT_EQ(1, firstname.selectionStart());
3440 EXPECT_EQ(1, firstname.selectionEnd());
3442 // Verify the previewed fields are cleared.
3443 EXPECT_TRUE(lastname.value().isEmpty());
3444 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3445 EXPECT_FALSE(lastname.isAutofilled());
3446 EXPECT_TRUE(email.value().isEmpty());
3447 EXPECT_TRUE(email.suggestedValue().isEmpty());
3448 EXPECT_FALSE(email.isAutofilled());
3449 EXPECT_TRUE(email2.value().isEmpty());
3450 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3451 EXPECT_FALSE(email2.isAutofilled());
3452 EXPECT_TRUE(phone.value().isEmpty());
3453 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3454 EXPECT_FALSE(phone.isAutofilled());
3457 // Autofill's "Clear Form" should clear only autofilled fields
3458 TEST_F(FormAutofillTest, ClearOnlyAutofilledFields) {
3459 // Load the form.
3460 LoadHTML(
3461 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3462 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3463 " <INPUT type='text' id='lastname' value='Earp'/>"
3464 " <INPUT type='email' id='email' value='wyatt@earp.com'/>"
3465 " <INPUT type='tel' id='phone' value='650-777-9999'/>"
3466 " <INPUT type='submit' value='Send'/>"
3467 "</FORM>");
3469 WebFrame* web_frame = GetMainFrame();
3470 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3472 FormCache form_cache;
3473 std::vector<FormData> forms;
3474 form_cache.ExtractNewForms(*web_frame, &forms);
3475 ASSERT_EQ(1U, forms.size());
3477 // Set the autofilled attribute.
3478 WebInputElement firstname =
3479 web_frame->document().getElementById("firstname").to<WebInputElement>();
3480 firstname.setAutofilled(false);
3481 WebInputElement lastname =
3482 web_frame->document().getElementById("lastname").to<WebInputElement>();
3483 lastname.setAutofilled(true);
3484 WebInputElement email =
3485 web_frame->document().getElementById("email").to<WebInputElement>();
3486 email.setAutofilled(true);
3487 WebInputElement phone =
3488 web_frame->document().getElementById("phone").to<WebInputElement>();
3489 phone.setAutofilled(true);
3491 // Clear the fields.
3492 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3494 // Verify only autofilled fields are cleared.
3495 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
3496 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3497 EXPECT_FALSE(firstname.isAutofilled());
3498 EXPECT_TRUE(lastname.value().isEmpty());
3499 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3500 EXPECT_FALSE(lastname.isAutofilled());
3501 EXPECT_TRUE(email.value().isEmpty());
3502 EXPECT_TRUE(email.suggestedValue().isEmpty());
3503 EXPECT_FALSE(email.isAutofilled());
3504 EXPECT_TRUE(phone.value().isEmpty());
3505 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3506 EXPECT_FALSE(phone.isAutofilled());
3509 TEST_F(FormAutofillTest, FormWithNodeIsAutofilled) {
3510 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3511 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3512 " <INPUT type='text' id='lastname'/>"
3513 " <INPUT type='text' id='email'/>"
3514 " <INPUT type='email' id='email2'/>"
3515 " <INPUT type='tel' id='phone'/>"
3516 " <INPUT type='submit' value='Send'/>"
3517 "</FORM>");
3519 WebFrame* web_frame = GetMainFrame();
3520 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3522 FormCache form_cache;
3523 std::vector<FormData> forms;
3524 form_cache.ExtractNewForms(*web_frame, &forms);
3525 ASSERT_EQ(1U, forms.size());
3527 WebInputElement firstname =
3528 web_frame->document().getElementById("firstname").to<WebInputElement>();
3530 // Auto-filled attribute not set yet.
3531 EXPECT_FALSE(FormWithElementIsAutofilled(firstname));
3533 // Set the auto-filled attribute.
3534 firstname.setAutofilled(true);
3536 EXPECT_TRUE(FormWithElementIsAutofilled(firstname));
3539 // If we have multiple labels per id, the labels concatenated into label string.
3540 TEST_F(FormAutofillTest, MultipleLabelsPerElement) {
3541 std::vector<base::string16> labels, names, values;
3543 labels.push_back(ASCIIToUTF16("First Name:"));
3544 names.push_back(ASCIIToUTF16("firstname"));
3545 values.push_back(ASCIIToUTF16("John"));
3547 labels.push_back(ASCIIToUTF16("Last Name:"));
3548 names.push_back(ASCIIToUTF16("lastname"));
3549 values.push_back(ASCIIToUTF16("Smith"));
3551 labels.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
3552 names.push_back(ASCIIToUTF16("email"));
3553 values.push_back(ASCIIToUTF16("john@example.com"));
3555 ExpectLabels(
3556 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3557 " <LABEL for='firstname'> First Name: </LABEL>"
3558 " <LABEL for='firstname'></LABEL>"
3559 " <INPUT type='text' id='firstname' value='John'/>"
3560 " <LABEL for='lastname'></LABEL>"
3561 " <LABEL for='lastname'> Last Name: </LABEL>"
3562 " <INPUT type='text' id='lastname' value='Smith'/>"
3563 " <LABEL for='email'> Email: </LABEL>"
3564 " <LABEL for='email'> xxx@yyy.com </LABEL>"
3565 " <INPUT type='text' id='email' value='john@example.com'/>"
3566 " <INPUT type='submit' name='reply-send' value='Send'/>"
3567 "</FORM>",
3568 labels, names, values);
3571 TEST_F(FormAutofillTest, ClickElement) {
3572 LoadHTML("<BUTTON id='link'>Button</BUTTON>"
3573 "<BUTTON name='button'>Button</BUTTON>");
3574 WebFrame* frame = GetMainFrame();
3575 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3577 // Successful retrieval by id.
3578 autofill::WebElementDescriptor clicker;
3579 clicker.retrieval_method = autofill::WebElementDescriptor::ID;
3580 clicker.descriptor = "link";
3581 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3583 // Successful retrieval by css selector.
3584 clicker.retrieval_method = autofill::WebElementDescriptor::CSS_SELECTOR;
3585 clicker.descriptor = "button[name='button']";
3586 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3588 // Unsuccessful retrieval due to invalid CSS selector.
3589 clicker.descriptor = "^*&";
3590 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3592 // Unsuccessful retrieval because element does not exist.
3593 clicker.descriptor = "#junk";
3594 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3597 TEST_F(FormAutofillTest, SelectOneAsText) {
3598 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3599 " <INPUT type='text' id='firstname' value='John'/>"
3600 " <INPUT type='text' id='lastname' value='Smith'/>"
3601 " <SELECT id='country'>"
3602 " <OPTION value='AF'>Afghanistan</OPTION>"
3603 " <OPTION value='AL'>Albania</OPTION>"
3604 " <OPTION value='DZ'>Algeria</OPTION>"
3605 " </SELECT>"
3606 " <INPUT type='submit' name='reply-send' value='Send'/>"
3607 "</FORM>");
3609 WebFrame* frame = GetMainFrame();
3610 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3612 // Set the value of the select-one.
3613 WebSelectElement select_element =
3614 frame->document().getElementById("country").to<WebSelectElement>();
3615 select_element.setValue(WebString::fromUTF8("AL"));
3617 WebVector<WebFormElement> forms;
3618 frame->document().forms(forms);
3619 ASSERT_EQ(1U, forms.size());
3621 FormData form;
3623 // Extract the country select-one value as text.
3624 EXPECT_TRUE(WebFormElementToFormData(
3625 forms[0], WebFormControlElement(), autofill::REQUIRE_NONE,
3626 static_cast<autofill::ExtractMask>(
3627 autofill::EXTRACT_VALUE | autofill::EXTRACT_OPTION_TEXT),
3628 &form, NULL));
3629 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3630 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3631 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3633 const std::vector<FormFieldData>& fields = form.fields;
3634 ASSERT_EQ(3U, fields.size());
3636 FormFieldData expected;
3638 expected.name = ASCIIToUTF16("firstname");
3639 expected.value = ASCIIToUTF16("John");
3640 expected.form_control_type = "text";
3641 expected.max_length = WebInputElement::defaultMaxLength();
3642 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3644 expected.name = ASCIIToUTF16("lastname");
3645 expected.value = ASCIIToUTF16("Smith");
3646 expected.form_control_type = "text";
3647 expected.max_length = WebInputElement::defaultMaxLength();
3648 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3650 expected.name = ASCIIToUTF16("country");
3651 expected.value = ASCIIToUTF16("Albania");
3652 expected.form_control_type = "select-one";
3653 expected.max_length = 0;
3654 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3656 form.fields.clear();
3657 // Extract the country select-one value as value.
3658 EXPECT_TRUE(WebFormElementToFormData(forms[0],
3659 WebFormControlElement(),
3660 autofill::REQUIRE_NONE,
3661 autofill::EXTRACT_VALUE,
3662 &form,
3663 NULL));
3664 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3665 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3666 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3668 ASSERT_EQ(3U, fields.size());
3670 expected.name = ASCIIToUTF16("firstname");
3671 expected.value = ASCIIToUTF16("John");
3672 expected.form_control_type = "text";
3673 expected.max_length = WebInputElement::defaultMaxLength();
3674 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3676 expected.name = ASCIIToUTF16("lastname");
3677 expected.value = ASCIIToUTF16("Smith");
3678 expected.form_control_type = "text";
3679 expected.max_length = WebInputElement::defaultMaxLength();
3680 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3682 expected.name = ASCIIToUTF16("country");
3683 expected.value = ASCIIToUTF16("AL");
3684 expected.form_control_type = "select-one";
3685 expected.max_length = 0;
3686 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3688 } // namespace autofill