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