Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add viewMatcher for assertTextColorIs and assertTextColorIsNot #485

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<View>, 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<View>, color: Int) {
viewMatcher.assertAny(not(TextColorMatcher(color)))
}

@JvmStatic
fun assertTextColorIsNot(@IdRes viewId: Int, color: Int) {
viewId.resourceMatcher().assertAny(not(TextColorMatcher(color)))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
)
}
}
15 changes: 15 additions & 0 deletions sample/src/main/res/layout/activity_color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,19 @@
android:layout_height="wrap_content"
/>

<LinearLayout
android:id="@+id/subLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textRed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Simple color red"
android:textColor="@color/red" />
</LinearLayout>

</LinearLayout>