package com.r0adkll.slidr.widget;

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.view.MotionEvent;
import android.view.View;

import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrPosition;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * Created by farid on 18/01/16.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({View.class})
public class SliderPanelTest {

    @Mock
    Context context;

    @Mock
    MotionEvent motionEvent;

    @Test
    public void testOnInterceptTouchEvent_whenLocked() throws Exception {
        //given
        SliderPanel sliderPanel = new SliderPanel(context);
        Whitebox.setInternalState(sliderPanel, "mIsLocked", true);

        //when
        boolean result = sliderPanel.onInterceptTouchEvent(motionEvent);

        //then
        assertFalse("Result must be false when isLocked is true", result);
    }


    @Test
    public void testOnInterceptTouchEvent_whenNotLoacked_edgeOnly() throws Exception {
        //given
        SliderPanel sliderPanel = Mockito.spy(new SliderPanel(context));
        PowerMockito.when(sliderPanel, "getWidth").thenReturn(10);

        SlidrConfig slidrConfig = Mockito.mock(SlidrConfig.class);
        Mockito.when(slidrConfig.isEdgeOnly()).thenReturn(true);
        Mockito.when(slidrConfig.getPosition()).thenReturn(SlidrPosition.LEFT);
        Mockito.when(slidrConfig.getEdgeSize(Matchers.anyInt())).thenReturn(10.1f);

        Whitebox.setInternalState(sliderPanel, "mIsLocked", false);
        Whitebox.setInternalState(sliderPanel, "mConfig", slidrConfig);

        //when
        boolean result = sliderPanel.onInterceptTouchEvent(motionEvent);

        //then
        assertFalse("Result must be false", result);
    }

    @Test
    public void testOnTouchEvent_whenLocked() throws Exception {
        //given
        SliderPanel sliderPanel = Mockito.spy(new SliderPanel(context));
        Whitebox.setInternalState(sliderPanel, "mIsLocked", true);

        //when
        boolean result = sliderPanel.onTouchEvent(motionEvent);

        //then
        assertFalse("Result must be false when locked", result);
    }

    @Test
    public void testOnTouchEvent_whenNotLocked() throws Exception {
        //given
        SliderPanel sliderPanel = Mockito.spy(new SliderPanel(context));
        Whitebox.setInternalState(sliderPanel, "mIsLocked", false);

        ViewDragHelper viewDragHelper = Mockito.mock(ViewDragHelper.class);
        Whitebox.setInternalState(sliderPanel, "mDragHelper", viewDragHelper);

        //when
        boolean result = sliderPanel.onTouchEvent(motionEvent);

        //then
        assertTrue("Result must be true when not locked", result);
    }

    @Test
    public void testOnTouchEvent_whenNotLocked_butExceptionInProcessTouchEvent() throws Exception {
        //given
        SliderPanel sliderPanel = Mockito.spy(new SliderPanel(context));
        Whitebox.setInternalState(sliderPanel, "mIsLocked", false);

        ViewDragHelper viewDragHelper = Mockito.mock(ViewDragHelper.class);
        PowerMockito.doThrow(new IllegalArgumentException()).when(viewDragHelper).processTouchEvent(motionEvent);
        Whitebox.setInternalState(sliderPanel, "mDragHelper", viewDragHelper);

        //when
        boolean result = sliderPanel.onTouchEvent(motionEvent);

        //then
        assertFalse("Result must be false when not locked but exception occured during processTouchEvent()", result);
    }
}
@@ -705,16 +705,134 @@
 ertTrue;
+%0Aimport static org.mockito.Mockito.when;%0Aimport static org.mockito.internal.util.reflection.Whitebox.setInternalState;
 %0A%0A/**%0A *
@@ -1196,33 +1196,24 @@
 t);%0A        
-Whitebox.
 setInternalS
@@ -1763,32 +1763,24 @@
 s);%0A        
-Mockito.
 when(slidrCo
@@ -1820,32 +1820,24 @@
 e);%0A        
-Mockito.
 when(slidrCo
@@ -1896,24 +1896,16 @@
         
-Mockito.
 when(sli
@@ -1969,33 +1969,24 @@
 );%0A%0A        
-Whitebox.
 setInternalS
@@ -2028,33 +2028,24 @@
 e);%0A        
-Whitebox.
 setInternalS
@@ -2420,33 +2420,24 @@
 ));%0A        
-Whitebox.
 setInternalS
@@ -2813,33 +2813,24 @@
 ));%0A        
-Whitebox.
 setInternalS
@@ -2949,33 +2949,24 @@
 s);%0A        
-Whitebox.
 setInternalS
@@ -3388,33 +3388,24 @@
 ));%0A        
-Whitebox.
 setInternalS
@@ -3642,25 +3642,16 @@
         
-Whitebox.
 setInter

Add static imports in order to improve the readability.