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