Skip to content

Commit

Permalink
idling resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Dendi6 committed May 2, 2021
1 parent 071020a commit 7c42693
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies {
implementation "androidx.legacy:legacy-support-v4:$legacySupport"
implementation "com.google.android.material:material:$materialVersion"
implementation "com.github.bumptech.glide:glide:$glideVersion"
implementation "androidx.test.espresso:espresso-idling-resource:$idleResourceVersion"

testImplementation "junit:junit:$jUnitVersion"
testImplementation "org.mockito:mockito-core:$mockitoVersion"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.dendi.filmscatalogs.ui.home

import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.dendi.filmscatalogs.R
import com.dendi.filmscatalogs.utils.DataDummy
import org.junit.Rule
import com.dendi.filmscatalogs.utils.EspressoIdlingResource
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

Expand All @@ -19,23 +22,28 @@ class HomeActivityTest {
private val dummyMovie = DataDummy.generateDummyMovies()
private val dummyTvShow = DataDummy.generateDummyTvShow()

@get:Rule
var activityRule = ActivityScenarioRule(HomeActivity::class.java)
@Before
fun setUp() {
ActivityScenario.launch(HomeActivity::class.java)
IdlingRegistry.getInstance().register(EspressoIdlingResource.idlingResource)
}

@After
fun tearDown() {
IdlingRegistry.getInstance().unregister(EspressoIdlingResource.idlingResource)
}

@Test
fun loadTabButton() {
onView(withId(R.id.tabs)).check(matches(isDisplayed()))
delayTwoSecond()
onView(withId(R.id.view_pager)).perform(swipeLeft())
delayTwoSecond()
onView(withId(R.id.view_pager)).perform(swipeRight())
onView(withText(R.string.tv_show)).perform(click())
onView(withText(R.string.movies)).perform(click())
}

@Test
fun loadMovies() {
delayTwoSecond()
onView(withId(R.id.rv_movies)).check(matches(isDisplayed()))
onView(withId(R.id.rv_movies)).perform(
RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(
Expand All @@ -46,15 +54,13 @@ class HomeActivityTest {

@Test
fun loadDetailMovie() {
delayTwoSecond()
onView(withText("Movies")).perform(click())
onView(withId(R.id.rv_movies)).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0,
click()
)
)
delayTwoSecond()
onView(withId(R.id.title_detail)).check(matches(isDisplayed()))
onView(withId(R.id.title_detail)).check(matches(withText(dummyMovie[0].title)))

Expand All @@ -67,10 +73,8 @@ class HomeActivityTest {

@Test
fun loadTvData() {
delayTwoSecond()
onView(withText(R.string.tv_show)).perform(click())

delayTwoSecond()
onView(withId(R.id.rv_tv_show)).check(matches(isDisplayed()))
onView(withId(R.id.rv_tv_show)).perform(
RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(
Expand All @@ -81,18 +85,15 @@ class HomeActivityTest {

@Test
fun loadDetailTvShow() {
delayTwoSecond()
onView(withText("Tv Show")).perform(click())

delayTwoSecond()
onView(withId(R.id.rv_tv_show)).perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0,
click()
)
)

delayTwoSecond()
onView(withId(R.id.title_detail)).check(matches(isDisplayed()))
onView(withId(R.id.title_detail)).check(matches(withText(dummyTvShow[0].name)))

Expand All @@ -102,12 +103,4 @@ class HomeActivityTest {
onView(withId(R.id.images_detail)).check(matches(isDisplayed()))
onView(withId(R.id.share)).check(matches(isDisplayed()))
}

private fun delayTwoSecond() {
try {
Thread.sleep(2000)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Handler
import android.os.Looper
import com.dendi.filmscatalogs.data.source.remote.response.DetailResponse
import com.dendi.filmscatalogs.data.source.remote.response.ListResponse
import com.dendi.filmscatalogs.utils.EspressoIdlingResource
import com.dendi.filmscatalogs.utils.JsonHelper

class RemoteDataSource private constructor(private val jsonHelper: JsonHelper) {
Expand All @@ -22,29 +23,45 @@ class RemoteDataSource private constructor(private val jsonHelper: JsonHelper) {
}

fun getAllMovies(callback: LoadAllMoviesCallback) {
EspressoIdlingResource.increment()
handler.postDelayed(
{ callback.onAllMoviesReceived(jsonHelper.getMovies()) },
{
callback.onAllMoviesReceived(jsonHelper.getMovies())
EspressoIdlingResource.decrement()
},
SERVICE_LATENCY_IN_MILLIS
)
}

fun getAllTvShow(callback: LoadAllTvCallback) {
EspressoIdlingResource.increment()
handler.postDelayed(
{ callback.onAllTvShowReceived(jsonHelper.getTvShow()) },
{
callback.onAllTvShowReceived(jsonHelper.getTvShow())
EspressoIdlingResource.decrement()
},
SERVICE_LATENCY_IN_MILLIS
)
}

fun getDetailMovies(id: Int, callback: LoadDetailMovieCallback) {
EspressoIdlingResource.increment()
handler.postDelayed(
{ callback.onDetailMovieReceived(jsonHelper.getDetailMovies(id)) },
{
callback.onDetailMovieReceived(jsonHelper.getDetailMovies(id))
EspressoIdlingResource.decrement()
},
SERVICE_LATENCY_IN_MILLIS
)
}

fun getDetailTvShow(id: Int, callback: LoadDetailTvShowCallback) {
EspressoIdlingResource.increment()
handler.postDelayed(
{ callback.onDetailTvShowReceived(jsonHelper.getDetailTvShow(id)) },
{
callback.onDetailTvShowReceived(jsonHelper.getDetailTvShow(id))
EspressoIdlingResource.decrement()
},
SERVICE_LATENCY_IN_MILLIS
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.dendi.filmscatalogs.utils

import androidx.test.espresso.idling.CountingIdlingResource

object EspressoIdlingResource {
private const val RESOURCE = "GLOBAL"
val idlingResource = CountingIdlingResource(RESOURCE)

fun increment() {
idlingResource.increment()
}

fun decrement() {
idlingResource.decrement()
}
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ext {
constraintlayoutVersion = '2.0.4'
materialVersion = '1.3.0'
glideVersion = '4.12.0'
idleResourceVersion = '3.2.0'

jUnitVersion = '4.13.2'
espressoVersion = '3.3.0'
Expand Down

0 comments on commit 7c42693

Please sign in to comment.