OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "components/prefs/json_pref_store.h" | 13 #include "components/prefs/json_pref_store.h" |
13 #include "components/prefs/mock_pref_change_callback.h" | 14 #include "components/prefs/mock_pref_change_callback.h" |
14 #include "components/prefs/pref_change_registrar.h" | 15 #include "components/prefs/pref_change_registrar.h" |
15 #include "components/prefs/pref_registry_simple.h" | 16 #include "components/prefs/pref_registry_simple.h" |
16 #include "components/prefs/pref_service_factory.h" | 17 #include "components/prefs/pref_service_factory.h" |
17 #include "components/prefs/pref_value_store.h" | 18 #include "components/prefs/pref_value_store.h" |
18 #include "components/prefs/testing_pref_service.h" | 19 #include "components/prefs/testing_pref_service.h" |
19 #include "components/prefs/testing_pref_store.h" | 20 #include "components/prefs/testing_pref_store.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 | 77 |
77 // Set a value and make sure we have a path. | 78 // Set a value and make sure we have a path. |
78 prefs.SetString(path, "blah"); | 79 prefs.SetString(path, "blah"); |
79 EXPECT_TRUE(prefs.HasPrefPath(path)); | 80 EXPECT_TRUE(prefs.HasPrefPath(path)); |
80 } | 81 } |
81 | 82 |
82 TEST(PrefServiceTest, Observers) { | 83 TEST(PrefServiceTest, Observers) { |
83 const char pref_name[] = "homepage"; | 84 const char pref_name[] = "homepage"; |
84 | 85 |
85 TestingPrefServiceSimple prefs; | 86 TestingPrefServiceSimple prefs; |
86 prefs.SetUserPref(pref_name, new base::Value("http://www.cnn.com")); | 87 prefs.SetUserPref(pref_name, |
| 88 base::MakeUnique<base::Value>("http://www.cnn.com")); |
87 prefs.registry()->RegisterStringPref(pref_name, std::string()); | 89 prefs.registry()->RegisterStringPref(pref_name, std::string()); |
88 | 90 |
89 const char new_pref_value[] = "http://www.google.com/"; | 91 const char new_pref_value[] = "http://www.google.com/"; |
90 const base::Value expected_new_pref_value(new_pref_value); | 92 const base::Value expected_new_pref_value(new_pref_value); |
91 MockPrefChangeCallback obs(&prefs); | 93 MockPrefChangeCallback obs(&prefs); |
92 PrefChangeRegistrar registrar; | 94 PrefChangeRegistrar registrar; |
93 registrar.Init(&prefs); | 95 registrar.Init(&prefs); |
94 registrar.Add(pref_name, obs.GetCallback()); | 96 registrar.Add(pref_name, obs.GetCallback()); |
95 | 97 |
96 PrefChangeRegistrar registrar_two; | 98 PrefChangeRegistrar registrar_two; |
(...skipping 15 matching lines...) Expand all Loading... |
112 prefs.SetString(pref_name, new_pref_value2); | 114 prefs.SetString(pref_name, new_pref_value2); |
113 Mock::VerifyAndClearExpectations(&obs); | 115 Mock::VerifyAndClearExpectations(&obs); |
114 Mock::VerifyAndClearExpectations(&obs2); | 116 Mock::VerifyAndClearExpectations(&obs2); |
115 | 117 |
116 // Set a recommended value. | 118 // Set a recommended value. |
117 const base::Value recommended_pref_value("http://www.gmail.com/"); | 119 const base::Value recommended_pref_value("http://www.gmail.com/"); |
118 obs.Expect(pref_name, &expected_new_pref_value2); | 120 obs.Expect(pref_name, &expected_new_pref_value2); |
119 obs2.Expect(pref_name, &expected_new_pref_value2); | 121 obs2.Expect(pref_name, &expected_new_pref_value2); |
120 // This should fire the checks in obs and obs2 but with an unchanged value | 122 // This should fire the checks in obs and obs2 but with an unchanged value |
121 // as the recommended value is being overridden by the user-set value. | 123 // as the recommended value is being overridden by the user-set value. |
122 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy()); | 124 prefs.SetRecommendedPref(pref_name, recommended_pref_value.CreateDeepCopy()); |
123 Mock::VerifyAndClearExpectations(&obs); | 125 Mock::VerifyAndClearExpectations(&obs); |
124 Mock::VerifyAndClearExpectations(&obs2); | 126 Mock::VerifyAndClearExpectations(&obs2); |
125 | 127 |
126 // Make sure obs2 still works after removing obs. | 128 // Make sure obs2 still works after removing obs. |
127 registrar.Remove(pref_name); | 129 registrar.Remove(pref_name); |
128 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); | 130 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
129 obs2.Expect(pref_name, &expected_new_pref_value); | 131 obs2.Expect(pref_name, &expected_new_pref_value); |
130 // This should only fire the observer in obs2. | 132 // This should only fire the observer in obs2. |
131 prefs.SetString(pref_name, new_pref_value); | 133 prefs.SetString(pref_name, new_pref_value); |
132 Mock::VerifyAndClearExpectations(&obs); | 134 Mock::VerifyAndClearExpectations(&obs); |
133 Mock::VerifyAndClearExpectations(&obs2); | 135 Mock::VerifyAndClearExpectations(&obs2); |
134 } | 136 } |
135 | 137 |
136 // Make sure that if a preference changes type, so the wrong type is stored in | 138 // Make sure that if a preference changes type, so the wrong type is stored in |
137 // the user pref file, it uses the correct fallback value instead. | 139 // the user pref file, it uses the correct fallback value instead. |
138 TEST(PrefServiceTest, GetValueChangedType) { | 140 TEST(PrefServiceTest, GetValueChangedType) { |
139 const int kTestValue = 10; | 141 const int kTestValue = 10; |
140 TestingPrefServiceSimple prefs; | 142 TestingPrefServiceSimple prefs; |
141 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue); | 143 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue); |
142 | 144 |
143 // Check falling back to a recommended value. | 145 // Check falling back to a recommended value. |
144 prefs.SetUserPref(kPrefName, new base::Value("not an integer")); | 146 prefs.SetUserPref(kPrefName, base::MakeUnique<base::Value>("not an integer")); |
145 const PrefService::Preference* pref = prefs.FindPreference(kPrefName); | 147 const PrefService::Preference* pref = prefs.FindPreference(kPrefName); |
146 ASSERT_TRUE(pref); | 148 ASSERT_TRUE(pref); |
147 const base::Value* value = pref->GetValue(); | 149 const base::Value* value = pref->GetValue(); |
148 ASSERT_TRUE(value); | 150 ASSERT_TRUE(value); |
149 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); | 151 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
150 int actual_int_value = -1; | 152 int actual_int_value = -1; |
151 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 153 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
152 EXPECT_EQ(kTestValue, actual_int_value); | 154 EXPECT_EQ(kTestValue, actual_int_value); |
153 } | 155 } |
154 | 156 |
(...skipping 14 matching lines...) Expand all Loading... |
169 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); | 171 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
170 int actual_int_value = -1; | 172 int actual_int_value = -1; |
171 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 173 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
172 EXPECT_EQ(kDefaultValue, actual_int_value); | 174 EXPECT_EQ(kDefaultValue, actual_int_value); |
173 | 175 |
174 // Check that GetRecommendedValue() returns no value. | 176 // Check that GetRecommendedValue() returns no value. |
175 value = pref->GetRecommendedValue(); | 177 value = pref->GetRecommendedValue(); |
176 ASSERT_FALSE(value); | 178 ASSERT_FALSE(value); |
177 | 179 |
178 // Set a user-set value. | 180 // Set a user-set value. |
179 prefs.SetUserPref(kPrefName, new base::Value(kUserValue)); | 181 prefs.SetUserPref(kPrefName, base::MakeUnique<base::Value>(kUserValue)); |
180 | 182 |
181 // Check that GetValue() returns the user-set value. | 183 // Check that GetValue() returns the user-set value. |
182 value = pref->GetValue(); | 184 value = pref->GetValue(); |
183 ASSERT_TRUE(value); | 185 ASSERT_TRUE(value); |
184 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); | 186 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
185 actual_int_value = -1; | 187 actual_int_value = -1; |
186 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 188 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
187 EXPECT_EQ(kUserValue, actual_int_value); | 189 EXPECT_EQ(kUserValue, actual_int_value); |
188 | 190 |
189 // Check that GetRecommendedValue() returns no value. | 191 // Check that GetRecommendedValue() returns no value. |
190 value = pref->GetRecommendedValue(); | 192 value = pref->GetRecommendedValue(); |
191 ASSERT_FALSE(value); | 193 ASSERT_FALSE(value); |
192 | 194 |
193 // Set a recommended value. | 195 // Set a recommended value. |
194 prefs.SetRecommendedPref(kPrefName, new base::Value(kRecommendedValue)); | 196 prefs.SetRecommendedPref(kPrefName, |
| 197 base::MakeUnique<base::Value>(kRecommendedValue)); |
195 | 198 |
196 // Check that GetValue() returns the user-set value. | 199 // Check that GetValue() returns the user-set value. |
197 value = pref->GetValue(); | 200 value = pref->GetValue(); |
198 ASSERT_TRUE(value); | 201 ASSERT_TRUE(value); |
199 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); | 202 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType()); |
200 actual_int_value = -1; | 203 actual_int_value = -1; |
201 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 204 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
202 EXPECT_EQ(kUserValue, actual_int_value); | 205 EXPECT_EQ(kUserValue, actual_int_value); |
203 | 206 |
204 // Check that GetRecommendedValue() returns the recommended value. | 207 // Check that GetRecommendedValue() returns the recommended value. |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 | 422 |
420 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 423 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
421 prefs_.Set(kName, new_value); | 424 prefs_.Set(kName, new_value); |
422 Mock::VerifyAndClearExpectations(&observer_); | 425 Mock::VerifyAndClearExpectations(&observer_); |
423 | 426 |
424 base::ListValue empty; | 427 base::ListValue empty; |
425 observer_.Expect(kName, &empty); | 428 observer_.Expect(kName, &empty); |
426 prefs_.Set(kName, empty); | 429 prefs_.Set(kName, empty); |
427 Mock::VerifyAndClearExpectations(&observer_); | 430 Mock::VerifyAndClearExpectations(&observer_); |
428 } | 431 } |
OLD | NEW |