AppWindowTokenTests.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_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@Presubmit
46@RunWith(AndroidJUnit4.class)
47public class AppWindowTokenTests {
48
49    private static WindowManagerService sWm = null;
50    private final IWindow mIWindow = new TestIWindow();
51
52    @Before
53    public void setUp() throws Exception {
54        final Context context = InstrumentationRegistry.getTargetContext();
55        sWm = TestWindowManagerPolicy.getWindowManagerService(context);
56    }
57
58    @Test
59    public void testFindMainWindow() throws Exception {
60        final TestAppWindowToken token = new TestAppWindowToken();
61
62        assertNull(token.findMainWindow());
63
64        final WindowState window1 = createWindow(null, TYPE_BASE_APPLICATION, token);
65        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
66        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
67        token.addWindow(window1);
68        assertEquals(window1, token.findMainWindow());
69        window1.mAnimatingExit = true;
70        assertEquals(window1, token.findMainWindow());
71        final WindowState window2 = createWindow(null, TYPE_APPLICATION_STARTING, token);
72        token.addWindow(window2);
73        assertEquals(window2, token.findMainWindow());
74    }
75
76    private WindowState createWindow(WindowState parent, int type, WindowToken token) {
77        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
78
79        return new WindowState(sWm, null, mIWindow, token, parent, 0, 0, attrs, 0, 0);
80    }
81
82    /* Used so we can gain access to some protected members of the {@link AppWindowToken} class */
83    private class TestAppWindowToken extends AppWindowToken {
84
85        TestAppWindowToken() {
86            super(sWm, null, false, sWm.getDefaultDisplayContentLocked());
87        }
88
89        int getWindowsCount() {
90            return mChildren.size();
91        }
92
93        boolean hasWindow(WindowState w) {
94            return mChildren.contains(w);
95        }
96    }
97}
98