1 /* 2 * Copyright (C) 2013 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.hardware.Sensor; 23 import android.hardware.SensorManager; 24 import android.hardware.cts.helpers.TestSensorEnvironment; 25 import android.hardware.cts.helpers.sensoroperations.TestSensorOperation; 26 import android.hardware.cts.helpers.sensorverification.MeanVerification; 27 28 import java.util.concurrent.TimeUnit; 29 30 /** 31 * Semi-automated test that focuses on characteristics associated with Accelerometer measurements. 32 */ 33 public class AccelerometerMeasurementTestActivity extends SensorCtsVerifierTestActivity { 34 public AccelerometerMeasurementTestActivity() { 35 super(AccelerometerMeasurementTestActivity.class); 36 } 37 38 public String testFaceUp() throws Throwable { 39 return verifyMeasurements( 40 R.string.snsr_accel_test_face_up, 41 0, 0, SensorManager.STANDARD_GRAVITY); 42 } 43 44 public String testFaceDown() throws Throwable { 45 return delayedVerifyMeasurements( 46 R.string.snsr_accel_test_face_down, 47 0, 0, -SensorManager.STANDARD_GRAVITY); 48 } 49 50 public String testRightSide() throws Throwable { 51 return verifyMeasurements( 52 R.string.snsr_accel_test_right_side, 53 -SensorManager.STANDARD_GRAVITY, 0, 0); 54 } 55 56 public String testLeftSide() throws Throwable { 57 return verifyMeasurements( 58 R.string.snsr_accel_test_left_side, 59 SensorManager.STANDARD_GRAVITY, 0, 0); 60 } 61 62 public String testTopSide() throws Throwable { 63 return verifyMeasurements( 64 R.string.snsr_accel_test_top_side, 65 0, -SensorManager.STANDARD_GRAVITY, 0); 66 } 67 68 public String testBottomSide() throws Throwable { 69 return verifyMeasurements( 70 R.string.snsr_accel_test_bottom_side, 71 0, SensorManager.STANDARD_GRAVITY, 0); 72 } 73 74 /** 75 * This test verifies that the Accelerometer measurements are close to the expected reference 76 * values (range and scale). 77 * 78 * The test takes a set of samples from the sensor under test and calculates the mean of each 79 * axis that the sensor data collects. It then compares it against the test expectations that 80 * are represented by reference values and a threshold. 81 82 * The reference values are coupled to the orientation of the device. The test is susceptible to 83 * errors when the device is not oriented properly, or the units in which the data are reported 84 * and the expectations are set are different. 85 * 86 * The error message associated with the test provides the required data needed to identify any 87 * possible issue. It provides: 88 * - the thread id on which the failure occurred 89 * - the sensor type and sensor handle that caused the failure 90 * - the values representing the expectation of the test 91 * - the mean of values sampled from the sensor 92 */ 93 private String verifyMeasurements(float ... expectations) throws Throwable { 94 Thread.sleep(500 /*ms*/); 95 TestSensorEnvironment environment = new TestSensorEnvironment( 96 getApplicationContext(), 97 Sensor.TYPE_ACCELEROMETER, 98 SensorManager.SENSOR_DELAY_FASTEST); 99 TestSensorOperation verifyMeasurements = 100 TestSensorOperation.createOperation(environment, 100 /* event count */); 101 verifyMeasurements.addVerification(new MeanVerification( 102 expectations, 103 new float[]{1.95f, 1.95f, 1.95f} /* m / s^2 */, 104 new float[]{1.95f, 1.95f, 1.95f} /* m / s^2 */)); 105 verifyMeasurements.execute(getCurrentTestNode()); 106 return null; 107 } 108 109 private String delayedVerifyMeasurements(int descriptionResId, float ... expectations) 110 throws Throwable { 111 SensorTestLogger logger = getTestLogger(); 112 logger.logInstructions(descriptionResId); 113 logger.logWaitForSound(); 114 waitForUserToBegin(); 115 Thread.sleep(TimeUnit.MILLISECONDS.convert(7, TimeUnit.SECONDS)); 116 117 try { 118 return verifyMeasurements(expectations); 119 } finally { 120 playSound(); 121 } 122 } 123 124 private String verifyMeasurements(int descriptionResId, float ... expectations) 125 throws Throwable { 126 SensorTestLogger logger = getTestLogger(); 127 logger.logInstructions(descriptionResId); 128 logger.logInstructions(R.string.snsr_device_steady); 129 waitForUserToBegin(); 130 131 return verifyMeasurements(expectations); 132 } 133 } 134