Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 27e2f115 authored by 62160286's avatar 62160286
Browse files

Test Navigation Component

parent a5ebf32e
Branches master
No related tags found
No related merge requests found
......@@ -61,4 +61,9 @@ dependencies {
implementation "androidx.constraintlayout:constraintlayout:$constraintlayout_version"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.4.0'
androidTestImplementation 'androidx.navigation:navigation-testing:2.4.2'
debugImplementation 'androidx.fragment:fragment-testing:1.4.1'
}
\ No newline at end of file
package com.example.wordsapp
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.navigation.Navigation
import androidx.navigation.testing.TestNavHostController
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.runner.AndroidJUnit4
import junit.framework.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class NavigationTests {
@Test
fun navigate_to_words_nav_component() {
val navController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)
val letterListScenario = launchFragmentInContainer<LetterListFragment>(
themeResId =
R.style.Theme_Words
)
letterListScenario.onFragment { fragment ->
navController.setGraph(R.navigation.nav_graph)
Navigation.setViewNavController(fragment.requireView(), navController)
}
onView(withId(R.id.recycler_view))
.perform(
RecyclerViewActions
.actionOnItemAtPosition<RecyclerView.ViewHolder>(2, click())
)
assertEquals(navController.currentDestination?.id, R.id.wordListFragment)
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment