1 /* 2 * Copyright (C) 2022 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.am; 18 19 import static android.view.Display.DEFAULT_DISPLAY; 20 21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; 22 23 import static com.google.common.truth.Truth.assertWithMessage; 24 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.hardware.display.DisplayManager; 30 import android.os.UserManager; 31 import android.util.Log; 32 import android.view.Display; 33 34 import com.android.server.ExtendedMockitoRule; 35 import com.android.server.am.ActivityManagerService.Injector; 36 37 import org.junit.Before; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.mockito.Mock; 41 42 import java.util.Arrays; 43 44 /** 45 * Run as {@code atest 46 * FrameworksMockingServicesTests:com.android.server.am.ActivityManagerServiceInjectorTest} 47 */ 48 public final class ActivityManagerServiceInjectorTest { 49 50 private static final String TAG = ActivityManagerServiceInjectorTest.class.getSimpleName(); 51 52 private final Display mDefaultDisplay = validDisplay(DEFAULT_DISPLAY); 53 54 @Mock private Context mContext; 55 @Mock private DisplayManager mDisplayManager; 56 57 private Injector mInjector; 58 59 @Before setFixture()60 public void setFixture() { 61 mInjector = new Injector(mContext); 62 63 when(mContext.getSystemService(DisplayManager.class)).thenReturn(mDisplayManager); 64 } 65 66 @Rule 67 public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this) 68 .spyStatic(UserManager.class) 69 .build(); 70 71 @Test testGetDisplayIdsForStartingBackgroundUsers_notSupported()72 public void testGetDisplayIdsForStartingBackgroundUsers_notSupported() { 73 mockUmIsUsersOnSecondaryDisplaysEnabled(false); 74 75 int [] displayIds = mInjector.getDisplayIdsForStartingVisibleBackgroundUsers(); 76 77 assertWithMessage("mAms.getDisplayIdsForStartingBackgroundUsers()") 78 .that(displayIds).isNull(); 79 } 80 81 @Test testGetDisplayIdsForStartingBackgroundUsers_noDisplaysAtAll()82 public void testGetDisplayIdsForStartingBackgroundUsers_noDisplaysAtAll() { 83 mockUmIsUsersOnSecondaryDisplaysEnabled(true); 84 mockGetDisplays(); 85 86 int[] displayIds = mInjector.getDisplayIdsForStartingVisibleBackgroundUsers(); 87 88 assertWithMessage("mAms.getDisplayIdsForStartingBackgroundUsers()") 89 .that(displayIds).isNull(); 90 } 91 92 @Test testGetDisplayIdsForStartingBackgroundUsers_defaultDisplayOnly()93 public void testGetDisplayIdsForStartingBackgroundUsers_defaultDisplayOnly() { 94 mockUmIsUsersOnSecondaryDisplaysEnabled(true); 95 mockGetDisplays(mDefaultDisplay); 96 97 int[] displayIds = mInjector.getDisplayIdsForStartingVisibleBackgroundUsers(); 98 99 assertWithMessage("mAms.getDisplayIdsForStartingBackgroundUsers()") 100 .that(displayIds).isNull(); 101 } 102 103 @Test testGetDisplayIdsForStartingBackgroundUsers_noDefaultDisplay()104 public void testGetDisplayIdsForStartingBackgroundUsers_noDefaultDisplay() { 105 mockUmIsUsersOnSecondaryDisplaysEnabled(true); 106 mockGetDisplays(validDisplay(42)); 107 108 int[] displayIds = mInjector.getDisplayIdsForStartingVisibleBackgroundUsers(); 109 110 assertWithMessage("mAms.getDisplayIdsForStartingBackgroundUsers()") 111 .that(displayIds).isNull(); 112 } 113 114 @Test testGetDisplayIdsForStartingBackgroundUsers_mixed()115 public void testGetDisplayIdsForStartingBackgroundUsers_mixed() { 116 mockUmIsUsersOnSecondaryDisplaysEnabled(true); 117 mockGetDisplays(mDefaultDisplay, validDisplay(42), invalidDisplay(108)); 118 119 int[] displayIds = mInjector.getDisplayIdsForStartingVisibleBackgroundUsers(); 120 121 assertWithMessage("mAms.getDisplayIdsForStartingBackgroundUsers()") 122 .that(displayIds).isNotNull(); 123 assertWithMessage("mAms.getDisplayIdsForStartingBackgroundUsers()") 124 .that(displayIds).asList().containsExactly(42); 125 } 126 127 // Extra test to make sure the array is properly copied... 128 @Test testGetDisplayIdsForStartingBackgroundUsers_mixed_invalidFirst()129 public void testGetDisplayIdsForStartingBackgroundUsers_mixed_invalidFirst() { 130 mockUmIsUsersOnSecondaryDisplaysEnabled(true); 131 mockGetDisplays(invalidDisplay(108), mDefaultDisplay, validDisplay(42)); 132 133 int[] displayIds = mInjector.getDisplayIdsForStartingVisibleBackgroundUsers(); 134 135 assertWithMessage("mAms.getDisplayIdsForStartingBackgroundUsers()") 136 .that(displayIds).asList().containsExactly(42); 137 } 138 validDisplay(int displayId)139 private Display validDisplay(int displayId) { 140 return mockDisplay(displayId, /* valid= */ true); 141 } 142 invalidDisplay(int displayId)143 private Display invalidDisplay(int displayId) { 144 return mockDisplay(displayId, /* valid= */ false); 145 } 146 mockDisplay(int displayId, boolean valid)147 private Display mockDisplay(int displayId, boolean valid) { 148 Display display = mock(Display.class); 149 150 when(display.getDisplayId()).thenReturn(displayId); 151 when(display.isValid()).thenReturn(valid); 152 153 return display; 154 } 155 mockGetDisplays(Display... displays)156 private void mockGetDisplays(Display... displays) { 157 Log.d(TAG, "mockGetDisplays(): " + Arrays.toString(displays)); 158 when(mDisplayManager.getDisplays()).thenReturn(displays); 159 } 160 mockUmIsUsersOnSecondaryDisplaysEnabled(boolean enabled)161 private void mockUmIsUsersOnSecondaryDisplaysEnabled(boolean enabled) { 162 Log.d(TAG, "Mocking UserManager.isUsersOnSecondaryDisplaysEnabled() to return " + enabled); 163 doReturn(enabled).when(() -> UserManager.isVisibleBackgroundUsersEnabled()); 164 } 165 } 166