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

Commit 105e2708 authored by Julia Tuttle's avatar Julia Tuttle Committed by Android (Google) Code Review
Browse files

Merge "HeadsUpManagerTest: Improve and add test cases" into udc-qpr-dev

parents f7690abf 434e9d79
Loading
Loading
Loading
Loading
+77 −66
Original line number Diff line number Diff line
@@ -17,21 +17,19 @@

package com.android.systemui.statusbar;

import static android.app.Notification.FLAG_FSI_REQUESTED_BUT_DENIED;

import static com.android.systemui.dump.LogBufferHelperKt.logcatLogBuffer;
import static com.android.systemui.statusbar.notification.row.NotificationRowContentBinder.FLAG_CONTENT_VIEW_CONTRACTED;

import static com.google.common.truth.Truth.assertThat;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.os.Handler;
import android.os.Looper;
import android.os.UserHandle;
@@ -67,17 +65,15 @@ public class AlertingNotificationManagerTest extends SysuiTestCase {
    private static final String TEST_PACKAGE_NAME = "test";
    private static final int TEST_UID = 0;

    protected static final int TEST_MINIMUM_DISPLAY_TIME = 200;
    protected static final int TEST_STICKY_DISPLAY_TIME = 1000;
    protected static final int TEST_AUTO_DISMISS_TIME = 500;
    protected static final int TEST_MINIMUM_DISPLAY_TIME = 400;
    protected static final int TEST_AUTO_DISMISS_TIME = 600;
    protected static final int TEST_STICKY_AUTO_DISMISS_TIME = 800;
    // Number of notifications to use in tests requiring multiple notifications
    private static final int TEST_NUM_NOTIFICATIONS = 4;
    protected static final int TEST_TIMEOUT_TIME = 15000;
    protected static final int TEST_TIMEOUT_TIME = 2_000;
    protected final Runnable mTestTimeoutRunnable = () -> mTimedOut = true;

    protected NotificationEntry mEntry;
    protected Handler mTestHandler;
    private StatusBarNotification mSbn;
    protected boolean mTimedOut = false;

    @Mock protected ExpandableNotificationRow mRow;
@@ -88,8 +84,8 @@ public class AlertingNotificationManagerTest extends SysuiTestCase {
        private TestableAlertingNotificationManager(Handler handler) {
            super(new HeadsUpManagerLogger(logcatLogBuffer()), handler);
            mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME;
            mStickyDisplayTime = TEST_STICKY_DISPLAY_TIME;
            mAutoDismissNotificationDecay = TEST_AUTO_DISMISS_TIME;
            mStickyDisplayTime = TEST_STICKY_AUTO_DISMISS_TIME;
        }

        @Override
@@ -114,7 +110,7 @@ public class AlertingNotificationManagerTest extends SysuiTestCase {
        return new TestableAlertingNotificationManager(mTestHandler);
    }

    protected StatusBarNotification createNewSbn(int id, Notification n) {
    protected StatusBarNotification createSbn(int id, Notification n) {
        return new StatusBarNotification(
                TEST_PACKAGE_NAME /* pkg */,
                TEST_PACKAGE_NAME,
@@ -128,45 +124,53 @@ public class AlertingNotificationManagerTest extends SysuiTestCase {
                0 /* postTime */);
    }

    protected StatusBarNotification createNewSbn(int id, Notification.Builder n) {
        return new StatusBarNotification(
                TEST_PACKAGE_NAME /* pkg */,
                TEST_PACKAGE_NAME,
                id,
                null /* tag */,
                TEST_UID,
                0 /* initialPid */,
                n.build(),
                new UserHandle(ActivityManager.getCurrentUser()),
                null /* overrideGroupKey */,
                0 /* postTime */);
    protected StatusBarNotification createSbn(int id, Notification.Builder n) {
        return createSbn(id, n.build());
    }

    protected StatusBarNotification createNewNotification(int id) {
        Notification.Builder n = new Notification.Builder(mContext, "")
    protected StatusBarNotification createSbn(int id) {
        final Notification.Builder b = new Notification.Builder(mContext, "")
                .setSmallIcon(R.drawable.ic_person)
                .setContentTitle("Title")
                .setContentText("Text");
        return createNewSbn(id, n);
        return createSbn(id, b);
    }

    protected StatusBarNotification createStickySbn(int id) {
        Notification stickyHun = new Notification.Builder(mContext, "")
                .setSmallIcon(R.drawable.ic_person)
                .setFullScreenIntent(mock(PendingIntent.class), /* highPriority */ true)
                .build();
        stickyHun.flags |= FLAG_FSI_REQUESTED_BUT_DENIED;
        return createNewSbn(id, stickyHun);
    protected NotificationEntry createEntry(int id, Notification n) {
        return new NotificationEntryBuilder().setSbn(createSbn(id, n)).build();
    }

    protected NotificationEntry createEntry(int id) {
        return new NotificationEntryBuilder().setSbn(createSbn(id)).build();
    }

    protected void verifyAlertingAtTime(AlertingNotificationManager anm, NotificationEntry entry,
            boolean shouldBeAlerting, int whenToCheckAlertingMillis, String whenCondition) {
        final Boolean[] wasAlerting = {null};
        final Runnable checkAlerting =
                () -> wasAlerting[0] = anm.isAlerting(entry.getKey());

        mTestHandler.postDelayed(checkAlerting, whenToCheckAlertingMillis);
        mTestHandler.postDelayed(mTestTimeoutRunnable, TEST_TIMEOUT_TIME);
        TestableLooper.get(this).processMessages(2);

        assertFalse("Test timed out", mTimedOut);
        if (shouldBeAlerting) {
            assertTrue("Should still be alerting after " + whenCondition, wasAlerting[0]);
        } else {
            assertFalse("Should not still be alerting after " + whenCondition, wasAlerting[0]);
        }
        assertFalse("Should not still be alerting after processing",
                anm.isAlerting(entry.getKey()));
    }

    @Before
    public void setUp() {
        mTestHandler = Handler.createAsync(Looper.myLooper());
        mSbn = createNewNotification(0 /* id */);
        mEntry = new NotificationEntryBuilder()
                .setSbn(mSbn)
                .build();
        mEntry.setRow(mRow);

        assertThat(TEST_MINIMUM_DISPLAY_TIME).isLessThan(TEST_AUTO_DISMISS_TIME);
        assertThat(TEST_AUTO_DISMISS_TIME).isLessThan(TEST_STICKY_AUTO_DISMISS_TIME);
        assertThat(TEST_STICKY_AUTO_DISMISS_TIME).isLessThan(TEST_TIMEOUT_TIME);
    }

    @After
@@ -176,59 +180,64 @@ public class AlertingNotificationManagerTest extends SysuiTestCase {

    @Test
    public void testShowNotification_addsEntry() {
        AlertingNotificationManager alm = createAlertingNotificationManager();
        final AlertingNotificationManager alm = createAlertingNotificationManager();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        alm.showNotification(mEntry);
        alm.showNotification(entry);

        assertTrue(alm.isAlerting(mEntry.getKey()));
        assertTrue(alm.isAlerting(entry.getKey()));
        assertTrue(alm.hasNotifications());
        assertEquals(mEntry, alm.getEntry(mEntry.getKey()));
        assertEquals(entry, alm.getEntry(entry.getKey()));
    }

    @Test
    public void testShowNotification_autoDismisses() {
        AlertingNotificationManager alm = createAlertingNotificationManager();
        final AlertingNotificationManager alm = createAlertingNotificationManager();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        alm.showNotification(mEntry);
        mTestHandler.postDelayed(mTestTimeoutRunnable, TEST_TIMEOUT_TIME);
        alm.showNotification(entry);

        // Wait for remove runnable and then process it immediately
        TestableLooper.get(this).processMessages(1);
        verifyAlertingAtTime(alm, entry, false, TEST_AUTO_DISMISS_TIME * 3 / 2,
                "auto dismiss time");

        assertFalse("Test timed out", mTimedOut);
        assertFalse(alm.isAlerting(mEntry.getKey()));
        assertFalse(alm.isAlerting(entry.getKey()));
    }

    @Test
    public void testRemoveNotification_removeDeferred() {
        AlertingNotificationManager alm = createAlertingNotificationManager();
        alm.showNotification(mEntry);
        final AlertingNotificationManager alm = createAlertingNotificationManager();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        alm.showNotification(entry);

        // Try to remove but defer, since the notification has not been shown long enough.
        alm.removeNotification(mEntry.getKey(), false /* releaseImmediately */);
        final boolean removedImmediately = alm.removeNotification(entry.getKey(),
                false /* releaseImmediately */);

        assertTrue(alm.isAlerting(mEntry.getKey()));
        assertFalse(removedImmediately);
        assertTrue(alm.isAlerting(entry.getKey()));
    }

    @Test
    public void testRemoveNotification_forceRemove() {
        AlertingNotificationManager alm = createAlertingNotificationManager();
        alm.showNotification(mEntry);
        final AlertingNotificationManager alm = createAlertingNotificationManager();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        alm.showNotification(entry);

        // Remove forcibly with releaseImmediately = true.
        alm.removeNotification(mEntry.getKey(), true /* releaseImmediately */);
        final boolean removedImmediately = alm.removeNotification(entry.getKey(),
                true /* releaseImmediately */);

        assertFalse(alm.isAlerting(mEntry.getKey()));
        assertTrue(removedImmediately);
        assertFalse(alm.isAlerting(entry.getKey()));
    }

    @Test
    public void testReleaseAllImmediately() {
        AlertingNotificationManager alm = createAlertingNotificationManager();
        final AlertingNotificationManager alm = createAlertingNotificationManager();
        for (int i = 0; i < TEST_NUM_NOTIFICATIONS; i++) {
            StatusBarNotification sbn = createNewNotification(i);
            NotificationEntry entry = new NotificationEntryBuilder()
                    .setSbn(sbn)
                    .build();
            final NotificationEntry entry = createEntry(i);
            entry.setRow(mRow);
            alm.showNotification(entry);
        }
@@ -240,10 +249,12 @@ public class AlertingNotificationManagerTest extends SysuiTestCase {

    @Test
    public void testCanRemoveImmediately_notShownLongEnough() {
        AlertingNotificationManager alm = createAlertingNotificationManager();
        alm.showNotification(mEntry);
        final AlertingNotificationManager alm = createAlertingNotificationManager();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        alm.showNotification(entry);

        // The entry has just been added so we should not remove immediately.
        assertFalse(alm.canRemoveImmediately(mEntry.getKey()));
        assertFalse(alm.canRemoveImmediately(entry.getKey()));
    }
}
+51 −47
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ import com.android.systemui.statusbar.AlertingNotificationManager;
import com.android.systemui.statusbar.AlertingNotificationManagerTest;
import com.android.systemui.statusbar.NotificationShadeWindowController;
import com.android.systemui.statusbar.notification.collection.NotificationEntry;
import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder;
import com.android.systemui.statusbar.notification.collection.provider.VisualStabilityProvider;
import com.android.systemui.statusbar.notification.collection.render.GroupMembershipManager;
import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
@@ -62,8 +61,6 @@ import org.mockito.junit.MockitoRule;
public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest {
    @Rule public MockitoRule rule = MockitoJUnit.rule();

    private HeadsUpManagerPhone mHeadsUpManager;

    private final HeadsUpManagerLogger mHeadsUpManagerLogger = new HeadsUpManagerLogger(
            logcatLogBuffer());
    @Mock private GroupMembershipManager mGroupManager;
@@ -108,15 +105,31 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest {
        }
    }

    private HeadsUpManagerPhone createHeadsUpManagerPhone() {
        return new TestableHeadsUpManagerPhone(
                mContext,
                mHeadsUpManagerLogger,
                mGroupManager,
                mVSProvider,
                mStatusBarStateController,
                mBypassController,
                mConfigurationController,
                mTestHandler,
                mAccessibilityManagerWrapper,
                mUiEventLogger,
                mShadeExpansionStateManager
        );
    }

    @Override
    protected AlertingNotificationManager createAlertingNotificationManager() {
        return mHeadsUpManager;
        return createHeadsUpManagerPhone();
    }

    @Before
    @Override
    public void setUp() {
        AccessibilityManagerWrapper accessibilityMgr =
        final AccessibilityManagerWrapper accessibilityMgr =
                mDependency.injectMockDependency(AccessibilityManagerWrapper.class);
        when(accessibilityMgr.getRecommendedTimeoutMillis(anyInt(), anyInt()))
                .thenReturn(TEST_AUTO_DISMISS_TIME);
@@ -126,19 +139,6 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest {
                R.integer.ambient_notification_extension_time, 500);

        super.setUp();
        mHeadsUpManager = new TestableHeadsUpManagerPhone(
                mContext,
                mHeadsUpManagerLogger,
                mGroupManager,
                mVSProvider,
                mStatusBarStateController,
                mBypassController,
                mConfigurationController,
                mTestHandler,
                mAccessibilityManagerWrapper,
                mUiEventLogger,
                mShadeExpansionStateManager
        );
    }

    @After
@@ -149,63 +149,67 @@ public class HeadsUpManagerPhoneTest extends AlertingNotificationManagerTest {

    @Test
    public void testSnooze() {
        mHeadsUpManager.showNotification(mEntry);
        final HeadsUpManagerPhone hmp = createHeadsUpManagerPhone();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        mHeadsUpManager.snooze();
        hmp.showNotification(entry);
        hmp.snooze();

        assertTrue(mHeadsUpManager.isSnoozed(mEntry.getSbn().getPackageName()));
        assertTrue(hmp.isSnoozed(entry.getSbn().getPackageName()));
    }

    @Test
    public void testSwipedOutNotification() {
        mHeadsUpManager.showNotification(mEntry);
        mHeadsUpManager.addSwipedOutNotification(mEntry.getKey());
        final HeadsUpManagerPhone hmp = createHeadsUpManagerPhone();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        hmp.showNotification(entry);
        hmp.addSwipedOutNotification(entry.getKey());

        // Remove should succeed because the notification is swiped out
        mHeadsUpManager.removeNotification(mEntry.getKey(), false /* releaseImmediately */);
        final boolean removedImmediately = hmp.removeNotification(entry.getKey(),
                /* releaseImmediately = */ false);

        assertFalse(mHeadsUpManager.isAlerting(mEntry.getKey()));
        assertTrue(removedImmediately);
        assertFalse(hmp.isAlerting(entry.getKey()));
    }

    @Test
    public void testCanRemoveImmediately_swipedOut() {
        mHeadsUpManager.showNotification(mEntry);
        mHeadsUpManager.addSwipedOutNotification(mEntry.getKey());
        final HeadsUpManagerPhone hmp = createHeadsUpManagerPhone();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        hmp.showNotification(entry);
        hmp.addSwipedOutNotification(entry.getKey());

        // Notification is swiped so it can be immediately removed.
        assertTrue(mHeadsUpManager.canRemoveImmediately(mEntry.getKey()));
        assertTrue(hmp.canRemoveImmediately(entry.getKey()));
    }

    @Ignore("b/141538055")
    @Test
    public void testCanRemoveImmediately_notTopEntry() {
        NotificationEntry laterEntry = new NotificationEntryBuilder()
                .setSbn(createNewNotification(1))
                .build();
        final HeadsUpManagerPhone hmp = createHeadsUpManagerPhone();
        final NotificationEntry earlierEntry = createEntry(/* id = */ 0);
        final NotificationEntry laterEntry = createEntry(/* id = */ 1);
        laterEntry.setRow(mRow);
        mHeadsUpManager.showNotification(mEntry);
        mHeadsUpManager.showNotification(laterEntry);

        hmp.showNotification(earlierEntry);
        hmp.showNotification(laterEntry);

        // Notification is "behind" a higher priority notification so we can remove it immediately.
        assertTrue(mHeadsUpManager.canRemoveImmediately(mEntry.getKey()));
        assertTrue(hmp.canRemoveImmediately(earlierEntry.getKey()));
    }

    @Test
    public void testExtendHeadsUp() {
        mHeadsUpManager.showNotification(mEntry);
        Runnable pastNormalTimeRunnable =
                () -> mLivesPastNormalTime = mHeadsUpManager.isAlerting(mEntry.getKey());
        mTestHandler.postDelayed(pastNormalTimeRunnable,
                TEST_AUTO_DISMISS_TIME + mHeadsUpManager.mExtensionTime / 2);
        mTestHandler.postDelayed(mTestTimeoutRunnable, TEST_TIMEOUT_TIME);

        mHeadsUpManager.extendHeadsUp();
        final HeadsUpManagerPhone hmp = createHeadsUpManagerPhone();
        final NotificationEntry entry = createEntry(/* id = */ 0);

        // Wait for normal time runnable and extended remove runnable and process them on arrival.
        TestableLooper.get(this).processMessages(2);
        hmp.showNotification(entry);
        hmp.extendHeadsUp();

        assertFalse("Test timed out", mTimedOut);
        assertTrue("Pulse was not extended", mLivesPastNormalTime);
        assertFalse(mHeadsUpManager.isAlerting(mEntry.getKey()));
        final int pastNormalTimeMillis = TEST_AUTO_DISMISS_TIME + hmp.mExtensionTime / 2;
        verifyAlertingAtTime(hmp, entry, true, pastNormalTimeMillis, "normal time");
    }
}
+324 −142

File changed.

Preview size limit exceeded, changes collapsed.