blob: 60620881b6f285ad00b04cdbd6e90d27df022915 [file] [log] [blame]
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001/*
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
17package android.view;
18
Tiger Huang332793b2019-10-29 23:21:27 +080019import static android.view.InsetsState.ISIDE_BOTTOM;
20import static android.view.InsetsState.ISIDE_TOP;
21import static android.view.InsetsState.ITYPE_CAPTION_BAR;
22import static android.view.InsetsState.ITYPE_IME;
23import static android.view.InsetsState.ITYPE_NAVIGATION_BAR;
24import static android.view.InsetsState.ITYPE_STATUS_BAR;
Jorim Jaggi648e5882019-01-24 13:24:02 +010025import static android.view.WindowInsets.Type.ime;
26import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090027
Tadashi G. Takaoka82eb7a52019-03-26 18:22:01 +090028import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertFalse;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020030import static org.junit.Assert.assertNotEquals;
Tadashi G. Takaoka82eb7a52019-03-26 18:22:01 +090031import static org.junit.Assert.assertTrue;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020032
Jorim Jaggibcf99ff2018-12-03 18:04:26 +010033import android.graphics.Insets;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020034import android.graphics.Rect;
35import android.os.Parcel;
36import android.platform.test.annotations.Presubmit;
Jorim Jaggi5bb571d2018-11-06 14:42:04 +010037import android.util.SparseIntArray;
Jorim Jaggibcf99ff2018-12-03 18:04:26 +010038import android.view.WindowInsets.Type;
Tarandeep Singha6f35612019-01-11 19:50:46 -080039import android.view.test.InsetsModeSession;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020040
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090041import androidx.test.runner.AndroidJUnit4;
42
Jorim Jaggif96c90a2018-09-26 16:55:15 +020043import org.junit.Test;
44import org.junit.runner.RunWith;
45
Tadashi G. Takaoka82eb7a52019-03-26 18:22:01 +090046/**
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 Jaggif96c90a2018-09-26 16:55:15 +020055@Presubmit
Jorim Jaggif96c90a2018-09-26 16:55:15 +020056@RunWith(AndroidJUnit4.class)
57public class InsetsStateTest {
58
59 private InsetsState mState = new InsetsState();
60 private InsetsState mState2 = new InsetsState();
61
62 @Test
Tarandeep Singha6f35612019-01-11 19:50:46 -080063 public void testCalculateInsets() throws Exception {
64 try (final InsetsModeSession session =
65 new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_FULL)) {
Tiger Huang332793b2019-10-29 23:21:27 +080066 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 Singha6f35612019-01-11 19:50:46 -080070 SparseIntArray typeSideMap = new SparseIntArray();
71 WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false,
Jorim Jaggi648e5882019-01-24 13:24:02 +010072 DisplayCutout.NO_CUTOUT, null, null, SOFT_INPUT_ADJUST_RESIZE, typeSideMap);
Tarandeep Singha6f35612019-01-11 19:50:46 -080073 assertEquals(Insets.of(0, 100, 0, 100), insets.getSystemWindowInsets());
74 assertEquals(Insets.of(0, 100, 0, 100), insets.getInsets(Type.all()));
Tiger Huang332793b2019-10-29 23:21:27 +080075 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 Singha6f35612019-01-11 19:50:46 -080078 assertEquals(Insets.of(0, 0, 0, 100), insets.getInsets(Type.ime()));
79 }
Jorim Jaggif96c90a2018-09-26 16:55:15 +020080 }
81
82 @Test
Tarandeep Singha6f35612019-01-11 19:50:46 -080083 public void testCalculateInsets_imeAndNav() throws Exception{
84 try (final InsetsModeSession session =
85 new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_FULL)) {
Tiger Huang332793b2019-10-29 23:21:27 +080086 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 Singha6f35612019-01-11 19:50:46 -080090 WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false,
Jorim Jaggi648e5882019-01-24 13:24:02 +010091 DisplayCutout.NO_CUTOUT, null, null, SOFT_INPUT_ADJUST_RESIZE, null);
Tarandeep Singha6f35612019-01-11 19:50:46 -080092 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 Huang332793b2019-10-29 23:21:27 +080096 assertEquals(Insets.of(0, 0, 0, 100), insets.getInsets(Type.navigationBars()));
Tarandeep Singha6f35612019-01-11 19:50:46 -080097 assertEquals(Insets.of(0, 0, 0, 200), insets.getInsets(Type.ime()));
98 }
Jorim Jaggif96c90a2018-09-26 16:55:15 +020099 }
100
101 @Test
Tarandeep Singha6f35612019-01-11 19:50:46 -0800102 public void testCalculateInsets_navRightStatusTop() throws Exception {
103 try (final InsetsModeSession session =
104 new InsetsModeSession(ViewRootImpl.NEW_INSETS_MODE_FULL)) {
Tiger Huang332793b2019-10-29 23:21:27 +0800105 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 Singha6f35612019-01-11 19:50:46 -0800109 WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false,
Jorim Jaggi648e5882019-01-24 13:24:02 +0100110 DisplayCutout.NO_CUTOUT, null, null, 0, null);
Tarandeep Singha6f35612019-01-11 19:50:46 -0800111 assertEquals(Insets.of(0, 100, 20, 0), insets.getSystemWindowInsets());
Tiger Huang332793b2019-10-29 23:21:27 +0800112 assertEquals(Insets.of(0, 100, 0, 0), insets.getInsets(Type.statusBars()));
113 assertEquals(Insets.of(0, 0, 20, 0), insets.getInsets(Type.navigationBars()));
Tarandeep Singha6f35612019-01-11 19:50:46 -0800114 }
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200115 }
116
117 @Test
Jorim Jaggi648e5882019-01-24 13:24:02 +0100118 public void testCalculateInsets_imeIgnoredWithoutAdjustResize() {
Tiger Huang332793b2019-10-29 23:21:27 +0800119 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 Jaggi648e5882019-01-24 13:24:02 +0100123 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 Jaggif96c90a2018-09-26 16:55:15 +0200130 public void testStripForDispatch() {
Tiger Huang332793b2019-10-29 23:21:27 +0800131 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 Jaggif96c90a2018-09-26 16:55:15 +0200136 WindowInsets insets = mState.calculateInsets(new Rect(0, 0, 100, 300), false, false,
Jorim Jaggi648e5882019-01-24 13:24:02 +0100137 DisplayCutout.NO_CUTOUT, null, null, SOFT_INPUT_ADJUST_RESIZE, null);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200138 assertEquals(0, insets.getSystemWindowInsetBottom());
139 }
140
141 @Test
142 public void testEquals_differentRect() {
Tiger Huang332793b2019-10-29 23:21:27 +0800143 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 Singha6f35612019-01-11 19:50:46 -0800145 assertNotEqualsAndHashCode();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200146 }
147
148 @Test
149 public void testEquals_differentSource() {
Tiger Huang332793b2019-10-29 23:21:27 +0800150 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 Singha6f35612019-01-11 19:50:46 -0800152 assertNotEqualsAndHashCode();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200153 }
154
155 @Test
156 public void testEquals_sameButDifferentInsertOrder() {
Tiger Huang332793b2019-10-29 23:21:27 +0800157 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 Singha6f35612019-01-11 19:50:46 -0800161 assertEqualsAndHashCode();
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200162 }
163
164 @Test
165 public void testEquals_visibility() {
Tiger Huang332793b2019-10-29 23:21:27 +0800166 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 Singha6f35612019-01-11 19:50:46 -0800169 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 Jaggif96c90a2018-09-26 16:55:15 +0200184 }
185
186 @Test
187 public void testParcelUnparcel() {
Tiger Huang332793b2019-10-29 23:21:27 +0800188 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 Jaggif96c90a2018-09-26 16:55:15 +0200191 Parcel p = Parcel.obtain();
192 mState.writeToParcel(p, 0 /* flags */);
Tarandeep Singha6f35612019-01-11 19:50:46 -0800193 p.setDataPosition(0);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200194 mState2.readFromParcel(p);
195 p.recycle();
196 assertEquals(mState, mState2);
197 }
Jorim Jaggie35c0592018-11-06 16:21:08 +0100198
199 @Test
200 public void testGetDefaultVisibility() {
Tiger Huang332793b2019-10-29 23:21:27 +0800201 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 Jaggie35c0592018-11-06 16:21:08 +0100205 }
Tarandeep Singha6f35612019-01-11 19:50:46 -0800206
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 Jaggif96c90a2018-09-26 16:55:15 +0200216}