Skip to content

Commit

Permalink
Feature: check checkbox (#465)
Browse files Browse the repository at this point in the history
* Feature: Add the ability to check a checkbox

* Feature: Add documentation

* Feature: Split interactions in check/uncheck methods

Co-authored-by: Sergi Castillo <[email protected]>
  • Loading branch information
SmasSive and Sergi Castillo authored Nov 15, 2022
1 parent 622a0c0 commit 0b67afb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
@@ -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<View> {
return object : BaseMatcher<View>() {
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
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 0b67afb

Please sign in to comment.