From 7c426933731e86ae74e6260cd35ca402f3a91594 Mon Sep 17 00:00:00 2001 From: dendi6 Date: Sun, 2 May 2021 13:40:02 +0700 Subject: [PATCH] idling resource --- app/build.gradle | 1 + .../filmscatalogs/ui/home/HomeActivityTest.kt | 37 ++++++++----------- .../data/source/remote/RemoteDataSource.kt | 25 +++++++++++-- .../utils/EspressoIdlingResource.kt | 16 ++++++++ build.gradle | 1 + 5 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/com/dendi/filmscatalogs/utils/EspressoIdlingResource.kt diff --git a/app/build.gradle b/app/build.gradle index b565bd0..88f7649 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" diff --git a/app/src/androidTest/java/com/dendi/filmscatalogs/ui/home/HomeActivityTest.kt b/app/src/androidTest/java/com/dendi/filmscatalogs/ui/home/HomeActivityTest.kt index c559ec7..180f713 100644 --- a/app/src/androidTest/java/com/dendi/filmscatalogs/ui/home/HomeActivityTest.kt +++ b/app/src/androidTest/java/com/dendi/filmscatalogs/ui/home/HomeActivityTest.kt @@ -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 @@ -19,15 +22,21 @@ 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()) @@ -35,7 +44,6 @@ class HomeActivityTest { @Test fun loadMovies() { - delayTwoSecond() onView(withId(R.id.rv_movies)).check(matches(isDisplayed())) onView(withId(R.id.rv_movies)).perform( RecyclerViewActions.scrollToPosition( @@ -46,7 +54,6 @@ class HomeActivityTest { @Test fun loadDetailMovie() { - delayTwoSecond() onView(withText("Movies")).perform(click()) onView(withId(R.id.rv_movies)).perform( RecyclerViewActions.actionOnItemAtPosition( @@ -54,7 +61,6 @@ class HomeActivityTest { click() ) ) - delayTwoSecond() onView(withId(R.id.title_detail)).check(matches(isDisplayed())) onView(withId(R.id.title_detail)).check(matches(withText(dummyMovie[0].title))) @@ -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( @@ -81,10 +85,8 @@ class HomeActivityTest { @Test fun loadDetailTvShow() { - delayTwoSecond() onView(withText("Tv Show")).perform(click()) - delayTwoSecond() onView(withId(R.id.rv_tv_show)).perform( RecyclerViewActions.actionOnItemAtPosition( 0, @@ -92,7 +94,6 @@ class HomeActivityTest { ) ) - delayTwoSecond() onView(withId(R.id.title_detail)).check(matches(isDisplayed())) onView(withId(R.id.title_detail)).check(matches(withText(dummyTvShow[0].name))) @@ -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() - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/dendi/filmscatalogs/data/source/remote/RemoteDataSource.kt b/app/src/main/java/com/dendi/filmscatalogs/data/source/remote/RemoteDataSource.kt index 7c25f46..554c353 100644 --- a/app/src/main/java/com/dendi/filmscatalogs/data/source/remote/RemoteDataSource.kt +++ b/app/src/main/java/com/dendi/filmscatalogs/data/source/remote/RemoteDataSource.kt @@ -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) { @@ -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 ) } diff --git a/app/src/main/java/com/dendi/filmscatalogs/utils/EspressoIdlingResource.kt b/app/src/main/java/com/dendi/filmscatalogs/utils/EspressoIdlingResource.kt new file mode 100644 index 0000000..36ceae2 --- /dev/null +++ b/app/src/main/java/com/dendi/filmscatalogs/utils/EspressoIdlingResource.kt @@ -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() + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4957210..2b998de 100644 --- a/build.gradle +++ b/build.gradle @@ -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'