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

Commit 16594465 authored by Geoffrey Pitsch's avatar Geoffrey Pitsch
Browse files

Fix NotificationManagerServiceTest hangs.

Bug caused by message queue going idle before the handler is added.

Bug: 34719217
Test: runtest systemui-notification
Change-Id: I03ba31e5263a43f3e3faa4a19167dbc998092dfa
parent e7c8aeef
Loading
Loading
Loading
Loading
+26 −5
Original line number Original line Diff line number Diff line
@@ -57,14 +57,15 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Arrays;
import java.util.List;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import com.android.server.lights.Light;
import com.android.server.lights.Light;
import com.android.server.lights.LightsManager;
import com.android.server.lights.LightsManager;


// THESE TESTS ARE DISABLED FOR NOW BECAUSE THEY DO NOT FINISH 1/2 THE TIME.
public class NotificationManagerServiceTest {
public class NotificationManagerServiceTest {
    private static final long WAIT_FOR_IDLE_TIMEOUT = 2;
    private final String pkg = "com.android.server.notification";
    private final String pkg = "com.android.server.notification";
    private final int uid = Binder.getCallingUid();
    private final int uid = Binder.getCallingUid();
    private NotificationManagerService mNotificationManagerService;
    private NotificationManagerService mNotificationManagerService;
@@ -74,6 +75,7 @@ public class NotificationManagerServiceTest {
    private HandlerThread mThread;
    private HandlerThread mThread;


    @Before
    @Before
    @Test
    @UiThreadTest
    @UiThreadTest
    public void setUp() throws Exception {
    public void setUp() throws Exception {
        mContext = InstrumentationRegistry.getTargetContext();
        mContext = InstrumentationRegistry.getTargetContext();
@@ -108,6 +110,9 @@ public class NotificationManagerServiceTest {


    public void waitForIdle() throws Exception {
    public void waitForIdle() throws Exception {
        MessageQueue queue = mThread.getLooper().getQueue();
        MessageQueue queue = mThread.getLooper().getQueue();
        if (queue.isIdle()) {
            return;
        }
        CountDownLatch latch = new CountDownLatch(1);
        CountDownLatch latch = new CountDownLatch(1);
        queue.addIdleHandler(new MessageQueue.IdleHandler() {
        queue.addIdleHandler(new MessageQueue.IdleHandler() {
                @Override public boolean queueIdle() {
                @Override public boolean queueIdle() {
@@ -115,11 +120,11 @@ public class NotificationManagerServiceTest {
                    return false;
                    return false;
                }
                }
        });
        });
        latch.await();
        // Timeout is valid in the cases where the queue goes idle before the IdleHandler
        if (!queue.isIdle()) {
        // is added.
        latch.await(WAIT_FOR_IDLE_TIMEOUT, TimeUnit.SECONDS);
        waitForIdle();
        waitForIdle();
    }
    }
    }


    private NotificationRecord generateNotificationRecord(NotificationChannel channel) {
    private NotificationRecord generateNotificationRecord(NotificationChannel channel) {
        if (channel == null) {
        if (channel == null) {
@@ -136,6 +141,7 @@ public class NotificationManagerServiceTest {
        return new NotificationRecord(mContext, sbn, channel);
        return new NotificationRecord(mContext, sbn, channel);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCreateNotificationChannels_SingleChannel() throws Exception {
    public void testCreateNotificationChannels_SingleChannel() throws Exception {
        final NotificationChannel channel =
        final NotificationChannel channel =
@@ -147,6 +153,7 @@ public class NotificationManagerServiceTest {
        assertTrue(createdChannel != null);
        assertTrue(createdChannel != null);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception {
    public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception {
        try {
        try {
@@ -158,6 +165,7 @@ public class NotificationManagerServiceTest {
        }
        }
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCreateNotificationChannels_TwoChannels() throws Exception {
    public void testCreateNotificationChannels_TwoChannels() throws Exception {
        final NotificationChannel channel1 =
        final NotificationChannel channel1 =
@@ -170,6 +178,7 @@ public class NotificationManagerServiceTest {
        assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null);
        assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance()
    public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance()
            throws Exception {
            throws Exception {
@@ -188,6 +197,7 @@ public class NotificationManagerServiceTest {
        assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
        assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond()
    public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond()
            throws Exception {
            throws Exception {
@@ -202,6 +212,7 @@ public class NotificationManagerServiceTest {
        assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
        assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testBlockedNotifications_suspended() throws Exception {
    public void testBlockedNotifications_suspended() throws Exception {
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
@@ -217,6 +228,7 @@ public class NotificationManagerServiceTest {
        verify(usageStats, times(1)).registerSuspendedByAdmin(eq(r));
        verify(usageStats, times(1)).registerSuspendedByAdmin(eq(r));
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testBlockedNotifications_blockedChannel() throws Exception {
    public void testBlockedNotifications_blockedChannel() throws Exception {
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
@@ -233,6 +245,7 @@ public class NotificationManagerServiceTest {
        verify(usageStats, times(1)).registerBlocked(eq(r));
        verify(usageStats, times(1)).registerBlocked(eq(r));
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testBlockedNotifications_blockedApp() throws Exception {
    public void testBlockedNotifications_blockedApp() throws Exception {
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
        NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
@@ -249,6 +262,7 @@ public class NotificationManagerServiceTest {
        verify(usageStats, times(1)).registerBlocked(eq(r));
        verify(usageStats, times(1)).registerBlocked(eq(r));
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception {
    public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception {
        mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0,
        mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0,
@@ -259,6 +273,7 @@ public class NotificationManagerServiceTest {
        assertEquals(1, notifs.length);
        assertEquals(1, notifs.length);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception {
    public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception {
        mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0,
        mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0,
@@ -270,6 +285,7 @@ public class NotificationManagerServiceTest {
        assertEquals(0, notifs.length);
        assertEquals(0, notifs.length);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception {
    public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
@@ -282,6 +298,7 @@ public class NotificationManagerServiceTest {
        assertEquals(0, notifs.length);
        assertEquals(0, notifs.length);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception {
    public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
@@ -294,6 +311,7 @@ public class NotificationManagerServiceTest {
        assertEquals(0, notifs.length);
        assertEquals(0, notifs.length);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCancelAllNotifications_IgnoreForegroundService() throws Exception {
    public void testCancelAllNotifications_IgnoreForegroundService() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
@@ -307,6 +325,7 @@ public class NotificationManagerServiceTest {
        assertEquals(1, notifs.length);
        assertEquals(1, notifs.length);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception {
    public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
@@ -320,6 +339,7 @@ public class NotificationManagerServiceTest {
        assertEquals(1, notifs.length);
        assertEquals(1, notifs.length);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception {
    public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
@@ -332,6 +352,7 @@ public class NotificationManagerServiceTest {
        assertEquals(0, notifs.length);
        assertEquals(0, notifs.length);
    }
    }


    @Test
    @UiThreadTest
    @UiThreadTest
    public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception {
    public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception {
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;