blob: c1ee360d6a61b6b902217a0514089898b93b50fb [file] [log] [blame]
[email protected]d3b05ea2012-01-24 22:57:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
avi9ef8bb02015-12-24 05:29:365#include <stddef.h>
6#include <stdint.h>
7
[email protected]ecde2742010-04-02 17:36:188#include <string>
9
avi9ef8bb02015-12-24 05:29:3610#include "base/macros.h"
Ilya Sherman557b2de72018-01-18 20:57:1711#include "base/time/time.h"
[email protected]ecde2742010-04-02 17:36:1812#include "base/values.h"
brettwf00b9b42016-02-01 22:11:3813#include "components/prefs/json_pref_store.h"
14#include "components/prefs/mock_pref_change_callback.h"
15#include "components/prefs/pref_change_registrar.h"
16#include "components/prefs/pref_registry_simple.h"
17#include "components/prefs/pref_service_factory.h"
18#include "components/prefs/pref_value_store.h"
19#include "components/prefs/testing_pref_service.h"
20#include "components/prefs/testing_pref_store.h"
[email protected]ecde2742010-04-02 17:36:1821#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2922#include "testing/gtest/include/gtest/gtest.h"
23
[email protected]ecde2742010-04-02 17:36:1824using testing::_;
25using testing::Mock;
[email protected]12a3c022010-11-03 10:24:1126
[email protected]aaa552312013-02-13 16:25:4027const char kPrefName[] = "pref.name";
28
[email protected]277404c22010-04-22 13:09:4529TEST(PrefServiceTest, NoObserverFire) {
[email protected]5b199522012-12-22 17:24:4430 TestingPrefServiceSimple prefs;
[email protected]7aa0a962010-04-21 17:24:4231
[email protected]57ecc4b2010-08-11 03:02:5132 const char pref_name[] = "homepage";
[email protected]b1de2c72013-02-06 02:45:4733 prefs.registry()->RegisterStringPref(pref_name, std::string());
[email protected]7aa0a962010-04-21 17:24:4234
[email protected]acd78969c2010-12-08 09:49:1135 const char new_pref_value[] = "http://www.google.com/";
[email protected]96a5c342012-12-04 18:14:0236 MockPrefChangeCallback obs(&prefs);
[email protected]2fb7dc982010-09-29 12:24:2837 PrefChangeRegistrar registrar;
38 registrar.Init(&prefs);
[email protected]96a5c342012-12-04 18:14:0239 registrar.Add(pref_name, obs.GetCallback());
[email protected]7aa0a962010-04-21 17:24:4240
[email protected]96a5c342012-12-04 18:14:0241 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
jdoerrie122c4da2017-03-06 11:12:0442 const base::Value expected_value(new_pref_value);
[email protected]96a5c342012-12-04 18:14:0243 obs.Expect(pref_name, &expected_value);
[email protected]acd78969c2010-12-08 09:49:1144 prefs.SetString(pref_name, new_pref_value);
45 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4246
47 // Setting the pref to the same value should not set the pref value a second
48 // time.
[email protected]96a5c342012-12-04 18:14:0249 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
[email protected]7aa0a962010-04-21 17:24:4250 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:1151 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4252
53 // Clearing the pref should cause the pref to fire.
jdoerrie122c4da2017-03-06 11:12:0454 const base::Value expected_default_value((std::string()));
[email protected]96a5c342012-12-04 18:14:0255 obs.Expect(pref_name, &expected_default_value);
[email protected]7aa0a962010-04-21 17:24:4256 prefs.ClearPref(pref_name);
[email protected]acd78969c2010-12-08 09:49:1157 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4258
59 // Clearing the pref again should not cause the pref to fire.
[email protected]96a5c342012-12-04 18:14:0260 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
[email protected]7aa0a962010-04-21 17:24:4261 prefs.ClearPref(pref_name);
[email protected]acd78969c2010-12-08 09:49:1162 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4263}
64
[email protected]277404c22010-04-22 13:09:4565TEST(PrefServiceTest, HasPrefPath) {
[email protected]5b199522012-12-22 17:24:4466 TestingPrefServiceSimple prefs;
[email protected]7aa0a962010-04-21 17:24:4267
[email protected]57ecc4b2010-08-11 03:02:5168 const char path[] = "fake.path";
[email protected]7aa0a962010-04-21 17:24:4269
70 // Shouldn't initially have a path.
71 EXPECT_FALSE(prefs.HasPrefPath(path));
72
73 // Register the path. This doesn't set a value, so the path still shouldn't
74 // exist.
[email protected]b1de2c72013-02-06 02:45:4775 prefs.registry()->RegisterStringPref(path, std::string());
[email protected]7aa0a962010-04-21 17:24:4276 EXPECT_FALSE(prefs.HasPrefPath(path));
77
78 // Set a value and make sure we have a path.
[email protected]ddd231e2010-06-29 20:35:1979 prefs.SetString(path, "blah");
[email protected]7aa0a962010-04-21 17:24:4280 EXPECT_TRUE(prefs.HasPrefPath(path));
81}
82
[email protected]277404c22010-04-22 13:09:4583TEST(PrefServiceTest, Observers) {
[email protected]57ecc4b2010-08-11 03:02:5184 const char pref_name[] = "homepage";
[email protected]277404c22010-04-22 13:09:4585
[email protected]5b199522012-12-22 17:24:4486 TestingPrefServiceSimple prefs;
vabr8684c9a2017-03-29 13:14:5787 prefs.SetUserPref(pref_name,
Jinho Bang84b58bd2018-01-01 21:44:4888 std::make_unique<base::Value>("http://www.cnn.com"));
[email protected]b1de2c72013-02-06 02:45:4789 prefs.registry()->RegisterStringPref(pref_name, std::string());
[email protected]277404c22010-04-22 13:09:4590
[email protected]acd78969c2010-12-08 09:49:1191 const char new_pref_value[] = "http://www.google.com/";
jdoerrie122c4da2017-03-06 11:12:0492 const base::Value expected_new_pref_value(new_pref_value);
[email protected]96a5c342012-12-04 18:14:0293 MockPrefChangeCallback obs(&prefs);
[email protected]2fb7dc982010-09-29 12:24:2894 PrefChangeRegistrar registrar;
95 registrar.Init(&prefs);
[email protected]96a5c342012-12-04 18:14:0296 registrar.Add(pref_name, obs.GetCallback());
[email protected]277404c22010-04-22 13:09:4597
[email protected]54ffd94a2012-11-12 15:29:2098 PrefChangeRegistrar registrar_two;
99 registrar_two.Init(&prefs);
100
[email protected]96a5c342012-12-04 18:14:02101 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
102 obs.Expect(pref_name, &expected_new_pref_value);
[email protected]acd78969c2010-12-08 09:49:11103 prefs.SetString(pref_name, new_pref_value);
104 Mock::VerifyAndClearExpectations(&obs);
[email protected]277404c22010-04-22 13:09:45105
106 // Now try adding a second pref observer.
[email protected]acd78969c2010-12-08 09:49:11107 const char new_pref_value2[] = "http://www.youtube.com/";
jdoerrie122c4da2017-03-06 11:12:04108 const base::Value expected_new_pref_value2(new_pref_value2);
[email protected]96a5c342012-12-04 18:14:02109 MockPrefChangeCallback obs2(&prefs);
110 obs.Expect(pref_name, &expected_new_pref_value2);
111 obs2.Expect(pref_name, &expected_new_pref_value2);
112 registrar_two.Add(pref_name, obs2.GetCallback());
[email protected]277404c22010-04-22 13:09:45113 // This should fire the checks in obs and obs2.
114 prefs.SetString(pref_name, new_pref_value2);
[email protected]acd78969c2010-12-08 09:49:11115 Mock::VerifyAndClearExpectations(&obs);
116 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45117
[email protected]7ca0f362012-07-30 10:14:03118 // Set a recommended value.
jdoerrie122c4da2017-03-06 11:12:04119 const base::Value recommended_pref_value("http://www.gmail.com/");
[email protected]96a5c342012-12-04 18:14:02120 obs.Expect(pref_name, &expected_new_pref_value2);
121 obs2.Expect(pref_name, &expected_new_pref_value2);
[email protected]7ca0f362012-07-30 10:14:03122 // This should fire the checks in obs and obs2 but with an unchanged value
123 // as the recommended value is being overridden by the user-set value.
vabr8684c9a2017-03-29 13:14:57124 prefs.SetRecommendedPref(pref_name, recommended_pref_value.CreateDeepCopy());
[email protected]7ca0f362012-07-30 10:14:03125 Mock::VerifyAndClearExpectations(&obs);
126 Mock::VerifyAndClearExpectations(&obs2);
127
[email protected]277404c22010-04-22 13:09:45128 // Make sure obs2 still works after removing obs.
[email protected]54ffd94a2012-11-12 15:29:20129 registrar.Remove(pref_name);
[email protected]96a5c342012-12-04 18:14:02130 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
131 obs2.Expect(pref_name, &expected_new_pref_value);
[email protected]277404c22010-04-22 13:09:45132 // This should only fire the observer in obs2.
133 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:11134 Mock::VerifyAndClearExpectations(&obs);
135 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45136}
137
[email protected]9a8c4022011-01-25 14:25:33138// Make sure that if a preference changes type, so the wrong type is stored in
139// the user pref file, it uses the correct fallback value instead.
140TEST(PrefServiceTest, GetValueChangedType) {
141 const int kTestValue = 10;
[email protected]5b199522012-12-22 17:24:44142 TestingPrefServiceSimple prefs;
[email protected]aaa552312013-02-13 16:25:40143 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue);
[email protected]9a8c4022011-01-25 14:25:33144
145 // Check falling back to a recommended value.
Jinho Bang84b58bd2018-01-01 21:44:48146 prefs.SetUserPref(kPrefName, std::make_unique<base::Value>("not an integer"));
[email protected]aaa552312013-02-13 16:25:40147 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
[email protected]9a8c4022011-01-25 14:25:33148 ASSERT_TRUE(pref);
[email protected]a43a667b2013-06-14 17:56:08149 const base::Value* value = pref->GetValue();
[email protected]9a8c4022011-01-25 14:25:33150 ASSERT_TRUE(value);
jdoerrie76cee9c2017-10-06 22:42:42151 EXPECT_EQ(base::Value::Type::INTEGER, value->type());
[email protected]9a8c4022011-01-25 14:25:33152 int actual_int_value = -1;
153 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
154 EXPECT_EQ(kTestValue, actual_int_value);
155}
156
[email protected]7ca0f362012-07-30 10:14:03157TEST(PrefServiceTest, GetValueAndGetRecommendedValue) {
158 const int kDefaultValue = 5;
159 const int kUserValue = 10;
160 const int kRecommendedValue = 15;
[email protected]5b199522012-12-22 17:24:44161 TestingPrefServiceSimple prefs;
[email protected]aaa552312013-02-13 16:25:40162 prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue);
[email protected]7ca0f362012-07-30 10:14:03163
164 // Create pref with a default value only.
[email protected]aaa552312013-02-13 16:25:40165 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
[email protected]7ca0f362012-07-30 10:14:03166 ASSERT_TRUE(pref);
167
168 // Check that GetValue() returns the default value.
[email protected]a43a667b2013-06-14 17:56:08169 const base::Value* value = pref->GetValue();
[email protected]7ca0f362012-07-30 10:14:03170 ASSERT_TRUE(value);
jdoerrie76cee9c2017-10-06 22:42:42171 EXPECT_EQ(base::Value::Type::INTEGER, value->type());
[email protected]7ca0f362012-07-30 10:14:03172 int actual_int_value = -1;
173 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
174 EXPECT_EQ(kDefaultValue, actual_int_value);
175
176 // Check that GetRecommendedValue() returns no value.
177 value = pref->GetRecommendedValue();
178 ASSERT_FALSE(value);
179
180 // Set a user-set value.
Jinho Bang84b58bd2018-01-01 21:44:48181 prefs.SetUserPref(kPrefName, std::make_unique<base::Value>(kUserValue));
[email protected]7ca0f362012-07-30 10:14:03182
183 // Check that GetValue() returns the user-set value.
184 value = pref->GetValue();
185 ASSERT_TRUE(value);
jdoerrie76cee9c2017-10-06 22:42:42186 EXPECT_EQ(base::Value::Type::INTEGER, value->type());
[email protected]7ca0f362012-07-30 10:14:03187 actual_int_value = -1;
188 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
189 EXPECT_EQ(kUserValue, actual_int_value);
190
191 // Check that GetRecommendedValue() returns no value.
192 value = pref->GetRecommendedValue();
193 ASSERT_FALSE(value);
194
195 // Set a recommended value.
vabr8684c9a2017-03-29 13:14:57196 prefs.SetRecommendedPref(kPrefName,
Jinho Bang84b58bd2018-01-01 21:44:48197 std::make_unique<base::Value>(kRecommendedValue));
[email protected]7ca0f362012-07-30 10:14:03198
199 // Check that GetValue() returns the user-set value.
200 value = pref->GetValue();
201 ASSERT_TRUE(value);
jdoerrie76cee9c2017-10-06 22:42:42202 EXPECT_EQ(base::Value::Type::INTEGER, value->type());
[email protected]7ca0f362012-07-30 10:14:03203 actual_int_value = -1;
204 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
205 EXPECT_EQ(kUserValue, actual_int_value);
206
207 // Check that GetRecommendedValue() returns the recommended value.
208 value = pref->GetRecommendedValue();
209 ASSERT_TRUE(value);
jdoerrie76cee9c2017-10-06 22:42:42210 EXPECT_EQ(base::Value::Type::INTEGER, value->type());
[email protected]7ca0f362012-07-30 10:14:03211 actual_int_value = -1;
212 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
213 EXPECT_EQ(kRecommendedValue, actual_int_value);
214
215 // Remove the user-set value.
[email protected]aaa552312013-02-13 16:25:40216 prefs.RemoveUserPref(kPrefName);
[email protected]7ca0f362012-07-30 10:14:03217
218 // Check that GetValue() returns the recommended value.
219 value = pref->GetValue();
220 ASSERT_TRUE(value);
jdoerrie76cee9c2017-10-06 22:42:42221 EXPECT_EQ(base::Value::Type::INTEGER, value->type());
[email protected]7ca0f362012-07-30 10:14:03222 actual_int_value = -1;
223 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
224 EXPECT_EQ(kRecommendedValue, actual_int_value);
225
226 // Check that GetRecommendedValue() returns the recommended value.
227 value = pref->GetRecommendedValue();
228 ASSERT_TRUE(value);
jdoerrie76cee9c2017-10-06 22:42:42229 EXPECT_EQ(base::Value::Type::INTEGER, value->type());
[email protected]7ca0f362012-07-30 10:14:03230 actual_int_value = -1;
231 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
232 EXPECT_EQ(kRecommendedValue, actual_int_value);
233}
234
Ilya Sherman557b2de72018-01-18 20:57:17235TEST(PrefServiceTest, SetTimeValue_RegularTime) {
236 TestingPrefServiceSimple prefs;
237
238 // Register a null time as the default.
239 prefs.registry()->RegisterTimePref(kPrefName, base::Time());
240 EXPECT_TRUE(prefs.GetTime(kPrefName).is_null());
241
242 // Set a time and make sure that we can read it without any loss of precision.
243 const base::Time time = base::Time::Now();
244 prefs.SetTime(kPrefName, time);
245 EXPECT_EQ(time, prefs.GetTime(kPrefName));
246}
247
248TEST(PrefServiceTest, SetTimeValue_NullTime) {
249 TestingPrefServiceSimple prefs;
250
251 // Register a non-null time as the default.
252 const base::Time default_time = base::Time::FromDeltaSinceWindowsEpoch(
253 base::TimeDelta::FromMicroseconds(12345));
254 prefs.registry()->RegisterTimePref(kPrefName, default_time);
255 EXPECT_FALSE(prefs.GetTime(kPrefName).is_null());
256
257 // Set a null time and make sure that it remains null upon deserialization.
258 prefs.SetTime(kPrefName, base::Time());
259 EXPECT_TRUE(prefs.GetTime(kPrefName).is_null());
260}
261
raymesf3a929b02015-05-07 03:54:45262// A PrefStore which just stores the last write flags that were used to write
263// values to it.
264class WriteFlagChecker : public TestingPrefStore {
265 public:
266 WriteFlagChecker() {}
267
avi9ef8bb02015-12-24 05:29:36268 void ReportValueChanged(const std::string& key, uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45269 SetLastWriteFlags(flags);
270 }
271
272 void SetValue(const std::string& key,
dcheng5f043bc2016-04-22 19:09:06273 std::unique_ptr<base::Value> value,
avi9ef8bb02015-12-24 05:29:36274 uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45275 SetLastWriteFlags(flags);
raymesf3a929b02015-05-07 03:54:45276 }
277
278 void SetValueSilently(const std::string& key,
dcheng5f043bc2016-04-22 19:09:06279 std::unique_ptr<base::Value> value,
avi9ef8bb02015-12-24 05:29:36280 uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45281 SetLastWriteFlags(flags);
raymesf3a929b02015-05-07 03:54:45282 }
283
avi9ef8bb02015-12-24 05:29:36284 void RemoveValue(const std::string& key, uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45285 SetLastWriteFlags(flags);
286 }
287
avi9ef8bb02015-12-24 05:29:36288 uint32_t GetLastFlagsAndClear() {
raymesf3a929b02015-05-07 03:54:45289 CHECK(last_write_flags_set_);
avi9ef8bb02015-12-24 05:29:36290 uint32_t result = last_write_flags_;
raymesf3a929b02015-05-07 03:54:45291 last_write_flags_set_ = false;
292 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
293 return result;
294 }
295
296 bool last_write_flags_set() { return last_write_flags_set_; }
297
298 private:
299 ~WriteFlagChecker() override {}
300
avi9ef8bb02015-12-24 05:29:36301 void SetLastWriteFlags(uint32_t flags) {
raymesf3a929b02015-05-07 03:54:45302 CHECK(!last_write_flags_set_);
303 last_write_flags_set_ = true;
304 last_write_flags_ = flags;
305 }
306
307 bool last_write_flags_set_ = false;
avi9ef8bb02015-12-24 05:29:36308 uint32_t last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
raymesf3a929b02015-05-07 03:54:45309};
310
311TEST(PrefServiceTest, WriteablePrefStoreFlags) {
312 scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker);
313 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple);
brettw066508682016-02-03 08:22:02314 PrefServiceFactory factory;
raymesf3a929b02015-05-07 03:54:45315 factory.set_user_prefs(flag_checker);
dcheng5f043bc2016-04-22 19:09:06316 std::unique_ptr<PrefService> prefs(factory.Create(registry.get()));
raymesf3a929b02015-05-07 03:54:45317
318 // The first 8 bits of write flags are reserved for subclasses. Create a
319 // custom flag in this range
avi9ef8bb02015-12-24 05:29:36320 uint32_t kCustomRegistrationFlag = 1 << 2;
raymesf3a929b02015-05-07 03:54:45321
322 // A map of the registration flags that will be tested and the write flags
323 // they are expected to convert to.
324 struct RegistrationToWriteFlags {
325 const char* pref_name;
avi9ef8bb02015-12-24 05:29:36326 uint32_t registration_flags;
327 uint32_t write_flags;
raymesf3a929b02015-05-07 03:54:45328 };
329 const RegistrationToWriteFlags kRegistrationToWriteFlags[] = {
330 {"none",
331 PrefRegistry::NO_REGISTRATION_FLAGS,
332 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
333 {"lossy",
334 PrefRegistry::LOSSY_PREF,
335 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG},
336 {"custom",
337 kCustomRegistrationFlag,
338 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
339 {"lossyandcustom",
340 PrefRegistry::LOSSY_PREF | kCustomRegistrationFlag,
341 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}};
342
343 for (size_t i = 0; i < arraysize(kRegistrationToWriteFlags); ++i) {
344 RegistrationToWriteFlags entry = kRegistrationToWriteFlags[i];
vabr88e507212017-03-29 13:22:26345 registry->RegisterDictionaryPref(entry.pref_name,
Jinho Bang84b58bd2018-01-01 21:44:48346 std::make_unique<base::DictionaryValue>(),
vabr88e507212017-03-29 13:22:26347 entry.registration_flags);
raymesf3a929b02015-05-07 03:54:45348
349 SCOPED_TRACE("Currently testing pref with name: " +
350 std::string(entry.pref_name));
351
jdoerriedc72ee942016-12-07 15:43:28352 prefs->GetMutableUserPref(entry.pref_name, base::Value::Type::DICTIONARY);
raymesf3a929b02015-05-07 03:54:45353 EXPECT_TRUE(flag_checker->last_write_flags_set());
354 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
355
356 prefs->ReportUserPrefChanged(entry.pref_name);
357 EXPECT_TRUE(flag_checker->last_write_flags_set());
358 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
359
360 prefs->ClearPref(entry.pref_name);
361 EXPECT_TRUE(flag_checker->last_write_flags_set());
362 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
363
vabrbce355c2017-03-23 18:52:43364 prefs->SetUserPrefValue(entry.pref_name,
Jinho Bang84b58bd2018-01-01 21:44:48365 std::make_unique<base::DictionaryValue>());
raymesf3a929b02015-05-07 03:54:45366 EXPECT_TRUE(flag_checker->last_write_flags_set());
367 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
368 }
369}
370
[email protected]ecde2742010-04-02 17:36:18371class PrefServiceSetValueTest : public testing::Test {
372 protected:
[email protected]12a3c022010-11-03 10:24:11373 static const char kName[];
374 static const char kValue[];
[email protected]ecde2742010-04-02 17:36:18375
[email protected]96a5c342012-12-04 18:14:02376 PrefServiceSetValueTest() : observer_(&prefs_) {}
377
[email protected]5b199522012-12-22 17:24:44378 TestingPrefServiceSimple prefs_;
[email protected]96a5c342012-12-04 18:14:02379 MockPrefChangeCallback observer_;
[email protected]ecde2742010-04-02 17:36:18380};
[email protected]ddd231e2010-06-29 20:35:19381
[email protected]12a3c022010-11-03 10:24:11382const char PrefServiceSetValueTest::kName[] = "name";
383const char PrefServiceSetValueTest::kValue[] = "value";
[email protected]ecde2742010-04-02 17:36:18384
385TEST_F(PrefServiceSetValueTest, SetStringValue) {
[email protected]20ce516d2010-06-18 02:20:04386 const char default_string[] = "default";
jdoerrie122c4da2017-03-06 11:12:04387 const base::Value default_value(default_string);
[email protected]b1de2c72013-02-06 02:45:47388 prefs_.registry()->RegisterStringPref(kName, default_string);
[email protected]2fb7dc982010-09-29 12:24:28389
390 PrefChangeRegistrar registrar;
391 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02392 registrar.Add(kName, observer_.GetCallback());
[email protected]2fb7dc982010-09-29 12:24:28393
[email protected]c3b54f372010-09-14 08:25:07394 // Changing the controlling store from default to user triggers notification.
[email protected]96a5c342012-12-04 18:14:02395 observer_.Expect(kName, &default_value);
[email protected]acd78969c2010-12-08 09:49:11396 prefs_.Set(kName, default_value);
[email protected]c3b54f372010-09-14 08:25:07397 Mock::VerifyAndClearExpectations(&observer_);
398
[email protected]96a5c342012-12-04 18:14:02399 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11400 prefs_.Set(kName, default_value);
[email protected]ecde2742010-04-02 17:36:18401 Mock::VerifyAndClearExpectations(&observer_);
402
jdoerrie122c4da2017-03-06 11:12:04403 base::Value new_value(kValue);
[email protected]96a5c342012-12-04 18:14:02404 observer_.Expect(kName, &new_value);
[email protected]acd78969c2010-12-08 09:49:11405 prefs_.Set(kName, new_value);
406 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18407}
408
409TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
[email protected]b1de2c72013-02-06 02:45:47410 prefs_.registry()->RegisterDictionaryPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28411 PrefChangeRegistrar registrar;
412 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02413 registrar.Add(kName, observer_.GetCallback());
[email protected]ecde2742010-04-02 17:36:18414
[email protected]96a5c342012-12-04 18:14:02415 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33416 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18417 Mock::VerifyAndClearExpectations(&observer_);
418
[email protected]a43a667b2013-06-14 17:56:08419 base::DictionaryValue new_value;
[email protected]12a3c022010-11-03 10:24:11420 new_value.SetString(kName, kValue);
[email protected]96a5c342012-12-04 18:14:02421 observer_.Expect(kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11422 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18423 Mock::VerifyAndClearExpectations(&observer_);
424
[email protected]96a5c342012-12-04 18:14:02425 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11426 prefs_.Set(kName, new_value);
427 Mock::VerifyAndClearExpectations(&observer_);
428
[email protected]a43a667b2013-06-14 17:56:08429 base::DictionaryValue empty;
[email protected]96a5c342012-12-04 18:14:02430 observer_.Expect(kName, &empty);
[email protected]9a8c4022011-01-25 14:25:33431 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18432 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18433}
434
435TEST_F(PrefServiceSetValueTest, SetListValue) {
[email protected]b1de2c72013-02-06 02:45:47436 prefs_.registry()->RegisterListPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28437 PrefChangeRegistrar registrar;
438 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02439 registrar.Add(kName, observer_.GetCallback());
[email protected]ecde2742010-04-02 17:36:18440
[email protected]96a5c342012-12-04 18:14:02441 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33442 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18443 Mock::VerifyAndClearExpectations(&observer_);
444
[email protected]a43a667b2013-06-14 17:56:08445 base::ListValue new_value;
dcheng58241a812016-06-03 18:18:42446 new_value.AppendString(kValue);
[email protected]96a5c342012-12-04 18:14:02447 observer_.Expect(kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11448 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18449 Mock::VerifyAndClearExpectations(&observer_);
450
[email protected]96a5c342012-12-04 18:14:02451 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11452 prefs_.Set(kName, new_value);
453 Mock::VerifyAndClearExpectations(&observer_);
454
[email protected]a43a667b2013-06-14 17:56:08455 base::ListValue empty;
[email protected]96a5c342012-12-04 18:14:02456 observer_.Expect(kName, &empty);
[email protected]9a8c4022011-01-25 14:25:33457 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18458 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18459}