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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/prefs/json_pref_store.h" | 7 #include "base/prefs/json_pref_store.h" |
8 #include "base/prefs/mock_pref_change_callback.h" | 8 #include "base/prefs/mock_pref_change_callback.h" |
9 #include "base/prefs/pref_change_registrar.h" | 9 #include "base/prefs/pref_change_registrar.h" |
10 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
| 11 #include "base/prefs/pref_service_factory.h" |
11 #include "base/prefs/pref_value_store.h" | 12 #include "base/prefs/pref_value_store.h" |
12 #include "base/prefs/testing_pref_service.h" | 13 #include "base/prefs/testing_pref_service.h" |
13 #include "base/prefs/testing_pref_store.h" | 14 #include "base/prefs/testing_pref_store.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 using testing::_; | 19 using testing::_; |
19 using testing::Mock; | 20 using testing::Mock; |
20 | 21 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 | 221 |
221 // Check that GetRecommendedValue() returns the recommended value. | 222 // Check that GetRecommendedValue() returns the recommended value. |
222 value = pref->GetRecommendedValue(); | 223 value = pref->GetRecommendedValue(); |
223 ASSERT_TRUE(value); | 224 ASSERT_TRUE(value); |
224 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType()); | 225 EXPECT_EQ(base::Value::TYPE_INTEGER, value->GetType()); |
225 actual_int_value = -1; | 226 actual_int_value = -1; |
226 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 227 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
227 EXPECT_EQ(kRecommendedValue, actual_int_value); | 228 EXPECT_EQ(kRecommendedValue, actual_int_value); |
228 } | 229 } |
229 | 230 |
| 231 // A PrefStore which just stores the last write flags that were used to write |
| 232 // values to it. |
| 233 class WriteFlagChecker : public TestingPrefStore { |
| 234 public: |
| 235 WriteFlagChecker() {} |
| 236 |
| 237 void ReportValueChanged(const std::string& key, uint32 flags) override { |
| 238 SetLastWriteFlags(flags); |
| 239 } |
| 240 |
| 241 void SetValue(const std::string& key, |
| 242 base::Value* value, |
| 243 uint32 flags) override { |
| 244 SetLastWriteFlags(flags); |
| 245 delete value; |
| 246 } |
| 247 |
| 248 void SetValueSilently(const std::string& key, |
| 249 base::Value* value, |
| 250 uint32 flags) override { |
| 251 SetLastWriteFlags(flags); |
| 252 delete value; |
| 253 } |
| 254 |
| 255 void RemoveValue(const std::string& key, uint32 flags) override { |
| 256 SetLastWriteFlags(flags); |
| 257 } |
| 258 |
| 259 uint32 GetLastFlagsAndClear() { |
| 260 CHECK(last_write_flags_set_); |
| 261 uint32 result = last_write_flags_; |
| 262 last_write_flags_set_ = false; |
| 263 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; |
| 264 return result; |
| 265 } |
| 266 |
| 267 bool last_write_flags_set() { return last_write_flags_set_; } |
| 268 |
| 269 private: |
| 270 ~WriteFlagChecker() override {} |
| 271 |
| 272 void SetLastWriteFlags(uint32 flags) { |
| 273 CHECK(!last_write_flags_set_); |
| 274 last_write_flags_set_ = true; |
| 275 last_write_flags_ = flags; |
| 276 } |
| 277 |
| 278 bool last_write_flags_set_ = false; |
| 279 uint32 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; |
| 280 }; |
| 281 |
| 282 TEST(PrefServiceTest, WriteablePrefStoreFlags) { |
| 283 scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker); |
| 284 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple); |
| 285 base::PrefServiceFactory factory; |
| 286 factory.set_user_prefs(flag_checker); |
| 287 scoped_ptr<PrefService> prefs(factory.Create(registry.get())); |
| 288 |
| 289 // The first 8 bits of write flags are reserved for subclasses. Create a |
| 290 // custom flag in this range |
| 291 uint32 kCustomRegistrationFlag = 1 << 2; |
| 292 |
| 293 // A map of the registration flags that will be tested and the write flags |
| 294 // they are expected to convert to. |
| 295 struct RegistrationToWriteFlags { |
| 296 const char* pref_name; |
| 297 uint32 registration_flags; |
| 298 uint32 write_flags; |
| 299 }; |
| 300 const RegistrationToWriteFlags kRegistrationToWriteFlags[] = { |
| 301 {"none", |
| 302 PrefRegistry::NO_REGISTRATION_FLAGS, |
| 303 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS}, |
| 304 {"lossy", |
| 305 PrefRegistry::LOSSY_PREF, |
| 306 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}, |
| 307 {"custom", |
| 308 kCustomRegistrationFlag, |
| 309 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS}, |
| 310 {"lossyandcustom", |
| 311 PrefRegistry::LOSSY_PREF | kCustomRegistrationFlag, |
| 312 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}}; |
| 313 |
| 314 for (size_t i = 0; i < arraysize(kRegistrationToWriteFlags); ++i) { |
| 315 RegistrationToWriteFlags entry = kRegistrationToWriteFlags[i]; |
| 316 registry->RegisterDictionaryPref( |
| 317 entry.pref_name, new base::DictionaryValue(), entry.registration_flags); |
| 318 |
| 319 SCOPED_TRACE("Currently testing pref with name: " + |
| 320 std::string(entry.pref_name)); |
| 321 |
| 322 prefs->GetMutableUserPref(entry.pref_name, base::Value::TYPE_DICTIONARY); |
| 323 EXPECT_TRUE(flag_checker->last_write_flags_set()); |
| 324 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear()); |
| 325 |
| 326 prefs->ReportUserPrefChanged(entry.pref_name); |
| 327 EXPECT_TRUE(flag_checker->last_write_flags_set()); |
| 328 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear()); |
| 329 |
| 330 prefs->ClearPref(entry.pref_name); |
| 331 EXPECT_TRUE(flag_checker->last_write_flags_set()); |
| 332 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear()); |
| 333 |
| 334 prefs->SetUserPrefValue(entry.pref_name, new base::DictionaryValue()); |
| 335 EXPECT_TRUE(flag_checker->last_write_flags_set()); |
| 336 EXPECT_EQ(entry.write_flags, flag_checker->GetLastFlagsAndClear()); |
| 337 } |
| 338 } |
| 339 |
230 class PrefServiceSetValueTest : public testing::Test { | 340 class PrefServiceSetValueTest : public testing::Test { |
231 protected: | 341 protected: |
232 static const char kName[]; | 342 static const char kName[]; |
233 static const char kValue[]; | 343 static const char kValue[]; |
234 | 344 |
235 PrefServiceSetValueTest() : observer_(&prefs_) {} | 345 PrefServiceSetValueTest() : observer_(&prefs_) {} |
236 | 346 |
237 TestingPrefServiceSimple prefs_; | 347 TestingPrefServiceSimple prefs_; |
238 MockPrefChangeCallback observer_; | 348 MockPrefChangeCallback observer_; |
239 }; | 349 }; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 | 419 |
310 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 420 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
311 prefs_.Set(kName, new_value); | 421 prefs_.Set(kName, new_value); |
312 Mock::VerifyAndClearExpectations(&observer_); | 422 Mock::VerifyAndClearExpectations(&observer_); |
313 | 423 |
314 base::ListValue empty; | 424 base::ListValue empty; |
315 observer_.Expect(kName, &empty); | 425 observer_.Expect(kName, &empty); |
316 prefs_.Set(kName, empty); | 426 prefs_.Set(kName, empty); |
317 Mock::VerifyAndClearExpectations(&observer_); | 427 Mock::VerifyAndClearExpectations(&observer_); |
318 } | 428 } |
OLD | NEW |