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 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 22 | import static org.junit.Assert.assertEquals; |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame^] | 23 | import static org.junit.Assert.assertNotEquals; |
| 24 | import static org.junit.Assert.assertNotNull; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertNull; |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame^] | 26 | import static org.junit.Assert.assertTrue; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 27 | |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame^] | 28 | import android.graphics.Rect; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 29 | import android.platform.test.annotations.Presubmit; |
| 30 | |
| 31 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 32 | |
| 33 | import org.junit.Test; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Tests for the {@link TaskStack} class. |
| 37 | * |
| 38 | * Build/Install/Run: |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 39 | * atest FrameworksServicesTests:TaskStackTests |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 40 | */ |
| 41 | @SmallTest |
| 42 | @Presubmit |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 43 | public class TaskStackTests extends WindowTestsBase { |
| 44 | |
| 45 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 46 | public void testStackPositionChildAt() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 47 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 48 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
| 49 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
| 50 | |
| 51 | // Current user task should be moved to top. |
| 52 | stack.positionChildAt(WindowContainer.POSITION_TOP, task1, false /* includingParents */); |
| 53 | assertEquals(stack.mChildren.get(0), task2); |
| 54 | assertEquals(stack.mChildren.get(1), task1); |
| 55 | |
| 56 | // Non-current user won't be moved to top. |
| 57 | stack.positionChildAt(WindowContainer.POSITION_TOP, task2, false /* includingParents */); |
| 58 | assertEquals(stack.mChildren.get(0), task2); |
| 59 | assertEquals(stack.mChildren.get(1), task1); |
| 60 | } |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 61 | |
| 62 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 63 | public void testClosingAppDifferentStackOrientation() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 64 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 65 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 66 | WindowTestUtils.TestAppWindowToken appWindowToken1 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 67 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 68 | task1.addChild(appWindowToken1, 0); |
| 69 | appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
| 70 | |
| 71 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 72 | WindowTestUtils.TestAppWindowToken appWindowToken2 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 73 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 74 | task2.addChild(appWindowToken2, 0); |
| 75 | appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
| 76 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 77 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 78 | mDisplayContent.mClosingApps.add(appWindowToken2); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 79 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 83 | public void testMoveTaskToBackDifferentStackOrientation() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 84 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 85 | final Task task1 = createTaskInStack(stack, 0 /* userId */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 86 | WindowTestUtils.TestAppWindowToken appWindowToken1 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 87 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 88 | task1.addChild(appWindowToken1, 0); |
| 89 | appWindowToken1.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
| 90 | |
| 91 | final Task task2 = createTaskInStack(stack, 1 /* userId */); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 92 | WindowTestUtils.TestAppWindowToken appWindowToken2 = |
chaviw | 97d2820 | 2018-02-27 16:23:53 -0800 | [diff] [blame] | 93 | WindowTestUtils.createTestAppWindowToken(mDisplayContent); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 94 | task2.addChild(appWindowToken2, 0); |
| 95 | appWindowToken2.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
| 96 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 97 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 98 | task2.setSendingToBottom(true); |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 99 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, stack.getOrientation()); |
Bryce Lee | 61fbcbc | 2017-03-10 14:14:03 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 103 | public void testStackRemoveImmediately() { |
Wale Ogunwale | 11cc516 | 2017-04-25 20:29:13 -0700 | [diff] [blame] | 104 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 105 | final Task task = createTaskInStack(stack, 0 /* userId */); |
| 106 | assertEquals(stack, task.mStack); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 107 | |
| 108 | // Remove stack and check if its child is also removed. |
| 109 | stack.removeImmediately(); |
| 110 | assertNull(stack.getDisplayContent()); |
| 111 | assertNull(task.mStack); |
Andrii Kulian | 45a61fe | 2017-01-05 16:53:19 -0800 | [diff] [blame] | 112 | } |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame^] | 113 | |
| 114 | @Test |
| 115 | public void testRemoveContainer() { |
| 116 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
| 117 | final WindowTestUtils.TestTask task = WindowTestUtils.createTestTask(stack); |
| 118 | |
| 119 | assertNotNull(stack); |
| 120 | assertNotNull(task); |
| 121 | stack.removeIfPossible(); |
| 122 | // Assert that the container was removed. |
| 123 | assertNull(stack.getParent()); |
| 124 | assertEquals(0, stack.getChildCount()); |
| 125 | assertNull(stack.getDisplayContent()); |
| 126 | assertNull(task.getDisplayContent()); |
| 127 | assertNull(task.mStack); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testRemoveContainer_deferRemoval() { |
| 132 | final TaskStack stack = createTaskStackOnDisplay(mDisplayContent); |
| 133 | final WindowTestUtils.TestTask task = WindowTestUtils.createTestTask(stack); |
| 134 | |
| 135 | // Stack removal is deferred if one of its child is animating. |
| 136 | task.setLocalIsAnimating(true); |
| 137 | |
| 138 | stack.removeIfPossible(); |
| 139 | // For the case of deferred removal the task controller will still be connected to the its |
| 140 | // container until the stack window container is removed. |
| 141 | assertNotNull(stack.getParent()); |
| 142 | assertNotEquals(0, stack.getChildCount()); |
| 143 | assertNotNull(task); |
| 144 | |
| 145 | stack.removeImmediately(); |
| 146 | // After removing, the task will be isolated. |
| 147 | assertNull(task.getParent()); |
| 148 | assertEquals(0, task.getChildCount()); |
| 149 | assertNull(task.getController()); |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void testReparent() { |
| 154 | // Create first stack on primary display. |
| 155 | final TaskStack stack1 = createTaskStackOnDisplay(mDisplayContent); |
| 156 | final WindowTestUtils.TestTask task1 = WindowTestUtils.createTestTask(stack1); |
| 157 | task1.mOnDisplayChangedCalled = false; |
| 158 | |
| 159 | // Create second display and put second stack on it. |
| 160 | final DisplayContent dc = createNewDisplay(); |
| 161 | final TaskStack stack2 = createTaskStackOnDisplay(dc); |
| 162 | |
| 163 | // Reparent |
| 164 | stack1.reparent(dc.getDisplayId(), new Rect(), true /* onTop */); |
| 165 | assertEquals(dc, stack1.getDisplayContent()); |
| 166 | final int stack1PositionInParent = stack1.getParent().mChildren.indexOf(stack1); |
| 167 | final int stack2PositionInParent = stack1.getParent().mChildren.indexOf(stack2); |
| 168 | assertEquals(stack1PositionInParent, stack2PositionInParent + 1); |
| 169 | assertTrue(task1.mOnDisplayChangedCalled); |
| 170 | } |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 171 | } |