fix(chat): 기본 이미지를 제거하고 빈 요청을 정리한다
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
package kr.co.vividnext.sodalive.chat.talk.room
|
||||
|
||||
import android.app.Application
|
||||
import android.app.Activity
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.net.Uri
|
||||
import android.os.Looper
|
||||
import android.widget.ImageView
|
||||
import coil.Coil
|
||||
import coil.EventListener
|
||||
import coil.ImageLoader
|
||||
import coil.decode.DataSource
|
||||
import coil.fetch.DrawableResult
|
||||
import coil.fetch.Fetcher
|
||||
import coil.load
|
||||
import coil.request.ErrorResult
|
||||
import coil.request.ImageRequest
|
||||
import coil.request.SuccessResult
|
||||
import coil.transform.CircleCropTransformation
|
||||
import coil.transform.RoundedCornersTransformation
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.IOException
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class ImageLoaderTest {
|
||||
private val requests = mutableListOf<ImageRequest>()
|
||||
private var failures = 0
|
||||
private var successes = 0
|
||||
private lateinit var loader: ImageLoader
|
||||
private lateinit var activity: Activity
|
||||
private val delayedFetch = CompletableDeferred<Unit>()
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
activity = Robolectric.buildActivity(Activity::class.java).setup().visible().get()
|
||||
loader = ImageLoader.Builder(RuntimeEnvironment.getApplication())
|
||||
.dispatcher(Dispatchers.Unconfined)
|
||||
.components {
|
||||
add(
|
||||
Fetcher.Factory<Uri> { data, _, _ ->
|
||||
if (data.scheme != "https") {
|
||||
null
|
||||
} else {
|
||||
Fetcher {
|
||||
if (data.lastPathSegment == "failure") throw IOException("test image fetch failure")
|
||||
if (data.lastPathSegment == "delayed") delayedFetch.await()
|
||||
DrawableResult(ColorDrawable(Color.GREEN), false, DataSource.NETWORK)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
.eventListenerFactory {
|
||||
object : EventListener {
|
||||
override fun onStart(request: ImageRequest) {
|
||||
requests += request
|
||||
}
|
||||
|
||||
override fun onSuccess(request: ImageRequest, result: SuccessResult) {
|
||||
successes++
|
||||
}
|
||||
|
||||
override fun onError(request: ImageRequest, result: ErrorResult) {
|
||||
failures++
|
||||
}
|
||||
}
|
||||
}
|
||||
.build()
|
||||
Coil.setImageLoader(loader)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
activity.finish()
|
||||
Coil.reset()
|
||||
loader.shutdown()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `프로필 URL 요청은 대체 이미지 없이 원본과 원형 변환을 유지한다`() {
|
||||
val image = imageView()
|
||||
|
||||
loadProfileImage(image, "https://example.com/profile")
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
|
||||
val request = requests.single()
|
||||
assertEquals("https://example.com/profile", request.data)
|
||||
assertEquals(listOf(CircleCropTransformation()), request.transformations)
|
||||
assertNull(request.placeholder)
|
||||
assertNull(request.error)
|
||||
assertNull(request.fallback)
|
||||
assertEquals(1, successes)
|
||||
assertNotNull(image.drawable)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `둥근 이미지 URL 요청은 대체 이미지 없이 원본과 모서리 변환을 유지한다`() {
|
||||
val image = imageView()
|
||||
|
||||
loadRoundedImage(image, "https://example.com/rounded", 12f)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
|
||||
val request = requests.single()
|
||||
assertEquals("https://example.com/rounded", request.data)
|
||||
assertEquals(listOf(RoundedCornersTransformation(12f * image.resources.displayMetrics.density)), request.transformations)
|
||||
assertNull(request.placeholder)
|
||||
assertNull(request.error)
|
||||
assertNull(request.fallback)
|
||||
assertEquals(1, successes)
|
||||
assertNotNull(image.drawable)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `이미지 fetch 실패는 이전 그림과 오류 대체 이미지를 남기지 않는다`() {
|
||||
val image = imageView()
|
||||
image.setImageDrawable(ColorDrawable(Color.RED))
|
||||
|
||||
loadProfileImage(image, "https://example.com/failure")
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
|
||||
assertEquals(1, failures)
|
||||
assertTrue(requests.single().data.toString().endsWith("failure"))
|
||||
assertNull(image.drawable)
|
||||
}
|
||||
|
||||
private fun imageView() = ImageView(activity).apply {
|
||||
activity.setContentView(this)
|
||||
layout(0, 0, 64, 64)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `빈 프로필 source는 이전 요청을 해제하고 기본 리소스 요청 없이 그림을 비운다`() {
|
||||
listOf(null, "", " ").forEach { source ->
|
||||
val image = imageView()
|
||||
val previous = image.load("https://example.com/delayed", loader)
|
||||
image.setImageDrawable(ColorDrawable(Color.RED))
|
||||
requests.clear()
|
||||
|
||||
loadProfileImage(image, source)
|
||||
|
||||
assertTrue(previous.isDisposed)
|
||||
assertNull(image.drawable)
|
||||
assertTrue(requests.isEmpty())
|
||||
}
|
||||
delayedFetch.complete(Unit)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(0, successes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `빈 둥근 이미지 source는 이전 요청을 해제하고 지연된 결과도 다시 표시하지 않는다`() {
|
||||
listOf(null, "", " ").forEach { source ->
|
||||
val image = imageView()
|
||||
val previous = image.load("https://example.com/delayed", loader)
|
||||
image.setImageDrawable(ColorDrawable(Color.RED))
|
||||
requests.clear()
|
||||
|
||||
loadRoundedImage(image, source)
|
||||
|
||||
assertTrue(previous.isDisposed)
|
||||
assertNull(image.drawable)
|
||||
assertTrue(requests.isEmpty())
|
||||
}
|
||||
delayedFetch.complete(Unit)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(0, successes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user