blob: 643fab6d442e0cd4e04025cc8539a04994bdcbb0 [file] [log] [blame]
Harry Cutts3952c832023-08-22 15:26:56 +00001/*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <gestures/HardwareProperties.h>
17
18#include <memory>
19#include <set>
20
21#include <gtest/gtest.h>
22#include <linux/input-event-codes.h>
23
24#include "EventHub.h"
25#include "InputDevice.h"
26#include "InterfaceMocks.h"
27#include "TestConstants.h"
28#include "include/gestures.h"
29
30namespace android {
31
32using testing::Return;
33
34class HardwarePropertiesTest : public testing::Test {
35public:
36 HardwarePropertiesTest() {
37 EXPECT_CALL(mMockInputReaderContext, getEventHub()).WillRepeatedly(Return(&mMockEventHub));
38 InputDeviceIdentifier identifier;
39 identifier.name = "device";
40 identifier.location = "USB1";
41 mDevice = std::make_unique<InputDevice>(&mMockInputReaderContext, DEVICE_ID,
42 /*generation=*/2, identifier);
43 mDeviceContext = std::make_unique<InputDeviceContext>(*mDevice, EVENTHUB_ID);
44 }
45
46protected:
47 static constexpr int32_t DEVICE_ID = END_RESERVED_ID + 1000;
48 static constexpr int32_t EVENTHUB_ID = 1;
49
50 void setupValidAxis(int axis, int32_t min, int32_t max, int32_t resolution) {
Harry Cutts207674d2024-06-06 18:53:41 +000051 EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis))
52 .WillRepeatedly(Return(std::optional<RawAbsoluteAxisInfo>{{
53 .valid = true,
54 .minValue = min,
55 .maxValue = max,
56 .flat = 0,
57 .fuzz = 0,
58 .resolution = resolution,
59 }}));
Harry Cutts3952c832023-08-22 15:26:56 +000060 }
61
62 void setupInvalidAxis(int axis) {
Harry Cutts207674d2024-06-06 18:53:41 +000063 EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis))
64 .WillRepeatedly(Return(std::nullopt));
Harry Cutts3952c832023-08-22 15:26:56 +000065 }
66
67 void setProperty(int property, bool value) {
68 EXPECT_CALL(mMockEventHub, hasInputProperty(EVENTHUB_ID, property))
69 .WillRepeatedly(Return(value));
70 }
71
72 void setButtonsPresent(std::set<int> buttonCodes, bool present) {
73 for (const auto& buttonCode : buttonCodes) {
74 EXPECT_CALL(mMockEventHub, hasScanCode(EVENTHUB_ID, buttonCode))
75 .WillRepeatedly(Return(present));
76 }
77 }
78
79 MockEventHubInterface mMockEventHub;
80 MockInputReaderContext mMockInputReaderContext;
81 std::unique_ptr<InputDevice> mDevice;
82 std::unique_ptr<InputDeviceContext> mDeviceContext;
83};
84
85TEST_F(HardwarePropertiesTest, FancyTouchpad) {
86 setupValidAxis(ABS_MT_POSITION_X, 0, 2048, 27);
87 setupValidAxis(ABS_MT_POSITION_Y, 0, 1500, 30);
88 setupValidAxis(ABS_MT_ORIENTATION, -3, 4, 0);
89 setupValidAxis(ABS_MT_SLOT, 0, 15, 0);
90 setupValidAxis(ABS_MT_PRESSURE, 0, 256, 0);
91
92 setProperty(INPUT_PROP_SEMI_MT, false);
93 setProperty(INPUT_PROP_BUTTONPAD, true);
94
95 setButtonsPresent({BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP, BTN_TOOL_TRIPLETAP, BTN_TOOL_QUADTAP,
96 BTN_TOOL_QUINTTAP},
97 true);
98
99 HardwareProperties hwprops = createHardwareProperties(*mDeviceContext);
100 EXPECT_NEAR(0, hwprops.left, EPSILON);
101 EXPECT_NEAR(0, hwprops.top, EPSILON);
102 EXPECT_NEAR(2048, hwprops.right, EPSILON);
103 EXPECT_NEAR(1500, hwprops.bottom, EPSILON);
104
105 EXPECT_NEAR(27, hwprops.res_x, EPSILON);
106 EXPECT_NEAR(30, hwprops.res_y, EPSILON);
107
108 EXPECT_NEAR(-3, hwprops.orientation_minimum, EPSILON);
109 EXPECT_NEAR(4, hwprops.orientation_maximum, EPSILON);
110
111 EXPECT_EQ(16, hwprops.max_finger_cnt);
112 EXPECT_EQ(5, hwprops.max_touch_cnt);
113
114 EXPECT_FALSE(hwprops.supports_t5r2);
115 EXPECT_FALSE(hwprops.support_semi_mt);
116 EXPECT_TRUE(hwprops.is_button_pad);
117 EXPECT_FALSE(hwprops.has_wheel);
118 EXPECT_FALSE(hwprops.wheel_is_hi_res);
119 EXPECT_FALSE(hwprops.is_haptic_pad);
120 EXPECT_TRUE(hwprops.reports_pressure);
121}
122
123TEST_F(HardwarePropertiesTest, BasicTouchpad) {
124 setupValidAxis(ABS_MT_POSITION_X, 0, 1024, 0);
125 setupValidAxis(ABS_MT_POSITION_Y, 0, 768, 0);
126 setupValidAxis(ABS_MT_SLOT, 0, 7, 0);
127
128 setupInvalidAxis(ABS_MT_ORIENTATION);
129 setupInvalidAxis(ABS_MT_PRESSURE);
130
131 setProperty(INPUT_PROP_SEMI_MT, false);
132 setProperty(INPUT_PROP_BUTTONPAD, false);
133
134 setButtonsPresent({BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP, BTN_TOOL_TRIPLETAP}, true);
135 setButtonsPresent({BTN_TOOL_QUADTAP, BTN_TOOL_QUINTTAP}, false);
136
137 HardwareProperties hwprops = createHardwareProperties(*mDeviceContext);
138 EXPECT_NEAR(0, hwprops.left, EPSILON);
139 EXPECT_NEAR(0, hwprops.top, EPSILON);
140 EXPECT_NEAR(1024, hwprops.right, EPSILON);
141 EXPECT_NEAR(768, hwprops.bottom, EPSILON);
142
143 EXPECT_NEAR(0, hwprops.res_x, EPSILON);
144 EXPECT_NEAR(0, hwprops.res_y, EPSILON);
145
146 EXPECT_NEAR(0, hwprops.orientation_minimum, EPSILON);
147 EXPECT_NEAR(0, hwprops.orientation_maximum, EPSILON);
148
149 EXPECT_EQ(8, hwprops.max_finger_cnt);
150 EXPECT_EQ(3, hwprops.max_touch_cnt);
151
152 EXPECT_FALSE(hwprops.supports_t5r2);
153 EXPECT_FALSE(hwprops.support_semi_mt);
154 EXPECT_FALSE(hwprops.is_button_pad);
155 EXPECT_FALSE(hwprops.has_wheel);
156 EXPECT_FALSE(hwprops.wheel_is_hi_res);
157 EXPECT_FALSE(hwprops.is_haptic_pad);
158 EXPECT_FALSE(hwprops.reports_pressure);
159}
160
161} // namespace android