1+ package com.example.android.testing.espresso.RecyclerViewSample
2+
3+ import androidx.test.espresso.Espresso.onView
4+ import androidx.test.espresso.PerformException
5+ import androidx.test.espresso.action.ViewActions.click
6+ import androidx.test.espresso.assertion.ViewAssertions.matches
7+ import androidx.test.espresso.contrib.RecyclerViewActions
8+ import androidx.test.espresso.matcher.ViewMatchers.*
9+ import androidx.test.ext.junit.rules.activityScenarioRule
10+ import androidx.test.ext.junit.runners.AndroidJUnit4
11+ import androidx.test.filters.LargeTest
12+ import org.hamcrest.Description
13+ import org.hamcrest.Matcher
14+ import org.hamcrest.TypeSafeMatcher
15+ import org.junit.Rule
16+ import org.junit.Test
17+ import org.junit.runner.RunWith
18+
19+ @RunWith(AndroidJUnit4 ::class )
20+ @LargeTest
21+ class RecyclerViewSampleKtTest {
22+
23+ /* *
24+ * Use {@link ActivityScenario} to create and launch the activity under test. This is a
25+ * replacement for {@link androidx.test.rule.ActivityTestRule}.
26+ */
27+ @get:Rule
28+ var activityScenarioRule = activityScenarioRule<MainActivity >()
29+
30+ @Test(expected = PerformException ::class )
31+ fun itemWithText_doesNotExist () {
32+ // Attempt to scroll to an item that contains the special text.
33+ onView(withId(R .id.recyclerView))
34+ // scrollTo will fail the test if no item matches.
35+ .perform(RecyclerViewActions .scrollTo<CustomAdapter .ViewHolder >(
36+ hasDescendant(withText(" not in the list" ))
37+ ))
38+ }
39+
40+ @Test
41+ fun scrollToItemBelowFold_checkItsText () {
42+ // First scroll to the position that needs to be matched and click on it.
43+ onView(withId(R .id.recyclerView))
44+ .perform(RecyclerViewActions .actionOnItemAtPosition<CustomAdapter .ViewHolder >(ITEM_BELOW_THE_FOLD , click()))
45+
46+ // Match the text in an item below the fold and check that it's displayed.
47+ val itemElementText = " This is element #${ITEM_BELOW_THE_FOLD } "
48+ onView(withText(itemElementText)).check(matches(isDisplayed()))
49+ }
50+
51+ @Test
52+ fun itemInMiddleOfList_hasSpecialText () {
53+ // First, scroll to the view holder using the isInTheMiddle matcher.
54+ onView(withId(R .id.recyclerView))
55+ .perform(RecyclerViewActions .scrollToHolder(isInTheMiddle()))
56+
57+ // Check that the item has the special text.
58+ val middleElementText = " This is the middle!"
59+ onView(withText(middleElementText)).check(matches(isDisplayed()))
60+ }
61+
62+ /* *
63+ * Matches the {@link CustomAdapter.ViewHolder}s in the middle of the list.
64+ */
65+ private fun isInTheMiddle (): Matcher <CustomAdapter .ViewHolder > {
66+ return object : TypeSafeMatcher <CustomAdapter .ViewHolder >() {
67+ override fun matchesSafely (customHolder : CustomAdapter .ViewHolder ): Boolean {
68+ return customHolder.isInTheMiddle
69+ }
70+
71+ override fun describeTo (description : Description ) {
72+ description.appendText(" item in the middle" )
73+ }
74+ }
75+ }
76+
77+ companion object {
78+ val ITEM_BELOW_THE_FOLD = 40
79+ }
80+
81+ }
0 commit comments