Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui; |
| 16 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 17 | import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY; |
| 18 | |
Charles Chen | 10ca70b | 2018-11-28 00:03:38 +0800 | [diff] [blame] | 19 | import static com.android.systemui.ScreenDecorations.rectsToRegion; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 20 | import static com.android.systemui.tuner.TunablePadding.FLAG_END; |
| 21 | import static com.android.systemui.tuner.TunablePadding.FLAG_START; |
| 22 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 23 | import static org.hamcrest.Matchers.is; |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
Charles Chen | 10ca70b | 2018-11-28 00:03:38 +0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertThat; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 26 | import static org.mockito.ArgumentMatchers.any; |
| 27 | import static org.mockito.ArgumentMatchers.anyInt; |
| 28 | import static org.mockito.ArgumentMatchers.anyString; |
| 29 | import static org.mockito.ArgumentMatchers.eq; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 30 | import static org.mockito.Mockito.mock; |
| 31 | import static org.mockito.Mockito.never; |
Lucas Dupin | 34f6d96 | 2019-02-22 14:47:50 -0800 | [diff] [blame] | 32 | import static org.mockito.Mockito.reset; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 33 | import static org.mockito.Mockito.spy; |
| 34 | import static org.mockito.Mockito.times; |
| 35 | import static org.mockito.Mockito.verify; |
| 36 | import static org.mockito.Mockito.when; |
| 37 | |
| 38 | import android.app.Fragment; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 39 | import android.content.res.Configuration; |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 40 | import android.graphics.Rect; |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 41 | import android.os.Handler; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 42 | import android.testing.AndroidTestingRunner; |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 43 | import android.testing.TestableLooper; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 44 | import android.testing.TestableLooper.RunWithLooper; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 45 | import android.view.Display; |
| 46 | import android.view.View; |
| 47 | import android.view.WindowManager; |
| 48 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 49 | import androidx.test.filters.SmallTest; |
| 50 | |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 51 | import com.android.systemui.R.dimen; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 52 | import com.android.systemui.ScreenDecorations.TunablePaddingTagListener; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 53 | import com.android.systemui.fragments.FragmentHostManager; |
| 54 | import com.android.systemui.fragments.FragmentService; |
| 55 | import com.android.systemui.statusbar.phone.StatusBar; |
| 56 | import com.android.systemui.statusbar.phone.StatusBarWindowView; |
| 57 | import com.android.systemui.tuner.TunablePadding; |
| 58 | import com.android.systemui.tuner.TunablePadding.TunablePaddingService; |
| 59 | import com.android.systemui.tuner.TunerService; |
| 60 | |
| 61 | import org.junit.Before; |
| 62 | import org.junit.Test; |
| 63 | import org.junit.runner.RunWith; |
| 64 | |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 65 | import java.util.Collections; |
| 66 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 67 | @RunWithLooper |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 68 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 69 | @SmallTest |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 70 | public class ScreenDecorationsTest extends SysuiTestCase { |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 71 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 72 | private TestableLooper mTestableLooper; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 73 | private ScreenDecorations mScreenDecorations; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 74 | private StatusBar mStatusBar; |
| 75 | private WindowManager mWindowManager; |
| 76 | private FragmentService mFragmentService; |
| 77 | private FragmentHostManager mFragmentHostManager; |
| 78 | private TunerService mTunerService; |
| 79 | private StatusBarWindowView mView; |
| 80 | private TunablePaddingService mTunablePaddingService; |
| 81 | |
| 82 | @Before |
| 83 | public void setup() { |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 84 | mTestableLooper = TestableLooper.get(this); |
| 85 | mDependency.injectTestDependency(Dependency.MAIN_HANDLER, |
| 86 | new Handler(mTestableLooper.getLooper())); |
Lucas Dupin | 34f6d96 | 2019-02-22 14:47:50 -0800 | [diff] [blame] | 87 | mTunablePaddingService = mDependency.injectMockDependency(TunablePaddingService.class); |
| 88 | mTunerService = mDependency.injectMockDependency(TunerService.class); |
| 89 | mFragmentService = mDependency.injectMockDependency(FragmentService.class); |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 90 | |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 91 | mStatusBar = mock(StatusBar.class); |
| 92 | mWindowManager = mock(WindowManager.class); |
| 93 | mView = spy(new StatusBarWindowView(mContext, null)); |
| 94 | when(mStatusBar.getStatusBarWindow()).thenReturn(mView); |
| 95 | mContext.putComponent(StatusBar.class, mStatusBar); |
| 96 | |
| 97 | Display display = mContext.getSystemService(WindowManager.class).getDefaultDisplay(); |
| 98 | when(mWindowManager.getDefaultDisplay()).thenReturn(display); |
| 99 | mContext.addMockSystemService(WindowManager.class, mWindowManager); |
| 100 | |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 101 | mFragmentHostManager = mock(FragmentHostManager.class); |
| 102 | when(mFragmentService.getFragmentHostManager(any())).thenReturn(mFragmentHostManager); |
| 103 | |
Adrian Roos | 73ab97c | 2018-08-02 15:56:15 +0200 | [diff] [blame] | 104 | |
| 105 | mScreenDecorations = new ScreenDecorations() { |
| 106 | @Override |
| 107 | public void start() { |
| 108 | super.start(); |
| 109 | mTestableLooper.processAllMessages(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | Handler startHandlerThread() { |
| 114 | return new Handler(mTestableLooper.getLooper()); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected void onConfigurationChanged(Configuration newConfig) { |
| 119 | super.onConfigurationChanged(newConfig); |
| 120 | mTestableLooper.processAllMessages(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onTuningChanged(String key, String newValue) { |
| 125 | super.onTuningChanged(key, newValue); |
| 126 | mTestableLooper.processAllMessages(); |
| 127 | } |
| 128 | }; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 129 | mScreenDecorations.mContext = mContext; |
| 130 | mScreenDecorations.mComponents = mContext.getComponents(); |
Lucas Dupin | 34f6d96 | 2019-02-22 14:47:50 -0800 | [diff] [blame] | 131 | reset(mTunerService); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 135 | public void testNoRounding_NoCutout() { |
| 136 | mContext.getOrCreateTestableResources().addOverride( |
| 137 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false); |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 138 | mContext.getOrCreateTestableResources().addOverride( |
| 139 | com.android.internal.R.dimen.rounded_corner_radius, 0); |
| 140 | mContext.getOrCreateTestableResources().addOverride( |
| 141 | com.android.internal.R.dimen.rounded_corner_radius_top, 0); |
| 142 | mContext.getOrCreateTestableResources().addOverride( |
| 143 | com.android.internal.R.dimen.rounded_corner_radius_bottom, 0); |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 144 | mContext.getOrCreateTestableResources() |
| 145 | .addOverride(dimen.rounded_corner_content_padding, 0); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 146 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 147 | mScreenDecorations.start(); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 148 | // No views added. |
| 149 | verify(mWindowManager, never()).addView(any(), any()); |
| 150 | // No Fragments watched. |
| 151 | verify(mFragmentHostManager, never()).addTagListener(any(), any()); |
| 152 | // No Tuners tuned. |
| 153 | verify(mTunerService, never()).addTunable(any(), any()); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void testRounding() { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 158 | mContext.getOrCreateTestableResources().addOverride( |
| 159 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false); |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 160 | mContext.getOrCreateTestableResources().addOverride( |
| 161 | com.android.internal.R.dimen.rounded_corner_radius, 20); |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 162 | mContext.getOrCreateTestableResources() |
| 163 | .addOverride(dimen.rounded_corner_content_padding, 20); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 164 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 165 | mScreenDecorations.start(); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 166 | // Add 2 windows for rounded corners (top and bottom). |
| 167 | verify(mWindowManager, times(2)).addView(any(), any()); |
| 168 | |
Jason Monk | 089981b | 2017-06-30 09:10:01 -0400 | [diff] [blame] | 169 | // Add 2 tag listeners for each of the fragments that are needed. |
| 170 | verify(mFragmentHostManager, times(2)).addTagListener(any(), any()); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 171 | // One tunable. |
| 172 | verify(mTunerService, times(1)).addTunable(any(), any()); |
| 173 | // One TunablePadding. |
| 174 | verify(mTunablePaddingService, times(1)).add(any(), anyString(), anyInt(), anyInt()); |
| 175 | } |
| 176 | |
| 177 | @Test |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 178 | public void testCutout() { |
| 179 | mContext.getOrCreateTestableResources().addOverride( |
| 180 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, true); |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 181 | mContext.getOrCreateTestableResources().addOverride( |
| 182 | com.android.internal.R.dimen.rounded_corner_radius, 0); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 183 | mContext.getOrCreateTestableResources() |
| 184 | .addOverride(dimen.rounded_corner_content_padding, 0); |
| 185 | |
| 186 | mScreenDecorations.start(); |
| 187 | // Add 2 windows for rounded corners (top and bottom). |
| 188 | verify(mWindowManager, times(2)).addView(any(), any()); |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void testDelayedCutout() { |
| 193 | mContext.getOrCreateTestableResources().addOverride( |
| 194 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false); |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 195 | mContext.getOrCreateTestableResources().addOverride( |
| 196 | com.android.internal.R.dimen.rounded_corner_radius, 0); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 197 | mContext.getOrCreateTestableResources() |
| 198 | .addOverride(dimen.rounded_corner_content_padding, 0); |
| 199 | |
| 200 | mScreenDecorations.start(); |
| 201 | |
| 202 | mContext.getOrCreateTestableResources().addOverride( |
| 203 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, true); |
| 204 | mScreenDecorations.onConfigurationChanged(new Configuration()); |
| 205 | |
| 206 | // Add 2 windows for rounded corners (top and bottom). |
| 207 | verify(mWindowManager, times(2)).addView(any(), any()); |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void hasRoundedCornerOverlayFlagSet() { |
| 212 | assertThat(mScreenDecorations.getWindowLayoutParams().privateFlags |
| 213 | & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY, |
| 214 | is(PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY)); |
| 215 | } |
| 216 | |
| 217 | @Test |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 218 | public void testPaddingTagListener() { |
| 219 | TunablePaddingTagListener tagListener = new TunablePaddingTagListener(14, 5); |
| 220 | View v = mock(View.class); |
| 221 | View child = mock(View.class); |
| 222 | Fragment f = mock(Fragment.class); |
| 223 | TunablePadding padding = mock(TunablePadding.class); |
| 224 | |
| 225 | when(mTunablePaddingService.add(any(), anyString(), anyInt(), anyInt())) |
| 226 | .thenReturn(padding); |
| 227 | when(f.getView()).thenReturn(v); |
| 228 | when(v.findViewById(5)).thenReturn(child); |
| 229 | |
| 230 | // Trigger callback and verify we get a TunablePadding created. |
| 231 | tagListener.onFragmentViewCreated(null, f); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 232 | verify(mTunablePaddingService).add(eq(child), eq(ScreenDecorations.PADDING), eq(14), |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 233 | eq(FLAG_START | FLAG_END)); |
| 234 | |
| 235 | // Call again and verify destroy is called. |
| 236 | tagListener.onFragmentViewCreated(null, f); |
| 237 | verify(padding).destroy(); |
| 238 | } |
| 239 | |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 240 | @Test |
| 241 | public void testUpdateRoundedCorners() { |
| 242 | mContext.getOrCreateTestableResources().addOverride( |
| 243 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false); |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 244 | mContext.getOrCreateTestableResources().addOverride( |
| 245 | com.android.internal.R.dimen.rounded_corner_radius, 20); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 246 | |
| 247 | mScreenDecorations.start(); |
| 248 | assertEquals(mScreenDecorations.mRoundedDefault, 20); |
| 249 | |
Beverly | 4d1113d | 2019-01-03 14:45:10 -0500 | [diff] [blame] | 250 | mContext.getOrCreateTestableResources().addOverride( |
| 251 | com.android.internal.R.dimen.rounded_corner_radius, 5); |
Adrian Roos | 64c9d90 | 2018-08-20 13:43:38 +0200 | [diff] [blame] | 252 | mScreenDecorations.onConfigurationChanged(null); |
| 253 | assertEquals(mScreenDecorations.mRoundedDefault, 5); |
| 254 | } |
Issei Suzuki | 43190bd | 2018-08-20 17:28:41 +0200 | [diff] [blame] | 255 | |
| 256 | @Test |
| 257 | public void testBoundingRectsToRegion() throws Exception { |
| 258 | Rect rect = new Rect(1, 2, 3, 4); |
| 259 | assertThat(rectsToRegion(Collections.singletonList(rect)).getBounds(), is(rect)); |
| 260 | } |
| 261 | |
Geoffrey Pitsch | 0053e99 | 2017-05-22 15:32:29 -0400 | [diff] [blame] | 262 | } |