AppWindowTokenTests.java revision a7e3b64507250ab19a1294d58febea03cfa2b327
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_STARTING;
33import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
34import static org.junit.Assert.assertEquals;
35import static org.junit.Assert.assertNull;
36
37/**
38 * Tests for the {@link WindowState} class.
39 *
40 * Build: mmma -j32 frameworks/base/services/tests/servicestests
41 * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk
42 * Run: adb shell am instrument -w -e class com.android.server.wm.AppWindowTokenTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
43 */
44@SmallTest
45@RunWith(AndroidJUnit4.class)
46public class AppWindowTokenTests {
47
48    private static WindowManagerService sWm = null;
49    private final WindowManagerPolicy mPolicy = new TestWindowManagerPolicy();
50    private final IWindow mIWindow = new TestIWindow();
51
52    @Before
53    public void setUp() throws Exception {
54        final Context context = InstrumentationRegistry.getTargetContext();
55        if (sWm == null) {
56            // We only want to do this once for the test process as we don't want WM to try to
57            // register a bunch of local services again.
58            sWm = WindowManagerService.main(context, null, true, false, false, mPolicy);
59        }
60    }
61
62    @Test
63    public void testFindMainWindow() throws Exception {
64        final TestAppWindowToken token = new TestAppWindowToken();
65
66        assertNull(token.findMainWindow());
67
68        final WindowState window1 = createWindow(null, TYPE_BASE_APPLICATION, token);
69        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
70        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
71        token.addWindow(window1);
72        assertEquals(window1, token.findMainWindow());
73        window1.mAnimatingExit = true;
74        assertEquals(window1, token.findMainWindow());
75        final WindowState window2 = createWindow(null, TYPE_APPLICATION_STARTING, token);
76        token.addWindow(window2);
77        assertEquals(window2, token.findMainWindow());
78    }
79
80    private WindowState createWindow(WindowState parent, int type, WindowToken token) {
81        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
82
83        return new WindowState(sWm, null, mIWindow, token, parent, 0, 0, attrs, 0,
84                sWm.getDefaultDisplayContentLocked(), 0);
85    }
86
87    /* Used so we can gain access to some protected members of the {@link AppWindowToken} class */
88    private class TestAppWindowToken extends AppWindowToken {
89
90        TestAppWindowToken() {
91            super(sWm, null, false);
92        }
93
94        int getWindowsCount() {
95            return mChildren.size();
96        }
97
98        boolean hasWindow(WindowState w) {
99            return mChildren.contains(w);
100        }
101    }
102}
103