Skip to content

Instantly share code, notes, and snippets.

View sergei-lapin's full-sized avatar

Sergey Lapin sergei-lapin

  • Vivid Money
  • Berlin, Germany
View GitHub Profile
@sergei-lapin
sergei-lapin / PrefetchViewPool.kt
Created August 5, 2021 06:55
setPrefetchBound
fun setPrefetchBound(viewType: Int, count: Int) {
recycledViewsBounds[viewType] = max(defaultMaxRecycledViews, count)
viewHolderSupplier.setPrefetchBound(viewType, count)
}
@sergei-lapin
sergei-lapin / PrefetchViewPool.kt
Created August 5, 2021 06:55
Full Consumer code
class PrefetchViewPool(
private val defaultMaxRecycledViews: Int,
private val viewHolderSupplier: ViewHolderSupplier
) : RecyclerView.RecycledViewPool() {
private val recycledViewsBounds = mutableMapOf<Int, Int>()
init {
attachToPreventFromClearing()
viewHolderSupplier.viewHolderConsumer = ::putViewFromSupplier
abstract class BaseTestCase : MockModelScope {
@get:Rule
val rule: RuleChain by lazy {
var chain = RuleChain.outerRule(RetryTestRule())
if (isMockServerEnabled) chain = chain.around(mockWebServerRule)
chain = chain.around(activityTestRule)
chain
}
class MockDispatcher(httpMocks: Set<MockModelWrapper>) : Dispatcher() {
private val httpMocksMap: Map<RequestMarkers, MockResponseModel>
private val requestsCount = mutableMapOf<RequestMarkers, Int>()
init {
httpMocksMap = mutableMapOf<RequestMarkers, MockResponseModel>().apply {
putAll(httpMocks.map { wrapper -> wrapper.markers to wrapper.responseModel })
}
}
class MockResponseModel(
private val code: Int = 200,
private val headers: Map<String, String> = mapOf(),
private val body: String = "{}",
val retryAttempts: Map<Int, MockResponseModel> = mapOf()
) {
val asMockResponse: MockResponse
get() = MockResponse()
.setResponseCode(code)
data class RequestMarkers(
val path: String,
val methodName: String? = null
)
val RecordedRequest.asRequestMarkers: RequestMarkers
get() {
val jsonBody = runCatching { JSONObject(body.inputStream().readToString()) }.getOrNull()
return RequestMarkers(
class MockModelWrapper(
val markers: RequestMarkers,
val responseModel: MockResponseModel
)
staging {
buildConfigField 'String', 'BASE_URL', '\"https://some_staging_url.com\"'
}
production {
buildConfigField 'String', 'BASE_URL', '\"https://some_production_url.com\"'
}
instrumented {
buildConfigField 'String', 'BASE_URL', '\"http://localhost:8000\"'
staging {
buildConfigField 'String', 'BASE_URL', '\"https://some_staging_url.com\"'
}
production {
buildConfigField 'String', 'BASE_URL', '\"https://some_production_url.com\"'
}
class MockWebServerRule(httpMocks: Set<MockModelWrapper>) : TestRule {
private val server = MockWebServer()
init {
server.setDispatcher(MockDispatcher(httpMocks))
val port = BuildConfig.BASE_URL.substringAfterLast(':').toInt()
server.start(port)
}