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