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.
5 #ifndef BASE_PREFS_TESTING_PREF_STORE_H_
6 #define BASE_PREFS_TESTING_PREF_STORE_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/observer_list.h"
13 #include "base/prefs/persistent_pref_store.h"
14 #include "base/prefs/pref_value_map.h"
16 // |TestingPrefStore| is a preference store implementation that allows tests to
17 // explicitly manipulate the contents of the store, triggering notifications
19 class TestingPrefStore
: public PersistentPrefStore
{
23 // Overriden from PrefStore.
24 bool GetValue(const std::string
& key
,
25 const base::Value
** result
) const override
;
26 void AddObserver(PrefStore::Observer
* observer
) override
;
27 void RemoveObserver(PrefStore::Observer
* observer
) override
;
28 bool HasObservers() const override
;
29 bool IsInitializationComplete() const override
;
31 // PersistentPrefStore overrides:
32 bool GetMutableValue(const std::string
& key
, base::Value
** result
) override
;
33 void ReportValueChanged(const std::string
& key
, uint32 flags
) override
;
34 void SetValue(const std::string
& key
,
35 scoped_ptr
<base::Value
> value
,
36 uint32 flags
) override
;
37 void SetValueSilently(const std::string
& key
,
38 scoped_ptr
<base::Value
> value
,
39 uint32 flags
) override
;
40 void RemoveValue(const std::string
& key
, uint32 flags
) override
;
41 bool ReadOnly() const override
;
42 PrefReadError
GetReadError() const override
;
43 PersistentPrefStore::PrefReadError
ReadPrefs() override
;
44 void ReadPrefsAsync(ReadErrorDelegate
* error_delegate
) override
;
45 void CommitPendingWrite() override
;
46 void SchedulePendingLossyWrites() override
;
48 // Marks the store as having completed initialization.
49 void SetInitializationCompleted();
51 // Used for tests to trigger notifications explicitly.
52 void NotifyPrefValueChanged(const std::string
& key
);
53 void NotifyInitializationCompleted();
55 // Some convenience getters/setters.
56 void SetString(const std::string
& key
, const std::string
& value
);
57 void SetInteger(const std::string
& key
, int value
);
58 void SetBoolean(const std::string
& key
, bool value
);
60 bool GetString(const std::string
& key
, std::string
* value
) const;
61 bool GetInteger(const std::string
& key
, int* value
) const;
62 bool GetBoolean(const std::string
& key
, bool* value
) const;
64 // Determines whether ReadPrefsAsync completes immediately. Defaults to false
65 // (non-blocking). To block, invoke this with true (blocking) before the call
66 // to ReadPrefsAsync. To unblock, invoke again with false (non-blocking) after
67 // the call to ReadPrefsAsync.
68 void SetBlockAsyncRead(bool block_async_read
);
70 // Getter and Setter methods for setting and getting the state of the
71 // |TestingPrefStore|.
72 virtual void set_read_only(bool read_only
);
73 void set_read_success(bool read_success
);
74 void set_read_error(PersistentPrefStore::PrefReadError read_error
);
75 bool committed() { return committed_
; }
78 ~TestingPrefStore() override
;
81 // Stores the preference values.
84 // Flag that indicates if the PrefStore is read-only
87 // The result to pass to PrefStore::Observer::OnInitializationCompleted
90 // The result to return from ReadPrefs or ReadPrefsAsync.
91 PersistentPrefStore::PrefReadError read_error_
;
93 // Whether a call to ReadPrefsAsync should block.
94 bool block_async_read_
;
96 // Whether there is a pending call to ReadPrefsAsync.
97 bool pending_async_read_
;
99 // Whether initialization has been completed.
102 // Whether the store contents have been committed to disk since the last
106 scoped_ptr
<ReadErrorDelegate
> error_delegate_
;
107 base::ObserverList
<PrefStore::Observer
, true> observers_
;
109 DISALLOW_COPY_AND_ASSIGN(TestingPrefStore
);
112 #endif // BASE_PREFS_TESTING_PREF_STORE_H_