blob: 26743c84212232218e8bd6881e65e0b8104b71f2 [file] [log] [blame]
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +09001/*
2 * Copyright (C) 2017 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
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090019import static android.app.AppOpsManager.OP_NONE;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090020
Tadashi G. Takaokaa7a66952018-11-16 15:04:21 +090021import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090022import static com.android.server.wm.WindowContainer.POSITION_TOP;
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090023
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090024import android.os.IBinder;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090025import android.view.IWindow;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090026import android.view.WindowManager;
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090027
Garfield Tane8d84ab2019-10-11 09:49:40 -070028import com.android.server.wm.ActivityTestsBase.ActivityBuilder;
29
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090030/**
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +090031 * A collection of static functions that provide access to WindowManager related test functionality.
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090032 */
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +090033class WindowTestUtils {
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090034
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070035 /** Creates a {@link Task} and adds it to the specified {@link ActivityStack}. */
36 static Task createTaskInStack(WindowManagerService service, ActivityStack stack, int userId) {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090037 synchronized (service.mGlobalLock) {
Louis Changcdec0802019-11-11 11:45:07 +080038 final Task task = new ActivityTestsBase.TaskBuilder(
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070039 stack.mStackSupervisor)
Wale Ogunwalea733c792019-10-16 21:31:15 -070040 .setUserId(userId)
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070041 .setStack(stack)
Wale Ogunwalea733c792019-10-16 21:31:15 -070042 .build();
Wale Ogunwale2322bed2019-10-10 17:24:19 +020043 return task;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090044 }
45 }
46
Garfield Tane8d84ab2019-10-11 09:49:40 -070047 /** Creates an {@link ActivityRecord} and adds it to the specified {@link Task}. */
48 static ActivityRecord createActivityRecordInTask(DisplayContent dc, Task task) {
49 final ActivityRecord activity = createTestActivityRecord(dc);
50 task.addChild(activity, POSITION_TOP);
51 return activity;
Yunfan Chen0e7aff92018-12-05 16:35:32 -080052 }
53
Wale Ogunwalea733c792019-10-16 21:31:15 -070054 static ActivityRecord createTestActivityRecord(ActivityStack stack) {
55 synchronized (stack.mService.mGlobalLock) {
56 final ActivityRecord activity = new ActivityTestsBase.ActivityBuilder(
57 stack.mService)
58 .setStack(stack)
59 .setCreateTask(true)
60 .build();
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070061 postCreateActivitySetup(activity, stack.getDisplayContent());
Wale Ogunwalea733c792019-10-16 21:31:15 -070062 return activity;
63 }
64 }
65
Garfield Tane8d84ab2019-10-11 09:49:40 -070066 static ActivityRecord createTestActivityRecord(DisplayContent dc) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -080067 synchronized (dc.mWmService.mGlobalLock) {
Garfield Tane8d84ab2019-10-11 09:49:40 -070068 final ActivityRecord activity = new ActivityBuilder(dc.mWmService.mAtmService).build();
Wale Ogunwalea733c792019-10-16 21:31:15 -070069 postCreateActivitySetup(activity, dc);
Garfield Tane8d84ab2019-10-11 09:49:40 -070070 return activity;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090071 }
72 }
73
Wale Ogunwalea733c792019-10-16 21:31:15 -070074 private static void postCreateActivitySetup(ActivityRecord activity, DisplayContent dc) {
75 activity.onDisplayChanged(dc);
76 activity.setOccludesParent(true);
Issei Suzukif2f6c912019-11-08 11:24:18 +010077 activity.setVisible(true);
Issei Suzuki1669ea42019-11-06 14:20:59 +010078 activity.mVisibleRequested = true;
Wale Ogunwalea733c792019-10-16 21:31:15 -070079 }
80
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090081 static TestWindowToken createTestWindowToken(int type, DisplayContent dc) {
82 return createTestWindowToken(type, dc, false /* persistOnEmpty */);
83 }
84
85 static TestWindowToken createTestWindowToken(int type, DisplayContent dc,
86 boolean persistOnEmpty) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -080087 synchronized (dc.mWmService.mGlobalLock) {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090088 return new TestWindowToken(type, dc, persistOnEmpty);
89 }
90 }
91
92 /* Used so we can gain access to some protected members of the {@link WindowToken} class */
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +090093 static class TestWindowToken extends WindowToken {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090094
95 private TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -080096 super(dc.mWmService, mock(IBinder.class), type, persistOnEmpty, dc,
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090097 false /* ownerCanManageAppTokens */);
98 }
99
100 int getWindowsCount() {
101 return mChildren.size();
102 }
103
104 boolean hasWindow(WindowState w) {
105 return mChildren.contains(w);
106 }
107 }
108
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +0900109 /** Used to track resize reports. */
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900110 static class TestWindowState extends WindowState {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +0900111 boolean mResizeReported;
112
113 TestWindowState(WindowManagerService service, Session session, IWindow window,
114 WindowManager.LayoutParams attrs, WindowToken token) {
115 super(service, session, window, token, null, OP_NONE, 0, attrs, 0, 0,
116 false /* ownerCanAddInternalSystemWindow */);
117 }
118
119 @Override
120 void reportResized() {
121 super.reportResized();
122 mResizeReported = true;
123 }
124
125 @Override
126 public boolean isGoneForLayoutLw() {
127 return false;
128 }
129
130 @Override
131 void updateResizingWindowIfNeeded() {
132 // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive
133 // the system that it can actually update the window.
134 boolean hadSurface = mHasSurface;
135 mHasSurface = true;
136
137 super.updateResizingWindowIfNeeded();
138
139 mHasSurface = hadSurface;
140 }
141 }
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +0900142}