Loading services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +26 −5 Original line number Diff line number Diff line Loading @@ -57,14 +57,15 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import com.android.server.lights.Light; import com.android.server.lights.LightsManager; // THESE TESTS ARE DISABLED FOR NOW BECAUSE THEY DO NOT FINISH 1/2 THE TIME. public class NotificationManagerServiceTest { private static final long WAIT_FOR_IDLE_TIMEOUT = 2; private final String pkg = "com.android.server.notification"; private final int uid = Binder.getCallingUid(); private NotificationManagerService mNotificationManagerService; Loading @@ -74,6 +75,7 @@ public class NotificationManagerServiceTest { private HandlerThread mThread; @Before @Test @UiThreadTest public void setUp() throws Exception { mContext = InstrumentationRegistry.getTargetContext(); Loading Loading @@ -108,6 +110,9 @@ public class NotificationManagerServiceTest { public void waitForIdle() throws Exception { MessageQueue queue = mThread.getLooper().getQueue(); if (queue.isIdle()) { return; } CountDownLatch latch = new CountDownLatch(1); queue.addIdleHandler(new MessageQueue.IdleHandler() { @Override public boolean queueIdle() { Loading @@ -115,11 +120,11 @@ public class NotificationManagerServiceTest { return false; } }); latch.await(); if (!queue.isIdle()) { // Timeout is valid in the cases where the queue goes idle before the IdleHandler // is added. latch.await(WAIT_FOR_IDLE_TIMEOUT, TimeUnit.SECONDS); waitForIdle(); } } private NotificationRecord generateNotificationRecord(NotificationChannel channel) { if (channel == null) { Loading @@ -136,6 +141,7 @@ public class NotificationManagerServiceTest { return new NotificationRecord(mContext, sbn, channel); } @Test @UiThreadTest public void testCreateNotificationChannels_SingleChannel() throws Exception { final NotificationChannel channel = Loading @@ -147,6 +153,7 @@ public class NotificationManagerServiceTest { assertTrue(createdChannel != null); } @Test @UiThreadTest public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { try { Loading @@ -158,6 +165,7 @@ public class NotificationManagerServiceTest { } } @Test @UiThreadTest public void testCreateNotificationChannels_TwoChannels() throws Exception { final NotificationChannel channel1 = Loading @@ -170,6 +178,7 @@ public class NotificationManagerServiceTest { assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null); } @Test @UiThreadTest public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance() throws Exception { Loading @@ -188,6 +197,7 @@ public class NotificationManagerServiceTest { assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); } @Test @UiThreadTest public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() throws Exception { Loading @@ -202,6 +212,7 @@ public class NotificationManagerServiceTest { assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); } @Test @UiThreadTest public void testBlockedNotifications_suspended() throws Exception { NotificationUsageStats usageStats = mock(NotificationUsageStats.class); Loading @@ -217,6 +228,7 @@ public class NotificationManagerServiceTest { verify(usageStats, times(1)).registerSuspendedByAdmin(eq(r)); } @Test @UiThreadTest public void testBlockedNotifications_blockedChannel() throws Exception { NotificationUsageStats usageStats = mock(NotificationUsageStats.class); Loading @@ -233,6 +245,7 @@ public class NotificationManagerServiceTest { verify(usageStats, times(1)).registerBlocked(eq(r)); } @Test @UiThreadTest public void testBlockedNotifications_blockedApp() throws Exception { NotificationUsageStats usageStats = mock(NotificationUsageStats.class); Loading @@ -249,6 +262,7 @@ public class NotificationManagerServiceTest { verify(usageStats, times(1)).registerBlocked(eq(r)); } @Test @UiThreadTest public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, Loading @@ -259,6 +273,7 @@ public class NotificationManagerServiceTest { assertEquals(1, notifs.length); } @Test @UiThreadTest public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, Loading @@ -270,6 +285,7 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -282,6 +298,7 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -294,6 +311,7 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -307,6 +325,7 @@ public class NotificationManagerServiceTest { assertEquals(1, notifs.length); } @Test @UiThreadTest public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -320,6 +339,7 @@ public class NotificationManagerServiceTest { assertEquals(1, notifs.length); } @Test @UiThreadTest public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -332,6 +352,7 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading Loading
services/tests/notification/src/com/android/server/notification/NotificationManagerServiceTest.java +26 −5 Original line number Diff line number Diff line Loading @@ -57,14 +57,15 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import com.android.server.lights.Light; import com.android.server.lights.LightsManager; // THESE TESTS ARE DISABLED FOR NOW BECAUSE THEY DO NOT FINISH 1/2 THE TIME. public class NotificationManagerServiceTest { private static final long WAIT_FOR_IDLE_TIMEOUT = 2; private final String pkg = "com.android.server.notification"; private final int uid = Binder.getCallingUid(); private NotificationManagerService mNotificationManagerService; Loading @@ -74,6 +75,7 @@ public class NotificationManagerServiceTest { private HandlerThread mThread; @Before @Test @UiThreadTest public void setUp() throws Exception { mContext = InstrumentationRegistry.getTargetContext(); Loading Loading @@ -108,6 +110,9 @@ public class NotificationManagerServiceTest { public void waitForIdle() throws Exception { MessageQueue queue = mThread.getLooper().getQueue(); if (queue.isIdle()) { return; } CountDownLatch latch = new CountDownLatch(1); queue.addIdleHandler(new MessageQueue.IdleHandler() { @Override public boolean queueIdle() { Loading @@ -115,11 +120,11 @@ public class NotificationManagerServiceTest { return false; } }); latch.await(); if (!queue.isIdle()) { // Timeout is valid in the cases where the queue goes idle before the IdleHandler // is added. latch.await(WAIT_FOR_IDLE_TIMEOUT, TimeUnit.SECONDS); waitForIdle(); } } private NotificationRecord generateNotificationRecord(NotificationChannel channel) { if (channel == null) { Loading @@ -136,6 +141,7 @@ public class NotificationManagerServiceTest { return new NotificationRecord(mContext, sbn, channel); } @Test @UiThreadTest public void testCreateNotificationChannels_SingleChannel() throws Exception { final NotificationChannel channel = Loading @@ -147,6 +153,7 @@ public class NotificationManagerServiceTest { assertTrue(createdChannel != null); } @Test @UiThreadTest public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { try { Loading @@ -158,6 +165,7 @@ public class NotificationManagerServiceTest { } } @Test @UiThreadTest public void testCreateNotificationChannels_TwoChannels() throws Exception { final NotificationChannel channel1 = Loading @@ -170,6 +178,7 @@ public class NotificationManagerServiceTest { assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null); } @Test @UiThreadTest public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance() throws Exception { Loading @@ -188,6 +197,7 @@ public class NotificationManagerServiceTest { assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); } @Test @UiThreadTest public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() throws Exception { Loading @@ -202,6 +212,7 @@ public class NotificationManagerServiceTest { assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); } @Test @UiThreadTest public void testBlockedNotifications_suspended() throws Exception { NotificationUsageStats usageStats = mock(NotificationUsageStats.class); Loading @@ -217,6 +228,7 @@ public class NotificationManagerServiceTest { verify(usageStats, times(1)).registerSuspendedByAdmin(eq(r)); } @Test @UiThreadTest public void testBlockedNotifications_blockedChannel() throws Exception { NotificationUsageStats usageStats = mock(NotificationUsageStats.class); Loading @@ -233,6 +245,7 @@ public class NotificationManagerServiceTest { verify(usageStats, times(1)).registerBlocked(eq(r)); } @Test @UiThreadTest public void testBlockedNotifications_blockedApp() throws Exception { NotificationUsageStats usageStats = mock(NotificationUsageStats.class); Loading @@ -249,6 +262,7 @@ public class NotificationManagerServiceTest { verify(usageStats, times(1)).registerBlocked(eq(r)); } @Test @UiThreadTest public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception { mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, Loading @@ -259,6 +273,7 @@ public class NotificationManagerServiceTest { assertEquals(1, notifs.length); } @Test @UiThreadTest public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception { mBinderService.enqueueNotificationWithTag(mContext.getPackageName(), "opPkg", "tag", 0, Loading @@ -270,6 +285,7 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -282,6 +298,7 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -294,6 +311,7 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelAllNotifications_IgnoreForegroundService() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -307,6 +325,7 @@ public class NotificationManagerServiceTest { assertEquals(1, notifs.length); } @Test @UiThreadTest public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -320,6 +339,7 @@ public class NotificationManagerServiceTest { assertEquals(1, notifs.length); } @Test @UiThreadTest public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading @@ -332,6 +352,7 @@ public class NotificationManagerServiceTest { assertEquals(0, notifs.length); } @Test @UiThreadTest public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception { final StatusBarNotification sbn = generateNotificationRecord(null).sbn; Loading