blob: e91a7e9bfcb6a4982ab585d829a46fe7c1fb60b2 [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;
32import static org.mockito.Mockito.spy;
33import static org.mockito.Mockito.times;
34import static org.mockito.Mockito.verify;
35import static org.mockito.Mockito.when;
36
37import android.app.Fragment;
Adrian Roos5b518852018-01-23 17:23:38 +010038import android.content.res.Configuration;
Issei Suzuki43190bd2018-08-20 17:28:41 +020039import android.graphics.Rect;
Adrian Roos73ab97c2018-08-02 15:56:15 +020040import android.os.Handler;
Jason Monkfba8faf2017-05-23 10:42:59 -040041import android.support.test.filters.SmallTest;
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
49import com.android.systemui.R.dimen;
Adrian Roos5b518852018-01-23 17:23:38 +010050import com.android.systemui.ScreenDecorations.TunablePaddingTagListener;
Jason Monkd4afe152017-05-01 15:37:43 -040051import com.android.systemui.fragments.FragmentHostManager;
52import com.android.systemui.fragments.FragmentService;
53import com.android.systemui.statusbar.phone.StatusBar;
54import com.android.systemui.statusbar.phone.StatusBarWindowView;
55import com.android.systemui.tuner.TunablePadding;
56import com.android.systemui.tuner.TunablePadding.TunablePaddingService;
57import com.android.systemui.tuner.TunerService;
58
59import org.junit.Before;
60import org.junit.Test;
61import org.junit.runner.RunWith;
62
Issei Suzuki43190bd2018-08-20 17:28:41 +020063import java.util.Collections;
64
Beverlye91f0d02018-05-15 14:40:47 -040065@RunWithLooper
Jason Monkd4afe152017-05-01 15:37:43 -040066@RunWith(AndroidTestingRunner.class)
Jason Monkfba8faf2017-05-23 10:42:59 -040067@SmallTest
Adrian Roos5b518852018-01-23 17:23:38 +010068public class ScreenDecorationsTest extends SysuiTestCase {
Jason Monkd4afe152017-05-01 15:37:43 -040069
Adrian Roos73ab97c2018-08-02 15:56:15 +020070 private TestableLooper mTestableLooper;
Adrian Roos5b518852018-01-23 17:23:38 +010071 private ScreenDecorations mScreenDecorations;
Jason Monkd4afe152017-05-01 15:37:43 -040072 private StatusBar mStatusBar;
73 private WindowManager mWindowManager;
74 private FragmentService mFragmentService;
75 private FragmentHostManager mFragmentHostManager;
76 private TunerService mTunerService;
77 private StatusBarWindowView mView;
78 private TunablePaddingService mTunablePaddingService;
79
80 @Before
81 public void setup() {
Adrian Roos73ab97c2018-08-02 15:56:15 +020082 mTestableLooper = TestableLooper.get(this);
83 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
84 new Handler(mTestableLooper.getLooper()));
85
Jason Monkd4afe152017-05-01 15:37:43 -040086 mStatusBar = mock(StatusBar.class);
87 mWindowManager = mock(WindowManager.class);
88 mView = spy(new StatusBarWindowView(mContext, null));
89 when(mStatusBar.getStatusBarWindow()).thenReturn(mView);
90 mContext.putComponent(StatusBar.class, mStatusBar);
91
92 Display display = mContext.getSystemService(WindowManager.class).getDefaultDisplay();
93 when(mWindowManager.getDefaultDisplay()).thenReturn(display);
94 mContext.addMockSystemService(WindowManager.class, mWindowManager);
95
96 mFragmentService = mDependency.injectMockDependency(FragmentService.class);
97 mFragmentHostManager = mock(FragmentHostManager.class);
98 when(mFragmentService.getFragmentHostManager(any())).thenReturn(mFragmentHostManager);
99
100 mTunerService = mDependency.injectMockDependency(TunerService.class);
101
Adrian Roos73ab97c2018-08-02 15:56:15 +0200102
103 mScreenDecorations = new ScreenDecorations() {
104 @Override
105 public void start() {
106 super.start();
107 mTestableLooper.processAllMessages();
108 }
109
110 @Override
111 Handler startHandlerThread() {
112 return new Handler(mTestableLooper.getLooper());
113 }
114
115 @Override
116 protected void onConfigurationChanged(Configuration newConfig) {
117 super.onConfigurationChanged(newConfig);
118 mTestableLooper.processAllMessages();
119 }
120
121 @Override
122 public void onTuningChanged(String key, String newValue) {
123 super.onTuningChanged(key, newValue);
124 mTestableLooper.processAllMessages();
125 }
126 };
Adrian Roos5b518852018-01-23 17:23:38 +0100127 mScreenDecorations.mContext = mContext;
128 mScreenDecorations.mComponents = mContext.getComponents();
Jason Monkd4afe152017-05-01 15:37:43 -0400129
130 mTunablePaddingService = mDependency.injectMockDependency(TunablePaddingService.class);
131 }
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}