| 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/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/notification_observer_mock.h" | 9 #include "chrome/common/notification_observer_mock.h" |
| 10 #include "chrome/common/notification_service.h" | 10 #include "chrome/common/notification_service.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 } | 53 } |
| 54 | 54 |
| 55 return count; | 55 return count; |
| 56 } | 56 } |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 // Test fixture class. | 59 // Test fixture class. |
| 60 class PrefNotifierTest : public testing::Test { | 60 class PrefNotifierTest : public testing::Test { |
| 61 protected: | 61 protected: |
| 62 virtual void SetUp() { | 62 virtual void SetUp() { |
| 63 pref_service_.RegisterBooleanPref(kChangedPref, true); | 63 pref_service_.reset(TestingPrefService::CreateTestingPrefService()); |
| 64 pref_service_.RegisterBooleanPref(kUnchangedPref, true); | 64 pref_service_->RegisterBooleanPref(kChangedPref, true); |
| 65 pref_service_->RegisterBooleanPref(kUnchangedPref, true); |
| 65 } | 66 } |
| 66 | 67 |
| 67 TestingPrefService pref_service_; | 68 scoped_ptr<TestingPrefService> pref_service_; |
| 68 | 69 |
| 69 PrefObserverMock obs1_; | 70 PrefObserverMock obs1_; |
| 70 PrefObserverMock obs2_; | 71 PrefObserverMock obs2_; |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 TEST_F(PrefNotifierTest, OnPreferenceChanged) { | 74 TEST_F(PrefNotifierTest, OnPreferenceChanged) { |
| 74 MockPrefNotifier notifier(&pref_service_); | 75 MockPrefNotifier notifier(pref_service_.get()); |
| 75 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1); | 76 EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1); |
| 76 notifier.OnPreferenceChanged(kChangedPref); | 77 notifier.OnPreferenceChanged(kChangedPref); |
| 77 } | 78 } |
| 78 | 79 |
| 79 TEST_F(PrefNotifierTest, OnInitializationCompleted) { | 80 TEST_F(PrefNotifierTest, OnInitializationCompleted) { |
| 80 MockPrefNotifier notifier(&pref_service_); | 81 MockPrefNotifier notifier(pref_service_.get()); |
| 81 NotificationObserverMock observer; | 82 NotificationObserverMock observer; |
| 82 NotificationRegistrar registrar; | 83 NotificationRegistrar registrar; |
| 83 registrar.Add(&observer, NotificationType::PREF_INITIALIZATION_COMPLETED, | 84 registrar.Add(&observer, NotificationType::PREF_INITIALIZATION_COMPLETED, |
| 84 Source<PrefService>(&pref_service_)); | 85 Source<PrefService>(pref_service_.get())); |
| 85 EXPECT_CALL(observer, Observe( | 86 EXPECT_CALL(observer, Observe( |
| 86 Field(&NotificationType::value, | 87 Field(&NotificationType::value, |
| 87 NotificationType::PREF_INITIALIZATION_COMPLETED), | 88 NotificationType::PREF_INITIALIZATION_COMPLETED), |
| 88 Source<PrefService>(&pref_service_), | 89 Source<PrefService>(pref_service_.get()), |
| 89 NotificationService::NoDetails())); | 90 NotificationService::NoDetails())); |
| 90 notifier.OnInitializationCompleted(); | 91 notifier.OnInitializationCompleted(); |
| 91 } | 92 } |
| 92 | 93 |
| 93 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { | 94 TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { |
| 94 const char pref_name[] = "homepage"; | 95 const char pref_name[] = "homepage"; |
| 95 const char pref_name2[] = "proxy"; | 96 const char pref_name2[] = "proxy"; |
| 96 | 97 |
| 97 MockPrefNotifier notifier(&pref_service_); | 98 MockPrefNotifier notifier(pref_service_.get()); |
| 98 notifier.AddPrefObserver(pref_name, &obs1_); | 99 notifier.AddPrefObserver(pref_name, &obs1_); |
| 99 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); | 100 ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 100 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); | 101 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
| 101 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); | 102 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 102 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); | 103 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 103 | 104 |
| 104 // 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. |
| 105 // 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 |
| 106 // thread-safe. | 107 // thread-safe. |
| 107 #if defined(NDEBUG) | 108 #if defined(NDEBUG) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 149 |
| 149 notifier.RemovePrefObserver(pref_name2, &obs1_); | 150 notifier.RemovePrefObserver(pref_name2, &obs1_); |
| 150 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); | 151 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 151 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); | 152 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
| 152 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); | 153 ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 153 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); | 154 ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 154 } | 155 } |
| 155 | 156 |
| 156 TEST_F(PrefNotifierTest, FireObservers) { | 157 TEST_F(PrefNotifierTest, FireObservers) { |
| 157 FundamentalValue value_true(true); | 158 FundamentalValue value_true(true); |
| 158 PrefNotifierImpl notifier(&pref_service_); | 159 PrefNotifierImpl notifier(pref_service_.get()); |
| 159 notifier.AddPrefObserver(kChangedPref, &obs1_); | 160 notifier.AddPrefObserver(kChangedPref, &obs1_); |
| 160 notifier.AddPrefObserver(kUnchangedPref, &obs1_); | 161 notifier.AddPrefObserver(kUnchangedPref, &obs1_); |
| 161 | 162 |
| 162 obs1_.Expect(&pref_service_, kChangedPref, &value_true); | 163 obs1_.Expect(pref_service_.get(), kChangedPref, &value_true); |
| 163 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); | 164 EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0); |
| 164 notifier.OnPreferenceChanged(kChangedPref); | 165 notifier.OnPreferenceChanged(kChangedPref); |
| 165 Mock::VerifyAndClearExpectations(&obs1_); | 166 Mock::VerifyAndClearExpectations(&obs1_); |
| 166 Mock::VerifyAndClearExpectations(&obs2_); | 167 Mock::VerifyAndClearExpectations(&obs2_); |
| 167 | 168 |
| 168 notifier.AddPrefObserver(kChangedPref, &obs2_); | 169 notifier.AddPrefObserver(kChangedPref, &obs2_); |
| 169 notifier.AddPrefObserver(kUnchangedPref, &obs2_); | 170 notifier.AddPrefObserver(kUnchangedPref, &obs2_); |
| 170 | 171 |
| 171 obs1_.Expect(&pref_service_, kChangedPref, &value_true); | 172 obs1_.Expect(pref_service_.get(), kChangedPref, &value_true); |
| 172 obs2_.Expect(&pref_service_, kChangedPref, &value_true); | 173 obs2_.Expect(pref_service_.get(), kChangedPref, &value_true); |
| 173 notifier.OnPreferenceChanged(kChangedPref); | 174 notifier.OnPreferenceChanged(kChangedPref); |
| 174 Mock::VerifyAndClearExpectations(&obs1_); | 175 Mock::VerifyAndClearExpectations(&obs1_); |
| 175 Mock::VerifyAndClearExpectations(&obs2_); | 176 Mock::VerifyAndClearExpectations(&obs2_); |
| 176 | 177 |
| 177 // Make sure removing an observer from one pref doesn't affect anything else. | 178 // Make sure removing an observer from one pref doesn't affect anything else. |
| 178 notifier.RemovePrefObserver(kChangedPref, &obs1_); | 179 notifier.RemovePrefObserver(kChangedPref, &obs1_); |
| 179 | 180 |
| 180 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 181 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 181 obs2_.Expect(&pref_service_, kChangedPref, &value_true); | 182 obs2_.Expect(pref_service_.get(), kChangedPref, &value_true); |
| 182 notifier.OnPreferenceChanged(kChangedPref); | 183 notifier.OnPreferenceChanged(kChangedPref); |
| 183 Mock::VerifyAndClearExpectations(&obs1_); | 184 Mock::VerifyAndClearExpectations(&obs1_); |
| 184 Mock::VerifyAndClearExpectations(&obs2_); | 185 Mock::VerifyAndClearExpectations(&obs2_); |
| 185 | 186 |
| 186 // Make sure removing an observer entirely doesn't affect anything else. | 187 // Make sure removing an observer entirely doesn't affect anything else. |
| 187 notifier.RemovePrefObserver(kUnchangedPref, &obs1_); | 188 notifier.RemovePrefObserver(kUnchangedPref, &obs1_); |
| 188 | 189 |
| 189 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); | 190 EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0); |
| 190 obs2_.Expect(&pref_service_, kChangedPref, &value_true); | 191 obs2_.Expect(pref_service_.get(), kChangedPref, &value_true); |
| 191 notifier.OnPreferenceChanged(kChangedPref); | 192 notifier.OnPreferenceChanged(kChangedPref); |
| 192 Mock::VerifyAndClearExpectations(&obs1_); | 193 Mock::VerifyAndClearExpectations(&obs1_); |
| 193 Mock::VerifyAndClearExpectations(&obs2_); | 194 Mock::VerifyAndClearExpectations(&obs2_); |
| 194 | 195 |
| 195 notifier.RemovePrefObserver(kChangedPref, &obs2_); | 196 notifier.RemovePrefObserver(kChangedPref, &obs2_); |
| 196 notifier.RemovePrefObserver(kUnchangedPref, &obs2_); | 197 notifier.RemovePrefObserver(kUnchangedPref, &obs2_); |
| 197 } | 198 } |
| 198 | 199 |
| 199 } // namespace | 200 } // namespace |
| OLD | NEW |