|
| 1 | +package com.flipcash.shared.chat.ui |
| 2 | + |
| 3 | +import androidx.compose.ui.graphics.Color |
| 4 | +import androidx.compose.ui.text.LinkAnnotation |
| 5 | +import androidx.compose.ui.text.SpanStyle |
| 6 | +import androidx.compose.ui.text.buildAnnotatedString |
| 7 | +import androidx.compose.ui.text.style.TextDecoration |
| 8 | +import org.junit.runner.RunWith |
| 9 | +import org.robolectric.RobolectricTestRunner |
| 10 | +import org.robolectric.annotation.Config |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertEquals |
| 13 | +import kotlin.test.assertTrue |
| 14 | + |
| 15 | +@RunWith(RobolectricTestRunner::class) |
| 16 | +@Config(manifest = Config.NONE) |
| 17 | +class UrlAnnotatorTest { |
| 18 | + |
| 19 | + private val linkStyle = SpanStyle( |
| 20 | + color = Color.Blue, |
| 21 | + textDecoration = TextDecoration.Underline, |
| 22 | + ) |
| 23 | + private val annotator = UrlAnnotator(linkStyle) |
| 24 | + |
| 25 | + private fun annotate(text: String) = buildAnnotatedString { |
| 26 | + append(text) |
| 27 | + with(annotator) { annotate(text) } |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `annotates https URL at correct offsets`() { |
| 32 | + val text = "Visit https://example.com today" |
| 33 | + val result = annotate(text) |
| 34 | + |
| 35 | + val links = result.getLinkAnnotations(0, result.length) |
| 36 | + assertEquals(1, links.size) |
| 37 | + |
| 38 | + val link = links.first() |
| 39 | + assertEquals(6, link.start) |
| 40 | + assertEquals(25, link.end) |
| 41 | + |
| 42 | + val annotation = link.item as LinkAnnotation.Url |
| 43 | + assertEquals("https://example.com", annotation.url) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `annotates http URL without adding https`() { |
| 48 | + val text = "Go to http://example.com now" |
| 49 | + val result = annotate(text) |
| 50 | + |
| 51 | + val links = result.getLinkAnnotations(0, result.length) |
| 52 | + assertEquals(1, links.size) |
| 53 | + |
| 54 | + val annotation = links.first().item as LinkAnnotation.Url |
| 55 | + assertEquals("http://example.com", annotation.url) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `prepends https for bare domain`() { |
| 60 | + val text = "Check example.com out" |
| 61 | + val result = annotate(text) |
| 62 | + |
| 63 | + val links = result.getLinkAnnotations(0, result.length) |
| 64 | + assertEquals(1, links.size) |
| 65 | + |
| 66 | + val annotation = links.first().item as LinkAnnotation.Url |
| 67 | + assertEquals("https://example.com", annotation.url) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `annotates multiple URLs`() { |
| 72 | + val text = "See https://a.com and https://b.com" |
| 73 | + val result = annotate(text) |
| 74 | + |
| 75 | + val links = result.getLinkAnnotations(0, result.length) |
| 76 | + assertEquals(2, links.size) |
| 77 | + |
| 78 | + val urls = links.map { (it.item as LinkAnnotation.Url).url } |
| 79 | + assertTrue("https://a.com" in urls) |
| 80 | + assertTrue("https://b.com" in urls) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun `no annotations for plain text`() { |
| 85 | + val text = "Just a plain message" |
| 86 | + val result = annotate(text) |
| 87 | + |
| 88 | + val links = result.getLinkAnnotations(0, result.length) |
| 89 | + assertTrue(links.isEmpty()) |
| 90 | + } |
| 91 | +} |
0 commit comments