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