Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [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 |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.display; |
| 18 | |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 19 | import static com.android.server.display.VirtualDisplayAdapter.UNIQUE_ID_PREFIX; |
| 20 | |
Kevin DuBois | bf76b11b | 2018-09-04 09:14:15 -0700 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertNotNull; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.junit.Assert.fail; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Dan Gittik | 122df86 | 2018-03-28 16:59:22 +0100 | [diff] [blame] | 30 | import android.hardware.display.BrightnessConfiguration; |
| 31 | import android.hardware.display.Curve; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 32 | import android.hardware.display.DisplayManager; |
| 33 | import android.hardware.display.DisplayViewport; |
Kevin DuBois | bf76b11b | 2018-09-04 09:14:15 -0700 | [diff] [blame] | 34 | import android.hardware.display.DisplayedContentSample; |
| 35 | import android.hardware.display.DisplayedContentSamplingAttributes; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 36 | import android.hardware.display.IVirtualDisplayCallback; |
| 37 | import android.hardware.input.InputManagerInternal; |
| 38 | import android.os.Handler; |
| 39 | import android.os.IBinder; |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 40 | import android.view.Display; |
| 41 | import android.view.DisplayInfo; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 42 | import android.view.SurfaceControl; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 43 | |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 44 | import androidx.test.InstrumentationRegistry; |
| 45 | import androidx.test.filters.SmallTest; |
Kevin DuBois | bf76b11b | 2018-09-04 09:14:15 -0700 | [diff] [blame] | 46 | import androidx.test.runner.AndroidJUnit4; |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 47 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 48 | import com.android.server.LocalServices; |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 49 | import com.android.server.SystemService; |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 50 | import com.android.server.display.DisplayDeviceInfo; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 51 | import com.android.server.display.DisplayManagerService.SyncRoot; |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 52 | import com.android.server.lights.LightsManager; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 53 | import com.android.server.wm.WindowManagerInternal; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 54 | |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 55 | import org.junit.Before; |
| 56 | import org.junit.Test; |
| 57 | import org.junit.runner.RunWith; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 58 | import org.mockito.ArgumentCaptor; |
| 59 | import org.mockito.Mock; |
| 60 | import org.mockito.MockitoAnnotations; |
| 61 | |
| 62 | import java.util.List; |
Kevin DuBois | bf76b11b | 2018-09-04 09:14:15 -0700 | [diff] [blame] | 63 | import java.util.stream.LongStream; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 64 | |
| 65 | @SmallTest |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 66 | @RunWith(AndroidJUnit4.class) |
| 67 | public class DisplayManagerServiceTest { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 68 | private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1; |
| 69 | private static final long SHORT_DEFAULT_DISPLAY_TIMEOUT_MILLIS = 10; |
| 70 | |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 71 | private Context mContext; |
| 72 | |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 73 | private final DisplayManagerService.Injector mShortMockedInjector = |
| 74 | new DisplayManagerService.Injector() { |
| 75 | @Override |
| 76 | VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, |
| 77 | Context context, Handler handler, DisplayAdapter.Listener listener) { |
| 78 | return mMockVirtualDisplayAdapter; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | long getDefaultDisplayDelayTimeout() { |
| 83 | return SHORT_DEFAULT_DISPLAY_TIMEOUT_MILLIS; |
| 84 | } |
| 85 | }; |
| 86 | private final DisplayManagerService.Injector mBasicInjector = |
| 87 | new DisplayManagerService.Injector() { |
| 88 | @Override |
| 89 | VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, |
| 90 | Context context, Handler handler, |
| 91 | DisplayAdapter.Listener displayAdapterListener) { |
| 92 | return new VirtualDisplayAdapter(syncRoot, context, handler, |
| 93 | displayAdapterListener, |
| 94 | (String name, boolean secure) -> mMockDisplayToken); |
| 95 | } |
| 96 | }; |
| 97 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 98 | @Mock InputManagerInternal mMockInputManagerInternal; |
| 99 | @Mock IVirtualDisplayCallback.Stub mMockAppToken; |
| 100 | @Mock WindowManagerInternal mMockWindowManagerInternal; |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 101 | @Mock LightsManager mMockLightsManager; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 102 | @Mock VirtualDisplayAdapter mMockVirtualDisplayAdapter; |
| 103 | @Mock IBinder mMockDisplayToken; |
| 104 | |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 105 | @Before |
| 106 | public void setUp() throws Exception { |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 107 | MockitoAnnotations.initMocks(this); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 108 | |
| 109 | LocalServices.removeServiceForTest(InputManagerInternal.class); |
| 110 | LocalServices.addService(InputManagerInternal.class, mMockInputManagerInternal); |
| 111 | LocalServices.removeServiceForTest(WindowManagerInternal.class); |
| 112 | LocalServices.addService(WindowManagerInternal.class, mMockWindowManagerInternal); |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 113 | LocalServices.removeServiceForTest(LightsManager.class); |
| 114 | LocalServices.addService(LightsManager.class, mMockLightsManager); |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 115 | |
| 116 | mContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 119 | @Test |
| 120 | public void testCreateVirtualDisplay_sentToInputManager() { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 121 | DisplayManagerService displayManager = |
| 122 | new DisplayManagerService(mContext, mBasicInjector); |
| 123 | registerDefaultDisplays(displayManager); |
| 124 | displayManager.systemReady(false /* safeMode */, true /* onlyCore */); |
| 125 | displayManager.windowManagerAndInputReady(); |
| 126 | |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 127 | // This is effectively the DisplayManager service published to ServiceManager. |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 128 | DisplayManagerService.BinderService bs = displayManager.new BinderService(); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 129 | |
| 130 | String uniqueId = "uniqueId --- Test"; |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 131 | String uniqueIdPrefix = UNIQUE_ID_PREFIX + mContext.getPackageName() + ":"; |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 132 | int width = 600; |
| 133 | int height = 800; |
| 134 | int dpi = 320; |
| 135 | int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH; |
| 136 | |
| 137 | when(mMockAppToken.asBinder()).thenReturn(mMockAppToken); |
| 138 | int displayId = bs.createVirtualDisplay(mMockAppToken /* callback */, |
| 139 | null /* projection */, "com.android.frameworks.servicestests", |
| 140 | "Test Virtual Display", width, height, dpi, null /* surface */, flags /* flags */, |
| 141 | uniqueId); |
| 142 | |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 143 | displayManager.performTraversalInternal(mock(SurfaceControl.Transaction.class)); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 144 | |
| 145 | // flush the handler |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 146 | displayManager.getDisplayHandler().runWithScissors(() -> {}, 0 /* now */); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 147 | |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 148 | ArgumentCaptor<List<DisplayViewport>> viewportCaptor = ArgumentCaptor.forClass(List.class); |
| 149 | verify(mMockInputManagerInternal).setDisplayViewports(viewportCaptor.capture()); |
| 150 | List<DisplayViewport> viewports = viewportCaptor.getValue(); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 151 | |
Arthur Hung | 41e81e7 | 2018-10-31 18:04:56 +0800 | [diff] [blame] | 152 | // Expect to receive 2 viewports: internal, and virtual |
| 153 | assertEquals(2, viewports.size()); |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 154 | |
| 155 | DisplayViewport virtualViewport = null; |
| 156 | DisplayViewport internalViewport = null; |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 157 | for (int i = 0; i < viewports.size(); i++) { |
| 158 | DisplayViewport v = viewports.get(i); |
| 159 | switch (v.type) { |
| 160 | case DisplayViewport.VIEWPORT_INTERNAL: { |
| 161 | internalViewport = v; |
| 162 | break; |
| 163 | } |
| 164 | case DisplayViewport.VIEWPORT_EXTERNAL: { |
Arthur Hung | 41e81e7 | 2018-10-31 18:04:56 +0800 | [diff] [blame] | 165 | fail("EXTERNAL viewport should not exist."); |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 166 | break; |
| 167 | } |
| 168 | case DisplayViewport.VIEWPORT_VIRTUAL: { |
| 169 | virtualViewport = v; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | } |
Arthur Hung | 41e81e7 | 2018-10-31 18:04:56 +0800 | [diff] [blame] | 174 | // INTERNAL viewport gets created upon access. |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 175 | assertNotNull(internalViewport); |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 176 | assertNotNull(virtualViewport); |
| 177 | |
Arthur Hung | 41e81e7 | 2018-10-31 18:04:56 +0800 | [diff] [blame] | 178 | // INTERNAL |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 179 | assertTrue(internalViewport.valid); |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 180 | |
| 181 | // VIRTUAL |
| 182 | assertEquals(height, virtualViewport.deviceHeight); |
| 183 | assertEquals(width, virtualViewport.deviceWidth); |
| 184 | assertEquals(uniqueIdPrefix + uniqueId, virtualViewport.uniqueId); |
| 185 | assertEquals(displayId, virtualViewport.displayId); |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 186 | } |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 187 | |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 188 | @Test |
| 189 | public void testPhysicalViewports() { |
| 190 | DisplayManagerService displayManager = |
| 191 | new DisplayManagerService(mContext, mBasicInjector); |
| 192 | registerDefaultDisplays(displayManager); |
| 193 | displayManager.systemReady(false /* safeMode */, true /* onlyCore */); |
| 194 | displayManager.windowManagerAndInputReady(); |
| 195 | |
| 196 | // This is effectively the DisplayManager service published to ServiceManager. |
| 197 | DisplayManagerService.BinderService bs = displayManager.new BinderService(); |
| 198 | |
| 199 | when(mMockAppToken.asBinder()).thenReturn(mMockAppToken); |
| 200 | |
| 201 | final int displayIds[] = bs.getDisplayIds(); |
| 202 | assertEquals(1, displayIds.length); |
| 203 | final int displayId = displayIds[0]; |
| 204 | DisplayInfo info = bs.getDisplayInfo(displayId); |
| 205 | assertEquals(info.type, Display.TYPE_BUILT_IN); |
| 206 | |
| 207 | displayManager.performTraversalInternal(mock(SurfaceControl.Transaction.class)); |
| 208 | |
| 209 | // flush the handler |
| 210 | displayManager.getDisplayHandler().runWithScissors(() -> {}, 0 /* now */); |
| 211 | |
| 212 | ArgumentCaptor<List<DisplayViewport>> viewportCaptor = ArgumentCaptor.forClass(List.class); |
| 213 | verify(mMockInputManagerInternal).setDisplayViewports(viewportCaptor.capture()); |
| 214 | List<DisplayViewport> viewports = viewportCaptor.getValue(); |
| 215 | |
Arthur Hung | 41e81e7 | 2018-10-31 18:04:56 +0800 | [diff] [blame] | 216 | // Expect to receive actual viewports: 1 internal |
| 217 | assertEquals(1, viewports.size()); |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 218 | |
Arthur Hung | 41e81e7 | 2018-10-31 18:04:56 +0800 | [diff] [blame] | 219 | DisplayViewport internalViewport = viewports.get(0); |
| 220 | |
| 221 | // INTERNAL is the only one actual display. |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 222 | assertNotNull(internalViewport); |
Arthur Hung | 41e81e7 | 2018-10-31 18:04:56 +0800 | [diff] [blame] | 223 | assertEquals(DisplayViewport.VIEWPORT_INTERNAL, internalViewport.type); |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 224 | assertTrue(internalViewport.valid); |
| 225 | assertEquals(displayId, internalViewport.displayId); |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Test |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 229 | public void testCreateVirtualDisplayRotatesWithContent() throws Exception { |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 230 | DisplayManagerService displayManager = |
| 231 | new DisplayManagerService(mContext, mBasicInjector); |
| 232 | registerDefaultDisplays(displayManager); |
| 233 | |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 234 | // This is effectively the DisplayManager service published to ServiceManager. |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 235 | DisplayManagerService.BinderService bs = displayManager.new BinderService(); |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 236 | |
| 237 | String uniqueId = "uniqueId --- Rotates With Content Test"; |
| 238 | int width = 600; |
| 239 | int height = 800; |
| 240 | int dpi = 320; |
| 241 | int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT; |
| 242 | |
| 243 | when(mMockAppToken.asBinder()).thenReturn(mMockAppToken); |
| 244 | int displayId = bs.createVirtualDisplay(mMockAppToken /* callback */, |
| 245 | null /* projection */, "com.android.frameworks.servicestests", |
| 246 | "Test Virtual Display", width, height, dpi, null /* surface */, flags /* flags */, |
| 247 | uniqueId); |
| 248 | |
Robert Carr | ae606b4 | 2018-02-15 15:36:23 -0800 | [diff] [blame] | 249 | displayManager.performTraversalInternal(mock(SurfaceControl.Transaction.class)); |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 250 | |
| 251 | // flush the handler |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 252 | displayManager.getDisplayHandler().runWithScissors(() -> {}, 0 /* now */); |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 253 | |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 254 | DisplayDeviceInfo ddi = displayManager.getDisplayDeviceInfoInternal(displayId); |
Alex Sakhartchouk | 879d24f | 2017-06-20 22:01:19 -0400 | [diff] [blame] | 255 | assertNotNull(ddi); |
| 256 | assertTrue((ddi.flags & DisplayDeviceInfo.FLAG_ROTATES_WITH_CONTENT) != 0); |
| 257 | } |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Tests that the virtual display is created along-side the default display. |
| 261 | */ |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 262 | @Test |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 263 | public void testStartVirtualDisplayWithDefaultDisplay_Succeeds() throws Exception { |
| 264 | DisplayManagerService displayManager = |
| 265 | new DisplayManagerService(mContext, mShortMockedInjector); |
| 266 | registerDefaultDisplays(displayManager); |
| 267 | displayManager.onBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Tests that we get a Runtime exception when we cannot initialize the default display. |
| 272 | */ |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 273 | @Test |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 274 | public void testStartVirtualDisplayWithDefDisplay_NoDefaultDisplay() throws Exception { |
| 275 | DisplayManagerService displayManager = |
| 276 | new DisplayManagerService(mContext, mShortMockedInjector); |
| 277 | Handler handler = displayManager.getDisplayHandler(); |
| 278 | handler.runWithScissors(() -> {}, 0 /* now */); |
| 279 | |
| 280 | try { |
| 281 | displayManager.onBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY); |
| 282 | } catch (RuntimeException e) { |
| 283 | return; |
| 284 | } |
| 285 | fail("Expected DisplayManager to throw RuntimeException when it cannot initialize the" |
| 286 | + " default display"); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Tests that we get a Runtime exception when we cannot initialize the virtual display. |
| 291 | */ |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 292 | @Test |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 293 | public void testStartVirtualDisplayWithDefDisplay_NoVirtualDisplayAdapter() throws Exception { |
| 294 | DisplayManagerService displayManager = new DisplayManagerService(mContext, |
| 295 | new DisplayManagerService.Injector() { |
| 296 | @Override |
| 297 | VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, |
| 298 | Context context, Handler handler, DisplayAdapter.Listener listener) { |
| 299 | return null; // return null for the adapter. This should cause a failure. |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | long getDefaultDisplayDelayTimeout() { |
| 304 | return SHORT_DEFAULT_DISPLAY_TIMEOUT_MILLIS; |
| 305 | } |
| 306 | }); |
| 307 | try { |
| 308 | displayManager.onBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY); |
| 309 | } catch (RuntimeException e) { |
| 310 | return; |
| 311 | } |
| 312 | fail("Expected DisplayManager to throw RuntimeException when it cannot initialize the" |
| 313 | + " virtual display adapter"); |
| 314 | } |
| 315 | |
Dan Gittik | 122df86 | 2018-03-28 16:59:22 +0100 | [diff] [blame] | 316 | /** |
| 317 | * Tests that an exception is raised for too dark a brightness configuration. |
| 318 | */ |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 319 | @Test |
Dan Gittik | 122df86 | 2018-03-28 16:59:22 +0100 | [diff] [blame] | 320 | public void testTooDarkBrightnessConfigurationThrowException() { |
| 321 | DisplayManagerService displayManager = |
| 322 | new DisplayManagerService(mContext, mShortMockedInjector); |
| 323 | Curve minimumBrightnessCurve = displayManager.getMinimumBrightnessCurveInternal(); |
| 324 | float[] lux = minimumBrightnessCurve.getX(); |
| 325 | float[] minimumNits = minimumBrightnessCurve.getY(); |
| 326 | float[] nits = new float[minimumNits.length]; |
| 327 | // For every control point, assert that making it slightly lower than the minimum throws an |
| 328 | // exception. |
| 329 | for (int i = 0; i < nits.length; i++) { |
| 330 | for (int j = 0; j < nits.length; j++) { |
| 331 | nits[j] = minimumNits[j]; |
| 332 | if (j == i) { |
| 333 | nits[j] -= 0.1f; |
| 334 | } |
| 335 | if (nits[j] < 0) { |
| 336 | nits[j] = 0; |
| 337 | } |
| 338 | } |
| 339 | BrightnessConfiguration config = |
| 340 | new BrightnessConfiguration.Builder(lux, nits).build(); |
| 341 | Exception thrown = null; |
| 342 | try { |
| 343 | displayManager.validateBrightnessConfiguration(config); |
| 344 | } catch (IllegalArgumentException e) { |
| 345 | thrown = e; |
| 346 | } |
| 347 | assertNotNull("Building too dark a brightness configuration must throw an exception"); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Tests that no exception is raised for not too dark a brightness configuration. |
| 353 | */ |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 354 | @Test |
Dan Gittik | 122df86 | 2018-03-28 16:59:22 +0100 | [diff] [blame] | 355 | public void testBrightEnoughBrightnessConfigurationDoesNotThrowException() { |
| 356 | DisplayManagerService displayManager = |
| 357 | new DisplayManagerService(mContext, mShortMockedInjector); |
| 358 | Curve minimumBrightnessCurve = displayManager.getMinimumBrightnessCurveInternal(); |
| 359 | float[] lux = minimumBrightnessCurve.getX(); |
| 360 | float[] nits = minimumBrightnessCurve.getY(); |
| 361 | BrightnessConfiguration config = new BrightnessConfiguration.Builder(lux, nits).build(); |
| 362 | displayManager.validateBrightnessConfiguration(config); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Tests that null brightness configurations are alright. |
| 367 | */ |
Siarhei Vishniakou | 2eb0f8f | 2018-07-06 23:30:12 +0100 | [diff] [blame] | 368 | @Test |
Dan Gittik | 122df86 | 2018-03-28 16:59:22 +0100 | [diff] [blame] | 369 | public void testNullBrightnessConfiguration() { |
| 370 | DisplayManagerService displayManager = |
| 371 | new DisplayManagerService(mContext, mShortMockedInjector); |
| 372 | displayManager.validateBrightnessConfiguration(null); |
| 373 | } |
| 374 | |
Kevin DuBois | bf76b11b | 2018-09-04 09:14:15 -0700 | [diff] [blame] | 375 | /** |
| 376 | * Tests that collection of display color sampling results are sensible. |
| 377 | */ |
| 378 | @Test |
| 379 | public void testDisplayedContentSampling() { |
| 380 | DisplayManagerService displayManager = |
| 381 | new DisplayManagerService(mContext, mShortMockedInjector); |
| 382 | registerDefaultDisplays(displayManager); |
| 383 | |
| 384 | DisplayDeviceInfo ddi = displayManager.getDisplayDeviceInfoInternal(0); |
| 385 | assertNotNull(ddi); |
| 386 | |
| 387 | DisplayedContentSamplingAttributes attr = |
| 388 | displayManager.getDisplayedContentSamplingAttributesInternal(0); |
| 389 | if (attr == null) return; //sampling not supported on device, skip remainder of test. |
| 390 | |
| 391 | boolean enabled = displayManager.setDisplayedContentSamplingEnabledInternal(0, true, 0, 0); |
Kevin DuBois | 205a680 | 2019-01-07 17:04:46 -0800 | [diff] [blame] | 392 | assertTrue(enabled); |
Kevin DuBois | bf76b11b | 2018-09-04 09:14:15 -0700 | [diff] [blame] | 393 | |
| 394 | displayManager.setDisplayedContentSamplingEnabledInternal(0, false, 0, 0); |
| 395 | DisplayedContentSample sample = displayManager.getDisplayedContentSampleInternal(0, 0, 0); |
| 396 | assertNotNull(sample); |
| 397 | |
| 398 | long numPixels = ddi.width * ddi.height * sample.getNumFrames(); |
| 399 | long[] samples = sample.getSampleComponent(DisplayedContentSample.ColorComponent.CHANNEL0); |
| 400 | assertTrue(samples.length == 0 || LongStream.of(samples).sum() == numPixels); |
| 401 | |
| 402 | samples = sample.getSampleComponent(DisplayedContentSample.ColorComponent.CHANNEL1); |
| 403 | assertTrue(samples.length == 0 || LongStream.of(samples).sum() == numPixels); |
| 404 | |
| 405 | samples = sample.getSampleComponent(DisplayedContentSample.ColorComponent.CHANNEL2); |
| 406 | assertTrue(samples.length == 0 || LongStream.of(samples).sum() == numPixels); |
| 407 | |
| 408 | samples = sample.getSampleComponent(DisplayedContentSample.ColorComponent.CHANNEL3); |
| 409 | assertTrue(samples.length == 0 || LongStream.of(samples).sum() == numPixels); |
| 410 | } |
| 411 | |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 412 | private void registerDefaultDisplays(DisplayManagerService displayManager) { |
| 413 | Handler handler = displayManager.getDisplayHandler(); |
| 414 | // Would prefer to call displayManager.onStart() directly here but it performs binderService |
| 415 | // registration which triggers security exceptions when running from a test. |
| 416 | handler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS); |
| 417 | // flush the handler |
| 418 | handler.runWithScissors(() -> {}, 0 /* now */); |
| 419 | } |
Santos Cordon | ee8931e | 2017-04-05 10:31:15 -0700 | [diff] [blame] | 420 | } |