AppWindowTokenTests.java revision 07bcab787ea7ce65081dffe7da196f872a1be37a
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.assertFalse;
37import static org.junit.Assert.assertNull;
38import static org.junit.Assert.assertTrue;
39
40/**
41 * Tests for the {@link WindowState} 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 {
50
51    private static WindowManagerService sWm = null;
52    private final IWindow mIWindow = new TestIWindow();
53
54    @Before
55    public void setUp() throws Exception {
56        final Context context = InstrumentationRegistry.getTargetContext();
57        sWm = TestWindowManagerPolicy.getWindowManagerService(context);
58    }
59
60    @Test
61    public void testAddWindow_Order() throws Exception {
62        final TestAppWindowToken token = new TestAppWindowToken();
63
64        assertEquals(0, token.getWindowsCount());
65
66        final WindowState win1 = createWindow(null, TYPE_APPLICATION, token);
67        final WindowState startingWin = createWindow(null, TYPE_APPLICATION_STARTING, token);
68        final WindowState baseWin = createWindow(null, TYPE_BASE_APPLICATION, token);
69        final WindowState win4 = createWindow(null, TYPE_APPLICATION, token);
70
71        token.addWindow(win1);
72        token.addWindow(startingWin);
73        token.addWindow(baseWin);
74        token.addWindow(win4);
75
76        // Should not contain the windows that were added above.
77        assertEquals(4, token.getWindowsCount());
78        assertTrue(token.hasWindow(win1));
79        assertTrue(token.hasWindow(startingWin));
80        assertTrue(token.hasWindow(baseWin));
81        assertTrue(token.hasWindow(win4));
82
83        // The starting window should be on-top of all other windows.
84        assertEquals(startingWin, token.getLastChild());
85
86        // The base application window should be below all other windows.
87        assertEquals(baseWin, token.getFirstChild());
88    }
89
90    @Test
91    public void testFindMainWindow() throws Exception {
92        final TestAppWindowToken token = new TestAppWindowToken();
93
94        assertNull(token.findMainWindow());
95
96        final WindowState window1 = createWindow(null, TYPE_BASE_APPLICATION, token);
97        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
98        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
99        token.addWindow(window1);
100        assertEquals(window1, token.findMainWindow());
101        window1.mAnimatingExit = true;
102        assertEquals(window1, token.findMainWindow());
103        final WindowState window2 = createWindow(null, TYPE_APPLICATION_STARTING, token);
104        token.addWindow(window2);
105        assertEquals(window2, token.findMainWindow());
106    }
107
108    private WindowState createWindow(WindowState parent, int type, WindowToken token) {
109        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
110
111        return new WindowState(sWm, null, mIWindow, token, parent, 0, 0, attrs, 0, 0);
112    }
113
114    /* Used so we can gain access to some protected members of the {@link AppWindowToken} class */
115    private class TestAppWindowToken extends AppWindowToken {
116
117        TestAppWindowToken() {
118            super(sWm, null, false, sWm.getDefaultDisplayContentLocked());
119        }
120
121        int getWindowsCount() {
122            return mChildren.size();
123        }
124
125        boolean hasWindow(WindowState w) {
126            return mChildren.contains(w);
127        }
128
129        WindowState getFirstChild() {
130            return mChildren.getFirst();
131        }
132
133        WindowState getLastChild() {
134            return mChildren.getLast();
135        }
136    }
137}
138