DisplayManagerServiceTest.java revision b0608636a29bcec8ecb4391cd50f29f68b3e7e81
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 17package com.android.server.display; 18 19import android.content.Context; 20import android.hardware.display.DisplayManager; 21import android.hardware.display.DisplayViewport; 22import android.hardware.display.IVirtualDisplayCallback; 23import android.hardware.input.InputManagerInternal; 24import android.os.Handler; 25import android.os.IBinder; 26import android.test.AndroidTestCase; 27import android.test.suitebuilder.annotation.SmallTest; 28import android.view.SurfaceControl; 29import android.view.WindowManagerInternal; 30 31import com.android.server.LocalServices; 32import com.android.server.display.DisplayManagerService.SyncRoot; 33import com.android.server.display.VirtualDisplayAdapter.SurfaceControlDisplayFactory; 34 35import org.mockito.ArgumentCaptor; 36import org.mockito.Mock; 37import org.mockito.MockitoAnnotations; 38 39import java.util.List; 40 41import static org.mockito.Matchers.any; 42import static org.mockito.Mockito.verify; 43import static org.mockito.Mockito.when; 44 45@SmallTest 46public class DisplayManagerServiceTest extends AndroidTestCase { 47 private Handler mHandler; 48 private DisplayManagerService mDisplayManager; 49 @Mock InputManagerInternal mMockInputManagerInternal; 50 @Mock IVirtualDisplayCallback.Stub mMockAppToken; 51 @Mock WindowManagerInternal mMockWindowManagerInternal; 52 @Mock VirtualDisplayAdapter mMockVirtualDisplayAdapter; 53 @Mock IBinder mMockDisplayToken; 54 55 @Override 56 protected void setUp() throws Exception { 57 MockitoAnnotations.initMocks(this); 58 mDisplayManager = new DisplayManagerService(mContext, 59 new DisplayManagerService.Injector() { 60 @Override 61 VirtualDisplayAdapter getVirtualDisplayAdapter(SyncRoot syncRoot, Context context, 62 Handler handler, DisplayAdapter.Listener displayAdapterListener) { 63 return new VirtualDisplayAdapter(syncRoot, context, handler, displayAdapterListener, 64 (String name, boolean secure) -> mMockDisplayToken); 65 } 66 }); 67 mHandler = mDisplayManager.getDisplayHandler(); 68 69 LocalServices.removeServiceForTest(InputManagerInternal.class); 70 LocalServices.addService(InputManagerInternal.class, mMockInputManagerInternal); 71 LocalServices.removeServiceForTest(WindowManagerInternal.class); 72 LocalServices.addService(WindowManagerInternal.class, mMockWindowManagerInternal); 73 74 mDisplayManager.systemReady(false /* safeMode */, false /* onlyCore */); 75 mDisplayManager.windowManagerAndInputReady(); 76 super.setUp(); 77 } 78 79 @Override 80 protected void tearDown() throws Exception { 81 super.tearDown(); 82 } 83 84 public void testCreateVirtualDisplay_sentToInputManager() throws Exception { 85 // This is effectively the DisplayManager service published to ServiceManager. 86 DisplayManagerService.BinderService bs = mDisplayManager.new BinderService(); 87 88 String uniqueId = "uniqueId --- Test"; 89 String uniqueIdPrefix = "virtual:" + mContext.getPackageName() + ":"; 90 int width = 600; 91 int height = 800; 92 int dpi = 320; 93 int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH; 94 95 when(mMockAppToken.asBinder()).thenReturn(mMockAppToken); 96 int displayId = bs.createVirtualDisplay(mMockAppToken /* callback */, 97 null /* projection */, "com.android.frameworks.servicestests", 98 "Test Virtual Display", width, height, dpi, null /* surface */, flags /* flags */, 99 uniqueId); 100 101 mDisplayManager.performTraversalInTransactionFromWindowManagerInternal(); 102 103 // flush the handler 104 mHandler.runWithScissors(() -> {}, 0 /* now */); 105 106 ArgumentCaptor<List<DisplayViewport>> virtualViewportCaptor = 107 ArgumentCaptor.forClass(List.class); 108 verify(mMockInputManagerInternal).setDisplayViewports( 109 any(), any(), virtualViewportCaptor.capture()); 110 111 assertEquals(1, virtualViewportCaptor.getValue().size()); 112 DisplayViewport dv = virtualViewportCaptor.getValue().get(0); 113 assertEquals(height, dv.deviceHeight); 114 assertEquals(width, dv.deviceWidth); 115 assertEquals(uniqueIdPrefix + uniqueId, dv.uniqueId); 116 assertEquals(displayId, dv.displayId); 117 } 118} 119