1 package org.robolectric.shadows; 2 3 import static android.app.NotificationManager.INTERRUPTION_FILTER_ALL; 4 import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY; 5 import static org.assertj.core.api.Assertions.assertThat; 6 import static org.junit.Assert.assertEquals; 7 import static org.junit.Assert.assertNull; 8 import static org.robolectric.Shadows.shadowOf; 9 10 import android.app.Notification; 11 import android.app.NotificationChannel; 12 import android.app.NotificationChannelGroup; 13 import android.app.NotificationManager; 14 import android.content.Context; 15 import android.os.Build; 16 import android.service.notification.StatusBarNotification; 17 import com.google.common.collect.ImmutableList; 18 import org.junit.Before; 19 import org.junit.Test; 20 import org.junit.runner.RunWith; 21 import org.robolectric.RobolectricTestRunner; 22 import org.robolectric.RuntimeEnvironment; 23 import org.robolectric.annotation.Config; 24 25 @RunWith(RobolectricTestRunner.class) 26 public class ShadowNotificationManagerTest { 27 private NotificationManager notificationManager; 28 private Notification notification1 = new Notification(); 29 private Notification notification2 = new Notification(); 30 31 @Before public void setUp() { 32 notificationManager = (NotificationManager) RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE); 33 } 34 35 @Test 36 @Config(minSdk = Build.VERSION_CODES.M) 37 public void getCurrentInterruptionFilter() { 38 // Sensible default 39 assertThat(notificationManager.getCurrentInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_ALL); 40 41 notificationManager.setInterruptionFilter(INTERRUPTION_FILTER_PRIORITY); 42 assertThat(notificationManager.getCurrentInterruptionFilter()).isEqualTo(INTERRUPTION_FILTER_PRIORITY); 43 } 44 45 @Test 46 @Config(minSdk = Build.VERSION_CODES.O) 47 public void createNotificationChannel() { 48 notificationManager.createNotificationChannel(new NotificationChannel("id", "name", 1)); 49 50 assertThat(shadowOf(notificationManager).getNotificationChannels()).hasSize(1); 51 NotificationChannel channel = (NotificationChannel)shadowOf(notificationManager) 52 .getNotificationChannel("id"); 53 assertThat(channel.getName()).isEqualTo("name"); 54 assertThat(channel.getImportance()).isEqualTo(1); 55 } 56 57 @Test 58 @Config(minSdk = Build.VERSION_CODES.O) 59 public void createNotificationChannelGroup() { 60 notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("id", "name")); 61 62 assertThat(shadowOf(notificationManager).getNotificationChannelGroups()).hasSize(1); 63 NotificationChannelGroup group = (NotificationChannelGroup)shadowOf(notificationManager) 64 .getNotificationChannelGroup("id"); 65 assertThat(group.getName()).isEqualTo("name"); 66 } 67 68 @Test 69 @Config(minSdk = Build.VERSION_CODES.O) 70 public void createNotificationChannels() { 71 NotificationChannel channel1 = new NotificationChannel("id", "name", 1); 72 NotificationChannel channel2 = new NotificationChannel("id2", "name2", 1); 73 74 notificationManager.createNotificationChannels(ImmutableList.of(channel1, channel2)); 75 76 assertThat(shadowOf(notificationManager).getNotificationChannels()).hasSize(2); 77 NotificationChannel channel = 78 (NotificationChannel) shadowOf(notificationManager).getNotificationChannel("id"); 79 assertThat(channel.getName()).isEqualTo("name"); 80 assertThat(channel.getImportance()).isEqualTo(1); 81 channel = (NotificationChannel) shadowOf(notificationManager).getNotificationChannel("id2"); 82 assertThat(channel.getName()).isEqualTo("name2"); 83 assertThat(channel.getImportance()).isEqualTo(1); 84 } 85 86 @Test 87 @Config(minSdk = Build.VERSION_CODES.O) 88 public void deleteNotificationChannel() { 89 final String channelId = "channelId"; 90 assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isFalse(); 91 notificationManager.createNotificationChannel(new NotificationChannel(channelId, "name", 1)); 92 assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isFalse(); 93 notificationManager.deleteNotificationChannel(channelId); 94 assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isTrue(); 95 assertThat(notificationManager.getNotificationChannel(channelId)).isNull(); 96 // Per documentation, recreating a deleted channel should have the same settings as the old 97 // deleted channel. 98 notificationManager.createNotificationChannel( 99 new NotificationChannel(channelId, "otherName", 2)); 100 assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isFalse(); 101 NotificationChannel channel = notificationManager.getNotificationChannel(channelId); 102 assertThat(channel.getName()).isEqualTo("name"); 103 assertThat(channel.getImportance()).isEqualTo(1); 104 } 105 106 @Test 107 @Config(minSdk = Build.VERSION_CODES.O) 108 public void deleteNotificationChannelGroup() { 109 final String channelId = "channelId"; 110 final String channelGroupId = "channelGroupId"; 111 notificationManager.createNotificationChannelGroup( 112 new NotificationChannelGroup(channelGroupId, "groupName")); 113 NotificationChannel channel = new NotificationChannel(channelId, "channelName", 1); 114 channel.setGroup(channelGroupId); 115 notificationManager.createNotificationChannel(channel); 116 assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isFalse(); 117 notificationManager.deleteNotificationChannelGroup(channelGroupId); 118 assertThat(shadowOf(notificationManager).getNotificationChannelGroup(channelGroupId)).isNull(); 119 // Per documentation, deleting a channel group also deletes all associated channels. 120 assertThat(shadowOf(notificationManager).isChannelDeleted(channelId)).isTrue(); 121 } 122 123 @Test 124 @Config(minSdk = Build.VERSION_CODES.N) 125 public void areNotificationsEnabled() { 126 shadowOf(notificationManager).setNotificationsEnabled(true); 127 assertThat(notificationManager.areNotificationsEnabled()).isTrue(); 128 shadowOf(notificationManager).setNotificationsEnabled(false); 129 assertThat(notificationManager.areNotificationsEnabled()).isFalse(); 130 } 131 132 @Test 133 public void testNotify() throws Exception { 134 notificationManager.notify(1, notification1); 135 assertEquals(1, shadowOf(notificationManager).size()); 136 assertEquals(notification1, shadowOf(notificationManager).getNotification(null, 1)); 137 138 notificationManager.notify(31, notification2); 139 assertEquals(2, shadowOf(notificationManager).size()); 140 assertEquals(notification2, shadowOf(notificationManager).getNotification(null, 31)); 141 } 142 143 @Test 144 public void testNotifyReplaces() throws Exception { 145 notificationManager.notify(1, notification1); 146 147 notificationManager.notify(1, notification2); 148 assertEquals(1, shadowOf(notificationManager).size()); 149 assertEquals(notification2, shadowOf(notificationManager).getNotification(null, 1)); 150 } 151 152 @Test 153 public void testNotifyWithTag() throws Exception { 154 notificationManager.notify("a tag", 1, notification1); 155 assertEquals(1, shadowOf(notificationManager).size()); 156 assertEquals(notification1, shadowOf(notificationManager).getNotification("a tag", 1)); 157 } 158 159 @Test 160 public void notifyWithTag_shouldReturnNullForNullTag() throws Exception { 161 notificationManager.notify("a tag", 1, notification1); 162 assertEquals(1, shadowOf(notificationManager).size()); 163 assertNull(shadowOf(notificationManager).getNotification(null, 1)); 164 } 165 166 @Test 167 public void notifyWithTag_shouldReturnNullForUnknownTag() throws Exception { 168 notificationManager.notify("a tag", 1, notification1); 169 assertEquals(1, shadowOf(notificationManager).size()); 170 assertNull(shadowOf(notificationManager).getNotification("unknown tag", 1)); 171 } 172 173 @Test 174 public void testCancel() throws Exception { 175 notificationManager.notify(1, notification1); 176 notificationManager.cancel(1); 177 178 assertEquals(0, shadowOf(notificationManager).size()); 179 assertNull(shadowOf(notificationManager).getNotification(null, 1)); 180 } 181 182 @Test 183 public void testCancelWithTag() throws Exception { 184 notificationManager.notify("a tag", 1, notification1); 185 notificationManager.cancel("a tag", 1); 186 187 assertEquals(0, shadowOf(notificationManager).size()); 188 assertNull(shadowOf(notificationManager).getNotification(null, 1)); 189 assertNull(shadowOf(notificationManager).getNotification("a tag", 1)); 190 } 191 192 @Test 193 public void testCancelAll() throws Exception { 194 notificationManager.notify(1, notification1); 195 notificationManager.notify(31, notification2); 196 notificationManager.cancelAll(); 197 198 assertEquals(0, shadowOf(notificationManager).size()); 199 assertNull(shadowOf(notificationManager).getNotification(null, 1)); 200 assertNull(shadowOf(notificationManager).getNotification(null, 31)); 201 } 202 203 @Test 204 @Config(minSdk = Build.VERSION_CODES.M) 205 public void testGetActiveNotifications() throws Exception { 206 notificationManager.notify(1, notification1); 207 notificationManager.notify(31, notification2); 208 209 StatusBarNotification[] statusBarNotifications = 210 shadowOf(notificationManager).getActiveNotifications(); 211 assertThat(statusBarNotifications) 212 .extractingResultOf("getNotification", Notification.class) 213 .containsOnly(notification1, notification2); 214 } 215 } 216