feat(dm): 날짜 행과 복귀 스크롤 보존을 연결한다
This commit is contained in:
+108
@@ -0,0 +1,108 @@
|
||||
package kr.co.vividnext.sodalive.v2.main.chat.dm
|
||||
|
||||
import android.app.Application
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatMessageStatus
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatMessageUiItem
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatTimelineItem
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.ui.DmChatMessageAdapter
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class DmChatMessageAdapterTest {
|
||||
private val adapter = DmChatMessageAdapter { }
|
||||
private val header = DmChatTimelineItem.DateHeader("2026-10-16", "2026년 10월 16일 금요일")
|
||||
|
||||
@Test
|
||||
fun `날짜와 송수신 메시지는 서로 다른 view type을 선택한다`() {
|
||||
adapter.submitItems(listOf(header, message(1L, mine = true), message(2L)))
|
||||
|
||||
assertEquals(listOf(3, 1, 2), (0 until adapter.itemCount).map(adapter::getItemViewType))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `날짜와 서버와 local 메시지 stable ID는 충돌하지 않는다`() {
|
||||
adapter.submitItems(listOf(header, message(0L), message(-1L), message(Long.MAX_VALUE), local("Aa"), local("BB")))
|
||||
val ids = (0 until adapter.itemCount).map(adapter::getItemId)
|
||||
|
||||
assertTrue(adapter.hasStableIds())
|
||||
assertEquals(6, ids.toSet().size)
|
||||
assertTrue(ids.none { it == RecyclerView.NO_ID })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `같은 날짜와 메시지는 재제출과 위치 변경 뒤에도 stable ID를 유지한다`() {
|
||||
adapter.submitItems(listOf(header, message(2L)))
|
||||
val headerId = adapter.getItemId(0)
|
||||
val messageId = adapter.getItemId(1)
|
||||
adapter.submitItems(listOf(header.copy(text = "Friday, October 16, 2026"), message(1L), message(2L)))
|
||||
|
||||
assertEquals(headerId, adapter.getItemId(0))
|
||||
assertEquals(messageId, adapter.getItemId(2))
|
||||
assertNotEquals(messageId, adapter.getItemId(1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `pending과 ACK와 재시도 상태는 같은 local identity와 stable ID를 유지한다`() {
|
||||
val pending = local("local-1")
|
||||
adapter.submitItems(listOf(header, pending))
|
||||
val id = adapter.getItemId(1)
|
||||
val changes = Changes()
|
||||
adapter.registerAdapterDataObserver(changes)
|
||||
val ack = pending.copy(item = pending.item.copy(messageId = 42L, status = DmChatMessageStatus.SENT))
|
||||
adapter.submitItems(listOf(header, ack))
|
||||
|
||||
assertEquals(id, adapter.getItemId(1))
|
||||
assertEquals(0, changes.inserted)
|
||||
assertEquals(0, changes.removed)
|
||||
assertEquals(1, changes.changed)
|
||||
adapter.submitItems(listOf(header, pending.copy(item = pending.item.copy(status = DmChatMessageStatus.FAILED))))
|
||||
assertEquals(id, adapter.getItemId(1))
|
||||
assertEquals(2, changes.changed)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `서버 ID가 같은 숫자여도 local 메시지와 서버 조회 메시지는 다른 identity다`() {
|
||||
val local = local("42").let { it.copy(item = it.item.copy(messageId = 42L)) }
|
||||
adapter.submitItems(listOf(local, message(42L)))
|
||||
|
||||
assertNotEquals(adapter.getItemId(0), adapter.getItemId(1))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `날짜 언어와 메시지 본문 변경은 삭제 삽입 없이 내용 변경으로 전달한다`() {
|
||||
adapter.submitItems(listOf(header, message(1L)))
|
||||
val changes = Changes()
|
||||
adapter.registerAdapterDataObserver(changes)
|
||||
adapter.submitItems(listOf(header.copy(text = "2026年10月16日金曜日"), message(1L, text = "수정")))
|
||||
|
||||
assertEquals(0, changes.inserted)
|
||||
assertEquals(0, changes.removed)
|
||||
assertEquals(2, changes.changed)
|
||||
}
|
||||
|
||||
private fun message(id: Long, mine: Boolean = false, text: String = "메시지") = DmChatTimelineItem.Message(
|
||||
DmChatMessageUiItem(id, null, null, mine, text, "상대", "", 1000L, DmChatMessageStatus.SENT)
|
||||
)
|
||||
|
||||
private fun local(id: String) = message(1L, mine = true).let {
|
||||
it.copy(item = it.item.copy(messageId = null, localId = id, status = DmChatMessageStatus.SENDING))
|
||||
}
|
||||
|
||||
private class Changes : RecyclerView.AdapterDataObserver() {
|
||||
var inserted = 0
|
||||
var removed = 0
|
||||
var changed = 0
|
||||
|
||||
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) { inserted += itemCount }
|
||||
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { removed += itemCount }
|
||||
override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) { changed += itemCount }
|
||||
}
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package kr.co.vividnext.sodalive.v2.main.chat.dm
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.LifecycleRegistry
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatMessageStatus
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatMessageUiItem
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatRoomUiState
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatScrollState
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.DmChatTimelineIdentity
|
||||
import kr.co.vividnext.sodalive.v2.main.chat.dm.model.toTimelineItems
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
import java.util.TimeZone
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class DmChatScrollLifecycleTest {
|
||||
private val anchor = DmChatScrollState.Anchor(DmChatTimelineIdentity.ServerMessage(2L), -17)
|
||||
|
||||
@Test
|
||||
fun `백그라운드에서 끝난 prepend는 복귀 후 새 목록의 anchor를 한 번 복원한다`() {
|
||||
val harness = Harness()
|
||||
harness.owner.registry.currentState = Lifecycle.State.STARTED
|
||||
harness.content.value = content(2L)
|
||||
assertTrue(harness.scroll.beginPrepend(content(2L)) { anchor })
|
||||
harness.content.value = content(2L).copy(isLoadingOlder = true)
|
||||
harness.owner.registry.currentState = Lifecycle.State.CREATED
|
||||
|
||||
harness.count.value = 1
|
||||
harness.content.value = content(1L, 2L)
|
||||
assertTrue(harness.restores.isEmpty())
|
||||
harness.owner.registry.currentState = Lifecycle.State.STARTED
|
||||
|
||||
assertEquals(listOf(DmChatScrollState.Position(2, -17)), harness.restores)
|
||||
harness.owner.registry.currentState = Lifecycle.State.CREATED
|
||||
harness.owner.registry.currentState = Lifecycle.State.STARTED
|
||||
assertEquals(1, harness.restores.size)
|
||||
harness.owner.registry.currentState = Lifecycle.State.DESTROYED
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `현재 요청이 없는 재생성에서는 저장된 count를 복원에 사용하지 않는다`() {
|
||||
val harness = Harness()
|
||||
harness.count.value = 9
|
||||
harness.content.value = content(1L, 2L)
|
||||
harness.owner.registry.currentState = Lifecycle.State.STARTED
|
||||
|
||||
assertTrue(harness.restores.isEmpty())
|
||||
assertTrue(harness.scroll.beginPrepend(content(1L, 2L)) { anchor })
|
||||
harness.content.value = content(1L, 2L)
|
||||
assertTrue(harness.restores.isEmpty())
|
||||
harness.owner.registry.currentState = Lifecycle.State.DESTROYED
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `비활성 중 실패하거나 빈 페이지로 끝나도 다음 요청을 막지 않는다`() {
|
||||
listOf<Int?>(null, 0).forEach { result ->
|
||||
val harness = Harness()
|
||||
harness.owner.registry.currentState = Lifecycle.State.STARTED
|
||||
harness.content.value = content(2L)
|
||||
assertTrue(harness.scroll.beginPrepend(content(2L)) { anchor })
|
||||
harness.content.value = content(2L).copy(isLoadingOlder = true)
|
||||
harness.owner.registry.currentState = Lifecycle.State.CREATED
|
||||
result?.let { harness.count.value = it }
|
||||
harness.content.value = content(2L)
|
||||
harness.owner.registry.currentState = Lifecycle.State.STARTED
|
||||
|
||||
assertTrue(harness.restores.isEmpty())
|
||||
assertTrue(harness.scroll.beginPrepend(content(2L)) { anchor })
|
||||
harness.owner.registry.currentState = Lifecycle.State.DESTROYED
|
||||
}
|
||||
}
|
||||
|
||||
private class Owner : LifecycleOwner {
|
||||
val registry = LifecycleRegistry(this)
|
||||
override val lifecycle: Lifecycle get() = registry
|
||||
}
|
||||
|
||||
private class Harness {
|
||||
val owner = Owner()
|
||||
val scroll = DmChatScrollState()
|
||||
val count = MutableLiveData(0)
|
||||
val content = MutableLiveData<DmChatRoomUiState.Content>()
|
||||
val restores = mutableListOf<DmChatScrollState.Position>()
|
||||
|
||||
init {
|
||||
val path = "app/src/main/java/kr/co/vividnext/sodalive/v2/main/chat/dm/DmChatRoomActivity.kt"
|
||||
val source = listOf(File(path), File("../$path")).first { it.exists() }.readText()
|
||||
val bindSource = source.substringAfter("private fun bindViewModel()")
|
||||
.substringBefore("private fun bindContent")
|
||||
val countOrder = bindSource.indexOf("viewModel.prependedMessageCountLiveData.observe(this)")
|
||||
val contentOrder = bindSource.indexOf("viewModel.chatRoomStateLiveData.observe(this)")
|
||||
check(countOrder >= 0 && contentOrder >= 0)
|
||||
val bindCount: () -> Unit = { count.observe(owner, scroll::onPrependCount) }
|
||||
val bindContent: () -> Unit = {
|
||||
content.observe(owner) { state ->
|
||||
val submitted = state.messages.toTimelineItems(Locale.KOREAN, TimeZone.getTimeZone("UTC"))
|
||||
val update = scroll.onContentSubmitted(state.isLoadingOlder, false)
|
||||
if (update is DmChatScrollState.Update.Restore) {
|
||||
scroll.resolveRestore(update, submitted)?.let(restores::add)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Activity의 실제 등록 순서로 LiveData를 연결해 재활성화 전달 순서 회귀를 검증한다.
|
||||
listOf(countOrder to bindCount, contentOrder to bindContent).sortedBy { it.first }.forEach { it.second() }
|
||||
}
|
||||
}
|
||||
|
||||
private fun content(vararg ids: Long) = DmChatRoomUiState.Content(
|
||||
roomId = 10L,
|
||||
opponentNickname = "상대",
|
||||
opponentProfileImageUrl = "",
|
||||
messages = ids.map { id ->
|
||||
DmChatMessageUiItem(id, null, null, false, "메시지", "상대", "", id * 1000, DmChatMessageStatus.SENT)
|
||||
},
|
||||
hasMore = true,
|
||||
nextCursor = 1L
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user