package com.novoda.downloadmanager.notifications;

import android.content.Context;

import com.novoda.downloadmanager.lib.BatchInfo;
import com.novoda.downloadmanager.lib.DownloadBatch;
import com.novoda.downloadmanager.lib.DownloadStatus;
import com.novoda.downloadmanager.lib.FileDownloadInfo;
import com.novoda.downloadmanager.notifications.DownloadNotifier;
import com.novoda.downloadmanager.notifications.NotificationDisplayer;
import com.novoda.downloadmanager.notifications.NotificationVisibility;

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

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

import static org.mockito.MockitoAnnotations.initMocks;

public class DownloadNotifierTest {

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

    @Before
    public void setUp() {
        initMocks(this);
    }

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

        DownloadBatch batchQueuedForWifi = getDownloadBatchWith(196); // Queued for Wifi
        DownloadBatch batchRunning = getDownloadBatchWith(192); // Running

        batches.add(batchQueuedForWifi);
        batches.add(batchRunning);

        Collection<DownloadBatch> updatedBatches = new ArrayList<>();
        DownloadBatch batchQueuedForWifiUpdated = getDownloadBatchWith(192); // Queued for Wifi
        updatedBatches.add(batchQueuedForWifiUpdated);

        DownloadNotifier downloadNotifier = new SynchronisedDownloadNotifier(mockContext, mockNotificationDisplayer);
        downloadNotifier.updateWith(batches);
        downloadNotifier.updateWith(updatedBatches);
    }

    private DownloadBatch getDownloadBatchWith(int status) {
        return new DownloadBatch(
                1,
                new BatchInfo("", "", "", NotificationVisibility.ACTIVE_OR_COMPLETE, ""),
                new ArrayList<FileDownloadInfo>(),
                status,
                1,
                1);
    }
}

@@ -119,389 +119,21 @@
 lib.
-BatchInfo;%0Aimport com.novoda.downloadmanager.lib.DownloadBatch;%0Aimport com.novoda.downloadmanager.lib.DownloadStatus;%0Aimport com.novoda.downloadmanager.lib.FileDownloadInfo;%0Aimport com.novoda.downloadmanager.notifications.DownloadNotifier;%0Aimport com.novoda.downloadmanager.notifications.NotificationDisplayer;%0Aimport com.novoda.downloadmanager.notifications.NotificationVisibility
+DownloadBatch
 ;%0A%0Ai
@@ -263,16 +263,96 @@
 .Mock;%0A%0A
+import static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;%0A
 import s
@@ -815,50 +815,37 @@
  get
-DownloadBatchWith(196); // Queued for Wifi
+QueuedForWifiDownloadBatch();
 %0A   
@@ -877,24 +877,31 @@
 unning = get
+Running
 DownloadBatc
@@ -905,29 +905,11 @@
 atch
-With(192); // Running
+();
 %0A%0A  
@@ -1110,50 +1110,37 @@
  get
-DownloadBatchWith(192); // Queued for Wifi
+QueuedForWifiDownloadBatch();
 %0A   
@@ -1449,59 +1449,82 @@
  get
-DownloadBatchWith(int status) %7B%0A        return new 
+QueuedForWifiDownloadBatch() %7B%0A        DownloadBatch downloadBatch = mock(
 Down
@@ -1532,17 +1532,24 @@
 oadBatch
-(
+.class);
 %0A       
@@ -1553,221 +1553,444 @@
     
-        1,%0A                new BatchInfo(%22%22, %22%22, %22%22, NotificationVisibility.ACTIVE_OR_COMPLETE, %22%22),%0A                new ArrayList%3CFileDownloadInfo%3E(),%0A                status,%0A                1,%0A                1)
+when(downloadBatch.shouldShowActiveItem()).thenReturn(true);%0A        when(downloadBatch.isQueuedForWifi()).thenReturn(true);%0A        return downloadBatch;%0A    %7D%0A%0A    private DownloadBatch getRunningDownloadBatch() %7B%0A        DownloadBatch downloadBatch = mock(DownloadBatch.class);%0A        when(downloadBatch.shouldShowActiveItem()).thenReturn(true);%0A        when(downloadBatch.isRunning()).thenReturn(true);%0A        return downloadBatch
 ;%0A  

use the power of mocks to alleviate all pains