1 // Copyright 2022 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 
7 #include "include/metrics_filter_interpreter.h"
8 #include "include/unittest_util.h"
9 
10 namespace gestures {
11 
12 class MetricsFilterInterpreterTest : public ::testing::Test {};
13 
14 class MetricsFilterInterpreterTestInterpreter : public Interpreter {
15  public:
MetricsFilterInterpreterTestInterpreter()16   MetricsFilterInterpreterTestInterpreter()
17       : Interpreter(NULL, NULL, false),
18         handle_timer_called_(false) {}
19 
SyncInterpret(HardwareState * hwstate,stime_t * timeout)20   virtual void SyncInterpret(HardwareState* hwstate, stime_t* timeout) {
21     EXPECT_NE(static_cast<HardwareState*>(NULL), hwstate);
22     EXPECT_EQ(1, hwstate->finger_cnt);
23     prev_ = hwstate->fingers[0];
24   }
25 
HandleTimer(stime_t now,stime_t * timeout)26   virtual void HandleTimer(stime_t now, stime_t* timeout) {
27     handle_timer_called_ = true;
28   }
29 
30   FingerState prev_;
31   bool handle_timer_called_;
32 };
33 
34 
TEST(MetricsFilterInterpreterTest,SimpleTestTouchpad)35 TEST(MetricsFilterInterpreterTest, SimpleTestTouchpad) {
36   MetricsFilterInterpreterTestInterpreter* base_interpreter =
37       new MetricsFilterInterpreterTestInterpreter;
38   MetricsFilterInterpreter interpreter(NULL, base_interpreter, NULL,
39                                        GESTURES_DEVCLASS_TOUCHPAD);
40 
41   HardwareProperties hwprops = {
42     0, 0, 100, 100,  // left, top, right, bottom
43     1, 1,  // x res (pixels/mm), y res (pixels/mm)
44     1, 1,  // scrn DPI X, Y
45     -1,  // orientation minimum
46     2,   // orientation maximum
47     5, 5,  // max fingers, max_touch,
48     0, 0, 1,  // t5r2, semi, button pad
49     0, 0,  // has wheel, vertical wheel is high resolution
50     0,  // haptic pad
51   };
52   TestInterpreterWrapper wrapper(&interpreter, &hwprops);
53 
54   EXPECT_FALSE(base_interpreter->handle_timer_called_);
55   wrapper.HandleTimer(0.0, NULL);
56   EXPECT_TRUE(base_interpreter->handle_timer_called_);
57 
58   FingerState finger_states[] = {
59     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
60     // Consistent movement for 16 frames
61     {0, 0, 0, 0, 20, 0, 40, 20, 1, 0}, // 0
62     {0, 0, 0, 0, 20, 0, 40, 25, 1, 0}, // 1
63     {0, 0, 0, 0, 20, 0, 40, 30, 1, 0}, // 2
64     {0, 0, 0, 0, 20, 0, 40, 35, 1, 0}, // 3
65     {0, 0, 0, 0, 20, 0, 40, 40, 1, 0}, // 4
66     {0, 0, 0, 0, 20, 0, 40, 45, 1, 0}, // 5
67     {0, 0, 0, 0, 20, 0, 40, 50, 1, 0}, // 6
68     {0, 0, 0, 0, 20, 0, 40, 55, 1, 0}, // 7
69     {0, 0, 0, 0, 20, 0, 40, 60, 1, 0}, // 8
70     {0, 0, 0, 0, 20, 0, 40, 65, 1, 0}, // 9
71     {0, 0, 0, 0, 20, 0, 40, 70, 1, 0}, // 10
72     {0, 0, 0, 0, 20, 0, 40, 75, 1, 0}, // 11
73     {0, 0, 0, 0, 20, 0, 40, 80, 1, 0}, // 12
74     {0, 0, 0, 0, 20, 0, 40, 85, 1, 0}, // 13
75     {0, 0, 0, 0, 20, 0, 40, 90, 1, 0}, // 14
76     {0, 0, 0, 0, 20, 0, 40, 95, 1, 0}, // 15
77   };
78 
79   HardwareState hardware_states[] = {
80     // time, buttons, finger count, touch count, finger states pointer
81     make_hwstate(1.00, 0, 1, 1, &finger_states[0]),
82     make_hwstate(1.01, 0, 1, 1, &finger_states[1]),
83     make_hwstate(1.02, 0, 1, 1, &finger_states[2]),
84     make_hwstate(1.03, 0, 1, 1, &finger_states[3]),
85     make_hwstate(1.04, 0, 1, 1, &finger_states[4]),
86     make_hwstate(1.05, 0, 1, 1, &finger_states[5]),
87     make_hwstate(1.06, 0, 1, 1, &finger_states[6]),
88     make_hwstate(1.07, 0, 1, 1, &finger_states[7]),
89     make_hwstate(1.08, 0, 1, 1, &finger_states[8]),
90     make_hwstate(1.09, 0, 1, 1, &finger_states[9]),
91     make_hwstate(1.10, 0, 1, 1, &finger_states[10]),
92     make_hwstate(1.11, 0, 1, 1, &finger_states[11]),
93     make_hwstate(1.12, 0, 1, 1, &finger_states[12]),
94     make_hwstate(1.13, 0, 1, 1, &finger_states[13]),
95     make_hwstate(1.14, 0, 1, 1, &finger_states[14]),
96     make_hwstate(1.15, 0, 1, 1, &finger_states[15]),
97   };
98 
99   for (size_t i = 0; i < arraysize(hardware_states); i++) {
100     wrapper.SyncInterpret(&hardware_states[i], NULL);
101   }
102 
103   EXPECT_EQ(interpreter.devclass_, GESTURES_DEVCLASS_TOUCHPAD);
104   EXPECT_EQ(interpreter.mouse_movement_session_index_, 0);
105   EXPECT_EQ(interpreter.mouse_movement_current_session_length, 0);
106   EXPECT_EQ(interpreter.mouse_movement_current_session_start, 0.0);
107   EXPECT_EQ(interpreter.mouse_movement_current_session_last, 0.0);
108   EXPECT_EQ(interpreter.mouse_movement_current_session_distance, 0.0);
109   EXPECT_EQ(interpreter.noisy_ground_distance_threshold_.val_, 10.0);
110   EXPECT_EQ(interpreter.noisy_ground_time_threshold_.val_, 0.1);
111   EXPECT_EQ(interpreter.mouse_moving_time_threshold_.val_, 0.05);
112   EXPECT_EQ(interpreter.mouse_control_warmup_sessions_.val_, 100);
113 }
114 
TEST(MetricsFilterInterpreterTest,SimpleTestMultitouchMouse)115 TEST(MetricsFilterInterpreterTest, SimpleTestMultitouchMouse) {
116   MetricsFilterInterpreterTestInterpreter* base_interpreter =
117       new MetricsFilterInterpreterTestInterpreter;
118   MetricsFilterInterpreter interpreter(NULL, base_interpreter, NULL,
119                                        GESTURES_DEVCLASS_MULTITOUCH_MOUSE);
120 
121   HardwareProperties hwprops = {
122     0, 0, 100, 100,  // left, top, right, bottom
123     1, 1,  // x res (pixels/mm), y res (pixels/mm)
124     1, 1,  // scrn DPI X, Y
125     -1,  // orientation minimum
126     2,   // orientation maximum
127     5, 5,  // max fingers, max_touch,
128     0, 0, 1,  // t5r2, semi, button pad
129     0, 0,  // has wheel, vertical wheel is high resolution
130     0,  // haptic pad
131   };
132   TestInterpreterWrapper wrapper(&interpreter, &hwprops);
133 
134   EXPECT_FALSE(base_interpreter->handle_timer_called_);
135   wrapper.HandleTimer(0.0, NULL);
136   EXPECT_TRUE(base_interpreter->handle_timer_called_);
137 
138   FingerState finger_states[] = {
139     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
140     // Consistent movement for 16 frames
141     {0, 0, 0, 0, 20, 0, 40, 20, 1, 0}, // 0
142     {0, 0, 0, 0, 20, 0, 40, 25, 1, 0}, // 1
143     {0, 0, 0, 0, 20, 0, 40, 30, 1, 0}, // 2
144     {0, 0, 0, 0, 20, 0, 40, 35, 1, 0}, // 3
145     {0, 0, 0, 0, 20, 0, 40, 40, 1, 0}, // 4
146     {0, 0, 0, 0, 20, 0, 40, 45, 1, 0}, // 5
147     {0, 0, 0, 0, 20, 0, 40, 50, 1, 0}, // 6
148     {0, 0, 0, 0, 20, 0, 40, 55, 1, 0}, // 7
149     {0, 0, 0, 0, 20, 0, 40, 60, 1, 0}, // 8
150     {0, 0, 0, 0, 20, 0, 40, 65, 1, 0}, // 9
151     {0, 0, 0, 0, 20, 0, 40, 70, 1, 0}, // 10
152     {0, 0, 0, 0, 20, 0, 40, 75, 1, 0}, // 11
153     {0, 0, 0, 0, 20, 0, 40, 80, 1, 0}, // 12
154     {0, 0, 0, 0, 20, 0, 40, 85, 1, 0}, // 13
155     {0, 0, 0, 0, 20, 0, 40, 90, 1, 0}, // 14
156     {0, 0, 0, 0, 20, 0, 40, 95, 1, 0}, // 15
157   };
158 
159   HardwareState hardware_states[] = {
160     // time, buttons, finger count, touch count, finger states pointer
161     make_hwstate(1.00, 0, 1, 1, &finger_states[0]),
162     make_hwstate(1.01, 0, 1, 1, &finger_states[1]),
163     make_hwstate(1.02, 0, 1, 1, &finger_states[2]),
164     make_hwstate(1.03, 0, 1, 1, &finger_states[3]),
165     make_hwstate(1.04, 0, 1, 1, &finger_states[4]),
166     make_hwstate(1.05, 0, 1, 1, &finger_states[5]),
167     make_hwstate(1.06, 0, 1, 1, &finger_states[6]),
168     make_hwstate(1.07, 0, 1, 1, &finger_states[7]),
169     make_hwstate(1.08, 0, 1, 1, &finger_states[8]),
170     make_hwstate(1.09, 0, 1, 1, &finger_states[9]),
171     make_hwstate(1.10, 0, 1, 1, &finger_states[10]),
172     make_hwstate(1.11, 0, 1, 1, &finger_states[11]),
173     make_hwstate(1.12, 0, 1, 1, &finger_states[12]),
174     make_hwstate(1.13, 0, 1, 1, &finger_states[13]),
175     make_hwstate(1.14, 0, 1, 1, &finger_states[14]),
176     make_hwstate(1.15, 0, 1, 1, &finger_states[15]),
177   };
178 
179   for (size_t i = 0; i < arraysize(hardware_states); i++) {
180     wrapper.SyncInterpret(&hardware_states[i], NULL);
181   }
182 
183   EXPECT_EQ(interpreter.devclass_, GESTURES_DEVCLASS_MULTITOUCH_MOUSE);
184   EXPECT_EQ(interpreter.mouse_movement_session_index_, 0);
185   EXPECT_EQ(interpreter.mouse_movement_current_session_length, 0);
186   EXPECT_EQ(interpreter.mouse_movement_current_session_start, 0.0);
187   EXPECT_EQ(interpreter.mouse_movement_current_session_last, 0.0);
188   EXPECT_EQ(interpreter.mouse_movement_current_session_distance, 0.0);
189   EXPECT_EQ(interpreter.noisy_ground_distance_threshold_.val_, 10.0);
190   EXPECT_EQ(interpreter.noisy_ground_time_threshold_.val_, 0.1);
191   EXPECT_EQ(interpreter.mouse_moving_time_threshold_.val_, 0.05);
192   EXPECT_EQ(interpreter.mouse_control_warmup_sessions_.val_, 100);
193 }
194 
TEST(MetricsFilterInterpreterTest,SimpleTestPointingStick)195 TEST(MetricsFilterInterpreterTest, SimpleTestPointingStick) {
196   MetricsFilterInterpreterTestInterpreter* base_interpreter =
197       new MetricsFilterInterpreterTestInterpreter;
198   MetricsFilterInterpreter interpreter(NULL, base_interpreter, NULL,
199                                        GESTURES_DEVCLASS_POINTING_STICK);
200 
201   HardwareProperties hwprops = {
202     0, 0, 100, 100,  // left, top, right, bottom
203     1, 1,  // x res (pixels/mm), y res (pixels/mm)
204     1, 1,  // scrn DPI X, Y
205     -1,  // orientation minimum
206     2,   // orientation maximum
207     5, 5,  // max fingers, max_touch,
208     0, 0, 1,  // t5r2, semi, button pad
209     0, 0,  // has wheel, vertical wheel is high resolution
210     0,  // haptic pad
211   };
212   TestInterpreterWrapper wrapper(&interpreter, &hwprops);
213 
214   EXPECT_FALSE(base_interpreter->handle_timer_called_);
215   wrapper.HandleTimer(0.0, NULL);
216   EXPECT_TRUE(base_interpreter->handle_timer_called_);
217 
218   EXPECT_EQ(interpreter.devclass_, GESTURES_DEVCLASS_POINTING_STICK);
219   EXPECT_EQ(interpreter.mouse_movement_session_index_, 0);
220   EXPECT_EQ(interpreter.mouse_movement_current_session_length, 0);
221   EXPECT_EQ(interpreter.mouse_movement_current_session_start, 0.0);
222   EXPECT_EQ(interpreter.mouse_movement_current_session_last, 0.0);
223   EXPECT_EQ(interpreter.mouse_movement_current_session_distance, 0.0);
224   EXPECT_EQ(interpreter.noisy_ground_distance_threshold_.val_, 10.0);
225   EXPECT_EQ(interpreter.noisy_ground_time_threshold_.val_, 0.1);
226   EXPECT_EQ(interpreter.mouse_moving_time_threshold_.val_, 0.05);
227   EXPECT_EQ(interpreter.mouse_control_warmup_sessions_.val_, 100);
228 }
229 
230 }  // namespace gestures
231