1 // Copyright 2011 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/gestures.h"
8 #include "include/iir_filter_interpreter.h"
9 #include "include/unittest_util.h"
10
11 namespace gestures {
12
13 class IirFilterInterpreterTest : public ::testing::Test {};
14
15 class IirFilterInterpreterTestInterpreter : public Interpreter {
16 public:
IirFilterInterpreterTestInterpreter()17 IirFilterInterpreterTestInterpreter()
18 : Interpreter(nullptr, nullptr, false),
19 sync_interpret_cnt_(0) {
20 prev_.position_x = 0.0;
21 prev_.position_y = 0.0;
22 }
23
SyncInterpret(HardwareState & hwstate,stime_t * timeout)24 virtual void SyncInterpret(HardwareState& hwstate, stime_t* timeout) {
25 if (sync_interpret_cnt_) {
26 EXPECT_GT(hwstate.fingers[0].position_x, prev_.position_x);
27 EXPECT_GT(hwstate.fingers[0].position_y, prev_.position_y);
28 }
29 EXPECT_EQ(1, hwstate.finger_cnt);
30 prev_ = hwstate.fingers[0];
31 sync_interpret_cnt_++;
32 }
33
HandleTimer(stime_t now,stime_t * timeout)34 virtual void HandleTimer(stime_t now, stime_t* timeout) {}
35
36 FingerState prev_;
37 size_t sync_interpret_cnt_;
38 };
39
TEST(IirFilterInterpreterTest,SimpleTest)40 TEST(IirFilterInterpreterTest, SimpleTest) {
41 IirFilterInterpreterTestInterpreter* base_interpreter =
42 new IirFilterInterpreterTestInterpreter;
43 IirFilterInterpreter interpreter(nullptr, base_interpreter, nullptr);
44 TestInterpreterWrapper wrapper(&interpreter);
45
46 FingerState fs[] = {
47 // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
48 { 0, 0, 0, 0, 30, 0, 1, 1, 1, GESTURES_FINGER_WARP_X },
49 { 0, 0, 0, 0, 30, 0, 2, 2, 1, 0 },
50 { 0, 0, 0, 0, 30, 0, 3, 3, 1, 0 },
51 { 0, 0, 0, 0, 30, 0, 5, 5, 1, 0 }
52 };
53 HardwareState hs[] = {
54 make_hwstate(0.000, 0, 1, 1, &fs[0]),
55 make_hwstate(0.010, 0, 1, 1, &fs[1]),
56 make_hwstate(0.020, 0, 1, 1, &fs[2]),
57 make_hwstate(0.030, 0, 1, 1, &fs[3])
58 };
59
60 for (size_t i = 0; i < arraysize(hs); i++) {
61 unsigned expected_flags = hs[i].fingers[0].flags;
62 wrapper.SyncInterpret(hs[i], nullptr);
63 EXPECT_EQ(base_interpreter->prev_.flags, expected_flags);
64 }
65 EXPECT_EQ(arraysize(hs), base_interpreter->sync_interpret_cnt_);
66 }
67
TEST(IirFilterInterpreterTest,DisableIIRTest)68 TEST(IirFilterInterpreterTest, DisableIIRTest) {
69 IirFilterInterpreterTestInterpreter* base_interpreter =
70 new IirFilterInterpreterTestInterpreter;
71 IirFilterInterpreter interpreter(nullptr, base_interpreter, nullptr);
72 TestInterpreterWrapper wrapper(&interpreter);
73
74 FingerState fs[] = {
75 // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
76 { 0, 0, 0, 0, 30, 0, 10, 10, 1, 0 },
77 { 0, 0, 0, 0, 30, 0, 11, 15, 1, 0 },
78 { 0, 0, 0, 0, 30, 0, 12, 30, 1, 0 },
79 { 0, 0, 0, 0, 30, 0, 13, 31, 1, 0 },
80 { 0, 0, 0, 0, 30, 0, 14, 32, 1, 0 },
81 { 0, 0, 0, 0, 30, 0, 14, 32, 1, 0 },
82 };
83 HardwareState hs[] = {
84 make_hwstate(0.000, 0, 1, 1, &fs[0]),
85 make_hwstate(0.010, 0, 1, 1, &fs[1]),
86 make_hwstate(0.020, 0, 1, 1, &fs[2]),
87 make_hwstate(0.030, 0, 1, 1, &fs[3]),
88 make_hwstate(0.040, 0, 1, 1, &fs[4]),
89 make_hwstate(0.050, 0, 1, 1, &fs[5]),
90 };
91
92 for (size_t i = 0; i < arraysize(hs); i++) {
93 wrapper.SyncInterpret(hs[i], nullptr);
94 // A quick move at hs[2] and IIR will be disabled. Even though
95 // hs[2] and hs[3] are close enough, the rolling average output
96 // of hs[2] is smoothed that IIR is still disabled for hs[3].
97 // After hs[3], the actual output of hs[i] is approaching hs[i] so
98 // IIR filter will be re-enabled.
99 if (i >= 2 && i <= 3)
100 EXPECT_EQ(interpreter.using_iir_, false);
101 else
102 EXPECT_EQ(interpreter.using_iir_, true);
103 }
104 }
105
TEST(IirFilterInterpreterTest,SemiMTIIRTest)106 TEST(IirFilterInterpreterTest, SemiMTIIRTest) {
107 IirFilterInterpreterTestInterpreter* base_interpreter =
108 new IirFilterInterpreterTestInterpreter;
109 IirFilterInterpreter interpreter(nullptr, base_interpreter, nullptr);
110
111 HardwareProperties hwprops = {
112 .right = 100, .bottom = 60,
113 .res_x = 1.0, .res_y = 1.0,
114 .orientation_minimum = -1,
115 .orientation_maximum = 2,
116 .max_finger_cnt = 2, .max_touch_cnt = 3,
117 .supports_t5r2 = 0, .support_semi_mt = 0, .is_button_pad = 0,
118 .has_wheel = 0, .wheel_is_hi_res = 0,
119 .is_haptic_pad = 0,
120 };
121 TestInterpreterWrapper wrapper(&interpreter, &hwprops);
122
123 float kTestPressure = 100;
124 FingerState fs_normal[] = {
125 // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
126 { 0, 0, 0, 0, 30, 0, 5, 5, 1, 0 },
127 { 0, 0, 0, 0, kTestPressure, 0, 6, 6, 1, 0 },
128 };
129
130 HardwareState hs_normal[] = {
131 make_hwstate(0.000, 0, 1, 1, &fs_normal[0]),
132 make_hwstate(0.010, 0, 1, 1, &fs_normal[1]),
133 };
134
135 // For Non-SemiMT, the pressure of the finger will be different from the
136 // original one after the IIR filter.
137 for (size_t i = 0; i < arraysize(hs_normal); i++)
138 wrapper.SyncInterpret(hs_normal[i], nullptr);
139 int n = arraysize(fs_normal);
140 EXPECT_NE(fs_normal[n - 1].pressure, kTestPressure);
141
142 // On the other hand, for SemiMT, the pressure of the finger should remain the
143 // same after IIR filter.
144 FingerState fs_semi_mt[] = {
145 // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
146 { 0, 0, 0, 0, 30, 0, 5, 5, 1, 0 },
147 { 0, 0, 0, 0, kTestPressure, 0, 6, 6, 1, 0 },
148 };
149 HardwareState hs_semi_mt[] = {
150 make_hwstate(0.000, 0, 1, 1, &fs_semi_mt[0]),
151 make_hwstate(0.010, 0, 1, 1, &fs_semi_mt[1]),
152 };
153 hwprops.support_semi_mt = true;
154 wrapper.Reset(&interpreter, &hwprops);
155 for (size_t i = 0; i < arraysize(hs_semi_mt); i++)
156 wrapper.SyncInterpret(hs_semi_mt[i], nullptr);
157 n = arraysize(fs_semi_mt);
158 EXPECT_EQ(fs_semi_mt[n - 1].pressure, kTestPressure);
159 }
160
161 } // namespace gestures
162