| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/default_pref_store.h" | 5 #include "chrome/browser/prefs/default_pref_store.h" |
| 6 #include "chrome/browser/prefs/pref_notifier.h" | 6 #include "chrome/browser/prefs/pref_notifier.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/notification_observer.h" | 9 #include "chrome/common/notification_observer_mock.h" |
| 10 #include "chrome/common/notification_type.h" | |
| 11 #include "chrome/common/notification_service.h" | 10 #include "chrome/common/notification_service.h" |
| 11 #include "chrome/test/testing_pref_service.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using testing::_; |
| 16 using testing::Field; |
| 17 using testing::Invoke; |
| 18 using testing::Mock; |
| 19 using testing::Truly; |
| 15 | 20 |
| 16 namespace { | 21 namespace { |
| 17 | 22 |
| 18 const char kChangedPref[] = "changed_pref"; | 23 const char kChangedPref[] = "changed_pref"; |
| 19 const char kUnchangedPref[] = "unchanged_pref"; | 24 const char kUnchangedPref[] = "unchanged_pref"; |
| 20 | 25 |
| 21 bool DetailsAreChangedPref(const Details<std::string>& details) { | 26 bool DetailsAreChangedPref(const Details<std::string>& details) { |
| 22 std::string* string_in = Details<std::string>(details).ptr(); | 27 std::string* string_in = Details<std::string>(details).ptr(); |
| 23 return strcmp(string_in->c_str(), kChangedPref) == 0; | 28 return strcmp(string_in->c_str(), kChangedPref) == 0; |
| 24 } | 29 } |
| 25 | 30 |
| 26 // Test PrefNotifier that allows tracking of observers and notifications. | 31 // Test PrefNotifier that allows tracking of observers and notifications. |
| 27 class MockPrefNotifier : public PrefNotifier { | 32 class MockPrefNotifier : public PrefNotifier { |
| 28 public: | 33 public: |
| 29 MockPrefNotifier(PrefService* prefs, PrefValueStore* value_store) | 34 MockPrefNotifier(PrefService* prefs) |
| 30 : PrefNotifier(prefs, value_store) {} | 35 : PrefNotifier(prefs) {} |
| 31 | 36 |
| 32 virtual ~MockPrefNotifier() {} | 37 virtual ~MockPrefNotifier() {} |
| 33 | 38 |
| 34 MOCK_METHOD1(FireObservers, void(const char* path)); | 39 MOCK_METHOD1(FireObservers, void(const std::string& path)); |
| 35 | |
| 36 void RealFireObservers(const char* path) { | |
| 37 PrefNotifier::FireObservers(path); | |
| 38 } | |
| 39 | 40 |
| 40 size_t CountObserver(const char* path, NotificationObserver* obs) { | 41 size_t CountObserver(const char* path, NotificationObserver* obs) { |
| 41 PrefObserverMap::const_iterator observer_iterator = | 42 PrefObserverMap::const_iterator observer_iterator = |
| 42 pref_observers()->find(path); | 43 pref_observers()->find(path); |
| 43 if (observer_iterator == pref_observers()->end()) | 44 if (observer_iterator == pref_observers()->end()) |
| 44 return false; | 45 return false; |
| 45 | 46 |
| 46 NotificationObserverList* observer_list = observer_iterator->second; | 47 NotificationObserverList* observer_list = observer_iterator->second; |
| 47 NotificationObserverList::Iterator it(*observer_list); | 48 NotificationObserverList::Iterator it(*observer_list); |
| 48 NotificationObserver* existing_obs; | 49 NotificationObserver* existing_obs; |
| 49 size_t count = 0; | 50 size_t count = 0; |
| 50 while ((existing_obs = it.GetNext()) != NULL) { | 51 while ((existing_obs = it.GetNext()) != NULL) { |
| 51 if (existing_obs == obs) | 52 if (existing_obs == obs) |
| 52 count++; | 53 count++; |
| 53 } | 54 } |
| 54 | 55 |
| 55 return count; | 56 return count; |
| 56 } | 57 } |
| 57 }; | 58 }; |
| 58 | 59 |
| 59 // Mock PrefValueStore that has no unnecessary PrefStores and injects a simpler | |
| 60 // PrefHasChanged(). | |
| 61 class MockPrefValueStore : public PrefValueStore { | |
| 62 public: | |
| 63 MockPrefValueStore() | |
| 64 : PrefValueStore(NULL, NULL, NULL, NULL, NULL, NULL, | |
| 65 new DefaultPrefStore(), NULL) {} | |
| 66 | |
| 67 virtual ~MockPrefValueStore() {} | |
| 68 | |
| 69 // This mock version returns true if the pref path starts with "changed". | |
| 70 virtual bool PrefHasChanged(const char* path, | |
| 71 PrefNotifier::PrefStoreType new_store) { | |
| 72 std::string path_str(path); | |
| 73 if (path_str.compare(0, 7, "changed") == 0) | |
| 74 return true; | |
| 75 return false; | |
| 76 } | |
| 77 }; | |
| 78 | |
| 79 // Mock PrefService that allows the PrefNotifier to be injected. | |
| 80 class MockPrefService : public PrefService { | |
| 81 public: | |
| 82 explicit MockPrefService(PrefValueStore* pref_value_store) | |
| 83 : PrefService(pref_value_store) {} | |
| 84 | |
| 85 void SetPrefNotifier(PrefNotifier* notifier) { | |
| 86 pref_notifier_.reset(notifier); | |
| 87 } | |
| 88 }; | |
| 89 | |
| 90 // Mock PrefObserver that verifies notifications. | |
| 91 class MockPrefObserver : public NotificationObserver { | |
| 92 public: | |
| 93 virtual ~MockPrefObserver() {} | |
| 94 | |
| 95 MOCK_METHOD3(Observe, void(NotificationType type, | |
| 96 const NotificationSource& source, | |
| 97 const NotificationDetails& details)); | |
| 98 }; | |
| 99 | |
| 100 // Test fixture class. | 60 // Test fixture class. |
| 101 class PrefNotifierTest : public testing::Test { | 61 class PrefNotifierTest : public testing::Test { |
| 102 protected: | 62 protected: |
| 103 virtual void SetUp() { | 63 virtual void SetUp() { |
| 104 value_store_ = new MockPrefValueStore; | 64 pref_service_.RegisterBooleanPref(kChangedPref, true); |
| 105 pref_service_.reset(new MockPrefService(value_store_)); | 65 pref_service_.RegisterBooleanPref(kUnchangedPref, true); |
| 106 notifier_ = new MockPrefNotifier(pref_service_.get(), value_store_); | |
| 107 pref_service_->SetPrefNotifier(notifier_); | |
| 108 | |
| 109 pref_service_->RegisterBooleanPref(kChangedPref, true); | |
| 110 pref_service_->RegisterBooleanPref(kUnchangedPref, true); | |
| 111 } | 66 } |
| 112 | 67 |
| 113 // The PrefService takes ownership of the PrefValueStore and PrefNotifier. | 68 TestingPrefService pref_service_; |
| 114 PrefValueStore* value_store_; | |
| 115 MockPrefNotifier* notifier_; | |
| 116 scoped_ptr<MockPrefService> pref_service_; | |
| 117 | 69 |
| 118 MockPrefObserver obs1_; | 70 NotificationObserverMock obs1_; |
| 119 MockPrefObserver obs2_; | 71 NotificationObserverMock obs2_; |
| 120 }; | 72 }; |
| 121 | 73 |
| 122 TEST_F(PrefNotifierTest, OnPreferenceSet) { | 74 TEST_F(PrefNotifierTest, OnPreferenceChanged) { |
| 123 EXPECT_CALL(*notifier_, FireObservers(kChangedPref)) | 75 MockPrefNotifier notifier(&pref_service_); |
| 124 .Times(PrefNotifier::PREF_STORE_TYPE_MAX + 1); | 76 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1); |
| 125 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); | 77 notifier.OnPreferenceChanged(kChangedPref); |
| 126 | |
| 127 for (size_t i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { | |
| 128 notifier_->OnPreferenceSet(kChangedPref, | |
| 129 static_cast<PrefNotifier::PrefStoreType>(i)); | |
| 130 notifier_->OnPreferenceSet(kUnchangedPref, | |
| 131 static_cast<PrefNotifier::PrefStoreType>(i)); | |
| 132 } | |
| 133 } | 78 } |
| 134 | 79 |
| 135 TEST_F(PrefNotifierTest, OnUserPreferenceSet) { | 80 TEST_F(PrefNotifierTest, OnInitializationCompleted) { |
| 136 EXPECT_CALL(*notifier_, FireObservers(kChangedPref)); | 81 MockPrefNotifier notifier(&pref_service_); |
| 137 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); | 82 NotificationObserverMock observer; |
| 138 notifier_->OnUserPreferenceSet(kChangedPref); | 83 NotificationRegistrar registrar; |
| 139 notifier_->OnUserPreferenceSet(kUnchangedPref); | 84 registrar.Add(&observer, NotificationType::PREF_INITIALIZATION_COMPLETED, |
| 85 Source<PrefService>(&pref_service_)); |
| 86 EXPECT_CALL(observer, Observe( |
| 87 Field(&NotificationType::value, |
| 88 NotificationType::PREF_INITIALIZATION_COMPLETED), |
| 89 Source<PrefService>(&pref_service_), |
| 90 NotificationService::NoDetails())); |
| 91 notifier.OnInitializationCompleted(); |
| 140 } | 92 } |
| 141 | 93 |
| 142 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { | 94 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { |
| 143 const char pref_name[] = "homepage"; | 95 const char pref_name[] = "homepage"; |
| 144 const char pref_name2[] = "proxy"; | 96 const char pref_name2[] = "proxy"; |
| 145 | 97 |
| 146 notifier_->AddPrefObserver(pref_name, &obs1_); | 98 MockPrefNotifier notifier(&pref_service_); |
| 147 ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_)); | 99 notifier.AddPrefObserver(pref_name, &obs1_); |
| 148 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_)); | 100 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 149 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_)); | 101 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
| 150 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_)); | 102 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 103 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 151 | 104 |
| 152 // Re-adding the same observer for the same pref doesn't change anything. | 105 // Re-adding the same observer for the same pref doesn't change anything. |
| 153 // Skip this in debug mode, since it hits a DCHECK and death tests aren't | 106 // Skip this in debug mode, since it hits a DCHECK and death tests aren't |
| 154 // thread-safe. | 107 // thread-safe. |
| 155 #if defined(NDEBUG) | 108 #if defined(NDEBUG) |
| 156 notifier_->AddPrefObserver(pref_name, &obs1_); | 109 notifier.AddPrefObserver(pref_name, &obs1_); |
| 157 ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_)); | 110 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 158 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_)); | 111 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
| 159 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_)); | 112 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 160 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_)); | 113 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 161 #endif // NDEBUG | 114 #endif // NDEBUG |
| 162 | 115 |
| 163 // Ensure that we can add the same observer to a different pref. | 116 // Ensure that we can add the same observer to a different pref. |
| 164 notifier_->AddPrefObserver(pref_name2, &obs1_); | 117 notifier.AddPrefObserver(pref_name2, &obs1_); |
| 165 ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_)); | 118 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 166 ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_)); | 119 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 167 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_)); | 120 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 168 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_)); | 121 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 169 | 122 |
| 170 // Ensure that we can add another observer to the same pref. | 123 // Ensure that we can add another observer to the same pref. |
| 171 notifier_->AddPrefObserver(pref_name, &obs2_); | 124 notifier.AddPrefObserver(pref_name, &obs2_); |
| 172 ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_)); | 125 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 173 ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_)); | 126 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 174 ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs2_)); | 127 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_)); |
| 175 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_)); | 128 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 176 | 129 |
| 177 // Ensure that we can remove all observers, and that removing a non-existent | 130 // Ensure that we can remove all observers, and that removing a non-existent |
| 178 // observer is harmless. | 131 // observer is harmless. |
| 179 notifier_->RemovePrefObserver(pref_name, &obs1_); | 132 notifier.RemovePrefObserver(pref_name, &obs1_); |
| 180 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_)); | 133 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 181 ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_)); | 134 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 182 ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs2_)); | 135 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_)); |
| 183 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_)); | 136 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 184 | 137 |
| 185 notifier_->RemovePrefObserver(pref_name, &obs2_); | 138 notifier.RemovePrefObserver(pref_name, &obs2_); |
| 186 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_)); | 139 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 187 ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_)); | 140 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 188 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_)); | 141 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 189 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_)); | 142 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 190 | 143 |
| 191 notifier_->RemovePrefObserver(pref_name, &obs1_); | 144 notifier.RemovePrefObserver(pref_name, &obs1_); |
| 192 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_)); | 145 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 193 ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_)); | 146 ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 194 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_)); | 147 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 195 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_)); | 148 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 196 | 149 |
| 197 notifier_->RemovePrefObserver(pref_name2, &obs1_); | 150 notifier.RemovePrefObserver(pref_name2, &obs1_); |
| 198 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_)); | 151 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 199 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_)); | 152 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
| 200 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_)); | 153 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 201 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_)); | 154 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 202 } | 155 } |
| 203 | 156 |
| 204 TEST_F(PrefNotifierTest, FireObservers) { | 157 TEST_F(PrefNotifierTest, FireObservers) { |
| 205 using testing::_; | 158 PrefNotifier notifier(&pref_service_); |
| 206 using testing::Field; | 159 notifier.AddPrefObserver(kChangedPref, &obs1_); |
| 207 using testing::Invoke; | 160 notifier.AddPrefObserver(kUnchangedPref, &obs1_); |
| 208 using testing::Mock; | |
| 209 using testing::Truly; | |
| 210 | |
| 211 // Tell googlemock to pass calls to the mock's "back door" too. | |
| 212 ON_CALL(*notifier_, FireObservers(_)) | |
| 213 .WillByDefault(Invoke(notifier_, &MockPrefNotifier::RealFireObservers)); | |
| 214 EXPECT_CALL(*notifier_, FireObservers(kChangedPref)).Times(4); | |
| 215 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); | |
| 216 | |
| 217 notifier_->AddPrefObserver(kChangedPref, &obs1_); | |
| 218 notifier_->AddPrefObserver(kUnchangedPref, &obs1_); | |
| 219 | 161 |
| 220 EXPECT_CALL(obs1_, Observe( | 162 EXPECT_CALL(obs1_, Observe( |
| 221 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 163 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 222 Source<PrefService>(pref_service_.get()), | 164 Source<PrefService>(&pref_service_), |
| 223 Truly(DetailsAreChangedPref))); | 165 Truly(DetailsAreChangedPref))); |
| 224 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 166 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); |
| 225 notifier_->OnUserPreferenceSet(kChangedPref); | 167 notifier.OnPreferenceChanged(kChangedPref); |
| 226 Mock::VerifyAndClearExpectations(&obs1_); | 168 Mock::VerifyAndClearExpectations(&obs1_); |
| 227 Mock::VerifyAndClearExpectations(&obs2_); | 169 Mock::VerifyAndClearExpectations(&obs2_); |
| 228 | 170 |
| 229 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 171 notifier.AddPrefObserver(kChangedPref, &obs2_); |
| 230 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 172 notifier.AddPrefObserver(kUnchangedPref, &obs2_); |
| 231 notifier_->OnUserPreferenceSet(kUnchangedPref); | |
| 232 Mock::VerifyAndClearExpectations(&obs1_); | |
| 233 Mock::VerifyAndClearExpectations(&obs2_); | |
| 234 | |
| 235 notifier_->AddPrefObserver(kChangedPref, &obs2_); | |
| 236 notifier_->AddPrefObserver(kUnchangedPref, &obs2_); | |
| 237 | 173 |
| 238 EXPECT_CALL(obs1_, Observe( | 174 EXPECT_CALL(obs1_, Observe( |
| 239 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 175 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 240 Source<PrefService>(pref_service_.get()), | 176 Source<PrefService>(&pref_service_), |
| 241 Truly(DetailsAreChangedPref))); | 177 Truly(DetailsAreChangedPref))); |
| 242 EXPECT_CALL(obs2_, Observe( | 178 EXPECT_CALL(obs2_, Observe( |
| 243 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 179 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 244 Source<PrefService>(pref_service_.get()), | 180 Source<PrefService>(&pref_service_), |
| 245 Truly(DetailsAreChangedPref))); | 181 Truly(DetailsAreChangedPref))); |
| 246 notifier_->OnUserPreferenceSet(kChangedPref); | 182 notifier.OnPreferenceChanged(kChangedPref); |
| 247 Mock::VerifyAndClearExpectations(&obs1_); | |
| 248 Mock::VerifyAndClearExpectations(&obs2_); | |
| 249 | |
| 250 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | |
| 251 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | |
| 252 notifier_->OnUserPreferenceSet(kUnchangedPref); | |
| 253 Mock::VerifyAndClearExpectations(&obs1_); | 183 Mock::VerifyAndClearExpectations(&obs1_); |
| 254 Mock::VerifyAndClearExpectations(&obs2_); | 184 Mock::VerifyAndClearExpectations(&obs2_); |
| 255 | 185 |
| 256 // Make sure removing an observer from one pref doesn't affect anything else. | 186 // Make sure removing an observer from one pref doesn't affect anything else. |
| 257 notifier_->RemovePrefObserver(kChangedPref, &obs1_); | 187 notifier.RemovePrefObserver(kChangedPref, &obs1_); |
| 258 | 188 |
| 259 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 189 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 260 EXPECT_CALL(obs2_, Observe( | 190 EXPECT_CALL(obs2_, Observe( |
| 261 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 191 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 262 Source<PrefService>(pref_service_.get()), | 192 Source<PrefService>(&pref_service_), |
| 263 Truly(DetailsAreChangedPref))); | 193 Truly(DetailsAreChangedPref))); |
| 264 notifier_->OnUserPreferenceSet(kChangedPref); | 194 notifier.OnPreferenceChanged(kChangedPref); |
| 265 Mock::VerifyAndClearExpectations(&obs1_); | |
| 266 Mock::VerifyAndClearExpectations(&obs2_); | |
| 267 | |
| 268 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | |
| 269 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | |
| 270 notifier_->OnUserPreferenceSet(kUnchangedPref); | |
| 271 Mock::VerifyAndClearExpectations(&obs1_); | 195 Mock::VerifyAndClearExpectations(&obs1_); |
| 272 Mock::VerifyAndClearExpectations(&obs2_); | 196 Mock::VerifyAndClearExpectations(&obs2_); |
| 273 | 197 |
| 274 // Make sure removing an observer entirely doesn't affect anything else. | 198 // Make sure removing an observer entirely doesn't affect anything else. |
| 275 notifier_->RemovePrefObserver(kUnchangedPref, &obs1_); | 199 notifier.RemovePrefObserver(kUnchangedPref, &obs1_); |
| 276 | 200 |
| 277 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 201 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 278 EXPECT_CALL(obs2_, Observe( | 202 EXPECT_CALL(obs2_, Observe( |
| 279 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 203 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 280 Source<PrefService>(pref_service_.get()), | 204 Source<PrefService>(&pref_service_), |
| 281 Truly(DetailsAreChangedPref))); | 205 Truly(DetailsAreChangedPref))); |
| 282 notifier_->OnUserPreferenceSet(kChangedPref); | 206 notifier.OnPreferenceChanged(kChangedPref); |
| 283 Mock::VerifyAndClearExpectations(&obs1_); | 207 Mock::VerifyAndClearExpectations(&obs1_); |
| 284 Mock::VerifyAndClearExpectations(&obs2_); | 208 Mock::VerifyAndClearExpectations(&obs2_); |
| 285 | 209 |
| 286 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 210 notifier.RemovePrefObserver(kChangedPref, &obs2_); |
| 287 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 211 notifier.RemovePrefObserver(kUnchangedPref, &obs2_); |
| 288 notifier_->OnUserPreferenceSet(kUnchangedPref); | |
| 289 | |
| 290 notifier_->RemovePrefObserver(kChangedPref, &obs2_); | |
| 291 notifier_->RemovePrefObserver(kUnchangedPref, &obs2_); | |
| 292 } | 212 } |
| 293 | 213 |
| 294 } // namespace | 214 } // namespace |
| OLD | NEW |