package com.njackson.test.hrm;

import com.njackson.application.modules.AndroidModule;
import com.njackson.events.GPSServiceCommand.GPSStatus;
import com.njackson.events.HrmServiceCommand.HrmStatus;
import com.njackson.events.base.BaseStatus;
import com.njackson.hrm.HrmServiceCommand;
import com.njackson.hrm.IHrm;
import com.njackson.test.application.TestApplication;

import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;

import static org.mockito.Mockito.*;

import android.content.SharedPreferences;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.inject.Singleton;

import dagger.Module;
import dagger.ObjectGraph;
import dagger.Provides;

public class HrmServiceCommandTest extends AndroidTestCase {
    private CountDownLatch _stateLatch;

    @Inject Bus _bus;
    @Inject SharedPreferences _mockPreferences;

    private HrmStatus _state;
    private HrmServiceCommand _service;
    private TestApplication _app;

    @Module(
            includes = AndroidModule.class,
            injects = HrmServiceCommandTest.class,
            overrides = true,
            complete = false
    )
    class TestModule {
        @Provides
        @Singleton
        SharedPreferences provideSharedPreferences() {
            return mock(SharedPreferences.class);
        }

        @Provides @Singleton IHrm provideHrm() { return mock(IHrm.class); }
    }
    @Subscribe
    public void onChangeStateEvent(HrmStatus state) {
        _state = state;
        _stateLatch.countDown();
    }
    @Override
    public void setUp() throws Exception {
        super.setUp();

        System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath());

        _app = new TestApplication();
        _app.setObjectGraph(ObjectGraph.create(new TestModule()));
        _app.inject(this);
        _bus.register(this);

        setupMocks();

        _stateLatch = new CountDownLatch(1);
        _service = new HrmServiceCommand();
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }
    private void setupMocks() {
        when(_mockPreferences.getString("hrm_address", "")).thenReturn("12");
    }

    @SmallTest
    public void testOnStartSendsServiceUnableToStartWhenDisable() throws Exception {
        when(_mockPreferences.getString("hrm_address", "")).thenReturn("");
        _service.execute(_app);

        _bus.post(new GPSStatus(BaseStatus.Status.STARTED));
        _stateLatch.await(1000, TimeUnit.MILLISECONDS);

        assertEquals(BaseStatus.Status.UNABLE_TO_START, _state.getStatus());
    }

    @SmallTest
    public void testOnStartSendsServiceStartedWhenEnable() throws Exception {
        when(_mockPreferences.getString("hrm_address", "")).thenReturn("12");
        _service.execute(_app);

        _bus.post(new GPSStatus(BaseStatus.Status.STARTED));
        _stateLatch.await(1000, TimeUnit.MILLISECONDS);

        assertEquals(BaseStatus.Status.STARTED, _state.getStatus());
    }

    @SmallTest
    public void testOnStopSendsServiceStopped() throws Exception {
        when(_mockPreferences.getString("hrm_address", "")).thenReturn("12");
        _service.execute(_app);

        // need to start the service before stopping
        _bus.post(new GPSStatus(BaseStatus.Status.STARTED));
        _stateLatch.await(1000, TimeUnit.MILLISECONDS);

        _stateLatch = new CountDownLatch(1);
        _bus.post(new GPSStatus(BaseStatus.Status.STOPPED));
        _stateLatch.await(1000, TimeUnit.MILLISECONDS);

        assertEquals(BaseStatus.Status.STOPPED, _state.getStatus());
    }
}

@@ -76,24 +76,80 @@
 roidModule;%0A
+import com.njackson.application.modules.ForApplication;%0A
 import com.n
@@ -184,32 +184,32 @@
 mand.GPSStatus;%0A
-
 import com.njack
@@ -525,16 +525,48 @@
 ito.*;%0A%0A
+import android.content.Context;%0A
 import a
@@ -1611,16 +1611,151 @@
 lass); %7D
+%0A%0A        @Provides @Singleton @ForApplication%0A        Context provideApplicationContext() %7B%0A            return getContext();%0A        %7D
 %0A    %7D%0A 
@@ -2536,32 +2536,103 @@
 n(%2212%22);%0A    %7D%0A%0A
+// we no longer start HrmServiceCommand if hrm_address is not setted%0A//
     @SmallTest%0A 
@@ -2622,32 +2622,34 @@
 /    @SmallTest%0A
+//
     public void 
@@ -2709,32 +2709,34 @@
 ows Exception %7B%0A
+//
         when(_mo
@@ -2791,24 +2791,26 @@
 Return(%22%22);%0A
+//
         _ser
@@ -2821,33 +2821,37 @@
 .execute(_app);%0A
-%0A
+//%0A//
         _bus.pos
@@ -2887,32 +2887,34 @@
 atus.STARTED));%0A
+//
         _stateLa
@@ -2945,33 +2945,37 @@
 .MILLISECONDS);%0A
-%0A
+//%0A//
         assertEq
@@ -3027,32 +3027,34 @@
 e.getStatus());%0A
+//
     %7D%0A%0A    @Smal

Fix HRM tests