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 #include "chrome/browser/value_store/testing_value_store.h"
7 #include "base/logging.h"
11 const char kGenericErrorMessage[] = "TestingValueStore configured to error";
15 TestingValueStore::TestingValueStore()
16 : read_count_(0), write_count_(0), error_code_(OK) {}
18 TestingValueStore::~TestingValueStore() {}
20 size_t TestingValueStore::GetBytesInUse(const std::string& key) {
21 // Let SettingsStorageQuotaEnforcer implement this.
22 NOTREACHED() << "Not implemented";
26 size_t TestingValueStore::GetBytesInUse(
27 const std::vector<std::string>& keys) {
28 // Let SettingsStorageQuotaEnforcer implement this.
29 NOTREACHED() << "Not implemented";
33 size_t TestingValueStore::GetBytesInUse() {
34 // Let SettingsStorageQuotaEnforcer implement this.
35 NOTREACHED() << "Not implemented";
39 ValueStore::ReadResult TestingValueStore::Get(const std::string& key) {
40 return Get(std::vector<std::string>(1, key));
43 ValueStore::ReadResult TestingValueStore::Get(
44 const std::vector<std::string>& keys) {
46 if (error_code_ != OK)
47 return MakeReadResult(TestingError());
49 DictionaryValue* settings = new DictionaryValue();
50 for (std::vector<std::string>::const_iterator it = keys.begin();
51 it != keys.end(); ++it) {
53 if (storage_.GetWithoutPathExpansion(*it, &value)) {
54 settings->SetWithoutPathExpansion(*it, value->DeepCopy());
57 return MakeReadResult(make_scoped_ptr(settings));
60 ValueStore::ReadResult TestingValueStore::Get() {
62 if (error_code_ != OK)
63 return MakeReadResult(TestingError());
64 return MakeReadResult(make_scoped_ptr(storage_.DeepCopy()));
67 ValueStore::WriteResult TestingValueStore::Set(
68 WriteOptions options, const std::string& key, const Value& value) {
69 DictionaryValue settings;
70 settings.SetWithoutPathExpansion(key, value.DeepCopy());
71 return Set(options, settings);
74 ValueStore::WriteResult TestingValueStore::Set(
75 WriteOptions options, const DictionaryValue& settings) {
77 if (error_code_ != OK)
78 return MakeWriteResult(TestingError());
80 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
81 for (DictionaryValue::Iterator it(settings); !it.IsAtEnd(); it.Advance()) {
82 Value* old_value = NULL;
83 if (!storage_.GetWithoutPathExpansion(it.key(), &old_value) ||
84 !old_value->Equals(&it.value())) {
88 old_value ? old_value->DeepCopy() : old_value,
89 it.value().DeepCopy()));
90 storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy());
93 return MakeWriteResult(changes.Pass());
96 ValueStore::WriteResult TestingValueStore::Remove(const std::string& key) {
97 return Remove(std::vector<std::string>(1, key));
100 ValueStore::WriteResult TestingValueStore::Remove(
101 const std::vector<std::string>& keys) {
103 if (error_code_ != OK)
104 return MakeWriteResult(TestingError());
106 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
107 for (std::vector<std::string>::const_iterator it = keys.begin();
108 it != keys.end(); ++it) {
109 scoped_ptr<Value> old_value;
110 if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) {
111 changes->push_back(ValueStoreChange(*it, old_value.release(), NULL));
114 return MakeWriteResult(changes.Pass());
117 ValueStore::WriteResult TestingValueStore::Clear() {
118 std::vector<std::string> keys;
119 for (DictionaryValue::Iterator it(storage_); !it.IsAtEnd(); it.Advance()) {
120 keys.push_back(it.key());
125 scoped_ptr<ValueStore::Error> TestingValueStore::TestingError() {
126 return make_scoped_ptr(new ValueStore::Error(
127 error_code_, kGenericErrorMessage, scoped_ptr<std::string>()));