blob: b1f942eb22618e9517f82596db7d5d1fba26328e [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;
Andrii Kuliand2765632016-12-12 22:26:34 -080037
38/**
39 * Tests for the {@link TaskStack} class.
40 *
41 * Build/Install/Run:
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070042 * atest FrameworksServicesTests:TaskStackTests
Andrii Kuliand2765632016-12-12 22:26:34 -080043 */
44@SmallTest
45@Presubmit
Andrii Kuliand2765632016-12-12 22:26:34 -080046public class TaskStackTests extends WindowTestsBase {
47
48 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070049 public void testStackPositionChildAt() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070050 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kuliand2765632016-12-12 22:26:34 -080051 final Task task1 = createTaskInStack(stack, 0 /* userId */);
52 final Task task2 = createTaskInStack(stack, 1 /* userId */);
53
54 // Current user task should be moved to top.
55 stack.positionChildAt(WindowContainer.POSITION_TOP, task1, false /* includingParents */);
56 assertEquals(stack.mChildren.get(0), task2);
57 assertEquals(stack.mChildren.get(1), task1);
58
59 // Non-current user won't be moved to top.
60 stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */);
61 assertEquals(stack.mChildren.get(0), task2);
62 assertEquals(stack.mChildren.get(1), task1);
63 }
Andrii Kulian45a61fe2017-01-05 16:53:19 -080064
65 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070066 public void testClosingAppDifferentStackOrientation() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070067 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080068 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -070069 WindowTestUtils.TestAppWindowToken appWindowToken1 =
chaviw97d28202018-02-27 16:23:53 -080070 WindowTestUtils.createTestAppWindowToken(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080071 task1.addChild(appWindowToken1, 0);
72 appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
73
74 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -070075 WindowTestUtils.TestAppWindowToken appWindowToken2 =
chaviw97d28202018-02-27 16:23:53 -080076 WindowTestUtils.createTestAppWindowToken(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080077 task2.addChild(appWindowToken2, 0);
78 appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
79
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070080 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
lumark588a3e82018-07-20 18:53:54 +080081 mDisplayContent.mClosingApps.add(appWindowToken2);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070082 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -080083 }
84
85 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070086 public void testMoveTaskToBackDifferentStackOrientation() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070087 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080088 final Task task1 = createTaskInStack(stack, 0 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -070089 WindowTestUtils.TestAppWindowToken appWindowToken1 =
chaviw97d28202018-02-27 16:23:53 -080090 WindowTestUtils.createTestAppWindowToken(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080091 task1.addChild(appWindowToken1, 0);
92 appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
93
94 final Task task2 = createTaskInStack(stack, 1 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -070095 WindowTestUtils.TestAppWindowToken appWindowToken2 =
chaviw97d28202018-02-27 16:23:53 -080096 WindowTestUtils.createTestAppWindowToken(mDisplayContent);
Bryce Lee61fbcbc2017-03-10 14:14:03 -080097 task2.addChild(appWindowToken2, 0);
98 appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT);
99
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700100 assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800101 task2.setSendingToBottom(true);
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -0700102 assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation());
Bryce Lee61fbcbc2017-03-10 14:14:03 -0800103 }
104
105 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700106 public void testStackRemoveImmediately() {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700107 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800108 final Task task = createTaskInStack(stack, 0 /* userId */);
109 assertEquals(stack, task.mStack);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800110
111 // Remove stack and check if its child is also removed.
112 stack.removeImmediately();
113 assertNull(stack.getDisplayContent());
114 assertNull(task.mStack);
Andrii Kulian45a61fe2017-01-05 16:53:19 -0800115 }
Yunfan Chen279f5582018-12-12 15:24:50 -0800116
117 @Test
118 public void testRemoveContainer() {
119 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
120 final WindowTestUtils.TestTask task = WindowTestUtils.createTestTask(stack);
121
122 assertNotNull(stack);
123 assertNotNull(task);
124 stack.removeIfPossible();
125 // Assert that the container was removed.
126 assertNull(stack.getParent());
127 assertEquals(0, stack.getChildCount());
128 assertNull(stack.getDisplayContent());
129 assertNull(task.getDisplayContent());
130 assertNull(task.mStack);
131 }
132
133 @Test
134 public void testRemoveContainer_deferRemoval() {
135 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
136 final WindowTestUtils.TestTask task = WindowTestUtils.createTestTask(stack);
137
138 // Stack removal is deferred if one of its child is animating.
139 task.setLocalIsAnimating(true);
140
141 stack.removeIfPossible();
142 // For the case of deferred removal the task controller will still be connected to the its
143 // container until the stack window container is removed.
144 assertNotNull(stack.getParent());
145 assertNotEquals(0, stack.getChildCount());
146 assertNotNull(task);
147
148 stack.removeImmediately();
149 // After removing, the task will be isolated.
150 assertNull(task.getParent());
151 assertEquals(0, task.getChildCount());
152 assertNull(task.getController());
153 }
154
155 @Test
156 public void testReparent() {
157 // Create first stack on primary display.
158 final TaskStack stack1 = createTaskStackOnDisplay(mDisplayContent);
159 final WindowTestUtils.TestTask task1 = WindowTestUtils.createTestTask(stack1);
160 task1.mOnDisplayChangedCalled = false;
161
162 // Create second display and put second stack on it.
163 final DisplayContent dc = createNewDisplay();
164 final TaskStack stack2 = createTaskStackOnDisplay(dc);
165
166 // Reparent
167 stack1.reparent(dc.getDisplayId(), new Rect(), true /* onTop */);
168 assertEquals(dc, stack1.getDisplayContent());
169 final int stack1PositionInParent = stack1.getParent().mChildren.indexOf(stack1);
170 final int stack2PositionInParent = stack1.getParent().mChildren.indexOf(stack2);
171 assertEquals(stack1PositionInParent, stack2PositionInParent + 1);
172 assertTrue(task1.mOnDisplayChangedCalled);
173 }
Riddle Hsu6619acb2019-02-20 19:12:57 +0800174
175 @Test
176 public void testStackOutset() {
177 final TaskStack stack = createTaskStackOnDisplay(mDisplayContent);
178 spyOn(stack);
179
180 final int stackOutset = 10;
181 doReturn(stackOutset).when(stack).getStackOutset();
182
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}