1 /* 2 * Copyright (C) 2019 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 17 package android.server.wm; 18 19 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; 20 import static android.server.wm.ActivityManagerTestBase.launchHomeActivityNoWait; 21 import static android.server.wm.UiDeviceUtils.pressUnlockButton; 22 import static android.server.wm.UiDeviceUtils.pressWakeupButton; 23 import static android.view.Display.DEFAULT_DISPLAY; 24 25 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; 26 27 import static org.junit.Assert.assertEquals; 28 29 import android.app.Activity; 30 import android.app.ActivityOptions; 31 import android.content.Intent; 32 import android.os.Bundle; 33 34 import com.android.compatibility.common.util.SystemUtil; 35 36 import org.junit.Before; 37 38 import java.lang.reflect.Array; 39 import java.util.ArrayList; 40 import java.util.List; 41 42 import javax.annotation.concurrent.GuardedBy; 43 44 public class WindowManagerTestBase extends MultiDisplayTestBase { 45 static final long TIMEOUT_WINDOW_FOCUS_CHANGED = 1000; // milliseconds 46 47 @Before setupBase()48 public void setupBase() { 49 pressWakeupButton(); 50 pressUnlockButton(); 51 launchHomeActivityNoWait(); 52 } 53 startActivity(Class<T> cls)54 static <T extends FocusableActivity> T startActivity(Class<T> cls) { 55 return startActivity(cls, DEFAULT_DISPLAY); 56 } 57 startActivity(Class<T> cls, int displayId, boolean hasFocus)58 static <T extends FocusableActivity> T startActivity(Class<T> cls, int displayId, 59 boolean hasFocus) { 60 final Bundle options = (displayId == DEFAULT_DISPLAY 61 ? null : ActivityOptions.makeBasic().setLaunchDisplayId(displayId).toBundle()); 62 final T[] activity = (T[]) Array.newInstance(FocusableActivity.class, 1); 63 SystemUtil.runWithShellPermissionIdentity(() -> { 64 activity[0] = (T) getInstrumentation().startActivitySync( 65 new Intent(getInstrumentation().getTargetContext(), cls) 66 .addFlags(FLAG_ACTIVITY_NEW_TASK), options); 67 activity[0].waitAndAssertWindowFocusState(hasFocus); 68 }); 69 return activity[0]; 70 } 71 startActivity(Class<T> cls, int displayId)72 static <T extends FocusableActivity> T startActivity(Class<T> cls, int displayId) { 73 return startActivity(cls, displayId, true /* hasFocus */); 74 } 75 76 static class FocusableActivity extends Activity { 77 private final Object mLockWindowFocus = new Object(); 78 79 @GuardedBy("mLockWindowFocus") 80 private boolean mHasWindowFocus; 81 getLogTag()82 final String getLogTag() { 83 return ComponentNameUtils.getLogTag(getComponentName()); 84 } 85 86 @Override onWindowFocusChanged(boolean hasFocus)87 public void onWindowFocusChanged(boolean hasFocus) { 88 synchronized (mLockWindowFocus) { 89 mHasWindowFocus = hasFocus; 90 mLockWindowFocus.notify(); 91 } 92 } 93 assertWindowFocusState(boolean hasFocus)94 void assertWindowFocusState(boolean hasFocus) { 95 synchronized (mLockWindowFocus) { 96 assertEquals(getLogTag() + " must" + (hasFocus ? "" : " not") 97 + " have window focus.", hasFocus, mHasWindowFocus); 98 } 99 } 100 waitAndAssertWindowFocusState(boolean hasFocus)101 void waitAndAssertWindowFocusState(boolean hasFocus) { 102 synchronized (mLockWindowFocus) { 103 if (mHasWindowFocus != hasFocus) { 104 try { 105 mLockWindowFocus.wait(TIMEOUT_WINDOW_FOCUS_CHANGED); 106 } catch (InterruptedException e) { 107 } 108 } 109 } 110 assertWindowFocusState(hasFocus); 111 } 112 } 113 } 114