AppWindowTokenTests.java revision 44fbdf5b1e13398e35d4bafb7236d194a51ee7af
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.Before; 20import org.junit.Test; 21import org.junit.runner.RunWith; 22 23import android.content.Context; 24import android.platform.test.annotations.Presubmit; 25import android.support.test.InstrumentationRegistry; 26import android.support.test.filters.SmallTest; 27import android.support.test.runner.AndroidJUnit4; 28import android.view.IWindow; 29import android.view.WindowManager; 30 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(); 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 token.addWindow(win1); 63 token.addWindow(startingWin); 64 token.addWindow(baseWin); 65 token.addWindow(win4); 66 67 // Should not contain the windows that were added above. 68 assertEquals(4, token.getWindowsCount()); 69 assertTrue(token.hasWindow(win1)); 70 assertTrue(token.hasWindow(startingWin)); 71 assertTrue(token.hasWindow(baseWin)); 72 assertTrue(token.hasWindow(win4)); 73 74 // The starting window should be on-top of all other windows. 75 assertEquals(startingWin, token.getLastChild()); 76 77 // The base application window should be below all other windows. 78 assertEquals(baseWin, token.getFirstChild()); 79 } 80 81 @Test 82 public void testFindMainWindow() throws Exception { 83 final TestAppWindowToken token = new TestAppWindowToken(); 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 token.addWindow(window1); 91 assertEquals(window1, token.findMainWindow()); 92 window1.mAnimatingExit = true; 93 assertEquals(window1, token.findMainWindow()); 94 final WindowState window2 = createWindow(null, TYPE_APPLICATION_STARTING, token, "window2"); 95 token.addWindow(window2); 96 assertEquals(window2, token.findMainWindow()); 97 } 98 99 /* Used so we can gain access to some protected members of the {@link AppWindowToken} class */ 100 private class TestAppWindowToken extends AppWindowToken { 101 102 TestAppWindowToken() { 103 super(sWm, null, false, sWm.getDefaultDisplayContentLocked()); 104 } 105 106 int getWindowsCount() { 107 return mChildren.size(); 108 } 109 110 boolean hasWindow(WindowState w) { 111 return mChildren.contains(w); 112 } 113 114 WindowState getFirstChild() { 115 return mChildren.getFirst(); 116 } 117 118 WindowState getLastChild() { 119 return mChildren.getLast(); 120 } 121 } 122} 123