diff options
author | tim@chromium.org <tim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-27 00:46:05 +0000 |
---|---|---|
committer | tim@chromium.org <tim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-27 00:46:05 +0000 |
commit | 11fb1b2734baf77c7eb02afcf464b1e04fc3f360 (patch) | |
tree | c89ce2fbcebf5a3dc48883a04a93d94d4b09dc8f /chrome/browser/webdata | |
parent | 3bf4d53ecb0a07e8639b4403997cddb6869ebc61 (diff) | |
download | chromium_src-11fb1b2734baf77c7eb02afcf464b1e04fc3f360.zip chromium_src-11fb1b2734baf77c7eb02afcf464b1e04fc3f360.tar.gz chromium_src-11fb1b2734baf77c7eb02afcf464b1e04fc3f360.tar.bz2 |
Hook autofill++ WebDatabase changes up to the NotificationService.
TEST=WebDataServiceAutofillTest
Review URL: http://codereview.chromium.org/1385002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42881 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/webdata')
-rw-r--r-- | chrome/browser/webdata/autofill_change.cc | 9 | ||||
-rw-r--r-- | chrome/browser/webdata/autofill_change.h | 62 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service.cc | 42 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service_unittest.cc | 177 |
4 files changed, 261 insertions, 29 deletions
diff --git a/chrome/browser/webdata/autofill_change.cc b/chrome/browser/webdata/autofill_change.cc deleted file mode 100644 index ceb8205..0000000 --- a/chrome/browser/webdata/autofill_change.cc +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/webdata/autofill_change.h" - -bool AutofillChange::operator==(const AutofillChange& change) const { - return type_ == change.type() && key_ == change.key(); -} diff --git a/chrome/browser/webdata/autofill_change.h b/chrome/browser/webdata/autofill_change.h index ea16846..2b7be32 100644 --- a/chrome/browser/webdata/autofill_change.h +++ b/chrome/browser/webdata/autofill_change.h @@ -5,9 +5,17 @@ #ifndef CHROME_BROWSER_WEBDATA_AUTOFILL_CHANGE_H__ #define CHROME_BROWSER_WEBDATA_AUTOFILL_CHANGE_H__ +#include "chrome/browser/autofill/autofill_profile.h" +#include "chrome/browser/autofill/credit_card.h" #include "chrome/browser/webdata/autofill_entry.h" -class AutofillChange { +class AutoFillProfile; +class CreditCard; + +// For classic Autofill form fields, the KeyType is AutofillKey. +// Autofill++ types such as AutoFillProfile and CreditCard simply use an int. +template <typename KeyType> +class GenericAutofillChange { public: typedef enum { ADD, @@ -15,19 +23,57 @@ class AutofillChange { REMOVE } Type; - AutofillChange(Type type, const AutofillKey& key) + virtual ~GenericAutofillChange() {} + + Type type() const { return type_; } + const KeyType& key() const { return key_; } + + protected: + GenericAutofillChange(Type type, const KeyType& key) : type_(type), key_(key) {} - virtual ~AutofillChange() {} + private: + Type type_; + KeyType key_; +}; - Type type() const { return type_; } - const AutofillKey& key() const { return key_; } +class AutofillChange : public GenericAutofillChange<AutofillKey> { + public: + AutofillChange(Type t, const AutofillKey& k) + : GenericAutofillChange<AutofillKey>(t, k) {} + bool operator==(const AutofillChange& change) const { + return type() == change.type() && key() == change.key(); + } +}; - bool operator==(const AutofillChange& change) const; +class AutofillProfileChange : public GenericAutofillChange<int> { + public: + // If t == REMOVE, |p| should be NULL. + AutofillProfileChange(Type t, int k, const AutoFillProfile* p) + : GenericAutofillChange<int>(t, k), profile_(p) {} + const AutoFillProfile* profile() const { return profile_; } + bool operator==(const AutofillProfileChange& change) const { + return type() == change.type() && key() == change.key() && + (type() != REMOVE) ? *profile() == *change.profile() : true; + } private: - Type type_; - AutofillKey key_; + const AutoFillProfile* profile_; // Unowned pointer, can be NULL. +}; + +class AutofillCreditCardChange : public GenericAutofillChange<int> { + public: + // If t == REMOVE, |card| should be NULL. + AutofillCreditCardChange(Type t, int k, const CreditCard* card) + : GenericAutofillChange<int>(t, k), credit_card_(card) {} + + const CreditCard* credit_card() const { return credit_card_; } + bool operator==(const AutofillCreditCardChange& change) const { + return type() == change.type() && key() == change.key() && + (type() != REMOVE) ? *credit_card() == *change.credit_card() : true; + } + private: + const CreditCard* credit_card_; // Unowned pointer, can be NULL. }; #endif // CHROME_BROWSER_WEBDATA_AUTOFILL_CHANGE_H__ diff --git a/chrome/browser/webdata/web_data_service.cc b/chrome/browser/webdata/web_data_service.cc index 68c330c..8811e10 100644 --- a/chrome/browser/webdata/web_data_service.cc +++ b/chrome/browser/webdata/web_data_service.cc @@ -787,6 +787,13 @@ void WebDataService::AddAutoFillProfileImpl( if (!db_->AddAutoFillProfile(profile)) NOTREACHED(); ScheduleCommit(); + + AutofillProfileChange change(AutofillProfileChange::ADD, + profile.unique_id(), &profile); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_PROFILE_CHANGED, + NotificationService::AllSources(), + Details<AutofillProfileChange>(&change)); } request->RequestComplete(); } @@ -799,6 +806,13 @@ void WebDataService::UpdateAutoFillProfileImpl( if (!db_->UpdateAutoFillProfile(profile)) NOTREACHED(); ScheduleCommit(); + + AutofillProfileChange change(AutofillProfileChange::UPDATE, + profile.unique_id(), &profile); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_PROFILE_CHANGED, + NotificationService::AllSources(), + Details<AutofillProfileChange>(&change)); } request->RequestComplete(); } @@ -811,6 +825,13 @@ void WebDataService::RemoveAutoFillProfileImpl( if (!db_->RemoveAutoFillProfile(profile_id)) NOTREACHED(); ScheduleCommit(); + + AutofillProfileChange change(AutofillProfileChange::REMOVE, + profile_id, NULL); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_PROFILE_CHANGED, + NotificationService::AllSources(), + Details<AutofillProfileChange>(&change)); } request->RequestComplete(); } @@ -835,6 +856,13 @@ void WebDataService::AddCreditCardImpl( if (!db_->AddCreditCard(creditcard)) NOTREACHED(); ScheduleCommit(); + + AutofillCreditCardChange change(AutofillCreditCardChange::ADD, + creditcard.unique_id(), &creditcard); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, + NotificationService::AllSources(), + Details<AutofillCreditCardChange>(&change)); } request->RequestComplete(); } @@ -847,6 +875,13 @@ void WebDataService::UpdateCreditCardImpl( if (!db_->UpdateCreditCard(creditcard)) NOTREACHED(); ScheduleCommit(); + + AutofillCreditCardChange change(AutofillCreditCardChange::UPDATE, + creditcard.unique_id(), &creditcard); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, + NotificationService::AllSources(), + Details<AutofillCreditCardChange>(&change)); } request->RequestComplete(); } @@ -859,6 +894,13 @@ void WebDataService::RemoveCreditCardImpl( if (!db_->RemoveCreditCard(creditcard_id)) NOTREACHED(); ScheduleCommit(); + + AutofillCreditCardChange change(AutofillCreditCardChange::REMOVE, + creditcard_id, NULL); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, + NotificationService::AllSources(), + Details<AutofillCreditCardChange>(&change)); } request->RequestComplete(); } diff --git a/chrome/browser/webdata/web_data_service_unittest.cc b/chrome/browser/webdata/web_data_service_unittest.cc index 6965d01..1797138 100644 --- a/chrome/browser/webdata/web_data_service_unittest.cc +++ b/chrome/browser/webdata/web_data_service_unittest.cc @@ -14,6 +14,8 @@ #include "base/string_util.h" #include "base/time.h" #include "base/waitable_event.h" +#include "chrome/browser/autofill/autofill_profile.h" +#include "chrome/browser/autofill/credit_card.h" #include "chrome/browser/chrome_thread.h" #include "chrome/browser/webdata/autofill_change.h" #include "chrome/browser/webdata/autofill_entry.h" @@ -33,12 +35,15 @@ using base::Time; using base::TimeDelta; using base::WaitableEvent; using testing::_; +using testing::DoDefault; using testing::ElementsAreArray; using testing::Pointee; using testing::Property; typedef std::vector<AutofillChange> AutofillChangeList; +static const int kWebDataServiceTimeoutSeconds = 8; + ACTION_P(SignalEvent, event) { event->Signal(); } @@ -90,9 +95,7 @@ class DBThreadObserverHelper : virtual ~DBThreadObserverHelper() { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::DB)); - registrar_.Remove(&observer_, - NotificationType::AUTOFILL_ENTRIES_CHANGED, - NotificationService::AllSources()); + registrar_.RemoveAll(); } NotificationObserverMock* observer() { @@ -106,6 +109,12 @@ class DBThreadObserverHelper : registrar_.Add(&observer_, NotificationType::AUTOFILL_ENTRIES_CHANGED, NotificationService::AllSources()); + registrar_.Add(&observer_, + NotificationType::AUTOFILL_PROFILE_CHANGED, + NotificationService::AllSources()); + registrar_.Add(&observer_, + NotificationType::AUTOFILL_CREDIT_CARD_CHANGED, + NotificationService::AllSources()); done_event_.Signal(); } @@ -153,7 +162,11 @@ class WebDataServiceTest : public testing::Test { class WebDataServiceAutofillTest : public WebDataServiceTest { public: WebDataServiceAutofillTest() - : WebDataServiceTest(), done_event_(true, false) {} + : WebDataServiceTest(), + unique_id1_(1), + unique_id2_(2), + test_timeout_(TimeDelta::FromSeconds(kWebDataServiceTimeoutSeconds)), + done_event_(false, false) {} protected: virtual void SetUp() { @@ -187,11 +200,13 @@ class WebDataServiceAutofillTest : public WebDataServiceTest { string16 name2_; string16 value1_; string16 value2_; + int unique_id1_, unique_id2_; + const TimeDelta test_timeout_; scoped_refptr<DBThreadObserverHelper> observer_helper_; WaitableEvent done_event_; }; -TEST_F(WebDataServiceAutofillTest, Add) { +TEST_F(WebDataServiceAutofillTest, FormFillAdd) { const AutofillChange expected_changes[] = { AutofillChange(AutofillChange::ADD, AutofillKey(name1_, value1_)), AutofillChange(AutofillChange::ADD, AutofillKey(name2_, value2_)) @@ -213,7 +228,7 @@ TEST_F(WebDataServiceAutofillTest, Add) { wds_->AddFormFieldValues(form_fields); // The event will be signaled when the mock observer is notified. - done_event_.Wait(); + done_event_.TimedWait(test_timeout_); AutofillWebDataServiceConsumer consumer; WebDataService::Handle handle; @@ -229,7 +244,7 @@ TEST_F(WebDataServiceAutofillTest, Add) { EXPECT_EQ(value1_, consumer.values()[0]); } -TEST_F(WebDataServiceAutofillTest, RemoveOne) { +TEST_F(WebDataServiceAutofillTest, FormFillRemoveOne) { // First add some values to autofill. EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). WillOnce(SignalEvent(&done_event_)); @@ -238,7 +253,7 @@ TEST_F(WebDataServiceAutofillTest, RemoveOne) { wds_->AddFormFieldValues(form_fields); // The event will be signaled when the mock observer is notified. - done_event_.Wait(); + done_event_.TimedWait(test_timeout_); // This will verify that the correct notification is triggered, // passing the correct list of autofill keys in the details. @@ -255,10 +270,10 @@ TEST_F(WebDataServiceAutofillTest, RemoveOne) { wds_->RemoveFormValueForElementName(name1_, value1_); // The event will be signaled when the mock observer is notified. - done_event_.Wait(); + done_event_.TimedWait(test_timeout_); } -TEST_F(WebDataServiceAutofillTest,RemoveMany) { +TEST_F(WebDataServiceAutofillTest,FormFillRemoveMany) { TimeDelta one_day(TimeDelta::FromDays(1)); Time t = Time::Now(); @@ -270,7 +285,7 @@ TEST_F(WebDataServiceAutofillTest,RemoveMany) { wds_->AddFormFieldValues(form_fields); // The event will be signaled when the mock observer is notified. - done_event_.Wait(); + done_event_.TimedWait(test_timeout_); // This will verify that the correct notification is triggered, // passing the correct list of autofill keys in the details. @@ -288,5 +303,143 @@ TEST_F(WebDataServiceAutofillTest,RemoveMany) { wds_->RemoveFormElementsAddedBetween(t, t + one_day); // The event will be signaled when the mock observer is notified. - done_event_.Wait(); + done_event_.TimedWait(test_timeout_); +} + +TEST_F(WebDataServiceAutofillTest, ProfileAdd) { + AutoFillProfile profile(name1_, unique_id1_); + const AutofillProfileChange expected_change( + AutofillProfileChange::ADD, unique_id1_, &profile); + + EXPECT_CALL( + *observer_helper_->observer(), + Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED), + NotificationService::AllSources(), + Property(&Details<const AutofillProfileChange>::ptr, + Pointee(expected_change)))). + WillOnce(SignalEvent(&done_event_)); + + wds_->AddAutoFillProfile(profile); + done_event_.TimedWait(test_timeout_); } + +TEST_F(WebDataServiceAutofillTest, ProfileRemove) { + AutoFillProfile profile(name1_, unique_id1_); + + EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). + WillOnce(SignalEvent(&done_event_)); + wds_->AddAutoFillProfile(profile); + done_event_.TimedWait(test_timeout_); + + const AutofillProfileChange expected_change( + AutofillProfileChange::REMOVE, unique_id1_, NULL); + EXPECT_CALL( + *observer_helper_->observer(), + Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED), + NotificationService::AllSources(), + Property(&Details<const AutofillProfileChange>::ptr, + Pointee(expected_change)))). + WillOnce(SignalEvent(&done_event_)); + + wds_->RemoveAutoFillProfile(profile.unique_id()); + done_event_.TimedWait(test_timeout_); +} + +TEST_F(WebDataServiceAutofillTest, ProfileUpdate) { + AutoFillProfile profile1(name1_, unique_id1_); + AutoFillProfile profile2(name2_, unique_id2_); + + EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). + Times(2). + WillOnce(DoDefault()). + WillOnce(SignalEvent(&done_event_)); + wds_->AddAutoFillProfile(profile1); + wds_->AddAutoFillProfile(profile2); + + done_event_.TimedWait(test_timeout_); + + AutoFillProfile profile1_delta(profile1); + profile1_delta.set_label(ASCIIToUTF16("new_label!")); + const AutofillProfileChange expected_change( + AutofillProfileChange::UPDATE, unique_id1_, &profile1_delta); + + EXPECT_CALL( + *observer_helper_->observer(), + Observe(NotificationType(NotificationType::AUTOFILL_PROFILE_CHANGED), + NotificationService::AllSources(), + Property(&Details<const AutofillProfileChange>::ptr, + Pointee(expected_change)))). + WillOnce(SignalEvent(&done_event_)); + + wds_->UpdateAutoFillProfile(profile1_delta); + done_event_.TimedWait(test_timeout_); +} + +TEST_F(WebDataServiceAutofillTest, CreditAdd) { + CreditCard card(name1_, unique_id1_); + const AutofillCreditCardChange expected_change( + AutofillCreditCardChange::ADD, unique_id1_, &card); + + EXPECT_CALL( + *observer_helper_->observer(), + Observe(NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED), + NotificationService::AllSources(), + Property(&Details<const AutofillCreditCardChange>::ptr, + Pointee(expected_change)))). + WillOnce(SignalEvent(&done_event_)); + + wds_->AddCreditCard(card); + done_event_.TimedWait(test_timeout_); +} + +TEST_F(WebDataServiceAutofillTest, CreditRemove) { + CreditCard card(name1_, unique_id1_); + EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). + WillOnce(SignalEvent(&done_event_)); + wds_->AddCreditCard(card); + done_event_.TimedWait(test_timeout_); + + const AutofillCreditCardChange expected_change( + AutofillCreditCardChange::REMOVE, unique_id1_, NULL); + + EXPECT_CALL( + *observer_helper_->observer(), + Observe(NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED), + NotificationService::AllSources(), + Property(&Details<const AutofillCreditCardChange>::ptr, + Pointee(expected_change)))). + WillOnce(SignalEvent(&done_event_)); + + wds_->RemoveCreditCard(card.unique_id()); + done_event_.TimedWait(test_timeout_); +} + +TEST_F(WebDataServiceAutofillTest, CreditUpdate) { + CreditCard card1(name1_, unique_id1_); + CreditCard card2(name2_, unique_id2_); + + EXPECT_CALL(*observer_helper_->observer(), Observe(_, _, _)). + Times(2). + WillOnce(DoDefault()). + WillOnce(SignalEvent(&done_event_)); + wds_->AddCreditCard(card1); + wds_->AddCreditCard(card2); + done_event_.TimedWait(test_timeout_); + + CreditCard card1_delta(card1); + card1_delta.set_label(ASCIIToUTF16("new_label!")); + const AutofillCreditCardChange expected_change( + AutofillCreditCardChange::UPDATE, unique_id1_, &card1_delta); + + EXPECT_CALL( + *observer_helper_->observer(), + Observe(NotificationType(NotificationType::AUTOFILL_CREDIT_CARD_CHANGED), + NotificationService::AllSources(), + Property(&Details<const AutofillCreditCardChange>::ptr, + Pointee(expected_change)))). + WillOnce(SignalEvent(&done_event_)); + + wds_->UpdateCreditCard(card1_delta); + done_event_.TimedWait(test_timeout_); +} + |