blob: 692e08baf7db941af0dfc4e42406a87af17f0c06 [file] [log] [blame]
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001/*
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
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070019import org.junit.Test;
20import org.junit.runner.RunWith;
21
Wale Ogunwale5fc70962016-09-09 22:36:19 -070022import android.platform.test.annotations.Presubmit;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070023import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070025
26import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
27import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -080028import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070029import static org.junit.Assert.assertEquals;
30import static org.junit.Assert.assertFalse;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -080031import static org.junit.Assert.assertNotNull;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070032import static org.junit.Assert.assertNull;
33import static org.junit.Assert.assertTrue;
Wale Ogunwalecfca2582016-10-19 09:53:25 -070034import static org.mockito.Mockito.mock;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070035
36/**
Wale Ogunwaleb783fd82016-11-04 09:51:54 -070037 * Tests for the {@link WindowToken} class.
Wale Ogunwaled1c37912016-08-16 03:19:39 -070038 *
Wale Ogunwale07bcab72016-10-14 15:30:09 -070039 * Build/Install/Run:
40 * bit FrameworksServicesTests:com.android.server.wm.WindowTokenTests
Wale Ogunwaled1c37912016-08-16 03:19:39 -070041 */
42@SmallTest
Wale Ogunwale5fc70962016-09-09 22:36:19 -070043@Presubmit
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070044@RunWith(AndroidJUnit4.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080045public class WindowTokenTests extends WindowTestsBase {
Wale Ogunwaled1c37912016-08-16 03:19:39 -070046
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070047 @Test
Wale Ogunwaled1c37912016-08-16 03:19:39 -070048 public void testAddWindow() throws Exception {
Bryce Leeaf691c02017-03-20 14:20:22 -070049 final WindowTestUtils.TestWindowToken token =
Wale Ogunwale11cc5162017-04-25 20:29:13 -070050 new WindowTestUtils.TestWindowToken(0, mDisplayContent);
Wale Ogunwaled1c37912016-08-16 03:19:39 -070051
52 assertEquals(0, token.getWindowsCount());
53
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080054 final WindowState window1 = createWindow(null, TYPE_APPLICATION, token, "window1");
55 final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token, "window11");
56 final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token, "window12");
57 final WindowState window2 = createWindow(null, TYPE_APPLICATION, token, "window2");
58 final WindowState window3 = createWindow(null, TYPE_APPLICATION, token, "window3");
Wale Ogunwaled1c37912016-08-16 03:19:39 -070059
60 token.addWindow(window1);
61 // NOTE: Child windows will not be added to the token as window containers can only
62 // contain/reference their direct children.
63 token.addWindow(window11);
64 token.addWindow(window12);
65 token.addWindow(window2);
66 token.addWindow(window3);
67
68 // Should not contain the child windows that were added above.
69 assertEquals(3, token.getWindowsCount());
70 assertTrue(token.hasWindow(window1));
71 assertFalse(token.hasWindow(window11));
72 assertFalse(token.hasWindow(window12));
73 assertTrue(token.hasWindow(window2));
74 assertTrue(token.hasWindow(window3));
75 }
76
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070077 @Test
Wale Ogunwalecfca2582016-10-19 09:53:25 -070078 public void testChildRemoval() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070079 final DisplayContent dc = mDisplayContent;
Bryce Leeaf691c02017-03-20 14:20:22 -070080 final WindowTestUtils.TestWindowToken token = new WindowTestUtils.TestWindowToken(0, dc);
Wale Ogunwalecfca2582016-10-19 09:53:25 -070081
82 assertEquals(token, dc.getWindowToken(token.token));
83
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080084 final WindowState window1 = createWindow(null, TYPE_APPLICATION, token, "window1");
85 final WindowState window2 = createWindow(null, TYPE_APPLICATION, token, "window2");
Wale Ogunwalecfca2582016-10-19 09:53:25 -070086
87 window2.removeImmediately();
88 // The token should still be mapped in the display content since it still has a child.
89 assertEquals(token, dc.getWindowToken(token.token));
90
91 window1.removeImmediately();
92 // The token should have been removed from the display content since it no longer has a
93 // child.
94 assertEquals(null, dc.getWindowToken(token.token));
95 }
96
97 @Test
Wale Ogunwaled1c37912016-08-16 03:19:39 -070098 public void testAdjustAnimLayer() throws Exception {
Bryce Leeaf691c02017-03-20 14:20:22 -070099 final WindowTestUtils.TestWindowToken token =
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700100 new WindowTestUtils.TestWindowToken(0, mDisplayContent);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800101 final WindowState window1 = createWindow(null, TYPE_APPLICATION, token, "window1");
102 final WindowState window11 = createWindow(window1, FIRST_SUB_WINDOW, token, "window11");
103 final WindowState window12 = createWindow(window1, FIRST_SUB_WINDOW, token, "window12");
104 final WindowState window2 = createWindow(null, TYPE_APPLICATION, token, "window2");
105 final WindowState window3 = createWindow(null, TYPE_APPLICATION, token, "window3");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700106
Robert Carrdee1b3f2017-02-27 11:33:33 -0800107 window2.mLayer = 100;
108 window3.mLayer = 200;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700109
Robert Carrdee1b3f2017-02-27 11:33:33 -0800110 // We assign layers once, to get the base values computed by
111 // the controller.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700112 mLayersController.assignWindowLayers(mDisplayContent);
Robert Carrdee1b3f2017-02-27 11:33:33 -0800113
114 final int window1StartLayer = window1.mWinAnimator.mAnimLayer;
115 final int window11StartLayer = window11.mWinAnimator.mAnimLayer;
116 final int window12StartLayer = window12.mWinAnimator.mAnimLayer;
117 final int window2StartLayer = window2.mWinAnimator.mAnimLayer;
118 final int window3StartLayer = window3.mWinAnimator.mAnimLayer;
119
120 // Then we set an adjustment, and assign them again, they should
121 // be offset.
122 int adj = token.adj = 50;
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700123 mLayersController.assignWindowLayers(mDisplayContent);
Robert Carrdee1b3f2017-02-27 11:33:33 -0800124 final int highestLayer = token.getHighestAnimLayer();
125
126 assertEquals(window1StartLayer + adj, window1.mWinAnimator.mAnimLayer);
127 assertEquals(window11StartLayer + adj, window11.mWinAnimator.mAnimLayer);
128 assertEquals(window12StartLayer + adj, window12.mWinAnimator.mAnimLayer);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700129 assertEquals(window2StartLayer + adj, window2.mWinAnimator.mAnimLayer);
130 assertEquals(window3StartLayer + adj, window3.mWinAnimator.mAnimLayer);
131 assertEquals(window3StartLayer + adj, highestLayer);
132 }
133
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800134 /**
135 * Test that a window token isn't orphaned by the system when it is requested to be removed.
136 * Tokens should only be removed from the system when all their windows are gone.
137 */
138 @Test
139 public void testTokenRemovalProcess() throws Exception {
Bryce Leeaf691c02017-03-20 14:20:22 -0700140 final WindowTestUtils.TestWindowToken token =
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700141 new WindowTestUtils.TestWindowToken(TYPE_TOAST, mDisplayContent,
Bryce Leeaf691c02017-03-20 14:20:22 -0700142 true /* persistOnEmpty */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700143
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800144 // Verify that the token is on the display
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700145 assertNotNull(mDisplayContent.getWindowToken(token.token));
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700146
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800147 final WindowState window1 = createWindow(null, TYPE_TOAST, token, "window1");
148 final WindowState window2 = createWindow(null, TYPE_TOAST, token, "window2");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700149
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700150 mDisplayContent.removeWindowToken(token.token);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800151 // Verify that the token is no longer mapped on the display
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700152 assertNull(mDisplayContent.getWindowToken(token.token));
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800153 // Verify that the token is still attached to its parent
154 assertNotNull(token.getParent());
155 // Verify that the token windows are still around.
156 assertEquals(2, token.getWindowsCount());
157
158 window1.removeImmediately();
159 // Verify that the token is still attached to its parent
160 assertNotNull(token.getParent());
161 // Verify that the other token window is still around.
162 assertEquals(1, token.getWindowsCount());
163
164 window2.removeImmediately();
165 // Verify that the token is no-longer attached to its parent
166 assertNull(token.getParent());
167 // Verify that the token windows are no longer attached to it.
168 assertEquals(0, token.getWindowsCount());
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700169 }
170}