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