Alex Deymo | 608a365 | 2014-04-15 13:26:35 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | #include "update_engine/fake_prefs.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | using std::map; |
| 10 | using std::string; |
| 11 | |
| 12 | using chromeos_update_engine::FakePrefs; |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | void CheckNotNull(const string& key, void* ptr) { |
| 17 | EXPECT_NE(nullptr, ptr) |
| 18 | << "Called Get*() for key \"" << key << "\" with a NULL parameter."; |
| 19 | } |
| 20 | |
| 21 | } // namespace |
| 22 | |
| 23 | namespace chromeos_update_engine { |
| 24 | |
| 25 | // Compile-time type-dependent constants definitions. |
| 26 | template<> |
| 27 | FakePrefs::PrefType const FakePrefs::PrefConsts<string>::type = |
| 28 | FakePrefs::PrefType::kString; |
| 29 | template<> |
| 30 | string FakePrefs::PrefValue::* const FakePrefs::PrefConsts<string>::member = |
| 31 | &FakePrefs::PrefValue::as_str; |
| 32 | |
| 33 | template<> |
| 34 | FakePrefs::PrefType const FakePrefs::PrefConsts<int64_t>::type = |
| 35 | FakePrefs::PrefType::kInt64; |
| 36 | template<> |
| 37 | int64_t FakePrefs::PrefValue::* const FakePrefs::PrefConsts<int64_t>::member = |
| 38 | &FakePrefs::PrefValue::as_int64; |
| 39 | |
| 40 | template<> |
| 41 | FakePrefs::PrefType const FakePrefs::PrefConsts<bool>::type = |
| 42 | FakePrefs::PrefType::kBool; |
| 43 | template<> |
| 44 | bool FakePrefs::PrefValue::* const FakePrefs::PrefConsts<bool>::member = |
| 45 | &FakePrefs::PrefValue::as_bool; |
| 46 | |
| 47 | |
| 48 | bool FakePrefs::GetString(const string& key, string* value) { |
| 49 | return GetValue(key, value); |
| 50 | } |
| 51 | |
| 52 | bool FakePrefs::SetString(const std::string& key, const std::string& value) { |
| 53 | SetValue(key, value); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool FakePrefs::GetInt64(const string& key, int64_t* value) { |
| 58 | return GetValue(key, value); |
| 59 | } |
| 60 | |
| 61 | bool FakePrefs::SetInt64(const string& key, const int64_t value) { |
| 62 | SetValue(key, value); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | bool FakePrefs::GetBoolean(const std::string& key, bool* value) { |
| 67 | return GetValue(key, value); |
| 68 | } |
| 69 | |
| 70 | bool FakePrefs::SetBoolean(const string& key, const bool value) { |
| 71 | SetValue(key, value); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | bool FakePrefs::Exists(const string& key) { |
| 76 | return values_.find(key) != values_.end(); |
| 77 | } |
| 78 | |
| 79 | bool FakePrefs::Delete(const string& key) { |
| 80 | if (values_.find(key) == values_.end()) |
| 81 | return false; |
| 82 | values_.erase(key); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | string FakePrefs::GetTypeName(PrefType type) { |
| 87 | switch (type) { |
| 88 | case PrefType::kString: |
| 89 | return "string"; |
| 90 | case PrefType::kInt64: |
| 91 | return "int64_t"; |
| 92 | case PrefType::kBool: |
| 93 | return "bool"; |
| 94 | } |
| 95 | return "Unknown"; |
| 96 | } |
| 97 | |
| 98 | void FakePrefs::CheckKeyType(const string& key, PrefType type) const { |
| 99 | auto it = values_.find(key); |
| 100 | EXPECT_TRUE(it == values_.end() || it->second.type == type) |
| 101 | << "Key \"" << key << "\" if defined as " << GetTypeName(it->second.type) |
| 102 | << " but is accessed as a " << GetTypeName(type); |
| 103 | } |
| 104 | |
| 105 | template<typename T> |
| 106 | void FakePrefs::SetValue(const string& key, const T& value) { |
| 107 | CheckKeyType(key, PrefConsts<T>::type); |
| 108 | values_[key].type = PrefConsts<T>::type; |
| 109 | values_[key].value.*(PrefConsts<T>::member) = value; |
| 110 | } |
| 111 | |
| 112 | template<typename T> |
| 113 | bool FakePrefs::GetValue(const string& key, T* value) const { |
| 114 | CheckKeyType(key, PrefConsts<T>::type); |
| 115 | auto it = values_.find(key); |
| 116 | if (it == values_.end()) |
| 117 | return false; |
| 118 | CheckNotNull(key, value); |
| 119 | *value = it->second.value.*(PrefConsts<T>::member); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | } // namespace chromeos_update_engine |