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. 4 5#ifndef BASE_PREFS_TESTING_PREF_STORE_H_ 6#define BASE_PREFS_TESTING_PREF_STORE_H_ 7 8#include <string> 9 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" 15 16// |TestingPrefStore| is a preference store implementation that allows tests to 17// explicitly manipulate the contents of the store, triggering notifications 18// where appropriate. 19class TestingPrefStore : public PersistentPrefStore { 20 public: 21 TestingPrefStore(); 22 23 // Overriden from PrefStore. 24 virtual bool GetValue(const std::string& key, 25 const base::Value** result) const OVERRIDE; 26 virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE; 27 virtual void RemoveObserver(PrefStore::Observer* observer) OVERRIDE; 28 virtual bool HasObservers() const OVERRIDE; 29 virtual bool IsInitializationComplete() const OVERRIDE; 30 31 // PersistentPrefStore overrides: 32 virtual bool GetMutableValue(const std::string& key, 33 base::Value** result) OVERRIDE; 34 virtual void ReportValueChanged(const std::string& key) OVERRIDE; 35 virtual void SetValue(const std::string& key, base::Value* value) OVERRIDE; 36 virtual void SetValueSilently(const std::string& key, 37 base::Value* value) OVERRIDE; 38 virtual void RemoveValue(const std::string& key) OVERRIDE; 39 virtual bool ReadOnly() const OVERRIDE; 40 virtual PrefReadError GetReadError() const OVERRIDE; 41 virtual PersistentPrefStore::PrefReadError ReadPrefs() OVERRIDE; 42 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; 43 virtual void CommitPendingWrite() OVERRIDE {} 44 45 // Marks the store as having completed initialization. 46 void SetInitializationCompleted(); 47 48 // Used for tests to trigger notifications explicitly. 49 void NotifyPrefValueChanged(const std::string& key); 50 void NotifyInitializationCompleted(); 51 52 // Some convenience getters/setters. 53 void SetString(const std::string& key, const std::string& value); 54 void SetInteger(const std::string& key, int value); 55 void SetBoolean(const std::string& key, bool value); 56 57 bool GetString(const std::string& key, std::string* value) const; 58 bool GetInteger(const std::string& key, int* value) const; 59 bool GetBoolean(const std::string& key, bool* value) const; 60 61 // Getter and Setter methods for setting and getting the state of the 62 // |TestingPrefStore|. 63 virtual void set_read_only(bool read_only); 64 65 protected: 66 virtual ~TestingPrefStore(); 67 68 private: 69 // Stores the preference values. 70 PrefValueMap prefs_; 71 72 // Flag that indicates if the PrefStore is read-only 73 bool read_only_; 74 75 // Whether initialization has been completed. 76 bool init_complete_; 77 78 ObserverList<PrefStore::Observer, true> observers_; 79 80 DISALLOW_COPY_AND_ASSIGN(TestingPrefStore); 81}; 82 83#endif // BASE_PREFS_TESTING_PREF_STORE_H_ 84