• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.junit.Assert.assertEquals;
19 
20 import android.car.Car;
21 import android.car.CarAppFocusManager;
22 import android.util.Log;
23 
24 import androidx.test.ext.junit.runners.AndroidJUnit4;
25 import androidx.test.filters.MediumTest;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.util.concurrent.Semaphore;
31 import java.util.concurrent.TimeUnit;
32 
33 @RunWith(AndroidJUnit4.class)
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 
39     @Test
testFocusChange()40     public void testFocusChange() throws Exception {
41         CarAppFocusManager manager = (CarAppFocusManager) getCar().getCarManager(
42                 Car.APP_FOCUS_SERVICE);
43         FocusChangedListener listener = new FocusChangedListener();
44         FocusOwnershipCallback ownershipListener = new FocusOwnershipCallback();
45         manager.addFocusListener(listener, CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION);
46         manager.requestAppFocus(CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, ownershipListener);
47         listener.waitForFocusChangeAndAssert(DEFAULT_WAIT_TIMEOUT_MS,
48                 CarAppFocusManager.APP_FOCUS_TYPE_NAVIGATION, true);
49         listener.resetWait();
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.removeFocusListener(listener);
54     }
55 
56     private class FocusChangedListener implements CarAppFocusManager.OnAppFocusChangedListener {
57         private int mLastChangeAppType;
58         private boolean mLastChangeAppActive;
59         private final Semaphore mChangeWait = new Semaphore(0);
60 
waitForFocusChangeAndAssert(long timeoutMs, int expectedAppType, boolean expectedAppActive)61         private boolean waitForFocusChangeAndAssert(long timeoutMs, int expectedAppType,
62                 boolean expectedAppActive) throws Exception {
63             if (!mChangeWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
64                 return false;
65             }
66             assertEquals(expectedAppType, mLastChangeAppType);
67             assertEquals(expectedAppActive, mLastChangeAppActive);
68             return true;
69         }
70 
resetWait()71         private void resetWait() {
72             mChangeWait.drainPermits();
73         }
74 
75         @Override
onAppFocusChanged(int appType, boolean active)76         public void onAppFocusChanged(int appType, boolean active) {
77             Log.i(TAG, "onAppFocusChanged appType=" + appType + " active=" + active);
78             mLastChangeAppType = appType;
79             mLastChangeAppActive = active;
80             mChangeWait.release();
81         }
82     }
83 
84     private class FocusOwnershipCallback
85             implements CarAppFocusManager.OnAppFocusOwnershipCallback {
86         private int mLastLossEvent;
87         private final Semaphore mLossEventWait = new Semaphore(0);
88 
89         private int mLastGrantEvent;
90         private final Semaphore mGrantEventWait = new Semaphore(0);
91 
waitForOwnershipLossAndAssert(long timeoutMs, int expectedLossAppType)92         public boolean waitForOwnershipLossAndAssert(long timeoutMs, int expectedLossAppType)
93                 throws Exception {
94             if (!mLossEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
95                 return false;
96             }
97             assertEquals(expectedLossAppType, mLastLossEvent);
98             return true;
99         }
100 
waitForOwnershipGrantAndAssert(long timeoutMs, int expectedGrantAppType)101         public boolean waitForOwnershipGrantAndAssert(long timeoutMs, int expectedGrantAppType)
102                 throws Exception {
103             if (!mGrantEventWait.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
104                 return false;
105             }
106             assertEquals(expectedGrantAppType, mLastGrantEvent);
107             return true;
108         }
109 
110         @Override
onAppFocusOwnershipLost(int appType)111         public void onAppFocusOwnershipLost(int appType) {
112             Log.i(TAG, "onAppFocusOwnershipLost " + appType);
113             mLastLossEvent = appType;
114             mLossEventWait.release();
115         }
116 
117         @Override
onAppFocusOwnershipGranted(int appType)118         public void onAppFocusOwnershipGranted(int appType) {
119             Log.i(TAG, "onAppFocusOwnershipGranted " + appType);
120             mLastGrantEvent = appType;
121             mGrantEventWait.release();
122         }
123     }
124 }
125