• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.test.ApiCheckerRule.Builder;
40 import android.os.Bundle;
41 import android.test.suitebuilder.annotation.MediumTest;
42 import android.util.Log;
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     // TODO(b/242350638): add missing annotations, remove (on child bug of 242350638)
60     @Override
configApiCheckerRule(Builder builder)61     protected void configApiCheckerRule(Builder builder) {
62         Log.w(TAG, "Disabling API requirements check");
63         builder.disableAnnotationsCheck();
64     }
65 
66     @Before
setUp()67     public void setUp() throws Exception {
68         mCarNavigationManager =
69                 (CarNavigationStatusManager) getCar().getCarManager(Car.CAR_NAVIGATION_SERVICE);
70         mCarAppFocusManager =
71                 (CarAppFocusManager) getCar().getCarManager(Car.APP_FOCUS_SERVICE);
72         assertThat(mCarAppFocusManager).isNotNull();
73     }
74 
75     @Test
testSerializeAndDeserializeProto()76     public void testSerializeAndDeserializeProto() throws Exception {
77         ImageReference imageReference = ImageReference.newBuilder().build();
78         Distance distance = Distance.newBuilder().build();
79         Maneuver maneuver = Maneuver.newBuilder().build();
80         Lane lane = Lane.newBuilder().build();
81         LaneDirection laneDirection = LaneDirection.newBuilder().build();
82         Cue cue = Cue.newBuilder().build();
83         CueElement cueElement = CueElement.newBuilder().build();
84         Step step = Step.newBuilder().build();
85         LatLng latLng = LatLng.newBuilder().build();
86         Destination destination = Destination.newBuilder().build();
87         Road road = Road.newBuilder().build();
88         Timestamp timestamp = Timestamp.newBuilder().build();
89         NavigationStateProto navigationStateProto = NavigationStateProto.newBuilder().build();
90 
91         assertThat(imageReference).isNotNull();
92         assertThat(distance).isNotNull();
93         assertThat(maneuver).isNotNull();
94         assertThat(lane).isNotNull();
95         assertThat(laneDirection).isNotNull();
96         assertThat(cue).isNotNull();
97         assertThat(cueElement).isNotNull();
98         assertThat(step).isNotNull();
99         assertThat(latLng).isNotNull();
100         assertThat(destination).isNotNull();
101         assertThat(road).isNotNull();
102         assertThat(timestamp).isNotNull();
103         assertThat(navigationStateProto).isNotNull();
104 
105 
106         assertThat(ImageReference.parseFrom(imageReference.toByteArray())).isNotNull();
107         assertThat(Distance.parseFrom(distance.toByteArray())).isNotNull();
108         assertThat(Maneuver.parseFrom(maneuver.toByteArray())).isNotNull();
109         assertThat(Lane.parseFrom(lane.toByteArray())).isNotNull();
110         assertThat(LaneDirection.parseFrom(laneDirection.toByteArray())).isNotNull();
111         assertThat(Cue.parseFrom(cue.toByteArray())).isNotNull();
112         assertThat(CueElement.parseFrom(cueElement.toByteArray())).isNotNull();
113         assertThat(Step.parseFrom(step.toByteArray())).isNotNull();
114         assertThat(LatLng.parseFrom(latLng.toByteArray())).isNotNull();
115         assertThat(Destination.parseFrom(destination.toByteArray())).isNotNull();
116         assertThat(Road.parseFrom(road.toByteArray())).isNotNull();
117         assertThat(Timestamp.parseFrom(timestamp.toByteArray())).isNotNull();
118         assertThat(NavigationStateProto.parseFrom(navigationStateProto.toByteArray())).isNotNull();
119     }
120 
121     @Test
testSendEvent()122     public void testSendEvent() throws Exception {
123         if (mCarNavigationManager == null) {
124             Log.w(TAG, "Unable to run the test: "
125                     + "car navigation manager was not created succesfully.");
126             return;
127         }
128 
129         NavigationStateProto state = NavigationStateProto.newBuilder().build();
130         Bundle bundle = new Bundle();
131         bundle.putByteArray(NAV_STATE_PROTO_BUNDLE_KEY, state.toByteArray());
132 
133         assertThrows(IllegalStateException.class, () -> mCarNavigationManager.sendEvent(1, bundle));
134 
135         mCarAppFocusManager.addFocusListener(new CarAppFocusManager.OnAppFocusChangedListener() {
136             @Override
137             public void onAppFocusChanged(int appType, boolean active) {
138                 // Nothing to do here.
139             }
140         }, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
141         OnAppFocusOwnershipCallback ownershipCallback = new OnAppFocusOwnershipCallback() {
142             @Override
143             public void onAppFocusOwnershipLost(int focus) {
144                 // Nothing to do here.
145             }
146 
147             @Override
148             public void onAppFocusOwnershipGranted(int focus) {
149                 // Nothing to do here.
150             }
151         };
152         mCarAppFocusManager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION,
153                 ownershipCallback);
154         assertThat(mCarAppFocusManager.isOwningFocus(ownershipCallback,
155                 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION)).isTrue();
156 
157         Log.i(TAG, "Instrument cluster: " + mCarNavigationManager.getInstrumentClusterInfo());
158 
159         // TODO: we should use mocked HAL to be able to verify this, right now just make sure that
160         // it is not crashing and logcat has appropriate traces.
161         mCarNavigationManager.sendEvent(1, bundle);
162     }
163 }
164