• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   void OnStoreDeletionFromDisk() override;
80 
81   // Getter and Setter methods for setting and getting the state of the
82   // |TestingPrefStore|.
83   virtual void set_read_only(bool read_only);
84   void set_read_success(bool read_success);
85   void set_read_error(PersistentPrefStore::PrefReadError read_error);
committed()86   bool committed() { return committed_; }
87 
88  protected:
89   ~TestingPrefStore() override;
90 
91  private:
92   void CheckPrefIsSerializable(const std::string& key,
93                                const base::Value& value);
94 
95   // Stores the preference values.
96   PrefValueMap prefs_;
97 
98   // Flag that indicates if the PrefStore is read-only
99   bool read_only_;
100 
101   // The result to pass to PrefStore::Observer::OnInitializationCompleted
102   bool read_success_;
103 
104   // The result to return from ReadPrefs or ReadPrefsAsync.
105   PersistentPrefStore::PrefReadError read_error_;
106 
107   // Whether a call to ReadPrefsAsync should block.
108   bool block_async_read_;
109 
110   // Whether there is a pending call to ReadPrefsAsync.
111   bool pending_async_read_;
112 
113   // Whether initialization has been completed.
114   bool init_complete_;
115 
116   // Whether the store contents have been committed to disk since the last
117   // mutation.
118   bool committed_;
119 
120   std::unique_ptr<ReadErrorDelegate> error_delegate_;
121   base::ObserverList<PrefStore::Observer, true>::Unchecked observers_;
122 };
123 
124 #endif  // COMPONENTS_PREFS_TESTING_PREF_STORE_H_
125