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(NULL, NULL, 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(NULL, base_interpreter, NULL);
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], NULL);
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(NULL, base_interpreter, NULL);
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], NULL);
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(NULL, base_interpreter, NULL);
110 
111   HardwareProperties hwprops = {
112     0, 0, 100, 60,  // left, top, right, bottom
113     1.0, 1.0, 25.4, 25.4, // x res, y res, x DPI, y DPI
114     -1,  // orientation minimum
115     2,   // orientation maximum
116     2, 3, 0, 0, 0,  // max_fingers, max_touch, t5r2, semi_mt, is_button_pad
117     0, 0,  // has wheel, vertical wheel is high resolution
118     0,  // is_haptic_pad
119   };
120   TestInterpreterWrapper wrapper(&interpreter, &hwprops);
121 
122   float kTestPressure = 100;
123   FingerState fs_normal[] = {
124     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
125     { 0, 0, 0, 0, 30, 0, 5, 5, 1, 0 },
126     { 0, 0, 0, 0, kTestPressure, 0, 6, 6, 1, 0 },
127   };
128 
129   HardwareState hs_normal[] = {
130     make_hwstate(0.000, 0, 1, 1, &fs_normal[0]),
131     make_hwstate(0.010, 0, 1, 1, &fs_normal[1]),
132   };
133 
134   // For Non-SemiMT, the pressure of the finger will be different from the
135   // original one after the IIR filter.
136   for (size_t i = 0; i < arraysize(hs_normal); i++)
137     wrapper.SyncInterpret(&hs_normal[i], NULL);
138   int n = arraysize(fs_normal);
139   EXPECT_NE(fs_normal[n - 1].pressure, kTestPressure);
140 
141   // On the other hand, for SemiMT, the pressure of the finger should remain the
142   // same after IIR filter.
143   FingerState fs_semi_mt[] = {
144     // TM, Tm, WM, Wm, Press, Orientation, X, Y, TrID
145     { 0, 0, 0, 0, 30, 0, 5, 5, 1, 0 },
146     { 0, 0, 0, 0, kTestPressure, 0, 6, 6, 1, 0 },
147   };
148   HardwareState hs_semi_mt[] = {
149     make_hwstate(0.000, 0, 1, 1, &fs_semi_mt[0]),
150     make_hwstate(0.010, 0, 1, 1, &fs_semi_mt[1]),
151   };
152   hwprops.support_semi_mt = true;
153   wrapper.Reset(&interpreter, &hwprops);
154   for (size_t i = 0; i < arraysize(hs_semi_mt); i++)
155     wrapper.SyncInterpret(&hs_semi_mt[i], NULL);
156   n = arraysize(fs_semi_mt);
157   EXPECT_EQ(fs_semi_mt[n - 1].pressure, kTestPressure);
158 }
159 
160 }  // namespace gestures
161