|
| 1 | +package expo.modules.t3reviewdiff |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.graphics.Color |
| 5 | +import android.os.Looper |
| 6 | +import expo.modules.kotlin.AppContext |
| 7 | +import java.util.concurrent.CountDownLatch |
| 8 | +import java.util.concurrent.ExecutorService |
| 9 | +import java.util.concurrent.Semaphore |
| 10 | +import java.util.concurrent.atomic.AtomicInteger |
| 11 | +import java.util.concurrent.atomic.AtomicReference |
| 12 | +import org.junit.Assert.assertEquals |
| 13 | +import org.junit.Assert.assertTrue |
| 14 | +import org.junit.Test |
| 15 | +import org.junit.runner.RunWith |
| 16 | +import org.robolectric.Robolectric |
| 17 | +import org.robolectric.RobolectricTestRunner |
| 18 | +import org.robolectric.Shadows.shadowOf |
| 19 | +import org.robolectric.annotation.Config |
| 20 | +import org.robolectric.annotation.LooperMode |
| 21 | + |
| 22 | +internal object TokenDecodeProbe { |
| 23 | + val entries = AtomicInteger() |
| 24 | + val publications = AtomicInteger() |
| 25 | + |
| 26 | + @Volatile var onEntry: ((Int) -> Unit)? = null |
| 27 | + |
| 28 | + fun reset() { |
| 29 | + entries.set(0) |
| 30 | + publications.set(0) |
| 31 | + onEntry = null |
| 32 | + } |
| 33 | + |
| 34 | + fun enter() { |
| 35 | + val entry = entries.incrementAndGet() |
| 36 | + onEntry?.invoke(entry) |
| 37 | + } |
| 38 | + fun publish() { |
| 39 | + publications.incrementAndGet() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +@RunWith(RobolectricTestRunner::class) |
| 44 | +@Config(sdk = [35], manifest = Config.NONE) |
| 45 | +@LooperMode(LooperMode.Mode.PAUSED) |
| 46 | +class ReviewDiffTokenDecodeTest { |
| 47 | + @Test |
| 48 | + fun queuedFullReplacementSkipsOnlyObsoleteWorkAndPreservesPatchOrdering() { |
| 49 | + for (case in queuedCases()) { |
| 50 | + fixture { view, executor -> |
| 51 | + TokenDecodeProbe.reset() |
| 52 | + view.setContentResetKey("content") |
| 53 | + view.setTokensResetKey("tokens") |
| 54 | + view.setTokensJson(payload("seed")) |
| 55 | + drain(executor) |
| 56 | + TokenDecodeProbe.reset() |
| 57 | + val entered = CountDownLatch(1) |
| 58 | + val release = Semaphore(0) |
| 59 | + executor.execute { |
| 60 | + entered.countDown() |
| 61 | + release.acquire() |
| 62 | + } |
| 63 | + entered.await() |
| 64 | + val acknowledgements = mutableListOf<Boolean>() |
| 65 | + try { |
| 66 | + case.enqueue(view) { id, chunk -> |
| 67 | + view.setTokensPatchJson(patchPayload(id, chunk)) { acknowledgements.add(it) } |
| 68 | + } |
| 69 | + } finally { |
| 70 | + release.release() |
| 71 | + } |
| 72 | + drain(executor) |
| 73 | + assertEquals("${case.name} decoder entry", case.entries, TokenDecodeProbe.entries.get()) |
| 74 | + // Android's malformed full payload publishes its existing empty-map fallback. |
| 75 | + assertEquals( |
| 76 | + "${case.name} retained full publication", |
| 77 | + case.entries, |
| 78 | + TokenDecodeProbe.publications.get() |
| 79 | + ) |
| 80 | + assertEquals("${case.name} token output", expected(case.ids), tokens(view)) |
| 81 | + assertEquals("${case.name} patch acknowledgements", case.acks, acknowledgements) |
| 82 | + println( |
| 83 | + "Passed tokens: ${case.name}, decoder entries=${case.entries}, " + |
| 84 | + "retained full publications=${case.entries}" |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private data class QueuedCase( |
| 91 | + val name: String, |
| 92 | + val ids: List<String> = listOf("b"), |
| 93 | + val entries: Int = 1, |
| 94 | + val acks: List<Boolean> = emptyList(), |
| 95 | + val enqueue: (T3ReviewDiffView, (String, Int) -> Unit) -> Unit |
| 96 | + ) |
| 97 | + |
| 98 | + private fun queuedCases() = listOf( |
| 99 | + QueuedCase("A B C burst", ids = listOf("c")) { view, _ -> |
| 100 | + listOf("a", "b", "c").forEach { view.setTokensJson(payload(it)) } |
| 101 | + }, |
| 102 | + QueuedCase("content reset", ids = emptyList(), entries = 0) { view, _ -> |
| 103 | + view.setTokensJson(payload("a")) |
| 104 | + view.setContentResetKey("new") |
| 105 | + }, |
| 106 | + QueuedCase("reset then B") { view, _ -> |
| 107 | + view.setTokensJson(payload("a")) |
| 108 | + view.setContentResetKey("new") |
| 109 | + view.setTokensJson(payload("b")) |
| 110 | + }, |
| 111 | + QueuedCase("same content key") { view, _ -> |
| 112 | + view.setTokensJson(payload("b")) |
| 113 | + view.setContentResetKey("content") |
| 114 | + }, |
| 115 | + QueuedCase("token reset preserves full") { view, _ -> |
| 116 | + view.setTokensJson(payload("b")) |
| 117 | + view.setTokensResetKey("new") |
| 118 | + }, |
| 119 | + QueuedCase("same token key") { view, _ -> |
| 120 | + view.setTokensJson(payload("b")) |
| 121 | + view.setTokensResetKey("tokens") |
| 122 | + }, |
| 123 | + QueuedCase("current full") { view, _ -> view.setTokensJson(payload("b")) }, |
| 124 | + QueuedCase("current malformed", ids = emptyList()) { view, _ -> |
| 125 | + view.setTokensJson("invalid JSON") |
| 126 | + }, |
| 127 | + QueuedCase("obsolete malformed") { view, _ -> |
| 128 | + view.setTokensJson("invalid JSON") |
| 129 | + view.setTokensJson(payload("b")) |
| 130 | + }, |
| 131 | + QueuedCase("full patch full", acks = listOf(true)) { view, patch -> |
| 132 | + view.setTokensJson(payload("a")) |
| 133 | + patch("patch", 0) |
| 134 | + view.setTokensJson(payload("b")) |
| 135 | + }, |
| 136 | + QueuedCase( |
| 137 | + "full then patches", |
| 138 | + ids = listOf("b", "one", "two"), |
| 139 | + acks = listOf(true, true) |
| 140 | + ) { view, patch -> |
| 141 | + view.setTokensJson(payload("b")) |
| 142 | + patch("one", 0) |
| 143 | + patch("two", 1) |
| 144 | + }, |
| 145 | + QueuedCase("patches then full", acks = listOf(true, true)) { view, patch -> |
| 146 | + patch("one", 0) |
| 147 | + patch("two", 1) |
| 148 | + view.setTokensJson(payload("b")) |
| 149 | + }, |
| 150 | + QueuedCase( |
| 151 | + "additive patches", |
| 152 | + ids = listOf("seed", "one", "two"), |
| 153 | + entries = 0, |
| 154 | + acks = listOf(true, true) |
| 155 | + ) { _, patch -> |
| 156 | + patch("one", 0) |
| 157 | + patch("two", 1) |
| 158 | + }, |
| 159 | + QueuedCase("reset rejects old patch", acks = listOf(false)) { view, patch -> |
| 160 | + patch("old", 0) |
| 161 | + view.setContentResetKey("new") |
| 162 | + view.setTokensJson(payload("b")) |
| 163 | + }, |
| 164 | + ) |
| 165 | + |
| 166 | + @Test |
| 167 | + fun enteredFullSuccessAndErrorFinishButOnlyTheReplacementPublishes() { |
| 168 | + for (invalid in listOf(false, true)) { |
| 169 | + fixture { view, executor -> |
| 170 | + TokenDecodeProbe.reset() |
| 171 | + val entered = CountDownLatch(1) |
| 172 | + val release = Semaphore(0) |
| 173 | + TokenDecodeProbe.onEntry = { entry -> |
| 174 | + if (entry == 1) { |
| 175 | + entered.countDown() |
| 176 | + release.acquire() |
| 177 | + } |
| 178 | + } |
| 179 | + view.setTokensJson(if (invalid) "invalid JSON" else payload("a")) |
| 180 | + entered.await() |
| 181 | + try { |
| 182 | + view.setContentResetKey("new") |
| 183 | + view.setTokensJson(payload("b")) |
| 184 | + } finally { |
| 185 | + release.release() |
| 186 | + } |
| 187 | + drain(executor) |
| 188 | + assertEquals(2, TokenDecodeProbe.entries.get()) |
| 189 | + assertEquals(1, TokenDecodeProbe.publications.get()) |
| 190 | + assertEquals(expected(listOf("b")), tokens(view)) |
| 191 | + println( |
| 192 | + "Passed entered tokens invalid=$invalid, decoder entries=2, retained full publications=1" |
| 193 | + ) |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + fun cleanupDiscardsQueuedFullWorkOnTheProductionExecutor() { |
| 200 | + fixture { view, executor -> |
| 201 | + TokenDecodeProbe.reset() |
| 202 | + val entered = CountDownLatch(1) |
| 203 | + val worker = AtomicReference<Thread>() |
| 204 | + val release = Semaphore(0) |
| 205 | + executor.execute { |
| 206 | + worker.set(Thread.currentThread()) |
| 207 | + entered.countDown() |
| 208 | + try { |
| 209 | + release.acquire() |
| 210 | + } catch ( |
| 211 | + _: InterruptedException |
| 212 | + ) { |
| 213 | + Thread.currentThread().interrupt() |
| 214 | + } |
| 215 | + } |
| 216 | + entered.await() |
| 217 | + view.setTokensJson(payload("discarded")) |
| 218 | + view.cleanup() |
| 219 | + worker.get().join() |
| 220 | + shadowOf(Looper.getMainLooper()).idle() |
| 221 | + assertEquals(0, TokenDecodeProbe.entries.get()) |
| 222 | + assertEquals(0, TokenDecodeProbe.publications.get()) |
| 223 | + assertTrue(tokens(view).isEmpty()) |
| 224 | + println("Passed cleanup before entry, decoder entries=0, retained full publications=0") |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + fun cleanupAfterEntryKeepsTheExistingCurrentFullPublication() { |
| 230 | + fixture { view, _ -> |
| 231 | + TokenDecodeProbe.reset() |
| 232 | + val entered = CountDownLatch(1) |
| 233 | + val release = Semaphore(0) |
| 234 | + val worker = AtomicReference<Thread>() |
| 235 | + TokenDecodeProbe.onEntry = { |
| 236 | + worker.set(Thread.currentThread()) |
| 237 | + entered.countDown() |
| 238 | + // shutdownNow interrupts this worker; entered decoding must still finish. |
| 239 | + release.acquireUninterruptibly() |
| 240 | + } |
| 241 | + view.setTokensJson(payload("entered")) |
| 242 | + entered.await() |
| 243 | + try { |
| 244 | + view.cleanup() |
| 245 | + } finally { |
| 246 | + release.release() |
| 247 | + } |
| 248 | + worker.get().join() |
| 249 | + shadowOf(Looper.getMainLooper()).idle() |
| 250 | + assertEquals(1, TokenDecodeProbe.entries.get()) |
| 251 | + assertEquals(1, TokenDecodeProbe.publications.get()) |
| 252 | + assertEquals(expected(listOf("entered")), tokens(view)) |
| 253 | + println("Passed cleanup after entry, decoder entries=1, retained full publications=1") |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + private fun fixture(run: (T3ReviewDiffView, ExecutorService) -> Unit) { |
| 258 | + val controller = Robolectric.buildActivity(Activity::class.java).setup() |
| 259 | + val view = T3ReviewDiffView(controller.get(), AppContext()) |
| 260 | + controller.get().setContentView(view) |
| 261 | + val executor = field(view, "payloadDecodeExecutor") as ExecutorService |
| 262 | + try { |
| 263 | + run(view, executor) |
| 264 | + } finally { |
| 265 | + view.cleanup() |
| 266 | + TokenDecodeProbe.reset() |
| 267 | + controller.pause().stop().destroy() |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + private fun drain(executor: ExecutorService) { |
| 272 | + executor.submit {}.get() |
| 273 | + shadowOf(Looper.getMainLooper()).idle() |
| 274 | + } |
| 275 | + |
| 276 | + private fun field(target: Any, name: String): Any = target.javaClass.getDeclaredField(name) |
| 277 | + .apply { isAccessible = true }.get(target) |
| 278 | + |
| 279 | + private fun tokens(view: T3ReviewDiffView) = field( |
| 280 | + view.getChildAt(0), |
| 281 | + "tokensByRowId" |
| 282 | + ) as Map<*, *> |
| 283 | + |
| 284 | + private fun expected(ids: List<String>) = ids.associateWith { |
| 285 | + listOf(DiffToken("漢字 👨👩👧👦 é", Color.RED, 3), DiffToken("tail", null, 0)) |
| 286 | + } |
| 287 | + |
| 288 | + private fun payload(id: String) = |
| 289 | + """{"$id":[{"content":"漢字 👨👩👧👦 é","color":"#ff0000","fontStyle":3},{"content":"tail","fontStyle":0}]}""" |
| 290 | + |
| 291 | + private fun patchPayload(id: String, chunk: Int) = |
| 292 | + """{"resetKey":"tokens","chunkIndex":$chunk,"tokensByRowId":${payload(id)}}""" |
| 293 | +} |
0 commit comments