blob: d48e82723295d17f8f5f805beabc145efc478338 [file] [log] [blame]
Andrii Kuliand2765632016-12-12 22:26:34 -08001/*
2 * Copyright (C) 2016 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
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070014 * limitations under the License.
Andrii Kuliand2765632016-12-12 22:26:34 -080015 */
16
17package com.android.server.wm;
18
Wale Ogunwaleea5e87f2020-02-10 08:33:05 -080019import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
Bryce Lee61fbcbc2017-03-10 14:14:03 -080020import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
21import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
22
Wale Ogunwaleea5e87f2020-02-10 08:33:05 -080023import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
Riddle Hsu6619acb2019-02-20 19:12:57 +080024import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
25import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
Wale Ogunwalea733c792019-10-16 21:31:15 -070026import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
27import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
lumark9bca6b42019-10-17 18:35:22 +080028import static com.android.server.wm.WindowContainer.AnimationFlags.CHILDREN;
29import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION;
Riddle Hsu6619acb2019-02-20 19:12:57 +080030
Andrii Kuliand2765632016-12-12 22:26:34 -080031import static org.junit.Assert.assertEquals;
Yunfan Chen279f5582018-12-12 15:24:50 -080032import static org.junit.Assert.assertNotEquals;
33import static org.junit.Assert.assertNotNull;
Andrii Kuliand2765632016-12-12 22:26:34 -080034import static org.junit.Assert.assertNull;
Wale Ogunwalea733c792019-10-16 21:31:15 -070035import static org.mockito.ArgumentMatchers.any;
lumarkbde15132019-12-18 22:29:43 +080036import static org.mockito.Mockito.clearInvocations;
Brett Chabota26eda92018-07-23 13:08:30 -070037
Wale Ogunwaleea5e87f2020-02-10 08:33:05 -080038import android.app.WindowConfiguration;
Yunfan Chen279f5582018-12-12 15:24:50 -080039import android.graphics.Rect;
Brett Chabota26eda92018-07-23 13:08:30 -070040import android.platform.test.annotations.Presubmit;
41
42import androidx.test.filters.SmallTest;
Brett Chabota26eda92018-07-23 13:08:30 -070043
44import org.junit.Test;
Riddle Hsu73f53572019-09-23 23:13:01 +080045import org.junit.runner.RunWith;
Andrii Kuliand2765632016-12-12 22:26:34 -080046
47/**
Louis Changdc077272019-11-12 16:52:56 +080048 * Tests for the {@link ActivityStack} class.
Andrii Kuliand2765632016-12-12 22:26:34 -080049 *
50 * Build/Install/Run:
Riddle Hsu73f53572019-09-23 23:13:01 +080051 * atest WmTests:TaskStackTests
Andrii Kuliand2765632016-12-12 22:26:34 -080052 */
53@SmallTest
54@Presubmit
Riddle Hsu73f53572019-09-23 23:13:01 +080055@RunWith(WindowTestRunner.class)
Andrii Kuliand2765632016-12-12 22:26:34 -080056public class TaskStackTests extends WindowTestsBase {
57
58 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070059 public void testStackPositionChildAt() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070060 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kuliand2765632016-12-12 22:26:34 -080061 final Task task1 = createTaskInStack(stack, 0 /* userId */);
62 final Task task2 = createTaskInStack(stack, 1 /* userId */);
63
64 // Current user task should be moved to top.
Louis Changcdec0802019-11-11 11:45:07 +080065 stack.positionChildAt(WindowContainer.POSITION_TOP, task1, false /* includingParents */);
Andrii Kuliand2765632016-12-12 22:26:34 -080066 assertEquals(stack.mChildren.get(0), task2);
67 assertEquals(stack.mChildren.get(1), task1);
68
69 // Non-current user won't be moved to top.
Louis Changcdec0802019-11-11 11:45:07 +080070 stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */);
Andrii Kuliand2765632016-12-12 22:26:34 -080071 assertEquals(stack.mChildren.get(0), task2);
72 assertEquals(stack.mChildren.get(1), task1);
73 }
Andrii Kulian45a61fe2017-01-05 16:53:19 -080074
75 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070076 public void testClosingAppDifferentStackOrientation() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070077 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080078 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070079 ActivityRecord activity1 =
80 WindowTestUtils.createTestActivityRecord(mDisplayContent);
81 task1.addChild(activity1, 0);
82 activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080083
84 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070085 ActivityRecord activity2=
86 WindowTestUtils.createTestActivityRecord(mDisplayContent);
87 task2.addChild(activity2, 0);
88 activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080089
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070090 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
Garfield Tane8d84ab2019-10-11 09:49:40 -070091 mDisplayContent.mClosingApps.add(activity2);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070092 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -080093 }
94
95 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070096 public void testMoveTaskToBackDifferentStackOrientation() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070097 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080098 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070099 ActivityRecord activity1 =
100 WindowTestUtils.createTestActivityRecord(mDisplayContent);
101 task1.addChild(activity1, 0);
102 activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800103
104 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -0700105 ActivityRecord activity2 =
106 WindowTestUtils.createTestActivityRecord(mDisplayContent);
107 task2.addChild(activity2, 0);
108 activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700109 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800110 }
111
112 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700113 public void testStackRemoveImmediately() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700114 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800115 final Task task = createTaskInStack(stack, 0 /* userId */);
Wale Ogunwale0b3d2922019-12-30 08:55:07 -0800116 assertEquals(stack, task.getStack());
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800117
118 // Remove stack and check if its child is also removed.
119 stack.removeImmediately();
120 assertNull(stack.getDisplayContent());
Wale Ogunwale8f93b642019-12-26 12:10:52 -0800121 assertNull(task.getParent());
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800122 }
Yunfan Chen279f5582018-12-12 15:24:50 -0800123
124 @Test
125 public void testRemoveContainer() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700126 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700127 final Task task = createTaskInStack(stack, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800128
129 assertNotNull(stack);
130 assertNotNull(task);
131 stack.removeIfPossible();
132 // Assert that the container was removed.
133 assertNull(stack.getParent());
134 assertEquals(0, stack.getChildCount());
135 assertNull(stack.getDisplayContent());
136 assertNull(task.getDisplayContent());
Wale Ogunwale8f93b642019-12-26 12:10:52 -0800137 assertNull(task.getParent());
Yunfan Chen279f5582018-12-12 15:24:50 -0800138 }
139
140 @Test
141 public void testRemoveContainer_deferRemoval() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700142 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700143 final Task task = createTaskInStack(stack, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800144
145 // Stack removal is deferred if one of its child is animating.
Wale Ogunwale8f93b642019-12-26 12:10:52 -0800146 doReturn(true).when(stack).hasWindowsAlive();
lumark9bca6b42019-10-17 18:35:22 +0800147 doReturn(true).when(task).isAnimating(TRANSITION | CHILDREN);
Yunfan Chen279f5582018-12-12 15:24:50 -0800148
149 stack.removeIfPossible();
150 // For the case of deferred removal the task controller will still be connected to the its
151 // container until the stack window container is removed.
152 assertNotNull(stack.getParent());
153 assertNotEquals(0, stack.getChildCount());
154 assertNotNull(task);
155
156 stack.removeImmediately();
157 // After removing, the task will be isolated.
158 assertNull(task.getParent());
159 assertEquals(0, task.getChildCount());
Yunfan Chen279f5582018-12-12 15:24:50 -0800160 }
161
162 @Test
163 public void testReparent() {
164 // Create first stack on primary display.
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700165 final ActivityStack stack1 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700166 final Task task1 = createTaskInStack(stack1, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800167
168 // Create second display and put second stack on it.
169 final DisplayContent dc = createNewDisplay();
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700170 final ActivityStack stack2 = createTaskStackOnDisplay(dc);
Yunfan Chen279f5582018-12-12 15:24:50 -0800171
172 // Reparent
lumarkbde15132019-12-18 22:29:43 +0800173 clearInvocations(task1); // reset the number of onDisplayChanged for task.
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700174 stack1.reparent(dc.getDefaultTaskDisplayArea(), true /* onTop */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800175 assertEquals(dc, stack1.getDisplayContent());
176 final int stack1PositionInParent = stack1.getParent().mChildren.indexOf(stack1);
177 final int stack2PositionInParent = stack1.getParent().mChildren.indexOf(stack2);
178 assertEquals(stack1PositionInParent, stack2PositionInParent + 1);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700179 verify(task1, times(1)).onDisplayChanged(any());
Yunfan Chen279f5582018-12-12 15:24:50 -0800180 }
Riddle Hsu6619acb2019-02-20 19:12:57 +0800181
182 @Test
183 public void testStackOutset() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700184 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Riddle Hsu6619acb2019-02-20 19:12:57 +0800185 final int stackOutset = 10;
Riddle Hsu73f53572019-09-23 23:13:01 +0800186 spyOn(stack);
187 doReturn(stackOutset).when(stack).getStackOutset();
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700188 doReturn(true).when(stack).inMultiWindowMode();
Riddle Hsu6619acb2019-02-20 19:12:57 +0800189
Wale Ogunwaleea5e87f2020-02-10 08:33:05 -0800190 // Mock the resolved override windowing mode to non-fullscreen
191 final WindowConfiguration windowConfiguration =
192 stack.getResolvedOverrideConfiguration().windowConfiguration;
193 spyOn(windowConfiguration);
194 doReturn(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY)
195 .when(windowConfiguration).getWindowingMode();
196
197 // Prevent adjust task dimensions
198 doNothing().when(stack).adjustForMinimalTaskDimensions(any(), any());
199
Riddle Hsu6619acb2019-02-20 19:12:57 +0800200 final Rect stackBounds = new Rect(200, 200, 800, 1000);
201 // Update surface position and size by the given bounds.
202 stack.setBounds(stackBounds);
203
204 assertEquals(stackBounds.width() + 2 * stackOutset, stack.getLastSurfaceSize().x);
205 assertEquals(stackBounds.height() + 2 * stackOutset, stack.getLastSurfaceSize().y);
206 assertEquals(stackBounds.left - stackOutset, stack.getLastSurfacePosition().x);
207 assertEquals(stackBounds.top - stackOutset, stack.getLastSurfacePosition().y);
208 }
Andrii Kuliand2765632016-12-12 22:26:34 -0800209}