AppWindowTokenTests.java revision af691c0be7bbfea63e880dd717c51a38a0bc874a
1/*
2 * Copyright (C) 2016 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;
20import org.junit.Ignore;
21import org.junit.runner.RunWith;
22
23import android.platform.test.annotations.Presubmit;
24import android.support.test.filters.SmallTest;
25import android.support.test.runner.AndroidJUnit4;
26import android.view.Surface;
27import android.view.WindowManager;
28
29import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
30import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
31import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
32import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
33import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
34import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
35import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
36import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
37import static org.junit.Assert.assertEquals;
38import static org.junit.Assert.assertNull;
39import static org.junit.Assert.assertTrue;
40
41/**
42 * Tests for the {@link AppWindowToken} class.
43 *
44 * Build/Install/Run:
45 *  bit FrameworksServicesTests:com.android.server.wm.AppWindowTokenTests
46 */
47@SmallTest
48@Presubmit
49@RunWith(AndroidJUnit4.class)
50public class AppWindowTokenTests extends WindowTestsBase {
51
52    @Test
53    public void testAddWindow_Order() throws Exception {
54        final WindowTestUtils.TestAppWindowToken token =
55                new WindowTestUtils.TestAppWindowToken(sDisplayContent);
56
57        assertEquals(0, token.getWindowsCount());
58
59        final WindowState win1 = createWindow(null, TYPE_APPLICATION, token, "win1");
60        final WindowState startingWin = createWindow(null, TYPE_APPLICATION_STARTING, token,
61                "startingWin");
62        final WindowState baseWin = createWindow(null, TYPE_BASE_APPLICATION, token, "baseWin");
63        final WindowState win4 = createWindow(null, TYPE_APPLICATION, token, "win4");
64
65        // Should not contain the windows that were added above.
66        assertEquals(4, token.getWindowsCount());
67        assertTrue(token.hasWindow(win1));
68        assertTrue(token.hasWindow(startingWin));
69        assertTrue(token.hasWindow(baseWin));
70        assertTrue(token.hasWindow(win4));
71
72        // The starting window should be on-top of all other windows.
73        assertEquals(startingWin, token.getLastChild());
74
75        // The base application window should be below all other windows.
76        assertEquals(baseWin, token.getFirstChild());
77        token.removeImmediately();
78    }
79
80    @Test
81    public void testFindMainWindow() throws Exception {
82        final WindowTestUtils.TestAppWindowToken token =
83                new WindowTestUtils.TestAppWindowToken(sDisplayContent);
84
85        assertNull(token.findMainWindow());
86
87        final WindowState window1 = createWindow(null, TYPE_BASE_APPLICATION, token, "window1");
88        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token, "window11");
89        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token, "window12");
90        assertEquals(window1, token.findMainWindow());
91        window1.mAnimatingExit = true;
92        assertEquals(window1, token.findMainWindow());
93        final WindowState window2 = createWindow(null, TYPE_APPLICATION_STARTING, token, "window2");
94        assertEquals(window2, token.findMainWindow());
95        token.removeImmediately();
96    }
97
98    @Test
99    public void testLandscapeSeascapeRotationByApp() throws Exception {
100        // Some plumbing to get the service ready for rotation updates.
101        sWm.mDisplayReady = true;
102        sWm.mDisplayEnabled = true;
103
104        // Create an app window with token on a display.
105        final TaskStack stack = createTaskStackOnDisplay(sDisplayContent);
106        final Task task = createTaskInStack(stack, 0 /* userId */);
107        final WindowTestUtils.TestAppWindowToken appWindowToken =
108                new WindowTestUtils.TestAppWindowToken(sDisplayContent);
109        task.addChild(appWindowToken, 0);
110        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(
111                TYPE_BASE_APPLICATION);
112        attrs.setTitle("AppWindow");
113        final WindowTestUtils.TestWindowState appWindow = createWindowState(attrs, appWindowToken);
114        appWindowToken.addWindow(appWindow);
115
116        // Set initial orientation and update.
117        appWindowToken.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
118        sWm.updateOrientationFromAppTokens(sDisplayContent.getOverrideConfiguration(), null,
119                sDisplayContent.getDisplayId());
120        assertEquals(SCREEN_ORIENTATION_LANDSCAPE, sDisplayContent.getLastOrientation());
121        appWindow.resizeReported = false;
122
123        // Update the orientation to perform 180 degree rotation and check that resize was reported.
124        appWindowToken.setOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
125        sWm.updateOrientationFromAppTokens(sDisplayContent.getOverrideConfiguration(), null,
126                sDisplayContent.getDisplayId());
127        sWm.mRoot.performSurfacePlacement(false /* recoveringMemory */);
128        assertEquals(SCREEN_ORIENTATION_REVERSE_LANDSCAPE, sDisplayContent.getLastOrientation());
129        assertTrue(appWindow.resizeReported);
130        appWindow.removeImmediately();
131    }
132
133    @Test
134    public void testLandscapeSeascapeRotationByPolicy() throws Exception {
135        // Some plumbing to get the service ready for rotation updates.
136        sWm.mDisplayReady = true;
137        sWm.mDisplayEnabled = true;
138
139        // Create an app window with token on a display.
140        final DisplayContent defaultDisplayContent = sWm.getDefaultDisplayContentLocked();
141        final TaskStack stack = createTaskStackOnDisplay(defaultDisplayContent);
142        final Task task = createTaskInStack(stack, 0 /* userId */);
143        final WindowTestUtils.TestAppWindowToken appWindowToken =
144                new WindowTestUtils.TestAppWindowToken(defaultDisplayContent);
145        task.addChild(appWindowToken, 0);
146        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(
147                TYPE_BASE_APPLICATION);
148        attrs.setTitle("AppWindow");
149        final WindowTestUtils.TestWindowState appWindow = createWindowState(attrs, appWindowToken);
150        appWindowToken.addWindow(appWindow);
151
152        // Set initial orientation and update.
153        performRotation(Surface.ROTATION_90);
154        appWindow.resizeReported = false;
155
156        // Update the rotation to perform 180 degree rotation and check that resize was reported.
157        performRotation(Surface.ROTATION_270);
158        assertTrue(appWindow.resizeReported);
159        appWindow.removeImmediately();
160    }
161
162    private void performRotation(int rotationToReport) {
163        ((TestWindowManagerPolicy) sWm.mPolicy).rotationToReport = rotationToReport;
164        sWm.updateRotation(false, false);
165        // Simulate animator finishing orientation change
166        sWm.mRoot.mOrientationChangeComplete = true;
167        sWm.mRoot.performSurfacePlacement(false /* recoveringMemory */);
168    }
169
170    @Test
171    public void testGetOrientation() throws Exception {
172        final WindowTestUtils.TestAppWindowToken token =
173                new WindowTestUtils.TestAppWindowToken(sDisplayContent);
174        token.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
175
176        token.setFillsParent(false);
177        // Can not specify orientation if app doesn't fill parent.
178        assertEquals(SCREEN_ORIENTATION_UNSET, token.getOrientation());
179
180        token.setFillsParent(true);
181        token.hidden = true;
182        token.sendingToBottom = true;
183        // Can not specify orientation if app isn't visible even though it fills parent.
184        assertEquals(SCREEN_ORIENTATION_UNSET, token.getOrientation());
185        // Can specify orientation if the current orientation candidate is orientation behind.
186        assertEquals(SCREEN_ORIENTATION_LANDSCAPE, token.getOrientation(SCREEN_ORIENTATION_BEHIND));
187    }
188}
189