blob: 0114075b1e654eacd71a768eea3e0ca733c0737f [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 Monkfba8faf2017-05-23 10:42:59 -040042import android.support.test.filters.SmallTest;
Jason Monkd4afe152017-05-01 15:37:43 -040043import android.testing.AndroidTestingRunner;
Adrian Roos73ab97c2018-08-02 15:56:15 +020044import android.testing.TestableLooper;
Beverlye91f0d02018-05-15 14:40:47 -040045import android.testing.TestableLooper.RunWithLooper;
Jason Monkd4afe152017-05-01 15:37:43 -040046import android.view.Display;
47import android.view.View;
48import android.view.WindowManager;
49
50import com.android.systemui.R.dimen;
Adrian Roos5b518852018-01-23 17:23:38 +010051import com.android.systemui.ScreenDecorations.TunablePaddingTagListener;
Jason Monkd4afe152017-05-01 15:37:43 -040052import com.android.systemui.fragments.FragmentHostManager;
53import com.android.systemui.fragments.FragmentService;
54import com.android.systemui.statusbar.phone.StatusBar;
55import com.android.systemui.statusbar.phone.StatusBarWindowView;
56import com.android.systemui.tuner.TunablePadding;
57import com.android.systemui.tuner.TunablePadding.TunablePaddingService;
58import com.android.systemui.tuner.TunerService;
59
60import org.junit.Before;
61import org.junit.Test;
62import org.junit.runner.RunWith;
63
Issei Suzuki43190bd2018-08-20 17:28:41 +020064import java.util.Collections;
65
Beverlye91f0d02018-05-15 14:40:47 -040066@RunWithLooper
Jason Monkd4afe152017-05-01 15:37:43 -040067@RunWith(AndroidTestingRunner.class)
Jason Monkfba8faf2017-05-23 10:42:59 -040068@SmallTest
Adrian Roos5b518852018-01-23 17:23:38 +010069public class ScreenDecorationsTest extends SysuiTestCase {
Jason Monkd4afe152017-05-01 15:37:43 -040070
Adrian Roos73ab97c2018-08-02 15:56:15 +020071 private TestableLooper mTestableLooper;
Adrian Roos5b518852018-01-23 17:23:38 +010072 private ScreenDecorations mScreenDecorations;
Jason Monkd4afe152017-05-01 15:37:43 -040073 private StatusBar mStatusBar;
74 private WindowManager mWindowManager;
75 private FragmentService mFragmentService;
76 private FragmentHostManager mFragmentHostManager;
77 private TunerService mTunerService;
78 private StatusBarWindowView mView;
79 private TunablePaddingService mTunablePaddingService;
80
81 @Before
82 public void setup() {
Adrian Roos73ab97c2018-08-02 15:56:15 +020083 mTestableLooper = TestableLooper.get(this);
84 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
85 new Handler(mTestableLooper.getLooper()));
Lucas Dupin34f6d962019-02-22 14:47:50 -080086 mTunablePaddingService = mDependency.injectMockDependency(TunablePaddingService.class);
87 mTunerService = mDependency.injectMockDependency(TunerService.class);
88 mFragmentService = mDependency.injectMockDependency(FragmentService.class);
Adrian Roos73ab97c2018-08-02 15:56:15 +020089
Jason Monkd4afe152017-05-01 15:37:43 -040090 mStatusBar = mock(StatusBar.class);
91 mWindowManager = mock(WindowManager.class);
92 mView = spy(new StatusBarWindowView(mContext, null));
93 when(mStatusBar.getStatusBarWindow()).thenReturn(mView);
94 mContext.putComponent(StatusBar.class, mStatusBar);
95
96 Display display = mContext.getSystemService(WindowManager.class).getDefaultDisplay();
97 when(mWindowManager.getDefaultDisplay()).thenReturn(display);
98 mContext.addMockSystemService(WindowManager.class, mWindowManager);
99
Jason Monkd4afe152017-05-01 15:37:43 -0400100 mFragmentHostManager = mock(FragmentHostManager.class);
101 when(mFragmentService.getFragmentHostManager(any())).thenReturn(mFragmentHostManager);
102
Adrian Roos73ab97c2018-08-02 15:56:15 +0200103
104 mScreenDecorations = new ScreenDecorations() {
105 @Override
106 public void start() {
107 super.start();
108 mTestableLooper.processAllMessages();
109 }
110
111 @Override
112 Handler startHandlerThread() {
113 return new Handler(mTestableLooper.getLooper());
114 }
115
116 @Override
117 protected void onConfigurationChanged(Configuration newConfig) {
118 super.onConfigurationChanged(newConfig);
119 mTestableLooper.processAllMessages();
120 }
121
122 @Override
123 public void onTuningChanged(String key, String newValue) {
124 super.onTuningChanged(key, newValue);
125 mTestableLooper.processAllMessages();
126 }
127 };
Adrian Roos5b518852018-01-23 17:23:38 +0100128 mScreenDecorations.mContext = mContext;
129 mScreenDecorations.mComponents = mContext.getComponents();
Lucas Dupin34f6d962019-02-22 14:47:50 -0800130 reset(mTunerService);
Jason Monkd4afe152017-05-01 15:37:43 -0400131 }
132
133 @Test
Adrian Roos5b518852018-01-23 17:23:38 +0100134 public void testNoRounding_NoCutout() {
135 mContext.getOrCreateTestableResources().addOverride(
136 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false);
Beverly4d1113d2019-01-03 14:45:10 -0500137 mContext.getOrCreateTestableResources().addOverride(
138 com.android.internal.R.dimen.rounded_corner_radius, 0);
139 mContext.getOrCreateTestableResources().addOverride(
140 com.android.internal.R.dimen.rounded_corner_radius_top, 0);
141 mContext.getOrCreateTestableResources().addOverride(
142 com.android.internal.R.dimen.rounded_corner_radius_bottom, 0);
Jason Monkc429f692017-06-27 13:13:49 -0400143 mContext.getOrCreateTestableResources()
144 .addOverride(dimen.rounded_corner_content_padding, 0);
Jason Monkd4afe152017-05-01 15:37:43 -0400145
Adrian Roos5b518852018-01-23 17:23:38 +0100146 mScreenDecorations.start();
Jason Monkd4afe152017-05-01 15:37:43 -0400147 // No views added.
148 verify(mWindowManager, never()).addView(any(), any());
149 // No Fragments watched.
150 verify(mFragmentHostManager, never()).addTagListener(any(), any());
151 // No Tuners tuned.
152 verify(mTunerService, never()).addTunable(any(), any());
153 }
154
155 @Test
156 public void testRounding() {
Adrian Roos5b518852018-01-23 17:23:38 +0100157 mContext.getOrCreateTestableResources().addOverride(
158 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false);
Beverly4d1113d2019-01-03 14:45:10 -0500159 mContext.getOrCreateTestableResources().addOverride(
160 com.android.internal.R.dimen.rounded_corner_radius, 20);
Jason Monkc429f692017-06-27 13:13:49 -0400161 mContext.getOrCreateTestableResources()
162 .addOverride(dimen.rounded_corner_content_padding, 20);
Jason Monkd4afe152017-05-01 15:37:43 -0400163
Adrian Roos5b518852018-01-23 17:23:38 +0100164 mScreenDecorations.start();
Jason Monkd4afe152017-05-01 15:37:43 -0400165 // Add 2 windows for rounded corners (top and bottom).
166 verify(mWindowManager, times(2)).addView(any(), any());
167
Jason Monk089981b2017-06-30 09:10:01 -0400168 // Add 2 tag listeners for each of the fragments that are needed.
169 verify(mFragmentHostManager, times(2)).addTagListener(any(), any());
Jason Monkd4afe152017-05-01 15:37:43 -0400170 // One tunable.
171 verify(mTunerService, times(1)).addTunable(any(), any());
172 // One TunablePadding.
173 verify(mTunablePaddingService, times(1)).add(any(), anyString(), anyInt(), anyInt());
174 }
175
176 @Test
Adrian Roos5b518852018-01-23 17:23:38 +0100177 public void testCutout() {
178 mContext.getOrCreateTestableResources().addOverride(
179 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, true);
Beverly4d1113d2019-01-03 14:45:10 -0500180 mContext.getOrCreateTestableResources().addOverride(
181 com.android.internal.R.dimen.rounded_corner_radius, 0);
Adrian Roos5b518852018-01-23 17:23:38 +0100182 mContext.getOrCreateTestableResources()
183 .addOverride(dimen.rounded_corner_content_padding, 0);
184
185 mScreenDecorations.start();
186 // Add 2 windows for rounded corners (top and bottom).
187 verify(mWindowManager, times(2)).addView(any(), any());
188 }
189
190 @Test
191 public void testDelayedCutout() {
192 mContext.getOrCreateTestableResources().addOverride(
193 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false);
Beverly4d1113d2019-01-03 14:45:10 -0500194 mContext.getOrCreateTestableResources().addOverride(
195 com.android.internal.R.dimen.rounded_corner_radius, 0);
Adrian Roos5b518852018-01-23 17:23:38 +0100196 mContext.getOrCreateTestableResources()
197 .addOverride(dimen.rounded_corner_content_padding, 0);
198
199 mScreenDecorations.start();
200
201 mContext.getOrCreateTestableResources().addOverride(
202 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, true);
203 mScreenDecorations.onConfigurationChanged(new Configuration());
204
205 // Add 2 windows for rounded corners (top and bottom).
206 verify(mWindowManager, times(2)).addView(any(), any());
207 }
208
209 @Test
210 public void hasRoundedCornerOverlayFlagSet() {
211 assertThat(mScreenDecorations.getWindowLayoutParams().privateFlags
212 & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY,
213 is(PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY));
214 }
215
216 @Test
Jason Monkd4afe152017-05-01 15:37:43 -0400217 public void testPaddingTagListener() {
218 TunablePaddingTagListener tagListener = new TunablePaddingTagListener(14, 5);
219 View v = mock(View.class);
220 View child = mock(View.class);
221 Fragment f = mock(Fragment.class);
222 TunablePadding padding = mock(TunablePadding.class);
223
224 when(mTunablePaddingService.add(any(), anyString(), anyInt(), anyInt()))
225 .thenReturn(padding);
226 when(f.getView()).thenReturn(v);
227 when(v.findViewById(5)).thenReturn(child);
228
229 // Trigger callback and verify we get a TunablePadding created.
230 tagListener.onFragmentViewCreated(null, f);
Adrian Roos5b518852018-01-23 17:23:38 +0100231 verify(mTunablePaddingService).add(eq(child), eq(ScreenDecorations.PADDING), eq(14),
Jason Monkd4afe152017-05-01 15:37:43 -0400232 eq(FLAG_START | FLAG_END));
233
234 // Call again and verify destroy is called.
235 tagListener.onFragmentViewCreated(null, f);
236 verify(padding).destroy();
237 }
238
Adrian Roos64c9d902018-08-20 13:43:38 +0200239 @Test
240 public void testUpdateRoundedCorners() {
241 mContext.getOrCreateTestableResources().addOverride(
242 com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false);
Beverly4d1113d2019-01-03 14:45:10 -0500243 mContext.getOrCreateTestableResources().addOverride(
244 com.android.internal.R.dimen.rounded_corner_radius, 20);
Adrian Roos64c9d902018-08-20 13:43:38 +0200245
246 mScreenDecorations.start();
247 assertEquals(mScreenDecorations.mRoundedDefault, 20);
248
Beverly4d1113d2019-01-03 14:45:10 -0500249 mContext.getOrCreateTestableResources().addOverride(
250 com.android.internal.R.dimen.rounded_corner_radius, 5);
Adrian Roos64c9d902018-08-20 13:43:38 +0200251 mScreenDecorations.onConfigurationChanged(null);
252 assertEquals(mScreenDecorations.mRoundedDefault, 5);
253 }
Issei Suzuki43190bd2018-08-20 17:28:41 +0200254
255 @Test
256 public void testBoundingRectsToRegion() throws Exception {
257 Rect rect = new Rect(1, 2, 3, 4);
258 assertThat(rectsToRegion(Collections.singletonList(rect)).getBounds(), is(rect));
259 }
260
Geoffrey Pitsch0053e992017-05-22 15:32:29 -0400261}