Home | History | Annotate | Download | only in sensors
      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 com.android.cts.verifier.sensors;
     18 
     19 import com.android.cts.verifier.R;
     20 import com.android.cts.verifier.sensors.base.SensorCtsVerifierTestActivity;
     21 
     22 import android.content.pm.PackageManager;
     23 import android.hardware.Sensor;
     24 import android.hardware.SensorManager;
     25 import android.hardware.cts.helpers.TestSensorEnvironment;
     26 import android.hardware.cts.helpers.sensoroperations.TestSensorOperation;
     27 
     28 import java.util.concurrent.TimeUnit;
     29 
     30 /**
     31  * Activity that verifies batching capabilities for sensors
     32  * (https://source.android.com/devices/sensors/batching.html).
     33  *
     34  * If a sensor supports the batching mode, FifoReservedEventCount for that sensor should be greater
     35  * than one.
     36  */
     37 public class BatchingTestActivity extends SensorCtsVerifierTestActivity {
     38     public BatchingTestActivity() {
     39         super(BatchingTestActivity.class);
     40     }
     41 
     42     private static final int SENSOR_BATCHING_RATE_US = SensorManager.SENSOR_DELAY_FASTEST;
     43     private static final int REPORT_LATENCY_10_SEC = 10;
     44     private static final int BATCHING_PADDING_TIME_S = 2;
     45 
     46     // we are testing sensors that only trigger based on external events, so leave enough time for
     47     // such events to generate
     48     private static final int REPORT_LATENCY_25_SEC = 25;
     49 
     50     // TODO: refactor to discover all available sensors of each type and dynamically generate test
     51     // cases for all of them
     52     @SuppressWarnings("unused")
     53     public String testStepCounter_batching() throws Throwable {
     54         return runBatchTest(
     55                 Sensor.TYPE_STEP_COUNTER,
     56                 REPORT_LATENCY_25_SEC,
     57                 R.string.snsr_batching_walking_needed);
     58     }
     59 
     60     @SuppressWarnings("unused")
     61     public String testStepCounter_flush() throws Throwable {
     62         return runFlushTest(
     63                 Sensor.TYPE_STEP_COUNTER,
     64                 REPORT_LATENCY_25_SEC,
     65                 R.string.snsr_batching_walking_needed);
     66     }
     67 
     68     @SuppressWarnings("unused")
     69     public String testStepDetector_batching() throws Throwable {
     70         return  runBatchTest(
     71                 Sensor.TYPE_STEP_DETECTOR,
     72                 REPORT_LATENCY_25_SEC,
     73                 R.string.snsr_batching_walking_needed);
     74     }
     75 
     76     @SuppressWarnings("unused")
     77     public String testStepDetector_flush() throws Throwable {
     78         return  runFlushTest(
     79                 Sensor.TYPE_STEP_DETECTOR,
     80                 REPORT_LATENCY_25_SEC,
     81                 R.string.snsr_batching_walking_needed);
     82     }
     83 
     84     @SuppressWarnings("unused")
     85     public String testProximity_batching() throws Throwable {
     86         if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_PROXIMITY)) {
     87             return null;
     88         }
     89         return runBatchTest(
     90                 Sensor.TYPE_PROXIMITY,
     91                 REPORT_LATENCY_10_SEC,
     92                 R.string.snsr_interaction_needed);
     93     }
     94 
     95     @SuppressWarnings("unused")
     96     public String testProximity_flush() throws Throwable {
     97         if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_PROXIMITY)) {
     98             return null;
     99         }
    100         return runFlushTest(
    101                 Sensor.TYPE_PROXIMITY,
    102                 REPORT_LATENCY_10_SEC,
    103                 R.string.snsr_interaction_needed);
    104     }
    105 
    106     @SuppressWarnings("unused")
    107     public String testLight_batching() throws Throwable {
    108         return runBatchTest(
    109                 Sensor.TYPE_LIGHT,
    110                 REPORT_LATENCY_10_SEC,
    111                 R.string.snsr_interaction_needed);
    112     }
    113 
    114     @SuppressWarnings("unused")
    115     public String testLight_flush() throws Throwable {
    116         return runFlushTest(
    117                 Sensor.TYPE_LIGHT,
    118                 REPORT_LATENCY_10_SEC,
    119                 R.string.snsr_interaction_needed);
    120     }
    121 
    122     private String runBatchTest(int sensorType, int maxBatchReportLatencySec, int instructionsResId)
    123             throws Throwable {
    124         getTestLogger().logInstructions(instructionsResId);
    125         waitForUserToBegin();
    126 
    127         int maxBatchReportLatencyUs = (int) TimeUnit.SECONDS.toMicros(maxBatchReportLatencySec);
    128         TestSensorEnvironment environment = new TestSensorEnvironment(
    129                 getApplicationContext(),
    130                 sensorType,
    131                 SENSOR_BATCHING_RATE_US,
    132                 maxBatchReportLatencyUs);
    133 
    134         int testDurationSec = maxBatchReportLatencySec + BATCHING_PADDING_TIME_S;
    135         TestSensorOperation operation =
    136                 TestSensorOperation.createOperation(environment, testDurationSec,TimeUnit.SECONDS);
    137         return executeTest(operation);
    138     }
    139 
    140     private String runFlushTest(int sensorType, int maxBatchReportLatencySec, int instructionsResId)
    141             throws Throwable {
    142         getTestLogger().logInstructions(instructionsResId);
    143         waitForUserToBegin();
    144 
    145         int maxBatchReportLatencyUs = (int) TimeUnit.SECONDS.toMicros(maxBatchReportLatencySec);
    146         TestSensorEnvironment environment = new TestSensorEnvironment(
    147                 getApplicationContext(),
    148                 sensorType,
    149                 SENSOR_BATCHING_RATE_US,
    150                 maxBatchReportLatencyUs);
    151 
    152         int flushDurationSec = maxBatchReportLatencySec / 2;
    153         TestSensorOperation operation = TestSensorOperation
    154                 .createFlushOperation(environment, flushDurationSec, TimeUnit.SECONDS);
    155         return executeTest(operation);
    156     }
    157 
    158     private String executeTest(TestSensorOperation operation) throws Exception {
    159         operation.addDefaultVerifications();
    160         operation.execute(getCurrentTestNode());
    161         return null;
    162     }
    163 }
    164