Display-list based menu slide animation

This commit is contained in:
James Babcock committed 2026-09-23 01:13:03 -07:00
1 parent 92e8def0f0
commit c8d835176f
28 files changed
+1546 -263

No files matched your search

@@ -75,7 +75,7 @@ class DisplayListTest {
@Test fun roundedRectsAndNestedOddNegativeDepthMatchFirmwareOnBothLenses() {
val resources = mapOf(
40 to hex("0404000200f00f0ff0"),
41 to hex("0202000f00080202010001000c0008000300040a0a000402022800050004001f"),
41 to hex("0202000d0008020201010c0008000300040a0a000402022800050004001f"),
42 to hex("02010005000702fd2900"))
val screen = hex("012345601234560112345601234560122345601234560123345601234560123445601234560123455601234f60123456601234560123456001234560123456011234560123456012234560123456012334560123456012344560123456012345")
val expected = listOf("01234560123456011aaaaaaaaaa56012aa45644444aa0123a4564444456a1234a564f44f564a2345a6444fff644a3456a4444456444a4560aa44456444aa56011aaaaaaaaaa56012234560123456012334560123456012344560123456012345", "012345601234560112aaaaaaaaaa60122aa56444445aa1233a5644444564a2344a644f44f644a3455a4444ff6444a4566a4444564444a5600aa44564444aa60112aaaaaaaaaa6012234560123456012334560123456012344560123456012345")
@@ -0,0 +1,140 @@
package com.faceclaw.app
import kotlin.test.*
class DrawExpressionTest {
private fun hex(s: String) = s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private fun evaluate(code: ByteArray, elapsed: Long = 0): Pair<Int, Boolean> {
val frame = DrawEvaluation(elapsed)
return DrawExpression.evaluate(DrawReader(code), frame) to frame.animationPending
}
@Test fun integerWidthsSignExtensionAndReservedPrefixes() {
val signed = mapOf(
0 to "00", 63 to "3f", -64 to "40", -1 to "7f", 64 to "8040", -65 to "bfbf",
8191 to "9fff", -8192 to "a000", 8192 to "c02000", -8193 to "dfdfff",
1048575 to "cfffff", -1048576 to "d00000", 1048576 to "e0100000",
-1048577 to "efefffff", 134217727 to "e7ffffff", -134217728 to "e8000000",
134217728 to "f008000000", Int.MAX_VALUE to "f07fffffff", Int.MIN_VALUE to "f080000000",
)
for ((number, bytes) in signed) {
assertContentEquals(hex(bytes), ExtendedVarint.signed(number))
assertEquals(number, ExtendedVarint.read(DrawReader(hex(bytes)), true))
}
for (value in listOf(0u, 127u, 128u, 16383u, 16384u, 2097151u, 2097152u, 268435455u, 268435456u, UInt.MAX_VALUE)) {
val bytes = ExtendedVarint.unsigned(value)
assertEquals(value, ExtendedVarint.read(DrawReader(bytes), false).toUInt())
for (n in 0 until bytes.size) assertFails { ExtendedVarint.read(DrawReader(bytes.copyOf(n)), false) }
}
for (prefix in 0xf1..0xff) assertFails { ExtendedVarint.read(DrawReader(byteArrayOf(prefix.toByte())), true) }
assertEquals(-1, ExtendedVarint.read(DrawReader(hex("bfff")), true)) // valid non-minimal form
assertFails { ExtendedVarint.evaluate(DrawReader(hex("ff05")), DrawEvaluation(0)) }
}
@Test fun arithmeticTypesStackErrorsAndTimeTracking() {
val vectors = listOf(
"0107010510" to 12, "0107010511" to 2, "0107010512" to 35,
"0179010213" to -3, "0179010214" to -1, "017915" to 7,
"0107010516" to 5, "0107010517" to 7, "01010109" to 9,
"01040310" to 8, "010401070504" to 7,
"010430010230202431" to -6,
"020000c03f02000000402231" to 3,
"01003001806430020000003f4031" to 50,
"01f07fffffff010110" to Int.MIN_VALUE,
"01f080000000017f13" to Int.MIN_VALUE,
"01f080000000017f14" to 0,
)
for ((code, expected) in vectors) assertEquals(expected to false, evaluate(hex(code)), code)
for (bad in listOf("", "10", "0101010013", "010130010110", "020000807f", "ff", "0180", "01013210")) {
assertEquals(0 to false, evaluate(hex(bad)), bad)
}
assertEquals(0 to false, evaluate(ByteArray(66) { if (it % 2 == 0) 1 else 0 })) // stack overflow
assertEquals(0 to false, evaluate(ByteArray(1025)))
for (op in DrawExpression.SMOOTHSTEP..DrawExpression.EASE_IN_OUT_CUBIC) {
assertEquals(0 to false, evaluate(hex("0200000000") + byteArrayOf(op.toByte(), 49)))
assertEquals(1 to false, evaluate(hex("020000803f") + byteArrayOf(op.toByte(), 49)))
}
assertEquals(499 to true, evaluate(hex("0181f432"), 499))
assertEquals(500 to false, evaluate(hex("0181f432"), 500))
assertEquals(500 to false, evaluate(hex("0181f432"), 900))
assertEquals(0 to false, evaluate(hex("0181f432ff"), 250)) // errors discard pending time
}
@Test fun smoothstepCoordinatesMatchFirmwareAndSurvivePresentTimeReset() {
val value = DrawValue.animate(10, 110) as DrawValue.Expression
val code = "010a3001806e300181f432010010300181f43023414031"
assertContentEquals(hex(code), value.program)
for ((time, expected) in listOf(0L to 10, 125L to 25, 250L to 60, 375L to 94, 500L to 110, 900L to 110)) {
assertEquals(expected to (time < 500), evaluate(value.program, time))
}
val continued = DrawValue.animate(10, 110, elapsedMs = 250) as DrawValue.Expression
assertEquals(60 to true, evaluate(continued.program, 0))
assertEquals(110 to false, evaluate(continued.program, 250))
}
@Test fun animatedMenuBridgePreservesElapsedTimeAndTranslation() {
// Tag 6 record emitted by the TypeScript bridge: elapsed=200, delta=(-2,-4).
val wire = hex("06030002000200020001001030020000fefffcffc8002a000000ffffffff")
val input = ArrayByteReader(wire)
assertEquals(6, input.get().toInt())
val row = MenuSelection.read(input, 6).translated(10, 10)
assertEquals(0, input.remaining())
val motion = assertNotNull(row.animation)
assertEquals(42, motion.token)
assertEquals(-2, motion.dx)
assertEquals(-4, motion.dy)
val call = row.calls(7, motion.startedAt + 200).first()
for ((elapsed, expected) in listOf(0L to (11 to 9), 300L to (13 to 12))) {
val reader = DrawReader(call)
assertEquals(8, reader.readU8())
assertEquals(DRAW_FLAG_DEPTH, reader.readU8())
assertEquals(2, reader.readS8())
val frame = DrawEvaluation(elapsed)
assertEquals(expected.first, ExtendedVarint.evaluate(reader, frame))
assertEquals(expected.second, ExtendedVarint.evaluate(reader, frame))
assertEquals(elapsed < 300, frame.animationPending)
}
for (end in 1 until wire.size) assertFails {
MenuSelection.read(ArrayByteReader(wire.copyOf(end), 1), 6)
}
}
@Test fun playerSchedulesOnlyUnfinishedFramesAndCancelsStaleCallbacks() {
val call = DrawProtocol.roundedRect(DrawValue.Integer(0), DrawValue.animate(0, 4), 2, 2, 0, 15)
val renderer = DisplayListRenderer(mapOf(1 to DrawProtocol.displayList(listOf(call))))
val screen = DisplayListRenderer.Target(ByteArray(16), 4, 8)
val output = DisplayListRenderer.Target(ByteArray(16), 4, 8)
var now = 1000L
val queued = mutableListOf<() -> Unit>()
var cancelled = 0
var displayed = 0
val player = DisplayListPlayer(renderer, screen, output, { now }, { delay, callback ->
assertEquals(45, delay)
queued.add(callback);
{ cancelled++ }
}, { displayed++ })
player.present(1)
assertEquals(15, output.get(0, 0))
val first = queued.removeAt(0)
now += 250
first()
assertEquals(15, output.get(0, 2))
assertEquals(0, output.get(0, 0))
val stale = queued.removeAt(0)
player.present(1)
assertEquals(15, output.get(0, 0))
stale()
assertEquals(3, displayed)
now += 500
queued.removeAt(0)()
assertEquals(15, output.get(0, 4))
assertFalse(renderer.animationPending)
assertTrue(queued.isEmpty())
player.present(1)
val stopped = queued.removeAt(0)
player.stop()
stopped()
assertEquals(5, displayed)
assertTrue(cancelled >= 2)
}
}
@@ -0,0 +1,128 @@
package com.faceclaw.app
import kotlin.test.*
class DrawWireTest {
private fun hex(value: String) = value.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
@Test fun encodersUseRevision27CoordinatesAndPreserveOtherWireBytes() {
// Literal vectors pin the firmware grammar independently of the reader.
val calls = listOf(
DrawProtocol.image(0x1234, -32768, 32767, 0x1f, target = 511, depth = -128) to
"0403ff018034120080ff7f1f",
DrawProtocol.rectCopy(DrawProtocol.CURRENT, 1, 2, 3, 4, -5, -6) to
"0200feff0100020003000400fbfffaff",
DrawProtocol.stockText(-1, 2, 15, hex("41c3a9")) to "0300ffff02000f0341c3a9",
DrawProtocol.text(511, -2, 3, 31, hex("01417f")) to "0500ff01feff03001f0301417f",
DrawProtocol.playList(511, depth = 127) to "07027fff01",
DrawProtocol.roundedRect(-1, 2, 640, 480, 65535, 15, 16) to
"08007f028002e001ffff0f10",
DrawProtocol.lut(640, 480, 256) to "0600000000008002e0010123456789abcdef",
DrawProtocol.bbox(hex("ffffffffffffffff"), 4, 0, 0, 8, 2) to "010000000002010f10",
DrawProtocol.bbox(hex("0f"), 1, 1, 0, 1, 1) to "01000101000000010001001f",
)
for ((call, expected) in calls) assertContentEquals(hex(expected), call)
assertContentEquals(hex("0100050007027fff01"), DrawProtocol.sequence(listOf(calls[4].first)))
assertContentEquals(hex("1a0100050007027fff01"), DrawProtocol.message(listOf(calls[4].first)))
assertContentEquals(hex("020100050007027fff01"), DrawProtocol.displayList(listOf(calls[4].first)))
assertContentEquals(hex("1bffff"), DrawProtocol.root(DrawProtocol.SCREEN))
}
@Test fun optimizerFieldsAreReencodedAndOpaqueBytesArePreserved() {
val records = listOf(
"1300020080ff7f1f" to "040000020080ff7f1f",
"14ff01ffff02000f0341017f" to "0500ff01ffff02000f0341017f",
"0fffff02001f0341c3a9" to "0300ffff02001f0341c3a9",
"030001020134120f10" to "010000000102010f10",
"060f10" to "01000100000000080002000f10",
"090100020003000400fbfffaff" to "0200feff0100020003000400fbfffaff",
)
for ((record, expected) in records) {
val calls = DrawProtocol.fromOptimized(hex(record), 8, 2)
assertEquals(1, calls.size)
assertContentEquals(hex(expected), calls.single(), record)
}
val nested = hex("0802") + records.take(2).fold(byteArrayOf()) { bytes, (record, _) ->
val payload = hex(record)
bytes + DrawProtocol.word(payload.size) + payload
}
val calls = DrawProtocol.fromOptimized(nested)
assertEquals(2, calls.size)
for (i in calls.indices) assertContentEquals(hex(records[i].second), calls[i])
assertFails { DrawProtocol.fromOptimized(hex("1300020080ff7f")) }
assertFails { DrawProtocol.fromOptimized(hex("1300020080ff7f1f00")) }
assertFails { DrawProtocol.fromOptimized(hex("0fffff02001f0441c3a9")) }
}
@Test fun everyTruncatedOrOverlongCallIsRejectedBeforeMutation() {
val font = ByteArray(197)
font[0] = 1
font[67] = 193.toByte() // Glyph A.
hex("0801011f").copyInto(font, 193)
val resources = mapOf(
1 to hex("0401000100f0"),
2 to font,
3 to DrawProtocol.displayList(emptyList()),
)
val renderer = DisplayListRenderer(resources, builtin = { _, _ ->
DisplayListRenderer.BuiltinGlyph(hex("0801011f"), 1)
})
val calls = listOf(
DrawProtocol.image(1, 0, 0, target = 1, depth = -1),
DrawProtocol.rectCopy(DrawProtocol.CURRENT, 0, 0, 2, 2, 1, 1),
DrawProtocol.stockText(0, 0, 15, hex("41")),
DrawProtocol.text(2, 0, 0, 15, hex("41")),
DrawProtocol.playList(3),
DrawProtocol.roundedRect(0, 0, 4, 4, 1, 15),
DrawProtocol.lut(8, 4, 128),
DrawProtocol.bbox(ByteArray(16) { -1 }, 4, 0, 0, 8, 4),
)
val screen = ByteArray(16) { 0x12 }
val target = DisplayListRenderer.Target(screen, 8, 4)
val valid = DrawProtocol.roundedRect(0, 0, 4, 4, 0, 15)
for (call in calls) {
// Establish that each fixture is valid before testing malformed forms.
renderer.execute(DrawProtocol.sequence(listOf(call)), target)
screen.fill(0x12)
val savedResources = resources.mapValues { it.value.copyOf() }
val malformed = (0 until call.size).map { call.copyOf(it) } + listOf(call + byteArrayOf(0))
for (bad in malformed) {
assertFails { renderer.execute(DrawProtocol.sequence(listOf(valid, bad, valid)), target) }
assertContentEquals(ByteArray(16) { 0x12 }, screen)
for ((id, bytes) in savedResources) assertContentEquals(bytes, resources[id])
}
}
val sequence = DrawProtocol.sequence(listOf(valid))
for (length in 0 until sequence.size) {
assertFails { renderer.execute(sequence.copyOf(length), target) }
assertContentEquals(ByteArray(16) { 0x12 }, screen)
}
assertFails { renderer.execute(sequence + byteArrayOf(0), target) }
assertContentEquals(ByteArray(16) { 0x12 }, screen)
}
@Test fun batchesRespectEncodedLengthsAndCallBudget() {
val call = DrawProtocol.playList(511)
val messages = DrawProtocol.messages(List(3) { call }, maxBytes = 15)
assertEquals(listOf(15, 9), messages.map { it.size })
assertContentEquals(hex("1a020004000700ff0104000700ff01"), messages[0])
assertFails { DrawProtocol.messages(listOf(call), maxBytes = 8) }
val many = DrawProtocol.messages(List(4097) { call })
assertEquals(listOf(4096, 1), many.map { DrawProtocol.u16(it, 1) })
assertTrue(DrawProtocol.messages(emptyList()).isEmpty())
}
@Test fun resourceReaderViewsUseRelativeOffsetsAndCannotEscapeTheirBounds() {
val reader = ArrayByteReader(hex("ff123456ee"), 1, 4)
assertEquals(0x12.toByte(), reader.get(0))
assertEquals(0x3412.toShort(), reader.getShort())
assertEquals(1, reader.remaining())
assertFails { reader.get(ByteArray(2)) }
assertEquals(0x56.toByte(), reader.get())
assertEquals(0, reader.remaining())
assertFails { reader.get() }
assertFails { reader.get(3) }
assertFails { reader.get(-1) }
assertFails { ArrayByteReader(byteArrayOf(1), 1, 2) }
}
}
@@ -0,0 +1,36 @@
package com.faceclaw.app
import kotlin.test.Test
import kotlin.test.assertTrue
import platform.Foundation.NSDate
import platform.Foundation.NSDefaultRunLoopMode
import platform.Foundation.NSRunLoop
import platform.Foundation.NSThread
import platform.Foundation.dateWithTimeIntervalSinceNow
import platform.Foundation.runMode
class IosBleCentralTest {
/** Registering a state listener must create the central and report a state without any scan. */
@Test
fun scanListenerAloneReceivesABluetoothStateReport() {
val central = IosBleCentral(IosProtocolPlatform)
val states = ArrayList<Int>()
var offMain = 0
central.setScanListener(object : IosBleScanListener {
override fun onBluetoothState(state: Int, authorization: Int) {
if (!NSThread.isMainThread) offMain++
states.add(state)
}
override fun onAdvertisement(identifier: String, name: String, manufacturerDataHex: String, rssi: Int, connectable: Boolean) {}
})
val deadline = IosProtocolPlatform.elapsedRealtimeMs() + 5_000
while (IosProtocolPlatform.elapsedRealtimeMs() < deadline && states.none { it != 0 }) {
NSRunLoop.mainRunLoop.runMode(NSDefaultRunLoopMode, NSDate.dateWithTimeIntervalSinceNow(0.02))
}
// The simulator reports unsupported/powered-off; a real phone powered-on. Any non-unknown state proves the central exists.
assertTrue(states.any { it != 0 }, "no CBManagerState reported: $states")
assertTrue(offMain == 0, "state reports must arrive on the main queue")
central.close()
}
}