feat(event): 딥링크 입력과 소비 상태를 정규화한다
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
package kr.co.vividnext.sodalive.v2.event
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Application
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import kr.co.vividnext.sodalive.BuildConfig
|
||||
import kr.co.vividnext.sodalive.common.Constants
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
class EventDeepLinkEntryTest {
|
||||
@Test
|
||||
fun `data는 별칭보다 우선하고 data 없는 충돌 별칭은 다른 ID를 선택하지 않는다`() {
|
||||
val entry = Intent(Intent.ACTION_VIEW, uri(7)).putExtra("deepLink", uri(8).toString())
|
||||
assertEquals(EventDeepLinkContract.Direct(7), EventDeepLinkEntry.classify(entry))
|
||||
val conflict = Intent().putExtra("deep_link", uri(7).toString())
|
||||
.putExtra(Constants.EXTRA_DATA, Bundle().apply { putString("deepLink", uri(8).toString()) })
|
||||
assertEquals(EventDeepLinkContract.InvalidEvent, EventDeepLinkEntry.classify(conflict))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `handoff 단계와 Main 소비 단계는 독립적이고 같은 entry는 각각 한 번이다`() {
|
||||
val entry = Intent(Intent.ACTION_VIEW, uri(7))
|
||||
assertTrue(EventDeepLinkEntry.forward(entry))
|
||||
assertFalse(EventDeepLinkEntry.forward(entry))
|
||||
val handoff = Intent().putExtra(Constants.EXTRA_DATA, EventDeepLinkEntry.payload(entry))
|
||||
assertTrue(EventDeepLinkEntry.consume(handoff))
|
||||
assertFalse(EventDeepLinkEntry.consume(handoff))
|
||||
assertTrue(EventDeepLinkEntry.consume(Intent(Intent.ACTION_VIEW, uri(7))))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `URL 없는 이벤트 라우팅 필드는 SDK 소유권을 우회해 다른 목적지로 승격되지 않는다`() {
|
||||
val entry = Intent().putExtra(
|
||||
Constants.EXTRA_DATA,
|
||||
Bundle().apply {
|
||||
putString("deep_link_value", "event-detail")
|
||||
putString("deep_link_sub5", "7")
|
||||
putString("room_id", "9")
|
||||
}
|
||||
)
|
||||
assertEquals(EventDeepLinkContract.InvalidEvent, EventDeepLinkEntry.classify(entry))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `이벤트가 아닌 Intent는 소비 상태나 빈 payload를 추가하지 않는다`() {
|
||||
val entry = Intent()
|
||||
assertFalse(EventDeepLinkEntry.consume(entry))
|
||||
assertFalse(entry.hasExtra(Constants.EXTRA_DATA))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `정규화는 data의 canonical URL과 typed ID만 남기고 충돌 메타데이터를 버린다`() {
|
||||
val entry = Intent(Intent.ACTION_VIEW, uri(Long.MAX_VALUE))
|
||||
.putExtra("deepLink", uri(8).toString())
|
||||
.putExtra(
|
||||
Constants.EXTRA_DATA,
|
||||
Bundle().apply {
|
||||
putString("room_id", "9")
|
||||
putString("content_id", "10")
|
||||
}
|
||||
)
|
||||
val normalized = requireNotNull(EventDeepLinkEntry.normalize(entry))
|
||||
assertEquals("${BuildConfig.APPSCHEME}://event-detail/9223372036854775807", normalized.getString("deep_link"))
|
||||
assertEquals("event-detail", normalized.getString("deep_link_value"))
|
||||
assertEquals(Long.MAX_VALUE, normalized.get("event_id"))
|
||||
assertEquals(setOf("deep_link", "deep_link_value", "event_id", "v2.event.entry"), normalized.keySet())
|
||||
assertEquals(EventDeepLinkContract.Direct(Long.MAX_VALUE), EventDeepLinkEntry.route(normalized))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `정규화는 두 root와 nested URL 별칭을 같은 typed Bundle로 만든다`() {
|
||||
listOf("deep_link", "deepLink").forEach { key ->
|
||||
listOf(false, true).forEach { nested ->
|
||||
val entry = if (nested) {
|
||||
Intent().putExtra(Constants.EXTRA_DATA, Bundle().apply { putString(key, uri(7).toString()) })
|
||||
} else {
|
||||
Intent().putExtra(key, uri(7).toString())
|
||||
}
|
||||
val normalized = requireNotNull(EventDeepLinkEntry.normalize(entry))
|
||||
assertEquals(7L, normalized.get("event_id"))
|
||||
assertEquals(EventDeepLinkContract.Direct(7), EventDeepLinkEntry.route(normalized))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `무관한 입력만 null이고 원본 non-event Bundle은 변하지 않는다`() {
|
||||
val bundle = Bundle().apply { putString("room_id", "9") }
|
||||
val entry = Intent().putExtra(Constants.EXTRA_DATA, bundle)
|
||||
assertNull(EventDeepLinkEntry.normalize(entry))
|
||||
assertEquals(setOf("room_id"), entry.getBundleExtra(Constants.EXTRA_DATA)?.keySet())
|
||||
assertEquals(EventDeepLinkContract.Unrelated, EventDeepLinkEntry.route(bundle))
|
||||
val authoritative = Intent(Intent.ACTION_VIEW, Uri.parse("${BuildConfig.APPSCHEME}://content/9"))
|
||||
.putExtra("deepLink", uri(7).toString())
|
||||
assertNull(EventDeepLinkEntry.normalize(authoritative))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `충돌 별칭과 invalid 또는 외부 이벤트는 URL과 ID 없는 terminal Bundle이다`() {
|
||||
val entries = listOf(
|
||||
Intent().putExtra("deep_link", uri(7).toString()).putExtra("deepLink", uri(8).toString()),
|
||||
Intent(Intent.ACTION_VIEW, Uri.parse("${BuildConfig.APPSCHEME}://event-detail/0?room_id=9")),
|
||||
Intent(Intent.ACTION_VIEW, Uri.parse("https://attacker.example/event-detail/7?content_id=9")),
|
||||
Intent().putExtra("event_id", 7L),
|
||||
Intent().putExtra("deep_link_value", "event-detail")
|
||||
)
|
||||
entries.forEach { entry ->
|
||||
entry.putExtra("room_id", "9")
|
||||
val normalized = requireNotNull(EventDeepLinkEntry.normalize(entry))
|
||||
assertNull(normalized.getString("deep_link"))
|
||||
assertFalse(normalized.containsKey("event_id"))
|
||||
assertFalse(normalized.containsKey("room_id"))
|
||||
assertEquals(EventDeepLinkContract.InvalidEvent, EventDeepLinkEntry.route(normalized))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `SDK 소유 정규화는 검증된 URL과 state만 남기고 실행 ID를 만들지 않는다`() {
|
||||
val url = "https://${BuildConfig.APPSCHEME}.onelink.me/a?deep_link_value=event-detail&deep_link_sub5=7&room_id=9"
|
||||
val entry = Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
||||
.putExtra(
|
||||
Constants.EXTRA_DATA,
|
||||
Bundle().apply {
|
||||
putLong("event_id", 7L)
|
||||
putString("content_id", "10")
|
||||
}
|
||||
)
|
||||
val normalized = requireNotNull(EventDeepLinkEntry.normalize(entry))
|
||||
assertEquals(setOf("deep_link", "v2.event.entry"), normalized.keySet())
|
||||
assertEquals(url, normalized.getString("deep_link"))
|
||||
assertEquals(EventDeepLinkContract.SdkOwned, EventDeepLinkEntry.route(normalized))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `data 없는 서로 다른 SDK 이벤트 URL 별칭은 소유 분류가 같아도 충돌로 거부한다`() {
|
||||
val url = "https://${BuildConfig.APPSCHEME}.onelink.me/a?deep_link_value=event-detail&deep_link_sub5="
|
||||
val entry = Intent().putExtra("deep_link", "${url}7")
|
||||
.putExtra(Constants.EXTRA_DATA, Bundle().apply { putString("deepLink", "${url}8") })
|
||||
val normalized = requireNotNull(EventDeepLinkEntry.normalize(entry))
|
||||
assertEquals(EventDeepLinkContract.InvalidEvent, EventDeepLinkEntry.route(normalized))
|
||||
assertNull(normalized.getString("deep_link"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Bundle route는 raw ID와 marker만 또는 URL과 불일치한 ID를 실행하지 않는다`() {
|
||||
val bundles = listOf(
|
||||
Bundle().apply { putLong("event_id", 7L) },
|
||||
Bundle().apply {
|
||||
putString("deep_link_value", "event-detail")
|
||||
putLong("event_id", 7L)
|
||||
},
|
||||
Bundle().apply {
|
||||
putString("deep_link", uri(7).toString())
|
||||
putLong("event_id", 7L)
|
||||
},
|
||||
Bundle().apply {
|
||||
putString("deep_link", uri(7).toString())
|
||||
putString("deep_link_value", "event-detail")
|
||||
putString("event_id", "7")
|
||||
},
|
||||
Bundle().apply {
|
||||
putString("deep_link", uri(7).toString())
|
||||
putString("deep_link_value", "event-detail")
|
||||
putLong("event_id", 8L)
|
||||
},
|
||||
Bundle().apply {
|
||||
putString("deep_link", "https://attacker.example/event-detail/7")
|
||||
putString("deep_link_value", "event-detail")
|
||||
putLong("event_id", 7L)
|
||||
}
|
||||
)
|
||||
bundles.forEach { assertEquals(EventDeepLinkContract.InvalidEvent, EventDeepLinkEntry.route(it)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `normalized handoff의 typed ID를 변조하면 재정규화에서도 거부한다`() {
|
||||
val normalized = requireNotNull(EventDeepLinkEntry.normalize(Intent(Intent.ACTION_VIEW, uri(7))))
|
||||
normalized.putLong("event_id", 8L)
|
||||
val handoff = Intent().putExtra(Constants.EXTRA_DATA, normalized)
|
||||
val renormalized = requireNotNull(EventDeepLinkEntry.normalize(handoff))
|
||||
assertEquals(EventDeepLinkContract.InvalidEvent, EventDeepLinkEntry.route(renormalized))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `정규화 Bundle 소비는 원본 saved state와 handoff에 남고 새 same-ID 진입은 독립적이다`() {
|
||||
val entry = Intent(Intent.ACTION_VIEW, uri(7))
|
||||
val normalized = requireNotNull(EventDeepLinkEntry.normalize(entry))
|
||||
assertTrue(EventDeepLinkEntry.forward(entry))
|
||||
val handoff = Intent().putExtra(Constants.EXTRA_DATA, Bundle(normalized))
|
||||
assertFalse(EventDeepLinkEntry.forward(handoff))
|
||||
assertTrue(EventDeepLinkEntry.consume(normalized))
|
||||
assertFalse(EventDeepLinkEntry.consume(entry))
|
||||
assertFalse(EventDeepLinkEntry.consume(handoff))
|
||||
val saved = Bundle()
|
||||
EventDeepLinkEntry.save(entry, saved)
|
||||
val restored = Intent(Intent.ACTION_VIEW, uri(7))
|
||||
EventDeepLinkEntry.restore(restored, saved)
|
||||
assertFalse(EventDeepLinkEntry.forward(restored))
|
||||
assertFalse(EventDeepLinkEntry.consume(restored))
|
||||
val fresh = requireNotNull(EventDeepLinkEntry.normalize(Intent(Intent.ACTION_VIEW, uri(7))))
|
||||
assertTrue(EventDeepLinkEntry.consume(fresh))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `실제 Activity 재생성은 원본 launch Intent와 saved state에서 소비를 복원한다`() {
|
||||
val original = Intent(Intent.ACTION_VIEW, uri(7))
|
||||
val controller = Robolectric.buildActivity(EntryActivity::class.java, Intent(original)).setup()
|
||||
assertTrue(controller.get().opened)
|
||||
controller.get().intent = Intent(original)
|
||||
val recreated = controller.recreate().get()
|
||||
assertFalse(recreated.opened)
|
||||
controller.newIntent(Intent(original))
|
||||
assertTrue(controller.get().opened)
|
||||
controller.pause().stop().destroy()
|
||||
}
|
||||
|
||||
class EntryActivity : Activity() {
|
||||
var opened = false
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
EventDeepLinkEntry.restore(intent, savedInstanceState)
|
||||
opened = EventDeepLinkEntry.consume(intent)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
EventDeepLinkEntry.save(intent, outState)
|
||||
super.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
setIntent(intent)
|
||||
opened = EventDeepLinkEntry.consume(intent)
|
||||
}
|
||||
}
|
||||
|
||||
private fun uri(id: Long): Uri = Uri.parse("${BuildConfig.APPSCHEME}://event-detail/$id")
|
||||
}
|
||||
Reference in New Issue
Block a user