Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 14 | * limitations under the License. |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Winson Chung | a4fa8d5 | 2018-04-20 15:54:51 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 20 | import static android.content.res.Configuration.ORIENTATION_PORTRAIT; |
| 21 | import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 22 | |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.never; |
| 25 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
| 26 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; |
| 27 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 28 | import static org.junit.Assert.assertEquals; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 29 | import static org.mockito.Matchers.any; |
| 30 | import static org.mockito.Matchers.anyInt; |
| 31 | import static org.mockito.Matchers.eq; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 32 | |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 33 | import android.app.ActivityManager.TaskDescription; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 34 | import android.app.ActivityManager.TaskSnapshot; |
Winson Chung | 48b2565 | 2018-10-22 14:04:30 -0700 | [diff] [blame] | 35 | import android.content.ComponentName; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 36 | import android.graphics.Canvas; |
| 37 | import android.graphics.Color; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 38 | import android.graphics.GraphicBuffer; |
| 39 | import android.graphics.PixelFormat; |
| 40 | import android.graphics.Rect; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 41 | import android.platform.test.annotations.Presubmit; |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 42 | import android.view.SurfaceControl; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 43 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 44 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 45 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 46 | import com.android.server.wm.TaskSnapshotSurface.Window; |
| 47 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 48 | import org.junit.Test; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Test class for {@link TaskSnapshotSurface}. |
| 52 | * |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 53 | * Build/Install/Run: |
| 54 | * atest FrameworksServicesTests:TaskSnapshotSurfaceTest |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 55 | */ |
| 56 | @SmallTest |
| 57 | @Presubmit |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 58 | public class TaskSnapshotSurfaceTest extends WindowTestsBase { |
| 59 | |
| 60 | private TaskSnapshotSurface mSurface; |
| 61 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 62 | private void setupSurface(int width, int height, Rect contentInsets, int sysuiVis, |
| 63 | int windowFlags, Rect taskBounds) { |
| 64 | final GraphicBuffer buffer = GraphicBuffer.create(width, height, PixelFormat.RGBA_8888, |
Tadashi G. Takaoka | 17aa9a5 | 2018-07-05 16:47:47 +0900 | [diff] [blame] | 65 | GraphicBuffer.USAGE_SW_READ_RARELY | GraphicBuffer.USAGE_SW_WRITE_NEVER); |
Winson Chung | 48b2565 | 2018-10-22 14:04:30 -0700 | [diff] [blame] | 66 | final TaskSnapshot snapshot = new TaskSnapshot(new ComponentName("", ""), buffer, |
Winson Chung | a4fa8d5 | 2018-04-20 15:54:51 -0700 | [diff] [blame] | 67 | ORIENTATION_PORTRAIT, contentInsets, false, 1.0f, true /* isRealSnapshot */, |
Winson Chung | 173020c | 2018-05-04 15:36:47 -0700 | [diff] [blame] | 68 | WINDOWING_MODE_FULLSCREEN, 0 /* systemUiVisibility */, false /* isTranslucent */); |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 69 | mSurface = new TaskSnapshotSurface(mWm, new Window(), new SurfaceControl(), snapshot, "Test", |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 70 | createTaskDescription(Color.WHITE, Color.RED, Color.BLUE), sysuiVis, windowFlags, 0, |
| 71 | taskBounds, ORIENTATION_PORTRAIT); |
| 72 | } |
| 73 | |
| 74 | private static TaskDescription createTaskDescription(int background, int statusBar, |
| 75 | int navigationBar) { |
| 76 | final TaskDescription td = new TaskDescription(); |
| 77 | td.setBackgroundColor(background); |
| 78 | td.setStatusBarColor(statusBar); |
| 79 | td.setNavigationBarColor(navigationBar); |
| 80 | return td; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private void setupSurface(int width, int height) { |
| 84 | setupSurface(width, height, new Rect(), 0, FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, |
| 85 | new Rect(0, 0, width, height)); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 89 | public void fillEmptyBackground_fillHorizontally() { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 90 | setupSurface(200, 100); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 91 | final Canvas mockCanvas = mock(Canvas.class); |
| 92 | when(mockCanvas.getWidth()).thenReturn(200); |
| 93 | when(mockCanvas.getHeight()).thenReturn(100); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 94 | mSurface.drawBackgroundAndBars(mockCanvas, new Rect(0, 0, 100, 200)); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 95 | verify(mockCanvas).drawRect(eq(100.0f), eq(0.0f), eq(200.0f), eq(100.0f), any()); |
| 96 | } |
| 97 | |
| 98 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 99 | public void fillEmptyBackground_fillVertically() { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 100 | setupSurface(100, 200); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 101 | final Canvas mockCanvas = mock(Canvas.class); |
| 102 | when(mockCanvas.getWidth()).thenReturn(100); |
| 103 | when(mockCanvas.getHeight()).thenReturn(200); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 104 | mSurface.drawBackgroundAndBars(mockCanvas, new Rect(0, 0, 200, 100)); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 105 | verify(mockCanvas).drawRect(eq(0.0f), eq(100.0f), eq(100.0f), eq(200.0f), any()); |
| 106 | } |
| 107 | |
| 108 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 109 | public void fillEmptyBackground_fillBoth() { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 110 | setupSurface(200, 200); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 111 | final Canvas mockCanvas = mock(Canvas.class); |
| 112 | when(mockCanvas.getWidth()).thenReturn(200); |
| 113 | when(mockCanvas.getHeight()).thenReturn(200); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 114 | mSurface.drawBackgroundAndBars(mockCanvas, new Rect(0, 0, 100, 100)); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 115 | verify(mockCanvas).drawRect(eq(100.0f), eq(0.0f), eq(200.0f), eq(100.0f), any()); |
| 116 | verify(mockCanvas).drawRect(eq(0.0f), eq(100.0f), eq(200.0f), eq(200.0f), any()); |
| 117 | } |
| 118 | |
| 119 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 120 | public void fillEmptyBackground_dontFill_sameSize() { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 121 | setupSurface(100, 100); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 122 | final Canvas mockCanvas = mock(Canvas.class); |
| 123 | when(mockCanvas.getWidth()).thenReturn(100); |
| 124 | when(mockCanvas.getHeight()).thenReturn(100); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 125 | mSurface.drawBackgroundAndBars(mockCanvas, new Rect(0, 0, 100, 100)); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 126 | verify(mockCanvas, never()).drawRect(anyInt(), anyInt(), anyInt(), anyInt(), any()); |
| 127 | } |
| 128 | |
| 129 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 130 | public void fillEmptyBackground_dontFill_bitmapLarger() { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 131 | setupSurface(100, 100); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 132 | final Canvas mockCanvas = mock(Canvas.class); |
| 133 | when(mockCanvas.getWidth()).thenReturn(100); |
| 134 | when(mockCanvas.getHeight()).thenReturn(100); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 135 | mSurface.drawBackgroundAndBars(mockCanvas, new Rect(0, 0, 200, 200)); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 136 | verify(mockCanvas, never()).drawRect(anyInt(), anyInt(), anyInt(), anyInt(), any()); |
| 137 | } |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 138 | |
| 139 | @Test |
| 140 | public void testCalculateSnapshotCrop() { |
| 141 | setupSurface(100, 100, new Rect(0, 10, 0, 10), 0, 0, new Rect(0, 0, 100, 100)); |
| 142 | assertEquals(new Rect(0, 0, 100, 90), mSurface.calculateSnapshotCrop()); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testCalculateSnapshotCrop_taskNotOnTop() { |
| 147 | setupSurface(100, 100, new Rect(0, 10, 0, 10), 0, 0, new Rect(0, 50, 100, 100)); |
| 148 | assertEquals(new Rect(0, 10, 100, 90), mSurface.calculateSnapshotCrop()); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testCalculateSnapshotCrop_navBarLeft() { |
| 153 | setupSurface(100, 100, new Rect(10, 10, 0, 0), 0, 0, new Rect(0, 0, 100, 100)); |
| 154 | assertEquals(new Rect(10, 0, 100, 100), mSurface.calculateSnapshotCrop()); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testCalculateSnapshotCrop_navBarRight() { |
| 159 | setupSurface(100, 100, new Rect(0, 10, 10, 0), 0, 0, new Rect(0, 0, 100, 100)); |
| 160 | assertEquals(new Rect(0, 0, 90, 100), mSurface.calculateSnapshotCrop()); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testCalculateSnapshotFrame() { |
| 165 | setupSurface(100, 100); |
| 166 | final Rect insets = new Rect(0, 10, 0, 10); |
| 167 | mSurface.setFrames(new Rect(0, 0, 100, 100), insets, insets); |
| 168 | assertEquals(new Rect(0, -10, 100, 70), |
| 169 | mSurface.calculateSnapshotFrame(new Rect(0, 10, 100, 90))); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void testCalculateSnapshotFrame_navBarLeft() { |
| 174 | setupSurface(100, 100); |
| 175 | final Rect insets = new Rect(10, 10, 0, 0); |
| 176 | mSurface.setFrames(new Rect(0, 0, 100, 100), insets, insets); |
| 177 | assertEquals(new Rect(0, -10, 90, 80), |
| 178 | mSurface.calculateSnapshotFrame(new Rect(10, 10, 100, 100))); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testDrawStatusBarBackground() { |
| 183 | setupSurface(100, 100); |
| 184 | final Rect insets = new Rect(0, 10, 10, 0); |
| 185 | mSurface.setFrames(new Rect(0, 0, 100, 100), insets, insets); |
| 186 | final Canvas mockCanvas = mock(Canvas.class); |
| 187 | when(mockCanvas.getWidth()).thenReturn(100); |
| 188 | when(mockCanvas.getHeight()).thenReturn(100); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 189 | mSurface.mSystemBarBackgroundPainter.drawStatusBarBackground( |
| 190 | mockCanvas, new Rect(0, 0, 50, 100), 10); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 191 | verify(mockCanvas).drawRect(eq(50.0f), eq(0.0f), eq(90.0f), eq(10.0f), any()); |
| 192 | } |
| 193 | |
| 194 | @Test |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 195 | public void testDrawStatusBarBackground_nullFrame() { |
| 196 | setupSurface(100, 100); |
| 197 | final Rect insets = new Rect(0, 10, 10, 0); |
| 198 | mSurface.setFrames(new Rect(0, 0, 100, 100), insets, insets); |
| 199 | final Canvas mockCanvas = mock(Canvas.class); |
| 200 | when(mockCanvas.getWidth()).thenReturn(100); |
| 201 | when(mockCanvas.getHeight()).thenReturn(100); |
| 202 | mSurface.mSystemBarBackgroundPainter.drawStatusBarBackground( |
| 203 | mockCanvas, null, 10); |
| 204 | verify(mockCanvas).drawRect(eq(0.0f), eq(0.0f), eq(90.0f), eq(10.0f), any()); |
| 205 | } |
| 206 | |
| 207 | @Test |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 208 | public void testDrawStatusBarBackground_nope() { |
| 209 | setupSurface(100, 100); |
| 210 | final Rect insets = new Rect(0, 10, 10, 0); |
| 211 | mSurface.setFrames(new Rect(0, 0, 100, 100), insets, insets); |
| 212 | final Canvas mockCanvas = mock(Canvas.class); |
| 213 | when(mockCanvas.getWidth()).thenReturn(100); |
| 214 | when(mockCanvas.getHeight()).thenReturn(100); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 215 | mSurface.mSystemBarBackgroundPainter.drawStatusBarBackground( |
| 216 | mockCanvas, new Rect(0, 0, 100, 100), 10); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 217 | verify(mockCanvas, never()).drawRect(anyInt(), anyInt(), anyInt(), anyInt(), any()); |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void testDrawNavigationBarBackground() { |
| 222 | final Rect insets = new Rect(0, 10, 0, 10); |
| 223 | setupSurface(100, 100, insets, 0, FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, |
| 224 | new Rect(0, 0, 100, 100)); |
| 225 | mSurface.setFrames(new Rect(0, 0, 100, 100), insets, insets); |
| 226 | final Canvas mockCanvas = mock(Canvas.class); |
| 227 | when(mockCanvas.getWidth()).thenReturn(100); |
| 228 | when(mockCanvas.getHeight()).thenReturn(100); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 229 | mSurface.mSystemBarBackgroundPainter.drawNavigationBarBackground(mockCanvas); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 230 | verify(mockCanvas).drawRect(eq(new Rect(0, 90, 100, 100)), any()); |
| 231 | } |
| 232 | |
| 233 | @Test |
| 234 | public void testDrawNavigationBarBackground_left() { |
| 235 | final Rect insets = new Rect(10, 10, 0, 0); |
| 236 | setupSurface(100, 100, insets, 0, FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, |
| 237 | new Rect(0, 0, 100, 100)); |
| 238 | mSurface.setFrames(new Rect(0, 0, 100, 100), insets, insets); |
| 239 | final Canvas mockCanvas = mock(Canvas.class); |
| 240 | when(mockCanvas.getWidth()).thenReturn(100); |
| 241 | when(mockCanvas.getHeight()).thenReturn(100); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 242 | mSurface.mSystemBarBackgroundPainter.drawNavigationBarBackground(mockCanvas); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 243 | verify(mockCanvas).drawRect(eq(new Rect(0, 0, 10, 100)), any()); |
| 244 | } |
| 245 | |
| 246 | @Test |
| 247 | public void testDrawNavigationBarBackground_right() { |
| 248 | final Rect insets = new Rect(0, 10, 10, 0); |
| 249 | setupSurface(100, 100, insets, 0, FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, |
| 250 | new Rect(0, 0, 100, 100)); |
| 251 | mSurface.setFrames(new Rect(0, 0, 100, 100), insets, insets); |
| 252 | final Canvas mockCanvas = mock(Canvas.class); |
| 253 | when(mockCanvas.getWidth()).thenReturn(100); |
| 254 | when(mockCanvas.getHeight()).thenReturn(100); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 255 | mSurface.mSystemBarBackgroundPainter.drawNavigationBarBackground(mockCanvas); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 256 | verify(mockCanvas).drawRect(eq(new Rect(90, 0, 100, 100)), any()); |
| 257 | } |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 258 | } |