Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: chrome/renderer/autofill/form_autofill_browsertest.cc

Issue 804443002: Autofill: Modify various utility function to deal with unowned form fields. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add more tests Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <vector> 5 #include <vector>
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 const char* const name; 50 const char* const name;
51 const char* const initial_value; 51 const char* const initial_value;
52 const char* const autocomplete_attribute; // The autocomplete attribute of 52 const char* const autocomplete_attribute; // The autocomplete attribute of
53 // the element. 53 // the element.
54 bool should_be_autofilled; // Whether the filed should be autofilled. 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. 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 56 const char* const expected_value; // The expected value after Autofill
57 // or Preview. 57 // or Preview.
58 }; 58 };
59 59
60 static const char kFormHtml[] = 60 const char kFormHtml[] =
61 "<FORM name='TestForm' action='http://buh.com' method='post'>" 61 "<FORM name='TestForm' action='http://buh.com' method='post'>"
62 " <INPUT type='text' id='firstname'/>" 62 " <INPUT type='text' id='firstname'/>"
63 " <INPUT type='text' id='lastname'/>" 63 " <INPUT type='text' id='lastname'/>"
64 " <INPUT type='hidden' id='imhidden'/>" 64 " <INPUT type='hidden' id='imhidden'/>"
65 " <INPUT type='text' id='notempty' value='Hi'/>" 65 " <INPUT type='text' id='notempty' value='Hi'/>"
66 " <INPUT type='text' autocomplete='off' id='noautocomplete'/>" 66 " <INPUT type='text' autocomplete='off' id='noautocomplete'/>"
67 " <INPUT type='text' disabled='disabled' id='notenabled'/>" 67 " <INPUT type='text' disabled='disabled' id='notenabled'/>"
68 " <INPUT type='text' readonly id='readonly'/>" 68 " <INPUT type='text' readonly id='readonly'/>"
69 " <INPUT type='text' style='visibility: hidden'" 69 " <INPUT type='text' style='visibility: hidden'"
70 " id='invisible'/>" 70 " id='invisible'/>"
(...skipping 11 matching lines...) Expand all
82 " </SELECT>" 82 " </SELECT>"
83 " <SELECT id='select-unchanged'>" 83 " <SELECT id='select-unchanged'>"
84 " <OPTION value='CA' selected>California</OPTION>" 84 " <OPTION value='CA' selected>California</OPTION>"
85 " <OPTION value='TX'>Texas</OPTION>" 85 " <OPTION value='TX'>Texas</OPTION>"
86 " </SELECT>" 86 " </SELECT>"
87 " <TEXTAREA id='textarea'></TEXTAREA>" 87 " <TEXTAREA id='textarea'></TEXTAREA>"
88 " <TEXTAREA id='textarea-nonempty'>Go&#10;away!</TEXTAREA>" 88 " <TEXTAREA id='textarea-nonempty'>Go&#10;away!</TEXTAREA>"
89 " <INPUT type='submit' name='reply-send' value='Send'/>" 89 " <INPUT type='submit' name='reply-send' value='Send'/>"
90 "</FORM>"; 90 "</FORM>";
91 91
92 const char kUnownedFormHtml[] =
93 "<INPUT type='text' id='firstname'/>"
94 "<INPUT type='text' id='lastname'/>"
95 "<INPUT type='hidden' id='imhidden'/>"
96 "<INPUT type='text' id='notempty' value='Hi'/>"
97 "<INPUT type='text' autocomplete='off' id='noautocomplete'/>"
98 "<INPUT type='text' disabled='disabled' id='notenabled'/>"
99 "<INPUT type='text' readonly id='readonly'/>"
100 "<INPUT type='text' style='visibility: hidden'"
101 " id='invisible'/>"
102 "<INPUT type='text' style='display: none' id='displaynone'/>"
103 "<INPUT type='month' id='month'/>"
104 "<INPUT type='month' id='month-nonempty' value='2011-12'/>"
105 "<SELECT id='select'>"
106 " <OPTION></OPTION>"
107 " <OPTION value='CA'>California</OPTION>"
108 " <OPTION value='TX'>Texas</OPTION>"
109 "</SELECT>"
110 "<SELECT id='select-nonempty'>"
111 " <OPTION value='CA' selected>California</OPTION>"
112 " <OPTION value='TX'>Texas</OPTION>"
113 "</SELECT>"
114 "<SELECT id='select-unchanged'>"
115 " <OPTION value='CA' selected>California</OPTION>"
116 " <OPTION value='TX'>Texas</OPTION>"
117 "</SELECT>"
118 "<TEXTAREA id='textarea'></TEXTAREA>"
119 "<TEXTAREA id='textarea-nonempty'>Go&#10;away!</TEXTAREA>"
120 "<INPUT type='submit' name='reply-send' value='Send'/>";
121
92 std::string RetrievalMethodToString( 122 std::string RetrievalMethodToString(
93 const WebElementDescriptor::RetrievalMethod& method) { 123 const WebElementDescriptor::RetrievalMethod& method) {
94 switch (method) { 124 switch (method) {
95 case WebElementDescriptor::CSS_SELECTOR: 125 case WebElementDescriptor::CSS_SELECTOR:
96 return "CSS_SELECTOR"; 126 return "CSS_SELECTOR";
97 case WebElementDescriptor::ID: 127 case WebElementDescriptor::ID:
98 return "ID"; 128 return "ID";
99 case WebElementDescriptor::NONE: 129 case WebElementDescriptor::NONE:
100 return "NONE"; 130 return "NONE";
101 } 131 }
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 values.push_back(ASCIIToUTF16("john@example.com")); 236 values.push_back(ASCIIToUTF16("john@example.com"));
207 237
208 ExpectLabels(html, labels, names, values); 238 ExpectLabels(html, labels, names, values);
209 } 239 }
210 240
211 typedef void (*FillFormFunction)(const FormData& form, 241 typedef void (*FillFormFunction)(const FormData& form,
212 const WebFormControlElement& element); 242 const WebFormControlElement& element);
213 243
214 typedef WebString (*GetValueFunction)(WebFormControlElement element); 244 typedef WebString (*GetValueFunction)(WebFormControlElement element);
215 245
246 bool FindFormAndField(WebFrame* web_frame,
247 const blink::WebFormControlElement& element,
248 bool unowned,
249 RequirementsMask requirement_mask,
250 FormData* form,
251 FormFieldData* field) {
252 if (!unowned) {
253 return FindFormAndFieldForFormControlElement(element,
254 form,
255 field,
256 requirement_mask);
257 }
258
259 WebDocument document = web_frame->document();
260 std::vector<WebElement> fieldsets;
261 std::vector<WebFormControlElement> control_elements =
262 GetUnownedAutofillableFormFieldElements(document.all(), &fieldsets);
263 ExtractMask extract_mask =
264 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS);
265 return UnownedFormElementsAndFieldSetsToFormData(fieldsets,
266 control_elements,
267 &element,
268 document.url(),
269 requirement_mask,
270 extract_mask,
271 form,
272 field);
273 }
274
216 // Test FormFillxxx functions. 275 // Test FormFillxxx functions.
217 void TestFormFillFunctions(const char* html, 276 void TestFormFillFunctions(const char* html,
277 bool unowned,
218 const AutofillFieldCase* field_cases, 278 const AutofillFieldCase* field_cases,
219 size_t number_of_field_cases, 279 size_t number_of_field_cases,
220 FillFormFunction fill_form_function, 280 FillFormFunction fill_form_function,
221 GetValueFunction get_value_function) { 281 GetValueFunction get_value_function) {
222 LoadHTML(html); 282 LoadHTML(html);
223 283
224 WebFrame* web_frame = GetMainFrame(); 284 WebFrame* web_frame = GetMainFrame();
225 ASSERT_NE(nullptr, web_frame); 285 ASSERT_NE(nullptr, web_frame);
226 286
227 FormCache form_cache; 287 FormCache form_cache;
228 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 288 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
229 ASSERT_EQ(1U, forms.size()); 289 ASSERT_EQ(1U, forms.size());
230 290
231 // Get the input element we want to find. 291 // Get the input element we want to find.
232 WebInputElement input_element = GetInputElementById("firstname"); 292 WebInputElement input_element = GetInputElementById("firstname");
233 293
234 // Find the form that contains the input element. 294 // Find the form that contains the input element.
235 FormData form_data; 295 FormData form_data;
236 FormFieldData field; 296 FormFieldData field;
237 EXPECT_TRUE(FindFormAndFieldForFormControlElement( 297 EXPECT_TRUE(FindFormAndField(
238 input_element, &form_data, &field, autofill::REQUIRE_NONE)); 298 web_frame, input_element, unowned, REQUIRE_NONE, &form_data, &field));
239 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name); 299 if (!unowned) {
240 EXPECT_EQ(GURL(web_frame->document().url()), form_data.origin); 300 EXPECT_EQ(ASCIIToUTF16("TestForm"), form_data.name);
241 EXPECT_EQ(GURL("http://buh.com"), form_data.action); 301 EXPECT_EQ(GURL("http://buh.com"), form_data.action);
302 }
242 303
243 const std::vector<FormFieldData>& fields = form_data.fields; 304 const std::vector<FormFieldData>& fields = form_data.fields;
244 ASSERT_EQ(number_of_field_cases, fields.size()); 305 ASSERT_EQ(number_of_field_cases, fields.size());
245 306
246 FormFieldData expected; 307 FormFieldData expected;
247 // Verify field's initial value. 308 // Verify field's initial value.
248 for (size_t i = 0; i < number_of_field_cases; ++i) { 309 for (size_t i = 0; i < number_of_field_cases; ++i) {
249 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s", 310 SCOPED_TRACE(base::StringPrintf("Verify initial value for field %s",
250 field_cases[i].name)); 311 field_cases[i].name));
251 expected.form_control_type = field_cases[i].form_control_type; 312 expected.form_control_type = field_cases[i].form_control_type;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 WebFormControlElement GetFormControlElementById(const WebString& id) { 361 WebFormControlElement GetFormControlElementById(const WebString& id) {
301 return GetMainFrame()->document().getElementById( 362 return GetMainFrame()->document().getElementById(
302 id).to<WebFormControlElement>(); 363 id).to<WebFormControlElement>();
303 } 364 }
304 365
305 WebInputElement GetInputElementById(const WebString& id) { 366 WebInputElement GetInputElementById(const WebString& id) {
306 return GetMainFrame()->document().getElementById( 367 return GetMainFrame()->document().getElementById(
307 id).to<WebInputElement>(); 368 id).to<WebInputElement>();
308 } 369 }
309 370
371 void TestFillForm(const char* html, bool unowned) {
372 static const AutofillFieldCase field_cases[] = {
373 // fields: form_control_type, name, initial_value, autocomplete_attribute,
374 // should_be_autofilled, autofill_value, expected_value
375
376 // Regular empty fields (firstname & lastname) should be autofilled.
377 {"text",
378 "firstname",
379 "",
380 "",
381 true,
382 "filled firstname",
383 "filled firstname"},
384 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
385 // hidden fields should not be extracted to form_data.
386 // Non empty fields should not be autofilled.
387 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
388 {"text",
389 "noautocomplete",
390 "",
391 "off",
392 true,
393 "filled noautocomplete",
394 "filled noautocomplete"},
395 // Disabled fields should not be autofilled.
396 {"text", "notenabled", "", "", false, "filled notenabled", ""},
397 // Readonly fields should not be autofilled.
398 {"text", "readonly", "", "", false, "filled readonly", ""},
399 // Fields with "visibility: hidden" should not be autofilled.
400 {"text", "invisible", "", "", false, "filled invisible", ""},
401 // Fields with "display:none" should not be autofilled.
402 {"text", "displaynone", "", "", false, "filled displaynone", ""},
403 // Regular <input type="month"> should be autofilled.
404 {"month", "month", "", "", true, "2017-11", "2017-11"},
405 // Non-empty <input type="month"> should not be autofilled.
406 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
407 // Regular select fields should be autofilled.
408 {"select-one", "select", "", "", true, "TX", "TX"},
409 // Select fields should be autofilled even if they already have a
410 // non-empty value.
411 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
412 // Select fields should not be autofilled if no new value is passed from
413 // autofill profile. The existing value should not be overriden.
414 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
415 // Regular textarea elements should be autofilled.
416 {"textarea",
417 "textarea",
418 "",
419 "",
420 true,
421 "some multi-\nline value",
422 "some multi-\nline value"},
423 // Non-empty textarea elements should not be autofilled.
424 {"textarea",
425 "textarea-nonempty",
426 "Go\naway!",
427 "",
428 false,
429 "some multi-\nline value",
430 "Go\naway!"},
431 };
432 TestFormFillFunctions(html, unowned, field_cases, arraysize(field_cases),
433 FillForm, &GetValueWrapper);
434 // Verify preview selection.
435 WebInputElement firstname = GetInputElementById("firstname");
436 EXPECT_EQ(16, firstname.selectionStart());
437 EXPECT_EQ(16, firstname.selectionEnd());
438 }
439
440 void TestPreviewForm(const char* html, bool unowned) {
441 static const AutofillFieldCase field_cases[] = {
442 // Normal empty fields should be previewed.
443 {"text",
444 "firstname",
445 "",
446 "",
447 true,
448 "suggested firstname",
449 "suggested firstname"},
450 {"text",
451 "lastname",
452 "",
453 "",
454 true,
455 "suggested lastname",
456 "suggested lastname"},
457 // Hidden fields should not be extracted to form_data.
458 // Non empty fields should not be previewed.
459 {"text", "notempty", "Hi", "", false, "suggested notempty", ""},
460 {"text",
461 "noautocomplete",
462 "",
463 "off",
464 true,
465 "filled noautocomplete",
466 "filled noautocomplete"},
467 // Disabled fields should not be previewed.
468 {"text", "notenabled", "", "", false, "suggested notenabled", ""},
469 // Readonly fields should not be previewed.
470 {"text", "readonly", "", "", false, "suggested readonly", ""},
471 // Fields with "visibility: hidden" should not be previewed.
472 {"text", "invisible", "", "", false, "suggested invisible", ""},
473 // Fields with "display:none" should not previewed.
474 {"text", "displaynone", "", "", false, "suggested displaynone", ""},
475 // Regular <input type="month"> should be previewed.
476 {"month", "month", "", "", true, "2017-11", "2017-11"},
477 // Non-empty <input type="month"> should not be previewed.
478 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
479 // Regular select fields should be previewed.
480 {"select-one", "select", "", "", true, "TX", "TX"},
481 // Select fields should be previewed even if they already have a
482 // non-empty value.
483 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
484 // Select fields should not be previewed if no suggestion is passed from
485 // autofill profile.
486 {"select-one", "select-unchanged", "CA", "", false, "", ""},
487 // Normal textarea elements should be previewed.
488 {"textarea",
489 "textarea",
490 "",
491 "",
492 true,
493 "suggested multi-\nline value",
494 "suggested multi-\nline value"},
495 // Nonempty textarea elements should not be previewed.
496 {"textarea",
497 "textarea-nonempty",
498 "Go\naway!",
499 "",
500 false,
501 "suggested multi-\nline value",
502 ""},
503 };
504 TestFormFillFunctions(html, unowned, field_cases, arraysize(field_cases),
505 &PreviewForm, &GetSuggestedValueWrapper);
506
507 // Verify preview selection.
508 WebInputElement firstname = GetInputElementById("firstname");
509 EXPECT_EQ(0, firstname.selectionStart());
510 EXPECT_EQ(19, firstname.selectionEnd());
511 }
512
513 void TestFindFormForInputElement(const char* html, bool unowned) {
514 LoadHTML(html);
515 WebFrame* web_frame = GetMainFrame();
516 ASSERT_NE(nullptr, web_frame);
517
518 FormCache form_cache;
519 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
520 ASSERT_EQ(1U, forms.size());
521
522 // Get the input element we want to find.
523 WebInputElement input_element = GetInputElementById("firstname");
524
525 // Find the form and verify it's the correct form.
526 FormData form;
527 FormFieldData field;
528 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
529 REQUIRE_NONE, &form, &field));
530 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
531 if (!unowned) {
532 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
533 EXPECT_EQ(GURL("http://buh.com"), form.action);
534 }
535
536 const std::vector<FormFieldData>& fields = form.fields;
537 ASSERT_EQ(4U, fields.size());
538
539 FormFieldData expected;
540 expected.form_control_type = "text";
541 expected.max_length = WebInputElement::defaultMaxLength();
542
543 expected.name = ASCIIToUTF16("firstname");
544 expected.value = ASCIIToUTF16("John");
545 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
546 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
547
548 expected.name = ASCIIToUTF16("lastname");
549 expected.value = ASCIIToUTF16("Smith");
550 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
551
552 expected.name = ASCIIToUTF16("email");
553 expected.value = ASCIIToUTF16("john@example.com");
554 expected.autocomplete_attribute = "off";
555 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
556 expected.autocomplete_attribute.clear();
557
558 expected.name = ASCIIToUTF16("phone");
559 expected.value = ASCIIToUTF16("1.800.555.1234");
560 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
561
562 // Try again, but require autocomplete.
563 FormData form2;
564 FormFieldData field2;
565 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
566 REQUIRE_AUTOCOMPLETE, &form2, &field2));
567 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
568 if (!unowned) {
569 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
570 EXPECT_EQ(GURL("http://buh.com"), form2.action);
571 }
572
573 const std::vector<FormFieldData>& fields2 = form2.fields;
574 ASSERT_EQ(3U, fields2.size());
575
576 expected.form_control_type = "text";
577 expected.max_length = WebInputElement::defaultMaxLength();
578
579 expected.name = ASCIIToUTF16("firstname");
580 expected.value = ASCIIToUTF16("John");
581 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
582 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
583
584 expected.name = ASCIIToUTF16("lastname");
585 expected.value = ASCIIToUTF16("Smith");
586 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
587
588 expected.name = ASCIIToUTF16("phone");
589 expected.value = ASCIIToUTF16("1.800.555.1234");
590 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
591 }
592
593 void TestFindFormForTextAreaElement(const char* html, bool unowned) {
594 LoadHTML(html);
595 WebFrame* web_frame = GetMainFrame();
596 ASSERT_NE(nullptr, web_frame);
597
598 FormCache form_cache;
599 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
600 ASSERT_EQ(1U, forms.size());
601
602 // Get the textarea element we want to find.
603 WebElement element = web_frame->document().getElementById("street-address");
604 WebTextAreaElement textarea_element = element.to<WebTextAreaElement>();
605
606 // Find the form and verify it's the correct form.
607 FormData form;
608 FormFieldData field;
609 EXPECT_TRUE(FindFormAndField(web_frame, textarea_element, unowned,
610 REQUIRE_NONE, &form, &field));
611 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
612 if (!unowned) {
613 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
614 EXPECT_EQ(GURL("http://buh.com"), form.action);
615 }
616
617 const std::vector<FormFieldData>& fields = form.fields;
618 ASSERT_EQ(4U, fields.size());
619
620 FormFieldData expected;
621
622 expected.name = ASCIIToUTF16("firstname");
623 expected.value = ASCIIToUTF16("John");
624 expected.form_control_type = "text";
625 expected.max_length = WebInputElement::defaultMaxLength();
626 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
627
628 expected.name = ASCIIToUTF16("lastname");
629 expected.value = ASCIIToUTF16("Smith");
630 expected.form_control_type = "text";
631 expected.max_length = WebInputElement::defaultMaxLength();
632 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
633
634 expected.name = ASCIIToUTF16("email");
635 expected.value = ASCIIToUTF16("john@example.com");
636 expected.autocomplete_attribute = "off";
637 expected.form_control_type = "text";
638 expected.max_length = WebInputElement::defaultMaxLength();
639 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
640 expected.autocomplete_attribute.clear();
641
642 expected.name = ASCIIToUTF16("street-address");
643 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
644 expected.form_control_type = "textarea";
645 expected.max_length = 0;
646 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
647 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
648
649 // Try again, but require autocomplete.
650 FormData form2;
651 FormFieldData field2;
652 EXPECT_TRUE(FindFormAndField(web_frame, textarea_element, unowned,
653 REQUIRE_AUTOCOMPLETE, &form2, &field2));
654 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
655 if (!unowned) {
656 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
657 EXPECT_EQ(GURL("http://buh.com"), form2.action);
658 }
659
660 const std::vector<FormFieldData>& fields2 = form2.fields;
661 ASSERT_EQ(3U, fields2.size());
662
663 expected.name = ASCIIToUTF16("firstname");
664 expected.value = ASCIIToUTF16("John");
665 expected.form_control_type = "text";
666 expected.max_length = WebInputElement::defaultMaxLength();
667 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
668
669 expected.name = ASCIIToUTF16("lastname");
670 expected.value = ASCIIToUTF16("Smith");
671 expected.form_control_type = "text";
672 expected.max_length = WebInputElement::defaultMaxLength();
673 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
674
675 expected.name = ASCIIToUTF16("street-address");
676 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
677 expected.form_control_type = "textarea";
678 expected.max_length = 0;
679 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
680 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
681 }
682
683 void TestFillFormMaxLength(const char* html, bool unowned) {
684 LoadHTML(html);
685 WebFrame* web_frame = GetMainFrame();
686 ASSERT_NE(nullptr, web_frame);
687
688 FormCache form_cache;
689 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
690 ASSERT_EQ(1U, forms.size());
691
692 // Get the input element we want to find.
693 WebInputElement input_element = GetInputElementById("firstname");
694
695 // Find the form that contains the input element.
696 FormData form;
697 FormFieldData field;
698 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
699 REQUIRE_NONE, &form, &field));
700 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
701 if (!unowned) {
702 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
703 EXPECT_EQ(GURL("http://buh.com"), form.action);
704 }
705
706 const std::vector<FormFieldData>& fields = form.fields;
707 ASSERT_EQ(3U, fields.size());
708
709 FormFieldData expected;
710 expected.form_control_type = "text";
711
712 expected.name = ASCIIToUTF16("firstname");
713 expected.max_length = 5;
714 expected.is_autofilled = false;
715 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
716
717 expected.name = ASCIIToUTF16("lastname");
718 expected.max_length = 7;
719 expected.is_autofilled = false;
720 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
721
722 expected.name = ASCIIToUTF16("email");
723 expected.max_length = 9;
724 expected.is_autofilled = false;
725 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
726
727 // Fill the form.
728 form.fields[0].value = ASCIIToUTF16("Brother");
729 form.fields[1].value = ASCIIToUTF16("Jonathan");
730 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
731 form.fields[0].is_autofilled = true;
732 form.fields[1].is_autofilled = true;
733 form.fields[2].is_autofilled = true;
734 FillForm(form, input_element);
735
736 // Find the newly-filled form that contains the input element.
737 FormData form2;
738 FormFieldData field2;
739 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
740 REQUIRE_NONE, &form2, &field2));
741 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
742 if (!unowned) {
743 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
744 EXPECT_EQ(GURL("http://buh.com"), form2.action);
745 }
746
747 const std::vector<FormFieldData>& fields2 = form2.fields;
748 ASSERT_EQ(3U, fields2.size());
749
750 expected.form_control_type = "text";
751
752 expected.name = ASCIIToUTF16("firstname");
753 expected.value = ASCIIToUTF16("Broth");
754 expected.max_length = 5;
755 expected.is_autofilled = true;
756 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
757
758 expected.name = ASCIIToUTF16("lastname");
759 expected.value = ASCIIToUTF16("Jonatha");
760 expected.max_length = 7;
761 expected.is_autofilled = true;
762 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
763
764 expected.name = ASCIIToUTF16("email");
765 expected.value = ASCIIToUTF16("brotherj@");
766 expected.max_length = 9;
767 expected.is_autofilled = true;
768 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
769 }
770
771 void TestFillFormNegativeMaxLength(const char* html, bool unowned) {
772 LoadHTML(html);
773 WebFrame* web_frame = GetMainFrame();
774 ASSERT_NE(nullptr, web_frame);
775
776 FormCache form_cache;
777 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
778 ASSERT_EQ(1U, forms.size());
779
780 // Get the input element we want to find.
781 WebInputElement input_element = GetInputElementById("firstname");
782
783 // Find the form that contains the input element.
784 FormData form;
785 FormFieldData field;
786 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
787 REQUIRE_NONE, &form, &field));
788 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
789 if (!unowned) {
790 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
791 EXPECT_EQ(GURL("http://buh.com"), form.action);
792 }
793
794 const std::vector<FormFieldData>& fields = form.fields;
795 ASSERT_EQ(3U, fields.size());
796
797 FormFieldData expected;
798 expected.form_control_type = "text";
799 expected.max_length = WebInputElement::defaultMaxLength();
800
801 expected.name = ASCIIToUTF16("firstname");
802 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
803
804 expected.name = ASCIIToUTF16("lastname");
805 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
806
807 expected.name = ASCIIToUTF16("email");
808 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
809
810 // Fill the form.
811 form.fields[0].value = ASCIIToUTF16("Brother");
812 form.fields[1].value = ASCIIToUTF16("Jonathan");
813 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
814 FillForm(form, input_element);
815
816 // Find the newly-filled form that contains the input element.
817 FormData form2;
818 FormFieldData field2;
819 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
820 REQUIRE_NONE, &form2, &field2));
821 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
822 if (!unowned) {
823 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
824 EXPECT_EQ(GURL("http://buh.com"), form2.action);
825 }
826
827 const std::vector<FormFieldData>& fields2 = form2.fields;
828 ASSERT_EQ(3U, fields2.size());
829
830 expected.name = ASCIIToUTF16("firstname");
831 expected.value = ASCIIToUTF16("Brother");
832 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
833
834 expected.name = ASCIIToUTF16("lastname");
835 expected.value = ASCIIToUTF16("Jonathan");
836 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
837
838 expected.name = ASCIIToUTF16("email");
839 expected.value = ASCIIToUTF16("brotherj@example.com");
840 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
841 }
842
843 void TestFillFormEmptyName(const char* html, bool unowned) {
844 LoadHTML(html);
845 WebFrame* web_frame = GetMainFrame();
846 ASSERT_NE(nullptr, web_frame);
847
848 FormCache form_cache;
849 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
850 ASSERT_EQ(1U, forms.size());
851
852 // Get the input element we want to find.
853 WebInputElement input_element = GetInputElementById("firstname");
854
855 // Find the form that contains the input element.
856 FormData form;
857 FormFieldData field;
858 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
859 REQUIRE_NONE, &form, &field));
860 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
861 if (!unowned) {
862 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
863 EXPECT_EQ(GURL("http://buh.com"), form.action);
864 }
865
866 const std::vector<FormFieldData>& fields = form.fields;
867 ASSERT_EQ(3U, fields.size());
868
869 FormFieldData expected;
870 expected.form_control_type = "text";
871 expected.max_length = WebInputElement::defaultMaxLength();
872
873 expected.name = ASCIIToUTF16("firstname");
874 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
875
876 expected.name = ASCIIToUTF16("lastname");
877 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
878
879 expected.name = ASCIIToUTF16("email");
880 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
881
882 // Fill the form.
883 form.fields[0].value = ASCIIToUTF16("Wyatt");
884 form.fields[1].value = ASCIIToUTF16("Earp");
885 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
886 FillForm(form, input_element);
887
888 // Find the newly-filled form that contains the input element.
889 FormData form2;
890 FormFieldData field2;
891 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
892 REQUIRE_NONE, &form2, &field2));
893 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
894 if (!unowned) {
895 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
896 EXPECT_EQ(GURL("http://buh.com"), form2.action);
897 }
898
899 const std::vector<FormFieldData>& fields2 = form2.fields;
900 ASSERT_EQ(3U, fields2.size());
901
902 expected.form_control_type = "text";
903 expected.max_length = WebInputElement::defaultMaxLength();
904
905 expected.name = ASCIIToUTF16("firstname");
906 expected.value = ASCIIToUTF16("Wyatt");
907 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
908
909 expected.name = ASCIIToUTF16("lastname");
910 expected.value = ASCIIToUTF16("Earp");
911 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
912
913 expected.name = ASCIIToUTF16("email");
914 expected.value = ASCIIToUTF16("wyatt@example.com");
915 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
916 }
917
918 void TestFillFormEmptyFormNames(const char* html, bool unowned) {
919 LoadHTML(html);
920 WebFrame* web_frame = GetMainFrame();
921 ASSERT_NE(nullptr, web_frame);
922
923 FormCache form_cache;
924 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
925 const size_t expected_size = unowned ? 1 : 2;
926 ASSERT_EQ(expected_size, forms.size());
927
928 // Get the input element we want to find.
929 WebInputElement input_element = GetInputElementById("apple");
930
931 // Find the form that contains the input element.
932 FormData form;
933 FormFieldData field;
934 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
935 REQUIRE_NONE, &form, &field));
936 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
937 if (!unowned) {
938 EXPECT_TRUE(form.name.empty());
939 EXPECT_EQ(GURL("http://abc.com"), form.action);
940 }
941
942 const std::vector<FormFieldData>& fields = form.fields;
943 const size_t unowned_offset = unowned ? 3 : 0;
944 ASSERT_EQ(unowned_offset + 3, fields.size());
945
946 FormFieldData expected;
947 expected.form_control_type = "text";
948 expected.max_length = WebInputElement::defaultMaxLength();
949
950 expected.name = ASCIIToUTF16("apple");
951 expected.is_autofilled = false;
952 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[unowned_offset]);
953
954 expected.name = ASCIIToUTF16("banana");
955 expected.is_autofilled = false;
956 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[unowned_offset + 1]);
957
958 expected.name = ASCIIToUTF16("cantelope");
959 expected.is_autofilled = false;
960 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[unowned_offset + 2]);
961
962 // Fill the form.
963 form.fields[unowned_offset + 0].value = ASCIIToUTF16("Red");
964 form.fields[unowned_offset + 1].value = ASCIIToUTF16("Yellow");
965 form.fields[unowned_offset + 2].value = ASCIIToUTF16("Also Yellow");
966 form.fields[unowned_offset + 0].is_autofilled = true;
967 form.fields[unowned_offset + 1].is_autofilled = true;
968 form.fields[unowned_offset + 2].is_autofilled = true;
969 FillForm(form, input_element);
970
971 // Find the newly-filled form that contains the input element.
972 FormData form2;
973 FormFieldData field2;
974 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
975 REQUIRE_NONE, &form2, &field2));
976 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
977 if (!unowned) {
978 EXPECT_TRUE(form2.name.empty());
979 EXPECT_EQ(GURL("http://abc.com"), form2.action);
980 }
981
982 const std::vector<FormFieldData>& fields2 = form2.fields;
983 ASSERT_EQ(unowned_offset + 3, fields2.size());
984
985 expected.name = ASCIIToUTF16("apple");
986 expected.value = ASCIIToUTF16("Red");
987 expected.is_autofilled = true;
988 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[unowned_offset + 0]);
989
990 expected.name = ASCIIToUTF16("banana");
991 expected.value = ASCIIToUTF16("Yellow");
992 expected.is_autofilled = true;
993 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[unowned_offset + 1]);
994
995 expected.name = ASCIIToUTF16("cantelope");
996 expected.value = ASCIIToUTF16("Also Yellow");
997 expected.is_autofilled = true;
998 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[unowned_offset + 2]);
999 }
1000
1001 void TestFillFormNonEmptyField(const char* html, bool unowned) {
1002 LoadHTML(html);
1003 WebFrame* web_frame = GetMainFrame();
1004 ASSERT_NE(nullptr, web_frame);
1005
1006 FormCache form_cache;
1007 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1008 ASSERT_EQ(1U, forms.size());
1009
1010 // Get the input element we want to find.
1011 WebInputElement input_element = GetInputElementById("firstname");
1012
1013 // Simulate typing by modifying the field value.
1014 input_element.setValue(ASCIIToUTF16("Wy"));
1015
1016 // Find the form that contains the input element.
1017 FormData form;
1018 FormFieldData field;
1019 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
1020 REQUIRE_NONE, &form, &field));
1021 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1022 if (!unowned) {
1023 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1024 EXPECT_EQ(GURL("http://buh.com"), form.action);
1025 }
1026
1027 const std::vector<FormFieldData>& fields = form.fields;
1028 ASSERT_EQ(3U, fields.size());
1029
1030 FormFieldData expected;
1031 expected.form_control_type = "text";
1032 expected.max_length = WebInputElement::defaultMaxLength();
1033
1034 expected.name = ASCIIToUTF16("firstname");
1035 expected.value = ASCIIToUTF16("Wy");
1036 expected.is_autofilled = false;
1037 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1038
1039 expected.name = ASCIIToUTF16("lastname");
1040 expected.value.clear();
1041 expected.is_autofilled = false;
1042 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1043
1044 expected.name = ASCIIToUTF16("email");
1045 expected.value.clear();
1046 expected.is_autofilled = false;
1047 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1048
1049 // Preview the form and verify that the cursor position has been updated.
1050 form.fields[0].value = ASCIIToUTF16("Wyatt");
1051 form.fields[1].value = ASCIIToUTF16("Earp");
1052 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
1053 form.fields[0].is_autofilled = true;
1054 form.fields[1].is_autofilled = true;
1055 form.fields[2].is_autofilled = true;
1056 PreviewForm(form, input_element);
1057 EXPECT_EQ(2, input_element.selectionStart());
1058 EXPECT_EQ(5, input_element.selectionEnd());
1059
1060 // Fill the form.
1061 FillForm(form, input_element);
1062
1063 // Find the newly-filled form that contains the input element.
1064 FormData form2;
1065 FormFieldData field2;
1066 EXPECT_TRUE(FindFormAndField(web_frame, input_element, unowned,
1067 REQUIRE_NONE, &form2, &field2));
1068 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1069 if (!unowned) {
1070 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1071 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1072 }
1073
1074 const std::vector<FormFieldData>& fields2 = form2.fields;
1075 ASSERT_EQ(3U, fields2.size());
1076
1077 expected.name = ASCIIToUTF16("firstname");
1078 expected.value = ASCIIToUTF16("Wyatt");
1079 expected.is_autofilled = true;
1080 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1081
1082 expected.name = ASCIIToUTF16("lastname");
1083 expected.value = ASCIIToUTF16("Earp");
1084 expected.is_autofilled = true;
1085 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1086
1087 expected.name = ASCIIToUTF16("email");
1088 expected.value = ASCIIToUTF16("wyatt@example.com");
1089 expected.is_autofilled = true;
1090 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1091
1092 // Verify that the cursor position has been updated.
1093 EXPECT_EQ(5, input_element.selectionStart());
1094 EXPECT_EQ(5, input_element.selectionEnd());
1095 }
1096
1097 void TestClearFormWithNode(const char* html, bool unowned) {
1098 LoadHTML(html);
1099 WebFrame* web_frame = GetMainFrame();
1100 ASSERT_NE(nullptr, web_frame);
1101
1102 FormCache form_cache;
1103 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1104 ASSERT_EQ(1U, forms.size());
1105
1106 // Set the auto-filled attribute.
1107 WebInputElement firstname = GetInputElementById("firstname");
1108 firstname.setAutofilled(true);
1109 WebInputElement lastname = GetInputElementById("lastname");
1110 lastname.setAutofilled(true);
1111 WebInputElement month = GetInputElementById("month");
1112 month.setAutofilled(true);
1113 WebInputElement textarea = GetInputElementById("textarea");
1114 textarea.setAutofilled(true);
1115
1116 // Set the value of the disabled text input element.
1117 WebInputElement notenabled = GetInputElementById("notenabled");
1118 notenabled.setValue(WebString::fromUTF8("no clear"));
1119
1120 // Clear the form.
1121 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
1122
1123 // Verify that the auto-filled attribute has been turned off.
1124 EXPECT_FALSE(firstname.isAutofilled());
1125
1126 // Verify the form is cleared.
1127 FormData form;
1128 FormFieldData field;
1129 EXPECT_TRUE(FindFormAndField(web_frame, firstname, unowned, REQUIRE_NONE,
1130 &form, &field));
1131 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1132 if (!unowned) {
1133 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1134 EXPECT_EQ(GURL("http://buh.com"), form.action);
1135 }
1136
1137 const std::vector<FormFieldData>& fields = form.fields;
1138 ASSERT_EQ(9U, fields.size());
1139
1140 FormFieldData expected;
1141 expected.form_control_type = "text";
1142 expected.max_length = WebInputElement::defaultMaxLength();
1143
1144 expected.name = ASCIIToUTF16("firstname");
1145 expected.value.clear();
1146 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1147
1148 expected.name = ASCIIToUTF16("lastname");
1149 expected.value.clear();
1150 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1151
1152 expected.name = ASCIIToUTF16("noAC");
1153 expected.value = ASCIIToUTF16("one");
1154 expected.autocomplete_attribute = "off";
1155 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1156 expected.autocomplete_attribute.clear();
1157
1158 expected.name = ASCIIToUTF16("notenabled");
1159 expected.value = ASCIIToUTF16("no clear");
1160 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1161
1162 expected.form_control_type = "month";
1163 expected.max_length = 0;
1164 expected.name = ASCIIToUTF16("month");
1165 expected.value.clear();
1166 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
1167
1168 expected.name = ASCIIToUTF16("month-disabled");
1169 expected.value = ASCIIToUTF16("2012-11");
1170 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
1171
1172 expected.form_control_type = "textarea";
1173 expected.name = ASCIIToUTF16("textarea");
1174 expected.value.clear();
1175 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[6]);
1176
1177 expected.name = ASCIIToUTF16("textarea-disabled");
1178 expected.value = ASCIIToUTF16(" Banana! ");
1179 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[7]);
1180
1181 expected.name = ASCIIToUTF16("textarea-noAC");
1182 expected.value = ASCIIToUTF16("Carrot?");
1183 expected.autocomplete_attribute = "off";
1184 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[8]);
1185 expected.autocomplete_attribute.clear();
1186
1187 // Verify that the cursor position has been updated.
1188 EXPECT_EQ(0, firstname.selectionStart());
1189 EXPECT_EQ(0, firstname.selectionEnd());
1190 }
1191
1192 void TestClearFormWithNodeContainingSelectOne(const char* html,
1193 bool unowned) {
1194 LoadHTML(html);
1195 WebFrame* web_frame = GetMainFrame();
1196 ASSERT_NE(nullptr, web_frame);
1197
1198 FormCache form_cache;
1199 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1200 ASSERT_EQ(1U, forms.size());
1201
1202 // Set the auto-filled attribute.
1203 WebInputElement firstname = GetInputElementById("firstname");
1204 firstname.setAutofilled(true);
1205 WebInputElement lastname = GetInputElementById("lastname");
1206 lastname.setAutofilled(true);
1207
1208 // Set the value and auto-filled attribute of the state element.
1209 WebSelectElement state =
1210 web_frame->document().getElementById("state").to<WebSelectElement>();
1211 state.setValue(WebString::fromUTF8("AK"));
1212 state.setAutofilled(true);
1213
1214 // Clear the form.
1215 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
1216
1217 // Verify that the auto-filled attribute has been turned off.
1218 EXPECT_FALSE(firstname.isAutofilled());
1219
1220 // Verify the form is cleared.
1221 FormData form;
1222 FormFieldData field;
1223 EXPECT_TRUE(FindFormAndField(web_frame, firstname, unowned, REQUIRE_NONE,
1224 &form, &field));
1225 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1226 if (!unowned) {
1227 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1228 EXPECT_EQ(GURL("http://buh.com"), form.action);
1229 }
1230
1231 const std::vector<FormFieldData>& fields = form.fields;
1232 ASSERT_EQ(3U, fields.size());
1233
1234 FormFieldData expected;
1235
1236 expected.name = ASCIIToUTF16("firstname");
1237 expected.value.clear();
1238 expected.form_control_type = "text";
1239 expected.max_length = WebInputElement::defaultMaxLength();
1240 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1241
1242 expected.name = ASCIIToUTF16("lastname");
1243 expected.value.clear();
1244 expected.form_control_type = "text";
1245 expected.max_length = WebInputElement::defaultMaxLength();
1246 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1247
1248 expected.name = ASCIIToUTF16("state");
1249 expected.value = ASCIIToUTF16("?");
1250 expected.form_control_type = "select-one";
1251 expected.max_length = 0;
1252 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1253
1254 // Verify that the cursor position has been updated.
1255 EXPECT_EQ(0, firstname.selectionStart());
1256 EXPECT_EQ(0, firstname.selectionEnd());
1257 }
1258
1259 void TestClearPreviewedFormWithElement(const char* html) {
1260 LoadHTML(html);
1261 WebFrame* web_frame = GetMainFrame();
1262 ASSERT_NE(nullptr, web_frame);
1263
1264 FormCache form_cache;
1265 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1266 ASSERT_EQ(1U, forms.size());
1267
1268 // Set the auto-filled attribute.
1269 WebInputElement firstname = GetInputElementById("firstname");
1270 firstname.setAutofilled(true);
1271 WebInputElement lastname = GetInputElementById("lastname");
1272 lastname.setAutofilled(true);
1273 WebInputElement email = GetInputElementById("email");
1274 email.setAutofilled(true);
1275 WebInputElement email2 = GetInputElementById("email2");
1276 email2.setAutofilled(true);
1277 WebInputElement phone = GetInputElementById("phone");
1278 phone.setAutofilled(true);
1279
1280 // Set the suggested values on two of the elements.
1281 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
1282 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1283 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1284 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1285
1286 // Clear the previewed fields.
1287 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
1288
1289 // Fields with empty suggestions suggestions are not modified.
1290 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
1291 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1292 EXPECT_TRUE(firstname.isAutofilled());
1293
1294 // Verify the previewed fields are cleared.
1295 EXPECT_TRUE(lastname.value().isEmpty());
1296 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1297 EXPECT_FALSE(lastname.isAutofilled());
1298 EXPECT_TRUE(email.value().isEmpty());
1299 EXPECT_TRUE(email.suggestedValue().isEmpty());
1300 EXPECT_FALSE(email.isAutofilled());
1301 EXPECT_TRUE(email2.value().isEmpty());
1302 EXPECT_TRUE(email2.suggestedValue().isEmpty());
1303 EXPECT_FALSE(email2.isAutofilled());
1304 EXPECT_TRUE(phone.value().isEmpty());
1305 EXPECT_TRUE(phone.suggestedValue().isEmpty());
1306 EXPECT_FALSE(phone.isAutofilled());
1307
1308 // Verify that the cursor position has been updated.
1309 EXPECT_EQ(0, lastname.selectionStart());
1310 EXPECT_EQ(0, lastname.selectionEnd());
1311 }
1312
1313 void TestClearPreviewedFormWithNonEmptyInitiatingNode(const char* html) {
1314 LoadHTML(html);
1315 WebFrame* web_frame = GetMainFrame();
1316 ASSERT_NE(nullptr, web_frame);
1317
1318 FormCache form_cache;
1319 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1320 ASSERT_EQ(1U, forms.size());
1321
1322 // Set the auto-filled attribute.
1323 WebInputElement firstname = GetInputElementById("firstname");
1324 firstname.setAutofilled(true);
1325 WebInputElement lastname = GetInputElementById("lastname");
1326 lastname.setAutofilled(true);
1327 WebInputElement email = GetInputElementById("email");
1328 email.setAutofilled(true);
1329 WebInputElement email2 = GetInputElementById("email2");
1330 email2.setAutofilled(true);
1331 WebInputElement phone = GetInputElementById("phone");
1332 phone.setAutofilled(true);
1333
1334
1335 // Set the suggested values on all of the elements.
1336 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
1337 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
1338 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1339 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1340 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1341
1342 // Clear the previewed fields.
1343 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
1344
1345 // Fields with non-empty values are restored.
1346 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
1347 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1348 EXPECT_FALSE(firstname.isAutofilled());
1349 EXPECT_EQ(1, firstname.selectionStart());
1350 EXPECT_EQ(1, firstname.selectionEnd());
1351
1352 // Verify the previewed fields are cleared.
1353 EXPECT_TRUE(lastname.value().isEmpty());
1354 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1355 EXPECT_FALSE(lastname.isAutofilled());
1356 EXPECT_TRUE(email.value().isEmpty());
1357 EXPECT_TRUE(email.suggestedValue().isEmpty());
1358 EXPECT_FALSE(email.isAutofilled());
1359 EXPECT_TRUE(email2.value().isEmpty());
1360 EXPECT_TRUE(email2.suggestedValue().isEmpty());
1361 EXPECT_FALSE(email2.isAutofilled());
1362 EXPECT_TRUE(phone.value().isEmpty());
1363 EXPECT_TRUE(phone.suggestedValue().isEmpty());
1364 EXPECT_FALSE(phone.isAutofilled());
1365 }
1366
1367 void TestClearPreviewedFormWithAutofilledInitiatingNode(const char* html) {
1368 LoadHTML(html);
1369 WebFrame* web_frame = GetMainFrame();
1370 ASSERT_NE(nullptr, web_frame);
1371
1372 FormCache form_cache;
1373 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1374 ASSERT_EQ(1U, forms.size());
1375
1376 // Set the auto-filled attribute.
1377 WebInputElement firstname = GetInputElementById("firstname");
1378 firstname.setAutofilled(true);
1379 WebInputElement lastname = GetInputElementById("lastname");
1380 lastname.setAutofilled(true);
1381 WebInputElement email = GetInputElementById("email");
1382 email.setAutofilled(true);
1383 WebInputElement email2 = GetInputElementById("email2");
1384 email2.setAutofilled(true);
1385 WebInputElement phone = GetInputElementById("phone");
1386 phone.setAutofilled(true);
1387
1388 // Set the suggested values on all of the elements.
1389 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
1390 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
1391 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1392 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
1393 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
1394
1395 // Clear the previewed fields.
1396 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
1397
1398 // Fields with non-empty values are restored.
1399 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
1400 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1401 EXPECT_TRUE(firstname.isAutofilled());
1402 EXPECT_EQ(1, firstname.selectionStart());
1403 EXPECT_EQ(1, firstname.selectionEnd());
1404
1405 // Verify the previewed fields are cleared.
1406 EXPECT_TRUE(lastname.value().isEmpty());
1407 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1408 EXPECT_FALSE(lastname.isAutofilled());
1409 EXPECT_TRUE(email.value().isEmpty());
1410 EXPECT_TRUE(email.suggestedValue().isEmpty());
1411 EXPECT_FALSE(email.isAutofilled());
1412 EXPECT_TRUE(email2.value().isEmpty());
1413 EXPECT_TRUE(email2.suggestedValue().isEmpty());
1414 EXPECT_FALSE(email2.isAutofilled());
1415 EXPECT_TRUE(phone.value().isEmpty());
1416 EXPECT_TRUE(phone.suggestedValue().isEmpty());
1417 EXPECT_FALSE(phone.isAutofilled());
1418 }
1419
1420 void TestClearOnlyAutofilledFields(const char* html) {
1421 LoadHTML(html);
1422
1423 WebFrame* web_frame = GetMainFrame();
1424 ASSERT_NE(nullptr, web_frame);
1425
1426 FormCache form_cache;
1427 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame);
1428 ASSERT_EQ(1U, forms.size());
1429
1430 // Set the autofilled attribute.
1431 WebInputElement firstname = GetInputElementById("firstname");
1432 firstname.setAutofilled(false);
1433 WebInputElement lastname = GetInputElementById("lastname");
1434 lastname.setAutofilled(true);
1435 WebInputElement email = GetInputElementById("email");
1436 email.setAutofilled(true);
1437 WebInputElement phone = GetInputElementById("phone");
1438 phone.setAutofilled(true);
1439
1440 // Clear the fields.
1441 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
1442
1443 // Verify only autofilled fields are cleared.
1444 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
1445 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
1446 EXPECT_FALSE(firstname.isAutofilled());
1447 EXPECT_TRUE(lastname.value().isEmpty());
1448 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
1449 EXPECT_FALSE(lastname.isAutofilled());
1450 EXPECT_TRUE(email.value().isEmpty());
1451 EXPECT_TRUE(email.suggestedValue().isEmpty());
1452 EXPECT_FALSE(email.isAutofilled());
1453 EXPECT_TRUE(phone.value().isEmpty());
1454 EXPECT_TRUE(phone.suggestedValue().isEmpty());
1455 EXPECT_FALSE(phone.isAutofilled());
1456 }
1457
310 static void FillFormIncludingNonFocusableElementsWrapper( 1458 static void FillFormIncludingNonFocusableElementsWrapper(
311 const FormData& form, 1459 const FormData& form,
312 const WebFormControlElement& element) { 1460 const WebFormControlElement& element) {
313 FillFormIncludingNonFocusableElements(form, element.form()); 1461 FillFormIncludingNonFocusableElements(form, element.form());
314 } 1462 }
315 1463
316 static WebString GetValueWrapper(WebFormControlElement element) { 1464 static WebString GetValueWrapper(WebFormControlElement element) {
317 if (element.formControlType() == "textarea") 1465 if (element.formControlType() == "textarea")
318 return element.to<WebTextAreaElement>().value(); 1466 return element.to<WebTextAreaElement>().value();
319 1467
(...skipping 19 matching lines...) Expand all
339 1487
340 // We should be able to extract a normal text field. 1488 // We should be able to extract a normal text field.
341 TEST_F(FormAutofillTest, WebFormControlElementToFormField) { 1489 TEST_F(FormAutofillTest, WebFormControlElementToFormField) {
342 LoadHTML("<INPUT type='text' id='element' value='value'/>"); 1490 LoadHTML("<INPUT type='text' id='element' value='value'/>");
343 1491
344 WebFrame* frame = GetMainFrame(); 1492 WebFrame* frame = GetMainFrame();
345 ASSERT_NE(nullptr, frame); 1493 ASSERT_NE(nullptr, frame);
346 1494
347 WebFormControlElement element = GetFormControlElementById("element"); 1495 WebFormControlElement element = GetFormControlElementById("element");
348 FormFieldData result1; 1496 FormFieldData result1;
349 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result1); 1497 WebFormControlElementToFormField(element, EXTRACT_NONE, &result1);
350 1498
351 FormFieldData expected; 1499 FormFieldData expected;
352 expected.form_control_type = "text"; 1500 expected.form_control_type = "text";
353 expected.max_length = WebInputElement::defaultMaxLength(); 1501 expected.max_length = WebInputElement::defaultMaxLength();
354 1502
355 expected.name = ASCIIToUTF16("element"); 1503 expected.name = ASCIIToUTF16("element");
356 expected.value = base::string16(); 1504 expected.value.clear();
357 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1); 1505 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
358 1506
359 FormFieldData result2; 1507 FormFieldData result2;
360 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result2); 1508 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result2);
361 1509
362 expected.name = ASCIIToUTF16("element"); 1510 expected.name = ASCIIToUTF16("element");
363 expected.value = ASCIIToUTF16("value"); 1511 expected.value = ASCIIToUTF16("value");
364 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2); 1512 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
365 } 1513 }
366 1514
367 // We should be able to extract a text field with autocomplete="off". 1515 // We should be able to extract a text field with autocomplete="off".
368 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompleteOff) { 1516 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutocompleteOff) {
369 LoadHTML("<INPUT type='text' id='element' value='value'" 1517 LoadHTML("<INPUT type='text' id='element' value='value'"
370 " autocomplete='off'/>"); 1518 " autocomplete='off'/>");
371 1519
372 WebFrame* frame = GetMainFrame(); 1520 WebFrame* frame = GetMainFrame();
373 ASSERT_NE(nullptr, frame); 1521 ASSERT_NE(nullptr, frame);
374 1522
375 WebFormControlElement element = GetFormControlElementById("element"); 1523 WebFormControlElement element = GetFormControlElementById("element");
376 FormFieldData result; 1524 FormFieldData result;
377 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1525 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
378 1526
379 FormFieldData expected; 1527 FormFieldData expected;
380 expected.name = ASCIIToUTF16("element"); 1528 expected.name = ASCIIToUTF16("element");
381 expected.value = ASCIIToUTF16("value"); 1529 expected.value = ASCIIToUTF16("value");
382 expected.form_control_type = "text"; 1530 expected.form_control_type = "text";
383 expected.autocomplete_attribute = "off"; 1531 expected.autocomplete_attribute = "off";
384 expected.max_length = WebInputElement::defaultMaxLength(); 1532 expected.max_length = WebInputElement::defaultMaxLength();
385 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1533 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
386 } 1534 }
387 1535
388 // We should be able to extract a text field with maxlength specified. 1536 // We should be able to extract a text field with maxlength specified.
389 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMaxLength) { 1537 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMaxLength) {
390 LoadHTML("<INPUT type='text' id='element' value='value'" 1538 LoadHTML("<INPUT type='text' id='element' value='value'"
391 " maxlength='5'/>"); 1539 " maxlength='5'/>");
392 1540
393 WebFrame* frame = GetMainFrame(); 1541 WebFrame* frame = GetMainFrame();
394 ASSERT_NE(nullptr, frame); 1542 ASSERT_NE(nullptr, frame);
395 1543
396 WebFormControlElement element = GetFormControlElementById("element"); 1544 WebFormControlElement element = GetFormControlElementById("element");
397 FormFieldData result; 1545 FormFieldData result;
398 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1546 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
399 1547
400 FormFieldData expected; 1548 FormFieldData expected;
401 expected.name = ASCIIToUTF16("element"); 1549 expected.name = ASCIIToUTF16("element");
402 expected.value = ASCIIToUTF16("value"); 1550 expected.value = ASCIIToUTF16("value");
403 expected.form_control_type = "text"; 1551 expected.form_control_type = "text";
404 expected.max_length = 5; 1552 expected.max_length = 5;
405 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1553 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
406 } 1554 }
407 1555
408 // We should be able to extract a text field that has been autofilled. 1556 // We should be able to extract a text field that has been autofilled.
409 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutofilled) { 1557 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldAutofilled) {
410 LoadHTML("<INPUT type='text' id='element' value='value'/>"); 1558 LoadHTML("<INPUT type='text' id='element' value='value'/>");
411 1559
412 WebFrame* frame = GetMainFrame(); 1560 WebFrame* frame = GetMainFrame();
413 ASSERT_NE(nullptr, frame); 1561 ASSERT_NE(nullptr, frame);
414 1562
415 WebInputElement element = GetInputElementById("element"); 1563 WebInputElement element = GetInputElementById("element");
416 element.setAutofilled(true); 1564 element.setAutofilled(true);
417 FormFieldData result; 1565 FormFieldData result;
418 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1566 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
419 1567
420 FormFieldData expected; 1568 FormFieldData expected;
421 expected.name = ASCIIToUTF16("element"); 1569 expected.name = ASCIIToUTF16("element");
422 expected.value = ASCIIToUTF16("value"); 1570 expected.value = ASCIIToUTF16("value");
423 expected.form_control_type = "text"; 1571 expected.form_control_type = "text";
424 expected.max_length = WebInputElement::defaultMaxLength(); 1572 expected.max_length = WebInputElement::defaultMaxLength();
425 expected.is_autofilled = true; 1573 expected.is_autofilled = true;
426 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1574 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
427 } 1575 }
428 1576
429 // We should be able to extract a radio or a checkbox field that has been 1577 // We should be able to extract a radio or a checkbox field that has been
430 // autofilled. 1578 // autofilled.
431 TEST_F(FormAutofillTest, WebFormControlElementToClickableFormField) { 1579 TEST_F(FormAutofillTest, WebFormControlElementToClickableFormField) {
432 LoadHTML("<INPUT type='checkbox' id='checkbox' value='mail' checked/>" 1580 LoadHTML("<INPUT type='checkbox' id='checkbox' value='mail' checked/>"
433 "<INPUT type='radio' id='radio' value='male'/>"); 1581 "<INPUT type='radio' id='radio' value='male'/>");
434 1582
435 WebFrame* frame = GetMainFrame(); 1583 WebFrame* frame = GetMainFrame();
436 ASSERT_NE(nullptr, frame); 1584 ASSERT_NE(nullptr, frame);
437 1585
438 WebInputElement element = GetInputElementById("checkbox"); 1586 WebInputElement element = GetInputElementById("checkbox");
439 element.setAutofilled(true); 1587 element.setAutofilled(true);
440 FormFieldData result; 1588 FormFieldData result;
441 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1589 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
442 1590
443 FormFieldData expected; 1591 FormFieldData expected;
444 expected.name = ASCIIToUTF16("checkbox"); 1592 expected.name = ASCIIToUTF16("checkbox");
445 expected.value = ASCIIToUTF16("mail"); 1593 expected.value = ASCIIToUTF16("mail");
446 expected.form_control_type = "checkbox"; 1594 expected.form_control_type = "checkbox";
447 expected.is_autofilled = true; 1595 expected.is_autofilled = true;
448 expected.is_checkable = true; 1596 expected.is_checkable = true;
449 expected.is_checked = true; 1597 expected.is_checked = true;
450 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1598 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
451 1599
452 element = GetInputElementById("radio"); 1600 element = GetInputElementById("radio");
453 element.setAutofilled(true); 1601 element.setAutofilled(true);
454 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1602 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
455 expected.name = ASCIIToUTF16("radio"); 1603 expected.name = ASCIIToUTF16("radio");
456 expected.value = ASCIIToUTF16("male"); 1604 expected.value = ASCIIToUTF16("male");
457 expected.form_control_type = "radio"; 1605 expected.form_control_type = "radio";
458 expected.is_autofilled = true; 1606 expected.is_autofilled = true;
459 expected.is_checkable = true; 1607 expected.is_checkable = true;
460 expected.is_checked = false; 1608 expected.is_checked = false;
461 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1609 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
462 } 1610 }
463 1611
464 // We should be able to extract a <select> field. 1612 // We should be able to extract a <select> field.
465 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) { 1613 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldSelect) {
466 LoadHTML("<SELECT id='element'/>" 1614 LoadHTML("<SELECT id='element'/>"
467 " <OPTION value='CA'>California</OPTION>" 1615 " <OPTION value='CA'>California</OPTION>"
468 " <OPTION value='TX'>Texas</OPTION>" 1616 " <OPTION value='TX'>Texas</OPTION>"
469 "</SELECT>"); 1617 "</SELECT>");
470 1618
471 WebFrame* frame = GetMainFrame(); 1619 WebFrame* frame = GetMainFrame();
472 ASSERT_NE(nullptr, frame); 1620 ASSERT_NE(nullptr, frame);
473 1621
474 WebFormControlElement element = GetFormControlElementById("element"); 1622 WebFormControlElement element = GetFormControlElementById("element");
475 FormFieldData result1; 1623 FormFieldData result1;
476 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result1); 1624 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result1);
477 1625
478 FormFieldData expected; 1626 FormFieldData expected;
479 expected.name = ASCIIToUTF16("element"); 1627 expected.name = ASCIIToUTF16("element");
480 expected.max_length = 0; 1628 expected.max_length = 0;
481 expected.form_control_type = "select-one"; 1629 expected.form_control_type = "select-one";
482 1630
483 expected.value = ASCIIToUTF16("CA"); 1631 expected.value = ASCIIToUTF16("CA");
484 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1); 1632 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result1);
485 1633
486 FormFieldData result2; 1634 FormFieldData result2;
487 WebFormControlElementToFormField( 1635 WebFormControlElementToFormField(
488 element, 1636 element,
489 static_cast<autofill::ExtractMask>(autofill::EXTRACT_VALUE | 1637 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTION_TEXT),
490 autofill::EXTRACT_OPTION_TEXT),
491 &result2); 1638 &result2);
492 expected.value = ASCIIToUTF16("California"); 1639 expected.value = ASCIIToUTF16("California");
493 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2); 1640 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result2);
494 1641
495 FormFieldData result3; 1642 FormFieldData result3;
496 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS, 1643 WebFormControlElementToFormField(element, EXTRACT_OPTIONS, &result3);
497 &result3); 1644 expected.value.clear();
498 expected.value = base::string16();
499 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result3); 1645 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result3);
500 1646
501 ASSERT_EQ(2U, result3.option_values.size()); 1647 ASSERT_EQ(2U, result3.option_values.size());
502 ASSERT_EQ(2U, result3.option_contents.size()); 1648 ASSERT_EQ(2U, result3.option_contents.size());
503 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]); 1649 EXPECT_EQ(ASCIIToUTF16("CA"), result3.option_values[0]);
504 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]); 1650 EXPECT_EQ(ASCIIToUTF16("California"), result3.option_contents[0]);
505 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]); 1651 EXPECT_EQ(ASCIIToUTF16("TX"), result3.option_values[1]);
506 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]); 1652 EXPECT_EQ(ASCIIToUTF16("Texas"), result3.option_contents[1]);
507 } 1653 }
508 1654
509 // When faced with <select> field with *many* options, we should trim them to a 1655 // When faced with <select> field with *many* options, we should trim them to a
510 // reasonable number. 1656 // reasonable number.
511 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldLongSelect) { 1657 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldLongSelect) {
512 std::string html = "<SELECT id='element'/>"; 1658 std::string html = "<SELECT id='element'/>";
513 for (size_t i = 0; i < 2 * kMaxListSize; ++i) { 1659 for (size_t i = 0; i < 2 * kMaxListSize; ++i) {
514 html += base::StringPrintf("<OPTION value='%" PRIuS "'>" 1660 html += base::StringPrintf("<OPTION value='%" PRIuS "'>"
515 "%" PRIuS "</OPTION>", i, i); 1661 "%" PRIuS "</OPTION>", i, i);
516 } 1662 }
517 html += "</SELECT>"; 1663 html += "</SELECT>";
518 LoadHTML(html.c_str()); 1664 LoadHTML(html.c_str());
519 1665
520 WebFrame* frame = GetMainFrame(); 1666 WebFrame* frame = GetMainFrame();
521 ASSERT_TRUE(frame); 1667 ASSERT_TRUE(frame);
522 1668
523 WebFormControlElement element = GetFormControlElementById("element"); 1669 WebFormControlElement element = GetFormControlElementById("element");
524 FormFieldData result; 1670 FormFieldData result;
525 WebFormControlElementToFormField(element, autofill::EXTRACT_OPTIONS, &result); 1671 WebFormControlElementToFormField(element, EXTRACT_OPTIONS, &result);
526 1672
527 EXPECT_TRUE(result.option_values.empty()); 1673 EXPECT_TRUE(result.option_values.empty());
528 EXPECT_TRUE(result.option_contents.empty()); 1674 EXPECT_TRUE(result.option_contents.empty());
529 } 1675 }
530 1676
531 // We should be able to extract a <textarea> field. 1677 // We should be able to extract a <textarea> field.
532 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) { 1678 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldTextArea) {
533 LoadHTML("<TEXTAREA id='element'>" 1679 LoadHTML("<TEXTAREA id='element'>"
534 "This element's value&#10;" 1680 "This element's value&#10;"
535 "spans multiple lines." 1681 "spans multiple lines."
536 "</TEXTAREA>"); 1682 "</TEXTAREA>");
537 1683
538 WebFrame* frame = GetMainFrame(); 1684 WebFrame* frame = GetMainFrame();
539 ASSERT_NE(nullptr, frame); 1685 ASSERT_NE(nullptr, frame);
540 1686
541 WebFormControlElement element = GetFormControlElementById("element"); 1687 WebFormControlElement element = GetFormControlElementById("element");
542 FormFieldData result_sans_value; 1688 FormFieldData result_sans_value;
543 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, 1689 WebFormControlElementToFormField(element, EXTRACT_NONE, &result_sans_value);
544 &result_sans_value);
545 1690
546 FormFieldData expected; 1691 FormFieldData expected;
547 expected.name = ASCIIToUTF16("element"); 1692 expected.name = ASCIIToUTF16("element");
548 expected.max_length = 0; 1693 expected.max_length = 0;
549 expected.form_control_type = "textarea"; 1694 expected.form_control_type = "textarea";
550 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value); 1695 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
551 1696
552 FormFieldData result_with_value; 1697 FormFieldData result_with_value;
553 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, 1698 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result_with_value);
554 &result_with_value);
555 expected.value = ASCIIToUTF16("This element's value\n" 1699 expected.value = ASCIIToUTF16("This element's value\n"
556 "spans multiple lines."); 1700 "spans multiple lines.");
557 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value); 1701 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
558 } 1702 }
559 1703
560 // We should be able to extract an <input type="month"> field. 1704 // We should be able to extract an <input type="month"> field.
561 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMonthInput) { 1705 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldMonthInput) {
562 LoadHTML("<INPUT type='month' id='element' value='2011-12'>"); 1706 LoadHTML("<INPUT type='month' id='element' value='2011-12'>");
563 1707
564 WebFrame* frame = GetMainFrame(); 1708 WebFrame* frame = GetMainFrame();
565 ASSERT_NE(nullptr, frame); 1709 ASSERT_NE(nullptr, frame);
566 1710
567 WebFormControlElement element = GetFormControlElementById("element"); 1711 WebFormControlElement element = GetFormControlElementById("element");
568 FormFieldData result_sans_value; 1712 FormFieldData result_sans_value;
569 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, 1713 WebFormControlElementToFormField(element, EXTRACT_NONE, &result_sans_value);
570 &result_sans_value);
571 1714
572 FormFieldData expected; 1715 FormFieldData expected;
573 expected.name = ASCIIToUTF16("element"); 1716 expected.name = ASCIIToUTF16("element");
574 expected.max_length = 0; 1717 expected.max_length = 0;
575 expected.form_control_type = "month"; 1718 expected.form_control_type = "month";
576 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value); 1719 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_sans_value);
577 1720
578 FormFieldData result_with_value; 1721 FormFieldData result_with_value;
579 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, 1722 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result_with_value);
580 &result_with_value);
581 expected.value = ASCIIToUTF16("2011-12"); 1723 expected.value = ASCIIToUTF16("2011-12");
582 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value); 1724 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result_with_value);
583 } 1725 }
584 1726
585 // We should not extract the value for non-text and non-select fields. 1727 // We should not extract the value for non-text and non-select fields.
586 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) { 1728 TEST_F(FormAutofillTest, WebFormControlElementToFormFieldInvalidType) {
587 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>" 1729 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
588 " <INPUT type='hidden' id='hidden' value='apple'/>" 1730 " <INPUT type='hidden' id='hidden' value='apple'/>"
589 " <INPUT type='submit' id='submit' value='Send'/>" 1731 " <INPUT type='submit' id='submit' value='Send'/>"
590 "</FORM>"); 1732 "</FORM>");
591 1733
592 WebFrame* frame = GetMainFrame(); 1734 WebFrame* frame = GetMainFrame();
593 ASSERT_NE(nullptr, frame); 1735 ASSERT_NE(nullptr, frame);
594 1736
595 WebFormControlElement element = GetFormControlElementById("hidden"); 1737 WebFormControlElement element = GetFormControlElementById("hidden");
596 FormFieldData result; 1738 FormFieldData result;
597 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1739 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
598 1740
599 FormFieldData expected; 1741 FormFieldData expected;
600 expected.max_length = 0; 1742 expected.max_length = 0;
601 1743
602 expected.name = ASCIIToUTF16("hidden"); 1744 expected.name = ASCIIToUTF16("hidden");
603 expected.form_control_type = "hidden"; 1745 expected.form_control_type = "hidden";
604 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1746 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
605 1747
606 element = GetFormControlElementById("submit"); 1748 element = GetFormControlElementById("submit");
607 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1749 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
608 expected.name = ASCIIToUTF16("submit"); 1750 expected.name = ASCIIToUTF16("submit");
609 expected.form_control_type = "submit"; 1751 expected.form_control_type = "submit";
610 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1752 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
611 } 1753 }
612 1754
613 // We should be able to extract password fields. 1755 // We should be able to extract password fields.
614 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) { 1756 TEST_F(FormAutofillTest, WebFormControlElementToPasswordFormField) {
615 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>" 1757 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
616 " <INPUT type='password' id='password' value='secret'/>" 1758 " <INPUT type='password' id='password' value='secret'/>"
617 "</FORM>"); 1759 "</FORM>");
618 1760
619 WebFrame* frame = GetMainFrame(); 1761 WebFrame* frame = GetMainFrame();
620 ASSERT_NE(nullptr, frame); 1762 ASSERT_NE(nullptr, frame);
621 1763
622 WebFormControlElement element = GetFormControlElementById("password"); 1764 WebFormControlElement element = GetFormControlElementById("password");
623 FormFieldData result; 1765 FormFieldData result;
624 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1766 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
625 1767
626 FormFieldData expected; 1768 FormFieldData expected;
627 expected.max_length = WebInputElement::defaultMaxLength(); 1769 expected.max_length = WebInputElement::defaultMaxLength();
628 expected.name = ASCIIToUTF16("password"); 1770 expected.name = ASCIIToUTF16("password");
629 expected.form_control_type = "password"; 1771 expected.form_control_type = "password";
630 expected.value = ASCIIToUTF16("secret"); 1772 expected.value = ASCIIToUTF16("secret");
631 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1773 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
632 } 1774 }
633 1775
634 // We should be able to extract the autocompletetype attribute. 1776 // We should be able to extract the autocompletetype attribute.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 // Very long attribute values should be replaced by a default string, to 1831 // Very long attribute values should be replaced by a default string, to
690 // prevent malicious websites from DOSing the browser process. 1832 // prevent malicious websites from DOSing the browser process.
691 { "malicious", "text", "x-max-data-length-exceeded" }, 1833 { "malicious", "text", "x-max-data-length-exceeded" },
692 }; 1834 };
693 1835
694 WebDocument document = frame->document(); 1836 WebDocument document = frame->document();
695 for (size_t i = 0; i < arraysize(test_cases); ++i) { 1837 for (size_t i = 0; i < arraysize(test_cases); ++i) {
696 WebFormControlElement element = 1838 WebFormControlElement element =
697 GetFormControlElementById(ASCIIToUTF16(test_cases[i].element_id)); 1839 GetFormControlElementById(ASCIIToUTF16(test_cases[i].element_id));
698 FormFieldData result; 1840 FormFieldData result;
699 WebFormControlElementToFormField(element, autofill::EXTRACT_NONE, &result); 1841 WebFormControlElementToFormField(element, EXTRACT_NONE, &result);
700 1842
701 FormFieldData expected; 1843 FormFieldData expected;
702 expected.name = ASCIIToUTF16(test_cases[i].element_id); 1844 expected.name = ASCIIToUTF16(test_cases[i].element_id);
703 expected.form_control_type = test_cases[i].form_control_type; 1845 expected.form_control_type = test_cases[i].form_control_type;
704 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute; 1846 expected.autocomplete_attribute = test_cases[i].autocomplete_attribute;
705 if (test_cases[i].form_control_type == "text") 1847 if (test_cases[i].form_control_type == "text")
706 expected.max_length = WebInputElement::defaultMaxLength(); 1848 expected.max_length = WebInputElement::defaultMaxLength();
707 else 1849 else
708 expected.max_length = 0; 1850 expected.max_length = 0;
709 1851
710 SCOPED_TRACE(test_cases[i].element_id); 1852 SCOPED_TRACE(test_cases[i].element_id);
711 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result); 1853 EXPECT_FORM_FIELD_DATA_EQUALS(expected, result);
712 } 1854 }
713 } 1855 }
714 1856
715 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectStyle) { 1857 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectStyle) {
716 LoadHTML("<STYLE>input{direction:rtl}</STYLE>" 1858 LoadHTML("<STYLE>input{direction:rtl}</STYLE>"
717 "<FORM>" 1859 "<FORM>"
718 " <INPUT type='text' id='element'>" 1860 " <INPUT type='text' id='element'>"
719 "</FORM>"); 1861 "</FORM>");
720 1862
721 WebFrame* frame = GetMainFrame(); 1863 WebFrame* frame = GetMainFrame();
722 ASSERT_NE(nullptr, frame); 1864 ASSERT_NE(nullptr, frame);
723 1865
724 WebFormControlElement element = GetFormControlElementById("element"); 1866 WebFormControlElement element = GetFormControlElementById("element");
725 1867
726 FormFieldData result; 1868 FormFieldData result;
727 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1869 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
728 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction); 1870 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
729 } 1871 }
730 1872
731 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectDIRAttribute) { 1873 TEST_F(FormAutofillTest, DetectTextDirectionFromDirectDIRAttribute) {
732 LoadHTML("<FORM>" 1874 LoadHTML("<FORM>"
733 " <INPUT dir='rtl' type='text' id='element'/>" 1875 " <INPUT dir='rtl' type='text' id='element'/>"
734 "</FORM>"); 1876 "</FORM>");
735 1877
736 WebFrame* frame = GetMainFrame(); 1878 WebFrame* frame = GetMainFrame();
737 ASSERT_NE(nullptr, frame); 1879 ASSERT_NE(nullptr, frame);
738 1880
739 WebFormControlElement element = GetFormControlElementById("element"); 1881 WebFormControlElement element = GetFormControlElementById("element");
740 1882
741 FormFieldData result; 1883 FormFieldData result;
742 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1884 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
743 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction); 1885 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
744 } 1886 }
745 1887
746 TEST_F(FormAutofillTest, DetectTextDirectionFromParentStyle) { 1888 TEST_F(FormAutofillTest, DetectTextDirectionFromParentStyle) {
747 LoadHTML("<STYLE>form{direction:rtl}</STYLE>" 1889 LoadHTML("<STYLE>form{direction:rtl}</STYLE>"
748 "<FORM>" 1890 "<FORM>"
749 " <INPUT type='text' id='element'/>" 1891 " <INPUT type='text' id='element'/>"
750 "</FORM>"); 1892 "</FORM>");
751 1893
752 WebFrame* frame = GetMainFrame(); 1894 WebFrame* frame = GetMainFrame();
753 ASSERT_NE(nullptr, frame); 1895 ASSERT_NE(nullptr, frame);
754 1896
755 WebFormControlElement element = GetFormControlElementById("element"); 1897 WebFormControlElement element = GetFormControlElementById("element");
756 1898
757 FormFieldData result; 1899 FormFieldData result;
758 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1900 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
759 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction); 1901 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
760 } 1902 }
761 1903
762 TEST_F(FormAutofillTest, DetectTextDirectionFromParentDIRAttribute) { 1904 TEST_F(FormAutofillTest, DetectTextDirectionFromParentDIRAttribute) {
763 LoadHTML("<FORM dir='rtl'>" 1905 LoadHTML("<FORM dir='rtl'>"
764 " <INPUT type='text' id='element'/>" 1906 " <INPUT type='text' id='element'/>"
765 "</FORM>"); 1907 "</FORM>");
766 1908
767 WebFrame* frame = GetMainFrame(); 1909 WebFrame* frame = GetMainFrame();
768 ASSERT_NE(nullptr, frame); 1910 ASSERT_NE(nullptr, frame);
769 1911
770 WebFormControlElement element = GetFormControlElementById("element"); 1912 WebFormControlElement element = GetFormControlElementById("element");
771 1913
772 FormFieldData result; 1914 FormFieldData result;
773 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1915 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
774 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction); 1916 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
775 } 1917 }
776 1918
777 TEST_F(FormAutofillTest, DetectTextDirectionWhenStyleAndDIRAttributMixed) { 1919 TEST_F(FormAutofillTest, DetectTextDirectionWhenStyleAndDIRAttributMixed) {
778 LoadHTML("<STYLE>input{direction:ltr}</STYLE>" 1920 LoadHTML("<STYLE>input{direction:ltr}</STYLE>"
779 "<FORM dir='rtl'>" 1921 "<FORM dir='rtl'>"
780 " <INPUT type='text' id='element'/>" 1922 " <INPUT type='text' id='element'/>"
781 "</FORM>"); 1923 "</FORM>");
782 1924
783 WebFrame* frame = GetMainFrame(); 1925 WebFrame* frame = GetMainFrame();
784 ASSERT_NE(nullptr, frame); 1926 ASSERT_NE(nullptr, frame);
785 1927
786 WebFormControlElement element = GetFormControlElementById("element"); 1928 WebFormControlElement element = GetFormControlElementById("element");
787 1929
788 FormFieldData result; 1930 FormFieldData result;
789 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1931 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
790 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction); 1932 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
791 } 1933 }
792 1934
793 TEST_F(FormAutofillTest, 1935 TEST_F(FormAutofillTest,
794 DetectTextDirectionWhenParentHasBothDIRAttributeAndStyle) { 1936 DetectTextDirectionWhenParentHasBothDIRAttributeAndStyle) {
795 LoadHTML("<STYLE>form{direction:ltr}</STYLE>" 1937 LoadHTML("<STYLE>form{direction:ltr}</STYLE>"
796 "<FORM dir='rtl'>" 1938 "<FORM dir='rtl'>"
797 " <INPUT type='text' id='element'/>" 1939 " <INPUT type='text' id='element'/>"
798 "</FORM>"); 1940 "</FORM>");
799 1941
800 WebFrame* frame = GetMainFrame(); 1942 WebFrame* frame = GetMainFrame();
801 ASSERT_NE(nullptr, frame); 1943 ASSERT_NE(nullptr, frame);
802 1944
803 WebFormControlElement element = GetFormControlElementById("element"); 1945 WebFormControlElement element = GetFormControlElementById("element");
804 1946
805 FormFieldData result; 1947 FormFieldData result;
806 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1948 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
807 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction); 1949 EXPECT_EQ(base::i18n::LEFT_TO_RIGHT, result.text_direction);
808 } 1950 }
809 1951
810 TEST_F(FormAutofillTest, DetectTextDirectionWhenAncestorHasInlineStyle) { 1952 TEST_F(FormAutofillTest, DetectTextDirectionWhenAncestorHasInlineStyle) {
811 LoadHTML("<FORM style='direction:ltr'>" 1953 LoadHTML("<FORM style='direction:ltr'>"
812 " <SPAN dir='rtl'>" 1954 " <SPAN dir='rtl'>"
813 " <INPUT type='text' id='element'/>" 1955 " <INPUT type='text' id='element'/>"
814 " </SPAN>" 1956 " </SPAN>"
815 "</FORM>"); 1957 "</FORM>");
816 1958
817 WebFrame* frame = GetMainFrame(); 1959 WebFrame* frame = GetMainFrame();
818 ASSERT_NE(nullptr, frame); 1960 ASSERT_NE(nullptr, frame);
819 1961
820 WebFormControlElement element = GetFormControlElementById("element"); 1962 WebFormControlElement element = GetFormControlElementById("element");
821 1963
822 FormFieldData result; 1964 FormFieldData result;
823 WebFormControlElementToFormField(element, autofill::EXTRACT_VALUE, &result); 1965 WebFormControlElementToFormField(element, EXTRACT_VALUE, &result);
824 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction); 1966 EXPECT_EQ(base::i18n::RIGHT_TO_LEFT, result.text_direction);
825 } 1967 }
826 1968
827 TEST_F(FormAutofillTest, WebFormElementToFormData) { 1969 TEST_F(FormAutofillTest, WebFormElementToFormData) {
828 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>" 1970 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
829 " <LABEL for='firstname'>First name:</LABEL>" 1971 " <LABEL for='firstname'>First name:</LABEL>"
830 " <INPUT type='text' id='firstname' value='John'/>" 1972 " <INPUT type='text' id='firstname' value='John'/>"
831 " <LABEL for='lastname'>Last name:</LABEL>" 1973 " <LABEL for='lastname'>Last name:</LABEL>"
832 " <INPUT type='text' id='lastname' value='Smith'/>" 1974 " <INPUT type='text' id='lastname' value='Smith'/>"
833 " <LABEL for='street-address'>Address:</LABEL>" 1975 " <LABEL for='street-address'>Address:</LABEL>"
(...skipping 22 matching lines...) Expand all
856 WebVector<WebFormElement> forms; 1998 WebVector<WebFormElement> forms;
857 frame->document().forms(forms); 1999 frame->document().forms(forms);
858 ASSERT_EQ(1U, forms.size()); 2000 ASSERT_EQ(1U, forms.size());
859 2001
860 WebInputElement input_element = GetInputElementById("firstname"); 2002 WebInputElement input_element = GetInputElementById("firstname");
861 2003
862 FormData form; 2004 FormData form;
863 FormFieldData field; 2005 FormFieldData field;
864 EXPECT_TRUE(WebFormElementToFormData(forms[0], 2006 EXPECT_TRUE(WebFormElementToFormData(forms[0],
865 input_element, 2007 input_element,
866 autofill::REQUIRE_NONE, 2008 REQUIRE_NONE,
867 autofill::EXTRACT_VALUE, 2009 EXTRACT_VALUE,
868 &form, 2010 &form,
869 &field)); 2011 &field));
870 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); 2012 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
871 EXPECT_EQ(GURL(frame->document().url()), form.origin); 2013 EXPECT_EQ(GURL(frame->document().url()), form.origin);
872 EXPECT_EQ(GURL("http://cnn.com"), form.action); 2014 EXPECT_EQ(GURL("http://cnn.com"), form.action);
873 2015
874 const std::vector<FormFieldData>& fields = form.fields; 2016 const std::vector<FormFieldData>& fields = form.fields;
875 ASSERT_EQ(6U, fields.size()); 2017 ASSERT_EQ(6U, fields.size());
876 2018
877 FormFieldData expected; 2019 FormFieldData expected;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
915 expected.label = ASCIIToUTF16("Card expiration:"); 2057 expected.label = ASCIIToUTF16("Card expiration:");
916 expected.form_control_type = "month"; 2058 expected.form_control_type = "month";
917 expected.max_length = 0; 2059 expected.max_length = 0;
918 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]); 2060 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
919 } 2061 }
920 2062
921 // We should not be able to serialize a form with too many fillable fields. 2063 // We should not be able to serialize a form with too many fillable fields.
922 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) { 2064 TEST_F(FormAutofillTest, WebFormElementToFormDataTooManyFields) {
923 std::string html = 2065 std::string html =
924 "<FORM name='TestForm' action='http://cnn.com' method='post'>"; 2066 "<FORM name='TestForm' action='http://cnn.com' method='post'>";
925 for (size_t i = 0; i < (autofill::kMaxParseableFields + 1); ++i) { 2067 for (size_t i = 0; i < (kMaxParseableFields + 1); ++i) {
926 html += "<INPUT type='text'/>"; 2068 html += "<INPUT type='text'/>";
927 } 2069 }
928 html += "</FORM>"; 2070 html += "</FORM>";
929 LoadHTML(html.c_str()); 2071 LoadHTML(html.c_str());
930 2072
931 WebFrame* frame = GetMainFrame(); 2073 WebFrame* frame = GetMainFrame();
932 ASSERT_NE(nullptr, frame); 2074 ASSERT_NE(nullptr, frame);
933 2075
934 WebVector<WebFormElement> forms; 2076 WebVector<WebFormElement> forms;
935 frame->document().forms(forms); 2077 frame->document().forms(forms);
936 ASSERT_EQ(1U, forms.size()); 2078 ASSERT_EQ(1U, forms.size());
937 2079
938 WebInputElement input_element = GetInputElementById("firstname"); 2080 WebInputElement input_element = GetInputElementById("firstname");
939 2081
940 FormData form; 2082 FormData form;
941 FormFieldData field; 2083 FormFieldData field;
942 EXPECT_FALSE(WebFormElementToFormData(forms[0], 2084 EXPECT_FALSE(WebFormElementToFormData(forms[0],
943 input_element, 2085 input_element,
944 autofill::REQUIRE_NONE, 2086 REQUIRE_NONE,
945 autofill::EXTRACT_VALUE, 2087 EXTRACT_VALUE,
946 &form, 2088 &form,
947 &field)); 2089 &field));
948 } 2090 }
949 2091
950 TEST_F(FormAutofillTest, ExtractForms) { 2092 TEST_F(FormAutofillTest, ExtractForms) {
951 ExpectJohnSmithLabels( 2093 ExpectJohnSmithLabels(
952 "<FORM name='TestForm' action='http://cnn.com' method='post'>" 2094 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
953 " First name: <INPUT type='text' id='firstname' value='John'/>" 2095 " First name: <INPUT type='text' id='firstname' value='John'/>"
954 " Last name: <INPUT type='text' id='lastname' value='Smith'/>" 2096 " Last name: <INPUT type='text' id='lastname' value='Smith'/>"
955 " Email: <INPUT type='text' id='email' value='john@example.com'/>" 2097 " Email: <INPUT type='text' id='email' value='john@example.com'/>"
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 WebFrame* web_frame = GetMainFrame(); 2345 WebFrame* web_frame = GetMainFrame();
1204 ASSERT_NE(nullptr, web_frame); 2346 ASSERT_NE(nullptr, web_frame);
1205 2347
1206 WebVector<WebFormElement> web_forms; 2348 WebVector<WebFormElement> web_forms;
1207 web_frame->document().forms(web_forms); 2349 web_frame->document().forms(web_forms);
1208 ASSERT_EQ(1U, web_forms.size()); 2350 ASSERT_EQ(1U, web_forms.size());
1209 WebFormElement web_form = web_forms[0]; 2351 WebFormElement web_form = web_forms[0];
1210 2352
1211 FormData form; 2353 FormData form;
1212 EXPECT_TRUE(WebFormElementToFormData( 2354 EXPECT_TRUE(WebFormElementToFormData(
1213 web_form, WebFormControlElement(), autofill::REQUIRE_NONE, 2355 web_form, WebFormControlElement(), REQUIRE_NONE, EXTRACT_NONE, &form,
1214 autofill::EXTRACT_NONE, &form, NULL)); 2356 nullptr));
1215 EXPECT_FALSE(WebFormElementToFormData( 2357 EXPECT_FALSE(WebFormElementToFormData(
1216 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE, 2358 web_form, WebFormControlElement(), REQUIRE_AUTOCOMPLETE, EXTRACT_NONE,
1217 autofill::EXTRACT_NONE, &form, NULL)); 2359 &form, nullptr));
1218 } 2360 }
1219 2361
1220 { 2362 {
1221 // The firstname element is not auto-completable due to autocomplete=off. 2363 // The firstname element is not auto-completable due to autocomplete=off.
1222 LoadHTML("<FORM name='TestForm' action='http://abc.com' " 2364 LoadHTML("<FORM name='TestForm' action='http://abc.com' "
1223 " method='post'>" 2365 " method='post'>"
1224 " <INPUT type='text' id='firstname' value='John'" 2366 " <INPUT type='text' id='firstname' value='John'"
1225 " autocomplete=off>" 2367 " autocomplete=off>"
1226 " <INPUT type='text' id='middlename' value='Jack'/>" 2368 " <INPUT type='text' id='middlename' value='Jack'/>"
1227 " <INPUT type='text' id='lastname' value='Smith'/>" 2369 " <INPUT type='text' id='lastname' value='Smith'/>"
1228 " <INPUT type='text' id='email' value='john@example.com'/>" 2370 " <INPUT type='text' id='email' value='john@example.com'/>"
1229 " <INPUT type='submit' name='reply' value='Send'/>" 2371 " <INPUT type='submit' name='reply' value='Send'/>"
1230 "</FORM>"); 2372 "</FORM>");
1231 2373
1232 WebFrame* web_frame = GetMainFrame(); 2374 WebFrame* web_frame = GetMainFrame();
1233 ASSERT_NE(nullptr, web_frame); 2375 ASSERT_NE(nullptr, web_frame);
1234 2376
1235 WebVector<WebFormElement> web_forms; 2377 WebVector<WebFormElement> web_forms;
1236 web_frame->document().forms(web_forms); 2378 web_frame->document().forms(web_forms);
1237 ASSERT_EQ(1U, web_forms.size()); 2379 ASSERT_EQ(1U, web_forms.size());
1238 WebFormElement web_form = web_forms[0]; 2380 WebFormElement web_form = web_forms[0];
1239 2381
1240 FormData form; 2382 FormData form;
1241 EXPECT_TRUE(WebFormElementToFormData( 2383 EXPECT_TRUE(WebFormElementToFormData(
1242 web_form, WebFormControlElement(), autofill::REQUIRE_AUTOCOMPLETE, 2384 web_form, WebFormControlElement(), REQUIRE_AUTOCOMPLETE, EXTRACT_VALUE,
1243 autofill::EXTRACT_VALUE, &form, NULL)); 2385 &form, nullptr));
1244 2386
1245 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); 2387 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1246 EXPECT_EQ(GURL(web_frame->document().url()), form.origin); 2388 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1247 EXPECT_EQ(GURL("http://abc.com"), form.action); 2389 EXPECT_EQ(GURL("http://abc.com"), form.action);
1248 2390
1249 const std::vector<FormFieldData>& fields = form.fields; 2391 const std::vector<FormFieldData>& fields = form.fields;
1250 ASSERT_EQ(3U, fields.size()); 2392 ASSERT_EQ(3U, fields.size());
1251 2393
1252 FormFieldData expected; 2394 FormFieldData expected;
1253 expected.form_control_type = "text"; 2395 expected.form_control_type = "text";
1254 expected.max_length = WebInputElement::defaultMaxLength(); 2396 expected.max_length = WebInputElement::defaultMaxLength();
1255 2397
1256 expected.name = ASCIIToUTF16("middlename"); 2398 expected.name = ASCIIToUTF16("middlename");
1257 expected.value = ASCIIToUTF16("Jack"); 2399 expected.value = ASCIIToUTF16("Jack");
1258 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]); 2400 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1259 2401
1260 expected.name = ASCIIToUTF16("lastname"); 2402 expected.name = ASCIIToUTF16("lastname");
1261 expected.value = ASCIIToUTF16("Smith"); 2403 expected.value = ASCIIToUTF16("Smith");
1262 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]); 2404 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1263 2405
1264 expected.name = ASCIIToUTF16("email"); 2406 expected.name = ASCIIToUTF16("email");
1265 expected.value = ASCIIToUTF16("john@example.com"); 2407 expected.value = ASCIIToUTF16("john@example.com");
1266 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); 2408 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1267 } 2409 }
1268 } 2410 }
1269 2411
1270 TEST_F(FormAutofillTest, FindFormForInputElement) { 2412 TEST_F(FormAutofillTest, FindFormForInputElement) {
1271 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 2413 TestFindFormForInputElement(
1272 " <INPUT type='text' id='firstname' value='John'/>" 2414 "<FORM name='TestForm' action='http://buh.com' method='post'>"
1273 " <INPUT type='text' id='lastname' value='Smith'/>" 2415 " <INPUT type='text' id='firstname' value='John'/>"
1274 " <INPUT type='text' id='email' value='john@example.com'" 2416 " <INPUT type='text' id='lastname' value='Smith'/>"
1275 "autocomplete='off' />" 2417 " <INPUT type='text' id='email' value='john@example.com'"
1276 " <INPUT type='text' id='phone' value='1.800.555.1234'/>" 2418 "autocomplete='off' />"
1277 " <INPUT type='submit' name='reply-send' value='Send'/>" 2419 " <INPUT type='text' id='phone' value='1.800.555.1234'/>"
1278 "</FORM>"); 2420 " <INPUT type='submit' name='reply-send' value='Send'/>"
2421 "</FORM>",
2422 false);
2423 }
1279 2424
1280 WebFrame* web_frame = GetMainFrame(); 2425 TEST_F(FormAutofillTest, FindFormForInputElementForUnownedForm) {
1281 ASSERT_NE(nullptr, web_frame); 2426 TestFindFormForInputElement(
1282 2427 "<INPUT type='text' id='firstname' value='John'/>"
1283 FormCache form_cache; 2428 "<INPUT type='text' id='lastname' value='Smith'/>"
1284 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 2429 "<INPUT type='text' id='email' value='john@example.com'"
1285 ASSERT_EQ(1U, forms.size()); 2430 "autocomplete='off' />"
1286 2431 "<INPUT type='text' id='phone' value='1.800.555.1234'/>"
1287 // Get the input element we want to find. 2432 "<INPUT type='submit' name='reply-send' value='Send'/>",
1288 WebInputElement input_element = GetInputElementById("firstname"); 2433 true);
1289
1290 // Find the form and verify it's the correct form.
1291 FormData form;
1292 FormFieldData field;
1293 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
1294 &form,
1295 &field,
1296 autofill::REQUIRE_NONE));
1297 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1298 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1299 EXPECT_EQ(GURL("http://buh.com"), form.action);
1300
1301 const std::vector<FormFieldData>& fields = form.fields;
1302 ASSERT_EQ(4U, fields.size());
1303
1304 FormFieldData expected;
1305 expected.form_control_type = "text";
1306 expected.max_length = WebInputElement::defaultMaxLength();
1307
1308 expected.name = ASCIIToUTF16("firstname");
1309 expected.value = ASCIIToUTF16("John");
1310 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1311 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1312
1313 expected.name = ASCIIToUTF16("lastname");
1314 expected.value = ASCIIToUTF16("Smith");
1315 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1316
1317 expected.name = ASCIIToUTF16("email");
1318 expected.value = ASCIIToUTF16("john@example.com");
1319 expected.autocomplete_attribute = "off";
1320 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1321 expected.autocomplete_attribute = std::string(); // reset
1322
1323 expected.name = ASCIIToUTF16("phone");
1324 expected.value = ASCIIToUTF16("1.800.555.1234");
1325 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1326
1327 // Try again, but require autocomplete.
1328 FormData form2;
1329 FormFieldData field2;
1330 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1331 input_element,
1332 &form2,
1333 &field2,
1334 autofill::REQUIRE_AUTOCOMPLETE));
1335 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1336 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1337 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1338
1339 const std::vector<FormFieldData>& fields2 = form2.fields;
1340 ASSERT_EQ(3U, fields2.size());
1341
1342 expected.form_control_type = "text";
1343 expected.max_length = WebInputElement::defaultMaxLength();
1344
1345 expected.name = ASCIIToUTF16("firstname");
1346 expected.value = ASCIIToUTF16("John");
1347 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1348 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1349
1350 expected.name = ASCIIToUTF16("lastname");
1351 expected.value = ASCIIToUTF16("Smith");
1352 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1353
1354 expected.name = ASCIIToUTF16("phone");
1355 expected.value = ASCIIToUTF16("1.800.555.1234");
1356 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1357 } 2434 }
1358 2435
1359 TEST_F(FormAutofillTest, FindFormForTextAreaElement) { 2436 TEST_F(FormAutofillTest, FindFormForTextAreaElement) {
1360 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 2437 TestFindFormForTextAreaElement(
1361 " <INPUT type='text' id='firstname' value='John'/>" 2438 "<FORM name='TestForm' action='http://buh.com' method='post'>"
1362 " <INPUT type='text' id='lastname' value='Smith'/>" 2439 " <INPUT type='text' id='firstname' value='John'/>"
1363 " <INPUT type='text' id='email' value='john@example.com'" 2440 " <INPUT type='text' id='lastname' value='Smith'/>"
1364 "autocomplete='off' />" 2441 " <INPUT type='text' id='email' value='john@example.com'"
1365 " <TEXTAREA id='street-address'>" 2442 "autocomplete='off' />"
1366 "123 Fantasy Ln.&#10;" 2443 " <TEXTAREA id='street-address'>"
1367 "Apt. 42" 2444 "123 Fantasy Ln.&#10;"
1368 "</TEXTAREA>" 2445 "Apt. 42"
1369 " <INPUT type='submit' name='reply-send' value='Send'/>" 2446 "</TEXTAREA>"
1370 "</FORM>"); 2447 " <INPUT type='submit' name='reply-send' value='Send'/>"
2448 "</FORM>",
2449 false);
2450 }
1371 2451
1372 WebFrame* web_frame = GetMainFrame(); 2452 TEST_F(FormAutofillTest, FindFormForTextAreaElementForUnownedForm) {
1373 ASSERT_NE(nullptr, web_frame); 2453 TestFindFormForTextAreaElement(
1374 2454 "<INPUT type='text' id='firstname' value='John'/>"
1375 FormCache form_cache; 2455 "<INPUT type='text' id='lastname' value='Smith'/>"
1376 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 2456 "<INPUT type='text' id='email' value='john@example.com'"
1377 ASSERT_EQ(1U, forms.size()); 2457 "autocomplete='off' />"
1378 2458 "<TEXTAREA id='street-address'>"
1379 // Get the textarea element we want to find. 2459 "123 Fantasy Ln.&#10;"
1380 WebElement element = web_frame->document().getElementById("street-address"); 2460 "Apt. 42"
1381 WebTextAreaElement textarea_element = element.to<WebTextAreaElement>(); 2461 "</TEXTAREA>"
1382 2462 "<INPUT type='submit' name='reply-send' value='Send'/>",
1383 // Find the form and verify it's the correct form. 2463 true);
1384 FormData form;
1385 FormFieldData field;
1386 EXPECT_TRUE(FindFormAndFieldForFormControlElement(textarea_element,
1387 &form,
1388 &field,
1389 autofill::REQUIRE_NONE));
1390 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
1391 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
1392 EXPECT_EQ(GURL("http://buh.com"), form.action);
1393
1394 const std::vector<FormFieldData>& fields = form.fields;
1395 ASSERT_EQ(4U, fields.size());
1396
1397 FormFieldData expected;
1398
1399 expected.name = ASCIIToUTF16("firstname");
1400 expected.value = ASCIIToUTF16("John");
1401 expected.form_control_type = "text";
1402 expected.max_length = WebInputElement::defaultMaxLength();
1403 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
1404
1405 expected.name = ASCIIToUTF16("lastname");
1406 expected.value = ASCIIToUTF16("Smith");
1407 expected.form_control_type = "text";
1408 expected.max_length = WebInputElement::defaultMaxLength();
1409 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
1410
1411 expected.name = ASCIIToUTF16("email");
1412 expected.value = ASCIIToUTF16("john@example.com");
1413 expected.autocomplete_attribute = "off";
1414 expected.form_control_type = "text";
1415 expected.max_length = WebInputElement::defaultMaxLength();
1416 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
1417 expected.autocomplete_attribute = std::string(); // reset
1418
1419 expected.name = ASCIIToUTF16("street-address");
1420 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1421 expected.form_control_type = "textarea";
1422 expected.max_length = 0;
1423 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
1424 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1425
1426 // Try again, but require autocomplete.
1427 FormData form2;
1428 FormFieldData field2;
1429 EXPECT_TRUE(FindFormAndFieldForFormControlElement(
1430 textarea_element,
1431 &form2,
1432 &field2,
1433 autofill::REQUIRE_AUTOCOMPLETE));
1434 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
1435 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
1436 EXPECT_EQ(GURL("http://buh.com"), form2.action);
1437
1438 const std::vector<FormFieldData>& fields2 = form2.fields;
1439 ASSERT_EQ(3U, fields2.size());
1440
1441 expected.name = ASCIIToUTF16("firstname");
1442 expected.value = ASCIIToUTF16("John");
1443 expected.form_control_type = "text";
1444 expected.max_length = WebInputElement::defaultMaxLength();
1445 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
1446
1447 expected.name = ASCIIToUTF16("lastname");
1448 expected.value = ASCIIToUTF16("Smith");
1449 expected.form_control_type = "text";
1450 expected.max_length = WebInputElement::defaultMaxLength();
1451 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
1452
1453 expected.name = ASCIIToUTF16("street-address");
1454 expected.value = ASCIIToUTF16("123 Fantasy Ln.\nApt. 42");
1455 expected.form_control_type = "textarea";
1456 expected.max_length = 0;
1457 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
1458 EXPECT_FORM_FIELD_DATA_EQUALS(expected, field);
1459 } 2464 }
1460 2465
1461 // Test regular FillForm function. 2466 // Test regular FillForm function.
1462 TEST_F(FormAutofillTest, FillForm) { 2467 TEST_F(FormAutofillTest, FillForm) {
1463 static const AutofillFieldCase field_cases[] = { 2468 TestFillForm(kFormHtml, false);
1464 // fields: form_control_type, name, initial_value, autocomplete_attribute, 2469 }
1465 // should_be_autofilled, autofill_value, expected_value
1466 2470
1467 // Regular empty fields (firstname & lastname) should be autofilled. 2471 TEST_F(FormAutofillTest, FillFormForUnownedForm) {
1468 {"text", 2472 TestFillForm(kUnownedFormHtml, true);
1469 "firstname",
1470 "",
1471 "",
1472 true,
1473 "filled firstname",
1474 "filled firstname"},
1475 {"text", "lastname", "", "", true, "filled lastname", "filled lastname"},
1476 // hidden fields should not be extracted to form_data.
1477 // Non empty fields should not be autofilled.
1478 {"text", "notempty", "Hi", "", false, "filled notempty", "Hi"},
1479 {"text",
1480 "noautocomplete",
1481 "",
1482 "off",
1483 true,
1484 "filled noautocomplete",
1485 "filled noautocomplete"},
1486 // Disabled fields should not be autofilled.
1487 {"text", "notenabled", "", "", false, "filled notenabled", ""},
1488 // Readonly fields should not be autofilled.
1489 {"text", "readonly", "", "", false, "filled readonly", ""},
1490 // Fields with "visibility: hidden" should not be autofilled.
1491 {"text", "invisible", "", "", false, "filled invisible", ""},
1492 // Fields with "display:none" should not be autofilled.
1493 {"text", "displaynone", "", "", false, "filled displaynone", ""},
1494 // Regular <input type="month"> should be autofilled.
1495 {"month", "month", "", "", true, "2017-11", "2017-11"},
1496 // Non-empty <input type="month"> should not be autofilled.
1497 {"month", "month-nonempty", "2011-12", "", false, "2017-11", "2011-12"},
1498 // Regular select fields should be autofilled.
1499 {"select-one", "select", "", "", true, "TX", "TX"},
1500 // Select fields should be autofilled even if they already have a
1501 // non-empty value.
1502 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1503 // Select fields should not be autofilled if no new value is passed from
1504 // autofill profile. The existing value should not be overriden.
1505 {"select-one", "select-unchanged", "CA", "", false, "CA", "CA"},
1506 // Regular textarea elements should be autofilled.
1507 {"textarea",
1508 "textarea",
1509 "",
1510 "",
1511 true,
1512 "some multi-\nline value",
1513 "some multi-\nline value"},
1514 // Non-empty textarea elements should not be autofilled.
1515 {"textarea",
1516 "textarea-nonempty",
1517 "Go\naway!",
1518 "",
1519 false,
1520 "some multi-\nline value",
1521 "Go\naway!"},
1522 };
1523 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1524 FillForm, &GetValueWrapper);
1525 // Verify preview selection.
1526 WebInputElement firstname = GetInputElementById("firstname");
1527 EXPECT_EQ(16, firstname.selectionStart());
1528 EXPECT_EQ(16, firstname.selectionEnd());
1529 } 2473 }
1530 2474
1531 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) { 2475 TEST_F(FormAutofillTest, FillFormIncludingNonFocusableElements) {
1532 static const AutofillFieldCase field_cases[] = { 2476 static const AutofillFieldCase field_cases[] = {
1533 // fields: form_control_type, name, initial_value, autocomplete_attribute, 2477 // fields: form_control_type, name, initial_value, autocomplete_attribute,
1534 // should_be_autofilled, autofill_value, expected_value 2478 // should_be_autofilled, autofill_value, expected_value
1535 2479
1536 // Regular empty fields (firstname & lastname) should be autofilled. 2480 // Regular empty fields (firstname & lastname) should be autofilled.
1537 {"text", 2481 {"text",
1538 "firstname", 2482 "firstname",
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1600 "some multi-\nline value"}, 2544 "some multi-\nline value"},
1601 // Nonempty textarea elements should be overridden. 2545 // Nonempty textarea elements should be overridden.
1602 {"textarea", 2546 {"textarea",
1603 "textarea-nonempty", 2547 "textarea-nonempty",
1604 "Go\naway!", 2548 "Go\naway!",
1605 "", 2549 "",
1606 true, 2550 true,
1607 "some multi-\nline value", 2551 "some multi-\nline value",
1608 "some multi-\nline value"}, 2552 "some multi-\nline value"},
1609 }; 2553 };
1610 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases), 2554 TestFormFillFunctions(kFormHtml, false, field_cases, arraysize(field_cases),
1611 &FillFormIncludingNonFocusableElementsWrapper, 2555 &FillFormIncludingNonFocusableElementsWrapper,
1612 &GetValueWrapper); 2556 &GetValueWrapper);
1613 } 2557 }
1614 2558
1615 TEST_F(FormAutofillTest, PreviewForm) { 2559 TEST_F(FormAutofillTest, PreviewForm) {
1616 static const AutofillFieldCase field_cases[] = { 2560 TestPreviewForm(kFormHtml, false);
1617 // Normal empty fields should be previewed. 2561 }
1618 {"text",
1619 "firstname",
1620 "",
1621 "",
1622 true,
1623 "suggested firstname",
1624 "suggested firstname"},
1625 {"text",
1626 "lastname",
1627 "",
1628 "",
1629 true,
1630 "suggested lastname",
1631 "suggested lastname"},
1632 // Hidden fields should not be extracted to form_data.
1633 // Non empty fields should not be previewed.
1634 {"text", "notempty", "Hi", "", false, "suggested notempty", ""},
1635 {"text",
1636 "noautocomplete",
1637 "",
1638 "off",
1639 true,
1640 "filled noautocomplete",
1641 "filled noautocomplete"},
1642 // Disabled fields should not be previewed.
1643 {"text", "notenabled", "", "", false, "suggested notenabled", ""},
1644 // Readonly fields should not be previewed.
1645 {"text", "readonly", "", "", false, "suggested readonly", ""},
1646 // Fields with "visibility: hidden" should not be previewed.
1647 {"text", "invisible", "", "", false, "suggested invisible", ""},
1648 // Fields with "display:none" should not previewed.
1649 {"text", "displaynone", "", "", false, "suggested displaynone", ""},
1650 // Regular <input type="month"> should be previewed.
1651 {"month", "month", "", "", true, "2017-11", "2017-11"},
1652 // Non-empty <input type="month"> should not be previewed.
1653 {"month", "month-nonempty", "2011-12", "", false, "2017-11", ""},
1654 // Regular select fields should be previewed.
1655 {"select-one", "select", "", "", true, "TX", "TX"},
1656 // Select fields should be previewed even if they already have a
1657 // non-empty value.
1658 {"select-one", "select-nonempty", "CA", "", true, "TX", "TX"},
1659 // Select fields should not be previewed if no suggestion is passed from
1660 // autofill profile.
1661 {"select-one", "select-unchanged", "CA", "", false, "", ""},
1662 // Normal textarea elements should be previewed.
1663 {"textarea",
1664 "textarea",
1665 "",
1666 "",
1667 true,
1668 "suggested multi-\nline value",
1669 "suggested multi-\nline value"},
1670 // Nonempty textarea elements should not be previewed.
1671 {"textarea",
1672 "textarea-nonempty",
1673 "Go\naway!",
1674 "",
1675 false,
1676 "suggested multi-\nline value",
1677 ""},
1678 };
1679 TestFormFillFunctions(kFormHtml, field_cases, arraysize(field_cases),
1680 &PreviewForm, &GetSuggestedValueWrapper);
1681 2562
1682 // Verify preview selection. 2563 TEST_F(FormAutofillTest, PreviewFormForUnownedForm) {
1683 WebInputElement firstname = GetInputElementById("firstname"); 2564 TestPreviewForm(kUnownedFormHtml, true);
1684 EXPECT_EQ(0, firstname.selectionStart());
1685 EXPECT_EQ(19, firstname.selectionEnd());
1686 } 2565 }
1687 2566
1688 TEST_F(FormAutofillTest, Labels) { 2567 TEST_F(FormAutofillTest, Labels) {
1689 ExpectJohnSmithLabels( 2568 ExpectJohnSmithLabels(
1690 "<FORM name='TestForm' action='http://cnn.com' method='post'>" 2569 "<FORM name='TestForm' action='http://cnn.com' method='post'>"
1691 " <LABEL for='firstname'> First name: </LABEL>" 2570 " <LABEL for='firstname'> First name: </LABEL>"
1692 " <INPUT type='text' id='firstname' value='John'/>" 2571 " <INPUT type='text' id='firstname' value='John'/>"
1693 " <LABEL for='lastname'> Last name: </LABEL>" 2572 " <LABEL for='lastname'> Last name: </LABEL>"
1694 " <INPUT type='text' id='lastname' value='Smith'/>" 2573 " <INPUT type='text' id='lastname' value='Smith'/>"
1695 " <LABEL for='email'> Email: </LABEL>" 2574 " <LABEL for='email'> Email: </LABEL>"
(...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after
2539 " <DT></DT>" 3418 " <DT></DT>"
2540 " <DD>" 3419 " <DD>"
2541 " <INPUT type='submit' name='reply-send' value='Send'/>" 3420 " <INPUT type='submit' name='reply-send' value='Send'/>"
2542 " </DD>" 3421 " </DD>"
2543 "</DL>" 3422 "</DL>"
2544 "</DIV>" 3423 "</DIV>"
2545 "</FORM>"); 3424 "</FORM>");
2546 } 3425 }
2547 3426
2548 TEST_F(FormAutofillTest, FillFormMaxLength) { 3427 TEST_F(FormAutofillTest, FillFormMaxLength) {
2549 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 3428 TestFillFormMaxLength(
2550 " <INPUT type='text' id='firstname' maxlength='5'/>" 3429 "<FORM name='TestForm' action='http://buh.com' method='post'>"
2551 " <INPUT type='text' id='lastname' maxlength='7'/>" 3430 " <INPUT type='text' id='firstname' maxlength='5'/>"
2552 " <INPUT type='text' id='email' maxlength='9'/>" 3431 " <INPUT type='text' id='lastname' maxlength='7'/>"
2553 " <INPUT type='submit' name='reply-send' value='Send'/>" 3432 " <INPUT type='text' id='email' maxlength='9'/>"
2554 "</FORM>"); 3433 " <INPUT type='submit' name='reply-send' value='Send'/>"
3434 "</FORM>",
3435 false);
3436 }
2555 3437
2556 WebFrame* web_frame = GetMainFrame(); 3438 TEST_F(FormAutofillTest, FillFormMaxLengthForUnownedForm) {
2557 ASSERT_NE(nullptr, web_frame); 3439 TestFillFormMaxLength(
2558 3440 "<INPUT type='text' id='firstname' maxlength='5'/>"
2559 FormCache form_cache; 3441 "<INPUT type='text' id='lastname' maxlength='7'/>"
2560 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3442 "<INPUT type='text' id='email' maxlength='9'/>"
2561 ASSERT_EQ(1U, forms.size()); 3443 "<INPUT type='submit' name='reply-send' value='Send'/>",
2562 3444 true);
2563 // Get the input element we want to find.
2564 WebInputElement input_element = GetInputElementById("firstname");
2565
2566 // Find the form that contains the input element.
2567 FormData form;
2568 FormFieldData field;
2569 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2570 &form,
2571 &field,
2572 autofill::REQUIRE_NONE));
2573 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2574 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2575 EXPECT_EQ(GURL("http://buh.com"), form.action);
2576
2577 const std::vector<FormFieldData>& fields = form.fields;
2578 ASSERT_EQ(3U, fields.size());
2579
2580 FormFieldData expected;
2581 expected.form_control_type = "text";
2582
2583 expected.name = ASCIIToUTF16("firstname");
2584 expected.max_length = 5;
2585 expected.is_autofilled = false;
2586 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2587
2588 expected.name = ASCIIToUTF16("lastname");
2589 expected.max_length = 7;
2590 expected.is_autofilled = false;
2591 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2592
2593 expected.name = ASCIIToUTF16("email");
2594 expected.max_length = 9;
2595 expected.is_autofilled = false;
2596 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2597
2598 // Fill the form.
2599 form.fields[0].value = ASCIIToUTF16("Brother");
2600 form.fields[1].value = ASCIIToUTF16("Jonathan");
2601 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2602 form.fields[0].is_autofilled = true;
2603 form.fields[1].is_autofilled = true;
2604 form.fields[2].is_autofilled = true;
2605 FillForm(form, input_element);
2606
2607 // Find the newly-filled form that contains the input element.
2608 FormData form2;
2609 FormFieldData field2;
2610 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2611 &form2,
2612 &field2,
2613 autofill::REQUIRE_NONE));
2614
2615 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2616 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2617 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2618
2619 const std::vector<FormFieldData>& fields2 = form2.fields;
2620 ASSERT_EQ(3U, fields2.size());
2621
2622 expected.form_control_type = "text";
2623
2624 expected.name = ASCIIToUTF16("firstname");
2625 expected.value = ASCIIToUTF16("Broth");
2626 expected.max_length = 5;
2627 expected.is_autofilled = true;
2628 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2629
2630 expected.name = ASCIIToUTF16("lastname");
2631 expected.value = ASCIIToUTF16("Jonatha");
2632 expected.max_length = 7;
2633 expected.is_autofilled = true;
2634 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2635
2636 expected.name = ASCIIToUTF16("email");
2637 expected.value = ASCIIToUTF16("brotherj@");
2638 expected.max_length = 9;
2639 expected.is_autofilled = true;
2640 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2641 } 3445 }
2642 3446
2643 // This test uses negative values of the maxlength attribute for input elements. 3447 // This test uses negative values of the maxlength attribute for input elements.
2644 // In this case, the maxlength of the input elements is set to the default 3448 // In this case, the maxlength of the input elements is set to the default
2645 // maxlength (defined in WebKit.) 3449 // maxlength (defined in WebKit.)
2646 TEST_F(FormAutofillTest, FillFormNegativeMaxLength) { 3450 TEST_F(FormAutofillTest, FillFormNegativeMaxLength) {
2647 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 3451 TestFillFormNegativeMaxLength(
2648 " <INPUT type='text' id='firstname' maxlength='-1'/>" 3452 "<FORM name='TestForm' action='http://buh.com' method='post'>"
2649 " <INPUT type='text' id='lastname' maxlength='-10'/>" 3453 " <INPUT type='text' id='firstname' maxlength='-1'/>"
2650 " <INPUT type='text' id='email' maxlength='-13'/>" 3454 " <INPUT type='text' id='lastname' maxlength='-10'/>"
2651 " <INPUT type='submit' name='reply-send' value='Send'/>" 3455 " <INPUT type='text' id='email' maxlength='-13'/>"
2652 "</FORM>"); 3456 " <INPUT type='submit' name='reply-send' value='Send'/>"
3457 "</FORM>",
3458 false);
3459 }
2653 3460
2654 WebFrame* web_frame = GetMainFrame(); 3461 TEST_F(FormAutofillTest, FillFormNegativeMaxLengthForUnownedForm) {
2655 ASSERT_NE(nullptr, web_frame); 3462 TestFillFormNegativeMaxLength(
2656 3463 "<INPUT type='text' id='firstname' maxlength='-1'/>"
2657 FormCache form_cache; 3464 "<INPUT type='text' id='lastname' maxlength='-10'/>"
2658 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3465 "<INPUT type='text' id='email' maxlength='-13'/>"
2659 ASSERT_EQ(1U, forms.size()); 3466 "<INPUT type='submit' name='reply-send' value='Send'/>",
2660 3467 true);
2661 // Get the input element we want to find.
2662 WebInputElement input_element = GetInputElementById("firstname");
2663
2664 // Find the form that contains the input element.
2665 FormData form;
2666 FormFieldData field;
2667 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2668 &form,
2669 &field,
2670 autofill::REQUIRE_NONE));
2671 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2672 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2673 EXPECT_EQ(GURL("http://buh.com"), form.action);
2674
2675 const std::vector<FormFieldData>& fields = form.fields;
2676 ASSERT_EQ(3U, fields.size());
2677
2678 FormFieldData expected;
2679 expected.form_control_type = "text";
2680 expected.max_length = WebInputElement::defaultMaxLength();
2681
2682 expected.name = ASCIIToUTF16("firstname");
2683 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2684
2685 expected.name = ASCIIToUTF16("lastname");
2686 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2687
2688 expected.name = ASCIIToUTF16("email");
2689 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2690
2691 // Fill the form.
2692 form.fields[0].value = ASCIIToUTF16("Brother");
2693 form.fields[1].value = ASCIIToUTF16("Jonathan");
2694 form.fields[2].value = ASCIIToUTF16("brotherj@example.com");
2695 FillForm(form, input_element);
2696
2697 // Find the newly-filled form that contains the input element.
2698 FormData form2;
2699 FormFieldData field2;
2700 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2701 &form2,
2702 &field2,
2703 autofill::REQUIRE_NONE));
2704
2705 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2706 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2707 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2708
2709 const std::vector<FormFieldData>& fields2 = form2.fields;
2710 ASSERT_EQ(3U, fields2.size());
2711
2712 expected.name = ASCIIToUTF16("firstname");
2713 expected.value = ASCIIToUTF16("Brother");
2714 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2715
2716 expected.name = ASCIIToUTF16("lastname");
2717 expected.value = ASCIIToUTF16("Jonathan");
2718 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2719
2720 expected.name = ASCIIToUTF16("email");
2721 expected.value = ASCIIToUTF16("brotherj@example.com");
2722 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2723 } 3468 }
2724 3469
2725 TEST_F(FormAutofillTest, FillFormEmptyName) { 3470 TEST_F(FormAutofillTest, FillFormEmptyName) {
2726 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 3471 TestFillFormEmptyName(
2727 " <INPUT type='text' id='firstname'/>" 3472 "<FORM name='TestForm' action='http://buh.com' method='post'>"
2728 " <INPUT type='text' id='lastname'/>" 3473 " <INPUT type='text' id='firstname'/>"
2729 " <INPUT type='text' id='email'/>" 3474 " <INPUT type='text' id='lastname'/>"
2730 " <INPUT type='submit' value='Send'/>" 3475 " <INPUT type='text' id='email'/>"
2731 "</FORM>"); 3476 " <INPUT type='submit' value='Send'/>"
3477 "</FORM>",
3478 false);
3479 }
2732 3480
2733 WebFrame* web_frame = GetMainFrame(); 3481 TEST_F(FormAutofillTest, FillFormEmptyNameForUnownedForm) {
2734 ASSERT_NE(nullptr, web_frame); 3482 TestFillFormEmptyName(
2735 3483 "<INPUT type='text' id='firstname'/>"
2736 FormCache form_cache; 3484 "<INPUT type='text' id='lastname'/>"
2737 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3485 "<INPUT type='text' id='email'/>"
2738 ASSERT_EQ(1U, forms.size()); 3486 "<INPUT type='submit' value='Send'/>",
2739 3487 true);
2740 // Get the input element we want to find.
2741 WebInputElement input_element = GetInputElementById("firstname");
2742
2743 // Find the form that contains the input element.
2744 FormData form;
2745 FormFieldData field;
2746 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2747 &form,
2748 &field,
2749 autofill::REQUIRE_NONE));
2750 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2751 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2752 EXPECT_EQ(GURL("http://buh.com"), form.action);
2753
2754 const std::vector<FormFieldData>& fields = form.fields;
2755 ASSERT_EQ(3U, fields.size());
2756
2757 FormFieldData expected;
2758 expected.form_control_type = "text";
2759 expected.max_length = WebInputElement::defaultMaxLength();
2760
2761 expected.name = ASCIIToUTF16("firstname");
2762 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2763
2764 expected.name = ASCIIToUTF16("lastname");
2765 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2766
2767 expected.name = ASCIIToUTF16("email");
2768 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2769
2770 // Fill the form.
2771 form.fields[0].value = ASCIIToUTF16("Wyatt");
2772 form.fields[1].value = ASCIIToUTF16("Earp");
2773 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
2774 FillForm(form, input_element);
2775
2776 // Find the newly-filled form that contains the input element.
2777 FormData form2;
2778 FormFieldData field2;
2779 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2780 &form2,
2781 &field2,
2782 autofill::REQUIRE_NONE));
2783
2784 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
2785 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2786 EXPECT_EQ(GURL("http://buh.com"), form2.action);
2787
2788 const std::vector<FormFieldData>& fields2 = form2.fields;
2789 ASSERT_EQ(3U, fields2.size());
2790
2791 expected.form_control_type = "text";
2792 expected.max_length = WebInputElement::defaultMaxLength();
2793
2794 expected.name = ASCIIToUTF16("firstname");
2795 expected.value = ASCIIToUTF16("Wyatt");
2796 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2797
2798 expected.name = ASCIIToUTF16("lastname");
2799 expected.value = ASCIIToUTF16("Earp");
2800 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2801
2802 expected.name = ASCIIToUTF16("email");
2803 expected.value = ASCIIToUTF16("wyatt@example.com");
2804 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2805 } 3488 }
2806 3489
2807 TEST_F(FormAutofillTest, FillFormEmptyFormNames) { 3490 TEST_F(FormAutofillTest, FillFormEmptyFormNames) {
2808 LoadHTML("<FORM action='http://buh.com' method='post'>" 3491 TestFillFormEmptyFormNames(
2809 " <INPUT type='text' id='firstname'/>" 3492 "<FORM action='http://buh.com' method='post'>"
2810 " <INPUT type='text' id='middlename'/>" 3493 " <INPUT type='text' id='firstname'/>"
2811 " <INPUT type='text' id='lastname'/>" 3494 " <INPUT type='text' id='middlename'/>"
2812 " <INPUT type='submit' value='Send'/>" 3495 " <INPUT type='text' id='lastname'/>"
2813 "</FORM>" 3496 " <INPUT type='submit' value='Send'/>"
2814 "<FORM action='http://abc.com' method='post'>" 3497 "</FORM>"
2815 " <INPUT type='text' id='apple'/>" 3498 "<FORM action='http://abc.com' method='post'>"
2816 " <INPUT type='text' id='banana'/>" 3499 " <INPUT type='text' id='apple'/>"
2817 " <INPUT type='text' id='cantelope'/>" 3500 " <INPUT type='text' id='banana'/>"
2818 " <INPUT type='submit' value='Send'/>" 3501 " <INPUT type='text' id='cantelope'/>"
2819 "</FORM>"); 3502 " <INPUT type='submit' value='Send'/>"
3503 "</FORM>",
3504 false);
3505 }
2820 3506
2821 WebFrame* web_frame = GetMainFrame(); 3507 TEST_F(FormAutofillTest, FillFormEmptyFormNamesForUnownedForm) {
2822 ASSERT_NE(nullptr, web_frame); 3508 TestFillFormEmptyFormNames(
2823 3509 "<INPUT type='text' id='firstname'/>"
2824 FormCache form_cache; 3510 "<INPUT type='text' id='middlename'/>"
2825 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3511 "<INPUT type='text' id='lastname'/>"
2826 ASSERT_EQ(2U, forms.size()); 3512 "<INPUT type='text' id='apple'/>"
2827 3513 "<INPUT type='text' id='banana'/>"
2828 // Get the input element we want to find. 3514 "<INPUT type='text' id='cantelope'/>"
2829 WebInputElement input_element = GetInputElementById("apple"); 3515 "<INPUT type='submit' value='Send'/>",
2830 3516 true);
2831 // Find the form that contains the input element.
2832 FormData form;
2833 FormFieldData field;
2834 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2835 &form,
2836 &field,
2837 autofill::REQUIRE_NONE));
2838 EXPECT_EQ(base::string16(), form.name);
2839 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
2840 EXPECT_EQ(GURL("http://abc.com"), form.action);
2841
2842 const std::vector<FormFieldData>& fields = form.fields;
2843 ASSERT_EQ(3U, fields.size());
2844
2845 FormFieldData expected;
2846 expected.form_control_type = "text";
2847 expected.max_length = WebInputElement::defaultMaxLength();
2848
2849 expected.name = ASCIIToUTF16("apple");
2850 expected.is_autofilled = false;
2851 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
2852
2853 expected.name = ASCIIToUTF16("banana");
2854 expected.is_autofilled = false;
2855 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
2856
2857 expected.name = ASCIIToUTF16("cantelope");
2858 expected.is_autofilled = false;
2859 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
2860
2861 // Fill the form.
2862 form.fields[0].value = ASCIIToUTF16("Red");
2863 form.fields[1].value = ASCIIToUTF16("Yellow");
2864 form.fields[2].value = ASCIIToUTF16("Also Yellow");
2865 form.fields[0].is_autofilled = true;
2866 form.fields[1].is_autofilled = true;
2867 form.fields[2].is_autofilled = true;
2868 FillForm(form, input_element);
2869
2870 // Find the newly-filled form that contains the input element.
2871 FormData form2;
2872 FormFieldData field2;
2873 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
2874 &form2,
2875 &field2,
2876 autofill::REQUIRE_NONE));
2877
2878 EXPECT_EQ(base::string16(), form2.name);
2879 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
2880 EXPECT_EQ(GURL("http://abc.com"), form2.action);
2881
2882 const std::vector<FormFieldData>& fields2 = form2.fields;
2883 ASSERT_EQ(3U, fields2.size());
2884
2885 expected.name = ASCIIToUTF16("apple");
2886 expected.value = ASCIIToUTF16("Red");
2887 expected.is_autofilled = true;
2888 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
2889
2890 expected.name = ASCIIToUTF16("banana");
2891 expected.value = ASCIIToUTF16("Yellow");
2892 expected.is_autofilled = true;
2893 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
2894
2895 expected.name = ASCIIToUTF16("cantelope");
2896 expected.value = ASCIIToUTF16("Also Yellow");
2897 expected.is_autofilled = true;
2898 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
2899 } 3517 }
2900 3518
2901 TEST_F(FormAutofillTest, ThreePartPhone) { 3519 TEST_F(FormAutofillTest, ThreePartPhone) {
2902 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>" 3520 LoadHTML("<FORM name='TestForm' action='http://cnn.com' method='post'>"
2903 " Phone:" 3521 " Phone:"
2904 " <input type='text' name='dayphone1'>" 3522 " <input type='text' name='dayphone1'>"
2905 " -" 3523 " -"
2906 " <input type='text' name='dayphone2'>" 3524 " <input type='text' name='dayphone2'>"
2907 " -" 3525 " -"
2908 " <input type='text' name='dayphone3'>" 3526 " <input type='text' name='dayphone3'>"
2909 " ext.:" 3527 " ext.:"
2910 " <input type='text' name='dayphone4'>" 3528 " <input type='text' name='dayphone4'>"
2911 " <input type='submit' name='reply-send' value='Send'>" 3529 " <input type='submit' name='reply-send' value='Send'>"
2912 "</FORM>"); 3530 "</FORM>");
2913 3531
2914 3532
2915 WebFrame* frame = GetMainFrame(); 3533 WebFrame* frame = GetMainFrame();
2916 ASSERT_NE(nullptr, frame); 3534 ASSERT_NE(nullptr, frame);
2917 3535
2918 WebVector<WebFormElement> forms; 3536 WebVector<WebFormElement> forms;
2919 frame->document().forms(forms); 3537 frame->document().forms(forms);
2920 ASSERT_EQ(1U, forms.size()); 3538 ASSERT_EQ(1U, forms.size());
2921 3539
2922 FormData form; 3540 FormData form;
2923 EXPECT_TRUE(WebFormElementToFormData(forms[0], 3541 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2924 WebFormControlElement(), 3542 WebFormControlElement(),
2925 autofill::REQUIRE_NONE, 3543 REQUIRE_NONE,
2926 autofill::EXTRACT_VALUE, 3544 EXTRACT_VALUE,
2927 &form, 3545 &form,
2928 NULL)); 3546 nullptr));
2929 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); 3547 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2930 EXPECT_EQ(GURL(frame->document().url()), form.origin); 3548 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2931 EXPECT_EQ(GURL("http://cnn.com"), form.action); 3549 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2932 3550
2933 const std::vector<FormFieldData>& fields = form.fields; 3551 const std::vector<FormFieldData>& fields = form.fields;
2934 ASSERT_EQ(4U, fields.size()); 3552 ASSERT_EQ(4U, fields.size());
2935 3553
2936 FormFieldData expected; 3554 FormFieldData expected;
2937 expected.form_control_type = "text"; 3555 expected.form_control_type = "text";
2938 expected.max_length = WebInputElement::defaultMaxLength(); 3556 expected.max_length = WebInputElement::defaultMaxLength();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2974 WebFrame* frame = GetMainFrame(); 3592 WebFrame* frame = GetMainFrame();
2975 ASSERT_NE(nullptr, frame); 3593 ASSERT_NE(nullptr, frame);
2976 3594
2977 WebVector<WebFormElement> forms; 3595 WebVector<WebFormElement> forms;
2978 frame->document().forms(forms); 3596 frame->document().forms(forms);
2979 ASSERT_EQ(1U, forms.size()); 3597 ASSERT_EQ(1U, forms.size());
2980 3598
2981 FormData form; 3599 FormData form;
2982 EXPECT_TRUE(WebFormElementToFormData(forms[0], 3600 EXPECT_TRUE(WebFormElementToFormData(forms[0],
2983 WebFormControlElement(), 3601 WebFormControlElement(),
2984 autofill::REQUIRE_NONE, 3602 REQUIRE_NONE,
2985 autofill::EXTRACT_VALUE, 3603 EXTRACT_VALUE,
2986 &form, 3604 &form,
2987 NULL)); 3605 nullptr));
2988 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); 3606 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
2989 EXPECT_EQ(GURL(frame->document().url()), form.origin); 3607 EXPECT_EQ(GURL(frame->document().url()), form.origin);
2990 EXPECT_EQ(GURL("http://cnn.com"), form.action); 3608 EXPECT_EQ(GURL("http://cnn.com"), form.action);
2991 3609
2992 const std::vector<FormFieldData>& fields = form.fields; 3610 const std::vector<FormFieldData>& fields = form.fields;
2993 ASSERT_EQ(6U, fields.size()); 3611 ASSERT_EQ(6U, fields.size());
2994 3612
2995 FormFieldData expected; 3613 FormFieldData expected;
2996 expected.form_control_type = "text"; 3614 expected.form_control_type = "text";
2997 3615
(...skipping 11 matching lines...) Expand all
3009 expected.name = ASCIIToUTF16("dayphone3"); 3627 expected.name = ASCIIToUTF16("dayphone3");
3010 expected.max_length = 4; 3628 expected.max_length = 4;
3011 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); 3629 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3012 3630
3013 expected.label = ASCIIToUTF16("ext.:"); 3631 expected.label = ASCIIToUTF16("ext.:");
3014 expected.name = ASCIIToUTF16("dayphone4"); 3632 expected.name = ASCIIToUTF16("dayphone4");
3015 expected.max_length = 5; 3633 expected.max_length = 5;
3016 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]); 3634 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[3]);
3017 3635
3018 // When unspecified |size|, default is returned. 3636 // When unspecified |size|, default is returned.
3019 expected.label = base::string16(); 3637 expected.label.clear();
3020 expected.name = ASCIIToUTF16("default1"); 3638 expected.name = ASCIIToUTF16("default1");
3021 expected.max_length = WebInputElement::defaultMaxLength(); 3639 expected.max_length = WebInputElement::defaultMaxLength();
3022 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]); 3640 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[4]);
3023 3641
3024 // When invalid |size|, default is returned. 3642 // When invalid |size|, default is returned.
3025 expected.label = base::string16(); 3643 expected.label.clear();
3026 expected.name = ASCIIToUTF16("invalid1"); 3644 expected.name = ASCIIToUTF16("invalid1");
3027 expected.max_length = WebInputElement::defaultMaxLength(); 3645 expected.max_length = WebInputElement::defaultMaxLength();
3028 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]); 3646 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[5]);
3029 } 3647 }
3030 3648
3031 // This test re-creates the experience of typing in a field then selecting a 3649 // This test re-creates the experience of typing in a field then selecting a
3032 // profile from the Autofill suggestions popup. The field that is being typed 3650 // profile from the Autofill suggestions popup. The field that is being typed
3033 // into should be filled even though it's not technically empty. 3651 // into should be filled even though it's not technically empty.
3034 TEST_F(FormAutofillTest, FillFormNonEmptyField) { 3652 TEST_F(FormAutofillTest, FillFormNonEmptyField) {
3035 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 3653 TestFillFormNonEmptyField(
3036 " <INPUT type='text' id='firstname'/>" 3654 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3037 " <INPUT type='text' id='lastname'/>" 3655 " <INPUT type='text' id='firstname'/>"
3038 " <INPUT type='text' id='email'/>" 3656 " <INPUT type='text' id='lastname'/>"
3039 " <INPUT type='submit' value='Send'/>" 3657 " <INPUT type='text' id='email'/>"
3040 "</FORM>"); 3658 " <INPUT type='submit' value='Send'/>"
3659 "</FORM>",
3660 false);
3661 }
3041 3662
3042 WebFrame* web_frame = GetMainFrame(); 3663 TEST_F(FormAutofillTest, FillFormNonEmptyFieldForUnownedForm) {
3043 ASSERT_NE(nullptr, web_frame); 3664 TestFillFormNonEmptyField("<INPUT type='text' id='firstname'/>"
3044 3665 "<INPUT type='text' id='lastname'/>"
3045 FormCache form_cache; 3666 "<INPUT type='text' id='email'/>"
3046 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3667 "<INPUT type='submit' value='Send'/>",
3047 ASSERT_EQ(1U, forms.size()); 3668 true);
3048
3049 // Get the input element we want to find.
3050 WebInputElement input_element = GetInputElementById("firstname");
3051
3052 // Simulate typing by modifying the field value.
3053 input_element.setValue(ASCIIToUTF16("Wy"));
3054
3055 // Find the form that contains the input element.
3056 FormData form;
3057 FormFieldData field;
3058 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
3059 &form,
3060 &field,
3061 autofill::REQUIRE_NONE));
3062 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3063 EXPECT_EQ(GURL(web_frame->document().url()), form.origin);
3064 EXPECT_EQ(GURL("http://buh.com"), form.action);
3065
3066 const std::vector<FormFieldData>& fields = form.fields;
3067 ASSERT_EQ(3U, fields.size());
3068
3069 FormFieldData expected;
3070 expected.form_control_type = "text";
3071 expected.max_length = WebInputElement::defaultMaxLength();
3072
3073 expected.name = ASCIIToUTF16("firstname");
3074 expected.value = ASCIIToUTF16("Wy");
3075 expected.is_autofilled = false;
3076 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[0]);
3077
3078 expected.name = ASCIIToUTF16("lastname");
3079 expected.value = base::string16();
3080 expected.is_autofilled = false;
3081 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[1]);
3082
3083 expected.name = ASCIIToUTF16("email");
3084 expected.value = base::string16();
3085 expected.is_autofilled = false;
3086 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3087
3088 // Preview the form and verify that the cursor position has been updated.
3089 form.fields[0].value = ASCIIToUTF16("Wyatt");
3090 form.fields[1].value = ASCIIToUTF16("Earp");
3091 form.fields[2].value = ASCIIToUTF16("wyatt@example.com");
3092 form.fields[0].is_autofilled = true;
3093 form.fields[1].is_autofilled = true;
3094 form.fields[2].is_autofilled = true;
3095 PreviewForm(form, input_element);
3096 EXPECT_EQ(2, input_element.selectionStart());
3097 EXPECT_EQ(5, input_element.selectionEnd());
3098
3099 // Fill the form.
3100 FillForm(form, input_element);
3101
3102 // Find the newly-filled form that contains the input element.
3103 FormData form2;
3104 FormFieldData field2;
3105 EXPECT_TRUE(FindFormAndFieldForFormControlElement(input_element,
3106 &form2,
3107 &field2,
3108 autofill::REQUIRE_NONE));
3109
3110 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3111 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3112 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3113
3114 const std::vector<FormFieldData>& fields2 = form2.fields;
3115 ASSERT_EQ(3U, fields2.size());
3116
3117 expected.name = ASCIIToUTF16("firstname");
3118 expected.value = ASCIIToUTF16("Wyatt");
3119 expected.is_autofilled = true;
3120 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3121
3122 expected.name = ASCIIToUTF16("lastname");
3123 expected.value = ASCIIToUTF16("Earp");
3124 expected.is_autofilled = true;
3125 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3126
3127 expected.name = ASCIIToUTF16("email");
3128 expected.value = ASCIIToUTF16("wyatt@example.com");
3129 expected.is_autofilled = true;
3130 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3131
3132 // Verify that the cursor position has been updated.
3133 EXPECT_EQ(5, input_element.selectionStart());
3134 EXPECT_EQ(5, input_element.selectionEnd());
3135 } 3669 }
3136 3670
3137 TEST_F(FormAutofillTest, ClearFormWithNode) { 3671 TEST_F(FormAutofillTest, ClearFormWithNode) {
3138 LoadHTML( 3672 TestClearFormWithNode(
3139 "<FORM name='TestForm' action='http://buh.com' method='post'>" 3673 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3140 " <INPUT type='text' id='firstname' value='Wyatt'/>" 3674 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3141 " <INPUT type='text' id='lastname' value='Earp'/>" 3675 " <INPUT type='text' id='lastname' value='Earp'/>"
3142 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>" 3676 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3143 " <INPUT type='text' id='notenabled' disabled='disabled'>" 3677 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3144 " <INPUT type='month' id='month' value='2012-11'>" 3678 " <INPUT type='month' id='month' value='2012-11'>"
3145 " <INPUT type='month' id='month-disabled' value='2012-11'" 3679 " <INPUT type='month' id='month-disabled' value='2012-11'"
3146 " disabled='disabled'>" 3680 " disabled='disabled'>"
3147 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>" 3681 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3148 " <TEXTAREA id='textarea-disabled' disabled='disabled'>" 3682 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3149 " Banana!" 3683 " Banana!"
3150 " </TEXTAREA>" 3684 " </TEXTAREA>"
3151 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>" 3685 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3152 " <INPUT type='submit' value='Send'/>" 3686 " <INPUT type='submit' value='Send'/>"
3153 "</FORM>"); 3687 "</FORM>",
3688 false);
3689 }
3154 3690
3155 WebFrame* web_frame = GetMainFrame(); 3691 TEST_F(FormAutofillTest, ClearFormWithNodeForUnownedForm) {
3156 ASSERT_NE(nullptr, web_frame); 3692 TestClearFormWithNode(
3157 3693 " <!-- Indented on purpose //-->"
3158 FormCache form_cache; 3694 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3159 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3695 " <INPUT type='text' id='lastname' value='Earp'/>"
3160 ASSERT_EQ(1U, forms.size()); 3696 " <INPUT type='text' autocomplete='off' id='noAC' value='one'/>"
3161 3697 " <INPUT type='text' id='notenabled' disabled='disabled'>"
3162 // Set the auto-filled attribute. 3698 " <INPUT type='month' id='month' value='2012-11'>"
3163 WebInputElement firstname = GetInputElementById("firstname"); 3699 " <INPUT type='month' id='month-disabled' value='2012-11'"
3164 firstname.setAutofilled(true); 3700 " disabled='disabled'>"
3165 WebInputElement lastname = GetInputElementById("lastname"); 3701 " <TEXTAREA id='textarea'>Apple.</TEXTAREA>"
3166 lastname.setAutofilled(true); 3702 " <TEXTAREA id='textarea-disabled' disabled='disabled'>"
3167 WebInputElement month = GetInputElementById("month"); 3703 " Banana!"
3168 month.setAutofilled(true); 3704 " </TEXTAREA>"
3169 WebInputElement textarea = GetInputElementById("textarea"); 3705 " <TEXTAREA id='textarea-noAC' autocomplete='off'>Carrot?</TEXTAREA>"
3170 textarea.setAutofilled(true); 3706 " <INPUT type='submit' value='Send'/>",
3171 3707 true);
3172 // Set the value of the disabled text input element.
3173 WebInputElement notenabled = GetInputElementById("notenabled");
3174 notenabled.setValue(WebString::fromUTF8("no clear"));
3175
3176 // Clear the form.
3177 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3178
3179 // Verify that the auto-filled attribute has been turned off.
3180 EXPECT_FALSE(firstname.isAutofilled());
3181
3182 // Verify the form is cleared.
3183 FormData form2;
3184 FormFieldData field2;
3185 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
3186 &form2,
3187 &field2,
3188 autofill::REQUIRE_NONE));
3189 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3190 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3191 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3192
3193 const std::vector<FormFieldData>& fields2 = form2.fields;
3194 ASSERT_EQ(9U, fields2.size());
3195
3196 FormFieldData expected;
3197 expected.form_control_type = "text";
3198 expected.max_length = WebInputElement::defaultMaxLength();
3199
3200 expected.name = ASCIIToUTF16("firstname");
3201 expected.value = base::string16();
3202 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3203
3204 expected.name = ASCIIToUTF16("lastname");
3205 expected.value = base::string16();
3206 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3207
3208 expected.name = ASCIIToUTF16("noAC");
3209 expected.value = ASCIIToUTF16("one");
3210 expected.autocomplete_attribute = "off";
3211 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3212 expected.autocomplete_attribute = std::string(); // reset
3213
3214 expected.name = ASCIIToUTF16("notenabled");
3215 expected.value = ASCIIToUTF16("no clear");
3216 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[3]);
3217
3218 expected.form_control_type = "month";
3219 expected.max_length = 0;
3220 expected.name = ASCIIToUTF16("month");
3221 expected.value = base::string16();
3222 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[4]);
3223
3224 expected.name = ASCIIToUTF16("month-disabled");
3225 expected.value = ASCIIToUTF16("2012-11");
3226 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[5]);
3227
3228 expected.form_control_type = "textarea";
3229 expected.name = ASCIIToUTF16("textarea");
3230 expected.value = base::string16();
3231 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[6]);
3232
3233 expected.name = ASCIIToUTF16("textarea-disabled");
3234 expected.value = ASCIIToUTF16(" Banana! ");
3235 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[7]);
3236
3237 expected.name = ASCIIToUTF16("textarea-noAC");
3238 expected.value = ASCIIToUTF16("Carrot?");
3239 expected.autocomplete_attribute = "off";
3240 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[8]);
3241 expected.autocomplete_attribute = std::string(); // reset
3242
3243 // Verify that the cursor position has been updated.
3244 EXPECT_EQ(0, firstname.selectionStart());
3245 EXPECT_EQ(0, firstname.selectionEnd());
3246 } 3708 }
3247 3709
3248 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) { 3710 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOne) {
3249 LoadHTML( 3711 TestClearFormWithNodeContainingSelectOne(
3250 "<FORM name='TestForm' action='http://buh.com' method='post'>" 3712 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3251 " <INPUT type='text' id='firstname' value='Wyatt'/>" 3713 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3252 " <INPUT type='text' id='lastname' value='Earp'/>" 3714 " <INPUT type='text' id='lastname' value='Earp'/>"
3253 " <SELECT id='state' name='state'>" 3715 " <SELECT id='state' name='state'>"
3254 " <OPTION selected>?</OPTION>" 3716 " <OPTION selected>?</OPTION>"
3255 " <OPTION>AA</OPTION>" 3717 " <OPTION>AA</OPTION>"
3256 " <OPTION>AE</OPTION>" 3718 " <OPTION>AE</OPTION>"
3257 " <OPTION>AK</OPTION>" 3719 " <OPTION>AK</OPTION>"
3258 " </SELECT>" 3720 " </SELECT>"
3259 " <INPUT type='submit' value='Send'/>" 3721 " <INPUT type='submit' value='Send'/>"
3260 "</FORM>"); 3722 "</FORM>",
3723 false);
3724 }
3261 3725
3262 WebFrame* web_frame = GetMainFrame(); 3726 TEST_F(FormAutofillTest, ClearFormWithNodeContainingSelectOneForUnownedForm) {
3263 ASSERT_NE(nullptr, web_frame); 3727 TestClearFormWithNodeContainingSelectOne(
3264 3728 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3265 FormCache form_cache; 3729 "<INPUT type='text' id='lastname' value='Earp'/>"
3266 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3730 "<SELECT id='state' name='state'>"
3267 ASSERT_EQ(1U, forms.size()); 3731 " <OPTION selected>?</OPTION>"
3268 3732 " <OPTION>AA</OPTION>"
3269 // Set the auto-filled attribute. 3733 " <OPTION>AE</OPTION>"
3270 WebInputElement firstname = GetInputElementById("firstname"); 3734 " <OPTION>AK</OPTION>"
3271 firstname.setAutofilled(true); 3735 "</SELECT>"
3272 WebInputElement lastname = GetInputElementById("lastname"); 3736 "<INPUT type='submit' value='Send'/>",
3273 lastname.setAutofilled(true); 3737 true);
3274
3275 // Set the value and auto-filled attribute of the state element.
3276 WebSelectElement state =
3277 web_frame->document().getElementById("state").to<WebSelectElement>();
3278 state.setValue(WebString::fromUTF8("AK"));
3279 state.setAutofilled(true);
3280
3281 // Clear the form.
3282 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3283
3284 // Verify that the auto-filled attribute has been turned off.
3285 EXPECT_FALSE(firstname.isAutofilled());
3286
3287 // Verify the form is cleared.
3288 FormData form2;
3289 FormFieldData field2;
3290 EXPECT_TRUE(FindFormAndFieldForFormControlElement(firstname,
3291 &form2,
3292 &field2,
3293 autofill::REQUIRE_NONE));
3294 EXPECT_EQ(ASCIIToUTF16("TestForm"), form2.name);
3295 EXPECT_EQ(GURL(web_frame->document().url()), form2.origin);
3296 EXPECT_EQ(GURL("http://buh.com"), form2.action);
3297
3298 const std::vector<FormFieldData>& fields2 = form2.fields;
3299 ASSERT_EQ(3U, fields2.size());
3300
3301 FormFieldData expected;
3302
3303 expected.name = ASCIIToUTF16("firstname");
3304 expected.value = base::string16();
3305 expected.form_control_type = "text";
3306 expected.max_length = WebInputElement::defaultMaxLength();
3307 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[0]);
3308
3309 expected.name = ASCIIToUTF16("lastname");
3310 expected.value = base::string16();
3311 expected.form_control_type = "text";
3312 expected.max_length = WebInputElement::defaultMaxLength();
3313 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[1]);
3314
3315 expected.name = ASCIIToUTF16("state");
3316 expected.value = ASCIIToUTF16("?");
3317 expected.form_control_type = "select-one";
3318 expected.max_length = 0;
3319 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields2[2]);
3320
3321 // Verify that the cursor position has been updated.
3322 EXPECT_EQ(0, firstname.selectionStart());
3323 EXPECT_EQ(0, firstname.selectionEnd());
3324 } 3738 }
3325 3739
3326 TEST_F(FormAutofillTest, ClearPreviewedFormWithElement) { 3740 TEST_F(FormAutofillTest, ClearPreviewedFormWithElement) {
3327 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 3741 TestClearPreviewedFormWithElement(
3328 " <INPUT type='text' id='firstname' value='Wyatt'/>" 3742 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3329 " <INPUT type='text' id='lastname'/>" 3743 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3330 " <INPUT type='text' id='email'/>" 3744 " <INPUT type='text' id='lastname'/>"
3331 " <INPUT type='email' id='email2'/>" 3745 " <INPUT type='text' id='email'/>"
3332 " <INPUT type='tel' id='phone'/>" 3746 " <INPUT type='email' id='email2'/>"
3333 " <INPUT type='submit' value='Send'/>" 3747 " <INPUT type='tel' id='phone'/>"
3334 "</FORM>"); 3748 " <INPUT type='submit' value='Send'/>"
3749 "</FORM>");
3750 }
3335 3751
3336 WebFrame* web_frame = GetMainFrame(); 3752 TEST_F(FormAutofillTest, ClearPreviewedFormWithElementForUnownedForm) {
3337 ASSERT_NE(nullptr, web_frame); 3753 TestClearPreviewedFormWithElement(
3338 3754 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3339 FormCache form_cache; 3755 "<INPUT type='text' id='lastname'/>"
3340 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3756 "<INPUT type='text' id='email'/>"
3341 ASSERT_EQ(1U, forms.size()); 3757 "<INPUT type='email' id='email2'/>"
3342 3758 "<INPUT type='tel' id='phone'/>"
3343 // Set the auto-filled attribute. 3759 "<INPUT type='submit' value='Send'/>");
3344 WebInputElement firstname = GetInputElementById("firstname");
3345 firstname.setAutofilled(true);
3346 WebInputElement lastname = GetInputElementById("lastname");
3347 lastname.setAutofilled(true);
3348 WebInputElement email = GetInputElementById("email");
3349 email.setAutofilled(true);
3350 WebInputElement email2 = GetInputElementById("email2");
3351 email2.setAutofilled(true);
3352 WebInputElement phone = GetInputElementById("phone");
3353 phone.setAutofilled(true);
3354
3355 // Set the suggested values on two of the elements.
3356 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3357 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3358 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3359 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3360
3361 // Clear the previewed fields.
3362 EXPECT_TRUE(ClearPreviewedFormWithElement(lastname, false));
3363
3364 // Fields with empty suggestions suggestions are not modified.
3365 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
3366 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3367 EXPECT_TRUE(firstname.isAutofilled());
3368
3369 // Verify the previewed fields are cleared.
3370 EXPECT_TRUE(lastname.value().isEmpty());
3371 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3372 EXPECT_FALSE(lastname.isAutofilled());
3373 EXPECT_TRUE(email.value().isEmpty());
3374 EXPECT_TRUE(email.suggestedValue().isEmpty());
3375 EXPECT_FALSE(email.isAutofilled());
3376 EXPECT_TRUE(email2.value().isEmpty());
3377 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3378 EXPECT_FALSE(email2.isAutofilled());
3379 EXPECT_TRUE(phone.value().isEmpty());
3380 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3381 EXPECT_FALSE(phone.isAutofilled());
3382
3383 // Verify that the cursor position has been updated.
3384 EXPECT_EQ(0, lastname.selectionStart());
3385 EXPECT_EQ(0, lastname.selectionEnd());
3386 } 3760 }
3387 3761
3388 TEST_F(FormAutofillTest, ClearPreviewedFormWithNonEmptyInitiatingNode) { 3762 TEST_F(FormAutofillTest, ClearPreviewedFormWithNonEmptyInitiatingNode) {
3389 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 3763 TestClearPreviewedFormWithNonEmptyInitiatingNode(
3390 " <INPUT type='text' id='firstname' value='W'/>" 3764 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3391 " <INPUT type='text' id='lastname'/>" 3765 " <INPUT type='text' id='firstname' value='W'/>"
3392 " <INPUT type='text' id='email'/>" 3766 " <INPUT type='text' id='lastname'/>"
3393 " <INPUT type='email' id='email2'/>" 3767 " <INPUT type='text' id='email'/>"
3394 " <INPUT type='tel' id='phone'/>" 3768 " <INPUT type='email' id='email2'/>"
3395 " <INPUT type='submit' value='Send'/>" 3769 " <INPUT type='tel' id='phone'/>"
3396 "</FORM>"); 3770 " <INPUT type='submit' value='Send'/>"
3771 "</FORM>");
3772 }
3397 3773
3398 WebFrame* web_frame = GetMainFrame(); 3774 TEST_F(FormAutofillTest,
3399 ASSERT_NE(nullptr, web_frame); 3775 ClearPreviewedFormWithNonEmptyInitiatingNodeForUnownedForm) {
3400 3776 TestClearPreviewedFormWithNonEmptyInitiatingNode(
3401 FormCache form_cache; 3777 "<INPUT type='text' id='firstname' value='W'/>"
3402 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3778 "<INPUT type='text' id='lastname'/>"
3403 ASSERT_EQ(1U, forms.size()); 3779 "<INPUT type='text' id='email'/>"
3404 3780 "<INPUT type='email' id='email2'/>"
3405 // Set the auto-filled attribute. 3781 "<INPUT type='tel' id='phone'/>"
3406 WebInputElement firstname = GetInputElementById("firstname"); 3782 "<INPUT type='submit' value='Send'/>");
3407 firstname.setAutofilled(true);
3408 WebInputElement lastname = GetInputElementById("lastname");
3409 lastname.setAutofilled(true);
3410 WebInputElement email = GetInputElementById("email");
3411 email.setAutofilled(true);
3412 WebInputElement email2 = GetInputElementById("email2");
3413 email2.setAutofilled(true);
3414 WebInputElement phone = GetInputElementById("phone");
3415 phone.setAutofilled(true);
3416
3417
3418 // Set the suggested values on all of the elements.
3419 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3420 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3421 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3422 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3423 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3424
3425 // Clear the previewed fields.
3426 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, false));
3427
3428 // Fields with non-empty values are restored.
3429 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
3430 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3431 EXPECT_FALSE(firstname.isAutofilled());
3432 EXPECT_EQ(1, firstname.selectionStart());
3433 EXPECT_EQ(1, firstname.selectionEnd());
3434
3435 // Verify the previewed fields are cleared.
3436 EXPECT_TRUE(lastname.value().isEmpty());
3437 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3438 EXPECT_FALSE(lastname.isAutofilled());
3439 EXPECT_TRUE(email.value().isEmpty());
3440 EXPECT_TRUE(email.suggestedValue().isEmpty());
3441 EXPECT_FALSE(email.isAutofilled());
3442 EXPECT_TRUE(email2.value().isEmpty());
3443 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3444 EXPECT_FALSE(email2.isAutofilled());
3445 EXPECT_TRUE(phone.value().isEmpty());
3446 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3447 EXPECT_FALSE(phone.isAutofilled());
3448 } 3783 }
3449 3784
3450 TEST_F(FormAutofillTest, ClearPreviewedFormWithAutofilledInitiatingNode) { 3785 TEST_F(FormAutofillTest, ClearPreviewedFormWithAutofilledInitiatingNode) {
3451 LoadHTML("<FORM name='TestForm' action='http://buh.com' method='post'>" 3786 TestClearPreviewedFormWithAutofilledInitiatingNode(
3452 " <INPUT type='text' id='firstname' value='W'/>" 3787 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3453 " <INPUT type='text' id='lastname'/>" 3788 " <INPUT type='text' id='firstname' value='W'/>"
3454 " <INPUT type='text' id='email'/>" 3789 " <INPUT type='text' id='lastname'/>"
3455 " <INPUT type='email' id='email2'/>" 3790 " <INPUT type='text' id='email'/>"
3456 " <INPUT type='tel' id='phone'/>" 3791 " <INPUT type='email' id='email2'/>"
3457 " <INPUT type='submit' value='Send'/>" 3792 " <INPUT type='tel' id='phone'/>"
3458 "</FORM>"); 3793 " <INPUT type='submit' value='Send'/>"
3794 "</FORM>");
3795 }
3459 3796
3460 WebFrame* web_frame = GetMainFrame(); 3797 TEST_F(FormAutofillTest,
3461 ASSERT_NE(nullptr, web_frame); 3798 ClearPreviewedFormWithAutofilledInitiatingNodeForUnownedForm) {
3462 3799 TestClearPreviewedFormWithAutofilledInitiatingNode(
3463 FormCache form_cache; 3800 "<INPUT type='text' id='firstname' value='W'/>"
3464 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3801 "<INPUT type='text' id='lastname'/>"
3465 ASSERT_EQ(1U, forms.size()); 3802 "<INPUT type='text' id='email'/>"
3466 3803 "<INPUT type='email' id='email2'/>"
3467 // Set the auto-filled attribute. 3804 "<INPUT type='tel' id='phone'/>"
3468 WebInputElement firstname = GetInputElementById("firstname"); 3805 "<INPUT type='submit' value='Send'/>");
3469 firstname.setAutofilled(true);
3470 WebInputElement lastname = GetInputElementById("lastname");
3471 lastname.setAutofilled(true);
3472 WebInputElement email = GetInputElementById("email");
3473 email.setAutofilled(true);
3474 WebInputElement email2 = GetInputElementById("email2");
3475 email2.setAutofilled(true);
3476 WebInputElement phone = GetInputElementById("phone");
3477 phone.setAutofilled(true);
3478
3479 // Set the suggested values on all of the elements.
3480 firstname.setSuggestedValue(ASCIIToUTF16("Wyatt"));
3481 lastname.setSuggestedValue(ASCIIToUTF16("Earp"));
3482 email.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3483 email2.setSuggestedValue(ASCIIToUTF16("wyatt@earp.com"));
3484 phone.setSuggestedValue(ASCIIToUTF16("650-777-9999"));
3485
3486 // Clear the previewed fields.
3487 EXPECT_TRUE(ClearPreviewedFormWithElement(firstname, true));
3488
3489 // Fields with non-empty values are restored.
3490 EXPECT_EQ(ASCIIToUTF16("W"), firstname.value());
3491 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3492 EXPECT_TRUE(firstname.isAutofilled());
3493 EXPECT_EQ(1, firstname.selectionStart());
3494 EXPECT_EQ(1, firstname.selectionEnd());
3495
3496 // Verify the previewed fields are cleared.
3497 EXPECT_TRUE(lastname.value().isEmpty());
3498 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3499 EXPECT_FALSE(lastname.isAutofilled());
3500 EXPECT_TRUE(email.value().isEmpty());
3501 EXPECT_TRUE(email.suggestedValue().isEmpty());
3502 EXPECT_FALSE(email.isAutofilled());
3503 EXPECT_TRUE(email2.value().isEmpty());
3504 EXPECT_TRUE(email2.suggestedValue().isEmpty());
3505 EXPECT_FALSE(email2.isAutofilled());
3506 EXPECT_TRUE(phone.value().isEmpty());
3507 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3508 EXPECT_FALSE(phone.isAutofilled());
3509 } 3806 }
3510 3807
3511 // Autofill's "Clear Form" should clear only autofilled fields 3808 // Autofill's "Clear Form" should clear only autofilled fields
3512 TEST_F(FormAutofillTest, ClearOnlyAutofilledFields) { 3809 TEST_F(FormAutofillTest, ClearOnlyAutofilledFields) {
3513 // Load the form. 3810 TestClearOnlyAutofilledFields(
3514 LoadHTML(
3515 "<FORM name='TestForm' action='http://buh.com' method='post'>" 3811 "<FORM name='TestForm' action='http://buh.com' method='post'>"
3516 " <INPUT type='text' id='firstname' value='Wyatt'/>" 3812 " <INPUT type='text' id='firstname' value='Wyatt'/>"
3517 " <INPUT type='text' id='lastname' value='Earp'/>" 3813 " <INPUT type='text' id='lastname' value='Earp'/>"
3518 " <INPUT type='email' id='email' value='wyatt@earp.com'/>" 3814 " <INPUT type='email' id='email' value='wyatt@earp.com'/>"
3519 " <INPUT type='tel' id='phone' value='650-777-9999'/>" 3815 " <INPUT type='tel' id='phone' value='650-777-9999'/>"
3520 " <INPUT type='submit' value='Send'/>" 3816 " <INPUT type='submit' value='Send'/>"
3521 "</FORM>"); 3817 "</FORM>");
3818 }
3522 3819
3523 WebFrame* web_frame = GetMainFrame(); 3820 TEST_F(FormAutofillTest, ClearOnlyAutofilledFieldsForUnownedForm) {
3524 ASSERT_NE(nullptr, web_frame); 3821 TestClearOnlyAutofilledFields(
3525 3822 "<INPUT type='text' id='firstname' value='Wyatt'/>"
3526 FormCache form_cache; 3823 "<INPUT type='text' id='lastname' value='Earp'/>"
3527 std::vector<FormData> forms = form_cache.ExtractNewForms(*web_frame); 3824 "<INPUT type='email' id='email' value='wyatt@earp.com'/>"
3528 ASSERT_EQ(1U, forms.size()); 3825 "<INPUT type='tel' id='phone' value='650-777-9999'/>"
3529 3826 "<INPUT type='submit' value='Send'/>");
3530 // Set the autofilled attribute.
3531 WebInputElement firstname = GetInputElementById("firstname");
3532 firstname.setAutofilled(false);
3533 WebInputElement lastname = GetInputElementById("lastname");
3534 lastname.setAutofilled(true);
3535 WebInputElement email = GetInputElementById("email");
3536 email.setAutofilled(true);
3537 WebInputElement phone = GetInputElementById("phone");
3538 phone.setAutofilled(true);
3539
3540 // Clear the fields.
3541 EXPECT_TRUE(form_cache.ClearFormWithElement(firstname));
3542
3543 // Verify only autofilled fields are cleared.
3544 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value());
3545 EXPECT_TRUE(firstname.suggestedValue().isEmpty());
3546 EXPECT_FALSE(firstname.isAutofilled());
3547 EXPECT_TRUE(lastname.value().isEmpty());
3548 EXPECT_TRUE(lastname.suggestedValue().isEmpty());
3549 EXPECT_FALSE(lastname.isAutofilled());
3550 EXPECT_TRUE(email.value().isEmpty());
3551 EXPECT_TRUE(email.suggestedValue().isEmpty());
3552 EXPECT_FALSE(email.isAutofilled());
3553 EXPECT_TRUE(phone.value().isEmpty());
3554 EXPECT_TRUE(phone.suggestedValue().isEmpty());
3555 EXPECT_FALSE(phone.isAutofilled());
3556 } 3827 }
3557 3828
3558 // If we have multiple labels per id, the labels concatenated into label string. 3829 // If we have multiple labels per id, the labels concatenated into label string.
3559 TEST_F(FormAutofillTest, MultipleLabelsPerElement) { 3830 TEST_F(FormAutofillTest, MultipleLabelsPerElement) {
3560 std::vector<base::string16> labels, names, values; 3831 std::vector<base::string16> labels, names, values;
3561 3832
3562 labels.push_back(ASCIIToUTF16("First Name:")); 3833 labels.push_back(ASCIIToUTF16("First Name:"));
3563 names.push_back(ASCIIToUTF16("firstname")); 3834 names.push_back(ASCIIToUTF16("firstname"));
3564 values.push_back(ASCIIToUTF16("John")); 3835 values.push_back(ASCIIToUTF16("John"));
3565 3836
(...skipping 21 matching lines...) Expand all
3587 labels, names, values); 3858 labels, names, values);
3588 } 3859 }
3589 3860
3590 TEST_F(FormAutofillTest, ClickElement) { 3861 TEST_F(FormAutofillTest, ClickElement) {
3591 LoadHTML("<BUTTON id='link'>Button</BUTTON>" 3862 LoadHTML("<BUTTON id='link'>Button</BUTTON>"
3592 "<BUTTON name='button'>Button</BUTTON>"); 3863 "<BUTTON name='button'>Button</BUTTON>");
3593 WebFrame* frame = GetMainFrame(); 3864 WebFrame* frame = GetMainFrame();
3594 ASSERT_NE(nullptr, frame); 3865 ASSERT_NE(nullptr, frame);
3595 3866
3596 // Successful retrieval by id. 3867 // Successful retrieval by id.
3597 autofill::WebElementDescriptor clicker; 3868 WebElementDescriptor clicker;
3598 clicker.retrieval_method = autofill::WebElementDescriptor::ID; 3869 clicker.retrieval_method = WebElementDescriptor::ID;
3599 clicker.descriptor = "link"; 3870 clicker.descriptor = "link";
3600 EXPECT_TRUE(ClickElement(frame->document(), clicker)); 3871 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3601 3872
3602 // Successful retrieval by css selector. 3873 // Successful retrieval by css selector.
3603 clicker.retrieval_method = autofill::WebElementDescriptor::CSS_SELECTOR; 3874 clicker.retrieval_method = WebElementDescriptor::CSS_SELECTOR;
3604 clicker.descriptor = "button[name='button']"; 3875 clicker.descriptor = "button[name='button']";
3605 EXPECT_TRUE(ClickElement(frame->document(), clicker)); 3876 EXPECT_TRUE(ClickElement(frame->document(), clicker));
3606 3877
3607 // Unsuccessful retrieval due to invalid CSS selector. 3878 // Unsuccessful retrieval due to invalid CSS selector.
3608 clicker.descriptor = "^*&"; 3879 clicker.descriptor = "^*&";
3609 EXPECT_FALSE(ClickElement(frame->document(), clicker)); 3880 EXPECT_FALSE(ClickElement(frame->document(), clicker));
3610 3881
3611 // Unsuccessful retrieval because element does not exist. 3882 // Unsuccessful retrieval because element does not exist.
3612 clicker.descriptor = "#junk"; 3883 clicker.descriptor = "#junk";
3613 EXPECT_FALSE(ClickElement(frame->document(), clicker)); 3884 EXPECT_FALSE(ClickElement(frame->document(), clicker));
(...skipping 20 matching lines...) Expand all
3634 select_element.setValue(WebString::fromUTF8("AL")); 3905 select_element.setValue(WebString::fromUTF8("AL"));
3635 3906
3636 WebVector<WebFormElement> forms; 3907 WebVector<WebFormElement> forms;
3637 frame->document().forms(forms); 3908 frame->document().forms(forms);
3638 ASSERT_EQ(1U, forms.size()); 3909 ASSERT_EQ(1U, forms.size());
3639 3910
3640 FormData form; 3911 FormData form;
3641 3912
3642 // Extract the country select-one value as text. 3913 // Extract the country select-one value as text.
3643 EXPECT_TRUE(WebFormElementToFormData( 3914 EXPECT_TRUE(WebFormElementToFormData(
3644 forms[0], WebFormControlElement(), autofill::REQUIRE_NONE, 3915 forms[0], WebFormControlElement(), REQUIRE_NONE,
3645 static_cast<autofill::ExtractMask>( 3916 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTION_TEXT),
3646 autofill::EXTRACT_VALUE | autofill::EXTRACT_OPTION_TEXT), 3917 &form, nullptr));
3647 &form, NULL));
3648 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); 3918 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3649 EXPECT_EQ(GURL(frame->document().url()), form.origin); 3919 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3650 EXPECT_EQ(GURL("http://cnn.com"), form.action); 3920 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3651 3921
3652 const std::vector<FormFieldData>& fields = form.fields; 3922 const std::vector<FormFieldData>& fields = form.fields;
3653 ASSERT_EQ(3U, fields.size()); 3923 ASSERT_EQ(3U, fields.size());
3654 3924
3655 FormFieldData expected; 3925 FormFieldData expected;
3656 3926
3657 expected.name = ASCIIToUTF16("firstname"); 3927 expected.name = ASCIIToUTF16("firstname");
(...skipping 11 matching lines...) Expand all
3669 expected.name = ASCIIToUTF16("country"); 3939 expected.name = ASCIIToUTF16("country");
3670 expected.value = ASCIIToUTF16("Albania"); 3940 expected.value = ASCIIToUTF16("Albania");
3671 expected.form_control_type = "select-one"; 3941 expected.form_control_type = "select-one";
3672 expected.max_length = 0; 3942 expected.max_length = 0;
3673 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]); 3943 EXPECT_FORM_FIELD_DATA_EQUALS(expected, fields[2]);
3674 3944
3675 form.fields.clear(); 3945 form.fields.clear();
3676 // Extract the country select-one value as value. 3946 // Extract the country select-one value as value.
3677 EXPECT_TRUE(WebFormElementToFormData(forms[0], 3947 EXPECT_TRUE(WebFormElementToFormData(forms[0],
3678 WebFormControlElement(), 3948 WebFormControlElement(),
3679 autofill::REQUIRE_NONE, 3949 REQUIRE_NONE,
3680 autofill::EXTRACT_VALUE, 3950 EXTRACT_VALUE,
3681 &form, 3951 &form,
3682 NULL)); 3952 nullptr));
3683 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); 3953 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name);
3684 EXPECT_EQ(GURL(frame->document().url()), form.origin); 3954 EXPECT_EQ(GURL(frame->document().url()), form.origin);
3685 EXPECT_EQ(GURL("http://cnn.com"), form.action); 3955 EXPECT_EQ(GURL("http://cnn.com"), form.action);
3686 3956
3687 ASSERT_EQ(3U, fields.size()); 3957 ASSERT_EQ(3U, fields.size());
3688 3958
3689 expected.name = ASCIIToUTF16("firstname"); 3959 expected.name = ASCIIToUTF16("firstname");
3690 expected.value = ASCIIToUTF16("John"); 3960 expected.value = ASCIIToUTF16("John");
3691 expected.form_control_type = "text"; 3961 expected.form_control_type = "text";
3692 expected.max_length = WebInputElement::defaultMaxLength(); 3962 expected.max_length = WebInputElement::defaultMaxLength();
(...skipping 30 matching lines...) Expand all
3723 " </FIELDSET>" 3993 " </FIELDSET>"
3724 " <FIELDSET>" 3994 " <FIELDSET>"
3725 " <LABEL for='email'>Email:</LABEL>" 3995 " <LABEL for='email'>Email:</LABEL>"
3726 " <INPUT type='text' id='email' value='john@example.com'/>" 3996 " <INPUT type='text' id='email' value='john@example.com'/>"
3727 " </FIELDSET>" 3997 " </FIELDSET>"
3728 "</DIV>"); 3998 "</DIV>");
3729 3999
3730 WebFrame* frame = GetMainFrame(); 4000 WebFrame* frame = GetMainFrame();
3731 ASSERT_NE(nullptr, frame); 4001 ASSERT_NE(nullptr, frame);
3732 4002
3733 control_elements = FormCache::GetUnownedAutofillableFormFieldElements( 4003 control_elements = GetUnownedAutofillableFormFieldElements(
3734 frame->document().all(), &fieldsets); 4004 frame->document().all(), &fieldsets);
3735 ASSERT_EQ(3U, control_elements.size()); 4005 ASSERT_EQ(3U, control_elements.size());
3736 ASSERT_EQ(2U, fieldsets.size()); 4006 ASSERT_EQ(2U, fieldsets.size());
3737 4007
3738 FormData form; 4008 FormData form;
3739 EXPECT_TRUE(UnownedFormElementsAndFieldSetsToFormData( 4009 EXPECT_TRUE(UnownedFormElementsAndFieldSetsToFormData(
3740 fieldsets, control_elements, dummy_origin, extract_mask, &form)); 4010 fieldsets, control_elements, nullptr, dummy_origin, REQUIRE_NONE,
4011 extract_mask, &form, nullptr));
3741 4012
3742 EXPECT_TRUE(form.name.empty()); 4013 EXPECT_TRUE(form.name.empty());
3743 EXPECT_EQ(dummy_origin, form.origin); 4014 EXPECT_EQ(dummy_origin, form.origin);
3744 EXPECT_FALSE(form.action.is_valid()); 4015 EXPECT_FALSE(form.action.is_valid());
3745 4016
3746 const std::vector<FormFieldData>& fields = form.fields; 4017 const std::vector<FormFieldData>& fields = form.fields;
3747 ASSERT_EQ(3U, fields.size()); 4018 ASSERT_EQ(3U, fields.size());
3748 4019
3749 FormFieldData expected; 4020 FormFieldData expected;
3750 expected.form_control_type = "text"; 4021 expected.form_control_type = "text";
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
3782 " <INPUT type='text' id='firstname' value='John'/>" 4053 " <INPUT type='text' id='firstname' value='John'/>"
3783 " <INPUT type='text' id='lastname' value='Smith'/>" 4054 " <INPUT type='text' id='lastname' value='Smith'/>"
3784 " <LABEL for='email'>Email:</LABEL>" 4055 " <LABEL for='email'>Email:</LABEL>"
3785 " </FIELDSET>" 4056 " </FIELDSET>"
3786 " <INPUT type='text' id='email' value='john@example.com'/>" 4057 " <INPUT type='text' id='email' value='john@example.com'/>"
3787 "</DIV>"); 4058 "</DIV>");
3788 4059
3789 WebFrame* frame = GetMainFrame(); 4060 WebFrame* frame = GetMainFrame();
3790 ASSERT_NE(nullptr, frame); 4061 ASSERT_NE(nullptr, frame);
3791 4062
3792 control_elements = FormCache::GetUnownedAutofillableFormFieldElements( 4063 control_elements = GetUnownedAutofillableFormFieldElements(
3793 frame->document().all(), &fieldsets); 4064 frame->document().all(), &fieldsets);
3794 ASSERT_EQ(3U, control_elements.size()); 4065 ASSERT_EQ(3U, control_elements.size());
3795 ASSERT_EQ(1U, fieldsets.size()); 4066 ASSERT_EQ(1U, fieldsets.size());
3796 4067
3797 FormData form; 4068 FormData form;
3798 EXPECT_TRUE(UnownedFormElementsAndFieldSetsToFormData( 4069 EXPECT_TRUE(UnownedFormElementsAndFieldSetsToFormData(
3799 fieldsets, control_elements, dummy_origin, extract_mask, &form)); 4070 fieldsets, control_elements, nullptr, dummy_origin, REQUIRE_NONE,
4071 extract_mask, &form, nullptr));
3800 4072
3801 EXPECT_TRUE(form.name.empty()); 4073 EXPECT_TRUE(form.name.empty());
3802 EXPECT_EQ(dummy_origin, form.origin); 4074 EXPECT_EQ(dummy_origin, form.origin);
3803 EXPECT_FALSE(form.action.is_valid()); 4075 EXPECT_FALSE(form.action.is_valid());
3804 4076
3805 const std::vector<FormFieldData>& fields = form.fields; 4077 const std::vector<FormFieldData>& fields = form.fields;
3806 ASSERT_EQ(3U, fields.size()); 4078 ASSERT_EQ(3U, fields.size());
3807 4079
3808 FormFieldData expected; 4080 FormFieldData expected;
3809 expected.form_control_type = "text"; 4081 expected.form_control_type = "text";
(...skipping 21 matching lines...) Expand all
3831 4103
3832 const ExtractMask extract_mask = 4104 const ExtractMask extract_mask =
3833 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS); 4105 static_cast<ExtractMask>(EXTRACT_VALUE | EXTRACT_OPTIONS);
3834 const GURL dummy_origin("http://www.example.com"); 4106 const GURL dummy_origin("http://www.example.com");
3835 4107
3836 LoadHTML(kFormHtml); 4108 LoadHTML(kFormHtml);
3837 4109
3838 WebFrame* frame = GetMainFrame(); 4110 WebFrame* frame = GetMainFrame();
3839 ASSERT_NE(nullptr, frame); 4111 ASSERT_NE(nullptr, frame);
3840 4112
3841 control_elements = FormCache::GetUnownedAutofillableFormFieldElements( 4113 control_elements = GetUnownedAutofillableFormFieldElements(
3842 frame->document().all(), &fieldsets); 4114 frame->document().all(), &fieldsets);
3843 ASSERT_EQ(0U, control_elements.size()); 4115 ASSERT_TRUE(control_elements.empty());
3844 ASSERT_EQ(0U, fieldsets.size()); 4116 ASSERT_TRUE(fieldsets.empty());
3845 4117
3846 FormData form; 4118 FormData form;
3847 EXPECT_FALSE(UnownedFormElementsAndFieldSetsToFormData( 4119 EXPECT_FALSE(UnownedFormElementsAndFieldSetsToFormData(
3848 fieldsets, control_elements, dummy_origin, extract_mask, &form)); 4120 fieldsets, control_elements, nullptr, dummy_origin, REQUIRE_NONE,
4121 extract_mask, &form, nullptr));
3849 } 4122 }
3850 4123
3851 } // namespace autofill 4124 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698