diff options
author | Pavel Maltsev <pavelm@google.com> | 2019-01-18 15:28:50 -0800 |
---|---|---|
committer | Pavel Maltsev <pavelm@google.com> | 2019-02-26 13:29:54 -0800 |
commit | 15d5ba358ff79b7a0aad5bdf5c2671caae6e6d17 (patch) | |
tree | f9157ef8a0b9d2d89f3494766d099a22b914c840 /tests/CarLibTests | |
parent | 589b703b9fcfa86a2c9e161b81eab09a43a12b3e (diff) | |
download | Car-15d5ba358ff79b7a0aad5bdf5c2671caae6e6d17.tar.gz |
Initial implementation of Car-Lib test API
Effectively this library will create a fake version of Car Service which
can be used in robolectric tests and provides additional API to allow
developers to simulate Car behavior as they need
Bug: 80507771
Test: atest CarLibTests
Change-Id: I240c2ea1214e949f78fc18dac32956b92f04a844
Diffstat (limited to 'tests/CarLibTests')
-rw-r--r-- | tests/CarLibTests/Android.mk | 44 | ||||
-rw-r--r-- | tests/CarLibTests/AndroidManifest.xml | 28 | ||||
-rw-r--r-- | tests/CarLibTests/src/android/car/CarProjectionManagerTest.java | 134 | ||||
-rw-r--r-- | tests/CarLibTests/src/android/car/CarPropertyManagerTest.java | 90 |
4 files changed, 296 insertions, 0 deletions
diff --git a/tests/CarLibTests/Android.mk b/tests/CarLibTests/Android.mk new file mode 100644 index 0000000000..bf86890a40 --- /dev/null +++ b/tests/CarLibTests/Android.mk @@ -0,0 +1,44 @@ +# Copyright (C) 2019 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# + +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := CarLibTests + +LOCAL_MODULE_TAGS := tests +LOCAL_PRIVATE_PLATFORM_APIS := true + +LOCAL_PROGUARD_ENABLED := disabled + +LOCAL_JAVA_LIBRARIES := \ + android.car \ + android.test.runner \ + android.test.base \ + +LOCAL_STATIC_JAVA_LIBRARIES := \ + junit \ + android.car.testapi \ + androidx.test.rules \ + androidx.test.core \ + mockito-target-minus-junit4 \ + com.android.car.test.utils \ + truth-prebuilt \ + +include $(BUILD_PACKAGE) diff --git a/tests/CarLibTests/AndroidManifest.xml b/tests/CarLibTests/AndroidManifest.xml new file mode 100644 index 0000000000..03379c7ef6 --- /dev/null +++ b/tests/CarLibTests/AndroidManifest.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2019 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="android.car.testapi.tests"> + + <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner" + android:targetPackage="android.car.testapi.tests" + android:label="Unit Tests for Car APIs"/> + + <application> + <uses-library android:name="android.test.runner" /> + </application> + +</manifest> diff --git a/tests/CarLibTests/src/android/car/CarProjectionManagerTest.java b/tests/CarLibTests/src/android/car/CarProjectionManagerTest.java new file mode 100644 index 0000000000..012bc9668a --- /dev/null +++ b/tests/CarLibTests/src/android/car/CarProjectionManagerTest.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.car; + +import static android.car.CarProjectionManager.ProjectionAccessPointCallback.ERROR_GENERIC; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; + +import android.car.CarProjectionManager.ProjectionAccessPointCallback; +import android.car.testapi.CarProjectionController; +import android.car.testapi.FakeCar; +import android.content.Context; +import android.content.Intent; +import android.net.wifi.WifiConfiguration; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +@RunWith(AndroidJUnit4.class) +public class CarProjectionManagerTest { + @Rule + public MockitoRule rule = MockitoJUnit.rule(); + + @Captor + private ArgumentCaptor<Intent> mIntentArgumentCaptor; + + @Spy + private final Context mContext = ApplicationProvider.getApplicationContext(); + + private static final int DEFAULT_TIMEOUT_MS = 1000; + + private CarProjectionManager mProjectionManager; + private CarProjectionController mController; + private ApCallback mApCallback; + + @Before + public void setUp() { + FakeCar fakeCar = FakeCar.createFakeCar(mContext); + mController = fakeCar.getCarProjectionController(); + mProjectionManager = + (CarProjectionManager) fakeCar.getCar().getCarManager(Car.PROJECTION_SERVICE); + assertThat(mProjectionManager).isNotNull(); + + mApCallback = new ApCallback(); + } + + @Test + public void startAp_fail() throws InterruptedException { + mController.setWifiConfiguration(null); + + mProjectionManager.startProjectionAccessPoint(mApCallback); + mApCallback.mFailed.await(DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS); + assertThat(mApCallback.mFailureReason).isEqualTo(ERROR_GENERIC); + } + + @Test + public void startAp_success() throws InterruptedException { + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = "Hello"; + wifiConfiguration.BSSID = "AA:BB:CC:CC:DD:EE"; + wifiConfiguration.preSharedKey = "password"; + + mController.setWifiConfiguration(wifiConfiguration); + + mProjectionManager.startProjectionAccessPoint(mApCallback); + mApCallback.mStarted.await(DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS); + assertThat(mApCallback.mWifiConfiguration).isEqualTo(wifiConfiguration); + } + + @Test + public void registerProjectionRunner() throws CarNotConnectedException { + Intent intent = new Intent("my_action"); + intent.setPackage("my.package"); + mProjectionManager.registerProjectionRunner(intent); + + verify(mContext).bindService(mIntentArgumentCaptor.capture(), any(), + eq(Context.BIND_AUTO_CREATE)); + assertThat(mIntentArgumentCaptor.getValue()).isEqualTo(intent); + } + + private static class ApCallback extends ProjectionAccessPointCallback { + CountDownLatch mStarted = new CountDownLatch(1); + CountDownLatch mFailed = new CountDownLatch(1); + int mFailureReason = -1; + WifiConfiguration mWifiConfiguration; + + @Override + public void onStarted(WifiConfiguration wifiConfiguration) { + mWifiConfiguration = wifiConfiguration; + mStarted.countDown(); + } + + @Override + public void onStopped() { + } + + @Override + public void onFailed(int reason) { + mFailureReason = reason; + mFailed.countDown(); + } + } +} diff --git a/tests/CarLibTests/src/android/car/CarPropertyManagerTest.java b/tests/CarLibTests/src/android/car/CarPropertyManagerTest.java new file mode 100644 index 0000000000..bc322c3ce9 --- /dev/null +++ b/tests/CarLibTests/src/android/car/CarPropertyManagerTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.car; + +import static android.car.VehiclePropertyIds.HVAC_FAN_SPEED; +import static android.car.VehiclePropertyIds.HVAC_TEMPERATURE_CURRENT; + +import static com.google.common.truth.Truth.assertThat; + +import android.car.hardware.property.CarPropertyManager; +import android.car.testapi.CarPropertyController; +import android.car.testapi.FakeCar; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +@RunWith(AndroidJUnit4.class) +public class CarPropertyManagerTest { + private static final int FAN_SPEED_VALUE = 42; + private static final float TEMPERATURE_VALUE = 42.24f; + + @Rule + public MockitoRule rule = MockitoJUnit.rule(); + + private CarPropertyController mController; + private CarPropertyManager mManager; + + @Before + public void setUp() { + FakeCar fakeCar = FakeCar.createFakeCar(ApplicationProvider.getApplicationContext()); + mController = fakeCar.getCarPropertyController(); + Car car = fakeCar.getCar(); + mManager = (CarPropertyManager) car.getCarManager(Car.PROPERTY_SERVICE); + assertThat(mManager).isNotNull(); + } + + @Test + public void carPropertyManager_int() { + mController.addProperty(HVAC_FAN_SPEED, FAN_SPEED_VALUE); + assertThat(mManager.getIntProperty(HVAC_FAN_SPEED, 0)).isEqualTo(FAN_SPEED_VALUE); + } + + @Test + public void carPropertyManager_intThrows() { + mController.addProperty(HVAC_FAN_SPEED, FAN_SPEED_VALUE); + try { + mManager.getFloatProperty(HVAC_FAN_SPEED, 0); + } catch (IllegalArgumentException expected) { + // Expected, the property is an integer value. + } + } + + @Test + public void carPropertyManager_float() { + mController.addProperty(HVAC_TEMPERATURE_CURRENT, TEMPERATURE_VALUE); + assertThat(mManager.getFloatProperty(HVAC_TEMPERATURE_CURRENT, 0)) + .isEqualTo(TEMPERATURE_VALUE); + } + + @Test + public void carPropertyManager_floatThrows() { + mController.addProperty(HVAC_TEMPERATURE_CURRENT, TEMPERATURE_VALUE); + try { + mManager.getIntProperty(HVAC_TEMPERATURE_CURRENT, 0); + } catch (IllegalArgumentException expected) { + // Expected, the property is an integer value. + } + } +} |