blob: 910a1326a5babc095a850b166e0a7a508b10310e [file] [log] [blame]
Vitalii Tomkiv46371472016-05-23 16:55:22 -07001/*
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 */
Pavel Maltsev8edd2552017-09-18 14:52:16 -070016package com.android.car;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070017
Pavel Maltseved2c8642017-12-18 12:56:26 -080018import static org.junit.Assert.assertEquals;
19
Keun-young Park9cea1142016-09-26 15:31:25 -070020import android.car.Car;
21import android.car.CarAppFocusManager;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070022import android.util.Log;
23
Brett Chabota81f3e82018-12-13 19:06:42 -080024import androidx.test.filters.MediumTest;
25import androidx.test.runner.AndroidJUnit4;
26
Pavel Maltseved2c8642017-12-18 12:56:26 -080027import org.junit.Test;
28import org.junit.runner.RunWith;
29
Vitalii Tomkiv46371472016-05-23 16:55:22 -070030import java.util.concurrent.Semaphore;
31import java.util.concurrent.TimeUnit;
32
Pavel Maltseved2c8642017-12-18 12:56:26 -080033@RunWith(AndroidJUnit4.class)
Vitalii Tomkiv46371472016-05-23 16:55:22 -070034@MediumTest
35public class AppFocusTest extends MockedCarTestBase {
36 private static final String TAG = AppFocusTest.class.getSimpleName();
37 private static final long DEFAULT_WAIT_TIMEOUT_MS = 1000;
38
Pavel Maltseved2c8642017-12-18 12:56:26 -080039 @Test
Vitalii Tomkiv46371472016-05-23 16:55:22 -070040 public void testFocusChange() throws Exception {
Keun-young Park9cea1142016-09-26 15:31:25 -070041 CarAppFocusManager manager = (CarAppFocusManager) getCar().getCarManager(
Vitalii Tomkiv46371472016-05-23 16:55:22 -070042 Car.APP_FOCUS_SERVICE);
Jason Tholstrupd72b5352016-09-22 16:32:14 -070043 FocusChangedListener listener = new FocusChangedListener();
Vitalii Tomkivd15d8872016-10-04 14:08:56 -070044 FocusOwnershipCallback ownershipListener = new FocusOwnershipCallback();
Jason Tholstrupd72b5352016-09-22 16:32:14 -070045 manager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
46 manager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND);
Jason Tholstrup57de6122016-08-18 16:55:56 -070047 manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, ownershipListener);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070048 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS,
49 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, true);
Jason Tholstrup57de6122016-08-18 16:55:56 -070050 manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND, ownershipListener);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070051 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS,
52 CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND, true);
53 manager.abandonAppFocus(ownershipListener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
54 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS,
55 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, false);
56 manager.abandonAppFocus(ownershipListener, CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND);
57 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS,
58 CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND, false);
Jason Tholstrupd72b5352016-09-22 16:32:14 -070059 manager.removeFocusListener(listener);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070060 }
61
Jason Tholstrupd72b5352016-09-22 16:32:14 -070062 private class FocusChangedListener implements CarAppFocusManager.OnAppFocusChangedListener {
Vitalii Tomkiv46371472016-05-23 16:55:22 -070063 private int mLastChangeAppType;
64 private boolean mLastChangeAppActive;
65 private final Semaphore mChangeWait = new Semaphore(0);
66
67 public boolean waitForFocusChangeAndAssert(long timeoutMs, int expectedAppType,
68 boolean expectedAppActive) throws Exception {
69 if (!mChangeWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
70 return false;
71 }
72 assertEquals(expectedAppType, mLastChangeAppType);
73 assertEquals(expectedAppActive, mLastChangeAppActive);
74 return true;
75 }
76
77 @Override
Jason Tholstrupd72b5352016-09-22 16:32:14 -070078 public void onAppFocusChanged(int appType, boolean active) {
79 Log.i(TAG, "onAppFocusChanged appType=" + appType + " active=" + active);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070080 mLastChangeAppType = appType;
81 mLastChangeAppActive = active;
82 mChangeWait.release();
83 }
84 }
85
Vitalii Tomkivd15d8872016-10-04 14:08:56 -070086 private class FocusOwnershipCallback
87 implements CarAppFocusManager.OnAppFocusOwnershipCallback {
Vitalii Tomkiv46371472016-05-23 16:55:22 -070088 private int mLastLossEvent;
89 private final Semaphore mLossEventWait = new Semaphore(0);
90
Vitalii Tomkivd15d8872016-10-04 14:08:56 -070091 private int mLastGrantEvent;
92 private final Semaphore mGrantEventWait = new Semaphore(0);
93
Vitalii Tomkiv46371472016-05-23 16:55:22 -070094 public boolean waitForOwnershipLossAndAssert(long timeoutMs, int expectedLossAppType)
95 throws Exception {
96 if (!mLossEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
97 return false;
98 }
99 assertEquals(expectedLossAppType, mLastLossEvent);
100 return true;
101 }
102
Vitalii Tomkivd15d8872016-10-04 14:08:56 -0700103 public boolean waitForOwnershipGrantAndAssert(long timeoutMs, int expectedGrantAppType)
104 throws Exception {
105 if (!mGrantEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
106 return false;
107 }
108 assertEquals(expectedGrantAppType, mLastGrantEvent);
109 return true;
110 }
111
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700112 @Override
Jason Tholstrupd72b5352016-09-22 16:32:14 -0700113 public void onAppFocusOwnershipLost(int appType) {
114 Log.i(TAG, "onAppFocusOwnershipLost " + appType);
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700115 mLastLossEvent = appType;
116 mLossEventWait.release();
117 }
Vitalii Tomkivd15d8872016-10-04 14:08:56 -0700118
119 @Override
120 public void onAppFocusOwnershipGranted(int appType) {
121 Log.i(TAG, "onAppFocusOwnershipGranted " + appType);
122 mLastGrantEvent = appType;
123 mGrantEventWait.release();
124 }
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700125 }
126}