Merge "Fix NPE in WakeLock#finalize for unit test" am: a29bc7bbd8

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2005611

Change-Id: I1cfbc86ff704c155cfeef3cf9d2852f7cb52dbd3
This commit is contained in:
Riddle Hsu
2022-03-04 05:57:27 +00:00
committed by Automerger Merge Worker
2 changed files with 19 additions and 3 deletions

View File

@@ -287,7 +287,8 @@ public class ActivityTaskManagerServiceTests extends WindowTestsBase {
@Test
public void testUpdateSleep() {
doCallRealMethod().when(mWm.mRoot).hasAwakeDisplay();
mSupervisor.mGoingToSleepWakeLock = mock(PowerManager.WakeLock.class);
mSupervisor.mGoingToSleepWakeLock =
mSystemServicesTestRule.createStubbedWakeLock(true /* needVerification */);
final Task rootHomeTask = mWm.mRoot.getDefaultTaskDisplayArea().getOrCreateRootHomeTask();
final ActivityRecord homeActivity = new ActivityBuilder(mAtm).setTask(rootHomeTask).build();
final ActivityRecord topActivity = new ActivityBuilder(mAtm).setCreateTask(true).build();

View File

@@ -102,6 +102,10 @@ public class SystemServicesTestRule implements TestRule {
static int sNextTaskId = 100;
private static final int[] TEST_USER_PROFILE_IDS = {};
/** Use a real static object so there won't be NPE in finalize() after clearInlineMocks(). */
private static final PowerManager.WakeLock sWakeLock = getInstrumentation().getContext()
.getSystemService(PowerManager.class).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
private PowerManager.WakeLock mStubbedWakeLock;
private Description mDescription;
private Context mContext;
@@ -196,7 +200,8 @@ public class SystemServicesTestRule implements TestRule {
// Prevent "WakeLock finalized while still held: SCREEN_FROZEN".
final PowerManager pm = mock(PowerManager.class);
doReturn(pm).when(mContext).getSystemService(eq(Context.POWER_SERVICE));
doReturn(mock(PowerManager.WakeLock.class)).when(pm).newWakeLock(anyInt(), anyString());
mStubbedWakeLock = createStubbedWakeLock(false /* needVerification */);
doReturn(mStubbedWakeLock).when(pm).newWakeLock(anyInt(), anyString());
// DisplayManagerInternal
final DisplayManagerInternal dmi = mock(DisplayManagerInternal.class);
@@ -402,6 +407,16 @@ public class SystemServicesTestRule implements TestRule {
return mPowerManagerWrapper;
}
/** Creates a no-op wakelock object. */
PowerManager.WakeLock createStubbedWakeLock(boolean needVerification) {
if (needVerification) {
return mock(PowerManager.WakeLock.class, Mockito.withSettings()
.spiedInstance(sWakeLock).defaultAnswer(Mockito.RETURNS_DEFAULTS));
}
return mock(PowerManager.WakeLock.class, Mockito.withSettings()
.spiedInstance(sWakeLock).stubOnly());
}
void setSurfaceFactory(Supplier<Surface> factory) {
mSurfaceFactory = factory;
}
@@ -557,7 +572,7 @@ public class SystemServicesTestRule implements TestRule {
// unit test version does not handle launch wake lock
doNothing().when(this).acquireLaunchWakelock();
mLaunchingActivityWakeLock = mock(PowerManager.WakeLock.class);
mLaunchingActivityWakeLock = mStubbedWakeLock;
initialize();