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 | |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 19 | import static android.view.InsetsController.ANIMATION_TYPE_NONE; |
| 20 | import static android.view.InsetsController.ANIMATION_TYPE_USER; |
| 21 | import static android.view.InsetsState.ITYPE_IME; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 22 | import static android.view.InsetsState.ITYPE_STATUS_BAR; |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 23 | import static android.view.WindowInsets.Type.statusBars; |
Diego Vela | a586fa3 | 2020-04-03 14:26:51 -0700 | [diff] [blame] | 24 | |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 25 | import static junit.framework.Assert.assertEquals; |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 26 | import static junit.framework.TestCase.assertFalse; |
| 27 | import static junit.framework.TestCase.assertTrue; |
| 28 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 29 | import static org.mockito.ArgumentMatchers.eq; |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 30 | import static org.mockito.Mockito.mock; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 31 | import static org.mockito.Mockito.reset; |
| 32 | import static org.mockito.Mockito.verify; |
| 33 | import static org.mockito.Mockito.verifyZeroInteractions; |
| 34 | |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 35 | import android.app.Instrumentation; |
| 36 | import android.content.Context; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 37 | import android.graphics.Point; |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [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; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 40 | import android.view.SurfaceControl.Transaction; |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 41 | import android.view.WindowManager.BadTokenException; |
| 42 | import android.view.WindowManager.LayoutParams; |
| 43 | import android.widget.TextView; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 44 | |
Diego Vela | a586fa3 | 2020-04-03 14:26:51 -0700 | [diff] [blame] | 45 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 46 | import androidx.test.platform.app.InstrumentationRegistry; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 47 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | import org.mockito.Mock; |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 52 | import org.mockito.Mockito; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 53 | import org.mockito.MockitoAnnotations; |
| 54 | |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 55 | /** |
| 56 | * Tests for {@link InsetsSourceConsumer}. |
| 57 | * |
| 58 | * <p>Build/Install/Run: |
| 59 | * atest FrameworksCoreTests:InsetsSourceConsumerTest |
| 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 InsetsSourceConsumerTest { |
| 67 | |
| 68 | private InsetsSourceConsumer mConsumer; |
| 69 | |
| 70 | private SurfaceSession mSession = new SurfaceSession(); |
| 71 | private SurfaceControl mLeash; |
| 72 | @Mock Transaction mMockTransaction; |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 73 | private InsetsSource mSpyInsetsSource; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 74 | |
| 75 | @Before |
| 76 | public void setup() { |
| 77 | MockitoAnnotations.initMocks(this); |
| 78 | mLeash = new SurfaceControl.Builder(mSession) |
| 79 | .setName("testSurface") |
| 80 | .build(); |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 81 | final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); |
| 82 | instrumentation.runOnMainSync(() -> { |
| 83 | final Context context = instrumentation.getTargetContext(); |
| 84 | // cannot mock ViewRootImpl since it's final. |
Andrii Kulian | 5877c7d | 2020-01-29 16:57:32 -0800 | [diff] [blame] | 85 | final ViewRootImpl viewRootImpl = new ViewRootImpl(context, |
| 86 | context.getDisplayNoVerify()); |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 87 | try { |
| 88 | viewRootImpl.setView(new TextView(context), new LayoutParams(), null); |
| 89 | } catch (BadTokenException e) { |
| 90 | // activity isn't running, lets ignore BadTokenException. |
| 91 | } |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 92 | InsetsState state = new InsetsState(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 93 | mSpyInsetsSource = Mockito.spy(new InsetsSource(ITYPE_STATUS_BAR)); |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 94 | state.addSource(mSpyInsetsSource); |
| 95 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 96 | mConsumer = new InsetsSourceConsumer(ITYPE_STATUS_BAR, state, |
Jorim Jaggi | bf87c15 | 2020-04-22 17:18:25 +0200 | [diff] [blame] | 97 | () -> mMockTransaction, |
| 98 | new InsetsController(new ViewRootInsetsControllerHost(viewRootImpl))); |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 99 | }); |
| 100 | instrumentation.waitForIdleSync(); |
| 101 | |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 102 | mConsumer.setControl(new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point()), |
| 103 | new int[1], new int[1]); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testHide() { |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 108 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 109 | mConsumer.hide(); |
| 110 | assertFalse("Consumer should not be visible", mConsumer.isRequestedVisible()); |
| 111 | verify(mSpyInsetsSource).setVisible(eq(false)); |
| 112 | }); |
| 113 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testShow() { |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 118 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 119 | // Insets source starts out visible |
| 120 | mConsumer.hide(); |
| 121 | mConsumer.show(false /* fromIme */); |
| 122 | assertTrue("Consumer should be visible", mConsumer.isRequestedVisible()); |
| 123 | verify(mSpyInsetsSource).setVisible(eq(false)); |
| 124 | verify(mSpyInsetsSource).setVisible(eq(true)); |
| 125 | }); |
Jorim Jaggi | 0dd0cf9 | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 126 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 130 | public void testPendingStates() { |
| 131 | InsetsState state = new InsetsState(); |
| 132 | InsetsController controller = mock(InsetsController.class); |
| 133 | InsetsSourceConsumer consumer = new InsetsSourceConsumer( |
| 134 | ITYPE_IME, state, null, controller); |
| 135 | |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 136 | InsetsSource source = new InsetsSource(ITYPE_IME); |
| 137 | source.setFrame(0, 1, 2, 3); |
Tiger Huang | 618dbe02 | 2020-06-19 00:12:55 +0800 | [diff] [blame] | 138 | consumer.updateSource(new InsetsSource(source), ANIMATION_TYPE_NONE); |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 139 | |
| 140 | // While we're animating, updates are delayed |
| 141 | source.setFrame(4, 5, 6, 7); |
Tiger Huang | 618dbe02 | 2020-06-19 00:12:55 +0800 | [diff] [blame] | 142 | consumer.updateSource(new InsetsSource(source), ANIMATION_TYPE_USER); |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 143 | assertEquals(new Rect(0, 1, 2, 3), state.peekSource(ITYPE_IME).getFrame()); |
| 144 | |
| 145 | // Finish the animation, now the pending frame should be applied |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 146 | assertTrue(consumer.notifyAnimationFinished()); |
| 147 | assertEquals(new Rect(4, 5, 6, 7), state.peekSource(ITYPE_IME).getFrame()); |
| 148 | |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 149 | // Animating again, updates are delayed |
| 150 | source.setFrame(8, 9, 10, 11); |
Tiger Huang | 618dbe02 | 2020-06-19 00:12:55 +0800 | [diff] [blame] | 151 | consumer.updateSource(new InsetsSource(source), ANIMATION_TYPE_USER); |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 152 | assertEquals(new Rect(4, 5, 6, 7), state.peekSource(ITYPE_IME).getFrame()); |
| 153 | |
| 154 | // Updating with the current frame triggers a different code path, verify this clears |
| 155 | // the pending 8, 9, 10, 11 frame: |
| 156 | source.setFrame(4, 5, 6, 7); |
Tiger Huang | 618dbe02 | 2020-06-19 00:12:55 +0800 | [diff] [blame] | 157 | consumer.updateSource(new InsetsSource(source), ANIMATION_TYPE_USER); |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 158 | |
Adrian Roos | 021f4a7 | 2020-06-05 17:06:07 +0200 | [diff] [blame] | 159 | assertFalse(consumer.notifyAnimationFinished()); |
| 160 | assertEquals(new Rect(4, 5, 6, 7), state.peekSource(ITYPE_IME).getFrame()); |
| 161 | } |
| 162 | |
| 163 | @Test |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 164 | public void testRestore() { |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 165 | InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> { |
| 166 | mConsumer.setControl(null, new int[1], new int[1]); |
| 167 | reset(mMockTransaction); |
| 168 | mConsumer.hide(); |
| 169 | verifyZeroInteractions(mMockTransaction); |
| 170 | int[] hideTypes = new int[1]; |
| 171 | mConsumer.setControl(new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point()), |
| 172 | new int[1], hideTypes); |
| 173 | assertEquals(statusBars(), hideTypes[0]); |
| 174 | }); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 175 | } |
| 176 | } |