blob: e17e51bb9da347af8426be182edab7019cdcd390 [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;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070026import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_DESTROYING;
Bryce Lee5daa3122017-04-19 10:40:42 -070027import static org.junit.Assert.assertEquals;
David Stevens18abd0e2017-08-17 14:55:47 -070028import static org.junit.Assert.assertFalse;
Bryce Lee840c5662017-04-13 10:02:51 -070029import static org.junit.Assert.assertNotNull;
30import static org.junit.Assert.assertNull;
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -070031import static org.junit.Assert.assertTrue;
Bryce Lee840c5662017-04-13 10:02:51 -070032
33import android.content.ComponentName;
Bryce Lee3345c4e2017-04-25 07:40:41 -070034import android.content.pm.ActivityInfo;
Bryce Lee9f6affd2017-09-01 09:18:35 -070035import android.os.UserHandle;
Bryce Lee840c5662017-04-13 10:02:51 -070036import android.platform.test.annotations.Presubmit;
37import android.support.test.filters.SmallTest;
38import android.support.test.runner.AndroidJUnit4;
39
40import org.junit.runner.RunWith;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070041import org.junit.Before;
Bryce Lee840c5662017-04-13 10:02:51 -070042import org.junit.Test;
43
44/**
45 * Tests for the {@link ActivityStack} class.
46 *
47 * Build/Install/Run:
48 * bit FrameworksServicesTests:com.android.server.am.ActivityStackTests
49 */
50@SmallTest
51@Presubmit
52@RunWith(AndroidJUnit4.class)
53public class ActivityStackTests extends ActivityTestsBase {
Bryce Lee04ab3462017-04-10 15:06:33 -070054 private static final ComponentName testActivityComponent =
Bryce Lee840c5662017-04-13 10:02:51 -070055 ComponentName.unflattenFromString("com.foo/.BarActivity");
Bryce Lee9f6affd2017-09-01 09:18:35 -070056 private static final ComponentName testOverlayComponent =
57 ComponentName.unflattenFromString("com.foo/.OverlayActivity");
Bryce Lee840c5662017-04-13 10:02:51 -070058
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070059 private ActivityManagerService mService;
60 private ActivityStackSupervisor mSupervisor;
61 private ActivityStack mStack;
62 private TaskRecord mTask;
63
64 @Before
65 @Override
66 public void setUp() throws Exception {
67 super.setUp();
68
69 mService = createActivityManagerService();
70 mSupervisor = mService.mStackSupervisor;
71 mStack = mService.mStackSupervisor.getDefaultDisplay().createStack(
72 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
73 mTask = createTask(mSupervisor, testActivityComponent, mStack);
74 }
75
Bryce Lee840c5662017-04-13 10:02:51 -070076 @Test
77 public void testEmptyTaskCleanupOnRemove() throws Exception {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070078 assertNotNull(mTask.getWindowContainerController());
79 mStack.removeTask(mTask, "testEmptyTaskCleanupOnRemove", REMOVE_TASK_MODE_DESTROYING);
80 assertNull(mTask.getWindowContainerController());
Bryce Lee840c5662017-04-13 10:02:51 -070081 }
Bryce Lee5daa3122017-04-19 10:40:42 -070082
Bryce Lee840c5662017-04-13 10:02:51 -070083 @Test
84 public void testOccupiedTaskCleanupOnRemove() throws Exception {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070085 final ActivityRecord r = createActivity(mService, testActivityComponent, mTask);
86 assertNotNull(mTask.getWindowContainerController());
87 mStack.removeTask(mTask, "testOccupiedTaskCleanupOnRemove", REMOVE_TASK_MODE_DESTROYING);
88 assertNotNull(mTask.getWindowContainerController());
Bryce Lee840c5662017-04-13 10:02:51 -070089 }
Bryce Lee5daa3122017-04-19 10:40:42 -070090
91 @Test
92 public void testNoPauseDuringResumeTopActivity() throws Exception {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070093 final ActivityRecord r = createActivity(mService, testActivityComponent, mTask);
Bryce Lee5daa3122017-04-19 10:40:42 -070094
95 // Simulate the a resumed activity set during
96 // {@link ActivityStack#resumeTopActivityUncheckedLocked}.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070097 mSupervisor.inResumeTopActivity = true;
98 mStack.mResumedActivity = r;
Bryce Lee5daa3122017-04-19 10:40:42 -070099
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700100 final boolean waiting = mStack.goToSleepIfPossible(false);
Bryce Lee5daa3122017-04-19 10:40:42 -0700101
102 // Ensure we report not being ready for sleep.
David Stevens18abd0e2017-08-17 14:55:47 -0700103 assertFalse(waiting);
Bryce Lee5daa3122017-04-19 10:40:42 -0700104
105 // Make sure the resumed activity is untouched.
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700106 assertEquals(mStack.mResumedActivity, r);
Bryce Lee5daa3122017-04-19 10:40:42 -0700107 }
Bryce Lee3345c4e2017-04-25 07:40:41 -0700108
109 @Test
110 public void testStopActivityWhenActivityDestroyed() throws Exception {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700111 final ActivityRecord r = createActivity(mService, testActivityComponent, mTask);
112 r.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
113 mSupervisor.setFocusStackUnchecked("testStopActivityWithDestroy", mStack);
114 mStack.stopActivityLocked(r);
115 // Mostly testing to make sure there is a crash in the call part, so if we get here we are
116 // good-to-go!
Bryce Lee3345c4e2017-04-25 07:40:41 -0700117 }
Bryce Lee9f6affd2017-09-01 09:18:35 -0700118
119 @Test
120 public void testFindTaskWithOverlay() throws Exception {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700121 final ActivityRecord r = createActivity(mService, testActivityComponent, mTask, 0);
Bryce Lee9f6affd2017-09-01 09:18:35 -0700122 // Overlay must be for a different user to prevent recognizing a matching top activity
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700123 final ActivityRecord taskOverlay = createActivity(mService, testOverlayComponent, mTask,
Bryce Lee9f6affd2017-09-01 09:18:35 -0700124 UserHandle.PER_USER_RANGE * 2);
125 taskOverlay.mTaskOverlay = true;
126
Bryce Lee9f6affd2017-09-01 09:18:35 -0700127 final ActivityStackSupervisor.FindTaskResult result =
128 new ActivityStackSupervisor.FindTaskResult();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700129 mStack.findTaskLocked(r, result);
Bryce Lee9f6affd2017-09-01 09:18:35 -0700130
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700131 assertEquals(mTask.getTopActivity(false /* includeOverlays */), r);
132 assertEquals(mTask.getTopActivity(true /* includeOverlays */), taskOverlay);
Bryce Lee9f6affd2017-09-01 09:18:35 -0700133 assertNotNull(result.r);
134 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700135
136 @Test
137 public void testShouldBeVisible_Fullscreen() throws Exception {
138 final ActivityDisplay display = mService.mStackSupervisor.getDefaultDisplay();
139 final TestActivityStack homeStack = createStackForShouldBeVisibleTest(display,
140 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, true /* onTop */);
141 final ActivityStack pinnedStack = createStackForShouldBeVisibleTest(display,
142 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, true /* onTop */);
143
144 assertTrue(homeStack.shouldBeVisible(null /* starting */));
145 assertTrue(pinnedStack.shouldBeVisible(null /* starting */));
146
147 final TestActivityStack fullscreenStack = createStackForShouldBeVisibleTest(display,
148 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
149 // Home stack shouldn't be visible behind an opaque fullscreen stack, but pinned stack
150 // should be visible since it is always on-top.
151 fullscreenStack.setIsTranslucent(false);
152 assertFalse(homeStack.shouldBeVisible(null /* starting */));
153 assertTrue(pinnedStack.shouldBeVisible(null /* starting */));
154 assertTrue(fullscreenStack.shouldBeVisible(null /* starting */));
155
156 // Home stack should be visible behind a translucent fullscreen stack.
157 fullscreenStack.setIsTranslucent(true);
158 assertTrue(homeStack.shouldBeVisible(null /* starting */));
159 assertTrue(pinnedStack.shouldBeVisible(null /* starting */));
160 }
161
162 @Test
163 public void testShouldBeVisible_SplitScreen() throws Exception {
164 final ActivityDisplay display = mService.mStackSupervisor.getDefaultDisplay();
165 final TestActivityStack homeStack = createStackForShouldBeVisibleTest(display,
166 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, true /* onTop */);
167 final TestActivityStack splitScreenPrimary = createStackForShouldBeVisibleTest(display,
168 WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_STANDARD, true /* onTop */);
169 final TestActivityStack splitScreenSecondary = createStackForShouldBeVisibleTest(display,
170 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, ACTIVITY_TYPE_STANDARD, true /* onTop */);
171
172 // Home stack shouldn't be visible if both halves of split-screen are opaque.
173 splitScreenPrimary.setIsTranslucent(false);
174 splitScreenSecondary.setIsTranslucent(false);
175 assertFalse(homeStack.shouldBeVisible(null /* starting */));
176 assertTrue(splitScreenPrimary.shouldBeVisible(null /* starting */));
177 assertTrue(splitScreenSecondary.shouldBeVisible(null /* starting */));
178
179 // Home stack should be visible if one of the halves of split-screen is translucent.
180 splitScreenPrimary.setIsTranslucent(true);
181 assertTrue(homeStack.shouldBeVisible(null /* starting */));
182 assertTrue(splitScreenPrimary.shouldBeVisible(null /* starting */));
183 assertTrue(splitScreenSecondary.shouldBeVisible(null /* starting */));
184
185 final TestActivityStack splitScreenSecondary2 = createStackForShouldBeVisibleTest(display,
186 WINDOWING_MODE_SPLIT_SCREEN_SECONDARY, ACTIVITY_TYPE_STANDARD, true /* onTop */);
187 // First split-screen secondary shouldn't be visible behind another opaque split-split
188 // secondary.
189 splitScreenSecondary2.setIsTranslucent(false);
190 assertFalse(splitScreenSecondary.shouldBeVisible(null /* starting */));
191 assertTrue(splitScreenSecondary2.shouldBeVisible(null /* starting */));
192
193 // First split-screen secondary should be visible behind another translucent split-split
194 // secondary.
195 splitScreenSecondary2.setIsTranslucent(true);
196 assertTrue(splitScreenSecondary.shouldBeVisible(null /* starting */));
197 assertTrue(splitScreenSecondary2.shouldBeVisible(null /* starting */));
198
199 final TestActivityStack assistantStack = createStackForShouldBeVisibleTest(display,
200 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_ASSISTANT, true /* onTop */);
201
202 // Split-screen stacks shouldn't be visible behind an opaque fullscreen stack.
203 assistantStack.setIsTranslucent(false);
204 assertTrue(assistantStack.shouldBeVisible(null /* starting */));
205 assertFalse(splitScreenPrimary.shouldBeVisible(null /* starting */));
206 assertFalse(splitScreenSecondary.shouldBeVisible(null /* starting */));
207 assertFalse(splitScreenSecondary2.shouldBeVisible(null /* starting */));
208
209 // Split-screen stacks should be visible behind a translucent fullscreen stack.
210 assistantStack.setIsTranslucent(true);
211 assertTrue(assistantStack.shouldBeVisible(null /* starting */));
212 assertTrue(splitScreenPrimary.shouldBeVisible(null /* starting */));
213 assertTrue(splitScreenSecondary.shouldBeVisible(null /* starting */));
214 assertTrue(splitScreenSecondary2.shouldBeVisible(null /* starting */));
215 }
216
217 @Test
218 public void testShouldBeVisible_Finishing() throws Exception {
219 final ActivityDisplay display = mService.mStackSupervisor.getDefaultDisplay();
220 final TestActivityStack homeStack = createStackForShouldBeVisibleTest(display,
221 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, true /* onTop */);
222 final TestActivityStack translucentStack = createStackForShouldBeVisibleTest(display,
223 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
224 translucentStack.setIsTranslucent(true);
225
226 assertTrue(homeStack.shouldBeVisible(null /* starting */));
227 assertTrue(translucentStack.shouldBeVisible(null /* starting */));
228
229 final ActivityRecord topRunningHomeActivity = homeStack.topRunningActivityLocked();
230 topRunningHomeActivity.finishing = true;
231 final ActivityRecord topRunningTranslucentActivity =
232 translucentStack.topRunningActivityLocked();
233 topRunningTranslucentActivity.finishing = true;
234
235 // Home shouldn't be visible since its activity is marked as finishing and it isn't the top
236 // of the stack list.
237 assertFalse(homeStack.shouldBeVisible(null /* starting */));
238 // Home should be visible if we are starting an activity within it.
239 assertTrue(homeStack.shouldBeVisible(topRunningHomeActivity /* starting */));
240 // The translucent stack should be visible since it is the top of the stack list even though
241 // it has its activity marked as finishing.
242 assertTrue(translucentStack.shouldBeVisible(null /* starting */));
243 }
244
245 private <T extends ActivityStack> T createStackForShouldBeVisibleTest(
246 ActivityDisplay display, int windowingMode, int activityType, boolean onTop) {
247 final T stack = display.createStack(windowingMode, activityType, onTop);
248 // Create a task and activity in the stack so that it has a top running activity.
249 final TaskRecord task = createTask(mSupervisor, testActivityComponent, stack);
250 final ActivityRecord r = createActivity(mService, testActivityComponent, task, 0);
251 return stack;
252 }
Bryce Lee840c5662017-04-13 10:02:51 -0700253}