Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2015 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.support.car.test;
     17 
     18 import android.car.test.VehicleHalEmulator.VehicleHalPropertyHandler;
     19 import android.content.Context;
     20 import android.media.AudioManager;
     21 import android.support.car.Car;
     22 import android.support.car.CarAppContextManager;
     23 import android.test.suitebuilder.annotation.MediumTest;
     24 import android.util.Log;
     25 
     26 import com.android.car.test.MockedCarTestBase;
     27 import com.android.car.vehiclenetwork.VehicleNetworkConsts;
     28 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleAudioContextFlag;
     29 import com.android.car.vehiclenetwork.VehiclePropConfigUtil;
     30 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePermissionModel;
     31 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropAccess;
     32 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropChangeMode;
     33 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
     34 import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
     35 
     36 import java.util.concurrent.Semaphore;
     37 import java.util.concurrent.TimeUnit;
     38 
     39 @MediumTest
     40 public class AppContextTest extends MockedCarTestBase {
     41     private static final String TAG = AppContextTest.class.getSimpleName();
     42     private static final long DEFAULT_WAIT_TIMEOUT_MS = 1000;
     43 
     44     @Override
     45     protected void setUp() throws Exception {
     46         super.setUp();
     47         getVehicleHalEmulator().start();
     48     }
     49 
     50     public void testContextChange() throws Exception {
     51         CarAppContextManager manager = (CarAppContextManager) getSupportCar().getCarManager(
     52                 Car.APP_CONTEXT_SERVICE);
     53         ContextChangeListener listener = new ContextChangeListener();
     54         ContextOwnershipChangeListerner ownershipListener = new ContextOwnershipChangeListerner();
     55         manager.registerContextListener(listener, CarAppContextManager.APP_CONTEXT_NAVIGATION |
     56                 CarAppContextManager.APP_CONTEXT_VOICE_COMMAND);
     57         manager.setActiveContexts(ownershipListener, CarAppContextManager.APP_CONTEXT_NAVIGATION);
     58         manager.setActiveContexts(ownershipListener, CarAppContextManager.APP_CONTEXT_VOICE_COMMAND);
     59         manager.resetActiveContexts(CarAppContextManager.APP_CONTEXT_NAVIGATION);
     60         manager.resetActiveContexts(CarAppContextManager.APP_CONTEXT_VOICE_COMMAND);
     61         manager.unregisterContextListener();
     62     }
     63 
     64     private class ContextChangeListener implements CarAppContextManager.AppContextChangeListener {
     65         private int mLastChangeEvent;
     66         private final Semaphore mChangeWait = new Semaphore(0);
     67 
     68         public boolean waitForContextChangeAndAssert(long timeoutMs, int expectedContexts)
     69                 throws Exception {
     70             if (!mChangeWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
     71                 return false;
     72             }
     73             assertEquals(expectedContexts, mLastChangeEvent);
     74             return true;
     75         }
     76 
     77         @Override
     78         public void onAppContextChange(int activeContexts) {
     79             Log.i(TAG, "onAppContextChange " + Integer.toHexString(activeContexts));
     80             mLastChangeEvent = activeContexts;
     81             mChangeWait.release();
     82         }
     83     }
     84 
     85     private class ContextOwnershipChangeListerner
     86             implements CarAppContextManager.AppContextOwnershipChangeListener {
     87         private int mLastLossEvent;
     88         private final Semaphore mLossEventWait = new Semaphore(0);
     89 
     90         public boolean waitForOwnershipLossAndAssert(long timeoutMs, int expectedContexts)
     91                 throws Exception {
     92             if (!mLossEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
     93                 return false;
     94             }
     95             assertEquals(expectedContexts, mLastLossEvent);
     96             return true;
     97         }
     98 
     99         @Override
    100         public void onAppContextOwnershipLoss(int context) {
    101             Log.i(TAG, "onAppContextOwnershipLoss " + Integer.toHexString(context));
    102             mLastLossEvent = context;
    103             mLossEventWait.release();
    104         }
    105     }
    106 }
    107