Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 19 | import org.junit.Test; |
| 20 | import org.junit.runner.RunWith; |
| 21 | |
Wale Ogunwale | 5fc7096 | 2016-09-09 22:36:19 -0700 | [diff] [blame] | 22 | import android.platform.test.annotations.Presubmit; |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 23 | import android.support.test.filters.SmallTest; |
| 24 | import android.support.test.runner.AndroidJUnit4; |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 25 | |
| 26 | import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW; |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 27 | import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 28 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 29 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 30 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD; |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertFalse; |
| 33 | import static org.junit.Assert.assertNull; |
| 34 | import static org.junit.Assert.assertTrue; |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Tests for the {@link WindowState} class. |
| 38 | * |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 39 | * Build/Install/Run: |
| 40 | * bit FrameworksServicesTests:com.android.server.wm.WindowStateTests |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 41 | */ |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 42 | @SmallTest |
Wale Ogunwale | 5fc7096 | 2016-09-09 22:36:19 -0700 | [diff] [blame] | 43 | @Presubmit |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 44 | @RunWith(AndroidJUnit4.class) |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 45 | public class WindowStateTests extends WindowTestsBase { |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 46 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 47 | @Test |
Wale Ogunwale | 9d14790 | 2016-07-16 11:58:55 -0700 | [diff] [blame] | 48 | public void testIsParentWindowHidden() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 49 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 50 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1"); |
| 51 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2"); |
Wale Ogunwale | 9d14790 | 2016-07-16 11:58:55 -0700 | [diff] [blame] | 52 | |
| 53 | assertFalse(parentWindow.mHidden); |
| 54 | assertFalse(parentWindow.isParentWindowHidden()); |
| 55 | assertFalse(child1.isParentWindowHidden()); |
| 56 | assertFalse(child2.isParentWindowHidden()); |
| 57 | |
| 58 | parentWindow.mHidden = true; |
| 59 | assertFalse(parentWindow.isParentWindowHidden()); |
| 60 | assertTrue(child1.isParentWindowHidden()); |
| 61 | assertTrue(child2.isParentWindowHidden()); |
| 62 | } |
| 63 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 64 | @Test |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 65 | public void testIsChildWindow() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 66 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 67 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1"); |
| 68 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2"); |
| 69 | final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow"); |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 70 | |
| 71 | assertFalse(parentWindow.isChildWindow()); |
| 72 | assertTrue(child1.isChildWindow()); |
| 73 | assertTrue(child2.isChildWindow()); |
| 74 | assertFalse(randomWindow.isChildWindow()); |
| 75 | } |
| 76 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 77 | @Test |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 78 | public void testHasChild() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 79 | final WindowState win1 = createWindow(null, TYPE_APPLICATION, "win1"); |
| 80 | final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW, "win11"); |
| 81 | final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW, "win12"); |
| 82 | final WindowState win2 = createWindow(null, TYPE_APPLICATION, "win2"); |
| 83 | final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW, "win21"); |
| 84 | final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow"); |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 85 | |
| 86 | assertTrue(win1.hasChild(win11)); |
| 87 | assertTrue(win1.hasChild(win12)); |
| 88 | assertTrue(win2.hasChild(win21)); |
| 89 | |
| 90 | assertFalse(win1.hasChild(win21)); |
| 91 | assertFalse(win1.hasChild(randomWindow)); |
| 92 | |
| 93 | assertFalse(win2.hasChild(win11)); |
| 94 | assertFalse(win2.hasChild(win12)); |
| 95 | assertFalse(win2.hasChild(randomWindow)); |
| 96 | } |
| 97 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 98 | @Test |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 99 | public void testGetParentWindow() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 100 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 101 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1"); |
| 102 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2"); |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 103 | |
| 104 | assertNull(parentWindow.getParentWindow()); |
| 105 | assertEquals(parentWindow, child1.getParentWindow()); |
| 106 | assertEquals(parentWindow, child2.getParentWindow()); |
| 107 | } |
| 108 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 109 | @Test |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 110 | public void testGetTopParentWindow() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 111 | final WindowState root = createWindow(null, TYPE_APPLICATION, "root"); |
| 112 | final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1"); |
| 113 | final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2"); |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 114 | |
| 115 | assertEquals(root, root.getTopParentWindow()); |
| 116 | assertEquals(root, child1.getTopParentWindow()); |
| 117 | assertEquals(child1, child2.getParentWindow()); |
| 118 | assertEquals(root, child2.getTopParentWindow()); |
Wale Ogunwale | ea92d97 | 2016-12-08 07:33:13 -0800 | [diff] [blame^] | 119 | |
| 120 | // Test case were child is detached from parent. |
| 121 | root.removeChild(child1); |
| 122 | assertEquals(child1, child1.getTopParentWindow()); |
| 123 | assertEquals(child1, child2.getParentWindow()); |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Jorim Jaggi | af221d1 | 2016-11-15 14:59:57 -0800 | [diff] [blame] | 126 | @Test |
| 127 | public void testIsOnScreen_hiddenByPolicy() { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 128 | final WindowState window = createWindow(null, TYPE_APPLICATION, "window"); |
Jorim Jaggi | af221d1 | 2016-11-15 14:59:57 -0800 | [diff] [blame] | 129 | window.setHasSurface(true); |
| 130 | assertTrue(window.isOnScreen()); |
| 131 | window.hideLw(false /* doAnimation */); |
| 132 | assertFalse(window.isOnScreen()); |
| 133 | } |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 134 | |
| 135 | @Test |
| 136 | public void testCanBeImeTarget() throws Exception { |
| 137 | final WindowState appWindow = createWindow(null, TYPE_APPLICATION, "appWindow"); |
| 138 | final WindowState imeWindow = createWindow(null, TYPE_INPUT_METHOD, "imeWindow"); |
| 139 | |
| 140 | // Setting FLAG_NOT_FOCUSABLE without FLAG_ALT_FOCUSABLE_IM prevents the window from being |
| 141 | // an IME target. |
| 142 | appWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE; |
| 143 | imeWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE; |
| 144 | |
| 145 | // Make windows visible |
| 146 | appWindow.setHasSurface(true); |
| 147 | imeWindow.setHasSurface(true); |
| 148 | |
| 149 | // Windows without flags (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM) can't be IME targets |
| 150 | assertFalse(appWindow.canBeImeTarget()); |
| 151 | assertFalse(imeWindow.canBeImeTarget()); |
| 152 | |
| 153 | // Add IME target flags |
| 154 | appWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM); |
| 155 | imeWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM); |
| 156 | |
| 157 | // Visible app window with flags can be IME target while an IME window can never be an IME |
| 158 | // target regardless of its visibility or flags. |
| 159 | assertTrue(appWindow.canBeImeTarget()); |
| 160 | assertFalse(imeWindow.canBeImeTarget()); |
| 161 | |
| 162 | // Make windows invisible |
| 163 | appWindow.hideLw(false /* doAnimation */); |
| 164 | imeWindow.hideLw(false /* doAnimation */); |
| 165 | |
| 166 | // Invisible window can't be IME targets even if they have the right flags. |
| 167 | assertFalse(appWindow.canBeImeTarget()); |
| 168 | assertFalse(imeWindow.canBeImeTarget()); |
| 169 | } |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 170 | } |