| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/value_store/testing_value_store.h" | 5 #include "chrome/browser/value_store/testing_value_store.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 const char kGenericErrorMessage[] = "TestingValueStore configured to error"; | 11 const char kGenericErrorMessage[] = "TestingValueStore configured to error"; |
| 12 | 12 |
| 13 ValueStore::ReadResult ReadResultError() { | |
| 14 return ValueStore::MakeReadResult(kGenericErrorMessage); | |
| 15 } | |
| 16 | |
| 17 ValueStore::WriteResult WriteResultError() { | |
| 18 return ValueStore::MakeWriteResult(kGenericErrorMessage); | |
| 19 } | |
| 20 | |
| 21 std::vector<std::string> CreateVector(const std::string& string) { | |
| 22 std::vector<std::string> strings; | |
| 23 strings.push_back(string); | |
| 24 return strings; | |
| 25 } | |
| 26 | |
| 27 } // namespace | 13 } // namespace |
| 28 | 14 |
| 29 TestingValueStore::TestingValueStore() | 15 TestingValueStore::TestingValueStore() |
| 30 : read_count_(0), write_count_(0), fail_all_requests_(false) {} | 16 : read_count_(0), write_count_(0), error_code_(OK) {} |
| 31 | 17 |
| 32 TestingValueStore::~TestingValueStore() {} | 18 TestingValueStore::~TestingValueStore() {} |
| 33 | 19 |
| 34 void TestingValueStore::SetFailAllRequests(bool fail_all_requests) { | |
| 35 fail_all_requests_ = fail_all_requests; | |
| 36 } | |
| 37 | |
| 38 size_t TestingValueStore::GetBytesInUse(const std::string& key) { | 20 size_t TestingValueStore::GetBytesInUse(const std::string& key) { |
| 39 // Let SettingsStorageQuotaEnforcer implement this. | 21 // Let SettingsStorageQuotaEnforcer implement this. |
| 40 NOTREACHED() << "Not implemented"; | 22 NOTREACHED() << "Not implemented"; |
| 41 return 0; | 23 return 0; |
| 42 } | 24 } |
| 43 | 25 |
| 44 size_t TestingValueStore::GetBytesInUse( | 26 size_t TestingValueStore::GetBytesInUse( |
| 45 const std::vector<std::string>& keys) { | 27 const std::vector<std::string>& keys) { |
| 46 // Let SettingsStorageQuotaEnforcer implement this. | 28 // Let SettingsStorageQuotaEnforcer implement this. |
| 47 NOTREACHED() << "Not implemented"; | 29 NOTREACHED() << "Not implemented"; |
| 48 return 0; | 30 return 0; |
| 49 } | 31 } |
| 50 | 32 |
| 51 size_t TestingValueStore::GetBytesInUse() { | 33 size_t TestingValueStore::GetBytesInUse() { |
| 52 // Let SettingsStorageQuotaEnforcer implement this. | 34 // Let SettingsStorageQuotaEnforcer implement this. |
| 53 NOTREACHED() << "Not implemented"; | 35 NOTREACHED() << "Not implemented"; |
| 54 return 0; | 36 return 0; |
| 55 } | 37 } |
| 56 | 38 |
| 57 ValueStore::ReadResult TestingValueStore::Get(const std::string& key) { | 39 ValueStore::ReadResult TestingValueStore::Get(const std::string& key) { |
| 58 return Get(CreateVector(key)); | 40 return Get(std::vector<std::string>(1, key)); |
| 59 } | 41 } |
| 60 | 42 |
| 61 ValueStore::ReadResult TestingValueStore::Get( | 43 ValueStore::ReadResult TestingValueStore::Get( |
| 62 const std::vector<std::string>& keys) { | 44 const std::vector<std::string>& keys) { |
| 63 read_count_++; | 45 read_count_++; |
| 64 if (fail_all_requests_) { | 46 if (error_code_ != OK) |
| 65 return ReadResultError(); | 47 return MakeReadResult(TestingError()); |
| 66 } | |
| 67 | 48 |
| 68 DictionaryValue* settings = new DictionaryValue(); | 49 DictionaryValue* settings = new DictionaryValue(); |
| 69 for (std::vector<std::string>::const_iterator it = keys.begin(); | 50 for (std::vector<std::string>::const_iterator it = keys.begin(); |
| 70 it != keys.end(); ++it) { | 51 it != keys.end(); ++it) { |
| 71 Value* value = NULL; | 52 Value* value = NULL; |
| 72 if (storage_.GetWithoutPathExpansion(*it, &value)) { | 53 if (storage_.GetWithoutPathExpansion(*it, &value)) { |
| 73 settings->SetWithoutPathExpansion(*it, value->DeepCopy()); | 54 settings->SetWithoutPathExpansion(*it, value->DeepCopy()); |
| 74 } | 55 } |
| 75 } | 56 } |
| 76 return MakeReadResult(settings); | 57 return MakeReadResult(make_scoped_ptr(settings)); |
| 77 } | 58 } |
| 78 | 59 |
| 79 ValueStore::ReadResult TestingValueStore::Get() { | 60 ValueStore::ReadResult TestingValueStore::Get() { |
| 80 read_count_++; | 61 read_count_++; |
| 81 if (fail_all_requests_) { | 62 if (error_code_ != OK) |
| 82 return ReadResultError(); | 63 return MakeReadResult(TestingError()); |
| 83 } | 64 return MakeReadResult(make_scoped_ptr(storage_.DeepCopy())); |
| 84 return MakeReadResult(storage_.DeepCopy()); | |
| 85 } | 65 } |
| 86 | 66 |
| 87 ValueStore::WriteResult TestingValueStore::Set( | 67 ValueStore::WriteResult TestingValueStore::Set( |
| 88 WriteOptions options, const std::string& key, const Value& value) { | 68 WriteOptions options, const std::string& key, const Value& value) { |
| 89 DictionaryValue settings; | 69 DictionaryValue settings; |
| 90 settings.SetWithoutPathExpansion(key, value.DeepCopy()); | 70 settings.SetWithoutPathExpansion(key, value.DeepCopy()); |
| 91 return Set(options, settings); | 71 return Set(options, settings); |
| 92 } | 72 } |
| 93 | 73 |
| 94 ValueStore::WriteResult TestingValueStore::Set( | 74 ValueStore::WriteResult TestingValueStore::Set( |
| 95 WriteOptions options, const DictionaryValue& settings) { | 75 WriteOptions options, const DictionaryValue& settings) { |
| 96 write_count_++; | 76 write_count_++; |
| 97 if (fail_all_requests_) { | 77 if (error_code_ != OK) |
| 98 return WriteResultError(); | 78 return MakeWriteResult(TestingError()); |
| 99 } | |
| 100 | 79 |
| 101 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 80 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
| 102 for (DictionaryValue::Iterator it(settings); !it.IsAtEnd(); it.Advance()) { | 81 for (DictionaryValue::Iterator it(settings); !it.IsAtEnd(); it.Advance()) { |
| 103 Value* old_value = NULL; | 82 Value* old_value = NULL; |
| 104 if (!storage_.GetWithoutPathExpansion(it.key(), &old_value) || | 83 if (!storage_.GetWithoutPathExpansion(it.key(), &old_value) || |
| 105 !old_value->Equals(&it.value())) { | 84 !old_value->Equals(&it.value())) { |
| 106 changes->push_back( | 85 changes->push_back( |
| 107 ValueStoreChange( | 86 ValueStoreChange( |
| 108 it.key(), | 87 it.key(), |
| 109 old_value ? old_value->DeepCopy() : old_value, | 88 old_value ? old_value->DeepCopy() : old_value, |
| 110 it.value().DeepCopy())); | 89 it.value().DeepCopy())); |
| 111 storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy()); | 90 storage_.SetWithoutPathExpansion(it.key(), it.value().DeepCopy()); |
| 112 } | 91 } |
| 113 } | 92 } |
| 114 return MakeWriteResult(changes.release()); | 93 return MakeWriteResult(changes.Pass()); |
| 115 } | 94 } |
| 116 | 95 |
| 117 ValueStore::WriteResult TestingValueStore::Remove(const std::string& key) { | 96 ValueStore::WriteResult TestingValueStore::Remove(const std::string& key) { |
| 118 return Remove(CreateVector(key)); | 97 return Remove(std::vector<std::string>(1, key)); |
| 119 } | 98 } |
| 120 | 99 |
| 121 ValueStore::WriteResult TestingValueStore::Remove( | 100 ValueStore::WriteResult TestingValueStore::Remove( |
| 122 const std::vector<std::string>& keys) { | 101 const std::vector<std::string>& keys) { |
| 123 write_count_++; | 102 write_count_++; |
| 124 if (fail_all_requests_) { | 103 if (error_code_ != OK) |
| 125 return WriteResultError(); | 104 return MakeWriteResult(TestingError()); |
| 126 } | |
| 127 | 105 |
| 128 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); | 106 scoped_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList()); |
| 129 for (std::vector<std::string>::const_iterator it = keys.begin(); | 107 for (std::vector<std::string>::const_iterator it = keys.begin(); |
| 130 it != keys.end(); ++it) { | 108 it != keys.end(); ++it) { |
| 131 scoped_ptr<Value> old_value; | 109 scoped_ptr<Value> old_value; |
| 132 if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) { | 110 if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) { |
| 133 changes->push_back(ValueStoreChange(*it, old_value.release(), NULL)); | 111 changes->push_back(ValueStoreChange(*it, old_value.release(), NULL)); |
| 134 } | 112 } |
| 135 } | 113 } |
| 136 return MakeWriteResult(changes.release()); | 114 return MakeWriteResult(changes.Pass()); |
| 137 } | 115 } |
| 138 | 116 |
| 139 ValueStore::WriteResult TestingValueStore::Clear() { | 117 ValueStore::WriteResult TestingValueStore::Clear() { |
| 140 std::vector<std::string> keys; | 118 std::vector<std::string> keys; |
| 141 for (DictionaryValue::Iterator it(storage_); !it.IsAtEnd(); it.Advance()) { | 119 for (DictionaryValue::Iterator it(storage_); !it.IsAtEnd(); it.Advance()) { |
| 142 keys.push_back(it.key()); | 120 keys.push_back(it.key()); |
| 143 } | 121 } |
| 144 return Remove(keys); | 122 return Remove(keys); |
| 145 } | 123 } |
| 124 |
| 125 scoped_ptr<ValueStore::Error> TestingValueStore::TestingError() { |
| 126 return make_scoped_ptr(new ValueStore::Error( |
| 127 error_code_, kGenericErrorMessage, scoped_ptr<std::string>())); |
| 128 } |
| OLD | NEW |