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> |
| 6 #include <stdint.h> |
| 7 |
5 #include <string> | 8 #include <string> |
6 | 9 |
| 10 #include "base/macros.h" |
7 #include "base/prefs/json_pref_store.h" | 11 #include "base/prefs/json_pref_store.h" |
8 #include "base/prefs/mock_pref_change_callback.h" | 12 #include "base/prefs/mock_pref_change_callback.h" |
9 #include "base/prefs/pref_change_registrar.h" | 13 #include "base/prefs/pref_change_registrar.h" |
10 #include "base/prefs/pref_registry_simple.h" | 14 #include "base/prefs/pref_registry_simple.h" |
11 #include "base/prefs/pref_service_factory.h" | 15 #include "base/prefs/pref_service_factory.h" |
12 #include "base/prefs/pref_value_store.h" | 16 #include "base/prefs/pref_value_store.h" |
13 #include "base/prefs/testing_pref_service.h" | 17 #include "base/prefs/testing_pref_service.h" |
14 #include "base/prefs/testing_pref_store.h" | 18 #include "base/prefs/testing_pref_store.h" |
15 #include "base/values.h" | 19 #include "base/values.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 231 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
228 EXPECT_EQ(kRecommendedValue, actual_int_value); | 232 EXPECT_EQ(kRecommendedValue, actual_int_value); |
229 } | 233 } |
230 | 234 |
231 // A PrefStore which just stores the last write flags that were used to write | 235 // A PrefStore which just stores the last write flags that were used to write |
232 // values to it. | 236 // values to it. |
233 class WriteFlagChecker : public TestingPrefStore { | 237 class WriteFlagChecker : public TestingPrefStore { |
234 public: | 238 public: |
235 WriteFlagChecker() {} | 239 WriteFlagChecker() {} |
236 | 240 |
237 void ReportValueChanged(const std::string& key, uint32 flags) override { | 241 void ReportValueChanged(const std::string& key, uint32_t flags) override { |
238 SetLastWriteFlags(flags); | 242 SetLastWriteFlags(flags); |
239 } | 243 } |
240 | 244 |
241 void SetValue(const std::string& key, | 245 void SetValue(const std::string& key, |
242 scoped_ptr<base::Value> value, | 246 scoped_ptr<base::Value> value, |
243 uint32 flags) override { | 247 uint32_t flags) override { |
244 SetLastWriteFlags(flags); | 248 SetLastWriteFlags(flags); |
245 } | 249 } |
246 | 250 |
247 void SetValueSilently(const std::string& key, | 251 void SetValueSilently(const std::string& key, |
248 scoped_ptr<base::Value> value, | 252 scoped_ptr<base::Value> value, |
249 uint32 flags) override { | 253 uint32_t flags) override { |
250 SetLastWriteFlags(flags); | 254 SetLastWriteFlags(flags); |
251 } | 255 } |
252 | 256 |
253 void RemoveValue(const std::string& key, uint32 flags) override { | 257 void RemoveValue(const std::string& key, uint32_t flags) override { |
254 SetLastWriteFlags(flags); | 258 SetLastWriteFlags(flags); |
255 } | 259 } |
256 | 260 |
257 uint32 GetLastFlagsAndClear() { | 261 uint32_t GetLastFlagsAndClear() { |
258 CHECK(last_write_flags_set_); | 262 CHECK(last_write_flags_set_); |
259 uint32 result = last_write_flags_; | 263 uint32_t result = last_write_flags_; |
260 last_write_flags_set_ = false; | 264 last_write_flags_set_ = false; |
261 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; | 265 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; |
262 return result; | 266 return result; |
263 } | 267 } |
264 | 268 |
265 bool last_write_flags_set() { return last_write_flags_set_; } | 269 bool last_write_flags_set() { return last_write_flags_set_; } |
266 | 270 |
267 private: | 271 private: |
268 ~WriteFlagChecker() override {} | 272 ~WriteFlagChecker() override {} |
269 | 273 |
270 void SetLastWriteFlags(uint32 flags) { | 274 void SetLastWriteFlags(uint32_t flags) { |
271 CHECK(!last_write_flags_set_); | 275 CHECK(!last_write_flags_set_); |
272 last_write_flags_set_ = true; | 276 last_write_flags_set_ = true; |
273 last_write_flags_ = flags; | 277 last_write_flags_ = flags; |
274 } | 278 } |
275 | 279 |
276 bool last_write_flags_set_ = false; | 280 bool last_write_flags_set_ = false; |
277 uint32 last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; | 281 uint32_t last_write_flags_ = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; |
278 }; | 282 }; |
279 | 283 |
280 TEST(PrefServiceTest, WriteablePrefStoreFlags) { | 284 TEST(PrefServiceTest, WriteablePrefStoreFlags) { |
281 scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker); | 285 scoped_refptr<WriteFlagChecker> flag_checker(new WriteFlagChecker); |
282 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple); | 286 scoped_refptr<PrefRegistrySimple> registry(new PrefRegistrySimple); |
283 base::PrefServiceFactory factory; | 287 base::PrefServiceFactory factory; |
284 factory.set_user_prefs(flag_checker); | 288 factory.set_user_prefs(flag_checker); |
285 scoped_ptr<PrefService> prefs(factory.Create(registry.get())); | 289 scoped_ptr<PrefService> prefs(factory.Create(registry.get())); |
286 | 290 |
287 // The first 8 bits of write flags are reserved for subclasses. Create a | 291 // The first 8 bits of write flags are reserved for subclasses. Create a |
288 // custom flag in this range | 292 // custom flag in this range |
289 uint32 kCustomRegistrationFlag = 1 << 2; | 293 uint32_t kCustomRegistrationFlag = 1 << 2; |
290 | 294 |
291 // A map of the registration flags that will be tested and the write flags | 295 // A map of the registration flags that will be tested and the write flags |
292 // they are expected to convert to. | 296 // they are expected to convert to. |
293 struct RegistrationToWriteFlags { | 297 struct RegistrationToWriteFlags { |
294 const char* pref_name; | 298 const char* pref_name; |
295 uint32 registration_flags; | 299 uint32_t registration_flags; |
296 uint32 write_flags; | 300 uint32_t write_flags; |
297 }; | 301 }; |
298 const RegistrationToWriteFlags kRegistrationToWriteFlags[] = { | 302 const RegistrationToWriteFlags kRegistrationToWriteFlags[] = { |
299 {"none", | 303 {"none", |
300 PrefRegistry::NO_REGISTRATION_FLAGS, | 304 PrefRegistry::NO_REGISTRATION_FLAGS, |
301 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS}, | 305 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS}, |
302 {"lossy", | 306 {"lossy", |
303 PrefRegistry::LOSSY_PREF, | 307 PrefRegistry::LOSSY_PREF, |
304 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}, | 308 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG}, |
305 {"custom", | 309 {"custom", |
306 kCustomRegistrationFlag, | 310 kCustomRegistrationFlag, |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 | 421 |
418 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); | 422 EXPECT_CALL(observer_, OnPreferenceChanged(_)).Times(0); |
419 prefs_.Set(kName, new_value); | 423 prefs_.Set(kName, new_value); |
420 Mock::VerifyAndClearExpectations(&observer_); | 424 Mock::VerifyAndClearExpectations(&observer_); |
421 | 425 |
422 base::ListValue empty; | 426 base::ListValue empty; |
423 observer_.Expect(kName, &empty); | 427 observer_.Expect(kName, &empty); |
424 prefs_.Set(kName, empty); | 428 prefs_.Set(kName, empty); |
425 Mock::VerifyAndClearExpectations(&observer_); | 429 Mock::VerifyAndClearExpectations(&observer_); |
426 } | 430 } |
OLD | NEW |