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 <deque>
6 #include <math.h>
7 #include <vector>
8 #include <utility>
9
10 #include <gtest/gtest.h>
11
12 #include "include/box_filter_interpreter.h"
13 #include "include/gestures.h"
14 #include "include/macros.h"
15 #include "include/unittest_util.h"
16
17 using std::deque;
18 using std::make_pair;
19 using std::pair;
20 using std::vector;
21
22 static const HardwareProperties hwprops = {
23 .right = 100, .bottom = 100,
24 .res_x = 1, .res_y = 1,
25 .orientation_minimum = -1,
26 .orientation_maximum = 2,
27 .max_finger_cnt = 5, .max_touch_cnt = 5,
28 .supports_t5r2 = 0, .support_semi_mt = 0, .is_button_pad = 1,
29 .has_wheel = 0, .wheel_is_hi_res = 0,
30 .is_haptic_pad = 0,
31 };
32
33 namespace gestures {
34
35 class BoxFilterInterpreterTest : public ::testing::Test {};
36
37 class BoxFilterInterpreterTestInterpreter : public Interpreter {
38 public:
BoxFilterInterpreterTestInterpreter()39 BoxFilterInterpreterTestInterpreter()
40 : Interpreter(nullptr, nullptr, false),
41 handle_timer_called_(false) {}
42
SyncInterpret(HardwareState & hwstate,stime_t * timeout)43 virtual void SyncInterpret(HardwareState& hwstate, stime_t* timeout) {
44 EXPECT_EQ(1, hwstate.finger_cnt);
45 prev_ = hwstate.fingers[0];
46 }
47
HandleTimer(stime_t now,stime_t * timeout)48 virtual void HandleTimer(stime_t now, stime_t* timeout) {
49 handle_timer_called_ = true;
50 }
51
52 FingerState prev_;
53 bool handle_timer_called_;
54 };
55
56 struct InputAndExpectedOutput {
57 float in;
58 float out;
59 };
60
TEST(BoxFilterInterpreterTest,SimpleTest)61 TEST(BoxFilterInterpreterTest, SimpleTest) {
62 BoxFilterInterpreterTestInterpreter* base_interpreter =
63 new BoxFilterInterpreterTestInterpreter;
64 BoxFilterInterpreter interpreter(nullptr, base_interpreter, nullptr);
65 interpreter.box_width_.val_ = 1.0;
66 interpreter.box_height_.val_ = 1.0;
67 TestInterpreterWrapper wrapper(&interpreter, &hwprops);
68
69 EXPECT_FALSE(base_interpreter->handle_timer_called_);
70 wrapper.HandleTimer(0.0, nullptr);
71 EXPECT_TRUE(base_interpreter->handle_timer_called_);
72
73 FingerState fs = { 0, 0, 0, 0, 1, 0, 3.0, 0.0, 1, 0 };
74 HardwareState hs = make_hwstate(0.0, 0, 1, 1, &fs);
75
76 InputAndExpectedOutput data[] = {
77 { 3.0, 3.0 },
78 { 4.0, 3.5 },
79 { 3.0, 3.5 },
80 { 4.0, 3.5 },
81 { 5.0, 4.5 },
82 { 6.0, 5.5 },
83 { 5.0, 5.5 },
84 { 4.0, 4.5 },
85 };
86
87 stime_t now = 0.0;
88 const stime_t kTimeDelta = 0.01;
89 for (size_t i = 0; i < arraysize(data); i++) {
90 now += kTimeDelta;
91 hs.timestamp = now;
92 fs.position_y = data[i].in;
93 wrapper.SyncInterpret(hs, nullptr);
94 EXPECT_FLOAT_EQ(data[i].out, fs.position_y) << "i=" << i;
95 }
96 }
97
TEST(BoxFilterInterpreterTest,ZeroSizeBoxTest)98 TEST(BoxFilterInterpreterTest, ZeroSizeBoxTest) {
99 BoxFilterInterpreterTestInterpreter* base_interpreter =
100 new BoxFilterInterpreterTestInterpreter;
101 BoxFilterInterpreter interpreter(nullptr, base_interpreter, nullptr);
102 interpreter.box_width_.val_ = 0.0;
103 interpreter.box_height_.val_ = 0.0;
104 TestInterpreterWrapper wrapper(&interpreter, &hwprops);
105
106 EXPECT_FALSE(base_interpreter->handle_timer_called_);
107 wrapper.HandleTimer(0.0, nullptr);
108 EXPECT_TRUE(base_interpreter->handle_timer_called_);
109
110 FingerState fs = { 0, 0, 0, 0, 1, 0, 3.0, 0.0, 1, 0 };
111 HardwareState hs = make_hwstate(0.0, 0, 1, 1, &fs);
112
113 InputAndExpectedOutput data[] = {
114 { 3.0, 3.0 },
115 { 4.0, 4.0 },
116 { 3.0, 3.0 },
117 { 4.0, 4.0 },
118 { 5.0, 5.0 },
119 { 6.0, 6.0 },
120 { 5.0, 5.0 },
121 { 4.0, 4.0 },
122 };
123
124 stime_t now = 0.0;
125 const stime_t kTimeDelta = 0.01;
126 for (size_t i = 0; i < arraysize(data); i++) {
127 now += kTimeDelta;
128 hs.timestamp = now;
129 fs.position_y = data[i].in;
130 wrapper.SyncInterpret(hs, nullptr);
131 EXPECT_FLOAT_EQ(data[i].out, fs.position_y) << "i=" << i;
132 }
133 }
134
135 } // namespace gestures
136