From 7e1bed5a2884241e97300c41f3715d2f9c9ced42 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Tue, 4 Jan 2022 18:10:59 -0500 Subject: [PATCH] Clear mocks in tests Inline mocks were leaking and the accumulation of all of them throughout all our test led to OOM. In order to prevent memory leak (https://github.com/mockito/mockito/issues/1614), use suggested fix in tests (https://github.com/mockito/mockito/wiki/What's-new-in-Mockito-2#mockito-2250). This fix uncovered a handful of leaked Handlers. Mitigated by removing cleaning up on teardown, but they probably should be injected in the future so we can directly use a TestableLooper (or an executor). Test: atest SystemUITests Fixes: 212492048 Change-Id: I108be93088d56863aa7dae5482229b74133d3105 --- .../src/com/android/systemui/SysuiTestCase.java | 8 +++++++- .../statusbar/AlertingNotificationManagerTest.java | 7 +++++++ .../logging/NotificationLoggerLegacyTest.java | 7 +++++++ .../notification/logging/NotificationLoggerTest.java | 7 +++++++ .../notification/row/NotificationTestHelper.java | 3 +++ .../statusbar/phone/HeadsUpManagerPhoneTest.java | 7 +++++++ .../NotificationGroupAlertTransferHelperTest.java | 6 ++++++ .../phone/NotificationPanelViewControllerTest.java | 12 +++++++++++- .../statusbar/policy/HeadsUpManagerTest.java | 7 +++++++ 9 files changed, 62 insertions(+), 2 deletions(-) diff --git a/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java b/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java index 4f3266d0db441..40632a85d7229 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java +++ b/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java @@ -44,8 +44,10 @@ import com.android.systemui.settings.UserTracker; import com.android.systemui.statusbar.SmartReplyController; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; import org.junit.Rule; +import org.mockito.Mockito; import java.io.FileInputStream; import java.io.IOException; @@ -120,11 +122,15 @@ public abstract class SysuiTestCase { TestableLooper.get(this).processAllMessages(); } disallowTestableLooperAsMainThread(); - SystemUIFactory.cleanup(); mContext.cleanUpReceivers(this.getClass().getSimpleName()); mFakeBroadcastDispatcher.cleanUpReceivers(this.getClass().getSimpleName()); } + @AfterClass + public static void mockitoTearDown() { + Mockito.framework().clearInlineMocks(); + } + /** * Tests are run on the TestableLooper; however, there are parts of SystemUI that assert that * the code is run from the main looper. Therefore, we allow the TestableLooper to pass these diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java index 9bf877584679e..8a388479c0e71 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/AlertingNotificationManagerTest.java @@ -45,6 +45,7 @@ import com.android.systemui.statusbar.notification.collection.NotificationEntryB import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; import com.android.systemui.statusbar.policy.HeadsUpManagerLogger; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -86,6 +87,7 @@ public class AlertingNotificationManagerTest extends SysuiTestCase { super(mock(HeadsUpManagerLogger.class)); mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME; mAutoDismissNotificationDecay = TEST_AUTO_DISMISS_TIME; + mHandler.removeCallbacksAndMessages(null); mHandler = mTestHandler; } @@ -145,6 +147,11 @@ public class AlertingNotificationManagerTest extends SysuiTestCase { mAlertingNotificationManager = createAlertingNotificationManager(); } + @After + public void tearDown() { + mTestHandler.removeCallbacksAndMessages(null); + } + @Test public void testShowNotification_addsEntry() { mAlertingNotificationManager.showNotification(mEntry); diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/logging/NotificationLoggerLegacyTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/logging/NotificationLoggerLegacyTest.java index 2dfb9fc674ba7..429d2ed36cc7d 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/logging/NotificationLoggerLegacyTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/logging/NotificationLoggerLegacyTest.java @@ -60,6 +60,7 @@ import com.android.systemui.util.time.FakeSystemClock; import com.google.android.collect.Lists; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -131,6 +132,11 @@ public class NotificationLoggerLegacyTest extends SysuiTestCase { verify(mNotifPipeline, never()).addCollectionListener(any()); } + @After + public void tearDown() { + mLogger.mHandler.removeCallbacksAndMessages(null); + } + @Test public void testOnChildLocationsChangedReportsVisibilityChanged() throws Exception { NotificationVisibility[] newlyVisibleKeys = { @@ -281,6 +287,7 @@ public class NotificationLoggerLegacyTest extends SysuiTestCase { mNotificationPanelLoggerFake ); mBarService = barService; + mHandler.removeCallbacksAndMessages(null); // Make this on the current thread so we can wait for it during tests. mHandler = Handler.createAsync(Looper.myLooper()); } diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/logging/NotificationLoggerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/logging/NotificationLoggerTest.java index 4d861f96a9438..b69bd8dfca9c7 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/logging/NotificationLoggerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/logging/NotificationLoggerTest.java @@ -60,6 +60,7 @@ import com.android.systemui.util.time.FakeSystemClock; import com.google.android.collect.Lists; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -132,6 +133,11 @@ public class NotificationLoggerTest extends SysuiTestCase { verify(mNotifPipeline).addCollectionListener(any()); } + @After + public void tearDown() { + mLogger.mHandler.removeCallbacksAndMessages(null); + } + @Test public void testOnChildLocationsChangedReportsVisibilityChanged() throws Exception { NotificationVisibility[] newlyVisibleKeys = { @@ -282,6 +288,7 @@ public class NotificationLoggerTest extends SysuiTestCase { mNotificationPanelLoggerFake ); mBarService = barService; + mHandler.removeCallbacksAndMessages(null); // Make this on the current thread so we can wait for it during tests. mHandler = Handler.createAsync(Looper.myLooper()); } diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationTestHelper.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationTestHelper.java index f3eece84e34f3..e4721b13332c8 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationTestHelper.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/row/NotificationTestHelper.java @@ -36,6 +36,7 @@ import android.content.Context; import android.content.Intent; import android.content.pm.LauncherApps; import android.graphics.drawable.Icon; +import android.os.Handler; import android.os.UserHandle; import android.service.notification.StatusBarNotification; import android.testing.TestableLooper; @@ -139,6 +140,8 @@ public class NotificationTestHelper { mock(NotificationGroupManagerLegacy.class), mock(ConfigurationControllerImpl.class) ); + mHeadsUpManager.mHandler.removeCallbacksAndMessages(null); + mHeadsUpManager.mHandler = new Handler(mTestLooper.getLooper()); mGroupMembershipManager.setHeadsUpManager(mHeadsUpManager); mIconManager = new IconManager( mock(CommonNotifCollection.class), diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhoneTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhoneTest.java index 0f419c7684306..e8b9c7b4d289b 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhoneTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/HeadsUpManagerPhoneTest.java @@ -41,6 +41,7 @@ import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper; import com.android.systemui.statusbar.policy.ConfigurationController; import com.android.systemui.statusbar.policy.HeadsUpManagerLogger; +import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; @@ -115,9 +116,15 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest { mConfigurationController ); super.setUp(); + mHeadsUpManager.mHandler.removeCallbacksAndMessages(null); mHeadsUpManager.mHandler = mTestHandler; } + @After + public void tearDown() { + mTestHandler.removeCallbacksAndMessages(null); + } + @Test public void testSnooze() { mHeadsUpManager.showNotification(mEntry); diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationGroupAlertTransferHelperTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationGroupAlertTransferHelperTest.java index b717d28b24ff6..9898b4b2fdbd3 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationGroupAlertTransferHelperTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationGroupAlertTransferHelperTest.java @@ -51,6 +51,7 @@ import com.android.systemui.statusbar.policy.HeadsUpManager; import com.android.systemui.statusbar.policy.HeadsUpManagerLogger; import com.android.wm.shell.bubbles.Bubbles; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -111,6 +112,11 @@ public class NotificationGroupAlertTransferHelperTest extends SysuiTestCase { mHeadsUpManager.addListener(mGroupAlertTransferHelper); } + @After + public void tearDown() { + mHeadsUpManager.mHandler.removeCallbacksAndMessages(null); + } + private void mockHasHeadsUpContentView(NotificationEntry entry, boolean hasHeadsUpContentView) { RowContentBindParams params = new RowContentBindParams(); diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationPanelViewControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationPanelViewControllerTest.java index 1cd9b9e774236..3b72f00a7b407 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationPanelViewControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NotificationPanelViewControllerTest.java @@ -148,6 +148,7 @@ import com.android.systemui.util.time.FakeSystemClock; import com.android.systemui.wallet.controller.QuickAccessWalletController; import com.android.wm.shell.animation.FlingAnimationUtils; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -366,6 +367,7 @@ public class NotificationPanelViewControllerTest extends SysuiTestCase { private List mOnAttachStateChangeListeners; private FalsingManagerFake mFalsingManager = new FalsingManagerFake(); private FakeExecutor mExecutor = new FakeExecutor(new FakeSystemClock()); + private Handler mMainHandler; @Before public void setup() { @@ -483,9 +485,11 @@ public class NotificationPanelViewControllerTest extends SysuiTestCase { .thenReturn(true); reset(mView); + mMainHandler = new Handler(Looper.getMainLooper()); + mNotificationPanelViewController = new NotificationPanelViewController(mView, mResources, - new Handler(Looper.getMainLooper()), + mMainHandler, mLayoutInflater, mFeatureFlags, coordinator, expansionHandler, mDynamicPrivacyController, mKeyguardBypassController, @@ -560,6 +564,12 @@ public class NotificationPanelViewControllerTest extends SysuiTestCase { .setHeadsUpAppearanceController(mock(HeadsUpAppearanceController.class)); } + @After + public void tearDown() { + mNotificationPanelViewController.cancelHeightAnimator(); + mMainHandler.removeCallbacksAndMessages(null); + } + @Test public void testSetPanelScrimMinFraction() { mNotificationPanelViewController.setPanelScrimMinFraction(0.5f); diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/HeadsUpManagerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/HeadsUpManagerTest.java index 5e852e303533b..d15ba2615b8ff 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/HeadsUpManagerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/HeadsUpManagerTest.java @@ -42,6 +42,7 @@ import com.android.systemui.statusbar.AlertingNotificationManager; import com.android.systemui.statusbar.AlertingNotificationManagerTest; import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -77,9 +78,15 @@ public class HeadsUpManagerTest extends AlertingNotificationManagerTest { mHeadsUpManager = new TestableHeadsUpManager(mContext); super.setUp(); + mHeadsUpManager.mHandler.removeCallbacksAndMessages(null); mHeadsUpManager.mHandler = mTestHandler; } + @After + public void tearDown() { + mTestHandler.removeCallbacksAndMessages(null); + } + @Test public void testShowNotification_autoDismissesWithAccessibilityTimeout() { doReturn(TEST_A11Y_AUTO_DISMISS_TIME).when(mAccessibilityMgr)