package com.novoda.downloadmanager.notifications;

import android.content.Context;

import com.novoda.downloadmanager.lib.DownloadBatch;

import java.util.ArrayList;
import java.util.Collection;

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

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

public class DownloadNotifierTest {

    @Mock
    private NotificationDisplayer mockNotificationDisplayer;
    @Mock
    private Context mockContext;
    private DownloadNotifier downloadNotifier;

    @Before
    public void setUp() {
        initMocks(this);
        downloadNotifier = new SynchronisedDownloadNotifier(mockContext, mockNotificationDisplayer);
    }

    @Test
    public void whenRemovingStaleNotificationsThenItDoesNotCrash() {
        Collection<DownloadBatch> batches = createDownloadBatches();

        Collection<DownloadBatch> updatedBatches = new ArrayList<>();
        DownloadBatch batchQueuedForWifiUpdated = getQueuedForWifiDownloadBatch();
        updatedBatches.add(batchQueuedForWifiUpdated);

        downloadNotifier.updateWith(batches, notificationNotifier);
        downloadNotifier.updateWith(updatedBatches, notificationNotifier);
    }

    private Collection<DownloadBatch> createDownloadBatches() {
        Collection<DownloadBatch> batches = new ArrayList<>();

        DownloadBatch batchRunning = getRunningDownloadBatch();

        batches.add(getQueuedForWifiDownloadBatch());
        batches.add(getQueuedForWifiDownloadBatch());
        batches.add(getQueuedForWifiDownloadBatch());
        batches.add(getQueuedForWifiDownloadBatch());
        batches.add(getQueuedForWifiDownloadBatch());
        batches.add(getQueuedForWifiDownloadBatch());

        batches.add(batchRunning);

        return batches;
    }

    private DownloadBatch getQueuedForWifiDownloadBatch() {
        DownloadBatch downloadBatch = mock(DownloadBatch.class);
        when(downloadBatch.shouldShowActiveItem()).thenReturn(true);
        when(downloadBatch.isQueuedForWifi()).thenReturn(true);
        return downloadBatch;
    }

    private DownloadBatch getRunningDownloadBatch() {
        DownloadBatch downloadBatch = mock(DownloadBatch.class);
        when(downloadBatch.shouldShowActiveItem()).thenReturn(true);
        when(downloadBatch.isRunning()).thenReturn(true);
        return downloadBatch;
    }
}

@@ -63,23 +63,103 @@
 oid.
-content.Context
+app.Notification;%0Aimport android.content.Context;%0Aimport android.support.v4.util.SimpleArrayMap
 ;%0A%0Ai
@@ -517,16 +517,312 @@
 Test %7B%0A%0A
+    private final DownloadNotifier.NotificationsCreatedCallback notificationsCreatedCallback = new DownloadNotifier.NotificationsCreatedCallback() %7B%0A        @Override%0A        public void onNotificationCreated(SimpleArrayMap%3CNotificationTag, Notification%3E taggedNotifications) %7B%0A%0A        %7D%0A    %7D;%0A
     @Moc
@@ -930,55 +930,8 @@
 ext;
-%0A    private DownloadNotifier downloadNotifier;
 %0A%0A  
@@ -995,109 +995,8 @@
 s);%0A
-        downloadNotifier = new SynchronisedDownloadNotifier(mockContext, mockNotificationDisplayer);%0A
     
@@ -1124,85 +1124,8 @@
 es =
- createDownloadBatches();%0A%0A        Collection%3CDownloadBatch%3E updatedBatches =
  new
@@ -1136,24 +1136,25 @@
 ayList%3C%3E();%0A
+%0A
         Down
@@ -1181,23 +1181,16 @@
 dForWifi
-Updated
  = getQu
@@ -1229,217 +1229,149 @@
     
-updatedBatches.add(batchQueuedForWifiUpdated);%0A%0A        downloadNotifier.updateWith(batches, notificationNotifier);%0A        downloadNotifier.updateWith(updatedBatches, notificationNotifier
+DownloadBatch batchRunning = getRunningDownloadBatch();%0A%0A        batches.add(batchQueuedForWifi);%0A        batches.add(batchRunning
 );%0A
+%0A
     
-%7D%0A%0A    private
+   
  Col
@@ -1397,69 +1397,16 @@
 ch%3E 
-createDownloadBatches() %7B%0A        Collection%3CDownloadBatch%3E b
+updatedB
 atch
@@ -1421,33 +1421,32 @@
  ArrayList%3C%3E();%0A
-%0A
         Download
@@ -1460,66 +1460,31 @@
 atch
-Running = getRunningDownloadBatch();%0A%0A        batches.add(
+QueuedForWifiUpdated = 
 getQ
@@ -1502,33 +1502,32 @@
 iDownloadBatch()
-)
 ;%0A        batche
@@ -1512,33 +1512,40 @@
 atch();%0A        
-b
+updatedB
 atches.add(getQu
@@ -1531,35 +1531,37 @@
 atedBatches.add(
-get
+batch
 QueuedForWifiDow
@@ -1561,34 +1561,27 @@
 Wifi
-DownloadBatch()
+Updated
 );%0A
+%0A
         
 batc
@@ -1580,225 +1580,202 @@
     
-batches.add(getQueuedForWifiDownloadBatch());%0A        batches.add(getQueuedForWifiDownloadBatch());%0A        batches.add(getQueuedForWifiDownloadBatch());%0A        batches.add(getQueuedForWifiDownloadBatch()
+DownloadNotifier downloadNotifier = new SynchronisedDownloadNotifier(mockContext, mockNotificationDisplayer);%0A        downloadNotifier.updateWith(batches, notificationsCreatedCallback
 );%0A
-%0A
         
 batc
@@ -1774,58 +1774,81 @@
     
-batches.add(batchRunning);%0A%0A        return batches
+downloadNotifier.updateWith(updatedBatches, notificationsCreatedCallback)
 ;%0A  

Update tests