WindowTokenTests.java revision 5136249a7147fb205e1b861c1d42a7d1f13b73cc
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.support.test.InstrumentationRegistry;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27import android.view.IWindow;
28import android.view.WindowManager;
29import android.view.WindowManagerPolicy;
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@RunWith(AndroidJUnit4.class)
47public class WindowTokenTests {
48
49    private WindowManagerService mWm = null;
50    private final IWindow mIWindow = new TestIWindow();
51
52    @Before
53    public void setUp() throws Exception {
54        final Context context = InstrumentationRegistry.getTargetContext();
55        mWm = TestWindowManagerPolicy.getWindowManagerService(context);
56    }
57
58    @Test
59    public void testAddWindow() throws Exception {
60        final TestWindowToken token = new TestWindowToken();
61
62        assertEquals(0, token.getWindowsCount());
63
64        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token);
65        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
66        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
67        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token);
68        final WindowState window3 = createWindow(null, TYPE_APPLICATION, token);
69
70        token.addWindow(window1);
71        // NOTE: Child windows will not be added to the token as window containers can only
72        // contain/reference their direct children.
73        token.addWindow(window11);
74        token.addWindow(window12);
75        token.addWindow(window2);
76        token.addWindow(window3);
77
78        // Should not contain the child windows that were added above.
79        assertEquals(3, token.getWindowsCount());
80        assertTrue(token.hasWindow(window1));
81        assertFalse(token.hasWindow(window11));
82        assertFalse(token.hasWindow(window12));
83        assertTrue(token.hasWindow(window2));
84        assertTrue(token.hasWindow(window3));
85    }
86
87    @Test
88    public void testAdjustAnimLayer() throws Exception {
89        final TestWindowToken token = new TestWindowToken();
90        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token);
91        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
92        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
93        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token);
94        final WindowState window3 = createWindow(null, TYPE_APPLICATION, token);
95
96        token.addWindow(window1);
97        token.addWindow(window2);
98        token.addWindow(window3);
99
100        final int adj = 50;
101        final int window2StartLayer = window2.mLayer = 100;
102        final int window3StartLayer = window3.mLayer = 200;
103        final int highestLayer = token.adjustAnimLayer(adj);
104
105        assertEquals(adj, window1.mWinAnimator.mAnimLayer);
106        assertEquals(adj, window11.mWinAnimator.mAnimLayer);
107        assertEquals(adj, window12.mWinAnimator.mAnimLayer);
108        assertEquals(window2StartLayer + adj, window2.mWinAnimator.mAnimLayer);
109        assertEquals(window3StartLayer + adj, window3.mWinAnimator.mAnimLayer);
110        assertEquals(window3StartLayer + adj, highestLayer);
111    }
112
113    @Test
114    public void testGetTopWindow() throws Exception {
115        final TestWindowToken token = new TestWindowToken();
116
117        assertNull(token.getTopWindow());
118
119        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token);
120        token.addWindow(window1);
121        assertEquals(window1, token.getTopWindow());
122        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
123        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
124        assertEquals(window12, token.getTopWindow());
125
126        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token);
127        token.addWindow(window2);
128        // Since new windows are added to the bottom of the token, we would still expect the
129        // previous one to the top.
130        assertEquals(window12, token.getTopWindow());
131    }
132
133    @Test
134    public void testGetWindowIndex() throws Exception {
135        final TestWindowToken token = new TestWindowToken();
136
137        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token);
138        assertEquals(-1, token.getWindowIndex(window1));
139        token.addWindow(window1);
140        assertEquals(0, token.getWindowIndex(window1));
141        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
142        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
143        // Child windows should report the same index as their parents.
144        assertEquals(0, token.getWindowIndex(window11));
145        assertEquals(0, token.getWindowIndex(window12));
146
147        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token);
148        assertEquals(-1, token.getWindowIndex(window2));
149        token.addWindow(window2);
150        // Since new windows are added to the bottom of the token, we would expect the added window
151        // to be at index 0.
152        assertEquals(0, token.getWindowIndex(window2));
153        assertEquals(1, token.getWindowIndex(window1));
154    }
155
156    private WindowState createWindow(WindowState parent, int type, WindowToken token) {
157        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
158
159        return new WindowState(mWm, null, mIWindow, token, parent, 0, 0, attrs, 0,
160                mWm.getDefaultDisplayContentLocked(), 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);
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