Issue#5 Shows basic example of testing NavController usage

This commit is contained in:
Gergely Hegedus 2022-09-23 13:53:05 +03:00
parent faf9cceb8e
commit 00e7a806eb
20 changed files with 383 additions and 2 deletions

View file

@ -114,6 +114,9 @@ dependencies {
testImplementation testFixtures(project(':core'))
androidTestImplementation testFixtures(project(':core'))
// case specific
implementation project(":examplecase:example-navcontroller")
}
apply from: '../gradlescripts/pull-screenshots.gradle'

View file

@ -2,6 +2,7 @@
buildscript {
ext.kotlin_version = "1.6.10"
ext.detekt_version = "1.19.0"
ext.navigation_version = "2.4.2"
repositories {
mavenCentral()
google()
@ -11,6 +12,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:7.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jlleitschuh.gradle:ktlint-gradle:10.2.1"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
}
}

View file

@ -0,0 +1 @@
/build

View file

@ -0,0 +1,67 @@
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'
}
sourceSets {
androidTest {
java.srcDirs += "src/sharedTest/java"
assets.srcDirs += files("$projectDir/schemas".toString())
}
test {
java.srcDirs += "src/sharedTest/java"
java.srcDirs += "src/robolectricTest/java"
resources.srcDirs += files("$projectDir/schemas".toString())
}
}
// needed for androidTest
packagingOptions {
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/AL2.0'
exclude 'META-INF/LICENSE.md'
exclude 'META-INF/LICENSE-notice.md'
}
}
dependencies {
implementation "androidx.core:core-ktx:$androidx_core_version"
implementation "androidx.appcompat:appcompat:$androidx_appcompat_version"
implementation "com.google.android.material:material:$androidx_material_version"
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
debugImplementation "androidx.fragment:fragment-testing:1.5.3"
applyAppTestDependenciesTo(this)
testImplementation "androidx.navigation:navigation-testing:$navigation_version"
testImplementation project(':test-util-android')
androidTestImplementation project(':test-util-android')
androidTestImplementation "androidx.navigation:navigation-testing:$navigation_version"
}

View 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

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.fnives.test.showcase.examplecase.navcontroller">
<application>
<activity android:name=".NavControllerActivity" android:exported="true"/>
</application>
</manifest>

View file

@ -0,0 +1,17 @@
package org.fnives.test.showcase.examplecase.navcontroller
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.navArgs
class DetailFragment : Fragment(R.layout.fragment_detail) {
private val args by navArgs<DetailFragmentArgs>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(view as TextView).text = getString(R.string.home_item, args.position)
}
}

View file

@ -0,0 +1,44 @@
package org.fnives.test.showcase.examplecase.navcontroller
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class HomeFragment : Fragment(R.layout.fragment_home) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recycler = view.findViewById<RecyclerView>(R.id.recycler)
recycler.layoutManager = LinearLayoutManager(view.context)
recycler.adapter = Adapter(onClick = {
if (findNavController().currentDestination?.id != R.id.homeFragment) return@Adapter
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToDetailFragment(it))
})
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
constructor(parent: ViewGroup) : this(LayoutInflater.from(parent.context).inflate(R.layout.item_home, parent, false))
}
class Adapter(
private val count: Int = 30,
private val onClick: (Int) -> Unit,
) : RecyclerView.Adapter<ViewHolder>() {
override fun getItemCount(): Int = count
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val context = holder.itemView.context
(holder.itemView as Button).text = context.getString(R.string.home_item, position)
holder.itemView.setOnClickListener { onClick(position) }
}
}
}

View file

@ -0,0 +1,15 @@
package org.fnives.test.showcase.examplecase.navcontroller
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
// to see the actual screen, not just in test use:
// adb shell am start -n org.fnives.test.showcase/org.fnives.test.showcase.examplecase.navcontroller.NavControllerActivity
// after installing the apk
class NavControllerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nav_controller)
}
}

View file

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:title="@string/activity_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:navGraph="@navigation/nav_example" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="?attr/textAppearanceHeadline1"
tools:text="Item 5">
</TextView>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler"
android:layout_width="match_parent"
tools:listitem="@layout/item_home"
tools:itemCount="30"
android:layout_height="match_parent" />

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_cta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:textAppearance="?attr/textAppearanceHeadline4"
tools:text="Item: 5" />

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_example.xml"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="org.fnives.test.showcase.examplecase.navcontroller.HomeFragment"
tools:layout="@layout/fragment_home"
android:label="HomeFragment" >
<action
android:id="@+id/action_homeFragment_to_detailFragment"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/detailFragment"
android:name="org.fnives.test.showcase.examplecase.navcontroller.DetailFragment"
tools:layout="@layout/fragment_detail"
android:label="DetailFragment" >
<argument
android:name="position"
app:argType="integer" />
</fragment>
</navigation>

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="home_item">Item %1$d</string>
<string name="activity_title">Nav Controller Example</string>
</resources>

View file

@ -0,0 +1,114 @@
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)
class HomeNavigationTest {
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)
Assert.assertEquals(listOf(R.id.nav_example_xml, R.id.homeFragment, R.id.detailFragment), 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)
Assert.assertEquals(listOf(R.id.nav_example_xml, R.id.homeFragment, R.id.detailFragment), 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() }
}
}
}

View file

@ -0,0 +1,2 @@
sdk=22,28
instrumentedPackages=androidx.loader.content

View file

@ -1,4 +1,4 @@
task jvmTests(dependsOn: ["app:testDebugUnitTest", "core:test", "network:test"]) {
task jvmTests(dependsOn: ["app:testDebugUnitTest", "core:test", "network:test", "examplecase:example-navcontroller:testDebugUnitTest"]) {
group = 'Tests'
description = 'Run all JVM tests'
}
@ -10,7 +10,7 @@ task robolectricTests(type: Exec) {
commandLine 'sh', './gradlew', 'testDebugUnitTest', '--tests', 'org.fnives.test.*InstrumentedTest'
}
task androidTests(dependsOn: ["app:connectedDebugAndroidTest"]) {
task androidTests(dependsOn: ["app:connectedDebugAndroidTest", "examplecase:example-navcontroller:connectedDebugAndroidTest"]) {
group = 'Tests'
description = 'Run Android tests'
}

View file

@ -8,3 +8,4 @@ include ':test-util-shared-android'
include ':test-util-shared-robolectric'
include ':test-util-android'
include ':test-util-junit5-android'
include ':examplecase:example-navcontroller'