1 // Copyright 2011 The ChromiumOS Authors
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 <string>
6
7 #include <gtest/gtest.h>
8
9 #include "include/activity_log.h"
10 #include "include/prop_registry.h"
11
12 using std::string;
13
14 // Mock struct GesturesProp implementation (outside of namespace gestures)
15 struct GesturesProp { };
16
17 namespace gestures {
18
19 class PropRegistryTest : public ::testing::Test {};
20
21 class PropRegistryTestDelegate : public PropertyDelegate {
22 public:
PropRegistryTestDelegate()23 PropRegistryTestDelegate() : call_cnt_(0) {}
BoolWasWritten(BoolProperty * prop)24 virtual void BoolWasWritten(BoolProperty* prop) { call_cnt_++; };
BoolArrayWasWritten(BoolArrayProperty * prop)25 virtual void BoolArrayWasWritten(BoolArrayProperty* prop) { call_cnt_++; };
DoubleWasWritten(DoubleProperty * prop)26 virtual void DoubleWasWritten(DoubleProperty* prop) { call_cnt_++; };
DoubleArrayWasWritten(DoubleArrayProperty * prop)27 virtual void DoubleArrayWasWritten(DoubleArrayProperty* prop) {
28 call_cnt_++;
29 };
IntWasWritten(IntProperty * prop)30 virtual void IntWasWritten(IntProperty* prop) { call_cnt_++; };
IntArrayWasWritten(IntArrayProperty * prop)31 virtual void IntArrayWasWritten(IntArrayProperty* prop) { call_cnt_++; };
StringWasWritten(StringProperty * prop)32 virtual void StringWasWritten(StringProperty* prop) { call_cnt_++; };
33
34 int call_cnt_;
35 };
36
37 namespace {
ValueForProperty(const Property & prop)38 string ValueForProperty(const Property& prop) {
39 Json::Value temp(Json::objectValue);
40 temp["tempkey"] = prop.NewValue();
41 return temp.toStyledString();
42 }
43
44 } // namespace {}
45
TEST(PropRegistryTest,SimpleTest)46 TEST(PropRegistryTest, SimpleTest) {
47 PropRegistry reg;
48 PropRegistryTestDelegate delegate;
49
50 int expected_call_cnt = 0;
51 BoolProperty bp1(®, "hi", false);
52 bp1.SetDelegate(&delegate);
53 EXPECT_TRUE(strstr(ValueForProperty(bp1).c_str(), "false"));
54 bp1.HandleGesturesPropWritten();
55 EXPECT_EQ(++expected_call_cnt, delegate.call_cnt_);
56
57 BoolProperty bp2(®, "hi", true);
58 EXPECT_TRUE(strstr(ValueForProperty(bp2).c_str(), "true"));
59 bp2.HandleGesturesPropWritten();
60 EXPECT_EQ(expected_call_cnt, delegate.call_cnt_);
61
62 DoubleProperty dp1(®, "hi", 2721.0);
63 dp1.SetDelegate(&delegate);
64 EXPECT_TRUE(strstr(ValueForProperty(dp1).c_str(), "2721"));
65 dp1.HandleGesturesPropWritten();
66 EXPECT_EQ(++expected_call_cnt, delegate.call_cnt_);
67
68 DoubleProperty dp2(®, "hi", 3.1);
69 EXPECT_TRUE(strstr(ValueForProperty(dp2).c_str(), "3.1"));
70 dp2.HandleGesturesPropWritten();
71 EXPECT_EQ(expected_call_cnt, delegate.call_cnt_);
72
73 IntProperty ip1(®, "hi", 567);
74 ip1.SetDelegate(&delegate);
75 EXPECT_TRUE(strstr(ValueForProperty(ip1).c_str(), "567"));
76 ip1.HandleGesturesPropWritten();
77 EXPECT_EQ(++expected_call_cnt, delegate.call_cnt_);
78
79 IntProperty ip2(®, "hi", 568);
80 EXPECT_TRUE(strstr(ValueForProperty(ip2).c_str(), "568"));
81 ip2.HandleGesturesPropWritten();
82 EXPECT_EQ(expected_call_cnt, delegate.call_cnt_);
83
84 StringProperty stp1(®, "hi", "foo");
85 stp1.SetDelegate(&delegate);
86 EXPECT_TRUE(strstr(ValueForProperty(stp1).c_str(), "foo"));
87 stp1.HandleGesturesPropWritten();
88 EXPECT_EQ(++expected_call_cnt, delegate.call_cnt_);
89
90 StringProperty stp2(®, "hi", "bar");
91 EXPECT_TRUE(strstr(ValueForProperty(stp2).c_str(), "bar"));
92 stp2.HandleGesturesPropWritten();
93 EXPECT_EQ(expected_call_cnt, delegate.call_cnt_);
94
95
96 Json::Value my_bool_val = bp1.NewValue();
97 Json::Value my_int_val = ip1.NewValue();
98 Json::Value my_double_val = dp1.NewValue();
99 Json::Value my_str_val = stp1.NewValue();
100 EXPECT_TRUE(bp1.SetValue(my_bool_val));
101 EXPECT_FALSE(bp1.SetValue(my_int_val));
102 EXPECT_FALSE(bp1.SetValue(my_double_val));
103 EXPECT_FALSE(bp1.SetValue(my_str_val));
104
105 EXPECT_FALSE(ip1.SetValue(my_bool_val));
106 EXPECT_TRUE(ip1.SetValue(my_int_val));
107 EXPECT_FALSE(ip1.SetValue(my_double_val));
108 EXPECT_FALSE(ip1.SetValue(my_str_val));
109
110 EXPECT_FALSE(dp1.SetValue(my_bool_val));
111 EXPECT_TRUE(dp1.SetValue(my_int_val));
112 EXPECT_TRUE(dp1.SetValue(my_double_val));
113 EXPECT_FALSE(dp1.SetValue(my_str_val));
114
115 EXPECT_FALSE(stp1.SetValue(my_bool_val));
116 EXPECT_FALSE(stp1.SetValue(my_int_val));
117 EXPECT_FALSE(stp1.SetValue(my_double_val));
118 EXPECT_TRUE(stp1.SetValue(my_str_val));
119
120 // This code does not do anything but the code coverage
121 // hits for not covered areas due to not running the
122 // unused default virtual functions.
123 PropertyDelegate pd;
124
125 pd.BoolWasWritten(&bp1);
126 pd.DoubleWasWritten(&dp1);
127 pd.IntWasWritten(&ip1);
128 pd.StringWasWritten(&stp1);
129 }
130
TEST(PropRegistryTest,PropChangeTest)131 TEST(PropRegistryTest, PropChangeTest) {
132 PropRegistry reg;
133 ActivityLog log(®);
134 reg.set_activity_log(&log);
135
136 DoubleProperty dp(®, "hi", 1234.0);
137 EXPECT_EQ(0, log.size());
138 dp.HandleGesturesPropWritten();
139 EXPECT_EQ(1, log.size());
140 }
141
142 // Mock GesturesPropProvider
MockGesturesPropCreateBool(void * data,const char * name,GesturesPropBool * loc,size_t count,const GesturesPropBool * init)143 GesturesProp* MockGesturesPropCreateBool(void* data, const char* name,
144 GesturesPropBool* loc,
145 size_t count,
146 const GesturesPropBool* init) {
147 GesturesProp *dummy = new GesturesProp();
148 *loc = true;
149 return dummy;
150 }
151
MockGesturesPropCreateInt(void * data,const char * name,int * loc,size_t count,const int * init)152 GesturesProp* MockGesturesPropCreateInt(void* data, const char* name,
153 int* loc, size_t count,
154 const int* init) {
155 GesturesProp *dummy = new GesturesProp();
156 *loc = 1;
157 return dummy;
158 }
159
MockGesturesPropCreateReal(void * data,const char * name,double * loc,size_t count,const double * init)160 GesturesProp* MockGesturesPropCreateReal(void* data, const char* name,
161 double* loc, size_t count,
162 const double* init) {
163 GesturesProp *dummy = new GesturesProp();
164 *loc = 1.0;
165 return dummy;
166 }
167
MockGesturesPropCreateString(void * data,const char * name,const char ** loc,const char * const init)168 GesturesProp* MockGesturesPropCreateString(void* data, const char* name,
169 const char** loc,
170 const char* const init) {
171 GesturesProp *dummy = new GesturesProp();
172 *loc = "1";
173 return dummy;
174 }
175
MockGesturesPropRegisterHandlers(void * data,GesturesProp * prop,void * handler_data,GesturesPropGetHandler getter,GesturesPropSetHandler setter)176 void MockGesturesPropRegisterHandlers(void* data, GesturesProp* prop,
177 void* handler_data,
178 GesturesPropGetHandler getter,
179 GesturesPropSetHandler setter) {}
180
MockGesturesPropFree(void * data,GesturesProp * prop)181 void MockGesturesPropFree(void* data, GesturesProp* prop) {
182 delete prop;
183 }
184
185 // This tests that if we create a prop, then set the prop provider, and the
186 // prop provider changes the value at that time, that we notify the prop
187 // delegate that the value was changed.
TEST(PropRegistryTest,SetAtCreateShouldNotifyTest)188 TEST(PropRegistryTest, SetAtCreateShouldNotifyTest) {
189 GesturesPropProvider mock_gestures_props_provider = {
190 MockGesturesPropCreateInt,
191 NULL,
192 MockGesturesPropCreateBool,
193 MockGesturesPropCreateString,
194 MockGesturesPropCreateReal,
195 MockGesturesPropRegisterHandlers,
196 MockGesturesPropFree
197 };
198
199 PropRegistry reg;
200 PropRegistryTestDelegate delegate;
201 BoolProperty my_bool(®, "MyBool", 0);
202 my_bool.SetDelegate(&delegate);
203 DoubleProperty my_double(®, "MyDouble", 0.0);
204 my_double.SetDelegate(&delegate);
205 IntProperty my_int(®, "MyInt", 0);
206 my_int.SetDelegate(&delegate);
207 IntProperty my_int_no_change(®, "MyIntNoChange", 1);
208 my_int_no_change.SetDelegate(&delegate);
209 StringProperty my_string(®, "MyString", "mine");
210 my_string.SetDelegate(&delegate);
211
212 EXPECT_EQ(0, delegate.call_cnt_);
213 reg.SetPropProvider(&mock_gestures_props_provider, NULL);
214 EXPECT_EQ(4, delegate.call_cnt_);
215 }
216
TEST(PropRegistryTest,DoublePromoteIntTest)217 TEST(PropRegistryTest, DoublePromoteIntTest) {
218 PropRegistry reg;
219 PropRegistryTestDelegate delegate;
220
221 DoubleProperty my_double(®, "MyDouble", 1234.5);
222 my_double.SetDelegate(&delegate);
223 EXPECT_TRUE(strstr(ValueForProperty(my_double).c_str(), "1234.5"));
224 IntProperty my_int(®, "MyInt", 321);
225 my_int.SetDelegate(&delegate);
226 Json::Value my_int_val = my_int.NewValue();
227 EXPECT_TRUE(my_double.SetValue(my_int_val));
228 EXPECT_TRUE(strstr(ValueForProperty(my_double).c_str(), "321"));
229 }
230
TEST(PropRegistryTest,BoolArrayTest)231 TEST(PropRegistryTest, BoolArrayTest) {
232 PropRegistry reg;
233 PropRegistry reg_with_delegate;
234 PropRegistryTestDelegate delegate;
235
236 GesturesPropBool vals[] = { false, true };
237 BoolArrayProperty my_bool_array_w_delegate(
238 ®, "MyBoolArray", vals, 2);
239 my_bool_array_w_delegate.SetDelegate(&delegate);
240 EXPECT_EQ(0, delegate.call_cnt_);
241 my_bool_array_w_delegate.HandleGesturesPropWritten();
242 EXPECT_EQ(1, delegate.call_cnt_);
243 delegate.BoolArrayWasWritten(&my_bool_array_w_delegate);
244 EXPECT_EQ(2, delegate.call_cnt_);
245
246 IntProperty ip1(®, "hi", 567);
247 ip1.SetDelegate(&delegate);
248 StringProperty stp1(®, "hi", "foo");
249 stp1.SetDelegate(&delegate);
250 Json::Value my_bool_array_val = my_bool_array_w_delegate.NewValue();
251 Json::Value my_int_val = ip1.NewValue();
252 Json::Value my_str_val = stp1.NewValue();
253 EXPECT_FALSE(my_bool_array_w_delegate.SetValue(my_int_val));
254 EXPECT_FALSE(my_bool_array_w_delegate.SetValue(my_str_val));
255 EXPECT_TRUE(my_bool_array_w_delegate.SetValue(my_bool_array_val));
256
257 // This code does not do anything but the code coverage
258 // hits for not covered areas due to not running the
259 // unused default virtual functions.
260 PropertyDelegate pd;
261
262 BoolArrayProperty my_bool_array(®, "MyBoolArray", vals, 2);
263 pd.BoolArrayWasWritten(&my_bool_array);
264 }
265
TEST(PropRegistryTest,DoubleArrayTest)266 TEST(PropRegistryTest, DoubleArrayTest) {
267 PropRegistry reg;
268 PropRegistry reg_with_delegate;
269 PropRegistryTestDelegate delegate;
270
271 double vals[] = { 0.0, 1.0 };
272 DoubleArrayProperty my_double_array_w_delegate(
273 ®, "MyDoubleArray", vals, 2);
274 my_double_array_w_delegate.SetDelegate(&delegate);
275 EXPECT_EQ(0, delegate.call_cnt_);
276 my_double_array_w_delegate.HandleGesturesPropWritten();
277 EXPECT_EQ(1, delegate.call_cnt_);
278 delegate.DoubleArrayWasWritten(&my_double_array_w_delegate);
279 EXPECT_EQ(2, delegate.call_cnt_);
280
281 IntProperty ip1(®, "hi", 567);
282 ip1.SetDelegate(&delegate);
283 StringProperty stp1(®, "hi", "foo");
284 stp1.SetDelegate(&delegate);
285 Json::Value my_double_array_val = my_double_array_w_delegate.NewValue();
286 Json::Value my_int_val = ip1.NewValue();
287 Json::Value my_str_val = stp1.NewValue();
288 EXPECT_FALSE(my_double_array_w_delegate.SetValue(my_int_val));
289 EXPECT_FALSE(my_double_array_w_delegate.SetValue(my_str_val));
290 EXPECT_TRUE(my_double_array_w_delegate.SetValue(my_double_array_val));
291
292 // This code does not do anything but the code coverage
293 // hits for not covered areas due to not running the
294 // unused default virtual functions.
295 PropertyDelegate pd;
296
297 DoubleArrayProperty my_double_array(®, "MyDoubleArray", vals, 2);
298 pd.DoubleArrayWasWritten(&my_double_array);
299 }
300
TEST(PropRegistryTest,IntArrayTest)301 TEST(PropRegistryTest, IntArrayTest) {
302 PropRegistry reg;
303 PropRegistry reg_with_delegate;
304 PropRegistryTestDelegate delegate;
305
306 int vals[] = { 0, 1 };
307 IntArrayProperty my_int_array_w_delegate(
308 ®, "MyIntArray", vals, 2);
309 my_int_array_w_delegate.SetDelegate(&delegate);
310 EXPECT_EQ(0, delegate.call_cnt_);
311 my_int_array_w_delegate.HandleGesturesPropWritten();
312 EXPECT_EQ(1, delegate.call_cnt_);
313 delegate.IntArrayWasWritten(&my_int_array_w_delegate);
314 EXPECT_EQ(2, delegate.call_cnt_);
315
316 IntProperty ip1(®, "hi", 567);
317 ip1.SetDelegate(&delegate);
318 StringProperty stp1(®, "hi", "foo");
319 stp1.SetDelegate(&delegate);
320 Json::Value my_int_array_val = my_int_array_w_delegate.NewValue();
321 Json::Value my_int_val = ip1.NewValue();
322 Json::Value my_str_val = stp1.NewValue();
323 EXPECT_FALSE(my_int_array_w_delegate.SetValue(my_int_val));
324 EXPECT_FALSE(my_int_array_w_delegate.SetValue(my_str_val));
325 EXPECT_TRUE(my_int_array_w_delegate.SetValue(my_int_array_val));
326
327 // This code does not do anything but the code coverage
328 // hits for not covered areas due to not running the
329 // unused default virtual functions.
330 PropertyDelegate pd;
331
332 IntArrayProperty my_int_array(®, "MyIntArray", vals, 2);
333 pd.IntArrayWasWritten(&my_int_array);
334 }
335
336 } // namespace gestures
337