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 <gtest/gtest.h>
6
7 #include "include/gestures.h"
8 #include "include/non_linearity_filter_interpreter.h"
9 #include "include/unittest_util.h"
10 #include "include/util.h"
11
12 const char kTestNonlinearData[] =
13 "data/non_linearity_data/testing_non_linearity_data.dat";
14
15 namespace gestures {
16
17 class NonLinearityFilterTest : public ::testing::Test {};
18
19 class NonLinearityFilterInterpreterTestInterpreter : public Interpreter {
20 public:
NonLinearityFilterInterpreterTestInterpreter()21 NonLinearityFilterInterpreterTestInterpreter()
22 : Interpreter(nullptr, nullptr, false) {}
23
SyncInterpret(HardwareState & hwstate,stime_t * timeout)24 virtual void SyncInterpret(HardwareState& hwstate, stime_t* timeout) {}
25 };
26
TEST(NonLinearityFilterInterpreterTest,DisablingTest)27 TEST(NonLinearityFilterInterpreterTest, DisablingTest) {
28 FingerState finger_state = { 0, 0, 0, 0, 35, 0, 999, 500, 1, 0 };
29 HardwareState hwstate = make_hwstate(200000, 0, 2, 2, &finger_state);
30
31 NonLinearityFilterInterpreterTestInterpreter* base =
32 new NonLinearityFilterInterpreterTestInterpreter;
33 NonLinearityFilterInterpreter interpreter(nullptr, base, nullptr);
34 TestInterpreterWrapper wrapper(&interpreter);
35
36 // Nothing should change since it is disabled by default without data
37 EXPECT_EQ(nullptr, wrapper.SyncInterpret(hwstate, nullptr));
38 EXPECT_FLOAT_EQ(hwstate.fingers[0].position_x, 999);
39 EXPECT_FLOAT_EQ(hwstate.fingers[0].position_y, 500);
40
41 // Nothing should change even though it's "enabled" since there is no data
42 interpreter.enabled_.val_ = 1;
43 EXPECT_EQ(nullptr, wrapper.SyncInterpret(hwstate, nullptr));
44 EXPECT_FLOAT_EQ(hwstate.fingers[0].position_x, 999);
45 EXPECT_FLOAT_EQ(hwstate.fingers[0].position_y, 500);
46
47 // Even with data loaded, if it is not enabled nothing should change
48 interpreter.data_location_.val_ = kTestNonlinearData;
49 interpreter.LoadData();
50 interpreter.enabled_.val_ = 0;
51 EXPECT_EQ(nullptr, wrapper.SyncInterpret(hwstate, nullptr));
52 EXPECT_FLOAT_EQ(hwstate.fingers[0].position_x, 999);
53 EXPECT_FLOAT_EQ(hwstate.fingers[0].position_y, 500);
54 }
55
TEST(NonLinearityFilterInterpreterTest,HWstateModificationTest)56 TEST(NonLinearityFilterInterpreterTest, HWstateModificationTest) {
57 FingerState finger_state = { 0, 0, 0, 0, 0.2, 0, 0.1, 0.3, 1, 0 };
58 HardwareState hwstate = make_hwstate(200000, 0, 1, 1, &finger_state);
59
60 NonLinearityFilterInterpreterTestInterpreter* base =
61 new NonLinearityFilterInterpreterTestInterpreter;
62 NonLinearityFilterInterpreter interpreter(nullptr, base, nullptr);
63 TestInterpreterWrapper wrapper(&interpreter);
64 interpreter.enabled_.val_ = 1;
65 interpreter.data_location_.val_ = kTestNonlinearData;
66 interpreter.LoadData();
67
68 // This reading should be modified slightly by the testing filter
69 // with errors of (0.325000, -0.325000)
70 EXPECT_EQ(nullptr, wrapper.SyncInterpret(hwstate, nullptr));
71 EXPECT_FLOAT_EQ((float)hwstate.fingers[0].position_x, 0.1 - 0.325);
72 EXPECT_FLOAT_EQ((float)hwstate.fingers[0].position_y, 0.3 + 0.325);
73 }
74
TEST(NonLinearityFilterInterpreterTest,HWstateNoChangesNeededTest)75 TEST(NonLinearityFilterInterpreterTest, HWstateNoChangesNeededTest) {
76 FingerState finger_states[] = {
77 { 0, 0, 0, 0, 0.5, 0, 0.5, 0.5, 1, 0 },
78 { 0, 0, 0, 0, 0.12, 0, 0.78, 0.34, 2, 0 },
79 };
80
81 HardwareState hwstates[] = {
82 make_hwstate(200000, 0, 2, 2, finger_states),
83 make_hwstate(200100, 0, 1, 1, finger_states),
84 };
85
86 NonLinearityFilterInterpreterTestInterpreter* base =
87 new NonLinearityFilterInterpreterTestInterpreter;
88 NonLinearityFilterInterpreter interpreter(nullptr, base, nullptr);
89 TestInterpreterWrapper wrapper(&interpreter);
90 interpreter.enabled_.val_ = 1;
91 interpreter.data_location_.val_ = kTestNonlinearData;
92 interpreter.LoadData();
93
94 // Nothing should change since 2 fingers are on the touchpad
95 EXPECT_EQ(nullptr, wrapper.SyncInterpret(hwstates[0], nullptr));
96 EXPECT_FLOAT_EQ(hwstates[0].fingers[0].position_x, 0.5);
97 EXPECT_FLOAT_EQ(hwstates[0].fingers[0].position_y, 0.5);
98 EXPECT_FLOAT_EQ(hwstates[0].fingers[1].position_x, 0.78);
99 EXPECT_FLOAT_EQ(hwstates[0].fingers[1].position_y, 0.34);
100
101 // This finger is at (0.5, 0.5, 0.5) which has 0 error in the test readings
102 EXPECT_EQ(nullptr, wrapper.SyncInterpret(hwstates[1], nullptr));
103 EXPECT_FLOAT_EQ(hwstates[1].fingers[0].position_x, 0.5);
104 EXPECT_FLOAT_EQ(hwstates[1].fingers[0].position_y, 0.5);
105 }
106
107 } // namespace gestures
108