blob: c627c19384385906b2c96a517fd9b1c9c10005df [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;
20import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
21
Wale Ogunwaleda8b8272018-11-29 19:37:37 -080022import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
Tadashi G. Takaokaa7a66952018-11-16 15:04:21 +090023import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
Wale Ogunwaleda8b8272018-11-29 19:37:37 -080024import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
25import static com.android.server.wm.ActivityTestsBase.ActivityBuilder.createIntentAndActivityInfo;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090026import static com.android.server.wm.WindowContainer.POSITION_TOP;
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090027
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090028import android.app.ActivityManager;
Wale Ogunwaleda8b8272018-11-29 19:37:37 -080029import android.content.Intent;
30import android.content.pm.ActivityInfo;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090031import android.os.IBinder;
Wale Ogunwaleda8b8272018-11-29 19:37:37 -080032import android.util.Pair;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090033import android.view.IWindow;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090034import android.view.WindowManager;
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090035
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090036/**
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +090037 * A collection of static functions that provide access to WindowManager related test functionality.
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090038 */
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +090039class WindowTestUtils {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090040 private static int sNextTaskId = 0;
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +090041
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090042 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +090043 static Task createTaskInStack(WindowManagerService service, TaskStack stack,
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090044 int userId) {
45 synchronized (service.mGlobalLock) {
46 final Task newTask = new Task(sNextTaskId++, stack, userId, service, 0, false,
47 new ActivityManager.TaskDescription(), null);
48 stack.addTask(newTask, POSITION_TOP);
49 return newTask;
50 }
51 }
52
Yunfan Chen0e7aff92018-12-05 16:35:32 -080053 /** Creates an {@link AppWindowToken} and adds it to the specified {@link Task}. */
Wale Ogunwaleda8b8272018-11-29 19:37:37 -080054 static AppWindowToken createAppWindowTokenInTask(DisplayContent dc, Task task) {
55 final AppWindowToken newToken = createTestAppWindowToken(dc);
Yunfan Chen0e7aff92018-12-05 16:35:32 -080056 task.addChild(newToken, POSITION_TOP);
57 return newToken;
58 }
59
Wale Ogunwaleda8b8272018-11-29 19:37:37 -080060 static AppWindowToken createTestAppWindowToken(DisplayContent dc) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -080061 synchronized (dc.mWmService.mGlobalLock) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -080062 Pair<Intent, ActivityInfo> pair = createIntentAndActivityInfo();
63 final AppWindowToken token = new AppWindowToken(dc.mWmService,
64 dc.mWmService.mAtmService, new ActivityRecord.Token(pair.first), pair.second,
65 null, pair.first, dc);
66 token.setOccludesParent(true);
67 token.setHidden(false);
68 token.hiddenRequested = false;
69 spyOn(token);
70 return token;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090071 }
72 }
73
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090074 static TestWindowToken createTestWindowToken(int type, DisplayContent dc) {
75 return createTestWindowToken(type, dc, false /* persistOnEmpty */);
76 }
77
78 static TestWindowToken createTestWindowToken(int type, DisplayContent dc,
79 boolean persistOnEmpty) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -080080 synchronized (dc.mWmService.mGlobalLock) {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090081 return new TestWindowToken(type, dc, persistOnEmpty);
82 }
83 }
84
85 /* Used so we can gain access to some protected members of the {@link WindowToken} class */
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +090086 static class TestWindowToken extends WindowToken {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090087
88 private TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -080089 super(dc.mWmService, mock(IBinder.class), type, persistOnEmpty, dc,
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090090 false /* ownerCanManageAppTokens */);
91 }
92
93 int getWindowsCount() {
94 return mChildren.size();
95 }
96
97 boolean hasWindow(WindowState w) {
98 return mChildren.contains(w);
99 }
100 }
101
102 /* Used so we can gain access to some protected members of the {@link Task} class */
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900103 static class TestTask extends Task {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +0900104 boolean mShouldDeferRemoval = false;
105 boolean mOnDisplayChangedCalled = false;
106 private boolean mIsAnimating = false;
107
108 TestTask(int taskId, TaskStack stack, int userId, WindowManagerService service,
109 int resizeMode, boolean supportsPictureInPicture,
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800110 TaskRecord taskRecord) {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +0900111 super(taskId, stack, userId, service, resizeMode, supportsPictureInPicture,
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800112 new ActivityManager.TaskDescription(), taskRecord);
113 stack.addTask(this, POSITION_TOP);
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +0900114 }
115
116 boolean shouldDeferRemoval() {
117 return mShouldDeferRemoval;
118 }
119
120 int positionInParent() {
121 return getParent().mChildren.indexOf(this);
122 }
123
124 @Override
125 void onDisplayChanged(DisplayContent dc) {
126 super.onDisplayChanged(dc);
127 mOnDisplayChangedCalled = true;
128 }
129
130 @Override
131 boolean isSelfAnimating() {
132 return mIsAnimating;
133 }
134
135 void setLocalIsAnimating(boolean isAnimating) {
136 mIsAnimating = isAnimating;
137 }
138 }
139
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900140 static TestTask createTestTask(TaskStack stack) {
Yunfan Chen279f5582018-12-12 15:24:50 -0800141 return new TestTask(sNextTaskId++, stack, 0, stack.mWmService, RESIZE_MODE_UNRESIZEABLE,
142 false, mock(TaskRecord.class));
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +0900143 }
144
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +0900145 /** Used to track resize reports. */
Tadashi G. Takaokad7aa79a2019-02-08 17:42:37 +0900146 static class TestWindowState extends WindowState {
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +0900147 boolean mResizeReported;
148
149 TestWindowState(WindowManagerService service, Session session, IWindow window,
150 WindowManager.LayoutParams attrs, WindowToken token) {
151 super(service, session, window, token, null, OP_NONE, 0, attrs, 0, 0,
152 false /* ownerCanAddInternalSystemWindow */);
153 }
154
155 @Override
156 void reportResized() {
157 super.reportResized();
158 mResizeReported = true;
159 }
160
161 @Override
162 public boolean isGoneForLayoutLw() {
163 return false;
164 }
165
166 @Override
167 void updateResizingWindowIfNeeded() {
168 // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive
169 // the system that it can actually update the window.
170 boolean hadSurface = mHasSurface;
171 mHasSurface = true;
172
173 super.updateResizingWindowIfNeeded();
174
175 mHasSurface = hadSurface;
176 }
177 }
Tadashi G. Takaoka74ccec22018-10-23 11:07:13 +0900178}