package com.novoda.downloadmanager.lib;

import android.app.Notification;
import android.app.Service;
import android.support.v4.util.SimpleArrayMap;

import com.novoda.downloadmanager.notifications.NotificationTag;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import static com.novoda.downloadmanager.notifications.DownloadNotifier.TYPE_ACTIVE;
import static com.novoda.downloadmanager.notifications.DownloadNotifier.TYPE_FAILED;
import static com.novoda.downloadmanager.notifications.NotificationTagFixtures.aTag;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;

public class NotificationCreatedListenerTest {

    private static final boolean KEEP_NOTIFICATION = false;

    @Mock
    private Service service;
    @Mock
    private Notification activeDownloadNotification;
    @Mock
    private Notification failedDownloadNotification;

    private final NotificationTag activeDownloadTag = aTag().withStatus(TYPE_ACTIVE).build();

    private final NotificationTag failedDownloadTag = aTag().withStatus(TYPE_FAILED).build();

    private NotificationCreatedListener listener;

    @Before
    public void setUp() {
        initMocks(this);
        listener = new NotificationCreatedListener(service);
    }

    @Test
    public void givenActiveDownload_whenNotificationCreated_thenBringsServiceToForeground() {
        SimpleArrayMap<NotificationTag, Notification> taggedNotifications = new SimpleArrayMap<>();
        taggedNotifications.put(activeDownloadTag, activeDownloadNotification);

        listener.onNotificationCreated(taggedNotifications);

        verify(service).startForeground(activeDownloadTag.hashCode(), activeDownloadNotification);
    }

    @Test
    public void givenFailedDownload_whenNotificationCreated_thenPutsServiceInBackground() {
        SimpleArrayMap<NotificationTag, Notification> taggedNotifications = new SimpleArrayMap<>();
        taggedNotifications.put(failedDownloadTag, failedDownloadNotification);

        listener.onNotificationCreated(taggedNotifications);

        verify(service).stopForeground(KEEP_NOTIFICATION);
    }
}

@@ -359,104 +359,9 @@
 ier.
-TYPE_ACTIVE;%0Aimport static com.novoda.downloadmanager.notifications.DownloadNotifier.TYPE_FAILED
+*
 ;%0Aim
@@ -649,16 +649,544 @@
 = false;
+%0A    private static final NotificationTag ACTIVE_DOWNLOAD_TAG = aTag().withStatus(TYPE_ACTIVE).build();%0A    private static final NotificationTag FAILED_DOWNLOAD_TAG = aTag().withStatus(TYPE_FAILED).build();%0A    private static final NotificationTag SUCCESSFUL_DOWNLOAD_TAG = aTag().withStatus(TYPE_SUCCESS).build();%0A    private static final NotificationTag CANCELLED_DOWNLOAD_TAG = aTag().withStatus(TYPE_CANCELLED).build();%0A    private static final NotificationTag WAITING_DOWNLOAD_TAG = aTag().withStatus(TYPE_WAITING).build();
 %0A%0A    @M
@@ -1218,16 +1218,17 @@
 ervice;%0A
+%0A
     @Moc
@@ -1345,16 +1345,25 @@
 cation;%0A
+    @Mock
 %0A    pri
@@ -1359,38 +1359,32 @@
 ock%0A    private 
-final 
 NotificationTag 
@@ -1383,72 +1383,50 @@
 tion
-Tag activeDownloadTag = aTag().withStatus(TYPE_ACTIVE).build();%0A
+ successfulDownloadNotification;%0A    @Mock
 %0A   
@@ -1434,22 +1434,16 @@
 private 
-final 
 Notifica
@@ -1450,15 +1450,15 @@
 tion
-Tag fai
+ cancel
 ledD
@@ -1468,52 +1468,84 @@
 load
-Tag = aTag().withStatus(TYPE_FAILED).build()
+Notification;%0A    @Mock%0A    private Notification waitingDownloadNotification
 ;%0A%0A 
@@ -1909,40 +1909,40 @@
 s = 
-new SimpleArrayMap%3C%3E
+givenActiveDownload
 ();%0A
+%0A
         
 tagg
@@ -1941,52 +1941,204 @@
     
-taggedNotifications.put(activeDownloadTag, a
+listener.onNotificationCreated(taggedNotifications);%0A%0A        verify(service).startForeground(ACTIVE_DOWNLOAD_TAG.hashCode(), activeDownloadNotification);%0A    %7D%0A%0A    @Test%0A    public void givenNoA
 ctiv
@@ -2138,32 +2138,37 @@
 NoActiveDownload
+_when
 Notification);%0A%0A
@@ -2155,32 +2155,170 @@
 whenNotification
+Created_thenPutsServiceInBackground() %7B%0A        SimpleArrayMap%3CNotificationTag, Notification%3E taggedNotifications = givenNoActiveDownload(
 );%0A%0A        list
@@ -2385,35 +2385,34 @@
 rify(service).st
-art
+op
 Foreground(activ
@@ -2410,36 +2410,269 @@
 und(
-activeDownloadTag.hashCode()
+KEEP_NOTIFICATION);%0A    %7D%0A%0A    private SimpleArrayMap%3CNotificationTag, Notification%3E givenActiveDownload() %7B%0A        SimpleArrayMap%3CNotificationTag, Notification%3E taggedNotifications = givenNoActiveDownload();%0A        taggedNotifications.put(ACTIVE_DOWNLOAD_TAG
 , ac
@@ -2706,107 +2706,125 @@
     
-%7D%0A%0A
     
-@Test%0A    public void givenFailedDownload_whenNotificationCreated_thenPutsServiceInBackgroun
+return taggedNotifications;%0A    %7D%0A%0A    private SimpleArrayMap%3CNotificationTag, Notification%3E givenNoActiveDownloa
 d() 
@@ -2961,33 +2961,125 @@
 put(
-failedDownloadTag, failed
+FAILED_DOWNLOAD_TAG, failedDownloadNotification);%0A        taggedNotifications.put(SUCCESSFUL_DOWNLOAD_TAG, successful
 Down
@@ -3097,25 +3097,24 @@
 ation);%0A
-%0A
         
 listener
@@ -3101,35 +3101,30 @@
 n);%0A        
-listener.on
+tagged
 Notification
@@ -3119,37 +3119,70 @@
 Notification
-Created(tagge
+s.put(CANCELLED_DOWNLOAD_TAG, cancelledDownloa
 dNotificatio
@@ -3178,37 +3178,35 @@
 Notification
-s
 );%0A
-%0A
         
 verify(servi
@@ -3197,57 +3197,118 @@
     
-verify(service).stopForeground(KEEP_NOTIFICATION)
+taggedNotifications.put(WAITING_DOWNLOAD_TAG, waitingDownloadNotification);%0A        return taggedNotifications
 ;%0A  

Add test cases