Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 android.view; |
| 18 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 19 | import static android.view.InsetsState.ITYPE_IME; |
| 20 | import static android.view.InsetsState.ITYPE_NAVIGATION_BAR; |
| 21 | import static android.view.InsetsState.ITYPE_STATUS_BAR; |
| 22 | import static android.view.WindowInsets.Type.statusBars; |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 23 | import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertNull; |
| 28 | import static org.junit.Assert.assertTrue; |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 29 | import static org.mockito.ArgumentMatchers.any; |
| 30 | import static org.mockito.ArgumentMatchers.anyInt; |
| 31 | import static org.mockito.Mockito.mock; |
| 32 | import static org.mockito.Mockito.never; |
| 33 | import static org.mockito.Mockito.verify; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 34 | |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 35 | import android.content.Context; |
| 36 | import android.graphics.Insets; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 37 | import android.graphics.Point; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 38 | import android.graphics.Rect; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 39 | import android.platform.test.annotations.Presubmit; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 40 | import android.view.WindowInsets.Type; |
| 41 | import android.view.WindowManager.BadTokenException; |
| 42 | import android.view.WindowManager.LayoutParams; |
| 43 | import android.widget.TextView; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 44 | |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 45 | import androidx.test.InstrumentationRegistry; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 46 | import androidx.test.runner.AndroidJUnit4; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 47 | |
| 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 51 | import org.mockito.ArgumentCaptor; |
| 52 | |
| 53 | import java.util.concurrent.CountDownLatch; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 54 | |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 55 | /** |
| 56 | * Tests for {@link InsetsController}. |
| 57 | * |
| 58 | * <p>Build/Install/Run: |
| 59 | * atest FrameworksCoreTests:InsetsControllerTest |
| 60 | * |
| 61 | * <p>This test class is a part of Window Manager Service tests and specified in |
| 62 | * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}. |
| 63 | */ |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 64 | @Presubmit |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 65 | @RunWith(AndroidJUnit4.class) |
| 66 | public class InsetsControllerTest { |
| 67 | |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 68 | private InsetsController mController; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 69 | private SurfaceSession mSession = new SurfaceSession(); |
| 70 | private SurfaceControl mLeash; |
| 71 | |
| 72 | @Before |
| 73 | public void setup() { |
| 74 | mLeash = new SurfaceControl.Builder(mSession) |
| 75 | .setName("testSurface") |
| 76 | .build(); |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 77 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 78 | Context context = InstrumentationRegistry.getTargetContext(); |
| 79 | // cannot mock ViewRootImpl since it's final. |
| 80 | ViewRootImpl viewRootImpl = new ViewRootImpl(context, context.getDisplay()); |
| 81 | try { |
| 82 | viewRootImpl.setView(new TextView(context), new LayoutParams(), null); |
| 83 | } catch (BadTokenException e) { |
| 84 | // activity isn't running, we will ignore BadTokenException. |
| 85 | } |
| 86 | mController = new InsetsController(viewRootImpl); |
| 87 | final Rect rect = new Rect(5, 5, 5, 5); |
| 88 | mController.calculateInsets( |
| 89 | false, |
| 90 | false, |
| 91 | new DisplayCutout( |
| 92 | Insets.of(10, 10, 10, 10), rect, rect, rect, rect), |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 93 | rect, rect, SOFT_INPUT_ADJUST_RESIZE); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 94 | mController.onFrameChanged(new Rect(0, 0, 100, 100)); |
| 95 | mController.getState().setDisplayFrame(new Rect(0, 0, 100, 100)); |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 96 | }); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 97 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testControlsChanged() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 102 | InsetsSourceControl control = |
| 103 | new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 104 | mController.onControlsChanged(new InsetsSourceControl[] { control }); |
| 105 | assertEquals(mLeash, |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 106 | mController.getSourceConsumer(ITYPE_STATUS_BAR).getControl().getLeash()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testControlsRevoked() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 111 | InsetsSourceControl control = |
| 112 | new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 113 | mController.onControlsChanged(new InsetsSourceControl[] { control }); |
| 114 | mController.onControlsChanged(new InsetsSourceControl[0]); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 115 | assertNull(mController.getSourceConsumer(ITYPE_STATUS_BAR).getControl()); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 116 | } |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 117 | |
| 118 | @Test |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 119 | public void testControlsRevoked_duringAnim() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 120 | InsetsSourceControl control = |
| 121 | new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point()); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 122 | mController.onControlsChanged(new InsetsSourceControl[] { control }); |
| 123 | |
| 124 | WindowInsetsAnimationControlListener mockListener = |
| 125 | mock(WindowInsetsAnimationControlListener.class); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 126 | mController.controlWindowInsetsAnimation(statusBars(), mockListener); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 127 | verify(mockListener).onReady(any(), anyInt()); |
| 128 | mController.onControlsChanged(new InsetsSourceControl[0]); |
| 129 | verify(mockListener).onCancelled(); |
| 130 | } |
| 131 | |
| 132 | @Test |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 133 | public void testFrameDoesntMatchDisplay() { |
| 134 | mController.onFrameChanged(new Rect(0, 0, 100, 100)); |
| 135 | mController.getState().setDisplayFrame(new Rect(0, 0, 200, 200)); |
| 136 | WindowInsetsAnimationControlListener controlListener = |
| 137 | mock(WindowInsetsAnimationControlListener.class); |
| 138 | mController.controlWindowInsetsAnimation(0, controlListener); |
| 139 | verify(controlListener).onCancelled(); |
| 140 | verify(controlListener, never()).onReady(any(), anyInt()); |
| 141 | } |
| 142 | |
| 143 | @Test |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 144 | public void testAnimationEndState() { |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 145 | InsetsSourceControl[] controls = prepareControls(); |
| 146 | InsetsSourceControl navBar = controls[0]; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 147 | InsetsSourceControl statusBar = controls[1]; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 148 | InsetsSourceControl ime = controls[2]; |
| 149 | |
| 150 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 151 | mController.getSourceConsumer(ITYPE_IME).onWindowFocusGained(); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 152 | // since there is no focused view, forcefully make IME visible. |
| 153 | mController.applyImeVisibility(true /* setVisible */); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 154 | mController.show(Type.all()); |
| 155 | // quickly jump to final state by cancelling it. |
| 156 | mController.cancelExistingAnimation(); |
| 157 | assertTrue(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 158 | assertTrue(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 159 | assertTrue(mController.getSourceConsumer(ime.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 160 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 161 | mController.applyImeVisibility(false /* setVisible */); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 162 | mController.hide(Type.all()); |
| 163 | mController.cancelExistingAnimation(); |
| 164 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 165 | assertFalse(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 166 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 167 | mController.getSourceConsumer(ITYPE_IME).onWindowFocusLost(); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 168 | }); |
| 169 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void testApplyImeVisibility() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 174 | final InsetsSourceControl ime = new InsetsSourceControl(ITYPE_IME, mLeash, new Point()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 175 | |
| 176 | InsetsSourceControl[] controls = new InsetsSourceControl[3]; |
| 177 | controls[0] = ime; |
| 178 | mController.onControlsChanged(controls); |
| 179 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 180 | mController.getSourceConsumer(ITYPE_IME).onWindowFocusGained(); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 181 | mController.applyImeVisibility(true); |
| 182 | mController.cancelExistingAnimation(); |
| 183 | assertTrue(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 184 | mController.applyImeVisibility(false); |
| 185 | mController.cancelExistingAnimation(); |
| 186 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 187 | mController.getSourceConsumer(ITYPE_IME).onWindowFocusLost(); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 188 | }); |
| 189 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void testShowHideSelectively() { |
| 194 | InsetsSourceControl[] controls = prepareControls(); |
| 195 | InsetsSourceControl navBar = controls[0]; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 196 | InsetsSourceControl statusBar = controls[1]; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 197 | InsetsSourceControl ime = controls[2]; |
| 198 | |
| 199 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 200 | int types = Type.navigationBars() | Type.systemBars(); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 201 | // test show select types. |
| 202 | mController.show(types); |
| 203 | mController.cancelExistingAnimation(); |
| 204 | assertTrue(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 205 | assertTrue(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 206 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 207 | |
| 208 | // test hide all |
| 209 | mController.hide(types); |
| 210 | mController.cancelExistingAnimation(); |
| 211 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 212 | assertFalse(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 213 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 214 | }); |
| 215 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 216 | } |
| 217 | |
| 218 | @Test |
| 219 | public void testShowHideSingle() { |
| 220 | InsetsSourceControl[] controls = prepareControls(); |
| 221 | InsetsSourceControl navBar = controls[0]; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 222 | InsetsSourceControl statusBar = controls[1]; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 223 | InsetsSourceControl ime = controls[2]; |
| 224 | |
| 225 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 226 | int types = Type.navigationBars() | Type.systemBars(); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 227 | // test show select types. |
| 228 | mController.show(types); |
| 229 | mController.cancelExistingAnimation(); |
| 230 | assertTrue(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 231 | assertTrue(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 232 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 233 | |
| 234 | // test hide all |
| 235 | mController.hide(Type.all()); |
| 236 | mController.cancelExistingAnimation(); |
| 237 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 238 | assertFalse(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 239 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 240 | |
| 241 | // test single show |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 242 | mController.show(Type.navigationBars()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 243 | mController.cancelExistingAnimation(); |
| 244 | assertTrue(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 245 | assertFalse(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 246 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 247 | |
| 248 | // test single hide |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 249 | mController.hide(Type.navigationBars()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 250 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 251 | assertFalse(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 252 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 253 | |
| 254 | }); |
| 255 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 256 | } |
| 257 | |
| 258 | @Test |
| 259 | public void testShowHideMultiple() { |
| 260 | InsetsSourceControl[] controls = prepareControls(); |
| 261 | InsetsSourceControl navBar = controls[0]; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 262 | InsetsSourceControl statusBar = controls[1]; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 263 | InsetsSourceControl ime = controls[2]; |
| 264 | |
| 265 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 266 | // start two animations and see if previous is cancelled and final state is reached. |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 267 | mController.show(Type.navigationBars()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 268 | mController.show(Type.systemBars()); |
| 269 | mController.cancelExistingAnimation(); |
| 270 | assertTrue(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 271 | assertTrue(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 272 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 273 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 274 | mController.hide(Type.navigationBars()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 275 | mController.hide(Type.systemBars()); |
| 276 | mController.cancelExistingAnimation(); |
| 277 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 278 | assertFalse(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 279 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 280 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 281 | int types = Type.navigationBars() | Type.systemBars(); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 282 | // show two at a time and hide one by one. |
| 283 | mController.show(types); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 284 | mController.hide(Type.navigationBars()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 285 | mController.cancelExistingAnimation(); |
| 286 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 287 | assertTrue(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 288 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 289 | |
| 290 | mController.hide(Type.systemBars()); |
| 291 | mController.cancelExistingAnimation(); |
| 292 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 293 | assertFalse(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 294 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 295 | }); |
| 296 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 297 | } |
| 298 | |
| 299 | @Test |
| 300 | public void testShowMultipleHideOneByOne() { |
| 301 | InsetsSourceControl[] controls = prepareControls(); |
| 302 | InsetsSourceControl navBar = controls[0]; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 303 | InsetsSourceControl statusBar = controls[1]; |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 304 | InsetsSourceControl ime = controls[2]; |
| 305 | |
| 306 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 307 | int types = Type.navigationBars() | Type.systemBars(); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 308 | // show two at a time and hide one by one. |
| 309 | mController.show(types); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 310 | mController.hide(Type.navigationBars()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 311 | mController.cancelExistingAnimation(); |
| 312 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 313 | assertTrue(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 314 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 315 | |
| 316 | mController.hide(Type.systemBars()); |
| 317 | mController.cancelExistingAnimation(); |
| 318 | assertFalse(mController.getSourceConsumer(navBar.getType()).isVisible()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 319 | assertFalse(mController.getSourceConsumer(statusBar.getType()).isVisible()); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 320 | assertFalse(mController.getSourceConsumer(ime.getType()).isVisible()); |
| 321 | }); |
| 322 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 323 | } |
| 324 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 325 | @Test |
| 326 | public void testAnimationEndState_controller() throws Exception { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 327 | InsetsSourceControl control = |
| 328 | new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point()); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 329 | mController.onControlsChanged(new InsetsSourceControl[] { control }); |
| 330 | |
| 331 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 332 | WindowInsetsAnimationControlListener mockListener = |
| 333 | mock(WindowInsetsAnimationControlListener.class); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 334 | mController.controlWindowInsetsAnimation(statusBars(), mockListener); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 335 | |
| 336 | ArgumentCaptor<WindowInsetsAnimationController> controllerCaptor = |
| 337 | ArgumentCaptor.forClass(WindowInsetsAnimationController.class); |
| 338 | verify(mockListener).onReady(controllerCaptor.capture(), anyInt()); |
| 339 | controllerCaptor.getValue().finish(0 /* shownTypes */); |
| 340 | }); |
| 341 | waitUntilNextFrame(); |
| 342 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 343 | assertFalse(mController.getSourceConsumer(ITYPE_STATUS_BAR).isVisible()); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 344 | }); |
| 345 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 346 | } |
| 347 | |
| 348 | private void waitUntilNextFrame() throws Exception { |
| 349 | final CountDownLatch latch = new CountDownLatch(1); |
| 350 | Choreographer.getMainThreadInstance().postCallback(Choreographer.CALLBACK_COMMIT, |
| 351 | latch::countDown, null /* token */); |
| 352 | latch.await(); |
| 353 | } |
| 354 | |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 355 | private InsetsSourceControl[] prepareControls() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 356 | final InsetsSourceControl navBar = new InsetsSourceControl(ITYPE_NAVIGATION_BAR, mLeash, |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 357 | new Point()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 358 | final InsetsSourceControl statusBar = new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 359 | new Point()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 360 | final InsetsSourceControl ime = new InsetsSourceControl(ITYPE_IME, mLeash, new Point()); |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 361 | |
| 362 | InsetsSourceControl[] controls = new InsetsSourceControl[3]; |
| 363 | controls[0] = navBar; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 364 | controls[1] = statusBar; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 365 | controls[2] = ime; |
| 366 | mController.onControlsChanged(controls); |
Tarandeep Singh | 46d59f0 | 2019-01-29 18:09:15 -0800 | [diff] [blame] | 367 | return controls; |
Tarandeep Singh | 22f2b4c | 2019-01-10 19:41:30 -0800 | [diff] [blame] | 368 | } |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 369 | } |