Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Espresso Idling Resource

Espresso Idling Resource

Takuji Nishibayashi

August 29, 2017
Tweet

More Decks by Takuji Nishibayashi

Other Decks in Technology

Transcript

  1. Loves • ! (AVG 175) • " # • h,p:/

    /photo.takuji31.jp • $ • h,p:/ /nazuna.takuji31.jp • Android • Kotlin
  2. SimpleIdlingResource class SimpleIdlingResource(private val name: String) : IdlingResource { private

    var callback: IdlingResource.ResourceCallback? = null var isIdle: Boolean = false set(value) { field = value if (value) { callback?.run { onTransitionToIdle() } } } override fun getName(): String = this.name override fun isIdleNow(): Boolean = isIdle override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) { this.callback = callback } }
  3. ViewModel class ViewModel { var artists: List<Artist> = emptyList() var

    loadingDisposable: Disposable? = null @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) val loadingIdlingResource : SimpleIdlingResource = SimpleIdlingResource("IdlingResourceViewModelLoading") }
  4. ViewModel class ViewModel { fun onCreate() { // fake API

    request fakeReload() } private fun fakeReload() { loadingDisposable = Single .timer(3, TimeUnit.SECONDS) .doOnSubscribe { loadingIdlingResource.isIdle = false } .doFinally { loadingIdlingResource.isIdle = true } .subscribe { _, _ -> artists = Artist.list } } }
  5. test class IdlingResourceActivityTest { @JvmField @Rule val rule : IntentsTestRule<IdlingResourceActivity>

    = IntentsTestRule(IdlingResourceActivity::class.java, true, true) var initializeIdlingResource : IdlingResource? = null }
  6. test class IdlingResourceActivityTest { @Before fun setUp() { initializeIdlingResource =

    rule.activity.viewModel.loadingIdlingResource IdlingRegistry.getInstance().register(initializeIdlingResource) } @After fun tearDown() { initializeIdlingResource?.run { IdlingRegistry.getInstance().unregister(this) initializeIdlingResource = null } } }
  7. test class IdlingResourceActivityTest { @Test fun testReload() { // remove

    3 items repeat(3) { openActionBarOverflowOrOptionsMenu(rule.activity) onView(withText("Remove first")).perform(click()) } onView(withId(R.id.swipeRefreshLayout)).perform(swipeDown()) onView(withId(R.id.recyclerView)).check { view, _ -> val recyclerView = view as RecyclerView assertEquals(recyclerView.childCount, Artist.list.size) assertEquals(recyclerView.adapter.itemCount, Artist.list.size) } } }
  8. !