package org.terst.feedreader.ui
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
/**
* The YouTube placeholder: recognising one, and building a page its embed will actually play in.
*
* Both halves come from the same report: "YouTube play is misconfigured, long click on YouTube
* placeholder should open the URL directly."
*/
class YouTubeLinksTest {
/**
* What a long-press on the reader's video placeholder actually reports.
*
* Not the link's href, which is what the first version of this assumed and got wrong on the
* device: the placeholder is `
`,
* and for an image inside a link WebView's hit test reports SRC_IMAGE_ANCHOR_TYPE with the
* *image's* URL in `extra`, not the anchor's. Long-pressing the thumbnail produced the
* ordinary link menu showing `https://i.ytimg.com/vi/dQw4w9WgXcQ/hq720.jpg`.
*
* The thumbnail URL carries the video id, so it is resolved back to the video.
*/
@Test
fun `a long-press on the thumbnail resolves to the video, which is what WebView reports`() {
assertEquals(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
youTubeTargetForLongPress("https://i.ytimg.com/vi/dQw4w9WgXcQ/hq720.jpg")
)
}
@Test
fun `any thumbnail size resolves, since the reader has used more than one`() {
assertEquals(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
youTubeTargetForLongPress("https://i.ytimg.com/vi/dQw4w9WgXcQ/maxresdefault.jpg")
)
assertEquals(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
youTubeTargetForLongPress("https://i9.ytimg.com/vi_webp/dQw4w9WgXcQ/hq720.webp")
)
}
@Test
fun `a watch link resolves to itself, for wherever one does come through`() {
assertEquals(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
youTubeTargetForLongPress("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
)
}
@Test
fun `a short youtu-be link resolves to the video`() {
assertEquals(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
youTubeTargetForLongPress("https://youtu.be/dQw4w9WgXcQ")
)
}
@Test
fun `an ordinary article link is not a video`() {
assertNull(youTubeTargetForLongPress("https://www.nytimes.com/2026/10/20/us/politics/x.html"))
}
@Test
fun `an ordinary article image is not a video`() {
assertNull(youTubeTargetForLongPress("https://www.westhawaiitoday.com/wp-content/uploads/reef.jpg"))
}
@Test
fun `a lookalike host is not YouTube`() {
assertNull(youTubeTargetForLongPress("https://notyoutube.com/watch?v=dQw4w9WgXcQ"))
assertNull(youTubeTargetForLongPress("https://youtube.com.evil.example/watch?v=dQw4w9WgXcQ"))
assertNull(youTubeTargetForLongPress("https://i.ytimg.com.evil.example/vi/dQw4w9WgXcQ/hq720.jpg"))
}
@Test
fun `a YouTube link that is not one video stays on the ordinary menu`() {
assertNull(youTubeTargetForLongPress("https://www.youtube.com/@someone"))
assertNull(youTubeTargetForLongPress("https://www.youtube.com/playlist?list=PL123"))
}
@Test
fun `rubbish is not a video`() {
assertNull(youTubeTargetForLongPress(""))
assertNull(youTubeTargetForLongPress("not a url at all"))
}
// --- the embed page ------------------------------------------------------------------
@Test
fun `the embed page puts the player in an iframe, which is the whole point`() {
val html = youTubeEmbedPageHtml("dQw4w9WgXcQ")
assertTrue("loading the embed top-level is what YouTube rejects: $html", html.contains("