1 /* 2 * Copyright (C) 2014 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 package android.hardware.cts; 18 19 import android.hardware.Sensor; 20 import android.hardware.SensorManager; 21 import android.hardware.cts.helpers.SensorStats; 22 import android.hardware.cts.helpers.TestSensorEnvironment; 23 import android.hardware.cts.helpers.sensoroperations.TestSensorOperation; 24 import android.hardware.cts.helpers.sensorverification.ISensorVerification; 25 26 import java.util.concurrent.TimeUnit; 27 28 /** 29 * Set of tests to verify that sensors operate correctly when operating in batching mode. 30 * This class defines tests for continuous sensors when the device is awake. 31 * On-change and special sensors are tested separately inside CtsVerifier, and they are defined in: 32 * {@link com.android.cts.verifier.sensors.BatchingTestActivity}. 33 * 34 * Each test is expected to pass even if batching is not supported for a particular sensor. This is 35 * usually achieved by ensuring that {@link ISensorVerification}s fallback accordingly. 36 * 37 * <p>To execute these test cases, the following command can be used:</p> 38 * <pre> 39 * adb shell am instrument -e class android.hardware.cts.SensorBatchingTests \ 40 * -w com.android.cts.hardware/android.test.AndroidJUnitRunner 41 * </pre> 42 */ 43 public class SensorBatchingTests extends SensorTestCase { 44 private static final String TAG = "SensorBatchingTests"; 45 46 private static final int BATCHING_10S = 10; 47 private static final int RATE_50HZ = 20000; 48 private static final int RATE_FASTEST = SensorManager.SENSOR_DELAY_FASTEST; 49 50 /** 51 * An arbitrary 'padding' time slot to wait for events after batching latency expires. 52 * This allows for the test to wait for event arrivals after batching was expected. 53 */ 54 private static final int BATCHING_PADDING_TIME_S = 2; 55 testAccelerometer_fastest_batching()56 public void testAccelerometer_fastest_batching() throws Throwable { 57 runBatchingSensorTest(Sensor.TYPE_ACCELEROMETER, RATE_FASTEST, BATCHING_10S); 58 } 59 testAccelerometer_50hz_batching()60 public void testAccelerometer_50hz_batching() throws Throwable { 61 runBatchingSensorTest(Sensor.TYPE_ACCELEROMETER, RATE_50HZ, BATCHING_10S); 62 } 63 testAccelerometer_fastest_flush()64 public void testAccelerometer_fastest_flush() throws Throwable { 65 runFlushSensorTest(Sensor.TYPE_ACCELEROMETER, RATE_FASTEST, BATCHING_10S); 66 } 67 testAccelerometer_50hz_flush()68 public void testAccelerometer_50hz_flush() throws Throwable { 69 runFlushSensorTest(Sensor.TYPE_ACCELEROMETER, RATE_50HZ, BATCHING_10S); 70 } 71 testMagneticField_fastest_batching()72 public void testMagneticField_fastest_batching() throws Throwable { 73 runBatchingSensorTest(Sensor.TYPE_MAGNETIC_FIELD, RATE_FASTEST, BATCHING_10S); 74 } 75 testMagneticField_50hz_batching()76 public void testMagneticField_50hz_batching() throws Throwable { 77 runBatchingSensorTest(Sensor.TYPE_MAGNETIC_FIELD, RATE_50HZ, BATCHING_10S); 78 } 79 testMagneticField_fastest_flush()80 public void testMagneticField_fastest_flush() throws Throwable { 81 runFlushSensorTest(Sensor.TYPE_MAGNETIC_FIELD, RATE_FASTEST, BATCHING_10S); 82 } 83 testMagneticField_50hz_flush()84 public void testMagneticField_50hz_flush() throws Throwable { 85 runFlushSensorTest(Sensor.TYPE_MAGNETIC_FIELD, RATE_50HZ, BATCHING_10S); 86 } 87 88 @SuppressWarnings("deprecation") testOrientation_fastest_batching()89 public void testOrientation_fastest_batching() throws Throwable { 90 runBatchingSensorTest(Sensor.TYPE_ORIENTATION, RATE_FASTEST, BATCHING_10S); 91 } 92 93 @SuppressWarnings("deprecation") testOrientation_50hz_batching()94 public void testOrientation_50hz_batching() throws Throwable { 95 runBatchingSensorTest(Sensor.TYPE_ORIENTATION, RATE_50HZ, BATCHING_10S); 96 } 97 98 @SuppressWarnings("deprecation") testOrientation_fastest_flush()99 public void testOrientation_fastest_flush() throws Throwable { 100 runFlushSensorTest(Sensor.TYPE_ORIENTATION, RATE_FASTEST, BATCHING_10S); 101 } 102 103 @SuppressWarnings("deprecation") testOrientation_50hz_flush()104 public void testOrientation_50hz_flush() throws Throwable { 105 runFlushSensorTest(Sensor.TYPE_ORIENTATION, RATE_50HZ, BATCHING_10S); 106 } 107 testGyroscope_fastest_batching()108 public void testGyroscope_fastest_batching() throws Throwable { 109 runBatchingSensorTest(Sensor.TYPE_GYROSCOPE, RATE_FASTEST, BATCHING_10S); 110 } 111 testGyroscope_50hz_batching()112 public void testGyroscope_50hz_batching() throws Throwable { 113 runBatchingSensorTest(Sensor.TYPE_GYROSCOPE, RATE_50HZ, BATCHING_10S); 114 } 115 testGyroscope_fastest_flush()116 public void testGyroscope_fastest_flush() throws Throwable { 117 runFlushSensorTest(Sensor.TYPE_GYROSCOPE, SensorManager.SENSOR_DELAY_FASTEST, BATCHING_10S); 118 } 119 testGyroscope_50hz_flush()120 public void testGyroscope_50hz_flush() throws Throwable { 121 runFlushSensorTest(Sensor.TYPE_GYROSCOPE, RATE_50HZ, BATCHING_10S); 122 } 123 testPressure_fastest_batching()124 public void testPressure_fastest_batching() throws Throwable { 125 runBatchingSensorTest(Sensor.TYPE_PRESSURE, RATE_FASTEST, BATCHING_10S); 126 } 127 testPressure_50hz_batching()128 public void testPressure_50hz_batching() throws Throwable { 129 runBatchingSensorTest(Sensor.TYPE_PRESSURE, RATE_50HZ, BATCHING_10S); 130 } 131 testPressure_fastest_flush()132 public void testPressure_fastest_flush() throws Throwable { 133 runFlushSensorTest(Sensor.TYPE_PRESSURE, SensorManager.SENSOR_DELAY_FASTEST, BATCHING_10S); 134 } 135 testPressure_50hz_flush()136 public void testPressure_50hz_flush() throws Throwable { 137 runFlushSensorTest(Sensor.TYPE_PRESSURE, RATE_50HZ, BATCHING_10S); 138 } 139 testGravity_fastest_batching()140 public void testGravity_fastest_batching() throws Throwable { 141 runBatchingSensorTest(Sensor.TYPE_GRAVITY, RATE_FASTEST, BATCHING_10S); 142 } 143 testGravity_50hz_batching()144 public void testGravity_50hz_batching() throws Throwable { 145 runBatchingSensorTest(Sensor.TYPE_GRAVITY, RATE_50HZ, BATCHING_10S); 146 } 147 testGravity_fastest_flush()148 public void testGravity_fastest_flush() throws Throwable { 149 runFlushSensorTest(Sensor.TYPE_GRAVITY, SensorManager.SENSOR_DELAY_FASTEST, BATCHING_10S); 150 } 151 testGravity_50hz_flush()152 public void testGravity_50hz_flush() throws Throwable { 153 runFlushSensorTest(Sensor.TYPE_GRAVITY, RATE_50HZ, BATCHING_10S); 154 } 155 testRotationVector_fastest_batching()156 public void testRotationVector_fastest_batching() throws Throwable { 157 runBatchingSensorTest(Sensor.TYPE_ROTATION_VECTOR, RATE_FASTEST, BATCHING_10S); 158 } 159 testRotationVector_50hz_batching()160 public void testRotationVector_50hz_batching() throws Throwable { 161 runBatchingSensorTest(Sensor.TYPE_ROTATION_VECTOR, RATE_50HZ, BATCHING_10S); 162 } 163 testRotationVector_fastest_flush()164 public void testRotationVector_fastest_flush() throws Throwable { 165 runFlushSensorTest(Sensor.TYPE_ROTATION_VECTOR, RATE_FASTEST, BATCHING_10S); 166 } 167 testRotationVector_50hz_flush()168 public void testRotationVector_50hz_flush() throws Throwable { 169 runFlushSensorTest(Sensor.TYPE_ROTATION_VECTOR, RATE_50HZ, BATCHING_10S); 170 } 171 testMagneticFieldUncalibrated_fastest_batching()172 public void testMagneticFieldUncalibrated_fastest_batching() throws Throwable { 173 runBatchingSensorTest(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED, RATE_FASTEST, BATCHING_10S); 174 } 175 testMagneticFieldUncalibrated_50hz_batching()176 public void testMagneticFieldUncalibrated_50hz_batching() throws Throwable { 177 runBatchingSensorTest(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED, RATE_50HZ, BATCHING_10S); 178 } 179 testMagneticFieldUncalibrated_fastest_flush()180 public void testMagneticFieldUncalibrated_fastest_flush() throws Throwable { 181 runFlushSensorTest(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED, RATE_FASTEST, BATCHING_10S); 182 } 183 testMagneticFieldUncalibrated_50hz_flush()184 public void testMagneticFieldUncalibrated_50hz_flush() throws Throwable { 185 runFlushSensorTest(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED, RATE_50HZ, BATCHING_10S); 186 } 187 testGameRotationVector_fastest_batching()188 public void testGameRotationVector_fastest_batching() throws Throwable { 189 runBatchingSensorTest(Sensor.TYPE_GAME_ROTATION_VECTOR, RATE_FASTEST, BATCHING_10S); 190 } 191 testGameRotationVector_50hz_batching()192 public void testGameRotationVector_50hz_batching() throws Throwable { 193 runBatchingSensorTest(Sensor.TYPE_GAME_ROTATION_VECTOR, RATE_50HZ, BATCHING_10S); 194 } 195 testGameRotationVector_fastest_flush()196 public void testGameRotationVector_fastest_flush() throws Throwable { 197 runFlushSensorTest(Sensor.TYPE_GAME_ROTATION_VECTOR, RATE_FASTEST, BATCHING_10S); 198 } 199 testGameRotationVector_50hz_flush()200 public void testGameRotationVector_50hz_flush() throws Throwable { 201 runFlushSensorTest(Sensor.TYPE_GAME_ROTATION_VECTOR, RATE_50HZ, BATCHING_10S); 202 } 203 testGyroscopeUncalibrated_fastest_batching()204 public void testGyroscopeUncalibrated_fastest_batching() throws Throwable { 205 runBatchingSensorTest(Sensor.TYPE_GYROSCOPE_UNCALIBRATED, RATE_FASTEST, BATCHING_10S); 206 } 207 testGyroscopeUncalibrated_50hz_batching()208 public void testGyroscopeUncalibrated_50hz_batching() throws Throwable { 209 runBatchingSensorTest(Sensor.TYPE_GYROSCOPE_UNCALIBRATED, RATE_50HZ, BATCHING_10S); 210 } 211 testGyroscopeUncalibrated_fastest_flush()212 public void testGyroscopeUncalibrated_fastest_flush() throws Throwable { 213 runFlushSensorTest(Sensor.TYPE_GYROSCOPE_UNCALIBRATED, RATE_FASTEST, BATCHING_10S); 214 } 215 testGyroscopeUncalibrated_50hz_flush()216 public void testGyroscopeUncalibrated_50hz_flush() throws Throwable { 217 runFlushSensorTest(Sensor.TYPE_GYROSCOPE_UNCALIBRATED, RATE_50HZ, BATCHING_10S); 218 } 219 testLinearAcceleration_fastest_batching()220 public void testLinearAcceleration_fastest_batching() throws Throwable { 221 runBatchingSensorTest(Sensor.TYPE_LINEAR_ACCELERATION, RATE_FASTEST, BATCHING_10S); 222 } 223 testLinearAcceleration_50hz_batching()224 public void testLinearAcceleration_50hz_batching() throws Throwable { 225 runBatchingSensorTest(Sensor.TYPE_LINEAR_ACCELERATION, RATE_50HZ, BATCHING_10S); 226 } 227 testLinearAcceleration_fastest_flush()228 public void testLinearAcceleration_fastest_flush() throws Throwable { 229 runFlushSensorTest(Sensor.TYPE_LINEAR_ACCELERATION, RATE_FASTEST, BATCHING_10S); 230 } 231 testLinearAcceleration_50hz_flush()232 public void testLinearAcceleration_50hz_flush() throws Throwable { 233 runFlushSensorTest(Sensor.TYPE_LINEAR_ACCELERATION, RATE_50HZ, BATCHING_10S); 234 } 235 testGeomagneticRotationVector_fastest_batching()236 public void testGeomagneticRotationVector_fastest_batching() throws Throwable { 237 runBatchingSensorTest(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, RATE_FASTEST, BATCHING_10S); 238 } 239 testGeomagneticRotationVector_50hz_batching()240 public void testGeomagneticRotationVector_50hz_batching() throws Throwable { 241 runBatchingSensorTest(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, RATE_50HZ, BATCHING_10S); 242 } 243 testGeomagneticRotationVector_fastest_flush()244 public void testGeomagneticRotationVector_fastest_flush() throws Throwable { 245 runFlushSensorTest(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, RATE_FASTEST, BATCHING_10S); 246 } 247 testGeomagneticRotationVector_50hz_flush()248 public void testGeomagneticRotationVector_50hz_flush() throws Throwable { 249 runFlushSensorTest(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR, RATE_50HZ, BATCHING_10S); 250 } 251 runBatchingSensorTest(int sensorType, int rateUs, int maxBatchReportLatencySec)252 private void runBatchingSensorTest(int sensorType, int rateUs, int maxBatchReportLatencySec) 253 throws Throwable { 254 int maxBatchReportLatencyUs = (int) TimeUnit.SECONDS.toMicros(maxBatchReportLatencySec); 255 int testDurationSec = maxBatchReportLatencySec + BATCHING_PADDING_TIME_S; 256 257 TestSensorEnvironment environment = new TestSensorEnvironment( 258 getContext(), 259 sensorType, 260 shouldEmulateSensorUnderLoad(), 261 rateUs, 262 maxBatchReportLatencyUs); 263 TestSensorOperation operation = 264 TestSensorOperation.createOperation(environment, testDurationSec, TimeUnit.SECONDS); 265 266 executeTest(environment, operation, false /* flushExpected */); 267 } 268 runFlushSensorTest(int sensorType, int rateUs, int maxBatchReportLatencySec)269 private void runFlushSensorTest(int sensorType, int rateUs, int maxBatchReportLatencySec) 270 throws Throwable { 271 int maxBatchReportLatencyUs = (int) TimeUnit.SECONDS.toMicros(maxBatchReportLatencySec); 272 int flushDurationSec = maxBatchReportLatencySec / 2; 273 274 TestSensorEnvironment environment = new TestSensorEnvironment( 275 getContext(), 276 sensorType, 277 shouldEmulateSensorUnderLoad(), 278 rateUs, 279 maxBatchReportLatencyUs); 280 TestSensorOperation operation = TestSensorOperation 281 .createFlushOperation(environment, flushDurationSec, TimeUnit.SECONDS); 282 283 executeTest(environment, operation, true /* flushExpected */); 284 } 285 executeTest( TestSensorEnvironment environment, TestSensorOperation operation, boolean flushExpected)286 private void executeTest( 287 TestSensorEnvironment environment, 288 TestSensorOperation operation, 289 boolean flushExpected) throws Throwable { 290 operation.addDefaultVerifications(); 291 292 try { 293 operation.execute(getCurrentTestNode()); 294 } finally { 295 SensorStats stats = operation.getStats(); 296 stats.log(TAG); 297 298 String sensorRate; 299 if (environment.getRequestedSamplingPeriodUs() == SensorManager.SENSOR_DELAY_FASTEST) { 300 sensorRate = "fastest"; 301 } else { 302 sensorRate = String.format("%.0fhz", environment.getFrequencyHz()); 303 } 304 String batching = environment.getMaxReportLatencyUs() > 0 ? "_batching" : ""; 305 String flush = flushExpected ? "_flush" : ""; 306 String fileName = String.format( 307 "batching_%s_%s%s%s.txt", 308 SensorStats.getSanitizedSensorName(environment.getSensor()), 309 sensorRate, 310 batching, 311 flush); 312 stats.logToFile(fileName); 313 } 314 } 315 } 316