Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.wm; |
| 18 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME; |
| 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 21 | import static android.testing.DexmakerShareClassLoaderRule.runWithDexmakerShareClassLoader; |
| 22 | import static android.view.Display.DEFAULT_DISPLAY; |
| 23 | |
| 24 | import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 25 | |
| 26 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.any; |
| 27 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyBoolean; |
| 28 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyInt; |
| 29 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyString; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 30 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; |
| 31 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
| 32 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.eq; |
| 33 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 34 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; |
| 35 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.nullable; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 36 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 37 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 38 | |
| 39 | import android.app.ActivityManagerInternal; |
| 40 | import android.app.AppOpsManager; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 41 | import android.app.usage.UsageStatsManagerInternal; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 42 | import android.content.BroadcastReceiver; |
| 43 | import android.content.ContentResolver; |
| 44 | import android.content.Context; |
| 45 | import android.content.IntentFilter; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 46 | import android.content.pm.IPackageManager; |
| 47 | import android.content.pm.PackageManagerInternal; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 48 | import android.database.ContentObserver; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 49 | import android.hardware.display.DisplayManager; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 50 | import android.hardware.display.DisplayManagerInternal; |
| 51 | import android.net.Uri; |
| 52 | import android.os.Handler; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 53 | import android.os.Looper; |
| 54 | import android.os.PowerManager; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 55 | import android.os.PowerManagerInternal; |
| 56 | import android.os.PowerSaveState; |
Riddle Hsu | 2da2d03 | 2019-08-28 21:08:58 +0800 | [diff] [blame] | 57 | import android.os.StrictMode; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 58 | import android.os.UserHandle; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 59 | import android.view.InputChannel; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 60 | import android.view.Surface; |
| 61 | import android.view.SurfaceControl; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 62 | |
| 63 | import com.android.dx.mockito.inline.extended.StaticMockitoSession; |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 64 | import com.android.server.AnimationThread; |
| 65 | import com.android.server.DisplayThread; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 66 | import com.android.server.LocalServices; |
| 67 | import com.android.server.LockGuard; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 68 | import com.android.server.UiThread; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 69 | import com.android.server.Watchdog; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 70 | import com.android.server.am.ActivityManagerService; |
| 71 | import com.android.server.appop.AppOpsService; |
| 72 | import com.android.server.display.color.ColorDisplayService; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 73 | import com.android.server.firewall.IntentFirewall; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 74 | import com.android.server.input.InputManagerService; |
Nicholas Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 75 | import com.android.server.pm.UserManagerService; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 76 | import com.android.server.policy.PermissionPolicyInternal; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 77 | import com.android.server.policy.WindowManagerPolicy; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 78 | import com.android.server.statusbar.StatusBarManagerInternal; |
| 79 | import com.android.server.uri.UriGrantsManagerInternal; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 80 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 81 | import org.junit.rules.TestRule; |
| 82 | import org.junit.runner.Description; |
| 83 | import org.junit.runners.model.Statement; |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 84 | import org.mockito.Mockito; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 85 | import org.mockito.quality.Strictness; |
| 86 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 87 | import java.io.File; |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 88 | import java.util.concurrent.atomic.AtomicBoolean; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 89 | |
| 90 | /** |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 91 | * JUnit test rule to correctly setting up system services like {@link WindowManagerService} |
| 92 | * and {@link ActivityTaskManagerService} for tests. |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 93 | */ |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 94 | public class SystemServicesTestRule implements TestRule { |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 95 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 96 | private static final String TAG = SystemServicesTestRule.class.getSimpleName(); |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 97 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 98 | static int sNextDisplayId = DEFAULT_DISPLAY + 100; |
| 99 | static int sNextTaskId = 100; |
| 100 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 101 | private final AtomicBoolean mCurrentMessagesProcessed = new AtomicBoolean(false); |
Nicholas Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 102 | private static final int[] TEST_USER_PROFILE_IDS = {}; |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 103 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 104 | private Context mContext; |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 105 | private StaticMockitoSession mMockitoSession; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 106 | private ActivityManagerService mAmService; |
| 107 | private ActivityTaskManagerService mAtmService; |
| 108 | private WindowManagerService mWmService; |
| 109 | private TestWindowManagerPolicy mWMPolicy; |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 110 | private WindowState.PowerManagerWrapper mPowerManagerWrapper; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 111 | private InputManagerService mImService; |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 112 | private InputChannel mInputChannel; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Spied {@link SurfaceControl.Transaction} class than can be used to verify calls. |
| 115 | */ |
| 116 | SurfaceControl.Transaction mTransaction; |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 117 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 118 | @Override |
| 119 | public Statement apply(Statement base, Description description) { |
| 120 | return new Statement() { |
| 121 | @Override |
| 122 | public void evaluate() throws Throwable { |
| 123 | try { |
| 124 | runWithDexmakerShareClassLoader(SystemServicesTestRule.this::setUp); |
| 125 | base.evaluate(); |
| 126 | } finally { |
| 127 | tearDown(); |
| 128 | } |
| 129 | } |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | private void setUp() { |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 134 | mMockitoSession = mockitoSession() |
| 135 | .spyStatic(LocalServices.class) |
| 136 | .mockStatic(LockGuard.class) |
| 137 | .mockStatic(Watchdog.class) |
| 138 | .strictness(Strictness.LENIENT) |
| 139 | .startMocking(); |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 140 | |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 141 | setUpSystemCore(); |
| 142 | setUpLocalServices(); |
| 143 | setUpActivityTaskManagerService(); |
| 144 | setUpWindowManagerService(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private void setUpSystemCore() { |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 148 | doReturn(mock(Watchdog.class)).when(Watchdog::getInstance); |
| 149 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 150 | mContext = getInstrumentation().getTargetContext(); |
| 151 | spyOn(mContext); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 152 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 153 | doReturn(null).when(mContext) |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 154 | .registerReceiver(nullable(BroadcastReceiver.class), any(IntentFilter.class)); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 155 | doReturn(null).when(mContext) |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 156 | .registerReceiverAsUser(any(BroadcastReceiver.class), any(UserHandle.class), |
| 157 | any(IntentFilter.class), nullable(String.class), nullable(Handler.class)); |
| 158 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 159 | final ContentResolver contentResolver = mContext.getContentResolver(); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 160 | spyOn(contentResolver); |
| 161 | doNothing().when(contentResolver) |
| 162 | .registerContentObserver(any(Uri.class), anyBoolean(), any(ContentObserver.class), |
| 163 | anyInt()); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 164 | } |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 165 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 166 | private void setUpLocalServices() { |
| 167 | // Tear down any local services just in case. |
| 168 | tearDownLocalServices(); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 169 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 170 | // UriGrantsManagerInternal |
| 171 | final UriGrantsManagerInternal ugmi = mock(UriGrantsManagerInternal.class); |
| 172 | LocalServices.addService(UriGrantsManagerInternal.class, ugmi); |
| 173 | |
| 174 | // AppOpsManager |
| 175 | final AppOpsManager aom = mock(AppOpsManager.class); |
| 176 | doReturn(aom).when(mContext).getSystemService(eq(Context.APP_OPS_SERVICE)); |
| 177 | |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 178 | // Prevent "WakeLock finalized while still held: SCREEN_FROZEN". |
| 179 | final PowerManager pm = mock(PowerManager.class); |
| 180 | doReturn(pm).when(mContext).getSystemService(eq(Context.POWER_SERVICE)); |
| 181 | doReturn(mock(PowerManager.WakeLock.class)).when(pm).newWakeLock(anyInt(), anyString()); |
| 182 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 183 | // DisplayManagerInternal |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 184 | final DisplayManagerInternal dmi = mock(DisplayManagerInternal.class); |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 185 | doReturn(dmi).when(() -> LocalServices.getService(eq(DisplayManagerInternal.class))); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 186 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 187 | // ColorDisplayServiceInternal |
| 188 | final ColorDisplayService.ColorDisplayServiceInternal cds = |
| 189 | mock(ColorDisplayService.ColorDisplayServiceInternal.class); |
| 190 | doReturn(cds).when(() -> LocalServices.getService( |
| 191 | eq(ColorDisplayService.ColorDisplayServiceInternal.class))); |
| 192 | |
| 193 | final UsageStatsManagerInternal usmi = mock(UsageStatsManagerInternal.class); |
| 194 | LocalServices.addService(UsageStatsManagerInternal.class, usmi); |
| 195 | |
| 196 | // PackageManagerInternal |
| 197 | final PackageManagerInternal packageManagerInternal = mock(PackageManagerInternal.class); |
| 198 | LocalServices.addService(PackageManagerInternal.class, packageManagerInternal); |
| 199 | doReturn(false).when(packageManagerInternal).isPermissionsReviewRequired( |
| 200 | anyString(), anyInt()); |
| 201 | doReturn(null).when(packageManagerInternal).getDefaultHomeActivity(anyInt()); |
| 202 | |
| 203 | // PowerManagerInternal |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 204 | final PowerManagerInternal pmi = mock(PowerManagerInternal.class); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 205 | final PowerSaveState state = new PowerSaveState.Builder().build(); |
| 206 | doReturn(state).when(pmi).getLowPowerState(anyInt()); |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 207 | doReturn(pmi).when(() -> LocalServices.getService(eq(PowerManagerInternal.class))); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 208 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 209 | // PermissionPolicyInternal |
| 210 | final PermissionPolicyInternal ppi = mock(PermissionPolicyInternal.class); |
| 211 | LocalServices.addService(PermissionPolicyInternal.class, ppi); |
| 212 | doReturn(true).when(ppi).checkStartActivity(any(), anyInt(), any()); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 213 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 214 | // InputManagerService |
| 215 | mImService = mock(InputManagerService.class); |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 216 | // InputChannel cannot be mocked because it may pass to InputEventReceiver. |
| 217 | final InputChannel[] inputChannels = InputChannel.openInputChannelPair(TAG); |
| 218 | inputChannels[0].dispose(); |
| 219 | mInputChannel = inputChannels[1]; |
| 220 | doReturn(mInputChannel).when(mImService).monitorInput(anyString(), anyInt()); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 221 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 222 | // StatusBarManagerInternal |
| 223 | final StatusBarManagerInternal sbmi = mock(StatusBarManagerInternal.class); |
| 224 | doReturn(sbmi).when(() -> LocalServices.getService(eq(StatusBarManagerInternal.class))); |
| 225 | } |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 226 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 227 | private void setUpActivityTaskManagerService() { |
| 228 | // ActivityManagerService |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 229 | mAmService = new ActivityManagerService(new AMTestInjector(mContext), null /* thread */); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 230 | spyOn(mAmService); |
| 231 | doReturn(mock(IPackageManager.class)).when(mAmService).getPackageManager(); |
Patrick Baumann | de37e43 | 2019-08-28 09:51:29 -0700 | [diff] [blame] | 232 | doNothing().when(mAmService).grantImplicitAccess( |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 233 | anyInt(), any(), anyInt(), anyInt()); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 234 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 235 | // ActivityManagerInternal |
| 236 | final ActivityManagerInternal amInternal = mAmService.mInternal; |
| 237 | spyOn(amInternal); |
| 238 | doNothing().when(amInternal).trimApplications(); |
| 239 | doNothing().when(amInternal).updateCpuStats(); |
| 240 | doNothing().when(amInternal).updateOomAdj(); |
| 241 | doNothing().when(amInternal).updateBatteryStats(any(), anyInt(), anyInt(), anyBoolean()); |
| 242 | doNothing().when(amInternal).updateActivityUsageStats( |
| 243 | any(), anyInt(), anyInt(), any(), any()); |
| 244 | doNothing().when(amInternal).startProcess( |
| 245 | any(), any(), anyBoolean(), anyBoolean(), any(), any()); |
| 246 | doNothing().when(amInternal).updateOomLevelsForDisplay(anyInt()); |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 247 | doNothing().when(amInternal).broadcastGlobalConfigurationChanged(anyInt(), anyBoolean()); |
| 248 | doNothing().when(amInternal).cleanUpServices(anyInt(), any(), any()); |
| 249 | doReturn(UserHandle.USER_SYSTEM).when(amInternal).getCurrentUserId(); |
| 250 | doReturn(TEST_USER_PROFILE_IDS).when(amInternal).getCurrentProfileIds(); |
| 251 | doReturn(true).when(amInternal).isCurrentProfile(anyInt()); |
| 252 | doReturn(true).when(amInternal).isUserRunning(anyInt(), anyInt()); |
| 253 | doReturn(true).when(amInternal).hasStartedUserState(anyInt()); |
| 254 | doReturn(false).when(amInternal).shouldConfirmCredentials(anyInt()); |
| 255 | doReturn(false).when(amInternal).isActivityStartsLoggingEnabled(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 256 | LocalServices.addService(ActivityManagerInternal.class, amInternal); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 257 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 258 | mAtmService = new TestActivityTaskManagerService(mContext, mAmService); |
| 259 | LocalServices.addService(ActivityTaskManagerInternal.class, mAtmService.getAtmInternal()); |
| 260 | } |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 261 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 262 | private void setUpWindowManagerService() { |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 263 | mPowerManagerWrapper = mock(WindowState.PowerManagerWrapper.class); |
| 264 | mWMPolicy = new TestWindowManagerPolicy(this::getWindowManagerService, |
| 265 | mPowerManagerWrapper); |
Riddle Hsu | 2da2d03 | 2019-08-28 21:08:58 +0800 | [diff] [blame] | 266 | // Suppress StrictMode violation (DisplayWindowSettings) to avoid log flood. |
| 267 | DisplayThread.getHandler().post(StrictMode::allowThreadDiskWritesMask); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 268 | mWmService = WindowManagerService.main( |
Vishnu Nair | 3319739 | 2019-08-30 10:29:37 -0700 | [diff] [blame] | 269 | mContext, mImService, false, false, mWMPolicy, mAtmService, StubTransaction::new, |
| 270 | () -> mock(Surface.class), (unused) -> new MockSurfaceControlBuilder()); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 271 | spyOn(mWmService); |
| 272 | |
| 273 | // Setup factory classes to prevent calls to native code. |
| 274 | mTransaction = spy(StubTransaction.class); |
| 275 | // Return a spied Transaction class than can be used to verify calls. |
| 276 | mWmService.mTransactionFactory = () -> mTransaction; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 277 | mWmService.mSurfaceAnimationRunner = new SurfaceAnimationRunner( |
| 278 | null, null, mTransaction, mWmService.mPowerManagerInternal); |
| 279 | |
| 280 | mWmService.onInitReady(); |
| 281 | mAmService.setWindowManager(mWmService); |
| 282 | mWmService.mDisplayEnabled = true; |
| 283 | mWmService.mDisplayReady = true; |
| 284 | // Set configuration for default display |
| 285 | mWmService.getDefaultDisplayContentLocked().reconfigureDisplayLocked(); |
| 286 | |
| 287 | // Mock root, some default display, and home stack. |
| 288 | spyOn(mWmService.mRoot); |
| 289 | final ActivityDisplay display = mAtmService.mRootActivityContainer.getDefaultDisplay(); |
| 290 | spyOn(display); |
| 291 | spyOn(display.mDisplayContent); |
| 292 | final ActivityStack homeStack = display.getStack( |
| 293 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME); |
| 294 | spyOn(homeStack); |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | private void tearDown() { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 298 | // Unregister display listener from root to avoid issues with subsequent tests. |
| 299 | mContext.getSystemService(DisplayManager.class) |
| 300 | .unregisterDisplayListener(mAtmService.mRootActivityContainer); |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 301 | // The constructor of WindowManagerService registers WindowManagerConstants and |
| 302 | // HighRefreshRateBlacklist with DeviceConfig. We need to undo that here to avoid |
| 303 | // leaking mWmService. |
| 304 | mWmService.mConstants.dispose(); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 305 | mWmService.mHighRefreshRateBlacklist.dispose(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 306 | |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 307 | waitUntilWindowManagerHandlersIdle(); |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 308 | // Needs to explicitly dispose current static threads because there could be messages |
| 309 | // scheduled at a later time, and all mocks are invalid when it's executed. |
| 310 | DisplayThread.dispose(); |
| 311 | AnimationThread.dispose(); |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 312 | UiThread.dispose(); |
Vadim Caen | e654aa4 | 2019-11-11 12:58:22 +0100 | [diff] [blame] | 313 | SurfaceAnimationThread.dispose(); |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 314 | mInputChannel.dispose(); |
| 315 | |
| 316 | tearDownLocalServices(); |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 317 | // Reset priority booster because animation thread has been changed. |
| 318 | WindowManagerService.sThreadPriorityBooster = new WindowManagerThreadPriorityBooster(); |
| 319 | |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 320 | mMockitoSession.finishMocking(); |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 321 | Mockito.framework().clearInlineMocks(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 324 | private static void tearDownLocalServices() { |
| 325 | LocalServices.removeServiceForTest(DisplayManagerInternal.class); |
| 326 | LocalServices.removeServiceForTest(PowerManagerInternal.class); |
| 327 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 328 | LocalServices.removeServiceForTest(ActivityTaskManagerInternal.class); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 329 | LocalServices.removeServiceForTest(WindowManagerInternal.class); |
| 330 | LocalServices.removeServiceForTest(WindowManagerPolicy.class); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 331 | LocalServices.removeServiceForTest(PackageManagerInternal.class); |
| 332 | LocalServices.removeServiceForTest(UriGrantsManagerInternal.class); |
| 333 | LocalServices.removeServiceForTest(PermissionPolicyInternal.class); |
| 334 | LocalServices.removeServiceForTest(ColorDisplayService.ColorDisplayServiceInternal.class); |
| 335 | LocalServices.removeServiceForTest(UsageStatsManagerInternal.class); |
| 336 | LocalServices.removeServiceForTest(StatusBarManagerInternal.class); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 337 | } |
| 338 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 339 | WindowManagerService getWindowManagerService() { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 340 | return mWmService; |
| 341 | } |
| 342 | |
| 343 | ActivityTaskManagerService getActivityTaskManagerService() { |
| 344 | return mAtmService; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 345 | } |
| 346 | |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 347 | WindowState.PowerManagerWrapper getPowerManagerWrapper() { |
| 348 | return mPowerManagerWrapper; |
| 349 | } |
| 350 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 351 | void cleanupWindowManagerHandlers() { |
Tadashi G. Takaoka | a5f7c2c | 2019-01-09 12:17:14 +0900 | [diff] [blame] | 352 | final WindowManagerService wm = getWindowManagerService(); |
| 353 | if (wm == null) { |
| 354 | return; |
| 355 | } |
| 356 | wm.mH.removeCallbacksAndMessages(null); |
| 357 | wm.mAnimationHandler.removeCallbacksAndMessages(null); |
| 358 | SurfaceAnimationThread.getHandler().removeCallbacksAndMessages(null); |
| 359 | } |
| 360 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 361 | void waitUntilWindowManagerHandlersIdle() { |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 362 | final WindowManagerService wm = getWindowManagerService(); |
| 363 | if (wm == null) { |
| 364 | return; |
| 365 | } |
| 366 | // Removing delayed FORCE_GC message decreases time for waiting idle. |
| 367 | wm.mH.removeMessages(WindowManagerService.H.FORCE_GC); |
| 368 | waitHandlerIdle(wm.mH); |
| 369 | waitHandlerIdle(wm.mAnimationHandler); |
| 370 | waitHandlerIdle(SurfaceAnimationThread.getHandler()); |
| 371 | } |
| 372 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 373 | private void waitHandlerIdle(Handler handler) { |
| 374 | synchronized (mCurrentMessagesProcessed) { |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 375 | // Add a message to the handler queue and make sure it is fully processed before we move |
| 376 | // on. This makes sure all previous messages in the handler are fully processed vs. just |
| 377 | // popping them from the message queue. |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 378 | mCurrentMessagesProcessed.set(false); |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 379 | handler.post(() -> { |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 380 | synchronized (mCurrentMessagesProcessed) { |
| 381 | mCurrentMessagesProcessed.set(true); |
| 382 | mCurrentMessagesProcessed.notifyAll(); |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 383 | } |
| 384 | }); |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 385 | while (!mCurrentMessagesProcessed.get()) { |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 386 | try { |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 387 | mCurrentMessagesProcessed.wait(); |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 388 | } catch (InterruptedException e) { |
| 389 | } |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 390 | } |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 391 | } |
| 392 | } |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 393 | |
| 394 | protected class TestActivityTaskManagerService extends ActivityTaskManagerService { |
| 395 | // ActivityStackSupervisor may be created more than once while setting up AMS and ATMS. |
| 396 | // We keep the reference in order to prevent creating it twice. |
| 397 | ActivityStackSupervisor mTestStackSupervisor; |
| 398 | |
| 399 | TestActivityTaskManagerService(Context context, ActivityManagerService ams) { |
| 400 | super(context); |
| 401 | spyOn(this); |
| 402 | |
| 403 | mSupportsMultiWindow = true; |
| 404 | mSupportsMultiDisplay = true; |
| 405 | mSupportsSplitScreenMultiWindow = true; |
| 406 | mSupportsFreeformWindowManagement = true; |
| 407 | mSupportsPictureInPicture = true; |
| 408 | |
| 409 | doReturn(mock(IPackageManager.class)).when(this).getPackageManager(); |
| 410 | // allow background activity starts by default |
| 411 | doReturn(true).when(this).isBackgroundActivityStartsEnabled(); |
| 412 | doNothing().when(this).updateCpuStats(); |
| 413 | |
| 414 | // AppOpsService |
| 415 | final AppOpsService aos = mock(AppOpsService.class); |
| 416 | doReturn(aos).when(this).getAppOpsService(); |
| 417 | // Make sure permission checks aren't overridden. |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 418 | doReturn(AppOpsManager.MODE_DEFAULT).when(aos).noteOperation(anyInt(), anyInt(), |
| 419 | anyString(), nullable(String.class)); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 420 | |
Nicholas Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 421 | // UserManagerService |
| 422 | final UserManagerService ums = mock(UserManagerService.class); |
| 423 | doReturn(ums).when(this).getUserManager(); |
| 424 | doReturn(TEST_USER_PROFILE_IDS).when(ums).getProfileIds(anyInt(), eq(true)); |
| 425 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 426 | setUsageStatsManager(LocalServices.getService(UsageStatsManagerInternal.class)); |
| 427 | ams.mActivityTaskManager = this; |
| 428 | ams.mAtmInternal = mInternal; |
| 429 | onActivityManagerInternalAdded(); |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 430 | |
| 431 | final IntentFirewall intentFirewall = mock(IntentFirewall.class); |
| 432 | doReturn(true).when(intentFirewall).checkStartActivity( |
| 433 | any(), anyInt(), anyInt(), nullable(String.class), any()); |
| 434 | initialize(intentFirewall, null /* intentController */, |
| 435 | DisplayThread.getHandler().getLooper()); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 436 | spyOn(getLifecycleManager()); |
| 437 | spyOn(getLockTaskController()); |
| 438 | spyOn(getTaskChangeNotificationController()); |
| 439 | initRootActivityContainerMocks(); |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 440 | |
| 441 | AppWarnings appWarnings = getAppWarningsLocked(); |
| 442 | spyOn(appWarnings); |
| 443 | doNothing().when(appWarnings).onStartActivity(any()); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void initRootActivityContainerMocks() { |
| 447 | spyOn(mRootActivityContainer); |
| 448 | // Invoked during {@link ActivityStack} creation. |
| 449 | doNothing().when(mRootActivityContainer).updateUIDsPresentOnDisplay(); |
| 450 | // Always keep things awake. |
| 451 | doReturn(true).when(mRootActivityContainer).hasAwakeDisplay(); |
| 452 | // Called when moving activity to pinned stack. |
| 453 | doNothing().when(mRootActivityContainer).ensureActivitiesVisible(any(), anyInt(), |
| 454 | anyBoolean()); |
| 455 | } |
| 456 | |
| 457 | @Override |
| 458 | int handleIncomingUser(int callingPid, int callingUid, int userId, String name) { |
| 459 | return userId; |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | protected ActivityStackSupervisor createStackSupervisor() { |
| 464 | if (mTestStackSupervisor == null) { |
| 465 | mTestStackSupervisor = new TestActivityStackSupervisor(this, mH.getLooper()); |
| 466 | } |
| 467 | return mTestStackSupervisor; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on |
| 473 | * setup not available in the test environment. Also specifies an injector for |
| 474 | */ |
| 475 | protected class TestActivityStackSupervisor extends ActivityStackSupervisor { |
| 476 | |
| 477 | TestActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) { |
| 478 | super(service, looper); |
| 479 | spyOn(this); |
| 480 | |
| 481 | // Do not schedule idle that may touch methods outside the scope of the test. |
| 482 | doNothing().when(this).scheduleIdleLocked(); |
| 483 | doNothing().when(this).scheduleIdleTimeoutLocked(any()); |
| 484 | // unit test version does not handle launch wake lock |
| 485 | doNothing().when(this).acquireLaunchWakelock(); |
| 486 | doReturn(mock(KeyguardController.class)).when(this).getKeyguardController(); |
| 487 | |
| 488 | mLaunchingActivityWakeLock = mock(PowerManager.WakeLock.class); |
| 489 | |
| 490 | initialize(); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // TODO: Can we just mock this? |
| 495 | private static class AMTestInjector extends ActivityManagerService.Injector { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 496 | |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 497 | AMTestInjector(Context context) { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 498 | super(context); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | @Override |
| 502 | public Context getContext() { |
| 503 | return getInstrumentation().getTargetContext(); |
| 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public AppOpsService getAppOpsService(File file, Handler handler) { |
| 508 | return null; |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public Handler getUiHandler(ActivityManagerService service) { |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 513 | return UiThread.getHandler(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public boolean isNetworkRestrictedForUid(int uid) { |
| 518 | return false; |
| 519 | } |
| 520 | } |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 521 | } |