Something went wrong. Try again.
A fork of Simple Time Tracker with WearOS support. (Now integrated upstream)
Something went wrong. Try again.
SimpleTimeTracker-WearOS app src androidTest java com example util simpletimetracker utils Matchers.kt
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102package com.example.util.simpletimetracker.utils
import android.view.Viewimport android.view.ViewGroupimport android.view.WindowManagerimport android.widget.TextViewimport androidx.annotation.ColorIntimport androidx.annotation.PluralsResimport androidx.cardview.widget.CardViewimport androidx.core.content.ContextCompatimport androidx.test.espresso.Rootimport androidx.test.espresso.matcher.BoundedMatcherimport androidx.test.espresso.matcher.ViewMatchers.withTagValueimport com.google.android.material.slider.Sliderimport kotlin.math.roundToIntimport org.hamcrest.CoreMatchers.equalToimport org.hamcrest.Descriptionimport org.hamcrest.Matcherimport org.hamcrest.TypeSafeMatcher
fun withCardColor(expectedId: Int): Matcher<View> = object : BoundedMatcher<View, CardView>(CardView::class.java) { override fun matchesSafely(view: CardView): Boolean { val colorInt: Int = ContextCompat.getColor(view.context, expectedId) return view.cardBackgroundColor.defaultColor == colorInt }
override fun describeTo(description: Description) { description.appendText("with card color: ") description.appendValue(expectedId) } }
fun withCardColorInt(@ColorInt colorInt: Int): Matcher<View> = object : BoundedMatcher<View, CardView>(CardView::class.java) { override fun matchesSafely(view: CardView): Boolean { return view.cardBackgroundColor.defaultColor == colorInt }
override fun describeTo(description: Description) { description.appendText("with card color int: ") description.appendValue(colorInt) } }
fun withTag(tagValueMatcher: Int): Matcher<View> = withTagValue(equalTo(tagValueMatcher))
fun isToast(): Matcher<Root> { return object : TypeSafeMatcher<Root>() { override fun matchesSafely(item: Root): Boolean { val type: Int = item.windowLayoutParams.get().type return type == WindowManager.LayoutParams.TYPE_TOAST }
override fun describeTo(description: Description) { description.appendText("is toast") } }}
fun withPluralText(@PluralsRes expectedId: Int, quantity: Int): Matcher<View> = object : BoundedMatcher<View, TextView>(TextView::class.java) { override fun matchesSafely(view: TextView): Boolean { val text: String = view.context.resources.getQuantityString(expectedId, quantity) return view.text == text }
override fun describeTo(description: Description) { description.appendText("with plural text: ") description.appendValue(expectedId) description.appendText(" and quantity: ") description.appendValue(quantity) } }
fun withSliderValue(expectedValue: Int): Matcher<View> = object : BoundedMatcher<View, Slider>(Slider::class.java) { override fun matchesSafely(slider: Slider): Boolean { return slider.value.roundToInt() == expectedValue }
override fun describeTo(description: Description) { description.appendText("expected: $expectedValue") } }
fun nthChildOf(parentMatcher: Matcher<View?>, childPosition: Int): Matcher<View> { return object : TypeSafeMatcher<View>() { override fun describeTo(description: Description) { description.appendText("with $childPosition child view of type parentMatcher") }
override fun matchesSafely(view: View): Boolean { if (view.parent !is ViewGroup) { return parentMatcher.matches(view.parent) } val group: ViewGroup = view.parent as ViewGroup return parentMatcher.matches(view.parent) && group.getChildAt(childPosition).equals(view) } }}