Please conduct the following tasks alone. For implementation details you can refer to the lecture slides or the Android developer website. Please do not hesitate to ask me or the tutor if you have any questions.
implementation("androidx.recyclerview:recyclerview:1.3.1")
https://developer.android.com/jetpack/androidx/releases/recyclerviewandroid:id="@+id/homeRecyclerView"
Use this android ids for each row:
title: @+id/title
year: @+id/year
actor: @+id/actor
Example Layout:
Use this data to fill out your rows:
title | year | actor |
---|---|---|
Dr. No | 1962 | Sean Connery |
From Russia with Love | 1963 | Sean Connery |
Goldfinger | 1964 | Sean Connery |
Thunderball | 1965 | Sean Connery |
You Only Live Twice | 1967 | Sean Connery |
package de.hdmstuttgart.movietracker
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.core.internal.deps.guava.base.Preconditions
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.filters.LargeTest
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@LargeTest
@RunWith(androidx.test.ext.junit.runners.AndroidJUnit4::class)
class Assignment2Test {
@get:Rule
var activityScenarioRule = ActivityScenarioRule(
MainActivity::class.java
)
@Test
fun mainActivityTest() {
Espresso.onView(withId(R.id.homeRecyclerView))
.check(
ViewAssertions.matches(
atPosition(
0,
ViewMatchers.hasDescendant(ViewMatchers.withText("Dr. No"))
)
)
)
Espresso.onView(withId(R.id.homeRecyclerView))
.check(
ViewAssertions.matches(
atPosition(
1,
ViewMatchers.hasDescendant(ViewMatchers.withText("From Russia with Love"))
)
)
)
Espresso.onView(withId(R.id.homeRecyclerView))
.check(
ViewAssertions.matches(
atPosition(
2,
ViewMatchers.hasDescendant(ViewMatchers.withText("Goldfinger"))
)
)
)
Espresso.onView(withId(R.id.homeRecyclerView))
.check(
ViewAssertions.matches(
atPosition(
3,
ViewMatchers.hasDescendant(ViewMatchers.withText("Thunderball"))
)
)
)
Espresso.onView(withId(R.id.homeRecyclerView))
.check(
ViewAssertions.matches(
atPosition(
4,
ViewMatchers.hasDescendant(ViewMatchers.withText("You Only Live Twice"))
)
)
)
Espresso.onView(withId(R.id.homeRecyclerView))
.perform(
RecyclerViewActions.actionOnItemAtPosition<MovieAdapter.ViewHolder>(
0,
ViewActions.click()
)
)
Espresso.onView(withId(R.id.homeRecyclerView))
.check(
ViewAssertions.matches(
atPosition(
0,
ViewMatchers.hasDescendant(ViewMatchers.withText("From Russia with Love"))
)
)
)
Espresso.onView(withId(R.id.homeRecyclerView))
.perform(
RecyclerViewActions.actionOnItemAtPosition<MovieAdapter.ViewHolder>(
0,
ViewActions.click()
)
)
Espresso.onView(withId(R.id.homeRecyclerView))
.check(
ViewAssertions.matches(
atPosition(
0,
ViewMatchers.hasDescendant(ViewMatchers.withText("Goldfinger"))
)
)
)
}
companion object {
fun atPosition(
position: Int,
itemMatcher: Matcher<View?>
): BoundedMatcher<View?, RecyclerView> {
Preconditions.checkNotNull(itemMatcher)
return object : BoundedMatcher<View?, RecyclerView>(RecyclerView::class.java) {
override fun describeTo(description: Description) {
description.appendText("has item at position $position: ")
itemMatcher.describeTo(description)
}
override fun matchesSafely(view: RecyclerView): Boolean {
val viewHolder = view.findViewHolderForAdapterPosition(position)
?: // has no item on such position
return false
return itemMatcher.matches(viewHolder.itemView)
}
}
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.9.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.recyclerview:recyclerview:1.3.1")
implementation("androidx.test.espresso:espresso-contrib:3.5.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}