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.car.apitest; 18 19 import android.content.ComponentName; 20 import android.content.ServiceConnection; 21 import android.os.IBinder; 22 import android.os.Looper; 23 import android.car.Car; 24 import android.car.hardware.CarSensorEvent; 25 import android.car.hardware.CarSensorManager; 26 import android.test.AndroidTestCase; 27 import android.test.suitebuilder.annotation.MediumTest; 28 29 import java.util.concurrent.Semaphore; 30 import java.util.concurrent.TimeUnit; 31 32 @MediumTest 33 public class CarSensorManagerTest extends AndroidTestCase { 34 private static final long DEFAULT_WAIT_TIMEOUT_MS = 3000; 35 36 private final Semaphore mConnectionWait = new Semaphore(0); 37 38 private Car mCar; 39 private CarSensorManager mCarSensorManager; 40 41 private final ServiceConnection mConnectionListener = new ServiceConnection() { 42 43 @Override 44 public void onServiceDisconnected(ComponentName name) { 45 assertMainThread(); 46 } 47 48 @Override 49 public void onServiceConnected(ComponentName name, IBinder service) { 50 assertMainThread(); 51 mConnectionWait.release(); 52 } 53 }; 54 55 private void assertMainThread() { 56 assertTrue(Looper.getMainLooper().isCurrentThread()); 57 } 58 private void waitForConnection(long timeoutMs) throws InterruptedException { 59 mConnectionWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS); 60 } 61 62 @Override 63 protected void setUp() throws Exception { 64 super.setUp(); 65 mCar = Car.createCar(getContext(), mConnectionListener); 66 mCar.connect(); 67 waitForConnection(DEFAULT_WAIT_TIMEOUT_MS); 68 mCarSensorManager = 69 (CarSensorManager) mCar.getCarManager(Car.SENSOR_SERVICE); 70 } 71 72 @Override 73 protected void tearDown() throws Exception { 74 super.tearDown(); 75 mCar.disconnect(); 76 } 77 78 public void testDrivingPolicy() throws Exception { 79 int[] supportedSensors = mCarSensorManager.getSupportedSensors(); 80 assertNotNull(supportedSensors); 81 boolean found = false; 82 for (int sensor: supportedSensors) { 83 if (sensor == CarSensorManager.SENSOR_TYPE_DRIVING_STATUS) { 84 found = true; 85 break; 86 } 87 } 88 assertTrue(found); 89 assertTrue(mCarSensorManager.isSensorSupported( 90 CarSensorManager.SENSOR_TYPE_DRIVING_STATUS)); 91 assertTrue(CarSensorManager.isSensorSupported(supportedSensors, 92 CarSensorManager.SENSOR_TYPE_DRIVING_STATUS)); 93 CarSensorEvent lastEvent = mCarSensorManager.getLatestSensorEvent( 94 CarSensorManager.SENSOR_TYPE_DRIVING_STATUS); 95 assertNotNull(lastEvent); 96 } 97 } 98