Adrian Roos | 7419a17 | 2018-05-24 18:20:51 +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 | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 19 | import static android.view.View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 20 | import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; |
| 21 | import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 22 | import static android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 23 | import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 24 | import static android.view.View.SYSTEM_UI_FLAG_LOW_PROFILE; |
| 25 | import static android.view.WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TOUCH; |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 26 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; |
| 27 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; |
| 28 | import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; |
| 29 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
| 30 | |
Tiger Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 31 | import static androidx.test.InstrumentationRegistry.getInstrumentation; |
| 32 | |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 33 | import static org.junit.Assert.assertEquals; |
Tiger Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 34 | import static org.junit.Assert.assertTrue; |
Adrian Roos | 7419a17 | 2018-05-24 18:20:51 +0200 | [diff] [blame] | 35 | |
| 36 | import android.content.Context; |
Adrian Roos | 7419a17 | 2018-05-24 18:20:51 +0200 | [diff] [blame] | 37 | import android.platform.test.annotations.Presubmit; |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 38 | import android.view.WindowInsets.Side; |
| 39 | import android.view.WindowInsets.Type; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 40 | |
Diego Vela | a586fa3 | 2020-04-03 14:26:51 -0700 | [diff] [blame] | 41 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 42 | import androidx.test.filters.SmallTest; |
Adrian Roos | 7419a17 | 2018-05-24 18:20:51 +0200 | [diff] [blame] | 43 | |
| 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | |
Diego Vela | a586fa3 | 2020-04-03 14:26:51 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Tests for {@link ViewRootImpl} |
| 50 | * |
| 51 | * Build/Install/Run: |
| 52 | * atest FrameworksCoreTests:ViewRootImplTest |
| 53 | */ |
Adrian Roos | 7419a17 | 2018-05-24 18:20:51 +0200 | [diff] [blame] | 54 | @Presubmit |
| 55 | @SmallTest |
| 56 | @RunWith(AndroidJUnit4.class) |
| 57 | public class ViewRootImplTest { |
| 58 | |
Tiger Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 59 | private ViewRootImpl mViewRootImpl; |
Adrian Roos | 7419a17 | 2018-05-24 18:20:51 +0200 | [diff] [blame] | 60 | |
| 61 | @Before |
| 62 | public void setUp() throws Exception { |
Tiger Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 63 | final Context context = getInstrumentation().getTargetContext(); |
Adrian Roos | 7419a17 | 2018-05-24 18:20:51 +0200 | [diff] [blame] | 64 | |
Tiger Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 65 | getInstrumentation().runOnMainSync(() -> |
| 66 | mViewRootImpl = new ViewRootImpl(context, context.getDisplayNoVerify())); |
Adrian Roos | 7419a17 | 2018-05-24 18:20:51 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 69 | @Test |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 70 | public void adjustLayoutParamsForCompatibility_layoutFullscreen() { |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 71 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION); |
| 72 | attrs.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; |
| 73 | ViewRootImpl.adjustLayoutParamsForCompatibility(attrs); |
| 74 | |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 75 | // Type.statusBars() must be removed. |
Tiger Huang | 5272444 | 2020-01-20 21:38:42 +0800 | [diff] [blame] | 76 | assertEquals(0, attrs.getFitInsetsTypes() & Type.statusBars()); |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 80 | public void adjustLayoutParamsForCompatibility_layoutInScreen() { |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 81 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION); |
| 82 | attrs.flags = FLAG_LAYOUT_IN_SCREEN; |
| 83 | ViewRootImpl.adjustLayoutParamsForCompatibility(attrs); |
| 84 | |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 85 | // Type.statusBars() must be removed. |
Tiger Huang | 5272444 | 2020-01-20 21:38:42 +0800 | [diff] [blame] | 86 | assertEquals(0, attrs.getFitInsetsTypes() & Type.statusBars()); |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Test |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 90 | public void adjustLayoutParamsForCompatibility_layoutHideNavigation() { |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 91 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION); |
| 92 | attrs.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; |
| 93 | ViewRootImpl.adjustLayoutParamsForCompatibility(attrs); |
| 94 | |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 95 | // Type.systemBars() must be removed. |
Tiger Huang | 5272444 | 2020-01-20 21:38:42 +0800 | [diff] [blame] | 96 | assertEquals(0, attrs.getFitInsetsTypes() & Type.systemBars()); |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 100 | public void adjustLayoutParamsForCompatibility_toast() { |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 101 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_TOAST); |
| 102 | ViewRootImpl.adjustLayoutParamsForCompatibility(attrs); |
| 103 | |
Tiger Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 104 | assertTrue(attrs.isFitInsetsIgnoringVisibility()); |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 108 | public void adjustLayoutParamsForCompatibility_systemAlert() { |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 109 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_SYSTEM_ALERT); |
| 110 | ViewRootImpl.adjustLayoutParamsForCompatibility(attrs); |
| 111 | |
Tiger Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 112 | assertTrue(attrs.isFitInsetsIgnoringVisibility()); |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Test |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 116 | public void adjustLayoutParamsForCompatibility_fitSystemBars() { |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 117 | 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 Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 126 | 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 Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 130 | attrs.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN; |
Tiger Huang | 5272444 | 2020-01-20 21:38:42 +0800 | [diff] [blame] | 131 | attrs.setFitInsetsTypes(types); |
| 132 | attrs.setFitInsetsSides(sides); |
| 133 | attrs.setFitInsetsIgnoringVisibility(fitMaxInsets); |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 134 | ViewRootImpl.adjustLayoutParamsForCompatibility(attrs); |
| 135 | |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 136 | // Fit-insets related fields must not be adjusted due to legacy system UI visibility |
| 137 | // after calling fit-insets related methods. |
Tiger Huang | 5272444 | 2020-01-20 21:38:42 +0800 | [diff] [blame] | 138 | assertEquals(types, attrs.getFitInsetsTypes()); |
| 139 | assertEquals(sides, attrs.getFitInsetsSides()); |
| 140 | assertEquals(fitMaxInsets, attrs.isFitInsetsIgnoringVisibility()); |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 141 | } |
| 142 | |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 143 | @Test |
| 144 | public void adjustLayoutParamsForCompatibility_noAdjustAppearance() { |
Tiger Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 145 | final WindowInsetsController controller = mViewRootImpl.getInsetsController(); |
| 146 | final WindowManager.LayoutParams attrs = mViewRootImpl.mWindowAttributes; |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 147 | 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 Huang | 1da312d | 2020-05-13 16:41:32 +0800 | [diff] [blame] | 161 | final WindowInsetsController controller = mViewRootImpl.getInsetsController(); |
| 162 | final WindowManager.LayoutParams attrs = mViewRootImpl.mWindowAttributes; |
Tiger Huang | 5fb9ab7 | 2020-03-02 16:16:01 +0800 | [diff] [blame] | 163 | 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 Roos | 7419a17 | 2018-05-24 18:20:51 +0200 | [diff] [blame] | 172 | } |