1 /* 2 * Copyright (C) 2021 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 com.android.car.hal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyBoolean; 23 import static org.mockito.ArgumentMatchers.anyInt; 24 import static org.mockito.Mockito.doAnswer; 25 import static org.mockito.Mockito.reset; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.car.evs.CarEvsManager; 30 import android.hardware.automotive.vehicle.V2_0.EvsServiceRequestIndex; 31 import android.hardware.automotive.vehicle.V2_0.EvsServiceState; 32 import android.hardware.automotive.vehicle.V2_0.EvsServiceType; 33 import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig; 34 import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; 35 import android.hardware.automotive.vehicle.V2_0.VehicleProperty; 36 import android.util.Log; 37 38 import com.android.car.vehiclehal.test.VehiclePropConfigBuilder; 39 40 import com.google.common.collect.ImmutableList; 41 import com.google.common.collect.ImmutableSet; 42 43 import org.junit.After; 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.junit.MockitoJUnitRunner; 49 50 import java.util.ArrayList; 51 import java.util.Collection; 52 import java.util.List; 53 import java.util.Set; 54 55 @RunWith(MockitoJUnitRunner.class) 56 public class EvsHalServiceTest { 57 @Mock VehicleHal mVehicleHal; 58 @Mock EvsHalService.EvsHalEventListener mListener; 59 60 private static final VehiclePropConfig EVS_SERVICE_REQUEST = 61 VehiclePropConfigBuilder.newBuilder(VehicleProperty.EVS_SERVICE_REQUEST).build(); 62 63 private EvsHalService mEvsHalService; 64 65 private final int TRUE = 1; 66 private final int FALSE = 0; 67 68 @Before setUp()69 public void setUp() { 70 mEvsHalService = new EvsHalService(mVehicleHal); 71 mEvsHalService.init(); 72 } 73 74 @After tearDown()75 public void tearDown() { 76 mEvsHalService.release(); 77 mEvsHalService = null; 78 } 79 80 @Test takesEvsServiceRequestProperty()81 public void takesEvsServiceRequestProperty() { 82 Set<VehiclePropConfig> offeredProps = ImmutableSet.of( 83 EVS_SERVICE_REQUEST); 84 85 mEvsHalService.takeProperties(offeredProps); 86 87 assertThat(mEvsHalService.isEvsServiceRequestSupported()).isTrue(); 88 } 89 90 @Test requestToStartRearviewViaEvsServiceRequest()91 public void requestToStartRearviewViaEvsServiceRequest() { 92 subscribeListener(ImmutableSet.of(EVS_SERVICE_REQUEST)); 93 94 List<Integer> events = new ArrayList<>(); 95 doAnswer(invocation -> { 96 events.add(invocation.getArgument(0)); 97 events.add(invocation.getArgument(1) ? TRUE : FALSE); 98 return null; 99 }).when(mListener).onEvent(anyInt(), anyBoolean()); 100 101 dispatchEvsServiceRequest(EvsServiceType.REARVIEW, EvsServiceState.ON); 102 103 assertThat(events.get(0)).isEqualTo(CarEvsManager.SERVICE_TYPE_REARVIEW); 104 assertThat(events.get(1)).isEqualTo(TRUE); 105 } 106 107 @Test requestToStopRearviewViaEvsServiceRequest()108 public void requestToStopRearviewViaEvsServiceRequest() { 109 subscribeListener(ImmutableSet.of(EVS_SERVICE_REQUEST)); 110 111 List<Integer> events = new ArrayList<>(); 112 doAnswer(invocation -> { 113 events.add(invocation.getArgument(0)); 114 events.add(invocation.getArgument(1) ? TRUE : FALSE); 115 return null; 116 }).when(mListener).onEvent(anyInt(), anyBoolean()); 117 118 dispatchEvsServiceRequest(EvsServiceType.REARVIEW, EvsServiceState.OFF); 119 120 assertThat(events.get(0)).isEqualTo(CarEvsManager.SERVICE_TYPE_REARVIEW); 121 assertThat(events.get(1)).isEqualTo(FALSE); 122 } 123 124 // TODO(b/179029031): Adds more tests to verify the surround view service integration. 125 dispatchEvsServiceRequest(int type, int state)126 private void dispatchEvsServiceRequest(int type, int state) { 127 mEvsHalService.onHalEvents( 128 ImmutableList.of(buildEvsServiceRequestProp(type, state))); 129 } 130 buildEvsServiceRequestProp(int type, int state)131 private VehiclePropValue buildEvsServiceRequestProp(int type, int state) { 132 VehiclePropValue v = new VehiclePropValue(); 133 v.prop = VehicleProperty.EVS_SERVICE_REQUEST; 134 v.value.int32Values.add(type); 135 v.value.int32Values.add(state); 136 137 return v; 138 } 139 subscribeListener(Collection<VehiclePropConfig> properties)140 private void subscribeListener(Collection<VehiclePropConfig> properties) { 141 reset(mListener); 142 mEvsHalService.takeProperties(properties); 143 mEvsHalService.setListener(mListener); 144 145 for (VehiclePropConfig config : properties) { 146 verify(mVehicleHal).subscribeProperty(mEvsHalService, config.prop); 147 } 148 } 149 } 150