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