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 "chrome/browser/prefs/pref_notifier_impl.h" | 5 #include "chrome/browser/prefs/pref_notifier_impl.h" |
6 #include "chrome/browser/prefs/pref_observer_mock.h" | 6 #include "chrome/browser/prefs/pref_observer_mock.h" |
7 #include "chrome/browser/prefs/pref_service.h" | 7 #include "chrome/browser/prefs/pref_service.h" |
8 #include "chrome/browser/prefs/pref_value_store.h" | 8 #include "chrome/browser/prefs/pref_value_store.h" |
9 #include "chrome/common/chrome_notification_types.h" | 9 #include "chrome/common/chrome_notification_types.h" |
10 #include "chrome/test/base/testing_pref_service.h" | 10 #include "chrome/test/base/testing_pref_service.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 using testing::Truly; | 22 using testing::Truly; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const char kChangedPref[] = "changed_pref"; | 26 const char kChangedPref[] = "changed_pref"; |
27 const char kUnchangedPref[] = "unchanged_pref"; | 27 const char kUnchangedPref[] = "unchanged_pref"; |
28 | 28 |
29 // Test PrefNotifier that allows tracking of observers and notifications. | 29 // Test PrefNotifier that allows tracking of observers and notifications. |
30 class MockPrefNotifier : public PrefNotifierImpl { | 30 class MockPrefNotifier : public PrefNotifierImpl { |
31 public: | 31 public: |
32 explicit MockPrefNotifier(PrefService* pref_service) | |
33 : PrefNotifierImpl(pref_service) {} | |
34 virtual ~MockPrefNotifier() {} | 32 virtual ~MockPrefNotifier() {} |
35 | 33 |
36 MOCK_METHOD1(FireObservers, void(const std::string& path)); | 34 MOCK_METHOD1(FireObservers, void(const std::string& path)); |
37 | 35 |
38 size_t CountObserver(const char* path, content::NotificationObserver* obs) { | 36 size_t CountObserver(const char* path, content::NotificationObserver* obs) { |
39 PrefObserverMap::const_iterator observer_iterator = | 37 PrefObserverMap::const_iterator observer_iterator = |
40 pref_observers()->find(path); | 38 pref_observers()->find(path); |
41 if (observer_iterator == pref_observers()->end()) | 39 if (observer_iterator == pref_observers()->end()) |
42 return false; | 40 return false; |
43 | 41 |
(...skipping 22 matching lines...) Expand all Loading... |
66 PrefService::UNSYNCABLE_PREF); | 64 PrefService::UNSYNCABLE_PREF); |
67 } | 65 } |
68 | 66 |
69 TestingPrefService pref_service_; | 67 TestingPrefService pref_service_; |
70 | 68 |
71 PrefObserverMock obs1_; | 69 PrefObserverMock obs1_; |
72 PrefObserverMock obs2_; | 70 PrefObserverMock obs2_; |
73 }; | 71 }; |
74 | 72 |
75 TEST_F(PrefNotifierTest, OnPreferenceChanged) { | 73 TEST_F(PrefNotifierTest, OnPreferenceChanged) { |
76 MockPrefNotifier notifier(&pref_service_); | 74 MockPrefNotifier notifier; |
| 75 notifier.SetPrefService(&pref_service_); |
77 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1); | 76 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1); |
78 notifier.OnPreferenceChanged(kChangedPref); | 77 notifier.OnPreferenceChanged(kChangedPref); |
79 } | 78 } |
80 | 79 |
81 TEST_F(PrefNotifierTest, OnInitializationCompleted) { | 80 TEST_F(PrefNotifierTest, OnInitializationCompleted) { |
82 MockPrefNotifier notifier(&pref_service_); | 81 MockPrefNotifier notifier; |
| 82 notifier.SetPrefService(&pref_service_); |
83 content::NotificationObserverMock observer; | 83 content::NotificationObserverMock observer; |
84 content::NotificationRegistrar registrar; | 84 content::NotificationRegistrar registrar; |
85 registrar.Add(&observer, chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED, | 85 registrar.Add(&observer, chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED, |
86 content::Source<PrefService>(&pref_service_)); | 86 content::Source<PrefService>(&pref_service_)); |
87 EXPECT_CALL(observer, Observe( | 87 EXPECT_CALL(observer, Observe( |
88 int(chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED), | 88 int(chrome::NOTIFICATION_PREF_INITIALIZATION_COMPLETED), |
89 content::Source<PrefService>(&pref_service_), | 89 content::Source<PrefService>(&pref_service_), |
90 Property(&content::Details<bool>::ptr, testing::Pointee(true)))); | 90 Property(&content::Details<bool>::ptr, testing::Pointee(true)))); |
91 notifier.OnInitializationCompleted(true); | 91 notifier.OnInitializationCompleted(true); |
92 } | 92 } |
93 | 93 |
94 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { | 94 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { |
95 const char pref_name[] = "homepage"; | 95 const char pref_name[] = "homepage"; |
96 const char pref_name2[] = "proxy"; | 96 const char pref_name2[] = "proxy"; |
97 | 97 |
98 MockPrefNotifier notifier(&pref_service_); | 98 MockPrefNotifier notifier; |
| 99 notifier.SetPrefService(&pref_service_); |
99 notifier.AddPrefObserver(pref_name, &obs1_); | 100 notifier.AddPrefObserver(pref_name, &obs1_); |
100 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); | 101 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
101 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); | 102 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
102 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); | 103 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
103 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); | 104 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
104 | 105 |
105 // Re-adding the same observer for the same pref doesn't change anything. | 106 // Re-adding the same observer for the same pref doesn't change anything. |
106 // Skip this in debug mode, since it hits a DCHECK and death tests aren't | 107 // Skip this in debug mode, since it hits a DCHECK and death tests aren't |
107 // thread-safe. | 108 // thread-safe. |
108 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 109 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 | 150 |
150 notifier.RemovePrefObserver(pref_name2, &obs1_); | 151 notifier.RemovePrefObserver(pref_name2, &obs1_); |
151 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); | 152 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
152 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); | 153 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
153 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); | 154 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
154 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); | 155 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
155 } | 156 } |
156 | 157 |
157 TEST_F(PrefNotifierTest, FireObservers) { | 158 TEST_F(PrefNotifierTest, FireObservers) { |
158 base::FundamentalValue value_true(true); | 159 base::FundamentalValue value_true(true); |
159 PrefNotifierImpl notifier(&pref_service_); | 160 PrefNotifierImpl notifier; |
| 161 notifier.SetPrefService(&pref_service_); |
160 notifier.AddPrefObserver(kChangedPref, &obs1_); | 162 notifier.AddPrefObserver(kChangedPref, &obs1_); |
161 notifier.AddPrefObserver(kUnchangedPref, &obs1_); | 163 notifier.AddPrefObserver(kUnchangedPref, &obs1_); |
162 | 164 |
163 obs1_.Expect(&pref_service_, kChangedPref, &value_true); | 165 obs1_.Expect(&pref_service_, kChangedPref, &value_true); |
164 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 166 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); |
165 notifier.OnPreferenceChanged(kChangedPref); | 167 notifier.OnPreferenceChanged(kChangedPref); |
166 Mock::VerifyAndClearExpectations(&obs1_); | 168 Mock::VerifyAndClearExpectations(&obs1_); |
167 Mock::VerifyAndClearExpectations(&obs2_); | 169 Mock::VerifyAndClearExpectations(&obs2_); |
168 | 170 |
169 notifier.AddPrefObserver(kChangedPref, &obs2_); | 171 notifier.AddPrefObserver(kChangedPref, &obs2_); |
(...skipping 21 matching lines...) Expand all Loading... |
191 obs2_.Expect(&pref_service_, kChangedPref, &value_true); | 193 obs2_.Expect(&pref_service_, kChangedPref, &value_true); |
192 notifier.OnPreferenceChanged(kChangedPref); | 194 notifier.OnPreferenceChanged(kChangedPref); |
193 Mock::VerifyAndClearExpectations(&obs1_); | 195 Mock::VerifyAndClearExpectations(&obs1_); |
194 Mock::VerifyAndClearExpectations(&obs2_); | 196 Mock::VerifyAndClearExpectations(&obs2_); |
195 | 197 |
196 notifier.RemovePrefObserver(kChangedPref, &obs2_); | 198 notifier.RemovePrefObserver(kChangedPref, &obs2_); |
197 notifier.RemovePrefObserver(kUnchangedPref, &obs2_); | 199 notifier.RemovePrefObserver(kUnchangedPref, &obs2_); |
198 } | 200 } |
199 | 201 |
200 } // namespace | 202 } // namespace |
OLD | NEW |