1 /*
2  * Copyright (C) 2018 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 "VtsHalSensorsV2_XTargetTest.h"
18 
19 // Test if sensor hal can do UI speed accelerometer streaming properly
TEST_P(SensorsHidlTest,AccelerometerStreamingOperationSlow)20 TEST_P(SensorsHidlTest, AccelerometerStreamingOperationSlow) {
21     testStreamingOperation(SensorTypeVersion::ACCELEROMETER, std::chrono::milliseconds(200),
22                            std::chrono::seconds(5), mAccelNormChecker);
23 }
24 
25 // Test if sensor hal can do normal speed accelerometer streaming properly
TEST_P(SensorsHidlTest,AccelerometerStreamingOperationNormal)26 TEST_P(SensorsHidlTest, AccelerometerStreamingOperationNormal) {
27     testStreamingOperation(SensorTypeVersion::ACCELEROMETER, std::chrono::milliseconds(20),
28                            std::chrono::seconds(5), mAccelNormChecker);
29 }
30 
31 // Test if sensor hal can do game speed accelerometer streaming properly
TEST_P(SensorsHidlTest,AccelerometerStreamingOperationFast)32 TEST_P(SensorsHidlTest, AccelerometerStreamingOperationFast) {
33     testStreamingOperation(SensorTypeVersion::ACCELEROMETER, std::chrono::milliseconds(5),
34                            std::chrono::seconds(5), mAccelNormChecker);
35 }
36 
37 // Test if sensor hal can do UI speed gyroscope streaming properly
TEST_P(SensorsHidlTest,GyroscopeStreamingOperationSlow)38 TEST_P(SensorsHidlTest, GyroscopeStreamingOperationSlow) {
39     testStreamingOperation(SensorTypeVersion::GYROSCOPE, std::chrono::milliseconds(200),
40                            std::chrono::seconds(5), mGyroNormChecker);
41 }
42 
43 // Test if sensor hal can do normal speed gyroscope streaming properly
TEST_P(SensorsHidlTest,GyroscopeStreamingOperationNormal)44 TEST_P(SensorsHidlTest, GyroscopeStreamingOperationNormal) {
45     testStreamingOperation(SensorTypeVersion::GYROSCOPE, std::chrono::milliseconds(20),
46                            std::chrono::seconds(5), mGyroNormChecker);
47 }
48 
49 // Test if sensor hal can do game speed gyroscope streaming properly
TEST_P(SensorsHidlTest,GyroscopeStreamingOperationFast)50 TEST_P(SensorsHidlTest, GyroscopeStreamingOperationFast) {
51     testStreamingOperation(SensorTypeVersion::GYROSCOPE, std::chrono::milliseconds(5),
52                            std::chrono::seconds(5), mGyroNormChecker);
53 }
54 
55 // Test if sensor hal can do UI speed magnetometer streaming properly
TEST_P(SensorsHidlTest,MagnetometerStreamingOperationSlow)56 TEST_P(SensorsHidlTest, MagnetometerStreamingOperationSlow) {
57     testStreamingOperation(SensorTypeVersion::MAGNETIC_FIELD, std::chrono::milliseconds(200),
58                            std::chrono::seconds(5), NullChecker<EventType>());
59 }
60 
61 // Test if sensor hal can do normal speed magnetometer streaming properly
TEST_P(SensorsHidlTest,MagnetometerStreamingOperationNormal)62 TEST_P(SensorsHidlTest, MagnetometerStreamingOperationNormal) {
63     testStreamingOperation(SensorTypeVersion::MAGNETIC_FIELD, std::chrono::milliseconds(20),
64                            std::chrono::seconds(5), NullChecker<EventType>());
65 }
66 
67 // Test if sensor hal can do game speed magnetometer streaming properly
TEST_P(SensorsHidlTest,MagnetometerStreamingOperationFast)68 TEST_P(SensorsHidlTest, MagnetometerStreamingOperationFast) {
69     testStreamingOperation(SensorTypeVersion::MAGNETIC_FIELD, std::chrono::milliseconds(5),
70                            std::chrono::seconds(5), NullChecker<EventType>());
71 }
72 
73 // Test if sensor hal can do accelerometer sampling rate switch properly when sensor is active
TEST_P(SensorsHidlTest,AccelerometerSamplingPeriodHotSwitchOperation)74 TEST_P(SensorsHidlTest, AccelerometerSamplingPeriodHotSwitchOperation) {
75     testSamplingRateHotSwitchOperation(SensorTypeVersion::ACCELEROMETER);
76     testSamplingRateHotSwitchOperation(SensorTypeVersion::ACCELEROMETER, false /*fastToSlow*/);
77 }
78 
79 // Test if sensor hal can do gyroscope sampling rate switch properly when sensor is active
TEST_P(SensorsHidlTest,GyroscopeSamplingPeriodHotSwitchOperation)80 TEST_P(SensorsHidlTest, GyroscopeSamplingPeriodHotSwitchOperation) {
81     testSamplingRateHotSwitchOperation(SensorTypeVersion::GYROSCOPE);
82     testSamplingRateHotSwitchOperation(SensorTypeVersion::GYROSCOPE, false /*fastToSlow*/);
83 }
84 
85 // Test if sensor hal can do magnetometer sampling rate switch properly when sensor is active
TEST_P(SensorsHidlTest,MagnetometerSamplingPeriodHotSwitchOperation)86 TEST_P(SensorsHidlTest, MagnetometerSamplingPeriodHotSwitchOperation) {
87     testSamplingRateHotSwitchOperation(SensorTypeVersion::MAGNETIC_FIELD);
88     testSamplingRateHotSwitchOperation(SensorTypeVersion::MAGNETIC_FIELD, false /*fastToSlow*/);
89 }
90 
91 // Test if sensor hal can do accelerometer batching properly
TEST_P(SensorsHidlTest,AccelerometerBatchingOperation)92 TEST_P(SensorsHidlTest, AccelerometerBatchingOperation) {
93     testBatchingOperation(SensorTypeVersion::ACCELEROMETER);
94 }
95 
96 // Test if sensor hal can do gyroscope batching properly
TEST_P(SensorsHidlTest,GyroscopeBatchingOperation)97 TEST_P(SensorsHidlTest, GyroscopeBatchingOperation) {
98     testBatchingOperation(SensorTypeVersion::GYROSCOPE);
99 }
100 
101 // Test if sensor hal can do magnetometer batching properly
TEST_P(SensorsHidlTest,MagnetometerBatchingOperation)102 TEST_P(SensorsHidlTest, MagnetometerBatchingOperation) {
103     testBatchingOperation(SensorTypeVersion::MAGNETIC_FIELD);
104 }
105 
106 // Test sensor event direct report with ashmem for accel sensor at normal rate
TEST_P(SensorsHidlTest,AccelerometerAshmemDirectReportOperationNormal)107 TEST_P(SensorsHidlTest, AccelerometerAshmemDirectReportOperationNormal) {
108     testDirectReportOperation(SensorTypeVersion::ACCELEROMETER, SharedMemType::ASHMEM,
109                               RateLevel::NORMAL, mAccelNormChecker);
110 }
111 
112 // Test sensor event direct report with ashmem for accel sensor at fast rate
TEST_P(SensorsHidlTest,AccelerometerAshmemDirectReportOperationFast)113 TEST_P(SensorsHidlTest, AccelerometerAshmemDirectReportOperationFast) {
114     testDirectReportOperation(SensorTypeVersion::ACCELEROMETER, SharedMemType::ASHMEM,
115                               RateLevel::FAST, mAccelNormChecker);
116 }
117 
118 // Test sensor event direct report with ashmem for accel sensor at very fast rate
TEST_P(SensorsHidlTest,AccelerometerAshmemDirectReportOperationVeryFast)119 TEST_P(SensorsHidlTest, AccelerometerAshmemDirectReportOperationVeryFast) {
120     testDirectReportOperation(SensorTypeVersion::ACCELEROMETER, SharedMemType::ASHMEM,
121                               RateLevel::VERY_FAST, mAccelNormChecker);
122 }
123 
124 // Test sensor event direct report with ashmem for gyro sensor at normal rate
TEST_P(SensorsHidlTest,GyroscopeAshmemDirectReportOperationNormal)125 TEST_P(SensorsHidlTest, GyroscopeAshmemDirectReportOperationNormal) {
126     testDirectReportOperation(SensorTypeVersion::GYROSCOPE, SharedMemType::ASHMEM,
127                               RateLevel::NORMAL, mGyroNormChecker);
128 }
129 
130 // Test sensor event direct report with ashmem for gyro sensor at fast rate
TEST_P(SensorsHidlTest,GyroscopeAshmemDirectReportOperationFast)131 TEST_P(SensorsHidlTest, GyroscopeAshmemDirectReportOperationFast) {
132     testDirectReportOperation(SensorTypeVersion::GYROSCOPE, SharedMemType::ASHMEM, RateLevel::FAST,
133                               mGyroNormChecker);
134 }
135 
136 // Test sensor event direct report with ashmem for gyro sensor at very fast rate
TEST_P(SensorsHidlTest,GyroscopeAshmemDirectReportOperationVeryFast)137 TEST_P(SensorsHidlTest, GyroscopeAshmemDirectReportOperationVeryFast) {
138     testDirectReportOperation(SensorTypeVersion::GYROSCOPE, SharedMemType::ASHMEM,
139                               RateLevel::VERY_FAST, mGyroNormChecker);
140 }
141 
142 // Test sensor event direct report with ashmem for mag sensor at normal rate
TEST_P(SensorsHidlTest,MagnetometerAshmemDirectReportOperationNormal)143 TEST_P(SensorsHidlTest, MagnetometerAshmemDirectReportOperationNormal) {
144     testDirectReportOperation(SensorTypeVersion::MAGNETIC_FIELD, SharedMemType::ASHMEM,
145                               RateLevel::NORMAL, NullChecker<EventType>());
146 }
147 
148 // Test sensor event direct report with ashmem for mag sensor at fast rate
TEST_P(SensorsHidlTest,MagnetometerAshmemDirectReportOperationFast)149 TEST_P(SensorsHidlTest, MagnetometerAshmemDirectReportOperationFast) {
150     testDirectReportOperation(SensorTypeVersion::MAGNETIC_FIELD, SharedMemType::ASHMEM,
151                               RateLevel::FAST, NullChecker<EventType>());
152 }
153 
154 // Test sensor event direct report with ashmem for mag sensor at very fast rate
TEST_P(SensorsHidlTest,MagnetometerAshmemDirectReportOperationVeryFast)155 TEST_P(SensorsHidlTest, MagnetometerAshmemDirectReportOperationVeryFast) {
156     testDirectReportOperation(SensorTypeVersion::MAGNETIC_FIELD, SharedMemType::ASHMEM,
157                               RateLevel::VERY_FAST, NullChecker<EventType>());
158 }
159 
160 // Test sensor event direct report with gralloc for accel sensor at normal rate
TEST_P(SensorsHidlTest,AccelerometerGrallocDirectReportOperationNormal)161 TEST_P(SensorsHidlTest, AccelerometerGrallocDirectReportOperationNormal) {
162     testDirectReportOperation(SensorTypeVersion::ACCELEROMETER, SharedMemType::GRALLOC,
163                               RateLevel::NORMAL, mAccelNormChecker);
164 }
165 
166 // Test sensor event direct report with gralloc for accel sensor at fast rate
TEST_P(SensorsHidlTest,AccelerometerGrallocDirectReportOperationFast)167 TEST_P(SensorsHidlTest, AccelerometerGrallocDirectReportOperationFast) {
168     testDirectReportOperation(SensorTypeVersion::ACCELEROMETER, SharedMemType::GRALLOC,
169                               RateLevel::FAST, mAccelNormChecker);
170 }
171 
172 // Test sensor event direct report with gralloc for accel sensor at very fast rate
TEST_P(SensorsHidlTest,AccelerometerGrallocDirectReportOperationVeryFast)173 TEST_P(SensorsHidlTest, AccelerometerGrallocDirectReportOperationVeryFast) {
174     testDirectReportOperation(SensorTypeVersion::ACCELEROMETER, SharedMemType::GRALLOC,
175                               RateLevel::VERY_FAST, mAccelNormChecker);
176 }
177 
178 // Test sensor event direct report with gralloc for gyro sensor at normal rate
TEST_P(SensorsHidlTest,GyroscopeGrallocDirectReportOperationNormal)179 TEST_P(SensorsHidlTest, GyroscopeGrallocDirectReportOperationNormal) {
180     testDirectReportOperation(SensorTypeVersion::GYROSCOPE, SharedMemType::GRALLOC,
181                               RateLevel::NORMAL, mGyroNormChecker);
182 }
183 
184 // Test sensor event direct report with gralloc for gyro sensor at fast rate
TEST_P(SensorsHidlTest,GyroscopeGrallocDirectReportOperationFast)185 TEST_P(SensorsHidlTest, GyroscopeGrallocDirectReportOperationFast) {
186     testDirectReportOperation(SensorTypeVersion::GYROSCOPE, SharedMemType::GRALLOC, RateLevel::FAST,
187                               mGyroNormChecker);
188 }
189 
190 // Test sensor event direct report with gralloc for gyro sensor at very fast rate
TEST_P(SensorsHidlTest,GyroscopeGrallocDirectReportOperationVeryFast)191 TEST_P(SensorsHidlTest, GyroscopeGrallocDirectReportOperationVeryFast) {
192     testDirectReportOperation(SensorTypeVersion::GYROSCOPE, SharedMemType::GRALLOC,
193                               RateLevel::VERY_FAST, mGyroNormChecker);
194 }
195 
196 // Test sensor event direct report with gralloc for mag sensor at normal rate
TEST_P(SensorsHidlTest,MagnetometerGrallocDirectReportOperationNormal)197 TEST_P(SensorsHidlTest, MagnetometerGrallocDirectReportOperationNormal) {
198     testDirectReportOperation(SensorTypeVersion::MAGNETIC_FIELD, SharedMemType::GRALLOC,
199                               RateLevel::NORMAL, NullChecker<EventType>());
200 }
201 
202 // Test sensor event direct report with gralloc for mag sensor at fast rate
TEST_P(SensorsHidlTest,MagnetometerGrallocDirectReportOperationFast)203 TEST_P(SensorsHidlTest, MagnetometerGrallocDirectReportOperationFast) {
204     testDirectReportOperation(SensorTypeVersion::MAGNETIC_FIELD, SharedMemType::GRALLOC,
205                               RateLevel::FAST, NullChecker<EventType>());
206 }
207 
208 // Test sensor event direct report with gralloc for mag sensor at very fast rate
TEST_P(SensorsHidlTest,MagnetometerGrallocDirectReportOperationVeryFast)209 TEST_P(SensorsHidlTest, MagnetometerGrallocDirectReportOperationVeryFast) {
210     testDirectReportOperation(SensorTypeVersion::MAGNETIC_FIELD, SharedMemType::GRALLOC,
211                               RateLevel::VERY_FAST, NullChecker<EventType>());
212 }
213 
TEST_P(SensorsHidlTest,ConfigureDirectChannelWithInvalidHandle)214 TEST_P(SensorsHidlTest, ConfigureDirectChannelWithInvalidHandle) {
215     SensorInfoType sensor;
216     SharedMemType memType;
217     RateLevel rate;
218     if (!getDirectChannelSensor(&sensor, &memType, &rate)) {
219         return;
220     }
221 
222     // Verify that an invalid channel handle produces a BAD_VALUE result
223     configDirectReport(sensor.sensorHandle, -1, rate, [](Result result, int32_t /* reportToken */) {
224         ASSERT_EQ(result, Result::BAD_VALUE);
225     });
226 }
227 
TEST_P(SensorsHidlTest,CleanupDirectConnectionOnInitialize)228 TEST_P(SensorsHidlTest, CleanupDirectConnectionOnInitialize) {
229     constexpr size_t kNumEvents = 1;
230     constexpr size_t kMemSize = kNumEvents * kEventSize;
231 
232     SensorInfoType sensor;
233     SharedMemType memType;
234     RateLevel rate;
235 
236     if (!getDirectChannelSensor(&sensor, &memType, &rate)) {
237         return;
238     }
239 
240     std::shared_ptr<SensorsTestSharedMemory<SensorTypeVersion, EventType>> mem(
241             SensorsTestSharedMemory<SensorTypeVersion, EventType>::create(memType, kMemSize));
242     ASSERT_NE(mem, nullptr);
243 
244     int32_t directChannelHandle = 0;
245     registerDirectChannel(mem->getSharedMemInfo(), [&](Result result, int32_t channelHandle) {
246         ASSERT_EQ(result, Result::OK);
247         directChannelHandle = channelHandle;
248     });
249 
250     // Configure the channel and expect success
251     configDirectReport(
252             sensor.sensorHandle, directChannelHandle, rate,
253             [](Result result, int32_t /* reportToken */) { ASSERT_EQ(result, Result::OK); });
254 
255     // Call initialize() via the environment setup to cause the HAL to re-initialize
256     // Clear the active direct connections so they are not stopped during TearDown
257     auto handles = mDirectChannelHandles;
258     mDirectChannelHandles.clear();
259     getEnvironment()->TearDown();
260     getEnvironment()->SetUp();
261     if (HasFatalFailure()) {
262         return;  // Exit early if resetting the environment failed
263     }
264 
265     // Attempt to configure the direct channel and expect it to fail
266     configDirectReport(
267             sensor.sensorHandle, directChannelHandle, rate,
268             [](Result result, int32_t /* reportToken */) { ASSERT_EQ(result, Result::BAD_VALUE); });
269 
270     // Restore original handles, though they should already be deactivated
271     mDirectChannelHandles = handles;
272 }
273 
TEST_P(SensorsHidlTest,SensorListDoesntContainInvalidType)274 TEST_P(SensorsHidlTest, SensorListDoesntContainInvalidType) {
275     getSensors()->getSensorsList([&](const auto& list) {
276         const size_t count = list.size();
277         for (size_t i = 0; i < count; ++i) {
278             const auto& s = list[i];
279             EXPECT_FALSE(s.type == ::android::hardware::sensors::V2_1::SensorType::HINGE_ANGLE);
280         }
281     });
282 }
283 
TEST_P(SensorsHidlTest,FlushNonexistentSensor)284 TEST_P(SensorsHidlTest, FlushNonexistentSensor) {
285     SensorInfoType sensor;
286     std::vector<SensorInfoType> sensors = getNonOneShotSensors();
287     if (sensors.size() == 0) {
288         sensors = getOneShotSensors();
289         if (sensors.size() == 0) {
290             return;
291         }
292     }
293     sensor = sensors.front();
294     sensor.sensorHandle = getInvalidSensorHandle();
295     runSingleFlushTest(std::vector<SensorInfoType>{sensor}, false /* activateSensor */,
296                        0 /* expectedFlushCount */, Result::BAD_VALUE);
297 }
298 
299 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SensorsHidlTest);
300 INSTANTIATE_TEST_SUITE_P(PerInstance, SensorsHidlTest,
301                          testing::ValuesIn(android::hardware::getAllHalInstanceNames(
302                                  android::hardware::sensors::V2_0::ISensors::descriptor)),
303                          android::hardware::PrintInstanceNameToString);