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 com.android.server.wm; 18 19 import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT; 20 21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; 22 23 import android.os.Handler; 24 import android.platform.test.flag.junit.CheckFlagsRule; 25 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 26 import android.platform.test.flag.junit.SetFlagsRule; 27 import android.testing.DexmakerShareClassLoaderRule; 28 29 import org.junit.Rule; 30 31 import java.util.concurrent.Callable; 32 33 /** The base class which provides the common rule for test classes under wm package. */ 34 class SystemServiceTestsBase { 35 @Rule(order = 0) 36 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule = 37 new DexmakerShareClassLoaderRule(); 38 39 @Rule(order = 1) 40 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(DEVICE_DEFAULT); 41 42 @Rule(order = 2) 43 public final SystemServicesTestRule mSystemServicesTestRule = new SystemServicesTestRule( 44 this::onBeforeSystemServicesCreated); 45 46 @Rule 47 public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); 48 49 @WindowTestRunner.MethodWrapperRule 50 public final WindowManagerGlobalLockRule mLockRule = 51 new WindowManagerGlobalLockRule(mSystemServicesTestRule); 52 53 /** Waits until the main handler for WM has processed all messages. */ waitUntilHandlersIdle()54 void waitUntilHandlersIdle() { 55 mLockRule.waitForLocked(mSystemServicesTestRule::waitUntilWindowManagerHandlersIdle); 56 } 57 58 /** Waits until the choreographer of WindowAnimator has processed all callbacks. */ waitUntilWindowAnimatorIdle()59 void waitUntilWindowAnimatorIdle() { 60 mLockRule.waitForLocked(mSystemServicesTestRule::waitUntilWindowAnimatorIdle); 61 } 62 waitHandlerIdle(Handler handler)63 boolean waitHandlerIdle(Handler handler) { 64 return waitHandlerIdle(handler, 0 /* timeout */); 65 } 66 waitHandlerIdle(Handler handler, long timeout)67 boolean waitHandlerIdle(Handler handler, long timeout) { 68 return runWithScissors(handler, () -> { }, timeout); 69 } 70 runWithScissors(Handler handler, Runnable r, long timeout)71 boolean runWithScissors(Handler handler, Runnable r, long timeout) { 72 return mLockRule.runWithScissors(handler, r, timeout); 73 } 74 75 /** It is used when we want to wait for a result inside {@link WindowManagerGlobalLock}. */ awaitInWmLock(Callable<T> callable)76 <T> T awaitInWmLock(Callable<T> callable) { 77 return mLockRule.waitForLocked(callable); 78 } 79 80 /** 81 * Called before system services are created 82 */ onBeforeSystemServicesCreated()83 protected void onBeforeSystemServicesCreated() {} 84 85 /** 86 * Make the system booted, so that {@link ActivityStack#resumeTopActivityInnerLocked} can really 87 * be executed to update activity state and configuration when resuming the current top. 88 */ setBooted(ActivityTaskManagerService atmService)89 static void setBooted(ActivityTaskManagerService atmService) { 90 doReturn(false).when(atmService).isBooting(); 91 doReturn(true).when(atmService).isBooted(); 92 } 93 94 /** 95 * Utility class to compare the output of T#toString. It is convenient to have readable output 96 * of assertion if the string content can represent the expected states. 97 */ 98 static class ToStringComparatorWrapper<T> { 99 final T mObject; 100 ToStringComparatorWrapper(T object)101 ToStringComparatorWrapper(T object) { 102 mObject = object; 103 } 104 105 @Override equals(Object obj)106 public boolean equals(Object obj) { 107 return mObject.toString().equals(obj.toString()); 108 } 109 110 @Override toString()111 public String toString() { 112 return mObject.toString(); 113 } 114 } 115 } 116