testing_pref_store.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_PREFS_TESTING_PREF_STORE_H_
  5. #define COMPONENTS_PREFS_TESTING_PREF_STORE_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/compiler_specific.h"
  9. #include "base/observer_list.h"
  10. #include "base/values.h"
  11. #include "components/prefs/persistent_pref_store.h"
  12. #include "components/prefs/pref_value_map.h"
  13. // |TestingPrefStore| is a preference store implementation that allows tests to
  14. // explicitly manipulate the contents of the store, triggering notifications
  15. // where appropriate.
  16. class TestingPrefStore : public PersistentPrefStore {
  17. public:
  18. TestingPrefStore();
  19. TestingPrefStore(const TestingPrefStore&) = delete;
  20. TestingPrefStore& operator=(const TestingPrefStore&) = delete;
  21. // Overriden from PrefStore.
  22. bool GetValue(const std::string& key,
  23. const base::Value** result) const override;
  24. base::Value::Dict GetValues() const override;
  25. void AddObserver(PrefStore::Observer* observer) override;
  26. void RemoveObserver(PrefStore::Observer* observer) override;
  27. bool HasObservers() const override;
  28. bool IsInitializationComplete() const override;
  29. // PersistentPrefStore overrides:
  30. bool GetMutableValue(const std::string& key, base::Value** result) override;
  31. void ReportValueChanged(const std::string& key, uint32_t flags) override;
  32. void SetValue(const std::string& key,
  33. std::unique_ptr<base::Value> value,
  34. uint32_t flags) override;
  35. void SetValueSilently(const std::string& key,
  36. std::unique_ptr<base::Value> value,
  37. uint32_t flags) override;
  38. void RemoveValue(const std::string& key, uint32_t flags) override;
  39. void RemoveValuesByPrefixSilently(const std::string& prefix) override;
  40. bool ReadOnly() const override;
  41. PrefReadError GetReadError() const override;
  42. PersistentPrefStore::PrefReadError ReadPrefs() override;
  43. void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
  44. void CommitPendingWrite(base::OnceClosure reply_callback,
  45. base::OnceClosure synchronous_done_callback) override;
  46. void SchedulePendingLossyWrites() override;
  47. // Marks the store as having completed initialization.
  48. void SetInitializationCompleted();
  49. // Used for tests to trigger notifications explicitly.
  50. void NotifyPrefValueChanged(const std::string& key);
  51. void NotifyInitializationCompleted();
  52. // Some convenience getters/setters.
  53. void SetString(const std::string& key, const std::string& value);
  54. void SetInteger(const std::string& key, int value);
  55. void SetBoolean(const std::string& key, bool value);
  56. bool GetString(const std::string& key, std::string* value) const;
  57. bool GetInteger(const std::string& key, int* value) const;
  58. bool GetBoolean(const std::string& key, bool* value) const;
  59. // Determines whether ReadPrefsAsync completes immediately. Defaults to false
  60. // (non-blocking). To block, invoke this with true (blocking) before the call
  61. // to ReadPrefsAsync. To unblock, invoke again with false (non-blocking) after
  62. // the call to ReadPrefsAsync.
  63. void SetBlockAsyncRead(bool block_async_read);
  64. void ClearMutableValues() override;
  65. void OnStoreDeletionFromDisk() override;
  66. // Getter and Setter methods for setting and getting the state of the
  67. // |TestingPrefStore|.
  68. virtual void set_read_only(bool read_only);
  69. void set_read_success(bool read_success);
  70. void set_read_error(PersistentPrefStore::PrefReadError read_error);
  71. bool committed() { return committed_; }
  72. protected:
  73. ~TestingPrefStore() override;
  74. private:
  75. void CheckPrefIsSerializable(const std::string& key,
  76. const base::Value& value);
  77. // Stores the preference values.
  78. PrefValueMap prefs_;
  79. // Flag that indicates if the PrefStore is read-only
  80. bool read_only_;
  81. // The result to pass to PrefStore::Observer::OnInitializationCompleted
  82. bool read_success_;
  83. // The result to return from ReadPrefs or ReadPrefsAsync.
  84. PersistentPrefStore::PrefReadError read_error_;
  85. // Whether a call to ReadPrefsAsync should block.
  86. bool block_async_read_;
  87. // Whether there is a pending call to ReadPrefsAsync.
  88. bool pending_async_read_;
  89. // Whether initialization has been completed.
  90. bool init_complete_;
  91. // Whether the store contents have been committed to disk since the last
  92. // mutation.
  93. bool committed_;
  94. std::unique_ptr<ReadErrorDelegate> error_delegate_;
  95. base::ObserverList<PrefStore::Observer, true>::Unchecked observers_;
  96. };
  97. #endif // COMPONENTS_PREFS_TESTING_PREF_STORE_H_