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

Commit

Permalink
Assertion for TextInputLayout Text Helper Hint (AdevintaSpain#303)
Browse files Browse the repository at this point in the history
* - Created feature request: AdevintaSpain#302
- Implemented a new class for asserting the helper text from the TextInputLayout
- Added Android tests to demonstrate the behaviour
- Added example to an existing layout file activity_hintanderrortext.xml
- Updated README for documentation on how to use

* - Renamed class name to AssistiveTextAssertions
- Renamed methods to assertAssistiveText() to be consistent with material guidelines specifications
- Modified README
  • Loading branch information
IanArb authored and alorma committed Jun 6, 2019
1 parent d6d47f8 commit cb0e51c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ assertError(R.id.edittext, R.string.error);
assertError(R.id.edittext, "Error message");
```

#### Check TextInputLayout's assistive helper text
```java
assertAssistiveText(R.id.textinputlayout, R.string.helper_text);
assertAssistiveText(R.id.textinputlayout, "Helper text");
```

#### Check if text on screen contains given text
```java
assertContains("text");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 com.google.android.material.textfield.TextInputLayout
import com.schibsted.spain.barista.internal.assertAny
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher

object BaristaAssistiveTextAssertions {
@JvmStatic
fun assertAssitiveText(@IdRes viewId: Int, @StringRes text: Int) {
val resourceString = InstrumentationRegistry.getTargetContext().resources.getString(text)
assertAssitiveText(viewId, resourceString)
}

@JvmStatic
fun assertAssitiveText(@IdRes viewId: Int, text: String) {
ViewMatchers.withId(viewId).assertAny(matchAssistiveText(text))
}

private fun matchAssistiveText(expectedAssistiveText: String): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("with helper text: ").appendText(expectedAssistiveText)
}

override fun matchesSafely(item: View): Boolean {
return when (item) {
is TextInputLayout -> expectedAssistiveText == item.helperText.toString()
else -> {
throw UnsupportedOperationException("View of class ${item.javaClass.simpleName} not supported")
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.schibsted.spain.barista.sample

import androidx.test.rule.ActivityTestRule
import androidx.test.runner.AndroidJUnit4
import com.schibsted.spain.barista.assertion.BaristaAssistiveTextAssertions.assertAssitiveText
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class HelperTextTest {

@get:Rule
var activityRule = ActivityTestRule(HintAndErrorActivity::class.java)

@Test
fun assertHelperTextByString() {
assertAssitiveText(R.id.texthelper_inputlayout, "This is a sample helper text")
}

@Test
fun assertHelperTextByResource() {
assertAssitiveText(R.id.texthelper_inputlayout, R.string.text_helper_text)
}
}
17 changes: 17 additions & 0 deletions sample/src/main/res/layout/activity_hintanderrortext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:padding="16dp"
>
Expand Down Expand Up @@ -38,4 +39,20 @@
android:layout_height="wrap_content"
android:hint="@string/hintanderror_edittext_hint"
/>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/texthelper_inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hintanderror_inputedittext_hint"
app:helperTextEnabled="true"
app:helperText="@string/text_helper_text"
>

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
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 @@ -27,6 +27,9 @@
<string name="hintanderror_edittext_hint">EditText hint</string>
<string name="hintanderror_edittext_error">EditText error</string>

<string name="text_helper_hint">TextInputLayout text helper hint</string>
<string name="text_helper_text">This is a sample helper text</string>

<string name="avocado">Avocado</string>
<string name="bilberry">Bilberry</string>
<string name="tamarind">Tamarind</string>
Expand All @@ -38,4 +41,6 @@
<string name="enabled">Enabled</string>
<string name="unexising_text">unexisting text</string>



</resources>

0 comments on commit cb0e51c

Please sign in to comment.