feat(content): 전체 탭 ViewModel을 추가한다

This commit is contained in:
2026-06-25 11:53:51 +09:00
parent 8150b680c1
commit 0a97194b31
4 changed files with 756 additions and 7 deletions

View File

@@ -0,0 +1,487 @@
package kr.co.vividnext.sodalive.v2.main.content
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.SharedPreferenceManager
import kr.co.vividnext.sodalive.home.SeriesPublishedDaysOfWeek
import kr.co.vividnext.sodalive.v2.common.data.ContentSort
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentAllTabRepository
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentAllTabResponse
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentAllType
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentAudioResponse
import kr.co.vividnext.sodalive.v2.main.content.data.MainContentSeriesResponse
import kr.co.vividnext.sodalive.v2.main.content.model.MainContentAllTabUiState
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 ContentAllTabViewModelTest {
private val context: Context = ApplicationProvider.getApplicationContext()
private lateinit var repository: MainContentAllTabRepository
private lateinit var viewModel: ContentAllTabViewModel
@Before
fun setUp() {
setImmediateRxSchedulers()
SharedPreferenceManager.resetForTest()
SharedPreferenceManager.init(context)
SharedPreferenceManager.token = "test-token"
repository = org.mockito.kotlin.mock()
viewModel = ContentAllTabViewModel(
repository = repository,
currentDayOfWeekProvider = { SeriesPublishedDaysOfWeek.WED }
)
}
@After
fun tearDown() {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
SharedPreferenceManager.resetForTest()
}
@Test
fun `초기 로드는 AUDIO LATEST 첫 페이지를 dayOfWeek 없이 요청한다`() {
stubGetContents(
type = MainContentAllType.AUDIO,
response = Single.just(ApiResponse(true, response(audios = listOf(audio(1L))), null))
)
viewModel.loadContents()
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Content
assertEquals(MainContentAllType.AUDIO, state.selectedType)
assertEquals(ContentSort.LATEST, state.selectedSort)
assertEquals(null, state.selectedDayOfWeek)
assertEquals(listOf(1L), state.audioItems.map { it.audioContentId })
verifyGetContents(type = MainContentAllType.AUDIO, page = 0, dayOfWeek = null)
}
@Test
fun `첫 페이지 표시 목록이 비어있으면 totalCount가 있어도 Empty 상태로 표시한다`() {
stubGetContents(
type = MainContentAllType.AUDIO,
response = Single.just(
ApiResponse(
true,
response(
type = MainContentAllType.AUDIO,
totalCount = 3,
audios = emptyList()
),
null
)
)
)
viewModel.loadContents()
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Empty
assertEquals(MainContentAllType.AUDIO, state.selectedType)
assertEquals(ContentSort.LATEST, state.selectedSort)
assertEquals(null, state.selectedDayOfWeek)
assertEquals(3, state.totalCount)
}
@Test
fun `SERIES 변경 Loading은 선택 메타데이터를 유지한다`() {
val pending = SingleSubject.create<ApiResponse<MainContentAllTabResponse>>()
stubGetContents(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
response = pending
)
viewModel.changeType(MainContentAllType.SERIES)
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Loading
assertEquals(MainContentAllType.SERIES, state.selectedType)
assertEquals(ContentSort.LATEST, state.selectedSort)
assertEquals(SeriesPublishedDaysOfWeek.WED, state.selectedDayOfWeek)
assertEquals(0, state.totalCount)
}
@Test
fun `SERIES 첫 페이지 표시 목록이 비어있으면 Empty가 선택 메타데이터와 totalCount를 유지한다`() {
stubGetContents(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
response = Single.just(
ApiResponse(
true,
response(
type = MainContentAllType.SERIES,
totalCount = 7,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
series = emptyList()
),
null
)
)
)
viewModel.changeType(MainContentAllType.SERIES)
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Empty
assertEquals(MainContentAllType.SERIES, state.selectedType)
assertEquals(ContentSort.LATEST, state.selectedSort)
assertEquals(SeriesPublishedDaysOfWeek.WED, state.selectedDayOfWeek)
assertEquals(7, state.totalCount)
}
@Test
fun `SERIES 첫 페이지 실패는 Error가 선택 메타데이터와 메시지를 유지한다`() {
stubGetContents(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
response = Single.just(ApiResponse(false, null, "failed"))
)
viewModel.changeType(MainContentAllType.SERIES)
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Error
assertEquals(MainContentAllType.SERIES, state.selectedType)
assertEquals(ContentSort.LATEST, state.selectedSort)
assertEquals(SeriesPublishedDaysOfWeek.WED, state.selectedDayOfWeek)
assertEquals(0, state.totalCount)
assertEquals("failed", state.message)
}
@Test
fun `SERIES 선택은 현재 디바이스 요일로 첫 페이지를 요청한다`() {
stubGetContents(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
response = Single.just(
ApiResponse(
true,
response(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
series = listOf(series(10L))
),
null
)
)
)
stubGetContents(response = Single.just(ApiResponse(true, response(), null)))
viewModel.loadContents()
viewModel.changeType(MainContentAllType.SERIES)
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Content
assertEquals(MainContentAllType.SERIES, state.selectedType)
assertEquals(SeriesPublishedDaysOfWeek.WED, state.selectedDayOfWeek)
assertEquals(listOf(10L), state.seriesItems.map { it.seriesId })
verifyGetContents(
type = MainContentAllType.SERIES,
page = 0,
dayOfWeek = SeriesPublishedDaysOfWeek.WED
)
}
@Test
fun `SERIES가 아닌 type은 dayOfWeek 없이 요청한다`() {
listOf(
MainContentAllType.AUDIO,
MainContentAllType.FREE,
MainContentAllType.POINT,
MainContentAllType.ORIGINAL
).forEach { type ->
stubGetContents(
type = type,
response = Single.just(ApiResponse(true, response(type = type), null))
)
}
viewModel.changeType(MainContentAllType.AUDIO)
viewModel.changeType(MainContentAllType.FREE)
viewModel.changeType(MainContentAllType.POINT)
viewModel.changeType(MainContentAllType.ORIGINAL)
verifyGetContents(type = MainContentAllType.AUDIO, page = 0, dayOfWeek = null)
verifyGetContents(type = MainContentAllType.FREE, page = 0, dayOfWeek = null)
verifyGetContents(type = MainContentAllType.POINT, page = 0, dayOfWeek = null)
verifyGetContents(type = MainContentAllType.ORIGINAL, page = 0, dayOfWeek = null)
}
@Test
fun `SERIES에서 요일 변경은 변경 요일과 page 0으로 요청한다`() {
stubGetContents(response = Single.just(ApiResponse(true, response(), null)))
stubGetContents(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
response = Single.just(
ApiResponse(
true,
response(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
series = listOf(series(20L))
),
null
)
)
)
stubGetContents(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.FRI,
response = Single.just(
ApiResponse(
true,
response(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.FRI,
series = listOf(series(21L))
),
null
)
)
)
viewModel.loadContents()
viewModel.changeType(MainContentAllType.SERIES)
viewModel.changeDayOfWeek(SeriesPublishedDaysOfWeek.FRI)
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Content
assertEquals(SeriesPublishedDaysOfWeek.FRI, state.selectedDayOfWeek)
assertEquals(0, state.page)
verifyGetContents(
type = MainContentAllType.SERIES,
page = 0,
dayOfWeek = SeriesPublishedDaysOfWeek.FRI
)
}
@Test
fun `정렬 변경은 현재 type과 dayOfWeek를 유지하고 page 0으로 요청한다`() {
stubGetContents(response = Single.just(ApiResponse(true, response(), null)))
stubGetContents(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
response = Single.just(
ApiResponse(
true,
response(
type = MainContentAllType.SERIES,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
series = listOf(series(20L))
),
null
)
)
)
stubGetContents(
type = MainContentAllType.SERIES,
sort = ContentSort.POPULAR,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
response = Single.just(
ApiResponse(
true,
response(
type = MainContentAllType.SERIES,
sort = ContentSort.POPULAR,
dayOfWeek = SeriesPublishedDaysOfWeek.WED,
series = listOf(series(22L))
),
null
)
)
)
viewModel.loadContents()
viewModel.changeType(MainContentAllType.SERIES)
viewModel.changeSort(ContentSort.POPULAR)
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Content
assertEquals(MainContentAllType.SERIES, state.selectedType)
assertEquals(ContentSort.POPULAR, state.selectedSort)
assertEquals(SeriesPublishedDaysOfWeek.WED, state.selectedDayOfWeek)
assertEquals(0, state.page)
verifyGetContents(
type = MainContentAllType.SERIES,
sort = ContentSort.POPULAR,
page = 0,
dayOfWeek = SeriesPublishedDaysOfWeek.WED
)
}
@Test
fun `hasNext가 true이면 loadMore는 다음 페이지를 append한다`() {
stubGetContents(
response = Single.just(ApiResponse(true, response(audios = listOf(audio(1L)), hasNext = true), null))
)
stubGetContents(
page = 1,
response = Single.just(ApiResponse(true, response(page = 1, audios = listOf(audio(2L)), hasNext = false), null))
)
viewModel.loadContents()
viewModel.loadMore()
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Content
assertEquals(1, state.page)
assertEquals(listOf(1L, 2L), state.audioItems.map { it.audioContentId })
assertFalse(state.hasNext)
verifyGetContents(page = 1)
}
@Test
fun `loadMore 요청이 진행 중이면 중복 요청하지 않는다`() {
val pending = SingleSubject.create<ApiResponse<MainContentAllTabResponse>>()
stubGetContents(
response = Single.just(ApiResponse(true, response(audios = listOf(audio(1L)), hasNext = true), null))
)
stubGetContents(page = 1, response = pending)
viewModel.loadContents()
viewModel.loadMore()
viewModel.loadMore()
val loadingState = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Content
assertTrue(loadingState.isLoadingMore)
verifyGetContents(page = 1, times = 1)
pending.onSuccess(ApiResponse(true, response(page = 1, audios = listOf(audio(2L)), hasNext = false), null))
}
@Test
fun `loadMore 실패는 기존 목록을 유지하고 pagination error를 표시한다`() {
stubGetContents(
response = Single.just(ApiResponse(true, response(audios = listOf(audio(1L)), hasNext = true), null))
)
stubGetContents(page = 1, response = Single.just(ApiResponse(false, null, "failed")))
viewModel.loadContents()
viewModel.loadMore()
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Content
assertEquals(listOf(1L), state.audioItems.map { it.audioContentId })
assertFalse(state.isLoadingMore)
assertEquals("failed", state.paginationErrorMessage)
}
@Test
fun `type 변경 후 이전 응답이 늦게 도착하면 현재 목록에 반영하지 않는다`() {
val oldTypePending = SingleSubject.create<ApiResponse<MainContentAllTabResponse>>()
stubGetContents(response = oldTypePending)
stubGetContents(
type = MainContentAllType.FREE,
response = Single.just(ApiResponse(true, response(type = MainContentAllType.FREE, audios = listOf(audio(10L))), null))
)
viewModel.loadContents()
viewModel.changeType(MainContentAllType.FREE)
oldTypePending.onSuccess(ApiResponse(true, response(audios = listOf(audio(1L))), null))
val state = viewModel.allTabStateLiveData.requireValue() as MainContentAllTabUiState.Content
assertEquals(MainContentAllType.FREE, state.selectedType)
assertEquals(listOf(10L), state.audioItems.map { it.audioContentId })
}
private fun stubGetContents(
type: MainContentAllType = MainContentAllType.AUDIO,
sort: ContentSort = ContentSort.LATEST,
page: Int = 0,
size: Int = DEFAULT_PAGE_SIZE,
dayOfWeek: SeriesPublishedDaysOfWeek? = null,
response: Single<ApiResponse<MainContentAllTabResponse>>
) {
whenever(repository.getContents("Bearer test-token", type, sort, page, size, dayOfWeek)).thenReturn(response)
}
private fun verifyGetContents(
type: MainContentAllType = MainContentAllType.AUDIO,
sort: ContentSort = ContentSort.LATEST,
page: Int = 0,
size: Int = DEFAULT_PAGE_SIZE,
dayOfWeek: SeriesPublishedDaysOfWeek? = null,
times: Int? = null
) {
val verification = times?.let { verify(repository, times(it)) } ?: verify(repository)
verification.getContents("Bearer test-token", type, sort, page, size, dayOfWeek)
}
private fun response(
type: MainContentAllType = MainContentAllType.AUDIO,
totalCount: Int = 1,
audios: List<MainContentAudioResponse> = emptyList(),
series: List<MainContentSeriesResponse> = emptyList(),
sort: ContentSort = ContentSort.LATEST,
dayOfWeek: SeriesPublishedDaysOfWeek? = null,
page: Int = 0,
size: Int = DEFAULT_PAGE_SIZE,
hasNext: Boolean = false
) = MainContentAllTabResponse(
type = type,
totalCount = totalCount,
audios = audios,
series = series,
sort = sort,
dayOfWeek = dayOfWeek,
page = page,
size = size,
hasNext = hasNext
)
private fun audio(id: Long) = MainContentAudioResponse(
audioContentId = id,
title = "오디오 $id",
imageUrl = null,
price = 100,
isAdult = false,
isPointAvailable = false,
isFirstContent = false,
isOriginalSeries = false,
creatorNickname = "크리에이터"
)
private fun series(id: Long) = MainContentSeriesResponse(
seriesId = id,
title = "시리즈 $id",
coverImageUrl = null,
creatorNickname = "크리에이터",
isOriginal = false,
isAdult = false
)
private fun <T> LiveData<T>.requireValue(): T? {
var value: T? = null
val observer = Observer<T> { value = it }
observeForever(observer)
removeObserver(observer)
return value
}
private fun setImmediateRxSchedulers() {
val trampoline = { _: Scheduler -> Schedulers.trampoline() }
RxJavaPlugins.setIoSchedulerHandler(trampoline)
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
RxAndroidPlugins.setMainThreadSchedulerHandler { Schedulers.trampoline() }
}
private companion object {
const val DEFAULT_PAGE_SIZE = 20
}
}