Home | History | Annotate | Download | only in apitest
      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 com.android.car.apitest;
     17 
     18 import android.car.Car;
     19 import android.car.CarAppContextManager;
     20 import android.car.CarAppContextManager.AppContextChangeListener;
     21 import android.car.CarAppContextManager.AppContextOwnershipChangeListener;
     22 import android.car.navigation.CarNavigationInstrumentCluster;
     23 import android.car.navigation.CarNavigationManager;
     24 import android.car.navigation.CarNavigationManager.CarNavigationListener;
     25 import android.test.suitebuilder.annotation.MediumTest;
     26 import android.util.Log;
     27 
     28 import java.util.concurrent.CountDownLatch;
     29 import java.util.concurrent.TimeUnit;
     30 
     31 /**
     32  * Unit tests for {@link CarNavigationManager}
     33  */
     34 @MediumTest
     35 public class CarNavigationManagerTest extends CarApiTestBase {
     36 
     37     private static final String TAG = CarNavigationManagerTest.class.getSimpleName();
     38 
     39     private CarNavigationManager mCarNavigationManager;
     40     private CarAppContextManager mCarAppContextManager;
     41 
     42     @Override
     43     protected void setUp() throws Exception {
     44         super.setUp();
     45         mCarNavigationManager =
     46                 (CarNavigationManager) getCar().getCarManager(Car.CAR_NAVIGATION_SERVICE);
     47         assertNotNull(mCarNavigationManager);
     48         mCarAppContextManager =
     49                 (CarAppContextManager) getCar().getCarManager(Car.APP_CONTEXT_SERVICE);
     50         assertNotNull(mCarAppContextManager);
     51     }
     52 
     53     public void testStart() throws Exception {
     54         if (!mCarNavigationManager.isInstrumentClusterSupported()) {
     55             Log.w(TAG, "Unable to run the test: instrument cluster is not supported");
     56             return;
     57         }
     58 
     59         final CountDownLatch onStartLatch = new CountDownLatch(1);
     60 
     61         mCarNavigationManager.registerListener(new CarNavigationListener() {
     62             @Override
     63             public void onInstrumentClusterStart(CarNavigationInstrumentCluster instrumentCluster) {
     64                 // TODO: we should use VehicleHalMock once we implement HAL support in
     65                 // CarNavigationStatusService.
     66                 assertFalse(instrumentCluster.supportsCustomImages());
     67                 assertEquals(1000, instrumentCluster.getMinIntervalMs());
     68                 onStartLatch.countDown();
     69             }
     70 
     71             @Override
     72             public void onInstrumentClusterStop() {
     73               // TODO
     74             }
     75         });
     76 
     77         assertTrue(onStartLatch.await(DEFAULT_WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS));
     78 
     79         try {
     80             mCarNavigationManager.sendNavigationStatus(1);
     81             fail();
     82         } catch (IllegalStateException expected) {
     83             // Expected. Client should acquire context ownership for APP_CONTEXT_NAVIGATION.
     84         }
     85 
     86         mCarAppContextManager.registerContextListener(new AppContextChangeListener() {
     87             @Override
     88             public void onAppContextChange(int activeContexts) {
     89                 // Nothing to do here.
     90             }
     91         }, CarAppContextManager.APP_CONTEXT_NAVIGATION);
     92         mCarAppContextManager.setActiveContexts(new AppContextOwnershipChangeListener() {
     93             @Override
     94             public void onAppContextOwnershipLoss(int context) {
     95                 // Nothing to do here.
     96             }
     97         }, CarAppContextManager.APP_CONTEXT_NAVIGATION);
     98         assertTrue(mCarAppContextManager.isOwningContext(
     99                 CarAppContextManager.APP_CONTEXT_NAVIGATION));
    100 
    101         // TODO: we should use mocked HAL to be able to verify this, right now just make sure that
    102         // it is not crashing and logcat has appropriate traces.
    103         mCarNavigationManager.sendNavigationStatus(1);
    104     }
    105 }
    106