blob: 40ce36324578846e9cff691ba37a1aa871c461ac [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
Bryce Lee61fbcbc2017-03-10 14:14:03 -080019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
20import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
21
Riddle Hsu6619acb2019-02-20 19:12:57 +080022import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
23import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
Wale Ogunwalea733c792019-10-16 21:31:15 -070024import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
25import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
lumark9bca6b42019-10-17 18:35:22 +080026import static com.android.server.wm.WindowContainer.AnimationFlags.CHILDREN;
27import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION;
Riddle Hsu6619acb2019-02-20 19:12:57 +080028
Andrii Kuliand2765632016-12-12 22:26:34 -080029import static org.junit.Assert.assertEquals;
Yunfan Chen279f5582018-12-12 15:24:50 -080030import static org.junit.Assert.assertNotEquals;
31import static org.junit.Assert.assertNotNull;
Andrii Kuliand2765632016-12-12 22:26:34 -080032import static org.junit.Assert.assertNull;
Wale Ogunwalea733c792019-10-16 21:31:15 -070033import static org.mockito.ArgumentMatchers.any;
Brett Chabota26eda92018-07-23 13:08:30 -070034
Yunfan Chen279f5582018-12-12 15:24:50 -080035import android.graphics.Rect;
Brett Chabota26eda92018-07-23 13:08:30 -070036import android.platform.test.annotations.Presubmit;
37
38import androidx.test.filters.SmallTest;
Brett Chabota26eda92018-07-23 13:08:30 -070039
40import org.junit.Test;
Riddle Hsu73f53572019-09-23 23:13:01 +080041import org.junit.runner.RunWith;
Andrii Kuliand2765632016-12-12 22:26:34 -080042
43/**
44 * Tests for the {@link TaskStack} class.
45 *
46 * Build/Install/Run:
Riddle Hsu73f53572019-09-23 23:13:01 +080047 * atest WmTests:TaskStackTests
Andrii Kuliand2765632016-12-12 22:26:34 -080048 */
49@SmallTest
50@Presubmit
Riddle Hsu73f53572019-09-23 23:13:01 +080051@RunWith(WindowTestRunner.class)
Andrii Kuliand2765632016-12-12 22:26:34 -080052public class TaskStackTests extends WindowTestsBase {
53
54 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070055 public void testStackPositionChildAt() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070056 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kuliand2765632016-12-12 22:26:34 -080057 final Task task1 = createTaskInStack(stack, 0 /* userId */);
58 final Task task2 = createTaskInStack(stack, 1 /* userId */);
59
60 // Current user task should be moved to top.
Louis Changcdec0802019-11-11 11:45:07 +080061 stack.positionChildAt(WindowContainer.POSITION_TOP, task1, false /* includingParents */);
Andrii Kuliand2765632016-12-12 22:26:34 -080062 assertEquals(stack.mChildren.get(0), task2);
63 assertEquals(stack.mChildren.get(1), task1);
64
65 // Non-current user won't be moved to top.
Louis Changcdec0802019-11-11 11:45:07 +080066 stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */);
Andrii Kuliand2765632016-12-12 22:26:34 -080067 assertEquals(stack.mChildren.get(0), task2);
68 assertEquals(stack.mChildren.get(1), task1);
69 }
Andrii Kulian45a61fe2017-01-05 16:53:19 -080070
71 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070072 public void testClosingAppDifferentStackOrientation() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070073 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080074 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070075 ActivityRecord activity1 =
76 WindowTestUtils.createTestActivityRecord(mDisplayContent);
77 task1.addChild(activity1, 0);
78 activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080079
80 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070081 ActivityRecord activity2=
82 WindowTestUtils.createTestActivityRecord(mDisplayContent);
83 task2.addChild(activity2, 0);
84 activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080085
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070086 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
Garfield Tane8d84ab2019-10-11 09:49:40 -070087 mDisplayContent.mClosingApps.add(activity2);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070088 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -080089 }
90
91 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070092 public void testMoveTaskToBackDifferentStackOrientation() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070093 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080094 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070095 ActivityRecord activity1 =
96 WindowTestUtils.createTestActivityRecord(mDisplayContent);
97 task1.addChild(activity1, 0);
98 activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080099
100 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -0700101 ActivityRecord activity2 =
102 WindowTestUtils.createTestActivityRecord(mDisplayContent);
103 task2.addChild(activity2, 0);
104 activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800105
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700106 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800107 task2.setSendingToBottom(true);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700108 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800109 }
110
111 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700112 public void testStackRemoveImmediately() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700113 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800114 final Task task = createTaskInStack(stack, 0 /* userId */);
Wale Ogunwale8577a052019-10-26 23:22:34 -0700115 assertEquals(stack, task.getTaskStack());
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800116
117 // Remove stack and check if its child is also removed.
118 stack.removeImmediately();
119 assertNull(stack.getDisplayContent());
Wale Ogunwale8577a052019-10-26 23:22:34 -0700120 assertNull(task.getTaskStack());
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800121 }
Yunfan Chen279f5582018-12-12 15:24:50 -0800122
123 @Test
124 public void testRemoveContainer() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700125 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700126 final Task task = createTaskInStack(stack, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800127
128 assertNotNull(stack);
129 assertNotNull(task);
130 stack.removeIfPossible();
131 // Assert that the container was removed.
132 assertNull(stack.getParent());
133 assertEquals(0, stack.getChildCount());
134 assertNull(stack.getDisplayContent());
135 assertNull(task.getDisplayContent());
Wale Ogunwale8577a052019-10-26 23:22:34 -0700136 assertNull(task.getTaskStack());
Yunfan Chen279f5582018-12-12 15:24:50 -0800137 }
138
139 @Test
140 public void testRemoveContainer_deferRemoval() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700141 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700142 final Task task = createTaskInStack(stack, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800143
144 // Stack removal is deferred if one of its child is animating.
lumark9bca6b42019-10-17 18:35:22 +0800145 doReturn(true).when(task).isAnimating(TRANSITION | CHILDREN);
Yunfan Chen279f5582018-12-12 15:24:50 -0800146
147 stack.removeIfPossible();
148 // For the case of deferred removal the task controller will still be connected to the its
149 // container until the stack window container is removed.
150 assertNotNull(stack.getParent());
151 assertNotEquals(0, stack.getChildCount());
152 assertNotNull(task);
153
154 stack.removeImmediately();
155 // After removing, the task will be isolated.
156 assertNull(task.getParent());
157 assertEquals(0, task.getChildCount());
Yunfan Chen279f5582018-12-12 15:24:50 -0800158 }
159
160 @Test
161 public void testReparent() {
162 // Create first stack on primary display.
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700163 final ActivityStack stack1 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700164 final Task task1 = createTaskInStack(stack1, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800165
166 // Create second display and put second stack on it.
167 final DisplayContent dc = createNewDisplay();
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700168 final ActivityStack stack2 = createTaskStackOnDisplay(dc);
Yunfan Chen279f5582018-12-12 15:24:50 -0800169
170 // Reparent
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700171 stack1.reparent(dc, true /* onTop */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800172 assertEquals(dc, stack1.getDisplayContent());
173 final int stack1PositionInParent = stack1.getParent().mChildren.indexOf(stack1);
174 final int stack2PositionInParent = stack1.getParent().mChildren.indexOf(stack2);
175 assertEquals(stack1PositionInParent, stack2PositionInParent + 1);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700176 verify(task1, times(1)).onDisplayChanged(any());
Yunfan Chen279f5582018-12-12 15:24:50 -0800177 }
Riddle Hsu6619acb2019-02-20 19:12:57 +0800178
179 @Test
180 public void testStackOutset() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700181 final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent);
Riddle Hsu6619acb2019-02-20 19:12:57 +0800182 final int stackOutset = 10;
Riddle Hsu73f53572019-09-23 23:13:01 +0800183 spyOn(stack);
184 doReturn(stackOutset).when(stack).getStackOutset();
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700185 doReturn(true).when(stack).inMultiWindowMode();
Riddle Hsu6619acb2019-02-20 19:12:57 +0800186
187 final Rect stackBounds = new Rect(200, 200, 800, 1000);
188 // Update surface position and size by the given bounds.
189 stack.setBounds(stackBounds);
190
191 assertEquals(stackBounds.width() + 2 * stackOutset, stack.getLastSurfaceSize().x);
192 assertEquals(stackBounds.height() + 2 * stackOutset, stack.getLastSurfaceSize().y);
193 assertEquals(stackBounds.left - stackOutset, stack.getLastSurfacePosition().x);
194 assertEquals(stackBounds.top - stackOutset, stack.getLastSurfacePosition().y);
195 }
Andrii Kuliand2765632016-12-12 22:26:34 -0800196}