Home | History | Annotate | Download | only in sensorverification
      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.helpers.sensorverification;
     18 
     19 import junit.framework.Assert;
     20 
     21 import android.hardware.Sensor;
     22 import android.hardware.SensorManager;
     23 import android.hardware.cts.helpers.SensorCtsHelper;
     24 import android.hardware.cts.helpers.SensorStats;
     25 import android.hardware.cts.helpers.TestSensorEnvironment;
     26 import android.hardware.cts.helpers.TestSensorEvent;
     27 
     28 import java.util.HashMap;
     29 import java.util.Map;
     30 
     31 /**
     32  * A {@link ISensorVerification} which verifies that the mean of the magnitude of the sensors vector
     33  * is within the expected range.
     34  */
     35 public class MagnitudeVerification extends AbstractSensorVerification {
     36     public static final String PASSED_KEY = "magnitude_passed";
     37 
     38     // sensorType: {expected, threshold}
     39     private static Map<Integer, Float[]> DEFAULTS = new HashMap<Integer, Float[]>(3);
     40     static {
     41         // Use a method so that the @deprecation warning can be set for that method only
     42         setDefaults();
     43     }
     44 
     45     private final float mExpected;
     46     private final float mThreshold;
     47 
     48     private float mSum = 0.0f;
     49     private int mCount = 0;
     50 
     51     /**
     52      * Construct a {@link MagnitudeVerification}
     53      *
     54      * @param expected the expected value
     55      * @param threshold the threshold
     56      */
     57     public MagnitudeVerification(float expected, float threshold) {
     58         mExpected = expected;
     59         mThreshold = threshold;
     60     }
     61 
     62     /**
     63      * Get the default {@link MagnitudeVerification} for a sensor.
     64      *
     65      * @param environment the test environment
     66      * @return the verification or null if the verification does not apply to the sensor.
     67      */
     68     public static MagnitudeVerification getDefault(TestSensorEnvironment environment) {
     69         int sensorType = environment.getSensor().getType();
     70         if (!DEFAULTS.containsKey(sensorType)) {
     71             return null;
     72         }
     73         Float expected = DEFAULTS.get(sensorType)[0];
     74         Float threshold = DEFAULTS.get(sensorType)[1];
     75         return new MagnitudeVerification(expected, threshold);
     76     }
     77 
     78     /**
     79      * Verify that the magnitude is in the acceptable range. Add {@value #PASSED_KEY} and
     80      * {@value SensorStats#MAGNITUDE_KEY} keys to {@link SensorStats}.
     81      *
     82      * @throws AssertionError if the verification failed.
     83      */
     84     @Override
     85     public void verify(TestSensorEnvironment environment, SensorStats stats) {
     86         verify(stats);
     87     }
     88 
     89     /**
     90      * Visible for unit tests only.
     91      */
     92     void verify(SensorStats stats) {
     93         if (mCount < 1) {
     94             stats.addValue(PASSED_KEY, true);
     95             return;
     96         }
     97 
     98         float mean = mSum / mCount;
     99         boolean failed = Math.abs(mean - mExpected) > mThreshold;
    100 
    101         stats.addValue(PASSED_KEY, !failed);
    102         stats.addValue(SensorStats.MAGNITUDE_KEY, mean);
    103 
    104         if (failed) {
    105             Assert.fail(String.format("Magnitude mean out of range: mean=%s (expected %s+/-%s)",
    106                     mean, mExpected, mThreshold));
    107         }
    108     }
    109 
    110     /**
    111      * {@inheritDoc}
    112      */
    113     @Override
    114     public MagnitudeVerification clone() {
    115         return new MagnitudeVerification(mExpected, mThreshold);
    116     }
    117 
    118     /**
    119      * {@inheritDoc}
    120      */
    121     @Override
    122     protected void addSensorEventInternal(TestSensorEvent event) {
    123         mSum += SensorCtsHelper.getMagnitude(event.values);
    124         mCount++;
    125     }
    126 
    127     @SuppressWarnings("deprecation")
    128     private static void setDefaults() {
    129         DEFAULTS.put(Sensor.TYPE_ACCELEROMETER, new Float[]{SensorManager.STANDARD_GRAVITY, 1.5f});
    130         DEFAULTS.put(Sensor.TYPE_GYROSCOPE, new Float[]{0.0f, 1.5f});
    131         // Sensors that we don't want to test at this time but still want to record the values.
    132         DEFAULTS.put(Sensor.TYPE_GRAVITY,
    133                 new Float[]{SensorManager.STANDARD_GRAVITY, Float.MAX_VALUE});
    134     }
    135 }
    136