1 /* 2 * Copyright (C) 2016 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 package android.car.apitest; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.testng.Assert.assertThrows; 21 22 import android.car.Car; 23 import android.car.CarAppFocusManager; 24 import android.car.CarAppFocusManager.OnAppFocusOwnershipCallback; 25 import android.car.cluster.navigation.NavigationState.Cue; 26 import android.car.cluster.navigation.NavigationState.Cue.CueElement; 27 import android.car.cluster.navigation.NavigationState.Destination; 28 import android.car.cluster.navigation.NavigationState.Distance; 29 import android.car.cluster.navigation.NavigationState.ImageReference; 30 import android.car.cluster.navigation.NavigationState.Lane; 31 import android.car.cluster.navigation.NavigationState.Lane.LaneDirection; 32 import android.car.cluster.navigation.NavigationState.LatLng; 33 import android.car.cluster.navigation.NavigationState.Maneuver; 34 import android.car.cluster.navigation.NavigationState.NavigationStateProto; 35 import android.car.cluster.navigation.NavigationState.Road; 36 import android.car.cluster.navigation.NavigationState.Step; 37 import android.car.cluster.navigation.NavigationState.Timestamp; 38 import android.car.navigation.CarNavigationStatusManager; 39 import android.os.Bundle; 40 import android.test.suitebuilder.annotation.MediumTest; 41 import android.util.Log; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 46 /** 47 * Unit tests for {@link CarNavigationStatusManager} 48 */ 49 @MediumTest 50 public class CarNavigationManagerTest extends CarApiTestBase { 51 52 private static final String TAG = CarNavigationManagerTest.class.getSimpleName(); 53 private static final String NAV_STATE_PROTO_BUNDLE_KEY = "navstate2"; 54 55 private CarNavigationStatusManager mCarNavigationManager; 56 private CarAppFocusManager mCarAppFocusManager; 57 58 @Before setUp()59 public void setUp() throws Exception { 60 mCarNavigationManager = 61 (CarNavigationStatusManager) getCar().getCarManager(Car.CAR_NAVIGATION_SERVICE); 62 mCarAppFocusManager = 63 (CarAppFocusManager) getCar().getCarManager(Car.APP_FOCUS_SERVICE); 64 assertThat(mCarAppFocusManager).isNotNull(); 65 } 66 67 @Test testSerializeAndDeserializeProto()68 public void testSerializeAndDeserializeProto() throws Exception { 69 ImageReference imageReference = ImageReference.newBuilder().build(); 70 Distance distance = Distance.newBuilder().build(); 71 Maneuver maneuver = Maneuver.newBuilder().build(); 72 Lane lane = Lane.newBuilder().build(); 73 LaneDirection laneDirection = LaneDirection.newBuilder().build(); 74 Cue cue = Cue.newBuilder().build(); 75 CueElement cueElement = CueElement.newBuilder().build(); 76 Step step = Step.newBuilder().build(); 77 LatLng latLng = LatLng.newBuilder().build(); 78 Destination destination = Destination.newBuilder().build(); 79 Road road = Road.newBuilder().build(); 80 Timestamp timestamp = Timestamp.newBuilder().build(); 81 NavigationStateProto navigationStateProto = NavigationStateProto.newBuilder().build(); 82 83 assertThat(imageReference).isNotNull(); 84 assertThat(distance).isNotNull(); 85 assertThat(maneuver).isNotNull(); 86 assertThat(lane).isNotNull(); 87 assertThat(laneDirection).isNotNull(); 88 assertThat(cue).isNotNull(); 89 assertThat(cueElement).isNotNull(); 90 assertThat(step).isNotNull(); 91 assertThat(latLng).isNotNull(); 92 assertThat(destination).isNotNull(); 93 assertThat(road).isNotNull(); 94 assertThat(timestamp).isNotNull(); 95 assertThat(navigationStateProto).isNotNull(); 96 97 98 assertThat(ImageReference.parseFrom(imageReference.toByteArray())).isNotNull(); 99 assertThat(Distance.parseFrom(distance.toByteArray())).isNotNull(); 100 assertThat(Maneuver.parseFrom(maneuver.toByteArray())).isNotNull(); 101 assertThat(Lane.parseFrom(lane.toByteArray())).isNotNull(); 102 assertThat(LaneDirection.parseFrom(laneDirection.toByteArray())).isNotNull(); 103 assertThat(Cue.parseFrom(cue.toByteArray())).isNotNull(); 104 assertThat(CueElement.parseFrom(cueElement.toByteArray())).isNotNull(); 105 assertThat(Step.parseFrom(step.toByteArray())).isNotNull(); 106 assertThat(LatLng.parseFrom(latLng.toByteArray())).isNotNull(); 107 assertThat(Destination.parseFrom(destination.toByteArray())).isNotNull(); 108 assertThat(Road.parseFrom(road.toByteArray())).isNotNull(); 109 assertThat(Timestamp.parseFrom(timestamp.toByteArray())).isNotNull(); 110 assertThat(NavigationStateProto.parseFrom(navigationStateProto.toByteArray())).isNotNull(); 111 } 112 113 @Test testSendEvent()114 public void testSendEvent() throws Exception { 115 if (mCarNavigationManager == null) { 116 Log.w(TAG, "Unable to run the test: " 117 + "car navigation manager was not created succesfully."); 118 return; 119 } 120 121 NavigationStateProto state = NavigationStateProto.newBuilder().build(); 122 Bundle bundle = new Bundle(); 123 bundle.putByteArray(NAV_STATE_PROTO_BUNDLE_KEY, state.toByteArray()); 124 125 assertThrows(IllegalStateException.class, () -> mCarNavigationManager.sendEvent(1, bundle)); 126 127 mCarAppFocusManager.addFocusListener(new CarAppFocusManager.OnAppFocusChangedListener() { 128 @Override 129 public void onAppFocusChanged(int appType, boolean active) { 130 // Nothing to do here. 131 } 132 }, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION); 133 OnAppFocusOwnershipCallback ownershipCallback = new OnAppFocusOwnershipCallback() { 134 @Override 135 public void onAppFocusOwnershipLost(int focus) { 136 // Nothing to do here. 137 } 138 139 @Override 140 public void onAppFocusOwnershipGranted(int focus) { 141 // Nothing to do here. 142 } 143 }; 144 mCarAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, 145 ownershipCallback); 146 assertThat(mCarAppFocusManager.isOwningFocus(ownershipCallback, 147 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION)).isTrue(); 148 149 Log.i(TAG, "Instrument cluster: " + mCarNavigationManager.getInstrumentClusterInfo()); 150 151 // TODO: we should use mocked HAL to be able to verify this, right now just make sure that 152 // it is not crashing and logcat has appropriate traces. 153 mCarNavigationManager.sendEvent(1, bundle); 154 } 155 } 156