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 
cleanupWindowManagerHandlers()50     void cleanupWindowManagerHandlers() {
51         mLockRule.waitForLocked(mSystemServicesTestRule::cleanupWindowManagerHandlers);
52     }
53 
waitHandlerIdle(Handler handler)54     boolean waitHandlerIdle(Handler handler) {
55         return waitHandlerIdle(handler, 0 /* timeout */);
56     }
57 
waitHandlerIdle(Handler handler, long timeout)58     boolean waitHandlerIdle(Handler handler, long timeout) {
59         return runWithScissors(handler, () -> { }, timeout);
60     }
61 
runWithScissors(Handler handler, Runnable r, long timeout)62     boolean runWithScissors(Handler handler, Runnable r, long timeout) {
63         return mLockRule.runWithScissors(handler, r, timeout);
64     }
65 
66     /** It is used when we want to wait for a result inside {@link WindowManagerGlobalLock}. */
awaitInWmLock(Callable<T> callable)67     <T> T awaitInWmLock(Callable<T> callable) {
68         return mLockRule.waitForLocked(callable);
69     }
70 
71     /**
72      * Make the system booted, so that {@link ActivityStack#resumeTopActivityInnerLocked} can really
73      * be executed to update activity state and configuration when resuming the current top.
74      */
setBooted(ActivityTaskManagerService atmService)75     static void setBooted(ActivityTaskManagerService atmService) {
76         doReturn(false).when(atmService).isBooting();
77         doReturn(true).when(atmService).isBooted();
78     }
79 
80     /**
81      * Utility class to compare the output of T#toString. It is convenient to have readable output
82      * of assertion if the string content can represent the expected states.
83      */
84     static class ToStringComparatorWrapper<T> {
85         final T mObject;
86 
ToStringComparatorWrapper(T object)87         ToStringComparatorWrapper(T object) {
88             mObject = object;
89         }
90 
91         @Override
equals(Object obj)92         public boolean equals(Object obj) {
93             return mObject.toString().equals(obj.toString());
94         }
95 
96         @Override
toString()97         public String toString() {
98             return mObject.toString();
99         }
100     }
101 }
102