Skip to content

Commit

Permalink
Assert content description (#308)
Browse files Browse the repository at this point in the history
* 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
alorma authored Jun 25, 2019
1 parent bb415f9 commit e9c44f2
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ assertHasBackground(R.id.view, R.drawable.ic_barista);
assertHasNoBackground(R.id.view);
```

#### Does this View have content description?
```java
assertHasContentDescription(R.id.anyView);
assertContentDescription(R.id.anyView, R.string.content_description);
assertContentDescription(R.id.anyView, "Some text");
```

#### What's the state of the Drawer?
```java
assertDrawerIsOpen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.schibsted.spain.barista.assertion
import android.view.View
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.test.InstrumentationRegistry
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.material.textfield.TextInputLayout
import com.schibsted.spain.barista.internal.assertAny
import org.hamcrest.Description
Expand All @@ -14,8 +14,8 @@ import org.hamcrest.TypeSafeMatcher
object BaristaAssistiveTextAssertions {
@JvmStatic
fun assertAssistiveText(@IdRes viewId: Int, @StringRes text: Int) {
val resourceString = InstrumentationRegistry.getTargetContext().resources.getString(text)
assertAssistiveText(viewId, resourceString)
val resourceString = InstrumentationRegistry.getInstrumentation().targetContext.resources.getString(text)
assertAssistiveText(viewId, resourceString)
}

@JvmStatic
Expand Down
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
}
}
}
}
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()
}

}
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<activity android:name=".KeyboardActivity"/>
<activity android:name=".HintAndErrorActivity"/>
<activity android:name=".WrappedEditTextActivity"/>
<activity android:name=".ContentDescriptionActivity"/>

</application>

Expand Down
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 sample/src/main/res/layout/activity_content_description.xml
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>
1 change: 1 addition & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<color name="checked">#FFC107</color>
<color name="red">#F44336</color>
<color name="blue">#0D47A1</color>
<color name="green">#69F436</color>
</resources>
5 changes: 5 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
<string name="unexising_text">unexisting text</string>


<string name="content_description_image_color_blue">Image color blue</string>
<string name="content_description_text_blue">Text blue</string>

<string name="content_description_image_color_green">Image color green</string>
<string name="content_description_text_green">Text green</string>

</resources>

0 comments on commit e9c44f2

Please sign in to comment.