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