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

Commit 433923a1 authored by Beverly's avatar Beverly Committed by Beverly Tai
Browse files

Edit NEM tests to prevent flakiness

- Set the Assert's main looper as the TestableLooper's looper for all
NEM tests
- in NEM test, bind content synchronously, so the callback won't run on
the 'real' main thread and will instead run on the TestableLooper's
thread
- Allow overriding already initialized dependencies in
TestableDependency

Test: atest SystemUITests
Fixes: 147685528
Change-Id: Ic171bf29e33322f44aa14c40c29c0d9ffa542ade
parent c6eeb791
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -19,8 +19,11 @@ import static org.mockito.Mockito.mock;
import android.content.Context;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;

public class TestableDependency extends Dependency {
    private static final String TAG = "TestableDependency";

    private final ArrayMap<Object, Object> mObjs = new ArrayMap<>();
    private final ArraySet<Object> mInstantiatedObjects = new ArraySet<>();

@@ -44,7 +47,7 @@ public class TestableDependency extends Dependency {

    public <T> void injectTestDependency(Class<T> key, T obj) {
        if (mInstantiatedObjects.contains(key)) {
            throw new IllegalStateException(key + " was already initialized");
            Log.d(TAG, key + " was already initialized but overriding with testDependency.");
        }
        mObjs.put(key, obj);
    }
+12 −17
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.NotificationListenerService.RankingMap;
@@ -87,7 +86,6 @@ import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow
import com.android.systemui.statusbar.notification.row.NotifRemoteViewCache;
import com.android.systemui.statusbar.notification.row.NotificationContentInflater;
import com.android.systemui.statusbar.notification.row.NotificationGutsManager;
import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder;
import com.android.systemui.statusbar.notification.row.RowInflaterTask;
import com.android.systemui.statusbar.notification.stack.NotificationListContainer;
import com.android.systemui.statusbar.phone.KeyguardBypassController;
@@ -97,6 +95,7 @@ import com.android.systemui.statusbar.policy.HeadsUpManager;
import com.android.systemui.util.Assert;
import com.android.systemui.util.leak.LeakDetector;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -185,15 +184,14 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        if (!mDependency.hasInstantiatedDependency(SmartReplyController.class)) {
        mDependency.injectMockDependency(SmartReplyController.class);
        }
        mDependency.injectMockDependency(NotificationMediaManager.class);

        mCountDownLatch = new CountDownLatch(1);

        Assert.sMainLooper = TestableLooper.get(this).getLooper();
        mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
                Handler.createAsync(Looper.myLooper()));
                Handler.createAsync(TestableLooper.get(this).getLooper()));
        when(mRemoteInputManager.getController()).thenReturn(mRemoteInputController);
        when(mListContainer.getViewParentForNotification(any())).thenReturn(
                new FrameLayout(mContext));
@@ -203,9 +201,10 @@ public class NotificationEntryManagerTest extends SysuiTestCase {

        mEntry.expandedIcon = mock(StatusBarIconView.class);

        NotificationRowContentBinder contentBinder = new NotificationContentInflater(
        NotificationContentInflater contentBinder = new NotificationContentInflater(
                mock(NotifRemoteViewCache.class),
                mRemoteInputManager);
        contentBinder.setInflateSynchronously(true);

        NotificationRowBinderImpl notificationRowBinder =
                new NotificationRowBinderImpl(mContext,
@@ -253,6 +252,12 @@ public class NotificationEntryManagerTest extends SysuiTestCase {
                mEntry.getKey(), Ranking.USER_SENTIMENT_NEUTRAL);
    }

    @After
    public void tearDown() {
        // CLEAN UP inflation tasks so they don't callback in a future test
        mEntry.abortTask();
    }

    @Test
    public void testAddNotification() throws Exception {
        TestableLooper.get(this).processAllMessages();
@@ -338,9 +343,6 @@ public class NotificationEntryManagerTest extends SysuiTestCase {

    @Test
    public void testRemoveNotification() {
        // Row inflation happens off thread, so pretend that this test looper is main
        Assert.sMainLooper = TestableLooper.get(this).getLooper();

        mEntry.setRow(mRow);
        mEntryManager.addActiveNotificationForTest(mEntry);

@@ -458,9 +460,6 @@ public class NotificationEntryManagerTest extends SysuiTestCase {

    @Test
    public void testLifetimeExtenders_whenRetentionEndsNotificationIsRemoved() {
        // Row inflation happens off thread, so pretend that this test looper is main
        Assert.sMainLooper = TestableLooper.get(this).getLooper();

        // GIVEN an entry manager with a notification whose life has been extended
        mEntryManager.addActiveNotificationForTest(mEntry);
        final FakeNotificationLifetimeExtender extender = new FakeNotificationLifetimeExtender();
@@ -551,9 +550,6 @@ public class NotificationEntryManagerTest extends SysuiTestCase {

    @Test
    public void testRemoveInterceptor_notInterceptedGetsRemoved() {
        // Row inflation happens off thread, so pretend that this test looper is main
        Assert.sMainLooper = TestableLooper.get(this).getLooper();

        // GIVEN an entry manager with a notification
        mEntryManager.addActiveNotificationForTest(mEntry);

@@ -616,7 +612,6 @@ public class NotificationEntryManagerTest extends SysuiTestCase {

    @Test
    public void testGetNotificationsForCurrentUser_shouldFilterNonCurrentUserNotifications() {
        Assert.sMainLooper = TestableLooper.get(this).getLooper();
        Notification.Builder n = new Notification.Builder(mContext, "di")
                .setSmallIcon(R.drawable.ic_person)
                .setContentTitle("Title")
+3 −1
Original line number Diff line number Diff line
@@ -234,7 +234,9 @@ public class TestableLooper {
            try {
                mLooper = setAsMain ? Looper.getMainLooper() : createLooper();
                mTestableLooper = new TestableLooper(mLooper, false);
                if (!setAsMain) {
                    mTestableLooper.getLooper().getThread().setName(test.getClass().getName());
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }