This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
forked from AdevintaSpain/Barista
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add color styleable assertTextColorIs (AdevintaSpain#341)
* 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
Showing
15 changed files
with
352 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...y/src/main/java/com/schibsted/spain/barista/internal/matcher/TextStyleableColorMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
sample/src/androidTest/java/com/schibsted/spain/barista/sample/ColorsSecondaryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
sample/src/main/java/com/schibsted/spain/barista/sample/ColorsSecondActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
sample/src/main/java/com/schibsted/spain/barista/sample/widget/SampleCustomView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
sample/src/main/res/color/secondary_selector_default_disabled_checked.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.