OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/renderer/form_manager.h" | 5 #include "chrome/renderer/form_manager.h" |
6 #include "chrome/test/render_view_test.h" | 6 #include "chrome/test/render_view_test.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" | 8 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" |
9 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" | 9 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" |
10 #include "third_party/WebKit/WebKit/chromium/public/WebFormElement.h" | 10 #include "third_party/WebKit/WebKit/chromium/public/WebFormElement.h" |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 " <INPUT type=\"submit\" name=\"submit\" value=\"Send\"/>" | 251 " <INPUT type=\"submit\" name=\"submit\" value=\"Send\"/>" |
252 "</FORM>"); | 252 "</FORM>"); |
253 | 253 |
254 WebFrame* web_frame = GetMainFrame(); | 254 WebFrame* web_frame = GetMainFrame(); |
255 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame); | 255 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame); |
256 | 256 |
257 FormManager form_manager; | 257 FormManager form_manager; |
258 form_manager.ExtractForms(web_frame); | 258 form_manager.ExtractForms(web_frame); |
259 | 259 |
260 std::vector<FormData> forms; | 260 std::vector<FormData> forms; |
261 form_manager.GetForms(FormManager::REQUIRE_ELEMENTS_ENABLED, &forms); | 261 form_manager.GetForms(FormManager::REQUIRE_ENABLED, &forms); |
262 ASSERT_EQ(1U, forms.size()); | 262 ASSERT_EQ(1U, forms.size()); |
263 | 263 |
264 const FormData& form = forms[0]; | 264 const FormData& form = forms[0]; |
265 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); | 265 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); |
266 EXPECT_EQ(GURL(web_frame->url()), form.origin); | 266 EXPECT_EQ(GURL(web_frame->url()), form.origin); |
267 EXPECT_EQ(GURL("http://xyz.com"), form.action); | 267 EXPECT_EQ(GURL("http://xyz.com"), form.action); |
268 | 268 |
269 const std::vector<FormField>& fields = form.fields; | 269 const std::vector<FormField>& fields = form.fields; |
270 ASSERT_EQ(2U, fields.size()); | 270 ASSERT_EQ(2U, fields.size()); |
271 EXPECT_EQ(FormField(string16(), | 271 EXPECT_EQ(FormField(string16(), |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 form.fields[2].set_value(ASCIIToUTF16("Alpha")); | 419 form.fields[2].set_value(ASCIIToUTF16("Alpha")); |
420 form.fields[3].set_value(ASCIIToUTF16("Beta")); | 420 form.fields[3].set_value(ASCIIToUTF16("Beta")); |
421 form.fields[4].set_value(ASCIIToUTF16("Gamma")); | 421 form.fields[4].set_value(ASCIIToUTF16("Gamma")); |
422 form.fields[5].set_value(ASCIIToUTF16("Delta")); | 422 form.fields[5].set_value(ASCIIToUTF16("Delta")); |
423 EXPECT_TRUE(form_manager.FillForm(form)); | 423 EXPECT_TRUE(form_manager.FillForm(form)); |
424 | 424 |
425 // Verify the filled elements. | 425 // Verify the filled elements. |
426 WebDocument document = web_frame->document(); | 426 WebDocument document = web_frame->document(); |
427 WebInputElement firstname = | 427 WebInputElement firstname = |
428 document.getElementById("firstname").to<WebInputElement>(); | 428 document.getElementById("firstname").to<WebInputElement>(); |
429 // TODO(jhawkins): Check firstname.isAutofilled() once support has been added | 429 EXPECT_TRUE(firstname.isAutofilled()); |
430 // in WebKit. | |
431 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value()); | 430 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.value()); |
432 | 431 |
433 WebInputElement lastname = | 432 WebInputElement lastname = |
434 document.getElementById("lastname").to<WebInputElement>(); | 433 document.getElementById("lastname").to<WebInputElement>(); |
| 434 EXPECT_TRUE(lastname.isAutofilled()); |
435 EXPECT_EQ(ASCIIToUTF16("Earp"), lastname.value()); | 435 EXPECT_EQ(ASCIIToUTF16("Earp"), lastname.value()); |
436 | 436 |
437 // Hidden fields are not filled. | 437 // Hidden fields are not filled. |
438 WebInputElement imhidden = | 438 WebInputElement imhidden = |
439 document.getElementById("imhidden").to<WebInputElement>(); | 439 document.getElementById("imhidden").to<WebInputElement>(); |
| 440 EXPECT_FALSE(imhidden.isAutofilled()); |
440 EXPECT_TRUE(imhidden.value().isEmpty()); | 441 EXPECT_TRUE(imhidden.value().isEmpty()); |
441 | 442 |
442 // Non-empty fields are not filled. | 443 // Non-empty fields are not filled. |
443 WebInputElement notempty = | 444 WebInputElement notempty = |
444 document.getElementById("notempty").to<WebInputElement>(); | 445 document.getElementById("notempty").to<WebInputElement>(); |
| 446 EXPECT_FALSE(notempty.isAutofilled()); |
445 EXPECT_EQ(ASCIIToUTF16("Hi"), notempty.value()); | 447 EXPECT_EQ(ASCIIToUTF16("Hi"), notempty.value()); |
446 | 448 |
447 // autocomplete=off fields are not filled. | 449 // autocomplete=off fields are not filled. |
448 WebInputElement noautocomplete = | 450 WebInputElement noautocomplete = |
449 document.getElementById("noautocomplete").to<WebInputElement>(); | 451 document.getElementById("noautocomplete").to<WebInputElement>(); |
| 452 EXPECT_FALSE(noautocomplete.isAutofilled()); |
450 EXPECT_TRUE(noautocomplete.value().isEmpty()); | 453 EXPECT_TRUE(noautocomplete.value().isEmpty()); |
451 | 454 |
452 // Disabled fields are not filled. | 455 // Disabled fields are not filled. |
453 WebInputElement notenabled = | 456 WebInputElement notenabled = |
454 document.getElementById("notenabled").to<WebInputElement>(); | 457 document.getElementById("notenabled").to<WebInputElement>(); |
| 458 EXPECT_FALSE(notenabled.isAutofilled()); |
455 EXPECT_TRUE(notenabled.value().isEmpty()); | 459 EXPECT_TRUE(notenabled.value().isEmpty()); |
456 } | 460 } |
457 | 461 |
| 462 TEST_F(FormManagerTest, PreviewForm) { |
| 463 LoadHTML("<FORM name=\"TestForm\" action=\"http://buh.com\" method=\"post\">" |
| 464 " <INPUT type=\"text\" id=\"firstname\"/>" |
| 465 " <INPUT type=\"text\" id=\"lastname\"/>" |
| 466 " <INPUT type=\"hidden\" id=\"imhidden\"/>" |
| 467 " <INPUT type=\"text\" id=\"notempty\" value=\"Hi\"/>" |
| 468 " <INPUT type=\"text\" autocomplete=\"off\" id=\"noautocomplete\"/>" |
| 469 " <INPUT type=\"text\" disabled=\"disabled\" id=\"notenabled\"/>" |
| 470 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" |
| 471 "</FORM>"); |
| 472 |
| 473 WebFrame* web_frame = GetMainFrame(); |
| 474 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame); |
| 475 |
| 476 FormManager form_manager; |
| 477 form_manager.ExtractForms(web_frame); |
| 478 |
| 479 // Verify that we have the form. |
| 480 std::vector<FormData> forms; |
| 481 form_manager.GetForms(FormManager::REQUIRE_NONE, &forms); |
| 482 ASSERT_EQ(1U, forms.size()); |
| 483 |
| 484 // Get the input element we want to find. |
| 485 WebElement element = web_frame->document().getElementById("firstname"); |
| 486 WebInputElement input_element = element.to<WebInputElement>(); |
| 487 |
| 488 // Find the form that contains the input element. |
| 489 FormData form; |
| 490 EXPECT_TRUE(form_manager.FindFormWithFormControlElement( |
| 491 input_element, FormManager::REQUIRE_NONE, &form)); |
| 492 EXPECT_EQ(ASCIIToUTF16("TestForm"), form.name); |
| 493 EXPECT_EQ(GURL(web_frame->url()), form.origin); |
| 494 EXPECT_EQ(GURL("http://buh.com"), form.action); |
| 495 |
| 496 const std::vector<FormField>& fields = form.fields; |
| 497 ASSERT_EQ(7U, fields.size()); |
| 498 EXPECT_EQ(FormField(string16(), |
| 499 ASCIIToUTF16("firstname"), |
| 500 string16(), |
| 501 ASCIIToUTF16("text"), |
| 502 20), |
| 503 fields[0]); |
| 504 EXPECT_EQ(FormField(string16(), |
| 505 ASCIIToUTF16("lastname"), |
| 506 string16(), |
| 507 ASCIIToUTF16("text"), |
| 508 20), |
| 509 fields[1]); |
| 510 EXPECT_EQ(FormField(string16(), |
| 511 ASCIIToUTF16("imhidden"), |
| 512 string16(), |
| 513 ASCIIToUTF16("hidden"), |
| 514 0), |
| 515 fields[2]); |
| 516 EXPECT_EQ(FormField(string16(), |
| 517 ASCIIToUTF16("notempty"), |
| 518 ASCIIToUTF16("Hi"), |
| 519 ASCIIToUTF16("text"), |
| 520 20), |
| 521 fields[3]); |
| 522 EXPECT_EQ(FormField(string16(), |
| 523 ASCIIToUTF16("noautocomplete"), |
| 524 string16(), |
| 525 ASCIIToUTF16("text"), |
| 526 20), |
| 527 fields[4]); |
| 528 EXPECT_EQ(FormField(string16(), |
| 529 ASCIIToUTF16("notenabled"), |
| 530 string16(), |
| 531 ASCIIToUTF16("text"), |
| 532 20), |
| 533 fields[5]); |
| 534 EXPECT_EQ(FormField(string16(), |
| 535 ASCIIToUTF16("reply-send"), |
| 536 ASCIIToUTF16("Send"), |
| 537 ASCIIToUTF16("submit"), |
| 538 0), |
| 539 fields[6]); |
| 540 |
| 541 // Preview the form. |
| 542 form.fields[0].set_value(ASCIIToUTF16("Wyatt")); |
| 543 form.fields[1].set_value(ASCIIToUTF16("Earp")); |
| 544 form.fields[2].set_value(ASCIIToUTF16("Alpha")); |
| 545 form.fields[3].set_value(ASCIIToUTF16("Beta")); |
| 546 form.fields[4].set_value(ASCIIToUTF16("Gamma")); |
| 547 form.fields[5].set_value(ASCIIToUTF16("Delta")); |
| 548 EXPECT_TRUE(form_manager.PreviewForm(form)); |
| 549 |
| 550 // Verify the previewed elements. |
| 551 WebDocument document = web_frame->document(); |
| 552 WebInputElement firstname = |
| 553 document.getElementById("firstname").to<WebInputElement>(); |
| 554 EXPECT_TRUE(firstname.isAutofilled()); |
| 555 EXPECT_EQ(ASCIIToUTF16("Wyatt"), firstname.placeholder()); |
| 556 |
| 557 WebInputElement lastname = |
| 558 document.getElementById("lastname").to<WebInputElement>(); |
| 559 EXPECT_TRUE(lastname.isAutofilled()); |
| 560 EXPECT_EQ(ASCIIToUTF16("Earp"), lastname.placeholder()); |
| 561 |
| 562 // Hidden fields are not previewed. |
| 563 WebInputElement imhidden = |
| 564 document.getElementById("imhidden").to<WebInputElement>(); |
| 565 EXPECT_FALSE(imhidden.isAutofilled()); |
| 566 EXPECT_TRUE(imhidden.placeholder().isEmpty()); |
| 567 |
| 568 // Non-empty fields are not previewed. |
| 569 WebInputElement notempty = |
| 570 document.getElementById("notempty").to<WebInputElement>(); |
| 571 EXPECT_FALSE(notempty.isAutofilled()); |
| 572 EXPECT_TRUE(notempty.placeholder().isEmpty()); |
| 573 |
| 574 // autocomplete=off fields are not previewed. |
| 575 WebInputElement noautocomplete = |
| 576 document.getElementById("noautocomplete").to<WebInputElement>(); |
| 577 EXPECT_FALSE(noautocomplete.isAutofilled()); |
| 578 EXPECT_TRUE(noautocomplete.placeholder().isEmpty()); |
| 579 |
| 580 // Disabled fields are not previewed. |
| 581 WebInputElement notenabled = |
| 582 document.getElementById("notenabled").to<WebInputElement>(); |
| 583 EXPECT_FALSE(notenabled.isAutofilled()); |
| 584 EXPECT_TRUE(notenabled.placeholder().isEmpty()); |
| 585 } |
| 586 |
458 TEST_F(FormManagerTest, Reset) { | 587 TEST_F(FormManagerTest, Reset) { |
459 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">" | 588 LoadHTML("<FORM name=\"TestForm\" action=\"http://cnn.com\" method=\"post\">" |
460 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>" | 589 " <INPUT type=\"text\" id=\"firstname\" value=\"John\"/>" |
461 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>" | 590 " <INPUT type=\"text\" id=\"lastname\" value=\"Smith\"/>" |
462 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" | 591 " <INPUT type=\"submit\" name=\"reply-send\" value=\"Send\"/>" |
463 "</FORM>"); | 592 "</FORM>"); |
464 | 593 |
465 WebFrame* web_frame = GetMainFrame(); | 594 WebFrame* web_frame = GetMainFrame(); |
466 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame); | 595 ASSERT_NE(static_cast<WebFrame*>(NULL), web_frame); |
467 | 596 |
(...skipping 1467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1935 fields[5]); | 2064 fields[5]); |
1936 EXPECT_EQ(FormField(string16(), | 2065 EXPECT_EQ(FormField(string16(), |
1937 ASCIIToUTF16("reply-send"), | 2066 ASCIIToUTF16("reply-send"), |
1938 ASCIIToUTF16("Send"), | 2067 ASCIIToUTF16("Send"), |
1939 ASCIIToUTF16("submit"), | 2068 ASCIIToUTF16("submit"), |
1940 0), | 2069 0), |
1941 fields[6]); | 2070 fields[6]); |
1942 } | 2071 } |
1943 | 2072 |
1944 } // namespace | 2073 } // namespace |
OLD | NEW |