Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 1 | /* |
| 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. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 14 | * limitations under the License. |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Wale Ogunwale | ea5e87f | 2020-02-10 08:33:05 -0800 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY; |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 20 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; |
| 21 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; |
| 22 | |
Wale Ogunwale | ea5e87f | 2020-02-10 08:33:05 -0800 | [diff] [blame] | 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
| 25 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 26 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.times; |
| 27 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 28 | import static com.android.server.wm.WindowContainer.AnimationFlags.CHILDREN; |
| 29 | import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION; |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 30 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 31 | import static org.junit.Assert.assertEquals; |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 32 | import static org.junit.Assert.assertNotEquals; |
| 33 | import static org.junit.Assert.assertNotNull; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 34 | import static org.junit.Assert.assertNull; |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 35 | import static org.mockito.ArgumentMatchers.any; |
chaviw | 9177c77 | 2020-03-24 11:35:22 -0700 | [diff] [blame] | 36 | import static org.mockito.ArgumentMatchers.anyInt; |
| 37 | import static org.mockito.ArgumentMatchers.eq; |
lumark | bde1513 | 2019-12-18 22:29:43 +0800 | [diff] [blame] | 38 | import static org.mockito.Mockito.clearInvocations; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 39 | |
Wale Ogunwale | ea5e87f | 2020-02-10 08:33:05 -0800 | [diff] [blame] | 40 | import android.app.WindowConfiguration; |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 41 | import android.graphics.Rect; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 42 | import android.platform.test.annotations.Presubmit; |
| 43 | |
| 44 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 45 | |
| 46 | import org.junit.Test; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 47 | import org.junit.runner.RunWith; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
Louis Chang | dc07727 | 2019-11-12 16:52:56 +0800 | [diff] [blame] | 50 | * Tests for the {@link ActivityStack} class. |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 51 | * |
| 52 | * Build/Install/Run: |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 53 | * atest WmTests:TaskStackTests |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 54 | */ |
| 55 | @SmallTest |
| 56 | @Presubmit |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 57 | @RunWith(WindowTestRunner.class) |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 58 | public class TaskStackTests extends WindowTestsBase { |
| 59 | |
| 60 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 61 | public void testStackPositionChildAt() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 62 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 63 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
| 64 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
| 65 | |
| 66 | // Current user task should be moved to top. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 67 | stack.positionChildAt(WindowContainer.POSITION_TOP, task1, false /* includingParents */); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 68 | assertEquals(stack.mChildren.get(0), task2); |
| 69 | assertEquals(stack.mChildren.get(1), task1); |
| 70 | |
| 71 | // Non-current user won't be moved to top. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 72 | stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 73 | assertEquals(stack.mChildren.get(0), task2); |
| 74 | assertEquals(stack.mChildren.get(1), task1); |
Louis Chang | edbda50 | 2020-04-27 15:51:42 +0800 | [diff] [blame] | 75 | |
| 76 | // Non-leaf task should be moved to top regardless of the user id. |
| 77 | createTaskInStack((ActivityStack) task2, 0 /* userId */); |
| 78 | createTaskInStack((ActivityStack) task2, 1 /* userId */); |
| 79 | stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */); |
| 80 | assertEquals(stack.mChildren.get(0), task1); |
| 81 | assertEquals(stack.mChildren.get(1), task2); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 82 | } |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 83 | |
| 84 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 85 | public void testClosingAppDifferentStackOrientation() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 86 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 87 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 88 | ActivityRecord activity1 = |
| 89 | WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 90 | task1.addChild(activity1, 0); |
| 91 | activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 92 | |
| 93 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 94 | ActivityRecord activity2= |
| 95 | WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 96 | task2.addChild(activity2, 0); |
| 97 | activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 98 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 99 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 100 | mDisplayContent.mClosingApps.add(activity2); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 101 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 105 | public void testMoveTaskToBackDifferentStackOrientation() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 106 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 107 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 108 | ActivityRecord activity1 = |
| 109 | WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 110 | task1.addChild(activity1, 0); |
| 111 | activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 112 | |
| 113 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 114 | ActivityRecord activity2 = |
| 115 | WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 116 | task2.addChild(activity2, 0); |
| 117 | activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 118 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 122 | public void testStackRemoveImmediately() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 123 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 124 | final Task task = createTaskInStack(stack, 0 /* userId */); |
Wale Ogunwale | 0b3d292 | 2019-12-30 08:55:07 -0800 | [diff] [blame] | 125 | assertEquals(stack, task.getStack()); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 126 | |
| 127 | // Remove stack and check if its child is also removed. |
| 128 | stack.removeImmediately(); |
| 129 | assertNull(stack.getDisplayContent()); |
Wale Ogunwale | 8f93b64 | 2019-12-26 12:10:52 -0800 | [diff] [blame] | 130 | assertNull(task.getParent()); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 131 | } |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 132 | |
| 133 | @Test |
| 134 | public void testRemoveContainer() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 135 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 136 | final Task task = createTaskInStack(stack, 0 /* userId */); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 137 | |
| 138 | assertNotNull(stack); |
| 139 | assertNotNull(task); |
| 140 | stack.removeIfPossible(); |
| 141 | // Assert that the container was removed. |
| 142 | assertNull(stack.getParent()); |
| 143 | assertEquals(0, stack.getChildCount()); |
| 144 | assertNull(stack.getDisplayContent()); |
| 145 | assertNull(task.getDisplayContent()); |
Wale Ogunwale | 8f93b64 | 2019-12-26 12:10:52 -0800 | [diff] [blame] | 146 | assertNull(task.getParent()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void testRemoveContainer_deferRemoval() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 151 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 152 | final Task task = createTaskInStack(stack, 0 /* userId */); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 153 | |
| 154 | // Stack removal is deferred if one of its child is animating. |
Wale Ogunwale | 8f93b64 | 2019-12-26 12:10:52 -0800 | [diff] [blame] | 155 | doReturn(true).when(stack).hasWindowsAlive(); |
chaviw | 9177c77 | 2020-03-24 11:35:22 -0700 | [diff] [blame] | 156 | doReturn(true).when(task).isAnimating(eq(TRANSITION | CHILDREN), anyInt()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 157 | |
| 158 | stack.removeIfPossible(); |
| 159 | // For the case of deferred removal the task controller will still be connected to the its |
| 160 | // container until the stack window container is removed. |
| 161 | assertNotNull(stack.getParent()); |
| 162 | assertNotEquals(0, stack.getChildCount()); |
| 163 | assertNotNull(task); |
| 164 | |
| 165 | stack.removeImmediately(); |
| 166 | // After removing, the task will be isolated. |
| 167 | assertNull(task.getParent()); |
| 168 | assertEquals(0, task.getChildCount()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void testReparent() { |
| 173 | // Create first stack on primary display. |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 174 | final ActivityStack stack1 = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 175 | final Task task1 = createTaskInStack(stack1, 0 /* userId */); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 176 | |
| 177 | // Create second display and put second stack on it. |
| 178 | final DisplayContent dc = createNewDisplay(); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 179 | final ActivityStack stack2 = createTaskStackOnDisplay(dc); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 180 | |
| 181 | // Reparent |
lumark | bde1513 | 2019-12-18 22:29:43 +0800 | [diff] [blame] | 182 | clearInvocations(task1); // reset the number of onDisplayChanged for task. |
Andrii Kulian | 4c0fd0d | 2020-03-29 13:32:14 -0700 | [diff] [blame] | 183 | stack1.reparent(dc.getDefaultTaskDisplayArea(), true /* onTop */); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 184 | assertEquals(dc, stack1.getDisplayContent()); |
| 185 | final int stack1PositionInParent = stack1.getParent().mChildren.indexOf(stack1); |
| 186 | final int stack2PositionInParent = stack1.getParent().mChildren.indexOf(stack2); |
| 187 | assertEquals(stack1PositionInParent, stack2PositionInParent + 1); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 188 | verify(task1, times(1)).onDisplayChanged(any()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 189 | } |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 190 | |
| 191 | @Test |
| 192 | public void testStackOutset() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 193 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 194 | final int stackOutset = 10; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 195 | spyOn(stack); |
Louis Chang | 2570e33 | 2020-04-10 11:58:49 +0800 | [diff] [blame] | 196 | doReturn(stackOutset).when(stack).getTaskOutset(); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 197 | doReturn(true).when(stack).inMultiWindowMode(); |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 198 | |
Wale Ogunwale | ea5e87f | 2020-02-10 08:33:05 -0800 | [diff] [blame] | 199 | // Mock the resolved override windowing mode to non-fullscreen |
| 200 | final WindowConfiguration windowConfiguration = |
| 201 | stack.getResolvedOverrideConfiguration().windowConfiguration; |
| 202 | spyOn(windowConfiguration); |
| 203 | doReturn(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY) |
| 204 | .when(windowConfiguration).getWindowingMode(); |
| 205 | |
| 206 | // Prevent adjust task dimensions |
Riddle Hsu | dd49c63 | 2020-04-30 22:39:51 +0800 | [diff] [blame] | 207 | doNothing().when(stack).adjustForMinimalTaskDimensions(any(), any(), any()); |
Wale Ogunwale | ea5e87f | 2020-02-10 08:33:05 -0800 | [diff] [blame] | 208 | |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 209 | final Rect stackBounds = new Rect(200, 200, 800, 1000); |
| 210 | // Update surface position and size by the given bounds. |
| 211 | stack.setBounds(stackBounds); |
| 212 | |
| 213 | assertEquals(stackBounds.width() + 2 * stackOutset, stack.getLastSurfaceSize().x); |
| 214 | assertEquals(stackBounds.height() + 2 * stackOutset, stack.getLastSurfaceSize().y); |
| 215 | assertEquals(stackBounds.left - stackOutset, stack.getLastSurfacePosition().x); |
| 216 | assertEquals(stackBounds.top - stackOutset, stack.getLastSurfacePosition().y); |
| 217 | } |
Evan Rosky | 660b175 | 2020-04-15 18:07:15 -0700 | [diff] [blame] | 218 | |
| 219 | @Test |
| 220 | public void testActivityAndTaskGetsProperType() { |
| 221 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
| 222 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
| 223 | ActivityRecord activity1 = WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 224 | |
| 225 | // First activity should become standard |
| 226 | task1.addChild(activity1, 0); |
| 227 | assertEquals(WindowConfiguration.ACTIVITY_TYPE_STANDARD, activity1.getActivityType()); |
| 228 | assertEquals(WindowConfiguration.ACTIVITY_TYPE_STANDARD, task1.getActivityType()); |
| 229 | |
| 230 | // Second activity should also become standard |
| 231 | ActivityRecord activity2 = WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 232 | task1.addChild(activity2, WindowContainer.POSITION_TOP); |
| 233 | assertEquals(WindowConfiguration.ACTIVITY_TYPE_STANDARD, activity2.getActivityType()); |
| 234 | assertEquals(WindowConfiguration.ACTIVITY_TYPE_STANDARD, task1.getActivityType()); |
| 235 | } |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 236 | } |