package kr.co.zumo.app.lifeplus.supervisor;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import org.junit.Before;
import org.junit.Test;

import kr.co.zumo.app.lifeplus.view.fragment.factory.FragmentFactory;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class FragmentChangerTest {

  FragmentChanger fragmentChanger;

  @Before
  public void setUp() throws Exception {

  }

  @Test
  public void changeTo() {

    FragmentFactory factory = mock(FragmentFactory.class);
    AppCompatActivity activity = spy(AppCompatActivity.class);
    FragmentManager fragmentManager = spy(FragmentManager.class);
    FragmentTransaction fragmentTransaction = mock(FragmentTransaction.class);
    when(activity.getSupportFragmentManager()).thenReturn(fragmentManager);
    when(fragmentManager.beginTransaction()).thenReturn(fragmentTransaction);

    fragmentChanger = new FragmentChanger(activity, 1, factory);

    int id = ScreenID.SIGN_UP_PIN;
    fragmentChanger.changeTo(id);

    verify(factory).getFragment(eq(id));
    verify(fragmentTransaction).commitAllowingStateLoss();

  }

  @Test
  public void changeToNext() {
    FragmentFactory factory = mock(FragmentFactory.class);
    AppCompatActivity activity = spy(AppCompatActivity.class);
    FragmentManager fragmentManager = spy(FragmentManager.class);
    FragmentTransaction fragmentTransaction = mock(FragmentTransaction.class);
    when(activity.getSupportFragmentManager()).thenReturn(fragmentManager);
    when(fragmentManager.beginTransaction()).thenReturn(fragmentTransaction);

    /**
     * spy() - 메서드의 내부 메서드로 사용 가능,
     * mock() - 메서드의 몸통은 비어있다. changeFragment(id) -> changeFragment(id, tran) 호출 안됨.
     */
    fragmentChanger = spy(new FragmentChanger(activity, 1, factory));

    int id = ScreenID.SIGN_UP_PIN;
    fragmentChanger.changeToNext(id);

    verify(factory).getFragment(eq(id));
    verify(fragmentTransaction).commitAllowingStateLoss();

  }

  @Test
  public void changeToBack() {

    FragmentFactory factory = mock(FragmentFactory.class);
    AppCompatActivity activity = spy(AppCompatActivity.class);
    FragmentManager fragmentManager = spy(FragmentManager.class);
    FragmentTransaction fragmentTransaction = mock(FragmentTransaction.class);
    when(activity.getSupportFragmentManager()).thenReturn(fragmentManager);
    when(fragmentManager.beginTransaction()).thenReturn(fragmentTransaction);

    fragmentChanger = new FragmentChanger(activity, 1, factory);

    int id = ScreenID.SIGN_UP_PIN;
    fragmentChanger.changeToBack(id);

    verify(factory).getFragment(eq(id));
    verify(fragmentTransaction).commitAllowingStateLoss();
  }

}