blob: 3474d88d3147cd6d3a56ba584244c3c024e61ae1 [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);
Jason Tholstrup57de6122016-08-18 16:55:56 -070046 manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, ownershipListener);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070047 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS,
48 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, true);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070049 manager.abandonAppFocus(ownershipListener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
50 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS,
51 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, false);
Jason Tholstrupd72b5352016-09-22 16:32:14 -070052 manager.removeFocusListener(listener);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070053 }
54
Jason Tholstrupd72b5352016-09-22 16:32:14 -070055 private class FocusChangedListener implements CarAppFocusManager.OnAppFocusChangedListener {
Vitalii Tomkiv46371472016-05-23 16:55:22 -070056 private int mLastChangeAppType;
57 private boolean mLastChangeAppActive;
58 private final Semaphore mChangeWait = new Semaphore(0);
59
60 public boolean waitForFocusChangeAndAssert(long timeoutMs, int expectedAppType,
61 boolean expectedAppActive) throws Exception {
62 if (!mChangeWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
63 return false;
64 }
65 assertEquals(expectedAppType, mLastChangeAppType);
66 assertEquals(expectedAppActive, mLastChangeAppActive);
67 return true;
68 }
69
70 @Override
Jason Tholstrupd72b5352016-09-22 16:32:14 -070071 public void onAppFocusChanged(int appType, boolean active) {
72 Log.i(TAG, "onAppFocusChanged appType=" + appType + " active=" + active);
Vitalii Tomkiv46371472016-05-23 16:55:22 -070073 mLastChangeAppType = appType;
74 mLastChangeAppActive = active;
75 mChangeWait.release();
76 }
77 }
78
Vitalii Tomkivd15d8872016-10-04 14:08:56 -070079 private class FocusOwnershipCallback
80 implements CarAppFocusManager.OnAppFocusOwnershipCallback {
Vitalii Tomkiv46371472016-05-23 16:55:22 -070081 private int mLastLossEvent;
82 private final Semaphore mLossEventWait = new Semaphore(0);
83
Vitalii Tomkivd15d8872016-10-04 14:08:56 -070084 private int mLastGrantEvent;
85 private final Semaphore mGrantEventWait = new Semaphore(0);
86
Vitalii Tomkiv46371472016-05-23 16:55:22 -070087 public boolean waitForOwnershipLossAndAssert(long timeoutMs, int expectedLossAppType)
88 throws Exception {
89 if (!mLossEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
90 return false;
91 }
92 assertEquals(expectedLossAppType, mLastLossEvent);
93 return true;
94 }
95
Vitalii Tomkivd15d8872016-10-04 14:08:56 -070096 public boolean waitForOwnershipGrantAndAssert(long timeoutMs, int expectedGrantAppType)
97 throws Exception {
98 if (!mGrantEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
99 return false;
100 }
101 assertEquals(expectedGrantAppType, mLastGrantEvent);
102 return true;
103 }
104
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700105 @Override
Jason Tholstrupd72b5352016-09-22 16:32:14 -0700106 public void onAppFocusOwnershipLost(int appType) {
107 Log.i(TAG, "onAppFocusOwnershipLost " + appType);
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700108 mLastLossEvent = appType;
109 mLossEventWait.release();
110 }
Vitalii Tomkivd15d8872016-10-04 14:08:56 -0700111
112 @Override
113 public void onAppFocusOwnershipGranted(int appType) {
114 Log.i(TAG, "onAppFocusOwnershipGranted " + appType);
115 mLastGrantEvent = appType;
116 mGrantEventWait.release();
117 }
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700118 }
119}