Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7e1bed5a authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

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
parent f8660097
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -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
+7 −0
Original line number Diff line number Diff line
@@ -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);
+7 −0
Original line number Diff line number Diff line
@@ -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());
        }
+7 −0
Original line number Diff line number Diff line
@@ -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());
        }
+3 −0
Original line number Diff line number Diff line
@@ -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),
Loading