Skip to content

Commit d862b2c

Browse files
HarryCuttsAndroid (Google) Code Review
authored andcommitted
Merge "SensorInputMapperTest: migrate to InputMapperUnitTest" into main
2 parents 4b32a10 + 68ce2f6 commit d862b2c

3 files changed

Lines changed: 129 additions & 132 deletions

File tree

services/inputflinger/tests/InputMapperTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ void InputMapperUnitTest::SetUpWithBus(int bus) {
5353
}
5454

5555
void InputMapperUnitTest::setupAxis(int axis, bool valid, int32_t min, int32_t max,
56-
int32_t resolution) {
56+
int32_t resolution, int32_t flat, int32_t fuzz) {
5757
EXPECT_CALL(mMockEventHub, getAbsoluteAxisInfo(EVENTHUB_ID, axis))
5858
.WillRepeatedly(Return(valid ? std::optional<RawAbsoluteAxisInfo>{{
5959
.minValue = min,
6060
.maxValue = max,
61-
.flat = 0,
62-
.fuzz = 0,
61+
.flat = flat,
62+
.fuzz = fuzz,
6363
.resolution = resolution,
6464
}}
6565
: std::nullopt));

services/inputflinger/tests/InputMapperTest.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class InputMapperUnitTest : public testing::Test {
4343
virtual void SetUp() override { SetUpWithBus(0); }
4444
virtual void SetUpWithBus(int bus);
4545

46-
void setupAxis(int axis, bool valid, int32_t min, int32_t max, int32_t resolution);
46+
void setupAxis(int axis, bool valid, int32_t min, int32_t max, int32_t resolution,
47+
int32_t flat = 0, int32_t fuzz = 0);
4748

4849
void expectScanCodes(bool present, std::set<int> scanCodes);
4950

services/inputflinger/tests/SensorInputMapper_test.cpp

Lines changed: 124 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -16,168 +16,164 @@
1616

1717
#include "SensorInputMapper.h"
1818

19+
#include <cstdint>
20+
#include <list>
21+
#include <optional>
22+
#include <utility>
1923
#include <vector>
2024

2125
#include <EventHub.h>
2226
#include <NotifyArgs.h>
27+
#include <ftl/enum.h>
28+
#include <gmock/gmock.h>
2329
#include <gtest/gtest.h>
2430
#include <input/Input.h>
2531
#include <input/InputDevice.h>
32+
#include <input/PrintTools.h>
2633
#include <linux/input-event-codes.h>
2734

2835
#include "InputMapperTest.h"
36+
#include "TestEventMatchers.h"
2937

3038
namespace android {
3139

32-
class SensorInputMapperTest : public InputMapperTest {
33-
protected:
34-
static const int32_t ACCEL_RAW_MIN;
35-
static const int32_t ACCEL_RAW_MAX;
36-
static const int32_t ACCEL_RAW_FUZZ;
37-
static const int32_t ACCEL_RAW_FLAT;
38-
static const int32_t ACCEL_RAW_RESOLUTION;
39-
40-
static const int32_t GYRO_RAW_MIN;
41-
static const int32_t GYRO_RAW_MAX;
42-
static const int32_t GYRO_RAW_FUZZ;
43-
static const int32_t GYRO_RAW_FLAT;
44-
static const int32_t GYRO_RAW_RESOLUTION;
45-
46-
static const float GRAVITY_MS2_UNIT;
47-
static const float DEGREE_RADIAN_UNIT;
48-
49-
void prepareAccelAxes();
50-
void prepareGyroAxes();
51-
void setAccelProperties();
52-
void setGyroProperties();
53-
void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
54-
};
40+
using testing::AllOf;
41+
using testing::ElementsAre;
42+
using testing::Return;
43+
using testing::VariantWith;
5544

56-
const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
57-
const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
58-
const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
59-
const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
60-
const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
61-
62-
const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
63-
const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
64-
const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
65-
const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
66-
const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
67-
68-
const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
69-
const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
70-
71-
void SensorInputMapperTest::prepareAccelAxes() {
72-
mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
73-
ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
74-
mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
75-
ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
76-
mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
77-
ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
78-
}
45+
namespace {
7946

80-
void SensorInputMapperTest::prepareGyroAxes() {
81-
mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
82-
GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
83-
mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
84-
GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
85-
mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
86-
GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
87-
}
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;
8852

89-
void SensorInputMapperTest::setAccelProperties() {
90-
mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
91-
/* sensorDataIndex */ 0);
92-
mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
93-
/* sensorDataIndex */ 1);
94-
mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
95-
/* sensorDataIndex */ 2);
96-
mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
97-
addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
98-
addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
99-
addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
100-
addConfigurationProperty("sensor.accelerometer.power", "1.5");
101-
}
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;
10258

103-
void SensorInputMapperTest::setGyroProperties() {
104-
mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
105-
/* sensorDataIndex */ 0);
106-
mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
107-
/* sensorDataIndex */ 1);
108-
mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
109-
/* sensorDataIndex */ 2);
110-
mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
111-
addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
112-
addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
113-
addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
114-
addConfigurationProperty("sensor.gyroscope.power", "0.8");
115-
}
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:
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+
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+
};
11681

11782
TEST_F(SensorInputMapperTest, GetSources) {
118-
SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
83+
mMapper = createInputMapper<SensorInputMapper>(*mDeviceContext,
84+
mFakePolicy->getReaderConfiguration());
11985

120-
ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
86+
ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mMapper->getSources());
12187
}
12288

12389
TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
124-
setAccelProperties();
125-
prepareAccelAxes();
126-
SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
127-
128-
ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
129-
std::chrono::microseconds(10000),
130-
std::chrono::microseconds(0)));
131-
ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
132-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
133-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
134-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
135-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
136-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
137-
138-
NotifySensorArgs args;
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+
139119
std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
140120
-20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
141121
40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
142122

143-
ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
144-
ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
145-
ASSERT_EQ(args.deviceId, DEVICE_ID);
146-
ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
147-
ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
148-
ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
149-
ASSERT_EQ(args.values, values);
150-
mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
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::ACCURACY_HIGH);
129+
ASSERT_EQ(arg.hwTimestamp, ARBITRARY_TIME);
130+
ASSERT_EQ(arg.values, values);
131+
mMapper->flushSensor(InputDeviceSensorType::ACCELEROMETER);
151132
}
152133

153134
TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
154-
setGyroProperties();
155-
prepareGyroAxes();
156-
SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
157-
158-
ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
159-
std::chrono::microseconds(10000),
160-
std::chrono::microseconds(0)));
161-
ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
162-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
163-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
164-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
165-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
166-
process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
167-
168-
NotifySensorArgs args;
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+
169164
std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
170165
-20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
171166
40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
172167

173-
ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
174-
ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
175-
ASSERT_EQ(args.deviceId, DEVICE_ID);
176-
ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
177-
ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
178-
ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
179-
ASSERT_EQ(args.values, values);
180-
mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
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::ACCURACY_HIGH);
174+
ASSERT_EQ(arg.hwTimestamp, ARBITRARY_TIME);
175+
ASSERT_EQ(arg.values, values);
176+
mMapper->flushSensor(InputDeviceSensorType::GYROSCOPE);
181177
}
182178

183179
} // namespace android

0 commit comments

Comments
 (0)