blob: 64ab060e14a25b4a6cfd745adf791031366e5599 [file] [log] [blame]
Jason Monkd4afe152017-05-01 15:37:43 -04001/*
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
15package com.android.systemui;
16
Adrian Roos5b518852018-01-23 17:23:38 +010017import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY;
18
Charles Chen10ca70b2018-11-28 00:03:38 +080019import static com.android.systemui.ScreenDecorations.rectsToRegion;
Jason Monkd4afe152017-05-01 15:37:43 -040020import static com.android.systemui.tuner.TunablePadding.FLAG_END;
21import static com.android.systemui.tuner.TunablePadding.FLAG_START;
22
Adrian Roos5b518852018-01-23 17:23:38 +010023import static org.hamcrest.Matchers.is;
Adrian Roos64c9d902018-08-20 13:43:38 +020024import static org.junit.Assert.assertEquals;
Charles Chen10ca70b2018-11-28 00:03:38 +080025import static org.junit.Assert.assertThat;
Jason Monkd4afe152017-05-01 15:37:43 -040026import static org.mockito.ArgumentMatchers.any;
27import static org.mockito.ArgumentMatchers.anyInt;
28import static org.mockito.ArgumentMatchers.anyString;
29import static org.mockito.ArgumentMatchers.eq;
Jason Monkd4afe152017-05-01 15:37:43 -040030import static org.mockito.Mockito.mock;
31import static org.mockito.Mockito.never;
Lucas Dupin34f6d962019-02-22 14:47:50 -080032import static org.mockito.Mockito.reset;
Jason Monkd4afe152017-05-01 15:37:43 -040033import static org.mockito.Mockito.spy;
34import static org.mockito.Mockito.times;
35import static org.mockito.Mockito.verify;
36import static org.mockito.Mockito.when;
37
38import android.app.Fragment;
Adrian Roos5b518852018-01-23 17:23:38 +010039import android.content.res.Configuration;
Issei Suzuki43190bd2018-08-20 17:28:41 +020040import android.graphics.Rect;
Adrian Roos73ab97c2018-08-02 15:56:15 +020041import android.os.Handler;
Jason Monkd4afe152017-05-01 15:37:43 -040042import android.testing.AndroidTestingRunner;
Adrian Roos73ab97c2018-08-02 15:56:15 +020043import android.testing.TestableLooper;
Beverlye91f0d02018-05-15 14:40:47 -040044import android.testing.TestableLooper.RunWithLooper;
Jason Monkd4afe152017-05-01 15:37:43 -040045import android.view.Display;
46import android.view.View;
47import android.view.WindowManager;
48
Brett Chabot84151d92019-02-27 15:37:59 -080049import androidx.test.filters.SmallTest;
50
Jason Monkd4afe152017-05-01 15:37:43 -040051import com.android.systemui.R.dimen;
Adrian Roos5b518852018-01-23 17:23:38 +010052import com.android.systemui.ScreenDecorations.TunablePaddingTagListener;
Jason Monkd4afe152017-05-01 15:37:43 -040053import com.android.systemui.fragments.FragmentHostManager;
54import com.android.systemui.fragments.FragmentService;
55import com.android.systemui.statusbar.phone.StatusBar;
56import com.android.systemui.statusbar.phone.StatusBarWindowView;
57import com.android.systemui.tuner.TunablePadding;
58import com.android.systemui.tuner.TunablePadding.TunablePaddingService;
59import com.android.systemui.tuner.TunerService;
60
61import org.junit.Before;
62import org.junit.Test;
63import org.junit.runner.RunWith;
64
Issei Suzuki43190bd2018-08-20 17:28:41 +020065import java.util.Collections;
66
Beverlye91f0d02018-05-15 14:40:47 -040067@RunWithLooper
Jason Monkd4afe152017-05-01 15:37:43 -040068@RunWith(AndroidTestingRunner.class)
Jason Monkfba8faf2017-05-23 10:42:59 -040069@SmallTest
Adrian Roos5b518852018-01-23 17:23:38 +010070public class ScreenDecorationsTest extends SysuiTestCase {
Jason Monkd4afe152017-05-01 15:37:43 -040071
Adrian Roos73ab97c2018-08-02 15:56:15 +020072 private TestableLooper mTestableLooper;
Adrian Roos5b518852018-01-23 17:23:38 +010073 private ScreenDecorations mScreenDecorations;
Jason Monkd4afe152017-05-01 15:37:43 -040074 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 Roos73ab97c2018-08-02 15:56:15 +020084 mTestableLooper = TestableLooper.get(this);
85 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
86 new Handler(mTestableLooper.getLooper()));
Lucas Dupin34f6d962019-02-22 14:47:50 -080087 mTunablePaddingService = mDependency.injectMockDependency(TunablePaddingService.class);
88 mTunerService = mDependency.injectMockDependency(TunerService.class);
89 mFragmentService = mDependency.injectMockDependency(FragmentService.class);
Adrian Roos73ab97c2018-08-02 15:56:15 +020090
Jason Monkd4afe152017-05-01 15:37:43 -040091 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 Monkd4afe152017-05-01 15:37:43 -0400101 mFragmentHostManager = mock(FragmentHostManager.class);
102 when(mFragmentService.getFragmentHostManager(any())).thenReturn(mFragmentHostManager);
103
Adrian Roos73ab97c2018-08-02 15:56:15 +0200104
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 Roos5b518852018-01-23 17:23:38 +0100129 mScreenDecorations.mContext = mContext;
130 mScreenDecorations.mComponents = mContext.getComponents();
Lucas Dupin34f6d962019-02-22 14:47:50 -0800131 reset(mTunerService);
Jason Monkd4afe152017-05-01 15:37:43 -0400132 }
133
134 @Test
Adrian Roos5b518852018-01-23 17:23:38 +0100135 public void testNoRounding_NoCutout() {
136 mContext.getOrCreateTestableResources().addOverride(
137 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false);
Beverly4d1113d2019-01-03 14:45:10 -0500138 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 Monkc429f692017-06-27 13:13:49 -0400144 mContext.getOrCreateTestableResources()
145 .addOverride(dimen.rounded_corner_content_padding, 0);
Jason Monkd4afe152017-05-01 15:37:43 -0400146
Adrian Roos5b518852018-01-23 17:23:38 +0100147 mScreenDecorations.start();
Jason Monkd4afe152017-05-01 15:37:43 -0400148 // 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 Roos5b518852018-01-23 17:23:38 +0100158 mContext.getOrCreateTestableResources().addOverride(
159 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false);
Beverly4d1113d2019-01-03 14:45:10 -0500160 mContext.getOrCreateTestableResources().addOverride(
161 com.android.internal.R.dimen.rounded_corner_radius, 20);
Jason Monkc429f692017-06-27 13:13:49 -0400162 mContext.getOrCreateTestableResources()
163 .addOverride(dimen.rounded_corner_content_padding, 20);
Jason Monkd4afe152017-05-01 15:37:43 -0400164
Adrian Roos5b518852018-01-23 17:23:38 +0100165 mScreenDecorations.start();
Jason Monkd4afe152017-05-01 15:37:43 -0400166 // Add 2 windows for rounded corners (top and bottom).
167 verify(mWindowManager, times(2)).addView(any(), any());
168
Jason Monk089981b2017-06-30 09:10:01 -0400169 // Add 2 tag listeners for each of the fragments that are needed.
170 verify(mFragmentHostManager, times(2)).addTagListener(any(), any());
Jason Monkd4afe152017-05-01 15:37:43 -0400171 // 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 Roos5b518852018-01-23 17:23:38 +0100178 public void testCutout() {
179 mContext.getOrCreateTestableResources().addOverride(
180 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, true);
Beverly4d1113d2019-01-03 14:45:10 -0500181 mContext.getOrCreateTestableResources().addOverride(
182 com.android.internal.R.dimen.rounded_corner_radius, 0);
Adrian Roos5b518852018-01-23 17:23:38 +0100183 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);
Beverly4d1113d2019-01-03 14:45:10 -0500195 mContext.getOrCreateTestableResources().addOverride(
196 com.android.internal.R.dimen.rounded_corner_radius, 0);
Adrian Roos5b518852018-01-23 17:23:38 +0100197 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 Monkd4afe152017-05-01 15:37:43 -0400218 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 Roos5b518852018-01-23 17:23:38 +0100232 verify(mTunablePaddingService).add(eq(child), eq(ScreenDecorations.PADDING), eq(14),
Jason Monkd4afe152017-05-01 15:37:43 -0400233 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 Roos64c9d902018-08-20 13:43:38 +0200240 @Test
241 public void testUpdateRoundedCorners() {
242 mContext.getOrCreateTestableResources().addOverride(
243 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false);
Beverly4d1113d2019-01-03 14:45:10 -0500244 mContext.getOrCreateTestableResources().addOverride(
245 com.android.internal.R.dimen.rounded_corner_radius, 20);
Adrian Roos64c9d902018-08-20 13:43:38 +0200246
247 mScreenDecorations.start();
248 assertEquals(mScreenDecorations.mRoundedDefault, 20);
249
Beverly4d1113d2019-01-03 14:45:10 -0500250 mContext.getOrCreateTestableResources().addOverride(
251 com.android.internal.R.dimen.rounded_corner_radius, 5);
Adrian Roos64c9d902018-08-20 13:43:38 +0200252 mScreenDecorations.onConfigurationChanged(null);
253 assertEquals(mScreenDecorations.mRoundedDefault, 5);
254 }
Issei Suzuki43190bd2018-08-20 17:28:41 +0200255
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 Pitsch0053e992017-05-22 15:32:29 -0400262}