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