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