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); |
Louis Chang | 3ff72a8 | 2019-12-17 12:12:59 +0800 | [diff] [blame] | 272 | spyOn(mWmService.mRoot); |
| 273 | // Invoked during {@link ActivityStack} creation. |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 274 | doNothing().when(mWmService.mRoot).updateUIDsPresentOnDisplay(); |
Louis Chang | 3ff72a8 | 2019-12-17 12:12:59 +0800 | [diff] [blame] | 275 | // Always keep things awake. |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 276 | doReturn(true).when(mWmService.mRoot).hasAwakeDisplay(); |
Louis Chang | 3ff72a8 | 2019-12-17 12:12:59 +0800 | [diff] [blame] | 277 | // Called when moving activity to pinned stack. |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 278 | doNothing().when(mWmService.mRoot).ensureActivitiesVisible(any(), |
Louis Chang | 3ff72a8 | 2019-12-17 12:12:59 +0800 | [diff] [blame] | 279 | anyInt(), anyBoolean(), anyBoolean()); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 280 | |
| 281 | // Setup factory classes to prevent calls to native code. |
| 282 | mTransaction = spy(StubTransaction.class); |
| 283 | // Return a spied Transaction class than can be used to verify calls. |
| 284 | mWmService.mTransactionFactory = () -> mTransaction; |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 285 | mWmService.mSurfaceAnimationRunner = new SurfaceAnimationRunner( |
| 286 | null, null, mTransaction, mWmService.mPowerManagerInternal); |
| 287 | |
| 288 | mWmService.onInitReady(); |
| 289 | mAmService.setWindowManager(mWmService); |
| 290 | mWmService.mDisplayEnabled = true; |
| 291 | mWmService.mDisplayReady = true; |
| 292 | // Set configuration for default display |
| 293 | mWmService.getDefaultDisplayContentLocked().reconfigureDisplayLocked(); |
| 294 | |
Louis Chang | 3ff72a8 | 2019-12-17 12:12:59 +0800 | [diff] [blame] | 295 | // Mock default display, and home stack. |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 296 | final DisplayContent display = mAtmService.mRootWindowContainer.getDefaultDisplay(); |
Garfield Tan | 4042852 | 2019-12-06 16:17:44 -0800 | [diff] [blame] | 297 | // Set default display to be in fullscreen mode. Devices with PC feature may start their |
| 298 | // default display in freeform mode but some of tests in WmTests have implicit assumption on |
| 299 | // that the default display is in fullscreen mode. |
| 300 | display.setDisplayWindowingMode(WINDOWING_MODE_FULLSCREEN); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 301 | spyOn(display); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 302 | final ActivityStack homeStack = display.getStack( |
| 303 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME); |
| 304 | spyOn(homeStack); |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | private void tearDown() { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 308 | // Unregister display listener from root to avoid issues with subsequent tests. |
| 309 | mContext.getSystemService(DisplayManager.class) |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 310 | .unregisterDisplayListener(mAtmService.mRootWindowContainer); |
Adrian Roos | 1c2e9a1 | 2019-08-20 18:23:47 +0200 | [diff] [blame] | 311 | // The constructor of WindowManagerService registers WindowManagerConstants and |
| 312 | // HighRefreshRateBlacklist with DeviceConfig. We need to undo that here to avoid |
| 313 | // leaking mWmService. |
| 314 | mWmService.mConstants.dispose(); |
Charles Chen | b9f9e64d | 2019-11-14 17:13:21 +0800 | [diff] [blame] | 315 | mWmService.mHighRefreshRateBlacklist.dispose(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 316 | |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 317 | waitUntilWindowManagerHandlersIdle(); |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 318 | // Needs to explicitly dispose current static threads because there could be messages |
| 319 | // scheduled at a later time, and all mocks are invalid when it's executed. |
| 320 | DisplayThread.dispose(); |
Louis Chang | 3ff72a8 | 2019-12-17 12:12:59 +0800 | [diff] [blame] | 321 | // Dispose SurfaceAnimationThread before AnimationThread does, so it won't create a new |
| 322 | // AnimationThread after AnimationThread disposed, see {@link |
| 323 | // AnimatorListenerAdapter#onAnimationEnd()} |
| 324 | SurfaceAnimationThread.dispose(); |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 325 | AnimationThread.dispose(); |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 326 | UiThread.dispose(); |
| 327 | mInputChannel.dispose(); |
| 328 | |
| 329 | tearDownLocalServices(); |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 330 | // Reset priority booster because animation thread has been changed. |
| 331 | WindowManagerService.sThreadPriorityBooster = new WindowManagerThreadPriorityBooster(); |
| 332 | |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 333 | mMockitoSession.finishMocking(); |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 334 | Mockito.framework().clearInlineMocks(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 337 | private static void tearDownLocalServices() { |
| 338 | LocalServices.removeServiceForTest(DisplayManagerInternal.class); |
| 339 | LocalServices.removeServiceForTest(PowerManagerInternal.class); |
| 340 | LocalServices.removeServiceForTest(ActivityManagerInternal.class); |
| 341 | LocalServices.removeServiceForTest(ActivityTaskManagerInternal.class); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 342 | LocalServices.removeServiceForTest(WindowManagerInternal.class); |
| 343 | LocalServices.removeServiceForTest(WindowManagerPolicy.class); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 344 | LocalServices.removeServiceForTest(PackageManagerInternal.class); |
| 345 | LocalServices.removeServiceForTest(UriGrantsManagerInternal.class); |
| 346 | LocalServices.removeServiceForTest(PermissionPolicyInternal.class); |
| 347 | LocalServices.removeServiceForTest(ColorDisplayService.ColorDisplayServiceInternal.class); |
| 348 | LocalServices.removeServiceForTest(UsageStatsManagerInternal.class); |
| 349 | LocalServices.removeServiceForTest(StatusBarManagerInternal.class); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 350 | } |
| 351 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 352 | WindowManagerService getWindowManagerService() { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 353 | return mWmService; |
| 354 | } |
| 355 | |
| 356 | ActivityTaskManagerService getActivityTaskManagerService() { |
| 357 | return mAtmService; |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 358 | } |
| 359 | |
Garfield Tan | e3d37b5 | 2019-07-23 12:43:05 -0700 | [diff] [blame] | 360 | WindowState.PowerManagerWrapper getPowerManagerWrapper() { |
| 361 | return mPowerManagerWrapper; |
| 362 | } |
| 363 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 364 | void cleanupWindowManagerHandlers() { |
Tadashi G. Takaoka | a5f7c2c | 2019-01-09 12:17:14 +0900 | [diff] [blame] | 365 | final WindowManagerService wm = getWindowManagerService(); |
| 366 | if (wm == null) { |
| 367 | return; |
| 368 | } |
| 369 | wm.mH.removeCallbacksAndMessages(null); |
| 370 | wm.mAnimationHandler.removeCallbacksAndMessages(null); |
Wale Ogunwale | c9dfbc5 | 2019-11-21 07:19:07 -0800 | [diff] [blame] | 371 | // This is a different handler object than the wm.mAnimationHandler above. |
| 372 | AnimationThread.getHandler().removeCallbacksAndMessages(null); |
Tadashi G. Takaoka | a5f7c2c | 2019-01-09 12:17:14 +0900 | [diff] [blame] | 373 | SurfaceAnimationThread.getHandler().removeCallbacksAndMessages(null); |
| 374 | } |
| 375 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 376 | void waitUntilWindowManagerHandlersIdle() { |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 377 | final WindowManagerService wm = getWindowManagerService(); |
| 378 | if (wm == null) { |
| 379 | return; |
| 380 | } |
| 381 | // Removing delayed FORCE_GC message decreases time for waiting idle. |
| 382 | wm.mH.removeMessages(WindowManagerService.H.FORCE_GC); |
| 383 | waitHandlerIdle(wm.mH); |
| 384 | waitHandlerIdle(wm.mAnimationHandler); |
Wale Ogunwale | c9dfbc5 | 2019-11-21 07:19:07 -0800 | [diff] [blame] | 385 | // This is a different handler object than the wm.mAnimationHandler above. |
| 386 | waitHandlerIdle(AnimationThread.getHandler()); |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 387 | waitHandlerIdle(SurfaceAnimationThread.getHandler()); |
| 388 | } |
| 389 | |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 390 | private void waitHandlerIdle(Handler handler) { |
| 391 | synchronized (mCurrentMessagesProcessed) { |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 392 | // Add a message to the handler queue and make sure it is fully processed before we move |
| 393 | // on. This makes sure all previous messages in the handler are fully processed vs. just |
| 394 | // popping them from the message queue. |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 395 | mCurrentMessagesProcessed.set(false); |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 396 | handler.post(() -> { |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 397 | synchronized (mCurrentMessagesProcessed) { |
| 398 | mCurrentMessagesProcessed.set(true); |
| 399 | mCurrentMessagesProcessed.notifyAll(); |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 400 | } |
| 401 | }); |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 402 | while (!mCurrentMessagesProcessed.get()) { |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 403 | try { |
Tadashi G. Takaoka | d7aa79a | 2019-02-08 17:42:37 +0900 | [diff] [blame] | 404 | mCurrentMessagesProcessed.wait(); |
Yunfan Chen | dcb1fcf | 2019-02-07 19:08:41 +0900 | [diff] [blame] | 405 | } catch (InterruptedException e) { |
| 406 | } |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 407 | } |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 408 | } |
| 409 | } |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 410 | |
| 411 | protected class TestActivityTaskManagerService extends ActivityTaskManagerService { |
| 412 | // ActivityStackSupervisor may be created more than once while setting up AMS and ATMS. |
| 413 | // We keep the reference in order to prevent creating it twice. |
| 414 | ActivityStackSupervisor mTestStackSupervisor; |
| 415 | |
| 416 | TestActivityTaskManagerService(Context context, ActivityManagerService ams) { |
| 417 | super(context); |
| 418 | spyOn(this); |
| 419 | |
| 420 | mSupportsMultiWindow = true; |
| 421 | mSupportsMultiDisplay = true; |
| 422 | mSupportsSplitScreenMultiWindow = true; |
| 423 | mSupportsFreeformWindowManagement = true; |
| 424 | mSupportsPictureInPicture = true; |
| 425 | |
| 426 | doReturn(mock(IPackageManager.class)).when(this).getPackageManager(); |
| 427 | // allow background activity starts by default |
| 428 | doReturn(true).when(this).isBackgroundActivityStartsEnabled(); |
| 429 | doNothing().when(this).updateCpuStats(); |
| 430 | |
| 431 | // AppOpsService |
| 432 | final AppOpsService aos = mock(AppOpsService.class); |
| 433 | doReturn(aos).when(this).getAppOpsService(); |
| 434 | // Make sure permission checks aren't overridden. |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 435 | doReturn(AppOpsManager.MODE_DEFAULT).when(aos).noteOperation(anyInt(), anyInt(), |
Philip P. Moltmann | da554e4 | 2019-12-20 11:21:02 -0800 | [diff] [blame] | 436 | anyString(), nullable(String.class), anyBoolean(), nullable(String.class)); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 437 | |
Nicholas Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 438 | // UserManagerService |
| 439 | final UserManagerService ums = mock(UserManagerService.class); |
| 440 | doReturn(ums).when(this).getUserManager(); |
| 441 | doReturn(TEST_USER_PROFILE_IDS).when(ums).getProfileIds(anyInt(), eq(true)); |
| 442 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 443 | setUsageStatsManager(LocalServices.getService(UsageStatsManagerInternal.class)); |
| 444 | ams.mActivityTaskManager = this; |
| 445 | ams.mAtmInternal = mInternal; |
| 446 | onActivityManagerInternalAdded(); |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 447 | |
| 448 | final IntentFirewall intentFirewall = mock(IntentFirewall.class); |
| 449 | doReturn(true).when(intentFirewall).checkStartActivity( |
| 450 | any(), anyInt(), anyInt(), nullable(String.class), any()); |
| 451 | initialize(intentFirewall, null /* intentController */, |
| 452 | DisplayThread.getHandler().getLooper()); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 453 | spyOn(getLifecycleManager()); |
| 454 | spyOn(getLockTaskController()); |
| 455 | spyOn(getTaskChangeNotificationController()); |
Wale Ogunwale | 2322bed | 2019-10-10 17:24:19 +0200 | [diff] [blame] | 456 | |
| 457 | AppWarnings appWarnings = getAppWarningsLocked(); |
| 458 | spyOn(appWarnings); |
| 459 | doNothing().when(appWarnings).onStartActivity(any()); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 462 | @Override |
| 463 | int handleIncomingUser(int callingPid, int callingUid, int userId, String name) { |
| 464 | return userId; |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | protected ActivityStackSupervisor createStackSupervisor() { |
| 469 | if (mTestStackSupervisor == null) { |
| 470 | mTestStackSupervisor = new TestActivityStackSupervisor(this, mH.getLooper()); |
| 471 | } |
| 472 | return mTestStackSupervisor; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on |
| 478 | * setup not available in the test environment. Also specifies an injector for |
| 479 | */ |
| 480 | protected class TestActivityStackSupervisor extends ActivityStackSupervisor { |
| 481 | |
| 482 | TestActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) { |
| 483 | super(service, looper); |
| 484 | spyOn(this); |
| 485 | |
| 486 | // Do not schedule idle that may touch methods outside the scope of the test. |
| 487 | doNothing().when(this).scheduleIdleLocked(); |
| 488 | doNothing().when(this).scheduleIdleTimeoutLocked(any()); |
| 489 | // unit test version does not handle launch wake lock |
| 490 | doNothing().when(this).acquireLaunchWakelock(); |
| 491 | doReturn(mock(KeyguardController.class)).when(this).getKeyguardController(); |
| 492 | |
| 493 | mLaunchingActivityWakeLock = mock(PowerManager.WakeLock.class); |
| 494 | |
| 495 | initialize(); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // TODO: Can we just mock this? |
| 500 | private static class AMTestInjector extends ActivityManagerService.Injector { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 501 | |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 502 | AMTestInjector(Context context) { |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 503 | super(context); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public Context getContext() { |
| 508 | return getInstrumentation().getTargetContext(); |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public AppOpsService getAppOpsService(File file, Handler handler) { |
| 513 | return null; |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public Handler getUiHandler(ActivityManagerService service) { |
Riddle Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 518 | return UiThread.getHandler(); |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public boolean isNetworkRestrictedForUid(int uid) { |
| 523 | return false; |
| 524 | } |
| 525 | } |
Tadashi G. Takaoka | 5a108b8 | 2018-12-13 17:09:12 +0900 | [diff] [blame] | 526 | } |