blob: 99a4a8931d639d7c8103bbce6d84c1deec18d7ec [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_PROPERTY_STORE_UNITTEST_H_
6#define SHILL_PROPERTY_STORE_UNITTEST_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
Chris Masone9d779932011-08-25 16:33:41 -070012#include <base/memory/scoped_ptr.h>
Paul Stewart5ad16062013-02-21 18:10:48 -080013#include <base/files/scoped_temp_dir.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <dbus-c++/dbus.h>
Chris Masone7156c922011-08-23 20:36:21 -070015#include <gmock/gmock.h>
Chris Masone2ae797d2011-08-23 20:41:00 -070016#include <gtest/gtest.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017
18#include "shill/dbus_adaptor.h"
19#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070020#include "shill/event_dispatcher.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070021#include "shill/manager.h"
22#include "shill/mock_control.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070023#include "shill/mock_glib.h"
Thieu Le3426c8f2012-01-11 17:35:11 -080024#include "shill/mock_metrics.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070025#include "shill/property_store.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070026
27namespace shill {
28
Chris Masoneb925cc82011-06-22 15:39:57 -070029class PropertyStoreTest : public testing::TestWithParam< ::DBus::Variant > {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070030 public:
mukesh agrawale7c7e652013-06-18 17:19:39 -070031 typedef ::testing::Types<bool, int16, int32, std::string, Stringmap,
32 Stringmaps, Strings, uint8, uint16, Uint16s, uint32>
33 PropertyTypes;
34
Chris Masoneb925cc82011-06-22 15:39:57 -070035 // In real code, it's frowned upon to have non-POD static members, as there
36 // can be ordering issues if your constructors have side effects.
37 // These constructors don't, and declaring these as static lets me
38 // autogenerate a bunch of unit test code that I would otherwise need to
Gaurav Shah1b7a6162011-11-09 11:41:01 -080039 // copypaste. So I think it's safe and worth it.
Chris Masoneb925cc82011-06-22 15:39:57 -070040 static const ::DBus::Variant kBoolV;
41 static const ::DBus::Variant kByteV;
42 static const ::DBus::Variant kInt16V;
43 static const ::DBus::Variant kInt32V;
Darin Petkov63138a92012-02-06 14:09:15 +010044 static const ::DBus::Variant kKeyValueStoreV;
Chris Masoneb925cc82011-06-22 15:39:57 -070045 static const ::DBus::Variant kStringV;
46 static const ::DBus::Variant kStringmapV;
47 static const ::DBus::Variant kStringmapsV;
48 static const ::DBus::Variant kStringsV;
49 static const ::DBus::Variant kUint16V;
mukesh agrawale7c7e652013-06-18 17:19:39 -070050 static const ::DBus::Variant kUint16sV;
Chris Masoneb925cc82011-06-22 15:39:57 -070051 static const ::DBus::Variant kUint32V;
Paul Stewarte18c33b2012-07-10 20:48:44 -070052 static const ::DBus::Variant kUint64V;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070053
Chris Masoneb925cc82011-06-22 15:39:57 -070054 PropertyStoreTest();
55 virtual ~PropertyStoreTest();
Chris Masone3bd3c8c2011-06-13 08:20:26 -070056
Chris Masone9d779932011-08-25 16:33:41 -070057 virtual void SetUp();
mukesh agrawalbebf1b82013-04-23 15:06:33 -070058 MOCK_METHOD1(TestCallback, void(const std::string &property_name));
Chris Masone9d779932011-08-25 16:33:41 -070059
mukesh agrawale7c7e652013-06-18 17:19:39 -070060 // Convenience overloads for GetProperty. Normally, we don't overload
61 // functions. But this is extremely useful for type-parameterized tests.
62 static bool GetProperty(const PropertyStore &store, const std::string &name,
63 bool *storage, Error *error) {
64 return store.GetBoolProperty(name, storage, error);
65 }
66
67 static bool GetProperty(const PropertyStore &store, const std::string &name,
68 int16 *storage, Error *error) {
69 return store.GetInt16Property(name, storage, error);
70 }
71
72 static bool GetProperty(const PropertyStore &store, const std::string &name,
73 int32 *storage, Error *error) {
74 return store.GetInt32Property(name, storage, error);
75 }
76
77 static bool GetProperty(const PropertyStore &store, const std::string &name,
78 std::string *storage, Error *error) {
79 return store.GetStringProperty(name, storage, error);
80 }
81
82 static bool GetProperty(const PropertyStore &store, const std::string &name,
83 Stringmap *storage, Error *error) {
84 return store.GetStringmapProperty(name, storage, error);
85 }
86
87 static bool GetProperty(const PropertyStore &store, const std::string &name,
88 Stringmaps *storage, Error *error) {
89 return store.GetStringmapsProperty(name, storage, error);
90 }
91
92 static bool GetProperty(const PropertyStore &store, const std::string &name,
93 Strings *storage, Error *error) {
94 return store.GetStringsProperty(name, storage, error);
95 }
96
97 static bool GetProperty(const PropertyStore &store, const std::string &name,
98 uint8 *storage, Error *error) {
99 return store.GetUint8Property(name, storage, error);
100 }
101
102 static bool GetProperty(const PropertyStore &store, const std::string &name,
103 uint16 *storage, Error *error) {
104 return store.GetUint16Property(name, storage, error);
105 }
106
107 static bool GetProperty(const PropertyStore &store, const std::string &name,
108 Uint16s *storage, Error *error) {
109 return store.GetUint16sProperty(name, storage, error);
110 }
111
112 static bool GetProperty(const PropertyStore &store, const std::string &name,
113 uint32 *storage, Error *error) {
114 return store.GetUint32Property(name, storage, error);
115 }
116
117 // Convenience overloads for RegisterProperty. Normally, we don't overload
118 // functions. But this is extremely useful for type-parameterized tests.
119 static void RegisterProperty(
120 PropertyStore *store, const std::string &name, bool *storage) {
121 store->RegisterBool(name, storage);
122 }
123
124 static void RegisterProperty(
125 PropertyStore *store, const std::string &name, int16 *storage) {
126 store->RegisterInt16(name, storage);
127 }
128
129 static void RegisterProperty(
130 PropertyStore *store, const std::string &name, int32 *storage) {
131 store->RegisterInt32(name, storage);
132 }
133
134 static void RegisterProperty(
135 PropertyStore *store, const std::string &name, std::string *storage) {
136 store->RegisterString(name, storage);
137 }
138
139 static void RegisterProperty(
140 PropertyStore *store, const std::string &name, Stringmap *storage) {
141 store->RegisterStringmap(name, storage);
142 }
143
144 static void RegisterProperty(
145 PropertyStore *store, const std::string &name, Stringmaps *storage) {
146 store->RegisterStringmaps(name, storage);
147 }
148
149 static void RegisterProperty(
150 PropertyStore *store, const std::string &name, Strings *storage) {
151 store->RegisterStrings(name, storage);
152 }
153
154 static void RegisterProperty(
155 PropertyStore *store, const std::string &name, uint8 *storage) {
156 store->RegisterUint8(name, storage);
157 }
158
159 static void RegisterProperty(
160 PropertyStore *store, const std::string &name, uint16 *storage) {
161 store->RegisterUint16(name, storage);
162 }
163
164 static void RegisterProperty(
165 PropertyStore *store, const std::string &name, Uint16s *storage) {
166 store->RegisterUint16s(name, storage);
167 }
168
169 static void RegisterProperty(
170 PropertyStore *store, const std::string &name, uint32 *storage) {
171 store->RegisterUint32(name, storage);
172 }
173
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700174 protected:
Chris Masone9d779932011-08-25 16:33:41 -0700175 Manager *manager() { return &manager_; }
Chris Masone2176a882011-09-14 22:29:15 -0700176 MockControl *control_interface() { return &control_interface_; }
177 EventDispatcher *dispatcher() { return &dispatcher_; }
178 MockGLib *glib() { return &glib_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800179 MockMetrics *metrics() { return &metrics_; }
Chris Masone9d779932011-08-25 16:33:41 -0700180
181 const std::string &run_path() const { return path_; }
182 const std::string &storage_path() const { return path_; }
183
mukesh agrawalffa3d042011-10-06 15:26:10 -0700184 const std::string &internal_error() const { return internal_error_; }
Chris Masone9d779932011-08-25 16:33:41 -0700185 const std::string &invalid_args() const { return invalid_args_; }
186 const std::string &invalid_prop() const { return invalid_prop_; }
187
Chris Masone9d779932011-08-25 16:33:41 -0700188 private:
mukesh agrawalffa3d042011-10-06 15:26:10 -0700189 const std::string internal_error_;
Chris Masone9d779932011-08-25 16:33:41 -0700190 const std::string invalid_args_;
191 const std::string invalid_prop_;
Paul Stewart5ad16062013-02-21 18:10:48 -0800192 base::ScopedTempDir dir_;
Chris Masone9d779932011-08-25 16:33:41 -0700193 const std::string path_;
Chris Masone2176a882011-09-14 22:29:15 -0700194 MockControl control_interface_;
195 EventDispatcher dispatcher_;
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200196 testing::NiceMock<MockMetrics> metrics_;
Chris Masone2176a882011-09-14 22:29:15 -0700197 MockGLib glib_;
Chris Masone9d779932011-08-25 16:33:41 -0700198 Manager manager_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700199};
200
201} // namespace shill
202#endif // SHILL_PROPERTY_STORE_UNITTEST_H_