aboutsummaryrefslogtreecommitdiff
path: root/tests/vehiclehal_test
diff options
context:
space:
mode:
authorEnrico Granata <egranata@google.com>2017-01-13 17:56:50 -0800
committerEnrico Granata <egranata@google.com>2017-01-18 15:53:28 -0800
commite190f57db08117902854e3934ac7956b3aaa3982 (patch)
tree06fc30bf2c8b4db29574d003840b5ec91ba5fab5 /tests/vehiclehal_test
parent5e9202ec768a6fb83f3e145aeba9d183162a0d82 (diff)
downloadCar-e190f57db08117902854e3934ac7956b3aaa3982.tar.gz
Write a Java test case for Vehicle HAL functionality
For now, this only contains basic tests of the OBD2 support Test: runtest -x packages/services/Car/tests/vehiclehal_test/ -j32 from the root of one's Android checkout Change-Id: I22fb27dbf418379a7b485570841841bfdf6b247d
Diffstat (limited to 'tests/vehiclehal_test')
-rw-r--r--tests/vehiclehal_test/Android.mk41
-rw-r--r--tests/vehiclehal_test/AndroidManifest.xml28
-rw-r--r--tests/vehiclehal_test/src/com/android/car/vehiclehal/test/Obd2LiveFrameTest.java105
-rw-r--r--tests/vehiclehal_test/src/com/android/car/vehiclehal/test/Utils.java116
4 files changed, 290 insertions, 0 deletions
diff --git a/tests/vehiclehal_test/Android.mk b/tests/vehiclehal_test/Android.mk
new file mode 100644
index 0000000000..53b7fd7ee6
--- /dev/null
+++ b/tests/vehiclehal_test/Android.mk
@@ -0,0 +1,41 @@
+# Copyright (C) 2017 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 := VehicleHALTest
+
+# for system|priviledged permission.
+LOCAL_CERTIFICATE := platform
+
+LOCAL_MODULE_TAGS := tests
+
+# When built explicitly put it in the data partition
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_PROGUARD_ENABLED := disabled
+
+LOCAL_STATIC_JAVA_LIBRARIES += vehicle-hal-support-lib \
+ android-support-test \
+ android.hardware.vehicle@2.0-java-static
+
+LOCAL_JAVA_LIBRARIES := android.car android.test.runner
+
+include $(BUILD_PACKAGE)
diff --git a/tests/vehiclehal_test/AndroidManifest.xml b/tests/vehiclehal_test/AndroidManifest.xml
new file mode 100644
index 0000000000..765d09993c
--- /dev/null
+++ b/tests/vehiclehal_test/AndroidManifest.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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="com.android.car.vehiclehal.test"
+ android:sharedUserId="android.uid.system" >
+
+ <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+ android:targetPackage="com.android.car.vehiclehal.test"
+ android:label="Tests for Vehicle HAL APIs"/>
+
+ <application android:label="VehicleHALTest">
+ <uses-library android:name="android.test.runner" />
+ </application>
+</manifest>
diff --git a/tests/vehiclehal_test/src/com/android/car/vehiclehal/test/Obd2LiveFrameTest.java b/tests/vehiclehal_test/src/com/android/car/vehiclehal/test/Obd2LiveFrameTest.java
new file mode 100644
index 0000000000..7770cf89bf
--- /dev/null
+++ b/tests/vehiclehal_test/src/com/android/car/vehiclehal/test/Obd2LiveFrameTest.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2017 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 com.android.car.vehiclehal.test;
+
+import static com.android.car.vehiclehal.test.Utils.dumpVehiclePropValue;
+import static com.android.car.vehiclehal.test.Utils.isVhalPropertyAvailable;
+import static com.android.car.vehiclehal.test.Utils.readVhalProperty;
+import static com.android.car.vehiclehal.test.Utils.tryWithDeadline;
+import static org.junit.Assert.*;
+
+import android.annotation.Nullable;
+import android.hardware.vehicle.V2_0.IVehicle;
+import android.hardware.vehicle.V2_0.StatusCode;
+import android.hardware.vehicle.V2_0.VehiclePropValue;
+import android.hardware.vehicle.V2_0.VehicleProperty;
+import android.os.RemoteException;
+import android.util.Log;
+import java.util.Objects;
+import org.junit.Before;
+import org.junit.Test;
+
+/** Test retrieving the OBD2_LIVE_FRAME property from VHAL */
+public class Obd2LiveFrameTest {
+ private static final String TAG = Obd2LiveFrameTest.class.getSimpleName();
+ private static final String VEHICLE_SERVICE_NAME = "Vehicle";
+ private static final long WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS = 10_000;
+
+ private IVehicle mVehicle = null;
+
+ @Before
+ public void setUp() throws Exception {
+ mVehicle = Objects.requireNonNull(getVehicle(WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS));
+ }
+
+ @Nullable
+ private IVehicle getVehicle(long waitMilliseconds) {
+ return tryWithDeadline(
+ waitMilliseconds,
+ () -> {
+ try {
+ return IVehicle.getService(VEHICLE_SERVICE_NAME);
+ } catch (RemoteException e) {
+ Log.w(TAG, "attempt to get IVehicle service " + VEHICLE_SERVICE_NAME
+ + " caused RemoteException: ", e);
+ return null;
+ }
+ });
+ }
+
+ @Test
+ public void testLiveFrame() throws RemoteException {
+ if (!isLiveFrameAvailable()) {
+ Log.i(TAG, "live frame not available; returning - our job here is done");
+ return;
+ }
+ readVhalProperty(
+ mVehicle,
+ VehicleProperty.OBD2_LIVE_FRAME,
+ (Integer status, VehiclePropValue value) -> {
+ assertEquals(StatusCode.OK, status.intValue());
+ assertNotNull("OBD2_LIVE_FRAME is supported; should not be null", value);
+ Log.i(TAG, "dump of OBD2_LIVE_FRAME:\n" + dumpVehiclePropValue(value));
+ return true;
+ });
+ }
+
+ @Test
+ public void testFreezeFrame() throws RemoteException {
+ if (!isFreezeFrameAvailable()) {
+ Log.i(TAG, "freeze frame not available; returning - our job here is done");
+ return;
+ }
+ readVhalProperty(
+ mVehicle,
+ VehicleProperty.OBD2_FREEZE_FRAME,
+ (Integer status, VehiclePropValue value) -> {
+ assertEquals(StatusCode.OK, status.intValue());
+ assertNotNull("OBD2_FREEZE_FRAME is supported; should not be null", value);
+ Log.i(TAG, "dump of OBD2_FREEZE_FRAME:\n" + dumpVehiclePropValue(value));
+ return true;
+ });
+ }
+
+ private boolean isLiveFrameAvailable() throws RemoteException {
+ return isVhalPropertyAvailable(mVehicle, VehicleProperty.OBD2_LIVE_FRAME);
+ }
+
+ private boolean isFreezeFrameAvailable() throws RemoteException {
+ return isVhalPropertyAvailable(mVehicle, VehicleProperty.OBD2_FREEZE_FRAME);
+ }
+}
diff --git a/tests/vehiclehal_test/src/com/android/car/vehiclehal/test/Utils.java b/tests/vehiclehal_test/src/com/android/car/vehiclehal/test/Utils.java
new file mode 100644
index 0000000000..3e79ce8472
--- /dev/null
+++ b/tests/vehiclehal_test/src/com/android/car/vehiclehal/test/Utils.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2017 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 com.android.car.vehiclehal.test;
+
+import static android.os.SystemClock.elapsedRealtime;
+
+import android.annotation.Nullable;
+import android.hardware.vehicle.V2_0.IVehicle;
+import android.hardware.vehicle.V2_0.VehiclePropConfig;
+import android.hardware.vehicle.V2_0.VehiclePropValue;
+import android.os.RemoteException;
+import android.util.Log;
+import com.android.car.vehiclehal.VehiclePropValueBuilder;
+import java.util.Objects;
+
+final class Utils {
+ private Utils() {}
+
+ private static final String TAG = Utils.class.getSimpleName();
+
+ @Nullable
+ static <T> T tryWithDeadline(long waitMilliseconds, java.util.function.Supplier<T> f) {
+ f = Objects.requireNonNull(f);
+ T object = f.get();
+ long start = elapsedRealtime();
+ while (object == null && (start + waitMilliseconds) > elapsedRealtime()) {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Sleep was interrupted", e);
+ }
+
+ object = f.get();
+ }
+ return object;
+ }
+
+ static String dumpVehiclePropValue(VehiclePropValue vpv) {
+ vpv = Objects.requireNonNull(vpv);
+ return "prop = "
+ + vpv.prop
+ + '\n'
+ + "areaId = "
+ + vpv.areaId
+ + '\n'
+ + "timestamp = "
+ + vpv.timestamp
+ + '\n'
+ + "int32Values = "
+ + vpv.value.int32Values
+ + '\n'
+ + "floatValues = "
+ + vpv.value.floatValues
+ + '\n'
+ + "int64Values ="
+ + vpv.value.int64Values
+ + '\n'
+ + "bytes = "
+ + vpv.value.bytes
+ + '\n'
+ + "string = "
+ + vpv.value.stringValue
+ + '\n';
+ }
+
+ static boolean isVhalPropertyAvailable(IVehicle vehicle, int prop) throws RemoteException {
+ return vehicle.getAllPropConfigs()
+ .stream()
+ .anyMatch((VehiclePropConfig config) -> config.prop == prop);
+ }
+
+ static VehiclePropValue readVhalProperty(
+ IVehicle vehicle,
+ int propertyId,
+ java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) {
+ return readVhalProperty(vehicle, propertyId, 0, f);
+ }
+
+ static VehiclePropValue readVhalProperty(
+ IVehicle vehicle,
+ int propertyId,
+ int areaId,
+ java.util.function.BiFunction<Integer, VehiclePropValue, Boolean> f) {
+ vehicle = Objects.requireNonNull(vehicle);
+ VehiclePropValue request =
+ VehiclePropValueBuilder.newBuilder(propertyId).setAreaId(areaId).build();
+ VehiclePropValue vpv[] = new VehiclePropValue[] {null};
+ try {
+ vehicle.get(
+ request,
+ (int status, VehiclePropValue propValue) -> {
+ if (f.apply(status, propValue)) {
+ vpv[0] = propValue;
+ }
+ });
+ } catch (RemoteException e) {
+ Log.w(TAG, "attempt to read VHAL property 0x" + Integer.toHexString(propertyId)
+ + " from area " + areaId + " caused RemoteException: ", e);
+ }
+ return vpv[0];
+ }
+}