WindowTokenTests.java revision d1c3791659572529dbc749b70d1c33bda1bdbc32
12355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk/*
22355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Copyright (C) 2016 The Android Open Source Project
32355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *
42355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Licensed under the Apache License, Version 2.0 (the "License");
52355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * you may not use this file except in compliance with the License.
62355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * You may obtain a copy of the License at
72355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *
82355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *      http://www.apache.org/licenses/LICENSE-2.0
92355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *
102355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Unless required by applicable law or agreed to in writing, software
112355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * distributed under the License is distributed on an "AS IS" BASIS,
122355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * See the License for the specific language governing permissions and
142355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * limitations under the License
152355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk */
162355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
172355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkpackage com.android.server.wm;
182355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
192355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport android.content.Context;
202355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport android.os.IBinder;
212355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport android.test.AndroidTestCase;
222355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport android.test.suitebuilder.annotation.SmallTest;
232355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport android.view.IWindow;
242355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport android.view.WindowManager;
252355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport android.view.WindowManagerPolicy;
262355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
272355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
282355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
292355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
302355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkimport static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
312355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
322355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk/**
332355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Tests for the {@link WindowState} class.
342355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk *
352355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Build: mmma -j32 frameworks/base/services/tests/servicestests
362355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk
372355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk * Run: adb shell am instrument -w -e class com.android.server.wm.WindowTokenTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
382355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk */
392355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk@SmallTest
402355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burkpublic class WindowTokenTests extends AndroidTestCase {
412355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
422355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    private static WindowManagerService sWm = null;
432355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    private final WindowManagerPolicy mPolicy = new TestWindowManagerPolicy();
442355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    private final IWindow mIWindow = new TestIWindow();
452355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
462355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    @Override
472355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    public void setUp() throws Exception {
482355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        final Context context = getContext();
492355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        if (sWm == null) {
502355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk            // We only want to do this once for the test process as we don't want WM to try to
512355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk            // register a bunch of local services again.
522355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk            sWm = WindowManagerService.main(context, null, true, false, false, mPolicy);
532355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        }
542355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    }
552355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
562355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk    public void testAddWindow() throws Exception {
572355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        final TestWindowToken token = new TestWindowToken();
582355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
592355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        assertEquals(0, token.getWindowsCount());
602355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk
612355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token);
622355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
632355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
642355edbcacfcb6e852a8707d893aaca788d42fdcPhil Burk        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token);
65        final WindowState window3 = createWindow(null, TYPE_APPLICATION, token);
66
67        token.addWindow(window1);
68        // NOTE: Child windows will not be added to the token as window containers can only
69        // contain/reference their direct children.
70        token.addWindow(window11);
71        token.addWindow(window12);
72        token.addWindow(window2);
73        token.addWindow(window3);
74
75        // Should not contain the child windows that were added above.
76        assertEquals(3, token.getWindowsCount());
77        assertTrue(token.hasWindow(window1));
78        assertFalse(token.hasWindow(window11));
79        assertFalse(token.hasWindow(window12));
80        assertTrue(token.hasWindow(window2));
81        assertTrue(token.hasWindow(window3));
82    }
83
84    public void testAdjustAnimLayer() throws Exception {
85        final TestWindowToken token = new TestWindowToken();
86        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token);
87        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
88        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
89        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token);
90        final WindowState window3 = createWindow(null, TYPE_APPLICATION, token);
91
92        token.addWindow(window1);
93        token.addWindow(window2);
94        token.addWindow(window3);
95
96        final int adj = 50;
97        final int window2StartLayer = window2.mLayer = 100;
98        final int window3StartLayer = window3.mLayer = 200;
99        final int highestLayer = token.adjustAnimLayer(adj);
100
101        assertEquals(adj, window1.mWinAnimator.mAnimLayer);
102        assertEquals(adj, window11.mWinAnimator.mAnimLayer);
103        assertEquals(adj, window12.mWinAnimator.mAnimLayer);
104        assertEquals(window2StartLayer + adj, window2.mWinAnimator.mAnimLayer);
105        assertEquals(window3StartLayer + adj, window3.mWinAnimator.mAnimLayer);
106        assertEquals(window3StartLayer + adj, highestLayer);
107    }
108
109    public void testGetTopWindow() throws Exception {
110        final TestWindowToken token = new TestWindowToken();
111
112        assertNull(token.getTopWindow());
113
114        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token);
115        token.addWindow(window1);
116        assertEquals(window1, token.getTopWindow());
117        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
118        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
119        assertEquals(window12, token.getTopWindow());
120
121        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token);
122        token.addWindow(window2);
123        // Since new windows are added to the bottom of the token, we would still expect the
124        // previous one to the top.
125        assertEquals(window12, token.getTopWindow());
126    }
127
128    public void testGetWindowIndex() throws Exception {
129        final TestWindowToken token = new TestWindowToken();
130
131        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token);
132        assertEquals(-1, token.getWindowIndex(window1));
133        token.addWindow(window1);
134        assertEquals(0, token.getWindowIndex(window1));
135        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token);
136        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token);
137        // Child windows should report the same index as their parents.
138        assertEquals(0, token.getWindowIndex(window11));
139        assertEquals(0, token.getWindowIndex(window12));
140
141        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token);
142        assertEquals(-1, token.getWindowIndex(window2));
143        token.addWindow(window2);
144        // Since new windows are added to the bottom of the token, we would expect the added window
145        // to be at index 0.
146        assertEquals(0, token.getWindowIndex(window2));
147        assertEquals(1, token.getWindowIndex(window1));
148    }
149
150    private WindowState createWindow(WindowState parent, int type, WindowToken token) {
151        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
152
153        return new WindowState(sWm, null, mIWindow, token, parent, 0, 0, attrs, 0,
154                sWm.getDefaultDisplayContentLocked(), 0);
155    }
156
157    /* Used so we can gain access to some protected members of the {@link WindowToken} class */
158    private class TestWindowToken extends WindowToken {
159
160        TestWindowToken() {
161            super(sWm, null, 0, false);
162        }
163
164        int getWindowsCount() {
165            return mChildren.size();
166        }
167
168        boolean hasWindow(WindowState w) {
169            return mChildren.contains(w);
170        }
171    }
172}
173