AppWindowContainerControllerTests.java revision 70aa4d18be4865ef28795aa9e78f6b36ac437d8a
1051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt/*
2051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * Copyright (C) 2017 The Android Open Source Project
3051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt *
4203eadb9eda41a1dde4a583edb4684319e3f399eDmitry Shmidt * Licensed under the Apache License, Version 2.0 (the "License");
5051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * you may not use this file except in compliance with the License.
6051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * You may obtain a copy of the License at
7051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt *
8051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt *      http://www.apache.org/licenses/LICENSE-2.0
9051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt *
10051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * Unless required by applicable law or agreed to in writing, software
11051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * distributed under the License is distributed on an "AS IS" BASIS,
12051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * See the License for the specific language governing permissions and
147f65602d49069f96a7bb44da8bd79ffe8d4c6a98Dmitry Shmidt * limitations under the License
15cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt */
16051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
17051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtpackage com.android.server.wm;
18051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
19051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtimport org.junit.Test;
20051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
21051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtimport android.platform.test.annotations.Presubmit;
22a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidtimport android.support.test.InstrumentationRegistry;
23051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtimport android.support.test.filters.SmallTest;
24051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtimport android.support.test.runner.AndroidJUnit4;
25051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
26a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidtimport static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
27a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidtimport static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
28cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtimport static android.content.res.Configuration.EMPTY;
29051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtimport static org.junit.Assert.assertEquals;
30051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidtimport static org.junit.Assert.assertNotNull;
31cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtimport static org.junit.Assert.assertNull;
32cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidtimport static org.junit.Assert.fail;
33051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
34051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt/**
35051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * Test class for {@link AppWindowContainerController}.
36051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt *
37051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt * Build/Install/Run:
38051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt *  bit FrameworksServicesTests:com.android.server.wm.AppWindowContainerControllerTests
39051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt */
40051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt@SmallTest
41a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt@Presubmit
42a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt@org.junit.runner.RunWith(AndroidJUnit4.class)
43a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidtpublic class AppWindowContainerControllerTests extends WindowTestsBase {
44a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt
45051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    @Test
46051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    public void testRemoveContainer() throws Exception {
47051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController controller =
48051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                createAppWindowController();
49051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
50051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        // Assert token was added to display.
51051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        assertNotNull(mDisplayContent.getWindowToken(controller.mToken.asBinder()));
52051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        // Assert that the container was created and linked.
53051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        assertNotNull(controller.mContainer);
5404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt
5504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        controller.removeContainer(mDisplayContent.getDisplayId());
56051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
5704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        // Assert token was remove from display.
5804f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        assertNull(mDisplayContent.getWindowToken(controller.mToken.asBinder()));
5904f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        // Assert that the container was removed.
609866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        assertNull(controller.mContainer);
6104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt    }
629866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
639866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    @Test
649866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt    public void testSetOrientation() throws Exception {
6504f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController controller =
6604f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt                createAppWindowController();
67051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
68051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        // Assert orientation is unspecified to start.
69051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        assertEquals(SCREEN_ORIENTATION_UNSPECIFIED, controller.getOrientation());
70051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
71051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        controller.setOrientation(SCREEN_ORIENTATION_LANDSCAPE, mDisplayContent.getDisplayId(),
72051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                EMPTY /* displayConfig */, false /* freezeScreenIfNeeded */);
73051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        assertEquals(SCREEN_ORIENTATION_LANDSCAPE, controller.getOrientation());
74051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
75051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        controller.removeContainer(mDisplayContent.getDisplayId());
76051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        // Assert orientation is unspecified to after container is removed.
7768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        assertEquals(SCREEN_ORIENTATION_UNSPECIFIED, controller.getOrientation());
78051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
7968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        // Reset display frozen state
8068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        sWm.mDisplayFrozen = false;
8168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    }
8268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
8368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    private void assertHasStartingWindow(AppWindowToken atoken) {
8468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        assertNotNull(atoken.startingSurface);
8568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        assertNotNull(atoken.startingData);
8668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        assertNotNull(atoken.startingWindow);
8768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    }
8868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt
8968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    private void assertNoStartingWindow(AppWindowToken atoken) {
90f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt        assertNull(atoken.startingSurface);
91f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt        assertNull(atoken.startingWindow);
92f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt        assertNull(atoken.startingData);
93f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    }
94f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt
9568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    @Test
9668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    public void testCreateRemoveStartingWindow() throws Exception {
9768d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController controller =
9868d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt                createAppWindowController();
9968d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        controller.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
10068d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false, true,
10168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt                false);
10268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        waitUntilHandlersIdle();
10368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        final AppWindowToken atoken = controller.getAppWindowToken(mDisplayContent);
10468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        assertHasStartingWindow(atoken);
10568d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        controller.removeStartingWindow();
10668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        waitUntilHandlersIdle();
107f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt        assertNoStartingWindow(atoken);
108f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    }
109f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt
110f21452aea786ac056eb01f1cbba4f553bd502747Dmitry Shmidt    @Test
11168d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt    public void testTransferStartingWindow() throws Exception {
11268d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController controller1 =
11368d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt                createAppWindowController();
11468d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController controller2 =
115051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                createAppWindowController();
11668d0e3ed07847339aedfac8e02f50db68c702e52Dmitry Shmidt        controller1.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
117051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false, true,
118051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                false);
119051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        waitUntilHandlersIdle();
120051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        controller2.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
121051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                android.R.style.Theme, null, "Test", 0, 0, 0, 0, controller1.mToken.asBinder(),
122051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                true, true, false, true, false);
123051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        waitUntilHandlersIdle();
124051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        assertNoStartingWindow(controller1.getAppWindowToken(mDisplayContent));
125dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt        assertHasStartingWindow(controller2.getAppWindowToken(mDisplayContent));
126dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt    }
127dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt
128dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt    @Test
129dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt    public void testTransferStartingWindowWhileCreating() throws Exception {
130dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController controller1 =
131dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt                createAppWindowController();
132dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController controller2 =
133dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt                createAppWindowController();
134dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt        ((TestWindowManagerPolicy) sWm.mPolicy).setRunnableWhenAddingSplashScreen(() -> {
135dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt
136dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt            // Surprise, ...! Transfer window in the middle of the creation flow.
137dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt            controller2.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
138dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt                    android.R.style.Theme, null, "Test", 0, 0, 0, 0, controller1.mToken.asBinder(),
139cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                    true, true, false, true, false);
14004f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        });
14104f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        controller1.addStartingWindow(InstrumentationRegistry.getContext().getPackageName(),
142cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                android.R.style.Theme, null, "Test", 0, 0, 0, 0, null, true, true, false, true,
143cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                false);
144cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        waitUntilHandlersIdle();
145cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        assertNoStartingWindow(controller1.getAppWindowToken(mDisplayContent));
146dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt        assertHasStartingWindow(controller2.getAppWindowToken(mDisplayContent));
147cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    }
148cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
149cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    @Test
150cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt    public void testReparent() throws Exception {
151cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        final StackWindowController stackController =
152dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt            createStackControllerOnDisplay(mDisplayContent);
153dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt        final WindowTestUtils.TestTaskWindowContainerController taskController1 =
154dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt                new WindowTestUtils.TestTaskWindowContainerController(stackController);
155cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController appWindowController1 =
156cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                createAppWindowController(taskController1);
15704f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        final WindowTestUtils.TestTaskWindowContainerController taskController2 =
158cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                new WindowTestUtils.TestTaskWindowContainerController(stackController);
159cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        final WindowTestUtils.TestAppWindowContainerController appWindowController2 =
160cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                createAppWindowController(taskController2);
161cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        final WindowTestUtils.TestTaskWindowContainerController taskController3 =
162cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                new WindowTestUtils.TestTaskWindowContainerController(stackController);
163cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
164cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        try {
1659866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt            appWindowController1.reparent(taskController1, 0);
1669866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt            fail("Should not be able to reparent to the same parent");
1679866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        } catch (IllegalArgumentException e) {
168dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt            // Expected
1699866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt        }
1709866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt
171dda10c2afb8378747491ea5d329a1de635d6d58eDmitry Shmidt        try {
1729866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt            taskController3.setContainer(null);
1739866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt            appWindowController1.reparent(taskController3, 0);
1749866086a955d00e237cc8df3722e7dff75c02532Dmitry Shmidt            fail("Should not be able to reparent to a task that doesn't have a container");
175cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        } catch (IllegalArgumentException e) {
176cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt            // Expected
177cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        }
178cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
179cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        // Reparent the app window and ensure that it is moved
180cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        appWindowController1.reparent(taskController2, 0);
181cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        assertEquals(taskController2.mContainer, appWindowController1.mContainer.getParent());
182cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt        assertEquals(0, ((WindowTestUtils.TestAppWindowToken) appWindowController1.mContainer)
183051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                .positionInParent());
18404f534e89ed127da4077485376f24debc50d80d5Dmitry Shmidt        assertEquals(1, ((WindowTestUtils.TestAppWindowToken) appWindowController2.mContainer)
185051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt                .positionInParent());
186051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    }
187cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt
188a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt    private WindowTestUtils.TestAppWindowContainerController createAppWindowController() {
189051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        return createAppWindowController(
190cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt                new WindowTestUtils.TestTaskWindowContainerController(this));
191a7b06faf528d1765cc2712cc9a31ad45d7c3110bDmitry Shmidt    }
192051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt
193051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    private WindowTestUtils.TestAppWindowContainerController createAppWindowController(
194051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt            WindowTestUtils.TestTaskWindowContainerController taskController) {
195051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt        return new WindowTestUtils.TestAppWindowContainerController(taskController);
196051af73b8f8014eff33330aead0f36944b3403e6Dmitry Shmidt    }
197cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt}
198cce06667447b5aec83452adb0c15100ada531095Dmitry Shmidt