feat(event): ID 기반 상세 화면과 기존 진입을 연결한다
This commit is contained in:
@@ -0,0 +1,380 @@
|
||||
package kr.co.vividnext.sodalive.v2.event
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.os.Looper
|
||||
import android.view.View
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import coil.Coil
|
||||
import coil.EventListener
|
||||
import coil.ImageLoader
|
||||
import coil.decode.DataSource
|
||||
import coil.intercept.Interceptor
|
||||
import coil.request.ImageRequest
|
||||
import coil.request.SuccessResult
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import io.reactivex.rxjava3.plugins.RxJavaPlugins
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import io.reactivex.rxjava3.subjects.SingleSubject
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.Constants
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.settings.event.EventItem
|
||||
import kr.co.vividnext.sodalive.v2.event.data.EventDetailApi
|
||||
import kr.co.vividnext.sodalive.v2.event.data.EventDetailRepository
|
||||
import kr.co.vividnext.sodalive.v2.event.data.EventDetailResponse
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.koin.androidx.viewmodel.dsl.viewModel
|
||||
import org.koin.core.context.startKoin
|
||||
import org.koin.core.context.stopKoin
|
||||
import org.koin.dsl.module
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.android.controller.ActivityController
|
||||
import org.robolectric.shadows.ShadowDialog
|
||||
import org.robolectric.shadows.ShadowToast
|
||||
import java.io.IOException
|
||||
import kr.co.vividnext.sodalive.settings.event.EventDetailActivity as LegacyEventDetailActivity
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class EventDetailNavigationTest {
|
||||
private val context: Context get() = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var api: NavigationFakeApi
|
||||
private lateinit var loader: ImageLoader
|
||||
private val imageRequests = mutableListOf<ImageRequest>()
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
api = NavigationFakeApi()
|
||||
startKoin { modules(module { viewModel { EventDetailViewModel(EventDetailRepository(api)) } }) }
|
||||
loader = ImageLoader.Builder(context)
|
||||
.dispatcher(Dispatchers.Unconfined)
|
||||
.components {
|
||||
add(
|
||||
Interceptor { chain ->
|
||||
SuccessResult(ColorDrawable(Color.TRANSPARENT), chain.request, DataSource.MEMORY)
|
||||
}
|
||||
)
|
||||
}
|
||||
.eventListenerFactory {
|
||||
object : EventListener {
|
||||
override fun onStart(request: ImageRequest) {
|
||||
imageRequests += request
|
||||
}
|
||||
}
|
||||
}
|
||||
.build()
|
||||
Coil.setImageLoader(loader)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
Coil.reset()
|
||||
loader.shutdown()
|
||||
stopKoin()
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ID 전용 진입은 Int 초과와 Long 최대 ID를 그대로 조회한다`() {
|
||||
listOf(Int.MAX_VALUE.toLong() + 1L, Long.MAX_VALUE).forEach { id ->
|
||||
val intent = EventDetailActivity.newIntent(context, id)
|
||||
assertEquals(EventDetailActivity::class.java.name, intent.component?.className)
|
||||
assertEquals(setOf(EventDetailActivity.EXTRA_EVENT_ID), intent.extras?.keySet())
|
||||
assertEquals(id, intent.getLongExtra(EventDetailActivity.EXTRA_EVENT_ID, 0L))
|
||||
val controller = open(intent)
|
||||
assertFalse(controller.get().isFinishing)
|
||||
assertEquals(id, api.requests.last())
|
||||
close(controller)
|
||||
}
|
||||
assertEquals(listOf(2147483648L, Long.MAX_VALUE), api.requests)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `누락과 잘못된 타입 및 양수가 아닌 ID는 요청 없이 종료한다`() {
|
||||
listOf(
|
||||
Intent(context, EventDetailActivity::class.java),
|
||||
EventDetailActivity.newIntent(context, 0L),
|
||||
EventDetailActivity.newIntent(context, -1L),
|
||||
Intent(context, EventDetailActivity::class.java).putExtra(EventDetailActivity.EXTRA_EVENT_ID, "7"),
|
||||
Intent(context, EventDetailActivity::class.java).putExtra(EventDetailActivity.EXTRA_EVENT_ID, 7)
|
||||
).forEach { intent ->
|
||||
val controller = open(intent)
|
||||
assertTrue(controller.get().isFinishing)
|
||||
assertNull(shadowOf(controller.get()).nextStartedActivity)
|
||||
close(controller)
|
||||
}
|
||||
assertTrue(api.requests.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `레거시 bridge는 Parcelable ID만 전달하고 즉시 종료한다`() {
|
||||
listOf(2147483648L, Long.MAX_VALUE).forEach { id ->
|
||||
val item = EventItem(id, "stale-thumbnail", "stale-detail", "stale-popup", "https://stale.example")
|
||||
val bridge = Robolectric.buildActivity(
|
||||
LegacyEventDetailActivity::class.java,
|
||||
Intent(context, LegacyEventDetailActivity::class.java).putExtra(Constants.EXTRA_EVENT, item)
|
||||
).setup()
|
||||
val forwarded = shadowOf(bridge.get()).nextStartedActivity
|
||||
assertTrue(bridge.get().isFinishing)
|
||||
assertEquals(EventDetailActivity::class.java.name, forwarded.component?.className)
|
||||
assertEquals(setOf(EventDetailActivity.EXTRA_EVENT_ID), forwarded.extras?.keySet())
|
||||
assertEquals(id, forwarded.getLongExtra(EventDetailActivity.EXTRA_EVENT_ID, 0L))
|
||||
assertNull(shadowOf(bridge.get()).nextStartedActivity)
|
||||
val detail = open(forwarded)
|
||||
assertEquals(id, api.requests.last())
|
||||
detail.get().binding.tvParticipate.performClick()
|
||||
assertEquals("https://server.example/join", shadowOf(detail.get()).nextStartedActivity.dataString)
|
||||
close(detail)
|
||||
bridge.pause().stop().destroy()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `레거시 extra 누락과 잘못된 값은 전달 없이 종료한다`() {
|
||||
listOf(
|
||||
Intent(context, LegacyEventDetailActivity::class.java),
|
||||
Intent(context, LegacyEventDetailActivity::class.java).putExtra(Constants.EXTRA_EVENT, "invalid"),
|
||||
Intent(context, LegacyEventDetailActivity::class.java).putExtra(Constants.EXTRA_EVENT, EventItem(0L, "old")),
|
||||
Intent(context, LegacyEventDetailActivity::class.java).putExtra(Constants.EXTRA_EVENT, EventItem(-1L, "old"))
|
||||
).forEach { intent ->
|
||||
val bridge = Robolectric.buildActivity(LegacyEventDetailActivity::class.java, intent).setup()
|
||||
assertTrue(bridge.get().isFinishing)
|
||||
assertNull(shadowOf(bridge.get()).nextStartedActivity)
|
||||
bridge.pause().stop().destroy()
|
||||
}
|
||||
assertTrue(api.requests.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `서버 success false는 신규 화면을 실제 종료한다`() {
|
||||
api.next = Single.just(ApiResponse(false, null, "잘못된 요청입니다."))
|
||||
val controller = open()
|
||||
assertTrue(controller.get().isFinishing)
|
||||
assertNull(shadowOf(controller.get()).nextStartedActivity)
|
||||
assertEquals(listOf(7L), api.requests)
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `이미지와 링크는 서버 값만 사용하고 참여 클릭 전에는 이동하지 않는다`() {
|
||||
val detail = api.detail(7L).copy(detailImageUrl = "https://server.example/detail.png")
|
||||
api.next = Single.just(ApiResponse(true, detail, null))
|
||||
val controller = open(
|
||||
EventDetailActivity.newIntent(context, 7L).putExtra(Constants.EXTRA_EVENT, EventItem(99L, "old", "old-detail"))
|
||||
)
|
||||
controller.visible()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertTrue(imageRequests.any { it.data == "https://server.example/detail.png" })
|
||||
assertFalse(imageRequests.any { it.data == "old-detail" || it.data == "old" })
|
||||
assertNull(shadowOf(controller.get()).nextStartedActivity)
|
||||
controller.get().binding.tvParticipate.performClick()
|
||||
assertEquals("https://server.example/join", shadowOf(controller.get()).nextStartedActivity.dataString)
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `링크만 있는 성공은 화면을 유지하고 클릭으로만 참여한다`() {
|
||||
val controller = open()
|
||||
val activity = controller.get()
|
||||
assertTrue(ViewModelProvider(activity)[EventDetailViewModel::class.java].state.value is EventDetailUiState.Content)
|
||||
assertFalse(activity.isFinishing)
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
activity.binding.tvParticipate.performClick()
|
||||
assertEquals(Intent.ACTION_VIEW, shadowOf(activity).nextStartedActivity.action)
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nullable 이미지와 누락 또는 blank 링크도 정상 상세이며 이동하지 않는다`() {
|
||||
listOf(null, "", " ").forEach { link ->
|
||||
api.next = Single.just(ApiResponse(true, api.detail(7L).copy(link = link), null))
|
||||
val controller = open()
|
||||
val activity = controller.get()
|
||||
assertFalse(activity.isFinishing)
|
||||
assertTrue(ViewModelProvider(activity)[EventDetailViewModel::class.java].state.value is EventDetailUiState.Content)
|
||||
activity.binding.tvParticipate.performClick()
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
assertNull(ShadowToast.getTextOfLatestToast())
|
||||
close(controller)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `지원하지 않는 서버 링크는 정상 상세를 유지하고 클릭 시 공통 오류만 알린다`() {
|
||||
api.next = Single.just(ApiResponse(true, api.detail(7L).copy(link = "javascript:alert(1)"), null))
|
||||
val controller = open()
|
||||
val activity = controller.get()
|
||||
activity.binding.tvParticipate.performClick()
|
||||
assertFalse(activity.isFinishing)
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
assertEquals(activity.getString(R.string.common_error_unknown), ShadowToast.getTextOfLatestToast())
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `참여 링크를 처리할 앱이 없어도 충돌 없이 공통 오류를 알린다`() {
|
||||
shadowOf(ApplicationProvider.getApplicationContext<Application>()).checkActivities(true)
|
||||
val controller = open()
|
||||
val activity = controller.get()
|
||||
activity.binding.tvParticipate.performClick()
|
||||
assertFalse(activity.isFinishing)
|
||||
assertEquals(activity.getString(R.string.common_error_unknown), ShadowToast.getTextOfLatestToast())
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `오류 modal은 공통 문구만 표시하고 같은 ID 재시도로 성공한다`() {
|
||||
api.next = Single.error(IOException("비공개 원문"))
|
||||
val controller = open()
|
||||
val activity = controller.get()
|
||||
val dialog = ShadowDialog.getLatestDialog()
|
||||
assertNotNull(dialog)
|
||||
assertTrue(dialog.isShowing)
|
||||
assertEquals(
|
||||
activity.getString(R.string.common_error_unknown),
|
||||
dialog.findViewById<android.widget.TextView>(R.id.tv_desc).text
|
||||
)
|
||||
dialog.findViewById<View>(R.id.tv_confirm).performClick()
|
||||
assertFalse(dialog.isShowing)
|
||||
assertEquals(listOf(7L, 7L), api.requests)
|
||||
assertTrue(ViewModelProvider(activity)[EventDetailViewModel::class.java].state.value is EventDetailUiState.Content)
|
||||
assertFalse(activity.isFinishing)
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `오류 modal 닫기는 추가 조회 없이 종료한다`() {
|
||||
api.next = Single.error(IOException())
|
||||
val controller = open()
|
||||
val dialog = ShadowDialog.getLatestDialog()
|
||||
dialog.findViewById<View>(R.id.tv_cancel).performClick()
|
||||
assertFalse(dialog.isShowing)
|
||||
assertTrue(controller.get().isFinishing)
|
||||
assertEquals(listOf(7L), api.requests)
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `로딩 중 재생성은 이전 dialog를 닫고 같은 ViewModel 요청을 유지한다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<EventDetailResponse>>()
|
||||
api.next = pending
|
||||
val controller = open()
|
||||
val previous = controller.get()
|
||||
val viewModel = ViewModelProvider(previous)[EventDetailViewModel::class.java]
|
||||
val oldDialog = ShadowDialog.getLatestDialog()
|
||||
assertTrue(oldDialog.isShowing)
|
||||
val recreated = controller.recreate().get()
|
||||
assertFalse(oldDialog.isShowing)
|
||||
assertSame(viewModel, ViewModelProvider(recreated)[EventDetailViewModel::class.java])
|
||||
assertEquals(listOf(7L), api.requests)
|
||||
val newDialog = ShadowDialog.getLatestDialog()
|
||||
assertTrue(newDialog.isShowing)
|
||||
pending.onSuccess(ApiResponse(true, api.detail(7L), null))
|
||||
assertFalse(newDialog.isShowing)
|
||||
assertFalse(recreated.isFinishing)
|
||||
assertNull(shadowOf(recreated).nextStartedActivity)
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `성공 후 재생성은 재조회나 자동 이동 없이 뒤로가기를 연결한다`() {
|
||||
val controller = open()
|
||||
val activity = controller.recreate().get()
|
||||
assertEquals(listOf(7L), api.requests)
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
activity.binding.toolbar.tvBack.performClick()
|
||||
assertTrue(activity.isFinishing)
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `오류 재생성은 이전 modal을 닫고 재조회 없이 새 modal의 재시도를 연결한다`() {
|
||||
api.next = Single.error(IOException())
|
||||
val controller = open()
|
||||
val oldDialog = ShadowDialog.getLatestDialog()
|
||||
controller.recreate()
|
||||
assertFalse(oldDialog.isShowing)
|
||||
assertEquals(listOf(7L), api.requests)
|
||||
val newDialog = ShadowDialog.getLatestDialog()
|
||||
assertTrue(newDialog.isShowing)
|
||||
newDialog.findViewById<View>(R.id.tv_confirm).performClick()
|
||||
assertEquals(listOf(7L, 7L), api.requests)
|
||||
assertFalse(newDialog.isShowing)
|
||||
close(controller)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `종료 시 로딩과 오류 dialog를 정리하고 늦은 응답은 실행하지 않는다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<EventDetailResponse>>()
|
||||
api.next = pending
|
||||
val loading = open()
|
||||
val activity = loading.get()
|
||||
val dialog = ShadowDialog.getLatestDialog()
|
||||
assertTrue(pending.hasObservers())
|
||||
close(loading)
|
||||
assertFalse(dialog.isShowing)
|
||||
assertFalse(pending.hasObservers())
|
||||
pending.onSuccess(ApiResponse(true, api.detail(7L), null))
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
api.next = Single.error(IOException())
|
||||
val error = open()
|
||||
val errorDialog = ShadowDialog.getLatestDialog()
|
||||
assertTrue(errorDialog.isShowing)
|
||||
close(error)
|
||||
assertFalse(errorDialog.isShowing)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `신규 상세는 외부 공개와 딥링크 등록 없이 manifest에 연결된다`() {
|
||||
val intent = EventDetailActivity.newIntent(context, 7L)
|
||||
val info = context.packageManager.getActivityInfo(intent.component!!, 0)
|
||||
assertFalse(info.exported)
|
||||
assertEquals(EventDetailActivity::class.java.name, info.name)
|
||||
}
|
||||
|
||||
private fun open(intent: Intent = EventDetailActivity.newIntent(context, 7L)) =
|
||||
Robolectric.buildActivity(EventDetailActivity::class.java, intent).setup()
|
||||
|
||||
private fun close(controller: ActivityController<EventDetailActivity>) {
|
||||
controller.pause().stop().destroy()
|
||||
}
|
||||
|
||||
private class NavigationFakeApi : EventDetailApi {
|
||||
val requests = mutableListOf<Long>()
|
||||
var next: Single<ApiResponse<EventDetailResponse>>? = null
|
||||
|
||||
override fun getEventDetail(id: Long, authHeader: String?): Single<ApiResponse<EventDetailResponse>> {
|
||||
requests += id
|
||||
return next.also { next = null } ?: Single.just(ApiResponse(true, detail(id), null))
|
||||
}
|
||||
|
||||
fun detail(id: Long) = EventDetailResponse(id, "server-thumbnail", null, null, "https://server.example/join")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user