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; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Tests for the {@link TaskStack} class. |
| 40 | * |
| 41 | * Build/Install/Run: |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 42 | * atest FrameworksServicesTests:TaskStackTests |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 43 | */ |
| 44 | @SmallTest |
| 45 | @Presubmit |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 46 | public class TaskStackTests extends WindowTestsBase { |
| 47 | |
| 48 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 49 | public void testStackPositionChildAt() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 50 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 51 | 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 Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 64 | |
| 65 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 66 | public void testClosingAppDifferentStackOrientation() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 67 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 68 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 69 | WindowTestUtils.TestAppWindowToken appWindowToken1 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 70 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 71 | task1.addChild(appWindowToken1, 0); |
| 72 | appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
| 73 | |
| 74 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 75 | WindowTestUtils.TestAppWindowToken appWindowToken2 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 76 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 77 | task2.addChild(appWindowToken2, 0); |
| 78 | appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
| 79 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 80 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 81 | mDisplayContent.mClosingApps.add(appWindowToken2); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 82 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 86 | public void testMoveTaskToBackDifferentStackOrientation() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 87 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 88 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 89 | WindowTestUtils.TestAppWindowToken appWindowToken1 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 90 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 91 | task1.addChild(appWindowToken1, 0); |
| 92 | appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
| 93 | |
| 94 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 95 | WindowTestUtils.TestAppWindowToken appWindowToken2 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 96 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 97 | task2.addChild(appWindowToken2, 0); |
| 98 | appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
| 99 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 100 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 101 | task2.setSendingToBottom(true); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 102 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 106 | public void testStackRemoveImmediately() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 107 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 108 | final Task task = createTaskInStack(stack, 0 /* userId */); |
| 109 | assertEquals(stack, task.mStack); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 110 | |
| 111 | // Remove stack and check if its child is also removed. |
| 112 | stack.removeImmediately(); |
| 113 | assertNull(stack.getDisplayContent()); |
| 114 | assertNull(task.mStack); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 115 | } |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 116 | |
| 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 Hsu | 6619acb | 2019-02-20 19:12:57 +0800 | [diff] [blame^] | 174 | |
| 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 Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 192 | } |