-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create content description matcher * Add test * Add to README.md * Add has content description * Update README * Add test top check failing test * Update error message * Add test top check failing test
- Loading branch information
Showing
9 changed files
with
211 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
54 changes: 54 additions & 0 deletions
54
...rc/main/java/com/schibsted/spain/barista/assertion/BaristaContentDescriptionAssertions.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,54 @@ | ||
package com.schibsted.spain.barista.assertion | ||
|
||
import android.view.View | ||
import androidx.annotation.IdRes | ||
import androidx.annotation.StringRes | ||
import androidx.test.espresso.matcher.ViewMatchers | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.schibsted.spain.barista.internal.assertAny | ||
import org.hamcrest.Description | ||
import org.hamcrest.Matcher | ||
import org.hamcrest.TypeSafeMatcher | ||
|
||
object BaristaContentDescriptionAssertions { | ||
|
||
@JvmStatic | ||
fun assertHasContentDescription(@IdRes viewId: Int) { | ||
ViewMatchers.withId(viewId).assertAny(matchHasContentDescription()) | ||
} | ||
|
||
@JvmStatic | ||
fun assertContentDescription(@IdRes viewId: Int, @StringRes text: Int) { | ||
val resourceString = InstrumentationRegistry.getInstrumentation().targetContext.resources.getString(text) | ||
assertContentDescription(viewId, resourceString) | ||
} | ||
|
||
@JvmStatic | ||
fun assertContentDescription(@IdRes viewId: Int, text: String) { | ||
ViewMatchers.withId(viewId).assertAny(matchContentDescription(text)) | ||
} | ||
|
||
private fun matchHasContentDescription(): Matcher<View> { | ||
return object : TypeSafeMatcher<View>() { | ||
override fun describeTo(description: Description) { | ||
description.appendText("with content description") | ||
} | ||
|
||
override fun matchesSafely(item: View): Boolean { | ||
return item.contentDescription.isNotBlank() | ||
} | ||
} | ||
} | ||
|
||
private fun matchContentDescription(expectedContentDescription: String): Matcher<View> { | ||
return object : TypeSafeMatcher<View>() { | ||
override fun describeTo(description: Description) { | ||
description.appendText("with content description: ").appendText(expectedContentDescription) | ||
} | ||
|
||
override fun matchesSafely(item: View): Boolean { | ||
return item.contentDescription == expectedContentDescription | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
sample/src/androidTest/java/com/schibsted/spain/barista/sample/ContentDescriptionTest.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,73 @@ | ||
package com.schibsted.spain.barista.sample | ||
|
||
import androidx.test.rule.ActivityTestRule | ||
import com.schibsted.spain.barista.assertion.BaristaContentDescriptionAssertions.assertContentDescription | ||
import com.schibsted.spain.barista.assertion.BaristaContentDescriptionAssertions.assertHasContentDescription | ||
import com.schibsted.spain.barista.internal.failurehandler.BaristaException | ||
import com.schibsted.spain.barista.sample.util.SpyFailureHandlerRule | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.catchThrowable | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class ContentDescriptionTest { | ||
|
||
@get:Rule | ||
var activityRule = ActivityTestRule(ContentDescriptionActivity::class.java) | ||
|
||
@get:Rule | ||
var spyFailureHandlerRule = SpyFailureHandlerRule() | ||
|
||
@Test | ||
fun assertContentDescriptionString() { | ||
assertContentDescription(R.id.imageRed, "Image color red") | ||
assertContentDescription(R.id.textRed, "Text red") | ||
|
||
spyFailureHandlerRule.assertNoEspressoFailures() | ||
} | ||
|
||
@Test | ||
fun assertFailsIfNoContentDescription() { | ||
val thrown = catchThrowable { | ||
assertContentDescription(R.id.imageGreen, R.string.content_description_image_color_green) | ||
} | ||
|
||
spyFailureHandlerRule.assertEspressoFailures(1) | ||
|
||
assertThat(thrown).isInstanceOf(BaristaException::class.java) | ||
.hasMessage("View (with id: com.schibsted.spain.barista.sample:id/imageGreen) " + | ||
"didn't match condition (with content description: Image color green)") | ||
} | ||
|
||
@Test | ||
fun assertFailsIfNotHasContentDescription() { | ||
val thrown = catchThrowable { | ||
assertHasContentDescription(R.id.imageGreen) | ||
} | ||
|
||
spyFailureHandlerRule.assertEspressoFailures(1) | ||
|
||
assertThat(thrown).isInstanceOf(BaristaException::class.java) | ||
.hasMessage("View (with id: com.schibsted.spain.barista.sample:id/imageGreen) " + | ||
"didn't match condition (with content description)") | ||
} | ||
|
||
@Test | ||
fun assertContentDescriptionStringResource() { | ||
assertContentDescription(R.id.imageBlue, R.string.content_description_image_color_blue) | ||
assertContentDescription(R.id.textBlue, R.string.content_description_text_blue) | ||
|
||
spyFailureHandlerRule.assertNoEspressoFailures() | ||
} | ||
|
||
@Test | ||
fun assertViewsHasContentDescription() { | ||
assertHasContentDescription(R.id.imageRed) | ||
assertHasContentDescription(R.id.textRed) | ||
assertHasContentDescription(R.id.imageBlue) | ||
assertHasContentDescription(R.id.textBlue) | ||
|
||
spyFailureHandlerRule.assertNoEspressoFailures() | ||
} | ||
|
||
} |
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
13 changes: 13 additions & 0 deletions
13
sample/src/main/java/com/schibsted/spain/barista/sample/ContentDescriptionActivity.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,13 @@ | ||
package com.schibsted.spain.barista.sample | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class ContentDescriptionActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_content_description) | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
sample/src/main/res/layout/activity_content_description.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,54 @@ | ||
<?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" | ||
> | ||
|
||
<ImageView | ||
android:layout_width="100dp" | ||
android:layout_height="100dp" | ||
android:background="@color/blue" | ||
android:id="@+id/imageBlue" | ||
android:contentDescription="@string/content_description_image_color_blue" | ||
/> | ||
|
||
<ImageView | ||
android:layout_width="100dp" | ||
android:layout_height="100dp" | ||
android:background="@color/red" | ||
android:id="@+id/imageRed" | ||
android:contentDescription="Image color red" | ||
/> | ||
|
||
<ImageView | ||
android:layout_width="100dp" | ||
android:layout_height="100dp" | ||
android:background="@color/green" | ||
android:id="@+id/imageGreen" | ||
/> | ||
|
||
<TextView | ||
android:layout_width="100dp" | ||
android:layout_height="wrap_content" | ||
android:text="Blue" | ||
android:id="@+id/textBlue" | ||
android:contentDescription="@string/content_description_text_blue" | ||
/> | ||
|
||
<TextView | ||
android:layout_width="100dp" | ||
android:layout_height="wrap_content" | ||
android:text="Red" | ||
android:id="@+id/textRed" | ||
android:contentDescription="Text red" | ||
/> | ||
|
||
<TextView | ||
android:layout_width="100dp" | ||
android:layout_height="wrap_content" | ||
android:text="Green" | ||
android:id="@+id/textGreen" | ||
/> | ||
|
||
</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
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