|
| 1 | +package expo.modules.t3markdowntext |
| 2 | + |
| 3 | +import android.graphics.drawable.ColorDrawable |
| 4 | +import android.text.SpannableString |
| 5 | +import android.text.Spanned |
| 6 | +import android.text.style.ImageSpan |
| 7 | +import org.junit.Assert.assertEquals |
| 8 | +import org.junit.Test |
| 9 | +import org.junit.runner.RunWith |
| 10 | +import org.robolectric.RobolectricTestRunner |
| 11 | +import org.robolectric.annotation.Config |
| 12 | + |
| 13 | +@RunWith(RobolectricTestRunner::class) |
| 14 | +@Config(sdk = [36], manifest = Config.NONE) |
| 15 | +class MarkdownSelectionCopyTest { |
| 16 | + private fun withIcon(value: String): SpannableString = SpannableString(value).apply { |
| 17 | + val index = value.indexOf('\uFFFC') |
| 18 | + setSpan(ImageSpan(ColorDrawable()), index, index + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + fun removesIconAndInjectedSpacer() { |
| 23 | + val text = withIcon("\uFFFC\u00A0main.go:12 starts the server.") |
| 24 | + assertEquals("main.go:12 starts the server.", copyTextWithoutInlineImages(text, 0, text.length)) |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + fun removesSpacerWhenSelectionStartsAfterIcon() { |
| 29 | + val text = withIcon("\uFFFC\u00A0main.go:12 starts the server.") |
| 30 | + assertEquals("main.go:12", copyTextWithoutInlineImages(text, 1, 12)) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun preservesAuthoredWhitespaceAndLiteralObjectCharacters() { |
| 35 | + val text = withIcon("before\u00A0 \uFFFC\u00A0\u00A0 main.go after\u00A0\uFFFC\u00A0") |
| 36 | + assertEquals( |
| 37 | + "before\u00A0 \u00A0 main.go after\u00A0\uFFFC\u00A0", |
| 38 | + copyTextWithoutInlineImages(text, 0, text.length) |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun preservesTextWithoutImageSpans() { |
| 44 | + val text = "\uFFFC\u00A0main.go" |
| 45 | + assertEquals(text, copyTextWithoutInlineImages(text, 0, text.length)) |
| 46 | + assertEquals(text, copyTextWithoutInlineImages(SpannableString(text), 0, text.length)) |
| 47 | + } |
| 48 | +} |
0 commit comments