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

Side by Side 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: Merge to head for commit Created 8 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/prefs/pref_notifier_impl.cc ('k') | chrome/browser/prefs/pref_observer_mock.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/bind.h"
6 #include "base/callback.h"
7 #include "base/prefs/public/pref_observer.h"
5 #include "chrome/browser/prefs/pref_notifier_impl.h" 8 #include "chrome/browser/prefs/pref_notifier_impl.h"
6 #include "chrome/browser/prefs/pref_observer_mock.h" 9 #include "chrome/browser/prefs/pref_observer_mock.h"
7 #include "chrome/browser/prefs/pref_service.h" 10 #include "chrome/browser/prefs/pref_service.h"
8 #include "chrome/browser/prefs/pref_value_store.h" 11 #include "chrome/browser/prefs/pref_value_store.h"
9 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
10 #include "chrome/test/base/testing_pref_service.h" 13 #include "chrome/test/base/testing_pref_service.h"
11 #include "content/public/browser/notification_details.h"
12 #include "content/public/browser/notification_registrar.h"
13 #include "content/public/browser/notification_source.h"
14 #include "content/public/test/mock_notification_observer.h"
15 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
17 16
18 using testing::_; 17 using testing::_;
19 using testing::Field; 18 using testing::Field;
20 using testing::Invoke; 19 using testing::Invoke;
21 using testing::Mock; 20 using testing::Mock;
22 using testing::Truly; 21 using testing::Truly;
23 22
24 namespace { 23 namespace {
25 24
26 const char kChangedPref[] = "changed_pref"; 25 const char kChangedPref[] = "changed_pref";
27 const char kUnchangedPref[] = "unchanged_pref"; 26 const char kUnchangedPref[] = "unchanged_pref";
28 27
29 // Test PrefNotifier that allows tracking of observers and notifications. 28 class MockPrefInitObserver {
29 public:
30 MOCK_METHOD1(OnInitializationCompleted, void(bool));
31 };
32
33 // This is an unmodified PrefNotifierImpl, except we make
34 // OnPreferenceChanged public for tests.
35 class TestingPrefNotifierImpl : public PrefNotifierImpl {
36 public:
37 TestingPrefNotifierImpl(PrefService* service) : PrefNotifierImpl(service) {}
38
39 // Make public for tests.
40 using PrefNotifierImpl::OnPreferenceChanged;
41 };
42
43 // Mock PrefNotifier that allows tracking of observers and notifications.
30 class MockPrefNotifier : public PrefNotifierImpl { 44 class MockPrefNotifier : public PrefNotifierImpl {
31 public: 45 public:
32 explicit MockPrefNotifier(PrefService* pref_service) 46 explicit MockPrefNotifier(PrefService* pref_service)
33 : PrefNotifierImpl(pref_service) {} 47 : PrefNotifierImpl(pref_service) {}
34 virtual ~MockPrefNotifier() {} 48 virtual ~MockPrefNotifier() {}
35 49
36 MOCK_METHOD1(FireObservers, void(const std::string& path)); 50 MOCK_METHOD1(FireObservers, void(const std::string& path));
37 51
38 size_t CountObserver(const char* path, content::NotificationObserver* obs) { 52 size_t CountObserver(const char* path, PrefObserver* obs) {
39 PrefObserverMap::const_iterator observer_iterator = 53 PrefObserverMap::const_iterator observer_iterator =
40 pref_observers()->find(path); 54 pref_observers()->find(path);
41 if (observer_iterator == pref_observers()->end()) 55 if (observer_iterator == pref_observers()->end())
42 return false; 56 return false;
43 57
44 NotificationObserverList* observer_list = observer_iterator->second; 58 PrefObserverList* observer_list = observer_iterator->second;
45 NotificationObserverList::Iterator it(*observer_list); 59 PrefObserverList::Iterator it(*observer_list);
46 content::NotificationObserver* existing_obs; 60 PrefObserver* existing_obs;
47 size_t count = 0; 61 size_t count = 0;
48 while ((existing_obs = it.GetNext()) != NULL) { 62 while ((existing_obs = it.GetNext()) != NULL) {
49 if (existing_obs == obs) 63 if (existing_obs == obs)
50 count++; 64 count++;
51 } 65 }
52 66
53 return count; 67 return count;
54 } 68 }
69
70 // Make public for tests below.
71 using PrefNotifierImpl::OnPreferenceChanged;
72 using PrefNotifierImpl::OnInitializationCompleted;
55 }; 73 };
56 74
57 // Test fixture class. 75 // Test fixture class.
58 class PrefNotifierTest : public testing::Test { 76 class PrefNotifierTest : public testing::Test {
59 protected: 77 protected:
60 virtual void SetUp() { 78 virtual void SetUp() {
61 pref_service_.RegisterBooleanPref(kChangedPref, 79 pref_service_.RegisterBooleanPref(kChangedPref,
62 true, 80 true,
63 PrefService::UNSYNCABLE_PREF); 81 PrefService::UNSYNCABLE_PREF);
64 pref_service_.RegisterBooleanPref(kUnchangedPref, 82 pref_service_.RegisterBooleanPref(kUnchangedPref,
65 true, 83 true,
66 PrefService::UNSYNCABLE_PREF); 84 PrefService::UNSYNCABLE_PREF);
67 } 85 }
68 86
69 TestingPrefService pref_service_; 87 TestingPrefService pref_service_;
70 88
71 PrefObserverMock obs1_; 89 PrefObserverMock obs1_;
72 PrefObserverMock obs2_; 90 PrefObserverMock obs2_;
73 }; 91 };
74 92
75 TEST_F(PrefNotifierTest, OnPreferenceChanged) { 93 TEST_F(PrefNotifierTest, OnPreferenceChanged) {
76 MockPrefNotifier notifier(&pref_service_); 94 MockPrefNotifier notifier(&pref_service_);
77 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1); 95 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1);
78 notifier.OnPreferenceChanged(kChangedPref); 96 notifier.OnPreferenceChanged(kChangedPref);
79 } 97 }
80 98
81 TEST_F(PrefNotifierTest, OnInitializationCompleted) { 99 TEST_F(PrefNotifierTest, OnInitializationCompleted) {
82 MockPrefNotifier notifier(&pref_service_); 100 MockPrefNotifier notifier(&pref_service_);
83 content::MockNotificationObserver observer; 101 MockPrefInitObserver observer;
84 content::NotificationRegistrar registrar; 102 notifier.AddInitObserver(
85 registrar.Add(&observer, chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED, 103 base::Bind(&MockPrefInitObserver::OnInitializationCompleted,
86 content::Source<PrefService>(&pref_service_)); 104 base::Unretained(&observer)));
87 EXPECT_CALL(observer, Observe( 105 EXPECT_CALL(observer, OnInitializationCompleted(true));
88 int(chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED),
89 content::Source<PrefService>(&pref_service_),
90 Property(&content::Details<bool>::ptr, testing::Pointee(true))));
91 notifier.OnInitializationCompleted(true); 106 notifier.OnInitializationCompleted(true);
92 } 107 }
93 108
94 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { 109 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) {
95 const char pref_name[] = "homepage"; 110 const char pref_name[] = "homepage";
96 const char pref_name2[] = "proxy"; 111 const char pref_name2[] = "proxy";
97 112
98 MockPrefNotifier notifier(&pref_service_); 113 MockPrefNotifier notifier(&pref_service_);
99 notifier.AddPrefObserver(pref_name, &obs1_); 114 notifier.AddPrefObserver(pref_name, &obs1_);
100 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); 115 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); 163 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
149 164
150 notifier.RemovePrefObserver(pref_name2, &obs1_); 165 notifier.RemovePrefObserver(pref_name2, &obs1_);
151 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); 166 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_));
152 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); 167 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_));
153 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); 168 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_));
154 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); 169 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_));
155 } 170 }
156 171
157 TEST_F(PrefNotifierTest, FireObservers) { 172 TEST_F(PrefNotifierTest, FireObservers) {
158 base::FundamentalValue value_true(true); 173 TestingPrefNotifierImpl notifier(&pref_service_);
159 PrefNotifierImpl notifier(&pref_service_);
160 notifier.AddPrefObserver(kChangedPref, &obs1_); 174 notifier.AddPrefObserver(kChangedPref, &obs1_);
161 notifier.AddPrefObserver(kUnchangedPref, &obs1_); 175 notifier.AddPrefObserver(kUnchangedPref, &obs1_);
162 176
163 obs1_.Expect(&pref_service_, kChangedPref, &value_true); 177 EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref));
164 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); 178 EXPECT_CALL(obs2_, OnPreferenceChanged(_, _)).Times(0);
165 notifier.OnPreferenceChanged(kChangedPref); 179 notifier.OnPreferenceChanged(kChangedPref);
166 Mock::VerifyAndClearExpectations(&obs1_); 180 Mock::VerifyAndClearExpectations(&obs1_);
167 Mock::VerifyAndClearExpectations(&obs2_); 181 Mock::VerifyAndClearExpectations(&obs2_);
168 182
169 notifier.AddPrefObserver(kChangedPref, &obs2_); 183 notifier.AddPrefObserver(kChangedPref, &obs2_);
170 notifier.AddPrefObserver(kUnchangedPref, &obs2_); 184 notifier.AddPrefObserver(kUnchangedPref, &obs2_);
171 185
172 obs1_.Expect(&pref_service_, kChangedPref, &value_true); 186 EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref));
173 obs2_.Expect(&pref_service_, kChangedPref, &value_true); 187 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
174 notifier.OnPreferenceChanged(kChangedPref); 188 notifier.OnPreferenceChanged(kChangedPref);
175 Mock::VerifyAndClearExpectations(&obs1_); 189 Mock::VerifyAndClearExpectations(&obs1_);
176 Mock::VerifyAndClearExpectations(&obs2_); 190 Mock::VerifyAndClearExpectations(&obs2_);
177 191
178 // Make sure removing an observer from one pref doesn't affect anything else. 192 // Make sure removing an observer from one pref doesn't affect anything else.
179 notifier.RemovePrefObserver(kChangedPref, &obs1_); 193 notifier.RemovePrefObserver(kChangedPref, &obs1_);
180 194
181 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); 195 EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0);
182 obs2_.Expect(&pref_service_, kChangedPref, &value_true); 196 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
183 notifier.OnPreferenceChanged(kChangedPref); 197 notifier.OnPreferenceChanged(kChangedPref);
184 Mock::VerifyAndClearExpectations(&obs1_); 198 Mock::VerifyAndClearExpectations(&obs1_);
185 Mock::VerifyAndClearExpectations(&obs2_); 199 Mock::VerifyAndClearExpectations(&obs2_);
186 200
187 // Make sure removing an observer entirely doesn't affect anything else. 201 // Make sure removing an observer entirely doesn't affect anything else.
188 notifier.RemovePrefObserver(kUnchangedPref, &obs1_); 202 notifier.RemovePrefObserver(kUnchangedPref, &obs1_);
189 203
190 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); 204 EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0);
191 obs2_.Expect(&pref_service_, kChangedPref, &value_true); 205 EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref));
192 notifier.OnPreferenceChanged(kChangedPref); 206 notifier.OnPreferenceChanged(kChangedPref);
193 Mock::VerifyAndClearExpectations(&obs1_); 207 Mock::VerifyAndClearExpectations(&obs1_);
194 Mock::VerifyAndClearExpectations(&obs2_); 208 Mock::VerifyAndClearExpectations(&obs2_);
195 209
196 notifier.RemovePrefObserver(kChangedPref, &obs2_); 210 notifier.RemovePrefObserver(kChangedPref, &obs2_);
197 notifier.RemovePrefObserver(kUnchangedPref, &obs2_); 211 notifier.RemovePrefObserver(kUnchangedPref, &obs2_);
198 } 212 }
199 213
200 } // namespace 214 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_notifier_impl.cc ('k') | chrome/browser/prefs/pref_observer_mock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698