1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_PREFS_TESTING_PREF_STORE_H_ 6 #define COMPONENTS_PREFS_TESTING_PREF_STORE_H_ 7 8 #include <stdint.h> 9 10 #include <optional> 11 #include <string> 12 #include <string_view> 13 14 #include "base/compiler_specific.h" 15 #include "base/observer_list.h" 16 #include "base/values.h" 17 #include "components/prefs/persistent_pref_store.h" 18 #include "components/prefs/pref_value_map.h" 19 20 // |TestingPrefStore| is a preference store implementation that allows tests to 21 // explicitly manipulate the contents of the store, triggering notifications 22 // where appropriate. 23 class TestingPrefStore : public PersistentPrefStore { 24 public: 25 TestingPrefStore(); 26 27 TestingPrefStore(const TestingPrefStore&) = delete; 28 TestingPrefStore& operator=(const TestingPrefStore&) = delete; 29 30 // Overridden from PrefStore. 31 bool GetValue(std::string_view key, 32 const base::Value** result) const override; 33 base::Value::Dict GetValues() const override; 34 void AddObserver(PrefStore::Observer* observer) override; 35 void RemoveObserver(PrefStore::Observer* observer) override; 36 bool HasObservers() const override; 37 bool IsInitializationComplete() const override; 38 39 // PersistentPrefStore overrides: 40 bool GetMutableValue(std::string_view key, base::Value** result) override; 41 void ReportValueChanged(std::string_view key, uint32_t flags) override; 42 void SetValue(std::string_view key, 43 base::Value value, 44 uint32_t flags) override; 45 void SetValueSilently(std::string_view key, 46 base::Value value, 47 uint32_t flags) override; 48 void RemoveValue(std::string_view key, uint32_t flags) override; 49 void RemoveValuesByPrefixSilently(std::string_view prefix) override; 50 bool ReadOnly() const override; 51 PrefReadError GetReadError() const override; 52 PersistentPrefStore::PrefReadError ReadPrefs() override; 53 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; 54 void CommitPendingWrite(base::OnceClosure reply_callback, 55 base::OnceClosure synchronous_done_callback) override; 56 void SchedulePendingLossyWrites() override; 57 bool HasReadErrorDelegate() const override; 58 59 // Marks the store as having completed initialization. 60 void SetInitializationCompleted(); 61 62 // Used for tests to trigger notifications explicitly. 63 void NotifyPrefValueChanged(std::string_view key); 64 void NotifyInitializationCompleted(); 65 66 // Some convenience getters/setters. 67 void SetString(const std::string& key, const std::string& value); 68 void SetInteger(const std::string& key, int value); 69 void SetBoolean(const std::string& key, bool value); 70 71 bool GetString(const std::string& key, std::string* value) const; 72 bool GetInteger(const std::string& key, int* value) const; 73 bool GetBoolean(const std::string& key, bool* value) const; 74 75 // Determines whether ReadPrefsAsync completes immediately. Defaults to false 76 // (non-blocking). To block, invoke this with true (blocking) before the call 77 // to ReadPrefsAsync. To unblock, invoke again with false (non-blocking) after 78 // the call to ReadPrefsAsync. 79 void SetBlockAsyncRead(bool block_async_read); 80 81 // Waits until the pref at `key` changes its value. 82 void WaitUntilValueChanges(std::string key); 83 84 // Waits until the pref at `key` equals to the `expected_value`. 85 // 86 // Will exit immediately if the current value is already equal to the 87 // `expected_value`. Otherwise, spins up a RunLoop until the value changes to 88 // the `expected_value`. 89 void WaitForValue(std::string key, base::Value expected_value); 90 91 void OnStoreDeletionFromDisk() override; 92 93 // Getter and Setter methods for setting and getting the state of the 94 // |TestingPrefStore|. 95 virtual void set_read_only(bool read_only); 96 void set_read_success(bool read_success); 97 void set_read_error(PersistentPrefStore::PrefReadError read_error); committed()98 bool committed() { return committed_; } 99 100 protected: 101 ~TestingPrefStore() override; 102 103 private: 104 void CheckPrefIsSerializable(std::string_view key, const base::Value& value); 105 106 // Stores the preference values. 107 PrefValueMap prefs_; 108 109 // Flag that indicates if the PrefStore is read-only 110 bool read_only_; 111 112 // The result to pass to PrefStore::Observer::OnInitializationCompleted 113 bool read_success_; 114 115 // The result to return from ReadPrefs or ReadPrefsAsync. 116 PersistentPrefStore::PrefReadError read_error_; 117 118 // Whether a call to ReadPrefsAsync should block. 119 bool block_async_read_; 120 121 // Whether there is a pending call to ReadPrefsAsync. 122 bool pending_async_read_; 123 124 // Whether initialization has been completed. 125 bool init_complete_; 126 127 // Whether the store contents have been committed to disk since the last 128 // mutation. 129 bool committed_; 130 131 // Optional so we can differentiate `nullopt` from `nullptr`. 132 std::optional<std::unique_ptr<ReadErrorDelegate>> error_delegate_; 133 base::ObserverList<PrefStore::Observer, true> observers_; 134 }; 135 136 #endif // COMPONENTS_PREFS_TESTING_PREF_STORE_H_ 137