blob: 5c16772488d0937fe1164844bea0224f8157256d [file] [log] [blame]
Adrian Roos7419a172018-05-24 18:20:51 +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 Huang5fb9ab72020-03-02 16:16:01 +080019import static android.view.View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
Tiger Huang4a7835f2019-11-06 00:07:56 +080020import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
21import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
Tiger Huang5fb9ab72020-03-02 16:16:01 +080022import static android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
23import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
24import static android.view.View.SYSTEM_UI_FLAG_LOW_PROFILE;
25import static android.view.WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TOUCH;
Tiger Huang4a7835f2019-11-06 00:07:56 +080026import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
27import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
28import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
29import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
30
Tiger Huang1da312d2020-05-13 16:41:32 +080031import static androidx.test.InstrumentationRegistry.getInstrumentation;
32
Tiger Huang4a7835f2019-11-06 00:07:56 +080033import static org.junit.Assert.assertEquals;
Tiger Huang1da312d2020-05-13 16:41:32 +080034import static org.junit.Assert.assertTrue;
Adrian Roos7419a172018-05-24 18:20:51 +020035
36import android.content.Context;
Adrian Roos7419a172018-05-24 18:20:51 +020037import android.platform.test.annotations.Presubmit;
Tiger Huang4a7835f2019-11-06 00:07:56 +080038import android.view.WindowInsets.Side;
39import android.view.WindowInsets.Type;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090040
Diego Velaa586fa32020-04-03 14:26:51 -070041import androidx.test.ext.junit.runners.AndroidJUnit4;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090042import androidx.test.filters.SmallTest;
Adrian Roos7419a172018-05-24 18:20:51 +020043
44import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47
Diego Velaa586fa32020-04-03 14:26:51 -070048/**
49 * Tests for {@link ViewRootImpl}
50 *
51 * Build/Install/Run:
52 * atest FrameworksCoreTests:ViewRootImplTest
53 */
Adrian Roos7419a172018-05-24 18:20:51 +020054@Presubmit
55@SmallTest
56@RunWith(AndroidJUnit4.class)
57public class ViewRootImplTest {
58
Tiger Huang1da312d2020-05-13 16:41:32 +080059 private ViewRootImpl mViewRootImpl;
Adrian Roos7419a172018-05-24 18:20:51 +020060
61 @Before
62 public void setUp() throws Exception {
Tiger Huang1da312d2020-05-13 16:41:32 +080063 final Context context = getInstrumentation().getTargetContext();
Adrian Roos7419a172018-05-24 18:20:51 +020064
Tiger Huang1da312d2020-05-13 16:41:32 +080065 getInstrumentation().runOnMainSync(() ->
66 mViewRootImpl = new ViewRootImpl(context, context.getDisplayNoVerify()));
Adrian Roos7419a172018-05-24 18:20:51 +020067 }
68
Tiger Huang4a7835f2019-11-06 00:07:56 +080069 @Test
Tiger Huang5fb9ab72020-03-02 16:16:01 +080070 public void adjustLayoutParamsForCompatibility_layoutFullscreen() {
Tiger Huang4a7835f2019-11-06 00:07:56 +080071 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION);
72 attrs.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
73 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
74
Tiger Huang5fb9ab72020-03-02 16:16:01 +080075 // Type.statusBars() must be removed.
Tiger Huang52724442020-01-20 21:38:42 +080076 assertEquals(0, attrs.getFitInsetsTypes() & Type.statusBars());
Tiger Huang4a7835f2019-11-06 00:07:56 +080077 }
78
79 @Test
Tiger Huang5fb9ab72020-03-02 16:16:01 +080080 public void adjustLayoutParamsForCompatibility_layoutInScreen() {
Tiger Huang4a7835f2019-11-06 00:07:56 +080081 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION);
82 attrs.flags = FLAG_LAYOUT_IN_SCREEN;
83 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
84
Tiger Huang5fb9ab72020-03-02 16:16:01 +080085 // Type.statusBars() must be removed.
Tiger Huang52724442020-01-20 21:38:42 +080086 assertEquals(0, attrs.getFitInsetsTypes() & Type.statusBars());
Tiger Huang4a7835f2019-11-06 00:07:56 +080087 }
88
89 @Test
Tiger Huang5fb9ab72020-03-02 16:16:01 +080090 public void adjustLayoutParamsForCompatibility_layoutHideNavigation() {
Tiger Huang4a7835f2019-11-06 00:07:56 +080091 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION);
92 attrs.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
93 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
94
Tiger Huang5fb9ab72020-03-02 16:16:01 +080095 // Type.systemBars() must be removed.
Tiger Huang52724442020-01-20 21:38:42 +080096 assertEquals(0, attrs.getFitInsetsTypes() & Type.systemBars());
Tiger Huang4a7835f2019-11-06 00:07:56 +080097 }
98
99 @Test
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800100 public void adjustLayoutParamsForCompatibility_toast() {
Tiger Huang4a7835f2019-11-06 00:07:56 +0800101 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_TOAST);
102 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
103
Tiger Huang1da312d2020-05-13 16:41:32 +0800104 assertTrue(attrs.isFitInsetsIgnoringVisibility());
Tiger Huang4a7835f2019-11-06 00:07:56 +0800105 }
106
107 @Test
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800108 public void adjustLayoutParamsForCompatibility_systemAlert() {
Tiger Huang4a7835f2019-11-06 00:07:56 +0800109 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_SYSTEM_ALERT);
110 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
111
Tiger Huang1da312d2020-05-13 16:41:32 +0800112 assertTrue(attrs.isFitInsetsIgnoringVisibility());
Tiger Huang4a7835f2019-11-06 00:07:56 +0800113 }
114
115 @Test
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800116 public void adjustLayoutParamsForCompatibility_fitSystemBars() {
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800117 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION);
118 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
119
120 // A window which fits system bars must fit IME, unless its type is toast or system alert.
121 assertEquals(Type.systemBars() | Type.ime(), attrs.getFitInsetsTypes());
122 }
123
124 @Test
125 public void adjustLayoutParamsForCompatibility_noAdjustLayout() {
Tiger Huang4a7835f2019-11-06 00:07:56 +0800126 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION);
127 final int types = Type.all();
128 final int sides = Side.TOP | Side.LEFT;
129 final boolean fitMaxInsets = true;
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800130 attrs.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
Tiger Huang52724442020-01-20 21:38:42 +0800131 attrs.setFitInsetsTypes(types);
132 attrs.setFitInsetsSides(sides);
133 attrs.setFitInsetsIgnoringVisibility(fitMaxInsets);
Tiger Huang4a7835f2019-11-06 00:07:56 +0800134 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
135
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800136 // Fit-insets related fields must not be adjusted due to legacy system UI visibility
137 // after calling fit-insets related methods.
Tiger Huang52724442020-01-20 21:38:42 +0800138 assertEquals(types, attrs.getFitInsetsTypes());
139 assertEquals(sides, attrs.getFitInsetsSides());
140 assertEquals(fitMaxInsets, attrs.isFitInsetsIgnoringVisibility());
Tiger Huang4a7835f2019-11-06 00:07:56 +0800141 }
142
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800143 @Test
144 public void adjustLayoutParamsForCompatibility_noAdjustAppearance() {
Tiger Huang1da312d2020-05-13 16:41:32 +0800145 final WindowInsetsController controller = mViewRootImpl.getInsetsController();
146 final WindowManager.LayoutParams attrs = mViewRootImpl.mWindowAttributes;
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800147 final int appearance = 0;
148 controller.setSystemBarsAppearance(appearance, 0xffffffff);
149 attrs.systemUiVisibility = SYSTEM_UI_FLAG_LOW_PROFILE
150 | SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
151 | SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
152 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
153
154 // Appearance must not be adjusted due to legacy system UI visibility after calling
155 // setSystemBarsAppearance.
156 assertEquals(appearance, controller.getSystemBarsAppearance());
157 }
158
159 @Test
160 public void adjustLayoutParamsForCompatibility_noAdjustBehavior() {
Tiger Huang1da312d2020-05-13 16:41:32 +0800161 final WindowInsetsController controller = mViewRootImpl.getInsetsController();
162 final WindowManager.LayoutParams attrs = mViewRootImpl.mWindowAttributes;
Tiger Huang5fb9ab72020-03-02 16:16:01 +0800163 final int behavior = BEHAVIOR_SHOW_BARS_BY_TOUCH;
164 controller.setSystemBarsBehavior(behavior);
165 attrs.systemUiVisibility = SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
166 ViewRootImpl.adjustLayoutParamsForCompatibility(attrs);
167
168 // Behavior must not be adjusted due to legacy system UI visibility after calling
169 // setSystemBarsBehavior.
170 assertEquals(behavior, controller.getSystemBarsBehavior());
171 }
Adrian Roos7419a172018-05-24 18:20:51 +0200172}