[email protected] | 39f8389 | 2011-04-25 17:15:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/callback.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 9 | #include "components/prefs/mock_pref_change_callback.h" |
| 10 | #include "components/prefs/pref_notifier_impl.h" |
| 11 | #include "components/prefs/pref_observer.h" |
| 12 | #include "components/prefs/pref_registry_simple.h" |
| 13 | #include "components/prefs/pref_service.h" |
| 14 | #include "components/prefs/pref_value_store.h" |
| 15 | #include "components/prefs/testing_pref_service.h" |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 16 | #include "testing/gmock/include/gmock/gmock.h" |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | using testing::_; |
| 20 | using testing::Field; |
| 21 | using testing::Invoke; |
| 22 | using testing::Mock; |
| 23 | using testing::Truly; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | const char kChangedPref[] = "changed_pref"; |
| 28 | const char kUnchangedPref[] = "unchanged_pref"; |
| 29 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 30 | class MockPrefInitObserver { |
| 31 | public: |
| 32 | MOCK_METHOD1(OnInitializationCompleted, void(bool)); |
| 33 | }; |
| 34 | |
| 35 | // This is an unmodified PrefNotifierImpl, except we make |
| 36 | // OnPreferenceChanged public for tests. |
| 37 | class TestingPrefNotifierImpl : public PrefNotifierImpl { |
| 38 | public: |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 39 | explicit TestingPrefNotifierImpl(PrefService* service) |
| 40 | : PrefNotifierImpl(service) { |
| 41 | } |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 42 | |
| 43 | // Make public for tests. |
| 44 | using PrefNotifierImpl::OnPreferenceChanged; |
| 45 | }; |
| 46 | |
| 47 | // Mock PrefNotifier that allows tracking of observers and notifications. |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 48 | class MockPrefNotifier : public PrefNotifierImpl { |
| 49 | public: |
| 50 | explicit MockPrefNotifier(PrefService* pref_service) |
| 51 | : PrefNotifierImpl(pref_service) {} |
Daniel Cheng | 6784852 | 2018-04-27 22:04:41 | [diff] [blame] | 52 | ~MockPrefNotifier() override {} |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 53 | |
| 54 | MOCK_METHOD1(FireObservers, void(const std::string& path)); |
| 55 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 56 | size_t CountObserver(const std::string& path, PrefObserver* obs) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 57 | auto observer_iterator = pref_observers()->find(path); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 58 | if (observer_iterator == pref_observers()->end()) |
| 59 | return false; |
| 60 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 61 | size_t count = 0; |
dcheng | 018341c | 2016-10-14 21:53:56 | [diff] [blame] | 62 | for (auto& existing_obs : *observer_iterator->second) { |
| 63 | if (&existing_obs == obs) |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 64 | count++; |
| 65 | } |
| 66 | |
| 67 | return count; |
| 68 | } |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 69 | |
| 70 | // Make public for tests below. |
| 71 | using PrefNotifierImpl::OnPreferenceChanged; |
| 72 | using PrefNotifierImpl::OnInitializationCompleted; |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 73 | }; |
| 74 | |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 75 | class PrefObserverMock : public PrefObserver { |
| 76 | public: |
| 77 | PrefObserverMock() {} |
| 78 | virtual ~PrefObserverMock() {} |
| 79 | |
[email protected] | d6ab845 | 2013-02-16 04:20:59 | [diff] [blame] | 80 | MOCK_METHOD2(OnPreferenceChanged, void(PrefService*, const std::string&)); |
[email protected] | 96a5c34 | 2012-12-04 18:14:02 | [diff] [blame] | 81 | }; |
| 82 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 83 | // Test fixture class. |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 84 | class PrefNotifierTest : public testing::Test { |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 85 | protected: |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 86 | void SetUp() override { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 87 | pref_service_.registry()->RegisterBooleanPref(kChangedPref, true); |
| 88 | pref_service_.registry()->RegisterBooleanPref(kUnchangedPref, true); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 91 | TestingPrefServiceSimple pref_service_; |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 92 | |
| 93 | PrefObserverMock obs1_; |
| 94 | PrefObserverMock obs2_; |
| 95 | }; |
| 96 | |
| 97 | TEST_F(PrefNotifierTest, OnPreferenceChanged) { |
| 98 | MockPrefNotifier notifier(&pref_service_); |
| 99 | EXPECT_CALL(notifier, FireObservers(kChangedPref)).Times(1); |
| 100 | notifier.OnPreferenceChanged(kChangedPref); |
| 101 | } |
| 102 | |
| 103 | TEST_F(PrefNotifierTest, OnInitializationCompleted) { |
| 104 | MockPrefNotifier notifier(&pref_service_); |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 105 | MockPrefInitObserver observer; |
| 106 | notifier.AddInitObserver( |
tzik | 2bcf8e4 | 2018-07-31 11:22:15 | [diff] [blame] | 107 | base::BindOnce(&MockPrefInitObserver::OnInitializationCompleted, |
| 108 | base::Unretained(&observer))); |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 109 | EXPECT_CALL(observer, OnInitializationCompleted(true)); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 110 | notifier.OnInitializationCompleted(true); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) { |
| 114 | const char pref_name[] = "homepage"; |
| 115 | const char pref_name2[] = "proxy"; |
| 116 | |
| 117 | MockPrefNotifier notifier(&pref_service_); |
| 118 | notifier.AddPrefObserver(pref_name, &obs1_); |
| 119 | ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 120 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
| 121 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 122 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 123 | |
| 124 | // Re-adding the same observer for the same pref doesn't change anything. |
| 125 | // Skip this in debug mode, since it hits a DCHECK and death tests aren't |
| 126 | // thread-safe. |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 127 | #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 128 | notifier.AddPrefObserver(pref_name, &obs1_); |
| 129 | ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 130 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
| 131 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 132 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
[email protected] | 20960e07 | 2011-09-20 20:59:01 | [diff] [blame] | 133 | #endif |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 134 | |
| 135 | // Ensure that we can add the same observer to a different pref. |
| 136 | notifier.AddPrefObserver(pref_name2, &obs1_); |
| 137 | ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 138 | ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 139 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 140 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 141 | |
| 142 | // Ensure that we can add another observer to the same pref. |
| 143 | notifier.AddPrefObserver(pref_name, &obs2_); |
| 144 | ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs1_)); |
| 145 | ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 146 | ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_)); |
| 147 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 148 | |
| 149 | // Ensure that we can remove all observers, and that removing a non-existent |
| 150 | // observer is harmless. |
| 151 | notifier.RemovePrefObserver(pref_name, &obs1_); |
| 152 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 153 | ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 154 | ASSERT_EQ(1u, notifier.CountObserver(pref_name, &obs2_)); |
| 155 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 156 | |
| 157 | notifier.RemovePrefObserver(pref_name, &obs2_); |
| 158 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 159 | ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 160 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 161 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 162 | |
| 163 | notifier.RemovePrefObserver(pref_name, &obs1_); |
| 164 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 165 | ASSERT_EQ(1u, notifier.CountObserver(pref_name2, &obs1_)); |
| 166 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 167 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 168 | |
| 169 | notifier.RemovePrefObserver(pref_name2, &obs1_); |
| 170 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs1_)); |
| 171 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs1_)); |
| 172 | ASSERT_EQ(0u, notifier.CountObserver(pref_name, &obs2_)); |
| 173 | ASSERT_EQ(0u, notifier.CountObserver(pref_name2, &obs2_)); |
| 174 | } |
| 175 | |
| 176 | TEST_F(PrefNotifierTest, FireObservers) { |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 177 | TestingPrefNotifierImpl notifier(&pref_service_); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 178 | notifier.AddPrefObserver(kChangedPref, &obs1_); |
| 179 | notifier.AddPrefObserver(kUnchangedPref, &obs1_); |
| 180 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 181 | EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref)); |
| 182 | EXPECT_CALL(obs2_, OnPreferenceChanged(_, _)).Times(0); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 183 | notifier.OnPreferenceChanged(kChangedPref); |
| 184 | Mock::VerifyAndClearExpectations(&obs1_); |
| 185 | Mock::VerifyAndClearExpectations(&obs2_); |
| 186 | |
| 187 | notifier.AddPrefObserver(kChangedPref, &obs2_); |
| 188 | notifier.AddPrefObserver(kUnchangedPref, &obs2_); |
| 189 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 190 | EXPECT_CALL(obs1_, OnPreferenceChanged(&pref_service_, kChangedPref)); |
| 191 | EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref)); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 192 | notifier.OnPreferenceChanged(kChangedPref); |
| 193 | Mock::VerifyAndClearExpectations(&obs1_); |
| 194 | Mock::VerifyAndClearExpectations(&obs2_); |
| 195 | |
| 196 | // Make sure removing an observer from one pref doesn't affect anything else. |
| 197 | notifier.RemovePrefObserver(kChangedPref, &obs1_); |
| 198 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 199 | EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0); |
| 200 | EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref)); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 201 | notifier.OnPreferenceChanged(kChangedPref); |
| 202 | Mock::VerifyAndClearExpectations(&obs1_); |
| 203 | Mock::VerifyAndClearExpectations(&obs2_); |
| 204 | |
| 205 | // Make sure removing an observer entirely doesn't affect anything else. |
| 206 | notifier.RemovePrefObserver(kUnchangedPref, &obs1_); |
| 207 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 208 | EXPECT_CALL(obs1_, OnPreferenceChanged(_, _)).Times(0); |
| 209 | EXPECT_CALL(obs2_, OnPreferenceChanged(&pref_service_, kChangedPref)); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 210 | notifier.OnPreferenceChanged(kChangedPref); |
| 211 | Mock::VerifyAndClearExpectations(&obs1_); |
| 212 | Mock::VerifyAndClearExpectations(&obs2_); |
| 213 | |
| 214 | notifier.RemovePrefObserver(kChangedPref, &obs2_); |
| 215 | notifier.RemovePrefObserver(kUnchangedPref, &obs2_); |
| 216 | } |
| 217 | |
| 218 | } // namespace |