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