WindowTestsBase.java revision 5d7e7f136e349e6cde86943dd03e7edd9ec92e6e
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.Assert;
20import org.junit.Before;
21
22import android.content.Context;
23import android.os.IBinder;
24import android.support.test.InstrumentationRegistry;
25import android.view.IWindow;
26import android.view.WindowManager;
27
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.AppOpsManager.OP_NONE;
30import static android.content.res.Configuration.EMPTY;
31import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
32import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
33import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
34import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
35import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
36import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
37import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
38import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
39import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
40import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
41import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
42import static org.mockito.Mockito.mock;
43
44/**
45 * Common base class for window manager unit test classes.
46 */
47public class WindowTestsBase {
48    static WindowManagerService sWm = null;
49    private final IWindow mIWindow = new TestIWindow();
50    private final Session mMockSession = mock(Session.class);
51    private static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
52    private static int sNextTaskId = 0;
53
54    private static boolean sOneTimeSetupDone = false;
55    protected static DisplayContent sDisplayContent;
56    protected static WindowLayersController sLayersController;
57    protected static WindowState sWallpaperWindow;
58    protected static WindowState sImeWindow;
59    protected static WindowState sImeDialogWindow;
60    protected static WindowState sStatusBarWindow;
61    protected static WindowState sDockedDividerWindow;
62    protected static WindowState sNavBarWindow;
63    protected static WindowState sAppWindow;
64    protected static WindowState sChildAppWindowAbove;
65    protected static WindowState sChildAppWindowBelow;
66
67    @Before
68    public void setUp() throws Exception {
69        if (sOneTimeSetupDone) {
70            return;
71        }
72        sOneTimeSetupDone = true;
73        final Context context = InstrumentationRegistry.getTargetContext();
74        sWm = TestWindowManagerPolicy.getWindowManagerService(context);
75        sLayersController = new WindowLayersController(sWm);
76        sDisplayContent = new DisplayContent(context.getDisplay(), sWm, sLayersController,
77                new WallpaperController(sWm));
78
79        // Set-up some common windows.
80        sWallpaperWindow = createWindow(null, TYPE_WALLPAPER, sDisplayContent, "wallpaperWindow");
81        sImeWindow = createWindow(null, TYPE_INPUT_METHOD, sDisplayContent, "sImeWindow");
82        sImeDialogWindow =
83                createWindow(null, TYPE_INPUT_METHOD_DIALOG, sDisplayContent, "sImeDialogWindow");
84        sStatusBarWindow = createWindow(null, TYPE_STATUS_BAR, sDisplayContent, "sStatusBarWindow");
85        final WindowToken statusBarToken = sStatusBarWindow.mToken;
86        sNavBarWindow =
87                createWindow(null, TYPE_NAVIGATION_BAR, statusBarToken, "sNavBarWindow");
88        sDockedDividerWindow =
89                createWindow(null, TYPE_DOCK_DIVIDER, statusBarToken, "sDockedDividerWindow");
90        sAppWindow = createWindow(null, TYPE_BASE_APPLICATION, sDisplayContent, "sAppWindow");
91        sChildAppWindowAbove = createWindow(sAppWindow,
92                TYPE_APPLICATION_ATTACHED_DIALOG, sAppWindow.mToken, "sChildAppWindowAbove");
93        sChildAppWindowBelow = createWindow(sAppWindow,
94                TYPE_APPLICATION_MEDIA_OVERLAY, sAppWindow.mToken, "sChildAppWindowBelow");
95    }
96
97    /** Asserts that the first entry is greater than the second entry. */
98    void assertGreaterThan(int first, int second) throws Exception {
99        Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
100    }
101
102    private WindowToken createWindowToken(DisplayContent dc, int type) {
103        if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
104            return new TestWindowToken(type, dc);
105        }
106
107        final int stackId = sNextStackId++;
108        dc.addStackToDisplay(stackId, true);
109        final TaskStack stack = sWm.mStackIdToStack.get(stackId);
110        final Task task = new Task(sNextTaskId++, stack, 0, sWm, null, EMPTY, false);
111        stack.addTask(task, true);
112        final TestAppWindowToken token = new TestAppWindowToken(dc);
113        task.addAppToken(0, token, 0, false);
114        return token;
115    }
116
117    WindowState createWindow(WindowState parent, int type, String name) {
118        return (parent == null)
119                ? createWindow(parent, type, sDisplayContent, name)
120                : createWindow(parent, type, parent.mToken, name);
121    }
122
123    WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
124        final WindowToken token = createWindowToken(dc, type);
125        return createWindow(parent, type, token, name);
126    }
127
128    WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
129        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
130        attrs.setTitle(name);
131
132        final WindowState w = new WindowState(sWm, mMockSession, mIWindow, token, parent, OP_NONE,
133                0, attrs, 0, 0);
134        // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
135        // adding it to the token...
136        token.addWindow(w);
137        return w;
138    }
139
140    /* Used so we can gain access to some protected members of the {@link WindowToken} class */
141    class TestWindowToken extends WindowToken {
142
143        TestWindowToken(int type, DisplayContent dc) {
144            this(type, dc, false /* persistOnEmpty */);
145        }
146
147        TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
148            super(sWm, mock(IBinder.class), type, persistOnEmpty, dc);
149        }
150
151        int getWindowsCount() {
152            return mChildren.size();
153        }
154
155        boolean hasWindow(WindowState w) {
156            return mChildren.contains(w);
157        }
158    }
159
160    /* Used so we can gain access to some protected members of the {@link AppWindowToken} class */
161    class TestAppWindowToken extends AppWindowToken {
162
163        TestAppWindowToken(DisplayContent dc) {
164            super(sWm, null, false, dc);
165        }
166
167        int getWindowsCount() {
168            return mChildren.size();
169        }
170
171        boolean hasWindow(WindowState w) {
172            return mChildren.contains(w);
173        }
174
175        WindowState getFirstChild() {
176            return mChildren.getFirst();
177        }
178
179        WindowState getLastChild() {
180            return mChildren.getLast();
181        }
182    }
183}
184