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 */
16package com.android.support.car.test;
17
18import android.car.test.VehicleHalEmulator.VehicleHalPropertyHandler;
19import android.content.Context;
20import android.media.AudioManager;
21import android.support.car.Car;
22import android.support.car.CarAppContextManager;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.util.Log;
25
26import com.android.car.test.MockedCarTestBase;
27import com.android.car.vehiclenetwork.VehicleNetworkConsts;
28import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleAudioContextFlag;
29import com.android.car.vehiclenetwork.VehiclePropConfigUtil;
30import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePermissionModel;
31import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropAccess;
32import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehiclePropChangeMode;
33import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
34import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
35
36import java.util.concurrent.Semaphore;
37import java.util.concurrent.TimeUnit;
38
39@MediumTest
40public 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