blob: ec77be85aebf71e93dcde769d0c56a52c45ea35e [file] [log] [blame]
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001/*
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
17package com.android.server.wm;
18
Wale Ogunwale2322bed2019-10-10 17:24:19 +020019import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
20
Wale Ogunwalea733c792019-10-16 21:31:15 -070021import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
22import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
23import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
24
Yunfan Chen0e7aff92018-12-05 16:35:32 -080025import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertNotEquals;
27import static org.junit.Assert.assertNotNull;
28import static org.junit.Assert.assertNull;
29import static org.junit.Assert.assertTrue;
Wale Ogunwalea733c792019-10-16 21:31:15 -070030import static org.mockito.ArgumentMatchers.any;
lumarkbde15132019-12-18 22:29:43 +080031import static org.mockito.Mockito.clearInvocations;
Yunfan Chen0e7aff92018-12-05 16:35:32 -080032
Evan Rosky89f5c1d2019-01-29 10:04:05 -080033import android.graphics.Point;
34import android.graphics.Rect;
Yunfan Chen0e7aff92018-12-05 16:35:32 -080035import android.platform.test.annotations.Presubmit;
36
37import androidx.test.filters.SmallTest;
38
39import org.junit.Test;
Riddle Hsu73f53572019-09-23 23:13:01 +080040import org.junit.runner.RunWith;
Yunfan Chen0e7aff92018-12-05 16:35:32 -080041
42/**
43 * Test class for {@link Task}.
44 *
45 * Build/Install/Run:
Riddle Hsu73f53572019-09-23 23:13:01 +080046 * atest WmTests:TaskTests
Yunfan Chen0e7aff92018-12-05 16:35:32 -080047 */
48@SmallTest
49@Presubmit
Riddle Hsu73f53572019-09-23 23:13:01 +080050@RunWith(WindowTestRunner.class)
Yunfan Chen0e7aff92018-12-05 16:35:32 -080051public class TaskTests extends WindowTestsBase {
52
53 @Test
54 public void testRemoveContainer() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070055 final ActivityStack stackController1 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -070056 final Task task = createTaskInStack(stackController1, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070057 final ActivityRecord activity =
58 WindowTestUtils.createActivityRecordInTask(mDisplayContent, task);
Yunfan Chen0e7aff92018-12-05 16:35:32 -080059
60 task.removeIfPossible();
61 // Assert that the container was removed.
62 assertNull(task.getParent());
Yunfan Chen279f5582018-12-12 15:24:50 -080063 assertEquals(0, task.getChildCount());
Garfield Tane8d84ab2019-10-11 09:49:40 -070064 assertNull(activity.getParent());
Yunfan Chen0e7aff92018-12-05 16:35:32 -080065 }
66
67 @Test
68 public void testRemoveContainer_deferRemoval() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070069 final ActivityStack stackController1 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -070070 final Task task = createTaskInStack(stackController1, 0 /* userId */);
Garfield Tane8d84ab2019-10-11 09:49:40 -070071 final ActivityRecord activity =
72 WindowTestUtils.createActivityRecordInTask(mDisplayContent, task);
Yunfan Chen0e7aff92018-12-05 16:35:32 -080073
Wale Ogunwalea733c792019-10-16 21:31:15 -070074 doReturn(true).when(task).shouldDeferRemoval();
Yunfan Chen0e7aff92018-12-05 16:35:32 -080075
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 Chen279f5582018-12-12 15:24:50 -080080 assertNotEquals(0, task.getChildCount());
Garfield Tane8d84ab2019-10-11 09:49:40 -070081 assertNotNull(activity.getParent());
Yunfan Chen0e7aff92018-12-05 16:35:32 -080082
83 task.removeImmediately();
84 assertNull(task.getParent());
Yunfan Chen279f5582018-12-12 15:24:50 -080085 assertEquals(0, task.getChildCount());
Garfield Tane8d84ab2019-10-11 09:49:40 -070086 assertNull(activity.getParent());
Yunfan Chen0e7aff92018-12-05 16:35:32 -080087 }
88
89 @Test
90 public void testReparent() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070091 final ActivityStack stackController1 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -070092 final Task task = createTaskInStack(stackController1, 0 /* userId */);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -070093 final ActivityStack stackController2 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -070094 final Task task2 = createTaskInStack(stackController2, 0 /* userId */);
Yunfan Chen0e7aff92018-12-05 16:35:32 -080095
96 boolean gotException = false;
97 try {
Wale Ogunwale2322bed2019-10-10 17:24:19 +020098 task.reparent(stackController1, 0, false/* moveParents */, "testReparent");
Yunfan Chen0e7aff92018-12-05 16:35:32 -080099 } catch (IllegalArgumentException e) {
100 gotException = true;
101 }
102 assertTrue("Should not be able to reparent to the same parent", gotException);
103
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800104 gotException = false;
105 try {
Wale Ogunwale2322bed2019-10-10 17:24:19 +0200106 task.reparent(null, 0, false/* moveParents */, "testReparent");
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700107 } catch (Exception e) {
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800108 gotException = true;
109 }
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700110 assertTrue("Should not be able to reparent to a stack that doesn't exist", gotException);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800111
Wale Ogunwale2322bed2019-10-10 17:24:19 +0200112 task.reparent(stackController2, 0, false/* moveParents */, "testReparent");
Yunfan Chen279f5582018-12-12 15:24:50 -0800113 assertEquals(stackController2, task.getParent());
Wale Ogunwalea733c792019-10-16 21:31:15 -0700114 assertEquals(0, task.getParent().mChildren.indexOf(task));
115 assertEquals(1, task2.getParent().mChildren.indexOf(task2));
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800116 }
117
118 @Test
119 public void testReparent_BetweenDisplays() {
120 // Create first stack on primary display.
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700121 final ActivityStack stack1 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700122 final Task task = createTaskInStack(stack1, 0 /* userId */);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800123 assertEquals(mDisplayContent, stack1.getDisplayContent());
124
125 // Create second display and put second stack on it.
126 final DisplayContent dc = createNewDisplay();
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700127 final ActivityStack stack2 = createTaskStackOnDisplay(dc);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700128 final Task task2 = createTaskInStack(stack2, 0 /* userId */);
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800129 // Reparent and check state
lumarkbde15132019-12-18 22:29:43 +0800130 clearInvocations(task); // reset the number of onDisplayChanged for task.
Wale Ogunwale2322bed2019-10-10 17:24:19 +0200131 task.reparent(stack2, 0, false /* moveParents */, "testReparent_BetweenDisplays");
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800132 assertEquals(stack2, task.getParent());
Wale Ogunwalea733c792019-10-16 21:31:15 -0700133 assertEquals(0, task.getParent().mChildren.indexOf(task));
134 assertEquals(1, task2.getParent().mChildren.indexOf(task2));
135 verify(task, times(1)).onDisplayChanged(any());
Yunfan Chen0e7aff92018-12-05 16:35:32 -0800136 }
Evan Rosky89f5c1d2019-01-29 10:04:05 -0800137
138 @Test
139 public void testBounds() {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700140 final ActivityStack stack1 = createTaskStackOnDisplay(mDisplayContent);
Wale Ogunwalea733c792019-10-16 21:31:15 -0700141 final Task task = createTaskInStack(stack1, 0 /* userId */);
Evan Rosky89f5c1d2019-01-29 10:04:05 -0800142
143 // Check that setting bounds also updates surface position
Wale Ogunwale2322bed2019-10-10 17:24:19 +0200144 task.setWindowingMode(WINDOWING_MODE_FREEFORM);
Evan Rosky89f5c1d2019-01-29 10:04:05 -0800145 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 Chen0e7aff92018-12-05 16:35:32 -0800153}