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.
Assertion for TextInputLayout Text Helper Hint (AdevintaSpain#303)
* - 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
Showing
5 changed files
with
95 additions
and
0 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
42 changes: 42 additions & 0 deletions
42
...ary/src/main/java/com/schibsted/spain/barista/assertion/BaristaAssistiveTextAssertions.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,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") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
sample/src/androidTest/java/com/schibsted/spain/barista/sample/HelperTextTest.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,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) | ||
} | ||
} |
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