package com.njackson.test.state;

import android.content.SharedPreferences;
import android.test.AndroidTestCase;

import com.njackson.Constants;
import com.njackson.state.GPSDataStore;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.internal.verification.VerificationModeFactory.times;

/**
 * Created by njackson on 30/01/15.
 */
public class GPSDataStoreTest extends AndroidTestCase {

    SharedPreferences _mockPreferences;
    SharedPreferences.Editor _mockEditor;

    @Override
    public void setUp() throws Exception {
        super.setUp();

        _mockPreferences = mock(SharedPreferences.class);
        _mockEditor = mock(SharedPreferences.Editor.class);

        when(_mockPreferences.edit()).thenReturn(_mockEditor);

        when(_mockPreferences.getString("UNITS_OF_MEASURE", "" + Constants.METRIC)).thenReturn("11");
        when(_mockPreferences.getLong("GPS_LAST_START", 0)).thenReturn(12l);
        when(_mockPreferences.getFloat("GPS_DISTANCE",0)).thenReturn(13f);
        when(_mockPreferences.getLong("GPS_ELAPSEDTIME",0)).thenReturn(14l);
        when(_mockPreferences.getFloat("GPS_ASCENT",0)).thenReturn(15f);
        when(_mockPreferences.getFloat("GEOID_HEIGHT",0)).thenReturn(16f);
        when(_mockPreferences.getFloat("GPS_FIRST_LOCATION_LAT",0)).thenReturn(17f);
        when(_mockPreferences.getFloat("GPS_FIRST_LOCATION_LON",0)).thenReturn(18f);
    }

    public void testLoadsUnitsFromPreferencesOnStart() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        assertEquals(11, store.getMeasurementUnits());
    }

    public void testLoadsStartTimeFromPreferencesOnStart() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        assertEquals(12l, store.getStartTime());
    }

    public void testLoadsDistanceFromPreferencesOnStart() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        assertEquals(13f, store.getDistance());
    }

    public void testLoadsElapsedTimeFromPreferencesOnStart() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        assertEquals(14l, store.getElapsedTime());
    }

    public void testLoadsAscentFromPreferencesOnStart() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        assertEquals(15f, store.getAscent());
    }

    public void testLoadsGEOIDFromPreferencesOnStart() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        assertEquals(16f, store.getGEOIDHeight());
    }

    public void testLoadsLattitudeFromPreferencesOnStart() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        assertEquals(17f, store.getFirstLocationLattitude());
    }

    public void testLoadsLongitudeFromPreferencesOnStart() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        assertEquals(18f, store.getFirstLocationLongitude());
    }

    public void testResetsDataOnReset() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);

        store.resetAllValues();

        assertEquals(0l, store.getStartTime());
        assertEquals(0f, store.getDistance());
        assertEquals(0l, store.getElapsedTime());
        assertEquals(0f, store.getAscent());
        assertEquals(0f, store.getGEOIDHeight());
        assertEquals(0f, store.getFirstLocationLattitude());
        assertEquals(0f, store.getFirstLocationLongitude());
    }

    public void testSavesStartTimeToPreferencesOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.setStartTime(22l);
        store.commit();

        verify(_mockEditor,times(1)).putLong("GPS_LAST_START",22l);
    }

    public void testSavesDistanceToPreferencesOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.setDistance(23f);
        store.commit();

        verify(_mockEditor,times(1)).putFloat("GPS_DISTANCE", 23f);
    }

    public void testSavesElapsedTimeToPreferencesOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.setElapsedTime(24l);
        store.commit();

        verify(_mockEditor,times(1)).putLong("GPS_ELAPSEDTIME", 24l);
    }

    public void testSavesAscentToPreferencesOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.setAscent(25f);
        store.commit();

        verify(_mockEditor,times(1)).putFloat("GPS_ASCENT", 25f);
    }

    public void testSavesGeoidToPreferencesOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.setGEOIDHeight(26f);
        store.commit();

        verify(_mockEditor,times(1)).putFloat("GEOID_HEIGHT",26f);
    }

    public void testSavesLattitudeToPreferencesOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.setFirstLocationLattitude(27f);
        store.commit();

        verify(_mockEditor,times(1)).putFloat("GPS_FIRST_LOCATION_LAT",27f);
    }

    public void testSavesLongitudeToPreferencesOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.setFirstLocationLongitude(28f);
        store.commit();

        verify(_mockEditor,times(1)).putFloat("GPS_FIRST_LOCATION_LON",28f);
    }

    public void testSavesUnitsToPreferencesOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.setMeasurementUnits(29);
        store.commit();

        verify(_mockEditor,times(1)).putString("UNITS_OF_MEASURE", "29");
    }

    public void testCallsEditorCommitOnCommit() {
        GPSDataStore store = new GPSDataStore(_mockPreferences);
        store.commit();

        verify(_mockEditor,times(1)).commit();
    }
}

@@ -3112,32 +3112,34 @@
 ues();%0A%0A        
+//
 assertEquals(0l,
@@ -3304,32 +3304,34 @@
 ent());%0A        
+//
 assertEquals(0f,
@@ -3356,32 +3356,34 @@
 ght());%0A        
+//
 assertEquals(0f,
@@ -3419,32 +3419,34 @@
 ude());%0A        
+//
 assertEquals(0f,

Fix test