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 | |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 19 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; |
| 20 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; |
| 21 | |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 22 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
| 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.times; |
| 25 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 26 | import static com.android.server.wm.WindowContainer.AnimationFlags.CHILDREN; |
| 27 | import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION; |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 28 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 29 | import static org.junit.Assert.assertEquals; |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 30 | import static org.junit.Assert.assertNotEquals; |
| 31 | import static org.junit.Assert.assertNotNull; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 32 | import static org.junit.Assert.assertNull; |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 33 | import static org.mockito.ArgumentMatchers.any; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 34 | |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 35 | import android.graphics.Rect; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 36 | import android.platform.test.annotations.Presubmit; |
| 37 | |
| 38 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 39 | |
| 40 | import org.junit.Test; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 41 | import org.junit.runner.RunWith; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Tests for the {@link TaskStack} class. |
| 45 | * |
| 46 | * Build/Install/Run: |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 47 | * atest WmTests:TaskStackTests |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 48 | */ |
| 49 | @SmallTest |
| 50 | @Presubmit |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 51 | @RunWith(WindowTestRunner.class) |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 52 | public class TaskStackTests extends WindowTestsBase { |
| 53 | |
| 54 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 55 | public void testStackPositionChildAt() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 56 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 57 | 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 Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame^] | 61 | stack.positionChildAt(WindowContainer.POSITION_TOP, task1, false /* includingParents */); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 62 | 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 Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame^] | 66 | stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 67 | assertEquals(stack.mChildren.get(0), task2); |
| 68 | assertEquals(stack.mChildren.get(1), task1); |
| 69 | } |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 70 | |
| 71 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 72 | public void testClosingAppDifferentStackOrientation() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 73 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 74 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 75 | ActivityRecord activity1 = |
| 76 | WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 77 | task1.addChild(activity1, 0); |
| 78 | activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 79 | |
| 80 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 81 | ActivityRecord activity2= |
| 82 | WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 83 | task2.addChild(activity2, 0); |
| 84 | activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 85 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 86 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 87 | mDisplayContent.mClosingApps.add(activity2); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 88 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 92 | public void testMoveTaskToBackDifferentStackOrientation() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 93 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 94 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 95 | ActivityRecord activity1 = |
| 96 | WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 97 | task1.addChild(activity1, 0); |
| 98 | activity1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 99 | |
| 100 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 101 | ActivityRecord activity2 = |
| 102 | WindowTestUtils.createTestActivityRecord(mDisplayContent); |
| 103 | task2.addChild(activity2, 0); |
| 104 | activity2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 105 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 106 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 107 | task2.setSendingToBottom(true); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 108 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 112 | public void testStackRemoveImmediately() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 113 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 114 | final Task task = createTaskInStack(stack, 0 /* userId */); |
Wale Ogunwale | 8577a05 | 2019-10-26 23:22:34 -0700 | [diff] [blame] | 115 | assertEquals(stack, task.getTaskStack()); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 116 | |
| 117 | // Remove stack and check if its child is also removed. |
| 118 | stack.removeImmediately(); |
| 119 | assertNull(stack.getDisplayContent()); |
Wale Ogunwale | 8577a05 | 2019-10-26 23:22:34 -0700 | [diff] [blame] | 120 | assertNull(task.getTaskStack()); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 121 | } |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 122 | |
| 123 | @Test |
| 124 | public void testRemoveContainer() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 125 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 126 | final Task task = createTaskInStack(stack, 0 /* userId */); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 127 | |
| 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 Ogunwale | 8577a05 | 2019-10-26 23:22:34 -0700 | [diff] [blame] | 136 | assertNull(task.getTaskStack()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testRemoveContainer_deferRemoval() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 141 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 142 | final Task task = createTaskInStack(stack, 0 /* userId */); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 143 | |
| 144 | // Stack removal is deferred if one of its child is animating. |
lumark | 9bca6b4 | 2019-10-17 18:35:22 +0800 | [diff] [blame] | 145 | doReturn(true).when(task).isAnimating(TRANSITION | CHILDREN); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 146 | |
| 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 Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testReparent() { |
| 162 | // Create first stack on primary display. |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 163 | final ActivityStack stack1 = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 164 | final Task task1 = createTaskInStack(stack1, 0 /* userId */); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 165 | |
| 166 | // Create second display and put second stack on it. |
| 167 | final DisplayContent dc = createNewDisplay(); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 168 | final ActivityStack stack2 = createTaskStackOnDisplay(dc); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 169 | |
| 170 | // Reparent |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 171 | stack1.reparent(dc, true /* onTop */); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 172 | 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 Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 176 | verify(task1, times(1)).onDisplayChanged(any()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 177 | } |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 178 | |
| 179 | @Test |
| 180 | public void testStackOutset() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 181 | final ActivityStack stack = createTaskStackOnDisplay(mDisplayContent); |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 182 | final int stackOutset = 10; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 183 | spyOn(stack); |
| 184 | doReturn(stackOutset).when(stack).getStackOutset(); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 185 | doReturn(true).when(stack).inMultiWindowMode(); |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 186 | |
| 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 Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 196 | } |