1*21373fa4SWill Schurman // Copyright 2015-present 650 Industries. All rights reserved.
2*21373fa4SWill Schurman package host.exp.exponent.utils
3*21373fa4SWill Schurman 
4*21373fa4SWill Schurman import android.view.View
5*21373fa4SWill Schurman import androidx.test.espresso.matcher.BoundedMatcher
6*21373fa4SWill Schurman import org.hamcrest.Description
7*21373fa4SWill Schurman import org.hamcrest.Matcher
8*21373fa4SWill Schurman import org.hamcrest.Matchers
9*21373fa4SWill Schurman 
10*21373fa4SWill Schurman object ExponentMatchers {
getTestIdnull11*21373fa4SWill Schurman   fun getTestId(view: View): String? {
12*21373fa4SWill Schurman     return if (view.tag is String) view.tag as String else null
13*21373fa4SWill Schurman   }
14*21373fa4SWill Schurman 
withTestIdnull15*21373fa4SWill Schurman   fun withTestId(text: String): Matcher<View> {
16*21373fa4SWill Schurman     return withTestId(Matchers.`is`(text))
17*21373fa4SWill Schurman   }
18*21373fa4SWill Schurman 
withTestIdnull19*21373fa4SWill Schurman   private fun withTestId(stringMatcher: Matcher<String>): Matcher<View> {
20*21373fa4SWill Schurman     return object : BoundedMatcher<View, View>(View::class.java) {
21*21373fa4SWill Schurman       override fun describeTo(description: Description) {
22*21373fa4SWill Schurman         description.appendText("with test id: ")
23*21373fa4SWill Schurman         stringMatcher.describeTo(description)
24*21373fa4SWill Schurman       }
25*21373fa4SWill Schurman 
26*21373fa4SWill Schurman       public override fun matchesSafely(view: View): Boolean {
27*21373fa4SWill Schurman         val testId = getTestId(view)
28*21373fa4SWill Schurman         return if (testId == null) {
29*21373fa4SWill Schurman           false
30*21373fa4SWill Schurman         } else {
31*21373fa4SWill Schurman           stringMatcher.matches(testId)
32*21373fa4SWill Schurman         }
33*21373fa4SWill Schurman       }
34*21373fa4SWill Schurman     }
35*21373fa4SWill Schurman   }
36*21373fa4SWill Schurman }
37