blob: d045073b5f78b8097c30a4f314a02c276510d763 [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;
24
Andrii Kuliand2765632016-12-12 22:26:34 -080025import static org.junit.Assert.assertEquals;
Yunfan Chen279f5582018-12-12 15:24:50 -080026import static org.junit.Assert.assertNotEquals;
27import static org.junit.Assert.assertNotNull;
Andrii Kuliand2765632016-12-12 22:26:34 -080028import static org.junit.Assert.assertNull;
Yunfan Chen279f5582018-12-12 15:24:50 -080029import static org.junit.Assert.assertTrue;
Brett Chabota26eda92018-07-23 13:08:30 -070030
Yunfan Chen279f5582018-12-12 15:24:50 -080031import android.graphics.Rect;
Brett Chabota26eda92018-07-23 13:08:30 -070032import android.platform.test.annotations.Presubmit;
33
34import androidx.test.filters.SmallTest;
Brett Chabota26eda92018-07-23 13:08:30 -070035
36import org.junit.Test;
Riddle Hsu73f53572019-09-23 23:13:01 +080037import org.junit.runner.RunWith;
Andrii Kuliand2765632016-12-12 22:26:34 -080038
39/**
40 * Tests for the {@link TaskStack} class.
41 *
42 * Build/Install/Run:
Riddle Hsu73f53572019-09-23 23:13:01 +080043 * atest WmTests:TaskStackTests
Andrii Kuliand2765632016-12-12 22:26:34 -080044 */
45@SmallTest
46@Presubmit
Riddle Hsu73f53572019-09-23 23:13:01 +080047@RunWith(WindowTestRunner.class)
Andrii Kuliand2765632016-12-12 22:26:34 -080048public class TaskStackTests extends WindowTestsBase {
49
50 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070051 public void testStackPositionChildAt() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070052 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kuliand2765632016-12-12 22:26:34 -080053 final Task task1 = createTaskInStack(stack, 0 /* userId */);
54 final Task task2 = createTaskInStack(stack, 1 /* userId */);
55
56 // Current user task should be moved to top.
57 stack.positionChildAt(WindowContainer.POSITION_TOP, task1, false /* includingParents */);
58 assertEquals(stack.mChildren.get(0), task2);
59 assertEquals(stack.mChildren.get(1), task1);
60
61 // Non-current user won't be moved to top.
62 stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */);
63 assertEquals(stack.mChildren.get(0), task2);
64 assertEquals(stack.mChildren.get(1), task1);
65 }
Andrii Kulian45a61fe2017-01-05 16:53:19 -080066
67 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070068 public void testClosingAppDifferentStackOrientation() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070069 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080070 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Wale Ogunwale3198da42019-10-10 14:45:03 +020071 ActivityRecord appWindowToken1 =
chaviw97d28202018-02-27 16:23:53 -080072 WindowTestUtils.createTestAppWindowToken(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080073 task1.addChild(appWindowToken1, 0);
74 appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
75
76 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Wale Ogunwale3198da42019-10-10 14:45:03 +020077 ActivityRecord appWindowToken2 =
chaviw97d28202018-02-27 16:23:53 -080078 WindowTestUtils.createTestAppWindowToken(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080079 task2.addChild(appWindowToken2, 0);
80 appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
81
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070082 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
lumark588a3e82018-07-20 18:53:54 +080083 mDisplayContent.mClosingApps.add(appWindowToken2);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070084 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -080085 }
86
87 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070088 public void testMoveTaskToBackDifferentStackOrientation() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070089 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080090 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Wale Ogunwale3198da42019-10-10 14:45:03 +020091 ActivityRecord appWindowToken1 =
chaviw97d28202018-02-27 16:23:53 -080092 WindowTestUtils.createTestAppWindowToken(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080093 task1.addChild(appWindowToken1, 0);
94 appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
95
96 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Wale Ogunwale3198da42019-10-10 14:45:03 +020097 ActivityRecord appWindowToken2 =
chaviw97d28202018-02-27 16:23:53 -080098 WindowTestUtils.createTestAppWindowToken(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080099 task2.addChild(appWindowToken2, 0);
100 appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
101
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700102 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800103 task2.setSendingToBottom(true);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700104 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800105 }
106
107 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700108 public void testStackRemoveImmediately() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700109 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800110 final Task task = createTaskInStack(stack, 0 /* userId */);
111 assertEquals(stack, task.mStack);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800112
113 // Remove stack and check if its child is also removed.
114 stack.removeImmediately();
115 assertNull(stack.getDisplayContent());
116 assertNull(task.mStack);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800117 }
Yunfan Chen279f5582018-12-12 15:24:50 -0800118
119 @Test
120 public void testRemoveContainer() {
121 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
122 final WindowTestUtils.TestTask task = WindowTestUtils.createTestTask(stack);
123
124 assertNotNull(stack);
125 assertNotNull(task);
126 stack.removeIfPossible();
127 // Assert that the container was removed.
128 assertNull(stack.getParent());
129 assertEquals(0, stack.getChildCount());
130 assertNull(stack.getDisplayContent());
131 assertNull(task.getDisplayContent());
132 assertNull(task.mStack);
133 }
134
135 @Test
136 public void testRemoveContainer_deferRemoval() {
137 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
138 final WindowTestUtils.TestTask task = WindowTestUtils.createTestTask(stack);
139
140 // Stack removal is deferred if one of its child is animating.
141 task.setLocalIsAnimating(true);
142
143 stack.removeIfPossible();
144 // For the case of deferred removal the task controller will still be connected to the its
145 // container until the stack window container is removed.
146 assertNotNull(stack.getParent());
147 assertNotEquals(0, stack.getChildCount());
148 assertNotNull(task);
149
150 stack.removeImmediately();
151 // After removing, the task will be isolated.
152 assertNull(task.getParent());
153 assertEquals(0, task.getChildCount());
Yunfan Chen279f5582018-12-12 15:24:50 -0800154 }
155
156 @Test
157 public void testReparent() {
158 // Create first stack on primary display.
159 final TaskStack stack1 = createTaskStackOnDisplay(mDisplayContent);
160 final WindowTestUtils.TestTask task1 = WindowTestUtils.createTestTask(stack1);
161 task1.mOnDisplayChangedCalled = false;
162
163 // Create second display and put second stack on it.
164 final DisplayContent dc = createNewDisplay();
165 final TaskStack stack2 = createTaskStackOnDisplay(dc);
166
167 // Reparent
168 stack1.reparent(dc.getDisplayId(), new Rect(), true /* onTop */);
169 assertEquals(dc, stack1.getDisplayContent());
170 final int stack1PositionInParent = stack1.getParent().mChildren.indexOf(stack1);
171 final int stack2PositionInParent = stack1.getParent().mChildren.indexOf(stack2);
172 assertEquals(stack1PositionInParent, stack2PositionInParent + 1);
173 assertTrue(task1.mOnDisplayChangedCalled);
174 }
Riddle Hsu6619acb2019-02-20 19:12:57 +0800175
176 @Test
177 public void testStackOutset() {
178 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Riddle Hsu6619acb2019-02-20 19:12:57 +0800179 final int stackOutset = 10;
Riddle Hsu73f53572019-09-23 23:13:01 +0800180 spyOn(stack);
181 doReturn(stackOutset).when(stack).getStackOutset();
Riddle Hsu6619acb2019-02-20 19:12:57 +0800182
183 final Rect stackBounds = new Rect(200, 200, 800, 1000);
184 // Update surface position and size by the given bounds.
185 stack.setBounds(stackBounds);
186
187 assertEquals(stackBounds.width() + 2 * stackOutset, stack.getLastSurfaceSize().x);
188 assertEquals(stackBounds.height() + 2 * stackOutset, stack.getLastSurfaceSize().y);
189 assertEquals(stackBounds.left - stackOutset, stack.getLastSurfacePosition().x);
190 assertEquals(stackBounds.top - stackOutset, stack.getLastSurfacePosition().y);
191 }
Andrii Kuliand2765632016-12-12 22:26:34 -0800192}