1 package com.xtremelabs.robolectric.shadows; 2 3 import android.app.Notification; 4 import android.app.NotificationManager; 5 import android.content.Context; 6 import com.xtremelabs.robolectric.Robolectric; 7 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 8 import org.junit.Before; 9 import org.junit.Test; 10 import org.junit.runner.RunWith; 11 12 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 13 import static org.junit.Assert.assertEquals; 14 import static org.junit.Assert.assertNull; 15 16 @RunWith(WithTestDefaultsRunner.class) 17 public class NotificationManagerTest { 18 private NotificationManager notificationManager; 19 private Notification notification1 = new Notification(); 20 private Notification notification2 = new Notification(); 21 22 @Before public void setUp() { 23 notificationManager = (NotificationManager) Robolectric.application.getSystemService(Context.NOTIFICATION_SERVICE); 24 } 25 26 @Test 27 public void testNotify() throws Exception { 28 notificationManager.notify(1, notification1); 29 assertEquals(1, shadowOf(notificationManager).size()); 30 assertEquals(notification1, shadowOf(notificationManager).getNotification(1)); 31 32 notificationManager.notify(31, notification2); 33 assertEquals(2, shadowOf(notificationManager).size()); 34 assertEquals(notification2, shadowOf(notificationManager).getNotification(31)); 35 } 36 37 @Test 38 public void testNotifyReplaces() throws Exception { 39 notificationManager.notify(1, notification1); 40 41 notificationManager.notify(1, notification2); 42 assertEquals(1, shadowOf(notificationManager).size()); 43 assertEquals(notification2, shadowOf(notificationManager).getNotification(1)); 44 } 45 46 @Test 47 public void testNotifyWithTag() throws Exception { 48 notificationManager.notify("a tag", 1, notification1); 49 assertEquals(1, shadowOf(notificationManager).size()); 50 assertEquals(notification1, shadowOf(notificationManager).getNotification("a tag")); 51 } 52 53 @Test 54 public void notifyWithTag_shouldReturnNullForNullTag() throws Exception { 55 notificationManager.notify("a tag", 1, notification1); 56 assertEquals(1, shadowOf(notificationManager).size()); 57 assertNull(shadowOf(notificationManager).getNotification(null)); 58 } 59 60 @Test 61 public void notifyWithTag_shouldReturnNullForUnknownTag() throws Exception { 62 notificationManager.notify("a tag", 1, notification1); 63 assertEquals(1, shadowOf(notificationManager).size()); 64 assertNull(shadowOf(notificationManager).getNotification("unknown tag")); 65 } 66 67 @Test 68 public void testCancel() throws Exception { 69 notificationManager.notify(1, notification1); 70 notificationManager.cancel(1); 71 72 assertEquals(0, shadowOf(notificationManager).size()); 73 assertNull(shadowOf(notificationManager).getNotification(1)); 74 } 75 76 @Test 77 public void testCancelWithTag() throws Exception { 78 notificationManager.notify("a tag", 1, notification1); 79 notificationManager.cancel("a tag", 1); 80 81 assertEquals(0, shadowOf(notificationManager).size()); 82 assertNull(shadowOf(notificationManager).getNotification(1)); 83 assertNull(shadowOf(notificationManager).getNotification("a tag")); 84 } 85 86 @Test 87 public void testCancelAll() throws Exception { 88 notificationManager.notify(1, notification1); 89 notificationManager.notify(31, notification2); 90 notificationManager.cancelAll(); 91 92 assertEquals(0, shadowOf(notificationManager).size()); 93 assertNull(shadowOf(notificationManager).getNotification(1)); 94 assertNull(shadowOf(notificationManager).getNotification(31)); 95 } 96 } 97