blob: 6b09363cff0b20423950ce7b4f232e99b13d7071 [file] [log] [blame]
Bryce Lee840c5662017-04-13 10:02:51 -07001/*
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.am;
18
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
20import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
22import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
24import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Bryce Lee18d51592017-10-25 10:22:19 -070026
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070027import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_DESTROYING;
Bryce Lee18d51592017-10-25 10:22:19 -070028
Bryce Lee5daa3122017-04-19 10:40:42 -070029import static org.junit.Assert.assertEquals;
David Stevens18abd0e2017-08-17 14:55:47 -070030import static org.junit.Assert.assertFalse;
Bryce Lee840c5662017-04-13 10:02:51 -070031import static org.junit.Assert.assertNotNull;
32import static org.junit.Assert.assertNull;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -070033import static org.junit.Assert.assertTrue;
Bryce Lee840c5662017-04-13 10:02:51 -070034
35import android.content.ComponentName;
Bryce Lee3345c4e2017-04-25 07:40:41 -070036import android.content.pm.ActivityInfo;
Bryce Lee9f6affd2017-09-01 09:18:35 -070037import android.os.UserHandle;
Bryce Lee840c5662017-04-13 10:02:51 -070038import android.platform.test.annotations.Presubmit;
39import android.support.test.filters.SmallTest;
40import android.support.test.runner.AndroidJUnit4;
41
42import org.junit.runner.RunWith;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070043import org.junit.Before;
Bryce Lee840c5662017-04-13 10:02:51 -070044import org.junit.Test;
45
46/**
47 * Tests for the {@link ActivityStack} class.
48 *
49 * Build/Install/Run:
50 * bit FrameworksServicesTests:com.android.server.am.ActivityStackTests
51 */
52@SmallTest
53@Presubmit
54@RunWith(AndroidJUnit4.class)
55public class ActivityStackTests extends ActivityTestsBase {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070056 private ActivityManagerService mService;
57 private ActivityStackSupervisor mSupervisor;
58 private ActivityStack mStack;
59 private TaskRecord mTask;
60
61 @Before
62 @Override
63 public void setUp() throws Exception {
64 super.setUp();
65
66 mService = createActivityManagerService();
67 mSupervisor = mService.mStackSupervisor;
68 mStack = mService.mStackSupervisor.getDefaultDisplay().createStack(
69 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
Bryce Lee18d51592017-10-25 10:22:19 -070070 mTask = new TaskBuilder(mSupervisor).setStack(mStack).build();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070071 }
72
Bryce Lee840c5662017-04-13 10:02:51 -070073 @Test
74 public void testEmptyTaskCleanupOnRemove() throws Exception {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070075 assertNotNull(mTask.getWindowContainerController());
76 mStack.removeTask(mTask, "testEmptyTaskCleanupOnRemove", REMOVE_TASK_MODE_DESTROYING);
77 assertNull(mTask.getWindowContainerController());
Bryce Lee840c5662017-04-13 10:02:51 -070078 }
Bryce Lee5daa3122017-04-19 10:40:42 -070079
Bryce Lee840c5662017-04-13 10:02:51 -070080 @Test
81 public void testOccupiedTaskCleanupOnRemove() throws Exception {
Bryce Lee18d51592017-10-25 10:22:19 -070082 final ActivityRecord r = new ActivityBuilder(mService).setTask(mTask).build();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070083 assertNotNull(mTask.getWindowContainerController());
84 mStack.removeTask(mTask, "testOccupiedTaskCleanupOnRemove", REMOVE_TASK_MODE_DESTROYING);
85 assertNotNull(mTask.getWindowContainerController());
Bryce Lee840c5662017-04-13 10:02:51 -070086 }
Bryce Lee5daa3122017-04-19 10:40:42 -070087
88 @Test
89 public void testNoPauseDuringResumeTopActivity() throws Exception {
Bryce Lee18d51592017-10-25 10:22:19 -070090 final ActivityRecord r = new ActivityBuilder(mService).setTask(mTask).build();
Bryce Lee5daa3122017-04-19 10:40:42 -070091
92 // Simulate the a resumed activity set during
93 // {@link ActivityStack#resumeTopActivityUncheckedLocked}.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070094 mSupervisor.inResumeTopActivity = true;
95 mStack.mResumedActivity = r;
Bryce Lee5daa3122017-04-19 10:40:42 -070096
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070097 final boolean waiting = mStack.goToSleepIfPossible(false);
Bryce Lee5daa3122017-04-19 10:40:42 -070098
99 // Ensure we report not being ready for sleep.
David Stevens18abd0e2017-08-17 14:55:47 -0700100 assertFalse(waiting);
Bryce Lee5daa3122017-04-19 10:40:42 -0700101
102 // Make sure the resumed activity is untouched.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700103 assertEquals(mStack.mResumedActivity, r);
Bryce Lee5daa3122017-04-19 10:40:42 -0700104 }
Bryce Lee3345c4e2017-04-25 07:40:41 -0700105
106 @Test
107 public void testStopActivityWhenActivityDestroyed() throws Exception {
Bryce Lee18d51592017-10-25 10:22:19 -0700108 final ActivityRecord r = new ActivityBuilder(mService).setTask(mTask).build();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700109 r.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
110 mSupervisor.setFocusStackUnchecked("testStopActivityWithDestroy", mStack);
111 mStack.stopActivityLocked(r);
112 // Mostly testing to make sure there is a crash in the call part, so if we get here we are
113 // good-to-go!
Bryce Lee3345c4e2017-04-25 07:40:41 -0700114 }
Bryce Lee9f6affd2017-09-01 09:18:35 -0700115
116 @Test
117 public void testFindTaskWithOverlay() throws Exception {
Bryce Lee18d51592017-10-25 10:22:19 -0700118 final ActivityRecord r = new ActivityBuilder(mService)
119 .setCreateTask(true)
120 .setStack(mStack)
121 .setUid(0)
122 .build();
123 final TaskRecord task = r.getTask();
Bryce Lee9f6affd2017-09-01 09:18:35 -0700124 // Overlay must be for a different user to prevent recognizing a matching top activity
Bryce Lee18d51592017-10-25 10:22:19 -0700125 final ActivityRecord taskOverlay = new ActivityBuilder(mService).setTask(task)
126 .setUid(UserHandle.PER_USER_RANGE * 2).build();
Bryce Lee9f6affd2017-09-01 09:18:35 -0700127 taskOverlay.mTaskOverlay = true;
128
Bryce Lee9f6affd2017-09-01 09:18:35 -0700129 final ActivityStackSupervisor.FindTaskResult result =
130 new ActivityStackSupervisor.FindTaskResult();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700131 mStack.findTaskLocked(r, result);
Bryce Lee9f6affd2017-09-01 09:18:35 -0700132
Bryce Lee18d51592017-10-25 10:22:19 -0700133 assertEquals(task.getTopActivity(false /* includeOverlays */), r);
134 assertEquals(task.getTopActivity(true /* includeOverlays */), taskOverlay);
Bryce Lee9f6affd2017-09-01 09:18:35 -0700135 assertNotNull(result.r);
136 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700137
138 @Test
139 public void testShouldBeVisible_Fullscreen() throws Exception {
140 final ActivityDisplay display = mService.mStackSupervisor.getDefaultDisplay();
141 final TestActivityStack homeStack = createStackForShouldBeVisibleTest(display,
142 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, true /* onTop */);
143 final ActivityStack pinnedStack = createStackForShouldBeVisibleTest(display,
144 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, true /* onTop */);
145
146 assertTrue(homeStack.shouldBeVisible(null /* starting */));
147 assertTrue(pinnedStack.shouldBeVisible(null /* starting */));
148
149 final TestActivityStack fullscreenStack = createStackForShouldBeVisibleTest(display,
150 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
151 // Home stack shouldn't be visible behind an opaque fullscreen stack, but pinned stack
152 // should be visible since it is always on-top.
153 fullscreenStack.setIsTranslucent(false);
154 assertFalse(homeStack.shouldBeVisible(null /* starting */));
155 assertTrue(pinnedStack.shouldBeVisible(null /* starting */));
156 assertTrue(fullscreenStack.shouldBeVisible(null /* starting */));
157
158 // Home stack should be visible behind a translucent fullscreen stack.
159 fullscreenStack.setIsTranslucent(true);
160 assertTrue(homeStack.shouldBeVisible(null /* starting */));
161 assertTrue(pinnedStack.shouldBeVisible(null /* starting */));
162 }
163
164 @Test
165 public void testShouldBeVisible_SplitScreen() throws Exception {
166 final ActivityDisplay display = mService.mStackSupervisor.getDefaultDisplay();
167 final TestActivityStack homeStack = createStackForShouldBeVisibleTest(display,
168 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, true /* onTop */);
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800169 // Home stack should always be fullscreen for this test.
170 homeStack.setSupportsSplitScreen(false);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700171 final TestActivityStack splitScreenPrimary = createStackForShouldBeVisibleTest(display,
172 WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_STANDARD, true /* onTop */);
173 final TestActivityStack splitScreenSecondary = createStackForShouldBeVisibleTest(display,
174 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, ACTIVITY_TYPE_STANDARD, true /* onTop */);
175
176 // Home stack shouldn't be visible if both halves of split-screen are opaque.
177 splitScreenPrimary.setIsTranslucent(false);
178 splitScreenSecondary.setIsTranslucent(false);
179 assertFalse(homeStack.shouldBeVisible(null /* starting */));
180 assertTrue(splitScreenPrimary.shouldBeVisible(null /* starting */));
181 assertTrue(splitScreenSecondary.shouldBeVisible(null /* starting */));
182
183 // Home stack should be visible if one of the halves of split-screen is translucent.
184 splitScreenPrimary.setIsTranslucent(true);
185 assertTrue(homeStack.shouldBeVisible(null /* starting */));
186 assertTrue(splitScreenPrimary.shouldBeVisible(null /* starting */));
187 assertTrue(splitScreenSecondary.shouldBeVisible(null /* starting */));
188
189 final TestActivityStack splitScreenSecondary2 = createStackForShouldBeVisibleTest(display,
190 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, ACTIVITY_TYPE_STANDARD, true /* onTop */);
191 // First split-screen secondary shouldn't be visible behind another opaque split-split
192 // secondary.
193 splitScreenSecondary2.setIsTranslucent(false);
194 assertFalse(splitScreenSecondary.shouldBeVisible(null /* starting */));
195 assertTrue(splitScreenSecondary2.shouldBeVisible(null /* starting */));
196
197 // First split-screen secondary should be visible behind another translucent split-split
198 // secondary.
199 splitScreenSecondary2.setIsTranslucent(true);
200 assertTrue(splitScreenSecondary.shouldBeVisible(null /* starting */));
201 assertTrue(splitScreenSecondary2.shouldBeVisible(null /* starting */));
202
203 final TestActivityStack assistantStack = createStackForShouldBeVisibleTest(display,
204 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_ASSISTANT, true /* onTop */);
205
206 // Split-screen stacks shouldn't be visible behind an opaque fullscreen stack.
207 assistantStack.setIsTranslucent(false);
208 assertTrue(assistantStack.shouldBeVisible(null /* starting */));
209 assertFalse(splitScreenPrimary.shouldBeVisible(null /* starting */));
210 assertFalse(splitScreenSecondary.shouldBeVisible(null /* starting */));
211 assertFalse(splitScreenSecondary2.shouldBeVisible(null /* starting */));
212
213 // Split-screen stacks should be visible behind a translucent fullscreen stack.
214 assistantStack.setIsTranslucent(true);
215 assertTrue(assistantStack.shouldBeVisible(null /* starting */));
216 assertTrue(splitScreenPrimary.shouldBeVisible(null /* starting */));
217 assertTrue(splitScreenSecondary.shouldBeVisible(null /* starting */));
218 assertTrue(splitScreenSecondary2.shouldBeVisible(null /* starting */));
219 }
220
221 @Test
222 public void testShouldBeVisible_Finishing() throws Exception {
223 final ActivityDisplay display = mService.mStackSupervisor.getDefaultDisplay();
224 final TestActivityStack homeStack = createStackForShouldBeVisibleTest(display,
225 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, true /* onTop */);
226 final TestActivityStack translucentStack = createStackForShouldBeVisibleTest(display,
227 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
228 translucentStack.setIsTranslucent(true);
229
230 assertTrue(homeStack.shouldBeVisible(null /* starting */));
231 assertTrue(translucentStack.shouldBeVisible(null /* starting */));
232
233 final ActivityRecord topRunningHomeActivity = homeStack.topRunningActivityLocked();
234 topRunningHomeActivity.finishing = true;
235 final ActivityRecord topRunningTranslucentActivity =
236 translucentStack.topRunningActivityLocked();
237 topRunningTranslucentActivity.finishing = true;
238
239 // Home shouldn't be visible since its activity is marked as finishing and it isn't the top
240 // of the stack list.
241 assertFalse(homeStack.shouldBeVisible(null /* starting */));
242 // Home should be visible if we are starting an activity within it.
243 assertTrue(homeStack.shouldBeVisible(topRunningHomeActivity /* starting */));
244 // The translucent stack should be visible since it is the top of the stack list even though
245 // it has its activity marked as finishing.
246 assertTrue(translucentStack.shouldBeVisible(null /* starting */));
247 }
248
249 private <T extends ActivityStack> T createStackForShouldBeVisibleTest(
250 ActivityDisplay display, int windowingMode, int activityType, boolean onTop) {
251 final T stack = display.createStack(windowingMode, activityType, onTop);
Bryce Lee18d51592017-10-25 10:22:19 -0700252 final ActivityRecord r = new ActivityBuilder(mService).setUid(0).setStack(stack)
253 .setCreateTask(true).build();
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700254 return stack;
255 }
Bryce Lee840c5662017-04-13 10:02:51 -0700256}