AppFocusTest.java revision d15d88777f07265b258f637e4967942d98bd6333
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 @Override 32 protected void setUp() throws Exception { 33 super.setUp(); 34 getVehicleHalEmulator().start(); 35 } 36 37 public void testFocusChange() throws Exception { 38 CarAppFocusManager manager = (CarAppFocusManager) getCar().getCarManager( 39 Car.APP_FOCUS_SERVICE); 40 FocusChangedListener listener = new FocusChangedListener(); 41 FocusOwnershipCallback ownershipListener = new FocusOwnershipCallback(); 42 manager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION); 43 manager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND); 44 manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, ownershipListener); 45 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, 46 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, true); 47 manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND, ownershipListener); 48 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, 49 CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND, true); 50 manager.abandonAppFocus(ownershipListener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION); 51 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, 52 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, false); 53 manager.abandonAppFocus(ownershipListener, CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND); 54 listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS, 55 CarAppFocusManager.APP_FOCUS_TYPE_VOICE_COMMAND, false); 56 manager.removeFocusListener(listener); 57 } 58 59 private class FocusChangedListener implements CarAppFocusManager.OnAppFocusChangedListener { 60 private int mLastChangeAppType; 61 private boolean mLastChangeAppActive; 62 private final Semaphore mChangeWait = new Semaphore(0); 63 64 public boolean waitForFocusChangeAndAssert(long timeoutMs, int expectedAppType, 65 boolean expectedAppActive) throws Exception { 66 if (!mChangeWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { 67 return false; 68 } 69 assertEquals(expectedAppType, mLastChangeAppType); 70 assertEquals(expectedAppActive, mLastChangeAppActive); 71 return true; 72 } 73 74 @Override 75 public void onAppFocusChanged(int appType, boolean active) { 76 Log.i(TAG, "onAppFocusChanged appType=" + appType + " active=" + active); 77 mLastChangeAppType = appType; 78 mLastChangeAppActive = active; 79 mChangeWait.release(); 80 } 81 } 82 83 private class FocusOwnershipCallback 84 implements CarAppFocusManager.OnAppFocusOwnershipCallback { 85 private int mLastLossEvent; 86 private final Semaphore mLossEventWait = new Semaphore(0); 87 88 private int mLastGrantEvent; 89 private final Semaphore mGrantEventWait = new Semaphore(0); 90 91 public boolean waitForOwnershipLossAndAssert(long timeoutMs, int expectedLossAppType) 92 throws Exception { 93 if (!mLossEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { 94 return false; 95 } 96 assertEquals(expectedLossAppType, mLastLossEvent); 97 return true; 98 } 99 100 public boolean waitForOwnershipGrantAndAssert(long timeoutMs, int expectedGrantAppType) 101 throws Exception { 102 if (!mGrantEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { 103 return false; 104 } 105 assertEquals(expectedGrantAppType, mLastGrantEvent); 106 return true; 107 } 108 109 @Override 110 public void onAppFocusOwnershipLost(int appType) { 111 Log.i(TAG, "onAppFocusOwnershipLost " + appType); 112 mLastLossEvent = appType; 113 mLossEventWait.release(); 114 } 115 116 @Override 117 public void onAppFocusOwnershipGranted(int appType) { 118 Log.i(TAG, "onAppFocusOwnershipGranted " + appType); 119 mLastGrantEvent = appType; 120 mGrantEventWait.release(); 121 } 122 } 123} 124