From 6129cfa7aff92e8d76f1f36f6e272d388a32f863 Mon Sep 17 00:00:00 2001 From: Jan Tomljanovic Date: Wed, 9 Dec 2020 20:17:42 +0000 Subject: [PATCH] Fix the bug where inner lock in MultiRateLimiter is null. The bug would occur when Mockito stopped mocking methods when the test is done, but the mocked object still exists, and is then accessed as a scheduled message triggers it. Test: atest NotificationManagerServiceTest Bug: 174997218 Change-Id: I3c76456927bb0c27945b137187011b5eb3c7102c --- .../NotificationManagerServiceTest.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index 849477c924bbc..dcb6ee702ecbb 100755 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -305,6 +305,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { @Mock StatusBarManagerInternal mStatusBar; + private NotificationManagerService.WorkerHandler mWorkerHandler; + // Use a Testable subclass so we can simulate calls from the system without failing. private static class TestableNotificationManagerService extends NotificationManagerService { int countSystemChecks = 0; @@ -482,14 +484,13 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { when(mAssistants.isAdjustmentAllowed(anyString())).thenReturn(true); - mService.init(mService.new WorkerHandler(mTestableLooper.getLooper()), - mRankingHandler, mPackageManager, mPackageManagerClient, mockLightsManager, - mListeners, mAssistants, mConditionProviders, - mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, - mGroupHelper, mAm, mAtm, mAppUsageStats, - mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal, - mAppOpsManager, mUm, mHistoryManager, mStatsManager, - mock(TelephonyManager.class), mAmi, mToastRateLimiter); + mWorkerHandler = mService.new WorkerHandler(mTestableLooper.getLooper()); + mService.init(mWorkerHandler, mRankingHandler, mPackageManager, mPackageManagerClient, + mockLightsManager, mListeners, mAssistants, mConditionProviders, mCompanionMgr, + mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager, mGroupHelper, mAm, mAtm, + mAppUsageStats, mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal, + mAppOpsManager, mUm, mHistoryManager, mStatsManager, mock(TelephonyManager.class), + mAmi, mToastRateLimiter); mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); mService.setAudioManager(mAudioManager); @@ -575,6 +576,10 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { InstrumentationRegistry.getInstrumentation() .getUiAutomation().dropShellPermissionIdentity(); + // Remove scheduled messages that would be processed when the test is already done, and + // could cause issues, for example, messages that remove/cancel shown toasts (this causes + // problematic interactions with mocks when they're no longer working as expected). + mWorkerHandler.removeCallbacksAndMessages(null); } private void simulatePackageSuspendBroadcast(boolean suspend, String pkg,