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.extendedapitest; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import static org.junit.Assert.assertThrows; 23 24 import android.car.Car; 25 import android.car.CarVersion; 26 import android.car.ICar; 27 import android.car.PlatformVersion; 28 import android.car.extendedapitest.testbase.CarApiTestBase; 29 import android.car.extendedapitest.testbase.CarLessApiTestBase; 30 import android.car.hardware.CarSensorManager; 31 import android.car.test.CarTestManager; 32 import android.content.ComponentName; 33 import android.content.ServiceConnection; 34 import android.os.Binder; 35 import android.os.Build; 36 import android.os.IBinder; 37 38 import androidx.test.filters.LargeTest; 39 import androidx.test.filters.SmallTest; 40 41 import org.junit.Test; 42 43 import java.util.concurrent.Semaphore; 44 import java.util.concurrent.TimeUnit; 45 46 // NOTE: not really "CarLess", but it's handling the Car connection itself 47 @SmallTest 48 public final class CarTest extends CarLessApiTestBase { 49 private static final long DEFAULT_WAIT_TIMEOUT_MS = 3000; 50 private static final String CODENAME_REL = "REL"; 51 52 private final Semaphore mConnectionWait = new Semaphore(0); 53 54 private ICar mICar; 55 56 private final ServiceConnection mConnectionListener = new ServiceConnection() { 57 58 @Override 59 public void onServiceDisconnected(ComponentName name) { 60 CarApiTestBase.assertMainThread(); 61 } 62 63 @Override 64 public void onServiceConnected(ComponentName name, IBinder service) { 65 CarApiTestBase.assertMainThread(); 66 mICar = ICar.Stub.asInterface(service); 67 mConnectionWait.release(); 68 } 69 }; 70 waitForConnection(long timeoutMs)71 private void waitForConnection(long timeoutMs) throws InterruptedException { 72 mConnectionWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS); 73 } 74 75 @Test testCarConnection()76 public void testCarConnection() throws Exception { 77 Car car = Car.createCar(mContext, mConnectionListener); 78 assertThat(car.isConnected()).isFalse(); 79 assertThat(car.isConnecting()).isFalse(); 80 car.connect(); 81 // TODO fix race here 82 // assertTrue(car.isConnecting()); // This makes test flaky. 83 waitForConnection(DEFAULT_WAIT_TIMEOUT_MS); 84 assertThat(car.isConnected()).isTrue(); 85 assertThat(car.isConnecting()).isFalse(); 86 CarSensorManager carSensorManager = 87 (CarSensorManager) car.getCarManager(Car.SENSOR_SERVICE); 88 assertThat(carSensorManager).isNotNull(); 89 CarSensorManager carSensorManager2 = 90 (CarSensorManager) car.getCarManager(Car.SENSOR_SERVICE); 91 assertThat(carSensorManager2).isSameInstanceAs(carSensorManager); 92 Object noSuchService = car.getCarManager("No such service"); 93 assertThat(noSuchService).isNull(); 94 // double disconnect should be safe. 95 car.disconnect(); 96 car.disconnect(); 97 assertThat(car.isConnected()).isFalse(); 98 assertThat(car.isConnecting()).isFalse(); 99 } 100 101 @Test testDoubleConnect()102 public void testDoubleConnect() throws Exception { 103 Car car = Car.createCar(mContext, mConnectionListener); 104 assertThat(car.isConnected()).isFalse(); 105 assertThat(car.isConnecting()).isFalse(); 106 car.connect(); 107 assertThrows(IllegalStateException.class, () -> car.connect()); 108 car.disconnect(); 109 } 110 111 @Test testConstructorWithICar()112 public void testConstructorWithICar() throws Exception { 113 Car car = Car.createCar(mContext, mConnectionListener); 114 car.connect(); 115 waitForConnection(DEFAULT_WAIT_TIMEOUT_MS); 116 assertThat(mICar).isNotNull(); 117 Car car2 = new Car(mContext, mICar, null); 118 assertThat(car2.isConnected()).isTrue(); 119 } 120 121 @Test 122 @SuppressWarnings("deprecation") testApiVersion_deprecated()123 public void testApiVersion_deprecated() throws Exception { 124 int ApiVersionTooHigh = 1000000; 125 int MinorApiVersionTooHigh = 1000000; 126 int apiVersionMajorInt = Car.getCarVersion().getMajorVersion(); 127 int apiVersionMinorInt = Car.getCarVersion().getMinorVersion(); 128 expectThat(Car.isApiVersionAtLeast(apiVersionMajorInt)).isTrue(); 129 expectThat(Car.isApiVersionAtLeast(ApiVersionTooHigh)).isFalse(); 130 131 expectThat(Car.isApiVersionAtLeast(apiVersionMajorInt - 1, MinorApiVersionTooHigh)) 132 .isTrue(); 133 expectThat(Car.isApiVersionAtLeast(apiVersionMajorInt, apiVersionMinorInt)).isTrue(); 134 expectThat(Car.isApiVersionAtLeast(apiVersionMajorInt, MinorApiVersionTooHigh)).isFalse(); 135 expectThat(Car.isApiVersionAtLeast(ApiVersionTooHigh, 0)).isFalse(); 136 137 expectThat(Car.isApiAndPlatformVersionAtLeast(apiVersionMajorInt, Build.VERSION.SDK_INT)) 138 .isTrue(); 139 expectThat(Car.isApiAndPlatformVersionAtLeast(apiVersionMajorInt, 140 apiVersionMinorInt, Build.VERSION.SDK_INT)).isTrue(); 141 142 // SDK + 1 only works for released platform. 143 if (CODENAME_REL.equals(Build.VERSION.CODENAME)) { 144 expectThat(Car.isApiAndPlatformVersionAtLeast(apiVersionMajorInt, 145 Build.VERSION.SDK_INT + 1)).isFalse(); 146 expectThat(Car.isApiAndPlatformVersionAtLeast(apiVersionMajorInt, 147 apiVersionMinorInt, Build.VERSION.SDK_INT + 1)).isFalse(); 148 } 149 } 150 151 @Test testApiVersion_car()152 public void testApiVersion_car() throws Exception { 153 CarVersion carVersion = Car.getCarVersion(); 154 155 assertThat(carVersion).isNotNull(); 156 assertThat(carVersion.getMajorVersion()).isAtLeast(Build.VERSION.SDK_INT); 157 assertThat(carVersion.getMajorVersion()).isAtMost(Build.VERSION_CODES.CUR_DEVELOPMENT); 158 assertThat(carVersion.getMinorVersion()).isAtLeast(0); 159 } 160 161 @Test testApiVersion_platform()162 public void testApiVersion_platform() throws Exception { 163 PlatformVersion platformVersion = Car.getPlatformVersion(); 164 165 assertThat(platformVersion).isNotNull(); 166 assertThat(platformVersion.getMajorVersion()).isEqualTo( 167 CODENAME_REL.equals(Build.VERSION.CODENAME) ? Build.VERSION.SDK_INT 168 : Build.VERSION_CODES.CUR_DEVELOPMENT); 169 assertThat(platformVersion.getMinorVersion()).isAtLeast(0); 170 } 171 172 // This test need to wait for car service release and initialization. 173 @Test 174 @LargeTest testCarServiceReleaseReInit()175 public void testCarServiceReleaseReInit() throws Exception { 176 Car car = Car.createCar(mContext); 177 178 CarTestManager carTestManager = (CarTestManager) (car.getCarManager(Car.TEST_SERVICE)); 179 180 assertWithMessage("Could not get service %s", Car.TEST_SERVICE).that(carTestManager) 181 .isNotNull(); 182 183 Binder token = new Binder("testCarServiceReleaseReInit"); 184 // Releaseing car service and re-initialize must not crash car service. 185 carTestManager.stopCarService(token); 186 carTestManager.startCarService(token); 187 } 188 } 189