Как я могу получить androidx.test.espresso.Root в моих тестах? - PullRequest
0 голосов
/ 09 мая 2019

Вот мой тест эспрессо:

@RunWith(AndroidJUnit4::class)
class AddTraderActivityTest {

    private lateinit var mockServer: MockWebServer

    private val TAG = AddTraderActivityTest::class.java.name

    // The IntentsTestRule class initializes Espresso Intents before each test, terminates the host activity, and releases Espresso Intents after each test
    @get:Rule
    var addTraderIntentTestRule: IntentsTestRule<AddTraderActivity> = IntentsTestRule(AddTraderActivity::class.java)

    private val baseEditText = viewWithId(baseTextInputEditText)
    private val quoteEditText = viewWithId(quoteTextInputEditText)
    private val buttonStart = viewWithId(startButton)

    @Before
    fun setup() {
        mockServer = MockWebServer()
        mockServer.start(8081)
        Debug.d(TAG, "SUCCCESS_START_MOCKWEBSRVER")
    }

    @After
    @Throws
    fun tearDown() {
        // We're done with tests, shut it down
        mockServer.shutdown()
    }

@Test
    fun buttonStart_click_clientError_showToast() {
        //stub response
        mockServer.enqueue(MockResponse()
                .setResponseCode(400))

        baseEditText.type(BASE_TEST)
        quoteEditText.type(QUOTE_TEST)
        buttonStart.click()
        onView(withText(client_error)).inRoot(ToastMatcher())
                .check(matches(isDisplayed()))
        // Wait to hide toast. Else next test fail (because toast still not hide)
        Thread.sleep(LONG_DELAY_MILLS)
       // get Root here 
}

class ToastMatcher : TypeSafeMatcher<Root>() {

    override fun describeTo(description: Description) {
        description.appendText("is toast")
    }

    override fun matchesSafely(root: Root): Boolean {
        val type = root.getWindowLayoutParams().get().type
        if (type == WindowManager.LayoutParams.TYPE_TOAST) {
            val windowToken = root.getDecorView().getWindowToken()
            val appToken = root.getDecorView().getApplicationWindowToken()
            if (windowToken === appToken) {
                return true
            }
        }
        return false
    }
}

Вопрос в следующем: Как я могу получить androidx.test.espresso.Root в моих тестах (например, в testbuttonStart_click_clientError_showToast)?

...