Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [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 |
Adrian Roos | db5b0c2 | 2020-02-12 15:05:27 -0800 | [diff] [blame] | 14 | * limitations under the License. |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 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_NAVIGATION_BAR; |
| 20 | import static android.view.InsetsState.ITYPE_STATUS_BAR; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 21 | import static android.view.ViewRootImpl.NEW_INSETS_MODE_FULL; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 22 | import static android.view.WindowInsets.Type.systemBars; |
Adrian Roos | 5398997 | 2020-03-19 20:15:57 +0100 | [diff] [blame] | 23 | |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
Adrian Roos | 02a3eb6 | 2020-03-18 18:03:39 +0100 | [diff] [blame] | 25 | import static org.junit.Assert.assertFalse; |
| 26 | import static org.junit.Assert.assertTrue; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 27 | import static org.junit.Assert.fail; |
Adrian Roos | 5398997 | 2020-03-19 20:15:57 +0100 | [diff] [blame] | 28 | import static org.mockito.ArgumentMatchers.any; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 29 | import static org.mockito.ArgumentMatchers.eq; |
Jorim Jaggi | f103588 | 2020-03-31 22:47:12 +0200 | [diff] [blame] | 30 | import static org.mockito.Mockito.doAnswer; |
Adrian Roos | 5398997 | 2020-03-19 20:15:57 +0100 | [diff] [blame] | 31 | import static org.mockito.Mockito.never; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 32 | import static org.mockito.Mockito.verify; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 33 | import static org.mockito.Mockito.when; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 34 | |
| 35 | import android.graphics.Insets; |
| 36 | import android.graphics.Matrix; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 37 | import android.graphics.Point; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 38 | import android.graphics.Rect; |
| 39 | import android.graphics.RectF; |
| 40 | import android.platform.test.annotations.Presubmit; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 41 | import android.util.SparseArray; |
| 42 | import android.view.SurfaceControl.Transaction; |
| 43 | import android.view.SyncRtSurfaceTransactionApplier.SurfaceParams; |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 44 | import android.view.animation.LinearInterpolator; |
Charles Chen | 2783faf | 2019-04-16 17:39:23 +0800 | [diff] [blame] | 45 | import android.view.test.InsetsModeSession; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 46 | |
Adrian Roos | 5398997 | 2020-03-19 20:15:57 +0100 | [diff] [blame] | 47 | import androidx.test.runner.AndroidJUnit4; |
| 48 | |
Charles Chen | 2783faf | 2019-04-16 17:39:23 +0800 | [diff] [blame] | 49 | import org.junit.AfterClass; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 50 | import org.junit.Before; |
Charles Chen | 2783faf | 2019-04-16 17:39:23 +0800 | [diff] [blame] | 51 | import org.junit.BeforeClass; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 52 | import org.junit.Test; |
| 53 | import org.junit.runner.RunWith; |
| 54 | import org.mockito.ArgumentCaptor; |
| 55 | import org.mockito.Mock; |
| 56 | import org.mockito.MockitoAnnotations; |
| 57 | |
| 58 | import java.util.List; |
| 59 | |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 60 | /** |
| 61 | * Tests for {@link InsetsAnimationControlImpl}. |
| 62 | * |
| 63 | * <p>Build/Install/Run: |
| 64 | * atest FrameworksCoreTests:InsetsAnimationControlImplTest |
| 65 | * |
| 66 | * <p>This test class is a part of Window Manager Service tests and specified in |
| 67 | * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}. |
| 68 | */ |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 69 | @Presubmit |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 70 | @RunWith(AndroidJUnit4.class) |
| 71 | public class InsetsAnimationControlImplTest { |
| 72 | |
| 73 | private InsetsAnimationControlImpl mController; |
| 74 | |
| 75 | private SurfaceSession mSession = new SurfaceSession(); |
| 76 | private SurfaceControl mTopLeash; |
| 77 | private SurfaceControl mNavLeash; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 78 | private InsetsState mInsetsState; |
Charles Chen | 2783faf | 2019-04-16 17:39:23 +0800 | [diff] [blame] | 79 | private static InsetsModeSession sInsetsModeSession; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 80 | |
| 81 | @Mock Transaction mMockTransaction; |
| 82 | @Mock InsetsController mMockController; |
| 83 | @Mock WindowInsetsAnimationControlListener mMockListener; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 84 | |
Charles Chen | 2783faf | 2019-04-16 17:39:23 +0800 | [diff] [blame] | 85 | @BeforeClass |
| 86 | public static void setupOnce() { |
| 87 | sInsetsModeSession = new InsetsModeSession(NEW_INSETS_MODE_FULL); |
| 88 | } |
| 89 | |
| 90 | @AfterClass |
Jorim Jaggi | 2862047 | 2019-01-02 23:21:49 +0100 | [diff] [blame] | 91 | public static void tearDownOnce() { |
Charles Chen | 2783faf | 2019-04-16 17:39:23 +0800 | [diff] [blame] | 92 | sInsetsModeSession.close(); |
| 93 | } |
| 94 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 95 | @Before |
| 96 | public void setup() { |
| 97 | MockitoAnnotations.initMocks(this); |
| 98 | mTopLeash = new SurfaceControl.Builder(mSession) |
| 99 | .setName("testSurface") |
| 100 | .build(); |
| 101 | mNavLeash = new SurfaceControl.Builder(mSession) |
| 102 | .setName("testSurface") |
| 103 | .build(); |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 104 | mInsetsState = new InsetsState(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 105 | mInsetsState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 500, 100)); |
| 106 | mInsetsState.getSource(ITYPE_NAVIGATION_BAR).setFrame(new Rect(400, 0, 500, 500)); |
| 107 | InsetsSourceConsumer topConsumer = new InsetsSourceConsumer(ITYPE_STATUS_BAR, mInsetsState, |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 108 | () -> mMockTransaction, mMockController); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 109 | topConsumer.setControl( |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 110 | new InsetsSourceControl(ITYPE_STATUS_BAR, mTopLeash, new Point(0, 0)), |
| 111 | new int[1], new int[1]); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 112 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 113 | InsetsSourceConsumer navConsumer = new InsetsSourceConsumer(ITYPE_NAVIGATION_BAR, |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 114 | mInsetsState, () -> mMockTransaction, mMockController); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 115 | navConsumer.setControl(new InsetsSourceControl(ITYPE_NAVIGATION_BAR, mNavLeash, |
Jorim Jaggi | 3182ef1 | 2020-01-30 00:16:18 +0100 | [diff] [blame] | 116 | new Point(400, 0)), new int[1], new int[1]); |
| 117 | navConsumer.hide(); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 118 | |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 119 | SparseArray<InsetsSourceControl> controls = new SparseArray<>(); |
| 120 | controls.put(ITYPE_STATUS_BAR, topConsumer.getControl()); |
| 121 | controls.put(ITYPE_NAVIGATION_BAR, navConsumer.getControl()); |
| 122 | mController = new InsetsAnimationControlImpl(controls, |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 123 | new Rect(0, 0, 500, 500), mInsetsState, mMockListener, systemBars(), |
Jorim Jaggi | dd3304e | 2020-01-20 17:24:51 +0100 | [diff] [blame] | 124 | mMockController, 10 /* durationMs */, new LinearInterpolator(), |
Jorim Jaggi | 5875cca | 2020-03-17 13:44:57 +0100 | [diff] [blame] | 125 | 0 /* animationType */); |
Adrian Roos | 5398997 | 2020-03-19 20:15:57 +0100 | [diff] [blame] | 126 | mController.mReadyDispatched = true; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testGetters() { |
| 131 | assertEquals(Insets.of(0, 100, 100, 0), mController.getShownStateInsets()); |
| 132 | assertEquals(Insets.of(0, 0, 0, 0), mController.getHiddenStateInsets()); |
| 133 | assertEquals(Insets.of(0, 100, 0, 0), mController.getCurrentInsets()); |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 134 | assertEquals(systemBars(), mController.getTypes()); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Test |
Adrian Roos | 02a3eb6 | 2020-03-18 18:03:39 +0100 | [diff] [blame] | 138 | public void testReady() { |
| 139 | assertTrue(mController.isReady()); |
| 140 | assertFalse(mController.isFinished()); |
| 141 | assertFalse(mController.isCancelled()); |
| 142 | } |
| 143 | |
| 144 | @Test |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 145 | public void testChangeInsets() { |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 146 | mController.setInsetsAndAlpha(Insets.of(0, 30, 40, 0), 1f /* alpha */, |
| 147 | 0f /* fraction */); |
Jorim Jaggi | fae3e27 | 2019-01-14 14:05:05 +0100 | [diff] [blame] | 148 | mController.applyChangeInsets(new InsetsState()); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 149 | assertEquals(Insets.of(0, 30, 40, 0), mController.getCurrentInsets()); |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 150 | assertEquals(1f, mController.getCurrentAlpha(), 1f - mController.getCurrentAlpha()); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 151 | |
| 152 | ArgumentCaptor<SurfaceParams> captor = ArgumentCaptor.forClass(SurfaceParams.class); |
Yunfan Chen | 02abf55 | 2019-12-05 14:51:09 +0900 | [diff] [blame] | 153 | verify(mMockController).applySurfaceParams(captor.capture()); |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 154 | List<SurfaceParams> params = captor.getAllValues(); |
| 155 | assertEquals(2, params.size()); |
| 156 | SurfaceParams first = params.get(0); |
| 157 | SurfaceParams second = params.get(1); |
| 158 | SurfaceParams topParams = first.surface == mTopLeash ? first : second; |
| 159 | SurfaceParams navParams = first.surface == mNavLeash ? first : second; |
| 160 | assertPosition(topParams.matrix, new Rect(0, 0, 500, 100), new Rect(0, -70, 500, 30)); |
| 161 | assertPosition(navParams.matrix, new Rect(400, 0, 500, 500), new Rect(460, 0, 560, 500)); |
| 162 | } |
| 163 | |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 164 | @Test |
Tarandeep Singh | 699aa67 | 2019-11-22 23:19:30 -0800 | [diff] [blame] | 165 | public void testChangeAlphaNoInsets() { |
| 166 | Insets initialInsets = mController.getCurrentInsets(); |
| 167 | mController.setInsetsAndAlpha(null, 0.5f, 0f /* fraction*/); |
| 168 | mController.applyChangeInsets(new InsetsState()); |
| 169 | assertEquals(0.5f, mController.getCurrentAlpha(), 0.5f - mController.getCurrentAlpha()); |
| 170 | assertEquals(initialInsets, mController.getCurrentInsets()); |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void testChangeInsetsAndAlpha() { |
| 175 | mController.setInsetsAndAlpha(Insets.of(0, 30, 40, 0), 0.5f, 1f); |
| 176 | mController.applyChangeInsets(new InsetsState()); |
| 177 | assertEquals(0.5f, mController.getCurrentAlpha(), 0.5f - mController.getCurrentAlpha()); |
| 178 | assertEquals(Insets.of(0, 30, 40, 0), mController.getCurrentInsets()); |
| 179 | } |
| 180 | |
| 181 | @Test |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 182 | public void testFinishing() { |
| 183 | when(mMockController.getState()).thenReturn(mInsetsState); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 184 | mController.finish(true /* shown */); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 185 | mController.applyChangeInsets(mInsetsState); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 186 | assertEquals(Insets.of(0, 100, 100, 0), mController.getCurrentInsets()); |
| 187 | verify(mMockController).notifyFinished(eq(mController), eq(true /* shown */)); |
Adrian Roos | 02a3eb6 | 2020-03-18 18:03:39 +0100 | [diff] [blame] | 188 | assertFalse(mController.isReady()); |
| 189 | assertTrue(mController.isFinished()); |
| 190 | assertFalse(mController.isCancelled()); |
| 191 | verify(mMockListener).onFinished(mController); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void testCancelled() { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 196 | mController.cancel(); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 197 | try { |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 198 | mController.setInsetsAndAlpha(Insets.NONE, 1f /*alpha */, 0f /* fraction */); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 199 | fail("Expected exception to be thrown"); |
| 200 | } catch (IllegalStateException ignored) { |
| 201 | } |
Adrian Roos | 02a3eb6 | 2020-03-18 18:03:39 +0100 | [diff] [blame] | 202 | assertFalse(mController.isReady()); |
| 203 | assertFalse(mController.isFinished()); |
| 204 | assertTrue(mController.isCancelled()); |
Adrian Roos | 5d557ed | 2020-03-17 20:04:35 +0100 | [diff] [blame] | 205 | verify(mMockListener).onCancelled(mController); |
Tarandeep Singh | 54554e2 | 2019-11-01 14:43:05 -0700 | [diff] [blame] | 206 | mController.finish(true /* shown */); |
Adrian Roos | 5398997 | 2020-03-19 20:15:57 +0100 | [diff] [blame] | 207 | verify(mMockListener, never()).onFinished(any()); |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void testCancelled_beforeReadyDispatched() { |
| 212 | mController.mReadyDispatched = false; |
| 213 | mController.cancel(); |
| 214 | assertFalse(mController.isReady()); |
| 215 | assertFalse(mController.isFinished()); |
| 216 | assertTrue(mController.isCancelled()); |
| 217 | verify(mMockListener).onCancelled(null); |
| 218 | verify(mMockListener, never()).onFinished(any()); |
Jorim Jaggi | 5ed50cc | 2019-01-23 16:59:42 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Jorim Jaggi | f103588 | 2020-03-31 22:47:12 +0200 | [diff] [blame] | 221 | @Test |
| 222 | public void testFinish_immediately() { |
| 223 | when(mMockController.getState()).thenReturn(mInsetsState); |
| 224 | doAnswer(invocation -> { |
| 225 | mController.applyChangeInsets(mInsetsState); |
| 226 | return null; |
Adrian Roos | 6a4448f | 2020-04-01 15:01:08 +0200 | [diff] [blame] | 227 | }).when(mMockController).scheduleApplyChangeInsets(any()); |
Jorim Jaggi | f103588 | 2020-03-31 22:47:12 +0200 | [diff] [blame] | 228 | mController.finish(true /* shown */); |
| 229 | assertEquals(Insets.of(0, 100, 100, 0), mController.getCurrentInsets()); |
| 230 | verify(mMockController).notifyFinished(eq(mController), eq(true /* shown */)); |
| 231 | assertFalse(mController.isReady()); |
| 232 | assertTrue(mController.isFinished()); |
| 233 | assertFalse(mController.isCancelled()); |
| 234 | verify(mMockListener).onFinished(mController); |
| 235 | } |
| 236 | |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 237 | private void assertPosition(Matrix m, Rect original, Rect transformed) { |
| 238 | RectF rect = new RectF(original); |
| 239 | rect.offsetTo(0, 0); |
| 240 | m.mapRect(rect); |
| 241 | rect.round(original); |
| 242 | assertEquals(original, transformed); |
| 243 | } |
| 244 | } |