package com.example.ericliu.weather2016.ui.presenter;

import com.example.ericliu.weather2016.framework.mvp.RequestStatus;
import com.example.ericliu.weather2016.ui.viewmodel.MainActivityViewModel;

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

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by ericliu on 22/05/2016.
 */
public class MainActivityPresenterTest {

    @Mock
    private MainActivityPresenter.HomepageCallbacks mHomepageCallbacks;


    @Mock
    private MainActivityViewModel mViewModel;

    private MainActivityPresenter mPresenter;
    private String city = "Adelaide";


    @Before
    public void setUp() throws Exception {

        // Mockito has a very convenient way to inject mocks by using the @Mock annotation. To
        // inject the mocks in the test the initMocks method needs to be called.
        MockitoAnnotations.initMocks(this);



        mPresenter = new MainActivityPresenter(0, mHomepageCallbacks, mViewModel);

    }

    @After
    public void tearDown() throws Exception {
        mPresenter = null;

    }

    @Test
    public void testOnUpdateComplete() throws Exception {
        when(mViewModel.getRequestStatus(MainActivityViewModel.QueryEnumMainActivity.UPDATE_WEATHER)).thenReturn(RequestStatus.SUCESS);
        when(mViewModel.getCity()).thenReturn(city);

        mPresenter.onUpdateComplete(mViewModel, MainActivityViewModel.QueryEnumMainActivity.UPDATE_WEATHER);

        verify(mHomepageCallbacks).showCityName(city);

    }

    @Test
    public void testOnSearchButtonClicked() throws Exception {

        mPresenter.onSearchButtonClicked("Sydney");

        verify(mHomepageCallbacks).showProgressBar();
    }
}
@@ -48,16 +48,59 @@
 enter;%0A%0A
+import android.content.SharedPreferences;%0A%0A
 import c
@@ -159,16 +159,16 @@
 Status;%0A
-
 import c
@@ -368,24 +368,73 @@
 notations;%0A%0A
+import static junit.framework.Assert.assertTrue;%0A
 import stati
@@ -587,24 +587,89 @@
 nterTest %7B%0A%0A
+    private static final String PREF_IS_HAPPY = %22pref.is.happy%22;%0A
     @Mock%0A  
@@ -736,24 +736,77 @@
 allbacks;%0A%0A%0A
+    @Mock%0A    SharedPreferences mSharedPreferences;%0A%0A
     @Mock%0A  
@@ -1213,18 +1213,16 @@
 this);%0A%0A
-%0A%0A
         
@@ -1819,32 +1819,325 @@
 (city);%0A%0A    %7D%0A%0A
+    @Test%0A    public void testPreferences()%7B%0A        when(mSharedPreferences.getBoolean(PREF_IS_HAPPY, false)).thenReturn(true);%0A%0A        boolean isHappy = mSharedPreferences.getBoolean(PREF_IS_HAPPY, false);%0A        assertTrue(%22The shared preference value should be false.%22, isHappy);%0A    %7D%0A%0A
     @Test%0A    pu

test with sharedPreferene value