Roll ANGLE e754fb8..6ffeb74
[chromium-blink-merge.git] / chrome / renderer / autofill / form_autofill_browsertest.cc
blob88d8994a424f5ce991acf4459c8ecd983fcbbb7b
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/WebElementCollection.h"
23 #include "third_party/WebKit/public/web/WebExceptionCode.h"
24 #include "third_party/WebKit/public/web/WebFormControlElement.h"
25 #include "third_party/WebKit/public/web/WebFormElement.h"
26 #include "third_party/WebKit/public/web/WebInputElement.h"
27 #include "third_party/WebKit/public/web/WebLocalFrame.h"
28 #include "third_party/WebKit/public/web/WebSelectElement.h"
29 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
31 using base::ASCIIToUTF16;
32 using blink::WebDocument;
33 using blink::WebElement;
34 using blink::WebExceptionCode;
35 using blink::WebFormControlElement;
36 using blink::WebFormElement;
37 using blink::WebFrame;
38 using blink::WebInputElement;
39 using blink::WebSelectElement;
40 using blink::WebString;
41 using blink::WebTextAreaElement;
42 using blink::WebVector;
44 namespace autofill {
46 namespace {
48 struct AutofillFieldCase {
49 const char* const form_control_type;
50 const char* const name;
51 const char* const initial_value;
52 const char* const autocomplete_attribute; // The autocomplete attribute of
53 // the element.
54 bool should_be_autofilled; // Whether the filed should be autofilled.
55 const char* const autofill_value; // The value being used to fill the field.
56 const char* const expected_value; // The expected value after Autofill
57 // or Preview.
60 const char kFormHtml[] =
61 "<FORM name='TestForm' action='http://buh.com' method='post'>"
62 " <INPUT type='text' id='firstname'/>"
63 " <INPUT type='text' id='lastname'/>"
64 " <INPUT type='hidden' id='imhidden'/>"
65 " <INPUT type='text' id='notempty' value='Hi'/>"
66 " <INPUT type='text' autocomplete='off' id='noautocomplete'/>"
67 " <INPUT type='text' disabled='disabled' id='notenabled'/>"
68 " <INPUT type='text' readonly id='readonly'/>"
69 " <INPUT type='text' style='visibility: hidden'"
70 " id='invisible'/>"
71 " <INPUT type='text' style='display: none' id='displaynone'/>"
72 " <INPUT type='month' id='month'/>"
73 " <INPUT type='month' id='month-nonempty' value='2011-12'/>"
74 " <SELECT id='select'>"
75 " <OPTION></OPTION>"
76 " <OPTION value='CA'>California</OPTION>"
77 " <OPTION value='TX'>Texas</OPTION>"
78 " </SELECT>"
79 " <SELECT id='select-nonempty'>"
80 " <OPTION value='CA' selected>California</OPTION>"
81 " <OPTION value='TX'>Texas</OPTION>"
82 " </SELECT>"
83 " <SELECT id='select-unchanged'>"
84 " <OPTION value='CA' selected>California</OPTION>"
85 " <OPTION value='TX'>Texas</OPTION>"
86 " </SELECT>"
87 " <TEXTAREA id='textarea'></TEXTAREA>"
88 " <TEXTAREA id='textarea-nonempty'>Go&#10;away!</TEXTAREA>"
89 " <INPUT type='submit' name='reply-send' value='Send'/>"
90 "</FORM>";
92 const char kUnownedFormHtml[] =
93 "<HEAD><TITLE>enter shipping info</TITLE></HEAD>"
94 "<INPUT type='text' id='firstname'/>"
95 "<INPUT type='text' id='lastname'/>"
96 "<INPUT type='hidden' id='imhidden'/>"
97 "<INPUT type='text' id='notempty' value='Hi'/>"
98 "<INPUT type='text' autocomplete='off' id='noautocomplete'/>"
99 "<INPUT type='text' disabled='disabled' id='notenabled'/>"
100 "<INPUT type='text' readonly id='readonly'/>"
101 "<INPUT type='text' style='visibility: hidden'"
102 " id='invisible'/>"
103 "<INPUT type='text' style='display: none' id='displaynone'/>"
104 "<INPUT type='month' id='month'/>"
105 "<INPUT type='month' id='month-nonempty' value='2011-12'/>"
106 "<SELECT id='select'>"
107 " <OPTION></OPTION>"
108 " <OPTION value='CA'>California</OPTION>"
109 " <OPTION value='TX'>Texas</OPTION>"
110 "</SELECT>"
111 "<SELECT id='select-nonempty'>"
112 " <OPTION value='CA' selected>California</OPTION>"
113 " <OPTION value='TX'>Texas</OPTION>"
114 "</SELECT>"
115 "<SELECT id='select-unchanged'>"
116 " <OPTION value='CA' selected>California</OPTION>"
117 " <OPTION value='TX'>Texas</OPTION>"
118 "</SELECT>"
119 "<TEXTAREA id='textarea'></TEXTAREA>"
120 "<TEXTAREA id='textarea-nonempty'>Go&#10;away!</TEXTAREA>"
121 "<INPUT type='submit' name='reply-send' value='Send'/>";
123 std::string RetrievalMethodToString(
124 const WebElementDescriptor::RetrievalMethod& method) {
125 switch (method) {
126 case WebElementDescriptor::CSS_SELECTOR:
127 return "CSS_SELECTOR";
128 case WebElementDescriptor::ID:
129 return "ID";
130 case WebElementDescriptor::NONE:
131 return "NONE";
133 NOTREACHED();
134 return "UNKNOWN";
137 bool ClickElement(const WebDocument& document,
138 const WebElementDescriptor& element_descriptor) {
139 WebString web_descriptor = WebString::fromUTF8(element_descriptor.descriptor);
140 blink::WebElement element;
142 switch (element_descriptor.retrieval_method) {
143 case WebElementDescriptor::CSS_SELECTOR: {
144 WebExceptionCode ec = 0;
145 element = document.querySelector(web_descriptor, ec);
146 if (ec)
147 DVLOG(1) << "Query selector failed. Error code: " << ec << ".";
148 break;
150 case WebElementDescriptor::ID:
151 element = document.getElementById(web_descriptor);
152 break;
153 case WebElementDescriptor::NONE:
154 return true;
157 if (element.isNull()) {
158 DVLOG(1) << "Could not find "
159 << element_descriptor.descriptor
160 << " by "
161 << RetrievalMethodToString(element_descriptor.retrieval_method)
162 << ".";
163 return false;
166 element.simulateClick();
167 return true;
170 } // namespace
172 class FormAutofillTest : public ChromeRenderViewTest {
173 public:
174 FormAutofillTest() : ChromeRenderViewTest() {}
175 ~FormAutofillTest() override {}
177 void ExpectLabels(const char* html,
178 const std::vector<base::string16>& labels,
179 const std::vector<base::string16>& names,
180 const std::vector<base::string16>& values) {
181 std::vector<std::string> control_types(labels.size(), "text");
182 ExpectLabelsAndTypes(html, labels, names, values, control_types);
185 void ExpectLabelsAndTypes(const char* html,
186 const std::vector<base::string16>& labels,
187 const std::vector<base::string16>& names,
188 const std::vector<base::string16>& values,
189 const std::vector<std::string>& control_types) {
190 ASSERT_EQ(labels.size(), names.size());
191 ASSERT_EQ(labels.size(), values.size());
192 ASSERT_EQ(labels.size(), control_types.size());
194 LoadHTML(html);
196 WebFrame* web_frame = GetMainFrame();
197 ASSERT_NE(nullptr, web_frame);
199 FormCache form_cache(*web_frame);
200 std::vector<FormData> forms = form_cache.ExtractNewForms();
201 ASSERT_EQ(1U, forms.size());
203 const FormData& form = forms[0];
204 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
205 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
206 EXPECT_EQ(GURL("http://cnn.com"), form.action);
208 const std::vector<FormFieldData>& fields = form.fields;
209 ASSERT_EQ(labels.size(), fields.size());
210 for (size_t i = 0; i < labels.size(); ++i) {
211 int max_length = control_types[i] == "text" ?
212 WebInputElement::defaultMaxLength() : 0;
213 FormFieldData expected;
214 expected.label = labels[i];
215 expected.name = names[i];
216 expected.value = values[i];
217 expected.form_control_type = control_types[i];
218 expected.max_length = max_length;
219 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
220 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
224 void ExpectJohnSmithLabels(const char* html) {
225 std::vector<base::string16> labels, names, values;
227 labels.push_back(ASCIIToUTF16("First name:"));
228 names.push_back(ASCIIToUTF16("firstname"));
229 values.push_back(ASCIIToUTF16("John"));
231 labels.push_back(ASCIIToUTF16("Last name:"));
232 names.push_back(ASCIIToUTF16("lastname"));
233 values.push_back(ASCIIToUTF16("Smith"));
235 labels.push_back(ASCIIToUTF16("Email:"));
236 names.push_back(ASCIIToUTF16("email"));
237 values.push_back(ASCIIToUTF16("john@example.com"));
239 ExpectLabels(html, labels, names, values);
242 typedef void (*FillFormFunction)(const FormData& form,
243 const WebFormControlElement& element);
245 typedef WebString (*GetValueFunction)(WebFormControlElement element);
247 // Test FormFillxxx functions.
248 void TestFormFillFunctions(const char* html,
249 bool unowned,
250 const AutofillFieldCase* field_cases,
251 size_t number_of_field_cases,
252 FillFormFunction fill_form_function,
253 GetValueFunction get_value_function) {
254 LoadHTML(html);
256 WebFrame* web_frame = GetMainFrame();
257 ASSERT_NE(nullptr, web_frame);
259 FormCache form_cache(*web_frame);
260 std::vector<FormData> forms = form_cache.ExtractNewForms();
261 ASSERT_EQ(1U, forms.size());
263 // Get the input element we want to find.
264 WebInputElement input_element = GetInputElementById("firstname");
266 // Find the form that contains the input element.
267 FormData form_data;
268 FormFieldData field;
269 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element, &form_data,
270 &field));
271 if (!unowned) {
272 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name);
273 EXPECT_EQ(GURL("http://buh.com"), form_data.action);
276 const std::vector<FormFieldData>& fields = form_data.fields;
277 ASSERT_EQ(number_of_field_cases, fields.size());
279 FormFieldData expected;
280 // Verify field's initial value.
281 for (size_t i = 0; i < number_of_field_cases; ++i) {
282 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
283 field_cases[i].name));
284 expected.form_control_type = field_cases[i].form_control_type;
285 expected.max_length =
286 expected.form_control_type == "text" ?
287 WebInputElement::defaultMaxLength() : 0;
288 expected.name = ASCIIToUTF16(field_cases[i].name);
289 expected.value = ASCIIToUTF16(field_cases[i].initial_value);
290 expected.autocomplete_attribute = field_cases[i].autocomplete_attribute;
291 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[i]);
292 // Fill the form_data for the field.
293 form_data.fields[i].value = ASCIIToUTF16(field_cases[i].autofill_value);
294 // Set the is_autofilled property for the field.
295 form_data.fields[i].is_autofilled = field_cases[i].should_be_autofilled;
298 // Autofill the form using the given fill form function.
299 fill_form_function(form_data, input_element);
301 // Validate Autofill or Preview results.
302 for (size_t i = 0; i < number_of_field_cases; ++i) {
303 ValidateFilledField(field_cases[i], get_value_function);
307 // Validate an Autofilled field.
308 void ValidateFilledField(const AutofillFieldCase& field_case,
309 GetValueFunction get_value_function) {
310 SCOPED_TRACE(base::StringPrintf("Verify autofilled value for field %s",
311 field_case.name));
312 WebString value;
313 WebFormControlElement element =
314 GetFormControlElementById(ASCIIToUTF16(field_case.name));
315 if ((element.formControlType() == "select-one") ||
316 (element.formControlType() == "textarea")) {
317 value = get_value_function(element);
318 } else {
319 ASSERT_TRUE(element.formControlType() == "text" ||
320 element.formControlType() == "month");
321 value = get_value_function(element);
324 const WebString expected_value = ASCIIToUTF16(field_case.expected_value);
325 if (expected_value.isEmpty())
326 EXPECT_TRUE(value.isEmpty());
327 else
328 EXPECT_EQ(expected_value.utf8(), value.utf8());
330 EXPECT_EQ(field_case.should_be_autofilled, element.isAutofilled());
333 WebFormControlElement GetFormControlElementById(const WebString& id) {
334 return GetMainFrame()->document().getElementById(
335 id).to<WebFormControlElement>();
338 WebInputElement GetInputElementById(const WebString& id) {
339 return GetMainFrame()->document().getElementById(
340 id).to<WebInputElement>();
343 void TestFillForm(const char* html, bool unowned) {
344 static const AutofillFieldCase field_cases[] = {
345 // fields: form_control_type, name, initial_value, autocomplete_attribute,
346 // should_be_autofilled, autofill_value, expected_value
348 // Regular empty fields (firstname & lastname) should be autofilled.
349 {"text",
350 "firstname",
353 true,
354 "filled firstname",
355 "filled firstname"},
356 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
357 // hidden fields should not be extracted to form_data.
358 // Non empty fields should not be autofilled.
359 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
360 {"text",
361 "noautocomplete",
363 "off",
364 true,
365 "filled noautocomplete",
366 "filled noautocomplete"},
367 // Disabled fields should not be autofilled.
368 {"text", "notenabled", "", "", false, "filled notenabled", ""},
369 // Readonly fields should not be autofilled.
370 {"text", "readonly", "", "", false, "filled readonly", ""},
371 // Fields with "visibility: hidden" should not be autofilled.
372 {"text", "invisible", "", "", false, "filled invisible", ""},
373 // Fields with "display:none" should not be autofilled.
374 {"text", "displaynone", "", "", false, "filled displaynone", ""},
375 // Regular <input type="month"> should be autofilled.
376 {"month", "month", "", "", true, "2017-11", "2017-11"},
377 // Non-empty <input type="month"> should not be autofilled.
378 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
379 // Regular select fields should be autofilled.
380 {"select-one", "select", "", "", true, "TX", "TX"},
381 // Select fields should be autofilled even if they already have a
382 // non-empty value.
383 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
384 // Select fields should not be autofilled if no new value is passed from
385 // autofill profile. The existing value should not be overriden.
386 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
387 // Regular textarea elements should be autofilled.
388 {"textarea",
389 "textarea",
392 true,
393 "some multi-\nline value",
394 "some multi-\nline value"},
395 // Non-empty textarea elements should not be autofilled.
396 {"textarea",
397 "textarea-nonempty",
398 "Go\naway!",
400 false,
401 "some multi-\nline value",
402 "Go\naway!"},
404 TestFormFillFunctions(html, unowned, field_cases, arraysize(field_cases),
405 FillForm, &GetValueWrapper);
406 // Verify preview selection.
407 WebInputElement firstname = GetInputElementById("firstname");
408 EXPECT_EQ(16, firstname.selectionStart());
409 EXPECT_EQ(16, firstname.selectionEnd());
412 void TestPreviewForm(const char* html, bool unowned) {
413 static const AutofillFieldCase field_cases[] = {
414 // Normal empty fields should be previewed.
415 {"text",
416 "firstname",
419 true,
420 "suggested firstname",
421 "suggested firstname"},
422 {"text",
423 "lastname",
426 true,
427 "suggested lastname",
428 "suggested lastname"},
429 // Hidden fields should not be extracted to form_data.
430 // Non empty fields should not be previewed.
431 {"text", "notempty", "Hi", "", false, "suggested notempty", ""},
432 {"text",
433 "noautocomplete",
435 "off",
436 true,
437 "filled noautocomplete",
438 "filled noautocomplete"},
439 // Disabled fields should not be previewed.
440 {"text", "notenabled", "", "", false, "suggested notenabled", ""},
441 // Readonly fields should not be previewed.
442 {"text", "readonly", "", "", false, "suggested readonly", ""},
443 // Fields with "visibility: hidden" should not be previewed.
444 {"text", "invisible", "", "", false, "suggested invisible", ""},
445 // Fields with "display:none" should not previewed.
446 {"text", "displaynone", "", "", false, "suggested displaynone", ""},
447 // Regular <input type="month"> should be previewed.
448 {"month", "month", "", "", true, "2017-11", "2017-11"},
449 // Non-empty <input type="month"> should not be previewed.
450 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
451 // Regular select fields should be previewed.
452 {"select-one", "select", "", "", true, "TX", "TX"},
453 // Select fields should be previewed even if they already have a
454 // non-empty value.
455 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
456 // Select fields should not be previewed if no suggestion is passed from
457 // autofill profile.
458 {"select-one", "select-unchanged", "CA", "", false, "", ""},
459 // Normal textarea elements should be previewed.
460 {"textarea",
461 "textarea",
464 true,
465 "suggested multi-\nline value",
466 "suggested multi-\nline value"},
467 // Nonempty textarea elements should not be previewed.
468 {"textarea",
469 "textarea-nonempty",
470 "Go\naway!",
472 false,
473 "suggested multi-\nline value",
474 ""},
476 TestFormFillFunctions(html, unowned, field_cases, arraysize(field_cases),
477 &PreviewForm, &GetSuggestedValueWrapper);
479 // Verify preview selection.
480 WebInputElement firstname = GetInputElementById("firstname");
481 EXPECT_EQ(0, firstname.selectionStart());
482 EXPECT_EQ(19, firstname.selectionEnd());
485 void TestFindFormForInputElement(const char* html, bool unowned) {
486 LoadHTML(html);
487 WebFrame* web_frame = GetMainFrame();
488 ASSERT_NE(nullptr, web_frame);
490 FormCache form_cache(*web_frame);
491 std::vector<FormData> forms = form_cache.ExtractNewForms();
492 ASSERT_EQ(1U, forms.size());
494 // Get the input element we want to find.
495 WebInputElement input_element = GetInputElementById("firstname");
497 // Find the form and verify it's the correct form.
498 FormData form;
499 FormFieldData field;
500 EXPECT_TRUE(
501 FindFormAndFieldForFormControlElement(input_element, &form, &field));
502 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
503 if (!unowned) {
504 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
505 EXPECT_EQ(GURL("http://buh.com"), form.action);
508 const std::vector<FormFieldData>& fields = form.fields;
509 ASSERT_EQ(4U, fields.size());
511 FormFieldData expected;
512 expected.form_control_type = "text";
513 expected.max_length = WebInputElement::defaultMaxLength();
515 expected.name = ASCIIToUTF16("firstname");
516 expected.value = ASCIIToUTF16("John");
517 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
518 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
520 expected.name = ASCIIToUTF16("lastname");
521 expected.value = ASCIIToUTF16("Smith");
522 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
524 expected.name = ASCIIToUTF16("email");
525 expected.value = ASCIIToUTF16("john@example.com");
526 expected.autocomplete_attribute = "off";
527 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
528 expected.autocomplete_attribute.clear();
530 expected.name = ASCIIToUTF16("phone");
531 expected.value = ASCIIToUTF16("1.800.555.1234");
532 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
535 void TestFindFormForTextAreaElement(const char* html, bool unowned) {
536 LoadHTML(html);
537 WebFrame* web_frame = GetMainFrame();
538 ASSERT_NE(nullptr, web_frame);
540 FormCache form_cache(*web_frame);
541 std::vector<FormData> forms = form_cache.ExtractNewForms();
542 ASSERT_EQ(1U, forms.size());
544 // Get the textarea element we want to find.
545 WebElement element = web_frame->document().getElementById("street-address");
546 WebTextAreaElement textarea_element = element.to<WebTextAreaElement>();
548 // Find the form and verify it's the correct form.
549 FormData form;
550 FormFieldData field;
551 EXPECT_TRUE(
552 FindFormAndFieldForFormControlElement(textarea_element, &form, &field));
553 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
554 if (!unowned) {
555 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
556 EXPECT_EQ(GURL("http://buh.com"), form.action);
559 const std::vector<FormFieldData>& fields = form.fields;
560 ASSERT_EQ(4U, fields.size());
562 FormFieldData expected;
564 expected.name = ASCIIToUTF16("firstname");
565 expected.value = ASCIIToUTF16("John");
566 expected.form_control_type = "text";
567 expected.max_length = WebInputElement::defaultMaxLength();
568 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
570 expected.name = ASCIIToUTF16("lastname");
571 expected.value = ASCIIToUTF16("Smith");
572 expected.form_control_type = "text";
573 expected.max_length = WebInputElement::defaultMaxLength();
574 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
576 expected.name = ASCIIToUTF16("email");
577 expected.value = ASCIIToUTF16("john@example.com");
578 expected.autocomplete_attribute = "off";
579 expected.form_control_type = "text";
580 expected.max_length = WebInputElement::defaultMaxLength();
581 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
582 expected.autocomplete_attribute.clear();
584 expected.name = ASCIIToUTF16("street-address");
585 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
586 expected.form_control_type = "textarea";
587 expected.max_length = 0;
588 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
589 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
592 void TestFillFormMaxLength(const char* html, bool unowned) {
593 LoadHTML(html);
594 WebFrame* web_frame = GetMainFrame();
595 ASSERT_NE(nullptr, web_frame);
597 FormCache form_cache(*web_frame);
598 std::vector<FormData> forms = form_cache.ExtractNewForms();
599 ASSERT_EQ(1U, forms.size());
601 // Get the input element we want to find.
602 WebInputElement input_element = GetInputElementById("firstname");
604 // Find the form that contains the input element.
605 FormData form;
606 FormFieldData field;
607 EXPECT_TRUE(
608 FindFormAndFieldForFormControlElement(input_element, &form, &field));
609 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
610 if (!unowned) {
611 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
612 EXPECT_EQ(GURL("http://buh.com"), form.action);
615 const std::vector<FormFieldData>& fields = form.fields;
616 ASSERT_EQ(3U, fields.size());
618 FormFieldData expected;
619 expected.form_control_type = "text";
621 expected.name = ASCIIToUTF16("firstname");
622 expected.max_length = 5;
623 expected.is_autofilled = false;
624 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
626 expected.name = ASCIIToUTF16("lastname");
627 expected.max_length = 7;
628 expected.is_autofilled = false;
629 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
631 expected.name = ASCIIToUTF16("email");
632 expected.max_length = 9;
633 expected.is_autofilled = false;
634 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
636 // Fill the form.
637 form.fields[0].value = ASCIIToUTF16("Brother");
638 form.fields[1].value = ASCIIToUTF16("Jonathan");
639 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
640 form.fields[0].is_autofilled = true;
641 form.fields[1].is_autofilled = true;
642 form.fields[2].is_autofilled = true;
643 FillForm(form, input_element);
645 // Find the newly-filled form that contains the input element.
646 FormData form2;
647 FormFieldData field2;
648 EXPECT_TRUE(
649 FindFormAndFieldForFormControlElement(input_element, &form2, &field2));
650 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
651 if (!unowned) {
652 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
653 EXPECT_EQ(GURL("http://buh.com"), form2.action);
656 const std::vector<FormFieldData>& fields2 = form2.fields;
657 ASSERT_EQ(3U, fields2.size());
659 expected.form_control_type = "text";
661 expected.name = ASCIIToUTF16("firstname");
662 expected.value = ASCIIToUTF16("Broth");
663 expected.max_length = 5;
664 expected.is_autofilled = true;
665 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
667 expected.name = ASCIIToUTF16("lastname");
668 expected.value = ASCIIToUTF16("Jonatha");
669 expected.max_length = 7;
670 expected.is_autofilled = true;
671 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
673 expected.name = ASCIIToUTF16("email");
674 expected.value = ASCIIToUTF16("brotherj@");
675 expected.max_length = 9;
676 expected.is_autofilled = true;
677 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
680 void TestFillFormNegativeMaxLength(const char* html, bool unowned) {
681 LoadHTML(html);
682 WebFrame* web_frame = GetMainFrame();
683 ASSERT_NE(nullptr, web_frame);
685 FormCache form_cache(*web_frame);
686 std::vector<FormData> forms = form_cache.ExtractNewForms();
687 ASSERT_EQ(1U, forms.size());
689 // Get the input element we want to find.
690 WebInputElement input_element = GetInputElementById("firstname");
692 // Find the form that contains the input element.
693 FormData form;
694 FormFieldData field;
695 EXPECT_TRUE(
696 FindFormAndFieldForFormControlElement(input_element, &form, &field));
697 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
698 if (!unowned) {
699 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
700 EXPECT_EQ(GURL("http://buh.com"), form.action);
703 const std::vector<FormFieldData>& fields = form.fields;
704 ASSERT_EQ(3U, fields.size());
706 FormFieldData expected;
707 expected.form_control_type = "text";
708 expected.max_length = WebInputElement::defaultMaxLength();
710 expected.name = ASCIIToUTF16("firstname");
711 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
713 expected.name = ASCIIToUTF16("lastname");
714 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
716 expected.name = ASCIIToUTF16("email");
717 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
719 // Fill the form.
720 form.fields[0].value = ASCIIToUTF16("Brother");
721 form.fields[1].value = ASCIIToUTF16("Jonathan");
722 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
723 FillForm(form, input_element);
725 // Find the newly-filled form that contains the input element.
726 FormData form2;
727 FormFieldData field2;
728 EXPECT_TRUE(
729 FindFormAndFieldForFormControlElement(input_element, &form2, &field2));
730 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
731 if (!unowned) {
732 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
733 EXPECT_EQ(GURL("http://buh.com"), form2.action);
736 const std::vector<FormFieldData>& fields2 = form2.fields;
737 ASSERT_EQ(3U, fields2.size());
739 expected.name = ASCIIToUTF16("firstname");
740 expected.value = ASCIIToUTF16("Brother");
741 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
743 expected.name = ASCIIToUTF16("lastname");
744 expected.value = ASCIIToUTF16("Jonathan");
745 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
747 expected.name = ASCIIToUTF16("email");
748 expected.value = ASCIIToUTF16("brotherj@example.com");
749 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
752 void TestFillFormEmptyName(const char* html, bool unowned) {
753 LoadHTML(html);
754 WebFrame* web_frame = GetMainFrame();
755 ASSERT_NE(nullptr, web_frame);
757 FormCache form_cache(*web_frame);
758 std::vector<FormData> forms = form_cache.ExtractNewForms();
759 ASSERT_EQ(1U, forms.size());
761 // Get the input element we want to find.
762 WebInputElement input_element = GetInputElementById("firstname");
764 // Find the form that contains the input element.
765 FormData form;
766 FormFieldData field;
767 EXPECT_TRUE(
768 FindFormAndFieldForFormControlElement(input_element, &form, &field));
769 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
770 if (!unowned) {
771 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
772 EXPECT_EQ(GURL("http://buh.com"), form.action);
775 const std::vector<FormFieldData>& fields = form.fields;
776 ASSERT_EQ(3U, fields.size());
778 FormFieldData expected;
779 expected.form_control_type = "text";
780 expected.max_length = WebInputElement::defaultMaxLength();
782 expected.name = ASCIIToUTF16("firstname");
783 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
785 expected.name = ASCIIToUTF16("lastname");
786 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
788 expected.name = ASCIIToUTF16("email");
789 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
791 // Fill the form.
792 form.fields[0].value = ASCIIToUTF16("Wyatt");
793 form.fields[1].value = ASCIIToUTF16("Earp");
794 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
795 FillForm(form, input_element);
797 // Find the newly-filled form that contains the input element.
798 FormData form2;
799 FormFieldData field2;
800 EXPECT_TRUE(
801 FindFormAndFieldForFormControlElement(input_element, &form2, &field2));
802 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
803 if (!unowned) {
804 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
805 EXPECT_EQ(GURL("http://buh.com"), form2.action);
808 const std::vector<FormFieldData>& fields2 = form2.fields;
809 ASSERT_EQ(3U, fields2.size());
811 expected.form_control_type = "text";
812 expected.max_length = WebInputElement::defaultMaxLength();
814 expected.name = ASCIIToUTF16("firstname");
815 expected.value = ASCIIToUTF16("Wyatt");
816 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
818 expected.name = ASCIIToUTF16("lastname");
819 expected.value = ASCIIToUTF16("Earp");
820 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
822 expected.name = ASCIIToUTF16("email");
823 expected.value = ASCIIToUTF16("wyatt@example.com");
824 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
827 void TestFillFormEmptyFormNames(const char* html, bool unowned) {
828 LoadHTML(html);
829 WebFrame* web_frame = GetMainFrame();
830 ASSERT_NE(nullptr, web_frame);
832 FormCache form_cache(*web_frame);
833 std::vector<FormData> forms = form_cache.ExtractNewForms();
834 const size_t expected_size = unowned ? 1 : 2;
835 ASSERT_EQ(expected_size, forms.size());
837 // Get the input element we want to find.
838 WebInputElement input_element = GetInputElementById("apple");
840 // Find the form that contains the input element.
841 FormData form;
842 FormFieldData field;
843 EXPECT_TRUE(
844 FindFormAndFieldForFormControlElement(input_element, &form, &field));
845 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
846 if (!unowned) {
847 EXPECT_TRUE(form.name.empty());
848 EXPECT_EQ(GURL("http://abc.com"), form.action);
851 const std::vector<FormFieldData>& fields = form.fields;
852 const size_t unowned_offset = unowned ? 3 : 0;
853 ASSERT_EQ(unowned_offset + 3, fields.size());
855 FormFieldData expected;
856 expected.form_control_type = "text";
857 expected.max_length = WebInputElement::defaultMaxLength();
859 expected.name = ASCIIToUTF16("apple");
860 expected.is_autofilled = false;
861 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[unowned_offset]);
863 expected.name = ASCIIToUTF16("banana");
864 expected.is_autofilled = false;
865 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[unowned_offset + 1]);
867 expected.name = ASCIIToUTF16("cantelope");
868 expected.is_autofilled = false;
869 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[unowned_offset + 2]);
871 // Fill the form.
872 form.fields[unowned_offset + 0].value = ASCIIToUTF16("Red");
873 form.fields[unowned_offset + 1].value = ASCIIToUTF16("Yellow");
874 form.fields[unowned_offset + 2].value = ASCIIToUTF16("Also Yellow");
875 form.fields[unowned_offset + 0].is_autofilled = true;
876 form.fields[unowned_offset + 1].is_autofilled = true;
877 form.fields[unowned_offset + 2].is_autofilled = true;
878 FillForm(form, input_element);
880 // Find the newly-filled form that contains the input element.
881 FormData form2;
882 FormFieldData field2;
883 EXPECT_TRUE(
884 FindFormAndFieldForFormControlElement(input_element, &form2, &field2));
885 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
886 if (!unowned) {
887 EXPECT_TRUE(form2.name.empty());
888 EXPECT_EQ(GURL("http://abc.com"), form2.action);
891 const std::vector<FormFieldData>& fields2 = form2.fields;
892 ASSERT_EQ(unowned_offset + 3, fields2.size());
894 expected.name = ASCIIToUTF16("apple");
895 expected.value = ASCIIToUTF16("Red");
896 expected.is_autofilled = true;
897 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[unowned_offset + 0]);
899 expected.name = ASCIIToUTF16("banana");
900 expected.value = ASCIIToUTF16("Yellow");
901 expected.is_autofilled = true;
902 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[unowned_offset + 1]);
904 expected.name = ASCIIToUTF16("cantelope");
905 expected.value = ASCIIToUTF16("Also Yellow");
906 expected.is_autofilled = true;
907 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[unowned_offset + 2]);
910 void TestFillFormNonEmptyField(const char* html, bool unowned) {
911 LoadHTML(html);
912 WebFrame* web_frame = GetMainFrame();
913 ASSERT_NE(nullptr, web_frame);
915 FormCache form_cache(*web_frame);
916 std::vector<FormData> forms = form_cache.ExtractNewForms();
917 ASSERT_EQ(1U, forms.size());
919 // Get the input element we want to find.
920 WebInputElement input_element = GetInputElementById("firstname");
922 // Simulate typing by modifying the field value.
923 input_element.setValue(ASCIIToUTF16("Wy"));
925 // Find the form that contains the input element.
926 FormData form;
927 FormFieldData field;
928 EXPECT_TRUE(
929 FindFormAndFieldForFormControlElement(input_element, &form, &field));
930 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
931 if (!unowned) {
932 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
933 EXPECT_EQ(GURL("http://buh.com"), form.action);
936 const std::vector<FormFieldData>& fields = form.fields;
937 ASSERT_EQ(3U, fields.size());
939 FormFieldData expected;
940 expected.form_control_type = "text";
941 expected.max_length = WebInputElement::defaultMaxLength();
943 expected.name = ASCIIToUTF16("firstname");
944 expected.value = ASCIIToUTF16("Wy");
945 expected.is_autofilled = false;
946 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
948 expected.name = ASCIIToUTF16("lastname");
949 expected.value.clear();
950 expected.is_autofilled = false;
951 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
953 expected.name = ASCIIToUTF16("email");
954 expected.value.clear();
955 expected.is_autofilled = false;
956 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
958 // Preview the form and verify that the cursor position has been updated.
959 form.fields[0].value = ASCIIToUTF16("Wyatt");
960 form.fields[1].value = ASCIIToUTF16("Earp");
961 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
962 form.fields[0].is_autofilled = true;
963 form.fields[1].is_autofilled = true;
964 form.fields[2].is_autofilled = true;
965 PreviewForm(form, input_element);
966 EXPECT_EQ(2, input_element.selectionStart());
967 EXPECT_EQ(5, input_element.selectionEnd());
969 // Fill the form.
970 FillForm(form, input_element);
972 // Find the newly-filled form that contains the input element.
973 FormData form2;
974 FormFieldData field2;
975 EXPECT_TRUE(
976 FindFormAndFieldForFormControlElement(input_element, &form2, &field2));
977 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
978 if (!unowned) {
979 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
980 EXPECT_EQ(GURL("http://buh.com"), form2.action);
983 const std::vector<FormFieldData>& fields2 = form2.fields;
984 ASSERT_EQ(3U, fields2.size());
986 expected.name = ASCIIToUTF16("firstname");
987 expected.value = ASCIIToUTF16("Wyatt");
988 expected.is_autofilled = true;
989 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
991 expected.name = ASCIIToUTF16("lastname");
992 expected.value = ASCIIToUTF16("Earp");
993 expected.is_autofilled = true;
994 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
996 expected.name = ASCIIToUTF16("email");
997 expected.value = ASCIIToUTF16("wyatt@example.com");
998 expected.is_autofilled = true;
999 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1001 // Verify that the cursor position has been updated.
1002 EXPECT_EQ(5, input_element.selectionStart());
1003 EXPECT_EQ(5, input_element.selectionEnd());
1006 void TestClearFormWithNode(const char* html, bool unowned) {
1007 LoadHTML(html);
1008 WebFrame* web_frame = GetMainFrame();
1009 ASSERT_NE(nullptr, web_frame);
1011 FormCache form_cache(*web_frame);
1012 std::vector<FormData> forms = form_cache.ExtractNewForms();
1013 ASSERT_EQ(1U, forms.size());
1015 // Set the auto-filled attribute.
1016 WebInputElement firstname = GetInputElementById("firstname");
1017 firstname.setAutofilled(true);
1018 WebInputElement lastname = GetInputElementById("lastname");
1019 lastname.setAutofilled(true);
1020 WebInputElement month = GetInputElementById("month");
1021 month.setAutofilled(true);
1022 WebInputElement textarea = GetInputElementById("textarea");
1023 textarea.setAutofilled(true);
1025 // Set the value of the disabled text input element.
1026 WebInputElement notenabled = GetInputElementById("notenabled");
1027 notenabled.setValue(WebString::fromUTF8("no clear"));
1029 // Clear the form.
1030 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
1032 // Verify that the auto-filled attribute has been turned off.
1033 EXPECT_FALSE(firstname.isAutofilled());
1035 // Verify the form is cleared.
1036 FormData form;
1037 FormFieldData field;
1038 EXPECT_TRUE(
1039 FindFormAndFieldForFormControlElement(firstname, &form, &field));
1040 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1041 EXPECT_FALSE(form.origin.is_empty());
1042 if (!unowned) {
1043 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1044 EXPECT_EQ(GURL("http://buh.com"), form.action);
1047 const std::vector<FormFieldData>& fields = form.fields;
1048 ASSERT_EQ(9U, fields.size());
1050 FormFieldData expected;
1051 expected.form_control_type = "text";
1052 expected.max_length = WebInputElement::defaultMaxLength();
1054 expected.name = ASCIIToUTF16("firstname");
1055 expected.value.clear();
1056 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1058 expected.name = ASCIIToUTF16("lastname");
1059 expected.value.clear();
1060 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1062 expected.name = ASCIIToUTF16("noAC");
1063 expected.value = ASCIIToUTF16("one");
1064 expected.autocomplete_attribute = "off";
1065 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1066 expected.autocomplete_attribute.clear();
1068 expected.name = ASCIIToUTF16("notenabled");
1069 expected.value = ASCIIToUTF16("no clear");
1070 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1072 expected.form_control_type = "month";
1073 expected.max_length = 0;
1074 expected.name = ASCIIToUTF16("month");
1075 expected.value.clear();
1076 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
1078 expected.name = ASCIIToUTF16("month-disabled");
1079 expected.value = ASCIIToUTF16("2012-11");
1080 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
1082 expected.form_control_type = "textarea";
1083 expected.name = ASCIIToUTF16("textarea");
1084 expected.value.clear();
1085 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[6]);
1087 expected.name = ASCIIToUTF16("textarea-disabled");
1088 expected.value = ASCIIToUTF16(" Banana! ");
1089 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[7]);
1091 expected.name = ASCIIToUTF16("textarea-noAC");
1092 expected.value = ASCIIToUTF16("Carrot?");
1093 expected.autocomplete_attribute = "off";
1094 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[8]);
1095 expected.autocomplete_attribute.clear();
1097 // Verify that the cursor position has been updated.
1098 EXPECT_EQ(0, firstname.selectionStart());
1099 EXPECT_EQ(0, firstname.selectionEnd());
1102 void TestClearFormWithNodeContainingSelectOne(const char* html,
1103 bool unowned) {
1104 LoadHTML(html);
1105 WebFrame* web_frame = GetMainFrame();
1106 ASSERT_NE(nullptr, web_frame);
1108 FormCache form_cache(*web_frame);
1109 std::vector<FormData> forms = form_cache.ExtractNewForms();
1110 ASSERT_EQ(1U, forms.size());
1112 // Set the auto-filled attribute.
1113 WebInputElement firstname = GetInputElementById("firstname");
1114 firstname.setAutofilled(true);
1115 WebInputElement lastname = GetInputElementById("lastname");
1116 lastname.setAutofilled(true);
1118 // Set the value and auto-filled attribute of the state element.
1119 WebSelectElement state =
1120 web_frame->document().getElementById("state").to<WebSelectElement>();
1121 state.setValue(WebString::fromUTF8("AK"));
1122 state.setAutofilled(true);
1124 // Clear the form.
1125 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
1127 // Verify that the auto-filled attribute has been turned off.
1128 EXPECT_FALSE(firstname.isAutofilled());
1130 // Verify the form is cleared.
1131 FormData form;
1132 FormFieldData field;
1133 EXPECT_TRUE(
1134 FindFormAndFieldForFormControlElement(firstname, &form, &field));
1135 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1136 EXPECT_FALSE(form.origin.is_empty());
1137 if (!unowned) {
1138 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1139 EXPECT_EQ(GURL("http://buh.com"), form.action);
1142 const std::vector<FormFieldData>& fields = form.fields;
1143 ASSERT_EQ(3U, fields.size());
1145 FormFieldData expected;
1147 expected.name = ASCIIToUTF16("firstname");
1148 expected.value.clear();
1149 expected.form_control_type = "text";
1150 expected.max_length = WebInputElement::defaultMaxLength();
1151 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1153 expected.name = ASCIIToUTF16("lastname");
1154 expected.value.clear();
1155 expected.form_control_type = "text";
1156 expected.max_length = WebInputElement::defaultMaxLength();
1157 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1159 expected.name = ASCIIToUTF16("state");
1160 expected.value = ASCIIToUTF16("?");
1161 expected.form_control_type = "select-one";
1162 expected.max_length = 0;
1163 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1165 // Verify that the cursor position has been updated.
1166 EXPECT_EQ(0, firstname.selectionStart());
1167 EXPECT_EQ(0, firstname.selectionEnd());
1170 void TestClearPreviewedFormWithElement(const char* html) {
1171 LoadHTML(html);
1172 WebFrame* web_frame = GetMainFrame();
1173 ASSERT_NE(nullptr, web_frame);
1175 FormCache form_cache(*web_frame);
1176 std::vector<FormData> forms = form_cache.ExtractNewForms();
1177 ASSERT_EQ(1U, forms.size());
1179 // Set the auto-filled attribute.
1180 WebInputElement firstname = GetInputElementById("firstname");
1181 firstname.setAutofilled(true);
1182 WebInputElement lastname = GetInputElementById("lastname");
1183 lastname.setAutofilled(true);
1184 WebInputElement email = GetInputElementById("email");
1185 email.setAutofilled(true);
1186 WebInputElement email2 = GetInputElementById("email2");
1187 email2.setAutofilled(true);
1188 WebInputElement phone = GetInputElementById("phone");
1189 phone.setAutofilled(true);
1191 // Set the suggested values on two of the elements.
1192 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
1193 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1194 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1195 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1197 // Clear the previewed fields.
1198 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
1200 // Fields with empty suggestions suggestions are not modified.
1201 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
1202 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1203 EXPECT_TRUE(firstname.isAutofilled());
1205 // Verify the previewed fields are cleared.
1206 EXPECT_TRUE(lastname.value().isEmpty());
1207 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1208 EXPECT_FALSE(lastname.isAutofilled());
1209 EXPECT_TRUE(email.value().isEmpty());
1210 EXPECT_TRUE(email.suggestedValue().isEmpty());
1211 EXPECT_FALSE(email.isAutofilled());
1212 EXPECT_TRUE(email2.value().isEmpty());
1213 EXPECT_TRUE(email2.suggestedValue().isEmpty());
1214 EXPECT_FALSE(email2.isAutofilled());
1215 EXPECT_TRUE(phone.value().isEmpty());
1216 EXPECT_TRUE(phone.suggestedValue().isEmpty());
1217 EXPECT_FALSE(phone.isAutofilled());
1219 // Verify that the cursor position has been updated.
1220 EXPECT_EQ(0, lastname.selectionStart());
1221 EXPECT_EQ(0, lastname.selectionEnd());
1224 void TestClearPreviewedFormWithNonEmptyInitiatingNode(const char* html) {
1225 LoadHTML(html);
1226 WebFrame* web_frame = GetMainFrame();
1227 ASSERT_NE(nullptr, web_frame);
1229 FormCache form_cache(*web_frame);
1230 std::vector<FormData> forms = form_cache.ExtractNewForms();
1231 ASSERT_EQ(1U, forms.size());
1233 // Set the auto-filled attribute.
1234 WebInputElement firstname = GetInputElementById("firstname");
1235 firstname.setAutofilled(true);
1236 WebInputElement lastname = GetInputElementById("lastname");
1237 lastname.setAutofilled(true);
1238 WebInputElement email = GetInputElementById("email");
1239 email.setAutofilled(true);
1240 WebInputElement email2 = GetInputElementById("email2");
1241 email2.setAutofilled(true);
1242 WebInputElement phone = GetInputElementById("phone");
1243 phone.setAutofilled(true);
1246 // Set the suggested values on all of the elements.
1247 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
1248 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
1249 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1250 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1251 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1253 // Clear the previewed fields.
1254 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
1256 // Fields with non-empty values are restored.
1257 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
1258 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1259 EXPECT_FALSE(firstname.isAutofilled());
1260 EXPECT_EQ(1, firstname.selectionStart());
1261 EXPECT_EQ(1, firstname.selectionEnd());
1263 // Verify the previewed fields are cleared.
1264 EXPECT_TRUE(lastname.value().isEmpty());
1265 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1266 EXPECT_FALSE(lastname.isAutofilled());
1267 EXPECT_TRUE(email.value().isEmpty());
1268 EXPECT_TRUE(email.suggestedValue().isEmpty());
1269 EXPECT_FALSE(email.isAutofilled());
1270 EXPECT_TRUE(email2.value().isEmpty());
1271 EXPECT_TRUE(email2.suggestedValue().isEmpty());
1272 EXPECT_FALSE(email2.isAutofilled());
1273 EXPECT_TRUE(phone.value().isEmpty());
1274 EXPECT_TRUE(phone.suggestedValue().isEmpty());
1275 EXPECT_FALSE(phone.isAutofilled());
1278 void TestClearPreviewedFormWithAutofilledInitiatingNode(const char* html) {
1279 LoadHTML(html);
1280 WebFrame* web_frame = GetMainFrame();
1281 ASSERT_NE(nullptr, web_frame);
1283 FormCache form_cache(*web_frame);
1284 std::vector<FormData> forms = form_cache.ExtractNewForms();
1285 ASSERT_EQ(1U, forms.size());
1287 // Set the auto-filled attribute.
1288 WebInputElement firstname = GetInputElementById("firstname");
1289 firstname.setAutofilled(true);
1290 WebInputElement lastname = GetInputElementById("lastname");
1291 lastname.setAutofilled(true);
1292 WebInputElement email = GetInputElementById("email");
1293 email.setAutofilled(true);
1294 WebInputElement email2 = GetInputElementById("email2");
1295 email2.setAutofilled(true);
1296 WebInputElement phone = GetInputElementById("phone");
1297 phone.setAutofilled(true);
1299 // Set the suggested values on all of the elements.
1300 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
1301 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
1302 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1303 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1304 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1306 // Clear the previewed fields.
1307 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
1309 // Fields with non-empty values are restored.
1310 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
1311 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1312 EXPECT_TRUE(firstname.isAutofilled());
1313 EXPECT_EQ(1, firstname.selectionStart());
1314 EXPECT_EQ(1, firstname.selectionEnd());
1316 // Verify the previewed fields are cleared.
1317 EXPECT_TRUE(lastname.value().isEmpty());
1318 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1319 EXPECT_FALSE(lastname.isAutofilled());
1320 EXPECT_TRUE(email.value().isEmpty());
1321 EXPECT_TRUE(email.suggestedValue().isEmpty());
1322 EXPECT_FALSE(email.isAutofilled());
1323 EXPECT_TRUE(email2.value().isEmpty());
1324 EXPECT_TRUE(email2.suggestedValue().isEmpty());
1325 EXPECT_FALSE(email2.isAutofilled());
1326 EXPECT_TRUE(phone.value().isEmpty());
1327 EXPECT_TRUE(phone.suggestedValue().isEmpty());
1328 EXPECT_FALSE(phone.isAutofilled());
1331 void TestClearOnlyAutofilledFields(const char* html) {
1332 LoadHTML(html);
1334 WebFrame* web_frame = GetMainFrame();
1335 ASSERT_NE(nullptr, web_frame);
1337 FormCache form_cache(*web_frame);
1338 std::vector<FormData> forms = form_cache.ExtractNewForms();
1339 ASSERT_EQ(1U, forms.size());
1341 // Set the autofilled attribute.
1342 WebInputElement firstname = GetInputElementById("firstname");
1343 firstname.setAutofilled(false);
1344 WebInputElement lastname = GetInputElementById("lastname");
1345 lastname.setAutofilled(true);
1346 WebInputElement email = GetInputElementById("email");
1347 email.setAutofilled(true);
1348 WebInputElement phone = GetInputElementById("phone");
1349 phone.setAutofilled(true);
1351 // Clear the fields.
1352 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
1354 // Verify only autofilled fields are cleared.
1355 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
1356 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1357 EXPECT_FALSE(firstname.isAutofilled());
1358 EXPECT_TRUE(lastname.value().isEmpty());
1359 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1360 EXPECT_FALSE(lastname.isAutofilled());
1361 EXPECT_TRUE(email.value().isEmpty());
1362 EXPECT_TRUE(email.suggestedValue().isEmpty());
1363 EXPECT_FALSE(email.isAutofilled());
1364 EXPECT_TRUE(phone.value().isEmpty());
1365 EXPECT_TRUE(phone.suggestedValue().isEmpty());
1366 EXPECT_FALSE(phone.isAutofilled());
1369 static void FillFormIncludingNonFocusableElementsWrapper(
1370 const FormData& form,
1371 const WebFormControlElement& element) {
1372 FillFormIncludingNonFocusableElements(form, element.form());
1375 static WebString GetValueWrapper(WebFormControlElement element) {
1376 if (element.formControlType() == "textarea")
1377 return element.to<WebTextAreaElement>().value();
1379 if (element.formControlType() == "select-one")
1380 return element.to<WebSelectElement>().value();
1382 return element.to<WebInputElement>().value();
1385 static WebString GetSuggestedValueWrapper(WebFormControlElement element) {
1386 if (element.formControlType() == "textarea")
1387 return element.to<WebTextAreaElement>().suggestedValue();
1389 if (element.formControlType() == "select-one")
1390 return element.to<WebSelectElement>().suggestedValue();
1392 return element.to<WebInputElement>().suggestedValue();
1395 private:
1396 DISALLOW_COPY_AND_ASSIGN(FormAutofillTest);
1399 // We should be able to extract a normal text field.
1400 TEST_F(FormAutofillTest, WebFormControlElementToFormField) {
1401 LoadHTML("<INPUT type='text' id='element' value='value'/>");
1403 WebFrame* frame = GetMainFrame();
1404 ASSERT_NE(nullptr, frame);
1406 WebFormControlElement element = GetFormControlElementById("element");
1407 FormFieldData result1;
1408 WebFormControlElementToFormField(element, EXTRACT_NONE, &result1);
1410 FormFieldData expected;
1411 expected.form_control_type = "text";
1412 expected.max_length = WebInputElement::defaultMaxLength();
1414 expected.name = ASCIIToUTF16("element");
1415 expected.value.clear();
1416 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
1418 FormFieldData result2;
1419 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result2);
1421 expected.name = ASCIIToUTF16("element");
1422 expected.value = ASCIIToUTF16("value");
1423 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
1426 // We should be able to extract a text field with autocomplete="off".
1427 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompleteOff) {
1428 LoadHTML("<INPUT type='text' id='element' value='value'"
1429 " autocomplete='off'/>");
1431 WebFrame* frame = GetMainFrame();
1432 ASSERT_NE(nullptr, frame);
1434 WebFormControlElement element = GetFormControlElementById("element");
1435 FormFieldData result;
1436 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1438 FormFieldData expected;
1439 expected.name = ASCIIToUTF16("element");
1440 expected.value = ASCIIToUTF16("value");
1441 expected.form_control_type = "text";
1442 expected.autocomplete_attribute = "off";
1443 expected.max_length = WebInputElement::defaultMaxLength();
1444 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1447 // We should be able to extract a text field with maxlength specified.
1448 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMaxLength) {
1449 LoadHTML("<INPUT type='text' id='element' value='value'"
1450 " maxlength='5'/>");
1452 WebFrame* frame = GetMainFrame();
1453 ASSERT_NE(nullptr, frame);
1455 WebFormControlElement element = GetFormControlElementById("element");
1456 FormFieldData result;
1457 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1459 FormFieldData expected;
1460 expected.name = ASCIIToUTF16("element");
1461 expected.value = ASCIIToUTF16("value");
1462 expected.form_control_type = "text";
1463 expected.max_length = 5;
1464 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1467 // We should be able to extract a text field that has been autofilled.
1468 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutofilled) {
1469 LoadHTML("<INPUT type='text' id='element' value='value'/>");
1471 WebFrame* frame = GetMainFrame();
1472 ASSERT_NE(nullptr, frame);
1474 WebInputElement element = GetInputElementById("element");
1475 element.setAutofilled(true);
1476 FormFieldData result;
1477 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1479 FormFieldData expected;
1480 expected.name = ASCIIToUTF16("element");
1481 expected.value = ASCIIToUTF16("value");
1482 expected.form_control_type = "text";
1483 expected.max_length = WebInputElement::defaultMaxLength();
1484 expected.is_autofilled = true;
1485 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1488 // We should be able to extract a radio or a checkbox field that has been
1489 // autofilled.
1490 TEST_F(FormAutofillTest, WebFormControlElementToClickableFormField) {
1491 LoadHTML("<INPUT type='checkbox' id='checkbox' value='mail' checked/>"
1492 "<INPUT type='radio' id='radio' value='male'/>");
1494 WebFrame* frame = GetMainFrame();
1495 ASSERT_NE(nullptr, frame);
1497 WebInputElement element = GetInputElementById("checkbox");
1498 element.setAutofilled(true);
1499 FormFieldData result;
1500 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1502 FormFieldData expected;
1503 expected.name = ASCIIToUTF16("checkbox");
1504 expected.value = ASCIIToUTF16("mail");
1505 expected.form_control_type = "checkbox";
1506 expected.is_autofilled = true;
1507 expected.is_checkable = true;
1508 expected.is_checked = true;
1509 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1511 element = GetInputElementById("radio");
1512 element.setAutofilled(true);
1513 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1514 expected.name = ASCIIToUTF16("radio");
1515 expected.value = ASCIIToUTF16("male");
1516 expected.form_control_type = "radio";
1517 expected.is_autofilled = true;
1518 expected.is_checkable = true;
1519 expected.is_checked = false;
1520 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1523 // We should be able to extract a <select> field.
1524 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) {
1525 LoadHTML("<SELECT id='element'/>"
1526 " <OPTION value='CA'>California</OPTION>"
1527 " <OPTION value='TX'>Texas</OPTION>"
1528 "</SELECT>");
1530 WebFrame* frame = GetMainFrame();
1531 ASSERT_NE(nullptr, frame);
1533 WebFormControlElement element = GetFormControlElementById("element");
1534 FormFieldData result1;
1535 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result1);
1537 FormFieldData expected;
1538 expected.name = ASCIIToUTF16("element");
1539 expected.max_length = 0;
1540 expected.form_control_type = "select-one";
1542 expected.value = ASCIIToUTF16("CA");
1543 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
1545 FormFieldData result2;
1546 WebFormControlElementToFormField(
1547 element,
1548 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTION_TEXT),
1549 &result2);
1550 expected.value = ASCIIToUTF16("California");
1551 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
1553 FormFieldData result3;
1554 WebFormControlElementToFormField(element, EXTRACT_OPTIONS, &result3);
1555 expected.value.clear();
1556 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result3);
1558 ASSERT_EQ(2U, result3.option_values.size());
1559 ASSERT_EQ(2U, result3.option_contents.size());
1560 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]);
1561 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]);
1562 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]);
1563 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]);
1566 // When faced with <select> field with *many* options, we should trim them to a
1567 // reasonable number.
1568 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldLongSelect) {
1569 std::string html = "<SELECT id='element'/>";
1570 for (size_t i = 0; i < 2 * kMaxListSize; ++i) {
1571 html += base::StringPrintf("<OPTION value='%" PRIuS "'>"
1572 "%" PRIuS "</OPTION>", i, i);
1574 html += "</SELECT>";
1575 LoadHTML(html.c_str());
1577 WebFrame* frame = GetMainFrame();
1578 ASSERT_TRUE(frame);
1580 WebFormControlElement element = GetFormControlElementById("element");
1581 FormFieldData result;
1582 WebFormControlElementToFormField(element, EXTRACT_OPTIONS, &result);
1584 EXPECT_TRUE(result.option_values.empty());
1585 EXPECT_TRUE(result.option_contents.empty());
1588 // We should be able to extract a <textarea> field.
1589 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) {
1590 LoadHTML("<TEXTAREA id='element'>"
1591 "This element's value&#10;"
1592 "spans multiple lines."
1593 "</TEXTAREA>");
1595 WebFrame* frame = GetMainFrame();
1596 ASSERT_NE(nullptr, frame);
1598 WebFormControlElement element = GetFormControlElementById("element");
1599 FormFieldData result_sans_value;
1600 WebFormControlElementToFormField(element, EXTRACT_NONE, &result_sans_value);
1602 FormFieldData expected;
1603 expected.name = ASCIIToUTF16("element");
1604 expected.max_length = 0;
1605 expected.form_control_type = "textarea";
1606 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
1608 FormFieldData result_with_value;
1609 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result_with_value);
1610 expected.value = ASCIIToUTF16("This element's value\n"
1611 "spans multiple lines.");
1612 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
1615 // We should be able to extract an <input type="month"> field.
1616 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMonthInput) {
1617 LoadHTML("<INPUT type='month' id='element' value='2011-12'>");
1619 WebFrame* frame = GetMainFrame();
1620 ASSERT_NE(nullptr, frame);
1622 WebFormControlElement element = GetFormControlElementById("element");
1623 FormFieldData result_sans_value;
1624 WebFormControlElementToFormField(element, EXTRACT_NONE, &result_sans_value);
1626 FormFieldData expected;
1627 expected.name = ASCIIToUTF16("element");
1628 expected.max_length = 0;
1629 expected.form_control_type = "month";
1630 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
1632 FormFieldData result_with_value;
1633 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result_with_value);
1634 expected.value = ASCIIToUTF16("2011-12");
1635 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
1638 // We should not extract the value for non-text and non-select fields.
1639 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) {
1640 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1641 " <INPUT type='hidden' id='hidden' value='apple'/>"
1642 " <INPUT type='submit' id='submit' value='Send'/>"
1643 "</FORM>");
1645 WebFrame* frame = GetMainFrame();
1646 ASSERT_NE(nullptr, frame);
1648 WebFormControlElement element = GetFormControlElementById("hidden");
1649 FormFieldData result;
1650 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1652 FormFieldData expected;
1653 expected.max_length = 0;
1655 expected.name = ASCIIToUTF16("hidden");
1656 expected.form_control_type = "hidden";
1657 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1659 element = GetFormControlElementById("submit");
1660 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1661 expected.name = ASCIIToUTF16("submit");
1662 expected.form_control_type = "submit";
1663 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1666 // We should be able to extract password fields.
1667 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) {
1668 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1669 " <INPUT type='password' id='password' value='secret'/>"
1670 "</FORM>");
1672 WebFrame* frame = GetMainFrame();
1673 ASSERT_NE(nullptr, frame);
1675 WebFormControlElement element = GetFormControlElementById("password");
1676 FormFieldData result;
1677 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1679 FormFieldData expected;
1680 expected.max_length = WebInputElement::defaultMaxLength();
1681 expected.name = ASCIIToUTF16("password");
1682 expected.form_control_type = "password";
1683 expected.value = ASCIIToUTF16("secret");
1684 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1687 // We should be able to extract the autocompletetype attribute.
1688 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompletetype) {
1689 std::string html =
1690 "<INPUT type='text' id='absent'/>"
1691 "<INPUT type='text' id='empty' autocomplete=''/>"
1692 "<INPUT type='text' id='off' autocomplete='off'/>"
1693 "<INPUT type='text' id='regular' autocomplete='email'/>"
1694 "<INPUT type='text' id='multi-valued' "
1695 " autocomplete='billing email'/>"
1696 "<INPUT type='text' id='experimental' x-autocompletetype='email'/>"
1697 "<INPUT type='month' id='month' autocomplete='cc-exp'/>"
1698 "<SELECT id='select' autocomplete='state'/>"
1699 " <OPTION value='CA'>California</OPTION>"
1700 " <OPTION value='TX'>Texas</OPTION>"
1701 "</SELECT>"
1702 "<TEXTAREA id='textarea' autocomplete='street-address'>"
1703 " Some multi-"
1704 " lined value"
1705 "</TEXTAREA>";
1706 html +=
1707 "<INPUT type='text' id='malicious' autocomplete='" +
1708 std::string(10000, 'x') + "'/>";
1709 LoadHTML(html.c_str());
1711 WebFrame* frame = GetMainFrame();
1712 ASSERT_NE(nullptr, frame);
1714 struct TestCase {
1715 const std::string element_id;
1716 const std::string form_control_type;
1717 const std::string autocomplete_attribute;
1719 TestCase test_cases[] = {
1720 // An absent attribute is equivalent to an empty one.
1721 { "absent", "text", "" },
1722 // Make sure there are no issues parsing an empty attribute.
1723 { "empty", "text", "" },
1724 // Make sure there are no issues parsing an attribute value that isn't a
1725 // type hint.
1726 { "off", "text", "off" },
1727 // Common case: exactly one type specified.
1728 { "regular", "text", "email" },
1729 // Verify that we correctly extract multiple tokens as well.
1730 { "multi-valued", "text", "billing email" },
1731 // Verify that <input type="month"> fields are supported.
1732 { "month", "month", "cc-exp" },
1733 // We previously extracted this data from the experimental
1734 // 'x-autocompletetype' attribute. Now that the field type hints are part
1735 // of the spec under the autocomplete attribute, we no longer support the
1736 // experimental version.
1737 { "experimental", "text", "" },
1738 // <select> elements should behave no differently from text fields here.
1739 { "select", "select-one", "state" },
1740 // <textarea> elements should also behave no differently from text fields.
1741 { "textarea", "textarea", "street-address" },
1742 // Very long attribute values should be replaced by a default string, to
1743 // prevent malicious websites from DOSing the browser process.
1744 { "malicious", "text", "x-max-data-length-exceeded" },
1747 WebDocument document = frame->document();
1748 for (size_t i = 0; i < arraysize(test_cases); ++i) {
1749 WebFormControlElement element =
1750 GetFormControlElementById(ASCIIToUTF16(test_cases[i].element_id));
1751 FormFieldData result;
1752 WebFormControlElementToFormField(element, EXTRACT_NONE, &result);
1754 FormFieldData expected;
1755 expected.name = ASCIIToUTF16(test_cases[i].element_id);
1756 expected.form_control_type = test_cases[i].form_control_type;
1757 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute;
1758 if (test_cases[i].form_control_type == "text")
1759 expected.max_length = WebInputElement::defaultMaxLength();
1760 else
1761 expected.max_length = 0;
1763 SCOPED_TRACE(test_cases[i].element_id);
1764 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
1768 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectStyle) {
1769 LoadHTML("<STYLE>input{direction:rtl}</STYLE>"
1770 "<FORM>"
1771 " <INPUT type='text' id='element'>"
1772 "</FORM>");
1774 WebFrame* frame = GetMainFrame();
1775 ASSERT_NE(nullptr, frame);
1777 WebFormControlElement element = GetFormControlElementById("element");
1779 FormFieldData result;
1780 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1781 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
1784 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectDIRAttribute) {
1785 LoadHTML("<FORM>"
1786 " <INPUT dir='rtl' type='text' id='element'/>"
1787 "</FORM>");
1789 WebFrame* frame = GetMainFrame();
1790 ASSERT_NE(nullptr, frame);
1792 WebFormControlElement element = GetFormControlElementById("element");
1794 FormFieldData result;
1795 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1796 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
1799 TEST_F(FormAutofillTest, DetectTextDirectionFromParentStyle) {
1800 LoadHTML("<STYLE>form{direction:rtl}</STYLE>"
1801 "<FORM>"
1802 " <INPUT type='text' id='element'/>"
1803 "</FORM>");
1805 WebFrame* frame = GetMainFrame();
1806 ASSERT_NE(nullptr, frame);
1808 WebFormControlElement element = GetFormControlElementById("element");
1810 FormFieldData result;
1811 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1812 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
1815 TEST_F(FormAutofillTest, DetectTextDirectionFromParentDIRAttribute) {
1816 LoadHTML("<FORM dir='rtl'>"
1817 " <INPUT type='text' id='element'/>"
1818 "</FORM>");
1820 WebFrame* frame = GetMainFrame();
1821 ASSERT_NE(nullptr, frame);
1823 WebFormControlElement element = GetFormControlElementById("element");
1825 FormFieldData result;
1826 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1827 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
1830 TEST_F(FormAutofillTest, DetectTextDirectionWhenStyleAndDIRAttributMixed) {
1831 LoadHTML("<STYLE>input{direction:ltr}</STYLE>"
1832 "<FORM dir='rtl'>"
1833 " <INPUT type='text' id='element'/>"
1834 "</FORM>");
1836 WebFrame* frame = GetMainFrame();
1837 ASSERT_NE(nullptr, frame);
1839 WebFormControlElement element = GetFormControlElementById("element");
1841 FormFieldData result;
1842 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1843 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
1846 TEST_F(FormAutofillTest,
1847 DetectTextDirectionWhenParentHasBothDIRAttributeAndStyle) {
1848 LoadHTML("<STYLE>form{direction:ltr}</STYLE>"
1849 "<FORM dir='rtl'>"
1850 " <INPUT type='text' id='element'/>"
1851 "</FORM>");
1853 WebFrame* frame = GetMainFrame();
1854 ASSERT_NE(nullptr, frame);
1856 WebFormControlElement element = GetFormControlElementById("element");
1858 FormFieldData result;
1859 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1860 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
1863 TEST_F(FormAutofillTest, DetectTextDirectionWhenAncestorHasInlineStyle) {
1864 LoadHTML("<FORM style='direction:ltr'>"
1865 " <SPAN dir='rtl'>"
1866 " <INPUT type='text' id='element'/>"
1867 " </SPAN>"
1868 "</FORM>");
1870 WebFrame* frame = GetMainFrame();
1871 ASSERT_NE(nullptr, frame);
1873 WebFormControlElement element = GetFormControlElementById("element");
1875 FormFieldData result;
1876 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
1877 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
1880 TEST_F(FormAutofillTest, WebFormElementToFormData) {
1881 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
1882 " <LABEL for='firstname'>First name:</LABEL>"
1883 " <INPUT type='text' id='firstname' value='John'/>"
1884 " <LABEL for='lastname'>Last name:</LABEL>"
1885 " <INPUT type='text' id='lastname' value='Smith'/>"
1886 " <LABEL for='street-address'>Address:</LABEL>"
1887 " <TEXTAREA id='street-address'>"
1888 "123 Fantasy Ln.&#10;"
1889 "Apt. 42"
1890 "</TEXTAREA>"
1891 " <LABEL for='state'>State:</LABEL>"
1892 " <SELECT id='state'/>"
1893 " <OPTION value='CA'>California</OPTION>"
1894 " <OPTION value='TX'>Texas</OPTION>"
1895 " </SELECT>"
1896 " <LABEL for='password'>Password:</LABEL>"
1897 " <INPUT type='password' id='password' value='secret'/>"
1898 " <LABEL for='month'>Card expiration:</LABEL>"
1899 " <INPUT type='month' id='month' value='2011-12'/>"
1900 " <INPUT type='submit' name='reply-send' value='Send'/>"
1901 // The below inputs should be ignored
1902 " <LABEL for='notvisible'>Hidden:</LABEL>"
1903 " <INPUT type='hidden' id='notvisible' value='apple'/>"
1904 "</FORM>");
1906 WebFrame* frame = GetMainFrame();
1907 ASSERT_NE(nullptr, frame);
1909 WebVector<WebFormElement> forms;
1910 frame->document().forms(forms);
1911 ASSERT_EQ(1U, forms.size());
1913 WebInputElement input_element = GetInputElementById("firstname");
1915 FormData form;
1916 FormFieldData field;
1917 EXPECT_TRUE(WebFormElementToFormData(forms[0],
1918 input_element,
1919 EXTRACT_VALUE,
1920 &form,
1921 &field));
1922 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1923 EXPECT_EQ(GURL(frame->document().url()), form.origin);
1924 EXPECT_FALSE(form.origin.is_empty());
1925 EXPECT_EQ(GURL("http://cnn.com"), form.action);
1927 const std::vector<FormFieldData>& fields = form.fields;
1928 ASSERT_EQ(6U, fields.size());
1930 FormFieldData expected;
1931 expected.name = ASCIIToUTF16("firstname");
1932 expected.value = ASCIIToUTF16("John");
1933 expected.label = ASCIIToUTF16("First name:");
1934 expected.form_control_type = "text";
1935 expected.max_length = WebInputElement::defaultMaxLength();
1936 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1938 expected.name = ASCIIToUTF16("lastname");
1939 expected.value = ASCIIToUTF16("Smith");
1940 expected.label = ASCIIToUTF16("Last name:");
1941 expected.form_control_type = "text";
1942 expected.max_length = WebInputElement::defaultMaxLength();
1943 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1945 expected.name = ASCIIToUTF16("street-address");
1946 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1947 expected.label = ASCIIToUTF16("Address:");
1948 expected.form_control_type = "textarea";
1949 expected.max_length = 0;
1950 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1952 expected.name = ASCIIToUTF16("state");
1953 expected.value = ASCIIToUTF16("CA");
1954 expected.label = ASCIIToUTF16("State:");
1955 expected.form_control_type = "select-one";
1956 expected.max_length = 0;
1957 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1959 expected.name = ASCIIToUTF16("password");
1960 expected.value = ASCIIToUTF16("secret");
1961 expected.label = ASCIIToUTF16("Password:");
1962 expected.form_control_type = "password";
1963 expected.max_length = WebInputElement::defaultMaxLength();
1964 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
1966 expected.name = ASCIIToUTF16("month");
1967 expected.value = ASCIIToUTF16("2011-12");
1968 expected.label = ASCIIToUTF16("Card expiration:");
1969 expected.form_control_type = "month";
1970 expected.max_length = 0;
1971 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
1974 // We should not be able to serialize a form with too many fillable fields.
1975 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) {
1976 std::string html =
1977 "<FORM name='TestForm' action='http://cnn.com' method='post'>";
1978 for (size_t i = 0; i < (kMaxParseableFields + 1); ++i) {
1979 html += "<INPUT type='text'/>";
1981 html += "</FORM>";
1982 LoadHTML(html.c_str());
1984 WebFrame* frame = GetMainFrame();
1985 ASSERT_NE(nullptr, frame);
1987 WebVector<WebFormElement> forms;
1988 frame->document().forms(forms);
1989 ASSERT_EQ(1U, forms.size());
1991 WebInputElement input_element = GetInputElementById("firstname");
1993 FormData form;
1994 FormFieldData field;
1995 EXPECT_FALSE(WebFormElementToFormData(forms[0],
1996 input_element,
1997 EXTRACT_VALUE,
1998 &form,
1999 &field));
2002 TEST_F(FormAutofillTest, ExtractForms) {
2003 ExpectJohnSmithLabels(
2004 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2005 " First name: <INPUT type='text' id='firstname' value='John'/>"
2006 " Last name: <INPUT type='text' id='lastname' value='Smith'/>"
2007 " Email: <INPUT type='text' id='email' value='john@example.com'/>"
2008 " <INPUT type='submit' name='reply-send' value='Send'/>"
2009 "</FORM>");
2012 TEST_F(FormAutofillTest, ExtractMultipleForms) {
2013 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2014 " <INPUT type='text' id='firstname' value='John'/>"
2015 " <INPUT type='text' id='lastname' value='Smith'/>"
2016 " <INPUT type='text' id='email' value='john@example.com'/>"
2017 " <INPUT type='submit' name='reply-send' value='Send'/>"
2018 "</FORM>"
2019 "<FORM name='TestForm2' action='http://zoo.com' method='post'>"
2020 " <INPUT type='text' id='firstname' value='Jack'/>"
2021 " <INPUT type='text' id='lastname' value='Adams'/>"
2022 " <INPUT type='text' id='email' value='jack@example.com'/>"
2023 " <INPUT type='submit' name='reply-send' value='Send'/>"
2024 "</FORM>");
2026 WebFrame* web_frame = GetMainFrame();
2027 ASSERT_NE(nullptr, web_frame);
2029 FormCache form_cache(*web_frame);
2030 std::vector<FormData> forms = form_cache.ExtractNewForms();
2031 ASSERT_EQ(2U, forms.size());
2033 // First form.
2034 const FormData& form = forms[0];
2035 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2036 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2037 EXPECT_FALSE(form.origin.is_empty());
2038 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2040 const std::vector<FormFieldData>& fields = form.fields;
2041 ASSERT_EQ(3U, fields.size());
2043 FormFieldData expected;
2044 expected.form_control_type = "text";
2045 expected.max_length = WebInputElement::defaultMaxLength();
2047 expected.name = ASCIIToUTF16("firstname");
2048 expected.value = ASCIIToUTF16("John");
2049 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2051 expected.name = ASCIIToUTF16("lastname");
2052 expected.value = ASCIIToUTF16("Smith");
2053 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2055 expected.name = ASCIIToUTF16("email");
2056 expected.value = ASCIIToUTF16("john@example.com");
2057 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2059 // Second form.
2060 const FormData& form2 = forms[1];
2061 EXPECT_EQ(ASCIIToUTF16("TestForm2"), form2.name);
2062 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2063 EXPECT_FALSE(form.origin.is_empty());
2064 EXPECT_EQ(GURL("http://zoo.com"), form2.action);
2066 const std::vector<FormFieldData>& fields2 = form2.fields;
2067 ASSERT_EQ(3U, fields2.size());
2069 expected.name = ASCIIToUTF16("firstname");
2070 expected.value = ASCIIToUTF16("Jack");
2071 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2073 expected.name = ASCIIToUTF16("lastname");
2074 expected.value = ASCIIToUTF16("Adams");
2075 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2077 expected.name = ASCIIToUTF16("email");
2078 expected.value = ASCIIToUTF16("jack@example.com");
2079 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2082 TEST_F(FormAutofillTest, OnlyExtractNewForms) {
2083 LoadHTML(
2084 "<FORM id='testform' action='http://cnn.com' method='post'>"
2085 " <INPUT type='text' id='firstname' value='John'/>"
2086 " <INPUT type='text' id='lastname' value='Smith'/>"
2087 " <INPUT type='text' id='email' value='john@example.com'/>"
2088 " <INPUT type='submit' name='reply-send' value='Send'/>"
2089 "</FORM>");
2091 WebFrame* web_frame = GetMainFrame();
2092 ASSERT_NE(nullptr, web_frame);
2094 FormCache form_cache(*web_frame);
2095 std::vector<FormData> forms = form_cache.ExtractNewForms();
2096 ASSERT_EQ(1U, forms.size());
2098 // Second call should give nothing as there are no new forms.
2099 forms = form_cache.ExtractNewForms();
2100 ASSERT_TRUE(forms.empty());
2102 // Append to the current form will re-extract.
2103 ExecuteJavaScript(
2104 "var newInput = document.createElement('input');"
2105 "newInput.setAttribute('type', 'text');"
2106 "newInput.setAttribute('id', 'telephone');"
2107 "newInput.value = '12345';"
2108 "document.getElementById('testform').appendChild(newInput);");
2109 msg_loop_.RunUntilIdle();
2111 forms = form_cache.ExtractNewForms();
2112 ASSERT_EQ(1U, forms.size());
2114 const std::vector<FormFieldData>& fields = forms[0].fields;
2115 ASSERT_EQ(4U, fields.size());
2117 FormFieldData expected;
2118 expected.form_control_type = "text";
2119 expected.max_length = WebInputElement::defaultMaxLength();
2121 expected.name = ASCIIToUTF16("firstname");
2122 expected.value = ASCIIToUTF16("John");
2123 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2125 expected.name = ASCIIToUTF16("lastname");
2126 expected.value = ASCIIToUTF16("Smith");
2127 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2129 expected.name = ASCIIToUTF16("email");
2130 expected.value = ASCIIToUTF16("john@example.com");
2131 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2133 expected.name = ASCIIToUTF16("telephone");
2134 expected.value = ASCIIToUTF16("12345");
2135 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
2137 forms.clear();
2139 // Completely new form will also be extracted.
2140 ExecuteJavaScript(
2141 "var newForm=document.createElement('form');"
2142 "newForm.id='new_testform';"
2143 "newForm.action='http://google.com';"
2144 "newForm.method='post';"
2145 "var newFirstname=document.createElement('input');"
2146 "newFirstname.setAttribute('type', 'text');"
2147 "newFirstname.setAttribute('id', 'second_firstname');"
2148 "newFirstname.value = 'Bob';"
2149 "var newLastname=document.createElement('input');"
2150 "newLastname.setAttribute('type', 'text');"
2151 "newLastname.setAttribute('id', 'second_lastname');"
2152 "newLastname.value = 'Hope';"
2153 "var newEmail=document.createElement('input');"
2154 "newEmail.setAttribute('type', 'text');"
2155 "newEmail.setAttribute('id', 'second_email');"
2156 "newEmail.value = 'bobhope@example.com';"
2157 "newForm.appendChild(newFirstname);"
2158 "newForm.appendChild(newLastname);"
2159 "newForm.appendChild(newEmail);"
2160 "document.body.appendChild(newForm);");
2161 msg_loop_.RunUntilIdle();
2163 web_frame = GetMainFrame();
2164 forms = form_cache.ExtractNewForms();
2165 ASSERT_EQ(1U, forms.size());
2167 const std::vector<FormFieldData>& fields2 = forms[0].fields;
2168 ASSERT_EQ(3U, fields2.size());
2170 expected.name = ASCIIToUTF16("second_firstname");
2171 expected.value = ASCIIToUTF16("Bob");
2172 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2174 expected.name = ASCIIToUTF16("second_lastname");
2175 expected.value = ASCIIToUTF16("Hope");
2176 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2178 expected.name = ASCIIToUTF16("second_email");
2179 expected.value = ASCIIToUTF16("bobhope@example.com");
2180 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2183 // We should not extract a form if it has too few fillable fields.
2184 TEST_F(FormAutofillTest, ExtractFormsTooFewFields) {
2185 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2186 " <INPUT type='text' id='firstname' value='John'/>"
2187 " <INPUT type='text' id='lastname' value='Smith'/>"
2188 " <INPUT type='submit' name='reply-send' value='Send'/>"
2189 "</FORM>");
2191 WebFrame* web_frame = GetMainFrame();
2192 ASSERT_NE(nullptr, web_frame);
2194 FormCache form_cache(*web_frame);
2195 std::vector<FormData> forms = form_cache.ExtractNewForms();
2196 ASSERT_TRUE(forms.empty());
2199 // We should not report additional forms for empty forms.
2200 TEST_F(FormAutofillTest, ExtractFormsSkippedForms) {
2201 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2202 " <INPUT type='text' id='firstname' value='John'/>"
2203 " <INPUT type='text' id='lastname' value='Smith'/>"
2204 "</FORM>");
2206 WebFrame* web_frame = GetMainFrame();
2207 ASSERT_NE(nullptr, web_frame);
2209 FormCache form_cache(*web_frame);
2210 std::vector<FormData> forms = form_cache.ExtractNewForms();
2211 ASSERT_TRUE(forms.empty());
2214 // We should not report additional forms for empty forms.
2215 TEST_F(FormAutofillTest, ExtractFormsNoFields) {
2216 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2217 "</FORM>");
2219 WebFrame* web_frame = GetMainFrame();
2220 ASSERT_NE(nullptr, web_frame);
2222 FormCache form_cache(*web_frame);
2223 std::vector<FormData> forms = form_cache.ExtractNewForms();
2224 ASSERT_TRUE(forms.empty());
2227 // We should not extract a form if it has too few fillable fields.
2228 // Make sure radio and checkbox fields don't count.
2229 TEST_F(FormAutofillTest, ExtractFormsTooFewFieldsSkipsCheckable) {
2230 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2231 " <INPUT type='text' id='firstname' value='John'/>"
2232 " <INPUT type='text' id='lastname' value='Smith'/>"
2233 " <INPUT type='radio' id='a_radio' value='0'/>"
2234 " <INPUT type='checkbox' id='a_check' value='1'/>"
2235 " <INPUT type='submit' name='reply-send' value='Send'/>"
2236 "</FORM>");
2238 WebFrame* web_frame = GetMainFrame();
2239 ASSERT_NE(nullptr, web_frame);
2241 FormCache form_cache(*web_frame);
2242 std::vector<FormData> forms = form_cache.ExtractNewForms();
2243 ASSERT_TRUE(forms.empty());
2246 TEST_F(FormAutofillTest, WebFormElementToFormDataAutocomplete) {
2248 // Form is still Autofill-able despite autocomplete=off.
2249 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'"
2250 " autocomplete=off>"
2251 " <INPUT type='text' id='firstname' value='John'/>"
2252 " <INPUT type='text' id='lastname' value='Smith'/>"
2253 " <INPUT type='text' id='email' value='john@example.com'/>"
2254 " <INPUT type='submit' name='reply-send' value='Send'/>"
2255 "</FORM>");
2257 WebFrame* web_frame = GetMainFrame();
2258 ASSERT_NE(nullptr, web_frame);
2260 WebVector<WebFormElement> web_forms;
2261 web_frame->document().forms(web_forms);
2262 ASSERT_EQ(1U, web_forms.size());
2263 WebFormElement web_form = web_forms[0];
2265 FormData form;
2266 EXPECT_TRUE(WebFormElementToFormData(web_form, WebFormControlElement(),
2267 EXTRACT_NONE, &form, nullptr));
2271 TEST_F(FormAutofillTest, FindFormForInputElement) {
2272 TestFindFormForInputElement(
2273 "<FORM name='TestForm' action='http://buh.com' method='post'>"
2274 " <INPUT type='text' id='firstname' value='John'/>"
2275 " <INPUT type='text' id='lastname' value='Smith'/>"
2276 " <INPUT type='text' id='email' value='john@example.com'"
2277 "autocomplete='off' />"
2278 " <INPUT type='text' id='phone' value='1.800.555.1234'/>"
2279 " <INPUT type='submit' name='reply-send' value='Send'/>"
2280 "</FORM>",
2281 false);
2284 TEST_F(FormAutofillTest, FindFormForInputElementForUnownedForm) {
2285 TestFindFormForInputElement(
2286 "<HEAD><TITLE>delivery recipient</TITLE></HEAD>"
2287 "<INPUT type='text' id='firstname' value='John'/>"
2288 "<INPUT type='text' id='lastname' value='Smith'/>"
2289 "<INPUT type='text' id='email' value='john@example.com'"
2290 "autocomplete='off' />"
2291 "<INPUT type='text' id='phone' value='1.800.555.1234'/>"
2292 "<INPUT type='submit' name='reply-send' value='Send'/>",
2293 true);
2296 TEST_F(FormAutofillTest, FindFormForTextAreaElement) {
2297 TestFindFormForTextAreaElement(
2298 "<FORM name='TestForm' action='http://buh.com' method='post'>"
2299 " <INPUT type='text' id='firstname' value='John'/>"
2300 " <INPUT type='text' id='lastname' value='Smith'/>"
2301 " <INPUT type='text' id='email' value='john@example.com'"
2302 "autocomplete='off' />"
2303 " <TEXTAREA id='street-address'>"
2304 "123 Fantasy Ln.&#10;"
2305 "Apt. 42"
2306 "</TEXTAREA>"
2307 " <INPUT type='submit' name='reply-send' value='Send'/>"
2308 "</FORM>",
2309 false);
2312 TEST_F(FormAutofillTest, FindFormForTextAreaElementForUnownedForm) {
2313 TestFindFormForTextAreaElement(
2314 "<HEAD><TITLE>delivery address</TITLE></HEAD>"
2315 "<INPUT type='text' id='firstname' value='John'/>"
2316 "<INPUT type='text' id='lastname' value='Smith'/>"
2317 "<INPUT type='text' id='email' value='john@example.com'"
2318 "autocomplete='off' />"
2319 "<TEXTAREA id='street-address'>"
2320 "123 Fantasy Ln.&#10;"
2321 "Apt. 42"
2322 "</TEXTAREA>"
2323 "<INPUT type='submit' name='reply-send' value='Send'/>",
2324 true);
2327 // Test regular FillForm function.
2328 TEST_F(FormAutofillTest, FillForm) {
2329 TestFillForm(kFormHtml, false);
2332 TEST_F(FormAutofillTest, FillFormForUnownedForm) {
2333 TestFillForm(kUnownedFormHtml, true);
2336 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) {
2337 static const AutofillFieldCase field_cases[] = {
2338 // fields: form_control_type, name, initial_value, autocomplete_attribute,
2339 // should_be_autofilled, autofill_value, expected_value
2341 // Regular empty fields (firstname & lastname) should be autofilled.
2342 {"text",
2343 "firstname",
2346 true,
2347 "filled firstname",
2348 "filled firstname"},
2349 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
2350 // hidden fields should not be extracted to form_data.
2351 // Non empty fields should be overriden.
2352 {"text",
2353 "notempty",
2354 "Hi",
2356 true,
2357 "filled notempty",
2358 "filled notempty"},
2359 {"text",
2360 "noautocomplete",
2362 "off",
2363 true,
2364 "filled noautocomplete",
2365 "filled noautocomplete"},
2366 // Disabled fields should not be autofilled.
2367 {"text", "notenabled", "", "", false, "filled notenabled", ""},
2368 // Readonly fields should not be autofilled.
2369 {"text", "readonly", "", "", false, "filled readonly", ""},
2370 // Fields with "visibility: hidden" should also be autofilled.
2371 {"text",
2372 "invisible",
2375 true,
2376 "filled invisible",
2377 "filled invisible"},
2378 // Fields with "display:none" should also be autofilled.
2379 {"text",
2380 "displaynone",
2383 true,
2384 "filled displaynone",
2385 "filled displaynone"},
2386 // Regular <input type="month"> should be autofilled.
2387 {"month", "month", "", "", true, "2017-11", "2017-11"},
2388 // Non-empty <input type="month"> should be overridden.
2389 {"month", "month-nonempty", "2011-12", "", true, "2017-11", "2017-11"},
2390 // Regular select fields should be autofilled.
2391 {"select-one", "select", "", "", true, "TX", "TX"},
2392 // Select fields should be autofilled even if they already have a
2393 // non-empty value.
2394 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
2395 // Select fields should not be autofilled if no new value is passed from
2396 // autofill profile. The existing value should not be overriden.
2397 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
2398 // Regular textarea elements should be autofilled.
2399 {"textarea",
2400 "textarea",
2403 true,
2404 "some multi-\nline value",
2405 "some multi-\nline value"},
2406 // Nonempty textarea elements should be overridden.
2407 {"textarea",
2408 "textarea-nonempty",
2409 "Go\naway!",
2411 true,
2412 "some multi-\nline value",
2413 "some multi-\nline value"},
2415 TestFormFillFunctions(kFormHtml, false, field_cases, arraysize(field_cases),
2416 &FillFormIncludingNonFocusableElementsWrapper,
2417 &GetValueWrapper);
2420 TEST_F(FormAutofillTest, PreviewForm) {
2421 TestPreviewForm(kFormHtml, false);
2424 TEST_F(FormAutofillTest, PreviewFormForUnownedForm) {
2425 TestPreviewForm(kUnownedFormHtml, true);
2428 TEST_F(FormAutofillTest, Labels) {
2429 ExpectJohnSmithLabels(
2430 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2431 " <LABEL for='firstname'> First name: </LABEL>"
2432 " <INPUT type='text' id='firstname' value='John'/>"
2433 " <LABEL for='lastname'> Last name: </LABEL>"
2434 " <INPUT type='text' id='lastname' value='Smith'/>"
2435 " <LABEL for='email'> Email: </LABEL>"
2436 " <INPUT type='text' id='email' value='john@example.com'/>"
2437 " <INPUT type='submit' name='reply-send' value='Send'/>"
2438 "</FORM>");
2441 TEST_F(FormAutofillTest, LabelsWithSpans) {
2442 ExpectJohnSmithLabels(
2443 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2444 " <LABEL for='firstname'><span>First name: </span></LABEL>"
2445 " <INPUT type='text' id='firstname' value='John'/>"
2446 " <LABEL for='lastname'><span>Last name: </span></LABEL>"
2447 " <INPUT type='text' id='lastname' value='Smith'/>"
2448 " <LABEL for='email'><span>Email: </span></LABEL>"
2449 " <INPUT type='text' id='email' value='john@example.com'/>"
2450 " <INPUT type='submit' name='reply-send' value='Send'/>"
2451 "</FORM>");
2454 // This test is different from FormAutofillTest.Labels in that the label
2455 // elements for= attribute is set to the name of the form control element it is
2456 // a label for instead of the id of the form control element. This is invalid
2457 // because the for= attribute must be set to the id of the form control element;
2458 // however, current label parsing code will extract the text from the previous
2459 // label element and apply it to the following input field.
2460 TEST_F(FormAutofillTest, InvalidLabels) {
2461 ExpectJohnSmithLabels(
2462 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2463 " <LABEL for='firstname'> First name: </LABEL>"
2464 " <INPUT type='text' name='firstname' value='John'/>"
2465 " <LABEL for='lastname'> Last name: </LABEL>"
2466 " <INPUT type='text' name='lastname' value='Smith'/>"
2467 " <LABEL for='email'> Email: </LABEL>"
2468 " <INPUT type='text' name='email' value='john@example.com'/>"
2469 " <INPUT type='submit' name='reply-send' value='Send'/>"
2470 "</FORM>");
2473 // This test has three form control elements, only one of which has a label
2474 // element associated with it.
2475 TEST_F(FormAutofillTest, OneLabelElement) {
2476 ExpectJohnSmithLabels(
2477 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2478 " First name:"
2479 " <INPUT type='text' id='firstname' value='John'/>"
2480 " <LABEL for='lastname'>Last name: </LABEL>"
2481 " <INPUT type='text' id='lastname' value='Smith'/>"
2482 " Email:"
2483 " <INPUT type='text' id='email' value='john@example.com'/>"
2484 " <INPUT type='submit' name='reply-send' value='Send'/>"
2485 "</FORM>");
2488 TEST_F(FormAutofillTest, LabelsInferredFromText) {
2489 ExpectJohnSmithLabels(
2490 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2491 " First name:"
2492 " <INPUT type='text' id='firstname' value='John'/>"
2493 " Last name:"
2494 " <INPUT type='text' id='lastname' value='Smith'/>"
2495 " Email:"
2496 " <INPUT type='text' id='email' value='john@example.com'/>"
2497 " <INPUT type='submit' name='reply-send' value='Send'/>"
2498 "</FORM>");
2501 TEST_F(FormAutofillTest, LabelsInferredFromParagraph) {
2502 ExpectJohnSmithLabels(
2503 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2504 " <P>First name:</P><INPUT type='text' "
2505 " id='firstname' value='John'/>"
2506 " <P>Last name:</P>"
2507 " <INPUT type='text' id='lastname' value='Smith'/>"
2508 " <P>Email:</P>"
2509 " <INPUT type='text' id='email' value='john@example.com'/>"
2510 " <INPUT type='submit' name='reply-send' value='Send'/>"
2511 "</FORM>");
2514 TEST_F(FormAutofillTest, LabelsInferredFromBold) {
2515 ExpectJohnSmithLabels(
2516 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2517 " <B>First name:</B><INPUT type='text' "
2518 " id='firstname' value='John'/>"
2519 " <B>Last name:</B>"
2520 " <INPUT type='text' id='lastname' value='Smith'/>"
2521 " <B>Email:</B>"
2522 " <INPUT type='text' id='email' value='john@example.com'/>"
2523 " <INPUT type='submit' name='reply-send' value='Send'/>"
2524 "</FORM>");
2527 TEST_F(FormAutofillTest, LabelsInferredPriorToImgOrBr) {
2528 ExpectJohnSmithLabels(
2529 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2530 " First name:<IMG/><INPUT type='text' "
2531 " id='firstname' value='John'/>"
2532 " Last name:<IMG/>"
2533 " <INPUT type='text' id='lastname' value='Smith'/>"
2534 " Email:<BR/>"
2535 " <INPUT type='text' id='email' value='john@example.com'/>"
2536 " <INPUT type='submit' name='reply-send' value='Send'/>"
2537 "</FORM>");
2540 TEST_F(FormAutofillTest, LabelsInferredFromTableCell) {
2541 ExpectJohnSmithLabels(
2542 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2543 "<TABLE>"
2544 " <TR>"
2545 " <TD>First name:</TD>"
2546 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
2547 " </TR>"
2548 " <TR>"
2549 " <TD>Last name:</TD>"
2550 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
2551 " </TR>"
2552 " <TR>"
2553 " <TD>Email:</TD>"
2554 " <TD><INPUT type='text' id='email'"
2555 " value='john@example.com'/></TD>"
2556 " </TR>"
2557 " <TR>"
2558 " <TD></TD>"
2559 " <TD>"
2560 " <INPUT type='submit' name='reply-send' value='Send'/>"
2561 " </TD>"
2562 " </TR>"
2563 "</TABLE>"
2564 "</FORM>");
2567 TEST_F(FormAutofillTest, LabelsInferredFromTableCellTH) {
2568 ExpectJohnSmithLabels(
2569 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2570 "<TABLE>"
2571 " <TR>"
2572 " <TH>First name:</TH>"
2573 " <TD><INPUT type='text' id='firstname' value='John'/></TD>"
2574 " </TR>"
2575 " <TR>"
2576 " <TH>Last name:</TH>"
2577 " <TD><INPUT type='text' id='lastname' value='Smith'/></TD>"
2578 " </TR>"
2579 " <TR>"
2580 " <TH>Email:</TH>"
2581 " <TD><INPUT type='text' id='email'"
2582 " value='john@example.com'/></TD>"
2583 " </TR>"
2584 " <TR>"
2585 " <TD></TD>"
2586 " <TD>"
2587 " <INPUT type='submit' name='reply-send' value='Send'/>"
2588 " </TD>"
2589 " </TR>"
2590 "</TABLE>"
2591 "</FORM>");
2594 TEST_F(FormAutofillTest, LabelsInferredFromTableCellNested) {
2595 std::vector<base::string16> labels, names, values;
2597 labels.push_back(ASCIIToUTF16("First name: Bogus"));
2598 names.push_back(ASCIIToUTF16("firstname"));
2599 values.push_back(ASCIIToUTF16("John"));
2601 labels.push_back(ASCIIToUTF16("Last name:"));
2602 names.push_back(ASCIIToUTF16("lastname"));
2603 values.push_back(ASCIIToUTF16("Smith"));
2605 labels.push_back(ASCIIToUTF16("Email:"));
2606 names.push_back(ASCIIToUTF16("email"));
2607 values.push_back(ASCIIToUTF16("john@example.com"));
2609 ExpectLabels(
2610 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2611 "<TABLE>"
2612 " <TR>"
2613 " <TD>"
2614 " <FONT>"
2615 " First name:"
2616 " </FONT>"
2617 " <FONT>"
2618 " Bogus"
2619 " </FONT>"
2620 " </TD>"
2621 " <TD>"
2622 " <FONT>"
2623 " <INPUT type='text' id='firstname' value='John'/>"
2624 " </FONT>"
2625 " </TD>"
2626 " </TR>"
2627 " <TR>"
2628 " <TD>"
2629 " <FONT>"
2630 " Last name:"
2631 " </FONT>"
2632 " </TD>"
2633 " <TD>"
2634 " <FONT>"
2635 " <INPUT type='text' id='lastname' value='Smith'/>"
2636 " </FONT>"
2637 " </TD>"
2638 " </TR>"
2639 " <TR>"
2640 " <TD>"
2641 " <FONT>"
2642 " Email:"
2643 " </FONT>"
2644 " </TD>"
2645 " <TD>"
2646 " <FONT>"
2647 " <INPUT type='text' id='email' value='john@example.com'/>"
2648 " </FONT>"
2649 " </TD>"
2650 " </TR>"
2651 " <TR>"
2652 " <TD></TD>"
2653 " <TD>"
2654 " <INPUT type='submit' name='reply-send' value='Send'/>"
2655 " </TD>"
2656 " </TR>"
2657 "</TABLE>"
2658 "</FORM>",
2659 labels, names, values);
2662 TEST_F(FormAutofillTest, LabelsInferredFromTableEmptyTDs) {
2663 std::vector<base::string16> labels, names, values;
2665 labels.push_back(ASCIIToUTF16("* First Name"));
2666 names.push_back(ASCIIToUTF16("firstname"));
2667 values.push_back(ASCIIToUTF16("John"));
2669 labels.push_back(ASCIIToUTF16("* Last Name"));
2670 names.push_back(ASCIIToUTF16("lastname"));
2671 values.push_back(ASCIIToUTF16("Smith"));
2673 labels.push_back(ASCIIToUTF16("* Email"));
2674 names.push_back(ASCIIToUTF16("email"));
2675 values.push_back(ASCIIToUTF16("john@example.com"));
2677 ExpectLabels(
2678 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2679 "<TABLE>"
2680 " <TR>"
2681 " <TD>"
2682 " <SPAN>*</SPAN>"
2683 " <B>First Name</B>"
2684 " </TD>"
2685 " <TD></TD>"
2686 " <TD>"
2687 " <INPUT type='text' id='firstname' value='John'/>"
2688 " </TD>"
2689 " </TR>"
2690 " <TR>"
2691 " <TD>"
2692 " <SPAN>*</SPAN>"
2693 " <B>Last Name</B>"
2694 " </TD>"
2695 " <TD></TD>"
2696 " <TD>"
2697 " <INPUT type='text' id='lastname' value='Smith'/>"
2698 " </TD>"
2699 " </TR>"
2700 " <TR>"
2701 " <TD>"
2702 " <SPAN>*</SPAN>"
2703 " <B>Email</B>"
2704 " </TD>"
2705 " <TD></TD>"
2706 " <TD>"
2707 " <INPUT type='text' id='email' value='john@example.com'/>"
2708 " </TD>"
2709 " </TR>"
2710 " <TR>"
2711 " <TD></TD>"
2712 " <TD>"
2713 " <INPUT type='submit' name='reply-send' value='Send'/>"
2714 " </TD>"
2715 " </TR>"
2716 "</TABLE>"
2717 "</FORM>",
2718 labels, names, values);
2721 TEST_F(FormAutofillTest, LabelsInferredFromPreviousTD) {
2722 std::vector<base::string16> labels, names, values;
2724 labels.push_back(ASCIIToUTF16("* First Name"));
2725 names.push_back(ASCIIToUTF16("firstname"));
2726 values.push_back(ASCIIToUTF16("John"));
2728 labels.push_back(ASCIIToUTF16("* Last Name"));
2729 names.push_back(ASCIIToUTF16("lastname"));
2730 values.push_back(ASCIIToUTF16("Smith"));
2732 labels.push_back(ASCIIToUTF16("* Email"));
2733 names.push_back(ASCIIToUTF16("email"));
2734 values.push_back(ASCIIToUTF16("john@example.com"));
2736 ExpectLabels(
2737 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2738 "<TABLE>"
2739 " <TR>"
2740 " <TD>* First Name</TD>"
2741 " <TD>"
2742 " Bogus"
2743 " <INPUT type='hidden'/>"
2744 " <INPUT type='text' id='firstname' value='John'/>"
2745 " </TD>"
2746 " </TR>"
2747 " <TR>"
2748 " <TD>* Last Name</TD>"
2749 " <TD>"
2750 " <INPUT type='text' id='lastname' value='Smith'/>"
2751 " </TD>"
2752 " </TR>"
2753 " <TR>"
2754 " <TD>* Email</TD>"
2755 " <TD>"
2756 " <INPUT type='text' id='email' value='john@example.com'/>"
2757 " </TD>"
2758 " </TR>"
2759 " <TR>"
2760 " <TD></TD>"
2761 " <TD>"
2762 " <INPUT type='submit' name='reply-send' value='Send'/>"
2763 " </TD>"
2764 " </TR>"
2765 "</TABLE>"
2766 "</FORM>",
2767 labels, names, values);
2770 // <script>, <noscript> and <option> tags are excluded when the labels are
2771 // inferred.
2772 // Also <!-- comment --> is excluded.
2773 TEST_F(FormAutofillTest, LabelsInferredFromTableWithSpecialElements) {
2774 std::vector<base::string16> labels, names, values;
2775 std::vector<std::string> control_types;
2777 labels.push_back(ASCIIToUTF16("* First Name"));
2778 names.push_back(ASCIIToUTF16("firstname"));
2779 values.push_back(ASCIIToUTF16("John"));
2780 control_types.push_back("text");
2782 labels.push_back(ASCIIToUTF16("* Middle Name"));
2783 names.push_back(ASCIIToUTF16("middlename"));
2784 values.push_back(ASCIIToUTF16("Joe"));
2785 control_types.push_back("text");
2787 labels.push_back(ASCIIToUTF16("* Last Name"));
2788 names.push_back(ASCIIToUTF16("lastname"));
2789 values.push_back(ASCIIToUTF16("Smith"));
2790 control_types.push_back("text");
2792 labels.push_back(ASCIIToUTF16("* Country"));
2793 names.push_back(ASCIIToUTF16("country"));
2794 values.push_back(ASCIIToUTF16("US"));
2795 control_types.push_back("select-one");
2797 labels.push_back(ASCIIToUTF16("* Email"));
2798 names.push_back(ASCIIToUTF16("email"));
2799 values.push_back(ASCIIToUTF16("john@example.com"));
2800 control_types.push_back("text");
2802 ExpectLabelsAndTypes(
2803 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2804 "<TABLE>"
2805 " <TR>"
2806 " <TD>"
2807 " <SPAN>*</SPAN>"
2808 " <B>First Name</B>"
2809 " </TD>"
2810 " <TD>"
2811 " <SCRIPT> <!-- function test() { alert('ignored as label'); } -->"
2812 " </SCRIPT>"
2813 " <INPUT type='text' id='firstname' value='John'/>"
2814 " </TD>"
2815 " </TR>"
2816 " <TR>"
2817 " <TD>"
2818 " <SPAN>*</SPAN>"
2819 " <B>Middle Name</B>"
2820 " </TD>"
2821 " <TD>"
2822 " <NOSCRIPT>"
2823 " <P>Bad</P>"
2824 " </NOSCRIPT>"
2825 " <INPUT type='text' id='middlename' value='Joe'/>"
2826 " </TD>"
2827 " </TR>"
2828 " <TR>"
2829 " <TD>"
2830 " <SPAN>*</SPAN>"
2831 " <B>Last Name</B>"
2832 " </TD>"
2833 " <TD>"
2834 " <INPUT type='text' id='lastname' value='Smith'/>"
2835 " </TD>"
2836 " </TR>"
2837 " <TR>"
2838 " <TD>"
2839 " <SPAN>*</SPAN>"
2840 " <B>Country</B>"
2841 " </TD>"
2842 " <TD>"
2843 " <SELECT id='country'>"
2844 " <OPTION VALUE='US'>The value should be ignored as label."
2845 " </OPTION>"
2846 " <OPTION VALUE='JP'>JAPAN</OPTION>"
2847 " </SELECT>"
2848 " </TD>"
2849 " </TR>"
2850 " <TR>"
2851 " <TD>"
2852 " <SPAN>*</SPAN>"
2853 " <B>Email</B>"
2854 " </TD>"
2855 " <TD>"
2856 " <!-- This comment should be ignored as inferred label.-->"
2857 " <INPUT type='text' id='email' value='john@example.com'/>"
2858 " </TD>"
2859 " </TR>"
2860 " <TR>"
2861 " <TD></TD>"
2862 " <TD>"
2863 " <INPUT type='submit' name='reply-send' value='Send'/>"
2864 " </TD>"
2865 " </TR>"
2866 "</TABLE>"
2867 "</FORM>",
2868 labels, names, values, control_types);
2871 TEST_F(FormAutofillTest, LabelsInferredFromTableLabels) {
2872 ExpectJohnSmithLabels(
2873 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2874 "<TABLE>"
2875 " <TR>"
2876 " <TD>"
2877 " <LABEL>First name:</LABEL>"
2878 " <INPUT type='text' id='firstname' value='John'/>"
2879 " </TD>"
2880 " </TR>"
2881 " <TR>"
2882 " <TD>"
2883 " <LABEL>Last name:</LABEL>"
2884 " <INPUT type='text' id='lastname' value='Smith'/>"
2885 " </TD>"
2886 " </TR>"
2887 " <TR>"
2888 " <TD>"
2889 " <LABEL>Email:</LABEL>"
2890 " <INPUT type='text' id='email' value='john@example.com'/>"
2891 " </TD>"
2892 " </TR>"
2893 "</TABLE>"
2894 "<INPUT type='submit' name='reply-send' value='Send'/>"
2895 "</FORM>");
2898 TEST_F(FormAutofillTest, LabelsInferredFromTableTDInterveningElements) {
2899 ExpectJohnSmithLabels(
2900 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2901 "<TABLE>"
2902 " <TR>"
2903 " <TD>"
2904 " First name:"
2905 " <BR>"
2906 " <INPUT type='text' id='firstname' value='John'/>"
2907 " </TD>"
2908 " </TR>"
2909 " <TR>"
2910 " <TD>"
2911 " Last name:"
2912 " <BR>"
2913 " <INPUT type='text' id='lastname' value='Smith'/>"
2914 " </TD>"
2915 " </TR>"
2916 " <TR>"
2917 " <TD>"
2918 " Email:"
2919 " <BR>"
2920 " <INPUT type='text' id='email' value='john@example.com'/>"
2921 " </TD>"
2922 " </TR>"
2923 "</TABLE>"
2924 "<INPUT type='submit' name='reply-send' value='Send'/>"
2925 "</FORM>");
2928 // Verify that we correctly infer labels when the label text spans multiple
2929 // adjacent HTML elements, not separated by whitespace.
2930 TEST_F(FormAutofillTest, LabelsInferredFromTableAdjacentElements) {
2931 std::vector<base::string16> labels, names, values;
2933 labels.push_back(ASCIIToUTF16("*First Name"));
2934 names.push_back(ASCIIToUTF16("firstname"));
2935 values.push_back(ASCIIToUTF16("John"));
2937 labels.push_back(ASCIIToUTF16("*Last Name"));
2938 names.push_back(ASCIIToUTF16("lastname"));
2939 values.push_back(ASCIIToUTF16("Smith"));
2941 labels.push_back(ASCIIToUTF16("*Email"));
2942 names.push_back(ASCIIToUTF16("email"));
2943 values.push_back(ASCIIToUTF16("john@example.com"));
2945 ExpectLabels(
2946 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
2947 "<TABLE>"
2948 " <TR>"
2949 " <TD>"
2950 " <SPAN>*</SPAN><B>First Name</B>"
2951 " </TD>"
2952 " <TD>"
2953 " <INPUT type='text' id='firstname' value='John'/>"
2954 " </TD>"
2955 " </TR>"
2956 " <TR>"
2957 " <TD>"
2958 " <SPAN>*</SPAN><B>Last Name</B>"
2959 " </TD>"
2960 " <TD>"
2961 " <INPUT type='text' id='lastname' value='Smith'/>"
2962 " </TD>"
2963 " </TR>"
2964 " <TR>"
2965 " <TD>"
2966 " <SPAN>*</SPAN><B>Email</B>"
2967 " </TD>"
2968 " <TD>"
2969 " <INPUT type='text' id='email' value='john@example.com'/>"
2970 " </TD>"
2971 " </TR>"
2972 " <TR>"
2973 " <TD>"
2974 " <INPUT type='submit' name='reply-send' value='Send'/>"
2975 " </TD>"
2976 " </TR>"
2977 "</TABLE>"
2978 "</FORM>",
2979 labels, names, values);
2982 // Verify that we correctly infer labels when the label text resides in the
2983 // previous row.
2984 TEST_F(FormAutofillTest, LabelsInferredFromTableRow) {
2985 std::vector<base::string16> labels, names, values;
2987 labels.push_back(ASCIIToUTF16("*First Name"));
2988 names.push_back(ASCIIToUTF16("firstname"));
2989 values.push_back(ASCIIToUTF16("John"));
2991 labels.push_back(ASCIIToUTF16("*Last Name"));
2992 names.push_back(ASCIIToUTF16("lastname"));
2993 values.push_back(ASCIIToUTF16("Smith"));
2995 labels.push_back(ASCIIToUTF16("*Email"));
2996 names.push_back(ASCIIToUTF16("email"));
2997 values.push_back(ASCIIToUTF16("john@example.com"));
2999 labels.push_back(ASCIIToUTF16("NAME"));
3000 names.push_back(ASCIIToUTF16("name2"));
3001 values.push_back(ASCIIToUTF16("John Smith"));
3003 labels.push_back(ASCIIToUTF16("EMAIL"));
3004 names.push_back(ASCIIToUTF16("email2"));
3005 values.push_back(ASCIIToUTF16("john@example2.com"));
3007 labels.push_back(ASCIIToUTF16("Phone"));
3008 names.push_back(ASCIIToUTF16("phone1"));
3009 values.push_back(ASCIIToUTF16("123"));
3011 labels.push_back(ASCIIToUTF16("Phone"));
3012 names.push_back(ASCIIToUTF16("phone2"));
3013 values.push_back(ASCIIToUTF16("456"));
3015 labels.push_back(ASCIIToUTF16("Phone"));
3016 names.push_back(ASCIIToUTF16("phone3"));
3017 values.push_back(ASCIIToUTF16("7890"));
3019 labels.push_back(ASCIIToUTF16("Credit Card Number"));
3020 names.push_back(ASCIIToUTF16("ccnumber"));
3021 values.push_back(ASCIIToUTF16("4444555544445555"));
3023 ExpectLabels(
3024 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3025 "<TABLE>"
3026 " <TR>"
3027 " <TD>*First Name</TD>"
3028 " <TD>*Last Name</TD>"
3029 " <TD>*Email</TD>"
3030 " </TR>"
3031 " <TR>"
3032 " <TD>"
3033 " <INPUT type='text' id='firstname' value='John'/>"
3034 " </TD>"
3035 " <TD>"
3036 " <INPUT type='text' id='lastname' value='Smith'/>"
3037 " </TD>"
3038 " <TD>"
3039 " <INPUT type='text' id='email' value='john@example.com'/>"
3040 " </TD>"
3041 " </TR>"
3042 " <TR>"
3043 " <TD colspan='2'>NAME</TD>"
3044 " <TD>EMAIL</TD>"
3045 " </TR>"
3046 " <TR>"
3047 " <TD colspan='2'>"
3048 " <INPUT type='text' id='name2' value='John Smith'/>"
3049 " </TD>"
3050 " <TD>"
3051 " <INPUT type='text' id='email2' value='john@example2.com'/>"
3052 " </TD>"
3053 " </TR>"
3054 " <TR>"
3055 " <TD>Phone</TD>"
3056 " </TR>"
3057 " <TR>"
3058 " <TD>"
3059 " <INPUT type='text' id='phone1' value='123'/>"
3060 " </TD>"
3061 " <TD>"
3062 " <INPUT type='text' id='phone2' value='456'/>"
3063 " </TD>"
3064 " <TD>"
3065 " <INPUT type='text' id='phone3' value='7890'/>"
3066 " </TD>"
3067 " </TR>"
3068 " <TR>"
3069 " <TH>"
3070 " Credit Card Number"
3071 " </TH>"
3072 " </TR>"
3073 " <TR>"
3074 " <TD>"
3075 " <INPUT type='text' name='ccnumber' value='4444555544445555'/>"
3076 " </TD>"
3077 " </TR>"
3078 " <TR>"
3079 " <TD>"
3080 " <INPUT type='submit' name='reply-send' value='Send'/>"
3081 " </TD>"
3082 " </TR>"
3083 "</TABLE>",
3084 labels, names, values);
3087 // Verify that we correctly infer labels when enclosed within a list item.
3088 TEST_F(FormAutofillTest, LabelsInferredFromListItem) {
3089 std::vector<base::string16> labels, names, values;
3091 labels.push_back(ASCIIToUTF16("* Home Phone"));
3092 names.push_back(ASCIIToUTF16("areacode"));
3093 values.push_back(ASCIIToUTF16("415"));
3095 labels.push_back(ASCIIToUTF16("* Home Phone"));
3096 names.push_back(ASCIIToUTF16("prefix"));
3097 values.push_back(ASCIIToUTF16("555"));
3099 labels.push_back(ASCIIToUTF16("* Home Phone"));
3100 names.push_back(ASCIIToUTF16("suffix"));
3101 values.push_back(ASCIIToUTF16("1212"));
3103 ExpectLabels(
3104 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3105 "<DIV>"
3106 " <LI>"
3107 " <SPAN>Bogus</SPAN>"
3108 " </LI>"
3109 " <LI>"
3110 " <LABEL><EM>*</EM> Home Phone</LABEL>"
3111 " <INPUT type='text' id='areacode' value='415'/>"
3112 " <INPUT type='text' id='prefix' value='555'/>"
3113 " <INPUT type='text' id='suffix' value='1212'/>"
3114 " </LI>"
3115 " <LI>"
3116 " <INPUT type='submit' name='reply-send' value='Send'/>"
3117 " </LI>"
3118 "</DIV>"
3119 "</FORM>",
3120 labels, names, values);
3123 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionList) {
3124 std::vector<base::string16> labels, names, values;
3126 labels.push_back(ASCIIToUTF16("* First name: Bogus"));
3127 names.push_back(ASCIIToUTF16("firstname"));
3128 values.push_back(ASCIIToUTF16("John"));
3130 labels.push_back(ASCIIToUTF16("Last name:"));
3131 names.push_back(ASCIIToUTF16("lastname"));
3132 values.push_back(ASCIIToUTF16("Smith"));
3134 labels.push_back(ASCIIToUTF16("Email:"));
3135 names.push_back(ASCIIToUTF16("email"));
3136 values.push_back(ASCIIToUTF16("john@example.com"));
3138 ExpectLabels(
3139 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3140 "<DL>"
3141 " <DT>"
3142 " <SPAN>"
3143 " *"
3144 " </SPAN>"
3145 " <SPAN>"
3146 " First name:"
3147 " </SPAN>"
3148 " <SPAN>"
3149 " Bogus"
3150 " </SPAN>"
3151 " </DT>"
3152 " <DD>"
3153 " <FONT>"
3154 " <INPUT type='text' id='firstname' value='John'/>"
3155 " </FONT>"
3156 " </DD>"
3157 " <DT>"
3158 " <SPAN>"
3159 " Last name:"
3160 " </SPAN>"
3161 " </DT>"
3162 " <DD>"
3163 " <FONT>"
3164 " <INPUT type='text' id='lastname' value='Smith'/>"
3165 " </FONT>"
3166 " </DD>"
3167 " <DT>"
3168 " <SPAN>"
3169 " Email:"
3170 " </SPAN>"
3171 " </DT>"
3172 " <DD>"
3173 " <FONT>"
3174 " <INPUT type='text' id='email' value='john@example.com'/>"
3175 " </FONT>"
3176 " </DD>"
3177 " <DT></DT>"
3178 " <DD>"
3179 " <INPUT type='submit' name='reply-send' value='Send'/>"
3180 " </DD>"
3181 "</DL>"
3182 "</FORM>",
3183 labels, names, values);
3186 TEST_F(FormAutofillTest, LabelsInferredWithSameName) {
3187 std::vector<base::string16> labels, names, values;
3189 labels.push_back(ASCIIToUTF16("Address Line 1:"));
3190 names.push_back(ASCIIToUTF16("Address"));
3191 values.push_back(base::string16());
3193 labels.push_back(ASCIIToUTF16("Address Line 2:"));
3194 names.push_back(ASCIIToUTF16("Address"));
3195 values.push_back(base::string16());
3197 labels.push_back(ASCIIToUTF16("Address Line 3:"));
3198 names.push_back(ASCIIToUTF16("Address"));
3199 values.push_back(base::string16());
3201 ExpectLabels(
3202 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3203 " Address Line 1:"
3204 " <INPUT type='text' name='Address'/>"
3205 " Address Line 2:"
3206 " <INPUT type='text' name='Address'/>"
3207 " Address Line 3:"
3208 " <INPUT type='text' name='Address'/>"
3209 " <INPUT type='submit' name='reply-send' value='Send'/>"
3210 "</FORM>",
3211 labels, names, values);
3214 TEST_F(FormAutofillTest, LabelsInferredWithImageTags) {
3215 std::vector<base::string16> labels, names, values;
3217 labels.push_back(ASCIIToUTF16("Phone:"));
3218 names.push_back(ASCIIToUTF16("dayphone1"));
3219 values.push_back(base::string16());
3221 labels.push_back(ASCIIToUTF16("-"));
3222 names.push_back(ASCIIToUTF16("dayphone2"));
3223 values.push_back(base::string16());
3225 labels.push_back(ASCIIToUTF16("-"));
3226 names.push_back(ASCIIToUTF16("dayphone3"));
3227 values.push_back(base::string16());
3229 labels.push_back(ASCIIToUTF16("ext.:"));
3230 names.push_back(ASCIIToUTF16("dayphone4"));
3231 values.push_back(base::string16());
3233 labels.push_back(base::string16());
3234 names.push_back(ASCIIToUTF16("dummy"));
3235 values.push_back(base::string16());
3237 ExpectLabels(
3238 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3239 " Phone:"
3240 " <input type='text' name='dayphone1'>"
3241 " <img/>"
3242 " -"
3243 " <img/>"
3244 " <input type='text' name='dayphone2'>"
3245 " <img/>"
3246 " -"
3247 " <img/>"
3248 " <input type='text' name='dayphone3'>"
3249 " ext.:"
3250 " <input type='text' name='dayphone4'>"
3251 " <input type='text' name='dummy'>"
3252 " <input type='submit' name='reply-send' value='Send'>"
3253 "</FORM>",
3254 labels, names, values);
3257 TEST_F(FormAutofillTest, LabelsInferredFromDivTable) {
3258 ExpectJohnSmithLabels(
3259 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3260 "<DIV>First name:<BR>"
3261 " <SPAN>"
3262 " <INPUT type='text' name='firstname' value='John'>"
3263 " </SPAN>"
3264 "</DIV>"
3265 "<DIV>Last name:<BR>"
3266 " <SPAN>"
3267 " <INPUT type='text' name='lastname' value='Smith'>"
3268 " </SPAN>"
3269 "</DIV>"
3270 "<DIV>Email:<BR>"
3271 " <SPAN>"
3272 " <INPUT type='text' name='email' value='john@example.com'>"
3273 " </SPAN>"
3274 "</DIV>"
3275 "<input type='submit' name='reply-send' value='Send'>"
3276 "</FORM>");
3279 TEST_F(FormAutofillTest, LabelsInferredFromDivSiblingTable) {
3280 ExpectJohnSmithLabels(
3281 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3282 "<DIV>First name:</DIV>"
3283 "<DIV>"
3284 " <SPAN>"
3285 " <INPUT type='text' name='firstname' value='John'>"
3286 " </SPAN>"
3287 "</DIV>"
3288 "<DIV>Last name:</DIV>"
3289 "<DIV>"
3290 " <SPAN>"
3291 " <INPUT type='text' name='lastname' value='Smith'>"
3292 " </SPAN>"
3293 "</DIV>"
3294 "<DIV>Email:</DIV>"
3295 "<DIV>"
3296 " <SPAN>"
3297 " <INPUT type='text' name='email' value='john@example.com'>"
3298 " </SPAN>"
3299 "</DIV>"
3300 "<input type='submit' name='reply-send' value='Send'>"
3301 "</FORM>");
3304 TEST_F(FormAutofillTest, LabelsInferredFromLabelInDivTable) {
3305 ExpectJohnSmithLabels(
3306 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3307 "<LABEL>First name:</LABEL>"
3308 "<LABEL for='lastname'>Last name:</LABEL>"
3309 "<DIV>"
3310 " <INPUT type='text' id='firstname' value='John'>"
3311 "</DIV>"
3312 "<DIV>"
3313 " <INPUT type='text' id='lastname' value='Smith'>"
3314 "</DIV>"
3315 "<LABEL>Email:</LABEL>"
3316 "<DIV>"
3317 " <SPAN>"
3318 " <INPUT type='text' id='email' value='john@example.com'>"
3319 " </SPAN>"
3320 "</DIV>"
3321 "<input type='submit' name='reply-send' value='Send'>"
3322 "</FORM>");
3325 TEST_F(FormAutofillTest, LabelsInferredFromDefinitionListRatherThanDivTable) {
3326 ExpectJohnSmithLabels(
3327 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3328 "<DIV>This is not a label.<BR>"
3329 "<DL>"
3330 " <DT>"
3331 " <SPAN>"
3332 " First name:"
3333 " </SPAN>"
3334 " </DT>"
3335 " <DD>"
3336 " <FONT>"
3337 " <INPUT type='text' id='firstname' value='John'/>"
3338 " </FONT>"
3339 " </DD>"
3340 " <DT>"
3341 " <SPAN>"
3342 " Last name:"
3343 " </SPAN>"
3344 " </DT>"
3345 " <DD>"
3346 " <FONT>"
3347 " <INPUT type='text' id='lastname' value='Smith'/>"
3348 " </FONT>"
3349 " </DD>"
3350 " <DT>"
3351 " <SPAN>"
3352 " Email:"
3353 " </SPAN>"
3354 " </DT>"
3355 " <DD>"
3356 " <FONT>"
3357 " <INPUT type='text' id='email' value='john@example.com'/>"
3358 " </FONT>"
3359 " </DD>"
3360 " <DT></DT>"
3361 " <DD>"
3362 " <INPUT type='submit' name='reply-send' value='Send'/>"
3363 " </DD>"
3364 "</DL>"
3365 "</DIV>"
3366 "</FORM>");
3369 TEST_F(FormAutofillTest, FillFormMaxLength) {
3370 TestFillFormMaxLength(
3371 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3372 " <INPUT type='text' id='firstname' maxlength='5'/>"
3373 " <INPUT type='text' id='lastname' maxlength='7'/>"
3374 " <INPUT type='text' id='email' maxlength='9'/>"
3375 " <INPUT type='submit' name='reply-send' value='Send'/>"
3376 "</FORM>",
3377 false);
3380 TEST_F(FormAutofillTest, FillFormMaxLengthForUnownedForm) {
3381 TestFillFormMaxLength(
3382 "<HEAD><TITLE>delivery recipient info</TITLE></HEAD>"
3383 "<INPUT type='text' id='firstname' maxlength='5'/>"
3384 "<INPUT type='text' id='lastname' maxlength='7'/>"
3385 "<INPUT type='text' id='email' maxlength='9'/>"
3386 "<INPUT type='submit' name='reply-send' value='Send'/>",
3387 true);
3390 // This test uses negative values of the maxlength attribute for input elements.
3391 // In this case, the maxlength of the input elements is set to the default
3392 // maxlength (defined in WebKit.)
3393 TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
3394 TestFillFormNegativeMaxLength(
3395 "<HEAD><TITLE>delivery recipient info</TITLE></HEAD>"
3396 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3397 " <INPUT type='text' id='firstname' maxlength='-1'/>"
3398 " <INPUT type='text' id='lastname' maxlength='-10'/>"
3399 " <INPUT type='text' id='email' maxlength='-13'/>"
3400 " <INPUT type='submit' name='reply-send' value='Send'/>"
3401 "</FORM>",
3402 false);
3405 TEST_F(FormAutofillTest, FillFormNegativeMaxLengthForUnownedForm) {
3406 TestFillFormNegativeMaxLength(
3407 "<HEAD><TITLE>delivery recipient info</TITLE></HEAD>"
3408 "<INPUT type='text' id='firstname' maxlength='-1'/>"
3409 "<INPUT type='text' id='lastname' maxlength='-10'/>"
3410 "<INPUT type='text' id='email' maxlength='-13'/>"
3411 "<INPUT type='submit' name='reply-send' value='Send'/>",
3412 true);
3415 TEST_F(FormAutofillTest, FillFormEmptyName) {
3416 TestFillFormEmptyName(
3417 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3418 " <INPUT type='text' id='firstname'/>"
3419 " <INPUT type='text' id='lastname'/>"
3420 " <INPUT type='text' id='email'/>"
3421 " <INPUT type='submit' value='Send'/>"
3422 "</FORM>",
3423 false);
3426 TEST_F(FormAutofillTest, FillFormEmptyNameForUnownedForm) {
3427 TestFillFormEmptyName(
3428 "<HEAD><TITLE>delivery recipient info</TITLE></HEAD>"
3429 "<INPUT type='text' id='firstname'/>"
3430 "<INPUT type='text' id='lastname'/>"
3431 "<INPUT type='text' id='email'/>"
3432 "<INPUT type='submit' value='Send'/>",
3433 true);
3436 TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
3437 TestFillFormEmptyFormNames(
3438 "<FORM action='http://buh.com' method='post'>"
3439 " <INPUT type='text' id='firstname'/>"
3440 " <INPUT type='text' id='middlename'/>"
3441 " <INPUT type='text' id='lastname'/>"
3442 " <INPUT type='submit' value='Send'/>"
3443 "</FORM>"
3444 "<FORM action='http://abc.com' method='post'>"
3445 " <INPUT type='text' id='apple'/>"
3446 " <INPUT type='text' id='banana'/>"
3447 " <INPUT type='text' id='cantelope'/>"
3448 " <INPUT type='submit' value='Send'/>"
3449 "</FORM>",
3450 false);
3453 TEST_F(FormAutofillTest, FillFormEmptyFormNamesForUnownedForm) {
3454 TestFillFormEmptyFormNames(
3455 "<HEAD><TITLE>enter delivery preferences</TITLE></HEAD>"
3456 "<INPUT type='text' id='firstname'/>"
3457 "<INPUT type='text' id='middlename'/>"
3458 "<INPUT type='text' id='lastname'/>"
3459 "<INPUT type='text' id='apple'/>"
3460 "<INPUT type='text' id='banana'/>"
3461 "<INPUT type='text' id='cantelope'/>"
3462 "<INPUT type='submit' value='Send'/>",
3463 true);
3466 TEST_F(FormAutofillTest, ThreePartPhone) {
3467 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3468 " Phone:"
3469 " <input type='text' name='dayphone1'>"
3470 " -"
3471 " <input type='text' name='dayphone2'>"
3472 " -"
3473 " <input type='text' name='dayphone3'>"
3474 " ext.:"
3475 " <input type='text' name='dayphone4'>"
3476 " <input type='submit' name='reply-send' value='Send'>"
3477 "</FORM>");
3479 WebFrame* frame = GetMainFrame();
3480 ASSERT_NE(nullptr, frame);
3482 WebVector<WebFormElement> forms;
3483 frame->document().forms(forms);
3484 ASSERT_EQ(1U, forms.size());
3486 FormData form;
3487 EXPECT_TRUE(WebFormElementToFormData(forms[0],
3488 WebFormControlElement(),
3489 EXTRACT_VALUE,
3490 &form,
3491 nullptr));
3492 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3493 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3494 EXPECT_FALSE(form.origin.is_empty());
3495 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3497 const std::vector<FormFieldData>& fields = form.fields;
3498 ASSERT_EQ(4U, fields.size());
3500 FormFieldData expected;
3501 expected.form_control_type = "text";
3502 expected.max_length = WebInputElement::defaultMaxLength();
3504 expected.label = ASCIIToUTF16("Phone:");
3505 expected.name = ASCIIToUTF16("dayphone1");
3506 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3508 expected.label = ASCIIToUTF16("-");
3509 expected.name = ASCIIToUTF16("dayphone2");
3510 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3512 expected.label = ASCIIToUTF16("-");
3513 expected.name = ASCIIToUTF16("dayphone3");
3514 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3516 expected.label = ASCIIToUTF16("ext.:");
3517 expected.name = ASCIIToUTF16("dayphone4");
3518 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
3522 TEST_F(FormAutofillTest, MaxLengthFields) {
3523 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3524 " Phone:"
3525 " <input type='text' maxlength='3' name='dayphone1'>"
3526 " -"
3527 " <input type='text' maxlength='3' name='dayphone2'>"
3528 " -"
3529 " <input type='text' maxlength='4' size='5'"
3530 " name='dayphone3'>"
3531 " ext.:"
3532 " <input type='text' maxlength='5' name='dayphone4'>"
3533 " <input type='text' name='default1'>"
3534 " <input type='text' maxlength='-1' name='invalid1'>"
3535 " <input type='submit' name='reply-send' value='Send'>"
3536 "</FORM>");
3538 WebFrame* frame = GetMainFrame();
3539 ASSERT_NE(nullptr, frame);
3541 WebVector<WebFormElement> forms;
3542 frame->document().forms(forms);
3543 ASSERT_EQ(1U, forms.size());
3545 FormData form;
3546 EXPECT_TRUE(WebFormElementToFormData(forms[0],
3547 WebFormControlElement(),
3548 EXTRACT_VALUE,
3549 &form,
3550 nullptr));
3551 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3552 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3553 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3555 const std::vector<FormFieldData>& fields = form.fields;
3556 ASSERT_EQ(6U, fields.size());
3558 FormFieldData expected;
3559 expected.form_control_type = "text";
3561 expected.label = ASCIIToUTF16("Phone:");
3562 expected.name = ASCIIToUTF16("dayphone1");
3563 expected.max_length = 3;
3564 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3566 expected.label = ASCIIToUTF16("-");
3567 expected.name = ASCIIToUTF16("dayphone2");
3568 expected.max_length = 3;
3569 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3571 expected.label = ASCIIToUTF16("-");
3572 expected.name = ASCIIToUTF16("dayphone3");
3573 expected.max_length = 4;
3574 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3576 expected.label = ASCIIToUTF16("ext.:");
3577 expected.name = ASCIIToUTF16("dayphone4");
3578 expected.max_length = 5;
3579 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
3581 // When unspecified |size|, default is returned.
3582 expected.label.clear();
3583 expected.name = ASCIIToUTF16("default1");
3584 expected.max_length = WebInputElement::defaultMaxLength();
3585 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
3587 // When invalid |size|, default is returned.
3588 expected.label.clear();
3589 expected.name = ASCIIToUTF16("invalid1");
3590 expected.max_length = WebInputElement::defaultMaxLength();
3591 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
3594 // This test re-creates the experience of typing in a field then selecting a
3595 // profile from the Autofill suggestions popup. The field that is being typed
3596 // into should be filled even though it's not technically empty.
3597 TEST_F(FormAutofillTest, FillFormNonEmptyField) {
3598 TestFillFormNonEmptyField(
3599 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3600 " <INPUT type='text' id='firstname'/>"
3601 " <INPUT type='text' id='lastname'/>"
3602 " <INPUT type='text' id='email'/>"
3603 " <INPUT type='submit' value='Send'/>"
3604 "</FORM>",
3605 false);
3608 TEST_F(FormAutofillTest, FillFormNonEmptyFieldForUnownedForm) {
3609 TestFillFormNonEmptyField(
3610 "<HEAD><TITLE>delivery recipient info</TITLE></HEAD>"
3611 "<INPUT type='text' id='firstname'/>"
3612 "<INPUT type='text' id='lastname'/>"
3613 "<INPUT type='text' id='email'/>"
3614 "<INPUT type='submit' value='Send'/>",
3615 true);
3618 TEST_F(FormAutofillTest, ClearFormWithNode) {
3619 TestClearFormWithNode(
3620 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3621 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3622 " <INPUT type='text' id='lastname' value='Earp'/>"
3623 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3624 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3625 " <INPUT type='month' id='month' value='2012-11'>"
3626 " <INPUT type='month' id='month-disabled' value='2012-11'"
3627 " disabled='disabled'>"
3628 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3629 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3630 " Banana!"
3631 " </TEXTAREA>"
3632 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3633 " <INPUT type='submit' value='Send'/>"
3634 "</FORM>",
3635 false);
3638 TEST_F(FormAutofillTest, ClearFormWithNodeForUnownedForm) {
3639 TestClearFormWithNode(
3640 "<HEAD><TITLE>store checkout</TITLE></HEAD>"
3641 " <!-- Indented on purpose //-->"
3642 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3643 " <INPUT type='text' id='lastname' value='Earp'/>"
3644 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3645 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3646 " <INPUT type='month' id='month' value='2012-11'>"
3647 " <INPUT type='month' id='month-disabled' value='2012-11'"
3648 " disabled='disabled'>"
3649 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3650 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3651 " Banana!"
3652 " </TEXTAREA>"
3653 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3654 " <INPUT type='submit' value='Send'/>",
3655 true);
3658 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) {
3659 TestClearFormWithNodeContainingSelectOne(
3660 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3661 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3662 " <INPUT type='text' id='lastname' value='Earp'/>"
3663 " <SELECT id='state' name='state'>"
3664 " <OPTION selected>?</OPTION>"
3665 " <OPTION>AA</OPTION>"
3666 " <OPTION>AE</OPTION>"
3667 " <OPTION>AK</OPTION>"
3668 " </SELECT>"
3669 " <INPUT type='submit' value='Send'/>"
3670 "</FORM>",
3671 false);
3674 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOneForUnownedForm) {
3675 TestClearFormWithNodeContainingSelectOne(
3676 "<HEAD><TITLE>store checkout</TITLE></HEAD>"
3677 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3678 "<INPUT type='text' id='lastname' value='Earp'/>"
3679 "<SELECT id='state' name='state'>"
3680 " <OPTION selected>?</OPTION>"
3681 " <OPTION>AA</OPTION>"
3682 " <OPTION>AE</OPTION>"
3683 " <OPTION>AK</OPTION>"
3684 "</SELECT>"
3685 "<INPUT type='submit' value='Send'/>",
3686 true);
3689 TEST_F(FormAutofillTest, ClearPreviewedFormWithElement) {
3690 TestClearPreviewedFormWithElement(
3691 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3692 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3693 " <INPUT type='text' id='lastname'/>"
3694 " <INPUT type='text' id='email'/>"
3695 " <INPUT type='email' id='email2'/>"
3696 " <INPUT type='tel' id='phone'/>"
3697 " <INPUT type='submit' value='Send'/>"
3698 "</FORM>");
3701 TEST_F(FormAutofillTest, ClearPreviewedFormWithElementForUnownedForm) {
3702 TestClearPreviewedFormWithElement(
3703 "<HEAD><TITLE>store checkout</TITLE></HEAD>"
3704 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3705 "<INPUT type='text' id='lastname'/>"
3706 "<INPUT type='text' id='email'/>"
3707 "<INPUT type='email' id='email2'/>"
3708 "<INPUT type='tel' id='phone'/>"
3709 "<INPUT type='submit' value='Send'/>");
3712 TEST_F(FormAutofillTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
3713 TestClearPreviewedFormWithNonEmptyInitiatingNode(
3714 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3715 " <INPUT type='text' id='firstname' value='W'/>"
3716 " <INPUT type='text' id='lastname'/>"
3717 " <INPUT type='text' id='email'/>"
3718 " <INPUT type='email' id='email2'/>"
3719 " <INPUT type='tel' id='phone'/>"
3720 " <INPUT type='submit' value='Send'/>"
3721 "</FORM>");
3724 TEST_F(FormAutofillTest,
3725 ClearPreviewedFormWithNonEmptyInitiatingNodeForUnownedForm) {
3726 TestClearPreviewedFormWithNonEmptyInitiatingNode(
3727 "<HEAD><TITLE>shipping details</TITLE></HEAD>"
3728 "<INPUT type='text' id='firstname' value='W'/>"
3729 "<INPUT type='text' id='lastname'/>"
3730 "<INPUT type='text' id='email'/>"
3731 "<INPUT type='email' id='email2'/>"
3732 "<INPUT type='tel' id='phone'/>"
3733 "<INPUT type='submit' value='Send'/>");
3736 TEST_F(FormAutofillTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
3737 TestClearPreviewedFormWithAutofilledInitiatingNode(
3738 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3739 " <INPUT type='text' id='firstname' value='W'/>"
3740 " <INPUT type='text' id='lastname'/>"
3741 " <INPUT type='text' id='email'/>"
3742 " <INPUT type='email' id='email2'/>"
3743 " <INPUT type='tel' id='phone'/>"
3744 " <INPUT type='submit' value='Send'/>"
3745 "</FORM>");
3748 TEST_F(FormAutofillTest,
3749 ClearPreviewedFormWithAutofilledInitiatingNodeForUnownedForm) {
3750 TestClearPreviewedFormWithAutofilledInitiatingNode(
3751 "<HEAD><TITLE>shipping details</TITLE></HEAD>"
3752 "<INPUT type='text' id='firstname' value='W'/>"
3753 "<INPUT type='text' id='lastname'/>"
3754 "<INPUT type='text' id='email'/>"
3755 "<INPUT type='email' id='email2'/>"
3756 "<INPUT type='tel' id='phone'/>"
3757 "<INPUT type='submit' value='Send'/>");
3760 // Autofill's "Clear Form" should clear only autofilled fields
3761 TEST_F(FormAutofillTest, ClearOnlyAutofilledFields) {
3762 TestClearOnlyAutofilledFields(
3763 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3764 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3765 " <INPUT type='text' id='lastname' value='Earp'/>"
3766 " <INPUT type='email' id='email' value='wyatt@earp.com'/>"
3767 " <INPUT type='tel' id='phone' value='650-777-9999'/>"
3768 " <INPUT type='submit' value='Send'/>"
3769 "</FORM>");
3772 TEST_F(FormAutofillTest, ClearOnlyAutofilledFieldsForUnownedForm) {
3773 TestClearOnlyAutofilledFields(
3774 "<HEAD><TITLE>shipping details</TITLE></HEAD>"
3775 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3776 "<INPUT type='text' id='lastname' value='Earp'/>"
3777 "<INPUT type='email' id='email' value='wyatt@earp.com'/>"
3778 "<INPUT type='tel' id='phone' value='650-777-9999'/>"
3779 "<INPUT type='submit' value='Send'/>");
3782 // If we have multiple labels per id, the labels concatenated into label string.
3783 TEST_F(FormAutofillTest, MultipleLabelsPerElement) {
3784 std::vector<base::string16> labels, names, values;
3786 labels.push_back(ASCIIToUTF16("First Name:"));
3787 names.push_back(ASCIIToUTF16("firstname"));
3788 values.push_back(ASCIIToUTF16("John"));
3790 labels.push_back(ASCIIToUTF16("Last Name:"));
3791 names.push_back(ASCIIToUTF16("lastname"));
3792 values.push_back(ASCIIToUTF16("Smith"));
3794 labels.push_back(ASCIIToUTF16("Email: xxx@yyy.com"));
3795 names.push_back(ASCIIToUTF16("email"));
3796 values.push_back(ASCIIToUTF16("john@example.com"));
3798 ExpectLabels(
3799 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
3800 " <LABEL for='firstname'> First Name: </LABEL>"
3801 " <LABEL for='firstname'></LABEL>"
3802 " <INPUT type='text' id='firstname' value='John'/>"
3803 " <LABEL for='lastname'></LABEL>"
3804 " <LABEL for='lastname'> Last Name: </LABEL>"
3805 " <INPUT type='text' id='lastname' value='Smith'/>"
3806 " <LABEL for='email'> Email: </LABEL>"
3807 " <LABEL for='email'> xxx@yyy.com </LABEL>"
3808 " <INPUT type='text' id='email' value='john@example.com'/>"
3809 " <INPUT type='submit' name='reply-send' value='Send'/>"
3810 "</FORM>",
3811 labels, names, values);
3814 TEST_F(FormAutofillTest, ClickElement) {
3815 LoadHTML("<BUTTON id='link'>Button</BUTTON>"
3816 "<BUTTON name='button'>Button</BUTTON>");
3817 WebFrame* frame = GetMainFrame();
3818 ASSERT_NE(nullptr, frame);
3820 // Successful retrieval by id.
3821 WebElementDescriptor clicker;
3822 clicker.retrieval_method = WebElementDescriptor::ID;
3823 clicker.descriptor = "link";
3824 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3826 // Successful retrieval by css selector.
3827 clicker.retrieval_method = WebElementDescriptor::CSS_SELECTOR;
3828 clicker.descriptor = "button[name='button']";
3829 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3831 // Unsuccessful retrieval due to invalid CSS selector.
3832 clicker.descriptor = "^*&";
3833 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3835 // Unsuccessful retrieval because element does not exist.
3836 clicker.descriptor = "#junk";
3837 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3840 TEST_F(FormAutofillTest, SelectOneAsText) {
3841 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
3842 " <INPUT type='text' id='firstname' value='John'/>"
3843 " <INPUT type='text' id='lastname' value='Smith'/>"
3844 " <SELECT id='country'>"
3845 " <OPTION value='AF'>Afghanistan</OPTION>"
3846 " <OPTION value='AL'>Albania</OPTION>"
3847 " <OPTION value='DZ'>Algeria</OPTION>"
3848 " </SELECT>"
3849 " <INPUT type='submit' name='reply-send' value='Send'/>"
3850 "</FORM>");
3852 WebFrame* frame = GetMainFrame();
3853 ASSERT_NE(nullptr, frame);
3855 // Set the value of the select-one.
3856 WebSelectElement select_element =
3857 frame->document().getElementById("country").to<WebSelectElement>();
3858 select_element.setValue(WebString::fromUTF8("AL"));
3860 WebVector<WebFormElement> forms;
3861 frame->document().forms(forms);
3862 ASSERT_EQ(1U, forms.size());
3864 FormData form;
3866 // Extract the country select-one value as text.
3867 EXPECT_TRUE(WebFormElementToFormData(
3868 forms[0], WebFormControlElement(),
3869 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTION_TEXT), &form,
3870 nullptr));
3871 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3872 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3873 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3875 const std::vector<FormFieldData>& fields = form.fields;
3876 ASSERT_EQ(3U, fields.size());
3878 FormFieldData expected;
3880 expected.name = ASCIIToUTF16("firstname");
3881 expected.value = ASCIIToUTF16("John");
3882 expected.form_control_type = "text";
3883 expected.max_length = WebInputElement::defaultMaxLength();
3884 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3886 expected.name = ASCIIToUTF16("lastname");
3887 expected.value = ASCIIToUTF16("Smith");
3888 expected.form_control_type = "text";
3889 expected.max_length = WebInputElement::defaultMaxLength();
3890 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3892 expected.name = ASCIIToUTF16("country");
3893 expected.value = ASCIIToUTF16("Albania");
3894 expected.form_control_type = "select-one";
3895 expected.max_length = 0;
3896 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3898 form.fields.clear();
3899 // Extract the country select-one value as value.
3900 EXPECT_TRUE(WebFormElementToFormData(forms[0],
3901 WebFormControlElement(),
3902 EXTRACT_VALUE,
3903 &form,
3904 nullptr));
3905 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3906 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3907 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3909 ASSERT_EQ(3U, fields.size());
3911 expected.name = ASCIIToUTF16("firstname");
3912 expected.value = ASCIIToUTF16("John");
3913 expected.form_control_type = "text";
3914 expected.max_length = WebInputElement::defaultMaxLength();
3915 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3917 expected.name = ASCIIToUTF16("lastname");
3918 expected.value = ASCIIToUTF16("Smith");
3919 expected.form_control_type = "text";
3920 expected.max_length = WebInputElement::defaultMaxLength();
3921 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3923 expected.name = ASCIIToUTF16("country");
3924 expected.value = ASCIIToUTF16("AL");
3925 expected.form_control_type = "select-one";
3926 expected.max_length = 0;
3927 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3930 TEST_F(FormAutofillTest,
3931 UnownedFormElementsAndFieldSetsToFormDataFieldsets) {
3932 std::vector<WebElement> fieldsets;
3933 std::vector<WebFormControlElement> control_elements;
3935 const ExtractMask extract_mask =
3936 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS);
3938 LoadHTML("<HEAD><TITLE>delivery info</TITLE></HEAD>"
3939 "<DIV>"
3940 " <FIELDSET>"
3941 " <LABEL for='firstname'>First name:</LABEL>"
3942 " <LABEL for='lastname'>Last name:</LABEL>"
3943 " <INPUT type='text' id='firstname' value='John'/>"
3944 " <INPUT type='text' id='lastname' value='Smith'/>"
3945 " </FIELDSET>"
3946 " <FIELDSET>"
3947 " <LABEL for='email'>Email:</LABEL>"
3948 " <INPUT type='text' id='email' value='john@example.com'/>"
3949 " </FIELDSET>"
3950 "</DIV>");
3952 WebFrame* frame = GetMainFrame();
3953 ASSERT_NE(nullptr, frame);
3955 control_elements = GetUnownedAutofillableFormFieldElements(
3956 frame->document().all(), &fieldsets);
3957 ASSERT_EQ(3U, control_elements.size());
3958 ASSERT_EQ(2U, fieldsets.size());
3960 FormData form;
3961 EXPECT_TRUE(UnownedFormElementsAndFieldSetsToFormData(
3962 fieldsets, control_elements, nullptr, frame->document(), extract_mask,
3963 &form, nullptr));
3965 EXPECT_TRUE(form.name.empty());
3966 EXPECT_EQ(frame->document().url(), form.origin);
3967 EXPECT_FALSE(form.action.is_valid());
3969 const std::vector<FormFieldData>& fields = form.fields;
3970 ASSERT_EQ(3U, fields.size());
3972 FormFieldData expected;
3973 expected.form_control_type = "text";
3974 expected.max_length = WebInputElement::defaultMaxLength();
3976 expected.name = ASCIIToUTF16("firstname");
3977 expected.value = ASCIIToUTF16("John");
3978 expected.label = ASCIIToUTF16("First name:");
3979 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3981 expected.name = ASCIIToUTF16("lastname");
3982 expected.value = ASCIIToUTF16("Smith");
3983 expected.label = ASCIIToUTF16("Last name:");
3984 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3986 expected.name = ASCIIToUTF16("email");
3987 expected.value = ASCIIToUTF16("john@example.com");
3988 expected.label = ASCIIToUTF16("Email:");
3989 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3992 TEST_F(FormAutofillTest,
3993 UnownedFormElementsAndFieldSetsToFormDataControlOutsideOfFieldset) {
3994 std::vector<WebElement> fieldsets;
3995 std::vector<WebFormControlElement> control_elements;
3997 const ExtractMask extract_mask =
3998 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS);
4000 LoadHTML("<HEAD><TITLE>shipping details</TITLE></HEAD>"
4001 "<DIV>"
4002 " <FIELDSET>"
4003 " <LABEL for='firstname'>First name:</LABEL>"
4004 " <LABEL for='lastname'>Last name:</LABEL>"
4005 " <INPUT type='text' id='firstname' value='John'/>"
4006 " <INPUT type='text' id='lastname' value='Smith'/>"
4007 " <LABEL for='email'>Email:</LABEL>"
4008 " </FIELDSET>"
4009 " <INPUT type='text' id='email' value='john@example.com'/>"
4010 "</DIV>");
4012 WebFrame* frame = GetMainFrame();
4013 ASSERT_NE(nullptr, frame);
4015 control_elements = GetUnownedAutofillableFormFieldElements(
4016 frame->document().all(), &fieldsets);
4017 ASSERT_EQ(3U, control_elements.size());
4018 ASSERT_EQ(1U, fieldsets.size());
4020 FormData form;
4021 EXPECT_TRUE(UnownedFormElementsAndFieldSetsToFormData(
4022 fieldsets, control_elements, nullptr, frame->document(), extract_mask,
4023 &form, nullptr));
4025 EXPECT_TRUE(form.name.empty());
4026 EXPECT_EQ(frame->document().url(), form.origin);
4027 EXPECT_FALSE(form.action.is_valid());
4029 const std::vector<FormFieldData>& fields = form.fields;
4030 ASSERT_EQ(3U, fields.size());
4032 FormFieldData expected;
4033 expected.form_control_type = "text";
4034 expected.max_length = WebInputElement::defaultMaxLength();
4036 expected.name = ASCIIToUTF16("firstname");
4037 expected.value = ASCIIToUTF16("John");
4038 expected.label = ASCIIToUTF16("First name:");
4039 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
4041 expected.name = ASCIIToUTF16("lastname");
4042 expected.value = ASCIIToUTF16("Smith");
4043 expected.label = ASCIIToUTF16("Last name:");
4044 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
4046 expected.name = ASCIIToUTF16("email");
4047 expected.value = ASCIIToUTF16("john@example.com");
4048 expected.label = ASCIIToUTF16("Email:");
4049 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
4052 TEST_F(FormAutofillTest, UnownedFormElementsAndFieldSetsToFormDataWithForm) {
4053 std::vector<WebElement> fieldsets;
4054 std::vector<WebFormControlElement> control_elements;
4056 const ExtractMask extract_mask =
4057 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS);
4059 LoadHTML(kFormHtml);
4061 WebFrame* frame = GetMainFrame();
4062 ASSERT_NE(nullptr, frame);
4064 control_elements = GetUnownedAutofillableFormFieldElements(
4065 frame->document().all(), &fieldsets);
4066 ASSERT_TRUE(control_elements.empty());
4067 ASSERT_TRUE(fieldsets.empty());
4069 FormData form;
4070 EXPECT_FALSE(UnownedFormElementsAndFieldSetsToFormData(
4071 fieldsets, control_elements, nullptr, frame->document(), extract_mask,
4072 &form, nullptr));
4075 } // namespace autofill