// -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; -*-
// vim: et ts=4 sts=4 sw=4 syntax=java
package com.aragaer.jtt;

import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.*;

import java.util.LinkedList;

import android.app.Service;
import android.content.*;
import android.preference.PreferenceManager;
import android.os.Handler;
import android.util.Log;

import org.junit.*;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.core.classloader.annotations.PrepareForTest;

import com.aragaer.jtt.mechanics.AndroidTicker;


@RunWith(PowerMockRunner.class)
@PrepareForTest({AndroidTicker.class, Log.class, Service.class,
            PreferenceManager.class})
public class JttServiceTest {

    private static Context mockContext = mock(Context.class);
    private static SharedPreferences mockPref = mock(SharedPreferences.class);

    private TestJttService service;

    @Before public void setUp() throws Exception {
        mockStatic(Log.class);
        mockStatic(PreferenceManager.class);
        when(PreferenceManager.getDefaultSharedPreferences(any(Context.class)))
            .thenReturn(mockPref);
        when(mockPref.getString(Settings.PREF_LOCATION, "0.0:0.0"))
            .thenReturn("0.0:0.0");
        suppress(method(Handler.class, "sendEmptyMessage"));
        suppress(method(Service.class, "onCreate"));

        service = spy(new TestJttService());
    }

    @Test public void testCreate() {
        service.onCreate();
        verify((Service) service).onCreate();
        assertEquals("Two receivers registered in onCreate",
                     2, service.receivers.size());
    }

    @Test public void testStart() {
        service.onCreate();
        service.resetReceivers();
        int result = service.onStartCommand(null, 0, 0);
        assertEquals("onStartCommand returns START_STICKY",
                     Service.START_STICKY, result);
        assertEquals("No receivers registered in onStartCommand",
                     0, service.receivers.size());
    }

    private class TestJttService extends JttService {
        LinkedList<BroadcastReceiver> receivers
            = new LinkedList<BroadcastReceiver>();
        LinkedList<IntentFilter> filters = new LinkedList<IntentFilter>();

        @Override public Intent registerReceiver(BroadcastReceiver receiver,
                                                 IntentFilter intentFilter) {
            receivers.add(receiver);
            filters.add(intentFilter);
            return null;
        }

        public void resetReceivers() {
            receivers.clear();
            filters.clear();
        }
    }
}

@@ -194,50 +194,8 @@
 .*;%0A
-import static org.mockito.Mockito.verify;%0A
 impo
@@ -1479,12 +1479,8 @@
 e = 
-spy(
 new 
@@ -1495,17 +1495,16 @@
 ervice()
-)
 ;%0A    %7D%0A
@@ -1573,54 +1573,8 @@
 ();%0A
-        verify((Service) service).onCreate();%0A
     
@@ -2079,28 +2079,19 @@
 %0A    %7D%0A%0A
-    private 
+%7D%0A%0A
 class Te
@@ -2124,28 +2124,24 @@
 rvice %7B%0A    
- 
-   
 LinkedList%3CB
@@ -2167,28 +2167,16 @@
 eceivers
-%0A           
  = new L
@@ -2203,28 +2203,24 @@
 eceiver%3E();%0A
-    
     LinkedLi
@@ -2283,20 +2283,16 @@
 );%0A%0A    
- 
-   
 @Overrid
@@ -2397,20 +2397,16 @@
         
-  
-  
 IntentFi
@@ -2426,36 +2426,32 @@
 lter) %7B%0A        
-    
 receivers.add(re
@@ -2455,28 +2455,24 @@
 (receiver);%0A
-    
         filt
@@ -2502,20 +2502,16 @@
         
-    
 return n
@@ -2515,20 +2515,16 @@
 n null;%0A
-    
     %7D%0A%0A 
@@ -2526,20 +2526,16 @@
  %7D%0A%0A    
-  
-  
 public v
@@ -2561,28 +2561,24 @@
 ) %7B%0A        
-    
 receivers.cl
@@ -2580,28 +2580,24 @@
 rs.clear();%0A
-    
         filt
@@ -2613,18 +2613,8 @@
 ();%0A
-        %7D%0A
     

Fix coverage report.