prep merge tv-trailer-intent

This commit is contained in:
root 2026-11-07 20:50:32 +01:00
commit 8b6edf5a96
2 changed files with 8 additions and 5 deletions

View file

@ -19,21 +19,23 @@ import org.robolectric.annotation.Config
@Config(sdk = [34], application = Application::class) @Config(sdk = [34], application = Application::class)
class TitleTrailerTest { class TitleTrailerTest {
private val context = ApplicationProvider.getApplicationContext<Context>() private val context = ApplicationProvider.getApplicationContext<Context>()
private val url = "https://www.youtube.com/watch?v=abcdefghijk" private val url = "https://www.youtube.com/watch?v=ab_-defghij"
@Test fun missingYoutubeHandlerStaysOnTheTitleEvenWhenABrowserExists() { @Test fun missingYoutubeHandlerStaysOnTheTitleEvenWhenABrowserExists() {
register(Intent(Intent.ACTION_VIEW, Uri.parse(url)), "test.browser") register(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=ab_-defghij")), "test.browser")
assertFalse(TitleTrailer(context).open(url)) assertFalse(TitleTrailer(context).open(url))
assertNull(shadowOf(context as Application).nextStartedActivity) assertNull(shadowOf(context as Application).nextStartedActivity)
} }
@Test fun installedYoutubeHandlerReceivesAnExplicitPackageAndNoBrowserUrl() { @Test fun installedYoutubeHandlerReceivesAnExplicitPackageAndNoBrowserUrl() {
register(Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:abcdefghijk")), "test.youtube") register(Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://ab_-defghij")), "test.youtube")
assertTrue(TitleTrailer(context).open(url)) assertTrue(TitleTrailer(context).open(url))
val launched = shadowOf(context as Application).nextStartedActivity val launched = shadowOf(context as Application).nextStartedActivity
assertEquals(Intent.ACTION_VIEW, launched.action)
assertEquals("test.youtube", launched.`package`) assertEquals("test.youtube", launched.`package`)
assertEquals("vnd.youtube", launched.data?.scheme) assertEquals("vnd.youtube", launched.data?.scheme)
assertEquals("abcdefghijk", launched.data?.schemeSpecificPart) // SmartTube (IntentExtractor.extractVideoId) takes the id from Uri.getHost() for this scheme; the opaque `vnd.youtube:ID` form yields null there.
assertEquals("ab_-defghij", launched.data?.host)
} }
@Test fun nonYoutubeTrailerNeverOpensAGeneralViewHandler() { @Test fun nonYoutubeTrailerNeverOpensAGeneralViewHandler() {

View file

@ -17,7 +17,8 @@ class TitleTrailer(private val context: Context) {
?: source.pathSegments.takeIf { it.firstOrNull() in setOf("shorts", "embed") }?.getOrNull(1) ?: source.pathSegments.takeIf { it.firstOrNull() in setOf("shorts", "embed") }?.getOrNull(1)
else -> null else -> null
} ?: return false } ?: return false
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:$video")).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // Authority form: SmartTube's IntentExtractor reads the id from Uri.getHost(), which is null for the opaque `vnd.youtube:ID`.
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://$video")).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val handler = context.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY) val handler = context.packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
.firstOrNull { it.activityInfo.exported } ?: return false .firstOrNull { it.activityInfo.exported } ?: return false
intent.setPackage(handler.activityInfo.packageName) intent.setPackage(handler.activityInfo.packageName)