Merge "GetActiveNotifications should never return null" into pi-dev

This commit is contained in:
Julia Reynolds
2018-04-20 20:21:41 +00:00
committed by Android (Google) Code Review
2 changed files with 34 additions and 3 deletions

View File

@@ -807,7 +807,8 @@ public abstract class NotificationListenerService extends Service {
* @return An array of active notifications, sorted in natural order.
*/
public StatusBarNotification[] getActiveNotifications() {
return getActiveNotifications(null, TRIM_FULL);
StatusBarNotification[] activeNotifications = getActiveNotifications(null, TRIM_FULL);
return activeNotifications != null ? activeNotifications : new StatusBarNotification[0];
}
/**
@@ -842,7 +843,8 @@ public abstract class NotificationListenerService extends Service {
*/
@SystemApi
public StatusBarNotification[] getActiveNotifications(int trim) {
return getActiveNotifications(null, trim);
StatusBarNotification[] activeNotifications = getActiveNotifications(null, trim);
return activeNotifications != null ? activeNotifications : new StatusBarNotification[0];
}
/**
@@ -858,7 +860,8 @@ public abstract class NotificationListenerService extends Service {
* same order as the key list.
*/
public StatusBarNotification[] getActiveNotifications(String[] keys) {
return getActiveNotifications(keys, TRIM_FULL);
StatusBarNotification[] activeNotifications = getActiveNotifications(keys, TRIM_FULL);
return activeNotifications != null ? activeNotifications : new StatusBarNotification[0];
}
/**
@@ -890,6 +893,9 @@ public abstract class NotificationListenerService extends Service {
private StatusBarNotification[] cleanUpNotificationList(
ParceledListSlice<StatusBarNotification> parceledList) {
if (parceledList == null || parceledList.getList() == null) {
return new StatusBarNotification[0];
}
List<StatusBarNotification> list = parceledList.getList();
ArrayList<StatusBarNotification> corruptNotifications = null;
int N = list.size();

View File

@@ -24,7 +24,13 @@ import static android.service.notification.NotificationListenerService.Ranking
.USER_SENTIMENT_POSITIVE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.app.INotificationManager;
import android.app.NotificationChannel;
import android.content.Intent;
import android.os.Binder;
@@ -34,6 +40,7 @@ import android.service.notification.NotificationListenerService;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.NotificationRankingUpdate;
import android.service.notification.SnoozeCriterion;
import android.service.notification.StatusBarNotification;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;
@@ -51,6 +58,19 @@ public class NotificationListenerServiceTest extends UiServiceTestCase {
private String[] mKeys = new String[] { "key", "key1", "key2", "key3"};
@Test
public void testGetActiveNotifications_notNull() throws Exception {
TestListenerService service = new TestListenerService();
INotificationManager noMan = service.getNoMan();
when(noMan.getActiveNotificationsFromListener(any(), any(), anyInt())).thenReturn(null);
assertNotNull(service.getActiveNotifications());
assertNotNull(service.getActiveNotifications(NotificationListenerService.TRIM_FULL));
assertNotNull(service.getActiveNotifications(new String[0]));
assertNotNull(service.getActiveNotifications(
new String[0], NotificationListenerService.TRIM_LIGHT));
}
@Test
public void testRanking() throws Exception {
TestListenerService service = new TestListenerService();
@@ -180,7 +200,12 @@ public class NotificationListenerServiceTest extends UiServiceTestCase {
private final IBinder binder = new LocalBinder();
public TestListenerService() {
mWrapper = mock(NotificationListenerWrapper.class);
mNoMan = mock(INotificationManager.class);
}
INotificationManager getNoMan() {
return mNoMan;
}
@Override