|
| 1 | +package com.philkes.notallyx.utils |
| 2 | + |
| 3 | +import android.graphics.Typeface |
| 4 | +import android.text.Editable |
| 5 | +import android.text.Spanned |
| 6 | +import android.text.style.StrikethroughSpan |
| 7 | +import android.text.style.StyleSpan |
| 8 | +import android.text.style.TypefaceSpan |
| 9 | +import android.text.style.URLSpan |
| 10 | +import com.philkes.notallyx.test.mockAndroidLog |
| 11 | +import com.philkes.notallyx.utils.changehistory.EditTextState |
| 12 | +import org.junit.Assert.assertEquals |
| 13 | +import org.junit.Assert.assertFalse |
| 14 | +import org.junit.Assert.assertTrue |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.runner.RunWith |
| 18 | +import org.robolectric.RobolectricTestRunner |
| 19 | +import org.robolectric.annotation.Config |
| 20 | + |
| 21 | +@RunWith(RobolectricTestRunner::class) |
| 22 | +@Config(manifest = Config.NONE, sdk = [35]) |
| 23 | +class EditTextCompressionTest { |
| 24 | + |
| 25 | + @Before |
| 26 | + fun setUp() { |
| 27 | + // Silence Android Log calls in unit tests |
| 28 | + mockAndroidLog() |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `compressIfNeeded does not compress at threshold and compresses above`() { |
| 33 | + val threshold = CompressUtility.COMPRESSION_THRESHOLD |
| 34 | + val base = "x".repeat(threshold) |
| 35 | + val atThreshold = CompressUtility.compressIfNeeded(base) |
| 36 | + // exact threshold should not be compressed |
| 37 | + assertFalse(atThreshold.startsWith("GZ:")) |
| 38 | + assertEquals(base, CompressUtility.decompressIfNeeded(atThreshold)) |
| 39 | + |
| 40 | + val above = base + "y" |
| 41 | + val compressed = CompressUtility.compressIfNeeded(above) |
| 42 | + // above threshold should be compressed and round-trip correctly |
| 43 | + assertTrue(compressed.startsWith("GZ:")) |
| 44 | + val roundTrip = CompressUtility.decompressIfNeeded(compressed) |
| 45 | + assertEquals(above, roundTrip) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `EditTextState stores uncompressed Editable when at or below threshold`() { |
| 50 | + val text = "a".repeat(CompressUtility.COMPRESSION_THRESHOLD) |
| 51 | + val editable = Editable.Factory.getInstance().newEditable(text) |
| 52 | + val state = EditTextState(editable, cursorPos = 0) |
| 53 | + |
| 54 | + // Reflect to access the private field textContent and assert it's an Editable |
| 55 | + val field = |
| 56 | + EditTextState::class.java.getDeclaredField("textContent").apply { isAccessible = true } |
| 57 | + val content = field.get(state) |
| 58 | + assertTrue("Expected Editable for small text", content is Editable) |
| 59 | + |
| 60 | + // getEditableText should return the same text |
| 61 | + val out = state.getEditableText() |
| 62 | + assertEquals(text, out.toString()) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `EditTextState compresses large text and round-trips text and spans`() { |
| 67 | + // Create text larger than threshold with distinct regions for spans |
| 68 | + val threshold = CompressUtility.COMPRESSION_THRESHOLD |
| 69 | + val prefix = "p".repeat(10) |
| 70 | + val body = "b".repeat(threshold + 100) |
| 71 | + val suffix = "s".repeat(10) |
| 72 | + val longText = prefix + body + suffix |
| 73 | + |
| 74 | + val editable = Editable.Factory.getInstance().newEditable(longText) |
| 75 | + |
| 76 | + // Apply multiple span types on defined ranges |
| 77 | + // bold on [5, 25) |
| 78 | + editable.setSpan(StyleSpan(Typeface.BOLD), 5, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| 79 | + // italic on [30, 60) |
| 80 | + editable.setSpan(StyleSpan(Typeface.ITALIC), 30, 60, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| 81 | + // monospace on [100, 130) |
| 82 | + editable.setSpan(TypefaceSpan("monospace"), 100, 130, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| 83 | + // strikethrough on [200, 240) |
| 84 | + editable.setSpan(StrikethroughSpan(), 200, 240, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| 85 | + // link on [300, 330) |
| 86 | + editable.setSpan(URLSpan("https://example.com"), 300, 330, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) |
| 87 | + |
| 88 | + val state = EditTextState(editable, cursorPos = 42) |
| 89 | + |
| 90 | + // Verify compressed backing storage via reflection |
| 91 | + val field = |
| 92 | + EditTextState::class.java.getDeclaredField("textContent").apply { isAccessible = true } |
| 93 | + val content = field.get(state) |
| 94 | + assertTrue("Expected ByteArray backing for large text", content is ByteArray) |
| 95 | + |
| 96 | + // Decompress and verify text and spans are preserved |
| 97 | + val out = state.getEditableText() |
| 98 | + assertEquals(longText, out.toString()) |
| 99 | + |
| 100 | + // Validate spans exist at the same ranges |
| 101 | + fun hasSpan( |
| 102 | + rangeStart: Int, |
| 103 | + rangeEnd: Int, |
| 104 | + clazz: Class<*>, |
| 105 | + extra: (Any) -> Boolean = { true }, |
| 106 | + ): Boolean { |
| 107 | + val spans = out.getSpans(rangeStart, rangeEnd, clazz) |
| 108 | + return spans.any { span -> |
| 109 | + val s = out.getSpanStart(span) |
| 110 | + val e = out.getSpanEnd(span) |
| 111 | + s == rangeStart && e == rangeEnd && extra(span) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + assertTrue( |
| 116 | + hasSpan(5, 25, StyleSpan::class.java) { (it as StyleSpan).style == Typeface.BOLD } |
| 117 | + ) |
| 118 | + assertTrue( |
| 119 | + hasSpan(30, 60, StyleSpan::class.java) { (it as StyleSpan).style == Typeface.ITALIC } |
| 120 | + ) |
| 121 | + assertTrue( |
| 122 | + hasSpan(100, 130, TypefaceSpan::class.java) { |
| 123 | + (it as TypefaceSpan).family == "monospace" |
| 124 | + } |
| 125 | + ) |
| 126 | + assertTrue(hasSpan(200, 240, StrikethroughSpan::class.java)) |
| 127 | + assertTrue( |
| 128 | + hasSpan(300, 330, URLSpan::class.java) { (it as URLSpan).url == "https://example.com" } |
| 129 | + ) |
| 130 | + } |
| 131 | +} |
0 commit comments