Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 0e001bf

Browse files
committed
Further simplify instrumented test
1 parent 2f48780 commit 0e001bf

File tree

2 files changed

+5
-28
lines changed

2 files changed

+5
-28
lines changed

PagingWithNetworkSample/app/build.gradle

-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ dependencies {
8787
androidTestImplementation deps.atsl.runner
8888
androidTestImplementation deps.atsl.rules
8989
androidTestImplementation deps.arch_core.testing
90-
androidTestImplementation deps.espresso.contrib
91-
androidTestImplementation deps.espresso.core
9290

9391
testImplementation deps.junit
9492
testImplementation deps.coroutines.test

PagingWithNetworkSample/app/src/androidTest/java/com/android/example/paging/pagingwithnetwork/reddit/ui/RedditActivityTest.kt

+5-26
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,9 @@ package com.android.example.paging.pagingwithnetwork.reddit.ui
1818

1919
import android.app.Application
2020
import android.content.Intent
21-
import android.view.View
22-
import androidx.arch.core.executor.testing.CountingTaskExecutorRule
23-
import androidx.recyclerview.widget.RecyclerView
2421
import androidx.test.annotation.UiThreadTest
2522
import androidx.test.core.app.ActivityScenario
2623
import androidx.test.core.app.ApplicationProvider
27-
import androidx.test.espresso.Espresso.onView
28-
import androidx.test.espresso.NoMatchingViewException
29-
import androidx.test.espresso.ViewAssertion
30-
import androidx.test.espresso.matcher.ViewMatchers.withId
31-
import com.android.example.paging.pagingwithnetwork.R
3224
import com.android.example.paging.pagingwithnetwork.reddit.DefaultServiceLocator
3325
import com.android.example.paging.pagingwithnetwork.reddit.ServiceLocator
3426
import com.android.example.paging.pagingwithnetwork.reddit.api.RedditApi
@@ -38,10 +30,8 @@ import com.android.example.paging.pagingwithnetwork.reddit.repository.RedditPost
3830
import com.android.example.paging.pagingwithnetwork.reddit.ui.SubRedditViewModel.Companion.DEFAULT_SUBREDDIT
3931
import com.android.example.paging.pagingwithnetwork.repository.FakeRedditApi
4032
import com.android.example.paging.pagingwithnetwork.repository.PostFactory
41-
import org.hamcrest.CoreMatchers.`is`
42-
import org.hamcrest.MatcherAssert.assertThat
33+
import org.junit.Assert.assertEquals
4334
import org.junit.Before
44-
import org.junit.Rule
4535
import org.junit.Test
4636
import org.junit.runner.RunWith
4737
import org.junit.runners.Parameterized
@@ -57,9 +47,6 @@ class RedditActivityTest(private val type: RedditPostRepository.Type) {
5747
fun params() = arrayOf(IN_MEMORY_BY_ITEM, IN_MEMORY_BY_PAGE)
5848
}
5949

60-
@get:Rule
61-
var testRule = CountingTaskExecutorRule()
62-
6350
private val postFactory = PostFactory()
6451

6552
@Before
@@ -80,7 +67,7 @@ class RedditActivityTest(private val type: RedditPostRepository.Type) {
8067
@Test
8168
@UiThreadTest
8269
fun showSomeResults() {
83-
ActivityScenario.launch<RedditActivity>(
70+
val scenario = ActivityScenario.launch<RedditActivity>(
8471
RedditActivity.intentFor(
8572
context = ApplicationProvider.getApplicationContext(),
8673
type = type
@@ -89,17 +76,9 @@ class RedditActivityTest(private val type: RedditPostRepository.Type) {
8976
}
9077
)
9178

92-
onView(withId(R.id.list)).check(RecyclerViewItemCountAssertion(3))
93-
}
94-
95-
class RecyclerViewItemCountAssertion(private val expectedCount: Int) : ViewAssertion {
96-
override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
97-
if (noViewFoundException != null) {
98-
throw noViewFoundException
99-
}
100-
val recyclerView = view as RecyclerView
101-
val adapter = recyclerView.adapter
102-
assertThat(adapter!!.itemCount, `is`(expectedCount))
79+
scenario.onActivity { activity ->
80+
val recyclerView = activity.binding.list
81+
assertEquals(3, recyclerView.adapter?.itemCount)
10382
}
10483
}
10584
}

0 commit comments

Comments
 (0)