package com.basgeekball.awesomevalidation.validators;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.basgeekball.awesomevalidation.ValidationHolder;
import com.basgeekball.awesomevalidation.utility.ViewsInfo;

import junit.framework.TestCase;

import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.api.support.membermodification.MemberModifier;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import java.util.ArrayList;

import static com.basgeekball.awesomevalidation.validators.MockValidationHolderHelper.ValidationHolderType.REGEX;
import static com.basgeekball.awesomevalidation.validators.MockValidationHolderHelper.generate;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({UnderlabelValidator.class, MockValidationHolderHelper.class})
public class UnderlabelValidatorTest extends TestCase {

    private UnderlabelValidator mUnderlabelValidator;
    private Context mMockedContext = mock(Context.class);

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mUnderlabelValidator = new UnderlabelValidator();
        mUnderlabelValidator.setContext(mMockedContext);
    }

    public void testReplaceView() throws Exception {
        UnderlabelValidator spiedUnderlabelValidator = PowerMockito.spy(new UnderlabelValidator());
        ArrayList<ViewsInfo> mockedViewsInfos = mock(ArrayList.class);
        MemberModifier.field(UnderlabelValidator.class, "mContext").set(spiedUnderlabelValidator, mMockedContext);
        MemberModifier.field(UnderlabelValidator.class, "mViewsInfos").set(spiedUnderlabelValidator, mockedViewsInfos);
        ValidationHolder mockedValidationHolder = generate(REGEX, false);
        EditText mockedEditText = mock(EditText.class);
        ViewGroup mockedParent = mock(ViewGroup.class);
        LinearLayout mockedContainer = mock(LinearLayout.class);
        int index = 128;
        ViewsInfo mockedViewsInfo = mock(ViewsInfo.class);
        when(mockedValidationHolder.getEditText()).thenReturn(mockedEditText);
        when(mockedEditText.getParent()).thenReturn(mockedParent);
        when(mockedParent.indexOfChild(mockedEditText)).thenReturn(index);
        PowerMockito.whenNew(LinearLayout.class).withArguments(mMockedContext).thenReturn(mockedContainer);
        PowerMockito.whenNew(ViewsInfo.class).withArguments(index, mockedParent, mockedContainer, mockedEditText).thenReturn(mockedViewsInfo);
        Whitebox.invokeMethod(spiedUnderlabelValidator, "replaceView", mockedValidationHolder);
        verify(mockedViewsInfos, times(1)).add(mockedViewsInfo);

        TextView mockedTextView = mock(TextView.class);
        PowerMockito.whenNew(TextView.class).withArguments(mMockedContext).thenReturn(mockedTextView);
        assertEquals(mockedTextView, Whitebox.invokeMethod(spiedUnderlabelValidator, "replaceView", mockedValidationHolder));
    }

}

@@ -701,16 +701,155 @@
 ayList;%0A
+import java.util.Arrays;%0A%0Aimport static com.basgeekball.awesomevalidation.validators.MockValidationHolderHelper.ValidationHolderType.RANGE;
 %0Aimport 
@@ -1085,20 +1085,110 @@
 Mockito.
-mock
+RETURNS_MOCKS;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.never
 ;%0Aimport
@@ -1506,16 +1506,21 @@
 idator m
+Spied
 Underlab
@@ -1570,30 +1570,8 @@
 text
- = mock(Context.class)
 ;%0A%0A 
@@ -1653,32 +1653,37 @@
 tUp();%0A        m
+Spied
 UnderlabelValida
@@ -1688,16 +1688,33 @@
 dator = 
+PowerMockito.spy(
 new Unde
@@ -1730,16 +1730,17 @@
 idator()
+)
 ;%0A      
@@ -1742,16 +1742,82 @@
        m
+MockedContext = mock(Context.class, RETURNS_MOCKS);%0A        mSpied
 Underlab
@@ -1920,108 +1920,8 @@
 n %7B%0A
-        UnderlabelValidator spiedUnderlabelValidator = PowerMockito.spy(new UnderlabelValidator());%0A
     
@@ -2055,25 +2055,26 @@
 ntext%22).set(
-s
+mS
 piedUnderlab
@@ -2178,17 +2178,18 @@
 s%22).set(
-s
+mS
 piedUnde
@@ -3053,33 +3053,34 @@
 ox.invokeMethod(
-s
+mS
 piedUnderlabelVa
@@ -3412,17 +3412,395 @@
 eMethod(
-s
+mSpiedUnderlabelValidator, %22replaceView%22, mockedValidationHolder));%0A    %7D%0A%0A    public void testTriggerOneWithoutError() throws Exception %7B%0A        ValidationHolder mockedValidationHolder = generate(REGEX, true);%0A        mSpiedUnderlabelValidator.mValidationHolderList.add(mockedValidationHolder);%0A        mSpiedUnderlabelValidator.trigger();%0A        PowerMockito.verifyPrivate(mS
 piedUnde
@@ -3808,32 +3808,48 @@
 labelValidator, 
+never()).invoke(
 %22replaceView%22, m
@@ -3862,28 +3862,707 @@
 alidationHolder)
+;%0A    %7D%0A%0A    public void testTriggerManyWithoutError() throws Exception %7B%0A        ValidationHolder mockedValidationHolderRegexType = generate(REGEX, true);%0A        ValidationHolder mockedValidationHolderRangeType = generate(RANGE, true);%0A        mSpiedUnderlabelValidator.mValidationHolderList.addAll(Arrays.asList(mockedValidationHolderRegexType, mockedValidationHolderRangeType));%0A        mSpiedUnderlabelValidator.trigger();%0A        PowerMockito.verifyPrivate(mSpiedUnderlabelValidator, never()).invoke(%22replaceView%22, mockedValidationHolderRegexType);%0A        PowerMockito.verifyPrivate(mSpiedUnderlabelValidator, never()).invoke(%22replaceView%22, mockedValidationHolderRangeType
 );%0A    %7D%0A%0A%7D%0A

Add some trigger tests for UnderlabelValidator