Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 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 | */ |
Pavel Maltsev | 8edd255 | 2017-09-18 14:52:16 -0700 | [diff] [blame] | 16 | package com.android.car; |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 17 | |
Pavel Maltsev | ed2c864 | 2017-12-18 12:56:26 -0800 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| 19 | |
Keun-young Park | 9cea114 | 2016-09-26 15:31:25 -0700 | [diff] [blame] | 20 | import android.car.Car; |
| 21 | import android.car.CarAppFocusManager; |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | |
Brett Chabot | a81f3e8 | 2018-12-13 19:06:42 -0800 | [diff] [blame] | 24 | import androidx.test.filters.MediumTest; |
| 25 | import androidx.test.runner.AndroidJUnit4; |
| 26 | |
Pavel Maltsev | ed2c864 | 2017-12-18 12:56:26 -0800 | [diff] [blame] | 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 30 | import java.util.concurrent.Semaphore; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | |
Pavel Maltsev | ed2c864 | 2017-12-18 12:56:26 -0800 | [diff] [blame] | 33 | @RunWith(AndroidJUnit4.class) |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 34 | @MediumTest |
| 35 | public 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 Maltsev | ed2c864 | 2017-12-18 12:56:26 -0800 | [diff] [blame] | 39 | @Test |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 40 | public void testFocusChange() throws Exception { |
Keun-young Park | 9cea114 | 2016-09-26 15:31:25 -0700 | [diff] [blame] | 41 | CarAppFocusManager manager = (CarAppFocusManager) getCar().getCarManager( |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 42 | Car.APP_FOCUS_SERVICE); |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 43 | FocusChangedListener listener = new FocusChangedListener(); |
Vitalii Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 44 | FocusOwnershipCallback ownershipListener = new FocusOwnershipCallback(); |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 45 | manager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION); |
| 46 | manager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND); |
Jason Tholstrup | 57de612 | 2016-08-18 16:55:56 -0700 | [diff] [blame] | 47 | manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, ownershipListener); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 48 | listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, |
| 49 | CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, true); |
Jason Tholstrup | 57de612 | 2016-08-18 16:55:56 -0700 | [diff] [blame] | 50 | manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND, ownershipListener); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 51 | 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 Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 59 | manager.removeFocusListener(listener); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 62 | private class FocusChangedListener implements CarAppFocusManager.OnAppFocusChangedListener { |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 63 | 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 Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 78 | public void onAppFocusChanged(int appType, boolean active) { |
| 79 | Log.i(TAG, "onAppFocusChanged appType=" + appType + " active=" + active); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 80 | mLastChangeAppType = appType; |
| 81 | mLastChangeAppActive = active; |
| 82 | mChangeWait.release(); |
| 83 | } |
| 84 | } |
| 85 | |
Vitalii Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 86 | private class FocusOwnershipCallback |
| 87 | implements CarAppFocusManager.OnAppFocusOwnershipCallback { |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 88 | private int mLastLossEvent; |
| 89 | private final Semaphore mLossEventWait = new Semaphore(0); |
| 90 | |
Vitalii Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 91 | private int mLastGrantEvent; |
| 92 | private final Semaphore mGrantEventWait = new Semaphore(0); |
| 93 | |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 94 | 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 Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 103 | 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 Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 112 | @Override |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 113 | public void onAppFocusOwnershipLost(int appType) { |
| 114 | Log.i(TAG, "onAppFocusOwnershipLost " + appType); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 115 | mLastLossEvent = appType; |
| 116 | mLossEventWait.release(); |
| 117 | } |
Vitalii Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 118 | |
| 119 | @Override |
| 120 | public void onAppFocusOwnershipGranted(int appType) { |
| 121 | Log.i(TAG, "onAppFocusOwnershipGranted " + appType); |
| 122 | mLastGrantEvent = appType; |
| 123 | mGrantEventWait.release(); |
| 124 | } |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 125 | } |
| 126 | } |