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 */ 16 package com.android.car; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.junit.Assert.assertEquals; 21 22 import android.car.Car; 23 import android.car.CarAppFocusManager; 24 import android.os.Process; 25 import android.util.Log; 26 27 import androidx.test.ext.junit.runners.AndroidJUnit4; 28 import androidx.test.filters.MediumTest; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.util.concurrent.Semaphore; 34 import java.util.concurrent.TimeUnit; 35 36 @RunWith(AndroidJUnit4.class) 37 @MediumTest 38 public class AppFocusTest extends MockedCarTestBase { 39 private static final String TAG = AppFocusTest.class.getSimpleName(); 40 private static final long DEFAULT_WAIT_TIMEOUT_MS = 1000; 41 42 @Test testFocusChange()43 public void testFocusChange() throws Exception { 44 CarAppFocusManager manager = (CarAppFocusManager) getCar().getCarManager( 45 Car.APP_FOCUS_SERVICE); 46 FocusChangedListener listener = new FocusChangedListener(); 47 FocusOwnershipCallback ownershipListener = new FocusOwnershipCallback(); 48 manager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION); 49 manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, ownershipListener); 50 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, 51 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, true); 52 String[] myPackages = getContext().getPackageManager().getPackagesForUid(Process.myUid()); 53 assertThat(manager.getAppTypeOwner(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION)) 54 .containsExactlyElementsIn(myPackages); 55 listener.resetWait(); 56 manager.abandonAppFocus(ownershipListener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION); 57 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, 58 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, false); 59 assertThat(manager.getAppTypeOwner(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION)).isEmpty(); 60 manager.removeFocusListener(listener); 61 } 62 63 private static final class FocusChangedListener 64 implements CarAppFocusManager.OnAppFocusChangedListener { 65 private int mLastChangeAppType; 66 private boolean mLastChangeAppActive; 67 private final Semaphore mChangeWait = new Semaphore(0); 68 waitForFocusChangeAndAssert(long timeoutMs, int expectedAppType, boolean expectedAppActive)69 private boolean waitForFocusChangeAndAssert(long timeoutMs, int expectedAppType, 70 boolean expectedAppActive) throws Exception { 71 if (!mChangeWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { 72 return false; 73 } 74 assertEquals(expectedAppType, mLastChangeAppType); 75 assertEquals(expectedAppActive, mLastChangeAppActive); 76 return true; 77 } 78 resetWait()79 private void resetWait() { 80 mChangeWait.drainPermits(); 81 } 82 83 @Override onAppFocusChanged(int appType, boolean active)84 public void onAppFocusChanged(int appType, boolean active) { 85 Log.i(TAG, "onAppFocusChanged appType=" + appType + " active=" + active); 86 mLastChangeAppType = appType; 87 mLastChangeAppActive = active; 88 mChangeWait.release(); 89 } 90 } 91 92 private static final class FocusOwnershipCallback 93 implements CarAppFocusManager.OnAppFocusOwnershipCallback { 94 private final Semaphore mLossEventWait = new Semaphore(0); 95 96 private final Semaphore mGrantEventWait = new Semaphore(0); 97 98 @Override onAppFocusOwnershipLost(int appType)99 public void onAppFocusOwnershipLost(int appType) { 100 Log.i(TAG, "onAppFocusOwnershipLost " + appType); 101 mLossEventWait.release(); 102 } 103 104 @Override onAppFocusOwnershipGranted(int appType)105 public void onAppFocusOwnershipGranted(int appType) { 106 Log.i(TAG, "onAppFocusOwnershipGranted " + appType); 107 mGrantEventWait.release(); 108 } 109 } 110 } 111