package com.snowble.android.verticalstepper;

import android.app.Activity;
import android.os.Build;
import android.support.v7.widget.AppCompatButton;
import android.view.View;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.util.ActivityController;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.M)
public class VerticalStepperTest {

    private Activity activity;
    private VerticalStepper stepper;

    @Before
    public void before() {
        ActivityController<DummyActivity> activityController = Robolectric.buildActivity(DummyActivity.class);
        activity = activityController.create().get();
        stepper = new VerticalStepper(activity);
    }

    @Test
    public void toggleStepExpandedState_Inactive_ShouldBecomeActiveAndExpanded() {
        VerticalStepper.LayoutParams lp = mockLayoutParams();
        lp.active = false;
        when(lp.continueButton.getVisibility()).thenReturn(View.GONE);

        View innerView = mock(View.class);
        when(innerView.getVisibility()).thenReturn(View.GONE);
        when(innerView.getLayoutParams()).thenReturn(lp);

        stepper.toggleStepExpandedState(innerView);

        assertThat(lp.active).isTrue();
        verify(innerView).setVisibility(View.VISIBLE);
        verify(lp.continueButton).setVisibility(View.VISIBLE);
    }

    @Test
    public void toggleStepExpandedState_Active_ShouldBecomeInactiveAndCollapsed() {
        VerticalStepper.LayoutParams lp = mockLayoutParams();
        lp.active = true;
        when(lp.continueButton.getVisibility()).thenReturn(View.VISIBLE);

        View innerView = mock(View.class);
        when(innerView.getVisibility()).thenReturn(View.VISIBLE);
        when(innerView.getLayoutParams()).thenReturn(lp);

        stepper.toggleStepExpandedState(innerView);

        assertThat(lp.active).isFalse();
        verify(innerView).setVisibility(View.GONE);
        verify(lp.continueButton).setVisibility(View.GONE);
    }

    private VerticalStepper.LayoutParams mockLayoutParams() {
        Robolectric.AttributeSetBuilder attributeSetBuilder = Robolectric.buildAttributeSet();
        attributeSetBuilder.addAttribute(android.R.attr.layout_width, "wrap_content");
        attributeSetBuilder.addAttribute(android.R.attr.layout_height, "wrap_content");

        attributeSetBuilder.addAttribute(R.attr.step_title, "title");
        VerticalStepper.LayoutParams lp = new VerticalStepper.LayoutParams(activity, attributeSetBuilder.build());
        lp.continueButton = mock(AppCompatButton.class);
        return lp;
    }

    private static class DummyActivity extends Activity {
    }
}

@@ -1186,271 +1186,66 @@
     
-VerticalStepper.LayoutParams lp = mockLayoutParams();%0A        lp.active = false;%0A        when(lp.continueButton.getVisibility()).thenReturn(View.GONE);%0A%0A        View innerView = mock(View.class);%0A        when(innerView.getVisibility()).thenReturn(View.GON
+testStepToggle(false, View.GONE, true, View.VISIBL
 E);%0A    
     
@@ -1244,215 +1244,171 @@
     
+%7D%0A%0A
     
-when(innerView.getLayoutParams()).thenReturn(lp);%0A%0A        stepper.toggleStepExpandedState(innerView);%0A%0A        assertThat(lp.active).isTrue();%0A        verify(innerView).setVisibility(View.VISIBL
+@Test%0A    public void toggleStepExpandedState_Active_ShouldBecomeInactiveAndCollapsed() %7B%0A        testStepToggle(true, View.VISIBLE, false, View.GON
 E);%0A    
     
@@ -1407,164 +1407,189 @@
     
+%7D%0A%0A
     
-verify(lp.continueButton).setVisibility(View.VISIBLE);
+private void testStepToggle(boolean initialActivateState, int initialVisibility,
 %0A    
-%7D%0A%0A
     
-@Test%0A    public void toggleStepExpandedState_
+                        boolean finalExpected
 Active
-_ShouldBecomeInactiveAndCollapsed(
+State, int finalExpectedVisibility
 ) %7B%0A
@@ -1630,20 +1630,26 @@
 ms lp = 
-mock
+createTest
 LayoutPa
@@ -1676,19 +1676,35 @@
 ctive = 
-tru
+initialActivateStat
 e;%0A     
@@ -1757,28 +1757,33 @@
 nReturn(
-View.VISIBLE
+initialVisibility
 );%0A%0A    
@@ -1872,28 +1872,33 @@
 nReturn(
-View.VISIBLE
+initialVisibility
 );%0A     
@@ -2040,14 +2040,40 @@
 ).is
-False(
+EqualTo(finalExpectedActiveState
 );%0A 
@@ -2107,33 +2107,47 @@
 tVisibility(
-View.GONE
+finalExpectedVisibility
 );%0A        v
@@ -2185,25 +2185,39 @@
 ibility(
-View.GONE
+finalExpectedVisibility
 );%0A    %7D
@@ -2259,20 +2259,26 @@
 tParams 
-mock
+createTest
 LayoutPa
@@ -2556,17 +2556,16 @@
 tent%22);%0A
-%0A
         

Refactor test code to avoid duplicate code