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); |
Jason Tholstrup | 57de612 | 2016-08-18 16:55:56 -0700 | [diff] [blame] | 46 | manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, ownershipListener); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 47 | listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, |
| 48 | CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, true); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 49 | manager.abandonAppFocus(ownershipListener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION); |
| 50 | listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, |
| 51 | CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, false); |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 52 | manager.removeFocusListener(listener); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 55 | private class FocusChangedListener implements CarAppFocusManager.OnAppFocusChangedListener { |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 56 | 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 Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 71 | public void onAppFocusChanged(int appType, boolean active) { |
| 72 | Log.i(TAG, "onAppFocusChanged appType=" + appType + " active=" + active); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 73 | mLastChangeAppType = appType; |
| 74 | mLastChangeAppActive = active; |
| 75 | mChangeWait.release(); |
| 76 | } |
| 77 | } |
| 78 | |
Vitalii Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 79 | private class FocusOwnershipCallback |
| 80 | implements CarAppFocusManager.OnAppFocusOwnershipCallback { |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 81 | private int mLastLossEvent; |
| 82 | private final Semaphore mLossEventWait = new Semaphore(0); |
| 83 | |
Vitalii Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 84 | private int mLastGrantEvent; |
| 85 | private final Semaphore mGrantEventWait = new Semaphore(0); |
| 86 | |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 87 | 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 Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 96 | 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 Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 105 | @Override |
Jason Tholstrup | d72b535 | 2016-09-22 16:32:14 -0700 | [diff] [blame] | 106 | public void onAppFocusOwnershipLost(int appType) { |
| 107 | Log.i(TAG, "onAppFocusOwnershipLost " + appType); |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 108 | mLastLossEvent = appType; |
| 109 | mLossEventWait.release(); |
| 110 | } |
Vitalii Tomkiv | d15d887 | 2016-10-04 14:08:56 -0700 | [diff] [blame] | 111 | |
| 112 | @Override |
| 113 | public void onAppFocusOwnershipGranted(int appType) { |
| 114 | Log.i(TAG, "onAppFocusOwnershipGranted " + appType); |
| 115 | mLastGrantEvent = appType; |
| 116 | mGrantEventWait.release(); |
| 117 | } |
Vitalii Tomkiv | 4637147 | 2016-05-23 16:55:22 -0700 | [diff] [blame] | 118 | } |
| 119 | } |