blob: 55d3ccce05549f21c399e23b9430dd184150fd5c [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"
[email protected]ecde2742010-04-02 17:36:1811#include "base/values.h"
brettwf00b9b42016-02-01 22:11:3812#include "components/prefs/json_pref_store.h"
13#include "components/prefs/mock_pref_change_callback.h"
14#include "components/prefs/pref_change_registrar.h"
15#include "components/prefs/pref_registry_simple.h"
16#include "components/prefs/pref_service_factory.h"
17#include "components/prefs/pref_value_store.h"
18#include "components/prefs/testing_pref_service.h"
19#include "components/prefs/testing_pref_store.h"
[email protected]ecde2742010-04-02 17:36:1820#include "testing/gmock/include/gmock/gmock.h"
initial.commit09911bf2008-07-26 23:55:2921#include "testing/gtest/include/gtest/gtest.h"
22
[email protected]ecde2742010-04-02 17:36:1823using testing::_;
24using testing::Mock;
[email protected]12a3c022010-11-03 10:24:1125
[email protected]aaa552312013-02-13 16:25:4026const char kPrefName[] = "pref.name";
27
[email protected]277404c22010-04-22 13:09:4528TEST(PrefServiceTest, NoObserverFire) {
[email protected]5b199522012-12-22 17:24:4429 TestingPrefServiceSimple prefs;
[email protected]7aa0a962010-04-21 17:24:4230
[email protected]57ecc4b2010-08-11 03:02:5131 const char pref_name[] = "homepage";
[email protected]b1de2c72013-02-06 02:45:4732 prefs.registry()->RegisterStringPref(pref_name, std::string());
[email protected]7aa0a962010-04-21 17:24:4233
[email protected]acd78969c2010-12-08 09:49:1134 const char new_pref_value[] = "http://www.google.com/";
[email protected]96a5c342012-12-04 18:14:0235 MockPrefChangeCallback obs(&prefs);
[email protected]2fb7dc982010-09-29 12:24:2836 PrefChangeRegistrar registrar;
37 registrar.Init(&prefs);
[email protected]96a5c342012-12-04 18:14:0238 registrar.Add(pref_name, obs.GetCallback());
[email protected]7aa0a962010-04-21 17:24:4239
[email protected]96a5c342012-12-04 18:14:0240 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
jdoerrie122c4da2017-03-06 11:12:0441 const base::Value expected_value(new_pref_value);
[email protected]96a5c342012-12-04 18:14:0242 obs.Expect(pref_name, &expected_value);
[email protected]acd78969c2010-12-08 09:49:1143 prefs.SetString(pref_name, new_pref_value);
44 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4245
46 // Setting the pref to the same value should not set the pref value a second
47 // time.
[email protected]96a5c342012-12-04 18:14:0248 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
[email protected]7aa0a962010-04-21 17:24:4249 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:1150 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4251
52 // Clearing the pref should cause the pref to fire.
jdoerrie122c4da2017-03-06 11:12:0453 const base::Value expected_default_value((std::string()));
[email protected]96a5c342012-12-04 18:14:0254 obs.Expect(pref_name, &expected_default_value);
[email protected]7aa0a962010-04-21 17:24:4255 prefs.ClearPref(pref_name);
[email protected]acd78969c2010-12-08 09:49:1156 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4257
58 // Clearing the pref again should not cause the pref to fire.
[email protected]96a5c342012-12-04 18:14:0259 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
[email protected]7aa0a962010-04-21 17:24:4260 prefs.ClearPref(pref_name);
[email protected]acd78969c2010-12-08 09:49:1161 Mock::VerifyAndClearExpectations(&obs);
[email protected]7aa0a962010-04-21 17:24:4262}
63
[email protected]277404c22010-04-22 13:09:4564TEST(PrefServiceTest, HasPrefPath) {
[email protected]5b199522012-12-22 17:24:4465 TestingPrefServiceSimple prefs;
[email protected]7aa0a962010-04-21 17:24:4266
[email protected]57ecc4b2010-08-11 03:02:5167 const char path[] = "fake.path";
[email protected]7aa0a962010-04-21 17:24:4268
69 // Shouldn't initially have a path.
70 EXPECT_FALSE(prefs.HasPrefPath(path));
71
72 // Register the path. This doesn't set a value, so the path still shouldn't
73 // exist.
[email protected]b1de2c72013-02-06 02:45:4774 prefs.registry()->RegisterStringPref(path, std::string());
[email protected]7aa0a962010-04-21 17:24:4275 EXPECT_FALSE(prefs.HasPrefPath(path));
76
77 // Set a value and make sure we have a path.
[email protected]ddd231e2010-06-29 20:35:1978 prefs.SetString(path, "blah");
[email protected]7aa0a962010-04-21 17:24:4279 EXPECT_TRUE(prefs.HasPrefPath(path));
80}
81
[email protected]277404c22010-04-22 13:09:4582TEST(PrefServiceTest, Observers) {
[email protected]57ecc4b2010-08-11 03:02:5183 const char pref_name[] = "homepage";
[email protected]277404c22010-04-22 13:09:4584
[email protected]5b199522012-12-22 17:24:4485 TestingPrefServiceSimple prefs;
jdoerrie122c4da2017-03-06 11:12:0486 prefs.SetUserPref(pref_name, new base::Value("http://www.cnn.com"));
[email protected]b1de2c72013-02-06 02:45:4787 prefs.registry()->RegisterStringPref(pref_name, std::string());
[email protected]277404c22010-04-22 13:09:4588
[email protected]acd78969c2010-12-08 09:49:1189 const char new_pref_value[] = "http://www.google.com/";
jdoerrie122c4da2017-03-06 11:12:0490 const base::Value expected_new_pref_value(new_pref_value);
[email protected]96a5c342012-12-04 18:14:0291 MockPrefChangeCallback obs(&prefs);
[email protected]2fb7dc982010-09-29 12:24:2892 PrefChangeRegistrar registrar;
93 registrar.Init(&prefs);
[email protected]96a5c342012-12-04 18:14:0294 registrar.Add(pref_name, obs.GetCallback());
[email protected]277404c22010-04-22 13:09:4595
[email protected]54ffd94a2012-11-12 15:29:2096 PrefChangeRegistrar registrar_two;
97 registrar_two.Init(&prefs);
98
[email protected]96a5c342012-12-04 18:14:0299 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
100 obs.Expect(pref_name, &expected_new_pref_value);
[email protected]acd78969c2010-12-08 09:49:11101 prefs.SetString(pref_name, new_pref_value);
102 Mock::VerifyAndClearExpectations(&obs);
[email protected]277404c22010-04-22 13:09:45103
104 // Now try adding a second pref observer.
[email protected]acd78969c2010-12-08 09:49:11105 const char new_pref_value2[] = "http://www.youtube.com/";
jdoerrie122c4da2017-03-06 11:12:04106 const base::Value expected_new_pref_value2(new_pref_value2);
[email protected]96a5c342012-12-04 18:14:02107 MockPrefChangeCallback obs2(&prefs);
108 obs.Expect(pref_name, &expected_new_pref_value2);
109 obs2.Expect(pref_name, &expected_new_pref_value2);
110 registrar_two.Add(pref_name, obs2.GetCallback());
[email protected]277404c22010-04-22 13:09:45111 // This should fire the checks in obs and obs2.
112 prefs.SetString(pref_name, new_pref_value2);
[email protected]acd78969c2010-12-08 09:49:11113 Mock::VerifyAndClearExpectations(&obs);
114 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45115
[email protected]7ca0f362012-07-30 10:14:03116 // Set a recommended value.
jdoerrie122c4da2017-03-06 11:12:04117 const base::Value recommended_pref_value("http://www.gmail.com/");
[email protected]96a5c342012-12-04 18:14:02118 obs.Expect(pref_name, &expected_new_pref_value2);
119 obs2.Expect(pref_name, &expected_new_pref_value2);
[email protected]7ca0f362012-07-30 10:14:03120 // 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.
122 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy());
123 Mock::VerifyAndClearExpectations(&obs);
124 Mock::VerifyAndClearExpectations(&obs2);
125
[email protected]277404c22010-04-22 13:09:45126 // Make sure obs2 still works after removing obs.
[email protected]54ffd94a2012-11-12 15:29:20127 registrar.Remove(pref_name);
[email protected]96a5c342012-12-04 18:14:02128 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
129 obs2.Expect(pref_name, &expected_new_pref_value);
[email protected]277404c22010-04-22 13:09:45130 // This should only fire the observer in obs2.
131 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:11132 Mock::VerifyAndClearExpectations(&obs);
133 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45134}
135
[email protected]9a8c4022011-01-25 14:25:33136// 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.
138TEST(PrefServiceTest, GetValueChangedType) {
139 const int kTestValue = 10;
[email protected]5b199522012-12-22 17:24:44140 TestingPrefServiceSimple prefs;
[email protected]aaa552312013-02-13 16:25:40141 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue);
[email protected]9a8c4022011-01-25 14:25:33142
143 // Check falling back to a recommended value.
jdoerrie122c4da2017-03-06 11:12:04144 prefs.SetUserPref(kPrefName, new base::Value("not an integer"));
[email protected]aaa552312013-02-13 16:25:40145 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
[email protected]9a8c4022011-01-25 14:25:33146 ASSERT_TRUE(pref);
[email protected]a43a667b2013-06-14 17:56:08147 const base::Value* value = pref->GetValue();
[email protected]9a8c4022011-01-25 14:25:33148 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28149 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]9a8c4022011-01-25 14:25:33150 int actual_int_value = -1;
151 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
152 EXPECT_EQ(kTestValue, actual_int_value);
153}
154
[email protected]7ca0f362012-07-30 10:14:03155TEST(PrefServiceTest, GetValueAndGetRecommendedValue) {
156 const int kDefaultValue = 5;
157 const int kUserValue = 10;
158 const int kRecommendedValue = 15;
[email protected]5b199522012-12-22 17:24:44159 TestingPrefServiceSimple prefs;
[email protected]aaa552312013-02-13 16:25:40160 prefs.registry()->RegisterIntegerPref(kPrefName, kDefaultValue);
[email protected]7ca0f362012-07-30 10:14:03161
162 // Create pref with a default value only.
[email protected]aaa552312013-02-13 16:25:40163 const PrefService::Preference* pref = prefs.FindPreference(kPrefName);
[email protected]7ca0f362012-07-30 10:14:03164 ASSERT_TRUE(pref);
165
166 // Check that GetValue() returns the default value.
[email protected]a43a667b2013-06-14 17:56:08167 const base::Value* value = pref->GetValue();
[email protected]7ca0f362012-07-30 10:14:03168 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28169 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03170 int actual_int_value = -1;
171 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
172 EXPECT_EQ(kDefaultValue, actual_int_value);
173
174 // Check that GetRecommendedValue() returns no value.
175 value = pref->GetRecommendedValue();
176 ASSERT_FALSE(value);
177
178 // Set a user-set value.
jdoerrie239723572017-03-02 12:09:19179 prefs.SetUserPref(kPrefName, new base::Value(kUserValue));
[email protected]7ca0f362012-07-30 10:14:03180
181 // Check that GetValue() returns the user-set value.
182 value = pref->GetValue();
183 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28184 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03185 actual_int_value = -1;
186 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
187 EXPECT_EQ(kUserValue, actual_int_value);
188
189 // Check that GetRecommendedValue() returns no value.
190 value = pref->GetRecommendedValue();
191 ASSERT_FALSE(value);
192
193 // Set a recommended value.
jdoerrie239723572017-03-02 12:09:19194 prefs.SetRecommendedPref(kPrefName, new base::Value(kRecommendedValue));
[email protected]7ca0f362012-07-30 10:14:03195
196 // Check that GetValue() returns the user-set value.
197 value = pref->GetValue();
198 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28199 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03200 actual_int_value = -1;
201 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
202 EXPECT_EQ(kUserValue, actual_int_value);
203
204 // Check that GetRecommendedValue() returns the recommended value.
205 value = pref->GetRecommendedValue();
206 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28207 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03208 actual_int_value = -1;
209 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
210 EXPECT_EQ(kRecommendedValue, actual_int_value);
211
212 // Remove the user-set value.
[email protected]aaa552312013-02-13 16:25:40213 prefs.RemoveUserPref(kPrefName);
[email protected]7ca0f362012-07-30 10:14:03214
215 // Check that GetValue() returns the recommended value.
216 value = pref->GetValue();
217 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28218 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03219 actual_int_value = -1;
220 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
221 EXPECT_EQ(kRecommendedValue, actual_int_value);
222
223 // Check that GetRecommendedValue() returns the recommended value.
224 value = pref->GetRecommendedValue();
225 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28226 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03227 actual_int_value = -1;
228 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
229 EXPECT_EQ(kRecommendedValue, actual_int_value);
230}
231
raymesf3a929b02015-05-07 03:54:45232// A PrefStore which just stores the last write flags that were used to write
233// values to it.
234class WriteFlagChecker : public TestingPrefStore {
235 public:
236 WriteFlagChecker() {}
237
avi9ef8bb02015-12-24 05:29:36238 void ReportValueChanged(const std::string& key, uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45239 SetLastWriteFlags(flags);
240 }
241
242 void SetValue(const std::string& key,
dcheng5f043bc2016-04-22 19:09:06243 std::unique_ptr<base::Value> value,
avi9ef8bb02015-12-24 05:29:36244 uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45245 SetLastWriteFlags(flags);
raymesf3a929b02015-05-07 03:54:45246 }
247
248 void SetValueSilently(const std::string& key,
dcheng5f043bc2016-04-22 19:09:06249 std::unique_ptr<base::Value> value,
avi9ef8bb02015-12-24 05:29:36250 uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45251 SetLastWriteFlags(flags);
raymesf3a929b02015-05-07 03:54:45252 }
253
avi9ef8bb02015-12-24 05:29:36254 void RemoveValue(const std::string& key, uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45255 SetLastWriteFlags(flags);
256 }
257
avi9ef8bb02015-12-24 05:29:36258 uint32_t GetLastFlagsAndClear() {
raymesf3a929b02015-05-07 03:54:45259 CHECK(last_write_flags_set_);
avi9ef8bb02015-12-24 05:29:36260 uint32_t result = last_write_flags_;
raymesf3a929b02015-05-07 03:54:45261 last_write_flags_set_ = false;
262 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
263 return result;
264 }
265
266 bool last_write_flags_set() { return last_write_flags_set_; }
267
268 private:
269 ~WriteFlagChecker() override {}
270
avi9ef8bb02015-12-24 05:29:36271 void SetLastWriteFlags(uint32_t flags) {
raymesf3a929b02015-05-07 03:54:45272 CHECK(!last_write_flags_set_);
273 last_write_flags_set_ = true;
274 last_write_flags_ = flags;
275 }
276
277 bool last_write_flags_set_ = false;
avi9ef8bb02015-12-24 05:29:36278 uint32_t last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
raymesf3a929b02015-05-07 03:54:45279};
280
281TEST(PrefServiceTest, WriteablePrefStoreFlags) {
282 scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker);
283 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple);
brettw066508682016-02-03 08:22:02284 PrefServiceFactory factory;
raymesf3a929b02015-05-07 03:54:45285 factory.set_user_prefs(flag_checker);
dcheng5f043bc2016-04-22 19:09:06286 std::unique_ptr<PrefService> prefs(factory.Create(registry.get()));
raymesf3a929b02015-05-07 03:54:45287
288 // The first 8 bits of write flags are reserved for subclasses. Create a
289 // custom flag in this range
avi9ef8bb02015-12-24 05:29:36290 uint32_t kCustomRegistrationFlag = 1 << 2;
raymesf3a929b02015-05-07 03:54:45291
292 // A map of the registration flags that will be tested and the write flags
293 // they are expected to convert to.
294 struct RegistrationToWriteFlags {
295 const char* pref_name;
avi9ef8bb02015-12-24 05:29:36296 uint32_t registration_flags;
297 uint32_t write_flags;
raymesf3a929b02015-05-07 03:54:45298 };
299 const RegistrationToWriteFlags kRegistrationToWriteFlags[] = {
300 {"none",
301 PrefRegistry::NO_REGISTRATION_FLAGS,
302 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
303 {"lossy",
304 PrefRegistry::LOSSY_PREF,
305 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG},
306 {"custom",
307 kCustomRegistrationFlag,
308 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
309 {"lossyandcustom",
310 PrefRegistry::LOSSY_PREF | kCustomRegistrationFlag,
311 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}};
312
313 for (size_t i = 0; i < arraysize(kRegistrationToWriteFlags); ++i) {
314 RegistrationToWriteFlags entry = kRegistrationToWriteFlags[i];
315 registry->RegisterDictionaryPref(
316 entry.pref_name, new base::DictionaryValue(), entry.registration_flags);
317
318 SCOPED_TRACE("Currently testing pref with name: " +
319 std::string(entry.pref_name));
320
jdoerriedc72ee942016-12-07 15:43:28321 prefs->GetMutableUserPref(entry.pref_name, base::Value::Type::DICTIONARY);
raymesf3a929b02015-05-07 03:54:45322 EXPECT_TRUE(flag_checker->last_write_flags_set());
323 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
324
325 prefs->ReportUserPrefChanged(entry.pref_name);
326 EXPECT_TRUE(flag_checker->last_write_flags_set());
327 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
328
329 prefs->ClearPref(entry.pref_name);
330 EXPECT_TRUE(flag_checker->last_write_flags_set());
331 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
332
333 prefs->SetUserPrefValue(entry.pref_name, new base::DictionaryValue());
334 EXPECT_TRUE(flag_checker->last_write_flags_set());
335 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
336 }
337}
338
[email protected]ecde2742010-04-02 17:36:18339class PrefServiceSetValueTest : public testing::Test {
340 protected:
[email protected]12a3c022010-11-03 10:24:11341 static const char kName[];
342 static const char kValue[];
[email protected]ecde2742010-04-02 17:36:18343
[email protected]96a5c342012-12-04 18:14:02344 PrefServiceSetValueTest() : observer_(&prefs_) {}
345
[email protected]5b199522012-12-22 17:24:44346 TestingPrefServiceSimple prefs_;
[email protected]96a5c342012-12-04 18:14:02347 MockPrefChangeCallback observer_;
[email protected]ecde2742010-04-02 17:36:18348};
[email protected]ddd231e2010-06-29 20:35:19349
[email protected]12a3c022010-11-03 10:24:11350const char PrefServiceSetValueTest::kName[] = "name";
351const char PrefServiceSetValueTest::kValue[] = "value";
[email protected]ecde2742010-04-02 17:36:18352
353TEST_F(PrefServiceSetValueTest, SetStringValue) {
[email protected]20ce516d2010-06-18 02:20:04354 const char default_string[] = "default";
jdoerrie122c4da2017-03-06 11:12:04355 const base::Value default_value(default_string);
[email protected]b1de2c72013-02-06 02:45:47356 prefs_.registry()->RegisterStringPref(kName, default_string);
[email protected]2fb7dc982010-09-29 12:24:28357
358 PrefChangeRegistrar registrar;
359 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02360 registrar.Add(kName, observer_.GetCallback());
[email protected]2fb7dc982010-09-29 12:24:28361
[email protected]c3b54f372010-09-14 08:25:07362 // Changing the controlling store from default to user triggers notification.
[email protected]96a5c342012-12-04 18:14:02363 observer_.Expect(kName, &default_value);
[email protected]acd78969c2010-12-08 09:49:11364 prefs_.Set(kName, default_value);
[email protected]c3b54f372010-09-14 08:25:07365 Mock::VerifyAndClearExpectations(&observer_);
366
[email protected]96a5c342012-12-04 18:14:02367 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11368 prefs_.Set(kName, default_value);
[email protected]ecde2742010-04-02 17:36:18369 Mock::VerifyAndClearExpectations(&observer_);
370
jdoerrie122c4da2017-03-06 11:12:04371 base::Value new_value(kValue);
[email protected]96a5c342012-12-04 18:14:02372 observer_.Expect(kName, &new_value);
[email protected]acd78969c2010-12-08 09:49:11373 prefs_.Set(kName, new_value);
374 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18375}
376
377TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
[email protected]b1de2c72013-02-06 02:45:47378 prefs_.registry()->RegisterDictionaryPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28379 PrefChangeRegistrar registrar;
380 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02381 registrar.Add(kName, observer_.GetCallback());
[email protected]ecde2742010-04-02 17:36:18382
[email protected]96a5c342012-12-04 18:14:02383 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33384 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18385 Mock::VerifyAndClearExpectations(&observer_);
386
[email protected]a43a667b2013-06-14 17:56:08387 base::DictionaryValue new_value;
[email protected]12a3c022010-11-03 10:24:11388 new_value.SetString(kName, kValue);
[email protected]96a5c342012-12-04 18:14:02389 observer_.Expect(kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11390 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18391 Mock::VerifyAndClearExpectations(&observer_);
392
[email protected]96a5c342012-12-04 18:14:02393 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11394 prefs_.Set(kName, new_value);
395 Mock::VerifyAndClearExpectations(&observer_);
396
[email protected]a43a667b2013-06-14 17:56:08397 base::DictionaryValue empty;
[email protected]96a5c342012-12-04 18:14:02398 observer_.Expect(kName, &empty);
[email protected]9a8c4022011-01-25 14:25:33399 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18400 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18401}
402
403TEST_F(PrefServiceSetValueTest, SetListValue) {
[email protected]b1de2c72013-02-06 02:45:47404 prefs_.registry()->RegisterListPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28405 PrefChangeRegistrar registrar;
406 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02407 registrar.Add(kName, observer_.GetCallback());
[email protected]ecde2742010-04-02 17:36:18408
[email protected]96a5c342012-12-04 18:14:02409 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33410 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18411 Mock::VerifyAndClearExpectations(&observer_);
412
[email protected]a43a667b2013-06-14 17:56:08413 base::ListValue new_value;
dcheng58241a812016-06-03 18:18:42414 new_value.AppendString(kValue);
[email protected]96a5c342012-12-04 18:14:02415 observer_.Expect(kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11416 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18417 Mock::VerifyAndClearExpectations(&observer_);
418
[email protected]96a5c342012-12-04 18:14:02419 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11420 prefs_.Set(kName, new_value);
421 Mock::VerifyAndClearExpectations(&observer_);
422
[email protected]a43a667b2013-06-14 17:56:08423 base::ListValue empty;
[email protected]96a5c342012-12-04 18:14:02424 observer_.Expect(kName, &empty);
[email protected]9a8c4022011-01-25 14:25:33425 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18426 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18427}