feat(chat): 채팅방 시간 표시를 추가한다

This commit is contained in:
2026-06-10 11:29:52 +09:00
parent 4c351da60c
commit 50f0fb3d15
5 changed files with 243 additions and 0 deletions

View File

@@ -0,0 +1,153 @@
package kr.co.vividnext.sodalive.v2.main.chat
import android.app.Application
import android.content.Context
import android.content.res.Configuration
import androidx.test.core.app.ApplicationProvider
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.v2.main.chat.model.formatChatRoomLastMessageTime
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import java.util.Locale
import java.util.TimeZone
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = Application::class)
class ChatRoomTimeTextFormatterTest {
private val nowMillis = 1_781_006_400_000L
private val seoulTimeZone: TimeZone = TimeZone.getTimeZone("Asia/Seoul")
@Test
fun `일주일 이내 시간은 상대 시간 문구로 표시한다`() {
val context = localizedContext(Locale.KOREAN)
assertEquals(
context.getString(R.string.screen_chat_time_just_now),
formatChatRoomLastMessageTime(context, "2026-06-09T11:59:30Z", nowMillis, seoulTimeZone, Locale.KOREAN)
)
assertEquals(
context.getString(R.string.screen_chat_time_minutes, 3),
formatChatRoomLastMessageTime(context, "2026-06-09T11:57:00Z", nowMillis, seoulTimeZone, Locale.KOREAN)
)
assertEquals(
context.getString(R.string.screen_chat_time_hours, 2),
formatChatRoomLastMessageTime(context, "2026-06-09T10:00:00Z", nowMillis, seoulTimeZone, Locale.KOREAN)
)
assertEquals(
context.getString(R.string.screen_chat_time_days, 7),
formatChatRoomLastMessageTime(context, "2026-06-02T12:00:00Z", nowMillis, seoulTimeZone, Locale.KOREAN)
)
}
@Test
fun `일주일이 지난 같은 연도 날짜는 locale별 날짜 문구로 표시한다`() {
assertEquals(
"6월 1일",
formatChatRoomLastMessageTime(
localizedContext(Locale.KOREAN),
"2026-06-01T12:00:00Z",
nowMillis,
seoulTimeZone,
Locale.KOREAN
)
)
assertEquals(
"Jun 1",
formatChatRoomLastMessageTime(
localizedContext(Locale.ENGLISH),
"2026-06-01T12:00:00Z",
nowMillis,
seoulTimeZone,
Locale.ENGLISH
)
)
assertEquals(
"6月1日",
formatChatRoomLastMessageTime(
localizedContext(Locale.JAPANESE),
"2026-06-01T12:00:00Z",
nowMillis,
seoulTimeZone,
Locale.JAPANESE
)
)
}
@Test
fun `전년도 날짜는 yyyy 점 MM 점 dd 형식으로 표시한다`() {
assertEquals(
"2025.12.31",
formatChatRoomLastMessageTime(
localizedContext(Locale.KOREAN),
"2025-12-31T12:00:00Z",
nowMillis,
seoulTimeZone,
Locale.KOREAN
)
)
}
@Test
fun `대상 timezone 변환 결과의 로컬 날짜를 사용한다`() {
assertEquals(
"6월 1일",
formatChatRoomLastMessageTime(
localizedContext(Locale.KOREAN),
"2026-05-31T15:30:00Z",
nowMillis,
seoulTimeZone,
Locale.KOREAN
)
)
}
@Test
fun `분 단위 offset이 포함된 ISO 시간은 offset 전체를 반영한다`() {
val context = localizedContext(Locale.KOREAN)
assertEquals(
context.getString(R.string.screen_chat_time_minutes, 30),
formatChatRoomLastMessageTime(
context,
"2026-06-09T17:00:00+05:30",
nowMillis,
seoulTimeZone,
Locale.KOREAN
)
)
}
@Test
fun `blank 또는 파싱 실패 시간은 방금 전으로 표시한다`() {
val context = localizedContext(Locale.KOREAN)
assertEquals(
context.getString(R.string.screen_chat_time_just_now),
formatChatRoomLastMessageTime(context, "", nowMillis, seoulTimeZone, Locale.KOREAN)
)
assertEquals(
context.getString(R.string.screen_chat_time_just_now),
formatChatRoomLastMessageTime(context, "not-a-date", nowMillis, seoulTimeZone, Locale.KOREAN)
)
assertEquals(
context.getString(R.string.screen_chat_time_just_now),
formatChatRoomLastMessageTime(
context,
"2026-06-09T12:00:00Zjunk",
nowMillis,
seoulTimeZone,
Locale.KOREAN
)
)
}
private fun localizedContext(locale: Locale): Context {
val context = ApplicationProvider.getApplicationContext<Context>()
val configuration = Configuration(context.resources.configuration).apply { setLocale(locale) }
return context.createConfigurationContext(configuration)
}
}