Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / chrome / renderer / autofill / form_autofill_browsertest.cc
blob8816786f85d1a2d01d131982efff7a69a084c2dd
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/strings/string16.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/test/base/chrome_render_view_test.h"
12 #include "components/autofill/content/renderer/form_autofill_util.h"
13 #include "components/autofill/content/renderer/form_cache.h"
14 #include "components/autofill/core/common/autofill_data_validation.h"
15 #include "components/autofill/core/common/form_data.h"
16 #include "components/autofill/core/common/web_element_descriptor.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/platform/WebVector.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebFormControlElement.h"
23 #include "third_party/WebKit/public/web/WebFormElement.h"
24 #include "third_party/WebKit/public/web/WebInputElement.h"
25 #include "third_party/WebKit/public/web/WebLocalFrame.h"
26 #include "third_party/WebKit/public/web/WebNode.h"
27 #include "third_party/WebKit/public/web/WebSelectElement.h"
28 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
30 using base::ASCIIToUTF16;
31 using blink::WebDocument;
32 using blink::WebElement;
33 using blink::WebFormControlElement;
34 using blink::WebFormElement;
35 using blink::WebFrame;
36 using blink::WebInputElement;
37 using blink::WebNode;
38 using blink::WebSelectElement;
39 using blink::WebString;
40 using blink::WebTextAreaElement;
41 using blink::WebVector;
43 namespace {
45 struct AutofillFieldCase {
46 const char* const form_control_type;
47 const char* const name;
48 const char* const initial_value;
49 const char* const autocomplete_attribute; // The autocomplete attribute of
50 // the element.
51 bool should_be_autofilled; // Whether the filed should be autofilled.
52 const char* const autofill_value; // The value being used to fill the field.
53 const char* const expected_value; // The expected value after Autofill
54 // or Preview.
57 static const char kFormHtml[] =
58 "<FORM name='TestForm' action='http://buh.com' method='post'>"
59 " <INPUT type='text' id='firstname'/>"
60 " <INPUT type='text' id='lastname'/>"
61 " <INPUT type='hidden' id='imhidden'/>"
62 " <INPUT type='text' id='notempty' value='Hi'/>"
63 " <INPUT type='text' autocomplete='off' id='noautocomplete'/>"
64 " <INPUT type='text' disabled='disabled' id='notenabled'/>"
65 " <INPUT type='text' readonly id='readonly'/>"
66 " <INPUT type='text' style='visibility: hidden'"
67 " id='invisible'/>"
68 " <INPUT type='text' style='display: none' id='displaynone'/>"
69 " <INPUT type='month' id='month'/>"
70 " <INPUT type='month' id='month-nonempty' value='2011-12'/>"
71 " <SELECT id='select'>"
72 " <OPTION></OPTION>"
73 " <OPTION value='CA'>California</OPTION>"
74 " <OPTION value='TX'>Texas</OPTION>"
75 " </SELECT>"
76 " <SELECT id='select-nonempty'>"
77 " <OPTION value='CA' selected>California</OPTION>"
78 " <OPTION value='TX'>Texas</OPTION>"
79 " </SELECT>"
80 " <SELECT id='select-unchanged'>"
81 " <OPTION value='CA' selected>California</OPTION>"
82 " <OPTION value='TX'>Texas</OPTION>"
83 " </SELECT>"
84 " <TEXTAREA id='textarea'></TEXTAREA>"
85 " <TEXTAREA id='textarea-nonempty'>Go&#10;away!</TEXTAREA>"
86 " <INPUT type='submit' name='reply-send' value='Send'/>"
87 "</FORM>";
89 } // namespace
91 namespace autofill {
93 class FormAutofillTest : public ChromeRenderViewTest {
94 public:
95 FormAutofillTest() : ChromeRenderViewTest() {}
96 ~FormAutofillTest() override {}
98 void ExpectLabels(const char* html,
99 const std::vector<base::string16>& labels,
100 const std::vector<base::string16>& names,
101 const std::vector<base::string16>& values) {
102 std::vector<std::string> control_types(labels.size(), "text");
103 ExpectLabelsAndTypes(html, labels, names, values, control_types);
106 void ExpectLabelsAndTypes(const char* html,
107 const std::vector<base::string16>& labels,
108 const std::vector<base::string16>& names,
109 const std::vector<base::string16>& values,
110 const std::vector<std::string>& control_types) {
111 ASSERT_EQ(labels.size(), names.size());
112 ASSERT_EQ(labels.size(), values.size());
113 ASSERT_EQ(labels.size(), control_types.size());
115 LoadHTML(html);
117 WebFrame* web_frame = GetMainFrame();
118 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
120 FormCache form_cache;
121 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
122 ASSERT_EQ(1U, forms.size());
124 const FormData& form = forms[0];
125 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
126 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
127 EXPECT_EQ(GURL("http://cnn.com"), form.action);
129 const std::vector<FormFieldData>& fields = form.fields;
130 ASSERT_EQ(labels.size(), fields.size());
131 for (size_t i = 0; i < labels.size(); ++i) {
132 int max_length = control_types[i] == "text" ?
133 WebInputElement::defaultMaxLength() : 0;
134 FormFieldData expected;
135 expected.label = labels[i];
136 expected.name = names[i];
137 expected.value = values[i];
138 expected.form_control_type = control_types[i];
139 expected.max_length = max_length;
140 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
141 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
145 void ExpectJohnSmithLabels(const char* html) {
146 std::vector<base::string16> labels, names, values;
148 labels.push_back(ASCIIToUTF16("First name:"));
149 names.push_back(ASCIIToUTF16("firstname"));
150 values.push_back(ASCIIToUTF16("John"));
152 labels.push_back(ASCIIToUTF16("Last name:"));
153 names.push_back(ASCIIToUTF16("lastname"));
154 values.push_back(ASCIIToUTF16("Smith"));
156 labels.push_back(ASCIIToUTF16("Email:"));
157 names.push_back(ASCIIToUTF16("email"));
158 values.push_back(ASCIIToUTF16("john@example.com"));
160 ExpectLabels(html, labels, names, values);
163 typedef void (*FillFormFunction)(const FormData& form,
164 const WebFormControlElement& element);
166 typedef WebString (*GetValueFunction)(WebFormControlElement element);
168 // Test FormFillxxx functions.
169 void TestFormFillFunctions(const char* html,
170 const AutofillFieldCase* field_cases,
171 size_t number_of_field_cases,
172 FillFormFunction fill_form_function,
173 GetValueFunction get_value_function) {
174 LoadHTML(html);
176 WebFrame* web_frame = GetMainFrame();
177 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
179 FormCache form_cache;
180 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
181 ASSERT_EQ(1U, forms.size());
183 // Get the input element we want to find.
184 WebElement element = web_frame->document().getElementById("firstname");
185 WebInputElement input_element = element.to<WebInputElement>();
187 // Find the form that contains the input element.
188 FormData form_data;
189 FormFieldData field;
190 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
191 input_element, &form_data, &field, autofill::REQUIRE_NONE));
192 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name);
193 EXPECT_EQ(GURL(web_frame->document().url()), form_data.origin);
194 EXPECT_EQ(GURL("http://buh.com"), form_data.action);
196 const std::vector<FormFieldData>& fields = form_data.fields;
197 ASSERT_EQ(number_of_field_cases, fields.size());
199 FormFieldData expected;
200 // Verify field's initial value.
201 for (size_t i = 0; i < number_of_field_cases; ++i) {
202 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
203 field_cases[i].name));
204 expected.form_control_type = field_cases[i].form_control_type;
205 expected.max_length =
206 expected.form_control_type == "text" ?
207 WebInputElement::defaultMaxLength() : 0;
208 expected.name = ASCIIToUTF16(field_cases[i].name);
209 expected.value = ASCIIToUTF16(field_cases[i].initial_value);
210 expected.autocomplete_attribute = field_cases[i].autocomplete_attribute;
211 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
212 // Fill the form_data for the field.
213 form_data.fields[i].value = ASCIIToUTF16(field_cases[i].autofill_value);
214 // Set the is_autofilled property for the field.
215 form_data.fields[i].is_autofilled = field_cases[i].should_be_autofilled;
218 // Autofill the form using the given fill form function.
219 fill_form_function(form_data, input_element);
221 // Validate Autofill or Preview results.
222 for (size_t i = 0; i < number_of_field_cases; ++i) {
223 ValidateFilledField(field_cases[i], get_value_function);
227 // Validate an Autofilled field.
228 void ValidateFilledField(const AutofillFieldCase& field_case,
229 GetValueFunction get_value_function) {
230 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
231 field_case.name));
232 WebString value;
233 WebFormControlElement element = GetMainFrame()->document().getElementById(
234 ASCIIToUTF16(field_case.name)).to<WebFormControlElement>();
235 if ((element.formControlType() == "select-one") ||
236 (element.formControlType() == "textarea")) {
237 value = get_value_function(element);
238 } else {
239 ASSERT_TRUE(element.formControlType() == "text" ||
240 element.formControlType() == "month");
241 value = get_value_function(element);
244 const WebString expected_value = ASCIIToUTF16(field_case.expected_value);
245 if (expected_value.isEmpty())
246 EXPECT_TRUE(value.isEmpty());
247 else
248 EXPECT_EQ(expected_value.utf8(), value.utf8());
250 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled());
253 static void FillFormForAllFieldsWrapper(const FormData& form,
254 const WebInputElement& element) {
255 FillFormForAllElements(form, element.form());
258 static void FillFormIncludingNonFocusableElementsWrapper(
259 const FormData& form,
260 const WebFormControlElement& element) {
261 FillFormIncludingNonFocusableElements(form, element.form());
264 static WebString GetValueWrapper(WebFormControlElement element) {
265 if (element.formControlType() == "textarea")
266 return element.to<WebTextAreaElement>().value();
268 if (element.formControlType() == "select-one")
269 return element.to<WebSelectElement>().value();
271 return element.to<WebInputElement>().value();
274 static WebString GetSuggestedValueWrapper(WebFormControlElement element) {
275 if (element.formControlType() == "textarea")
276 return element.to<WebTextAreaElement>().suggestedValue();
278 if (element.formControlType() == "select-one")
279 return element.to<WebSelectElement>().suggestedValue();
281 return element.to<WebInputElement>().suggestedValue();
284 private:
285 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest);
288 // We should be able to extract a normal text field.
289 TEST_F(FormAutofillTest, WebFormControlElementToFormField) {
290 LoadHTML("<INPUT type='text' id='element' value='value'/>");
292 WebFrame* frame = GetMainFrame();
293 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
295 WebElement web_element = frame->document().getElementById("element");
296 WebFormControlElement element = web_element.to<WebFormControlElement>();
297 FormFieldData result1;
298 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result1);
300 FormFieldData expected;
301 expected.form_control_type = "text";
302 expected.max_length = WebInputElement::defaultMaxLength();
304 expected.name = ASCIIToUTF16("element");
305 expected.value = base::string16();
306 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
308 FormFieldData result2;
309 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result2);
311 expected.name = ASCIIToUTF16("element");
312 expected.value = ASCIIToUTF16("value");
313 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
316 // We should be able to extract a text field with autocomplete="off".
317 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompleteOff) {
318 LoadHTML("<INPUT type='text' id='element' value='value'"
319 " autocomplete='off'/>");
321 WebFrame* frame = GetMainFrame();
322 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
324 WebElement web_element = frame->document().getElementById("element");
325 WebFormControlElement element = web_element.to<WebFormControlElement>();
326 FormFieldData result;
327 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
329 FormFieldData expected;
330 expected.name = ASCIIToUTF16("element");
331 expected.value = ASCIIToUTF16("value");
332 expected.form_control_type = "text";
333 expected.autocomplete_attribute = "off";
334 expected.max_length = WebInputElement::defaultMaxLength();
335 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
338 // We should be able to extract a text field with maxlength specified.
339 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMaxLength) {
340 LoadHTML("<INPUT type='text' id='element' value='value'"
341 " maxlength='5'/>");
343 WebFrame* frame = GetMainFrame();
344 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
346 WebElement web_element = frame->document().getElementById("element");
347 WebFormControlElement element = web_element.to<WebFormControlElement>();
348 FormFieldData result;
349 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
351 FormFieldData expected;
352 expected.name = ASCIIToUTF16("element");
353 expected.value = ASCIIToUTF16("value");
354 expected.form_control_type = "text";
355 expected.max_length = 5;
356 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
359 // We should be able to extract a text field that has been autofilled.
360 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutofilled) {
361 LoadHTML("<INPUT type='text' id='element' value='value'/>");
363 WebFrame* frame = GetMainFrame();
364 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
366 WebElement web_element = frame->document().getElementById("element");
367 WebInputElement element = web_element.to<WebInputElement>();
368 element.setAutofilled(true);
369 FormFieldData result;
370 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
372 FormFieldData expected;
373 expected.name = ASCIIToUTF16("element");
374 expected.value = ASCIIToUTF16("value");
375 expected.form_control_type = "text";
376 expected.max_length = WebInputElement::defaultMaxLength();
377 expected.is_autofilled = true;
378 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
381 // We should be able to extract a radio or a checkbox field that has been
382 // autofilled.
383 TEST_F(FormAutofillTest, WebFormControlElementToClickableFormField) {
384 LoadHTML("<INPUT type='checkbox' id='checkbox' value='mail' checked/>"
385 "<INPUT type='radio' id='radio' value='male'/>");
387 WebFrame* frame = GetMainFrame();
388 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
390 WebElement web_element = frame->document().getElementById("checkbox");
391 WebInputElement element = web_element.to<WebInputElement>();
392 element.setAutofilled(true);
393 FormFieldData result;
394 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
396 FormFieldData expected;
397 expected.name = ASCIIToUTF16("checkbox");
398 expected.value = ASCIIToUTF16("mail");
399 expected.form_control_type = "checkbox";
400 expected.is_autofilled = true;
401 expected.is_checkable = true;
402 expected.is_checked = true;
403 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
405 web_element = frame->document().getElementById("radio");
406 element = web_element.to<WebInputElement>();
407 element.setAutofilled(true);
408 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
409 expected.name = ASCIIToUTF16("radio");
410 expected.value = ASCIIToUTF16("male");
411 expected.form_control_type = "radio";
412 expected.is_autofilled = true;
413 expected.is_checkable = true;
414 expected.is_checked = false;
415 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
418 // We should be able to extract a <select> field.
419 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) {
420 LoadHTML("<SELECT id='element'/>"
421 " <OPTION value='CA'>California</OPTION>"
422 " <OPTION value='TX'>Texas</OPTION>"
423 "</SELECT>");
425 WebFrame* frame = GetMainFrame();
426 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
428 WebElement web_element = frame->document().getElementById("element");
429 WebFormControlElement element = web_element.to<WebFormControlElement>();
430 FormFieldData result1;
431 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result1);
433 FormFieldData expected;
434 expected.name = ASCIIToUTF16("element");
435 expected.max_length = 0;
436 expected.form_control_type = "select-one";
438 expected.value = ASCIIToUTF16("CA");
439 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
441 FormFieldData result2;
442 WebFormControlElementToFormField(
443 element,
444 static_cast<autofill::ExtractMask>(autofill::EXTRACT_VALUE |
445 autofill::EXTRACT_OPTION_TEXT),
446 &result2);
447 expected.value = ASCIIToUTF16("California");
448 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
450 FormFieldData result3;
451 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS,
452 &result3);
453 expected.value = base::string16();
454 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result3);
456 ASSERT_EQ(2U, result3.option_values.size());
457 ASSERT_EQ(2U, result3.option_contents.size());
458 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]);
459 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]);
460 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]);
461 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]);
464 // When faced with <select> field with *many* options, we should trim them to a
465 // reasonable number.
466 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldLongSelect) {
467 std::string html = "<SELECT id='element'/>";
468 for (size_t i = 0; i < 2 * kMaxListSize; ++i) {
469 html += base::StringPrintf("<OPTION value='%" PRIuS "'>"
470 "%" PRIuS "</OPTION>", i, i);
472 html += "</SELECT>";
473 LoadHTML(html.c_str());
475 WebFrame* frame = GetMainFrame();
476 ASSERT_TRUE(frame);
478 WebElement web_element = frame->document().getElementById("element");
479 WebFormControlElement element = web_element.to<WebFormControlElement>();
480 FormFieldData result;
481 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS, &result);
483 EXPECT_TRUE(result.option_values.empty());
484 EXPECT_TRUE(result.option_contents.empty());
487 // We should be able to extract a <textarea> field.
488 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) {
489 LoadHTML("<TEXTAREA id='element'>"
490 "This element's value&#10;"
491 "spans multiple lines."
492 "</TEXTAREA>");
494 WebFrame* frame = GetMainFrame();
495 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
497 WebElement web_element = frame->document().getElementById("element");
498 WebFormControlElement element = web_element.to<WebFormControlElement>();
499 FormFieldData result_sans_value;
500 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
501 &result_sans_value);
503 FormFieldData expected;
504 expected.name = ASCIIToUTF16("element");
505 expected.max_length = 0;
506 expected.form_control_type = "textarea";
507 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
509 FormFieldData result_with_value;
510 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
511 &result_with_value);
512 expected.value = ASCIIToUTF16("This element's value\n"
513 "spans multiple lines.");
514 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
517 // We should be able to extract an <input type="month"> field.
518 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMonthInput) {
519 LoadHTML("<INPUT type='month' id='element' value='2011-12'>");
521 WebFrame* frame = GetMainFrame();
522 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
524 WebElement web_element = frame->document().getElementById("element");
525 WebFormControlElement element = web_element.to<WebFormControlElement>();
526 FormFieldData result_sans_value;
527 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE,
528 &result_sans_value);
530 FormFieldData expected;
531 expected.name = ASCIIToUTF16("element");
532 expected.max_length = 0;
533 expected.form_control_type = "month";
534 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
536 FormFieldData result_with_value;
537 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE,
538 &result_with_value);
539 expected.value = ASCIIToUTF16("2011-12");
540 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
543 // We should not extract the value for non-text and non-select fields.
544 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) {
545 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
546 " <INPUT type='hidden' id='hidden' value='apple'/>"
547 " <INPUT type='submit' id='submit' value='Send'/>"
548 "</FORM>");
550 WebFrame* frame = GetMainFrame();
551 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
553 WebElement web_element = frame->document().getElementById("hidden");
554 WebFormControlElement element = web_element.to<WebFormControlElement>();
555 FormFieldData result;
556 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
558 FormFieldData expected;
559 expected.max_length = 0;
561 expected.name = ASCIIToUTF16("hidden");
562 expected.form_control_type = "hidden";
563 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
565 web_element = frame->document().getElementById("submit");
566 element = web_element.to<WebFormControlElement>();
567 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
568 expected.name = ASCIIToUTF16("submit");
569 expected.form_control_type = "submit";
570 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
573 // We should be able to extract password fields.
574 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) {
575 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
576 " <INPUT type='password' id='password' value='secret'/>"
577 "</FORM>");
579 WebFrame* frame = GetMainFrame();
580 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
582 WebElement web_element = frame->document().getElementById("password");
583 WebFormControlElement element = web_element.to<WebFormControlElement>();
584 FormFieldData result;
585 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
587 FormFieldData expected;
588 expected.max_length = WebInputElement::defaultMaxLength();
589 expected.name = ASCIIToUTF16("password");
590 expected.form_control_type = "password";
591 expected.value = ASCIIToUTF16("secret");
592 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
595 // We should be able to extract the autocompletetype attribute.
596 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) {
597 std::string html =
598 "<INPUT type='text' id='absent'/>"
599 "<INPUT type='text' id='empty' autocomplete=''/>"
600 "<INPUT type='text' id='off' autocomplete='off'/>"
601 "<INPUT type='text' id='regular' autocomplete='email'/>"
602 "<INPUT type='text' id='multi-valued' "
603 " autocomplete='billing email'/>"
604 "<INPUT type='text' id='experimental' x-autocompletetype='email'/>"
605 "<INPUT type='month' id='month' autocomplete='cc-exp'/>"
606 "<SELECT id='select' autocomplete='state'/>"
607 " <OPTION value='CA'>California</OPTION>"
608 " <OPTION value='TX'>Texas</OPTION>"
609 "</SELECT>"
610 "<TEXTAREA id='textarea' autocomplete='street-address'>"
611 " Some multi-"
612 " lined value"
613 "</TEXTAREA>";
614 html +=
615 "<INPUT type='text' id='malicious' autocomplete='" +
616 std::string(10000, 'x') + "'/>";
617 LoadHTML(html.c_str());
619 WebFrame* frame = GetMainFrame();
620 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
622 struct TestCase {
623 const std::string element_id;
624 const std::string form_control_type;
625 const std::string autocomplete_attribute;
627 TestCase test_cases[] = {
628 // An absent attribute is equivalent to an empty one.
629 { "absent", "text", "" },
630 // Make sure there are no issues parsing an empty attribute.
631 { "empty", "text", "" },
632 // Make sure there are no issues parsing an attribute value that isn't a
633 // type hint.
634 { "off", "text", "off" },
635 // Common case: exactly one type specified.
636 { "regular", "text", "email" },
637 // Verify that we correctly extract multiple tokens as well.
638 { "multi-valued", "text", "billing email" },
639 // Verify that <input type="month"> fields are supported.
640 { "month", "month", "cc-exp" },
641 // We previously extracted this data from the experimental
642 // 'x-autocompletetype' attribute. Now that the field type hints are part
643 // of the spec under the autocomplete attribute, we no longer support the
644 // experimental version.
645 { "experimental", "text", "" },
646 // <select> elements should behave no differently from text fields here.
647 { "select", "select-one", "state" },
648 // <textarea> elements should also behave no differently from text fields.
649 { "textarea", "textarea", "street-address" },
650 // Very long attribute values should be replaced by a default string, to
651 // prevent malicious websites from DOSing the browser process.
652 { "malicious", "text", "x-max-data-length-exceeded" },
655 for (size_t i = 0; i < arraysize(test_cases); ++i) {
656 WebElement web_element = frame->document().getElementById(
657 ASCIIToUTF16(test_cases[i].element_id));
658 WebFormControlElement element = web_element.to<WebFormControlElement>();
659 FormFieldData result;
660 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result);
662 FormFieldData expected;
663 expected.name = ASCIIToUTF16(test_cases[i].element_id);
664 expected.form_control_type = test_cases[i].form_control_type;
665 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute;
666 if (test_cases[i].form_control_type == "text")
667 expected.max_length = WebInputElement::defaultMaxLength();
668 else
669 expected.max_length = 0;
671 SCOPED_TRACE(test_cases[i].element_id);
672 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
676 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectStyle) {
677 LoadHTML("<STYLE>input{direction:rtl}</STYLE>"
678 "<FORM>"
679 " <INPUT type='text' id='element'>"
680 "</FORM>");
682 WebFrame* frame = GetMainFrame();
683 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
685 WebElement web_element = frame->document().getElementById("element");
686 WebFormControlElement element = web_element.to<WebFormControlElement>();
688 FormFieldData result;
689 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
690 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
693 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectDIRAttribute) {
694 LoadHTML("<FORM>"
695 " <INPUT dir='rtl' type='text' id='element'/>"
696 "</FORM>");
698 WebFrame* frame = GetMainFrame();
699 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
701 WebElement web_element = frame->document().getElementById("element");
702 WebFormControlElement element = web_element.to<WebFormControlElement>();
704 FormFieldData result;
705 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
706 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
709 TEST_F(FormAutofillTest, DetectTextDirectionFromParentStyle) {
710 LoadHTML("<STYLE>form{direction:rtl}</STYLE>"
711 "<FORM>"
712 " <INPUT type='text' id='element'/>"
713 "</FORM>");
715 WebFrame* frame = GetMainFrame();
716 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
718 WebElement web_element = frame->document().getElementById("element");
719 WebFormControlElement element = web_element.to<WebFormControlElement>();
721 FormFieldData result;
722 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
723 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
726 TEST_F(FormAutofillTest, DetectTextDirectionFromParentDIRAttribute) {
727 LoadHTML("<FORM dir='rtl'>"
728 " <INPUT type='text' id='element'/>"
729 "</FORM>");
731 WebFrame* frame = GetMainFrame();
732 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
734 WebElement web_element = frame->document().getElementById("element");
735 WebFormControlElement element = web_element.to<WebFormControlElement>();
737 FormFieldData result;
738 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
739 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
742 TEST_F(FormAutofillTest, DetectTextDirectionWhenStyleAndDIRAttributMixed) {
743 LoadHTML("<STYLE>input{direction:ltr}</STYLE>"
744 "<FORM dir='rtl'>"
745 " <INPUT type='text' id='element'/>"
746 "</FORM>");
748 WebFrame* frame = GetMainFrame();
749 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
751 WebElement web_element = frame->document().getElementById("element");
752 WebFormControlElement element = web_element.to<WebFormControlElement>();
754 FormFieldData result;
755 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
756 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
759 TEST_F(FormAutofillTest,
760 DetectTextDirectionWhenParentHasBothDIRAttributeAndStyle) {
761 LoadHTML("<STYLE>form{direction:ltr}</STYLE>"
762 "<FORM dir='rtl'>"
763 " <INPUT type='text' id='element'/>"
764 "</FORM>");
766 WebFrame* frame = GetMainFrame();
767 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
769 WebElement web_element = frame->document().getElementById("element");
770 WebFormControlElement element = web_element.to<WebFormControlElement>();
772 FormFieldData result;
773 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
774 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
777 TEST_F(FormAutofillTest, DetectTextDirectionWhenAncestorHasInlineStyle) {
778 LoadHTML("<FORM style='direction:ltr'>"
779 " <SPAN dir='rtl'>"
780 " <INPUT type='text' id='element'/>"
781 " </SPAN>"
782 "</FORM>");
784 WebFrame* frame = GetMainFrame();
785 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
787 WebElement web_element = frame->document().getElementById("element");
788 WebFormControlElement element = web_element.to<WebFormControlElement>();
790 FormFieldData result;
791 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result);
792 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
795 TEST_F(FormAutofillTest, WebFormElementToFormData) {
796 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
797 " <LABEL for='firstname'>First name:</LABEL>"
798 " <INPUT type='text' id='firstname' value='John'/>"
799 " <LABEL for='lastname'>Last name:</LABEL>"
800 " <INPUT type='text' id='lastname' value='Smith'/>"
801 " <LABEL for='street-address'>Address:</LABEL>"
802 " <TEXTAREA id='street-address'>"
803 "123 Fantasy Ln.&#10;"
804 "Apt. 42"
805 "</TEXTAREA>"
806 " <LABEL for='state'>State:</LABEL>"
807 " <SELECT id='state'/>"
808 " <OPTION value='CA'>California</OPTION>"
809 " <OPTION value='TX'>Texas</OPTION>"
810 " </SELECT>"
811 " <LABEL for='password'>Password:</LABEL>"
812 " <INPUT type='password' id='password' value='secret'/>"
813 " <LABEL for='month'>Card expiration:</LABEL>"
814 " <INPUT type='month' id='month' value='2011-12'/>"
815 " <INPUT type='submit' name='reply-send' value='Send'/>"
816 // The below inputs should be ignored
817 " <LABEL for='notvisible'>Hidden:</LABEL>"
818 " <INPUT type='hidden' id='notvisible' value='apple'/>"
819 "</FORM>");
821 WebFrame* frame = GetMainFrame();
822 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
824 WebVector<WebFormElement> forms;
825 frame->document().forms(forms);
826 ASSERT_EQ(1U, forms.size());
828 WebElement element = frame->document().getElementById("firstname");
829 WebInputElement input_element = element.to<WebInputElement>();
831 FormData form;
832 FormFieldData field;
833 EXPECT_TRUE(WebFormElementToFormData(forms[0],
834 input_element,
835 autofill::REQUIRE_NONE,
836 autofill::EXTRACT_VALUE,
837 &form,
838 &field));
839 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
840 EXPECT_EQ(GURL(frame->document().url()), form.origin);
841 EXPECT_EQ(GURL("http://cnn.com"), form.action);
843 const std::vector<FormFieldData>& fields = form.fields;
844 ASSERT_EQ(6U, fields.size());
846 FormFieldData expected;
847 expected.name = ASCIIToUTF16("firstname");
848 expected.value = ASCIIToUTF16("John");
849 expected.label = ASCIIToUTF16("First name:");
850 expected.form_control_type = "text";
851 expected.max_length = WebInputElement::defaultMaxLength();
852 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
854 expected.name = ASCIIToUTF16("lastname");
855 expected.value = ASCIIToUTF16("Smith");
856 expected.label = ASCIIToUTF16("Last name:");
857 expected.form_control_type = "text";
858 expected.max_length = WebInputElement::defaultMaxLength();
859 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
861 expected.name = ASCIIToUTF16("street-address");
862 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
863 expected.label = ASCIIToUTF16("Address:");
864 expected.form_control_type = "textarea";
865 expected.max_length = 0;
866 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
868 expected.name = ASCIIToUTF16("state");
869 expected.value = ASCIIToUTF16("CA");
870 expected.label = ASCIIToUTF16("State:");
871 expected.form_control_type = "select-one";
872 expected.max_length = 0;
873 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
875 expected.name = ASCIIToUTF16("password");
876 expected.value = ASCIIToUTF16("secret");
877 expected.label = ASCIIToUTF16("Password:");
878 expected.form_control_type = "password";
879 expected.max_length = WebInputElement::defaultMaxLength();
880 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
882 expected.name = ASCIIToUTF16("month");
883 expected.value = ASCIIToUTF16("2011-12");
884 expected.label = ASCIIToUTF16("Card expiration:");
885 expected.form_control_type = "month";
886 expected.max_length = 0;
887 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
890 // We should not be able to serialize a form with too many fillable fields.
891 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) {
892 std::string html =
893 "<FORM name='TestForm' action='http://cnn.com' method='post'>";
894 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) {
895 html += "<INPUT type='text'/>";
897 html += "</FORM>";
898 LoadHTML(html.c_str());
900 WebFrame* frame = GetMainFrame();
901 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
903 WebVector<WebFormElement> forms;
904 frame->document().forms(forms);
905 ASSERT_EQ(1U, forms.size());
907 WebElement element = frame->document().getElementById("firstname");
908 WebInputElement input_element = element.to<WebInputElement>();
910 FormData form;
911 FormFieldData field;
912 EXPECT_FALSE(WebFormElementToFormData(forms[0],
913 input_element,
914 autofill::REQUIRE_NONE,
915 autofill::EXTRACT_VALUE,
916 &form,
917 &field));
920 TEST_F(FormAutofillTest, ExtractForms) {
921 ExpectJohnSmithLabels(
922 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
923 " First name: <INPUT type='text' id='firstname' value='John'/>"
924 " Last name: <INPUT type='text' id='lastname' value='Smith'/>"
925 " Email: <INPUT type='text' id='email' value='john@example.com'/>"
926 " <INPUT type='submit' name='reply-send' value='Send'/>"
927 "</FORM>");
930 TEST_F(FormAutofillTest, ExtractMultipleForms) {
931 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
932 " <INPUT type='text' id='firstname' value='John'/>"
933 " <INPUT type='text' id='lastname' value='Smith'/>"
934 " <INPUT type='text' id='email' value='john@example.com'/>"
935 " <INPUT type='submit' name='reply-send' value='Send'/>"
936 "</FORM>"
937 "<FORM name='TestForm2' action='http://zoo.com' method='post'>"
938 " <INPUT type='text' id='firstname' value='Jack'/>"
939 " <INPUT type='text' id='lastname' value='Adams'/>"
940 " <INPUT type='text' id='email' value='jack@example.com'/>"
941 " <INPUT type='submit' name='reply-send' value='Send'/>"
942 "</FORM>");
944 WebFrame* web_frame = GetMainFrame();
945 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
947 FormCache form_cache;
948 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
949 ASSERT_EQ(2U, forms.size());
951 // First form.
952 const FormData& form = forms[0];
953 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
954 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
955 EXPECT_EQ(GURL("http://cnn.com"), form.action);
957 const std::vector<FormFieldData>& fields = form.fields;
958 ASSERT_EQ(3U, fields.size());
960 FormFieldData expected;
961 expected.form_control_type = "text";
962 expected.max_length = WebInputElement::defaultMaxLength();
964 expected.name = ASCIIToUTF16("firstname");
965 expected.value = ASCIIToUTF16("John");
966 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
968 expected.name = ASCIIToUTF16("lastname");
969 expected.value = ASCIIToUTF16("Smith");
970 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
972 expected.name = ASCIIToUTF16("email");
973 expected.value = ASCIIToUTF16("john@example.com");
974 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
976 // Second form.
977 const FormData& form2 = forms[1];
978 EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2.name);
979 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
980 EXPECT_EQ(GURL("http://zoo.com"), form2.action);
982 const std::vector<FormFieldData>& fields2 = form2.fields;
983 ASSERT_EQ(3U, fields2.size());
985 expected.name = ASCIIToUTF16("firstname");
986 expected.value = ASCIIToUTF16("Jack");
987 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
989 expected.name = ASCIIToUTF16("lastname");
990 expected.value = ASCIIToUTF16("Adams");
991 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
993 expected.name = ASCIIToUTF16("email");
994 expected.value = ASCIIToUTF16("jack@example.com");
995 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
998 TEST_F(FormAutofillTest, OnlyExtractNewForms) {
999 LoadHTML(
1000 "<FORM id='testform' action='http://cnn.com' method='post'>"
1001 " <INPUT type='text' id='firstname' value='John'/>"
1002 " <INPUT type='text' id='lastname' value='Smith'/>"
1003 " <INPUT type='text' id='email' value='john@example.com'/>"
1004 " <INPUT type='submit' name='reply-send' value='Send'/>"
1005 "</FORM>");
1007 WebFrame* web_frame = GetMainFrame();
1008 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1010 FormCache form_cache;
1011 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1012 ASSERT_EQ(1U, forms.size());
1014 // Second call should give nothing as there are no new forms.
1015 forms = form_cache.ExtractNewForms(*web_frame);
1016 ASSERT_TRUE(forms.empty());
1018 // Append to the current form will re-extract.
1019 ExecuteJavaScript(
1020 "var newInput = document.createElement('input');"
1021 "newInput.setAttribute('type', 'text');"
1022 "newInput.setAttribute('id', 'telephone');"
1023 "newInput.value = '12345';"
1024 "document.getElementById('testform').appendChild(newInput);");
1025 msg_loop_.RunUntilIdle();
1027 forms = form_cache.ExtractNewForms(*web_frame);
1028 ASSERT_EQ(1U, forms.size());
1030 const std::vector<FormFieldData>& fields = forms[0].fields;
1031 ASSERT_EQ(4U, fields.size());
1033 FormFieldData expected;
1034 expected.form_control_type = "text";
1035 expected.max_length = WebInputElement::defaultMaxLength();
1037 expected.name = ASCIIToUTF16("firstname");
1038 expected.value = ASCIIToUTF16("John");
1039 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1041 expected.name = ASCIIToUTF16("lastname");
1042 expected.value = ASCIIToUTF16("Smith");
1043 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1045 expected.name = ASCIIToUTF16("email");
1046 expected.value = ASCIIToUTF16("john@example.com");
1047 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1049 expected.name = ASCIIToUTF16("telephone");
1050 expected.value = ASCIIToUTF16("12345");
1051 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1053 forms.clear();
1055 // Completely new form will also be extracted.
1056 ExecuteJavaScript(
1057 "var newForm=document.createElement('form');"
1058 "newForm.id='new_testform';"
1059 "newForm.action='http://google.com';"
1060 "newForm.method='post';"
1061 "var newFirstname=document.createElement('input');"
1062 "newFirstname.setAttribute('type', 'text');"
1063 "newFirstname.setAttribute('id', 'second_firstname');"
1064 "newFirstname.value = 'Bob';"
1065 "var newLastname=document.createElement('input');"
1066 "newLastname.setAttribute('type', 'text');"
1067 "newLastname.setAttribute('id', 'second_lastname');"
1068 "newLastname.value = 'Hope';"
1069 "var newEmail=document.createElement('input');"
1070 "newEmail.setAttribute('type', 'text');"
1071 "newEmail.setAttribute('id', 'second_email');"
1072 "newEmail.value = 'bobhope@example.com';"
1073 "newForm.appendChild(newFirstname);"
1074 "newForm.appendChild(newLastname);"
1075 "newForm.appendChild(newEmail);"
1076 "document.body.appendChild(newForm);");
1077 msg_loop_.RunUntilIdle();
1079 web_frame = GetMainFrame();
1080 forms = form_cache.ExtractNewForms(*web_frame);
1081 ASSERT_EQ(1U, forms.size());
1083 const std::vector<FormFieldData>& fields2 = forms[0].fields;
1084 ASSERT_EQ(3U, fields2.size());
1086 expected.name = ASCIIToUTF16("second_firstname");
1087 expected.value = ASCIIToUTF16("Bob");
1088 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1090 expected.name = ASCIIToUTF16("second_lastname");
1091 expected.value = ASCIIToUTF16("Hope");
1092 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1094 expected.name = ASCIIToUTF16("second_email");
1095 expected.value = ASCIIToUTF16("bobhope@example.com");
1096 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1099 // We should not extract a form if it has too few fillable fields.
1100 TEST_F(FormAutofillTest, ExtractFormsTooFewFields) {
1101 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1102 " <INPUT type='text' id='firstname' value='John'/>"
1103 " <INPUT type='text' id='lastname' value='Smith'/>"
1104 " <INPUT type='submit' name='reply-send' value='Send'/>"
1105 "</FORM>");
1107 WebFrame* web_frame = GetMainFrame();
1108 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1110 FormCache form_cache;
1111 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1112 ASSERT_TRUE(forms.empty());
1115 // We should not report additional forms for empty forms.
1116 TEST_F(FormAutofillTest, ExtractFormsSkippedForms) {
1117 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1118 " <INPUT type='text' id='firstname' value='John'/>"
1119 " <INPUT type='text' id='lastname' value='Smith'/>"
1120 "</FORM>");
1122 WebFrame* web_frame = GetMainFrame();
1123 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1125 FormCache form_cache;
1126 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1127 ASSERT_TRUE(forms.empty());
1130 // We should not report additional forms for empty forms.
1131 TEST_F(FormAutofillTest, ExtractFormsNoFields) {
1132 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1133 "</FORM>");
1135 WebFrame* web_frame = GetMainFrame();
1136 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1138 FormCache form_cache;
1139 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1140 ASSERT_TRUE(forms.empty());
1143 // We should not extract a form if it has too few fillable fields.
1144 // Make sure radio and checkbox fields don't count.
1145 TEST_F(FormAutofillTest, ExtractFormsTooFewFieldsSkipsCheckable) {
1146 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1147 " <INPUT type='text' id='firstname' value='John'/>"
1148 " <INPUT type='text' id='lastname' value='Smith'/>"
1149 " <INPUT type='radio' id='a_radio' value='0'/>"
1150 " <INPUT type='checkbox' id='a_check' value='1'/>"
1151 " <INPUT type='submit' name='reply-send' value='Send'/>"
1152 "</FORM>");
1154 WebFrame* web_frame = GetMainFrame();
1155 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1157 FormCache form_cache;
1158 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1159 ASSERT_TRUE(forms.empty());
1162 TEST_F(FormAutofillTest, WebFormElementToFormDataAutocomplete) {
1164 // Form is not auto-completable due to autocomplete=off.
1165 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'"
1166 " autocomplete=off>"
1167 " <INPUT type='text' id='firstname' value='John'/>"
1168 " <INPUT type='text' id='lastname' value='Smith'/>"
1169 " <INPUT type='text' id='email' value='john@example.com'/>"
1170 " <INPUT type='submit' name='reply-send' value='Send'/>"
1171 "</FORM>");
1173 WebFrame* web_frame = GetMainFrame();
1174 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1176 WebVector<WebFormElement> web_forms;
1177 web_frame->document().forms(web_forms);
1178 ASSERT_EQ(1U, web_forms.size());
1179 WebFormElement web_form = web_forms[0];
1181 FormData form;
1182 EXPECT_TRUE(WebFormElementToFormData(
1183 web_form, WebFormControlElement(), autofill::REQUIRE_NONE,
1184 autofill::EXTRACT_NONE, &form, NULL));
1185 EXPECT_FALSE(WebFormElementToFormData(
1186 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
1187 autofill::EXTRACT_NONE, &form, NULL));
1191 // The firstname element is not auto-completable due to autocomplete=off.
1192 LoadHTML("<FORM name='TestForm' action='http://abc.com' "
1193 " method='post'>"
1194 " <INPUT type='text' id='firstname' value='John'"
1195 " autocomplete=off>"
1196 " <INPUT type='text' id='middlename' value='Jack'/>"
1197 " <INPUT type='text' id='lastname' value='Smith'/>"
1198 " <INPUT type='text' id='email' value='john@example.com'/>"
1199 " <INPUT type='submit' name='reply' value='Send'/>"
1200 "</FORM>");
1202 WebFrame* web_frame = GetMainFrame();
1203 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1205 WebVector<WebFormElement> web_forms;
1206 web_frame->document().forms(web_forms);
1207 ASSERT_EQ(1U, web_forms.size());
1208 WebFormElement web_form = web_forms[0];
1210 FormData form;
1211 EXPECT_TRUE(WebFormElementToFormData(
1212 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE,
1213 autofill::EXTRACT_VALUE, &form, NULL));
1215 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1216 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1217 EXPECT_EQ(GURL("http://abc.com"), form.action);
1219 const std::vector<FormFieldData>& fields = form.fields;
1220 ASSERT_EQ(3U, fields.size());
1222 FormFieldData expected;
1223 expected.form_control_type = "text";
1224 expected.max_length = WebInputElement::defaultMaxLength();
1226 expected.name = ASCIIToUTF16("middlename");
1227 expected.value = ASCIIToUTF16("Jack");
1228 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1230 expected.name = ASCIIToUTF16("lastname");
1231 expected.value = ASCIIToUTF16("Smith");
1232 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1234 expected.name = ASCIIToUTF16("email");
1235 expected.value = ASCIIToUTF16("john@example.com");
1236 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1240 TEST_F(FormAutofillTest, FindFormForInputElement) {
1241 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
1242 " <INPUT type='text' id='firstname' value='John'/>"
1243 " <INPUT type='text' id='lastname' value='Smith'/>"
1244 " <INPUT type='text' id='email' value='john@example.com'"
1245 "autocomplete='off' />"
1246 " <INPUT type='text' id='phone' value='1.800.555.1234'/>"
1247 " <INPUT type='submit' name='reply-send' value='Send'/>"
1248 "</FORM>");
1250 WebFrame* web_frame = GetMainFrame();
1251 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1253 FormCache form_cache;
1254 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1255 ASSERT_EQ(1U, forms.size());
1257 // Get the input element we want to find.
1258 WebElement element = web_frame->document().getElementById("firstname");
1259 WebInputElement input_element = element.to<WebInputElement>();
1261 // Find the form and verify it's the correct form.
1262 FormData form;
1263 FormFieldData field;
1264 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
1265 &form,
1266 &field,
1267 autofill::REQUIRE_NONE));
1268 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1269 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1270 EXPECT_EQ(GURL("http://buh.com"), form.action);
1272 const std::vector<FormFieldData>& fields = form.fields;
1273 ASSERT_EQ(4U, fields.size());
1275 FormFieldData expected;
1276 expected.form_control_type = "text";
1277 expected.max_length = WebInputElement::defaultMaxLength();
1279 expected.name = ASCIIToUTF16("firstname");
1280 expected.value = ASCIIToUTF16("John");
1281 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1282 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1284 expected.name = ASCIIToUTF16("lastname");
1285 expected.value = ASCIIToUTF16("Smith");
1286 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1288 expected.name = ASCIIToUTF16("email");
1289 expected.value = ASCIIToUTF16("john@example.com");
1290 expected.autocomplete_attribute = "off";
1291 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1292 expected.autocomplete_attribute = std::string(); // reset
1294 expected.name = ASCIIToUTF16("phone");
1295 expected.value = ASCIIToUTF16("1.800.555.1234");
1296 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1298 // Try again, but require autocomplete.
1299 FormData form2;
1300 FormFieldData field2;
1301 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1302 input_element,
1303 &form2,
1304 &field2,
1305 autofill::REQUIRE_AUTOCOMPLETE));
1306 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1307 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1308 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1310 const std::vector<FormFieldData>& fields2 = form2.fields;
1311 ASSERT_EQ(3U, fields2.size());
1313 expected.form_control_type = "text";
1314 expected.max_length = WebInputElement::defaultMaxLength();
1316 expected.name = ASCIIToUTF16("firstname");
1317 expected.value = ASCIIToUTF16("John");
1318 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1319 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1321 expected.name = ASCIIToUTF16("lastname");
1322 expected.value = ASCIIToUTF16("Smith");
1323 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1325 expected.name = ASCIIToUTF16("phone");
1326 expected.value = ASCIIToUTF16("1.800.555.1234");
1327 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1330 TEST_F(FormAutofillTest, FindFormForTextAreaElement) {
1331 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
1332 " <INPUT type='text' id='firstname' value='John'/>"
1333 " <INPUT type='text' id='lastname' value='Smith'/>"
1334 " <INPUT type='text' id='email' value='john@example.com'"
1335 "autocomplete='off' />"
1336 " <TEXTAREA id='street-address'>"
1337 "123 Fantasy Ln.&#10;"
1338 "Apt. 42"
1339 "</TEXTAREA>"
1340 " <INPUT type='submit' name='reply-send' value='Send'/>"
1341 "</FORM>");
1343 WebFrame* web_frame = GetMainFrame();
1344 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
1346 FormCache form_cache;
1347 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1348 ASSERT_EQ(1U, forms.size());
1350 // Get the textarea element we want to find.
1351 WebElement element = web_frame->document().getElementById("street-address");
1352 WebTextAreaElement textarea_element = element.to<WebTextAreaElement>();
1354 // Find the form and verify it's the correct form.
1355 FormData form;
1356 FormFieldData field;
1357 EXPECT_TRUE(FindFormAndFieldForFormControlElement(textarea_element,
1358 &form,
1359 &field,
1360 autofill::REQUIRE_NONE));
1361 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1362 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1363 EXPECT_EQ(GURL("http://buh.com"), form.action);
1365 const std::vector<FormFieldData>& fields = form.fields;
1366 ASSERT_EQ(4U, fields.size());
1368 FormFieldData expected;
1370 expected.name = ASCIIToUTF16("firstname");
1371 expected.value = ASCIIToUTF16("John");
1372 expected.form_control_type = "text";
1373 expected.max_length = WebInputElement::defaultMaxLength();
1374 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1376 expected.name = ASCIIToUTF16("lastname");
1377 expected.value = ASCIIToUTF16("Smith");
1378 expected.form_control_type = "text";
1379 expected.max_length = WebInputElement::defaultMaxLength();
1380 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1382 expected.name = ASCIIToUTF16("email");
1383 expected.value = ASCIIToUTF16("john@example.com");
1384 expected.autocomplete_attribute = "off";
1385 expected.form_control_type = "text";
1386 expected.max_length = WebInputElement::defaultMaxLength();
1387 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1388 expected.autocomplete_attribute = std::string(); // reset
1390 expected.name = ASCIIToUTF16("street-address");
1391 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1392 expected.form_control_type = "textarea";
1393 expected.max_length = 0;
1394 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1395 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1397 // Try again, but require autocomplete.
1398 FormData form2;
1399 FormFieldData field2;
1400 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1401 textarea_element,
1402 &form2,
1403 &field2,
1404 autofill::REQUIRE_AUTOCOMPLETE));
1405 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1406 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1407 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1409 const std::vector<FormFieldData>& fields2 = form2.fields;
1410 ASSERT_EQ(3U, fields2.size());
1412 expected.name = ASCIIToUTF16("firstname");
1413 expected.value = ASCIIToUTF16("John");
1414 expected.form_control_type = "text";
1415 expected.max_length = WebInputElement::defaultMaxLength();
1416 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1418 expected.name = ASCIIToUTF16("lastname");
1419 expected.value = ASCIIToUTF16("Smith");
1420 expected.form_control_type = "text";
1421 expected.max_length = WebInputElement::defaultMaxLength();
1422 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1424 expected.name = ASCIIToUTF16("street-address");
1425 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1426 expected.form_control_type = "textarea";
1427 expected.max_length = 0;
1428 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1429 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1432 // Test regular FillForm function.
1433 TEST_F(FormAutofillTest, FillForm) {
1434 static const AutofillFieldCase field_cases[] = {
1435 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1436 // should_be_autofilled, autofill_value, expected_value
1438 // Regular empty fields (firstname & lastname) should be autofilled.
1439 {"text",
1440 "firstname",
1443 true,
1444 "filled firstname",
1445 "filled firstname"},
1446 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1447 // hidden fields should not be extracted to form_data.
1448 // Non empty fields should not be autofilled.
1449 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
1450 {"text",
1451 "noautocomplete",
1453 "off",
1454 true,
1455 "filled noautocomplete",
1456 "filled noautocomplete"},
1457 // Disabled fields should not be autofilled.
1458 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1459 // Readonly fields should not be autofilled.
1460 {"text", "readonly", "", "", false, "filled readonly", ""},
1461 // Fields with "visibility: hidden" should not be autofilled.
1462 {"text", "invisible", "", "", false, "filled invisible", ""},
1463 // Fields with "display:none" should not be autofilled.
1464 {"text", "displaynone", "", "", false, "filled displaynone", ""},
1465 // Regular <input type="month"> should be autofilled.
1466 {"month", "month", "", "", true, "2017-11", "2017-11"},
1467 // Non-empty <input type="month"> should not be autofilled.
1468 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
1469 // Regular select fields should be autofilled.
1470 {"select-one", "select", "", "", true, "TX", "TX"},
1471 // Select fields should be autofilled even if they already have a
1472 // non-empty value.
1473 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1474 // Select fields should not be autofilled if no new value is passed from
1475 // autofill profile. The existing value should not be overriden.
1476 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
1477 // Regular textarea elements should be autofilled.
1478 {"textarea",
1479 "textarea",
1482 true,
1483 "some multi-\nline value",
1484 "some multi-\nline value"},
1485 // Non-empty textarea elements should not be autofilled.
1486 {"textarea",
1487 "textarea-nonempty",
1488 "Go\naway!",
1490 false,
1491 "some multi-\nline value",
1492 "Go\naway!"},
1494 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1495 FillForm, &GetValueWrapper);
1496 // Verify preview selection.
1497 WebInputElement firstname = GetMainFrame()->document().
1498 getElementById("firstname").to<WebInputElement>();
1499 EXPECT_EQ(16, firstname.selectionStart());
1500 EXPECT_EQ(16, firstname.selectionEnd());
1503 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) {
1504 static const AutofillFieldCase field_cases[] = {
1505 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1506 // should_be_autofilled, autofill_value, expected_value
1508 // Regular empty fields (firstname & lastname) should be autofilled.
1509 {"text",
1510 "firstname",
1513 true,
1514 "filled firstname",
1515 "filled firstname"},
1516 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1517 // hidden fields should not be extracted to form_data.
1518 // Non empty fields should be overriden.
1519 {"text",
1520 "notempty",
1521 "Hi",
1523 true,
1524 "filled notempty",
1525 "filled notempty"},
1526 {"text",
1527 "noautocomplete",
1529 "off",
1530 true,
1531 "filled noautocomplete",
1532 "filled noautocomplete"},
1533 // Disabled fields should not be autofilled.
1534 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1535 // Readonly fields should not be autofilled.
1536 {"text", "readonly", "", "", false, "filled readonly", ""},
1537 // Fields with "visibility: hidden" should also be autofilled.
1538 {"text",
1539 "invisible",
1542 true,
1543 "filled invisible",
1544 "filled invisible"},
1545 // Fields with "display:none" should also be autofilled.
1546 {"text",
1547 "displaynone",
1550 true,
1551 "filled displaynone",
1552 "filled displaynone"},
1553 // Regular <input type="month"> should be autofilled.
1554 {"month", "month", "", "", true, "2017-11", "2017-11"},
1555 // Non-empty <input type="month"> should be overridden.
1556 {"month", "month-nonempty", "2011-12", "", true, "2017-11", "2017-11"},
1557 // Regular select fields should be autofilled.
1558 {"select-one", "select", "", "", true, "TX", "TX"},
1559 // Select fields should be autofilled even if they already have a
1560 // non-empty value.
1561 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1562 // Select fields should not be autofilled if no new value is passed from
1563 // autofill profile. The existing value should not be overriden.
1564 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
1565 // Regular textarea elements should be autofilled.
1566 {"textarea",
1567 "textarea",
1570 true,
1571 "some multi-\nline value",
1572 "some multi-\nline value"},
1573 // Nonempty textarea elements should be overridden.
1574 {"textarea",
1575 "textarea-nonempty",
1576 "Go\naway!",
1578 true,
1579 "some multi-\nline value",
1580 "some multi-\nline value"},
1582 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1583 &FillFormIncludingNonFocusableElementsWrapper,
1584 &GetValueWrapper);
1587 TEST_F(FormAutofillTest, PreviewForm) {
1588 static const AutofillFieldCase field_cases[] = {
1589 // Normal empty fields should be previewed.
1590 {"text",
1591 "firstname",
1594 true,
1595 "suggested firstname",
1596 "suggested firstname"},
1597 {"text",
1598 "lastname",
1601 true,
1602 "suggested lastname",
1603 "suggested lastname"},
1604 // Hidden fields should not be extracted to form_data.
1605 // Non empty fields should not be previewed.
1606 {"text", "notempty", "Hi", "", false, "suggested notempty", ""},
1607 {"text",
1608 "noautocomplete",
1610 "off",
1611 true,
1612 "filled noautocomplete",
1613 "filled noautocomplete"},
1614 // Disabled fields should not be previewed.
1615 {"text", "notenabled", "", "", false, "suggested notenabled", ""},
1616 // Readonly fields should not be previewed.
1617 {"text", "readonly", "", "", false, "suggested readonly", ""},
1618 // Fields with "visibility: hidden" should not be previewed.
1619 {"text", "invisible", "", "", false, "suggested invisible", ""},
1620 // Fields with "display:none" should not previewed.
1621 {"text", "displaynone", "", "", false, "suggested displaynone", ""},
1622 // Regular <input type="month"> should be previewed.
1623 {"month", "month", "", "", true, "2017-11", "2017-11"},
1624 // Non-empty <input type="month"> should not be previewed.
1625 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
1626 // Regular select fields should be previewed.
1627 {"select-one", "select", "", "", true, "TX", "TX"},
1628 // Select fields should be previewed even if they already have a
1629 // non-empty value.
1630 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1631 // Select fields should not be previewed if no suggestion is passed from
1632 // autofill profile.
1633 {"select-one", "select-unchanged", "CA", "", false, "", ""},
1634 // Normal textarea elements should be previewed.
1635 {"textarea",
1636 "textarea",
1639 true,
1640 "suggested multi-\nline value",
1641 "suggested multi-\nline value"},
1642 // Nonempty textarea elements should not be previewed.
1643 {"textarea",
1644 "textarea-nonempty",
1645 "Go\naway!",
1647 false,
1648 "suggested multi-\nline value",
1649 ""},
1651 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1652 &PreviewForm, &GetSuggestedValueWrapper);
1654 // Verify preview selection.
1655 WebInputElement firstname = GetMainFrame()->document().
1656 getElementById("firstname").to<WebInputElement>();
1657 EXPECT_EQ(0, firstname.selectionStart());
1658 EXPECT_EQ(19, firstname.selectionEnd());
1661 TEST_F(FormAutofillTest, Labels) {
1662 ExpectJohnSmithLabels(
1663 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1664 " <LABEL for='firstname'> First name: </LABEL>"
1665 " <INPUT type='text' id='firstname' value='John'/>"
1666 " <LABEL for='lastname'> Last name: </LABEL>"
1667 " <INPUT type='text' id='lastname' value='Smith'/>"
1668 " <LABEL for='email'> Email: </LABEL>"
1669 " <INPUT type='text' id='email' value='john@example.com'/>"
1670 " <INPUT type='submit' name='reply-send' value='Send'/>"
1671 "</FORM>");
1674 TEST_F(FormAutofillTest, LabelsWithSpans) {
1675 ExpectJohnSmithLabels(
1676 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1677 " <LABEL for='firstname'><span>First name: </span></LABEL>"
1678 " <INPUT type='text' id='firstname' value='John'/>"
1679 " <LABEL for='lastname'><span>Last name: </span></LABEL>"
1680 " <INPUT type='text' id='lastname' value='Smith'/>"
1681 " <LABEL for='email'><span>Email: </span></LABEL>"
1682 " <INPUT type='text' id='email' value='john@example.com'/>"
1683 " <INPUT type='submit' name='reply-send' value='Send'/>"
1684 "</FORM>");
1687 // This test is different from FormAutofillTest.Labels in that the label
1688 // elements for= attribute is set to the name of the form control element it is
1689 // a label for instead of the id of the form control element. This is invalid
1690 // because the for= attribute must be set to the id of the form control element;
1691 // however, current label parsing code will extract the text from the previous
1692 // label element and apply it to the following input field.
1693 TEST_F(FormAutofillTest, InvalidLabels) {
1694 ExpectJohnSmithLabels(
1695 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1696 " <LABEL for='firstname'> First name: </LABEL>"
1697 " <INPUT type='text' name='firstname' value='John'/>"
1698 " <LABEL for='lastname'> Last name: </LABEL>"
1699 " <INPUT type='text' name='lastname' value='Smith'/>"
1700 " <LABEL for='email'> Email: </LABEL>"
1701 " <INPUT type='text' name='email' value='john@example.com'/>"
1702 " <INPUT type='submit' name='reply-send' value='Send'/>"
1703 "</FORM>");
1706 // This test has three form control elements, only one of which has a label
1707 // element associated with it.
1708 TEST_F(FormAutofillTest, OneLabelElement) {
1709 ExpectJohnSmithLabels(
1710 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1711 " First name:"
1712 " <INPUT type='text' id='firstname' value='John'/>"
1713 " <LABEL for='lastname'>Last name: </LABEL>"
1714 " <INPUT type='text' id='lastname' value='Smith'/>"
1715 " Email:"
1716 " <INPUT type='text' id='email' value='john@example.com'/>"
1717 " <INPUT type='submit' name='reply-send' value='Send'/>"
1718 "</FORM>");
1721 TEST_F(FormAutofillTest, LabelsInferredFromText) {
1722 ExpectJohnSmithLabels(
1723 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1724 " First name:"
1725 " <INPUT type='text' id='firstname' value='John'/>"
1726 " Last name:"
1727 " <INPUT type='text' id='lastname' value='Smith'/>"
1728 " Email:"
1729 " <INPUT type='text' id='email' value='john@example.com'/>"
1730 " <INPUT type='submit' name='reply-send' value='Send'/>"
1731 "</FORM>");
1734 TEST_F(FormAutofillTest, LabelsInferredFromParagraph) {
1735 ExpectJohnSmithLabels(
1736 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1737 " <P>First name:</P><INPUT type='text' "
1738 " id='firstname' value='John'/>"
1739 " <P>Last name:</P>"
1740 " <INPUT type='text' id='lastname' value='Smith'/>"
1741 " <P>Email:</P>"
1742 " <INPUT type='text' id='email' value='john@example.com'/>"
1743 " <INPUT type='submit' name='reply-send' value='Send'/>"
1744 "</FORM>");
1747 TEST_F(FormAutofillTest, LabelsInferredFromBold) {
1748 ExpectJohnSmithLabels(
1749 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1750 " <B>First name:</B><INPUT type='text' "
1751 " id='firstname' value='John'/>"
1752 " <B>Last name:</B>"
1753 " <INPUT type='text' id='lastname' value='Smith'/>"
1754 " <B>Email:</B>"
1755 " <INPUT type='text' id='email' value='john@example.com'/>"
1756 " <INPUT type='submit' name='reply-send' value='Send'/>"
1757 "</FORM>");
1760 TEST_F(FormAutofillTest, LabelsInferredPriorToImgOrBr) {
1761 ExpectJohnSmithLabels(
1762 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1763 " First name:<IMG/><INPUT type='text' "
1764 " id='firstname' value='John'/>"
1765 " Last name:<IMG/>"
1766 " <INPUT type='text' id='lastname' value='Smith'/>"
1767 " Email:<BR/>"
1768 " <INPUT type='text' id='email' value='john@example.com'/>"
1769 " <INPUT type='submit' name='reply-send' value='Send'/>"
1770 "</FORM>");
1773 TEST_F(FormAutofillTest, LabelsInferredFromTableCell) {
1774 ExpectJohnSmithLabels(
1775 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1776 "<TABLE>"
1777 " <TR>"
1778 " <TD>First name:</TD>"
1779 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
1780 " </TR>"
1781 " <TR>"
1782 " <TD>Last name:</TD>"
1783 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
1784 " </TR>"
1785 " <TR>"
1786 " <TD>Email:</TD>"
1787 " <TD><INPUT type='text' id='email'"
1788 " value='john@example.com'/></TD>"
1789 " </TR>"
1790 " <TR>"
1791 " <TD></TD>"
1792 " <TD>"
1793 " <INPUT type='submit' name='reply-send' value='Send'/>"
1794 " </TD>"
1795 " </TR>"
1796 "</TABLE>"
1797 "</FORM>");
1800 TEST_F(FormAutofillTest, LabelsInferredFromTableCellTH) {
1801 ExpectJohnSmithLabels(
1802 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1803 "<TABLE>"
1804 " <TR>"
1805 " <TH>First name:</TH>"
1806 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
1807 " </TR>"
1808 " <TR>"
1809 " <TH>Last name:</TH>"
1810 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
1811 " </TR>"
1812 " <TR>"
1813 " <TH>Email:</TH>"
1814 " <TD><INPUT type='text' id='email'"
1815 " value='john@example.com'/></TD>"
1816 " </TR>"
1817 " <TR>"
1818 " <TD></TD>"
1819 " <TD>"
1820 " <INPUT type='submit' name='reply-send' value='Send'/>"
1821 " </TD>"
1822 " </TR>"
1823 "</TABLE>"
1824 "</FORM>");
1827 TEST_F(FormAutofillTest, LabelsInferredFromTableCellNested) {
1828 std::vector<base::string16> labels, names, values;
1830 labels.push_back(ASCIIToUTF16("First name: Bogus"));
1831 names.push_back(ASCIIToUTF16("firstname"));
1832 values.push_back(ASCIIToUTF16("John"));
1834 labels.push_back(ASCIIToUTF16("Last name:"));
1835 names.push_back(ASCIIToUTF16("lastname"));
1836 values.push_back(ASCIIToUTF16("Smith"));
1838 labels.push_back(ASCIIToUTF16("Email:"));
1839 names.push_back(ASCIIToUTF16("email"));
1840 values.push_back(ASCIIToUTF16("john@example.com"));
1842 ExpectLabels(
1843 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1844 "<TABLE>"
1845 " <TR>"
1846 " <TD>"
1847 " <FONT>"
1848 " First name:"
1849 " </FONT>"
1850 " <FONT>"
1851 " Bogus"
1852 " </FONT>"
1853 " </TD>"
1854 " <TD>"
1855 " <FONT>"
1856 " <INPUT type='text' id='firstname' value='John'/>"
1857 " </FONT>"
1858 " </TD>"
1859 " </TR>"
1860 " <TR>"
1861 " <TD>"
1862 " <FONT>"
1863 " Last name:"
1864 " </FONT>"
1865 " </TD>"
1866 " <TD>"
1867 " <FONT>"
1868 " <INPUT type='text' id='lastname' value='Smith'/>"
1869 " </FONT>"
1870 " </TD>"
1871 " </TR>"
1872 " <TR>"
1873 " <TD>"
1874 " <FONT>"
1875 " Email:"
1876 " </FONT>"
1877 " </TD>"
1878 " <TD>"
1879 " <FONT>"
1880 " <INPUT type='text' id='email' value='john@example.com'/>"
1881 " </FONT>"
1882 " </TD>"
1883 " </TR>"
1884 " <TR>"
1885 " <TD></TD>"
1886 " <TD>"
1887 " <INPUT type='submit' name='reply-send' value='Send'/>"
1888 " </TD>"
1889 " </TR>"
1890 "</TABLE>"
1891 "</FORM>",
1892 labels, names, values);
1895 TEST_F(FormAutofillTest, LabelsInferredFromTableEmptyTDs) {
1896 std::vector<base::string16> labels, names, values;
1898 labels.push_back(ASCIIToUTF16("* First Name"));
1899 names.push_back(ASCIIToUTF16("firstname"));
1900 values.push_back(ASCIIToUTF16("John"));
1902 labels.push_back(ASCIIToUTF16("* Last Name"));
1903 names.push_back(ASCIIToUTF16("lastname"));
1904 values.push_back(ASCIIToUTF16("Smith"));
1906 labels.push_back(ASCIIToUTF16("* Email"));
1907 names.push_back(ASCIIToUTF16("email"));
1908 values.push_back(ASCIIToUTF16("john@example.com"));
1910 ExpectLabels(
1911 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1912 "<TABLE>"
1913 " <TR>"
1914 " <TD>"
1915 " <SPAN>*</SPAN>"
1916 " <B>First Name</B>"
1917 " </TD>"
1918 " <TD></TD>"
1919 " <TD>"
1920 " <INPUT type='text' id='firstname' value='John'/>"
1921 " </TD>"
1922 " </TR>"
1923 " <TR>"
1924 " <TD>"
1925 " <SPAN>*</SPAN>"
1926 " <B>Last Name</B>"
1927 " </TD>"
1928 " <TD></TD>"
1929 " <TD>"
1930 " <INPUT type='text' id='lastname' value='Smith'/>"
1931 " </TD>"
1932 " </TR>"
1933 " <TR>"
1934 " <TD>"
1935 " <SPAN>*</SPAN>"
1936 " <B>Email</B>"
1937 " </TD>"
1938 " <TD></TD>"
1939 " <TD>"
1940 " <INPUT type='text' id='email' value='john@example.com'/>"
1941 " </TD>"
1942 " </TR>"
1943 " <TR>"
1944 " <TD></TD>"
1945 " <TD>"
1946 " <INPUT type='submit' name='reply-send' value='Send'/>"
1947 " </TD>"
1948 " </TR>"
1949 "</TABLE>"
1950 "</FORM>",
1951 labels, names, values);
1954 TEST_F(FormAutofillTest, LabelsInferredFromPreviousTD) {
1955 std::vector<base::string16> labels, names, values;
1957 labels.push_back(ASCIIToUTF16("* First Name"));
1958 names.push_back(ASCIIToUTF16("firstname"));
1959 values.push_back(ASCIIToUTF16("John"));
1961 labels.push_back(ASCIIToUTF16("* Last Name"));
1962 names.push_back(ASCIIToUTF16("lastname"));
1963 values.push_back(ASCIIToUTF16("Smith"));
1965 labels.push_back(ASCIIToUTF16("* Email"));
1966 names.push_back(ASCIIToUTF16("email"));
1967 values.push_back(ASCIIToUTF16("john@example.com"));
1969 ExpectLabels(
1970 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1971 "<TABLE>"
1972 " <TR>"
1973 " <TD>* First Name</TD>"
1974 " <TD>"
1975 " Bogus"
1976 " <INPUT type='hidden'/>"
1977 " <INPUT type='text' id='firstname' value='John'/>"
1978 " </TD>"
1979 " </TR>"
1980 " <TR>"
1981 " <TD>* Last Name</TD>"
1982 " <TD>"
1983 " <INPUT type='text' id='lastname' value='Smith'/>"
1984 " </TD>"
1985 " </TR>"
1986 " <TR>"
1987 " <TD>* Email</TD>"
1988 " <TD>"
1989 " <INPUT type='text' id='email' value='john@example.com'/>"
1990 " </TD>"
1991 " </TR>"
1992 " <TR>"
1993 " <TD></TD>"
1994 " <TD>"
1995 " <INPUT type='submit' name='reply-send' value='Send'/>"
1996 " </TD>"
1997 " </TR>"
1998 "</TABLE>"
1999 "</FORM>",
2000 labels, names, values);
2003 // <script>, <noscript> and <option> tags are excluded when the labels are
2004 // inferred.
2005 // Also <!-- comment --> is excluded.
2006 TEST_F(FormAutofillTest, LabelsInferredFromTableWithSpecialElements) {
2007 std::vector<base::string16> labels, names, values;
2008 std::vector<std::string> control_types;
2010 labels.push_back(ASCIIToUTF16("* First Name"));
2011 names.push_back(ASCIIToUTF16("firstname"));
2012 values.push_back(ASCIIToUTF16("John"));
2013 control_types.push_back("text");
2015 labels.push_back(ASCIIToUTF16("* Middle Name"));
2016 names.push_back(ASCIIToUTF16("middlename"));
2017 values.push_back(ASCIIToUTF16("Joe"));
2018 control_types.push_back("text");
2020 labels.push_back(ASCIIToUTF16("* Last Name"));
2021 names.push_back(ASCIIToUTF16("lastname"));
2022 values.push_back(ASCIIToUTF16("Smith"));
2023 control_types.push_back("text");
2025 labels.push_back(ASCIIToUTF16("* Country"));
2026 names.push_back(ASCIIToUTF16("country"));
2027 values.push_back(ASCIIToUTF16("US"));
2028 control_types.push_back("select-one");
2030 labels.push_back(ASCIIToUTF16("* Email"));
2031 names.push_back(ASCIIToUTF16("email"));
2032 values.push_back(ASCIIToUTF16("john@example.com"));
2033 control_types.push_back("text");
2035 ExpectLabelsAndTypes(
2036 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2037 "<TABLE>"
2038 " <TR>"
2039 " <TD>"
2040 " <SPAN>*</SPAN>"
2041 " <B>First Name</B>"
2042 " </TD>"
2043 " <TD>"
2044 " <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
2045 " </SCRIPT>"
2046 " <INPUT type='text' id='firstname' value='John'/>"
2047 " </TD>"
2048 " </TR>"
2049 " <TR>"
2050 " <TD>"
2051 " <SPAN>*</SPAN>"
2052 " <B>Middle Name</B>"
2053 " </TD>"
2054 " <TD>"
2055 " <NOSCRIPT>"
2056 " <P>Bad</P>"
2057 " </NOSCRIPT>"
2058 " <INPUT type='text' id='middlename' value='Joe'/>"
2059 " </TD>"
2060 " </TR>"
2061 " <TR>"
2062 " <TD>"
2063 " <SPAN>*</SPAN>"
2064 " <B>Last Name</B>"
2065 " </TD>"
2066 " <TD>"
2067 " <INPUT type='text' id='lastname' value='Smith'/>"
2068 " </TD>"
2069 " </TR>"
2070 " <TR>"
2071 " <TD>"
2072 " <SPAN>*</SPAN>"
2073 " <B>Country</B>"
2074 " </TD>"
2075 " <TD>"
2076 " <SELECT id='country'>"
2077 " <OPTION VALUE='US'>The value should be ignored as label."
2078 " </OPTION>"
2079 " <OPTION VALUE='JP'>JAPAN</OPTION>"
2080 " </SELECT>"
2081 " </TD>"
2082 " </TR>"
2083 " <TR>"
2084 " <TD>"
2085 " <SPAN>*</SPAN>"
2086 " <B>Email</B>"
2087 " </TD>"
2088 " <TD>"
2089 " <!-- This comment should be ignored as inferred label.-->"
2090 " <INPUT type='text' id='email' value='john@example.com'/>"
2091 " </TD>"
2092 " </TR>"
2093 " <TR>"
2094 " <TD></TD>"
2095 " <TD>"
2096 " <INPUT type='submit' name='reply-send' value='Send'/>"
2097 " </TD>"
2098 " </TR>"
2099 "</TABLE>"
2100 "</FORM>",
2101 labels, names, values, control_types);
2104 TEST_F(FormAutofillTest, LabelsInferredFromTableLabels) {
2105 ExpectJohnSmithLabels(
2106 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2107 "<TABLE>"
2108 " <TR>"
2109 " <TD>"
2110 " <LABEL>First name:</LABEL>"
2111 " <INPUT type='text' id='firstname' value='John'/>"
2112 " </TD>"
2113 " </TR>"
2114 " <TR>"
2115 " <TD>"
2116 " <LABEL>Last name:</LABEL>"
2117 " <INPUT type='text' id='lastname' value='Smith'/>"
2118 " </TD>"
2119 " </TR>"
2120 " <TR>"
2121 " <TD>"
2122 " <LABEL>Email:</LABEL>"
2123 " <INPUT type='text' id='email' value='john@example.com'/>"
2124 " </TD>"
2125 " </TR>"
2126 "</TABLE>"
2127 "<INPUT type='submit' name='reply-send' value='Send'/>"
2128 "</FORM>");
2131 TEST_F(FormAutofillTest, LabelsInferredFromTableTDInterveningElements) {
2132 ExpectJohnSmithLabels(
2133 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2134 "<TABLE>"
2135 " <TR>"
2136 " <TD>"
2137 " First name:"
2138 " <BR>"
2139 " <INPUT type='text' id='firstname' value='John'/>"
2140 " </TD>"
2141 " </TR>"
2142 " <TR>"
2143 " <TD>"
2144 " Last name:"
2145 " <BR>"
2146 " <INPUT type='text' id='lastname' value='Smith'/>"
2147 " </TD>"
2148 " </TR>"
2149 " <TR>"
2150 " <TD>"
2151 " Email:"
2152 " <BR>"
2153 " <INPUT type='text' id='email' value='john@example.com'/>"
2154 " </TD>"
2155 " </TR>"
2156 "</TABLE>"
2157 "<INPUT type='submit' name='reply-send' value='Send'/>"
2158 "</FORM>");
2161 // Verify that we correctly infer labels when the label text spans multiple
2162 // adjacent HTML elements, not separated by whitespace.
2163 TEST_F(FormAutofillTest, LabelsInferredFromTableAdjacentElements) {
2164 std::vector<base::string16> labels, names, values;
2166 labels.push_back(ASCIIToUTF16("*First Name"));
2167 names.push_back(ASCIIToUTF16("firstname"));
2168 values.push_back(ASCIIToUTF16("John"));
2170 labels.push_back(ASCIIToUTF16("*Last Name"));
2171 names.push_back(ASCIIToUTF16("lastname"));
2172 values.push_back(ASCIIToUTF16("Smith"));
2174 labels.push_back(ASCIIToUTF16("*Email"));
2175 names.push_back(ASCIIToUTF16("email"));
2176 values.push_back(ASCIIToUTF16("john@example.com"));
2178 ExpectLabels(
2179 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2180 "<TABLE>"
2181 " <TR>"
2182 " <TD>"
2183 " <SPAN>*</SPAN><B>First Name</B>"
2184 " </TD>"
2185 " <TD>"
2186 " <INPUT type='text' id='firstname' value='John'/>"
2187 " </TD>"
2188 " </TR>"
2189 " <TR>"
2190 " <TD>"
2191 " <SPAN>*</SPAN><B>Last Name</B>"
2192 " </TD>"
2193 " <TD>"
2194 " <INPUT type='text' id='lastname' value='Smith'/>"
2195 " </TD>"
2196 " </TR>"
2197 " <TR>"
2198 " <TD>"
2199 " <SPAN>*</SPAN><B>Email</B>"
2200 " </TD>"
2201 " <TD>"
2202 " <INPUT type='text' id='email' value='john@example.com'/>"
2203 " </TD>"
2204 " </TR>"
2205 " <TR>"
2206 " <TD>"
2207 " <INPUT type='submit' name='reply-send' value='Send'/>"
2208 " </TD>"
2209 " </TR>"
2210 "</TABLE>"
2211 "</FORM>",
2212 labels, names, values);
2215 // Verify that we correctly infer labels when the label text resides in the
2216 // previous row.
2217 TEST_F(FormAutofillTest, LabelsInferredFromTableRow) {
2218 std::vector<base::string16> labels, names, values;
2220 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2221 names.push_back(ASCIIToUTF16("firstname"));
2222 values.push_back(ASCIIToUTF16("John"));
2224 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2225 names.push_back(ASCIIToUTF16("lastname"));
2226 values.push_back(ASCIIToUTF16("Smith"));
2228 labels.push_back(ASCIIToUTF16("*First Name *Last Name *Email"));
2229 names.push_back(ASCIIToUTF16("email"));
2230 values.push_back(ASCIIToUTF16("john@example.com"));
2232 ExpectLabels(
2233 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2234 "<TABLE>"
2235 " <TR>"
2236 " <TD>*First Name</TD>"
2237 " <TD>*Last Name</TD>"
2238 " <TD>*Email</TD>"
2239 " </TR>"
2240 " <TR>"
2241 " <TD>"
2242 " <INPUT type='text' id='firstname' value='John'/>"
2243 " </TD>"
2244 " <TD>"
2245 " <INPUT type='text' id='lastname' value='Smith'/>"
2246 " </TD>"
2247 " <TD>"
2248 " <INPUT type='text' id='email' value='john@example.com'/>"
2249 " </TD>"
2250 " </TR>"
2251 " <TR>"
2252 " <TD>"
2253 " <INPUT type='submit' name='reply-send' value='Send'/>"
2254 " </TD>"
2255 " </TR>"
2256 "</TABLE>",
2257 labels, names, values);
2260 // Verify that we correctly infer labels when enclosed within a list item.
2261 TEST_F(FormAutofillTest, LabelsInferredFromListItem) {
2262 std::vector<base::string16> labels, names, values;
2264 labels.push_back(ASCIIToUTF16("* Home Phone"));
2265 names.push_back(ASCIIToUTF16("areacode"));
2266 values.push_back(ASCIIToUTF16("415"));
2268 labels.push_back(ASCIIToUTF16("* Home Phone"));
2269 names.push_back(ASCIIToUTF16("prefix"));
2270 values.push_back(ASCIIToUTF16("555"));
2272 labels.push_back(ASCIIToUTF16("* Home Phone"));
2273 names.push_back(ASCIIToUTF16("suffix"));
2274 values.push_back(ASCIIToUTF16("1212"));
2276 ExpectLabels(
2277 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2278 "<DIV>"
2279 " <LI>"
2280 " <SPAN>Bogus</SPAN>"
2281 " </LI>"
2282 " <LI>"
2283 " <LABEL><EM>*</EM> Home Phone</LABEL>"
2284 " <INPUT type='text' id='areacode' value='415'/>"
2285 " <INPUT type='text' id='prefix' value='555'/>"
2286 " <INPUT type='text' id='suffix' value='1212'/>"
2287 " </LI>"
2288 " <LI>"
2289 " <INPUT type='submit' name='reply-send' value='Send'/>"
2290 " </LI>"
2291 "</DIV>"
2292 "</FORM>",
2293 labels, names, values);
2296 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionList) {
2297 std::vector<base::string16> labels, names, values;
2299 labels.push_back(ASCIIToUTF16("* First name: Bogus"));
2300 names.push_back(ASCIIToUTF16("firstname"));
2301 values.push_back(ASCIIToUTF16("John"));
2303 labels.push_back(ASCIIToUTF16("Last name:"));
2304 names.push_back(ASCIIToUTF16("lastname"));
2305 values.push_back(ASCIIToUTF16("Smith"));
2307 labels.push_back(ASCIIToUTF16("Email:"));
2308 names.push_back(ASCIIToUTF16("email"));
2309 values.push_back(ASCIIToUTF16("john@example.com"));
2311 ExpectLabels(
2312 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2313 "<DL>"
2314 " <DT>"
2315 " <SPAN>"
2316 " *"
2317 " </SPAN>"
2318 " <SPAN>"
2319 " First name:"
2320 " </SPAN>"
2321 " <SPAN>"
2322 " Bogus"
2323 " </SPAN>"
2324 " </DT>"
2325 " <DD>"
2326 " <FONT>"
2327 " <INPUT type='text' id='firstname' value='John'/>"
2328 " </FONT>"
2329 " </DD>"
2330 " <DT>"
2331 " <SPAN>"
2332 " Last name:"
2333 " </SPAN>"
2334 " </DT>"
2335 " <DD>"
2336 " <FONT>"
2337 " <INPUT type='text' id='lastname' value='Smith'/>"
2338 " </FONT>"
2339 " </DD>"
2340 " <DT>"
2341 " <SPAN>"
2342 " Email:"
2343 " </SPAN>"
2344 " </DT>"
2345 " <DD>"
2346 " <FONT>"
2347 " <INPUT type='text' id='email' value='john@example.com'/>"
2348 " </FONT>"
2349 " </DD>"
2350 " <DT></DT>"
2351 " <DD>"
2352 " <INPUT type='submit' name='reply-send' value='Send'/>"
2353 " </DD>"
2354 "</DL>"
2355 "</FORM>",
2356 labels, names, values);
2359 TEST_F(FormAutofillTest, LabelsInferredWithSameName) {
2360 std::vector<base::string16> labels, names, values;
2362 labels.push_back(ASCIIToUTF16("Address Line 1:"));
2363 names.push_back(ASCIIToUTF16("Address"));
2364 values.push_back(base::string16());
2366 labels.push_back(ASCIIToUTF16("Address Line 2:"));
2367 names.push_back(ASCIIToUTF16("Address"));
2368 values.push_back(base::string16());
2370 labels.push_back(ASCIIToUTF16("Address Line 3:"));
2371 names.push_back(ASCIIToUTF16("Address"));
2372 values.push_back(base::string16());
2374 ExpectLabels(
2375 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2376 " Address Line 1:"
2377 " <INPUT type='text' name='Address'/>"
2378 " Address Line 2:"
2379 " <INPUT type='text' name='Address'/>"
2380 " Address Line 3:"
2381 " <INPUT type='text' name='Address'/>"
2382 " <INPUT type='submit' name='reply-send' value='Send'/>"
2383 "</FORM>",
2384 labels, names, values);
2387 TEST_F(FormAutofillTest, LabelsInferredWithImageTags) {
2388 std::vector<base::string16> labels, names, values;
2390 labels.push_back(ASCIIToUTF16("Phone:"));
2391 names.push_back(ASCIIToUTF16("dayphone1"));
2392 values.push_back(base::string16());
2394 labels.push_back(ASCIIToUTF16("-"));
2395 names.push_back(ASCIIToUTF16("dayphone2"));
2396 values.push_back(base::string16());
2398 labels.push_back(ASCIIToUTF16("-"));
2399 names.push_back(ASCIIToUTF16("dayphone3"));
2400 values.push_back(base::string16());
2402 labels.push_back(ASCIIToUTF16("ext.:"));
2403 names.push_back(ASCIIToUTF16("dayphone4"));
2404 values.push_back(base::string16());
2406 labels.push_back(base::string16());
2407 names.push_back(ASCIIToUTF16("dummy"));
2408 values.push_back(base::string16());
2410 ExpectLabels(
2411 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2412 " Phone:"
2413 " <input type='text' name='dayphone1'>"
2414 " <img/>"
2415 " -"
2416 " <img/>"
2417 " <input type='text' name='dayphone2'>"
2418 " <img/>"
2419 " -"
2420 " <img/>"
2421 " <input type='text' name='dayphone3'>"
2422 " ext.:"
2423 " <input type='text' name='dayphone4'>"
2424 " <input type='text' name='dummy'>"
2425 " <input type='submit' name='reply-send' value='Send'>"
2426 "</FORM>",
2427 labels, names, values);
2430 TEST_F(FormAutofillTest, LabelsInferredFromDivTable) {
2431 ExpectJohnSmithLabels(
2432 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2433 "<DIV>First name:<BR>"
2434 " <SPAN>"
2435 " <INPUT type='text' name='firstname' value='John'>"
2436 " </SPAN>"
2437 "</DIV>"
2438 "<DIV>Last name:<BR>"
2439 " <SPAN>"
2440 " <INPUT type='text' name='lastname' value='Smith'>"
2441 " </SPAN>"
2442 "</DIV>"
2443 "<DIV>Email:<BR>"
2444 " <SPAN>"
2445 " <INPUT type='text' name='email' value='john@example.com'>"
2446 " </SPAN>"
2447 "</DIV>"
2448 "<input type='submit' name='reply-send' value='Send'>"
2449 "</FORM>");
2452 TEST_F(FormAutofillTest, LabelsInferredFromDivSiblingTable) {
2453 ExpectJohnSmithLabels(
2454 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2455 "<DIV>First name:</DIV>"
2456 "<DIV>"
2457 " <SPAN>"
2458 " <INPUT type='text' name='firstname' value='John'>"
2459 " </SPAN>"
2460 "</DIV>"
2461 "<DIV>Last name:</DIV>"
2462 "<DIV>"
2463 " <SPAN>"
2464 " <INPUT type='text' name='lastname' value='Smith'>"
2465 " </SPAN>"
2466 "</DIV>"
2467 "<DIV>Email:</DIV>"
2468 "<DIV>"
2469 " <SPAN>"
2470 " <INPUT type='text' name='email' value='john@example.com'>"
2471 " </SPAN>"
2472 "</DIV>"
2473 "<input type='submit' name='reply-send' value='Send'>"
2474 "</FORM>");
2477 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionListRatherThanDivTable) {
2478 ExpectJohnSmithLabels(
2479 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2480 "<DIV>This is not a label.<BR>"
2481 "<DL>"
2482 " <DT>"
2483 " <SPAN>"
2484 " First name:"
2485 " </SPAN>"
2486 " </DT>"
2487 " <DD>"
2488 " <FONT>"
2489 " <INPUT type='text' id='firstname' value='John'/>"
2490 " </FONT>"
2491 " </DD>"
2492 " <DT>"
2493 " <SPAN>"
2494 " Last name:"
2495 " </SPAN>"
2496 " </DT>"
2497 " <DD>"
2498 " <FONT>"
2499 " <INPUT type='text' id='lastname' value='Smith'/>"
2500 " </FONT>"
2501 " </DD>"
2502 " <DT>"
2503 " <SPAN>"
2504 " Email:"
2505 " </SPAN>"
2506 " </DT>"
2507 " <DD>"
2508 " <FONT>"
2509 " <INPUT type='text' id='email' value='john@example.com'/>"
2510 " </FONT>"
2511 " </DD>"
2512 " <DT></DT>"
2513 " <DD>"
2514 " <INPUT type='submit' name='reply-send' value='Send'/>"
2515 " </DD>"
2516 "</DL>"
2517 "</DIV>"
2518 "</FORM>");
2521 TEST_F(FormAutofillTest, FillFormMaxLength) {
2522 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2523 " <INPUT type='text' id='firstname' maxlength='5'/>"
2524 " <INPUT type='text' id='lastname' maxlength='7'/>"
2525 " <INPUT type='text' id='email' maxlength='9'/>"
2526 " <INPUT type='submit' name='reply-send' value='Send'/>"
2527 "</FORM>");
2529 WebFrame* web_frame = GetMainFrame();
2530 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2532 FormCache form_cache;
2533 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2534 ASSERT_EQ(1U, forms.size());
2536 // Get the input element we want to find.
2537 WebElement element = web_frame->document().getElementById("firstname");
2538 WebInputElement input_element = element.to<WebInputElement>();
2540 // Find the form that contains the input element.
2541 FormData form;
2542 FormFieldData field;
2543 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2544 &form,
2545 &field,
2546 autofill::REQUIRE_NONE));
2547 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2548 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2549 EXPECT_EQ(GURL("http://buh.com"), form.action);
2551 const std::vector<FormFieldData>& fields = form.fields;
2552 ASSERT_EQ(3U, fields.size());
2554 FormFieldData expected;
2555 expected.form_control_type = "text";
2557 expected.name = ASCIIToUTF16("firstname");
2558 expected.max_length = 5;
2559 expected.is_autofilled = false;
2560 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2562 expected.name = ASCIIToUTF16("lastname");
2563 expected.max_length = 7;
2564 expected.is_autofilled = false;
2565 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2567 expected.name = ASCIIToUTF16("email");
2568 expected.max_length = 9;
2569 expected.is_autofilled = false;
2570 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2572 // Fill the form.
2573 form.fields[0].value = ASCIIToUTF16("Brother");
2574 form.fields[1].value = ASCIIToUTF16("Jonathan");
2575 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2576 form.fields[0].is_autofilled = true;
2577 form.fields[1].is_autofilled = true;
2578 form.fields[2].is_autofilled = true;
2579 FillForm(form, input_element);
2581 // Find the newly-filled form that contains the input element.
2582 FormData form2;
2583 FormFieldData field2;
2584 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2585 &form2,
2586 &field2,
2587 autofill::REQUIRE_NONE));
2589 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2590 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2591 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2593 const std::vector<FormFieldData>& fields2 = form2.fields;
2594 ASSERT_EQ(3U, fields2.size());
2596 expected.form_control_type = "text";
2598 expected.name = ASCIIToUTF16("firstname");
2599 expected.value = ASCIIToUTF16("Broth");
2600 expected.max_length = 5;
2601 expected.is_autofilled = true;
2602 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2604 expected.name = ASCIIToUTF16("lastname");
2605 expected.value = ASCIIToUTF16("Jonatha");
2606 expected.max_length = 7;
2607 expected.is_autofilled = true;
2608 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2610 expected.name = ASCIIToUTF16("email");
2611 expected.value = ASCIIToUTF16("brotherj@");
2612 expected.max_length = 9;
2613 expected.is_autofilled = true;
2614 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2617 // This test uses negative values of the maxlength attribute for input elements.
2618 // In this case, the maxlength of the input elements is set to the default
2619 // maxlength (defined in WebKit.)
2620 TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
2621 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2622 " <INPUT type='text' id='firstname' maxlength='-1'/>"
2623 " <INPUT type='text' id='lastname' maxlength='-10'/>"
2624 " <INPUT type='text' id='email' maxlength='-13'/>"
2625 " <INPUT type='submit' name='reply-send' value='Send'/>"
2626 "</FORM>");
2628 WebFrame* web_frame = GetMainFrame();
2629 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2631 FormCache form_cache;
2632 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2633 ASSERT_EQ(1U, forms.size());
2635 // Get the input element we want to find.
2636 WebElement element = web_frame->document().getElementById("firstname");
2637 WebInputElement input_element = element.to<WebInputElement>();
2639 // Find the form that contains the input element.
2640 FormData form;
2641 FormFieldData field;
2642 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2643 &form,
2644 &field,
2645 autofill::REQUIRE_NONE));
2646 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2647 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2648 EXPECT_EQ(GURL("http://buh.com"), form.action);
2650 const std::vector<FormFieldData>& fields = form.fields;
2651 ASSERT_EQ(3U, fields.size());
2653 FormFieldData expected;
2654 expected.form_control_type = "text";
2655 expected.max_length = WebInputElement::defaultMaxLength();
2657 expected.name = ASCIIToUTF16("firstname");
2658 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2660 expected.name = ASCIIToUTF16("lastname");
2661 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2663 expected.name = ASCIIToUTF16("email");
2664 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2666 // Fill the form.
2667 form.fields[0].value = ASCIIToUTF16("Brother");
2668 form.fields[1].value = ASCIIToUTF16("Jonathan");
2669 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2670 FillForm(form, input_element);
2672 // Find the newly-filled form that contains the input element.
2673 FormData form2;
2674 FormFieldData field2;
2675 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2676 &form2,
2677 &field2,
2678 autofill::REQUIRE_NONE));
2680 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2681 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2682 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2684 const std::vector<FormFieldData>& fields2 = form2.fields;
2685 ASSERT_EQ(3U, fields2.size());
2687 expected.name = ASCIIToUTF16("firstname");
2688 expected.value = ASCIIToUTF16("Brother");
2689 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2691 expected.name = ASCIIToUTF16("lastname");
2692 expected.value = ASCIIToUTF16("Jonathan");
2693 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2695 expected.name = ASCIIToUTF16("email");
2696 expected.value = ASCIIToUTF16("brotherj@example.com");
2697 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2700 TEST_F(FormAutofillTest, FillFormEmptyName) {
2701 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
2702 " <INPUT type='text' id='firstname'/>"
2703 " <INPUT type='text' id='lastname'/>"
2704 " <INPUT type='text' id='email'/>"
2705 " <INPUT type='submit' value='Send'/>"
2706 "</FORM>");
2708 WebFrame* web_frame = GetMainFrame();
2709 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2711 FormCache form_cache;
2712 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2713 ASSERT_EQ(1U, forms.size());
2715 // Get the input element we want to find.
2716 WebElement element = web_frame->document().getElementById("firstname");
2717 WebInputElement input_element = element.to<WebInputElement>();
2719 // Find the form that contains the input element.
2720 FormData form;
2721 FormFieldData field;
2722 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2723 &form,
2724 &field,
2725 autofill::REQUIRE_NONE));
2726 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2727 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2728 EXPECT_EQ(GURL("http://buh.com"), form.action);
2730 const std::vector<FormFieldData>& fields = form.fields;
2731 ASSERT_EQ(3U, fields.size());
2733 FormFieldData expected;
2734 expected.form_control_type = "text";
2735 expected.max_length = WebInputElement::defaultMaxLength();
2737 expected.name = ASCIIToUTF16("firstname");
2738 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2740 expected.name = ASCIIToUTF16("lastname");
2741 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2743 expected.name = ASCIIToUTF16("email");
2744 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2746 // Fill the form.
2747 form.fields[0].value = ASCIIToUTF16("Wyatt");
2748 form.fields[1].value = ASCIIToUTF16("Earp");
2749 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2750 FillForm(form, input_element);
2752 // Find the newly-filled form that contains the input element.
2753 FormData form2;
2754 FormFieldData field2;
2755 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2756 &form2,
2757 &field2,
2758 autofill::REQUIRE_NONE));
2760 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2761 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2762 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2764 const std::vector<FormFieldData>& fields2 = form2.fields;
2765 ASSERT_EQ(3U, fields2.size());
2767 expected.form_control_type = "text";
2768 expected.max_length = WebInputElement::defaultMaxLength();
2770 expected.name = ASCIIToUTF16("firstname");
2771 expected.value = ASCIIToUTF16("Wyatt");
2772 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2774 expected.name = ASCIIToUTF16("lastname");
2775 expected.value = ASCIIToUTF16("Earp");
2776 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2778 expected.name = ASCIIToUTF16("email");
2779 expected.value = ASCIIToUTF16("wyatt@example.com");
2780 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2783 TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
2784 LoadHTML("<FORM action='http://buh.com' method='post'>"
2785 " <INPUT type='text' id='firstname'/>"
2786 " <INPUT type='text' id='middlename'/>"
2787 " <INPUT type='text' id='lastname'/>"
2788 " <INPUT type='submit' value='Send'/>"
2789 "</FORM>"
2790 "<FORM action='http://abc.com' method='post'>"
2791 " <INPUT type='text' id='apple'/>"
2792 " <INPUT type='text' id='banana'/>"
2793 " <INPUT type='text' id='cantelope'/>"
2794 " <INPUT type='submit' value='Send'/>"
2795 "</FORM>");
2797 WebFrame* web_frame = GetMainFrame();
2798 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
2800 FormCache form_cache;
2801 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
2802 ASSERT_EQ(2U, forms.size());
2804 // Get the input element we want to find.
2805 WebElement element = web_frame->document().getElementById("apple");
2806 WebInputElement input_element = element.to<WebInputElement>();
2808 // Find the form that contains the input element.
2809 FormData form;
2810 FormFieldData field;
2811 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2812 &form,
2813 &field,
2814 autofill::REQUIRE_NONE));
2815 EXPECT_EQ(base::string16(), form.name);
2816 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2817 EXPECT_EQ(GURL("http://abc.com"), form.action);
2819 const std::vector<FormFieldData>& fields = form.fields;
2820 ASSERT_EQ(3U, fields.size());
2822 FormFieldData expected;
2823 expected.form_control_type = "text";
2824 expected.max_length = WebInputElement::defaultMaxLength();
2826 expected.name = ASCIIToUTF16("apple");
2827 expected.is_autofilled = false;
2828 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2830 expected.name = ASCIIToUTF16("banana");
2831 expected.is_autofilled = false;
2832 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2834 expected.name = ASCIIToUTF16("cantelope");
2835 expected.is_autofilled = false;
2836 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2838 // Fill the form.
2839 form.fields[0].value = ASCIIToUTF16("Red");
2840 form.fields[1].value = ASCIIToUTF16("Yellow");
2841 form.fields[2].value = ASCIIToUTF16("Also Yellow");
2842 form.fields[0].is_autofilled = true;
2843 form.fields[1].is_autofilled = true;
2844 form.fields[2].is_autofilled = true;
2845 FillForm(form, input_element);
2847 // Find the newly-filled form that contains the input element.
2848 FormData form2;
2849 FormFieldData field2;
2850 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2851 &form2,
2852 &field2,
2853 autofill::REQUIRE_NONE));
2855 EXPECT_EQ(base::string16(), form2.name);
2856 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2857 EXPECT_EQ(GURL("http://abc.com"), form2.action);
2859 const std::vector<FormFieldData>& fields2 = form2.fields;
2860 ASSERT_EQ(3U, fields2.size());
2862 expected.name = ASCIIToUTF16("apple");
2863 expected.value = ASCIIToUTF16("Red");
2864 expected.is_autofilled = true;
2865 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2867 expected.name = ASCIIToUTF16("banana");
2868 expected.value = ASCIIToUTF16("Yellow");
2869 expected.is_autofilled = true;
2870 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2872 expected.name = ASCIIToUTF16("cantelope");
2873 expected.value = ASCIIToUTF16("Also Yellow");
2874 expected.is_autofilled = true;
2875 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2878 TEST_F(FormAutofillTest, ThreePartPhone) {
2879 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2880 " Phone:"
2881 " <input type='text' name='dayphone1'>"
2882 " -"
2883 " <input type='text' name='dayphone2'>"
2884 " -"
2885 " <input type='text' name='dayphone3'>"
2886 " ext.:"
2887 " <input type='text' name='dayphone4'>"
2888 " <input type='submit' name='reply-send' value='Send'>"
2889 "</FORM>");
2892 WebFrame* frame = GetMainFrame();
2893 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2895 WebVector<WebFormElement> forms;
2896 frame->document().forms(forms);
2897 ASSERT_EQ(1U, forms.size());
2899 FormData form;
2900 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2901 WebFormControlElement(),
2902 autofill::REQUIRE_NONE,
2903 autofill::EXTRACT_VALUE,
2904 &form,
2905 NULL));
2906 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2907 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2908 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2910 const std::vector<FormFieldData>& fields = form.fields;
2911 ASSERT_EQ(4U, fields.size());
2913 FormFieldData expected;
2914 expected.form_control_type = "text";
2915 expected.max_length = WebInputElement::defaultMaxLength();
2917 expected.label = ASCIIToUTF16("Phone:");
2918 expected.name = ASCIIToUTF16("dayphone1");
2919 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2921 expected.label = ASCIIToUTF16("-");
2922 expected.name = ASCIIToUTF16("dayphone2");
2923 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2925 expected.label = ASCIIToUTF16("-");
2926 expected.name = ASCIIToUTF16("dayphone3");
2927 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2929 expected.label = ASCIIToUTF16("ext.:");
2930 expected.name = ASCIIToUTF16("dayphone4");
2931 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2935 TEST_F(FormAutofillTest, MaxLengthFields) {
2936 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2937 " Phone:"
2938 " <input type='text' maxlength='3' name='dayphone1'>"
2939 " -"
2940 " <input type='text' maxlength='3' name='dayphone2'>"
2941 " -"
2942 " <input type='text' maxlength='4' size='5'"
2943 " name='dayphone3'>"
2944 " ext.:"
2945 " <input type='text' maxlength='5' name='dayphone4'>"
2946 " <input type='text' name='default1'>"
2947 " <input type='text' maxlength='-1' name='invalid1'>"
2948 " <input type='submit' name='reply-send' value='Send'>"
2949 "</FORM>");
2951 WebFrame* frame = GetMainFrame();
2952 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
2954 WebVector<WebFormElement> forms;
2955 frame->document().forms(forms);
2956 ASSERT_EQ(1U, forms.size());
2958 FormData form;
2959 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2960 WebFormControlElement(),
2961 autofill::REQUIRE_NONE,
2962 autofill::EXTRACT_VALUE,
2963 &form,
2964 NULL));
2965 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2966 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2967 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2969 const std::vector<FormFieldData>& fields = form.fields;
2970 ASSERT_EQ(6U, fields.size());
2972 FormFieldData expected;
2973 expected.form_control_type = "text";
2975 expected.label = ASCIIToUTF16("Phone:");
2976 expected.name = ASCIIToUTF16("dayphone1");
2977 expected.max_length = 3;
2978 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2980 expected.label = ASCIIToUTF16("-");
2981 expected.name = ASCIIToUTF16("dayphone2");
2982 expected.max_length = 3;
2983 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2985 expected.label = ASCIIToUTF16("-");
2986 expected.name = ASCIIToUTF16("dayphone3");
2987 expected.max_length = 4;
2988 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2990 expected.label = ASCIIToUTF16("ext.:");
2991 expected.name = ASCIIToUTF16("dayphone4");
2992 expected.max_length = 5;
2993 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2995 // When unspecified |size|, default is returned.
2996 expected.label = base::string16();
2997 expected.name = ASCIIToUTF16("default1");
2998 expected.max_length = WebInputElement::defaultMaxLength();
2999 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
3001 // When invalid |size|, default is returned.
3002 expected.label = base::string16();
3003 expected.name = ASCIIToUTF16("invalid1");
3004 expected.max_length = WebInputElement::defaultMaxLength();
3005 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
3008 // This test re-creates the experience of typing in a field then selecting a
3009 // profile from the Autofill suggestions popup. The field that is being typed
3010 // into should be filled even though it's not technically empty.
3011 TEST_F(FormAutofillTest, FillFormNonEmptyField) {
3012 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3013 " <INPUT type='text' id='firstname'/>"
3014 " <INPUT type='text' id='lastname'/>"
3015 " <INPUT type='text' id='email'/>"
3016 " <INPUT type='submit' value='Send'/>"
3017 "</FORM>");
3019 WebFrame* web_frame = GetMainFrame();
3020 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3022 FormCache form_cache;
3023 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3024 ASSERT_EQ(1U, forms.size());
3026 // Get the input element we want to find.
3027 WebElement element = web_frame->document().getElementById("firstname");
3028 WebInputElement input_element = element.to<WebInputElement>();
3030 // Simulate typing by modifying the field value.
3031 input_element.setValue(ASCIIToUTF16("Wy"));
3033 // Find the form that contains the input element.
3034 FormData form;
3035 FormFieldData field;
3036 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
3037 &form,
3038 &field,
3039 autofill::REQUIRE_NONE));
3040 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3041 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
3042 EXPECT_EQ(GURL("http://buh.com"), form.action);
3044 const std::vector<FormFieldData>& fields = form.fields;
3045 ASSERT_EQ(3U, fields.size());
3047 FormFieldData expected;
3048 expected.form_control_type = "text";
3049 expected.max_length = WebInputElement::defaultMaxLength();
3051 expected.name = ASCIIToUTF16("firstname");
3052 expected.value = ASCIIToUTF16("Wy");
3053 expected.is_autofilled = false;
3054 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3056 expected.name = ASCIIToUTF16("lastname");
3057 expected.value = base::string16();
3058 expected.is_autofilled = false;
3059 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3061 expected.name = ASCIIToUTF16("email");
3062 expected.value = base::string16();
3063 expected.is_autofilled = false;
3064 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3066 // Preview the form and verify that the cursor position has been updated.
3067 form.fields[0].value = ASCIIToUTF16("Wyatt");
3068 form.fields[1].value = ASCIIToUTF16("Earp");
3069 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
3070 form.fields[0].is_autofilled = true;
3071 form.fields[1].is_autofilled = true;
3072 form.fields[2].is_autofilled = true;
3073 PreviewForm(form, input_element);
3074 EXPECT_EQ(2, input_element.selectionStart());
3075 EXPECT_EQ(5, input_element.selectionEnd());
3077 // Fill the form.
3078 FillForm(form, input_element);
3080 // Find the newly-filled form that contains the input element.
3081 FormData form2;
3082 FormFieldData field2;
3083 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
3084 &form2,
3085 &field2,
3086 autofill::REQUIRE_NONE));
3088 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3089 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3090 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3092 const std::vector<FormFieldData>& fields2 = form2.fields;
3093 ASSERT_EQ(3U, fields2.size());
3095 expected.name = ASCIIToUTF16("firstname");
3096 expected.value = ASCIIToUTF16("Wyatt");
3097 expected.is_autofilled = true;
3098 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3100 expected.name = ASCIIToUTF16("lastname");
3101 expected.value = ASCIIToUTF16("Earp");
3102 expected.is_autofilled = true;
3103 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3105 expected.name = ASCIIToUTF16("email");
3106 expected.value = ASCIIToUTF16("wyatt@example.com");
3107 expected.is_autofilled = true;
3108 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3110 // Verify that the cursor position has been updated.
3111 EXPECT_EQ(5, input_element.selectionStart());
3112 EXPECT_EQ(5, input_element.selectionEnd());
3115 TEST_F(FormAutofillTest, ClearFormWithNode) {
3116 LoadHTML(
3117 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3118 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3119 " <INPUT type='text' id='lastname' value='Earp'/>"
3120 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3121 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3122 " <INPUT type='month' id='month' value='2012-11'>"
3123 " <INPUT type='month' id='month-disabled' value='2012-11'"
3124 " disabled='disabled'>"
3125 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3126 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3127 " Banana!"
3128 " </TEXTAREA>"
3129 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3130 " <INPUT type='submit' value='Send'/>"
3131 "</FORM>");
3133 WebFrame* web_frame = GetMainFrame();
3134 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3136 FormCache form_cache;
3137 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3138 ASSERT_EQ(1U, forms.size());
3140 // Set the auto-filled attribute.
3141 WebInputElement firstname =
3142 web_frame->document().getElementById("firstname").to<WebInputElement>();
3143 firstname.setAutofilled(true);
3144 WebInputElement lastname =
3145 web_frame->document().getElementById("lastname").to<WebInputElement>();
3146 lastname.setAutofilled(true);
3147 WebInputElement month =
3148 web_frame->document().getElementById("month").to<WebInputElement>();
3149 month.setAutofilled(true);
3150 WebInputElement textarea =
3151 web_frame->document().getElementById("textarea").to<WebInputElement>();
3152 textarea.setAutofilled(true);
3154 // Set the value of the disabled text input element.
3155 WebInputElement notenabled =
3156 web_frame->document().getElementById("notenabled").to<WebInputElement>();
3157 notenabled.setValue(WebString::fromUTF8("no clear"));
3159 // Clear the form.
3160 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3162 // Verify that the auto-filled attribute has been turned off.
3163 EXPECT_FALSE(firstname.isAutofilled());
3165 // Verify the form is cleared.
3166 FormData form2;
3167 FormFieldData field2;
3168 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
3169 &form2,
3170 &field2,
3171 autofill::REQUIRE_NONE));
3172 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3173 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3174 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3176 const std::vector<FormFieldData>& fields2 = form2.fields;
3177 ASSERT_EQ(9U, fields2.size());
3179 FormFieldData expected;
3180 expected.form_control_type = "text";
3181 expected.max_length = WebInputElement::defaultMaxLength();
3183 expected.name = ASCIIToUTF16("firstname");
3184 expected.value = base::string16();
3185 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3187 expected.name = ASCIIToUTF16("lastname");
3188 expected.value = base::string16();
3189 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3191 expected.name = ASCIIToUTF16("noAC");
3192 expected.value = ASCIIToUTF16("one");
3193 expected.autocomplete_attribute = "off";
3194 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3195 expected.autocomplete_attribute = std::string(); // reset
3197 expected.name = ASCIIToUTF16("notenabled");
3198 expected.value = ASCIIToUTF16("no clear");
3199 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[3]);
3201 expected.form_control_type = "month";
3202 expected.max_length = 0;
3203 expected.name = ASCIIToUTF16("month");
3204 expected.value = base::string16();
3205 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[4]);
3207 expected.name = ASCIIToUTF16("month-disabled");
3208 expected.value = ASCIIToUTF16("2012-11");
3209 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[5]);
3211 expected.form_control_type = "textarea";
3212 expected.name = ASCIIToUTF16("textarea");
3213 expected.value = base::string16();
3214 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[6]);
3216 expected.name = ASCIIToUTF16("textarea-disabled");
3217 expected.value = ASCIIToUTF16(" Banana! ");
3218 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[7]);
3220 expected.name = ASCIIToUTF16("textarea-noAC");
3221 expected.value = ASCIIToUTF16("Carrot?");
3222 expected.autocomplete_attribute = "off";
3223 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[8]);
3224 expected.autocomplete_attribute = std::string(); // reset
3226 // Verify that the cursor position has been updated.
3227 EXPECT_EQ(0, firstname.selectionStart());
3228 EXPECT_EQ(0, firstname.selectionEnd());
3231 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) {
3232 LoadHTML(
3233 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3234 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3235 " <INPUT type='text' id='lastname' value='Earp'/>"
3236 " <SELECT id='state' name='state'>"
3237 " <OPTION selected>?</OPTION>"
3238 " <OPTION>AA</OPTION>"
3239 " <OPTION>AE</OPTION>"
3240 " <OPTION>AK</OPTION>"
3241 " </SELECT>"
3242 " <INPUT type='submit' value='Send'/>"
3243 "</FORM>");
3245 WebFrame* web_frame = GetMainFrame();
3246 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3248 FormCache form_cache;
3249 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3250 ASSERT_EQ(1U, forms.size());
3252 // Set the auto-filled attribute.
3253 WebInputElement firstname =
3254 web_frame->document().getElementById("firstname").to<WebInputElement>();
3255 firstname.setAutofilled(true);
3256 WebInputElement lastname =
3257 web_frame->document().getElementById("lastname").to<WebInputElement>();
3258 lastname.setAutofilled(true);
3260 // Set the value and auto-filled attribute of the state element.
3261 WebSelectElement state =
3262 web_frame->document().getElementById("state").to<WebSelectElement>();
3263 state.setValue(WebString::fromUTF8("AK"));
3264 state.setAutofilled(true);
3266 // Clear the form.
3267 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3269 // Verify that the auto-filled attribute has been turned off.
3270 EXPECT_FALSE(firstname.isAutofilled());
3272 // Verify the form is cleared.
3273 FormData form2;
3274 FormFieldData field2;
3275 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
3276 &form2,
3277 &field2,
3278 autofill::REQUIRE_NONE));
3279 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3280 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3281 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3283 const std::vector<FormFieldData>& fields2 = form2.fields;
3284 ASSERT_EQ(3U, fields2.size());
3286 FormFieldData expected;
3288 expected.name = ASCIIToUTF16("firstname");
3289 expected.value = base::string16();
3290 expected.form_control_type = "text";
3291 expected.max_length = WebInputElement::defaultMaxLength();
3292 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3294 expected.name = ASCIIToUTF16("lastname");
3295 expected.value = base::string16();
3296 expected.form_control_type = "text";
3297 expected.max_length = WebInputElement::defaultMaxLength();
3298 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3300 expected.name = ASCIIToUTF16("state");
3301 expected.value = ASCIIToUTF16("?");
3302 expected.form_control_type = "select-one";
3303 expected.max_length = 0;
3304 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3306 // Verify that the cursor position has been updated.
3307 EXPECT_EQ(0, firstname.selectionStart());
3308 EXPECT_EQ(0, firstname.selectionEnd());
3311 TEST_F(FormAutofillTest, ClearPreviewedFormWithElement) {
3312 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3313 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3314 " <INPUT type='text' id='lastname'/>"
3315 " <INPUT type='text' id='email'/>"
3316 " <INPUT type='email' id='email2'/>"
3317 " <INPUT type='tel' id='phone'/>"
3318 " <INPUT type='submit' value='Send'/>"
3319 "</FORM>");
3321 WebFrame* web_frame = GetMainFrame();
3322 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3324 FormCache form_cache;
3325 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3326 ASSERT_EQ(1U, forms.size());
3328 // Set the auto-filled attribute.
3329 WebInputElement firstname =
3330 web_frame->document().getElementById("firstname").to<WebInputElement>();
3331 firstname.setAutofilled(true);
3332 WebInputElement lastname =
3333 web_frame->document().getElementById("lastname").to<WebInputElement>();
3334 lastname.setAutofilled(true);
3335 WebInputElement email =
3336 web_frame->document().getElementById("email").to<WebInputElement>();
3337 email.setAutofilled(true);
3338 WebInputElement email2 =
3339 web_frame->document().getElementById("email2").to<WebInputElement>();
3340 email2.setAutofilled(true);
3341 WebInputElement phone =
3342 web_frame->document().getElementById("phone").to<WebInputElement>();
3343 phone.setAutofilled(true);
3345 // Set the suggested values on two of the elements.
3346 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3347 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3348 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3349 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3351 // Clear the previewed fields.
3352 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
3354 // Fields with empty suggestions suggestions are not modified.
3355 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
3356 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3357 EXPECT_TRUE(firstname.isAutofilled());
3359 // Verify the previewed fields are cleared.
3360 EXPECT_TRUE(lastname.value().isEmpty());
3361 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3362 EXPECT_FALSE(lastname.isAutofilled());
3363 EXPECT_TRUE(email.value().isEmpty());
3364 EXPECT_TRUE(email.suggestedValue().isEmpty());
3365 EXPECT_FALSE(email.isAutofilled());
3366 EXPECT_TRUE(email2.value().isEmpty());
3367 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3368 EXPECT_FALSE(email2.isAutofilled());
3369 EXPECT_TRUE(phone.value().isEmpty());
3370 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3371 EXPECT_FALSE(phone.isAutofilled());
3373 // Verify that the cursor position has been updated.
3374 EXPECT_EQ(0, lastname.selectionStart());
3375 EXPECT_EQ(0, lastname.selectionEnd());
3378 TEST_F(FormAutofillTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
3379 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3380 " <INPUT type='text' id='firstname' value='W'/>"
3381 " <INPUT type='text' id='lastname'/>"
3382 " <INPUT type='text' id='email'/>"
3383 " <INPUT type='email' id='email2'/>"
3384 " <INPUT type='tel' id='phone'/>"
3385 " <INPUT type='submit' value='Send'/>"
3386 "</FORM>");
3388 WebFrame* web_frame = GetMainFrame();
3389 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3391 FormCache form_cache;
3392 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3393 ASSERT_EQ(1U, forms.size());
3395 // Set the auto-filled attribute.
3396 WebInputElement firstname =
3397 web_frame->document().getElementById("firstname").to<WebInputElement>();
3398 firstname.setAutofilled(true);
3399 WebInputElement lastname =
3400 web_frame->document().getElementById("lastname").to<WebInputElement>();
3401 lastname.setAutofilled(true);
3402 WebInputElement email =
3403 web_frame->document().getElementById("email").to<WebInputElement>();
3404 email.setAutofilled(true);
3405 WebInputElement email2 =
3406 web_frame->document().getElementById("email2").to<WebInputElement>();
3407 email2.setAutofilled(true);
3408 WebInputElement phone =
3409 web_frame->document().getElementById("phone").to<WebInputElement>();
3410 phone.setAutofilled(true);
3413 // Set the suggested values on all of the elements.
3414 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3415 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3416 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3417 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3418 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3420 // Clear the previewed fields.
3421 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
3423 // Fields with non-empty values are restored.
3424 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
3425 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3426 EXPECT_FALSE(firstname.isAutofilled());
3427 EXPECT_EQ(1, firstname.selectionStart());
3428 EXPECT_EQ(1, firstname.selectionEnd());
3430 // Verify the previewed fields are cleared.
3431 EXPECT_TRUE(lastname.value().isEmpty());
3432 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3433 EXPECT_FALSE(lastname.isAutofilled());
3434 EXPECT_TRUE(email.value().isEmpty());
3435 EXPECT_TRUE(email.suggestedValue().isEmpty());
3436 EXPECT_FALSE(email.isAutofilled());
3437 EXPECT_TRUE(email2.value().isEmpty());
3438 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3439 EXPECT_FALSE(email2.isAutofilled());
3440 EXPECT_TRUE(phone.value().isEmpty());
3441 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3442 EXPECT_FALSE(phone.isAutofilled());
3445 TEST_F(FormAutofillTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
3446 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>"
3447 " <INPUT type='text' id='firstname' value='W'/>"
3448 " <INPUT type='text' id='lastname'/>"
3449 " <INPUT type='text' id='email'/>"
3450 " <INPUT type='email' id='email2'/>"
3451 " <INPUT type='tel' id='phone'/>"
3452 " <INPUT type='submit' value='Send'/>"
3453 "</FORM>");
3455 WebFrame* web_frame = GetMainFrame();
3456 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3458 FormCache form_cache;
3459 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3460 ASSERT_EQ(1U, forms.size());
3462 // Set the auto-filled attribute.
3463 WebInputElement firstname =
3464 web_frame->document().getElementById("firstname").to<WebInputElement>();
3465 firstname.setAutofilled(true);
3466 WebInputElement lastname =
3467 web_frame->document().getElementById("lastname").to<WebInputElement>();
3468 lastname.setAutofilled(true);
3469 WebInputElement email =
3470 web_frame->document().getElementById("email").to<WebInputElement>();
3471 email.setAutofilled(true);
3472 WebInputElement email2 =
3473 web_frame->document().getElementById("email2").to<WebInputElement>();
3474 email2.setAutofilled(true);
3475 WebInputElement phone =
3476 web_frame->document().getElementById("phone").to<WebInputElement>();
3477 phone.setAutofilled(true);
3479 // Set the suggested values on all of the elements.
3480 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3481 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3482 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3483 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3484 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3486 // Clear the previewed fields.
3487 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
3489 // Fields with non-empty values are restored.
3490 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
3491 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3492 EXPECT_TRUE(firstname.isAutofilled());
3493 EXPECT_EQ(1, firstname.selectionStart());
3494 EXPECT_EQ(1, firstname.selectionEnd());
3496 // Verify the previewed fields are cleared.
3497 EXPECT_TRUE(lastname.value().isEmpty());
3498 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3499 EXPECT_FALSE(lastname.isAutofilled());
3500 EXPECT_TRUE(email.value().isEmpty());
3501 EXPECT_TRUE(email.suggestedValue().isEmpty());
3502 EXPECT_FALSE(email.isAutofilled());
3503 EXPECT_TRUE(email2.value().isEmpty());
3504 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3505 EXPECT_FALSE(email2.isAutofilled());
3506 EXPECT_TRUE(phone.value().isEmpty());
3507 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3508 EXPECT_FALSE(phone.isAutofilled());
3511 // Autofill's "Clear Form" should clear only autofilled fields
3512 TEST_F(FormAutofillTest, ClearOnlyAutofilledFields) {
3513 // Load the form.
3514 LoadHTML(
3515 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3516 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3517 " <INPUT type='text' id='lastname' value='Earp'/>"
3518 " <INPUT type='email' id='email' value='wyatt@earp.com'/>"
3519 " <INPUT type='tel' id='phone' value='650-777-9999'/>"
3520 " <INPUT type='submit' value='Send'/>"
3521 "</FORM>");
3523 WebFrame* web_frame = GetMainFrame();
3524 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame);
3526 FormCache form_cache;
3527 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
3528 ASSERT_EQ(1U, forms.size());
3530 // Set the autofilled attribute.
3531 WebInputElement firstname =
3532 web_frame->document().getElementById("firstname").to<WebInputElement>();
3533 firstname.setAutofilled(false);
3534 WebInputElement lastname =
3535 web_frame->document().getElementById("lastname").to<WebInputElement>();
3536 lastname.setAutofilled(true);
3537 WebInputElement email =
3538 web_frame->document().getElementById("email").to<WebInputElement>();
3539 email.setAutofilled(true);
3540 WebInputElement phone =
3541 web_frame->document().getElementById("phone").to<WebInputElement>();
3542 phone.setAutofilled(true);
3544 // Clear the fields.
3545 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3547 // Verify only autofilled fields are cleared.
3548 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
3549 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3550 EXPECT_FALSE(firstname.isAutofilled());
3551 EXPECT_TRUE(lastname.value().isEmpty());
3552 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3553 EXPECT_FALSE(lastname.isAutofilled());
3554 EXPECT_TRUE(email.value().isEmpty());
3555 EXPECT_TRUE(email.suggestedValue().isEmpty());
3556 EXPECT_FALSE(email.isAutofilled());
3557 EXPECT_TRUE(phone.value().isEmpty());
3558 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3559 EXPECT_FALSE(phone.isAutofilled());
3562 // If we have multiple labels per id, the labels concatenated into label string.
3563 TEST_F(FormAutofillTest, MultipleLabelsPerElement) {
3564 std::vector<base::string16> labels, names, values;
3566 labels.push_back(ASCIIToUTF16("First Name:"));
3567 names.push_back(ASCIIToUTF16("firstname"));
3568 values.push_back(ASCIIToUTF16("John"));
3570 labels.push_back(ASCIIToUTF16("Last Name:"));
3571 names.push_back(ASCIIToUTF16("lastname"));
3572 values.push_back(ASCIIToUTF16("Smith"));
3574 labels.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
3575 names.push_back(ASCIIToUTF16("email"));
3576 values.push_back(ASCIIToUTF16("john@example.com"));
3578 ExpectLabels(
3579 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3580 " <LABEL for='firstname'> First Name: </LABEL>"
3581 " <LABEL for='firstname'></LABEL>"
3582 " <INPUT type='text' id='firstname' value='John'/>"
3583 " <LABEL for='lastname'></LABEL>"
3584 " <LABEL for='lastname'> Last Name: </LABEL>"
3585 " <INPUT type='text' id='lastname' value='Smith'/>"
3586 " <LABEL for='email'> Email: </LABEL>"
3587 " <LABEL for='email'> xxx@yyy.com </LABEL>"
3588 " <INPUT type='text' id='email' value='john@example.com'/>"
3589 " <INPUT type='submit' name='reply-send' value='Send'/>"
3590 "</FORM>",
3591 labels, names, values);
3594 TEST_F(FormAutofillTest, ClickElement) {
3595 LoadHTML("<BUTTON id='link'>Button</BUTTON>"
3596 "<BUTTON name='button'>Button</BUTTON>");
3597 WebFrame* frame = GetMainFrame();
3598 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3600 // Successful retrieval by id.
3601 autofill::WebElementDescriptor clicker;
3602 clicker.retrieval_method = autofill::WebElementDescriptor::ID;
3603 clicker.descriptor = "link";
3604 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3606 // Successful retrieval by css selector.
3607 clicker.retrieval_method = autofill::WebElementDescriptor::CSS_SELECTOR;
3608 clicker.descriptor = "button[name='button']";
3609 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3611 // Unsuccessful retrieval due to invalid CSS selector.
3612 clicker.descriptor = "^*&";
3613 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3615 // Unsuccessful retrieval because element does not exist.
3616 clicker.descriptor = "#junk";
3617 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3620 TEST_F(FormAutofillTest, SelectOneAsText) {
3621 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3622 " <INPUT type='text' id='firstname' value='John'/>"
3623 " <INPUT type='text' id='lastname' value='Smith'/>"
3624 " <SELECT id='country'>"
3625 " <OPTION value='AF'>Afghanistan</OPTION>"
3626 " <OPTION value='AL'>Albania</OPTION>"
3627 " <OPTION value='DZ'>Algeria</OPTION>"
3628 " </SELECT>"
3629 " <INPUT type='submit' name='reply-send' value='Send'/>"
3630 "</FORM>");
3632 WebFrame* frame = GetMainFrame();
3633 ASSERT_NE(static_cast<WebFrame*>(NULL), frame);
3635 // Set the value of the select-one.
3636 WebSelectElement select_element =
3637 frame->document().getElementById("country").to<WebSelectElement>();
3638 select_element.setValue(WebString::fromUTF8("AL"));
3640 WebVector<WebFormElement> forms;
3641 frame->document().forms(forms);
3642 ASSERT_EQ(1U, forms.size());
3644 FormData form;
3646 // Extract the country select-one value as text.
3647 EXPECT_TRUE(WebFormElementToFormData(
3648 forms[0], WebFormControlElement(), autofill::REQUIRE_NONE,
3649 static_cast<autofill::ExtractMask>(
3650 autofill::EXTRACT_VALUE | autofill::EXTRACT_OPTION_TEXT),
3651 &form, NULL));
3652 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3653 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3654 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3656 const std::vector<FormFieldData>& fields = form.fields;
3657 ASSERT_EQ(3U, fields.size());
3659 FormFieldData expected;
3661 expected.name = ASCIIToUTF16("firstname");
3662 expected.value = ASCIIToUTF16("John");
3663 expected.form_control_type = "text";
3664 expected.max_length = WebInputElement::defaultMaxLength();
3665 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3667 expected.name = ASCIIToUTF16("lastname");
3668 expected.value = ASCIIToUTF16("Smith");
3669 expected.form_control_type = "text";
3670 expected.max_length = WebInputElement::defaultMaxLength();
3671 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3673 expected.name = ASCIIToUTF16("country");
3674 expected.value = ASCIIToUTF16("Albania");
3675 expected.form_control_type = "select-one";
3676 expected.max_length = 0;
3677 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3679 form.fields.clear();
3680 // Extract the country select-one value as value.
3681 EXPECT_TRUE(WebFormElementToFormData(forms[0],
3682 WebFormControlElement(),
3683 autofill::REQUIRE_NONE,
3684 autofill::EXTRACT_VALUE,
3685 &form,
3686 NULL));
3687 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3688 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3689 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3691 ASSERT_EQ(3U, fields.size());
3693 expected.name = ASCIIToUTF16("firstname");
3694 expected.value = ASCIIToUTF16("John");
3695 expected.form_control_type = "text";
3696 expected.max_length = WebInputElement::defaultMaxLength();
3697 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3699 expected.name = ASCIIToUTF16("lastname");
3700 expected.value = ASCIIToUTF16("Smith");
3701 expected.form_control_type = "text";
3702 expected.max_length = WebInputElement::defaultMaxLength();
3703 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3705 expected.name = ASCIIToUTF16("country");
3706 expected.value = ASCIIToUTF16("AL");
3707 expected.form_control_type = "select-one";
3708 expected.max_length = 0;
3709 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3712 } // namespace autofill