Skip to content

Instantly share code, notes, and snippets.

@Chesteer89
Created October 19, 2017 08:41
Show Gist options
  • Save Chesteer89/d6bfe7383c287935ce938e9dfa98c0a1 to your computer and use it in GitHub Desktop.
Save Chesteer89/d6bfe7383c287935ce938e9dfa98c0a1 to your computer and use it in GitHub Desktop.
class KToast : BaseActions, TextViewAssertions {
private val builder = ViewBuilder().apply( {withAnyText()} )
val matcher = this.builder.getViewMatcher()
override val view = this.builder.getViewInteraction().inRoot(ToastMatcher())
/**
* Operator that allows usage of DSL style
*
* @param function Tail lambda with receiver which is your view
*/
operator fun invoke(function: KToast.() -> Unit) {
function(this)
}
}
class ToastMatcher : TypeSafeMatcher<Root>() {
override fun describeTo(description: Description) {
description.appendText("is toast")
}
override fun matchesSafely(root: Root): Boolean {
val type = root.windowLayoutParams.get().type
if (type == WindowManager.LayoutParams.TYPE_TOAST) {
val windowToken = root.decorView.windowToken
val appToken = root.decorView.applicationWindowToken
if (windowToken === appToken) {
//means this window isn't contained by any other windows.
return true
}
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment