diff --git a/README.md b/README.md index 0756ab01..af1b40e2 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,12 @@ clickRadioButtonItem(R.id.radiogroup, "The radio text"); clickRadioButtonPosition(R.id.radiogroup, 42); ``` +#### Check/Uncheck a Checkbox +```java +check(R.id.check_box_item_1); +uncheck(R.id.check_box_item_2); +``` + #### Pick data on pickers ```java setDateOnPicker(1986, 03, 23); diff --git a/library/src/main/java/com/adevinta/android/barista/interaction/BaristaCheckboxInteractions.kt b/library/src/main/java/com/adevinta/android/barista/interaction/BaristaCheckboxInteractions.kt new file mode 100644 index 00000000..9bcbbe5f --- /dev/null +++ b/library/src/main/java/com/adevinta/android/barista/interaction/BaristaCheckboxInteractions.kt @@ -0,0 +1,19 @@ +package com.adevinta.android.barista.interaction + +import androidx.annotation.IdRes +import androidx.test.espresso.matcher.ViewMatchers.withId +import com.adevinta.android.barista.internal.performAction +import com.adevinta.android.barista.internal.viewaction.CheckableViewActions.setCheckedState + +object BaristaCheckboxInteractions { + + @JvmStatic + fun check(@IdRes checkboxId: Int) { + withId(checkboxId).performAction(setCheckedState(true)) + } + + @JvmStatic + fun uncheck(@IdRes checkboxId: Int) { + withId(checkboxId).performAction(setCheckedState(false)) + } +} \ No newline at end of file diff --git a/library/src/main/java/com/adevinta/android/barista/internal/viewaction/CheckableViewActions.kt b/library/src/main/java/com/adevinta/android/barista/internal/viewaction/CheckableViewActions.kt new file mode 100644 index 00000000..858e7522 --- /dev/null +++ b/library/src/main/java/com/adevinta/android/barista/internal/viewaction/CheckableViewActions.kt @@ -0,0 +1,33 @@ +package com.adevinta.android.barista.internal.viewaction + +import android.view.View +import android.widget.Checkable +import androidx.test.espresso.UiController +import androidx.test.espresso.ViewAction +import org.hamcrest.BaseMatcher +import org.hamcrest.CoreMatchers.isA +import org.hamcrest.Description +import org.hamcrest.Matcher + +object CheckableViewActions { + const val DESCRIPTION = "Click on a checkable view like checkboxes, radiobuttons, etc..." + + @JvmStatic + fun setCheckedState(checked: Boolean): ViewAction { + return object : ViewAction { + override fun getConstraints(): Matcher { + return object : BaseMatcher() { + override fun matches(item: Any?) = isA(Checkable::class.java).matches(item) + override fun describeMismatch(item: Any?, mismatchDescription: Description?) {} + override fun describeTo(description: Description?) {} + } + } + + override fun getDescription() = DESCRIPTION + + override fun perform(uiController: UiController?, view: View) { + (view as? Checkable)?.isChecked = checked + } + } + } +} \ No newline at end of file diff --git a/sample/src/androidTest/java/com/adevinta/android/barista/sample/CheckboxTest.kt b/sample/src/androidTest/java/com/adevinta/android/barista/sample/CheckboxTest.kt new file mode 100644 index 00000000..484fbf2f --- /dev/null +++ b/sample/src/androidTest/java/com/adevinta/android/barista/sample/CheckboxTest.kt @@ -0,0 +1,40 @@ +package com.adevinta.android.barista.sample + +import androidx.test.rule.ActivityTestRule +import com.adevinta.android.barista.assertion.BaristaCheckedAssertions.assertChecked +import com.adevinta.android.barista.assertion.BaristaCheckedAssertions.assertUnchecked +import com.adevinta.android.barista.interaction.BaristaCheckboxInteractions.check +import com.adevinta.android.barista.interaction.BaristaCheckboxInteractions.uncheck +import com.adevinta.android.barista.sample.util.SpyFailureHandlerRule +import org.junit.Rule +import org.junit.Test + +class CheckboxTest { + + @get:Rule + var activityRule = ActivityTestRule(CheckBoxActivity::class.java) + + @get:Rule + var spyFailureHandlerRule = SpyFailureHandlerRule() + + @Test + fun check_checkBox() { + check(R.id.first_item) + + assertChecked(R.id.first_item) + assertUnchecked(R.id.second_item) + spyFailureHandlerRule.assertNoEspressoFailures() + } + + @Test + fun uncheck_checkBox() { + check(R.id.first_item) + check(R.id.second_item) + + uncheck(R.id.first_item) + + assertUnchecked(R.id.first_item) + assertChecked(R.id.second_item) + spyFailureHandlerRule.assertNoEspressoFailures() + } +} \ No newline at end of file