|
| 1 | +package dev.hotwire.turbo.session |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.os.Build |
| 5 | +import androidx.appcompat.app.AppCompatActivity |
| 6 | +import androidx.core.os.bundleOf |
| 7 | +import androidx.fragment.app.Fragment |
| 8 | +import dev.hotwire.turbo.BaseUnitTest |
| 9 | +import dev.hotwire.turbo.config.TurboPathConfiguration |
| 10 | +import org.assertj.core.api.Assertions.assertThat |
| 11 | +import org.junit.Before |
| 12 | +import org.junit.Test |
| 13 | +import org.junit.runner.RunWith |
| 14 | +import org.robolectric.Robolectric |
| 15 | +import org.robolectric.RobolectricTestRunner |
| 16 | +import org.robolectric.annotation.Config |
| 17 | +import kotlin.reflect.KClass |
| 18 | + |
| 19 | +@RunWith(RobolectricTestRunner::class) |
| 20 | +@Config(sdk = [Build.VERSION_CODES.O]) |
| 21 | +class TurboSessionNavHostFragmentTest : BaseUnitTest() { |
| 22 | + |
| 23 | + private lateinit var activity: AppCompatActivity |
| 24 | + private lateinit var host: TestNavHostFragment |
| 25 | + |
| 26 | + @Before |
| 27 | + override fun setup() { |
| 28 | + super.setup() |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `reverts to config start location when deep link host differs`() { |
| 33 | + val extras = bundleOf(LOCATION_KEY to "https://other.com/path") |
| 34 | + val intent = Intent().apply { putExtra(DEEPLINK_EXTRAS_KEY, extras) } |
| 35 | + activity = Robolectric.buildActivity(TestActivity::class.java, intent).create().get() |
| 36 | + |
| 37 | + host = TestNavHostFragment() |
| 38 | + host.ensureDeeplinkStartLocationValid(activity) |
| 39 | + |
| 40 | + val resultBundle = activity.intent.getBundleExtra(DEEPLINK_EXTRAS_KEY) |
| 41 | + assertThat(resultBundle?.getString(LOCATION_KEY)).isEqualTo("https://example.com/start") |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `does not change start location when deep link host matches config`() { |
| 46 | + val extras = bundleOf(LOCATION_KEY to "https://example.com/path") |
| 47 | + val intent = Intent().apply { putExtra(DEEPLINK_EXTRAS_KEY, extras) } |
| 48 | + activity = Robolectric.buildActivity(TestActivity::class.java, intent).create().get() |
| 49 | + |
| 50 | + host = TestNavHostFragment() |
| 51 | + host.ensureDeeplinkStartLocationValid(activity) |
| 52 | + |
| 53 | + val resultBundle = activity.intent.getBundleExtra(DEEPLINK_EXTRAS_KEY) |
| 54 | + assertThat(resultBundle?.getString(LOCATION_KEY)).isEqualTo("https://example.com/path") |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +class TestActivity : AppCompatActivity() |
| 60 | + |
| 61 | +class TestNavHostFragment : TurboSessionNavHostFragment() { |
| 62 | + override val sessionName = "test" |
| 63 | + override val startLocation = "https://example.com/start" |
| 64 | + override val pathConfigurationLocation = TurboPathConfiguration.Location( |
| 65 | + assetFilePath = "json/test-configuration.json" |
| 66 | + ) |
| 67 | + override val registeredFragments: List<KClass<out Fragment>> = emptyList() |
| 68 | +} |
| 69 | + |
0 commit comments