ZOrderingTests.java revision ee4d4b96657ce45d8f555dffb2f7f200f667b2a4
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 java.util.HashMap;
20import java.util.LinkedList;
21
22import org.junit.After;
23import org.junit.Assert;
24import org.junit.Before;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27
28import android.view.SurfaceControl;
29import android.view.SurfaceSession;
30import android.util.Log;
31
32import android.platform.test.annotations.Presubmit;
33import android.support.test.filters.SmallTest;
34import android.support.test.runner.AndroidJUnit4;
35
36import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
37import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
38import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
39import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
40import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
41import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
42import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
43import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
44import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
45
46/**
47 * Tests for the {@link WindowLayersController} class.
48 *
49 * Build/Install/Run:
50 *  bit FrameworksServicesTests:com.android.server.wm.ZOrderingTests
51 */
52@SmallTest
53@Presubmit
54@RunWith(AndroidJUnit4.class)
55public class ZOrderingTests extends WindowTestsBase {
56
57    private class LayerRecordingTransaction extends SurfaceControl.Transaction {
58        HashMap<SurfaceControl, Integer> mLayersForControl = new HashMap();
59        HashMap<SurfaceControl, SurfaceControl> mRelativeLayersForControl = new HashMap();
60
61        @Override
62        public SurfaceControl.Transaction setLayer(SurfaceControl sc, int layer) {
63            mRelativeLayersForControl.remove(sc);
64            mLayersForControl.put(sc, layer);
65            return super.setLayer(sc, layer);
66        }
67
68        @Override
69        public SurfaceControl.Transaction setRelativeLayer(SurfaceControl sc,
70                SurfaceControl relativeTo,
71                int layer) {
72            mRelativeLayersForControl.put(sc, relativeTo);
73            mLayersForControl.put(sc, layer);
74            return super.setRelativeLayer(sc, relativeTo, layer);
75        }
76
77        int getLayer(SurfaceControl sc) {
78            return mLayersForControl.get(sc);
79        }
80
81        SurfaceControl getRelativeLayer(SurfaceControl sc) {
82            return mRelativeLayersForControl.get(sc);
83        }
84    };
85
86    // We have WM use our Hierarchy recording subclass of SurfaceControl.Builder
87    // such that we can keep track of the parents of Surfaces as they are constructed.
88    private HashMap<SurfaceControl, SurfaceControl> mParentFor = new HashMap();
89
90    private class HierarchyRecorder extends SurfaceControl.Builder {
91        SurfaceControl mPendingParent;
92
93        HierarchyRecorder(SurfaceSession s) {
94            super(s);
95        }
96
97        public SurfaceControl.Builder setParent(SurfaceControl sc) {
98            mPendingParent = sc;
99            return super.setParent(sc);
100        }
101        public SurfaceControl build() {
102            SurfaceControl sc = super.build();
103            mParentFor.put(sc, mPendingParent);
104            mPendingParent = null;
105            return sc;
106        }
107    };
108
109    class HierarchyRecordingBuilderFactory implements SurfaceBuilderFactory {
110        public SurfaceControl.Builder make(SurfaceSession s) {
111            return new HierarchyRecorder(s);
112        }
113    };
114
115    private LayerRecordingTransaction mTransaction;
116
117    @Override
118    void beforeCreateDisplay() {
119        // We can't use @Before here because it may happen after WindowTestsBase @Before
120        // which is after construction of the DisplayContent, meaning the HierarchyRecorder
121        // would miss construction of the top-level layers.
122        mTransaction = new LayerRecordingTransaction();
123        sWm.mSurfaceBuilderFactory = new HierarchyRecordingBuilderFactory();
124    }
125
126    @After
127    public void after() {
128        mTransaction.close();
129        mParentFor.clear();
130    }
131
132    LinkedList<SurfaceControl> getAncestors(LayerRecordingTransaction t, SurfaceControl sc) {
133        LinkedList<SurfaceControl> p = new LinkedList();
134        SurfaceControl current = sc;
135        do {
136            p.addLast(current);
137
138            SurfaceControl rs = t.getRelativeLayer(current);
139            if (rs != null) {
140                current = rs;
141            } else {
142                current = mParentFor.get(current);
143            }
144        } while (current != null);
145        return p;
146    }
147
148    void assertZOrderGreaterThan(LayerRecordingTransaction t,
149            SurfaceControl left, SurfaceControl right) throws Exception {
150        final LinkedList<SurfaceControl> leftParentChain = getAncestors(t, left);
151        final LinkedList<SurfaceControl> rightParentChain = getAncestors(t, right);
152
153        SurfaceControl commonAncestor = null;
154        SurfaceControl leftTop = leftParentChain.peekLast();
155        SurfaceControl rightTop = rightParentChain.peekLast();
156        while (leftTop != null && rightTop != null && leftTop == rightTop) {
157            commonAncestor = leftParentChain.removeLast();
158            rightParentChain.removeLast();
159            leftTop = leftParentChain.peekLast();
160            rightTop = rightParentChain.peekLast();
161        }
162
163        if (rightTop == null) { // right is the parent of left.
164            assertGreaterThan(t.getLayer(leftTop), 0);
165        } else if (leftTop == null) { // left is the parent of right.
166            assertGreaterThan(0, t.getLayer(rightTop));
167        } else {
168            assertGreaterThan(t.getLayer(leftTop),
169                    t.getLayer(rightTop));
170        }
171    }
172
173    void assertWindowLayerGreaterThan(LayerRecordingTransaction t,
174            WindowState left, WindowState right) throws Exception {
175        assertZOrderGreaterThan(t, left.getSurfaceControl(), right.getSurfaceControl());
176    }
177
178    @Test
179    public void testAssignWindowLayers_ForImeWithNoTarget() throws Exception {
180        sWm.mInputMethodTarget = null;
181        mDisplayContent.assignChildLayers(mTransaction);
182
183        // The Ime has an higher base layer than app windows and lower base layer than system
184        // windows, so it should be above app windows and below system windows if there isn't an IME
185        // target.
186        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mChildAppWindowAbove);
187        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mAppWindow);
188        assertWindowLayerGreaterThan(mTransaction, mNavBarWindow, mImeWindow);
189        assertWindowLayerGreaterThan(mTransaction, mStatusBarWindow, mImeWindow);
190
191        // And, IME dialogs should always have an higher layer than the IME.
192        assertWindowLayerGreaterThan(mTransaction, mImeDialogWindow, mImeWindow);
193    }
194
195    @Test
196    public void testAssignWindowLayers_ForImeWithAppTarget() throws Exception {
197        final WindowState imeAppTarget =
198                createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "imeAppTarget");
199        sWm.mInputMethodTarget = imeAppTarget;
200        mDisplayContent.assignChildLayers(mTransaction);
201
202        // Ime should be above all app windows and below system windows if it is targeting an app
203        // window.
204        assertWindowLayerGreaterThan(mTransaction, mImeWindow, imeAppTarget);
205        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mChildAppWindowAbove);
206        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mAppWindow);
207        assertWindowLayerGreaterThan(mTransaction, mNavBarWindow, mImeWindow);
208        assertWindowLayerGreaterThan(mTransaction, mStatusBarWindow, mImeWindow);
209
210        // And, IME dialogs should always have an higher layer than the IME.
211        assertWindowLayerGreaterThan(mTransaction, mImeDialogWindow, mImeWindow);
212    }
213
214    @Test
215    public void testAssignWindowLayers_ForImeWithAppTargetWithChildWindows() throws Exception {
216        final WindowState imeAppTarget =
217                createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "imeAppTarget");
218        final WindowState imeAppTargetChildAboveWindow = createWindow(imeAppTarget,
219                TYPE_APPLICATION_ATTACHED_DIALOG, imeAppTarget.mToken,
220                "imeAppTargetChildAboveWindow");
221        final WindowState imeAppTargetChildBelowWindow = createWindow(imeAppTarget,
222                TYPE_APPLICATION_MEDIA_OVERLAY, imeAppTarget.mToken,
223                "imeAppTargetChildBelowWindow");
224
225        sWm.mInputMethodTarget = imeAppTarget;
226        mDisplayContent.assignChildLayers(mTransaction);
227
228        // Ime should be above all app windows except for child windows that are z-ordered above it
229        // and below system windows if it is targeting an app window.
230        assertWindowLayerGreaterThan(mTransaction, mImeWindow, imeAppTarget);
231        assertWindowLayerGreaterThan(mTransaction, imeAppTargetChildAboveWindow, mImeWindow);
232        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mChildAppWindowAbove);
233        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mAppWindow);
234        assertWindowLayerGreaterThan(mTransaction, mNavBarWindow, mImeWindow);
235        assertWindowLayerGreaterThan(mTransaction, mStatusBarWindow, mImeWindow);
236
237        // And, IME dialogs should always have an higher layer than the IME.
238        assertWindowLayerGreaterThan(mTransaction, mImeDialogWindow, mImeWindow);
239    }
240
241    @Test
242    public void testAssignWindowLayers_ForImeWithAppTargetAndAppAbove() throws Exception {
243        final WindowState appBelowImeTarget =
244                createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "appBelowImeTarget");
245        final WindowState imeAppTarget =
246                createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "imeAppTarget");
247        final WindowState appAboveImeTarget =
248                createWindow(null, TYPE_BASE_APPLICATION, mDisplayContent, "appAboveImeTarget");
249
250        sWm.mInputMethodTarget = imeAppTarget;
251        mDisplayContent.assignChildLayers(mTransaction);
252
253        // Ime should be above all app windows except for non-fullscreen app window above it and
254        // below system windows if it is targeting an app window.
255        assertWindowLayerGreaterThan(mTransaction, mImeWindow, imeAppTarget);
256        assertWindowLayerGreaterThan(mTransaction, mImeWindow, appBelowImeTarget);
257        assertWindowLayerGreaterThan(mTransaction, appAboveImeTarget, mImeWindow);
258        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mChildAppWindowAbove);
259        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mAppWindow);
260        assertWindowLayerGreaterThan(mTransaction, mNavBarWindow, mImeWindow);
261        assertWindowLayerGreaterThan(mTransaction, mStatusBarWindow, mImeWindow);
262
263        // And, IME dialogs should always have an higher layer than the IME.
264        assertWindowLayerGreaterThan(mTransaction, mImeDialogWindow, mImeWindow);
265    }
266
267    @Test
268    public void testAssignWindowLayers_ForImeNonAppImeTarget() throws Exception {
269        final WindowState imeSystemOverlayTarget = createWindow(null, TYPE_SYSTEM_OVERLAY,
270                mDisplayContent, "imeSystemOverlayTarget",
271                true /* ownerCanAddInternalSystemWindow */);
272
273        sWm.mInputMethodTarget = imeSystemOverlayTarget;
274        mDisplayContent.assignChildLayers(mTransaction);
275
276        // The IME target base layer is higher than all window except for the nav bar window, so the
277        // IME should be above all windows except for the nav bar.
278        assertWindowLayerGreaterThan(mTransaction, mImeWindow, imeSystemOverlayTarget);
279        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mChildAppWindowAbove);
280        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mAppWindow);
281        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mDockedDividerWindow);
282
283        // The IME has a higher base layer than the status bar so we may expect it to go
284        // above the status bar once they are both in the Non-App layer, as past versions of this
285        // test enforced. However this seems like the wrong behavior unless the status bar is the
286        // IME target.
287        assertWindowLayerGreaterThan(mTransaction, mNavBarWindow, mImeWindow);
288        assertWindowLayerGreaterThan(mTransaction, mStatusBarWindow, mImeWindow);
289
290        // And, IME dialogs should always have an higher layer than the IME.
291        assertWindowLayerGreaterThan(mTransaction, mImeDialogWindow, mImeWindow);
292    }
293
294    @Test
295    public void testAssignWindowLayers_ForStatusBarImeTarget() throws Exception {
296        sWm.mInputMethodTarget = mStatusBarWindow;
297        mDisplayContent.assignChildLayers(mTransaction);
298
299        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mChildAppWindowAbove);
300        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mAppWindow);
301        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mDockedDividerWindow);
302        assertWindowLayerGreaterThan(mTransaction, mImeWindow, mStatusBarWindow);
303
304        // And, IME dialogs should always have an higher layer than the IME.
305        assertWindowLayerGreaterThan(mTransaction, mImeDialogWindow, mImeWindow);
306    }
307
308    @Test
309    public void testStackLayers() throws Exception {
310        final WindowState pinnedStackWindow = createWindowOnStack(null, WINDOWING_MODE_PINNED,
311                ACTIVITY_TYPE_STANDARD, TYPE_BASE_APPLICATION, mDisplayContent,
312                "pinnedStackWindow");
313        final WindowState dockedStackWindow = createWindowOnStack(null,
314                WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_STANDARD, TYPE_BASE_APPLICATION,
315                mDisplayContent, "dockedStackWindow");
316        final WindowState assistantStackWindow = createWindowOnStack(null, WINDOWING_MODE_FULLSCREEN,
317                ACTIVITY_TYPE_ASSISTANT, TYPE_BASE_APPLICATION,
318                mDisplayContent, "assistantStackWindow");
319
320        mDisplayContent.assignChildLayers(mTransaction);
321
322        assertWindowLayerGreaterThan(mTransaction, dockedStackWindow, mAppWindow);
323        assertWindowLayerGreaterThan(mTransaction, assistantStackWindow, dockedStackWindow);
324        assertWindowLayerGreaterThan(mTransaction, pinnedStackWindow, assistantStackWindow);
325    }
326}
327