| Index: chrome/browser/prefs/pref_notifier_unittest.cc
|
| diff --git a/chrome/browser/prefs/pref_notifier_unittest.cc b/chrome/browser/prefs/pref_notifier_unittest.cc
|
| index 6792adae68396a990c3929ba989e297c39f1917a..55b0d0550c935d929d6d5ab37c4c8812e3951f05 100644
|
| --- a/chrome/browser/prefs/pref_notifier_unittest.cc
|
| +++ b/chrome/browser/prefs/pref_notifier_unittest.cc
|
| @@ -6,12 +6,17 @@
|
| #include "chrome/browser/prefs/pref_notifier.h"
|
| #include "chrome/browser/prefs/pref_service.h"
|
| #include "chrome/browser/prefs/pref_value_store.h"
|
| -#include "chrome/common/notification_observer.h"
|
| -#include "chrome/common/notification_type.h"
|
| +#include "chrome/common/notification_observer_mock.h"
|
| #include "chrome/common/notification_service.h"
|
| +#include "chrome/test/testing_pref_service.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| +using testing::_;
|
| +using testing::Field;
|
| +using testing::Invoke;
|
| +using testing::Mock;
|
| +using testing::Truly;
|
|
|
| namespace {
|
|
|
| @@ -26,16 +31,12 @@ bool DetailsAreChangedPref(const Details<std::string>& details) {
|
| // Test PrefNotifier that allows tracking of observers and notifications.
|
| class MockPrefNotifier : public PrefNotifier {
|
| public:
|
| - MockPrefNotifier(PrefService* prefs, PrefValueStore* value_store)
|
| - : PrefNotifier(prefs, value_store) {}
|
| + MockPrefNotifier(PrefService* prefs)
|
| + : PrefNotifier(prefs) {}
|
|
|
| virtual ~MockPrefNotifier() {}
|
|
|
| - MOCK_METHOD1(FireObservers, void(const char* path));
|
| -
|
| - void RealFireObservers(const char* path) {
|
| - PrefNotifier::FireObservers(path);
|
| - }
|
| + MOCK_METHOD1(FireObservers, void(const std::string& path));
|
|
|
| size_t CountObserver(const char* path, NotificationObserver* obs) {
|
| PrefObserverMap::const_iterator observer_iterator =
|
| @@ -56,239 +57,158 @@ class MockPrefNotifier : public PrefNotifier {
|
| }
|
| };
|
|
|
| -// Mock PrefValueStore that has no unnecessary PrefStores and injects a simpler
|
| -// PrefHasChanged().
|
| -class MockPrefValueStore : public PrefValueStore {
|
| - public:
|
| - MockPrefValueStore()
|
| - : PrefValueStore(NULL, NULL, NULL, NULL, NULL, NULL,
|
| - new DefaultPrefStore(), NULL) {}
|
| -
|
| - virtual ~MockPrefValueStore() {}
|
| -
|
| - // This mock version returns true if the pref path starts with "changed".
|
| - virtual bool PrefHasChanged(const char* path,
|
| - PrefNotifier::PrefStoreType new_store) {
|
| - std::string path_str(path);
|
| - if (path_str.compare(0, 7, "changed") == 0)
|
| - return true;
|
| - return false;
|
| - }
|
| -};
|
| -
|
| -// Mock PrefService that allows the PrefNotifier to be injected.
|
| -class MockPrefService : public PrefService {
|
| - public:
|
| - explicit MockPrefService(PrefValueStore* pref_value_store)
|
| - : PrefService(pref_value_store) {}
|
| -
|
| - void SetPrefNotifier(PrefNotifier* notifier) {
|
| - pref_notifier_.reset(notifier);
|
| - }
|
| -};
|
| -
|
| -// Mock PrefObserver that verifies notifications.
|
| -class MockPrefObserver : public NotificationObserver {
|
| - public:
|
| - virtual ~MockPrefObserver() {}
|
| -
|
| - MOCK_METHOD3(Observe, void(NotificationType type,
|
| - const NotificationSource& source,
|
| - const NotificationDetails& details));
|
| -};
|
| -
|
| // Test fixture class.
|
| class PrefNotifierTest : public testing::Test {
|
| protected:
|
| virtual void SetUp() {
|
| - value_store_ = new MockPrefValueStore;
|
| - pref_service_.reset(new MockPrefService(value_store_));
|
| - notifier_ = new MockPrefNotifier(pref_service_.get(), value_store_);
|
| - pref_service_->SetPrefNotifier(notifier_);
|
| -
|
| - pref_service_->RegisterBooleanPref(kChangedPref, true);
|
| - pref_service_->RegisterBooleanPref(kUnchangedPref, true);
|
| + pref_service_.RegisterBooleanPref(kChangedPref, true);
|
| + pref_service_.RegisterBooleanPref(kUnchangedPref, true);
|
| }
|
|
|
| - // The PrefService takes ownership of the PrefValueStore and PrefNotifier.
|
| - PrefValueStore* value_store_;
|
| - MockPrefNotifier* notifier_;
|
| - scoped_ptr<MockPrefService> pref_service_;
|
| + TestingPrefService pref_service_;
|
|
|
| - MockPrefObserver obs1_;
|
| - MockPrefObserver obs2_;
|
| + NotificationObserverMock obs1_;
|
| + NotificationObserverMock obs2_;
|
| };
|
|
|
| -TEST_F(PrefNotifierTest, OnPreferenceSet) {
|
| - EXPECT_CALL(*notifier_, FireObservers(kChangedPref))
|
| - .Times(PrefNotifier::PREF_STORE_TYPE_MAX + 1);
|
| - EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0);
|
| -
|
| - for (size_t i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) {
|
| - notifier_->OnPreferenceSet(kChangedPref,
|
| - static_cast<PrefNotifier::PrefStoreType>(i));
|
| - notifier_->OnPreferenceSet(kUnchangedPref,
|
| - static_cast<PrefNotifier::PrefStoreType>(i));
|
| - }
|
| +TEST_F(PrefNotifierTest, OnPreferenceChanged) {
|
| + MockPrefNotifier notifier(&pref_service_);
|
| + EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1);
|
| + notifier.OnPreferenceChanged(kChangedPref);
|
| }
|
|
|
| -TEST_F(PrefNotifierTest, OnUserPreferenceSet) {
|
| - EXPECT_CALL(*notifier_, FireObservers(kChangedPref));
|
| - EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0);
|
| - notifier_->OnUserPreferenceSet(kChangedPref);
|
| - notifier_->OnUserPreferenceSet(kUnchangedPref);
|
| +TEST_F(PrefNotifierTest, OnInitializationCompleted) {
|
| + MockPrefNotifier notifier(&pref_service_);
|
| + NotificationObserverMock observer;
|
| + NotificationRegistrar registrar;
|
| + registrar.Add(&observer, NotificationType::PREF_INITIALIZATION_COMPLETED,
|
| + Source<PrefService>(&pref_service_));
|
| + EXPECT_CALL(observer, Observe(
|
| + Field(&NotificationType::value,
|
| + NotificationType::PREF_INITIALIZATION_COMPLETED),
|
| + Source<PrefService>(&pref_service_),
|
| + NotificationService::NoDetails()));
|
| + notifier.OnInitializationCompleted();
|
| }
|
|
|
| TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) {
|
| const char pref_name[] = "homepage";
|
| const char pref_name2[] = "proxy";
|
|
|
| - notifier_->AddPrefObserver(pref_name, &obs1_);
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
|
| + MockPrefNotifier notifier(&pref_service_);
|
| + notifier.AddPrefObserver(pref_name, &obs1_);
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
|
|
|
| // Re-adding the same observer for the same pref doesn't change anything.
|
| // Skip this in debug mode, since it hits a DCHECK and death tests aren't
|
| // thread-safe.
|
| #if defined(NDEBUG)
|
| - notifier_->AddPrefObserver(pref_name, &obs1_);
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
|
| + notifier.AddPrefObserver(pref_name, &obs1_);
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
|
| #endif // NDEBUG
|
|
|
| // Ensure that we can add the same observer to a different pref.
|
| - notifier_->AddPrefObserver(pref_name2, &obs1_);
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_));
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
|
| + notifier.AddPrefObserver(pref_name2, &obs1_);
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
|
|
|
| // Ensure that we can add another observer to the same pref.
|
| - notifier_->AddPrefObserver(pref_name, &obs2_);
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_));
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs2_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
|
| + notifier.AddPrefObserver(pref_name, &obs2_);
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
|
|
|
| // Ensure that we can remove all observers, and that removing a non-existent
|
| // observer is harmless.
|
| - notifier_->RemovePrefObserver(pref_name, &obs1_);
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_));
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs2_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
|
| -
|
| - notifier_->RemovePrefObserver(pref_name, &obs2_);
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_));
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
|
| -
|
| - notifier_->RemovePrefObserver(pref_name, &obs1_);
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_));
|
| - ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
|
| -
|
| - notifier_->RemovePrefObserver(pref_name2, &obs1_);
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
|
| - ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
|
| + notifier.RemovePrefObserver(pref_name, &obs1_);
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
|
| +
|
| + notifier.RemovePrefObserver(pref_name, &obs2_);
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
|
| +
|
| + notifier.RemovePrefObserver(pref_name, &obs1_);
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
|
| + ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
|
| +
|
| + notifier.RemovePrefObserver(pref_name2, &obs1_);
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
|
| + ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
|
| }
|
|
|
| TEST_F(PrefNotifierTest, FireObservers) {
|
| - using testing::_;
|
| - using testing::Field;
|
| - using testing::Invoke;
|
| - using testing::Mock;
|
| - using testing::Truly;
|
| -
|
| - // Tell googlemock to pass calls to the mock's "back door" too.
|
| - ON_CALL(*notifier_, FireObservers(_))
|
| - .WillByDefault(Invoke(notifier_, &MockPrefNotifier::RealFireObservers));
|
| - EXPECT_CALL(*notifier_, FireObservers(kChangedPref)).Times(4);
|
| - EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0);
|
| -
|
| - notifier_->AddPrefObserver(kChangedPref, &obs1_);
|
| - notifier_->AddPrefObserver(kUnchangedPref, &obs1_);
|
| + PrefNotifier notifier(&pref_service_);
|
| + notifier.AddPrefObserver(kChangedPref, &obs1_);
|
| + notifier.AddPrefObserver(kUnchangedPref, &obs1_);
|
|
|
| EXPECT_CALL(obs1_, Observe(
|
| Field(&NotificationType::value, NotificationType::PREF_CHANGED),
|
| - Source<PrefService>(pref_service_.get()),
|
| + Source<PrefService>(&pref_service_),
|
| Truly(DetailsAreChangedPref)));
|
| EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
|
| - notifier_->OnUserPreferenceSet(kChangedPref);
|
| - Mock::VerifyAndClearExpectations(&obs1_);
|
| - Mock::VerifyAndClearExpectations(&obs2_);
|
| -
|
| - EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
|
| - EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
|
| - notifier_->OnUserPreferenceSet(kUnchangedPref);
|
| + notifier.OnPreferenceChanged(kChangedPref);
|
| Mock::VerifyAndClearExpectations(&obs1_);
|
| Mock::VerifyAndClearExpectations(&obs2_);
|
|
|
| - notifier_->AddPrefObserver(kChangedPref, &obs2_);
|
| - notifier_->AddPrefObserver(kUnchangedPref, &obs2_);
|
| + notifier.AddPrefObserver(kChangedPref, &obs2_);
|
| + notifier.AddPrefObserver(kUnchangedPref, &obs2_);
|
|
|
| EXPECT_CALL(obs1_, Observe(
|
| Field(&NotificationType::value, NotificationType::PREF_CHANGED),
|
| - Source<PrefService>(pref_service_.get()),
|
| + Source<PrefService>(&pref_service_),
|
| Truly(DetailsAreChangedPref)));
|
| EXPECT_CALL(obs2_, Observe(
|
| Field(&NotificationType::value, NotificationType::PREF_CHANGED),
|
| - Source<PrefService>(pref_service_.get()),
|
| + Source<PrefService>(&pref_service_),
|
| Truly(DetailsAreChangedPref)));
|
| - notifier_->OnUserPreferenceSet(kChangedPref);
|
| - Mock::VerifyAndClearExpectations(&obs1_);
|
| - Mock::VerifyAndClearExpectations(&obs2_);
|
| -
|
| - EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
|
| - EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
|
| - notifier_->OnUserPreferenceSet(kUnchangedPref);
|
| + notifier.OnPreferenceChanged(kChangedPref);
|
| Mock::VerifyAndClearExpectations(&obs1_);
|
| Mock::VerifyAndClearExpectations(&obs2_);
|
|
|
| // Make sure removing an observer from one pref doesn't affect anything else.
|
| - notifier_->RemovePrefObserver(kChangedPref, &obs1_);
|
| + notifier.RemovePrefObserver(kChangedPref, &obs1_);
|
|
|
| EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
|
| EXPECT_CALL(obs2_, Observe(
|
| Field(&NotificationType::value, NotificationType::PREF_CHANGED),
|
| - Source<PrefService>(pref_service_.get()),
|
| + Source<PrefService>(&pref_service_),
|
| Truly(DetailsAreChangedPref)));
|
| - notifier_->OnUserPreferenceSet(kChangedPref);
|
| - Mock::VerifyAndClearExpectations(&obs1_);
|
| - Mock::VerifyAndClearExpectations(&obs2_);
|
| -
|
| - EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
|
| - EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
|
| - notifier_->OnUserPreferenceSet(kUnchangedPref);
|
| + notifier.OnPreferenceChanged(kChangedPref);
|
| Mock::VerifyAndClearExpectations(&obs1_);
|
| Mock::VerifyAndClearExpectations(&obs2_);
|
|
|
| // Make sure removing an observer entirely doesn't affect anything else.
|
| - notifier_->RemovePrefObserver(kUnchangedPref, &obs1_);
|
| + notifier.RemovePrefObserver(kUnchangedPref, &obs1_);
|
|
|
| EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
|
| EXPECT_CALL(obs2_, Observe(
|
| Field(&NotificationType::value, NotificationType::PREF_CHANGED),
|
| - Source<PrefService>(pref_service_.get()),
|
| + Source<PrefService>(&pref_service_),
|
| Truly(DetailsAreChangedPref)));
|
| - notifier_->OnUserPreferenceSet(kChangedPref);
|
| + notifier.OnPreferenceChanged(kChangedPref);
|
| Mock::VerifyAndClearExpectations(&obs1_);
|
| Mock::VerifyAndClearExpectations(&obs2_);
|
|
|
| - EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
|
| - EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
|
| - notifier_->OnUserPreferenceSet(kUnchangedPref);
|
| -
|
| - notifier_->RemovePrefObserver(kChangedPref, &obs2_);
|
| - notifier_->RemovePrefObserver(kUnchangedPref, &obs2_);
|
| + notifier.RemovePrefObserver(kChangedPref, &obs2_);
|
| + notifier.RemovePrefObserver(kUnchangedPref, &obs2_);
|
| }
|
|
|
| } // namespace
|
|
|