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 android.hardware.cts;
18 
19 import android.hardware.Sensor;
20 import android.hardware.cts.helpers.SensorTestStateNotSupportedException;
21 import android.hardware.cts.helpers.TestSensorEnvironment;
22 import android.hardware.cts.helpers.reporting.ISensorTestNode;
23 import android.hardware.cts.helpers.sensoroperations.SensorOperation;
24 import android.test.AndroidTestCase;
25 import android.util.Log;
26 
27 /**
28  * Test Case class that handles gracefully sensors that are not available in the device.
29  */
30 public abstract class SensorTestCase extends AndroidTestCase {
31     // TODO: consolidate all log tags
32     protected static final String LOG_TAG = "TestRunner";
33 
34     /**
35      * Previously for L release, we had this flag to know if each sensor is running with multiple
36      * listeners each requesting different data rates. Now before running CTS tests all sensors
37      * are de-activated by putting SensorService in RESTRICTED mode. Only CTS tests can
38      * activate/deactivate sensors in this mode. So we can default this flag value to false.
39      */
40     private volatile boolean mEmulateSensorUnderLoad = false;
41 
42     /**
43      * By default the test class is the root of the test hierarchy.
44      */
45     private volatile ISensorTestNode mCurrentTestNode = new TestClassNode(getClass());
46 
SensorTestCase()47     protected SensorTestCase() {}
48 
49     @Override
runBare()50     public void runBare() throws Throwable {
51         try {
52             super.runBare();
53         } catch (SensorTestStateNotSupportedException e) {
54             // the sensor state is not supported in the device, log a warning and skip the test
55             Log.w(LOG_TAG, e.getMessage());
56         }
57     }
58 
setEmulateSensorUnderLoad(boolean value)59     public void setEmulateSensorUnderLoad(boolean value) {
60         mEmulateSensorUnderLoad = value;
61     }
62 
shouldEmulateSensorUnderLoad()63     protected boolean shouldEmulateSensorUnderLoad() {
64         return mEmulateSensorUnderLoad;
65     }
66 
setCurrentTestNode(ISensorTestNode value)67     public void setCurrentTestNode(ISensorTestNode value) {
68         mCurrentTestNode = value;
69     }
70 
getCurrentTestNode()71     protected ISensorTestNode getCurrentTestNode() {
72         return mCurrentTestNode;
73     }
74 
75     private class TestClassNode implements ISensorTestNode {
76         private final Class<?> mTestClass;
77 
TestClassNode(Class<?> testClass)78         public TestClassNode(Class<?> testClass) {
79             mTestClass = testClass;
80         }
81 
82         @Override
getName()83         public String getName() {
84             return mTestClass.getSimpleName();
85         }
86     }
87 }
88