blob: 66b915299ce21be5b497ab772417949baafe5cb9 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Alex Deymo608a3652014-04-15 13:26:35 -070016
17#include "update_engine/fake_prefs.h"
18
19#include <gtest/gtest.h>
20
Alex Deymo608a3652014-04-15 13:26:35 -070021using std::string;
22
23using chromeos_update_engine::FakePrefs;
24
25namespace {
26
27void CheckNotNull(const string& key, void* ptr) {
28 EXPECT_NE(nullptr, ptr)
Alex Vakulenko88b591f2014-08-28 16:48:57 -070029 << "Called Get*() for key \"" << key << "\" with a null parameter.";
Alex Deymo608a3652014-04-15 13:26:35 -070030}
31
32} // namespace
33
34namespace chromeos_update_engine {
35
36// Compile-time type-dependent constants definitions.
37template<>
38FakePrefs::PrefType const FakePrefs::PrefConsts<string>::type =
39 FakePrefs::PrefType::kString;
40template<>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070041string FakePrefs::PrefValue::* const // NOLINT(runtime/string), not static str.
42 FakePrefs::PrefConsts<string>::member = &FakePrefs::PrefValue::as_str;
Alex Deymo608a3652014-04-15 13:26:35 -070043
44template<>
45FakePrefs::PrefType const FakePrefs::PrefConsts<int64_t>::type =
46 FakePrefs::PrefType::kInt64;
47template<>
48int64_t FakePrefs::PrefValue::* const FakePrefs::PrefConsts<int64_t>::member =
49 &FakePrefs::PrefValue::as_int64;
50
51template<>
52FakePrefs::PrefType const FakePrefs::PrefConsts<bool>::type =
53 FakePrefs::PrefType::kBool;
54template<>
55bool FakePrefs::PrefValue::* const FakePrefs::PrefConsts<bool>::member =
56 &FakePrefs::PrefValue::as_bool;
57
58
59bool FakePrefs::GetString(const string& key, string* value) {
60 return GetValue(key, value);
61}
62
Alex Deymof329b932014-10-30 01:37:48 -070063bool FakePrefs::SetString(const string& key, const string& value) {
Alex Deymo608a3652014-04-15 13:26:35 -070064 SetValue(key, value);
65 return true;
66}
67
68bool FakePrefs::GetInt64(const string& key, int64_t* value) {
69 return GetValue(key, value);
70}
71
72bool FakePrefs::SetInt64(const string& key, const int64_t value) {
73 SetValue(key, value);
74 return true;
75}
76
Alex Deymof329b932014-10-30 01:37:48 -070077bool FakePrefs::GetBoolean(const string& key, bool* value) {
Alex Deymo608a3652014-04-15 13:26:35 -070078 return GetValue(key, value);
79}
80
81bool FakePrefs::SetBoolean(const string& key, const bool value) {
82 SetValue(key, value);
83 return true;
84}
85
86bool FakePrefs::Exists(const string& key) {
87 return values_.find(key) != values_.end();
88}
89
90bool FakePrefs::Delete(const string& key) {
91 if (values_.find(key) == values_.end())
92 return false;
93 values_.erase(key);
94 return true;
95}
96
97string FakePrefs::GetTypeName(PrefType type) {
98 switch (type) {
99 case PrefType::kString:
100 return "string";
101 case PrefType::kInt64:
102 return "int64_t";
103 case PrefType::kBool:
104 return "bool";
105 }
106 return "Unknown";
107}
108
109void FakePrefs::CheckKeyType(const string& key, PrefType type) const {
110 auto it = values_.find(key);
111 EXPECT_TRUE(it == values_.end() || it->second.type == type)
112 << "Key \"" << key << "\" if defined as " << GetTypeName(it->second.type)
113 << " but is accessed as a " << GetTypeName(type);
114}
115
116template<typename T>
117void FakePrefs::SetValue(const string& key, const T& value) {
118 CheckKeyType(key, PrefConsts<T>::type);
119 values_[key].type = PrefConsts<T>::type;
120 values_[key].value.*(PrefConsts<T>::member) = value;
121}
122
123template<typename T>
124bool FakePrefs::GetValue(const string& key, T* value) const {
125 CheckKeyType(key, PrefConsts<T>::type);
126 auto it = values_.find(key);
127 if (it == values_.end())
128 return false;
129 CheckNotNull(key, value);
130 *value = it->second.value.*(PrefConsts<T>::member);
131 return true;
132}
133
134} // namespace chromeos_update_engine