WindowTokenTests.java revision 360a8bce7386dc9b231c698af1730e04362b6c2e
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 org.junit.Assert.assertEquals; 34import static org.junit.Assert.assertFalse; 35import static org.junit.Assert.assertNull; 36import static org.junit.Assert.assertTrue; 37 38/** 39 * Tests for the {@link WindowState} class. 40 * 41 * Build: mmma -j32 frameworks/base/services/tests/servicestests 42 * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk 43 * Run: adb shell am instrument -w -e class com.android.server.wm.WindowTokenTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner 44 */ 45@SmallTest 46@Presubmit 47@RunWith(AndroidJUnit4.class) 48public class WindowTokenTests { 49 50 private WindowManagerService mWm = null; 51 private final IWindow mIWindow = new TestIWindow(); 52 53 @Before 54 public void setUp() throws Exception { 55 final Context context = InstrumentationRegistry.getTargetContext(); 56 mWm = TestWindowManagerPolicy.getWindowManagerService(context); 57 } 58 59 @Test 60 public void testAddWindow() throws Exception { 61 final TestWindowToken token = new TestWindowToken(); 62 63 assertEquals(0, token.getWindowsCount()); 64 65 final WindowState window1 = createWindow(null, TYPE_APPLICATION, token); 66 final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token); 67 final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token); 68 final WindowState window2 = createWindow(null, TYPE_APPLICATION, token); 69 final WindowState window3 = createWindow(null, TYPE_APPLICATION, token); 70 71 token.addWindow(window1); 72 // NOTE: Child windows will not be added to the token as window containers can only 73 // contain/reference their direct children. 74 token.addWindow(window11); 75 token.addWindow(window12); 76 token.addWindow(window2); 77 token.addWindow(window3); 78 79 // Should not contain the child windows that were added above. 80 assertEquals(3, token.getWindowsCount()); 81 assertTrue(token.hasWindow(window1)); 82 assertFalse(token.hasWindow(window11)); 83 assertFalse(token.hasWindow(window12)); 84 assertTrue(token.hasWindow(window2)); 85 assertTrue(token.hasWindow(window3)); 86 } 87 88 @Test 89 public void testAdjustAnimLayer() throws Exception { 90 final TestWindowToken token = new TestWindowToken(); 91 final WindowState window1 = createWindow(null, TYPE_APPLICATION, token); 92 final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token); 93 final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token); 94 final WindowState window2 = createWindow(null, TYPE_APPLICATION, token); 95 final WindowState window3 = createWindow(null, TYPE_APPLICATION, token); 96 97 token.addWindow(window1); 98 token.addWindow(window2); 99 token.addWindow(window3); 100 101 final int adj = 50; 102 final int window2StartLayer = window2.mLayer = 100; 103 final int window3StartLayer = window3.mLayer = 200; 104 final int highestLayer = token.adjustAnimLayer(adj); 105 106 assertEquals(adj, window1.mWinAnimator.mAnimLayer); 107 assertEquals(adj, window11.mWinAnimator.mAnimLayer); 108 assertEquals(adj, window12.mWinAnimator.mAnimLayer); 109 assertEquals(window2StartLayer + adj, window2.mWinAnimator.mAnimLayer); 110 assertEquals(window3StartLayer + adj, window3.mWinAnimator.mAnimLayer); 111 assertEquals(window3StartLayer + adj, highestLayer); 112 } 113 114 @Test 115 public void testGetTopWindow() throws Exception { 116 final TestWindowToken token = new TestWindowToken(); 117 118 assertNull(token.getTopWindow()); 119 120 final WindowState window1 = createWindow(null, TYPE_APPLICATION, token); 121 token.addWindow(window1); 122 assertEquals(window1, token.getTopWindow()); 123 final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token); 124 final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token); 125 assertEquals(window12, token.getTopWindow()); 126 127 final WindowState window2 = createWindow(null, TYPE_APPLICATION, token); 128 token.addWindow(window2); 129 // Since new windows are added to the bottom of the token, we would still expect the 130 // previous one to the top. 131 assertEquals(window12, token.getTopWindow()); 132 } 133 134 @Test 135 public void testGetWindowIndex() throws Exception { 136 final TestWindowToken token = new TestWindowToken(); 137 138 final WindowState window1 = createWindow(null, TYPE_APPLICATION, token); 139 assertEquals(-1, token.getWindowIndex(window1)); 140 token.addWindow(window1); 141 assertEquals(0, token.getWindowIndex(window1)); 142 final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token); 143 final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token); 144 // Child windows should report the same index as their parents. 145 assertEquals(0, token.getWindowIndex(window11)); 146 assertEquals(0, token.getWindowIndex(window12)); 147 148 final WindowState window2 = createWindow(null, TYPE_APPLICATION, token); 149 assertEquals(-1, token.getWindowIndex(window2)); 150 token.addWindow(window2); 151 // Since new windows are added to the bottom of the token, we would expect the added window 152 // to be at index 0. 153 assertEquals(0, token.getWindowIndex(window2)); 154 assertEquals(1, token.getWindowIndex(window1)); 155 } 156 157 private WindowState createWindow(WindowState parent, int type, WindowToken token) { 158 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type); 159 160 return new WindowState(mWm, null, mIWindow, token, parent, 0, 0, attrs, 0, 0); 161 } 162 163 /* Used so we can gain access to some protected members of the {@link WindowToken} class */ 164 private class TestWindowToken extends WindowToken { 165 166 TestWindowToken() { 167 super(mWm, null, 0, false, mWm.getDefaultDisplayContentLocked()); 168 } 169 170 int getWindowsCount() { 171 return mChildren.size(); 172 } 173 174 boolean hasWindow(WindowState w) { 175 return mChildren.contains(w); 176 } 177 } 178} 179