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.
5 #ifndef COMPONENTS_PREFS_TESTING_PREF_STORE_H_
6 #define COMPONENTS_PREFS_TESTING_PREF_STORE_H_
12 #include "base/compiler_specific.h"
13 #include "base/observer_list.h"
14 #include "base/strings/string_piece.h"
15 #include "base/values.h"
16 #include "components/prefs/persistent_pref_store.h"
17 #include "components/prefs/pref_value_map.h"
19 // |TestingPrefStore| is a preference store implementation that allows tests to
20 // explicitly manipulate the contents of the store, triggering notifications
22 class TestingPrefStore : public PersistentPrefStore {
26 TestingPrefStore(const TestingPrefStore&) = delete;
27 TestingPrefStore& operator=(const TestingPrefStore&) = delete;
29 // Overridden from PrefStore.
30 bool GetValue(base::StringPiece key,
31 const base::Value** result) const override;
32 base::Value::Dict GetValues() const override;
33 void AddObserver(PrefStore::Observer* observer) override;
34 void RemoveObserver(PrefStore::Observer* observer) override;
35 bool HasObservers() const override;
36 bool IsInitializationComplete() const override;
38 // PersistentPrefStore overrides:
39 bool GetMutableValue(const std::string& key, base::Value** result) override;
40 void ReportValueChanged(const std::string& key, uint32_t flags) override;
41 void SetValue(const std::string& key,
43 uint32_t flags) override;
44 void SetValueSilently(const std::string& key,
46 uint32_t flags) override;
47 void RemoveValue(const std::string& key, uint32_t flags) override;
48 void RemoveValuesByPrefixSilently(const std::string& prefix) override;
49 bool ReadOnly() const override;
50 PrefReadError GetReadError() const override;
51 PersistentPrefStore::PrefReadError ReadPrefs() override;
52 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
53 void CommitPendingWrite(base::OnceClosure reply_callback,
54 base::OnceClosure synchronous_done_callback) override;
55 void SchedulePendingLossyWrites() override;
57 // Marks the store as having completed initialization.
58 void SetInitializationCompleted();
60 // Used for tests to trigger notifications explicitly.
61 void NotifyPrefValueChanged(const std::string& key);
62 void NotifyInitializationCompleted();
64 // Some convenience getters/setters.
65 void SetString(const std::string& key, const std::string& value);
66 void SetInteger(const std::string& key, int value);
67 void SetBoolean(const std::string& key, bool value);
69 bool GetString(const std::string& key, std::string* value) const;
70 bool GetInteger(const std::string& key, int* value) const;
71 bool GetBoolean(const std::string& key, bool* value) const;
73 // Determines whether ReadPrefsAsync completes immediately. Defaults to false
74 // (non-blocking). To block, invoke this with true (blocking) before the call
75 // to ReadPrefsAsync. To unblock, invoke again with false (non-blocking) after
76 // the call to ReadPrefsAsync.
77 void SetBlockAsyncRead(bool block_async_read);
79 // Waits until the pref at `key` changes its value.
80 void WaitUntilValueChanges(std::string key);
82 // Waits until the pref at `key` equals to the `expected_value`.
84 // Will exit immediately if the current value is already equal to the
85 // `expected_value`. Otherwise, spins up a RunLoop until the value changes to
86 // the `expected_value`.
87 void WaitForValue(std::string key, base::Value expected_value);
89 void OnStoreDeletionFromDisk() override;
91 // Getter and Setter methods for setting and getting the state of the
92 // |TestingPrefStore|.
93 virtual void set_read_only(bool read_only);
94 void set_read_success(bool read_success);
95 void set_read_error(PersistentPrefStore::PrefReadError read_error);
96 bool committed() { return committed_; }
99 ~TestingPrefStore() override;
102 void CheckPrefIsSerializable(const std::string& key,
103 const base::Value& value);
105 // Stores the preference values.
108 // Flag that indicates if the PrefStore is read-only
111 // The result to pass to PrefStore::Observer::OnInitializationCompleted
114 // The result to return from ReadPrefs or ReadPrefsAsync.
115 PersistentPrefStore::PrefReadError read_error_;
117 // Whether a call to ReadPrefsAsync should block.
118 bool block_async_read_;
120 // Whether there is a pending call to ReadPrefsAsync.
121 bool pending_async_read_;
123 // Whether initialization has been completed.
126 // Whether the store contents have been committed to disk since the last
130 std::unique_ptr<ReadErrorDelegate> error_delegate_;
131 base::ObserverList<PrefStore::Observer, true>::Unchecked observers_;
134 #endif // COMPONENTS_PREFS_TESTING_PREF_STORE_H_