WindowTokenTests.java revision 44fbdf5b1e13398e35d4bafb7236d194a51ee7af
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.os.IBinder;
25import android.platform.test.annotations.Presubmit;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29import android.view.IWindow;
30import android.view.WindowManager;
31
32import static android.app.AppOpsManager.OP_NONE;
33import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
34import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
35import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertFalse;
37import static org.junit.Assert.assertNull;
38import static org.junit.Assert.assertTrue;
39import static org.mockito.Mockito.mock;
40
41/**
42 * Tests for the {@link WindowToken} class.
43 *
44 * Build/Install/Run:
45 *  bit FrameworksServicesTests:com.android.server.wm.WindowTokenTests
46 */
47@SmallTest
48@Presubmit
49@RunWith(AndroidJUnit4.class)
50public class WindowTokenTests extends WindowTestsBase {
51
52    @Test
53    public void testAddWindow() throws Exception {
54        final TestWindowToken token = new TestWindowToken();
55
56        assertEquals(0, token.getWindowsCount());
57
58        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token, "window1");
59        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token, "window11");
60        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token, "window12");
61        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token, "window2");
62        final WindowState window3 = createWindow(null, TYPE_APPLICATION, token, "window3");
63
64        token.addWindow(window1);
65        // NOTE: Child windows will not be added to the token as window containers can only
66        // contain/reference their direct children.
67        token.addWindow(window11);
68        token.addWindow(window12);
69        token.addWindow(window2);
70        token.addWindow(window3);
71
72        // Should not contain the child windows that were added above.
73        assertEquals(3, token.getWindowsCount());
74        assertTrue(token.hasWindow(window1));
75        assertFalse(token.hasWindow(window11));
76        assertFalse(token.hasWindow(window12));
77        assertTrue(token.hasWindow(window2));
78        assertTrue(token.hasWindow(window3));
79    }
80
81    @Test
82    public void testChildRemoval() throws Exception {
83        final TestWindowToken token = new TestWindowToken();
84        final DisplayContent dc = sWm.getDefaultDisplayContentLocked();
85
86        assertEquals(token, dc.getWindowToken(token.token));
87
88        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token, "window1");
89        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token, "window2");
90        token.addWindow(window1);
91        token.addWindow(window2);
92
93        window2.removeImmediately();
94        // The token should still be mapped in the display content since it still has a child.
95        assertEquals(token, dc.getWindowToken(token.token));
96
97        window1.removeImmediately();
98        // The token should have been removed from the display content since it no longer has a
99        // child.
100        assertEquals(null, dc.getWindowToken(token.token));
101    }
102
103    @Test
104    public void testAdjustAnimLayer() throws Exception {
105        final TestWindowToken token = new TestWindowToken();
106        final WindowState window1 = createWindow(null, TYPE_APPLICATION, token, "window1");
107        final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token, "window11");
108        final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token, "window12");
109        final WindowState window2 = createWindow(null, TYPE_APPLICATION, token, "window2");
110        final WindowState window3 = createWindow(null, TYPE_APPLICATION, token, "window3");
111
112        token.addWindow(window1);
113        token.addWindow(window2);
114        token.addWindow(window3);
115
116        final int adj = 50;
117        final int window2StartLayer = window2.mLayer = 100;
118        final int window3StartLayer = window3.mLayer = 200;
119        final int highestLayer = token.adjustAnimLayer(adj);
120
121        assertEquals(adj, window1.mWinAnimator.mAnimLayer);
122        assertEquals(adj, window11.mWinAnimator.mAnimLayer);
123        assertEquals(adj, window12.mWinAnimator.mAnimLayer);
124        assertEquals(window2StartLayer + adj, window2.mWinAnimator.mAnimLayer);
125        assertEquals(window3StartLayer + adj, window3.mWinAnimator.mAnimLayer);
126        assertEquals(window3StartLayer + adj, highestLayer);
127    }
128
129    /* Used so we can gain access to some protected members of the {@link WindowToken} class */
130    private class TestWindowToken extends WindowToken {
131
132        TestWindowToken() {
133            super(sWm, mock(IBinder.class), 0, false, sWm.getDefaultDisplayContentLocked());
134        }
135
136        int getWindowsCount() {
137            return mChildren.size();
138        }
139
140        boolean hasWindow(WindowState w) {
141            return mChildren.contains(w);
142        }
143    }
144}
145