blob: 8c97e5e7a6d52c4baa19225b17032afceaecf824 [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.
[email protected]a43a667b2013-06-14 17:56:0841 const base::StringValue 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.
[email protected]a43a667b2013-06-14 17:56:0853 const base::StringValue 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;
[email protected]a43a667b2013-06-14 17:56:0886 prefs.SetUserPref(pref_name,
[email protected]b54e6252014-01-30 10:32:4187 new base::StringValue("http://www.cnn.com"));
[email protected]b1de2c72013-02-06 02:45:4788 prefs.registry()->RegisterStringPref(pref_name, std::string());
[email protected]277404c22010-04-22 13:09:4589
[email protected]acd78969c2010-12-08 09:49:1190 const char new_pref_value[] = "http://www.google.com/";
[email protected]a43a667b2013-06-14 17:56:0891 const base::StringValue expected_new_pref_value(new_pref_value);
[email protected]96a5c342012-12-04 18:14:0292 MockPrefChangeCallback obs(&prefs);
[email protected]2fb7dc982010-09-29 12:24:2893 PrefChangeRegistrar registrar;
94 registrar.Init(&prefs);
[email protected]96a5c342012-12-04 18:14:0295 registrar.Add(pref_name, obs.GetCallback());
[email protected]277404c22010-04-22 13:09:4596
[email protected]54ffd94a2012-11-12 15:29:2097 PrefChangeRegistrar registrar_two;
98 registrar_two.Init(&prefs);
99
[email protected]96a5c342012-12-04 18:14:02100 // This should fire the checks in MockPrefChangeCallback::OnPreferenceChanged.
101 obs.Expect(pref_name, &expected_new_pref_value);
[email protected]acd78969c2010-12-08 09:49:11102 prefs.SetString(pref_name, new_pref_value);
103 Mock::VerifyAndClearExpectations(&obs);
[email protected]277404c22010-04-22 13:09:45104
105 // Now try adding a second pref observer.
[email protected]acd78969c2010-12-08 09:49:11106 const char new_pref_value2[] = "http://www.youtube.com/";
[email protected]a43a667b2013-06-14 17:56:08107 const base::StringValue expected_new_pref_value2(new_pref_value2);
[email protected]96a5c342012-12-04 18:14:02108 MockPrefChangeCallback obs2(&prefs);
109 obs.Expect(pref_name, &expected_new_pref_value2);
110 obs2.Expect(pref_name, &expected_new_pref_value2);
111 registrar_two.Add(pref_name, obs2.GetCallback());
[email protected]277404c22010-04-22 13:09:45112 // This should fire the checks in obs and obs2.
113 prefs.SetString(pref_name, new_pref_value2);
[email protected]acd78969c2010-12-08 09:49:11114 Mock::VerifyAndClearExpectations(&obs);
115 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45116
[email protected]7ca0f362012-07-30 10:14:03117 // Set a recommended value.
[email protected]a43a667b2013-06-14 17:56:08118 const base::StringValue recommended_pref_value("http://www.gmail.com/");
[email protected]96a5c342012-12-04 18:14:02119 obs.Expect(pref_name, &expected_new_pref_value2);
120 obs2.Expect(pref_name, &expected_new_pref_value2);
[email protected]7ca0f362012-07-30 10:14:03121 // This should fire the checks in obs and obs2 but with an unchanged value
122 // as the recommended value is being overridden by the user-set value.
123 prefs.SetRecommendedPref(pref_name, recommended_pref_value.DeepCopy());
124 Mock::VerifyAndClearExpectations(&obs);
125 Mock::VerifyAndClearExpectations(&obs2);
126
[email protected]277404c22010-04-22 13:09:45127 // Make sure obs2 still works after removing obs.
[email protected]54ffd94a2012-11-12 15:29:20128 registrar.Remove(pref_name);
[email protected]96a5c342012-12-04 18:14:02129 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0);
130 obs2.Expect(pref_name, &expected_new_pref_value);
[email protected]277404c22010-04-22 13:09:45131 // This should only fire the observer in obs2.
132 prefs.SetString(pref_name, new_pref_value);
[email protected]acd78969c2010-12-08 09:49:11133 Mock::VerifyAndClearExpectations(&obs);
134 Mock::VerifyAndClearExpectations(&obs2);
[email protected]277404c22010-04-22 13:09:45135}
136
[email protected]9a8c4022011-01-25 14:25:33137// Make sure that if a preference changes type, so the wrong type is stored in
138// the user pref file, it uses the correct fallback value instead.
139TEST(PrefServiceTest, GetValueChangedType) {
140 const int kTestValue = 10;
[email protected]5b199522012-12-22 17:24:44141 TestingPrefServiceSimple prefs;
[email protected]aaa552312013-02-13 16:25:40142 prefs.registry()->RegisterIntegerPref(kPrefName, kTestValue);
[email protected]9a8c4022011-01-25 14:25:33143
144 // Check falling back to a recommended value.
[email protected]a43a667b2013-06-14 17:56:08145 prefs.SetUserPref(kPrefName,
[email protected]b54e6252014-01-30 10:32:41146 new base::StringValue("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);
jdoerriedc72ee942016-12-07 15:43:28151 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[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);
jdoerriedc72ee942016-12-07 15:43:28171 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[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.
jdoerrie239723572017-03-02 12:09:19181 prefs.SetUserPref(kPrefName, new 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);
jdoerriedc72ee942016-12-07 15:43:28186 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[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.
jdoerrie239723572017-03-02 12:09:19196 prefs.SetRecommendedPref(kPrefName, new base::Value(kRecommendedValue));
[email protected]7ca0f362012-07-30 10:14:03197
198 // Check that GetValue() returns the user-set value.
199 value = pref->GetValue();
200 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28201 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03202 actual_int_value = -1;
203 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
204 EXPECT_EQ(kUserValue, actual_int_value);
205
206 // Check that GetRecommendedValue() returns the recommended value.
207 value = pref->GetRecommendedValue();
208 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28209 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03210 actual_int_value = -1;
211 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
212 EXPECT_EQ(kRecommendedValue, actual_int_value);
213
214 // Remove the user-set value.
[email protected]aaa552312013-02-13 16:25:40215 prefs.RemoveUserPref(kPrefName);
[email protected]7ca0f362012-07-30 10:14:03216
217 // Check that GetValue() returns the recommended value.
218 value = pref->GetValue();
219 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28220 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03221 actual_int_value = -1;
222 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
223 EXPECT_EQ(kRecommendedValue, actual_int_value);
224
225 // Check that GetRecommendedValue() returns the recommended value.
226 value = pref->GetRecommendedValue();
227 ASSERT_TRUE(value);
jdoerriedc72ee942016-12-07 15:43:28228 EXPECT_EQ(base::Value::Type::INTEGER, value->GetType());
[email protected]7ca0f362012-07-30 10:14:03229 actual_int_value = -1;
230 EXPECT_TRUE(value->GetAsInteger(&actual_int_value));
231 EXPECT_EQ(kRecommendedValue, actual_int_value);
232}
233
raymesf3a929b02015-05-07 03:54:45234// A PrefStore which just stores the last write flags that were used to write
235// values to it.
236class WriteFlagChecker : public TestingPrefStore {
237 public:
238 WriteFlagChecker() {}
239
avi9ef8bb02015-12-24 05:29:36240 void ReportValueChanged(const std::string& key, uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45241 SetLastWriteFlags(flags);
242 }
243
244 void SetValue(const std::string& key,
dcheng5f043bc2016-04-22 19:09:06245 std::unique_ptr<base::Value> value,
avi9ef8bb02015-12-24 05:29:36246 uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45247 SetLastWriteFlags(flags);
raymesf3a929b02015-05-07 03:54:45248 }
249
250 void SetValueSilently(const std::string& key,
dcheng5f043bc2016-04-22 19:09:06251 std::unique_ptr<base::Value> value,
avi9ef8bb02015-12-24 05:29:36252 uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45253 SetLastWriteFlags(flags);
raymesf3a929b02015-05-07 03:54:45254 }
255
avi9ef8bb02015-12-24 05:29:36256 void RemoveValue(const std::string& key, uint32_t flags) override {
raymesf3a929b02015-05-07 03:54:45257 SetLastWriteFlags(flags);
258 }
259
avi9ef8bb02015-12-24 05:29:36260 uint32_t GetLastFlagsAndClear() {
raymesf3a929b02015-05-07 03:54:45261 CHECK(last_write_flags_set_);
avi9ef8bb02015-12-24 05:29:36262 uint32_t result = last_write_flags_;
raymesf3a929b02015-05-07 03:54:45263 last_write_flags_set_ = false;
264 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
265 return result;
266 }
267
268 bool last_write_flags_set() { return last_write_flags_set_; }
269
270 private:
271 ~WriteFlagChecker() override {}
272
avi9ef8bb02015-12-24 05:29:36273 void SetLastWriteFlags(uint32_t flags) {
raymesf3a929b02015-05-07 03:54:45274 CHECK(!last_write_flags_set_);
275 last_write_flags_set_ = true;
276 last_write_flags_ = flags;
277 }
278
279 bool last_write_flags_set_ = false;
avi9ef8bb02015-12-24 05:29:36280 uint32_t last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
raymesf3a929b02015-05-07 03:54:45281};
282
283TEST(PrefServiceTest, WriteablePrefStoreFlags) {
284 scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker);
285 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple);
brettw066508682016-02-03 08:22:02286 PrefServiceFactory factory;
raymesf3a929b02015-05-07 03:54:45287 factory.set_user_prefs(flag_checker);
dcheng5f043bc2016-04-22 19:09:06288 std::unique_ptr<PrefService> prefs(factory.Create(registry.get()));
raymesf3a929b02015-05-07 03:54:45289
290 // The first 8 bits of write flags are reserved for subclasses. Create a
291 // custom flag in this range
avi9ef8bb02015-12-24 05:29:36292 uint32_t kCustomRegistrationFlag = 1 << 2;
raymesf3a929b02015-05-07 03:54:45293
294 // A map of the registration flags that will be tested and the write flags
295 // they are expected to convert to.
296 struct RegistrationToWriteFlags {
297 const char* pref_name;
avi9ef8bb02015-12-24 05:29:36298 uint32_t registration_flags;
299 uint32_t write_flags;
raymesf3a929b02015-05-07 03:54:45300 };
301 const RegistrationToWriteFlags kRegistrationToWriteFlags[] = {
302 {"none",
303 PrefRegistry::NO_REGISTRATION_FLAGS,
304 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
305 {"lossy",
306 PrefRegistry::LOSSY_PREF,
307 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG},
308 {"custom",
309 kCustomRegistrationFlag,
310 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS},
311 {"lossyandcustom",
312 PrefRegistry::LOSSY_PREF | kCustomRegistrationFlag,
313 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}};
314
315 for (size_t i = 0; i < arraysize(kRegistrationToWriteFlags); ++i) {
316 RegistrationToWriteFlags entry = kRegistrationToWriteFlags[i];
317 registry->RegisterDictionaryPref(
318 entry.pref_name, new base::DictionaryValue(), entry.registration_flags);
319
320 SCOPED_TRACE("Currently testing pref with name: " +
321 std::string(entry.pref_name));
322
jdoerriedc72ee942016-12-07 15:43:28323 prefs->GetMutableUserPref(entry.pref_name, base::Value::Type::DICTIONARY);
raymesf3a929b02015-05-07 03:54:45324 EXPECT_TRUE(flag_checker->last_write_flags_set());
325 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
326
327 prefs->ReportUserPrefChanged(entry.pref_name);
328 EXPECT_TRUE(flag_checker->last_write_flags_set());
329 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
330
331 prefs->ClearPref(entry.pref_name);
332 EXPECT_TRUE(flag_checker->last_write_flags_set());
333 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
334
335 prefs->SetUserPrefValue(entry.pref_name, new base::DictionaryValue());
336 EXPECT_TRUE(flag_checker->last_write_flags_set());
337 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear());
338 }
339}
340
[email protected]ecde2742010-04-02 17:36:18341class PrefServiceSetValueTest : public testing::Test {
342 protected:
[email protected]12a3c022010-11-03 10:24:11343 static const char kName[];
344 static const char kValue[];
[email protected]ecde2742010-04-02 17:36:18345
[email protected]96a5c342012-12-04 18:14:02346 PrefServiceSetValueTest() : observer_(&prefs_) {}
347
[email protected]5b199522012-12-22 17:24:44348 TestingPrefServiceSimple prefs_;
[email protected]96a5c342012-12-04 18:14:02349 MockPrefChangeCallback observer_;
[email protected]ecde2742010-04-02 17:36:18350};
[email protected]ddd231e2010-06-29 20:35:19351
[email protected]12a3c022010-11-03 10:24:11352const char PrefServiceSetValueTest::kName[] = "name";
353const char PrefServiceSetValueTest::kValue[] = "value";
[email protected]ecde2742010-04-02 17:36:18354
355TEST_F(PrefServiceSetValueTest, SetStringValue) {
[email protected]20ce516d2010-06-18 02:20:04356 const char default_string[] = "default";
[email protected]a43a667b2013-06-14 17:56:08357 const base::StringValue default_value(default_string);
[email protected]b1de2c72013-02-06 02:45:47358 prefs_.registry()->RegisterStringPref(kName, default_string);
[email protected]2fb7dc982010-09-29 12:24:28359
360 PrefChangeRegistrar registrar;
361 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02362 registrar.Add(kName, observer_.GetCallback());
[email protected]2fb7dc982010-09-29 12:24:28363
[email protected]c3b54f372010-09-14 08:25:07364 // Changing the controlling store from default to user triggers notification.
[email protected]96a5c342012-12-04 18:14:02365 observer_.Expect(kName, &default_value);
[email protected]acd78969c2010-12-08 09:49:11366 prefs_.Set(kName, default_value);
[email protected]c3b54f372010-09-14 08:25:07367 Mock::VerifyAndClearExpectations(&observer_);
368
[email protected]96a5c342012-12-04 18:14:02369 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11370 prefs_.Set(kName, default_value);
[email protected]ecde2742010-04-02 17:36:18371 Mock::VerifyAndClearExpectations(&observer_);
372
[email protected]a43a667b2013-06-14 17:56:08373 base::StringValue new_value(kValue);
[email protected]96a5c342012-12-04 18:14:02374 observer_.Expect(kName, &new_value);
[email protected]acd78969c2010-12-08 09:49:11375 prefs_.Set(kName, new_value);
376 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18377}
378
379TEST_F(PrefServiceSetValueTest, SetDictionaryValue) {
[email protected]b1de2c72013-02-06 02:45:47380 prefs_.registry()->RegisterDictionaryPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28381 PrefChangeRegistrar registrar;
382 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02383 registrar.Add(kName, observer_.GetCallback());
[email protected]ecde2742010-04-02 17:36:18384
[email protected]96a5c342012-12-04 18:14:02385 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33386 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18387 Mock::VerifyAndClearExpectations(&observer_);
388
[email protected]a43a667b2013-06-14 17:56:08389 base::DictionaryValue new_value;
[email protected]12a3c022010-11-03 10:24:11390 new_value.SetString(kName, kValue);
[email protected]96a5c342012-12-04 18:14:02391 observer_.Expect(kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11392 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18393 Mock::VerifyAndClearExpectations(&observer_);
394
[email protected]96a5c342012-12-04 18:14:02395 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11396 prefs_.Set(kName, new_value);
397 Mock::VerifyAndClearExpectations(&observer_);
398
[email protected]a43a667b2013-06-14 17:56:08399 base::DictionaryValue empty;
[email protected]96a5c342012-12-04 18:14:02400 observer_.Expect(kName, &empty);
[email protected]9a8c4022011-01-25 14:25:33401 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18402 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18403}
404
405TEST_F(PrefServiceSetValueTest, SetListValue) {
[email protected]b1de2c72013-02-06 02:45:47406 prefs_.registry()->RegisterListPref(kName);
[email protected]2fb7dc982010-09-29 12:24:28407 PrefChangeRegistrar registrar;
408 registrar.Init(&prefs_);
[email protected]96a5c342012-12-04 18:14:02409 registrar.Add(kName, observer_.GetCallback());
[email protected]ecde2742010-04-02 17:36:18410
[email protected]96a5c342012-12-04 18:14:02411 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]9a8c4022011-01-25 14:25:33412 prefs_.RemoveUserPref(kName);
[email protected]ecde2742010-04-02 17:36:18413 Mock::VerifyAndClearExpectations(&observer_);
414
[email protected]a43a667b2013-06-14 17:56:08415 base::ListValue new_value;
dcheng58241a812016-06-03 18:18:42416 new_value.AppendString(kValue);
[email protected]96a5c342012-12-04 18:14:02417 observer_.Expect(kName, &new_value);
[email protected]12a3c022010-11-03 10:24:11418 prefs_.Set(kName, new_value);
[email protected]ecde2742010-04-02 17:36:18419 Mock::VerifyAndClearExpectations(&observer_);
420
[email protected]96a5c342012-12-04 18:14:02421 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0);
[email protected]acd78969c2010-12-08 09:49:11422 prefs_.Set(kName, new_value);
423 Mock::VerifyAndClearExpectations(&observer_);
424
[email protected]a43a667b2013-06-14 17:56:08425 base::ListValue empty;
[email protected]96a5c342012-12-04 18:14:02426 observer_.Expect(kName, &empty);
[email protected]9a8c4022011-01-25 14:25:33427 prefs_.Set(kName, empty);
[email protected]ecde2742010-04-02 17:36:18428 Mock::VerifyAndClearExpectations(&observer_);
[email protected]ecde2742010-04-02 17:36:18429}