xref: /aosp_15_r20/external/cronet/components/prefs/testing_pref_store.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <string>
11 
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"
18 
19 // |TestingPrefStore| is a preference store implementation that allows tests to
20 // explicitly manipulate the contents of the store, triggering notifications
21 // where appropriate.
22 class TestingPrefStore : public PersistentPrefStore {
23  public:
24   TestingPrefStore();
25 
26   TestingPrefStore(const TestingPrefStore&) = delete;
27   TestingPrefStore& operator=(const TestingPrefStore&) = delete;
28 
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;
37 
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,
42                 base::Value value,
43                 uint32_t flags) override;
44   void SetValueSilently(const std::string& key,
45                         base::Value value,
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;
56 
57   // Marks the store as having completed initialization.
58   void SetInitializationCompleted();
59 
60   // Used for tests to trigger notifications explicitly.
61   void NotifyPrefValueChanged(const std::string& key);
62   void NotifyInitializationCompleted();
63 
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);
68 
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;
72 
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);
78 
79   // Waits until the pref at `key` changes its value.
80   void WaitUntilValueChanges(std::string key);
81 
82   // Waits until the pref at `key` equals to the `expected_value`.
83   //
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);
88 
89   void OnStoreDeletionFromDisk() override;
90 
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);
committed()96   bool committed() { return committed_; }
97 
98  protected:
99   ~TestingPrefStore() override;
100 
101  private:
102   void CheckPrefIsSerializable(const std::string& key,
103                                const base::Value& value);
104 
105   // Stores the preference values.
106   PrefValueMap prefs_;
107 
108   // Flag that indicates if the PrefStore is read-only
109   bool read_only_;
110 
111   // The result to pass to PrefStore::Observer::OnInitializationCompleted
112   bool read_success_;
113 
114   // The result to return from ReadPrefs or ReadPrefsAsync.
115   PersistentPrefStore::PrefReadError read_error_;
116 
117   // Whether a call to ReadPrefsAsync should block.
118   bool block_async_read_;
119 
120   // Whether there is a pending call to ReadPrefsAsync.
121   bool pending_async_read_;
122 
123   // Whether initialization has been completed.
124   bool init_complete_;
125 
126   // Whether the store contents have been committed to disk since the last
127   // mutation.
128   bool committed_;
129 
130   std::unique_ptr<ReadErrorDelegate> error_delegate_;
131   base::ObserverList<PrefStore::Observer, true>::Unchecked observers_;
132 };
133 
134 #endif  // COMPONENTS_PREFS_TESTING_PREF_STORE_H_
135