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 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 */ 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 */ 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 boolean result = mSensorManager.registerListener( 81 mTestSensorEventListener, 82 mEnvironment.getSensor(), 83 mEnvironment.getRequestedSamplingPeriodUs(), 84 mEnvironment.getMaxReportLatencyUs(), 85 mTestSensorEventListener.getHandler()); 86 Assert.assertTrue(message, result); 87 } 88 89 /** 90 * Register the listener. This method will perform a no-op if the sensor is already registered. 91 * 92 * @return A CountDownLatch initialized with eventCount which is used to wait for sensor 93 * events. 94 * @throws AssertionError if there was an error registering the listener with the 95 * {@link SensorManager} 96 */ 97 public CountDownLatch registerListener(TestSensorEventListener listener, int eventCount) { 98 if (mTestSensorEventListener != null) { 99 Log.w(LOG_TAG, "Listener already registered, returning."); 100 return null; 101 } 102 103 CountDownLatch latch = listener.getLatchForSensorEvents(eventCount); 104 mTestSensorEventListener = listener; 105 String message = SensorCtsHelper.formatAssertionMessage("registerListener", mEnvironment); 106 boolean result = mSensorManager.registerListener( 107 mTestSensorEventListener, 108 mEnvironment.getSensor(), 109 mEnvironment.getRequestedSamplingPeriodUs(), 110 mEnvironment.getMaxReportLatencyUs(), 111 mTestSensorEventListener.getHandler()); 112 Assert.assertTrue(message, result); 113 return latch; 114 } 115 116 /** 117 * Unregister the listener. This method will perform a no-op if the sensor is not registered. 118 */ 119 public void unregisterListener() { 120 if (mTestSensorEventListener == null) { 121 Log.w(LOG_TAG, "No listener registered, returning."); 122 return; 123 } 124 mSensorManager.unregisterListener(mTestSensorEventListener, mEnvironment.getSensor()); 125 mTestSensorEventListener.assertEventsReceivedInHandler(); 126 mTestSensorEventListener = null; 127 } 128 129 /** 130 * Call {@link SensorManager#flush(SensorEventListener)}. This method will perform a no-op if 131 * the sensor is not registered. 132 * 133 * @return A CountDownLatch which can be used to wait for a flush complete event. 134 * @throws AssertionError if {@link SensorManager#flush(SensorEventListener)} fails. 135 */ 136 public CountDownLatch requestFlush() { 137 if (mTestSensorEventListener == null) { 138 Log.w(LOG_TAG, "No listener registered, returning."); 139 return null; 140 } 141 CountDownLatch latch = mTestSensorEventListener.getLatchForFlushCompleteEvent(); 142 Assert.assertTrue( 143 SensorCtsHelper.formatAssertionMessage("Flush", mEnvironment), 144 mSensorManager.flush(mTestSensorEventListener)); 145 return latch; 146 } 147 } 148