Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (70 sloc) 3.2 KB
package org.buffer.android.boilerplate.ui.browse
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.contrib.RecyclerViewActions
import android.support.test.espresso.matcher.ViewMatchers.hasDescendant
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.test.espresso.matcher.ViewMatchers.withText
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import android.support.v7.widget.RecyclerView
import com.nhaarman.mockito_kotlin.whenever
import io.reactivex.Flowable
import org.buffer.android.boilerplate.data.browse.Bufferoo
import org.buffer.android.boilerplate.data.repository.BufferooRepository
import org.buffer.android.boilerplate.ui.R
import org.buffer.android.boilerplate.ui.di.applicationModule
import org.buffer.android.boilerplate.ui.di.browseModule
import org.buffer.android.boilerplate.ui.test.util.BufferooFactory
import org.buffer.android.boilerplate.ui.test.util.RecyclerViewMatcher
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.koin.standalone.StandAloneContext.loadKoinModules
import org.koin.standalone.inject
import org.koin.test.KoinTest
import org.koin.test.declareMock
@RunWith(AndroidJUnit4::class)
class BrowseActivityTest: KoinTest {
val mockBufferooRepository: BufferooRepository by inject()
@Rule @JvmField
val activity = ActivityTestRule<BrowseActivity>(BrowseActivity::class.java, false, false)
@Before
fun setUp() {
loadKoinModules(applicationModule, browseModule)
declareMock<BufferooRepository>()
}
@Test
fun activityLaunches() {
stubBufferooRepositoryGetBufferoos(Flowable.just(BufferooFactory.makeBufferooList(2)))
activity.launchActivity(null)
}
@Test
fun bufferoosDisplay() {
val bufferoos = BufferooFactory.makeBufferooList(1)
stubBufferooRepositoryGetBufferoos(Flowable.just(bufferoos))
activity.launchActivity(null)
checkBufferooDetailsDisplay(bufferoos[0], 0)
}
@Test
fun bufferoosAreScrollable() {
val bufferoos = BufferooFactory.makeBufferooList(20)
stubBufferooRepositoryGetBufferoos(Flowable.just(bufferoos))
activity.launchActivity(null)
bufferoos.forEachIndexed { index, bufferoo ->
onView(withId(R.id.recycler_browse)).perform(RecyclerViewActions.
scrollToPosition<RecyclerView.ViewHolder>(index))
checkBufferooDetailsDisplay(bufferoo, index) }
}
private fun checkBufferooDetailsDisplay(bufferoo: Bufferoo, position: Int) {
onView(RecyclerViewMatcher.withRecyclerView(R.id.recycler_browse).atPosition(position))
.check(matches(hasDescendant(withText(bufferoo.name))))
onView(RecyclerViewMatcher.withRecyclerView(R.id.recycler_browse).atPosition(position))
.check(matches(hasDescendant(withText(bufferoo.title))))
}
private fun stubBufferooRepositoryGetBufferoos(single: Flowable<List<Bufferoo>>) {
whenever(mockBufferooRepository.getBufferoos())
.thenReturn(single)
}
}