AppWindowTokenTests.java revision 17f175ca1a75d5a4864b88126f5d2a59935d52fd
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_LANDSCAPE;
30import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
31import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
32import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
33import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
34import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
35import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertNull;
37import static org.junit.Assert.assertTrue;
38
39/**
40 * Tests for the {@link AppWindowToken} class.
41 *
42 * Build/Install/Run:
43 *  bit FrameworksServicesTests:com.android.server.wm.AppWindowTokenTests
44 */
45@SmallTest
46@Presubmit
47@RunWith(AndroidJUnit4.class)
48public class AppWindowTokenTests extends WindowTestsBase {
49
50    @Test
51    public void testAddWindow_Order() throws Exception {
52        final TestAppWindowToken token = new TestAppWindowToken(sDisplayContent);
53
54        assertEquals(0, token.getWindowsCount());
55
56        final WindowState win1 = createWindow(null, TYPE_APPLICATION, token, "win1");
57        final WindowState startingWin = createWindow(null, TYPE_APPLICATION_STARTING, token,
58                "startingWin");
59        final WindowState baseWin = createWindow(null, TYPE_BASE_APPLICATION, token, "baseWin");
60        final WindowState win4 = createWindow(null, TYPE_APPLICATION, token, "win4");
61
62        // Should not contain the windows that were added above.
63        assertEquals(4, token.getWindowsCount());
64        assertTrue(token.hasWindow(win1));
65        assertTrue(token.hasWindow(startingWin));
66        assertTrue(token.hasWindow(baseWin));
67        assertTrue(token.hasWindow(win4));
68
69        // The starting window should be on-top of all other windows.
70        assertEquals(startingWin, token.getLastChild());
71
72        // The base application window should be below all other windows.
73        assertEquals(baseWin, token.getFirstChild());
74        token.removeImmediately();
75    }
76
77    @Test
78    public void testFindMainWindow() throws Exception {
79        final TestAppWindowToken token = new TestAppWindowToken(sDisplayContent);
80
81        assertNull(token.findMainWindow());
82
83        final WindowState window1 = createWindow(null, TYPE_BASE_APPLICATION, token, "window1");
84        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token, "window11");
85        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token, "window12");
86        assertEquals(window1, token.findMainWindow());
87        window1.mAnimatingExit = true;
88        assertEquals(window1, token.findMainWindow());
89        final WindowState window2 = createWindow(null, TYPE_APPLICATION_STARTING, token, "window2");
90        assertEquals(window2, token.findMainWindow());
91        token.removeImmediately();
92    }
93
94    @Test
95    public void testLandscapeSeascapeRotationByApp() throws Exception {
96        // Some plumbing to get the service ready for rotation updates.
97        sWm.mDisplayReady = true;
98        sWm.mDisplayEnabled = true;
99
100        // Create an app window with token on a display.
101        final TaskStack stack = createTaskStackOnDisplay(sDisplayContent);
102        final Task task = createTaskInStack(stack, 0 /* userId */);
103        final TestAppWindowToken appWindowToken = new TestAppWindowToken(sDisplayContent);
104        task.addChild(appWindowToken, 0);
105        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(
106                TYPE_BASE_APPLICATION);
107        attrs.setTitle("AppWindow");
108        final TestWindowState appWindow = new TestWindowState(attrs, appWindowToken);
109        appWindowToken.addWindow(appWindow);
110
111        // Set initial orientation and update.
112        appWindowToken.setOrientation(SCREEN_ORIENTATION_LANDSCAPE);
113        sWm.updateOrientationFromAppTokens(sDisplayContent.getOverrideConfiguration(), null,
114                sDisplayContent.getDisplayId());
115        assertEquals(SCREEN_ORIENTATION_LANDSCAPE, sWm.mLastOrientation);
116        appWindow.resizeReported = false;
117
118        // Update the orientation to perform 180 degree rotation and check that resize was reported.
119        appWindowToken.setOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
120        sWm.updateOrientationFromAppTokens(sDisplayContent.getOverrideConfiguration(), null,
121                sDisplayContent.getDisplayId());
122        sWm.mRoot.performSurfacePlacement(false /* recoveringMemory */);
123        assertEquals(SCREEN_ORIENTATION_REVERSE_LANDSCAPE, sWm.mLastOrientation);
124        assertTrue(appWindow.resizeReported);
125        appWindow.removeImmediately();
126    }
127
128    @Test
129    @Ignore
130    // TODO(b/35034729): Need to fix before re-enabling
131    public void testLandscapeSeascapeRotationByPolicy() throws Exception {
132        // Some plumbing to get the service ready for rotation updates.
133        sWm.mDisplayReady = true;
134        sWm.mDisplayEnabled = true;
135
136        // Create an app window with token on a display.
137        final TaskStack stack = createTaskStackOnDisplay(sDisplayContent);
138        final Task task = createTaskInStack(stack, 0 /* userId */);
139        final TestAppWindowToken appWindowToken = new TestAppWindowToken(sDisplayContent);
140        task.addChild(appWindowToken, 0);
141        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(
142                TYPE_BASE_APPLICATION);
143        attrs.setTitle("AppWindow");
144        final TestWindowState appWindow = new TestWindowState(attrs, appWindowToken);
145        appWindowToken.addWindow(appWindow);
146
147        // Set initial orientation and update.
148        ((TestWindowManagerPolicy) sWm.mPolicy).rotationToReport = Surface.ROTATION_90;
149        sWm.updateRotation(false, false);
150        appWindow.resizeReported = false;
151
152        // Update the rotation to perform 180 degree rotation and check that resize was reported.
153        ((TestWindowManagerPolicy) sWm.mPolicy).rotationToReport = Surface.ROTATION_270;
154        sWm.updateRotation(false, false);
155        sWm.mRoot.performSurfacePlacement(false /* recoveringMemory */);
156        assertTrue(appWindow.resizeReported);
157        appWindow.removeImmediately();
158    }
159}
160