blob: e26b8655f508e86ed491f7a1fa4e12b46df156fa [file] [log] [blame]
Alex Deymo608a3652014-04-15 13:26:35 -07001// 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
9using std::map;
10using std::string;
11
12using chromeos_update_engine::FakePrefs;
13
14namespace {
15
16void 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
23namespace chromeos_update_engine {
24
25// Compile-time type-dependent constants definitions.
26template<>
27FakePrefs::PrefType const FakePrefs::PrefConsts<string>::type =
28 FakePrefs::PrefType::kString;
29template<>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070030string FakePrefs::PrefValue::* const // NOLINT(runtime/string), not static str.
31 FakePrefs::PrefConsts<string>::member = &FakePrefs::PrefValue::as_str;
Alex Deymo608a3652014-04-15 13:26:35 -070032
33template<>
34FakePrefs::PrefType const FakePrefs::PrefConsts<int64_t>::type =
35 FakePrefs::PrefType::kInt64;
36template<>
37int64_t FakePrefs::PrefValue::* const FakePrefs::PrefConsts<int64_t>::member =
38 &FakePrefs::PrefValue::as_int64;
39
40template<>
41FakePrefs::PrefType const FakePrefs::PrefConsts<bool>::type =
42 FakePrefs::PrefType::kBool;
43template<>
44bool FakePrefs::PrefValue::* const FakePrefs::PrefConsts<bool>::member =
45 &FakePrefs::PrefValue::as_bool;
46
47
48bool FakePrefs::GetString(const string& key, string* value) {
49 return GetValue(key, value);
50}
51
52bool FakePrefs::SetString(const std::string& key, const std::string& value) {
53 SetValue(key, value);
54 return true;
55}
56
57bool FakePrefs::GetInt64(const string& key, int64_t* value) {
58 return GetValue(key, value);
59}
60
61bool FakePrefs::SetInt64(const string& key, const int64_t value) {
62 SetValue(key, value);
63 return true;
64}
65
66bool FakePrefs::GetBoolean(const std::string& key, bool* value) {
67 return GetValue(key, value);
68}
69
70bool FakePrefs::SetBoolean(const string& key, const bool value) {
71 SetValue(key, value);
72 return true;
73}
74
75bool FakePrefs::Exists(const string& key) {
76 return values_.find(key) != values_.end();
77}
78
79bool FakePrefs::Delete(const string& key) {
80 if (values_.find(key) == values_.end())
81 return false;
82 values_.erase(key);
83 return true;
84}
85
86string 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
98void 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
105template<typename T>
106void 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
112template<typename T>
113bool 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