Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM; |
| 20 | |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 21 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
| 22 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.times; |
| 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
| 24 | |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertNotEquals; |
| 27 | import static org.junit.Assert.assertNotNull; |
| 28 | import static org.junit.Assert.assertNull; |
| 29 | import static org.junit.Assert.assertTrue; |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 30 | import static org.mockito.ArgumentMatchers.any; |
lumark | bde1513 | 2019-12-18 22:29:43 +0800 | [diff] [blame] | 31 | import static org.mockito.Mockito.clearInvocations; |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 32 | |
Evan Rosky | 89f5c1d | 2019-01-29 10:04:05 -0800 | [diff] [blame] | 33 | import android.graphics.Point; |
| 34 | import android.graphics.Rect; |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 35 | import android.platform.test.annotations.Presubmit; |
| 36 | |
| 37 | import androidx.test.filters.SmallTest; |
| 38 | |
| 39 | import org.junit.Test; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 40 | import org.junit.runner.RunWith; |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Test class for {@link Task}. |
| 44 | * |
| 45 | * Build/Install/Run: |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 46 | * atest WmTests:TaskTests |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 47 | */ |
| 48 | @SmallTest |
| 49 | @Presubmit |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 50 | @RunWith(WindowTestRunner.class) |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 51 | public class TaskTests extends WindowTestsBase { |
| 52 | |
| 53 | @Test |
| 54 | public void testRemoveContainer() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 55 | final ActivityStack stackController1 = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 56 | final Task task = createTaskInStack(stackController1, 0 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 57 | final ActivityRecord activity = |
| 58 | WindowTestUtils.createActivityRecordInTask(mDisplayContent, task); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 59 | |
| 60 | task.removeIfPossible(); |
| 61 | // Assert that the container was removed. |
| 62 | assertNull(task.getParent()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 63 | assertEquals(0, task.getChildCount()); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 64 | assertNull(activity.getParent()); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testRemoveContainer_deferRemoval() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 69 | final ActivityStack stackController1 = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 70 | final Task task = createTaskInStack(stackController1, 0 /* userId */); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 71 | final ActivityRecord activity = |
| 72 | WindowTestUtils.createActivityRecordInTask(mDisplayContent, task); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 73 | |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 74 | doReturn(true).when(task).shouldDeferRemoval(); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 75 | |
| 76 | task.removeIfPossible(); |
| 77 | // For the case of deferred removal the task will still be connected to the its app token |
| 78 | // until the task window container is removed. |
| 79 | assertNotNull(task.getParent()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 80 | assertNotEquals(0, task.getChildCount()); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 81 | assertNotNull(activity.getParent()); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 82 | |
| 83 | task.removeImmediately(); |
| 84 | assertNull(task.getParent()); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 85 | assertEquals(0, task.getChildCount()); |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 86 | assertNull(activity.getParent()); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testReparent() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 91 | final ActivityStack stackController1 = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 92 | final Task task = createTaskInStack(stackController1, 0 /* userId */); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 93 | final ActivityStack stackController2 = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 94 | final Task task2 = createTaskInStack(stackController2, 0 /* userId */); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 95 | |
| 96 | boolean gotException = false; |
| 97 | try { |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 98 | task.reparent(stackController1, 0, false/* moveParents */, "testReparent"); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 99 | } catch (IllegalArgumentException e) { |
| 100 | gotException = true; |
| 101 | } |
| 102 | assertTrue("Should not be able to reparent to the same parent", gotException); |
| 103 | |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 104 | gotException = false; |
| 105 | try { |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 106 | task.reparent(null, 0, false/* moveParents */, "testReparent"); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 107 | } catch (Exception e) { |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 108 | gotException = true; |
| 109 | } |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 110 | assertTrue("Should not be able to reparent to a stack that doesn't exist", gotException); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 111 | |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 112 | task.reparent(stackController2, 0, false/* moveParents */, "testReparent"); |
Yunfan Chen | 279f558 | 2018-12-12 15:24:50 -0800 | [diff] [blame] | 113 | assertEquals(stackController2, task.getParent()); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 114 | assertEquals(0, task.getParent().mChildren.indexOf(task)); |
| 115 | assertEquals(1, task2.getParent().mChildren.indexOf(task2)); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testReparent_BetweenDisplays() { |
| 120 | // Create first stack on primary display. |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 121 | final ActivityStack stack1 = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 122 | final Task task = createTaskInStack(stack1, 0 /* userId */); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 123 | assertEquals(mDisplayContent, stack1.getDisplayContent()); |
| 124 | |
| 125 | // Create second display and put second stack on it. |
| 126 | final DisplayContent dc = createNewDisplay(); |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 127 | final ActivityStack stack2 = createTaskStackOnDisplay(dc); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 128 | final Task task2 = createTaskInStack(stack2, 0 /* userId */); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 129 | // Reparent and check state |
lumark | bde1513 | 2019-12-18 22:29:43 +0800 | [diff] [blame] | 130 | clearInvocations(task); // reset the number of onDisplayChanged for task. |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 131 | task.reparent(stack2, 0, false /* moveParents */, "testReparent_BetweenDisplays"); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 132 | assertEquals(stack2, task.getParent()); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 133 | assertEquals(0, task.getParent().mChildren.indexOf(task)); |
| 134 | assertEquals(1, task2.getParent().mChildren.indexOf(task2)); |
| 135 | verify(task, times(1)).onDisplayChanged(any()); |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 136 | } |
Evan Rosky | 89f5c1d | 2019-01-29 10:04:05 -0800 | [diff] [blame] | 137 | |
| 138 | @Test |
| 139 | public void testBounds() { |
Wale Ogunwale | bebd8cd | 2019-10-28 15:53:31 -0700 | [diff] [blame] | 140 | final ActivityStack stack1 = createTaskStackOnDisplay(mDisplayContent); |
Wale Ogunwale | a733c79 | 2019-10-16 21:31:15 -0700 | [diff] [blame] | 141 | final Task task = createTaskInStack(stack1, 0 /* userId */); |
Evan Rosky | 89f5c1d | 2019-01-29 10:04:05 -0800 | [diff] [blame] | 142 | |
| 143 | // Check that setting bounds also updates surface position |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 144 | task.setWindowingMode(WINDOWING_MODE_FREEFORM); |
Evan Rosky | 89f5c1d | 2019-01-29 10:04:05 -0800 | [diff] [blame] | 145 | Rect bounds = new Rect(10, 10, 100, 200); |
| 146 | task.setBounds(bounds); |
| 147 | assertEquals(new Point(bounds.left, bounds.top), task.getLastSurfacePosition()); |
| 148 | |
| 149 | Rect dispBounds = new Rect(20, 30, 110, 220); |
| 150 | task.setOverrideDisplayedBounds(dispBounds); |
| 151 | assertEquals(new Point(dispBounds.left, dispBounds.top), task.getLastSurfacePosition()); |
| 152 | } |
Yunfan Chen | 0e7aff9 | 2018-12-05 16:35:32 -0800 | [diff] [blame] | 153 | } |