• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     .screen_x_dpi = 0,
87     .screen_y_dpi = 0,
88     .orientation_minimum = 1,
89     .orientation_maximum = 2,
90     .max_finger_cnt = 2, .max_touch_cnt = 5,
91     .supports_t5r2 = 1, .support_semi_mt = 0, .is_button_pad = 0,
92     .has_wheel = 0, .wheel_is_hi_res = 0,
93     .is_haptic_pad = 0,
94   };
95 
96   TestInterpreterWrapper wrapper(&base_interpreter, &hwprops);
97 
98   base_interpreter.bool_prop_.val_ = 1;
99   base_interpreter.double_prop_.val_ = 1;
100   base_interpreter.int_prop_.val_ = 1;
101   base_interpreter.string_prop_.val_ = "x";
102 
103   //if (prop_reg)
104   //  prop_reg->set_activity_log(&(base_interpreter.log_));
105 
106   char interpreter_name[] = "InterpreterTestInterpreter";
107   base_interpreter.expected_interpreter_name_ = interpreter_name;
108   base_interpreter.return_value_ = Gesture(kGestureMove,
109                                             0,  // start time
110                                             1,  // end time
111                                             -4,  // dx
112                                             2.8);  // dy
113 
114   FingerState finger_state = {
115     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
116     0, 0, 0, 0, 10, 0, 50, 50, 1, 0
117   };
118   HardwareState hardware_state = make_hwstate(200000, 0, 1, 1, &finger_state);
119 
120   stime_t timeout = NO_DEADLINE;
121   base_interpreter.expected_hwstate_ = &hardware_state;
122   Gesture* result = wrapper.SyncInterpret(hardware_state, &timeout);
123   EXPECT_TRUE(base_interpreter.return_value_ == *result);
124   ASSERT_GT(timeout, 0);
125   stime_t now = hardware_state.timestamp + timeout;
126   timeout = NO_DEADLINE;
127   result = wrapper.HandleTimer(now, &timeout);
128   EXPECT_TRUE(base_interpreter.return_value_ == *result);
129   ASSERT_LT(timeout, 0);
130   EXPECT_EQ(1, base_interpreter.interpret_call_count_);
131   EXPECT_EQ(1, base_interpreter.handle_timer_call_count_);
132 
133   // Now, get the log
134   string initial_log = base_interpreter.Encode();
135   // Make a new interpreter and push the log through it
136   PropRegistry prop_reg2;
137   InterpreterTestInterpreter base_interpreter2(&prop_reg2);
138   base_interpreter2.SetEventLoggingEnabled(true);
139   base_interpreter2.return_value_ = base_interpreter.return_value_;
140   base_interpreter2.expected_interpreter_name_ = interpreter_name;
141   MetricsProperties mprops2(&prop_reg2);
142 
143   ActivityReplay replay(&prop_reg2);
144   replay.Parse(initial_log);
145 
146   base_interpreter2.expected_hwstate_ = &hardware_state;
147 
148   replay.Replay(&base_interpreter2, &mprops2);
149   string final_log = base_interpreter2.Encode();
150   EXPECT_EQ(initial_log, final_log);
151   EXPECT_EQ(1, base_interpreter2.interpret_call_count_);
152   EXPECT_EQ(1, base_interpreter2.handle_timer_call_count_);
153 }
154 
155 class InterpreterResetLogTestInterpreter : public Interpreter {
156  public:
InterpreterResetLogTestInterpreter()157   InterpreterResetLogTestInterpreter() : Interpreter(nullptr, nullptr, true) {
158     log_.reset(new ActivityLog(nullptr));
159   }
160  protected:
SyncInterpretImpl(HardwareState & hwstate,stime_t * timeout)161   virtual void SyncInterpretImpl(HardwareState& hwstate,
162                                      stime_t* timeout) {}
163 
HandleTimerImpl(stime_t now,stime_t * timeout)164   virtual void HandleTimerImpl(stime_t now, stime_t* timeout) {}
165 };
166 
TEST(InterpreterTest,ResetLogTest)167 TEST(InterpreterTest, ResetLogTest) {
168   PropRegistry prop_reg;
169   InterpreterResetLogTestInterpreter base_interpreter;
170   base_interpreter.SetEventLoggingEnabled(true);
171   TestInterpreterWrapper wrapper(&base_interpreter);
172 
173   FingerState finger_state = {
174     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
175     0, 0, 0, 0, 10, 0, 50, 50, 1, 0
176   };
177   HardwareState hardware_state = make_hwstate(200000, 0, 1, 1, &finger_state);
178   stime_t timeout = NO_DEADLINE;
179   wrapper.SyncInterpret(hardware_state, &timeout);
180   EXPECT_EQ(base_interpreter.log_->size(), 1);
181 
182   wrapper.SyncInterpret(hardware_state, &timeout);
183   EXPECT_EQ(base_interpreter.log_->size(), 2);
184 
185   // Assume the ResetLog property is set.
186   base_interpreter.Clear();
187   EXPECT_EQ(base_interpreter.log_->size(), 0);
188 
189   wrapper.SyncInterpret(hardware_state, &timeout);
190   EXPECT_EQ(base_interpreter.log_->size(), 1);
191 }
192 
TEST(InterpreterTest,LoggingDisabledByDefault)193 TEST(InterpreterTest, LoggingDisabledByDefault) {
194   PropRegistry prop_reg;
195   InterpreterResetLogTestInterpreter base_interpreter;
196   TestInterpreterWrapper wrapper(&base_interpreter);
197 
198   FingerState finger_state = {
199     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
200     0, 0, 0, 0, 10, 0, 50, 50, 1, 0
201   };
202   HardwareState hardware_state = make_hwstate(200000, 0, 1, 1, &finger_state);
203   stime_t timeout = NO_DEADLINE;
204   wrapper.SyncInterpret(hardware_state, &timeout);
205   EXPECT_EQ(base_interpreter.log_->size(), 0);
206 
207   wrapper.SyncInterpret(hardware_state, &timeout);
208   EXPECT_EQ(base_interpreter.log_->size(), 0);
209 }
210 
TEST(InterpreterTest,EventDebugLoggingEnableTest)211 TEST(InterpreterTest, EventDebugLoggingEnableTest) {
212   InterpreterResetLogTestInterpreter base_interpreter;
213 
214   base_interpreter.SetEventDebugLoggingEnabled(0);
215   EXPECT_EQ(base_interpreter.GetEventDebugLoggingEnabled(), 0);
216 
217   using EventDebug = ActivityLog::EventDebug;
218   base_interpreter.EventDebugLoggingEnable(EventDebug::HardwareState);
219   EXPECT_EQ(base_interpreter.GetEventDebugLoggingEnabled(),
220             1 << static_cast<int>(EventDebug::HardwareState));
221 
222   base_interpreter.EventDebugLoggingDisable(EventDebug::HardwareState);
223   EXPECT_EQ(base_interpreter.GetEventDebugLoggingEnabled(), 0);
224 }
225 
TEST(InterpreterTest,LogHardwareStateTest)226 TEST(InterpreterTest, LogHardwareStateTest) {
227   PropRegistry prop_reg;
228   InterpreterResetLogTestInterpreter base_interpreter;
229 
230   FingerState fs = { 0.0, 0.0, 0.0, 0.0, 9.0, 0.0, 3.0, 4.0, 22, 0 };
231   HardwareState hs = make_hwstate(1.0, 0, 1, 1, &fs);
232 
233   base_interpreter.SetEventLoggingEnabled(false);
234   base_interpreter.SetEventDebugLoggingEnabled(0);
235 
236   base_interpreter.LogHardwareStatePre(
237       "InterpreterTest_LogHardwareStateTest", hs);
238   EXPECT_EQ(base_interpreter.log_->size(), 0);
239 
240   base_interpreter.LogHardwareStatePost(
241       "InterpreterTest_LogHardwareStateTest", hs);
242   EXPECT_EQ(base_interpreter.log_->size(), 0);
243 
244   using EventDebug = ActivityLog::EventDebug;
245   base_interpreter.SetEventLoggingEnabled(true);
246   base_interpreter.EventDebugLoggingEnable(EventDebug::HardwareState);
247 
248   base_interpreter.LogHardwareStatePre(
249       "InterpreterTest_LogHardwareStateTest", hs);
250   EXPECT_EQ(base_interpreter.log_->size(), 1);
251 
252   base_interpreter.LogHardwareStatePost(
253       "InterpreterTest_LogHardwareStateTest", hs);
254   EXPECT_EQ(base_interpreter.log_->size(), 2);
255 }
256 
TEST(InterpreterTest,LogGestureTest)257 TEST(InterpreterTest, LogGestureTest) {
258   PropRegistry prop_reg;
259   InterpreterResetLogTestInterpreter base_interpreter;
260 
261   Gesture move(kGestureMove, 1.0, 2.0, 773, 4.0);
262 
263   base_interpreter.SetEventLoggingEnabled(false);
264   base_interpreter.SetEventDebugLoggingEnabled(0);
265   base_interpreter.LogGestureConsume("InterpreterTest_LogGestureTest", move);
266   EXPECT_EQ(base_interpreter.log_->size(), 0);
267   base_interpreter.LogGestureProduce("InterpreterTest_LogGestureTest", move);
268   EXPECT_EQ(base_interpreter.log_->size(), 0);
269 
270 
271   using EventDebug = ActivityLog::EventDebug;
272   base_interpreter.SetEventLoggingEnabled(true);
273   base_interpreter.EventDebugLoggingEnable(EventDebug::Gesture);
274   base_interpreter.LogGestureConsume("InterpreterTest_LogGestureTest", move);
275   EXPECT_EQ(base_interpreter.log_->size(), 1);
276   base_interpreter.LogGestureProduce("InterpreterTest_LogGestureTest", move);
277   EXPECT_EQ(base_interpreter.log_->size(), 2);
278 }
279 
TEST(InterpreterTest,LogHandleTimerTest)280 TEST(InterpreterTest, LogHandleTimerTest) {
281   PropRegistry prop_reg;
282   InterpreterResetLogTestInterpreter base_interpreter;
283 
284   using EventDebug = ActivityLog::EventDebug;
285   base_interpreter.SetEventLoggingEnabled(true);
286   base_interpreter.EventDebugLoggingEnable(EventDebug::HandleTimer);
287 
288   stime_t timeout = 10;
289 
290   base_interpreter.LogHandleTimerPre("InterpreterTest_LogHandleTimerTest",
291         0, &timeout);
292   EXPECT_EQ(base_interpreter.log_->size(), 1);
293 
294   base_interpreter.LogHandleTimerPost("InterpreterTest_LogHandleTimerTest",
295         0, &timeout);
296   EXPECT_EQ(base_interpreter.log_->size(), 2);
297 }
298 
299 }  // namespace gestures
300