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