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