| 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/pref_notifier.h" | 6 #include "chrome/browser/prefs/pref_notifier.h" |
| 6 #include "chrome/browser/prefs/pref_service.h" | 7 #include "chrome/browser/prefs/pref_service.h" |
| 7 #include "chrome/browser/prefs/pref_value_store.h" | 8 #include "chrome/browser/prefs/pref_value_store.h" |
| 8 #include "chrome/common/notification_observer.h" | 9 #include "chrome/common/notification_observer.h" |
| 9 #include "chrome/common/notification_type.h" | 10 #include "chrome/common/notification_type.h" |
| 10 #include "chrome/common/notification_service.h" | 11 #include "chrome/common/notification_service.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 | 15 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 size_t count = 0; | 49 size_t count = 0; |
| 49 while ((existing_obs = it.GetNext()) != NULL) { | 50 while ((existing_obs = it.GetNext()) != NULL) { |
| 50 if (existing_obs == obs) | 51 if (existing_obs == obs) |
| 51 count++; | 52 count++; |
| 52 } | 53 } |
| 53 | 54 |
| 54 return count; | 55 return count; |
| 55 } | 56 } |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 // Mock PrefValueStore that has no PrefStores and injects a simpler | 59 // Mock PrefValueStore that has no unnecessary PrefStores and injects a simpler |
| 59 // PrefHasChanged(). | 60 // PrefHasChanged(). |
| 60 class MockPrefValueStore : public PrefValueStore { | 61 class MockPrefValueStore : public PrefValueStore { |
| 61 public: | 62 public: |
| 62 MockPrefValueStore() : PrefValueStore(NULL, NULL, NULL, NULL, NULL) {} | 63 MockPrefValueStore() |
| 64 : PrefValueStore(NULL, NULL, NULL, NULL, NULL, new DefaultPrefStore()) {} |
| 63 | 65 |
| 64 virtual ~MockPrefValueStore() {} | 66 virtual ~MockPrefValueStore() {} |
| 65 | 67 |
| 66 // This mock version returns true if the pref path starts with "changed". | 68 // This mock version returns true if the pref path starts with "changed". |
| 67 virtual bool PrefHasChanged(const char* path, | 69 virtual bool PrefHasChanged(const char* path, |
| 68 PrefNotifier::PrefStoreType new_store, | 70 PrefNotifier::PrefStoreType new_store) { |
| 69 const Value* old_value) { | |
| 70 std::string path_str(path); | 71 std::string path_str(path); |
| 71 if (path_str.compare(0, 7, "changed") == 0) | 72 if (path_str.compare(0, 7, "changed") == 0) |
| 72 return true; | 73 return true; |
| 73 return false; | 74 return false; |
| 74 } | 75 } |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 // Mock PrefService that allows the PrefNotifier to be injected. | 78 // Mock PrefService that allows the PrefNotifier to be injected. |
| 78 class MockPrefService : public PrefService { | 79 class MockPrefService : public PrefService { |
| 79 public: | 80 public: |
| (...skipping 16 matching lines...) Expand all Loading... |
| 96 }; | 97 }; |
| 97 | 98 |
| 98 // Test fixture class. | 99 // Test fixture class. |
| 99 class PrefNotifierTest : public testing::Test { | 100 class PrefNotifierTest : public testing::Test { |
| 100 protected: | 101 protected: |
| 101 virtual void SetUp() { | 102 virtual void SetUp() { |
| 102 value_store_ = new MockPrefValueStore; | 103 value_store_ = new MockPrefValueStore; |
| 103 pref_service_.reset(new MockPrefService(value_store_)); | 104 pref_service_.reset(new MockPrefService(value_store_)); |
| 104 notifier_ = new MockPrefNotifier(pref_service_.get(), value_store_); | 105 notifier_ = new MockPrefNotifier(pref_service_.get(), value_store_); |
| 105 pref_service_->SetPrefNotifier(notifier_); | 106 pref_service_->SetPrefNotifier(notifier_); |
| 107 |
| 108 pref_service_->RegisterBooleanPref(kChangedPref, true); |
| 109 pref_service_->RegisterBooleanPref(kUnchangedPref, true); |
| 106 } | 110 } |
| 107 | 111 |
| 108 // The PrefService takes ownership of the PrefValueStore and PrefNotifier. | 112 // The PrefService takes ownership of the PrefValueStore and PrefNotifier. |
| 109 PrefValueStore* value_store_; | 113 PrefValueStore* value_store_; |
| 110 MockPrefNotifier* notifier_; | 114 MockPrefNotifier* notifier_; |
| 111 scoped_ptr<MockPrefService> pref_service_; | 115 scoped_ptr<MockPrefService> pref_service_; |
| 112 | 116 |
| 113 MockPrefObserver obs1_; | 117 MockPrefObserver obs1_; |
| 114 MockPrefObserver obs2_; | 118 MockPrefObserver obs2_; |
| 115 }; | 119 }; |
| 116 | 120 |
| 117 | |
| 118 TEST_F(PrefNotifierTest, OnPreferenceSet) { | 121 TEST_F(PrefNotifierTest, OnPreferenceSet) { |
| 119 EXPECT_CALL(*notifier_, FireObservers(kChangedPref)) | 122 EXPECT_CALL(*notifier_, FireObservers(kChangedPref)) |
| 120 .Times(PrefNotifier::PREF_STORE_TYPE_MAX + 1); | 123 .Times(PrefNotifier::PREF_STORE_TYPE_MAX + 1); |
| 121 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); | 124 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); |
| 122 | 125 |
| 123 for (size_t i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { | 126 for (size_t i = 0; i <= PrefNotifier::PREF_STORE_TYPE_MAX; ++i) { |
| 124 notifier_->OnPreferenceSet(kChangedPref, | 127 notifier_->OnPreferenceSet(kChangedPref, |
| 125 static_cast<PrefNotifier::PrefStoreType>(i), NULL); | 128 static_cast<PrefNotifier::PrefStoreType>(i)); |
| 126 notifier_->OnPreferenceSet(kUnchangedPref, | 129 notifier_->OnPreferenceSet(kUnchangedPref, |
| 127 static_cast<PrefNotifier::PrefStoreType>(i), NULL); | 130 static_cast<PrefNotifier::PrefStoreType>(i)); |
| 128 } | 131 } |
| 129 } | 132 } |
| 130 | 133 |
| 131 TEST_F(PrefNotifierTest, OnUserPreferenceSet) { | 134 TEST_F(PrefNotifierTest, OnUserPreferenceSet) { |
| 132 EXPECT_CALL(*notifier_, FireObservers(kChangedPref)); | 135 EXPECT_CALL(*notifier_, FireObservers(kChangedPref)); |
| 133 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); | 136 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); |
| 134 notifier_->OnUserPreferenceSet(kChangedPref, NULL); | 137 notifier_->OnUserPreferenceSet(kChangedPref); |
| 135 notifier_->OnUserPreferenceSet(kUnchangedPref, NULL); | 138 notifier_->OnUserPreferenceSet(kUnchangedPref); |
| 136 } | 139 } |
| 137 | 140 |
| 138 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { | 141 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { |
| 139 const char pref_name[] = "homepage"; | 142 const char pref_name[] = "homepage"; |
| 140 const char pref_name2[] = "proxy"; | 143 const char pref_name2[] = "proxy"; |
| 141 | 144 |
| 142 notifier_->AddPrefObserver(pref_name, &obs1_); | 145 notifier_->AddPrefObserver(pref_name, &obs1_); |
| 143 ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_)); | 146 ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_)); |
| 144 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_)); | 147 ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_)); |
| 145 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_)); | 148 ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_)); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); | 214 EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0); |
| 212 | 215 |
| 213 notifier_->AddPrefObserver(kChangedPref, &obs1_); | 216 notifier_->AddPrefObserver(kChangedPref, &obs1_); |
| 214 notifier_->AddPrefObserver(kUnchangedPref, &obs1_); | 217 notifier_->AddPrefObserver(kUnchangedPref, &obs1_); |
| 215 | 218 |
| 216 EXPECT_CALL(obs1_, Observe( | 219 EXPECT_CALL(obs1_, Observe( |
| 217 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 220 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 218 Source<PrefService>(pref_service_.get()), | 221 Source<PrefService>(pref_service_.get()), |
| 219 Truly(DetailsAreChangedPref))); | 222 Truly(DetailsAreChangedPref))); |
| 220 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 223 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); |
| 221 notifier_->OnUserPreferenceSet(kChangedPref, NULL); | 224 notifier_->OnUserPreferenceSet(kChangedPref); |
| 222 Mock::VerifyAndClearExpectations(&obs1_); | 225 Mock::VerifyAndClearExpectations(&obs1_); |
| 223 Mock::VerifyAndClearExpectations(&obs2_); | 226 Mock::VerifyAndClearExpectations(&obs2_); |
| 224 | 227 |
| 225 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 228 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 226 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 229 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); |
| 227 notifier_->OnUserPreferenceSet(kUnchangedPref, NULL); | 230 notifier_->OnUserPreferenceSet(kUnchangedPref); |
| 228 Mock::VerifyAndClearExpectations(&obs1_); | 231 Mock::VerifyAndClearExpectations(&obs1_); |
| 229 Mock::VerifyAndClearExpectations(&obs2_); | 232 Mock::VerifyAndClearExpectations(&obs2_); |
| 230 | 233 |
| 231 notifier_->AddPrefObserver(kChangedPref, &obs2_); | 234 notifier_->AddPrefObserver(kChangedPref, &obs2_); |
| 232 notifier_->AddPrefObserver(kUnchangedPref, &obs2_); | 235 notifier_->AddPrefObserver(kUnchangedPref, &obs2_); |
| 233 | 236 |
| 234 EXPECT_CALL(obs1_, Observe( | 237 EXPECT_CALL(obs1_, Observe( |
| 235 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 238 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 236 Source<PrefService>(pref_service_.get()), | 239 Source<PrefService>(pref_service_.get()), |
| 237 Truly(DetailsAreChangedPref))); | 240 Truly(DetailsAreChangedPref))); |
| 238 EXPECT_CALL(obs2_, Observe( | 241 EXPECT_CALL(obs2_, Observe( |
| 239 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 242 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 240 Source<PrefService>(pref_service_.get()), | 243 Source<PrefService>(pref_service_.get()), |
| 241 Truly(DetailsAreChangedPref))); | 244 Truly(DetailsAreChangedPref))); |
| 242 notifier_->OnUserPreferenceSet(kChangedPref, NULL); | 245 notifier_->OnUserPreferenceSet(kChangedPref); |
| 243 Mock::VerifyAndClearExpectations(&obs1_); | 246 Mock::VerifyAndClearExpectations(&obs1_); |
| 244 Mock::VerifyAndClearExpectations(&obs2_); | 247 Mock::VerifyAndClearExpectations(&obs2_); |
| 245 | 248 |
| 246 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 249 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 247 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 250 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); |
| 248 notifier_->OnUserPreferenceSet(kUnchangedPref, NULL); | 251 notifier_->OnUserPreferenceSet(kUnchangedPref); |
| 249 Mock::VerifyAndClearExpectations(&obs1_); | 252 Mock::VerifyAndClearExpectations(&obs1_); |
| 250 Mock::VerifyAndClearExpectations(&obs2_); | 253 Mock::VerifyAndClearExpectations(&obs2_); |
| 251 | 254 |
| 252 // Make sure removing an observer from one pref doesn't affect anything else. | 255 // Make sure removing an observer from one pref doesn't affect anything else. |
| 253 notifier_->RemovePrefObserver(kChangedPref, &obs1_); | 256 notifier_->RemovePrefObserver(kChangedPref, &obs1_); |
| 254 | 257 |
| 255 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 258 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 256 EXPECT_CALL(obs2_, Observe( | 259 EXPECT_CALL(obs2_, Observe( |
| 257 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 260 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 258 Source<PrefService>(pref_service_.get()), | 261 Source<PrefService>(pref_service_.get()), |
| 259 Truly(DetailsAreChangedPref))); | 262 Truly(DetailsAreChangedPref))); |
| 260 notifier_->OnUserPreferenceSet(kChangedPref, NULL); | 263 notifier_->OnUserPreferenceSet(kChangedPref); |
| 261 Mock::VerifyAndClearExpectations(&obs1_); | 264 Mock::VerifyAndClearExpectations(&obs1_); |
| 262 Mock::VerifyAndClearExpectations(&obs2_); | 265 Mock::VerifyAndClearExpectations(&obs2_); |
| 263 | 266 |
| 264 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 267 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 265 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 268 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); |
| 266 notifier_->OnUserPreferenceSet(kUnchangedPref, NULL); | 269 notifier_->OnUserPreferenceSet(kUnchangedPref); |
| 267 Mock::VerifyAndClearExpectations(&obs1_); | 270 Mock::VerifyAndClearExpectations(&obs1_); |
| 268 Mock::VerifyAndClearExpectations(&obs2_); | 271 Mock::VerifyAndClearExpectations(&obs2_); |
| 269 | 272 |
| 270 // Make sure removing an observer entirely doesn't affect anything else. | 273 // Make sure removing an observer entirely doesn't affect anything else. |
| 271 notifier_->RemovePrefObserver(kUnchangedPref, &obs1_); | 274 notifier_->RemovePrefObserver(kUnchangedPref, &obs1_); |
| 272 | 275 |
| 273 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 276 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 274 EXPECT_CALL(obs2_, Observe( | 277 EXPECT_CALL(obs2_, Observe( |
| 275 Field(&NotificationType::value, NotificationType::PREF_CHANGED), | 278 Field(&NotificationType::value, NotificationType::PREF_CHANGED), |
| 276 Source<PrefService>(pref_service_.get()), | 279 Source<PrefService>(pref_service_.get()), |
| 277 Truly(DetailsAreChangedPref))); | 280 Truly(DetailsAreChangedPref))); |
| 278 notifier_->OnUserPreferenceSet(kChangedPref, NULL); | 281 notifier_->OnUserPreferenceSet(kChangedPref); |
| 279 Mock::VerifyAndClearExpectations(&obs1_); | 282 Mock::VerifyAndClearExpectations(&obs1_); |
| 280 Mock::VerifyAndClearExpectations(&obs2_); | 283 Mock::VerifyAndClearExpectations(&obs2_); |
| 281 | 284 |
| 282 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 285 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 283 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 286 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); |
| 284 notifier_->OnUserPreferenceSet(kUnchangedPref, NULL); | 287 notifier_->OnUserPreferenceSet(kUnchangedPref); |
| 285 | 288 |
| 286 notifier_->RemovePrefObserver(kChangedPref, &obs2_); | 289 notifier_->RemovePrefObserver(kChangedPref, &obs2_); |
| 287 notifier_->RemovePrefObserver(kUnchangedPref, &obs2_); | 290 notifier_->RemovePrefObserver(kUnchangedPref, &obs2_); |
| 288 } | 291 } |
| 289 | 292 |
| 290 } // namespace | 293 } // namespace |
| OLD | NEW |