feat(gift): 선물 신청 후 상세 화면을 연다

This commit is contained in:
2026-10-06 15:29:25 +09:00
parent 5eef1e1cc4
commit fe13dcf2bb
4 changed files with 577 additions and 21 deletions
@@ -4,8 +4,14 @@ import android.app.Activity
import android.app.Application
import android.content.Intent
import android.os.Bundle
import android.os.Looper
import android.os.Parcel
import android.text.Spanned
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.widget.CheckBox
import android.widget.TextView
import androidx.core.view.children
import androidx.lifecycle.ViewModelProvider
@@ -21,14 +27,19 @@ import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.v2.gift.data.GiftApi
import kr.co.vividnext.sodalive.v2.gift.data.GiftCancelResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftAddressResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftCategoryResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftCreateRequest
import kr.co.vividnext.sodalive.v2.gift.data.GiftCreateResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftDeliveryResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailInfoResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftFormOptionsResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftInboxResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftRepository
import kr.co.vividnext.sodalive.v2.gift.data.GiftRecipientAddressRequest
import kr.co.vividnext.sodalive.v2.gift.data.GiftRecipientAddressResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftStatusTimelineResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftSizeResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftTrackingRequest
import kr.co.vividnext.sodalive.v2.gift.data.GiftTrackingResponse
import org.junit.After
@@ -45,6 +56,7 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.Robolectric
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
import org.robolectric.shadows.ShadowToast
import org.robolectric.shadows.ShadowDialog
import org.koin.core.context.startKoin
@@ -75,6 +87,7 @@ class GiftNavigationTest {
viewModel { GiftTrackingViewModel(GiftRepository(api)) }
viewModel { GiftCancelViewModel(GiftRepository(api)) }
viewModel { GiftRecipientAddressViewModel(GiftRepository(api)) }
viewModel { GiftSendRequestViewModel(GiftRepository(api)) }
}
)
}
@@ -647,7 +660,501 @@ class GiftNavigationTest {
assertEquals(applicationNo, intent.getStringExtra(GiftDetailActivity.EXTRA_APPLICATION_NO))
assertEquals(setOf(GiftDetailActivity.EXTRA_APPLICATION_NO), intent.extras?.keySet())
}
@Test
@Config(qualifiers = "ko")
fun `선물 신청 성공은 신청번호만 담은 상세를 열고 신청 화면을 종료한다`() {
api.createGiftResponse = Single.just(ApiResponse(true, createResponse(), null))
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
activity.binding.btnSend.performClick()
val launched = shadowOf(activity).nextStartedActivity
assertEquals(GiftDetailActivity::class.java.name, launched.component?.className)
assertEquals("GIFT-001", launched.getStringExtra(GiftDetailActivity.EXTRA_APPLICATION_NO))
assertEquals(setOf(GiftDetailActivity.EXTRA_APPLICATION_NO), launched.extras?.keySet())
assertEquals(activity.getString(R.string.gift_send_submit_success), ShadowToast.getTextOfLatestToast())
assertTrue(activity.isFinishing)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
fun `신청 응답 data가 없거나 신청번호가 blank면 상세 이동 없이 입력 상태를 유지한다`() {
listOf(
ApiResponse<GiftCreateResponse>(true, null, null) to activityString(R.string.common_error_unknown),
ApiResponse(true, createResponse(" "), "신청번호 누락") to "신청번호 누락"
).forEach { (response, expectedMessage) ->
api.createGiftResponse = Single.just(response)
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
activity.binding.btnSend.performClick()
assertNull(shadowOf(activity).nextStartedActivity)
assertFalse(activity.isFinishing)
assertEquals(expectedMessage, ShadowToast.getTextOfLatestToast())
assertEquals("보내는사람", activity.binding.etSenderName.text.toString())
assertTrue(activity.binding.btnSend.isEnabled)
controller.pause().stop().destroy()
}
}
@Test
@Config(qualifiers = "ko")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
fun `이용약관 필수 표기는 동의와 CTA 상태를 바꾸지 않고 Notion을 연다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
assertRequiredLinkOpensNotionWithoutChangingForm(activity, activity.binding.cbTerms)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
fun `여러 줄 필수 표기의 각 줄은 Notion을 열고 동의와 CTA를 유지한다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
val terms = activity.binding.cbTerms
terms.gravity = Gravity.TOP
terms.layoutRequiredLinkAcrossLines()
val linkLineCount = terms.requiredLinkLineCenters().size
assertTrue("(필수) span은 실제 여러 줄에 있어야 한다", linkLineCount > 1)
controller.pause().stop().destroy()
repeat(linkLineCount) { index ->
val lineController = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val lineActivity = lineController.get()
completeGiftSendForm(lineActivity)
val lineTerms = lineActivity.binding.cbTerms
lineTerms.gravity = Gravity.TOP
lineTerms.layoutRequiredLinkAcrossLines()
val center = lineTerms.requiredLinkLineCenters()[index]
val pointDescription = lineTerms.describeRequiredLinkPoint(center)
lineTerms.dispatchTap(center)
val launched = shadowOf(lineActivity).nextStartedActivity
assertNotNull("(필수) line $index 터치는 외부 링크를 열어야 한다: $pointDescription", launched)
assertEquals(Intent.ACTION_VIEW, launched?.action)
assertEquals("https://app.notion.com/p/3eb27b359673802b937ae741ab7b4ad3", launched?.data.toString())
assertTrue(lineTerms.isChecked)
assertTrue(lineActivity.binding.btnSend.isEnabled)
lineController.pause().stop().destroy()
}
}
@Test
@Config(qualifiers = "ko")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
fun `필수 표기 마지막 괄호 오른쪽 내부는 동의와 CTA를 유지하고 Notion을 연다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
val terms = activity.binding.cbTerms
terms.dispatchTap(terms.glyphRightInteriorPoint(terms.requiredLinkEnd() - 1))
val launched = shadowOf(activity).nextStartedActivity
assertEquals(Intent.ACTION_VIEW, launched.action)
assertEquals("https://app.notion.com/p/3eb27b359673802b937ae741ab7b4ad3", launched.data.toString())
assertTrue(terms.isChecked)
assertTrue(activity.binding.btnSend.isEnabled)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
fun `필수 표기 직전 glyph 오른쪽 내부는 Notion을 열지 않고 CheckBox를 토글한다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
val terms = activity.binding.cbTerms
terms.isChecked = false
terms.dispatchTap(terms.glyphRightInteriorPoint(terms.requiredLinkStart() - 1))
assertNull(shadowOf(activity).nextStartedActivity)
assertTrue(terms.isChecked)
assertTrue(activity.binding.btnSend.isEnabled)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
fun `개인정보 필수 표기는 동의와 CTA 상태를 바꾸지 않고 Notion을 연다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
assertRequiredLinkOpensNotionWithoutChangingForm(activity, activity.binding.cbPrivacy)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
fun `필수 표기 밖 CheckBox 빈 영역은 외부 링크를 열지 않고 동의와 CTA를 갱신한다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
val terms = activity.binding.cbTerms
terms.isChecked = false
assertFalse(activity.binding.btnSend.isEnabled)
terms.dispatchTap(terms.blankAreaCenter())
assertNull(shadowOf(activity).nextStartedActivity)
assertTrue(terms.isChecked)
assertTrue(activity.binding.btnSend.isEnabled)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
fun `필수 표기 수직 빈 영역은 외부 링크를 열지 않고 동의와 CTA를 갱신한다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
val terms = activity.binding.cbTerms
terms.isChecked = false
assertFalse(activity.binding.btnSend.isEnabled)
terms.dispatchTap(terms.verticalBlankAreaAtRequiredLink())
assertNull(shadowOf(activity).nextStartedActivity)
assertTrue(terms.isChecked)
assertTrue(activity.binding.btnSend.isEnabled)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
fun `필수 표기에서 벗어나 release하면 외부 링크나 동의 변경이 없다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
val terms = activity.binding.cbTerms
terms.dispatchGesture(
terms.requiredLinkCenter(),
MotionEvent.ACTION_MOVE to terms.blankAreaCenter(),
MotionEvent.ACTION_UP to terms.blankAreaCenter()
)
assertNull(shadowOf(activity).nextStartedActivity)
assertTrue(terms.isChecked)
assertTrue(activity.binding.btnSend.isEnabled)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
fun `필수 표기에서 cancel하면 외부 링크나 동의 변경이 없다`() {
val controller = Robolectric.buildActivity(
GiftSendRequestActivity::class.java,
GiftSendRequestActivity.newIntent(ApplicationProvider.getApplicationContext(), 99L, "크리에이터")
).setup()
val activity = controller.get()
completeGiftSendForm(activity)
val terms = activity.binding.cbTerms
terms.dispatchGesture(
terms.requiredLinkCenter(),
MotionEvent.ACTION_CANCEL to terms.blankAreaCenter()
)
assertNull(shadowOf(activity).nextStartedActivity)
assertTrue(terms.isChecked)
assertTrue(activity.binding.btnSend.isEnabled)
controller.pause().stop().destroy()
}
@Test
@Config(qualifiers = "ko")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
fun `받은 사람 이용약관 필수 터치는 Notion을 열고 입력과 동의와 CTA를 유지한다`() {
assertRecipientAgreementLink(GiftDetailAddressAgreement.TERMS)
}
@Test
@Config(qualifiers = "ko")
@GraphicsMode(GraphicsMode.Mode.NATIVE)
fun `받은 사람 개인정보 필수 터치는 Notion을 열고 입력과 동의와 CTA를 유지한다`() {
assertRecipientAgreementLink(GiftDetailAddressAgreement.PRIVACY)
}
private fun assertRecipientAgreementLink(agreement: GiftDetailAddressAgreement) {
api.direction = "RECEIVED"
val controller = Robolectric.buildActivity(
GiftDetailActivity::class.java,
GiftDetailActivity.newIntent(ApplicationProvider.getApplicationContext(), "received-link")
).setup().visible()
val activity = controller.get()
val recipient = ViewModelProvider(activity)[GiftRecipientAddressViewModel::class.java]
val checkBox = when (agreement) {
GiftDetailAddressAgreement.TERMS -> activity.binding.cbTerms
GiftDetailAddressAgreement.PRIVACY -> activity.binding.cbPrivacy
}
val spans = (checkBox.text as? Spanned)?.getSpans(0, checkBox.text.length, ClickableSpan::class.java)
assertEquals("받은 사람 필수 문구의 전용 링크가 있어야 한다", 1, spans?.size ?: 0)
listOf(false, true).forEach { agreed ->
val form = GiftRecipientAddressFormState("수령인", "01012345678", "12345", "입력 주소", "상세", agreed, agreed)
recipient.updateForm(form)
checkBox.dispatchTap(checkBox.requiredLinkCenter())
val launched = shadowOf(activity).nextStartedActivity
assertNotNull(launched)
assertEquals(Intent.ACTION_VIEW, launched.action)
assertEquals("https://app.notion.com/p/3eb27b359673802b937ae741ab7b4ad3", launched.data.toString())
assertEquals(form, recipient.formState.value)
assertEquals(form.isSubmitEnabled, activity.binding.receive.isEnabled)
assertEquals(agreed, activity.binding.cbTerms.isChecked)
assertEquals(agreed, activity.binding.cbPrivacy.isChecked)
assertEquals(form.recipientName, activity.binding.etRecipientName.text.toString())
assertEquals(form.recipientPhoneNumber, activity.binding.etPhoneNumber.text.toString())
assertEquals(form.recipientZipCode, activity.binding.tvPostcode.text.toString())
assertEquals(form.recipientAddress, activity.binding.tvAddress.text.toString())
assertEquals(form.recipientAddressDetail, activity.binding.etDetailAddress.text.toString())
assertNull(shadowOf(activity).nextStartedActivity)
}
assertTrue(api.recipientSubmissions.isEmpty())
assertEquals(listOf("received-link"), api.requests)
assertEquals(Activity.RESULT_CANCELED, shadowOf(activity).resultCode)
controller.pause().stop().destroy()
}
private fun completeGiftSendForm(activity: GiftSendRequestActivity) = with(activity.binding) {
etSenderName.setText("보내는사람")
etPhoneNumber.setText("01000000000")
tvPostcode.text = "12345"
tvAddress.text = "기본 주소"
etDetailAddress.setText("상세 주소")
spinnerCategory.setSelection(1)
cbTerms.isChecked = true
cbPrivacy.isChecked = true
assertTrue(btnSend.isEnabled)
}
private fun assertRequiredLinkOpensNotionWithoutChangingForm(activity: GiftSendRequestActivity, agreement: CheckBox) {
val termsAgreed = activity.binding.cbTerms.isChecked
val privacyAgreed = activity.binding.cbPrivacy.isChecked
val ctaEnabled = activity.binding.btnSend.isEnabled
agreement.dispatchTap(agreement.requiredLinkCenter())
val launched = shadowOf(activity).nextStartedActivity
assertEquals(Intent.ACTION_VIEW, launched.action)
assertEquals("https://app.notion.com/p/3eb27b359673802b937ae741ab7b4ad3", launched.data.toString())
assertEquals(termsAgreed, activity.binding.cbTerms.isChecked)
assertEquals(privacyAgreed, activity.binding.cbPrivacy.isChecked)
assertEquals(ctaEnabled, activity.binding.btnSend.isEnabled)
}
private fun CheckBox.requiredLink(): ClickableSpan {
val spanned = text as Spanned
return spanned.getSpans(0, spanned.length, ClickableSpan::class.java).single()
}
private fun CheckBox.requiredLinkStart(): Int = (text as Spanned).getSpanStart(requiredLink())
private fun CheckBox.requiredLinkEnd(): Int = (text as Spanned).getSpanEnd(requiredLink())
private fun CheckBox.requiredLinkCenter(): Pair<Float, Float> = requiredLinkLineCenters().first()
private fun CheckBox.glyphRightInteriorPoint(offset: Int): Pair<Float, Float> {
val textLayout = checkNotNull(layout)
val line = textLayout.getLineForOffset(offset)
val first = textLayout.getPrimaryHorizontal(offset)
val second = textLayout.getPrimaryHorizontal(offset + 1)
val left = minOf(first, second)
val right = maxOf(first, second)
check(right > left)
val horizontal = left + (right - left) * 0.75f
check(textLayout.getOffsetForHorizontal(line, horizontal) == offset + 1)
return touchPointWithinLine(horizontal, line)
}
private fun CheckBox.layoutRequiredLinkAcrossLines() {
val spanned = text as Spanned
val link = requiredLink()
val spanStart = spanned.getSpanStart(link)
val spanEnd = spanned.getSpanEnd(link)
check(
(48..240).any { width ->
measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
)
layout(0, 0, measuredWidth, measuredHeight)
val textLayout = checkNotNull(layout)
requiredLinkLineCenters().size > 1 &&
textLayout.getPrimaryHorizontal(spanStart) > textLayout.getPrimaryHorizontal(spanEnd)
}
) { "(필수) span이 여러 줄의 역순 수평 좌표를 만들지 못했다" }
}
private fun CheckBox.requiredLinkLineCenters(): List<Pair<Float, Float>> {
val spanned = text as Spanned
val link = requiredLink()
val spanStart = spanned.getSpanStart(link)
val spanEnd = spanned.getSpanEnd(link)
val textLayout = checkNotNull(layout)
return (0 until textLayout.lineCount).mapNotNull { line ->
val lineSpanStart = maxOf(spanStart, textLayout.getLineStart(line))
val lineSpanEnd = minOf(spanEnd, textLayout.getLineEnd(line))
if (lineSpanStart >= lineSpanEnd) return@mapNotNull null
val nextOffset = lineSpanStart + 1
val horizontal = (
textLayout.getPrimaryHorizontal(lineSpanStart) +
textLayout.getPrimaryHorizontal(nextOffset)
) / 2f
touchPointWithinLine(horizontal, line)
}
}
private fun CheckBox.touchPointWithinLine(horizontal: Float, line: Int): Pair<Float, Float> {
val textLayout = checkNotNull(layout)
val vertical = (textLayout.getLineTop(line) until textLayout.getLineBottom(line)).firstOrNull {
textLayout.getLineForVertical(it) == line
} ?: error("line ${line}의 터치 가능한 y 좌표가 없다")
return Pair(
horizontal + totalPaddingLeft - scrollX,
vertical.toFloat() + totalPaddingTop - scrollY
)
}
private fun CheckBox.describeRequiredLinkPoint(point: Pair<Float, Float>): String {
val textLayout = checkNotNull(layout)
val vertical = point.second - totalPaddingTop + scrollY
val horizontal = point.first - totalPaddingLeft + scrollX
val line = textLayout.getLineForVertical(vertical.toInt())
val offset = textLayout.getOffsetForHorizontal(line, horizontal)
val spanned = text as Spanned
val link = requiredLink()
return "line=$line, offset=$offset, " +
"span=${spanned.getSpanStart(link)}..${spanned.getSpanEnd(link)}, " +
"matches=${spanned.getSpans(offset, offset, ClickableSpan::class.java).size}"
}
private fun CheckBox.blankAreaCenter(): Pair<Float, Float> {
val layout = checkNotNull(layout)
val line = layout.lineCount - 1
val horizontal = layout.getLineRight(line) + 1f
check(horizontal < layout.width)
return touchPoint(horizontal, line)
}
private fun CheckBox.verticalBlankAreaAtRequiredLink(): Pair<Float, Float> {
val spanned = text as Spanned
val link = requiredLink()
val layout = checkNotNull(layout)
val start = layout.getPrimaryHorizontal(spanned.getSpanStart(link))
val end = layout.getPrimaryHorizontal(spanned.getSpanEnd(link))
val line = layout.getLineForOffset(spanned.getSpanStart(link))
val vertical = layout.getLineBottom(line) + 1f
check(vertical < height - totalPaddingBottom)
return Pair(
(start + end) / 2f + totalPaddingLeft - scrollX,
vertical + totalPaddingTop - scrollY
)
}
private fun CheckBox.touchPoint(horizontal: Float, line: Int): Pair<Float, Float> {
val layout = checkNotNull(layout)
return Pair(
horizontal + totalPaddingLeft - scrollX,
(layout.getLineTop(line) + layout.getLineBottom(line)) / 2f + totalPaddingTop - scrollY
)
}
private fun CheckBox.dispatchTap(point: Pair<Float, Float>) {
val down = MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, point.first, point.second, 0)
val up = MotionEvent.obtain(0, 1, MotionEvent.ACTION_UP, point.first, point.second, 0)
try {
dispatchTouchEvent(down)
dispatchTouchEvent(up)
} finally {
down.recycle()
up.recycle()
}
shadowOf(Looper.getMainLooper()).idle()
}
private fun CheckBox.dispatchGesture(
downPoint: Pair<Float, Float>,
vararg events: Pair<Int, Pair<Float, Float>>
) {
dispatchEvent(MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, downPoint.first, downPoint.second, 0))
events.forEachIndexed { index, (action, point) ->
dispatchEvent(MotionEvent.obtain(0, index + 1L, action, point.first, point.second, 0))
}
shadowOf(Looper.getMainLooper()).idle()
}
private fun CheckBox.dispatchEvent(event: MotionEvent) {
try {
dispatchTouchEvent(event)
} finally {
event.recycle()
}
}
private fun createResponse(applicationNo: String = "GIFT-001") = GiftCreateResponse(
applicationNo = applicationNo,
status = "REQUESTED",
statusName = "신청 완료",
priceCan = 100,
trackingDeadlineAt = "2026-10-09T00:00:00Z"
)
private fun activityString(resId: Int): String = ApplicationProvider.getApplicationContext<Application>().getString(resId)
private class NavigationFakeApi : GiftApi by InboxFakeApi() {
var createGiftResponse: Single<ApiResponse<GiftCreateResponse>> = Single.error(UnsupportedOperationException())
val createGiftRequests = mutableListOf<GiftCreateRequest>()
val cancelRequests = mutableListOf<String>()
var cancelResponse = SingleSubject.create<ApiResponse<GiftCancelResponse>>()
var detailStatus = "RECEIVED"
@@ -656,6 +1163,22 @@ class GiftNavigationTest {
var recipientAddress: GiftAddressResponse? = null
val recipientSubmissions = mutableListOf<Pair<String, GiftRecipientAddressRequest>>()
var recipientResponse = SingleSubject.create<ApiResponse<GiftRecipientAddressResponse>>()
override fun getFormOptions(authHeader: String): Single<ApiResponse<GiftFormOptionsResponse>> = Single.just(
ApiResponse(
true,
GiftFormOptionsResponse(
sizes = listOf(GiftSizeResponse("SMALL", "소형", salePriceCan = 100)),
categories = listOf(GiftCategoryResponse(10L, "일반", false))
),
null
)
)
override fun createGift(
request: GiftCreateRequest,
authHeader: String
): Single<ApiResponse<GiftCreateResponse>> = createGiftResponse.also { createGiftRequests.add(request) }
override fun registerRecipientAddress(
applicationNo: String,
request: GiftRecipientAddressRequest,
@@ -11,11 +11,13 @@ 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.SharedPreferenceManager
import kr.co.vividnext.sodalive.v2.gift.data.GiftApi
import kr.co.vividnext.sodalive.v2.gift.data.GiftCancelResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftCategoryResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftCreateResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftCreateRequest
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftFormOptionsResponse
@@ -27,7 +29,7 @@ import kr.co.vividnext.sodalive.v2.gift.data.GiftTrackingRequest
import kr.co.vividnext.sodalive.v2.gift.data.GiftTrackingResponse
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@@ -97,36 +99,55 @@ class GiftSendRequestViewModelTest {
}
@Test
fun `제출 성공 이벤트는 한 번만 소비된다`() {
api.enqueueCreateGift(ApiResponse(true, Any(), null))
fun `유효한 신청번호는 한 번만 소비되는 제출 성공 event로 전달된다`() {
api.enqueueCreateGift(ApiResponse(true, createResponse(), null))
viewModel.submitGift(entry(), completeForm())
assertTrue(viewModel.consumeSubmitSuccessEvent())
assertFalse(viewModel.consumeSubmitSuccessEvent())
assertEquals("GIFT-001", viewModel.consumeSubmitSuccessEvent())
assertNull(viewModel.consumeSubmitSuccessEvent())
assertEquals("small", api.lastCreateRequest?.sizeCode)
}
@Test
fun `응답 data가 없거나 신청번호가 blank면 성공 event 없이 오류 토스트가 만들어진다`() {
listOf(
ApiResponse<GiftCreateResponse>(true, null, null) to null,
ApiResponse(true, createResponse(" "), "신청번호 누락") to "신청번호 누락"
).forEach { (response, expectedMessage) ->
api.enqueueCreateGift(response)
viewModel.submitGift(entry(), completeForm())
assertNull(viewModel.consumeSubmitSuccessEvent())
if (expectedMessage == null) {
assertEquals(R.string.common_error_unknown, viewModel.toastLiveData.requireValue()?.resId)
} else {
assertEquals(expectedMessage, viewModel.toastLiveData.requireValue()?.message)
}
}
}
@Test
fun `제출 실패 시 성공 이벤트 없이 오류 토스트가 만들어진다`() {
api.enqueueCreateGift(ApiResponse(false, null, "신청 실패"))
viewModel.submitGift(entry(), completeForm())
assertFalse(viewModel.consumeSubmitSuccessEvent())
assertNull(viewModel.consumeSubmitSuccessEvent())
assertEquals("신청 실패", viewModel.toastLiveData.requireValue()?.message)
}
@Test
fun `제출 중에는 loading이 true이고 완료되면 false가 된다`() {
val pending = SingleSubject.create<ApiResponse<Any>>()
val pending = SingleSubject.create<ApiResponse<GiftCreateResponse>>()
api.enqueueCreateGift(pending)
viewModel.submitGift(entry(), completeForm())
assertEquals(true, viewModel.isSubmitLoading.requireValue())
pending.onSuccess(ApiResponse(true, Any(), null))
pending.onSuccess(ApiResponse(true, createResponse(), null))
assertEquals(false, viewModel.isSubmitLoading.requireValue())
}
@@ -138,6 +159,14 @@ class GiftSendRequestViewModelTest {
private fun entry(): GiftSendEntry = GiftSendEntry(creatorMemberId = 99L, creatorNickname = "크리에이터")
private fun createResponse(applicationNo: String = "GIFT-001") = GiftCreateResponse(
applicationNo = applicationNo,
status = "REQUESTED",
statusName = "신청 완료",
priceCan = 100,
trackingDeadlineAt = "2026-10-09T00:00:00Z"
)
private fun completeForm(): GiftSendFormState = GiftSendFormState(
senderName = "홍길동",
phoneNumber = "01000000000",
@@ -200,7 +229,7 @@ class GiftSendRequestViewModelTest {
): Single<ApiResponse<GiftCancelResponse>> = Single.error(UnsupportedOperationException())
private val formOptionsResponses = ArrayDeque<Single<ApiResponse<GiftFormOptionsResponse>>>()
private val createGiftResponses = ArrayDeque<Single<ApiResponse<Any>>>()
private val createGiftResponses = ArrayDeque<Single<ApiResponse<GiftCreateResponse>>>()
var lastAuthHeader: String? = null
private set
var lastCreateRequest: GiftCreateRequest? = null
@@ -214,11 +243,11 @@ class GiftSendRequestViewModelTest {
formOptionsResponses.addLast(response)
}
fun enqueueCreateGift(response: ApiResponse<Any>) {
fun enqueueCreateGift(response: ApiResponse<GiftCreateResponse>) {
createGiftResponses.addLast(Single.just(response))
}
fun enqueueCreateGift(response: Single<ApiResponse<Any>>) {
fun enqueueCreateGift(response: Single<ApiResponse<GiftCreateResponse>>) {
createGiftResponses.addLast(response)
}
@@ -230,7 +259,7 @@ class GiftSendRequestViewModelTest {
override fun createGift(
request: GiftCreateRequest,
authHeader: String
): Single<ApiResponse<Any>> {
): Single<ApiResponse<GiftCreateResponse>> {
lastAuthHeader = authHeader
lastCreateRequest = request
return createGiftResponses.removeFirst()