| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "base/stl_util-inl.h" | 7 #include "base/stl_util-inl.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 case AutofillChange::REMOVE: { | 42 case AutofillChange::REMOVE: { |
| 43 os << "REMOVE"; | 43 os << "REMOVE"; |
| 44 break; | 44 break; |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 return os << " " << change.key(); | 47 return os << " " << change.key(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 class WebDatabaseTest : public testing::Test { | 50 class WebDatabaseTest : public testing::Test { |
| 51 protected: | 51 protected: |
| 52 | 52 typedef std::vector<AutofillChange> AutofillChangeList; |
| 53 virtual void SetUp() { | 53 virtual void SetUp() { |
| 54 PathService::Get(chrome::DIR_TEST_DATA, &file_); | 54 PathService::Get(chrome::DIR_TEST_DATA, &file_); |
| 55 const std::string test_db = "TestWebDatabase" + | 55 const std::string test_db = "TestWebDatabase" + |
| 56 Int64ToString(base::Time::Now().ToInternalValue()) + | 56 Int64ToString(base::Time::Now().ToInternalValue()) + |
| 57 ".db"; | 57 ".db"; |
| 58 file_ = file_.AppendASCII(test_db); | 58 file_ = file_.AppendASCII(test_db); |
| 59 file_util::Delete(file_, false); | 59 file_util::Delete(file_, false); |
| 60 } | 60 } |
| 61 | 61 |
| 62 virtual void TearDown() { | 62 virtual void TearDown() { |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 | 410 |
| 411 TEST_F(WebDatabaseTest, Autofill) { | 411 TEST_F(WebDatabaseTest, Autofill) { |
| 412 WebDatabase db; | 412 WebDatabase db; |
| 413 | 413 |
| 414 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); | 414 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); |
| 415 | 415 |
| 416 Time t1 = Time::Now(); | 416 Time t1 = Time::Now(); |
| 417 | 417 |
| 418 // Simulate the submission of a handful of entries in a field called "Name", | 418 // Simulate the submission of a handful of entries in a field called "Name", |
| 419 // some more often than others. | 419 // some more often than others. |
| 420 AutofillChangeList changes; |
| 420 EXPECT_TRUE(db.AddFormFieldValue( | 421 EXPECT_TRUE(db.AddFormFieldValue( |
| 421 FormField(string16(), | 422 FormField(string16(), |
| 422 ASCIIToUTF16("Name"), | 423 ASCIIToUTF16("Name"), |
| 423 string16(), | 424 string16(), |
| 424 ASCIIToUTF16("Superman")))); | 425 ASCIIToUTF16("Superman")), |
| 426 &changes)); |
| 425 std::vector<string16> v; | 427 std::vector<string16> v; |
| 426 for (int i = 0; i < 5; i++) { | 428 for (int i = 0; i < 5; i++) { |
| 427 EXPECT_TRUE(db.AddFormFieldValue( | 429 EXPECT_TRUE(db.AddFormFieldValue( |
| 428 FormField(string16(), | 430 FormField(string16(), |
| 429 ASCIIToUTF16("Name"), | 431 ASCIIToUTF16("Name"), |
| 430 string16(), | 432 string16(), |
| 431 ASCIIToUTF16("Clark Kent")))); | 433 ASCIIToUTF16("Clark Kent")), |
| 434 &changes)); |
| 432 } | 435 } |
| 433 for (int i = 0; i < 3; i++) { | 436 for (int i = 0; i < 3; i++) { |
| 434 EXPECT_TRUE(db.AddFormFieldValue( | 437 EXPECT_TRUE(db.AddFormFieldValue( |
| 435 FormField(string16(), | 438 FormField(string16(), |
| 436 ASCIIToUTF16("Name"), | 439 ASCIIToUTF16("Name"), |
| 437 string16(), | 440 string16(), |
| 438 ASCIIToUTF16("Clark Sutter")))); | 441 ASCIIToUTF16("Clark Sutter")), |
| 442 &changes)); |
| 439 } | 443 } |
| 440 for (int i = 0; i < 2; i++) { | 444 for (int i = 0; i < 2; i++) { |
| 441 EXPECT_TRUE(db.AddFormFieldValue( | 445 EXPECT_TRUE(db.AddFormFieldValue( |
| 442 FormField(string16(), | 446 FormField(string16(), |
| 443 ASCIIToUTF16("Favorite Color"), | 447 ASCIIToUTF16("Favorite Color"), |
| 444 string16(), | 448 string16(), |
| 445 ASCIIToUTF16("Green")))); | 449 ASCIIToUTF16("Green")), |
| 450 &changes)); |
| 446 } | 451 } |
| 447 | 452 |
| 448 int count = 0; | 453 int count = 0; |
| 449 int64 pair_id = 0; | 454 int64 pair_id = 0; |
| 450 | 455 |
| 451 // We have added the name Clark Kent 5 times, so count should be 5 and pair_id | 456 // We have added the name Clark Kent 5 times, so count should be 5 and pair_id |
| 452 // should be somthing non-zero. | 457 // should be somthing non-zero. |
| 453 EXPECT_TRUE(db.GetIDAndCountOfFormElement( | 458 EXPECT_TRUE(db.GetIDAndCountOfFormElement( |
| 454 FormField(string16(), | 459 FormField(string16(), |
| 455 ASCIIToUTF16("Name"), | 460 ASCIIToUTF16("Name"), |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 EXPECT_TRUE(db.GetFormValuesForElementName( | 509 EXPECT_TRUE(db.GetFormValuesForElementName( |
| 505 ASCIIToUTF16("Name"), ASCIIToUTF16("cLa"), &v, 6)); | 510 ASCIIToUTF16("Name"), ASCIIToUTF16("cLa"), &v, 6)); |
| 506 EXPECT_EQ(2U, v.size()); | 511 EXPECT_EQ(2U, v.size()); |
| 507 if (v.size() == 2) { | 512 if (v.size() == 2) { |
| 508 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]); | 513 EXPECT_EQ(ASCIIToUTF16("Clark Kent"), v[0]); |
| 509 EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]); | 514 EXPECT_EQ(ASCIIToUTF16("Clark Sutter"), v[1]); |
| 510 } | 515 } |
| 511 | 516 |
| 512 // Removing all elements since the beginning of this function should remove | 517 // Removing all elements since the beginning of this function should remove |
| 513 // everything from the database. | 518 // everything from the database. |
| 514 std::vector<AutofillChange> changes; | 519 changes.clear(); |
| 515 EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t1, Time(), &changes)); | 520 EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t1, Time(), &changes)); |
| 516 | 521 |
| 517 const AutofillChange expected_changes[] = { | 522 const AutofillChange expected_changes[] = { |
| 518 AutofillChange(AutofillChange::REMOVE, | 523 AutofillChange(AutofillChange::REMOVE, |
| 519 AutofillKey(ASCIIToUTF16("Name"), | 524 AutofillKey(ASCIIToUTF16("Name"), |
| 520 ASCIIToUTF16("Superman"))), | 525 ASCIIToUTF16("Superman"))), |
| 521 AutofillChange(AutofillChange::REMOVE, | 526 AutofillChange(AutofillChange::REMOVE, |
| 522 AutofillKey(ASCIIToUTF16("Name"), | 527 AutofillKey(ASCIIToUTF16("Name"), |
| 523 ASCIIToUTF16("Clark Kent"))), | 528 ASCIIToUTF16("Clark Kent"))), |
| 524 AutofillChange(AutofillChange::REMOVE, | 529 AutofillChange(AutofillChange::REMOVE, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 543 | 548 |
| 544 EXPECT_TRUE( | 549 EXPECT_TRUE( |
| 545 db.GetFormValuesForElementName(ASCIIToUTF16("Name"), string16(), &v, 6)); | 550 db.GetFormValuesForElementName(ASCIIToUTF16("Name"), string16(), &v, 6)); |
| 546 EXPECT_EQ(0U, v.size()); | 551 EXPECT_EQ(0U, v.size()); |
| 547 | 552 |
| 548 // Now add some values with empty strings. | 553 // Now add some values with empty strings. |
| 549 const string16 kValue = ASCIIToUTF16(" toto "); | 554 const string16 kValue = ASCIIToUTF16(" toto "); |
| 550 EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(), | 555 EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(), |
| 551 ASCIIToUTF16("blank"), | 556 ASCIIToUTF16("blank"), |
| 552 string16(), | 557 string16(), |
| 553 string16()))); | 558 string16()), |
| 559 &changes)); |
| 554 EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(), | 560 EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(), |
| 555 ASCIIToUTF16("blank"), | 561 ASCIIToUTF16("blank"), |
| 556 string16(), | 562 string16(), |
| 557 ASCIIToUTF16(" ")))); | 563 ASCIIToUTF16(" ")), |
| 564 &changes)); |
| 558 EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(), | 565 EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(), |
| 559 ASCIIToUTF16("blank"), | 566 ASCIIToUTF16("blank"), |
| 560 string16(), | 567 string16(), |
| 561 ASCIIToUTF16(" ")))); | 568 ASCIIToUTF16(" ")), |
| 569 &changes)); |
| 562 EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(), | 570 EXPECT_TRUE(db.AddFormFieldValue(FormField(string16(), |
| 563 ASCIIToUTF16("blank"), | 571 ASCIIToUTF16("blank"), |
| 564 string16(), | 572 string16(), |
| 565 kValue))); | 573 kValue), |
| 574 &changes)); |
| 566 | 575 |
| 567 // They should be stored normally as the DB layer does not check for empty | 576 // They should be stored normally as the DB layer does not check for empty |
| 568 // values. | 577 // values. |
| 569 v.clear(); | 578 v.clear(); |
| 570 EXPECT_TRUE(db.GetFormValuesForElementName( | 579 EXPECT_TRUE(db.GetFormValuesForElementName( |
| 571 ASCIIToUTF16("blank"), string16(), &v, 10)); | 580 ASCIIToUTF16("blank"), string16(), &v, 10)); |
| 572 EXPECT_EQ(4U, v.size()); | 581 EXPECT_EQ(4U, v.size()); |
| 573 | 582 |
| 574 // Now we'll check that ClearAutofillEmptyValueElements() works as expected. | 583 // Now we'll check that ClearAutofillEmptyValueElements() works as expected. |
| 575 db.ClearAutofillEmptyValueElements(); | 584 db.ClearAutofillEmptyValueElements(); |
| 576 | 585 |
| 577 v.clear(); | 586 v.clear(); |
| 578 EXPECT_TRUE(db.GetFormValuesForElementName(ASCIIToUTF16("blank"), | 587 EXPECT_TRUE(db.GetFormValuesForElementName(ASCIIToUTF16("blank"), |
| 579 string16(), &v, 10)); | 588 string16(), &v, 10)); |
| 580 ASSERT_EQ(1U, v.size()); | 589 ASSERT_EQ(1U, v.size()); |
| 581 | 590 |
| 582 EXPECT_EQ(kValue, v[0]); | 591 EXPECT_EQ(kValue, v[0]); |
| 583 } | 592 } |
| 584 | 593 |
| 585 TEST_F(WebDatabaseTest, Autofill_RemoveBetweenChanges) { | 594 TEST_F(WebDatabaseTest, Autofill_RemoveBetweenChanges) { |
| 586 WebDatabase db; | 595 WebDatabase db; |
| 587 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); | 596 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); |
| 588 | 597 |
| 589 TimeDelta one_day(TimeDelta::FromDays(1)); | 598 TimeDelta one_day(TimeDelta::FromDays(1)); |
| 590 Time t1 = Time::Now(); | 599 Time t1 = Time::Now(); |
| 591 Time t2 = t1 + one_day; | 600 Time t2 = t1 + one_day; |
| 592 | 601 |
| 602 AutofillChangeList changes; |
| 593 EXPECT_TRUE(db.AddFormFieldValueTime( | 603 EXPECT_TRUE(db.AddFormFieldValueTime( |
| 594 FormField(string16(), | 604 FormField(string16(), |
| 595 ASCIIToUTF16("Name"), | 605 ASCIIToUTF16("Name"), |
| 596 string16(), | 606 string16(), |
| 597 ASCIIToUTF16("Superman")), | 607 ASCIIToUTF16("Superman")), |
| 608 &changes, |
| 598 t1)); | 609 t1)); |
| 599 EXPECT_TRUE(db.AddFormFieldValueTime( | 610 EXPECT_TRUE(db.AddFormFieldValueTime( |
| 600 FormField(string16(), | 611 FormField(string16(), |
| 601 ASCIIToUTF16("Name"), | 612 ASCIIToUTF16("Name"), |
| 602 string16(), | 613 string16(), |
| 603 ASCIIToUTF16("Superman")), | 614 ASCIIToUTF16("Superman")), |
| 615 &changes, |
| 604 t2)); | 616 t2)); |
| 605 | 617 |
| 606 std::vector<AutofillChange> changes; | 618 changes.clear(); |
| 607 EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t1, t2, &changes)); | 619 EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t1, t2, &changes)); |
| 608 ASSERT_EQ(1U, changes.size()); | 620 ASSERT_EQ(1U, changes.size()); |
| 609 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, | 621 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, |
| 610 AutofillKey(ASCIIToUTF16("Name"), | 622 AutofillKey(ASCIIToUTF16("Name"), |
| 611 ASCIIToUTF16("Superman"))), | 623 ASCIIToUTF16("Superman"))), |
| 612 changes[0]); | 624 changes[0]); |
| 613 changes.clear(); | 625 changes.clear(); |
| 614 | 626 |
| 615 EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t2, t2 + one_day, &changes)); | 627 EXPECT_TRUE(db.RemoveFormElementsAddedBetween(t2, t2 + one_day, &changes)); |
| 616 ASSERT_EQ(1U, changes.size()); | 628 ASSERT_EQ(1U, changes.size()); |
| 617 EXPECT_EQ(AutofillChange(AutofillChange::REMOVE, | 629 EXPECT_EQ(AutofillChange(AutofillChange::REMOVE, |
| 618 AutofillKey(ASCIIToUTF16("Name"), | 630 AutofillKey(ASCIIToUTF16("Name"), |
| 619 ASCIIToUTF16("Superman"))), | 631 ASCIIToUTF16("Superman"))), |
| 620 changes[0]); | 632 changes[0]); |
| 621 } | 633 } |
| 622 | 634 |
| 635 TEST_F(WebDatabaseTest, Autofill_AddChanges) { |
| 636 WebDatabase db; |
| 637 ASSERT_EQ(sql::INIT_OK, db.Init(file_)); |
| 638 |
| 639 TimeDelta one_day(TimeDelta::FromDays(1)); |
| 640 Time t1 = Time::Now(); |
| 641 Time t2 = t1 + one_day; |
| 642 |
| 643 AutofillChangeList changes; |
| 644 EXPECT_TRUE(db.AddFormFieldValueTime( |
| 645 FormField(string16(), |
| 646 ASCIIToUTF16("Name"), |
| 647 string16(), |
| 648 ASCIIToUTF16("Superman")), |
| 649 &changes, |
| 650 t1)); |
| 651 ASSERT_EQ(1U, changes.size()); |
| 652 EXPECT_EQ(AutofillChange(AutofillChange::ADD, |
| 653 AutofillKey(ASCIIToUTF16("Name"), |
| 654 ASCIIToUTF16("Superman"))), |
| 655 changes[0]); |
| 656 |
| 657 changes.clear(); |
| 658 EXPECT_TRUE(db.AddFormFieldValueTime( |
| 659 FormField(string16(), |
| 660 ASCIIToUTF16("Name"), |
| 661 string16(), |
| 662 ASCIIToUTF16("Superman")), |
| 663 &changes, |
| 664 t2)); |
| 665 ASSERT_EQ(1U, changes.size()); |
| 666 EXPECT_EQ(AutofillChange(AutofillChange::UPDATE, |
| 667 AutofillKey(ASCIIToUTF16("Name"), |
| 668 ASCIIToUTF16("Superman"))), |
| 669 changes[0]); |
| 670 } |
| 671 |
| 623 static bool AddTimestampedLogin(WebDatabase* db, std::string url, | 672 static bool AddTimestampedLogin(WebDatabase* db, std::string url, |
| 624 const std::string& unique_string, | 673 const std::string& unique_string, |
| 625 const Time& time) { | 674 const Time& time) { |
| 626 // Example password form. | 675 // Example password form. |
| 627 PasswordForm form; | 676 PasswordForm form; |
| 628 form.origin = GURL(url + std::string("/LoginAuth")); | 677 form.origin = GURL(url + std::string("/LoginAuth")); |
| 629 form.username_element = ASCIIToUTF16(unique_string); | 678 form.username_element = ASCIIToUTF16(unique_string); |
| 630 form.username_value = ASCIIToUTF16(unique_string); | 679 form.username_value = ASCIIToUTF16(unique_string); |
| 631 form.password_element = ASCIIToUTF16(unique_string); | 680 form.password_element = ASCIIToUTF16(unique_string); |
| 632 form.submit_element = ASCIIToUTF16("signIn"); | 681 form.submit_element = ASCIIToUTF16("signIn"); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 807 ASSERT_EQ(16, images[0].height()); | 856 ASSERT_EQ(16, images[0].height()); |
| 808 ASSERT_EQ(32, images[1].width()); | 857 ASSERT_EQ(32, images[1].width()); |
| 809 ASSERT_EQ(32, images[1].height()); | 858 ASSERT_EQ(32, images[1].height()); |
| 810 } else { | 859 } else { |
| 811 ASSERT_EQ(32, images[0].width()); | 860 ASSERT_EQ(32, images[0].width()); |
| 812 ASSERT_EQ(32, images[0].height()); | 861 ASSERT_EQ(32, images[0].height()); |
| 813 ASSERT_EQ(16, images[1].width()); | 862 ASSERT_EQ(16, images[1].width()); |
| 814 ASSERT_EQ(16, images[1].height()); | 863 ASSERT_EQ(16, images[1].height()); |
| 815 } | 864 } |
| 816 } | 865 } |
| OLD | NEW |