Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Feature: Add color styleable assertTextColorIs (AdevintaSpain#341)
Browse files Browse the repository at this point in the history
* Add styleable color check

* Add color state tests

* Add name

* Fix annotations

* FIX manifest

* Update readme

* Update README.md

* Fix checkstyle

* Fix checkstyle
  • Loading branch information
alorma authored Apr 15, 2020
1 parent dddbe88 commit 7ec8e1f
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,20 @@ assertTextColorIsNot(R.id.some_red_text, R.color.blue);
assertTextColorIsNot(R.id.some_color_list_text, R.color.another_state_list);
```

`assertTextColorIs` and its variant `assertTextColorIsNot` work with:

- *Color int*: `Color.parse("#ff00ff")`
- *Color resource*: `R.color.green`
- *Color attribute*: `R.attr.colorPrimary`

Also Barista can check colors parsed from `declarable-style` custom attribute:
```java
assertTextColorIs(R.id.customTextView, R.styleable.SampleCustomView, R.style.SampleCustomStyle, R.styleable.SampleCustomView_customColor);

// ...or not?
assertTextColorIsNot(R.id.customTextView, R.styleable.SampleCustomView, R.style.SampleCustomStyle_Green, R.styleable.SampleCustomView_customColor);
```

#### Check recyclerView item count against expected item count
```java
assertRecyclerViewItemCount(R.id.recycler, 10);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.schibsted.spain.barista.assertion

import androidx.annotation.ColorRes
import android.view.View
import androidx.annotation.AttrRes
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.annotation.StyleableRes
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import android.view.View
import androidx.test.platform.app.InstrumentationRegistry
import com.schibsted.spain.barista.internal.assertAny
import com.schibsted.spain.barista.internal.matcher.TextColorMatcher
import com.schibsted.spain.barista.internal.matcher.TextStyleableColorMatcher
import com.schibsted.spain.barista.internal.util.resourceMatcher
import org.hamcrest.CoreMatchers.containsString
import org.hamcrest.Matcher
Expand Down Expand Up @@ -133,4 +136,30 @@ object BaristaVisibilityAssertions {
fun assertTextColorIsNot(@IdRes viewId: Int, color: Int) {
viewId.resourceMatcher().assertAny(not(TextColorMatcher(color)))
}

@JvmStatic
fun assertTextColorIs(
@IdRes viewId: Int,
@StyleableRes styleableRes: IntArray,
@StyleRes styleRes: Int,
@StyleableRes attrColor: Int
) {
viewId.resourceMatcher().assertAny(TextStyleableColorMatcher(
styleableRes, styleRes, attrColor
))
}

@JvmStatic
fun assertTextColorIsNot(
@IdRes viewId: Int,
@StyleableRes styleableRes: IntArray,
@StyleRes styleRes: Int,
@StyleableRes attrColor: Int
) {
viewId.resourceMatcher().assertAny(not(
TextStyleableColorMatcher(
styleableRes, styleRes, attrColor
)
))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.schibsted.spain.barista.internal.matcher
import android.content.res.ColorStateList
import android.view.View
import android.widget.TextView
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes
import androidx.annotation.StyleableRes
import androidx.test.espresso.matcher.BoundedMatcher
import com.schibsted.spain.barista.internal.util.ColorResourceType
import com.schibsted.spain.barista.internal.util.colorResourceType
import org.hamcrest.Description

class TextStyleableColorMatcher(
@StyleableRes private val styleableRes: IntArray,
@StyleRes private val styleRes: Int,
@StyleableRes private val attrColor: Int
) : BoundedMatcher<View, TextView>(TextView::class.java) {

private var styleResName: String? = null

override fun matchesSafely(textView: TextView): Boolean {
styleResName = textView.context.resources.getResourceEntryName(styleRes)

return matchesColor(textView) || matchesColorList(textView)
}

private fun matchesColor(textView: TextView): Boolean {
val currentColorInt = textView.currentTextColor
val expectedColorInt = getStyleableColor(textView)
return currentColorInt == expectedColorInt
}

private fun matchesColorList(textView: TextView): Boolean {
val currentColorList = textView.textColors
val expectedColorList = getStyleableColorList(textView)
return currentColorList == expectedColorList
}

private fun getStyleableColor(textView: TextView): Int {
val typedArray = textView.context.obtainStyledAttributes(
null,
styleableRes,
0,
styleRes
)

val expectedColorInt = typedArray.getColor(attrColor, -1)

typedArray.recycle()
return expectedColorInt
}

private fun getStyleableColorList(textView: TextView): ColorStateList? {
val typedArray = textView.context.obtainStyledAttributes(
null,
styleableRes,
0,
styleRes
)

val expectedColorInt = typedArray.getColorStateList(attrColor)

typedArray.recycle()
return expectedColorInt
}

override fun describeTo(description: Description) {
description.appendText("with style matching [$styleResName]")
}
}
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation 'androidx.annotation:annotation:1.0.2'
implementation 'com.github.bumptech.glide:glide:4.7.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.core:core-ktx:1.0.1'

androidTestImplementation project(':library')
androidTestImplementation "org.assertj:assertj-core:2.9.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.schibsted.spain.barista.sample;

import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import com.schibsted.spain.barista.internal.failurehandler.BaristaException;
import com.schibsted.spain.barista.sample.util.FailureHandlerValidatorRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertTextColorIs;
import static com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertTextColorIsNot;

@RunWith(AndroidJUnit4.class)
public class ColorsSecondaryTest {

@Rule
public ActivityTestRule<ColorsSecondActivity> activityRule = new ActivityTestRule<>(ColorsSecondActivity.class);

@Rule
public FailureHandlerValidatorRule handlerValidator = new FailureHandlerValidatorRule();

@Test
public void checkColorStyleable_colorStateList() {
assertTextColorIs(
R.id.customTextView,
R.styleable.SampleCustomView,
R.style.SampleCustomStyle_ColorState,
R.styleable.SampleCustomView_customColor
);

assertTextColorIsNot(
R.id.customTextView,
R.styleable.SampleCustomView,
R.style.SampleCustomStyle_ColorState_Secondary,
R.styleable.SampleCustomView_otherColor
);
}

@Test(expected = BaristaException.class)
public void checkColorStyleable_colorStateList_fail() {
assertTextColorIs(
R.id.customTextView,
R.styleable.SampleCustomView,
R.style.SampleCustomStyle_ColorState_Secondary,
R.styleable.SampleCustomView_customColor
);
}

@Test(expected = BaristaException.class)
public void checkNotColorStyleable_colorStateList_fail() {
assertTextColorIsNot(
R.id.customTextView,
R.styleable.SampleCustomView,
R.style.SampleCustomStyle_ColorState,
R.styleable.SampleCustomView_customColor
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,31 @@ public void checkColorAttribute() {
@Test
public void checkColorInt() {
assertTextColorIs(R.id.textColorInt, Color.parseColor("#ff0000"));
assertTextColorIsNot(R.id.textColorInt, Color.parseColor("#ff00ff"));
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
Expand Down Expand Up @@ -100,4 +124,24 @@ public void checkColorList_whenDisabled_fails() {
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
);
}
}
4 changes: 4 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
android:name=".ColorsActivity"
android:exported="true"
/>
<activity
android:name=".ColorsSecondActivity"
android:exported="true"
/>
<activity android:name=".KeyboardActivity"/>
<activity android:name=".HintAndErrorActivity"/>
<activity android:name=".WrappedEditTextActivity"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.schibsted.spain.barista.sample;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ColorsSecondActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_second);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.schibsted.spain.barista.sample.widget;

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.core.content.withStyledAttributes
import com.schibsted.spain.barista.sample.R
import kotlinx.android.synthetic.main.sample_custom_view.view.customTextView

class SampleCustomView @JvmOverloads
constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = R.attr.sampleCustomViewStyle,
defStyleRes: Int = R.style.SampleCustomStyle
) : LinearLayout(context, attributeSet, defStyleAttr) {

private var customColor: Int = Color.GRAY

init {
context.withStyledAttributes(
attributeSet,
R.styleable.SampleCustomView,
defStyleAttr,
defStyleRes
) {
customColor = getColor(R.styleable.SampleCustomView_customColor, customColor)
}

LayoutInflater.from(context).inflate(R.layout.sample_custom_view, this, true)
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)

customTextView.text = "Demo text"
customTextView.setTextColor(customColor)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:color="@color/disabled"
android:state_checked="true"/>
<item android:color="@color/checked"
android:state_enabled="false"/>
<item android:color="#ff0000"/>

</selector>
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/activity_color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@
android:textColor="@color/selector_default_disabled_checked"
/>

<com.schibsted.spain.barista.sample.widget.SampleCustomView
android:id="@+id/sampleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>
15 changes: 15 additions & 0 deletions sample/src/main/res/layout/activity_color_second.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<com.schibsted.spain.barista.sample.widget.SampleCustomView
android:id="@+id/sampleView"
style="@style/SampleCustomStyle.ColorState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>
15 changes: 15 additions & 0 deletions sample/src/main/res/layout/sample_custom_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:parentTag="android.widget.LinearLayout"
>

<TextView
android:id="@+id/customTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</merge>
Loading

0 comments on commit 7ec8e1f

Please sign in to comment.