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 <string>
6
7 #include <gtest/gtest.h>
8
9 #include "include/activity_replay.h"
10 #include "include/gestures.h"
11 #include "include/interpreter.h"
12 #include "include/prop_registry.h"
13 #include "include/unittest_util.h"
14 #include "include/util.h"
15
16 using std::string;
17
18 namespace gestures {
19
20 class InterpreterTest : public ::testing::Test {};
21
22 class InterpreterTestInterpreter : public Interpreter {
23 public:
InterpreterTestInterpreter(PropRegistry * prop_reg)24 explicit InterpreterTestInterpreter(PropRegistry* prop_reg)
25 : Interpreter(prop_reg, nullptr, true),
26 expected_hwstate_(nullptr),
27 interpret_call_count_(0),
28 handle_timer_call_count_(0),
29 bool_prop_(prop_reg, "BoolProp", 0),
30 double_prop_(prop_reg, "DoubleProp", 0),
31 int_prop_(prop_reg, "IntProp", 0),
32 string_prop_(prop_reg, "StringProp", "") {
33 InitName();
34 log_.reset(new ActivityLog(prop_reg));
35 }
36
37 Gesture return_value_;
38 HardwareState* expected_hwstate_;
39 int interpret_call_count_;
40 int handle_timer_call_count_;
41 BoolProperty bool_prop_;
42 DoubleProperty double_prop_;
43 IntProperty int_prop_;
44 StringProperty string_prop_;
45 char* expected_interpreter_name_;
46
47 protected:
SyncInterpretImpl(HardwareState & hwstate,stime_t * timeout)48 virtual void SyncInterpretImpl(HardwareState& hwstate, stime_t* timeout) {
49 interpret_call_count_++;
50 EXPECT_STREQ(expected_interpreter_name_, name());
51 EXPECT_NE(0, bool_prop_.val_);
52 EXPECT_NE(0, double_prop_.val_);
53 EXPECT_NE(0, int_prop_.val_);
54 EXPECT_NE("", string_prop_.val_);
55 EXPECT_TRUE(expected_hwstate_);
56 EXPECT_DOUBLE_EQ(expected_hwstate_->timestamp, hwstate.timestamp);
57 EXPECT_EQ(expected_hwstate_->buttons_down, hwstate.buttons_down);
58 EXPECT_EQ(expected_hwstate_->finger_cnt, hwstate.finger_cnt);
59 EXPECT_EQ(expected_hwstate_->touch_cnt, hwstate.touch_cnt);
60 if (expected_hwstate_->finger_cnt == hwstate.finger_cnt) {
61 for (size_t i = 0; i < expected_hwstate_->finger_cnt; i++)
62 EXPECT_TRUE(expected_hwstate_->fingers[i] == hwstate.fingers[i]);
63 }
64 *timeout = 0.01;
65 ProduceGesture(return_value_);
66 }
67
HandleTimerImpl(stime_t now,stime_t * timeout)68 virtual void HandleTimerImpl(stime_t now, stime_t* timeout) {
69 handle_timer_call_count_++;
70 Interpreter::HandleTimerImpl(now, timeout);
71 ProduceGesture(return_value_);
72 }
73 };
74
75
TEST(InterpreterTest,SimpleTest)76 TEST(InterpreterTest, SimpleTest) {
77 PropRegistry prop_reg;
78 InterpreterTestInterpreter base_interpreter(&prop_reg);
79 base_interpreter.SetEventLoggingEnabled(true);
80 MetricsProperties mprops(&prop_reg);
81
82 HardwareProperties hwprops = {
83 .right = 100, .bottom = 100,
84 .res_x = 10,
85 .res_y = 10,
86 .orientation_minimum = 1,
87 .orientation_maximum = 2,
88 .max_finger_cnt = 2, .max_touch_cnt = 5,
89 .supports_t5r2 = 1, .support_semi_mt = 0, .is_button_pad = 0,
90 .has_wheel = 0, .wheel_is_hi_res = 0,
91 .is_haptic_pad = 0,
92 };
93
94 TestInterpreterWrapper wrapper(&base_interpreter, &hwprops);
95
96 base_interpreter.bool_prop_.val_ = 1;
97 base_interpreter.double_prop_.val_ = 1;
98 base_interpreter.int_prop_.val_ = 1;
99 base_interpreter.string_prop_.val_ = "x";
100
101 //if (prop_reg)
102 // prop_reg->set_activity_log(&(base_interpreter.log_));
103
104 char interpreter_name[] = "InterpreterTestInterpreter";
105 base_interpreter.expected_interpreter_name_ = interpreter_name;
106 base_interpreter.return_value_ = Gesture(kGestureMove,
107 0, // start time
108 1, // end time
109 -4, // dx
110 2.8); // dy
111
112 FingerState finger_state = {
113 // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
114 0, 0, 0, 0, 10, 0, 50, 50, 1, 0
115 };
116 HardwareState hardware_state = make_hwstate(200000, 0, 1, 1, &finger_state);
117
118 stime_t timeout = NO_DEADLINE;
119 base_interpreter.expected_hwstate_ = &hardware_state;
120 Gesture* result = wrapper.SyncInterpret(hardware_state, &timeout);
121 EXPECT_TRUE(base_interpreter.return_value_ == *result);
122 ASSERT_GT(timeout, 0);
123 stime_t now = hardware_state.timestamp + timeout;
124 timeout = NO_DEADLINE;
125 result = wrapper.HandleTimer(now, &timeout);
126 EXPECT_TRUE(base_interpreter.return_value_ == *result);
127 ASSERT_LT(timeout, 0);
128 EXPECT_EQ(1, base_interpreter.interpret_call_count_);
129 EXPECT_EQ(1, base_interpreter.handle_timer_call_count_);
130
131 // Now, get the log
132 string initial_log = base_interpreter.Encode();
133 // Make a new interpreter and push the log through it
134 PropRegistry prop_reg2;
135 InterpreterTestInterpreter base_interpreter2(&prop_reg2);
136 base_interpreter2.SetEventLoggingEnabled(true);
137 base_interpreter2.return_value_ = base_interpreter.return_value_;
138 base_interpreter2.expected_interpreter_name_ = interpreter_name;
139 MetricsProperties mprops2(&prop_reg2);
140
141 ActivityReplay replay(&prop_reg2);
142 replay.Parse(initial_log);
143
144 base_interpreter2.expected_hwstate_ = &hardware_state;
145
146 replay.Replay(&base_interpreter2, &mprops2);
147 string final_log = base_interpreter2.Encode();
148 EXPECT_EQ(initial_log, final_log);
149 EXPECT_EQ(1, base_interpreter2.interpret_call_count_);
150 EXPECT_EQ(1, base_interpreter2.handle_timer_call_count_);
151 }
152
153 class InterpreterResetLogTestInterpreter : public Interpreter {
154 public:
InterpreterResetLogTestInterpreter()155 InterpreterResetLogTestInterpreter() : Interpreter(nullptr, nullptr, true) {
156 log_.reset(new ActivityLog(nullptr));
157 }
158 protected:
SyncInterpretImpl(HardwareState & hwstate,stime_t * timeout)159 virtual void SyncInterpretImpl(HardwareState& hwstate,
160 stime_t* timeout) {}
161
HandleTimerImpl(stime_t now,stime_t * timeout)162 virtual void HandleTimerImpl(stime_t now, stime_t* timeout) {}
163 };
164
TEST(InterpreterTest,ResetLogTest)165 TEST(InterpreterTest, ResetLogTest) {
166 PropRegistry prop_reg;
167 InterpreterResetLogTestInterpreter base_interpreter;
168 base_interpreter.SetEventLoggingEnabled(true);
169 TestInterpreterWrapper wrapper(&base_interpreter);
170
171 FingerState finger_state = {
172 // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
173 0, 0, 0, 0, 10, 0, 50, 50, 1, 0
174 };
175 HardwareState hardware_state = make_hwstate(200000, 0, 1, 1, &finger_state);
176 stime_t timeout = NO_DEADLINE;
177 wrapper.SyncInterpret(hardware_state, &timeout);
178 EXPECT_EQ(base_interpreter.log_->size(), 1);
179
180 wrapper.SyncInterpret(hardware_state, &timeout);
181 EXPECT_EQ(base_interpreter.log_->size(), 2);
182
183 // Assume the ResetLog property is set.
184 base_interpreter.Clear();
185 EXPECT_EQ(base_interpreter.log_->size(), 0);
186
187 wrapper.SyncInterpret(hardware_state, &timeout);
188 EXPECT_EQ(base_interpreter.log_->size(), 1);
189 }
190
TEST(InterpreterTest,LoggingDisabledByDefault)191 TEST(InterpreterTest, LoggingDisabledByDefault) {
192 PropRegistry prop_reg;
193 InterpreterResetLogTestInterpreter base_interpreter;
194 TestInterpreterWrapper wrapper(&base_interpreter);
195
196 FingerState finger_state = {
197 // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
198 0, 0, 0, 0, 10, 0, 50, 50, 1, 0
199 };
200 HardwareState hardware_state = make_hwstate(200000, 0, 1, 1, &finger_state);
201 stime_t timeout = NO_DEADLINE;
202 wrapper.SyncInterpret(hardware_state, &timeout);
203 EXPECT_EQ(base_interpreter.log_->size(), 0);
204
205 wrapper.SyncInterpret(hardware_state, &timeout);
206 EXPECT_EQ(base_interpreter.log_->size(), 0);
207 }
208
TEST(InterpreterTest,EventDebugLoggingEnableTest)209 TEST(InterpreterTest, EventDebugLoggingEnableTest) {
210 InterpreterResetLogTestInterpreter base_interpreter;
211
212 base_interpreter.SetEventDebugLoggingEnabled(0);
213 EXPECT_EQ(base_interpreter.GetEventDebugLoggingEnabled(), 0);
214
215 using EventDebug = ActivityLog::EventDebug;
216 base_interpreter.EventDebugLoggingEnable(EventDebug::HardwareState);
217 EXPECT_EQ(base_interpreter.GetEventDebugLoggingEnabled(),
218 1 << static_cast<int>(EventDebug::HardwareState));
219
220 base_interpreter.EventDebugLoggingDisable(EventDebug::HardwareState);
221 EXPECT_EQ(base_interpreter.GetEventDebugLoggingEnabled(), 0);
222 }
223
TEST(InterpreterTest,LogHardwareStateTest)224 TEST(InterpreterTest, LogHardwareStateTest) {
225 PropRegistry prop_reg;
226 InterpreterResetLogTestInterpreter base_interpreter;
227
228 FingerState fs = { 0.0, 0.0, 0.0, 0.0, 9.0, 0.0, 3.0, 4.0, 22, 0 };
229 HardwareState hs = make_hwstate(1.0, 0, 1, 1, &fs);
230
231 base_interpreter.SetEventLoggingEnabled(false);
232 base_interpreter.SetEventDebugLoggingEnabled(0);
233
234 base_interpreter.LogHardwareStatePre(
235 "InterpreterTest_LogHardwareStateTest", hs);
236 EXPECT_EQ(base_interpreter.log_->size(), 0);
237
238 base_interpreter.LogHardwareStatePost(
239 "InterpreterTest_LogHardwareStateTest", hs);
240 EXPECT_EQ(base_interpreter.log_->size(), 0);
241
242 using EventDebug = ActivityLog::EventDebug;
243 base_interpreter.SetEventLoggingEnabled(true);
244 base_interpreter.EventDebugLoggingEnable(EventDebug::HardwareState);
245
246 base_interpreter.LogHardwareStatePre(
247 "InterpreterTest_LogHardwareStateTest", hs);
248 EXPECT_EQ(base_interpreter.log_->size(), 1);
249
250 base_interpreter.LogHardwareStatePost(
251 "InterpreterTest_LogHardwareStateTest", hs);
252 EXPECT_EQ(base_interpreter.log_->size(), 2);
253 }
254
TEST(InterpreterTest,LogGestureTest)255 TEST(InterpreterTest, LogGestureTest) {
256 PropRegistry prop_reg;
257 InterpreterResetLogTestInterpreter base_interpreter;
258
259 Gesture move(kGestureMove, 1.0, 2.0, 773, 4.0);
260
261 base_interpreter.SetEventLoggingEnabled(false);
262 base_interpreter.SetEventDebugLoggingEnabled(0);
263 base_interpreter.LogGestureConsume("InterpreterTest_LogGestureTest", move);
264 EXPECT_EQ(base_interpreter.log_->size(), 0);
265 base_interpreter.LogGestureProduce("InterpreterTest_LogGestureTest", move);
266 EXPECT_EQ(base_interpreter.log_->size(), 0);
267
268
269 using EventDebug = ActivityLog::EventDebug;
270 base_interpreter.SetEventLoggingEnabled(true);
271 base_interpreter.EventDebugLoggingEnable(EventDebug::Gesture);
272 base_interpreter.LogGestureConsume("InterpreterTest_LogGestureTest", move);
273 EXPECT_EQ(base_interpreter.log_->size(), 1);
274 base_interpreter.LogGestureProduce("InterpreterTest_LogGestureTest", move);
275 EXPECT_EQ(base_interpreter.log_->size(), 2);
276 }
277
TEST(InterpreterTest,LogHandleTimerTest)278 TEST(InterpreterTest, LogHandleTimerTest) {
279 PropRegistry prop_reg;
280 InterpreterResetLogTestInterpreter base_interpreter;
281
282 using EventDebug = ActivityLog::EventDebug;
283 base_interpreter.SetEventLoggingEnabled(true);
284 base_interpreter.EventDebugLoggingEnable(EventDebug::HandleTimer);
285
286 stime_t timeout = 10;
287
288 base_interpreter.LogHandleTimerPre("InterpreterTest_LogHandleTimerTest",
289 0, &timeout);
290 EXPECT_EQ(base_interpreter.log_->size(), 1);
291
292 base_interpreter.LogHandleTimerPost("InterpreterTest_LogHandleTimerTest",
293 0, &timeout);
294 EXPECT_EQ(base_interpreter.log_->size(), 2);
295 }
296
297 } // namespace gestures
298