1 /* 2 * Copyright (C) 2017 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 package com.android.devicehealthchecks; 17 18 import android.content.Context; 19 import android.content.pm.PackageManager; 20 import android.hardware.Sensor; 21 import android.hardware.SensorManager; 22 import android.platform.test.annotations.GlobalPresubmit; 23 24 import androidx.test.InstrumentationRegistry; 25 26 import org.junit.Assert; 27 import org.junit.Before; 28 import org.junit.Test; 29 30 /* 31 * Tests used for basic sensors validation after the device boot is completed. 32 * This test is used for global presubmit. 33 */ 34 35 @GlobalPresubmit 36 public class SensorsBootCheck { 37 private Context mContext; 38 private PackageManager mPackageManager; 39 private SensorManager mSensorManager; 40 41 @Before setUp()42 public void setUp() throws Exception { 43 mContext = InstrumentationRegistry.getInstrumentation().getContext(); 44 } 45 46 /* 47 * Test if sensors are available on the device as advertised. 48 */ 49 @Test checkSensors()50 public void checkSensors() { 51 /* 52 * This is a simple test that checks if sensors that are expected on the device are actually 53 * available. This is a generic test and relies on the fact that a device must declare some 54 * of the sensors it supports as features. We do not check the actual function of the 55 * sensors in this method. 56 */ 57 58 int numErrors = 0; 59 60 mPackageManager = mContext.getPackageManager(); 61 Assert.assertNotNull("Package Manager not found", mPackageManager); 62 63 mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); 64 Assert.assertNotNull("Sensor Manager not found", mSensorManager); 65 66 StringBuilder errorDetails = new StringBuilder("Error details: \n"); 67 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_ACCELEROMETER, 68 Sensor.TYPE_ACCELEROMETER, errorDetails) ? 1 : 0; 69 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_AMBIENT_TEMPERATURE, 70 Sensor.TYPE_AMBIENT_TEMPERATURE, errorDetails) ? 1 : 0; 71 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_BAROMETER, 72 Sensor.TYPE_PRESSURE, errorDetails) ? 1 : 0; 73 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_COMPASS, 74 Sensor.TYPE_MAGNETIC_FIELD, errorDetails) ? 1 : 0; 75 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_GYROSCOPE, 76 Sensor.TYPE_GYROSCOPE, errorDetails) ? 1 : 0; 77 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_LIGHT, 78 Sensor.TYPE_LIGHT, errorDetails) ? 1 : 0; 79 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_PROXIMITY, 80 Sensor.TYPE_PROXIMITY, errorDetails) ? 1 : 0; 81 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_RELATIVE_HUMIDITY, 82 Sensor.TYPE_RELATIVE_HUMIDITY, errorDetails) ? 1 : 0; 83 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_STEP_COUNTER, 84 Sensor.TYPE_STEP_COUNTER, errorDetails) ? 1 : 0; 85 numErrors += isSensorMissing(PackageManager.FEATURE_SENSOR_STEP_DETECTOR, 86 Sensor.TYPE_STEP_DETECTOR, errorDetails) ? 1 : 0; 87 88 // TODO: test heart rate and other related sensor types. 89 90 Assert.assertEquals(errorDetails.toString(), 0, numErrors); 91 } 92 isSensorMissing(String featureString, int sensorType, StringBuilder errString)93 private boolean isSensorMissing(String featureString, int sensorType, StringBuilder errString) { 94 if (!mPackageManager.hasSystemFeature(featureString)) { 95 // no claim to support the sensor, do not check farther 96 return false; 97 } 98 99 if (mSensorManager.getDefaultSensor(sensorType) == null) { 100 errString.append("Cannot find sensor type " + sensorType + " even though " + 101 featureString + " is defined\n"); 102 return true; 103 } 104 return false; 105 } 106 } 107