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/macros.h"
11 #include "include/prop_registry.h"
12 #include "include/unittest_util.h"
13
14 using std::string;
15
16 namespace gestures {
17
18 class ActivityLogTest : public ::testing::Test {};
19
TEST(ActivityLogTest,SimpleTest)20 TEST(ActivityLogTest, SimpleTest) {
21 PropRegistry prop_reg;
22 BoolProperty true_prop(&prop_reg, "true prop", true);
23 BoolProperty false_prop(&prop_reg, "false prop", false);
24 DoubleProperty double_prop(&prop_reg, "double prop", 77.25);
25 IntProperty int_prop(&prop_reg, "int prop", -816);
26 StringProperty string_prop(&prop_reg, "string prop", "foobarstr");
27
28 ActivityLog log(&prop_reg);
29 EXPECT_TRUE(strstr(log.Encode().c_str(), "true"));
30 EXPECT_TRUE(strstr(log.Encode().c_str(), "false"));
31 EXPECT_TRUE(strstr(log.Encode().c_str(), "77.25"));
32 EXPECT_TRUE(strstr(log.Encode().c_str(), "-816"));
33 EXPECT_TRUE(strstr(log.Encode().c_str(), "foobarstr"));
34
35 HardwareProperties hwprops = {
36 .left = 6011,
37 .top = 6012,
38 .right = 6013,
39 .bottom = 6014,
40 .res_x = 6015,
41 .res_y = 6016,
42 .orientation_minimum = 6019,
43 .orientation_maximum = 6020,
44 .max_finger_cnt = 6021,
45 .max_touch_cnt = 6022,
46 .supports_t5r2 = 1,
47 .support_semi_mt = 0,
48 .is_button_pad = 1,
49 .has_wheel = 0,
50 .wheel_is_hi_res = 0,
51 .is_haptic_pad = 0,
52 };
53
54 log.SetHardwareProperties(hwprops);
55
56 const char* expected_strings[] = {
57 "6011", "6012", "6013", "6014", "6015",
58 "6016", "6019", "6020", "6021", "6022"
59 };
60 string hwprops_log = log.Encode();
61 for (size_t i = 0; i < arraysize(expected_strings); i++)
62 EXPECT_TRUE(strstr(hwprops_log.c_str(), expected_strings[i]));
63
64 EXPECT_EQ(0, log.size());
65 EXPECT_GT(log.MaxSize(), 10);
66
67 FingerState fs = { 0.0, 0.0, 0.0, 0.0, 9.0, 0.0, 3.0, 4.0, 22, 0 };
68 HardwareState hs = make_hwstate(1.0, 0, 1, 1, &fs);
69 log.LogHardwareState(hs);
70 EXPECT_EQ(1, log.size());
71 EXPECT_TRUE(strstr(log.Encode().c_str(), "22"));
72 ActivityLog::Entry* entry = log.GetEntry(0);
73 EXPECT_TRUE(std::holds_alternative<HardwareState>(entry->details));
74
75 log.LogTimerCallback(234.5);
76 EXPECT_EQ(2, log.size());
77 EXPECT_TRUE(strstr(log.Encode().c_str(), "234.5"));
78 entry = log.GetEntry(1);
79 EXPECT_TRUE(std::holds_alternative<ActivityLog::TimerCallbackEntry>
80 (entry->details));
81
82 log.LogCallbackRequest(90210);
83 EXPECT_EQ(3, log.size());
84 EXPECT_TRUE(strstr(log.Encode().c_str(), "90210"));
85 entry = log.GetEntry(2);
86 EXPECT_TRUE(std::holds_alternative<ActivityLog::CallbackRequestEntry>
87 (entry->details));
88
89 Gesture null;
90 Gesture move(kGestureMove, 1.0, 2.0, 773, 4.0);
91 Gesture scroll(kGestureScroll, 1.0, 2.0, 312, 4.0);
92 Gesture buttons(kGestureButtonsChange, 1.0, 847, 3, 4, false);
93 Gesture contact_initiated;
94 contact_initiated.type = kGestureTypeContactInitiated;
95 Gesture mousewheel(kGestureMouseWheel, 1.0, 2.0, 30.0, 40.0, 3, 4);
96 Gesture pinch(kGesturePinch, 1.0, 2.0, 3.0, 4.0);
97 Gesture fling(kGestureFling, 1.0, 2.0, 42.0, 24.0, 1);
98 Gesture swipe(kGestureSwipe, 1.0, 2.0, 128.0, 4.0);
99 Gesture swipelift(kGestureSwipeLift, 1.0, 2.0);
100 Gesture swipe4f(kGestureFourFingerSwipe, 1.0, 2.0, 256.0, 4.0);
101 Gesture swipe4flift(kGestureFourFingerSwipeLift, 1.0, 2.0);
102 Gesture metrics(kGestureMetrics, 1.0, 2.0,
103 kGestureMetricsTypeMouseMovement, 3.0, 4.0);
104
105 Gesture* gs[] = {
106 &null, &move, &scroll, &buttons, &contact_initiated,
107 &mousewheel, &pinch, &fling, &swipe, &swipelift,
108 &swipe4f, &swipe4flift, &metrics
109 };
110 const char* test_strs[] = {
111 "null", "773", "312", "847", "nitiated",
112 "30", "3", "42", "128", "null",
113 "256", "null", "1"
114 };
115
116 ASSERT_EQ(arraysize(gs), arraysize(test_strs));
117 for (size_t i = 0; i < arraysize(gs); ++i) {
118 log.LogGesture(*gs[i]);
119 EXPECT_TRUE(strstr(log.Encode().c_str(), test_strs[i]))
120 << "i=" << i;
121 entry = log.GetEntry(log.size() - 1);
122 EXPECT_TRUE(std::holds_alternative<Gesture>(entry->details))
123 << "i=" << i;
124 }
125
126 log.Clear();
127 EXPECT_EQ(0, log.size());
128 }
129
TEST(ActivityLogTest,WrapAroundTest)130 TEST(ActivityLogTest, WrapAroundTest) {
131 ActivityLog log(nullptr);
132 // overfill the buffer
133 const size_t fill_size = (ActivityLog::kBufferSize * 3) / 2;
134 for (size_t i = 0; i < fill_size; i++)
135 log.LogCallbackRequest(static_cast<stime_t>(i));
136 const string::size_type prefix_length = 100;
137 string first_prefix = log.Encode().substr(0, prefix_length);
138 log.LogCallbackRequest(static_cast<stime_t>(fill_size));
139 string second_prefix = log.Encode().substr(0, prefix_length);
140 EXPECT_NE(first_prefix, second_prefix);
141 }
142
TEST(ActivityLogTest,VersionTest)143 TEST(ActivityLogTest, VersionTest) {
144 ActivityLog log(nullptr);
145 string thelog = log.Encode();
146 EXPECT_TRUE(thelog.find(VCSID) != string::npos);
147 }
148
TEST(ActivityLogTest,EncodePropChangeBoolTest)149 TEST(ActivityLogTest, EncodePropChangeBoolTest) {
150 ActivityLog log(nullptr);
151 Json::Value ret;
152
153 ActivityLog::PropChangeEntry bool_prop;
154 bool_prop.name = "boolean";
155 bool_prop.value = static_cast<GesturesPropBool>(true);
156 ret = log.EncodePropChange(bool_prop);
157 EXPECT_EQ(ret[ActivityLog::kKeyType],
158 Json::Value(ActivityLog::kKeyPropChange));
159 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeName],
160 Json::Value(bool_prop.name));
161 EXPECT_EQ(static_cast<GesturesPropBool>(
162 ret[ActivityLog::kKeyPropChangeValue].asBool()),
163 std::get<GesturesPropBool>(bool_prop.value));
164 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeType],
165 ActivityLog::kValuePropChangeTypeBool);
166 }
167
TEST(ActivityLogTest,EncodePropChangeDoubleTest)168 TEST(ActivityLogTest, EncodePropChangeDoubleTest) {
169 ActivityLog log(nullptr);
170 Json::Value ret;
171
172 ActivityLog::PropChangeEntry double_prop;
173 double_prop.name = "double";
174 double_prop.value = 42.0;
175 ret = log.EncodePropChange(double_prop);
176 EXPECT_EQ(ret[ActivityLog::kKeyType],
177 Json::Value(ActivityLog::kKeyPropChange));
178 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeName],
179 Json::Value(double_prop.name));
180 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeValue].asDouble(),
181 std::get<double>(double_prop.value));
182 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeType],
183 ActivityLog::kValuePropChangeTypeDouble);
184 }
185
TEST(ActivityLogTest,EncodePropChangeIntTest)186 TEST(ActivityLogTest, EncodePropChangeIntTest) {
187 ActivityLog log(nullptr);
188 Json::Value ret;
189
190 ActivityLog::PropChangeEntry int_prop;
191 int_prop.name = "int";
192 int_prop.value = 42;
193 ret = log.EncodePropChange(int_prop);
194 EXPECT_EQ(ret[ActivityLog::kKeyType],
195 Json::Value(ActivityLog::kKeyPropChange));
196 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeName],
197 Json::Value(int_prop.name));
198 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeValue].asInt(),
199 std::get<int>(int_prop.value));
200 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeType],
201 ActivityLog::kValuePropChangeTypeInt);
202 }
203
TEST(ActivityLogTest,EncodePropChangeShortTest)204 TEST(ActivityLogTest, EncodePropChangeShortTest) {
205 ActivityLog log(nullptr);
206 Json::Value ret;
207
208 ActivityLog::PropChangeEntry short_prop;
209 short_prop.name = "short";
210 short_prop.value = static_cast<short>(42);
211 ret = log.EncodePropChange(short_prop);
212 EXPECT_EQ(ret[ActivityLog::kKeyType],
213 Json::Value(ActivityLog::kKeyPropChange));
214 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeName],
215 Json::Value(short_prop.name));
216 EXPECT_EQ(static_cast<short>(
217 ret[ActivityLog::kKeyPropChangeValue].asInt()),
218 std::get<short>(short_prop.value));
219 EXPECT_EQ(ret[ActivityLog::kKeyPropChangeType],
220 ActivityLog::kValuePropChangeTypeShort);
221 }
222
TEST(ActivityLogTest,HardwareStatePreTest)223 TEST(ActivityLogTest, HardwareStatePreTest) {
224 PropRegistry prop_reg;
225 ActivityLog log(&prop_reg);
226
227 HardwareProperties hwprops = {
228 .left = 6011,
229 .top = 6012,
230 .right = 6013,
231 .bottom = 6014,
232 .res_x = 6015,
233 .res_y = 6016,
234 .orientation_minimum = 6019,
235 .orientation_maximum = 6020,
236 .max_finger_cnt = 6021,
237 .max_touch_cnt = 6022,
238 .supports_t5r2 = 1,
239 .support_semi_mt = 0,
240 .is_button_pad = 1,
241 .has_wheel = 0,
242 .wheel_is_hi_res = 0,
243 .is_haptic_pad = 0,
244 };
245 log.SetHardwareProperties(hwprops);
246
247 FingerState fs = { 0.0, 0.0, 0.0, 0.0, 9.0, 0.0, 3.0, 4.0, 22, 0 };
248 HardwareState hs = make_hwstate(1.0, 0, 1, 1, &fs);
249
250 ActivityLog::Entry* entry;
251 Json::Value result;
252
253 EXPECT_EQ(0, log.size());
254
255 // Build and log a HardwareStatePre structure
256 log.LogHardwareStatePre("ActivityLogTest_HwStateTest", hs);
257 ASSERT_EQ(1, log.size());
258 entry = log.GetEntry(0);
259 ASSERT_TRUE(std::holds_alternative<ActivityLog::HardwareStatePre>
260 (entry->details));
261
262 // Encode the HardwareStatePre into Json
263 result = log.EncodeCommonInfo();
264 result = result[ActivityLog::kKeyRoot][0];
265
266 // Verify the Json information
267 EXPECT_EQ(result[ActivityLog::kKeyType],
268 Json::Value(ActivityLog::kKeyHardwareStatePre));
269 EXPECT_EQ(result[ActivityLog::kKeyMethodName],
270 Json::Value("ActivityLogTest_HwStateTest"));
271 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateButtonsDown],
272 Json::Value(hs.buttons_down));
273 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateTouchCnt],
274 Json::Value(hs.touch_cnt));
275 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateTimestamp],
276 Json::Value(hs.timestamp));
277 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateRelX], Json::Value(hs.rel_x));
278 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateRelY], Json::Value(hs.rel_y));
279 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateRelWheel],
280 Json::Value(hs.rel_wheel));
281 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateRelHWheel],
282 Json::Value(hs.rel_hwheel));
283 log.Clear();
284 }
285
TEST(ActivityLogTest,HardwareStatePostTest)286 TEST(ActivityLogTest, HardwareStatePostTest) {
287 PropRegistry prop_reg;
288 ActivityLog log(&prop_reg);
289
290 HardwareProperties hwprops = {
291 .left = 6011,
292 .top = 6012,
293 .right = 6013,
294 .bottom = 6014,
295 .res_x = 6015,
296 .res_y = 6016,
297 .orientation_minimum = 6019,
298 .orientation_maximum = 6020,
299 .max_finger_cnt = 6021,
300 .max_touch_cnt = 6022,
301 .supports_t5r2 = 1,
302 .support_semi_mt = 0,
303 .is_button_pad = 1,
304 .has_wheel = 0,
305 .wheel_is_hi_res = 0,
306 .is_haptic_pad = 0,
307 };
308 log.SetHardwareProperties(hwprops);
309
310 FingerState fs = { 0.0, 0.0, 0.0, 0.0, 9.0, 0.0, 3.0, 4.0, 22, 0 };
311 HardwareState hs = make_hwstate(1.0, 0, 1, 1, &fs);
312
313 ActivityLog::Entry* entry;
314 Json::Value result;
315
316 EXPECT_EQ(0, log.size());
317
318 // Build and log a HardwareStatePost structure
319 log.LogHardwareStatePost("ActivityLogTest_HwStateTest", hs);
320 ASSERT_EQ(1, log.size());
321 entry = log.GetEntry(0);
322 ASSERT_TRUE(std::holds_alternative<ActivityLog::HardwareStatePost>
323 (entry->details));
324
325 // Encode the HardwareStatePost into Json
326 result = log.EncodeCommonInfo();
327 result = result[ActivityLog::kKeyRoot][0];
328
329 // Verify the Json information
330 EXPECT_EQ(result[ActivityLog::kKeyType],
331 Json::Value(ActivityLog::kKeyHardwareStatePost));
332 EXPECT_EQ(result[ActivityLog::kKeyMethodName],
333 Json::Value("ActivityLogTest_HwStateTest"));
334 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateButtonsDown],
335 Json::Value(hs.buttons_down));
336 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateTouchCnt],
337 Json::Value(hs.touch_cnt));
338 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateTimestamp],
339 Json::Value(hs.timestamp));
340 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateRelX], Json::Value(hs.rel_x));
341 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateRelY], Json::Value(hs.rel_y));
342 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateRelWheel],
343 Json::Value(hs.rel_wheel));
344 EXPECT_EQ(result[ActivityLog::kKeyHardwareStateRelHWheel],
345 Json::Value(hs.rel_hwheel));
346 log.Clear();
347 }
348
349
TEST(ActivityLogTest,GestureConsumeTest)350 TEST(ActivityLogTest, GestureConsumeTest) {
351 PropRegistry prop_reg;
352 ActivityLog log(&prop_reg);
353 ActivityLog::Entry* entry;
354 Json::Value result;
355
356 EXPECT_EQ(0, log.size());
357
358 // Build and log a GestureConsume structure
359 Gesture move(kGestureMove, 1.0, 2.0, 773, 4.0);
360 log.LogGestureConsume("ActivityLogTest_GestureTest", move);
361 ASSERT_EQ(1, log.size());
362 entry = log.GetEntry(0);
363 ASSERT_TRUE(std::holds_alternative<ActivityLog::GestureConsume>
364 (entry->details));
365
366 // Encode the GestureConsume into Json
367 result = log.EncodeCommonInfo();
368 result = result[ActivityLog::kKeyRoot][0];
369
370 // Verify the Json information
371 EXPECT_EQ(result[ActivityLog::kKeyType],
372 Json::Value(ActivityLog::kKeyGestureConsume));
373 EXPECT_EQ(result[ActivityLog::kKeyMethodName],
374 Json::Value("ActivityLogTest_GestureTest"));
375 EXPECT_EQ(result[ActivityLog::kKeyGestureType],
376 Json::Value(ActivityLog::kValueGestureTypeMove));
377 EXPECT_EQ(result[ActivityLog::kKeyGestureDX],
378 Json::Value(move.details.move.dx));
379 EXPECT_EQ(result[ActivityLog::kKeyGestureDY],
380 Json::Value(move.details.move.dy));
381 EXPECT_EQ(result[ActivityLog::kKeyGestureOrdinalDX],
382 Json::Value(move.details.move.ordinal_dx));
383 EXPECT_EQ(result[ActivityLog::kKeyGestureOrdinalDY],
384 Json::Value(move.details.move.ordinal_dy));
385 log.Clear();
386 }
387
TEST(ActivityLogTest,GestureProduceTest)388 TEST(ActivityLogTest, GestureProduceTest) {
389 PropRegistry prop_reg;
390 ActivityLog log(&prop_reg);
391 ActivityLog::Entry* entry;
392 Json::Value result;
393
394 EXPECT_EQ(0, log.size());
395
396 // Build and log a GestureProduce structure
397 Gesture scroll(kGestureScroll, 1.0, 2.0, 312, 4.0);
398 log.LogGestureProduce("ActivityLogTest_GestureTest", scroll);
399 ASSERT_EQ(1, log.size());
400 entry = log.GetEntry(0);
401 ASSERT_TRUE(std::holds_alternative<ActivityLog::GestureProduce>
402 (entry->details));
403
404 // Encode the GestureProduce into Json
405 result = log.EncodeCommonInfo();
406 result = result[ActivityLog::kKeyRoot][0];
407
408 // Verify the Json information
409 EXPECT_EQ(result[ActivityLog::kKeyType],
410 Json::Value(ActivityLog::kKeyGestureProduce));
411 EXPECT_EQ(result[ActivityLog::kKeyMethodName],
412 Json::Value("ActivityLogTest_GestureTest"));
413 EXPECT_EQ(result[ActivityLog::kKeyGestureType],
414 Json::Value(ActivityLog::kValueGestureTypeScroll));
415 EXPECT_EQ(result[ActivityLog::kKeyGestureDX],
416 Json::Value(scroll.details.scroll.dx));
417 EXPECT_EQ(result[ActivityLog::kKeyGestureDY],
418 Json::Value(scroll.details.scroll.dy));
419 EXPECT_EQ(result[ActivityLog::kKeyGestureOrdinalDX],
420 Json::Value(scroll.details.scroll.ordinal_dx));
421 EXPECT_EQ(result[ActivityLog::kKeyGestureOrdinalDY],
422 Json::Value(scroll.details.scroll.ordinal_dy));
423 log.Clear();
424 }
425
TEST(ActivityLogTest,HandleTimerPreTest)426 TEST(ActivityLogTest, HandleTimerPreTest) {
427 PropRegistry prop_reg;
428 ActivityLog log(&prop_reg);
429 ActivityLog::Entry* entry;
430 Json::Value result;
431 stime_t timeout = 1;
432
433 EXPECT_EQ(0, log.size());
434
435 // Build and log a HandleTimerPre structure
436 log.LogHandleTimerPre("ActivityLogTest_HandleTimerTest", 0, &timeout);
437 EXPECT_EQ(1, log.size());
438 entry = log.GetEntry(0);
439 EXPECT_TRUE(std::holds_alternative<ActivityLog::HandleTimerPre>
440 (entry->details));
441
442 // Encode the HandleTimerPre into Json
443 result = log.EncodeCommonInfo();
444 result = result[ActivityLog::kKeyRoot][0];
445
446 // Verify the Json information
447 EXPECT_EQ(result[ActivityLog::kKeyType],
448 Json::Value(ActivityLog::kKeyHandleTimerPre));
449 EXPECT_EQ(result[ActivityLog::kKeyMethodName],
450 Json::Value("ActivityLogTest_HandleTimerTest"));
451 EXPECT_EQ(result[ActivityLog::kKeyTimerNow],
452 Json::Value(static_cast<stime_t>(0)));
453 EXPECT_EQ(result[ActivityLog::kKeyHandleTimerTimeout], Json::Value(timeout));
454 log.Clear();
455 }
456
TEST(ActivityLogTest,HandleTimerPostTest)457 TEST(ActivityLogTest, HandleTimerPostTest) {
458 PropRegistry prop_reg;
459 ActivityLog log(&prop_reg);
460 ActivityLog::Entry* entry;
461 Json::Value result;
462 stime_t timeout = 1;
463
464 EXPECT_EQ(0, log.size());
465
466 // Build and log a HandleTimerPost structure
467 log.LogHandleTimerPost("ActivityLogTest_HandleTimerTest", 0, &timeout);
468 EXPECT_EQ(1, log.size());
469 entry = log.GetEntry(0);
470 EXPECT_TRUE(std::holds_alternative<ActivityLog::HandleTimerPost>
471 (entry->details));
472
473 // Encode the HandleTimerPost into Json
474 result = log.EncodeCommonInfo();
475 result = result[ActivityLog::kKeyRoot][0];
476
477 // Verify the Json information
478 EXPECT_EQ(result[ActivityLog::kKeyType],
479 Json::Value(ActivityLog::kKeyHandleTimerPost));
480 EXPECT_EQ(result[ActivityLog::kKeyMethodName],
481 Json::Value("ActivityLogTest_HandleTimerTest"));
482 EXPECT_EQ(result[ActivityLog::kKeyTimerNow],
483 Json::Value(static_cast<stime_t>(0)));
484 EXPECT_EQ(result[ActivityLog::kKeyHandleTimerTimeout], Json::Value(timeout));
485 log.Clear();
486 }
487
488 } // namespace gestures
489