xref: /aosp_15_r20/frameworks/native/services/inputflinger/tests/SensorInputMapper_test.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 2024 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 
17 #include "SensorInputMapper.h"
18 
19 #include <cstdint>
20 #include <list>
21 #include <optional>
22 #include <utility>
23 #include <vector>
24 
25 #include <EventHub.h>
26 #include <NotifyArgs.h>
27 #include <ftl/enum.h>
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30 #include <input/Input.h>
31 #include <input/InputDevice.h>
32 #include <input/PrintTools.h>
33 #include <linux/input-event-codes.h>
34 
35 #include "InputMapperTest.h"
36 #include "TestEventMatchers.h"
37 
38 namespace android {
39 
40 using testing::AllOf;
41 using testing::ElementsAre;
42 using testing::Return;
43 using testing::VariantWith;
44 
45 namespace {
46 
47 constexpr int32_t ACCEL_RAW_MIN = -32768;
48 constexpr int32_t ACCEL_RAW_MAX = 32768;
49 constexpr int32_t ACCEL_RAW_FUZZ = 16;
50 constexpr int32_t ACCEL_RAW_FLAT = 0;
51 constexpr int32_t ACCEL_RAW_RESOLUTION = 8192;
52 
53 constexpr int32_t GYRO_RAW_MIN = -2097152;
54 constexpr int32_t GYRO_RAW_MAX = 2097152;
55 constexpr int32_t GYRO_RAW_FUZZ = 16;
56 constexpr int32_t GYRO_RAW_FLAT = 0;
57 constexpr int32_t GYRO_RAW_RESOLUTION = 1024;
58 
59 constexpr float GRAVITY_MS2_UNIT = 9.80665f;
60 constexpr float DEGREE_RADIAN_UNIT = 0.0174533f;
61 
62 } // namespace
63 
64 class SensorInputMapperTest : public InputMapperUnitTest {
65 protected:
SetUp()66     void SetUp() override {
67         InputMapperUnitTest::SetUp();
68         EXPECT_CALL(mMockEventHub, getDeviceClasses(EVENTHUB_ID))
69                 .WillRepeatedly(Return(InputDeviceClass::SENSOR));
70         // The mapper requests info on all ABS axes, including ones which aren't actually used, so
71         // just return nullopt for all axes we don't explicitly set up.
72         EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, testing::_))
73                 .WillRepeatedly(Return(std::nullopt));
74     }
75 
setupSensor(int32_t absCode,InputDeviceSensorType type,int32_t sensorDataIndex)76     void setupSensor(int32_t absCode, InputDeviceSensorType type, int32_t sensorDataIndex) {
77         EXPECT_CALL(mMockEventHub, mapSensor(EVENTHUB_ID, absCode))
78                 .WillRepeatedly(Return(std::make_pair(type, sensorDataIndex)));
79     }
80 };
81 
TEST_F(SensorInputMapperTest,GetSources)82 TEST_F(SensorInputMapperTest, GetSources) {
83     mMapper = createInputMapper<SensorInputMapper>(*mDeviceContext,
84                                                    mFakePolicy->getReaderConfiguration());
85 
86     ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mMapper->getSources());
87 }
88 
TEST_F(SensorInputMapperTest,ProcessAccelerometerSensor)89 TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
90     EXPECT_CALL(mMockEventHub, hasMscEvent(EVENTHUB_ID, MSC_TIMESTAMP))
91             .WillRepeatedly(Return(true));
92     setupSensor(ABS_X, InputDeviceSensorType::ACCELEROMETER, /*sensorDataIndex=*/0);
93     setupSensor(ABS_Y, InputDeviceSensorType::ACCELEROMETER, /*sensorDataIndex=*/1);
94     setupSensor(ABS_Z, InputDeviceSensorType::ACCELEROMETER, /*sensorDataIndex=*/2);
95     setupAxis(ABS_X, /*valid=*/true, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_RESOLUTION,
96               ACCEL_RAW_FLAT, ACCEL_RAW_FUZZ);
97     setupAxis(ABS_Y, /*valid=*/true, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_RESOLUTION,
98               ACCEL_RAW_FLAT, ACCEL_RAW_FUZZ);
99     setupAxis(ABS_Z, /*valid=*/true, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_RESOLUTION,
100               ACCEL_RAW_FLAT, ACCEL_RAW_FUZZ);
101     mPropertyMap.addProperty("sensor.accelerometer.reportingMode", "0");
102     mPropertyMap.addProperty("sensor.accelerometer.maxDelay", "100000");
103     mPropertyMap.addProperty("sensor.accelerometer.minDelay", "5000");
104     mPropertyMap.addProperty("sensor.accelerometer.power", "1.5");
105     mMapper = createInputMapper<SensorInputMapper>(*mDeviceContext,
106                                                    mFakePolicy->getReaderConfiguration());
107 
108     EXPECT_CALL(mMockEventHub, enableDevice(EVENTHUB_ID));
109     ASSERT_TRUE(mMapper->enableSensor(InputDeviceSensorType::ACCELEROMETER,
110                                       std::chrono::microseconds(10000),
111                                       std::chrono::microseconds(0)));
112     std::list<NotifyArgs> args;
113     args += process(ARBITRARY_TIME, EV_ABS, ABS_X, 20000);
114     args += process(ARBITRARY_TIME, EV_ABS, ABS_Y, -20000);
115     args += process(ARBITRARY_TIME, EV_ABS, ABS_Z, 40000);
116     args += process(ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
117     args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
118 
119     std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
120                                  -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
121                                  40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
122 
123     ASSERT_EQ(args.size(), 1u);
124     const NotifySensorArgs& arg = std::get<NotifySensorArgs>(args.front());
125     ASSERT_EQ(arg.source, AINPUT_SOURCE_SENSOR);
126     ASSERT_EQ(arg.deviceId, DEVICE_ID);
127     ASSERT_EQ(arg.sensorType, InputDeviceSensorType::ACCELEROMETER);
128     ASSERT_EQ(arg.accuracy, InputDeviceSensorAccuracy::HIGH);
129     ASSERT_EQ(arg.hwTimestamp, ARBITRARY_TIME);
130     ASSERT_EQ(arg.values, values);
131     mMapper->flushSensor(InputDeviceSensorType::ACCELEROMETER);
132 }
133 
TEST_F(SensorInputMapperTest,ProcessGyroscopeSensor)134 TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
135     EXPECT_CALL(mMockEventHub, hasMscEvent(EVENTHUB_ID, MSC_TIMESTAMP))
136             .WillRepeatedly(Return(true));
137     setupSensor(ABS_RX, InputDeviceSensorType::GYROSCOPE, /*sensorDataIndex=*/0);
138     setupSensor(ABS_RY, InputDeviceSensorType::GYROSCOPE, /*sensorDataIndex=*/1);
139     setupSensor(ABS_RZ, InputDeviceSensorType::GYROSCOPE, /*sensorDataIndex=*/2);
140     setupAxis(ABS_RX, /*valid=*/true, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_RESOLUTION,
141               GYRO_RAW_FLAT, GYRO_RAW_FUZZ);
142     setupAxis(ABS_RY, /*valid=*/true, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_RESOLUTION,
143               GYRO_RAW_FLAT, GYRO_RAW_FUZZ);
144     setupAxis(ABS_RZ, /*valid=*/true, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_RESOLUTION,
145               GYRO_RAW_FLAT, GYRO_RAW_FUZZ);
146     mPropertyMap.addProperty("sensor.gyroscope.reportingMode", "0");
147     mPropertyMap.addProperty("sensor.gyroscope.maxDelay", "100000");
148     mPropertyMap.addProperty("sensor.gyroscope.minDelay", "5000");
149     mPropertyMap.addProperty("sensor.gyroscope.power", "0.8");
150     mMapper = createInputMapper<SensorInputMapper>(*mDeviceContext,
151                                                    mFakePolicy->getReaderConfiguration());
152 
153     EXPECT_CALL(mMockEventHub, enableDevice(EVENTHUB_ID));
154     ASSERT_TRUE(mMapper->enableSensor(InputDeviceSensorType::GYROSCOPE,
155                                       std::chrono::microseconds(10000),
156                                       std::chrono::microseconds(0)));
157     std::list<NotifyArgs> args;
158     args += process(ARBITRARY_TIME, EV_ABS, ABS_RX, 20000);
159     args += process(ARBITRARY_TIME, EV_ABS, ABS_RY, -20000);
160     args += process(ARBITRARY_TIME, EV_ABS, ABS_RZ, 40000);
161     args += process(ARBITRARY_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
162     args += process(ARBITRARY_TIME, EV_SYN, SYN_REPORT, 0);
163 
164     std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
165                                  -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
166                                  40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
167 
168     ASSERT_EQ(args.size(), 1u);
169     const NotifySensorArgs& arg = std::get<NotifySensorArgs>(args.front());
170     ASSERT_EQ(arg.source, AINPUT_SOURCE_SENSOR);
171     ASSERT_EQ(arg.deviceId, DEVICE_ID);
172     ASSERT_EQ(arg.sensorType, InputDeviceSensorType::GYROSCOPE);
173     ASSERT_EQ(arg.accuracy, InputDeviceSensorAccuracy::HIGH);
174     ASSERT_EQ(arg.hwTimestamp, ARBITRARY_TIME);
175     ASSERT_EQ(arg.values, values);
176     mMapper->flushSensor(InputDeviceSensorType::GYROSCOPE);
177 }
178 
179 } // namespace android