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