property_store_unittest.h revision c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5
1// 2// Copyright (C) 2012 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// 16 17#ifndef SHILL_PROPERTY_STORE_UNITTEST_H_ 18#define SHILL_PROPERTY_STORE_UNITTEST_H_ 19 20#include <map> 21#include <memory> 22#include <string> 23#include <vector> 24 25#include <base/files/scoped_temp_dir.h> 26#include <chromeos/any.h> 27#include <gmock/gmock.h> 28#include <gtest/gtest.h> 29 30#include "shill/error.h" 31#include "shill/key_value_store.h" 32#include "shill/manager.h" 33#include "shill/mock_control.h" 34#include "shill/mock_glib.h" 35#include "shill/mock_metrics.h" 36#include "shill/property_store.h" 37#include "shill/test_event_dispatcher.h" 38 39namespace shill { 40 41class PropertyStoreTest : public testing::TestWithParam<chromeos::Any> { 42 public: 43 typedef ::testing::Types<bool, int16_t, int32_t, std::string, Stringmap, 44 Stringmaps, Strings, uint8_t, uint16_t, Uint16s, 45 uint32_t> PropertyTypes; 46 47 // In real code, it's frowned upon to have non-POD static members, as there 48 // can be ordering issues if your constructors have side effects. 49 // These constructors don't, and declaring these as static lets me 50 // autogenerate a bunch of unit test code that I would otherwise need to 51 // copypaste. So I think it's safe and worth it. 52 static const chromeos::Any kBoolV; 53 static const chromeos::Any kByteV; 54 static const chromeos::Any kInt16V; 55 static const chromeos::Any kInt32V; 56 static const chromeos::Any kKeyValueStoreV; 57 static const chromeos::Any kStringV; 58 static const chromeos::Any kStringmapV; 59 static const chromeos::Any kStringmapsV; 60 static const chromeos::Any kStringsV; 61 static const chromeos::Any kUint16V; 62 static const chromeos::Any kUint16sV; 63 static const chromeos::Any kUint32V; 64 static const chromeos::Any kUint64V; 65 66 PropertyStoreTest(); 67 ~PropertyStoreTest() override; 68 69 void SetUp() override; 70 MOCK_METHOD1(TestCallback, void(const std::string& property_name)); 71 MOCK_METHOD1(GetKeyValueStoreCallback, KeyValueStore(Error* error)); 72 MOCK_METHOD2(SetKeyValueStoreCallback, bool(const KeyValueStore& value, 73 Error* error)); 74 75 // Convenience overloads for GetProperty. Normally, we don't overload 76 // functions. But this is extremely useful for type-parameterized tests. 77 static bool GetProperty(const PropertyStore& store, const std::string& name, 78 bool* storage, Error* error) { 79 return store.GetBoolProperty(name, storage, error); 80 } 81 82 static bool GetProperty(const PropertyStore& store, const std::string& name, 83 int16_t* storage, Error* error) { 84 return store.GetInt16Property(name, storage, error); 85 } 86 87 static bool GetProperty(const PropertyStore& store, const std::string& name, 88 int32_t* storage, Error* error) { 89 return store.GetInt32Property(name, storage, error); 90 } 91 92 static bool GetProperty(const PropertyStore& store, const std::string& name, 93 std::string* storage, Error* error) { 94 return store.GetStringProperty(name, storage, error); 95 } 96 97 static bool GetProperty(const PropertyStore& store, const std::string& name, 98 Stringmap* storage, Error* error) { 99 return store.GetStringmapProperty(name, storage, error); 100 } 101 102 static bool GetProperty(const PropertyStore& store, const std::string& name, 103 Stringmaps* storage, Error* error) { 104 return store.GetStringmapsProperty(name, storage, error); 105 } 106 107 static bool GetProperty(const PropertyStore& store, const std::string& name, 108 Strings* storage, Error* error) { 109 return store.GetStringsProperty(name, storage, error); 110 } 111 112 static bool GetProperty(const PropertyStore& store, const std::string& name, 113 uint8_t* storage, Error* error) { 114 return store.GetUint8Property(name, storage, error); 115 } 116 117 static bool GetProperty(const PropertyStore& store, const std::string& name, 118 uint16_t* storage, Error* error) { 119 return store.GetUint16Property(name, storage, error); 120 } 121 122 static bool GetProperty(const PropertyStore& store, const std::string& name, 123 Uint16s* storage, Error* error) { 124 return store.GetUint16sProperty(name, storage, error); 125 } 126 127 static bool GetProperty(const PropertyStore& store, const std::string& name, 128 uint32_t* storage, Error* error) { 129 return store.GetUint32Property(name, storage, error); 130 } 131 132 // Convenience overloads for RegisterProperty. Normally, we don't overload 133 // functions. But this is extremely useful for type-parameterized tests. 134 static void RegisterProperty( 135 PropertyStore* store, const std::string& name, bool* storage) { 136 store->RegisterBool(name, storage); 137 } 138 139 static void RegisterProperty( 140 PropertyStore* store, const std::string& name, int16_t* storage) { 141 store->RegisterInt16(name, storage); 142 } 143 144 static void RegisterProperty( 145 PropertyStore* store, const std::string& name, int32_t* storage) { 146 store->RegisterInt32(name, storage); 147 } 148 149 static void RegisterProperty( 150 PropertyStore* store, const std::string& name, std::string* storage) { 151 store->RegisterString(name, storage); 152 } 153 154 static void RegisterProperty( 155 PropertyStore* store, const std::string& name, Stringmap* storage) { 156 store->RegisterStringmap(name, storage); 157 } 158 159 static void RegisterProperty( 160 PropertyStore* store, const std::string& name, Stringmaps* storage) { 161 store->RegisterStringmaps(name, storage); 162 } 163 164 static void RegisterProperty( 165 PropertyStore* store, const std::string& name, Strings* storage) { 166 store->RegisterStrings(name, storage); 167 } 168 169 static void RegisterProperty( 170 PropertyStore* store, const std::string& name, uint8_t* storage) { 171 store->RegisterUint8(name, storage); 172 } 173 174 static void RegisterProperty( 175 PropertyStore* store, const std::string& name, uint16_t* storage) { 176 store->RegisterUint16(name, storage); 177 } 178 179 static void RegisterProperty( 180 PropertyStore* store, const std::string& name, Uint16s* storage) { 181 store->RegisterUint16s(name, storage); 182 } 183 184 static void RegisterProperty( 185 PropertyStore* store, const std::string& name, uint32_t* storage) { 186 store->RegisterUint32(name, storage); 187 } 188 189 protected: 190 Manager* manager() { return &manager_; } 191 MockControl* control_interface() { return &control_interface_; } 192 EventDispatcher* dispatcher() { return &dispatcher_; } 193 MockGLib* glib() { return &glib_; } 194 MockMetrics* metrics() { return &metrics_; } 195 const std::vector<Technology::Identifier>& default_technology_order() { 196 return default_technology_order_; 197 } 198 199 const std::string& run_path() const { return path_; } 200 const std::string& storage_path() const { return path_; } 201 202 const std::string& internal_error() const { return internal_error_; } 203 const std::string& invalid_args() const { return invalid_args_; } 204 const std::string& invalid_prop() const { return invalid_prop_; } 205 206 private: 207 const std::string internal_error_; 208 const std::string invalid_args_; 209 const std::string invalid_prop_; 210 base::ScopedTempDir dir_; 211 const std::string path_; 212 213 MockControl control_interface_; 214 EventDispatcherForTest dispatcher_; 215 testing::NiceMock<MockMetrics> metrics_; 216 MockGLib glib_; 217 const std::vector<Technology::Identifier> default_technology_order_; 218 Manager manager_; 219}; 220 221} // namespace shill 222 223#endif // SHILL_PROPERTY_STORE_UNITTEST_H_ 224