xref: /aosp_15_r20/external/libchrome-gestures/src/gestures_unittest.cc (revision aed3e5085e770be5b69ce25295ecf6ddf906af95)
1 // Copyright 2012 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 <gtest/gtest.h>
6 #include <memory>
7 #include <stdio.h>
8 
9 #include "include/gestures.h"
10 #include "include/macros.h"
11 #include "include/unittest_util.h"
12 
13 namespace gestures {
14 
15 using std::string;
16 
17 class GesturesTest : public ::testing::Test {};
18 
TEST(GesturesTest,SameFingersAsTest)19 TEST(GesturesTest, SameFingersAsTest) {
20   FingerState finger_states[] = {
21     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
22     {0, 0, 0, 0, 1, 0, 0, 0, 1, 0},
23     {0, 0, 0, 0, 1, 0, 0, 0, 1, 0},
24     {0, 0, 0, 0, 1, 0, 0, 0, 2, 0},
25     {0, 0, 0, 0, 1, 0, 0, 0, 3, 0},
26     {0, 0, 0, 0, 1, 0, 0, 0, 4, 0},
27     {0, 0, 0, 0, 1, 0, 0, 0, 5, 0}
28   };
29   HardwareState hardware_state[] = {
30     // time, buttons, finger count, finger states pointer
31     make_hwstate(200000, 0, 1, 1, &finger_states[0]),
32     make_hwstate(200001, 0, 1, 1, &finger_states[1]),
33     make_hwstate(200001, 0, 2, 2, &finger_states[1]),
34     make_hwstate(200001, 0, 2, 2, &finger_states[2]),
35   };
36 
37   EXPECT_TRUE(hardware_state[0].SameFingersAs(hardware_state[1]));
38   EXPECT_FALSE(hardware_state[0].SameFingersAs(hardware_state[2]));
39   EXPECT_TRUE(hardware_state[2].SameFingersAs(hardware_state[2]));
40   EXPECT_FALSE(hardware_state[2].SameFingersAs(hardware_state[3]));
41 }
42 
TEST(GesturesTest,GestureStringTest)43 TEST(GesturesTest, GestureStringTest) {
44   Gesture null;
45   EXPECT_TRUE(strstr(null.String().c_str(), "null"));
46 
47   Gesture move(kGestureMove, 1.0, 2.0, 3.0, 4.0);
48   EXPECT_TRUE(strstr(move.String().c_str(), "1"));
49   EXPECT_TRUE(strstr(move.String().c_str(), "2"));
50   EXPECT_TRUE(strstr(move.String().c_str(), "3"));
51   EXPECT_TRUE(strstr(move.String().c_str(), "4"));
52 
53   Gesture scroll(kGestureScroll, 1.0, 2.0, 3.0, 4.0);
54   EXPECT_TRUE(strstr(scroll.String().c_str(), "1"));
55   EXPECT_TRUE(strstr(scroll.String().c_str(), "2"));
56   EXPECT_TRUE(strstr(scroll.String().c_str(), "3"));
57   EXPECT_TRUE(strstr(scroll.String().c_str(), "4"));
58 
59   Gesture buttons(kGestureButtonsChange, 1.0, 2.0, 3, 4, false);
60   EXPECT_TRUE(strstr(buttons.String().c_str(), "1"));
61   EXPECT_TRUE(strstr(buttons.String().c_str(), "2"));
62   EXPECT_TRUE(strstr(buttons.String().c_str(), "3"));
63   EXPECT_TRUE(strstr(buttons.String().c_str(), "4"));
64 
65   Gesture mousewheel(kGestureMouseWheel, 1.0, 2.0, 3.0, 4.0, 3, 4);
66   EXPECT_TRUE(strstr(mousewheel.String().c_str(), "1"));
67   EXPECT_TRUE(strstr(mousewheel.String().c_str(), "2"));
68   EXPECT_TRUE(strstr(mousewheel.String().c_str(), "3"));
69   EXPECT_TRUE(strstr(mousewheel.String().c_str(), "4"));
70 
71   Gesture pinch(kGesturePinch, 1.0, 2.0, 3.0, 4.0);
72   EXPECT_TRUE(strstr(pinch.String().c_str(), "1"));
73   EXPECT_TRUE(strstr(pinch.String().c_str(), "2"));
74   EXPECT_TRUE(strstr(pinch.String().c_str(), "3"));
75   EXPECT_TRUE(strstr(pinch.String().c_str(), "4"));
76 
77   Gesture swipe(kGestureSwipe, 1.0, 2.0, 3.0, 4.0);
78   EXPECT_TRUE(strstr(swipe.String().c_str(), "1"));
79   EXPECT_TRUE(strstr(swipe.String().c_str(), "2"));
80   EXPECT_TRUE(strstr(swipe.String().c_str(), "3"));
81   EXPECT_TRUE(strstr(swipe.String().c_str(), "4"));
82 
83   Gesture swipelift(kGestureSwipeLift, 1.0, 2.0);
84   EXPECT_TRUE(strstr(swipelift.String().c_str(), "1"));
85   EXPECT_TRUE(strstr(swipelift.String().c_str(), "2"));
86 
87   Gesture swipe4f(kGestureFourFingerSwipe, 1.0, 2.0, 3.0, 4.0);
88   EXPECT_TRUE(strstr(swipe4f.String().c_str(), "1"));
89   EXPECT_TRUE(strstr(swipe4f.String().c_str(), "2"));
90   EXPECT_TRUE(strstr(swipe4f.String().c_str(), "3"));
91   EXPECT_TRUE(strstr(swipe4f.String().c_str(), "4"));
92 
93   Gesture swipe4flift(kGestureFourFingerSwipeLift, 1.0, 2.0);
94   EXPECT_TRUE(strstr(swipe4flift.String().c_str(), "1"));
95   EXPECT_TRUE(strstr(swipe4flift.String().c_str(), "2"));
96 
97   Gesture metrics(kGestureMetrics, 1.0, 2.0,
98                   kGestureMetricsTypeMouseMovement, 3.0, 4.0);
99   EXPECT_TRUE(strstr(metrics.String().c_str(), "1"));
100   EXPECT_TRUE(strstr(metrics.String().c_str(), "2"));
101 
102   Gesture contact_initiated;
103   contact_initiated.type = kGestureTypeContactInitiated;
104   EXPECT_TRUE(strstr(contact_initiated.String().c_str(), "nitiated"));
105 }
106 
TEST(GesturesTest,GestureEqTest)107 TEST(GesturesTest, GestureEqTest) {
108   Gesture null;
109   Gesture null2;
110   EXPECT_TRUE(null == null2);
111   EXPECT_FALSE(null != null2);
112 
113   Gesture move(kGestureMove, 1.0, 2.0, 3.0, 4.0);
114   Gesture move2(kGestureMove, 1.0, 2.0, 3.0, 4.0);
115   Gesture move_ne0(kGestureMove, 9.0, 2.0, 3.0, 4.0);
116   Gesture move_ne1(kGestureMove, 1.0, 9.0, 3.0, 4.0);
117   Gesture move_ne2(kGestureMove, 1.0, 2.0, 9.0, 4.0);
118   Gesture move_ne3(kGestureMove, 1.0, 2.0, 3.0, 9.0);
119   EXPECT_TRUE(move == move2);
120   EXPECT_FALSE(move == move_ne0);
121   EXPECT_FALSE(move == move_ne1);
122   EXPECT_FALSE(move == move_ne2);
123   EXPECT_FALSE(move == move_ne3);
124   EXPECT_FALSE(move != move2);
125   EXPECT_TRUE(move != move_ne0);
126   EXPECT_TRUE(move != move_ne1);
127   EXPECT_TRUE(move != move_ne2);
128   EXPECT_TRUE(move != move_ne3);
129 
130   Gesture scroll(kGestureScroll, 1.0, 2.0, 3.0, 4.0);
131   Gesture scroll2(kGestureScroll, 1.0, 2.0, 3.0, 4.0);
132   Gesture scroll_ne0(kGestureScroll, 9.0, 2.0, 3.0, 4.0);
133   Gesture scroll_ne1(kGestureScroll, 1.0, 9.0, 3.0, 4.0);
134   Gesture scroll_ne2(kGestureScroll, 1.0, 2.0, 9.0, 4.0);
135   Gesture scroll_ne3(kGestureScroll, 1.0, 2.0, 3.0, 9.0);
136   EXPECT_TRUE(scroll == scroll2);
137   EXPECT_FALSE(scroll == scroll_ne0);
138   EXPECT_FALSE(scroll == scroll_ne1);
139   EXPECT_FALSE(scroll == scroll_ne2);
140   EXPECT_FALSE(scroll == scroll_ne3);
141   EXPECT_FALSE(scroll != scroll2);
142   EXPECT_TRUE(scroll != scroll_ne0);
143   EXPECT_TRUE(scroll != scroll_ne1);
144   EXPECT_TRUE(scroll != scroll_ne2);
145   EXPECT_TRUE(scroll != scroll_ne3);
146 
147   Gesture buttons(kGestureButtonsChange, 1.0, 2.0, 3, 4, false);
148   Gesture buttons2(kGestureButtonsChange, 1.0, 2.0, 3, 4, false);
149   Gesture buttons_ne0(kGestureButtonsChange, 9.0, 2.0, 3, 4, false);
150   Gesture buttons_ne1(kGestureButtonsChange, 1.0, 9.0, 3, 4, false);
151   Gesture buttons_ne2(kGestureButtonsChange, 1.0, 2.0, 9, 4, false);
152   Gesture buttons_ne3(kGestureButtonsChange, 1.0, 2.0, 3, 9, false);
153   EXPECT_TRUE(buttons == buttons2);
154   EXPECT_FALSE(buttons == buttons_ne0);
155   EXPECT_FALSE(buttons == buttons_ne1);
156   EXPECT_FALSE(buttons == buttons_ne2);
157   EXPECT_FALSE(buttons == buttons_ne3);
158   EXPECT_FALSE(buttons != buttons2);
159   EXPECT_TRUE(buttons != buttons_ne0);
160   EXPECT_TRUE(buttons != buttons_ne1);
161   EXPECT_TRUE(buttons != buttons_ne2);
162   EXPECT_TRUE(buttons != buttons_ne3);
163 
164   Gesture fling(kGestureFling, 1.0, 2.0, 3.0, 4.0, GESTURES_FLING_START);
165   Gesture fling2(kGestureFling, 1.0, 2.0, 3.0, 4.0, GESTURES_FLING_TAP_DOWN);
166   Gesture fling_ne0(kGestureFling, 1.0, 2.0, 5.0, 4.0, GESTURES_FLING_START);
167   Gesture fling_ne1(kGestureFling, 1.0, 2.0, 3.0, 5.0, GESTURES_FLING_START);
168   Gesture fling_ne2(kGestureFling, 5.0, 2.0, 3.0, 4.0, GESTURES_FLING_START);
169   Gesture fling_ne3(kGestureFling, 1.0, 5.0, 3.0, 4.0, GESTURES_FLING_START);
170   EXPECT_TRUE(fling == fling2);
171   EXPECT_FALSE(fling == fling_ne0);
172   EXPECT_FALSE(fling == fling_ne1);
173   EXPECT_FALSE(fling == fling_ne2);
174   EXPECT_FALSE(fling == fling_ne3);
175   EXPECT_FALSE(fling != fling2);
176   EXPECT_TRUE(fling != fling_ne0);
177   EXPECT_TRUE(fling != fling_ne1);
178   EXPECT_TRUE(fling != fling_ne2);
179   EXPECT_TRUE(fling != fling_ne3);
180 
181   Gesture contact_initiated;
182   contact_initiated.type = kGestureTypeContactInitiated;
183   Gesture contact_initiated2;
184   contact_initiated2.type = kGestureTypeContactInitiated;
185   EXPECT_TRUE(contact_initiated == contact_initiated2);
186   EXPECT_FALSE(contact_initiated != contact_initiated2);
187 
188   Gesture wheelmouse(kGestureMouseWheel, 1.0, 2.0, 3.0, 4.0, 3, 4);
189   Gesture wheelmouse2(kGestureMouseWheel, 1.0, 2.0, 3.0, 4.0, 3, 4);
190   Gesture wheelmouse_ne0(kGestureMouseWheel, 9.0, 2.0, 3.0, 4.0, 3, 4);
191   Gesture wheelmouse_ne1(kGestureMouseWheel, 1.0, 9.0, 3.0, 4.0, 3, 4);
192   Gesture wheelmouse_ne2(kGestureMouseWheel, 1.0, 2.0, 9.0, 4.0, 3, 4);
193   Gesture wheelmouse_ne3(kGestureMouseWheel, 1.0, 2.0, 3.0, 9.0, 3, 4);
194   Gesture wheelmouse_ne4(kGestureMouseWheel, 1.0, 2.0, 3.0, 4.0, 9, 4);
195   Gesture wheelmouse_ne5(kGestureMouseWheel, 1.0, 2.0, 3.0, 4.0, 3, 9);
196   EXPECT_TRUE(wheelmouse == wheelmouse2);
197   EXPECT_FALSE(wheelmouse == wheelmouse_ne0);
198   EXPECT_FALSE(wheelmouse == wheelmouse_ne1);
199   EXPECT_FALSE(wheelmouse == wheelmouse_ne2);
200   EXPECT_FALSE(wheelmouse == wheelmouse_ne3);
201   EXPECT_FALSE(wheelmouse == wheelmouse_ne4);
202   EXPECT_FALSE(wheelmouse == wheelmouse_ne5);
203   EXPECT_FALSE(wheelmouse != wheelmouse2);
204   EXPECT_TRUE(wheelmouse != wheelmouse_ne0);
205   EXPECT_TRUE(wheelmouse != wheelmouse_ne1);
206   EXPECT_TRUE(wheelmouse != wheelmouse_ne2);
207   EXPECT_TRUE(wheelmouse != wheelmouse_ne3);
208   EXPECT_TRUE(wheelmouse != wheelmouse_ne4);
209   EXPECT_TRUE(wheelmouse != wheelmouse_ne5);
210 
211   Gesture pinch(kGesturePinch, 1.0, 2.0, 3.0, 4.0);
212   Gesture pinch2(kGesturePinch, 1.0, 2.0, 3.0, 4.0);
213   Gesture pinch_ne0(kGesturePinch, 9.0, 2.0, 3.0, 4.0);
214   Gesture pinch_ne1(kGesturePinch, 1.0, 9.0, 3.0, 4.0);
215   Gesture pinch_ne2(kGesturePinch, 1.0, 2.0, 9.0, 4.0);
216   EXPECT_TRUE(pinch == pinch2);
217   EXPECT_FALSE(pinch == pinch_ne0);
218   EXPECT_FALSE(pinch == pinch_ne1);
219   EXPECT_FALSE(pinch == pinch_ne2);
220   EXPECT_FALSE(pinch != pinch2);
221   EXPECT_TRUE(pinch != pinch_ne0);
222   EXPECT_TRUE(pinch != pinch_ne1);
223   EXPECT_TRUE(pinch != pinch_ne2);
224 
225   Gesture swipe(kGestureSwipe, 1.0, 2.0, 3.0, 4.0);
226   Gesture swipe2(kGestureSwipe, 1.0, 2.0, 3.0, 4.0);
227   Gesture swipe_ne0(kGestureSwipe, 9.0, 2.0, 3.0, 4.0);
228   Gesture swipe_ne1(kGestureSwipe, 1.0, 9.0, 3.0, 4.0);
229   Gesture swipe_ne2(kGestureSwipe, 1.0, 2.0, 9.0, 4.0);
230   EXPECT_TRUE(swipe == swipe2);
231   EXPECT_FALSE(swipe == swipe_ne0);
232   EXPECT_FALSE(swipe == swipe_ne1);
233   EXPECT_FALSE(swipe == swipe_ne2);
234   EXPECT_FALSE(swipe != swipe2);
235   EXPECT_TRUE(swipe != swipe_ne0);
236   EXPECT_TRUE(swipe != swipe_ne1);
237   EXPECT_TRUE(swipe != swipe_ne2);
238 
239   Gesture swipelift(kGestureSwipeLift, 1.0, 2.0);
240   Gesture swipelift2(kGestureSwipeLift, 1.0, 2.0);
241   Gesture swipelift_ne0(kGestureSwipeLift, 9.0, 2.0);
242   Gesture swipelift_ne1(kGestureSwipeLift, 1.0, 9.0);
243   EXPECT_TRUE(swipelift == swipelift2);
244   EXPECT_FALSE(swipelift == swipelift_ne0);
245   EXPECT_FALSE(swipelift == swipelift_ne1);
246   EXPECT_FALSE(swipelift != swipelift2);
247   EXPECT_TRUE(swipelift != swipelift_ne0);
248   EXPECT_TRUE(swipelift != swipelift_ne1);
249 
250   Gesture swipe4f(kGestureFourFingerSwipe, 1.0, 2.0, 3.0, 4.0);
251   Gesture swipe4f2(kGestureFourFingerSwipe, 1.0, 2.0, 3.0, 4.0);
252   Gesture swipe4f_ne0(kGestureFourFingerSwipe, 9.0, 2.0, 3.0, 4.0);
253   Gesture swipe4f_ne1(kGestureFourFingerSwipe, 1.0, 9.0, 3.0, 4.0);
254   Gesture swipe4f_ne2(kGestureFourFingerSwipe, 1.0, 2.0, 9.0, 4.0);
255   EXPECT_TRUE(swipe4f == swipe4f2);
256   EXPECT_FALSE(swipe4f == swipe4f_ne0);
257   EXPECT_FALSE(swipe4f == swipe4f_ne1);
258   EXPECT_FALSE(swipe4f == swipe4f_ne2);
259   EXPECT_FALSE(swipe4f != swipe4f2);
260   EXPECT_TRUE(swipe4f != swipe4f_ne0);
261   EXPECT_TRUE(swipe4f != swipe4f_ne1);
262   EXPECT_TRUE(swipe4f != swipe4f_ne2);
263 
264   Gesture swipe4flift(kGestureFourFingerSwipeLift, 1.0, 2.0);
265   Gesture swipe4flift2(kGestureFourFingerSwipeLift, 1.0, 2.0);
266   Gesture swipe4flift_ne0(kGestureFourFingerSwipeLift, 9.0, 2.0);
267   Gesture swipe4flift_ne1(kGestureFourFingerSwipeLift, 1.0, 9.0);
268   EXPECT_TRUE(swipe4flift == swipe4flift2);
269   EXPECT_FALSE(swipe4flift == swipe4flift_ne0);
270   EXPECT_FALSE(swipe4flift == swipe4flift_ne1);
271   EXPECT_FALSE(swipe4flift != swipe4flift2);
272   EXPECT_TRUE(swipe4flift != swipe4flift_ne0);
273   EXPECT_TRUE(swipe4flift != swipe4flift_ne1);
274 
275   Gesture metrics(kGestureMetrics, 1.0, 2.0,
276                   kGestureMetricsTypeMouseMovement, 3.0, 4.0);
277   Gesture metrics2(kGestureMetrics, 1.0, 2.0,
278                   kGestureMetricsTypeMouseMovement, 3.0, 4.0);
279   Gesture metrics_ne0(kGestureMetrics, 9.0, 2.0,
280                   kGestureMetricsTypeMouseMovement, 3.0, 4.0);
281   Gesture metrics_ne1(kGestureMetrics, 1.0, 9.0,
282                   kGestureMetricsTypeMouseMovement, 3.0, 4.0);
283   Gesture metrics_ne2(kGestureMetrics, 1.0, 2.0,
284                   kGestureMetricsTypeNoisyGround, 3.0, 4.0);
285   Gesture metrics_ne3(kGestureMetrics, 1.0, 2.0,
286                   kGestureMetricsTypeMouseMovement, 9.0, 4.0);
287   Gesture metrics_ne4(kGestureMetrics, 1.0, 2.0,
288                   kGestureMetricsTypeMouseMovement, 3.0, 9.0);
289   EXPECT_TRUE(metrics == metrics2);
290   EXPECT_FALSE(metrics == metrics_ne0);
291   EXPECT_FALSE(metrics == metrics_ne1);
292   EXPECT_FALSE(metrics == metrics_ne2);
293   EXPECT_FALSE(metrics == metrics_ne3);
294   EXPECT_FALSE(metrics == metrics_ne4);
295   EXPECT_FALSE(metrics != metrics2);
296   EXPECT_TRUE(metrics != metrics_ne0);
297   EXPECT_TRUE(metrics != metrics_ne1);
298   EXPECT_TRUE(metrics != metrics_ne2);
299   EXPECT_TRUE(metrics != metrics_ne3);
300   EXPECT_TRUE(metrics != metrics_ne4);
301 
302   // Compare different types, should all fail to equate
303   Gesture* gs[] = { &null, &move, &scroll, &buttons, &contact_initiated };
304   for (size_t i = 0; i < arraysize(gs); ++i) {
305     for (size_t j = 0; j < arraysize(gs); ++j) {
306       if (i == j)
307         continue;
308       EXPECT_FALSE(*gs[i] == *gs[j]) << "i=" << i << ", j=" << j;
309       EXPECT_TRUE(*gs[i] != *gs[j]) << "i=" << i << ", j=" << j;
310     }
311   }
312 }
313 
TEST(GesturesTest,SimpleTest)314 TEST(GesturesTest, SimpleTest) {
315   // Simple allocate/free test
316   std::unique_ptr<GestureInterpreter> gs(NewGestureInterpreter());
317   EXPECT_NE(nullptr, gs.get());
318   EXPECT_EQ(nullptr, gs.get()->interpreter());
319 
320   GestureInterpreter* gs_version_under = NewGestureInterpreterImpl(0);
321   EXPECT_EQ(nullptr, gs_version_under);
322   GestureInterpreter* gs_version_over = NewGestureInterpreterImpl(1000);
323   EXPECT_EQ(nullptr, gs_version_over);
324 
325   GestureInterpreter* gs_ptr = NewGestureInterpreter();
326   EXPECT_NE(nullptr, gs_ptr);
327   gs_ptr->Initialize(GESTURES_DEVCLASS_TOUCHPAD);
328   DeleteGestureInterpreter(gs_ptr);
329 
330   gs_ptr = NewGestureInterpreter();
331   EXPECT_NE(nullptr, gs_ptr);
332   gs_ptr->Initialize(GESTURES_DEVCLASS_TOUCHSCREEN);
333   DeleteGestureInterpreter(gs_ptr);
334 
335   gs_ptr = NewGestureInterpreter();
336   EXPECT_NE(nullptr, gs_ptr);
337   gs_ptr->Initialize(GESTURES_DEVCLASS_MOUSE);
338   DeleteGestureInterpreter(gs_ptr);
339 
340   gs_ptr = NewGestureInterpreter();
341   EXPECT_NE(nullptr, gs_ptr);
342   gs_ptr->Initialize(GESTURES_DEVCLASS_POINTING_STICK);
343   DeleteGestureInterpreter(gs_ptr);
344 
345   gs_ptr = NewGestureInterpreter();
346   EXPECT_NE(nullptr, gs_ptr);
347   gs_ptr->Initialize(GESTURES_DEVCLASS_MULTITOUCH_MOUSE);
348   std::string activity = gs_ptr->EncodeActivityLog();
349   EXPECT_NE(activity.size(), 0);
350   DeleteGestureInterpreter(gs_ptr);
351 
352   EXPECT_EQ("1073741824", FingerState::FlagsString(1 << 30));
353 }
354 
TEST(GesturesTest,CtorTest)355 TEST(GesturesTest, CtorTest) {
356   Gesture move_gs(kGestureMove, 2, 3, 4.0, 5.0);
357   EXPECT_EQ(move_gs.type, kGestureTypeMove);
358   EXPECT_EQ(move_gs.start_time, 2);
359   EXPECT_EQ(move_gs.end_time, 3);
360   EXPECT_EQ(move_gs.details.move.dx, 4.0);
361   EXPECT_EQ(move_gs.details.move.dy, 5.0);
362 
363   Gesture scroll_gs(kGestureScroll, 2, 3, 4.0, 5.0);
364   EXPECT_EQ(scroll_gs.type, kGestureTypeScroll);
365   EXPECT_EQ(scroll_gs.start_time, 2);
366   EXPECT_EQ(scroll_gs.end_time, 3);
367   EXPECT_EQ(scroll_gs.details.scroll.dx, 4.0);
368   EXPECT_EQ(scroll_gs.details.scroll.dy, 5.0);
369 
370   Gesture bdown_gs(kGestureButtonsChange, 2, 3, GESTURES_BUTTON_LEFT, 0, false);
371   EXPECT_EQ(bdown_gs.type, kGestureTypeButtonsChange);
372   EXPECT_EQ(bdown_gs.start_time, 2);
373   EXPECT_EQ(bdown_gs.end_time, 3);
374   EXPECT_EQ(bdown_gs.details.buttons.down, GESTURES_BUTTON_LEFT);
375   EXPECT_EQ(bdown_gs.details.buttons.up, 0);
376 
377   Gesture bup_gs(kGestureButtonsChange, 2, 3, 0, GESTURES_BUTTON_LEFT, false);
378   EXPECT_EQ(bup_gs.type, kGestureTypeButtonsChange);
379   EXPECT_EQ(bup_gs.start_time, 2);
380   EXPECT_EQ(bup_gs.end_time, 3);
381   EXPECT_EQ(bup_gs.details.buttons.down, 0);
382   EXPECT_EQ(bup_gs.details.buttons.up, GESTURES_BUTTON_LEFT);
383 
384   Gesture bdownup_gs(
385       kGestureButtonsChange, 2, 3,
386       GESTURES_BUTTON_LEFT, GESTURES_BUTTON_LEFT, false);
387   EXPECT_EQ(bdownup_gs.type, kGestureTypeButtonsChange);
388   EXPECT_EQ(bdownup_gs.start_time, 2);
389   EXPECT_EQ(bdownup_gs.end_time, 3);
390   EXPECT_EQ(bdownup_gs.details.buttons.down, GESTURES_BUTTON_LEFT);
391   EXPECT_EQ(bdownup_gs.details.buttons.up, GESTURES_BUTTON_LEFT);
392 }
393 
TEST(GesturesTest,StimeFromTimevalTest)394 TEST(GesturesTest, StimeFromTimevalTest) {
395   struct timeval tv;
396   tv.tv_sec = 3;
397   tv.tv_usec = 88;
398   EXPECT_DOUBLE_EQ(3.000088, StimeFromTimeval(&tv));
399   tv.tv_sec = 2000000000;
400   tv.tv_usec = 999999;
401   EXPECT_DOUBLE_EQ(2000000000.999999, StimeFromTimeval(&tv));
402 }
403 
TEST(GesturesTest,StimeFromTimespecTest)404 TEST(GesturesTest, StimeFromTimespecTest) {
405   struct timespec tv;
406   tv.tv_sec = 3;
407   tv.tv_nsec = 88;
408   EXPECT_DOUBLE_EQ(3.000000088, StimeFromTimespec(&tv));
409   tv.tv_sec = 2000000000;
410   tv.tv_nsec = 999999999;
411   EXPECT_DOUBLE_EQ(2000000000.999999999, StimeFromTimespec(&tv));
412 }
413 
TEST(GesturesTest,FingerStateFlagsStringTest)414 TEST(GesturesTest, FingerStateFlagsStringTest) {
415   EXPECT_EQ("no flags", FingerState::FlagsString(0));
416   EXPECT_EQ("PALM",
417             FingerState::FlagsString(GESTURES_FINGER_PALM));
418   EXPECT_EQ("PALM | WARP_X_MOVE",
419             FingerState::FlagsString(
420                 GESTURES_FINGER_PALM | GESTURES_FINGER_WARP_X_MOVE));
421   // 1 << 31 probably won't be used as a finger flag value anytime soon, so use
422   // it to test prepending the remaining number.
423   EXPECT_EQ("2147483648 | PALM",
424             FingerState::FlagsString(GESTURES_FINGER_PALM | (1 << 31)));
425 }
426 
TEST(GesturesTest,HardwareStateGetFingerStateTest)427 TEST(GesturesTest, HardwareStateGetFingerStateTest) {
428   FingerState fs[] = {
429     { 0, 0, 0, 0, 1, 0, 150, 4000, 4, 0 },
430     { 0, 0, 0, 0, 1, 0, 550, 2000, 2, 0 },
431     { 0, 0, 0, 0, 1, 0, 250, 3000, 7, 0 }
432   };
433   HardwareState hs = make_hwstate(10000, 0, 3, 3, &fs[0]);
434   EXPECT_EQ(&fs[0], hs.GetFingerState(4));
435   EXPECT_EQ(&fs[1], hs.GetFingerState(2));
436   EXPECT_EQ(&fs[2], hs.GetFingerState(7));
437   EXPECT_EQ(nullptr, hs.GetFingerState(8));
438 
439   const HardwareState& const_hs = hs;
440   EXPECT_EQ(&fs[0], const_hs.GetFingerState(4));
441   EXPECT_EQ(&fs[1], const_hs.GetFingerState(2));
442   EXPECT_EQ(&fs[2], const_hs.GetFingerState(7));
443   EXPECT_EQ(nullptr, hs.GetFingerState(8));
444 }
445 
TEST(GesturesTest,HardwarePropertiesToStringTest)446 TEST(GesturesTest, HardwarePropertiesToStringTest) {
447   HardwareProperties hp = {
448     .left = 1009.5, .top = 1002.4, .right = 1003.9, .bottom = 1004.5,
449     .res_x = 1005.4, .res_y = 1006.9,
450     .orientation_minimum = -1,
451     .orientation_maximum = 2,
452     .max_finger_cnt = 12,
453     .max_touch_cnt = 11,
454     .supports_t5r2 = 0, .support_semi_mt = 1, .is_button_pad = 1,
455     .has_wheel = 0, .wheel_is_hi_res = 0,
456     .is_haptic_pad = 0,
457   };
458   string str = hp.String();
459   fprintf(stderr, "str: %s\n", str.c_str());
460   // expect all these numbers in order
461   const char* expected[] = {
462     "1009.5",
463     "1002.4",
464     "1003.9",
465     "1004.5",
466     "1005.4",
467     "1006.9",
468     "12,",
469     "11,",
470     "0,",
471     "1,",
472     "1 "
473   };
474   const char* last_found = str.c_str();
475   for (size_t i = 0; i < arraysize(expected); i++) {
476     ASSERT_NE(nullptr, last_found);
477     const char* found = strstr(last_found, expected[i]);
478     EXPECT_GE(found, last_found) << "i=" << i;
479     last_found = found;
480   }
481 }
482 
TEST(GesturesTest,HardwareStateToStringTest)483 TEST(GesturesTest, HardwareStateToStringTest) {
484   FingerState fs[] = {
485     { 1.0, 2.0, 3.0, 4.5, 30.0, 11.0, 20.0, 30.0, 14,
486       GESTURES_FINGER_WARP_Y_NON_MOVE | GESTURES_FINGER_PALM },
487     { 1.5, 2.5, 3.5, 5.0, 30.5, 11.5, 20.5, 30.5, 15,
488       GESTURES_FINGER_WARP_X_NON_MOVE }
489   };
490 
491   HardwareState hs[] = {
492     make_hwstate(1.123, 1, 2, 2, fs),
493     make_hwstate(2.123, 0, 0, 0, nullptr)
494   };
495 
496   const char* expected[] = {
497     "1.0",
498     "2.0",
499     "3.0",
500     "4.5",
501     "30.0",
502     "11.0",
503     "20.0",
504     "30.0",
505     "14",
506     "WARP_Y_NON_MOVE",
507     "PALM",
508     "1.5",
509     "2.5",
510     "3.5",
511     "5.0",
512     "30.5",
513     "11.5",
514     "20.5",
515     "30.5",
516     "15",
517     "WARP_X_NON_MOVE",
518     "1.123",
519     "buttons 0x1",
520     "2 f",
521     "2 t",
522   };
523   const char* short_expected[] = {
524     "2.123",
525     "buttons 0x0",
526     "0 f",
527     "0 t",
528     "{}",
529   };
530   string long_str = hs[0].String();
531   string short_str = hs[1].String();
532 
533   for (size_t i = 0; i < arraysize(expected); i++) {
534     EXPECT_NE(nullptr, strstr(long_str.c_str(), expected[i]))
535         << "\"" << long_str << "\" should contain \"" << expected[i] << "\"";
536   }
537   for (size_t i = 0; i < arraysize(short_expected); i++) {
538     EXPECT_NE(nullptr, strstr(short_str.c_str(), short_expected[i]))
539         << "\"" << short_str << "\" should contain \"" << short_expected[i]
540         << "\"";
541   }
542 
543   return;
544 }
545 
TEST(GesturesTest,HardwareStateDeepCopyWithFingersTest)546 TEST(GesturesTest, HardwareStateDeepCopyWithFingersTest) {
547   FingerState fingerStates[] = {
548     { 1.0, 2.0, 3.0, 4.5, 30.0, 11.0, 20.0, 30.0, 14, 0 },
549     { 1.5, 2.5, 3.5, 5.0, 30.5, 11.5, 20.5, 30.5, 15, 0 }
550   };
551   const HardwareState hardwareState = make_hwstate(1.123, 1, 2, 2, fingerStates);
552 
553   HardwareState hardwareStateCopy;
554   hardwareStateCopy.fingers = new FingerState[hardwareState.finger_cnt];
555   hardwareStateCopy.DeepCopy(hardwareState, hardwareState.finger_cnt);
556 
557   EXPECT_EQ(hardwareStateCopy.String(), hardwareState.String());
558   delete[] hardwareStateCopy.fingers;
559 }
560 
TEST(GesturesTest,HardwareStateDeepCopyWithoutFingersTest)561 TEST(GesturesTest, HardwareStateDeepCopyWithoutFingersTest) {
562   const HardwareState hardwareState = make_hwstate(1.123, 1, 0, 2, nullptr);
563 
564   HardwareState hardwareStateCopy;
565   hardwareStateCopy.DeepCopy(hardwareState, hardwareState.finger_cnt);
566 
567   EXPECT_EQ(hardwareStateCopy.String(), hardwareState.String());
568 }
569 
TEST(GesturesTest,InvalidHardwareStateDeepCopyTest)570 TEST(GesturesTest, InvalidHardwareStateDeepCopyTest) {
571   // 2 finger_cnt without any fingersState(s) specified
572   const HardwareState invalidHardwareState = make_hwstate(1.123, 1, 2, 2, nullptr);
573 
574   HardwareState hardwareStateCopy;
575   hardwareStateCopy.DeepCopy(invalidHardwareState, invalidHardwareState.finger_cnt);
576 
577   EXPECT_EQ(invalidHardwareState.timestamp, hardwareStateCopy.timestamp);
578   EXPECT_EQ(invalidHardwareState.buttons_down, hardwareStateCopy.buttons_down);
579   EXPECT_EQ(invalidHardwareState.finger_cnt, hardwareStateCopy.finger_cnt);
580   EXPECT_EQ(invalidHardwareState.touch_cnt, hardwareStateCopy.touch_cnt);
581   EXPECT_EQ(invalidHardwareState.fingers, hardwareStateCopy.fingers);
582   EXPECT_EQ(invalidHardwareState.rel_x, hardwareStateCopy.rel_x);
583   EXPECT_EQ(invalidHardwareState.rel_y, hardwareStateCopy.rel_y);
584   EXPECT_EQ(invalidHardwareState.rel_wheel, hardwareStateCopy.rel_wheel);
585   EXPECT_EQ(invalidHardwareState.rel_wheel_hi_res, hardwareStateCopy.rel_wheel_hi_res);
586   EXPECT_EQ(invalidHardwareState.rel_hwheel, hardwareStateCopy.rel_wheel);
587   EXPECT_EQ(invalidHardwareState.msc_timestamp, hardwareStateCopy.msc_timestamp);
588 }
589 
590 }  // namespace gestures
591