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 | |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 19 | import static com.android.systemui.tuner.TunablePadding.FLAG_END; |
| 20 | import static com.android.systemui.tuner.TunablePadding.FLAG_START; |
| 21 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 22 | import static org.hamcrest.Matchers.is; |
| 23 | import static org.junit.Assert.assertThat; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 24 | import static org.mockito.ArgumentMatchers.any; |
| 25 | import static org.mockito.ArgumentMatchers.anyInt; |
| 26 | import static org.mockito.ArgumentMatchers.anyString; |
| 27 | import static org.mockito.ArgumentMatchers.eq; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 28 | import static org.mockito.Mockito.mock; |
| 29 | import static org.mockito.Mockito.never; |
| 30 | import static org.mockito.Mockito.spy; |
| 31 | import static org.mockito.Mockito.times; |
| 32 | import static org.mockito.Mockito.verify; |
| 33 | import static org.mockito.Mockito.when; |
| 34 | |
| 35 | import android.app.Fragment; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 36 | import android.content.res.Configuration; |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 37 | import android.support.test.filters.SmallTest; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 38 | import android.testing.AndroidTestingRunner; |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 39 | import android.testing.TestableLooper.RunWithLooper; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 40 | import android.view.Display; |
| 41 | import android.view.View; |
| 42 | import android.view.WindowManager; |
| 43 | |
| 44 | import com.android.systemui.R.dimen; |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 45 | import com.android.systemui.ScreenDecorations.TunablePaddingTagListener; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 46 | import com.android.systemui.fragments.FragmentHostManager; |
| 47 | import com.android.systemui.fragments.FragmentService; |
| 48 | import com.android.systemui.statusbar.phone.StatusBar; |
| 49 | import com.android.systemui.statusbar.phone.StatusBarWindowView; |
| 50 | import com.android.systemui.tuner.TunablePadding; |
| 51 | import com.android.systemui.tuner.TunablePadding.TunablePaddingService; |
| 52 | import com.android.systemui.tuner.TunerService; |
| 53 | |
| 54 | import org.junit.Before; |
| 55 | import org.junit.Test; |
| 56 | import org.junit.runner.RunWith; |
| 57 | |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 58 | @RunWithLooper |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 59 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 60 | @SmallTest |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 61 | public class ScreenDecorationsTest extends SysuiTestCase { |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 62 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 63 | private ScreenDecorations mScreenDecorations; |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 64 | private StatusBar mStatusBar; |
| 65 | private WindowManager mWindowManager; |
| 66 | private FragmentService mFragmentService; |
| 67 | private FragmentHostManager mFragmentHostManager; |
| 68 | private TunerService mTunerService; |
| 69 | private StatusBarWindowView mView; |
| 70 | private TunablePaddingService mTunablePaddingService; |
| 71 | |
| 72 | @Before |
| 73 | public void setup() { |
| 74 | mStatusBar = mock(StatusBar.class); |
| 75 | mWindowManager = mock(WindowManager.class); |
| 76 | mView = spy(new StatusBarWindowView(mContext, null)); |
| 77 | when(mStatusBar.getStatusBarWindow()).thenReturn(mView); |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 78 | when(mStatusBar.getNavigationBarWindow()).thenReturn(mView); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 79 | mContext.putComponent(StatusBar.class, mStatusBar); |
| 80 | |
| 81 | Display display = mContext.getSystemService(WindowManager.class).getDefaultDisplay(); |
| 82 | when(mWindowManager.getDefaultDisplay()).thenReturn(display); |
| 83 | mContext.addMockSystemService(WindowManager.class, mWindowManager); |
| 84 | |
| 85 | mFragmentService = mDependency.injectMockDependency(FragmentService.class); |
| 86 | mFragmentHostManager = mock(FragmentHostManager.class); |
| 87 | when(mFragmentService.getFragmentHostManager(any())).thenReturn(mFragmentHostManager); |
| 88 | |
| 89 | mTunerService = mDependency.injectMockDependency(TunerService.class); |
| 90 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 91 | mScreenDecorations = new ScreenDecorations(); |
| 92 | mScreenDecorations.mContext = mContext; |
| 93 | mScreenDecorations.mComponents = mContext.getComponents(); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 94 | |
| 95 | mTunablePaddingService = mDependency.injectMockDependency(TunablePaddingService.class); |
| 96 | } |
| 97 | |
| 98 | @Test |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 99 | public void testNoRounding_NoCutout() { |
| 100 | mContext.getOrCreateTestableResources().addOverride( |
| 101 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 102 | mContext.getOrCreateTestableResources().addOverride(dimen.rounded_corner_radius, 0); |
Beverly | e91f0d0 | 2018-05-15 14:40:47 -0400 | [diff] [blame] | 103 | mContext.getOrCreateTestableResources().addOverride(dimen.rounded_corner_radius_top, 0); |
| 104 | mContext.getOrCreateTestableResources().addOverride(dimen.rounded_corner_radius_bottom, 0); |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 105 | mContext.getOrCreateTestableResources() |
| 106 | .addOverride(dimen.rounded_corner_content_padding, 0); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 107 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 108 | mScreenDecorations.start(); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 109 | // No views added. |
| 110 | verify(mWindowManager, never()).addView(any(), any()); |
| 111 | // No Fragments watched. |
| 112 | verify(mFragmentHostManager, never()).addTagListener(any(), any()); |
| 113 | // No Tuners tuned. |
| 114 | verify(mTunerService, never()).addTunable(any(), any()); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testRounding() { |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 119 | mContext.getOrCreateTestableResources().addOverride( |
| 120 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 121 | mContext.getOrCreateTestableResources().addOverride(dimen.rounded_corner_radius, 20); |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 122 | mContext.getOrCreateTestableResources() |
| 123 | .addOverride(dimen.rounded_corner_content_padding, 20); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 124 | |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 125 | mScreenDecorations.start(); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 126 | // Add 2 windows for rounded corners (top and bottom). |
| 127 | verify(mWindowManager, times(2)).addView(any(), any()); |
| 128 | |
Jason Monk | 089981b | 2017-06-30 09:10:01 -0400 | [diff] [blame] | 129 | // Add 2 tag listeners for each of the fragments that are needed. |
| 130 | verify(mFragmentHostManager, times(2)).addTagListener(any(), any()); |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 131 | // One tunable. |
| 132 | verify(mTunerService, times(1)).addTunable(any(), any()); |
| 133 | // One TunablePadding. |
| 134 | verify(mTunablePaddingService, times(1)).add(any(), anyString(), anyInt(), anyInt()); |
| 135 | } |
| 136 | |
| 137 | @Test |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 138 | public void testCutout() { |
| 139 | mContext.getOrCreateTestableResources().addOverride( |
| 140 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, true); |
| 141 | mContext.getOrCreateTestableResources().addOverride(dimen.rounded_corner_radius, 0); |
| 142 | mContext.getOrCreateTestableResources() |
| 143 | .addOverride(dimen.rounded_corner_content_padding, 0); |
| 144 | |
| 145 | mScreenDecorations.start(); |
| 146 | // Add 2 windows for rounded corners (top and bottom). |
| 147 | verify(mWindowManager, times(2)).addView(any(), any()); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testDelayedCutout() { |
| 152 | mContext.getOrCreateTestableResources().addOverride( |
| 153 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, false); |
| 154 | mContext.getOrCreateTestableResources().addOverride(dimen.rounded_corner_radius, 0); |
| 155 | mContext.getOrCreateTestableResources() |
| 156 | .addOverride(dimen.rounded_corner_content_padding, 0); |
| 157 | |
| 158 | mScreenDecorations.start(); |
| 159 | |
| 160 | mContext.getOrCreateTestableResources().addOverride( |
| 161 | com.android.internal.R.bool.config_fillMainBuiltInDisplayCutout, true); |
| 162 | mScreenDecorations.onConfigurationChanged(new Configuration()); |
| 163 | |
| 164 | // Add 2 windows for rounded corners (top and bottom). |
| 165 | verify(mWindowManager, times(2)).addView(any(), any()); |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void hasRoundedCornerOverlayFlagSet() { |
| 170 | assertThat(mScreenDecorations.getWindowLayoutParams().privateFlags |
| 171 | & PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY, |
| 172 | is(PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY)); |
| 173 | } |
| 174 | |
| 175 | @Test |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 176 | public void testPaddingTagListener() { |
| 177 | TunablePaddingTagListener tagListener = new TunablePaddingTagListener(14, 5); |
| 178 | View v = mock(View.class); |
| 179 | View child = mock(View.class); |
| 180 | Fragment f = mock(Fragment.class); |
| 181 | TunablePadding padding = mock(TunablePadding.class); |
| 182 | |
| 183 | when(mTunablePaddingService.add(any(), anyString(), anyInt(), anyInt())) |
| 184 | .thenReturn(padding); |
| 185 | when(f.getView()).thenReturn(v); |
| 186 | when(v.findViewById(5)).thenReturn(child); |
| 187 | |
| 188 | // Trigger callback and verify we get a TunablePadding created. |
| 189 | tagListener.onFragmentViewCreated(null, f); |
Adrian Roos | 5b51885 | 2018-01-23 17:23:38 +0100 | [diff] [blame] | 190 | verify(mTunablePaddingService).add(eq(child), eq(ScreenDecorations.PADDING), eq(14), |
Jason Monk | d4afe15 | 2017-05-01 15:37:43 -0400 | [diff] [blame] | 191 | eq(FLAG_START | FLAG_END)); |
| 192 | |
| 193 | // Call again and verify destroy is called. |
| 194 | tagListener.onFragmentViewCreated(null, f); |
| 195 | verify(padding).destroy(); |
| 196 | } |
| 197 | |
Geoffrey Pitsch | 0053e99 | 2017-05-22 15:32:29 -0400 | [diff] [blame] | 198 | } |