Issue#106 Update NavController example's sharedTest as well
This commit is contained in:
parent
9752d1643b
commit
d5ce57769f
10 changed files with 94 additions and 21 deletions
1
examplecase/example-navcontroller-shared-test/.gitignore
vendored
Normal file
1
examplecase/example-navcontroller-shared-test/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/build
|
||||
44
examplecase/example-navcontroller-shared-test/build.gradle
Normal file
44
examplecase/example-navcontroller-shared-test/build.gradle
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
plugins {
|
||||
id 'com.android.library'
|
||||
id 'org.jetbrains.kotlin.android'
|
||||
id 'androidx.navigation.safeargs.kotlin'
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdk 31
|
||||
|
||||
defaultConfig {
|
||||
minSdk 21
|
||||
targetSdk 31
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
ignore 'FragmentGradleConfiguration'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
applyAppSharedTestDependenciesTo(this)
|
||||
implementation project(":examplecase:example-navcontroller")
|
||||
//noinspection FragmentGradleConfiguration
|
||||
implementation "androidx.fragment:fragment-testing:1.5.3"
|
||||
implementation "androidx.navigation:navigation-testing:$navigation_version"
|
||||
implementation project(':test-util-android')
|
||||
}
|
||||
21
examplecase/example-navcontroller-shared-test/proguard-rules.pro
vendored
Normal file
21
examplecase/example-navcontroller-shared-test/proguard-rules.pro
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.fnives.test.showcase.examplecase.navcontroller.shared.test">
|
||||
|
||||
</manifest>
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
package org.fnives.test.showcase.examplecase.navcontroller
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.testing.FragmentScenario
|
||||
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
|
||||
import androidx.test.espresso.action.ViewActions
|
||||
import androidx.test.espresso.contrib.RecyclerViewActions
|
||||
import androidx.test.espresso.matcher.ViewMatchers
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import org.fnives.test.showcase.android.testutil.viewaction.recycler.RemoveItemAnimations
|
||||
import org.hamcrest.Matchers
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/**
|
||||
* Shows basic navigation test from a ListScreen to a Detail Screen.
|
||||
*
|
||||
* Sets up TestNavController and shows how it can be used to verify proper arguments, destination.
|
||||
*
|
||||
* For more info check out https://developer.android.com/guide/navigation/navigation-testing
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
open class HomeNavigationSharedTest {
|
||||
|
||||
private lateinit var fragmentScenario: FragmentScenario<HomeFragment>
|
||||
private lateinit var testNavController: TestNavHostController
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
testNavController = TestNavHostController(ApplicationProvider.getApplicationContext())
|
||||
fragmentScenario = launchFragmentInContainer()
|
||||
fragmentScenario.runOnMain { testNavController.setGraph(R.navigation.nav_example) }
|
||||
|
||||
fragmentScenario.onFragment { fragment ->
|
||||
Navigation.setViewNavController(fragment.requireView(), testNavController)
|
||||
}
|
||||
|
||||
Espresso.onView(ViewMatchers.withId(R.id.recycler))
|
||||
.perform(RemoveItemAnimations())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clickingOnItemNavigatesProperlyAndBackstackIsCorrect() {
|
||||
val position = 25
|
||||
Espresso.onView(ViewMatchers.withId(R.id.recycler))
|
||||
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(position))
|
||||
|
||||
Espresso.onView(
|
||||
Matchers.allOf(
|
||||
ViewMatchers.withText("Item $position"), ViewMatchers.withId(R.id.item_cta),
|
||||
ViewMatchers.withParent(ViewMatchers.withId(R.id.recycler))
|
||||
)
|
||||
)
|
||||
.perform(ViewActions.click())
|
||||
|
||||
Assert.assertEquals(R.id.detailFragment, testNavController.currentDestination?.id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clickingOnItemTwiceNavigatesProperly() {
|
||||
val position = 16
|
||||
Espresso.onView(ViewMatchers.withId(R.id.recycler))
|
||||
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(position))
|
||||
|
||||
Espresso.onView(
|
||||
Matchers.allOf(
|
||||
ViewMatchers.withText("Item $position"), ViewMatchers.withId(R.id.item_cta),
|
||||
ViewMatchers.withParent(ViewMatchers.withId(R.id.recycler))
|
||||
)
|
||||
)
|
||||
.perform(ViewActions.click())
|
||||
.perform(ViewActions.click())
|
||||
|
||||
Assert.assertEquals(R.id.detailFragment, testNavController.currentDestination?.id)
|
||||
val expectedBackstack = listOf(R.id.nav_example_xml, R.id.homeFragment, R.id.detailFragment)
|
||||
Assert.assertEquals(expectedBackstack, testNavController.backStack.map { it.destination.id })
|
||||
testNavController.backStack.map { it.arguments }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun clickingOnTwoItemsOpensOnlyTheFirst() {
|
||||
val position1 = 16
|
||||
val position2 = 15
|
||||
Espresso.onView(ViewMatchers.withId(R.id.recycler))
|
||||
.perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(position1))
|
||||
|
||||
Espresso.onView(itemViewMatcher(position1)).perform(ViewActions.click())
|
||||
Espresso.onView(itemViewMatcher(position2)).perform(ViewActions.click())
|
||||
|
||||
Assert.assertEquals(R.id.detailFragment, testNavController.currentDestination?.id)
|
||||
val expectedBackstack = listOf(R.id.nav_example_xml, R.id.homeFragment, R.id.detailFragment)
|
||||
Assert.assertEquals(expectedBackstack, testNavController.backStack.map { it.destination.id })
|
||||
val actualArgs = DetailFragmentArgs.fromBundle(testNavController.backStack.last().arguments ?: Bundle())
|
||||
Assert.assertEquals(position1, actualArgs.position)
|
||||
}
|
||||
|
||||
private fun itemViewMatcher(position: Int) =
|
||||
Matchers.allOf(
|
||||
ViewMatchers.withText("Item $position"), ViewMatchers.withId(R.id.item_cta),
|
||||
ViewMatchers.withParent(ViewMatchers.withId(R.id.recycler))
|
||||
)
|
||||
|
||||
companion object {
|
||||
inline fun <T : Fragment> FragmentScenario<T>.runOnMain(crossinline action: () -> Unit) {
|
||||
onFragment { action() }
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue