Add Notification Flag behavior test

Verify the behaviors of FLAG_ONGOING_EVENT, FLAG_NO_CLEAR,
FLAG_FOREGROUND_SERVICE after calling the below API.
1. onClearAll
2. cancelAllNotifications
3. cancelAllNotificationsInt
4. cancelNotificationsFromListener with or without keys

Bug: 69646960
Merged-In: I67fef5dbfd61ec9ef6dbee32a0cb80e9c1a2d1b4
Change-Id: I019660efc964a786b54c045dc4309f8a00d4ff01
This commit is contained in:
Yan Sun
2017-11-21 16:36:00 +08:00
committed by Shunta Sato
parent d09d106ec0
commit 56a96d3675

View File

@@ -539,6 +539,77 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
}
@Test
public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlag()
throws Exception {
final NotificationRecord parent = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
final NotificationRecord child = generateNotificationRecord(
mTestNotificationChannel, 2, "group", false);
final NotificationRecord child2 = generateNotificationRecord(
mTestNotificationChannel, 3, "group", false);
child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
final NotificationRecord newGroup = generateNotificationRecord(
mTestNotificationChannel, 4, "group2", false);
mNotificationManagerService.addNotification(parent);
mNotificationManagerService.addNotification(child);
mNotificationManagerService.addNotification(child2);
mNotificationManagerService.addNotification(newGroup);
mNotificationManagerService.getBinderService().cancelNotificationsFromListener(null, null);
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(parent.sbn.getPackageName());
assertEquals(0, notifs.length);
}
@Test
public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlagWithParameter()
throws Exception {
final NotificationRecord parent = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
final NotificationRecord child = generateNotificationRecord(
mTestNotificationChannel, 2, "group", false);
final NotificationRecord child2 = generateNotificationRecord(
mTestNotificationChannel, 3, "group", false);
child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
final NotificationRecord newGroup = generateNotificationRecord(
mTestNotificationChannel, 4, "group2", false);
mNotificationManagerService.addNotification(parent);
mNotificationManagerService.addNotification(child);
mNotificationManagerService.addNotification(child2);
mNotificationManagerService.addNotification(newGroup);
String[] keys = {parent.sbn.getKey(), child.sbn.getKey(),
child2.sbn.getKey(), newGroup.sbn.getKey()};
mNotificationManagerService.getBinderService().cancelNotificationsFromListener(null, keys);
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(parent.sbn.getPackageName());
assertEquals(1, notifs.length);
}
@Test
public void testUserInitiatedCancelAllWithGroup_ForegroundServiceFlag() throws Exception {
final NotificationRecord parent = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
final NotificationRecord child = generateNotificationRecord(
mTestNotificationChannel, 2, "group", false);
final NotificationRecord child2 = generateNotificationRecord(
mTestNotificationChannel, 3, "group", false);
child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
final NotificationRecord newGroup = generateNotificationRecord(
mTestNotificationChannel, 4, "group2", false);
mNotificationManagerService.addNotification(parent);
mNotificationManagerService.addNotification(child);
mNotificationManagerService.addNotification(child2);
mNotificationManagerService.addNotification(newGroup);
mNotificationManagerService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
parent.getUserId());
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(parent.sbn.getPackageName());
assertEquals(0, notifs.length);
}
@Test
public void testFindGroupNotificationsLocked() throws Exception {
// make sure the same notification can be found in both lists and returned
@@ -1077,4 +1148,157 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
assertEquals(39, mNotificationManagerService.getNotificationCountLocked(
PKG, new UserHandle(mUid).getIdentifier(), 0, "tag"));
}
@Test
public void testCancelAllNotifications_CancelsNoClearFlagOnGoing() throws Exception {
final NotificationRecord notif = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
notif.getNotification().flags |= Notification.FLAG_NO_CLEAR;
mNotificationManagerService.addNotification(notif);
mNotificationManagerService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0,
Notification.FLAG_ONGOING_EVENT, true, notif.getUserId(), 0, null);
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(notif.sbn.getPackageName());
assertEquals(0, notifs.length);
}
@Test
public void testCancelAllCancelNotificationsFromListener_NoClearFlagWithParameter()
throws Exception {
final NotificationRecord parent = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
final NotificationRecord child = generateNotificationRecord(
mTestNotificationChannel, 2, "group", false);
final NotificationRecord child2 = generateNotificationRecord(
mTestNotificationChannel, 3, "group", false);
child2.getNotification().flags |= Notification.FLAG_NO_CLEAR;
final NotificationRecord newGroup = generateNotificationRecord(
mTestNotificationChannel, 4, "group2", false);
mNotificationManagerService.addNotification(parent);
mNotificationManagerService.addNotification(child);
mNotificationManagerService.addNotification(child2);
mNotificationManagerService.addNotification(newGroup);
String[] keys = {parent.sbn.getKey(), child.sbn.getKey(),
child2.sbn.getKey(), newGroup.sbn.getKey()};
mNotificationManagerService.getBinderService().cancelNotificationsFromListener(null, keys);
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(parent.sbn.getPackageName());
assertEquals(0, notifs.length);
}
@Test
public void testAppInitiatedCancelAllNotifications_CancelsOnGoingFlag() throws Exception {
final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
sbn.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
sbn.getId(), sbn.getNotification(), sbn.getUserId());
mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(sbn.getPackageName());
assertEquals(0, notifs.length);
}
@Test
public void testCancelAllNotifications_CancelsOnGoingFlag() throws Exception {
final NotificationRecord notif = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManagerService.addNotification(notif);
mNotificationManagerService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true,
notif.getUserId(), 0, null);
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(notif.sbn.getPackageName());
assertEquals(0, notifs.length);
}
@Test
public void testUserInitiatedCancelAllOnClearAll_OnGoingFlag() throws Exception {
final NotificationRecord notif = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManagerService.addNotification(notif);
mNotificationManagerService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
notif.getUserId());
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(notif.sbn.getPackageName());
assertEquals(1, notifs.length);
}
@Test
public void testCancelAllCancelNotificationsFromListener_OnGoingFlag() throws Exception {
final NotificationRecord parent = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
final NotificationRecord child = generateNotificationRecord(
mTestNotificationChannel, 2, "group", false);
final NotificationRecord child2 = generateNotificationRecord(
mTestNotificationChannel, 3, "group", false);
child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
final NotificationRecord newGroup = generateNotificationRecord(
mTestNotificationChannel, 4, "group2", false);
mNotificationManagerService.addNotification(parent);
mNotificationManagerService.addNotification(child);
mNotificationManagerService.addNotification(child2);
mNotificationManagerService.addNotification(newGroup);
mNotificationManagerService.getBinderService().cancelNotificationsFromListener(null, null);
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(parent.sbn.getPackageName());
assertEquals(1, notifs.length);
}
@Test
public void testCancelAllCancelNotificationsFromListener_OnGoingFlagWithParameter()
throws Exception {
final NotificationRecord parent = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
final NotificationRecord child = generateNotificationRecord(
mTestNotificationChannel, 2, "group", false);
final NotificationRecord child2 = generateNotificationRecord(
mTestNotificationChannel, 3, "group", false);
child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
final NotificationRecord newGroup = generateNotificationRecord(
mTestNotificationChannel, 4, "group2", false);
mNotificationManagerService.addNotification(parent);
mNotificationManagerService.addNotification(child);
mNotificationManagerService.addNotification(child2);
mNotificationManagerService.addNotification(newGroup);
String[] keys = {parent.sbn.getKey(), child.sbn.getKey(),
child2.sbn.getKey(), newGroup.sbn.getKey()};
mNotificationManagerService.getBinderService().cancelNotificationsFromListener(null, keys);
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(parent.sbn.getPackageName());
// Flags aren't checked in the cancel children call,
// so all of the notifications are canceled (expected value is 0)
assertEquals(0, notifs.length);
}
@Test
public void testUserInitiatedCancelAllWithGroup_OnGoingFlag() throws Exception {
final NotificationRecord parent = generateNotificationRecord(
mTestNotificationChannel, 1, "group", true);
final NotificationRecord child = generateNotificationRecord(
mTestNotificationChannel, 2, "group", false);
final NotificationRecord child2 = generateNotificationRecord(
mTestNotificationChannel, 3, "group", false);
child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
final NotificationRecord newGroup = generateNotificationRecord(
mTestNotificationChannel, 4, "group2", false);
mNotificationManagerService.addNotification(parent);
mNotificationManagerService.addNotification(child);
mNotificationManagerService.addNotification(child2);
mNotificationManagerService.addNotification(newGroup);
mNotificationManagerService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
parent.getUserId());
waitForIdle();
StatusBarNotification[] notifs =
mBinderService.getActiveNotifications(parent.sbn.getPackageName());
assertEquals(1, notifs.length);
}
}