package com.devinci.lib.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import com.devinci.lib.BuildConfig;
import com.devinci.lib.R;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.res.Attribute;
import org.robolectric.res.ResourceLoader;
import org.robolectric.shadows.RoboAttributeSet;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import static org.robolectric.Shadows.shadowOf;

@RunWith(RobolectricGradleTestRunner.class) public class SignInButtonTest {

  private Context context;

  @Before public void setUp() {
    context = RuntimeEnvironment.application;
  }

  @Test public void shouldCreateSignInButtonFromContext() {
    // when
    SignInButton signInButton = new SignInButton(context);

    // then
    assertThat(signInButton.getTextColor()).isNotEqualTo(0);
    assertThat(signInButton.getText()).isEmpty();
    assertThat(signInButton.getIcon()).isNull();
    assertThat(shadowOf(signInButton.getForeground()).getCreatedFromResId()).isEqualTo(
        R.drawable.lib_button_sign_in_pressed_selector);
    assertThat(shadowOf(signInButton.getBackground()).getCreatedFromResId()).isEqualTo(
        R.drawable.lib_button_sign_in);
  }

  @Test public void shouldObtainTextFromAttrs() {
    ResourceLoader resourceLoader = shadowOf(context.getResources()).getResourceLoader();
    String expectedTitle = context.getString(R.string.lib_sign_in_with_google);
    AttributeSet attributeSet = new RoboAttributeSet(newArrayList(
        new Attribute(BuildConfig.APPLICATION_ID + ":attr/lib_signInText",
            "@string/lib_sign_in_with_google", BuildConfig.APPLICATION_ID)), resourceLoader);

    SignInButton signInButton = new SignInButton(context, attributeSet);

    assertThat(signInButton.getText()).isEqualTo(expectedTitle);
  }

  @Test public void shouldObtainIconFromAttrs() {
    ResourceLoader resourceLoader = shadowOf(context.getResources()).getResourceLoader();
    List<Attribute> attributes = new ArrayList<>(1);
    attributes.add(new Attribute(BuildConfig.APPLICATION_ID + ":attr/lib_signInIcon",
        "@drawable/lib_ic_sign_in_google", BuildConfig.APPLICATION_ID));
    AttributeSet attributeSet = new RoboAttributeSet(attributes, resourceLoader);

    SignInButton signInButton = new SignInButton(context, attributeSet);

    assertThat(shadowOf(signInButton.getIcon()).getCreatedFromResId()).isEqualTo(
        R.drawable.lib_ic_sign_in_google);
  }

  @Test public void shouldObtainTextColorFromAttrs() {
    ResourceLoader resourceLoader = shadowOf(context.getResources()).getResourceLoader();
    int expectedTextColor = ContextCompat.getColor(context, R.color.lib_black);
    List<Attribute> attributes = new ArrayList<>(1);
    attributes.add(
        new Attribute(BuildConfig.APPLICATION_ID + ":attr/lib_signInTextColor", "@color/lib_black",
            BuildConfig.APPLICATION_ID));
    AttributeSet attributeSet = new RoboAttributeSet(attributes, resourceLoader);

    SignInButton signInButton = new SignInButton(context, attributeSet);

    assertThat(signInButton.getTextColor()).isEqualTo(expectedTextColor);
  }

  @Test public void shouldSetIcon() {
    SignInButton signInButton = new SignInButton(context);

    signInButton.setIcon(android.R.drawable.ic_btn_speak_now);

    assertThat(shadowOf(signInButton.getIcon()).getCreatedFromResId()).isEqualTo(
        android.R.drawable.ic_btn_speak_now);
  }

  @Test public void shouldSetText() {
    SignInButton signInButton = new SignInButton(context);
    String expectedText = context.getString(android.R.string.ok);

    signInButton.setText(android.R.string.ok);

    assertThat(signInButton.getText()).isEqualTo(expectedText);
  }

  @Test public void shouldSetTextColor() {
    SignInButton signInButton = new SignInButton(context);
    int expectedColor = ContextCompat.getColor(context, android.R.color.darker_gray);

    signInButton.setTextColor(android.R.color.darker_gray);

    assertThat(signInButton.getTextColor()).isEqualTo(expectedColor);
  }
}

@@ -56,16 +56,59 @@
 ontext;%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -248,59 +248,8 @@
 .R;%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A
 impo
@@ -462,51 +462,8 @@
 te;%0A
-import org.robolectric.res.ResourceLoader;%0A
 impo
@@ -1495,98 +1495,8 @@
 ) %7B%0A
-    ResourceLoader resourceLoader = shadowOf(context.getResources()).getResourceLoader();%0A
     
@@ -1606,102 +1606,51 @@
 et =
- new RoboAttributeSet(newArrayList(%0A        new Attribute(BuildConfig.APPLICATION_ID + %22:attr/
+%0A        createAttributeSet(withAttribute(%22
 lib_
@@ -1661,28 +1661,16 @@
 InText%22,
-%0A           
  %22@strin
@@ -1699,54 +1699,9 @@
 gle%22
-, BuildConfig.APPLICATION_ID)), resourceLoader
+)
 );%0A%0A
@@ -1902,103 +1902,8 @@
     
-ResourceLoader resourceLoader = shadowOf(context.getResources()).getResourceLoader();%0A    List%3C
 Attr
@@ -1899,33 +1899,35 @@
  %7B%0A    Attribute
-%3E
+Set
  attributes = ne
@@ -1924,101 +1924,56 @@
 bute
-s = new ArrayList%3C%3E(1);%0A    attributes.add(new Attribute(BuildConfig.APPLICATION_ID + %22:attr/
+Set =%0A        createAttributeSet(withAttribute(%22
 lib_
@@ -1984,24 +1984,16 @@
 InIcon%22,
-%0A       
  %22@drawa
@@ -2022,119 +2022,9 @@
 gle%22
-, BuildConfig.APPLICATION_ID));%0A    AttributeSet attributeSet = new RoboAttributeSet(attributes, resourceLoader
+)
 );%0A%0A
@@ -2286,98 +2286,8 @@
 ) %7B%0A
-    ResourceLoader resourceLoader = shadowOf(context.getResources()).getResourceLoader();%0A
     
@@ -2366,21 +2366,16 @@
 k);%0A    
-List%3C
 Attribut
@@ -2375,17 +2375,19 @@
 ttribute
-%3E
+Set
  attribu
@@ -2392,273 +2392,97 @@
 bute
-s = new ArrayList%3C%3E(1);%0A    attributes.add(%0A        new Attribute(BuildConfig.APPLICATION_ID + %22:attr/lib_signInTextColor%22, %22@color/lib_black%22,%0A            BuildConfig.APPLICATION_ID));%0A    AttributeSet attributeSet = new RoboAttributeSet(attributes, resourceLoader
+Set =%0A        createAttributeSet(withAttribute(%22lib_signInTextColor%22, %22@color/lib_black%22)
 );%0A%0A
@@ -3533,11 +3533,411 @@
 or);%0A  %7D
+%0A%0A  @NonNull private RoboAttributeSet createAttributeSet(Attribute... attributes) %7B%0A    return new RoboAttributeSet(newArrayList(attributes),%0A        shadowOf(context.getResources()).getResourceLoader());%0A  %7D%0A%0A  @NonNull private Attribute withAttribute(String name, String value) %7B%0A    return new Attribute(BuildConfig.APPLICATION_ID + %22:attr/%22 + name, value,%0A        BuildConfig.APPLICATION_ID);%0A  %7D
 %0A%7D%0A

Refactor SignInButtonTest to remove duplicate code