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;
18 
19 import junit.framework.Assert;
20 
21 import android.content.Context;
22 import android.hardware.SensorEventListener;
23 import android.hardware.SensorManager;
24 import android.util.Log;
25 
26 import java.util.concurrent.CountDownLatch;
27 
28 /**
29  * A test class that performs the actions of {@link SensorManager} on a single sensor.
30  * This class allows for a single sensor to be registered and unregistered as well as performing
31  * operations such as flushing the sensor events and gathering events.
32  * This class also manages performing the test verifications for the sensor manager.
33  *
34  * NOTE: this class is expected to mirror {@link SensorManager} operations, and perform the
35  * required test verifications along with them.
36  */
37 public class TestSensorManager {
38     private static final String LOG_TAG = "TestSensorManager";
39 
40     private final SensorManager mSensorManager;
41     private final TestSensorEnvironment mEnvironment;
42 
43     private volatile TestSensorEventListener mTestSensorEventListener;
44 
45     /**
46      * @deprecated Use {@link #TestSensorManager(TestSensorEnvironment)} instead.
47      */
48     @Deprecated
TestSensorManager( Context context, int sensorType, int rateUs, int maxBatchReportLatencyUs)49     public TestSensorManager(
50             Context context,
51             int sensorType,
52             int rateUs,
53             int maxBatchReportLatencyUs) {
54         this(new TestSensorEnvironment(context, sensorType, rateUs, maxBatchReportLatencyUs));
55     }
56 
57     /**
58      * Construct a {@link TestSensorManager}.
59      */
TestSensorManager(TestSensorEnvironment environment)60     public TestSensorManager(TestSensorEnvironment environment) {
61         mSensorManager =
62                 (SensorManager) environment.getContext().getSystemService(Context.SENSOR_SERVICE);
63         mEnvironment = environment;
64     }
65 
66     /**
67      * Register the listener. This method will perform a no-op if the sensor is already registered.
68      *
69      * @throws AssertionError if there was an error registering the listener with the
70      * {@link SensorManager}
71      */
registerListener(TestSensorEventListener listener)72     public void registerListener(TestSensorEventListener listener) {
73         if (mTestSensorEventListener != null) {
74             Log.w(LOG_TAG, "Listener already registered, returning.");
75             return;
76         }
77 
78         mTestSensorEventListener = listener;
79         String message = SensorCtsHelper.formatAssertionMessage("registerListener", mEnvironment);
80 
81         boolean result = mSensorManager.registerListener(
82                 mTestSensorEventListener,
83                 mEnvironment.getSensor(),
84                 mEnvironment.getRequestedSamplingPeriodUs(),
85                 mEnvironment.getMaxReportLatencyUs(),
86                 mTestSensorEventListener.getHandler());
87         Assert.assertTrue(message, result);
88     }
89 
90     /**
91      * Register the listener. This method will perform a no-op if the sensor is already registered.
92      *
93      * @return A CountDownLatch initialized with eventCount which is used to wait for sensor
94      * events.
95      * @throws AssertionError if there was an error registering the listener with the
96      * {@link SensorManager}
97      */
registerListener( TestSensorEventListener listener, int eventCount, boolean specifyHandler)98     public CountDownLatch registerListener(
99             TestSensorEventListener listener,
100             int eventCount,
101             boolean specifyHandler) {
102         if (mTestSensorEventListener != null) {
103             Log.w(LOG_TAG, "Listener already registered, returning.");
104             return null;
105         }
106 
107         CountDownLatch latch = listener.getLatchForSensorEvents(eventCount);
108         mTestSensorEventListener = listener;
109         String message = SensorCtsHelper.formatAssertionMessage("registerListener", mEnvironment);
110 
111         boolean result;
112         if (specifyHandler) {
113             result = mSensorManager.registerListener(
114                     mTestSensorEventListener,
115                     mEnvironment.getSensor(),
116                     mEnvironment.getRequestedSamplingPeriodUs(),
117                     mEnvironment.getMaxReportLatencyUs(),
118                     mTestSensorEventListener.getHandler());
119         } else {
120             result = mSensorManager.registerListener(
121                     mTestSensorEventListener,
122                     mEnvironment.getSensor(),
123                     mEnvironment.getRequestedSamplingPeriodUs(),
124                     mEnvironment.getMaxReportLatencyUs());
125         }
126         Assert.assertTrue(message, result);
127         return latch;
128     }
129 
130     /**
131      * Register the listener. This method will perform a no-op if the sensor is already registered.
132      *
133      * @return A CountDownLatch initialized with eventCount which is used to wait for sensor
134      * events.
135      * @throws AssertionError if there was an error registering the listener with the
136      * {@link SensorManager}
137      */
registerListener( TestSensorEventListener listener, int eventCount)138     public CountDownLatch registerListener(
139             TestSensorEventListener listener,
140             int eventCount) {
141         return registerListener(listener, eventCount, true);
142     }
143 
144     /**
145      * Unregister the listener. This method will perform a no-op if the sensor is not registered.
146      */
unregisterListener()147     public void unregisterListener() {
148         if (mTestSensorEventListener == null) {
149             Log.w(LOG_TAG, "No listener registered, returning.");
150             return;
151         }
152         mSensorManager.unregisterListener(mTestSensorEventListener, mEnvironment.getSensor());
153         mTestSensorEventListener.assertEventsReceivedInHandler();
154         mTestSensorEventListener.releaseWakeLock(); // clean up wakelock if it is acquired
155         mTestSensorEventListener = null;
156     }
157 
158     /**
159      * Call {@link SensorManager#flush(SensorEventListener)}. This method will perform a no-op if
160      * the sensor is not registered.
161      *
162      * @return A CountDownLatch which can be used to wait for a flush complete event.
163      * @throws AssertionError if {@link SensorManager#flush(SensorEventListener)} fails.
164      */
requestFlush()165     public CountDownLatch requestFlush() {
166         if (mTestSensorEventListener == null) {
167             Log.w(LOG_TAG, "No listener registered, returning.");
168             return null;
169         }
170         CountDownLatch latch = mTestSensorEventListener.getLatchForFlushCompleteEvent();
171         Assert.assertTrue(
172                 SensorCtsHelper.formatAssertionMessage("Flush", mEnvironment),
173                 mSensorManager.flush(mTestSensorEventListener));
174         return latch;
175     }
176 }
177