AppWindowContainerControllerTests.java revision 3048004087968674c3af97e1c0b110a223f04703
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 org.junit.Test;
20
21import android.platform.test.annotations.Presubmit;
22import android.support.test.InstrumentationRegistry;
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
27import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
28import static android.content.res.Configuration.EMPTY;
29import static org.junit.Assert.assertEquals;
30import static org.junit.Assert.assertNotNull;
31import static org.junit.Assert.assertNull;
32import static org.junit.Assert.fail;
33
34/**
35 * Test class for {@link AppWindowContainerController}.
36 *
37 * Build/Install/Run:
38 *  bit FrameworksServicesTests:com.android.server.wm.AppWindowContainerControllerTests
39 */
40@SmallTest
41@Presubmit
42@org.junit.runner.RunWith(AndroidJUnit4.class)
43public class AppWindowContainerControllerTests extends WindowTestsBase {
44
45    @Test
46    public void testRemoveContainer() throws Exception {
47        final TestAppWindowContainerController controller = createAppWindowController();
48
49        // Assert token was added to display.
50        assertNotNull(sDisplayContent.getWindowToken(controller.mToken.asBinder()));
51        // Assert that the container was created and linked.
52        assertNotNull(controller.mContainer);
53
54        controller.removeContainer(sDisplayContent.getDisplayId());
55
56        // Assert token was remove from display.
57        assertNull(sDisplayContent.getWindowToken(controller.mToken.asBinder()));
58        // Assert that the container was removed.
59        assertNull(controller.mContainer);
60    }
61
62    @Test
63    public void testSetOrientation() throws Exception {
64        final TestAppWindowContainerController controller = createAppWindowController();
65
66        // Assert orientation is unspecified to start.
67        assertEquals(SCREEN_ORIENTATION_UNSPECIFIED, controller.getOrientation());
68
69        controller.setOrientation(SCREEN_ORIENTATION_LANDSCAPE, sDisplayContent.getDisplayId(),
70                EMPTY /* displayConfig */, false /* freezeScreenIfNeeded */);
71        assertEquals(SCREEN_ORIENTATION_LANDSCAPE, controller.getOrientation());
72
73        controller.removeContainer(sDisplayContent.getDisplayId());
74        // Assert orientation is unspecified to after container is removed.
75        assertEquals(SCREEN_ORIENTATION_UNSPECIFIED, controller.getOrientation());
76
77        // Reset display frozen state
78        sWm.mDisplayFrozen = false;
79    }
80
81    private void assertHasStartingWindow(AppWindowToken atoken) {
82        assertNotNull(atoken.startingSurface);
83        assertNotNull(atoken.startingData);
84        assertNotNull(atoken.startingWindow);
85    }
86
87    private void assertNoStartingWindow(AppWindowToken atoken) {
88        assertNull(atoken.startingSurface);
89        assertNull(atoken.startingWindow);
90        assertNull(atoken.startingData);
91    }
92
93    @Test
94    public void testCreateRemoveStartingWindow() throws Exception {
95        final TestAppWindowContainerController controller = createAppWindowController();
96        controller.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
97                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false);
98        waitUntilHandlerIdle();
99        final AppWindowToken atoken = controller.getAppWindowToken();
100        assertHasStartingWindow(atoken);
101        controller.removeStartingWindow();
102        waitUntilHandlerIdle();
103        assertNoStartingWindow(atoken);
104    }
105
106    @Test
107    public void testTransferStartingWindow() throws Exception {
108        final TestAppWindowContainerController controller1 = createAppWindowController();
109        final TestAppWindowContainerController controller2 = createAppWindowController();
110        controller1.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
111                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false);
112        waitUntilHandlerIdle();
113        controller2.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
114                android.R.style.Theme, null, "Test", 0, 0, 0, 0, controller1.mToken.asBinder(),
115                true, true, false);
116        waitUntilHandlerIdle();
117        assertNoStartingWindow(controller1.getAppWindowToken());
118        assertHasStartingWindow(controller2.getAppWindowToken());
119    }
120
121    @Test
122    public void testTransferStartingWindowWhileCreating() throws Exception {
123        final TestAppWindowContainerController controller1 = createAppWindowController();
124        final TestAppWindowContainerController controller2 = createAppWindowController();
125        sPolicy.setRunnableWhenAddingSplashScreen(() -> {
126
127            // Surprise, ...! Transfer window in the middle of the creation flow.
128            controller2.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
129                    android.R.style.Theme, null, "Test", 0, 0, 0, 0, controller1.mToken.asBinder(),
130                    true, true, false);
131        });
132        controller1.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
133                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false);
134        waitUntilHandlerIdle();
135        assertNoStartingWindow(controller1.getAppWindowToken());
136        assertHasStartingWindow(controller2.getAppWindowToken());
137    }
138
139    @Test
140    public void testReparent() throws Exception {
141        final TestTaskWindowContainerController taskController1 =
142                new TestTaskWindowContainerController(
143                        createStackControllerOnDisplay(sDisplayContent));
144        final TestAppWindowContainerController appWindowController1 = createAppWindowController(
145                taskController1);
146        final TestTaskWindowContainerController taskController2 =
147                new TestTaskWindowContainerController(
148                        createStackControllerOnDisplay(sDisplayContent));
149        final TestAppWindowContainerController appWindowController2 = createAppWindowController(
150                taskController2);
151        final TestTaskWindowContainerController taskController3 =
152                new TestTaskWindowContainerController(
153                        createStackControllerOnDisplay(sDisplayContent));
154
155        try {
156            appWindowController1.reparent(taskController1, 0);
157            fail("Should not be able to reparent to the same parent");
158        } catch (IllegalArgumentException e) {
159            // Expected
160        }
161
162        try {
163            taskController3.setContainer(null);
164            appWindowController1.reparent(taskController3, 0);
165            fail("Should not be able to reparent to a task that doesn't have a container");
166        } catch (IllegalArgumentException e) {
167            // Expected
168        }
169
170        // Reparent the app window and ensure that it is moved
171        appWindowController1.reparent(taskController2, 0);
172        assertEquals(taskController2.mContainer, appWindowController1.mContainer.getParent());
173        assertEquals(0, ((TestAppWindowToken) appWindowController1.mContainer).positionInParent());
174        assertEquals(1, ((TestAppWindowToken) appWindowController2.mContainer).positionInParent());
175    }
176
177    private TestAppWindowContainerController createAppWindowController() {
178        return createAppWindowController(new TestTaskWindowContainerController());
179    }
180
181    private TestAppWindowContainerController createAppWindowController(
182            TestTaskWindowContainerController taskController) {
183        return new TestAppWindowContainerController(taskController);
184    }
185}
186