blob: d1b6f6f3f4cf15dc54e01e6cc65010b8077ca370 [file] [log] [blame]
Riddle Hsu73f53572019-09-23 23:13:01 +08001/*
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
17package com.android.server.wm;
18
19import android.os.Handler;
20import android.testing.DexmakerShareClassLoaderRule;
21
22import org.junit.Rule;
23
24import java.util.concurrent.Callable;
25
26/** The base class which provides the common rule for test classes under wm package. */
27class SystemServiceTestsBase {
28 @Rule
29 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
30 new DexmakerShareClassLoaderRule();
31 @Rule
32 public final SystemServicesTestRule mSystemServicesTestRule = new SystemServicesTestRule();
33
34 @WindowTestRunner.MethodWrapperRule
35 public final WindowManagerGlobalLockRule mLockRule =
36 new WindowManagerGlobalLockRule(mSystemServicesTestRule);
37
38 /** Waits until the main handler for WM has processed all messages. */
39 void waitUntilHandlersIdle() {
40 mLockRule.waitForLocked(mSystemServicesTestRule::waitUntilWindowManagerHandlersIdle);
41 }
42
43 boolean waitHandlerIdle(Handler handler) {
44 return waitHandlerIdle(handler, 0 /* timeout */);
45 }
46
47 boolean waitHandlerIdle(Handler handler, long timeout) {
48 return runWithScissors(handler, () -> { }, timeout);
49 }
50
51 boolean runWithScissors(Handler handler, Runnable r, long timeout) {
52 return mLockRule.runWithScissors(handler, r, timeout);
53 }
54
55 /** It is used when we want to wait for a result inside {@link WindowManagerGlobalLock}. */
56 <T> T awaitInWmLock(Callable<T> callable) {
57 return mLockRule.waitForLocked(callable);
58 }
59}