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
17package com.android.server.wm;
18
19import android.graphics.Rect;
20import org.junit.Ignore;
21import org.junit.Test;
22import org.junit.runner.RunWith;
23
24import android.platform.test.annotations.Presubmit;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27
28import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertNotNull;
30import static org.junit.Assert.assertNull;
31import static org.junit.Assert.assertTrue;
32
33/**
34 * Test class for {@link StackWindowController}.
35 *
36 * Build/Install/Run:
37 *  atest FrameworksServicesTests:StackWindowControllerTests
38 */
39@SmallTest
40@Presubmit
41@RunWith(AndroidJUnit4.class)
42public class StackWindowControllerTests extends WindowTestsBase {
43    @Test
44    public void testRemoveContainer() throws Exception {
45        final StackWindowController stackController =
46                createStackControllerOnDisplay(mDisplayContent);
47        final WindowTestUtils.TestTaskWindowContainerController taskController =
48                new WindowTestUtils.TestTaskWindowContainerController(stackController);
49
50        final TaskStack stack = stackController.mContainer;
51        final Task task = taskController.mContainer;
52        assertNotNull(stack);
53        assertNotNull(task);
54        stackController.removeContainer();
55        // Assert that the container was removed.
56        assertNull(stackController.mContainer);
57        assertNull(taskController.mContainer);
58        assertNull(stack.getDisplayContent());
59        assertNull(task.getDisplayContent());
60        assertNull(task.mStack);
61    }
62
63    @Test
64    public void testRemoveContainer_deferRemoval() throws Exception {
65        final StackWindowController stackController =
66                createStackControllerOnDisplay(mDisplayContent);
67        final WindowTestUtils.TestTaskWindowContainerController taskController =
68                new WindowTestUtils.TestTaskWindowContainerController(stackController);
69
70        final TaskStack stack = stackController.mContainer;
71        final WindowTestUtils.TestTask task = (WindowTestUtils.TestTask) taskController.mContainer;
72        // Stack removal is deferred if one of its child is animating.
73        task.setLocalIsAnimating(true);
74
75        stackController.removeContainer();
76        // For the case of deferred removal the stack controller will no longer be connected to the
77        // container, but the task controller will still be connected to the its container until
78        // the stack window container is removed.
79        assertNull(stackController.mContainer);
80        assertNull(stack.getController());
81        assertNotNull(taskController.mContainer);
82        assertNotNull(task.getController());
83
84        stack.removeImmediately();
85        assertNull(taskController.mContainer);
86        assertNull(task.getController());
87    }
88
89    @Test
90    public void testReparent() throws Exception {
91        // Create first stack on primary display.
92        final StackWindowController stack1Controller =
93                createStackControllerOnDisplay(mDisplayContent);
94        final TaskStack stack1 = stack1Controller.mContainer;
95        final WindowTestUtils.TestTaskWindowContainerController taskController =
96                new WindowTestUtils.TestTaskWindowContainerController(stack1Controller);
97        final WindowTestUtils.TestTask task1 = (WindowTestUtils.TestTask) taskController.mContainer;
98        task1.mOnDisplayChangedCalled = false;
99
100        // Create second display and put second stack on it.
101        final DisplayContent dc = createNewDisplay();
102        final StackWindowController stack2Controller =
103                createStackControllerOnDisplay(dc);
104        final TaskStack stack2 = stack2Controller.mContainer;
105
106        // Reparent
107        stack1Controller.reparent(dc.getDisplayId(), new Rect(), true /* onTop */);
108        assertEquals(dc, stack1.getDisplayContent());
109        final int stack1PositionInParent = stack1.getParent().mChildren.indexOf(stack1);
110        final int stack2PositionInParent = stack1.getParent().mChildren.indexOf(stack2);
111        assertEquals(stack1PositionInParent, stack2PositionInParent + 1);
112        assertTrue(task1.mOnDisplayChangedCalled);
113    }
114}
115