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 BoolProperty bp(®, "there", true);
142 EXPECT_EQ(1, log.size());
143 bp.HandleGesturesPropWritten();
144 EXPECT_EQ(2, log.size());
145 }
146
147 // Mock GesturesPropProvider
MockGesturesPropCreateBool(void * data,const char * name,GesturesPropBool * loc,size_t count,const GesturesPropBool * init)148 GesturesProp* MockGesturesPropCreateBool(void* data, const char* name,
149 GesturesPropBool* loc,
150 size_t count,
151 const GesturesPropBool* init) {
152 GesturesProp *dummy = new GesturesProp();
153 *loc = true;
154 return dummy;
155 }
156
MockGesturesPropCreateInt(void * data,const char * name,int * loc,size_t count,const int * init)157 GesturesProp* MockGesturesPropCreateInt(void* data, const char* name,
158 int* loc, size_t count,
159 const int* init) {
160 GesturesProp *dummy = new GesturesProp();
161 *loc = 1;
162 return dummy;
163 }
164
MockGesturesPropCreateReal(void * data,const char * name,double * loc,size_t count,const double * init)165 GesturesProp* MockGesturesPropCreateReal(void* data, const char* name,
166 double* loc, size_t count,
167 const double* init) {
168 GesturesProp *dummy = new GesturesProp();
169 *loc = 1.0;
170 return dummy;
171 }
172
MockGesturesPropCreateString(void * data,const char * name,const char ** loc,const char * const init)173 GesturesProp* MockGesturesPropCreateString(void* data, const char* name,
174 const char** loc,
175 const char* const init) {
176 GesturesProp *dummy = new GesturesProp();
177 *loc = "1";
178 return dummy;
179 }
180
MockGesturesPropRegisterHandlers(void * data,GesturesProp * prop,void * handler_data,GesturesPropGetHandler getter,GesturesPropSetHandler setter)181 void MockGesturesPropRegisterHandlers(void* data, GesturesProp* prop,
182 void* handler_data,
183 GesturesPropGetHandler getter,
184 GesturesPropSetHandler setter) {}
185
MockGesturesPropFree(void * data,GesturesProp * prop)186 void MockGesturesPropFree(void* data, GesturesProp* prop) {
187 delete prop;
188 }
189
190 // This tests that if we create a prop, then set the prop provider, and the
191 // prop provider changes the value at that time, that we notify the prop
192 // delegate that the value was changed.
TEST(PropRegistryTest,SetAtCreateShouldNotifyTest)193 TEST(PropRegistryTest, SetAtCreateShouldNotifyTest) {
194 GesturesPropProvider mock_gestures_props_provider = {
195 MockGesturesPropCreateInt,
196 nullptr,
197 MockGesturesPropCreateBool,
198 MockGesturesPropCreateString,
199 MockGesturesPropCreateReal,
200 MockGesturesPropRegisterHandlers,
201 MockGesturesPropFree
202 };
203
204 PropRegistry reg;
205 PropRegistryTestDelegate delegate;
206 BoolProperty my_bool(®, "MyBool", 0);
207 my_bool.SetDelegate(&delegate);
208 DoubleProperty my_double(®, "MyDouble", 0.0);
209 my_double.SetDelegate(&delegate);
210 IntProperty my_int(®, "MyInt", 0);
211 my_int.SetDelegate(&delegate);
212 IntProperty my_int_no_change(®, "MyIntNoChange", 1);
213 my_int_no_change.SetDelegate(&delegate);
214 StringProperty my_string(®, "MyString", "mine");
215 my_string.SetDelegate(&delegate);
216
217 EXPECT_EQ(0, delegate.call_cnt_);
218 reg.SetPropProvider(&mock_gestures_props_provider, nullptr);
219 EXPECT_EQ(4, delegate.call_cnt_);
220 }
221
TEST(PropRegistryTest,DoublePromoteIntTest)222 TEST(PropRegistryTest, DoublePromoteIntTest) {
223 PropRegistry reg;
224 PropRegistryTestDelegate delegate;
225
226 DoubleProperty my_double(®, "MyDouble", 1234.5);
227 my_double.SetDelegate(&delegate);
228 EXPECT_TRUE(strstr(ValueForProperty(my_double).c_str(), "1234.5"));
229 IntProperty my_int(®, "MyInt", 321);
230 my_int.SetDelegate(&delegate);
231 Json::Value my_int_val = my_int.NewValue();
232 EXPECT_TRUE(my_double.SetValue(my_int_val));
233 EXPECT_TRUE(strstr(ValueForProperty(my_double).c_str(), "321"));
234 }
235
TEST(PropRegistryTest,BoolArrayTest)236 TEST(PropRegistryTest, BoolArrayTest) {
237 PropRegistry reg;
238 PropRegistry reg_with_delegate;
239 PropRegistryTestDelegate delegate;
240
241 GesturesPropBool vals[] = { false, true };
242 BoolArrayProperty my_bool_array_w_delegate(
243 ®, "MyBoolArray", vals, 2);
244 my_bool_array_w_delegate.SetDelegate(&delegate);
245 EXPECT_EQ(0, delegate.call_cnt_);
246 my_bool_array_w_delegate.HandleGesturesPropWritten();
247 EXPECT_EQ(1, delegate.call_cnt_);
248 delegate.BoolArrayWasWritten(&my_bool_array_w_delegate);
249 EXPECT_EQ(2, delegate.call_cnt_);
250
251 IntProperty ip1(®, "hi", 567);
252 ip1.SetDelegate(&delegate);
253 StringProperty stp1(®, "hi", "foo");
254 stp1.SetDelegate(&delegate);
255 Json::Value my_bool_array_val = my_bool_array_w_delegate.NewValue();
256 Json::Value my_int_val = ip1.NewValue();
257 Json::Value my_str_val = stp1.NewValue();
258 EXPECT_FALSE(my_bool_array_w_delegate.SetValue(my_int_val));
259 EXPECT_FALSE(my_bool_array_w_delegate.SetValue(my_str_val));
260 EXPECT_TRUE(my_bool_array_w_delegate.SetValue(my_bool_array_val));
261
262 // This code does not do anything but the code coverage
263 // hits for not covered areas due to not running the
264 // unused default virtual functions.
265 PropertyDelegate pd;
266
267 BoolArrayProperty my_bool_array(®, "MyBoolArray", vals, 2);
268 pd.BoolArrayWasWritten(&my_bool_array);
269 }
270
TEST(PropRegistryTest,DoubleArrayTest)271 TEST(PropRegistryTest, DoubleArrayTest) {
272 PropRegistry reg;
273 PropRegistry reg_with_delegate;
274 PropRegistryTestDelegate delegate;
275
276 double vals[] = { 0.0, 1.0 };
277 DoubleArrayProperty my_double_array_w_delegate(
278 ®, "MyDoubleArray", vals, 2);
279 my_double_array_w_delegate.SetDelegate(&delegate);
280 EXPECT_EQ(0, delegate.call_cnt_);
281 my_double_array_w_delegate.HandleGesturesPropWritten();
282 EXPECT_EQ(1, delegate.call_cnt_);
283 delegate.DoubleArrayWasWritten(&my_double_array_w_delegate);
284 EXPECT_EQ(2, delegate.call_cnt_);
285
286 IntProperty ip1(®, "hi", 567);
287 ip1.SetDelegate(&delegate);
288 StringProperty stp1(®, "hi", "foo");
289 stp1.SetDelegate(&delegate);
290 Json::Value my_double_array_val = my_double_array_w_delegate.NewValue();
291 Json::Value my_int_val = ip1.NewValue();
292 Json::Value my_str_val = stp1.NewValue();
293 EXPECT_FALSE(my_double_array_w_delegate.SetValue(my_int_val));
294 EXPECT_FALSE(my_double_array_w_delegate.SetValue(my_str_val));
295 EXPECT_TRUE(my_double_array_w_delegate.SetValue(my_double_array_val));
296
297 // This code does not do anything but the code coverage
298 // hits for not covered areas due to not running the
299 // unused default virtual functions.
300 PropertyDelegate pd;
301
302 DoubleArrayProperty my_double_array(®, "MyDoubleArray", vals, 2);
303 pd.DoubleArrayWasWritten(&my_double_array);
304 }
305
TEST(PropRegistryTest,IntArrayTest)306 TEST(PropRegistryTest, IntArrayTest) {
307 PropRegistry reg;
308 PropRegistry reg_with_delegate;
309 PropRegistryTestDelegate delegate;
310
311 int vals[] = { 0, 1 };
312 IntArrayProperty my_int_array_w_delegate(
313 ®, "MyIntArray", vals, 2);
314 my_int_array_w_delegate.SetDelegate(&delegate);
315 EXPECT_EQ(0, delegate.call_cnt_);
316 my_int_array_w_delegate.HandleGesturesPropWritten();
317 EXPECT_EQ(1, delegate.call_cnt_);
318 delegate.IntArrayWasWritten(&my_int_array_w_delegate);
319 EXPECT_EQ(2, delegate.call_cnt_);
320
321 IntProperty ip1(®, "hi", 567);
322 ip1.SetDelegate(&delegate);
323 StringProperty stp1(®, "hi", "foo");
324 stp1.SetDelegate(&delegate);
325 Json::Value my_int_array_val = my_int_array_w_delegate.NewValue();
326 Json::Value my_int_val = ip1.NewValue();
327 Json::Value my_str_val = stp1.NewValue();
328 EXPECT_FALSE(my_int_array_w_delegate.SetValue(my_int_val));
329 EXPECT_FALSE(my_int_array_w_delegate.SetValue(my_str_val));
330 EXPECT_TRUE(my_int_array_w_delegate.SetValue(my_int_array_val));
331
332 // This code does not do anything but the code coverage
333 // hits for not covered areas due to not running the
334 // unused default virtual functions.
335 PropertyDelegate pd;
336
337 IntArrayProperty my_int_array(®, "MyIntArray", vals, 2);
338 pd.IntArrayWasWritten(&my_int_array);
339 }
340
341 } // namespace gestures
342