提交 211259c4 编写于 作者: D Dustin Lam

Use Espresso to wait for idle UI

上级 07714e6d
......@@ -75,4 +75,5 @@ dependencies {
androidTestImplementation deps.atsl.runner
androidTestImplementation deps.coroutines.test
androidTestImplementation deps.truth
androidTestImplementation deps.espresso.contrib
}
......@@ -20,6 +20,8 @@ import androidx.annotation.UiThread
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import org.junit.Test
......@@ -36,11 +38,15 @@ class MainActivityTest {
fun showSomeResults() {
val intent = Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val scenario = ActivityScenario.launch<MainActivity>(intent)
scenario.onActivity { activity ->
val recyclerView: RecyclerView = activity.binding.cheeseList
ActivityScenario.launch<MainActivity>(intent)
onView(withId(R.id.cheeseList)).check { view, noViewFoundException ->
if (noViewFoundException != null) {
throw noViewFoundException
}
val recyclerView = view as RecyclerView
assertThat(recyclerView.adapter).isNotNull()
assertThat(recyclerView.adapter!!.itemCount).isGreaterThan(0)
}
}
}
\ No newline at end of file
}
......@@ -87,6 +87,7 @@ dependencies {
androidTestImplementation deps.atsl.runner
androidTestImplementation deps.atsl.rules
androidTestImplementation deps.arch_core.testing
androidTestImplementation deps.espresso.contrib
testImplementation deps.junit
testImplementation deps.coroutines.test
......
......@@ -18,9 +18,12 @@ package com.android.example.paging.pagingwithnetwork.reddit.ui
import android.app.Application
import android.content.Intent
import androidx.test.annotation.UiThreadTest
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.withId
import com.android.example.paging.pagingwithnetwork.R
import com.android.example.paging.pagingwithnetwork.reddit.DefaultServiceLocator
import com.android.example.paging.pagingwithnetwork.reddit.ServiceLocator
import com.android.example.paging.pagingwithnetwork.reddit.api.RedditApi
......@@ -65,9 +68,8 @@ class RedditActivityTest(private val type: RedditPostRepository.Type) {
}
@Test
@UiThreadTest
fun showSomeResults() {
val scenario = ActivityScenario.launch<RedditActivity>(
ActivityScenario.launch<RedditActivity>(
RedditActivity.intentFor(
context = ApplicationProvider.getApplicationContext(),
type = type
......@@ -76,8 +78,12 @@ class RedditActivityTest(private val type: RedditPostRepository.Type) {
}
)
scenario.onActivity { activity ->
val recyclerView = activity.binding.list
onView(withId(R.id.list)).check { view, noViewFoundException ->
if (noViewFoundException != null) {
throw noViewFoundException
}
val recyclerView = view as RecyclerView
assertEquals(3, recyclerView.adapter?.itemCount)
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册