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; |
| 24 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 26 | import static org.junit.Assert.assertNotEquals; |
| 27 | import static org.junit.Assert.assertNotNull; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 28 | import static org.junit.Assert.assertNull; |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 29 | import static org.junit.Assert.assertTrue; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 30 | |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 31 | import android.graphics.Rect; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 32 | import android.platform.test.annotations.Presubmit; |
| 33 | |
| 34 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 35 | |
| 36 | import org.junit.Test; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 37 | import org.junit.runner.RunWith; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Tests for the {@link TaskStack} class. |
| 41 | * |
| 42 | * Build/Install/Run: |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 43 | * atest WmTests:TaskStackTests |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 44 | */ |
| 45 | @SmallTest |
| 46 | @Presubmit |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 47 | @RunWith(WindowTestRunner.class) |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 48 | public class TaskStackTests extends WindowTestsBase { |
| 49 | |
| 50 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 51 | public void testStackPositionChildAt() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 52 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 53 | 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 Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 66 | |
| 67 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 68 | public void testClosingAppDifferentStackOrientation() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 69 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 70 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 71 | AppWindowToken appWindowToken1 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 72 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 73 | task1.addChild(appWindowToken1, 0); |
| 74 | appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
| 75 | |
| 76 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 77 | AppWindowToken appWindowToken2 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 78 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 79 | task2.addChild(appWindowToken2, 0); |
| 80 | appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
| 81 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 82 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 83 | mDisplayContent.mClosingApps.add(appWindowToken2); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 84 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 88 | public void testMoveTaskToBackDifferentStackOrientation() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 89 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 90 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 91 | AppWindowToken appWindowToken1 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 92 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 93 | task1.addChild(appWindowToken1, 0); |
| 94 | appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
| 95 | |
| 96 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Wale Ogunwale | da8b827 | 2018-11-29 19:37:37 -0800 | [diff] [blame] | 97 | AppWindowToken appWindowToken2 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 98 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 99 | task2.addChild(appWindowToken2, 0); |
| 100 | appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
| 101 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 102 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 103 | task2.setSendingToBottom(true); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 104 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 108 | public void testStackRemoveImmediately() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 109 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 110 | final Task task = createTaskInStack(stack, 0 /* userId */); |
| 111 | assertEquals(stack, task.mStack); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 112 | |
| 113 | // Remove stack and check if its child is also removed. |
| 114 | stack.removeImmediately(); |
| 115 | assertNull(stack.getDisplayContent()); |
| 116 | assertNull(task.mStack); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 117 | } |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 118 | |
| 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 Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 154 | } |
| 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 Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 175 | |
| 176 | @Test |
| 177 | public void testStackOutset() { |
| 178 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 179 | final int stackOutset = 10; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 180 | spyOn(stack); |
| 181 | doReturn(stackOutset).when(stack).getStackOutset(); |
Riddle Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame] | 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 Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 192 | } |