107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinpackage com.xtremelabs.robolectric.shadows; 207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport android.app.Notification; 407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport android.app.NotificationManager; 507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport android.content.Context; 607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport com.xtremelabs.robolectric.Robolectric; 707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport com.xtremelabs.robolectric.WithTestDefaultsRunner; 807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport org.junit.Before; 907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport org.junit.Test; 1007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport org.junit.runner.RunWith; 1107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 1207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport static com.xtremelabs.robolectric.Robolectric.shadowOf; 1307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport static org.junit.Assert.assertEquals; 1407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinimport static org.junit.Assert.assertNull; 1507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 1607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin@RunWith(WithTestDefaultsRunner.class) 1707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwinpublic class NotificationManagerTest { 1807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin private NotificationManager notificationManager; 1907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin private Notification notification1 = new Notification(); 2007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin private Notification notification2 = new Notification(); 2107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 2207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Before public void setUp() { 2307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager = (NotificationManager) Robolectric.application.getSystemService(Context.NOTIFICATION_SERVICE); 2407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 2507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 2607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Test 2707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin public void testNotify() throws Exception { 2807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify(1, notification1); 2907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(1, shadowOf(notificationManager).size()); 3007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(notification1, shadowOf(notificationManager).getNotification(1)); 3107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 3207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify(31, notification2); 3307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(2, shadowOf(notificationManager).size()); 3407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(notification2, shadowOf(notificationManager).getNotification(31)); 3507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 3607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 3707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Test 3807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin public void testNotifyReplaces() throws Exception { 3907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify(1, notification1); 4007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 4107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify(1, notification2); 4207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(1, shadowOf(notificationManager).size()); 4307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(notification2, shadowOf(notificationManager).getNotification(1)); 4407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 4507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 4607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Test 4707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin public void testNotifyWithTag() throws Exception { 4807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify("a tag", 1, notification1); 4907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(1, shadowOf(notificationManager).size()); 5007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(notification1, shadowOf(notificationManager).getNotification("a tag")); 5107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 5207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 5307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Test 5407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin public void notifyWithTag_shouldReturnNullForNullTag() throws Exception { 5507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify("a tag", 1, notification1); 5607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(1, shadowOf(notificationManager).size()); 5707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertNull(shadowOf(notificationManager).getNotification(null)); 5807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 5907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 6007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Test 6107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin public void notifyWithTag_shouldReturnNullForUnknownTag() throws Exception { 6207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify("a tag", 1, notification1); 6307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(1, shadowOf(notificationManager).size()); 6407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertNull(shadowOf(notificationManager).getNotification("unknown tag")); 6507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 6607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 6707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Test 6807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin public void testCancel() throws Exception { 6907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify(1, notification1); 7007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.cancel(1); 7107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 7207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(0, shadowOf(notificationManager).size()); 7307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertNull(shadowOf(notificationManager).getNotification(1)); 7407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 7507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 7607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Test 7707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin public void testCancelWithTag() throws Exception { 7807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify("a tag", 1, notification1); 7907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.cancel("a tag", 1); 8007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 8107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(0, shadowOf(notificationManager).size()); 8207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertNull(shadowOf(notificationManager).getNotification(1)); 8307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertNull(shadowOf(notificationManager).getNotification("a tag")); 8407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 8507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 8607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin @Test 8707a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin public void testCancelAll() throws Exception { 8807a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify(1, notification1); 8907a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.notify(31, notification2); 9007a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin notificationManager.cancelAll(); 9107a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin 9207a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertEquals(0, shadowOf(notificationManager).size()); 9307a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertNull(shadowOf(notificationManager).getNotification(1)); 9407a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin assertNull(shadowOf(notificationManager).getNotification(31)); 9507a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin } 9607a3254138fdfe62b70c194b458879c51bea72b4Phil Goodwin} 97