-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
4 changed files
with
98 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
19 changes: 19 additions & 0 deletions
19
...ary/src/main/java/com/adevinta/android/barista/interaction/BaristaCheckboxInteractions.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,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)) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ry/src/main/java/com/adevinta/android/barista/internal/viewaction/CheckableViewActions.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,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 | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
sample/src/androidTest/java/com/adevinta/android/barista/sample/CheckboxTest.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,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() | ||
} | ||
} |