Skip to content

Adding Espresso Tests #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ dependencies {
// Architecture
implementation("androidx.core:core-ktx:1.2.0")
implementation("androidx.appcompat:appcompat:1.1.0")
implementation("androidx.navigation:navigation-ui:2.2.2")
implementation("androidx.navigation:navigation-ui-ktx:2.2.2")
implementation("androidx.navigation:navigation-fragment:2.2.2")
implementation("androidx.navigation:navigation-fragment-ktx:2.2.2")
implementation("androidx.navigation:navigation-ui:2.3.5")
implementation("androidx.navigation:navigation-ui-ktx:2.3.5")
implementation("androidx.navigation:navigation-fragment:2.3.5")
implementation("androidx.navigation:navigation-fragment-ktx:2.3.5")
implementation("androidx.lifecycle:lifecycle-extensions:2.2.0")
implementation("androidx.constraintlayout:constraintlayout:1.1.3")
implementation("androidx.coordinatorlayout:coordinatorlayout:1.1.0")
Expand Down Expand Up @@ -72,4 +72,11 @@ dependencies {
testImplementation("junit:junit:4.12")
androidTestImplementation("androidx.test.ext:junit:1.1.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0")
androidTestImplementation("com.android.support.test.espresso:espresso-contrib:2.0")
debugImplementation("androidx.fragment:fragment-testing:1.3.2")
androidTestImplementation("androidx.navigation:navigation-testing:2.3.5")
androidTestImplementation("io.mockk:mockk-android:1.11.0")
androidTestImplementation ("androidx.arch.core:core-testing:2.1.0")

implementation("androidx.room:room-runtime:2.2.6")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package dev.marcosfarias.pokedex.ui.dashboard

import android.content.Context
import android.os.Bundle
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.core.os.bundleOf
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.navigation.Navigation
import androidx.navigation.testing.TestNavHostController
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.database.AppDatabase
import dev.marcosfarias.pokedex.model.Pokemon
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.Executors

@RunWith(AndroidJUnit4::class)
class DashboardFragmentTest {
lateinit var navController: TestNavHostController
lateinit var bundle: Bundle

val pokemon = Pokemon().apply {
id = "#001"
name = "Bulbasaur"
}

@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()

@Before
fun setup() {

bundle = bundleOf("id" to "#001")

// Create a TestNavHostController
navController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)

// Create a graphical FragmentScenario for the TitleScreen
launchFragmentInContainer(themeResId = R.style.AppTheme, fragmentArgs = bundle) {
DashboardFragment().also { fragment ->

// In addition to returning a new instance of our Fragment,
// get a callback whenever the fragment’s view is created
// or destroyed so that we can set the NavController
fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
// The fragment’s view has just been created
navController.setGraph(R.navigation.mobile_navigation)
Navigation.setViewNavController(fragment.requireView(), navController)
}
}
}
}
}



@Test
fun checkPokemonInfo() {
Espresso.onView(
ViewMatchers.withId(R.id.textViewName)
).check(ViewAssertions.matches(ViewMatchers.withText("Bulbasaur")))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package dev.marcosfarias.pokedex.ui.home

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.assertion.ViewAssertions
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import dev.marcosfarias.pokedex.R
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class HomeFragmentTest {
lateinit var navController: TestNavHostController

@Before
fun setup() {
// Create a TestNavHostController
navController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)

// Create a graphical FragmentScenario for the TitleScreen
launchFragmentInContainer(themeResId = R.style.AppTheme) {
HomeFragment().also { fragment ->

// In addition to returning a new instance of our Fragment,
// get a callback whenever the fragment’s view is created
// or destroyed so that we can set the NavController
fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
// The fragment’s view has just been created
navController.setGraph(R.navigation.mobile_navigation)
Navigation.setViewNavController(fragment.requireView(), navController)
}
}
}
}
}

@Test
fun checkIfRecyclerViewMenuIsDisplayed() {
Espresso.onView(
ViewMatchers.withId(R.id.recyclerViewMenu)
).check(
ViewAssertions.matches(ViewMatchers.isDisplayed())
)
}

@Test
fun checkIfPokedexOpen() {
Espresso.onView(
ViewMatchers.withId(R.id.recyclerViewMenu)
).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0,
ViewActions.click()
)
)

Assert.assertEquals(R.id.navigation_pokedex, navController.currentDestination?.id)
}

@Test
fun checkIfnEwsRecyclerViewIsDisplayed() {
Espresso.onView(
ViewMatchers.withId(R.id.recyclerViewNews)
).check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}

@Test
fun checkIfNewsOpen() {
Espresso.onView(
ViewMatchers.withId(R.id.recyclerViewNews)
).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0,
ViewActions.click()
)
)

Assert.assertEquals(R.id.navigation_news_detail, navController.currentDestination?.id)
}

@Test
fun checkIfPokedexOpenOfAnotherPosition() {

Espresso.onView(
ViewMatchers.withId(R.id.recyclerViewMenu)
).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
2,
ViewActions.click()
)
)

Assert.assertEquals(R.id.navigation_pokedex, navController.currentDestination?.id)
}

@Test
fun checkTitleText() {
Espresso.onView(
ViewMatchers.withId(R.id.textViewHomeTitle)
).check(ViewAssertions.matches(ViewMatchers.withText(R.string.main_title)))
}

@Test
fun checkTextFromSearchBar() {
Espresso.onView(
ViewMatchers.withId(R.id.textViewSearchBar)
).check(ViewAssertions.matches(ViewMatchers.withText(R.string.main_search)))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dev.marcosfarias.pokedex.ui.pokedex

import android.util.Log
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.ViewAssertion
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import dev.marcosfarias.pokedex.R
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
class PokedexFragmentTest {

lateinit var navController: TestNavHostController

@Before
fun setup() {
// Create a TestNavHostController
navController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)

// Create a graphical FragmentScenario for the TitleScreen
launchFragmentInContainer(themeResId = R.style.AppTheme) {
PokedexFragment().also { fragment ->

// In addition to returning a new instance of our Fragment,
// get a callback whenever the fragment’s view is created
// or destroyed so that we can set the NavController
fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
// The fragment’s view has just been created
navController.setGraph(R.navigation.mobile_navigation)
navController.setCurrentDestination(R.id.navigation_pokedex)
Navigation.setViewNavController(fragment.requireView(), navController)
}
}
}
}
}

@Test
fun checkIfOpenPokemonDetails() {

Thread.sleep(3000)
Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0,
ViewActions.click()
)
)

assertEquals(R.id.navigation_dashboard,navController.currentDestination?.id)
}

@Test
fun checkIfRecyclerViewIsDisplayed(){
Espresso.onView(
ViewMatchers.withId(R.id.recyclerView)
).check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
}
4 changes: 3 additions & 1 deletion app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
android:orientation="vertical">

<TextView
android:id="@+id/textViewHomeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
Expand Down Expand Up @@ -94,6 +95,7 @@
android:src="@drawable/search" />

<TextView
android:id="@+id/textViewSearchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
Expand Down Expand Up @@ -164,7 +166,7 @@
android:id="@+id/recyclerViewNews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundLight"
android:background="@color/backgroundLight"
android:overScrollMode="never"
app:layoutManager="GridLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Expand Down