Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +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.ISIDE_BOTTOM; |
| 20 | import static android.view.InsetsState.ISIDE_TOP; |
| 21 | import static android.view.InsetsState.ITYPE_CAPTION_BAR; |
| 22 | import static android.view.InsetsState.ITYPE_IME; |
| 23 | import static android.view.InsetsState.ITYPE_NAVIGATION_BAR; |
| 24 | import static android.view.InsetsState.ITYPE_STATUS_BAR; |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 25 | import static android.view.WindowInsets.Type.ime; |
| 26 | import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 27 | |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.junit.Assert.assertFalse; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 30 | import static org.junit.Assert.assertNotEquals; |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 31 | import static org.junit.Assert.assertTrue; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 32 | |
Jorim Jaggi | bcf99ff | 2018-12-03 18:04:26 +0100 | [diff] [blame] | 33 | import android.graphics.Insets; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 34 | import android.graphics.Rect; |
| 35 | import android.os.Parcel; |
| 36 | import android.platform.test.annotations.Presubmit; |
Jorim Jaggi | 5bb571d | 2018-11-06 14:42:04 +0100 | [diff] [blame] | 37 | import android.util.SparseIntArray; |
Jorim Jaggi | bcf99ff | 2018-12-03 18:04:26 +0100 | [diff] [blame] | 38 | import android.view.WindowInsets.Type; |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 39 | import android.view.test.InsetsModeSession; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 40 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 41 | import androidx.test.runner.AndroidJUnit4; |
| 42 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 46 | /** |
| 47 | * Tests for {@link InsetsState}. |
| 48 | * |
| 49 | * <p>Build/Install/Run: |
| 50 | * atest FrameworksCoreTests:InsetsStateTest |
| 51 | * |
| 52 | * <p>This test class is a part of Window Manager Service tests and specified in |
| 53 | * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}. |
| 54 | */ |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 55 | @Presubmit |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 56 | @RunWith(AndroidJUnit4.class) |
| 57 | public class InsetsStateTest { |
| 58 | |
| 59 | private InsetsState mState = new InsetsState(); |
| 60 | private InsetsState mState2 = new InsetsState(); |
| 61 | |
| 62 | @Test |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 63 | public void testCalculateInsets() throws Exception { |
| 64 | try (final InsetsModeSession session = |
| 65 | new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_FULL)) { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 66 | mState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
| 67 | mState.getSource(ITYPE_STATUS_BAR).setVisible(true); |
| 68 | mState.getSource(ITYPE_IME).setFrame(new Rect(0, 200, 100, 300)); |
| 69 | mState.getSource(ITYPE_IME).setVisible(true); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 70 | SparseIntArray typeSideMap = new SparseIntArray(); |
| 71 | WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false, |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 72 | DisplayCutout.NO_CUTOUT, null, null, SOFT_INPUT_ADJUST_RESIZE, typeSideMap); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 73 | assertEquals(Insets.of(0, 100, 0, 100), insets.getSystemWindowInsets()); |
| 74 | assertEquals(Insets.of(0, 100, 0, 100), insets.getInsets(Type.all())); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 75 | assertEquals(ISIDE_TOP, typeSideMap.get(ITYPE_STATUS_BAR)); |
| 76 | assertEquals(ISIDE_BOTTOM, typeSideMap.get(ITYPE_IME)); |
| 77 | assertEquals(Insets.of(0, 100, 0, 0), insets.getInsets(Type.statusBars())); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 78 | assertEquals(Insets.of(0, 0, 0, 100), insets.getInsets(Type.ime())); |
| 79 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Test |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 83 | public void testCalculateInsets_imeAndNav() throws Exception{ |
| 84 | try (final InsetsModeSession session = |
| 85 | new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_FULL)) { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 86 | mState.getSource(ITYPE_NAVIGATION_BAR).setFrame(new Rect(0, 200, 100, 300)); |
| 87 | mState.getSource(ITYPE_NAVIGATION_BAR).setVisible(true); |
| 88 | mState.getSource(ITYPE_IME).setFrame(new Rect(0, 100, 100, 300)); |
| 89 | mState.getSource(ITYPE_IME).setVisible(true); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 90 | WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false, |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 91 | DisplayCutout.NO_CUTOUT, null, null, SOFT_INPUT_ADJUST_RESIZE, null); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 92 | assertEquals(100, insets.getStableInsetBottom()); |
| 93 | assertEquals(Insets.of(0, 0, 0, 100), insets.getMaxInsets(Type.systemBars())); |
| 94 | assertEquals(Insets.of(0, 0, 0, 200), insets.getSystemWindowInsets()); |
| 95 | assertEquals(Insets.of(0, 0, 0, 200), insets.getInsets(Type.all())); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 96 | assertEquals(Insets.of(0, 0, 0, 100), insets.getInsets(Type.navigationBars())); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 97 | assertEquals(Insets.of(0, 0, 0, 200), insets.getInsets(Type.ime())); |
| 98 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 102 | public void testCalculateInsets_navRightStatusTop() throws Exception { |
| 103 | try (final InsetsModeSession session = |
| 104 | new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_FULL)) { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 105 | mState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
| 106 | mState.getSource(ITYPE_STATUS_BAR).setVisible(true); |
| 107 | mState.getSource(ITYPE_NAVIGATION_BAR).setFrame(new Rect(80, 0, 100, 300)); |
| 108 | mState.getSource(ITYPE_NAVIGATION_BAR).setVisible(true); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 109 | WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false, |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 110 | DisplayCutout.NO_CUTOUT, null, null, 0, null); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 111 | assertEquals(Insets.of(0, 100, 20, 0), insets.getSystemWindowInsets()); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 112 | assertEquals(Insets.of(0, 100, 0, 0), insets.getInsets(Type.statusBars())); |
| 113 | assertEquals(Insets.of(0, 0, 20, 0), insets.getInsets(Type.navigationBars())); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 114 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Test |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 118 | public void testCalculateInsets_imeIgnoredWithoutAdjustResize() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 119 | mState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
| 120 | mState.getSource(ITYPE_STATUS_BAR).setVisible(true); |
| 121 | mState.getSource(ITYPE_IME).setFrame(new Rect(0, 200, 100, 300)); |
| 122 | mState.getSource(ITYPE_IME).setVisible(true); |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 123 | WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false, |
| 124 | DisplayCutout.NO_CUTOUT, null, null, 0, null); |
| 125 | assertEquals(0, insets.getSystemWindowInsetBottom()); |
| 126 | assertTrue(insets.isVisible(ime())); |
| 127 | } |
| 128 | |
| 129 | @Test |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 130 | public void testStripForDispatch() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 131 | mState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
| 132 | mState.getSource(ITYPE_STATUS_BAR).setVisible(true); |
| 133 | mState.getSource(ITYPE_IME).setFrame(new Rect(0, 200, 100, 300)); |
| 134 | mState.getSource(ITYPE_IME).setVisible(true); |
| 135 | mState.removeSource(ITYPE_IME); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 136 | WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false, |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 137 | DisplayCutout.NO_CUTOUT, null, null, SOFT_INPUT_ADJUST_RESIZE, null); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 138 | assertEquals(0, insets.getSystemWindowInsetBottom()); |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void testEquals_differentRect() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 143 | mState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
| 144 | mState2.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 10, 10)); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 145 | assertNotEqualsAndHashCode(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void testEquals_differentSource() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 150 | mState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
| 151 | mState2.getSource(ITYPE_IME).setFrame(new Rect(0, 0, 100, 100)); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 152 | assertNotEqualsAndHashCode(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void testEquals_sameButDifferentInsertOrder() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 157 | mState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
| 158 | mState.getSource(ITYPE_IME).setFrame(new Rect(0, 0, 100, 100)); |
| 159 | mState2.getSource(ITYPE_IME).setFrame(new Rect(0, 0, 100, 100)); |
| 160 | mState2.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 161 | assertEqualsAndHashCode(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testEquals_visibility() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 166 | mState.getSource(ITYPE_IME).setFrame(new Rect(0, 0, 100, 100)); |
| 167 | mState.getSource(ITYPE_IME).setVisible(true); |
| 168 | mState2.getSource(ITYPE_IME).setFrame(new Rect(0, 0, 100, 100)); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 169 | assertNotEqualsAndHashCode(); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void testEquals_differentFrame() { |
| 174 | mState.setDisplayFrame(new Rect(0, 1, 2, 3)); |
| 175 | mState.setDisplayFrame(new Rect(4, 5, 6, 7)); |
| 176 | assertNotEqualsAndHashCode(); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testEquals_sameFrame() { |
| 181 | mState.setDisplayFrame(new Rect(0, 1, 2, 3)); |
| 182 | mState2.setDisplayFrame(new Rect(0, 1, 2, 3)); |
| 183 | assertEqualsAndHashCode(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void testParcelUnparcel() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 188 | mState.getSource(ITYPE_IME).setFrame(new Rect(0, 0, 100, 100)); |
| 189 | mState.getSource(ITYPE_IME).setVisible(true); |
| 190 | mState.getSource(ITYPE_STATUS_BAR).setFrame(new Rect(0, 0, 100, 100)); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 191 | Parcel p = Parcel.obtain(); |
| 192 | mState.writeToParcel(p, 0 /* flags */); |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 193 | p.setDataPosition(0); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 194 | mState2.readFromParcel(p); |
| 195 | p.recycle(); |
| 196 | assertEquals(mState, mState2); |
| 197 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 198 | |
| 199 | @Test |
| 200 | public void testGetDefaultVisibility() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 201 | assertTrue(InsetsState.getDefaultVisibility(ITYPE_STATUS_BAR)); |
| 202 | assertTrue(InsetsState.getDefaultVisibility(ITYPE_NAVIGATION_BAR)); |
| 203 | assertTrue(InsetsState.getDefaultVisibility(ITYPE_CAPTION_BAR)); |
| 204 | assertFalse(InsetsState.getDefaultVisibility(ITYPE_IME)); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 205 | } |
Tarandeep Singh | a6f3561 | 2019-01-11 19:50:46 -0800 | [diff] [blame] | 206 | |
| 207 | private void assertEqualsAndHashCode() { |
| 208 | assertEquals(mState, mState2); |
| 209 | assertEquals(mState.hashCode(), mState2.hashCode()); |
| 210 | } |
| 211 | |
| 212 | private void assertNotEqualsAndHashCode() { |
| 213 | assertNotEquals(mState, mState2); |
| 214 | assertNotEquals(mState.hashCode(), mState2.hashCode()); |
| 215 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 216 | } |