Skip to content

Commit

Permalink
Fix tests on < API 29
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Sep 22, 2020
1 parent a5165ea commit d7ef436
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class CoilTest {
@get:Rule
val composeTestRule = createComposeRule()

private val server = coilTestWebServer()
// Our MockWebServer. We use a response delay to simulate real-world conditions
private val server = coilTestWebServer(responseDelayMs = 200)

@Before
fun setup() {
Expand Down Expand Up @@ -485,7 +486,7 @@ private fun resourceUri(id: Int): Uri {
* [MockWebServer] which returns a valid at the path `/image` and a 404 for anything else.
* We add a small delay to simulate 'real-world' network conditions.
*/
private fun coilTestWebServer(): MockWebServer {
private fun coilTestWebServer(responseDelayMs: Long = 0): MockWebServer {
val dispatcher = object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse = when (request.path) {
"/image" -> {
Expand All @@ -497,10 +498,14 @@ private fun coilTestWebServer(): MockWebServer {
}

MockResponse()
.setHeadersDelay(responseDelayMs, TimeUnit.MILLISECONDS)
.addHeader("Content-Type", "image/jpeg")
.setBody(imageBuffer)
}
else -> MockResponse().setResponseCode(404)
else ->
MockResponse()
.setHeadersDelay(responseDelayMs, TimeUnit.MILLISECONDS)
.setResponseCode(404)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,41 @@

package dev.chrisbanes.accompanist.coil

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.os.StrictMode

class CoilTestApplication : Application() {
override fun onCreate() {
super.onCreate()

// CoilTest uses MockWebServer.url() which internally does a network check, and
// triggers StrictMode. To workaround that in the tests, we allow network on main thread.
val threadPolicy = StrictMode.ThreadPolicy.Builder()
.detectAll()
.permitNetwork()
.build()
StrictMode.setThreadPolicy(threadPolicy)
registerActivityLifecycleCallbacks(
object : DefaultActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedState: Bundle?) {
// [CoilTest] uses MockWebServer.url() which internally does a network check,
// and triggers StrictMode. To workaround that in the tests, we allow network
// on main thread.
val threadPolicy = StrictMode.ThreadPolicy.Builder()
.detectAll()
.permitNetwork()
.build()
StrictMode.setThreadPolicy(threadPolicy)
}
}
)
}
}

/**
* [Application.ActivityLifecycleCallbacks] which adds default empty method implementations.
*/
private interface DefaultActivityLifecycleCallbacks : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedState: Bundle?) = Unit
override fun onActivityStarted(activity: Activity) = Unit
override fun onActivityResumed(activity: Activity) = Unit
override fun onActivityPaused(activity: Activity) = Unit
override fun onActivityStopped(activity: Activity) = Unit
override fun onActivitySaveInstanceState(activity: Activity, savedState: Bundle) = Unit
override fun onActivityDestroyed(activity: Activity) = Unit
}

0 comments on commit d7ef436

Please sign in to comment.