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