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

Unified Diff: chrome/browser/prefs/pref_notifier_impl_unittest.cc

Issue 11345008: Remove content::NotificationObserver dependency from most Prefs code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/prefs/pref_notifier_impl_unittest.cc
diff --git a/chrome/browser/prefs/pref_notifier_impl_unittest.cc b/chrome/browser/prefs/pref_notifier_impl_unittest.cc
index 6a5388ea27317484d6b0a2a3b7e2dfafd0c1ebcd..063e721a7d9be998bfcf25754bd4aae2f6de5857 100644
--- a/chrome/browser/prefs/pref_notifier_impl_unittest.cc
+++ b/chrome/browser/prefs/pref_notifier_impl_unittest.cc
@@ -2,16 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/prefs/public/pref_init_observer.h"
+#include "base/prefs/public/pref_observer.h"
#include "chrome/browser/prefs/pref_notifier_impl.h"
#include "chrome/browser/prefs/pref_observer_mock.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/pref_value_store.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/test/base/testing_pref_service.h"
-#include "content/public/browser/notification_details.h"
-#include "content/public/browser/notification_registrar.h"
-#include "content/public/browser/notification_source.h"
-#include "content/public/test/mock_notification_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -26,7 +24,22 @@ namespace {
const char kChangedPref[] = "changed_pref";
const char kUnchangedPref[] = "unchanged_pref";
-// Test PrefNotifier that allows tracking of observers and notifications.
+class MockPrefInitObserver : public PrefInitObserver {
+ public:
+ MOCK_METHOD2(OnInitializationCompleted, void(PrefServiceBase*, bool));
+};
+
+// This is an unmodified PrefNotifierImpl, except we make
+// OnPreferenceChanged public for tests.
+class TestingPrefNotifierImpl : public PrefNotifierImpl {
+ public:
+ TestingPrefNotifierImpl(PrefService* service) : PrefNotifierImpl(service) {}
+
+ // Make public for tests.
+ using PrefNotifierImpl::OnPreferenceChanged;
+};
+
+// Mock PrefNotifier that allows tracking of observers and notifications.
class MockPrefNotifier : public PrefNotifierImpl {
public:
explicit MockPrefNotifier(PrefService* pref_service)
@@ -35,15 +48,15 @@ class MockPrefNotifier : public PrefNotifierImpl {
MOCK_METHOD1(FireObservers, void(const std::string& path));
- size_t CountObserver(const char* path, content::NotificationObserver* obs) {
+ size_t CountObserver(const char* path, PrefObserver* obs) {
PrefObserverMap::const_iterator observer_iterator =
pref_observers()->find(path);
if (observer_iterator == pref_observers()->end())
return false;
- NotificationObserverList* observer_list = observer_iterator->second;
- NotificationObserverList::Iterator it(*observer_list);
- content::NotificationObserver* existing_obs;
+ PrefObserverList* observer_list = observer_iterator->second;
+ PrefObserverList::Iterator it(*observer_list);
+ PrefObserver* existing_obs;
size_t count = 0;
while ((existing_obs = it.GetNext()) != NULL) {
if (existing_obs == obs)
@@ -52,6 +65,10 @@ class MockPrefNotifier : public PrefNotifierImpl {
return count;
}
+
+ // Make public for tests below.
+ using PrefNotifierImpl::OnPreferenceChanged;
+ using PrefNotifierImpl::OnInitializationCompleted;
};
// Test fixture class.
@@ -80,15 +97,14 @@ TEST_F(PrefNotifierTest, OnPreferenceChanged) {
TEST_F(PrefNotifierTest, OnInitializationCompleted) {
MockPrefNotifier notifier(&pref_service_);
- content::MockNotificationObserver observer;
- content::NotificationRegistrar registrar;
- registrar.Add(&observer, chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED,
- content::Source<PrefService>(&pref_service_));
- EXPECT_CALL(observer, Observe(
- int(chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED),
- content::Source<PrefService>(&pref_service_),
- Property(&content::Details<bool>::ptr, testing::Pointee(true))));
+ MockPrefInitObserver observer;
+ notifier.AddInitObserver(&observer);
Mattias Nissler (ping if slow) 2012/10/30 13:37:07 The init observer would also be better served thro
+ EXPECT_CALL(observer,
+ OnInitializationCompleted(
+ &pref_service_,
+ true));
notifier.OnInitializationCompleted(true);
+ notifier.RemoveInitObserver(&observer);
Mattias Nissler (ping if slow) 2012/10/30 13:37:07 So this shouldn't be necessary.
}
TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) {
@@ -155,13 +171,12 @@ TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) {
}
TEST_F(PrefNotifierTest, FireObservers) {
- base::FundamentalValue value_true(true);
- PrefNotifierImpl notifier(&pref_service_);
+ TestingPrefNotifierImpl notifier(&pref_service_);
notifier.AddPrefObserver(kChangedPref, &obs1_);
notifier.AddPrefObserver(kUnchangedPref, &obs1_);
- obs1_.Expect(&pref_service_, kChangedPref, &value_true);
- EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
+ EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref));
+ EXPECT_CALL(obs2_, OnPreferenceChanged(_, _)).Times(0);
notifier.OnPreferenceChanged(kChangedPref);
Mock::VerifyAndClearExpectations(&obs1_);
Mock::VerifyAndClearExpectations(&obs2_);
@@ -169,8 +184,8 @@ TEST_F(PrefNotifierTest, FireObservers) {
notifier.AddPrefObserver(kChangedPref, &obs2_);
notifier.AddPrefObserver(kUnchangedPref, &obs2_);
- obs1_.Expect(&pref_service_, kChangedPref, &value_true);
- obs2_.Expect(&pref_service_, kChangedPref, &value_true);
+ EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref));
+ EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
notifier.OnPreferenceChanged(kChangedPref);
Mock::VerifyAndClearExpectations(&obs1_);
Mock::VerifyAndClearExpectations(&obs2_);
@@ -178,8 +193,8 @@ TEST_F(PrefNotifierTest, FireObservers) {
// Make sure removing an observer from one pref doesn't affect anything else.
notifier.RemovePrefObserver(kChangedPref, &obs1_);
- EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
- obs2_.Expect(&pref_service_, kChangedPref, &value_true);
+ EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0);
+ EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
notifier.OnPreferenceChanged(kChangedPref);
Mock::VerifyAndClearExpectations(&obs1_);
Mock::VerifyAndClearExpectations(&obs2_);
@@ -187,8 +202,8 @@ TEST_F(PrefNotifierTest, FireObservers) {
// Make sure removing an observer entirely doesn't affect anything else.
notifier.RemovePrefObserver(kUnchangedPref, &obs1_);
- EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
- obs2_.Expect(&pref_service_, kChangedPref, &value_true);
+ EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0);
+ EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
notifier.OnPreferenceChanged(kChangedPref);
Mock::VerifyAndClearExpectations(&obs1_);
Mock::VerifyAndClearExpectations(&obs2_);

Powered by Google App Engine
This is Rietveld 408576698