blob: 64ff18a5d39b1364cef919e3803ae7e7aca2fe82 [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
Alex Deymo608a3652014-04-15 13:26:35 -07009using std::string;
10
11using chromeos_update_engine::FakePrefs;
12
13namespace {
14
15void CheckNotNull(const string& key, void* ptr) {
16 EXPECT_NE(nullptr, ptr)
Alex Vakulenko88b591f2014-08-28 16:48:57 -070017 << "Called Get*() for key \"" << key << "\" with a null parameter.";
Alex Deymo608a3652014-04-15 13:26:35 -070018}
19
20} // namespace
21
22namespace chromeos_update_engine {
23
24// Compile-time type-dependent constants definitions.
25template<>
26FakePrefs::PrefType const FakePrefs::PrefConsts<string>::type =
27 FakePrefs::PrefType::kString;
28template<>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070029string FakePrefs::PrefValue::* const // NOLINT(runtime/string), not static str.
30 FakePrefs::PrefConsts<string>::member = &FakePrefs::PrefValue::as_str;
Alex Deymo608a3652014-04-15 13:26:35 -070031
32template<>
33FakePrefs::PrefType const FakePrefs::PrefConsts<int64_t>::type =
34 FakePrefs::PrefType::kInt64;
35template<>
36int64_t FakePrefs::PrefValue::* const FakePrefs::PrefConsts<int64_t>::member =
37 &FakePrefs::PrefValue::as_int64;
38
39template<>
40FakePrefs::PrefType const FakePrefs::PrefConsts<bool>::type =
41 FakePrefs::PrefType::kBool;
42template<>
43bool FakePrefs::PrefValue::* const FakePrefs::PrefConsts<bool>::member =
44 &FakePrefs::PrefValue::as_bool;
45
46
47bool FakePrefs::GetString(const string& key, string* value) {
48 return GetValue(key, value);
49}
50
Alex Deymof329b932014-10-30 01:37:48 -070051bool FakePrefs::SetString(const string& key, const string& value) {
Alex Deymo608a3652014-04-15 13:26:35 -070052 SetValue(key, value);
53 return true;
54}
55
56bool FakePrefs::GetInt64(const string& key, int64_t* value) {
57 return GetValue(key, value);
58}
59
60bool FakePrefs::SetInt64(const string& key, const int64_t value) {
61 SetValue(key, value);
62 return true;
63}
64
Alex Deymof329b932014-10-30 01:37:48 -070065bool FakePrefs::GetBoolean(const string& key, bool* value) {
Alex Deymo608a3652014-04-15 13:26:35 -070066 return GetValue(key, value);
67}
68
69bool FakePrefs::SetBoolean(const string& key, const bool value) {
70 SetValue(key, value);
71 return true;
72}
73
74bool FakePrefs::Exists(const string& key) {
75 return values_.find(key) != values_.end();
76}
77
78bool FakePrefs::Delete(const string& key) {
79 if (values_.find(key) == values_.end())
80 return false;
81 values_.erase(key);
82 return true;
83}
84
85string 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
97void 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
104template<typename T>
105void 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
111template<typename T>
112bool 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