diff --git a/barista/src/main/java/com/adevinta/android/barista/assertion/BaristaVisibilityAssertions.kt b/barista/src/main/java/com/adevinta/android/barista/assertion/BaristaVisibilityAssertions.kt index bf2787b1..40be472d 100644 --- a/barista/src/main/java/com/adevinta/android/barista/assertion/BaristaVisibilityAssertions.kt +++ b/barista/src/main/java/com/adevinta/android/barista/assertion/BaristaVisibilityAssertions.kt @@ -158,11 +158,27 @@ object BaristaVisibilityAssertions { assertNotContains(resId, resourceText) } + /** + * Assert that the view found with the provided view matcher contains the color + */ + @JvmStatic + fun assertTextColorIs(viewMatcher: Matcher, color: Int) { + viewMatcher.assertAny(TextColorMatcher(color)) + } + @JvmStatic fun assertTextColorIs(@IdRes viewId: Int, color: Int) { viewId.resourceMatcher().assertAny(TextColorMatcher(color)) } + /** + * Assert that the view found with the provided view matcher do not contains the color + */ + @JvmStatic + fun assertTextColorIsNot(viewMatcher: Matcher, color: Int) { + viewMatcher.assertAny(not(TextColorMatcher(color))) + } + @JvmStatic fun assertTextColorIsNot(@IdRes viewId: Int, color: Int) { viewId.resourceMatcher().assertAny(not(TextColorMatcher(color))) diff --git a/sample/src/androidTest/java/com/adevinta/android/barista/sample/ColorsTest.java b/sample/src/androidTest/java/com/adevinta/android/barista/sample/ColorsTest.java deleted file mode 100644 index d928abde..00000000 --- a/sample/src/androidTest/java/com/adevinta/android/barista/sample/ColorsTest.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.adevinta.android.barista.sample; - -import android.graphics.Color; -import androidx.test.rule.ActivityTestRule; -import com.adevinta.android.barista.internal.failurehandler.BaristaException; -import com.adevinta.android.barista.sample.util.FailureHandlerValidatorRule; -import org.junit.Rule; -import org.junit.Test; - -import static com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertTextColorIs; -import static com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertTextColorIsNot; - -public class ColorsTest { - - @Rule - public ActivityTestRule activityRule = new ActivityTestRule<>(ColorsActivity.class); - - @Rule - public FailureHandlerValidatorRule handlerValidator = new FailureHandlerValidatorRule(); - - @Test - public void checkSimpleColor() { - assertTextColorIs(R.id.textRed, R.color.red); - } - - @Test - public void checkColorList_whenDefault() { - assertTextColorIs(R.id.textSelectorDefault, R.color.selector_default_disabled_checked); - assertTextColorIs(R.id.textSelectorDefault, R.color.defaultColor); - } - - @Test - public void checkColorAttribute() { - assertTextColorIs(R.id.textColorAttribute, R.attr.colorPrimary); - assertTextColorIsNot(R.id.textColorAttribute, R.attr.colorError); - } - - @Test - public void checkColorInt() { - assertTextColorIs(R.id.textColorInt, Color.parseColor("#ff0000")); - assertTextColorIsNot(R.id.textColorInt, Color.parseColor("#ff00ff")); - } - - @Test - public void checkColorStyleable() { - assertTextColorIs( - R.id.customTextView, - R.styleable.SampleCustomView, - R.style.SampleCustomStyle, - R.styleable.SampleCustomView_customColor - ); - - assertTextColorIsNot( - R.id.customTextView, - R.styleable.SampleCustomView, - R.style.SampleCustomStyle_Green, - R.styleable.SampleCustomView_customColor - ); - - assertTextColorIsNot( - R.id.customTextView, - R.styleable.SampleCustomView, - R.style.SampleCustomStyle, - R.styleable.SampleCustomView_otherColor - ); - } - - @Test - public void checkColorList_whenDisabled() { - assertTextColorIs(R.id.textSelectorDisabled, R.color.selector_default_disabled_checked); - assertTextColorIs(R.id.textSelectorDisabled, R.color.disabled); - } - - @Test - public void checkColorList_whenChecked() { - assertTextColorIs(R.id.textSelectorChecked, R.color.selector_default_disabled_checked); - assertTextColorIs(R.id.textSelectorChecked, R.color.checked); - } - - @Test(expected = BaristaException.class) - public void checkSimpleColor_fails() { - assertTextColorIs(R.id.textRed, R.color.blue); - } - - @Test(expected = BaristaException.class) - public void checkColorAttr_fails() { - assertTextColorIs(R.id.textColorAttribute, R.attr.colorError); - } - - @Test(expected = BaristaException.class) - public void checkColorInt_fails() { - assertTextColorIs(R.id.textColorInt, Color.parseColor("#00ff00")); - } - - @Test(expected = BaristaException.class) - public void checkNotSimpleColor_fails() { - assertTextColorIsNot(R.id.textRed, R.color.red); - } - - @Test(expected = BaristaException.class) - public void checkNotColorAttr_fails() { - assertTextColorIsNot(R.id.textColorAttribute, R.attr.colorPrimary); - } - - @Test(expected = BaristaException.class) - public void checkNotColorInt_fails() { - assertTextColorIsNot(R.id.textColorInt, Color.parseColor("#ff0000")); - } - - @Test(expected = BaristaException.class) - public void checkColorList_whenDefault_fails() { - assertTextColorIs(R.id.textSelectorDefault, R.color.checked); - } - - @Test(expected = BaristaException.class) - public void checkColorList_whenDisabled_fails() { - assertTextColorIs(R.id.textSelectorDisabled, R.color.checked); - } - - @Test(expected = BaristaException.class) - public void checkColorList_whenChecked_fails() { - assertTextColorIs(R.id.textSelectorChecked, R.color.disabled); - } - - @Test(expected = BaristaException.class) - public void checkColorStyleable_fail() { - assertTextColorIs( - R.id.customTextView, - R.styleable.SampleCustomView, - R.style.SampleCustomStyle_Green, - R.styleable.SampleCustomView_customColor - ); - } - - @Test(expected = BaristaException.class) - public void checkNotColorStyleable_fail() { - assertTextColorIsNot( - R.id.customTextView, - R.styleable.SampleCustomView, - R.style.SampleCustomStyle, - R.styleable.SampleCustomView_customColor - ); - } -} \ No newline at end of file diff --git a/sample/src/androidTest/java/com/adevinta/android/barista/sample/ColorsTest.kt b/sample/src/androidTest/java/com/adevinta/android/barista/sample/ColorsTest.kt new file mode 100644 index 00000000..bb6638a1 --- /dev/null +++ b/sample/src/androidTest/java/com/adevinta/android/barista/sample/ColorsTest.kt @@ -0,0 +1,155 @@ +package com.adevinta.android.barista.sample + +import android.R.* +import android.graphics.Color +import androidx.test.espresso.matcher.ViewMatchers.* +import androidx.test.rule.ActivityTestRule +import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertTextColorIs +import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assertTextColorIsNot +import com.adevinta.android.barista.internal.failurehandler.BaristaException +import com.adevinta.android.barista.sample.util.FailureHandlerValidatorRule +import org.hamcrest.CoreMatchers.allOf +import org.junit.Rule +import org.junit.Test + + +class ColorsTest { + @Rule + var activityRule = ActivityTestRule(ColorsActivity::class.java) + + @Rule + var handlerValidator = FailureHandlerValidatorRule() + + @Test + fun checkSimpleColor() { + assertTextColorIs(R.id.textRed, R.color.red) + } + + @Test + fun checkColorInNested() { + assertTextColorIs(allOf(withText("Simple color red"), isDescendantOfA(withId(R.id.subLayout))), R.color.red) + } + + + @Test + fun checkColorList_whenDefault() { + assertTextColorIs(R.id.textSelectorDefault, R.color.selector_default_disabled_checked) + assertTextColorIs(R.id.textSelectorDefault, R.color.defaultColor) + } + + @Test + fun checkColorAttribute() { + assertTextColorIs(R.id.textColorAttribute, R.attr.colorPrimary) + assertTextColorIsNot(R.id.textColorAttribute, R.attr.colorError) + } + + @Test + fun checkColorInt() { + assertTextColorIs(R.id.textColorInt, Color.parseColor("#ff0000")) + assertTextColorIsNot(R.id.textColorInt, Color.parseColor("#ff00ff")) + } + + @Test + fun checkColorStyleable() { + assertTextColorIs( + R.id.customTextView, + R.styleable.SampleCustomView, + R.style.SampleCustomStyle, + R.styleable.SampleCustomView_customColor + ) + assertTextColorIsNot( + R.id.customTextView, + R.styleable.SampleCustomView, + R.style.SampleCustomStyle_Green, + R.styleable.SampleCustomView_customColor + ) + assertTextColorIsNot( + R.id.customTextView, + R.styleable.SampleCustomView, + R.style.SampleCustomStyle, + R.styleable.SampleCustomView_otherColor + ) + } + + @Test + fun checkColorList_whenDisabled() { + assertTextColorIs(R.id.textSelectorDisabled, R.color.selector_default_disabled_checked) + assertTextColorIs(R.id.textSelectorDisabled, R.color.disabled) + } + + @Test + fun checkColorList_whenChecked() { + assertTextColorIs(R.id.textSelectorChecked, R.color.selector_default_disabled_checked) + assertTextColorIs(R.id.textSelectorChecked, R.color.checked) + } + + @Test(expected = BaristaException::class) + fun checkNotSimpleColorInNested_fails() { + assertTextColorIsNot(allOf(withText("Simple color red"), isDescendantOfA(withId(R.id.subLayout))), R.color.red) + } + + @Test(expected = BaristaException::class) + fun checkSimpleColor_fails() { + assertTextColorIs(R.id.textRed, R.color.blue) + } + + @Test(expected = BaristaException::class) + fun checkColorAttr_fails() { + assertTextColorIs(R.id.textColorAttribute, R.attr.colorError) + } + + @Test(expected = BaristaException::class) + fun checkColorInt_fails() { + assertTextColorIs(R.id.textColorInt, Color.parseColor("#00ff00")) + } + + @Test(expected = BaristaException::class) + fun checkNotSimpleColor_fails() { + assertTextColorIsNot(R.id.textRed, R.color.red) + } + + @Test(expected = BaristaException::class) + fun checkNotColorAttr_fails() { + assertTextColorIsNot(R.id.textColorAttribute, R.attr.colorPrimary) + } + + @Test(expected = BaristaException::class) + fun checkNotColorInt_fails() { + assertTextColorIsNot(R.id.textColorInt, Color.parseColor("#ff0000")) + } + + @Test(expected = BaristaException::class) + fun checkColorList_whenDefault_fails() { + assertTextColorIs(R.id.textSelectorDefault, R.color.checked) + } + + @Test(expected = BaristaException::class) + fun checkColorList_whenDisabled_fails() { + assertTextColorIs(R.id.textSelectorDisabled, R.color.checked) + } + + @Test(expected = BaristaException::class) + fun checkColorList_whenChecked_fails() { + assertTextColorIs(R.id.textSelectorChecked, R.color.disabled) + } + + @Test(expected = BaristaException::class) + fun checkColorStyleable_fail() { + assertTextColorIs( + R.id.customTextView, + R.styleable.SampleCustomView, + R.style.SampleCustomStyle_Green, + R.styleable.SampleCustomView_customColor + ) + } + + @Test(expected = BaristaException::class) + fun checkNotColorStyleable_fail() { + assertTextColorIsNot( + R.id.customTextView, + R.styleable.SampleCustomView, + R.style.SampleCustomStyle, + R.styleable.SampleCustomView_customColor + ) + } +} \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_color.xml b/sample/src/main/res/layout/activity_color.xml index a4109a1f..f86ace0a 100644 --- a/sample/src/main/res/layout/activity_color.xml +++ b/sample/src/main/res/layout/activity_color.xml @@ -68,4 +68,19 @@ android:layout_height="wrap_content" /> + + + + + \ No newline at end of file