package org.robolectric.shadows;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.TestRunners;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.robolectric.Shadows.shadowOf;

@RunWith(TestRunners.MultiApiWithDefaults.class)
public class ShadowNotificationManagerTest {
  private NotificationManager notificationManager;
  private Notification notification1 = new Notification();
  private Notification notification2 = new Notification();

  @Before public void setUp() {
    notificationManager = (NotificationManager) RuntimeEnvironment.application.getSystemService(Context.NOTIFICATION_SERVICE);
  }

  @Test
  public void testNotify() throws Exception {
    notificationManager.notify(1, notification1);
    assertEquals(1, shadowOf(notificationManager).size());
    assertEquals(notification1, shadowOf(notificationManager).getNotification(null, 1));

    notificationManager.notify(31, notification2);
    assertEquals(2, shadowOf(notificationManager).size());
    assertEquals(notification2, shadowOf(notificationManager).getNotification(null, 31));
  }

  @Test
  public void testNotifyReplaces() throws Exception {
    notificationManager.notify(1, notification1);

    notificationManager.notify(1, notification2);
    assertEquals(1, shadowOf(notificationManager).size());
    assertEquals(notification2, shadowOf(notificationManager).getNotification(null, 1));
  }

  @Test
  public void testNotifyWithTag() throws Exception {
    notificationManager.notify("a tag", 1, notification1);
    assertEquals(1, shadowOf(notificationManager).size());
    assertEquals(notification1, shadowOf(notificationManager).getNotification("a tag", 1));
  }

  @Test
  public void notifyWithTag_shouldReturnNullForNullTag() throws Exception {
    notificationManager.notify("a tag", 1, notification1);
    assertEquals(1, shadowOf(notificationManager).size());
    assertNull(shadowOf(notificationManager).getNotification(null, 1));
  }

  @Test
  public void notifyWithTag_shouldReturnNullForUnknownTag() throws Exception {
    notificationManager.notify("a tag", 1, notification1);
    assertEquals(1, shadowOf(notificationManager).size());
    assertNull(shadowOf(notificationManager).getNotification("unknown tag", 1));
  }

  @Test
  public void testCancel() throws Exception {
    notificationManager.notify(1, notification1);
    notificationManager.cancel(1);

    assertEquals(0, shadowOf(notificationManager).size());
    assertNull(shadowOf(notificationManager).getNotification(null, 1));
  }

  @Test
  public void testCancelWithTag() throws Exception {
    notificationManager.notify("a tag", 1, notification1);
    notificationManager.cancel("a tag", 1);

    assertEquals(0, shadowOf(notificationManager).size());
    assertNull(shadowOf(notificationManager).getNotification(null, 1));
    assertNull(shadowOf(notificationManager).getNotification("a tag", 1));
  }

  @Test
  public void testCancelAll() throws Exception {
    notificationManager.notify(1, notification1);
    notificationManager.notify(31, notification2);
    notificationManager.cancelAll();

    assertEquals(0, shadowOf(notificationManager).size());
    assertNull(shadowOf(notificationManager).getNotification(null, 1));
    assertNull(shadowOf(notificationManager).getNotification(null, 31));
  }
}

@@ -100,16 +100,100 @@
 anager;%0A
+import android.os.Build;%0Aimport android.service.notification.StatusBarNotification;%0A
 import a
@@ -465,16 +465,96 @@
 rtNull;%0A
+import static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail;%0A
 import s
@@ -3697,11 +3697,1057 @@
 1));%0A  %7D
+%0A%0A  @Test%0A  public void testGetActiveNotifications() throws Exception %7B%0A    if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.M) %7B%0A      // This API was added in M.%0A      return;%0A    %7D%0A    notificationManager.notify(1, notification1);%0A    notificationManager.notify(31, notification2);%0A%0A    StatusBarNotification%5B%5D statusBarNotifications =%0A        shadowOf(notificationManager).getActiveNotifications();%0A    assertEquals(2, statusBarNotifications.length);%0A    boolean hasNotification1 = false;%0A    boolean hasNotification2 = false;%0A    for (StatusBarNotification notification : statusBarNotifications) %7B%0A      if (notification.getId() == 1) %7B%0A        hasNotification1 = true;%0A        assertEquals(notification1, notification.getNotification());%0A      %7D else if (notification.getId() == 31) %7B%0A        hasNotification2 = true;%0A        assertEquals(notification2, notification.getNotification());%0A      %7D else %7B%0A        fail(%22Unexpected notification id %22 + notification.getId());%0A      %7D%0A    %7D%0A    assertTrue(hasNotification1 && hasNotification2);%0A  %7D
 %0A%7D%0A

Add unit test for NotificationManager#getActiveNotifications().