Last active
April 4, 2024 11:25
-
-
Save cmathew/28dbec914a06035f023894cd36ef73ea to your computer and use it in GitHub Desktop.
Outline for an Espresso test that interacts with BottomSheets in a reliable way
This file contains hidden or 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
@Test | |
fun clickButtonInsideExpandedBottomSheet() { | |
val sheetBehavior = activity.getBottomSheetBehavior() | |
val expandedSheetIdlingResource = BottomSheetStateResource(sheetBehavior, STATE_EXPANDED) | |
val settledSheetIdlingResource = BottomSheetSettledResource(sheetBehavior) | |
// Wait for settled state | |
withBottomSheetResource(settledSheetIdlingResource) { | |
// Use BottomSheetSetStateAction to expand the Bottom Sheet | |
} | |
// Wait for expanded state | |
withBottomSheetResource(expandedSheetIdlingResource) { | |
// Click on button inside Bottom Sheet content | |
} | |
// Assert that button had the intended effect | |
} | |
private fun withBottomSheetResource( | |
sheetIdlingResource: IdlingResource, | |
actionsAndAssertions: () -> Unit | |
) { | |
with(sheetIdlingResource) { | |
idlingRegistry.register(this) | |
try { | |
actionsAndAssertions.invoke() | |
} finally { | |
idlingRegistry.unregister(this) | |
} | |
} | |
} |
@davwheat thanks for checking out my post! Here's the source for BottomSheetSettledResource
:
class BottomSheetSettledResource(
bottomSheetBehavior: BottomSheetBehaviorWrapper,
) : BottomSheetResource(bottomSheetBehavior) {
override fun getName(): String {
return BottomSheetSettledResource::class.java.simpleName
}
override fun isDesiredState(@BottomSheetBehavior.State state: Int): Boolean {
return state != BottomSheetBehavior.STATE_DRAGGING && state != BottomSheetBehavior.STATE_SETTLING
}
}
Thanks for getting back to me even after so many year since your post! Thanks for the help :)
hey, we added following dummy assertion to force bottom sheet idling resource to wait until it is done.
Espresso.onIdle { }
actionsAndAssertions.invoke()
this ensure that espresso waits for bottom sheet resource to become idle
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this is really old now, but where does
BottomSheetSettledResource
come from? It's not included in the article you've written up. Thanks!