blob: 2fc03c7f13d3a59f75f283f4f9848ffe7b193bcc [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;
Riddle Hsu6619acb2019-02-20 19:12:57 +080026
Andrii Kuliand2765632016-12-12 22:26:34 -080027import static org.junit.Assert.assertEquals;
Yunfan Chen279f5582018-12-12 15:24:50 -080028import static org.junit.Assert.assertNotEquals;
29import static org.junit.Assert.assertNotNull;
Andrii Kuliand2765632016-12-12 22:26:34 -080030import static org.junit.Assert.assertNull;
Yunfan Chen279f5582018-12-12 15:24:50 -080031import static org.junit.Assert.assertTrue;
Wale Ogunwalea733c792019-10-16 21:31:15 -070032import static org.mockito.ArgumentMatchers.any;
Brett Chabota26eda92018-07-23 13:08:30 -070033
Yunfan Chen279f5582018-12-12 15:24:50 -080034import android.graphics.Rect;
Brett Chabota26eda92018-07-23 13:08:30 -070035import android.platform.test.annotations.Presubmit;
36
37import androidx.test.filters.SmallTest;
Brett Chabota26eda92018-07-23 13:08:30 -070038
39import org.junit.Test;
Riddle Hsu73f53572019-09-23 23:13:01 +080040import org.junit.runner.RunWith;
Andrii Kuliand2765632016-12-12 22:26:34 -080041
42/**
43 * Tests for the {@link TaskStack} class.
44 *
45 * Build/Install/Run:
Riddle Hsu73f53572019-09-23 23:13:01 +080046 * atest WmTests:TaskStackTests
Andrii Kuliand2765632016-12-12 22:26:34 -080047 */
48@SmallTest
49@Presubmit
Riddle Hsu73f53572019-09-23 23:13:01 +080050@RunWith(WindowTestRunner.class)
Andrii Kuliand2765632016-12-12 22:26:34 -080051public class TaskStackTests extends WindowTestsBase {
52
53 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070054 public void testStackPositionChildAt() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070055 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kuliand2765632016-12-12 22:26:34 -080056 final Task task1 = createTaskInStack(stack, 0 /* userId */);
57 final Task task2 = createTaskInStack(stack, 1 /* userId */);
58
59 // Current user task should be moved to top.
60 stack.positionChildAt(WindowContainer.POSITION_TOP, task1, false /* includingParents */);
61 assertEquals(stack.mChildren.get(0), task2);
62 assertEquals(stack.mChildren.get(1), task1);
63
64 // Non-current user won't be moved to top.
65 stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */);
66 assertEquals(stack.mChildren.get(0), task2);
67 assertEquals(stack.mChildren.get(1), task1);
68 }
Andrii Kulian45a61fe2017-01-05 16:53:19 -080069
70 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070071 public void testClosingAppDifferentStackOrientation() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070072 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080073 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070074 ActivityRecord activity1 =
75 WindowTestUtils.createTestActivityRecord(mDisplayContent);
76 task1.addChild(activity1, 0);
77 activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080078
79 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070080 ActivityRecord activity2=
81 WindowTestUtils.createTestActivityRecord(mDisplayContent);
82 task2.addChild(activity2, 0);
83 activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080084
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070085 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
Garfield Tane8d84ab2019-10-11 09:49:40 -070086 mDisplayContent.mClosingApps.add(activity2);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070087 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -080088 }
89
90 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070091 public void testMoveTaskToBackDifferentStackOrientation() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070092 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080093 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070094 ActivityRecord activity1 =
95 WindowTestUtils.createTestActivityRecord(mDisplayContent);
96 task1.addChild(activity1, 0);
97 activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080098
99 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -0700100 ActivityRecord activity2 =
101 WindowTestUtils.createTestActivityRecord(mDisplayContent);
102 task2.addChild(activity2, 0);
103 activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800104
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700105 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800106 task2.setSendingToBottom(true);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700107 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800108 }
109
110 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700111 public void testStackRemoveImmediately() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700112 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800113 final Task task = createTaskInStack(stack, 0 /* userId */);
114 assertEquals(stack, task.mStack);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800115
116 // Remove stack and check if its child is also removed.
117 stack.removeImmediately();
118 assertNull(stack.getDisplayContent());
119 assertNull(task.mStack);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800120 }
Yunfan Chen279f5582018-12-12 15:24:50 -0800121
122 @Test
123 public void testRemoveContainer() {
124 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700125 final Task task = createTaskInStack(stack, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800126
127 assertNotNull(stack);
128 assertNotNull(task);
129 stack.removeIfPossible();
130 // Assert that the container was removed.
131 assertNull(stack.getParent());
132 assertEquals(0, stack.getChildCount());
133 assertNull(stack.getDisplayContent());
134 assertNull(task.getDisplayContent());
135 assertNull(task.mStack);
136 }
137
138 @Test
139 public void testRemoveContainer_deferRemoval() {
140 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700141 final Task task = createTaskInStack(stack, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800142
143 // Stack removal is deferred if one of its child is animating.
Wale Ogunwalea733c792019-10-16 21:31:15 -0700144 doReturn(true).when(task).isSelfAnimating();
Yunfan Chen279f5582018-12-12 15:24:50 -0800145
146 stack.removeIfPossible();
147 // For the case of deferred removal the task controller will still be connected to the its
148 // container until the stack window container is removed.
149 assertNotNull(stack.getParent());
150 assertNotEquals(0, stack.getChildCount());
151 assertNotNull(task);
152
153 stack.removeImmediately();
154 // After removing, the task will be isolated.
155 assertNull(task.getParent());
156 assertEquals(0, task.getChildCount());
Yunfan Chen279f5582018-12-12 15:24:50 -0800157 }
158
159 @Test
160 public void testReparent() {
161 // Create first stack on primary display.
162 final TaskStack stack1 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700163 final Task task1 = createTaskInStack(stack1, 0 /* userId */);
Yunfan Chen279f5582018-12-12 15:24:50 -0800164
165 // Create second display and put second stack on it.
166 final DisplayContent dc = createNewDisplay();
167 final TaskStack stack2 = createTaskStackOnDisplay(dc);
168
169 // Reparent
170 stack1.reparent(dc.getDisplayId(), new Rect(), true /* onTop */);
171 assertEquals(dc, stack1.getDisplayContent());
172 final int stack1PositionInParent = stack1.getParent().mChildren.indexOf(stack1);
173 final int stack2PositionInParent = stack1.getParent().mChildren.indexOf(stack2);
174 assertEquals(stack1PositionInParent, stack2PositionInParent + 1);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700175 verify(task1, times(1)).onDisplayChanged(any());
Yunfan Chen279f5582018-12-12 15:24:50 -0800176 }
Riddle Hsu6619acb2019-02-20 19:12:57 +0800177
178 @Test
179 public void testStackOutset() {
180 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Riddle Hsu6619acb2019-02-20 19:12:57 +0800181 final int stackOutset = 10;
Riddle Hsu73f53572019-09-23 23:13:01 +0800182 spyOn(stack);
183 doReturn(stackOutset).when(stack).getStackOutset();
Riddle Hsu6619acb2019-02-20 19:12:57 +0800184
185 final Rect stackBounds = new Rect(200, 200, 800, 1000);
186 // Update surface position and size by the given bounds.
187 stack.setBounds(stackBounds);
188
189 assertEquals(stackBounds.width() + 2 * stackOutset, stack.getLastSurfaceSize().x);
190 assertEquals(stackBounds.height() + 2 * stackOutset, stack.getLastSurfaceSize().y);
191 assertEquals(stackBounds.left - stackOutset, stack.getLastSurfacePosition().x);
192 assertEquals(stackBounds.top - stackOutset, stack.getLastSurfacePosition().y);
193 }
Andrii Kuliand2765632016-12-12 22:26:34 -0800194}