From 14e67b0cf46ba8a96d97be3bf448b0dfac9cec0d Mon Sep 17 00:00:00 2001 From: on-ferreira Date: Wed, 17 May 2023 20:10:02 -0300 Subject: [PATCH] implementing expresso testing on recycler view --- .idea/androidTestResultsUserPreferences.xml | 24 +++++++++ app/build.gradle | 7 +++ .../ExampleInstrumentedTest.kt | 52 +++++++++++++++---- 3 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 .idea/androidTestResultsUserPreferences.xml diff --git a/.idea/androidTestResultsUserPreferences.xml b/.idea/androidTestResultsUserPreferences.xml new file mode 100644 index 0000000..ab5471c --- /dev/null +++ b/.idea/androidTestResultsUserPreferences.xml @@ -0,0 +1,24 @@ + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index cc371d1..6b1eeba 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -58,12 +58,19 @@ dependencies { implementation "androidx.room:room-runtime:$roomVersion" kapt "androidx.room:room-compiler:$roomVersion" + implementation 'androidx.recyclerview:recyclerview:1.2.1' + + implementation "androidx.room:room-ktx:$roomVersion" implementation 'androidx.core:core-ktx:1.9.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.8.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' + androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1' + + } diff --git a/app/src/androidTest/java/me/joaovictorsl/adafreetoplaygames/ExampleInstrumentedTest.kt b/app/src/androidTest/java/me/joaovictorsl/adafreetoplaygames/ExampleInstrumentedTest.kt index 44ae4e3..dca3063 100644 --- a/app/src/androidTest/java/me/joaovictorsl/adafreetoplaygames/ExampleInstrumentedTest.kt +++ b/app/src/androidTest/java/me/joaovictorsl/adafreetoplaygames/ExampleInstrumentedTest.kt @@ -1,24 +1,54 @@ package me.joaovictorsl.adafreetoplaygames -import androidx.test.platform.app.InstrumentationRegistry +import androidx.recyclerview.widget.RecyclerView +import androidx.test.core.app.ActivityScenario +import androidx.test.espresso.Espresso +import androidx.test.espresso.action.ViewActions +import androidx.test.espresso.contrib.RecyclerViewActions +import androidx.test.espresso.matcher.ViewMatchers +import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 - +import androidx.test.platform.app.InstrumentationRegistry +import me.joaovictorsl.adafreetoplaygames.ui.gamelist.GameListActivity +import me.joaovictorsl.adafreetoplaygames.ui.recyclerview.adapter.GameAdapter +import org.junit.Assert.assertEquals +import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith - -import org.junit.Assert.* - /** * Instrumented test, which will execute on an Android device. * * See [testing documentation](http://d.android.com/tools/testing). */ @RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { +class GameListActivityTest { + + @get:Rule + val activityRule = ActivityScenarioRule(GameListActivity::class.java) + @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("me.joaovictorsl.adafreetoplaygames", appContext.packageName) + fun scrollToAndClickItem() { + ActivityScenario.launch(GameListActivity::class.java).use { scenario -> + // Delay for RecyclerView to populate with data + Thread.sleep(2000) + + // Replace `desiredPosition` with the index of the desired cell you want to scroll to and click + val desiredPosition = 10 + + // Scroll to the desired position + Espresso.onView(ViewMatchers.withId(R.id.game_list_recycler_view)) + .perform( + RecyclerViewActions.scrollToPosition(desiredPosition) + ) + + // Click on the desired cell + Espresso.onView(ViewMatchers.withId(R.id.game_list_recycler_view)) + .perform( + RecyclerViewActions.actionOnItemAtPosition( + desiredPosition, + ViewActions.click() + ) + ) + } } -} \ No newline at end of file +}