• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.content.Context;
20 import android.content.pm.PackageManager;
21 import android.content.res.Configuration;
22 import android.hardware.Sensor;
23 import android.hardware.SensorDirectChannel;
24 import android.hardware.SensorManager;
25 import android.os.Build;
26 
27 import com.android.compatibility.common.util.CddTest;
28 import com.android.compatibility.common.util.PropertyUtil;
29 
30 /**
31  * Checks if required sensor types are present, for example sensors required
32  * when Hifi sensors or VR High performance mode features are enabled. Also
33  * checks that required composite sensor types are present if the underlying
34  * physical sensors are present.
35  *
36  * <p>To execute these test cases, the following command can be used:</p>
37  * <pre>
38  * adb shell am instrument -e class android.hardware.cts.SensorSupportTest \
39  *     -w android.hardware.cts/android.test.AndroidJUnitRunner
40  * </pre>
41  */
42 public class SensorSupportTest extends SensorTestCase {
43     private SensorManager mSensorManager;
44     private boolean mAreHifiSensorsSupported;
45     private boolean mVrHighPerformanceModeSupported;
46     private boolean mIsVrHeadset;
47     private boolean mHasAccel;
48     private boolean mHasGyro;
49     private boolean mHasMag;
50 
51     @Override
setUp()52     public void setUp() {
53         PackageManager pm = getContext().getPackageManager();
54         // Some tests will only run if either HIFI_SENSORS or VR high performance mode is supported.
55         mAreHifiSensorsSupported = pm.hasSystemFeature(PackageManager.FEATURE_HIFI_SENSORS);
56         mVrHighPerformanceModeSupported = pm.hasSystemFeature(PackageManager.FEATURE_VR_MODE_HIGH_PERFORMANCE);
57         mIsVrHeadset = (getContext().getResources().getConfiguration().uiMode
58             & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_VR_HEADSET;
59         mSensorManager =
60                 (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
61 
62         mHasAccel = hasSensorType(Sensor.TYPE_ACCELEROMETER);
63         mHasGyro = hasSensorType(Sensor.TYPE_GYROSCOPE);
64         mHasMag = hasSensorType(Sensor.TYPE_MAGNETIC_FIELD);
65     }
66 
67     @CddTest(requirement="7.3.9/C-2-1")
testSupportsAccelerometer()68     public void testSupportsAccelerometer() {
69         checkHifiVrSensorSupport(Sensor.TYPE_ACCELEROMETER);
70     }
71 
72     @CddTest(requirement="7.3.9/C-2-2")
testSupportsAccelerometerUncalibrated()73     public void testSupportsAccelerometerUncalibrated() {
74         // Uncalibrated accelerometer was not required before Android O
75         if (PropertyUtil.getFirstApiLevel() >= Build.VERSION_CODES.O) {
76             checkHifiVrSensorSupport(Sensor.TYPE_ACCELEROMETER_UNCALIBRATED);
77         }
78     }
79 
80     @CddTest(requirement="7.3.9/C-2-3")
testSupportsGyroscope()81     public void testSupportsGyroscope() {
82         checkHifiVrSensorSupport(Sensor.TYPE_GYROSCOPE);
83     }
84 
85     @CddTest(requirement="7.3.9/C-2-4")
testSupportsGyroscopeUncalibrated()86     public void testSupportsGyroscopeUncalibrated() {
87         checkHifiVrSensorSupport(Sensor.TYPE_GYROSCOPE_UNCALIBRATED);
88     }
89 
90     @CddTest(requirement="7.3.9/C-2-5")
testSupportsGeoMagneticField()91     public void testSupportsGeoMagneticField() {
92         checkHifiVrSensorSupport(Sensor.TYPE_MAGNETIC_FIELD);
93     }
94 
95     @CddTest(requirement="7.3.9/C-2-6")
testSupportsMagneticFieldUncalibrated()96     public void testSupportsMagneticFieldUncalibrated() {
97         checkHifiVrSensorSupport(Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED);
98     }
99 
100     @CddTest(requirement="7.3.9/C-2-7")
testSupportsPressure()101     public void testSupportsPressure() {
102         checkHifiVrSensorSupport(Sensor.TYPE_PRESSURE);
103     }
104 
105     @CddTest(requirement="7.3.9/C-2-8")
testSupportsGameRotationVector()106     public void testSupportsGameRotationVector() {
107         checkHifiVrSensorSupport(Sensor.TYPE_GAME_ROTATION_VECTOR);
108     }
109 
110     @CddTest(requirement="7.3.9/C-2-9")
testSupportsSignificantMotion()111     public void testSupportsSignificantMotion() {
112         checkHifiVrSensorSupport(Sensor.TYPE_SIGNIFICANT_MOTION);
113     }
114 
115     @CddTest(requirement="7.3.9/C-2-10")
testSupportsStepDetector()116     public void testSupportsStepDetector() {
117         checkHifiVrSensorSupport(Sensor.TYPE_STEP_DETECTOR);
118     }
119 
120     @CddTest(requirement="7.3.9/C-2-11")
testSupportsStepCounter()121     public void testSupportsStepCounter() {
122         checkHifiVrSensorSupport(Sensor.TYPE_STEP_COUNTER);
123     }
124 
125     @CddTest(requirement="7.3.9/C-2-12")
testSupportsTiltDetector()126     public void testSupportsTiltDetector() {
127         final int TYPE_TILT_DETECTOR = 22;
128         checkHifiVrSensorSupport(TYPE_TILT_DETECTOR);
129     }
130 
131     @CddTest(requirement="7.3.1/C-3-1")
testSupportsGravityAndLinearAccelIfHasAG()132     public void testSupportsGravityAndLinearAccelIfHasAG() {
133         if (mHasAccel && mHasGyro) {
134             assertTrue(hasSensorType(Sensor.TYPE_GRAVITY));
135             assertTrue(hasSensorType(Sensor.TYPE_LINEAR_ACCELERATION));
136         }
137     }
138 
139     @CddTest(requirement="7.3.1/C-4-1")
testSupportsRotationVectorIfHasAGM()140     public void testSupportsRotationVectorIfHasAGM() {
141         if (mHasAccel && mHasGyro && mHasMag) {
142             assertTrue(hasSensorType(Sensor.TYPE_ROTATION_VECTOR));
143         }
144     }
145 
sensorRequiredForVrHighPerformanceMode(int sensorType)146     private boolean sensorRequiredForVrHighPerformanceMode(int sensorType) {
147         if (sensorType == Sensor.TYPE_ACCELEROMETER ||
148             sensorType == Sensor.TYPE_ACCELEROMETER_UNCALIBRATED ||
149             sensorType == Sensor.TYPE_GYROSCOPE ||
150             sensorType == Sensor.TYPE_GYROSCOPE_UNCALIBRATED ||
151             sensorType == Sensor.TYPE_MAGNETIC_FIELD ||
152             sensorType == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED) {
153             return true;
154         } else {
155             return false;
156         }
157     }
158 
checkHifiVrSensorSupport(int sensorType)159     private void checkHifiVrSensorSupport(int sensorType) {
160         boolean isVrSensor = mVrHighPerformanceModeSupported &&
161             sensorRequiredForVrHighPerformanceMode(sensorType);
162         if (mAreHifiSensorsSupported || isVrSensor) {
163             Sensor sensor = mSensorManager.getDefaultSensor(sensorType);
164             assertTrue(sensor != null);
165             if (isVrSensor && mIsVrHeadset) {
166                 assertTrue(sensor.isDirectChannelTypeSupported(SensorDirectChannel.TYPE_HARDWARE_BUFFER));
167             }
168         }
169     }
170 
hasSensorType(int sensorType)171     private boolean hasSensorType(int sensorType) {
172         return (mSensorManager != null && mSensorManager.getDefaultSensor(sensorType) != null);
173     }
174 }
175