feat(creator): FanTalk 탭 상태 관리를 추가한다
This commit is contained in:
@@ -183,6 +183,7 @@ import kr.co.vividnext.sodalive.v2.creator.channel.audio.CreatorChannelAudioView
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.community.CreatorChannelCommunityViewModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelApi
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.CreatorChannelFanTalkViewModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.live.CreatorChannelLiveViewModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.series.CreatorChannelSeriesViewModel
|
||||
import kr.co.vividnext.sodalive.v2.main.MainV2ViewModel
|
||||
@@ -419,6 +420,7 @@ class AppDI(private val context: Context, isDebugMode: Boolean) {
|
||||
viewModel { CreatorChannelAudioViewModel(get()) }
|
||||
viewModel { CreatorChannelSeriesViewModel(get()) }
|
||||
viewModel { CreatorChannelCommunityViewModel(get(), get()) }
|
||||
viewModel { CreatorChannelFanTalkViewModel(get(), get()) }
|
||||
viewModel { PushNotificationListViewModel(get()) }
|
||||
viewModel { CharacterTabViewModel(get()) }
|
||||
viewModel { CharacterDetailViewModel(get()) }
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.fantalk
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.common.SodaLiveApplicationHolder
|
||||
import kr.co.vividnext.sodalive.base.BaseViewModel
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.common.UtcRelativeTimeTextFormatter
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.data.CreatorChannelFanTalkTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.model.CreatorChannelFanTalkUiModel
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.model.toFanTalkUiModels
|
||||
|
||||
class CreatorChannelFanTalkViewModel(
|
||||
private val repository: CreatorChannelRepository,
|
||||
private val relativeTimeTextFormatter: UtcRelativeTimeTextFormatter
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _fanTalkStateLiveData = MutableLiveData<CreatorChannelFanTalkUiState>()
|
||||
val fanTalkStateLiveData: LiveData<CreatorChannelFanTalkUiState>
|
||||
get() = _fanTalkStateLiveData
|
||||
|
||||
private var creatorId: Long = 0L
|
||||
private var isOwner: Boolean = false
|
||||
private var requestGeneration: Int = 0
|
||||
|
||||
fun loadFanTalks(creatorId: Long, isOwner: Boolean) {
|
||||
if (creatorId <= 0) return
|
||||
val shouldSkipReload = this.creatorId == creatorId && this.isOwner == isOwner && _fanTalkStateLiveData.value != null
|
||||
if (shouldSkipReload) return
|
||||
|
||||
this.creatorId = creatorId
|
||||
this.isOwner = isOwner
|
||||
loadFirstPage()
|
||||
}
|
||||
|
||||
fun retryFanTalks() {
|
||||
if (creatorId <= 0) return
|
||||
|
||||
loadFirstPage()
|
||||
}
|
||||
|
||||
fun refreshFanTalks() {
|
||||
if (creatorId <= 0) return
|
||||
|
||||
loadFirstPage()
|
||||
}
|
||||
|
||||
fun loadMore() {
|
||||
val content = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: return
|
||||
if (!content.hasNext || content.isLoadingMore || creatorId <= 0) return
|
||||
|
||||
val generation = requestGeneration
|
||||
_fanTalkStateLiveData.value = content.copy(isLoadingMore = true, paginationErrorMessage = null)
|
||||
requestFanTalks(page = content.page + 1, generation = generation) { response ->
|
||||
val data = response.data
|
||||
val current = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: content
|
||||
if (response.success && data != null) {
|
||||
_fanTalkStateLiveData.value = current.copy(
|
||||
fanTalks = current.fanTalks + data.toFanTalkUiModels(),
|
||||
page = data.page,
|
||||
size = data.size,
|
||||
hasNext = data.hasNext,
|
||||
isLoadingMore = false
|
||||
)
|
||||
} else {
|
||||
_fanTalkStateLiveData.value = current.copy(
|
||||
isLoadingMore = false,
|
||||
paginationErrorMessage = response.message
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun consumePaginationErrorMessage() {
|
||||
val content = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: return
|
||||
if (content.paginationErrorMessage == null) return
|
||||
|
||||
_fanTalkStateLiveData.value = content.copy(paginationErrorMessage = null)
|
||||
}
|
||||
|
||||
fun consumeActionToastMessage() {
|
||||
val content = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: return
|
||||
if (content.actionToastMessage == null) return
|
||||
|
||||
_fanTalkStateLiveData.value = content.copy(actionToastMessage = null)
|
||||
}
|
||||
|
||||
fun reportFanTalk(fanTalkId: Long, reason: String) {
|
||||
val content = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: return
|
||||
compositeDisposable.add(
|
||||
repository.reportFanTalk(fanTalkId = fanTalkId, reason = reason, token = authToken())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
{
|
||||
val current = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: content
|
||||
val message = it.message.takeUnless { message -> message.isNullOrBlank() }
|
||||
?: SodaLiveApplicationHolder.get().getString(R.string.character_comment_report_submitted)
|
||||
_fanTalkStateLiveData.value = current.copy(actionToastMessage = message)
|
||||
},
|
||||
{
|
||||
val current = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: content
|
||||
_fanTalkStateLiveData.value = current.copy(actionToastMessage = it.message)
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun deleteFanTalk(fanTalkId: Long) {
|
||||
val content = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: return
|
||||
compositeDisposable.add(
|
||||
repository.deleteFanTalk(fanTalkId = fanTalkId, token = authToken())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
{
|
||||
if (it.success) {
|
||||
refreshFanTalks()
|
||||
} else {
|
||||
val current = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: content
|
||||
_fanTalkStateLiveData.value = current.copy(actionToastMessage = it.message)
|
||||
}
|
||||
},
|
||||
{
|
||||
val current = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content ?: content
|
||||
_fanTalkStateLiveData.value = current.copy(actionToastMessage = it.message)
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun loadFirstPage() {
|
||||
val generation = ++requestGeneration
|
||||
_fanTalkStateLiveData.value = CreatorChannelFanTalkUiState.Loading
|
||||
requestFanTalks(page = FIRST_PAGE, generation = generation) { response ->
|
||||
val data = response.data
|
||||
if (response.success && data != null) {
|
||||
val fanTalks = data.toFanTalkUiModels()
|
||||
_fanTalkStateLiveData.value = if (fanTalks.isEmpty() || data.fanTalkCount == 0) {
|
||||
CreatorChannelFanTalkUiState.Empty(data.fanTalkCount)
|
||||
} else {
|
||||
data.toContentState(fanTalks = fanTalks)
|
||||
}
|
||||
} else {
|
||||
_fanTalkStateLiveData.value = CreatorChannelFanTalkUiState.Error(response.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestFanTalks(
|
||||
page: Int,
|
||||
generation: Int,
|
||||
onSuccess: (ApiResponse<CreatorChannelFanTalkTabResponse>) -> Unit
|
||||
) {
|
||||
compositeDisposable.add(
|
||||
repository.getFanTalks(
|
||||
creatorId = creatorId,
|
||||
page = page,
|
||||
size = DEFAULT_PAGE_SIZE,
|
||||
token = authToken()
|
||||
)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
{
|
||||
if (generation == requestGeneration) {
|
||||
onSuccess(it)
|
||||
}
|
||||
},
|
||||
{
|
||||
if (generation != requestGeneration) return@subscribe
|
||||
|
||||
val current = _fanTalkStateLiveData.value as? CreatorChannelFanTalkUiState.Content
|
||||
_fanTalkStateLiveData.value = if (current != null && page > FIRST_PAGE) {
|
||||
current.copy(isLoadingMore = false, paginationErrorMessage = it.message)
|
||||
} else {
|
||||
CreatorChannelFanTalkUiState.Error(it.message)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun CreatorChannelFanTalkTabResponse.toContentState(
|
||||
fanTalks: List<CreatorChannelFanTalkUiModel>
|
||||
) = CreatorChannelFanTalkUiState.Content(
|
||||
fanTalkCount = fanTalkCount,
|
||||
fanTalks = fanTalks,
|
||||
page = page,
|
||||
size = size,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun CreatorChannelFanTalkTabResponse.toFanTalkUiModels(): List<CreatorChannelFanTalkUiModel> =
|
||||
fanTalks.toFanTalkUiModels(
|
||||
relativeTimeTextFormatter = relativeTimeTextFormatter,
|
||||
isOwner = isOwner,
|
||||
currentUserId = SharedPreferenceManager.userId
|
||||
)
|
||||
|
||||
private fun authToken(): String = "Bearer ${SharedPreferenceManager.token}"
|
||||
|
||||
companion object {
|
||||
const val DEFAULT_PAGE_SIZE = 20
|
||||
private const val FIRST_PAGE = 0
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface CreatorChannelFanTalkUiState {
|
||||
data object Loading : CreatorChannelFanTalkUiState
|
||||
data class Empty(val fanTalkCount: Int) : CreatorChannelFanTalkUiState
|
||||
data class Error(val message: String?) : CreatorChannelFanTalkUiState
|
||||
data class Content(
|
||||
val fanTalkCount: Int,
|
||||
val fanTalks: List<CreatorChannelFanTalkUiModel>,
|
||||
val page: Int,
|
||||
val size: Int,
|
||||
val hasNext: Boolean,
|
||||
val isLoadingMore: Boolean = false,
|
||||
val paginationErrorMessage: String? = null,
|
||||
val actionToastMessage: String? = null
|
||||
) : CreatorChannelFanTalkUiState
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.fantalk
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
import io.reactivex.rxjava3.core.Scheduler
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import io.reactivex.rxjava3.plugins.RxJavaPlugins
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.common.SodaLiveApplicationHolder
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.AndroidUtcRelativeTimeTextFormatter
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.data.CreatorChannelFanTalkResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.data.CreatorChannelFanTalkTabResponse
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.times
|
||||
import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.whenever
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class CreatorChannelFanTalkActionTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var repository: CreatorChannelRepository
|
||||
private lateinit var viewModel: CreatorChannelFanTalkViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SodaLiveApplicationHolder.init(context as Application)
|
||||
SharedPreferenceManager.init(context)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
SharedPreferenceManager.userId = 10L
|
||||
repository = org.mockito.kotlin.mock()
|
||||
viewModel = CreatorChannelFanTalkViewModel(repository, AndroidUtcRelativeTimeTextFormatter(context))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `신고 성공 시 action toast message가 설정된다`() {
|
||||
stubFirstPage(ids = listOf(1L))
|
||||
whenever(repository.reportFanTalk(1L, "spam", "Bearer test-token"))
|
||||
.thenReturn(Single.just(ApiResponse(true, Any(), "reported")))
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
viewModel.reportFanTalk(1L, "spam")
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals("reported", state.actionToastMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `신고 성공 응답 message가 null이면 접수 완료 fallback toast message가 설정된다`() {
|
||||
stubFirstPage(ids = listOf(1L))
|
||||
whenever(repository.reportFanTalk(1L, "spam", "Bearer test-token"))
|
||||
.thenReturn(Single.just(ApiResponse(true, Any(), null)))
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
viewModel.reportFanTalk(1L, "spam")
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(context.getString(R.string.character_comment_report_submitted), state.actionToastMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `신고 실패 시 기존 content 상태를 유지하고 toast message가 설정된다`() {
|
||||
stubFirstPage(ids = listOf(1L))
|
||||
whenever(repository.reportFanTalk(1L, "spam", "Bearer test-token"))
|
||||
.thenReturn(Single.just(ApiResponse(false, null, "report failed")))
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
viewModel.reportFanTalk(1L, "spam")
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(listOf(1L), state.fanTalks.map { it.fanTalkId })
|
||||
assertEquals("report failed", state.actionToastMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `삭제 성공 시 첫 페이지 refresh가 호출된다`() {
|
||||
whenever(repository.getFanTalks(100L, 0, CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token"))
|
||||
.thenReturn(
|
||||
Single.just(ApiResponse(true, fanTalkResponse(ids = listOf(1L)), null)),
|
||||
Single.just(ApiResponse(true, fanTalkResponse(ids = listOf(2L)), null))
|
||||
)
|
||||
whenever(repository.deleteFanTalk(1L, "Bearer test-token"))
|
||||
.thenReturn(Single.just(ApiResponse(true, Any(), "deleted")))
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
viewModel.deleteFanTalk(1L)
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(listOf(2L), state.fanTalks.map { it.fanTalkId })
|
||||
verify(repository, times(2)).getFanTalks(100L, 0, CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `삭제 실패 시 기존 content 상태를 유지하고 toast message가 설정된다`() {
|
||||
stubFirstPage(ids = listOf(1L))
|
||||
whenever(repository.deleteFanTalk(1L, "Bearer test-token"))
|
||||
.thenReturn(Single.just(ApiResponse(false, null, "delete failed")))
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
viewModel.deleteFanTalk(1L)
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(listOf(1L), state.fanTalks.map { it.fanTalkId })
|
||||
assertEquals("delete failed", state.actionToastMessage)
|
||||
}
|
||||
|
||||
private fun stubFirstPage(ids: List<Long>) {
|
||||
whenever(repository.getFanTalks(100L, 0, CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token"))
|
||||
.thenReturn(Single.just(ApiResponse(true, fanTalkResponse(ids = ids), null)))
|
||||
}
|
||||
|
||||
private fun setImmediateRxSchedulers() {
|
||||
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
|
||||
RxJavaPlugins.setIoSchedulerHandler(trampoline)
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
}
|
||||
|
||||
private fun fanTalkResponse(ids: List<Long>) = CreatorChannelFanTalkTabResponse(
|
||||
fanTalkCount = ids.size,
|
||||
fanTalks = ids.map { fanTalk(it) },
|
||||
page = 0,
|
||||
size = CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE,
|
||||
hasNext = false
|
||||
)
|
||||
|
||||
private fun fanTalk(id: Long) = CreatorChannelFanTalkResponse(
|
||||
fanTalkId = id,
|
||||
writerId = 10L,
|
||||
writerNickname = "writer",
|
||||
writerProfileImageUrl = "profile.png",
|
||||
content = "fan talk $id",
|
||||
createdAtUtc = "2026-06-21T00:00:00Z",
|
||||
creatorReplies = emptyList()
|
||||
)
|
||||
|
||||
private fun <T> LiveData<T>.requireValue(): T? {
|
||||
var value: T? = null
|
||||
val observer = Observer<T> { value = it }
|
||||
observeForever(observer)
|
||||
removeObserver(observer)
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.fantalk
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
import io.reactivex.rxjava3.core.Scheduler
|
||||
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.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.AndroidUtcRelativeTimeTextFormatter
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.data.CreatorChannelFanTalkResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.data.CreatorChannelFanTalkTabResponse
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.times
|
||||
import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.whenever
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class CreatorChannelFanTalkPaginationTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var repository: CreatorChannelRepository
|
||||
private lateinit var viewModel: CreatorChannelFanTalkViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
SharedPreferenceManager.userId = 10L
|
||||
repository = org.mockito.kotlin.mock()
|
||||
viewModel = CreatorChannelFanTalkViewModel(repository, AndroidUtcRelativeTimeTextFormatter(context))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hasNext가 true이면 다음 페이지는 마지막 응답의 page plus 1로 요청하고 append한다`() {
|
||||
stubGetFanTalks(0, Single.just(ApiResponse(true, fanTalkResponse(page = 0, ids = listOf(1L), hasNext = true), null)))
|
||||
stubGetFanTalks(1, Single.just(ApiResponse(true, fanTalkResponse(page = 1, ids = listOf(2L), hasNext = false), null)))
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(1, state.page)
|
||||
assertEquals(listOf(1L, 2L), state.fanTalks.map { it.fanTalkId })
|
||||
assertFalse(state.hasNext)
|
||||
verifyGetFanTalks(page = 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `다음 페이지 로딩 중 중복 load-more 요청은 막고 size 20을 유지한다`() {
|
||||
val pending = SingleSubject.create<ApiResponse<CreatorChannelFanTalkTabResponse>>()
|
||||
stubGetFanTalks(0, Single.just(ApiResponse(true, fanTalkResponse(page = 0, ids = listOf(1L), hasNext = true), null)))
|
||||
stubGetFanTalks(1, pending)
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
viewModel.loadMore()
|
||||
|
||||
val loadingState = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertTrue(loadingState.isLoadingMore)
|
||||
verifyGetFanTalks(page = 1, times = 1)
|
||||
pending.onSuccess(ApiResponse(true, fanTalkResponse(page = 1, ids = listOf(2L), hasNext = false), null))
|
||||
val loadedState = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, loadedState.size)
|
||||
assertEquals(listOf(1L, 2L), loadedState.fanTalks.map { it.fanTalkId })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `다음 페이지 실패는 기존 목록을 유지하고 pagination error message만 설정한다`() {
|
||||
stubGetFanTalks(0, Single.just(ApiResponse(true, fanTalkResponse(page = 0, ids = listOf(1L), hasNext = true), null)))
|
||||
stubGetFanTalks(1, Single.just(ApiResponse(false, null, "failed")))
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
viewModel.loadMore()
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(listOf(1L), state.fanTalks.map { it.fanTalkId })
|
||||
assertFalse(state.isLoadingMore)
|
||||
assertEquals("failed", state.paginationErrorMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `pagination error message는 consume 후 null이 된다`() {
|
||||
stubGetFanTalks(0, Single.just(ApiResponse(true, fanTalkResponse(page = 0, ids = listOf(1L), hasNext = true), null)))
|
||||
stubGetFanTalks(1, Single.just(ApiResponse(false, null, "failed")))
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
viewModel.loadMore()
|
||||
|
||||
viewModel.consumePaginationErrorMessage()
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(null, state.paginationErrorMessage)
|
||||
assertEquals(listOf(1L), state.fanTalks.map { it.fanTalkId })
|
||||
}
|
||||
|
||||
private fun stubGetFanTalks(page: Int, response: Single<ApiResponse<CreatorChannelFanTalkTabResponse>>) {
|
||||
whenever(repository.getFanTalks(100L, page, CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token"))
|
||||
.thenReturn(response)
|
||||
}
|
||||
|
||||
private fun verifyGetFanTalks(page: Int, times: Int? = null) {
|
||||
val verification = times?.let { verify(repository, times(it)) } ?: verify(repository)
|
||||
verification.getFanTalks(100L, page, CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token")
|
||||
}
|
||||
|
||||
private fun setImmediateRxSchedulers() {
|
||||
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
|
||||
RxJavaPlugins.setIoSchedulerHandler(trampoline)
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
}
|
||||
|
||||
private fun fanTalkResponse(page: Int, ids: List<Long>, hasNext: Boolean) = CreatorChannelFanTalkTabResponse(
|
||||
fanTalkCount = ids.size,
|
||||
fanTalks = ids.map { fanTalk(it) },
|
||||
page = page,
|
||||
size = CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun fanTalk(id: Long) = CreatorChannelFanTalkResponse(
|
||||
fanTalkId = id,
|
||||
writerId = 10L,
|
||||
writerNickname = "writer",
|
||||
writerProfileImageUrl = "profile.png",
|
||||
content = "fan talk $id",
|
||||
createdAtUtc = "2026-06-21T00:00:00Z",
|
||||
creatorReplies = emptyList()
|
||||
)
|
||||
|
||||
private fun <T> LiveData<T>.requireValue(): T? {
|
||||
var value: T? = null
|
||||
val observer = Observer<T> { value = it }
|
||||
observeForever(observer)
|
||||
removeObserver(observer)
|
||||
return value
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.fantalk
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins
|
||||
import io.reactivex.rxjava3.core.Scheduler
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import io.reactivex.rxjava3.plugins.RxJavaPlugins
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.AndroidUtcRelativeTimeTextFormatter
|
||||
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.data.CreatorChannelRepository
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.data.CreatorChannelFanTalkResponse
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.fantalk.data.CreatorChannelFanTalkTabResponse
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.never
|
||||
import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.whenever
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class CreatorChannelFanTalkViewModelTest {
|
||||
|
||||
private val context: Context = ApplicationProvider.getApplicationContext()
|
||||
private lateinit var repository: CreatorChannelRepository
|
||||
private lateinit var viewModel: CreatorChannelFanTalkViewModel
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
setImmediateRxSchedulers()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
SharedPreferenceManager.init(context)
|
||||
SharedPreferenceManager.token = "test-token"
|
||||
SharedPreferenceManager.userId = 10L
|
||||
repository = org.mockito.kotlin.mock()
|
||||
viewModel = CreatorChannelFanTalkViewModel(repository, AndroidUtcRelativeTimeTextFormatter(context))
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
RxJavaPlugins.reset()
|
||||
RxAndroidPlugins.reset()
|
||||
SharedPreferenceManager.resetForTest()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `최초 로드는 page 0 size 20으로 FanTalk API를 호출하고 Content를 emit한다`() {
|
||||
stubGetFanTalks(response = Single.just(ApiResponse(true, fanTalkResponse(ids = listOf(1L)), null)))
|
||||
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Content
|
||||
assertEquals(0, state.page)
|
||||
assertEquals(CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, state.size)
|
||||
assertEquals(listOf(1L), state.fanTalks.map { it.fanTalkId })
|
||||
verifyGetFanTalks()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `fanTalkCount가 0이면 Empty 상태가 된다`() {
|
||||
stubGetFanTalks(response = Single.just(ApiResponse(true, fanTalkResponse(fanTalkCount = 0, ids = listOf(1L)), null)))
|
||||
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Empty
|
||||
assertEquals(0, state.fanTalkCount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `표시 가능한 fanTalks가 없으면 Empty 상태가 된다`() {
|
||||
stubGetFanTalks(response = Single.just(ApiResponse(true, fanTalkResponse(ids = emptyList()), null)))
|
||||
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
assertTrue(viewModel.fanTalkStateLiveData.requireValue() is CreatorChannelFanTalkUiState.Empty)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `fanTalkCount가 있지만 첫 페이지 목록이 비어 있으면 count를 유지한 Empty 상태가 된다`() {
|
||||
stubGetFanTalks(response = Single.just(ApiResponse(true, fanTalkResponse(fanTalkCount = 5, ids = emptyList()), null)))
|
||||
|
||||
viewModel.loadFanTalks(100L, isOwner = false)
|
||||
|
||||
val state = viewModel.fanTalkStateLiveData.requireValue() as CreatorChannelFanTalkUiState.Empty
|
||||
assertEquals(5, state.fanTalkCount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `creatorId가 0 이하이면 API를 호출하지 않는다`() {
|
||||
viewModel.loadFanTalks(0L, isOwner = false)
|
||||
|
||||
verify(repository, never()).getFanTalks(any(), any(), any(), any())
|
||||
}
|
||||
|
||||
private fun stubGetFanTalks(
|
||||
page: Int = 0,
|
||||
response: Single<ApiResponse<CreatorChannelFanTalkTabResponse>>
|
||||
) {
|
||||
whenever(repository.getFanTalks(100L, page, CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token"))
|
||||
.thenReturn(response)
|
||||
}
|
||||
|
||||
private fun verifyGetFanTalks(page: Int = 0) {
|
||||
verify(repository).getFanTalks(100L, page, CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE, "Bearer test-token")
|
||||
}
|
||||
|
||||
private fun setImmediateRxSchedulers() {
|
||||
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
|
||||
RxJavaPlugins.setIoSchedulerHandler(trampoline)
|
||||
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
|
||||
}
|
||||
|
||||
private fun fanTalkResponse(
|
||||
page: Int = 0,
|
||||
fanTalkCount: Int? = null,
|
||||
ids: List<Long>,
|
||||
hasNext: Boolean = false
|
||||
) = CreatorChannelFanTalkTabResponse(
|
||||
fanTalkCount = fanTalkCount ?: ids.size,
|
||||
fanTalks = ids.map { fanTalk(it) },
|
||||
page = page,
|
||||
size = CreatorChannelFanTalkViewModel.DEFAULT_PAGE_SIZE,
|
||||
hasNext = hasNext
|
||||
)
|
||||
|
||||
private fun fanTalk(id: Long) = CreatorChannelFanTalkResponse(
|
||||
fanTalkId = id,
|
||||
writerId = 10L,
|
||||
writerNickname = "writer",
|
||||
writerProfileImageUrl = "profile.png",
|
||||
content = "fan talk $id",
|
||||
createdAtUtc = "2026-06-21T00:00:00Z",
|
||||
creatorReplies = emptyList()
|
||||
)
|
||||
|
||||
private fun <T> LiveData<T>.requireValue(): T? {
|
||||
var value: T? = null
|
||||
val observer = Observer<T> { value = it }
|
||||
observeForever(observer)
|
||||
removeObserver(observer)
|
||||
return value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user