Merge branch 'main' of https://github.com/jimrandomh/faceclaw into ring-health-fixes

This commit is contained in:
James Babcock committed 2026-10-06 13:32:49 -07:00
commit e12cfaaf38
309 files changed
+15906 -3352

No files matched your search

+8 -1
View File
@@ -7,13 +7,20 @@ plugins {
// Compile the production sources without building NativeScript or touching BLE.
kotlin {
// Match native/kotlin/shared/build.gradle.kts.
compilerOptions {
freeCompilerArgs.add("-Xexpect-actual-classes")
allWarningsAsErrors.set(true)
}
android {
namespace = "com.faceclaw.protocoltests"
compileSdk = 35
minSdk = 24
withJava()
compilerOptions { jvmTarget.set(JvmTarget.JVM_17) }
withHostTestBuilder {}.configure {}
// Production code logs through android.util.Log, which the host-test android.jar
// stubs to throw; a throw on the session worker turns into a spurious reconnect.
withHostTestBuilder {}.configure { isReturnDefaultValues = true }
}
iosArm64()
iosSimulatorArm64()
@@ -0,0 +1,44 @@
package com.faceclaw.app
import java.util.Collections
import kotlin.test.*
/**
* The phone mirror pulls previews only when told to, while the compositor's player advances an
* animated display list on its own timer. It needs a notification after every redraw, the
* settled one included, or it keeps a mid-slide frame. Host-only: it runs the real redraw thread.
*/
class PreviewAnimationListenerTest {
// FrameDisplayListTest's menu golden (a 2x2 highlight sliding in to rest at (3, 2)),
// restarted at 0 of its 240 ms so the slide is still in flight at the first pull.
private val menu = "077f0000000300020002000200022a00000096000000010002000200ffffffff0200080000ff24017e30020000000001812c8001812c1611010017328001812c16103001812c3023414031ff24017c30020000000001812c8001812c1611010017328001812c16103001812c30234140310200020001000103100400000000000000001f"
.chunked(2).map { it.toInt(16).toByte() }.toByteArray().also { it.fill(0, 18, 22) }
private fun settledPreview(): ByteArray {
val reader = ArrayByteReader(menu)
assertEquals(DrawRecordKind.DISPLAY_LIST, reader.get().toInt())
val list = FrameDisplayList.read(reader, drawAnimationTimeMs() - 10_000)
return ShellScene(emptyList(), listOf(list)).preview(ByteArray(64), 8, 8)
}
@Test fun mirrorIsNotifiedOfEachRedrawThroughTheSettledFrame() {
val c = SurfaceCompositor()
c.configureScreen(8, 8)
c.configureSurface("app", 0, 0, 8, 8, 0, 0)
val pulled = Collections.synchronizedList(ArrayList<ByteArray>())
c.setPreviewAnimationListener { pulled.add(assertNotNull(c.previewComposite()).gray) }
c.submitSurface("app", ArrayByteReader(ByteArray(64)), 0, 0, 8, 8, "menu", ArrayByteReader(menu))
val settled = settledPreview()
assertFalse(assertNotNull(c.previewComposite()).gray.contentEquals(settled), "the first pull is mid-slide")
// Redraws are 45 ms apart; 150 ms without one means the player has stopped.
val deadline = System.nanoTime() + 2_000_000_000L
var count = -1
while (System.nanoTime() < deadline && pulled.size != count) {
count = pulled.size
Thread.sleep(150)
}
assertEquals(count, pulled.size, "the player stops once the slide's timeline ends")
assertTrue(pulled.size >= 2, "one notification per redraw")
assertContentEquals(settled, pulled.last(), "the last notification's pull is the settled frame")
}
}
@@ -36,6 +36,46 @@ class DisplayListTest {
renderer.render(DrawProtocol.SCREEN, DisplayListRenderer.Target(source,640,480), DisplayListRenderer.Target(destination,640,480))
assertContentEquals(source, destination)
}
@Test fun rectCopyMatchesPerPixelReferenceWithShiftsClipsAndAliasing() {
val random = Random(7)
repeat(4000) { case ->
val width = random.nextInt(1, 20); val height = random.nextInt(1, 6)
val offset = random.nextInt(3); val shift = random.nextInt(-4, 5)
val clip = if (random.nextBoolean()) null else {
val left = random.nextInt(-3, width + 1); val top = random.nextInt(-2, height + 1)
intArrayOf(left, top, left + random.nextInt(0, width + 3), top + random.nextInt(0, height + 2))
}
// Screen as a separate array, the target's own array at another offset, or the target itself.
val mode = random.nextInt(3)
val screenWidth = random.nextInt(1, 20); val screenHeight = random.nextInt(1, 6)
val screenOffset = random.nextInt(3)
val targetSize = offset + (width + 1) / 2 * height
val screenSize = screenOffset + (screenWidth + 1) / 2 * screenHeight
val bytes = random.nextBytes(maxOf(targetSize, if (mode == 1) screenSize else 0) + 2)
val screenBytes = if (mode == 0) random.nextBytes(screenSize + 1) else bytes
val sourceId = if (mode == 2) DrawProtocol.CURRENT else DrawProtocol.SCREEN
val sourceWidth = if (mode == 2) width else screenWidth
val sourceHeight = if (mode == 2) height else screenHeight
val w = random.nextInt(1, sourceWidth + 1); val h = random.nextInt(1, sourceHeight + 1)
val x = random.nextInt(sourceWidth - w + 1); val y = random.nextInt(sourceHeight - h + 1)
val dx = random.nextInt(-w - 5, width + 5); val dy = random.nextInt(-h - 2, height + 2)
val expected = bytes.copyOf()
val expectedScreen = if (mode == 0) screenBytes.copyOf() else expected
val referenceTarget = DisplayListRenderer.Target(expected, width, height, offset, shift, clip)
val referenceSource = if (mode == 2) referenceTarget
else DisplayListRenderer.Target(expectedScreen, screenWidth, screenHeight, screenOffset)
val snapshot = IntArray(w * h) { referenceSource.get(x + it % w, y + it / w) }
for (i in snapshot.indices) referenceTarget.put(dx + i % w, dy + i / w, snapshot[i])
val target = DisplayListRenderer.Target(bytes, width, height, offset, shift, clip)
val screen = if (mode == 2) target else DisplayListRenderer.Target(screenBytes, screenWidth, screenHeight, screenOffset)
DisplayListRenderer(emptyMap()).execute(
DrawProtocol.sequence(listOf(DrawProtocol.rectCopy(sourceId, x, y, w, h, dx, dy))), target, screen)
assertContentEquals(expected, bytes, "case $case")
if (mode == 0) assertContentEquals(expectedScreen, screenBytes, "case $case screen")
}
}
@Test fun invalidGraphDoesNotCopyScreenIntoComposition() {
val screen = hex("12345678"); val composition = hex("9abcdef0")
val resources = mapOf(1 to DrawProtocol.displayList(listOf(byteArrayOf(99,0))))
@@ -259,6 +299,79 @@ class DisplayListTest {
assertEquals(0, output.get(if (right) 639 else 0, 240)) // cleared, not stale
}
}
@Test fun addedDepthJoinsOrInsertsTheDepthField() {
val copy = DrawProtocol.rectCopy(DrawProtocol.SCREEN, 0, 0, 4, 4, 0, 0)
assertContentEquals(DrawProtocol.rectCopy(DrawProtocol.SCREEN, 0, 0, 4, 4, 0, 0, depth = 8), DrawProtocol.withAddedDepth(copy, 8))
val targeted = DrawProtocol.image(3, 1, 2, target = 5, depth = -2)
assertContentEquals(DrawProtocol.image(3, 1, 2, target = 5, depth = 6), DrawProtocol.withAddedDepth(targeted, 8))
assertSame(copy, DrawProtocol.withAddedDepth(copy, 0))
assertContentEquals(DrawProtocol.image(3, 1, 2, depth = 127), DrawProtocol.withAddedDepth(DrawProtocol.image(3, 1, 2, depth = 120), 16))
}
/** The bottom switcher's Depth setting: the shell scene's depth moves the screen, shell layers and selections together. */
@Test fun shellSceneDepthShiftsTheWholePresentationButNotDimming() {
val c = SurfaceCompositor()
c.configureScreen(640, 480)
c.configureSurface("app", 32, 0, 576, 480, 0, 0)
c.submitSurface("app", ArrayByteReader(ByteArray(576 * 480) { if (it % 576 in 100 until 110) 255.toByte() else 0 }),
0, 0, 576, 480, "stripe")
// One white 2x1 shell layer at (200, 10), dimming what is under it, at layer depth -2; scene depth 8.
val header = listOf(1, 1, 200, 10, 2, 1, 128, 0, -2).flatMap { listOf(it.toByte(), (it shr 8).toByte()) }.toByteArray()
c.setShellScene(ArrayByteReader(header + byteArrayOf(-1, -1) + DrawProtocol.word(8)))
val composite = c.composite()
assertEquals(8, composite.shellScene.screenDepth)
val calls = composite.shellScene.calls(640, 480, intArrayOf(1), IntArray(0))
assertTrue(calls.any { it.contentEquals(DrawProtocol.lut(640, 480, 128)) }) // the dim stays whole-screen
val screen = BmpUtil.pack4bppFromGray8(composite.screenGray, 640, 480)
for (right in listOf(false, true)) {
val glasses = Glasses(right)
glasses.apply(ScenePlanner(ResourceCacheState()).plan(screen, 640, 480, null, composite.shellScene, 1).commands)
val output = DisplayListRenderer.Target(glasses.composition, 640, 480)
// Screen: half of 8 per lens. Layer: depth -2 + 8, so one pixel less than the screen's shift on each lens.
val shift = if (right) -4 else 4
val layerShift = if (right) -3 else 3
assertEquals(0, output.get(131 + shift, 200)); assertTrue(output.get(132 + shift, 200) > 0)
assertTrue(output.get(141 + shift, 200) > 0); assertEquals(0, output.get(142 + shift, 200))
assertEquals(15, output.get(200 + layerShift, 10)); assertEquals(15, output.get(201 + layerShift, 10))
}
// The phone mirror shows the scene unshifted, so touches land where they look.
val preview = assertNotNull(c.previewComposite()).gray
assertEquals(0, preview[131].toInt() and 255); assertTrue((preview[132].toInt() and 255) > 0)
// A covering surface's own depth still wins over the scene's.
c.configureSurface("glance", 0, 0, 640, 480, 900, 0)
c.setSurfaceDepth("glance", -16)
assertEquals(-16, c.composite().shellScene.screenDepth)
}
/** #42: stacked modal-sized layers overflow the cache, so the frame bakes the bottom ones instead of throwing. */
@Test fun shellOverTheResourceBudgetBakesBottomLayersAndRecovers() {
val cache = ResourceCacheState(); val planner = ScenePlanner(cache)
val gray = ByteArray(640 * 480) { 48 }; val app = BmpUtil.pack4bppFromGray8(gray, 640, 480)
fun layer(key: Int, y: Int, h: Int, nibble: Int, depth: Int = 0) =
ShellScene.Layer(key, 14, y, 612, h, 256, ByteArray(306 * h) { (nibble * 17).toByte() }, depth = depth)
// Each 612x214 layer is just under the 64 KiB resource cap; three exceed the arena on their own.
fun chrome(depth: Int) = layer(1, 444, 36, 7, depth)
val modals = listOf(layer(2, 10, 214, 9), layer(3, 120, 214, 11), layer(4, 230, 214, 13, depth = 4))
val overflow = ShellScene(listOf(chrome(6)) + modals)
val plan = planner.plan(app, 640, 480, null, overflow, 1)
assertEquals(2, plan.bakedLayers); assertFalse(plan.bakedSelections)
assertTrue(plan.commands.all { it.size <= 65535 })
assertTrue(cache.usedBytes() <= ResourceCacheState.CACHE_SIZE)
// Baked layers lose their stereo depth; the live ones keep it.
val expected = ShellScene(listOf(chrome(0)) + modals)
val lenses = listOf(Glasses(), Glasses(true))
for (glasses in lenses) {
glasses.apply(plan.commands)
assertContentEquals(BmpUtil.pack4bppFromGray8(expected.preview(gray, 640, 480, glasses.right), 640, 480), glasses.composition)
}
// Once the shell fits again, the next frame is fully live and the screen is the app's again.
val fits = ShellScene(listOf(chrome(0), modals[2]))
val next = planner.plan(app, 640, 480, null, fits, plan.nextFid)
assertEquals(0, next.bakedLayers)
for (glasses in lenses) {
glasses.apply(next.commands)
assertContentEquals(app, glasses.screen)
assertContentEquals(BmpUtil.pack4bppFromGray8(fits.preview(gray, 640, 480, glasses.right), 640, 480), glasses.composition)
}
}
@Test fun noisyFramesRemainBoundedAndResourceBudgetIs192KiB() {
val cache=ResourceCacheState();val planner=ScenePlanner(cache);val glasses=Glasses()
val app=ByteArray(640*480/2){(it*37).toByte()}
@@ -96,7 +96,9 @@ class DrawWireTest {
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))
// A rounded rect may end in one outside-color byte (revision 36), but not one past 15.
val overlong = if (call[0].toInt() == DRAW_OP_ROUNDED_RECT) byteArrayOf(16) else byteArrayOf(0)
val malformed = (0 until call.size).map { call.copyOf(it) } + listOf(call + overlong, call + byteArrayOf(0, 0))
for (bad in malformed) {
assertFails { renderer.execute(DrawProtocol.sequence(listOf(valid, bad, valid)), target) }
assertContentEquals(ByteArray(16) { 0x12 }, screen)
@@ -4,13 +4,13 @@ import kotlin.test.*
class FrameDisplayListTest {
// Golden records emitted by display-list-authoring.test.cjs.
private val menu = "077e0000000300020002000200022a00000096000000010002000200ffffffff0200080000ff24017e30020000000001812c8001812c1611010017328001812c16103001812c3023414031ff24017c30020000000001812c8001812c1611010017328001812c16103001812c302341403102000200010001030400000000000000001f"
private val menu = "077f0000000300020002000200022a00000096000000010002000200ffffffff0200080000ff24017e30020000000001812c8001812c1611010017328001812c16103001812c3023414031ff24017c30020000000001812c8001812c1611010017328001812c16103001812c30234140310200020001000103100400000000000000001f"
private val multi = "0744000000030002000400010002000000000000000002000200010000ff010001006003000400000000000000001f0200000100000001000100020002feffffff7f00010001007f00"
private val scroll = "078a000000030002000400020000070000009600000001000200040011223344556677880200020000000000ff250100300102300180f0800180f0161101001732800180f01610300180f0302341403101001102000200010008000000ff280101300100300180f0800180f0161101001732800180f01610300180f030234140310100170100160400020000000103"
private val scroll = "078b000000030002000400020000070000009600000001000200040011223344556677880200020000000000ff250100300102300180f0800180f0161101001732800180f01610300180f0302341403101001102000200010008000000ff280101300100300180f0800180f0161101001732800180f01610300180f03023414031010017010016040002000000010310"
// Clipped clear, clipped rounded rect, and replayed glyph + icon records with
// placeholder atlas ids 0xbeef / 0xcafebabe, plus what they paint over a
// nibble-1 16x8 screen (display-list-authoring.test.cjs).
private val replay = "0780000000020003000800040000070000006400000000000300890000000000000600040002880000010001000200020000000400030000000510a00000000000000600040000ff2502000080c002000000000180c8800180c8161101001732800180c81610300180c830234031020000efbe4100000000000000ff01bebafeca04000100"
private val replay = "078100000002000300080004000007000000640000000000030089000000000000060004000288000001000100020002000000040003000000051010a00000000000000600040000ff2502000080c002000000000180c8800180c8161101001732800180c81610300180c830234031020000efbe4100000000000000ff01bebafeca04000100"
private val replayPixels = "11111111111111111111111111111111111111111111111111fff28f111111111125522211111111112552221111111111222222111111111111111111111111"
private fun hex(s: String) = s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private fun read(bytes: ByteArray, now: Long = 1150): FrameDisplayList {
@@ -43,7 +43,8 @@ class FrameDisplayListTest {
}
@Test fun animatedCopySourcesRebindOnPresentAndDestinationsTranslate() {
// Emitted by menu-scroll-animation.test.cjs: a menu strip scrolling from row 0 to row 2.
// Recorded from a since-removed menu-scroll-animation.test.cjs case (240ms slides):
// a menu strip scrolling from row 0 to row 2.
val list = read(hex(scroll)).translated(10, 10)
fun copy(elapsed: Long): List<Int> {
val call = DrawReader(list.calls(intArrayOf(5), 1150).first())
@@ -98,7 +99,7 @@ class FrameDisplayListTest {
// One opaque 1x1 shell layer, with one generic list after its pixels.
val layer = DrawProtocol.word(1) + DrawProtocol.word(1) + DrawProtocol.word(0) + DrawProtocol.word(0) +
DrawProtocol.word(1) + DrawProtocol.word(1) + DrawProtocol.word(256) + DrawProtocol.word(1) +
DrawProtocol.word(0) + byteArrayOf(32) + hex(multi)
DrawProtocol.word(0) + byteArrayOf(32) + hex(multi) + DrawProtocol.word(0)
val shell = ShellScene.decode(ArrayByteReader(layer))
assertEquals(2, shell.retainedResources.size)
assertEquals(240, shell.preview(ByteArray(64) { 32 }, 8, 8)[21].toInt() and 255)
@@ -141,6 +142,28 @@ class FrameDisplayListTest {
assertEquals(replayPixels, nibbles)
}
// A window-frame rounded rect (display-list-authoring.test.cjs): no fill, a
// border of 3, black outside the curve (revision 36), clipped short of its
// bottom row, over a nibble-9 12x8 screen.
private val frame = "072b00000002000100080006000000000000000000000000010088000000000000080005000000080006000300000300"
private val framePixels = "999999999999990333333099993399993399993999999399993999999399993399993399999999999999999999999999"
@Test fun roundedRectOutsideColorBridgesToTheTrailingByteAndPaintsLikeTheSoftwareRenderer() {
val call = read(hex(frame)).calls(IntArray(0), 0).single()
val plain = DrawProtocol.roundedRect(DrawValue.Integer(2), DrawValue.Integer(1), 8, 6, 3, 0, 3,
depth = 0, clip = DrawClip(2, 1, 8, 5))
assertContentEquals(plain + byteArrayOf(0), call)
val target = DisplayListRenderer.Target(ByteArray(48) { 0x99.toByte() }, 12, 8)
DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(call)), target)
assertEquals(framePixels, (0 until 96).joinToString("") { target.get(it % 12, it / 12).toString(16) })
// Without the trailing byte the corners keep what was there; a color past 15 is rejected.
val corners = DisplayListRenderer.Target(ByteArray(48) { 0x99.toByte() }, 12, 8)
DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(plain)), corners)
assertEquals(9, corners.get(2, 1)); assertEquals(3, corners.get(5, 1))
assertFails { DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(plain + byteArrayOf(16))), corners) }
assertFails { DrawProtocol.roundedRect(DrawValue.Integer(0), DrawValue.Integer(0), 8, 6, 3, 0, 3, outside = 16) }
}
@Test fun replayWithAnUnknownRecordDrawsNothing() {
val list = read(registeredReplay(imageKnown = false), 1100)
assertTrue(list.resources.isEmpty())
@@ -10,6 +10,7 @@ import kotlin.test.assertTrue
private const val RIGHT = "AA:00:00:00:00:01"
private const val LEFT = "AA:00:00:00:00:02"
private const val STOCK_VERSION = "2.3.0.24"
/** The test platform with a clock the tests can jump forward (the real waits keep real time). */
private class ClockPlatform(private val base: ProtocolPlatform) : ProtocolPlatform by base {
@@ -62,6 +63,14 @@ private class FakeLink(private val platform: ProtocolPlatform) : SessionLink {
val connected = HashSet<String>()
val disconnected = ArrayList<String>()
@Volatile var closed = false
/** An address whose connection attempts fail, like an arm that isn't advertising. */
@Volatile var unreachable: String? = null
/**
* The firmware-extension string settings READ acks report alongside [STOCK_VERSION] on both
* arms ("" is stock firmware, which omits the field), or null for an ack without firmware info.
*/
@Volatile var firmwareExtension: String? = null
@Volatile var connects = 0
var seq = 0
val writes: List<Write>
@@ -71,10 +80,14 @@ private class FakeLink(private val platform: ProtocolPlatform) : SessionLink {
fun cfwMessages(): List<ByteArray> = writes.filter { it.sid == CfwTransport.SID && it.kind == "image" }.mapNotNull { it.message }
override fun connect(address: String, timeoutMs: Int): Boolean {
connected.add(address)
connects++
if (address == unreachable) return false
lock.withLock { connected.add(address) }
return true
}
override fun isConnected(address: String): Boolean = lock.withLock { address in connected }
override fun requestHighPriority(address: String) {}
override fun requestMtu(address: String, mtu: Int, timeoutMs: Int): Boolean = true
@@ -100,23 +113,32 @@ private class FakeLink(private val platform: ProtocolPlatform) : SessionLink {
if (sid == CfwTransport.SID) {
for (lens in 1..2) core.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID, cfwAck(magic, lens, inFlight.message.size, inFlight.cfwChecksum))
} else {
core.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID, ackFrame(sid, magic, pb))
core.onNotification(address, BleProtocol.NOTIFY_CHAR_UUID, ackFrame(sid, magic, inFlight.kind))
}
}
return true
}
private fun ackFrame(sid: Int, magic: Int, request: ByteArray): ByteArray {
private fun ackFrame(sid: Int, magic: Int, kind: String?): ByteArray {
val extension = firmwareExtension
val body = if (sid == BleProtocol.SID_SECURITY_AUTH)
BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + byteArrayOf(0x1a, 0x00)
else if (kind == "battery" && extension != null)
BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, magic) +
BleProtocol.encodeBytesField(4,
BleProtocol.encodeBytesField(5, STOCK_VERSION.encodeToByteArray()) +
BleProtocol.encodeBytesField(6, STOCK_VERSION.encodeToByteArray())) +
(if (extension.isEmpty()) ByteArray(0) else BleProtocol.encodeBytesField(100, extension.encodeToByteArray()))
else
BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, magic)
return BleProtocol.framePb(body, sid, 0x00, seq++)[0]
}
override fun disconnect(address: String) {
disconnected.add(address)
connected.remove(address)
lock.withLock {
disconnected.add(address)
connected.remove(address)
}
}
override fun close() {
@@ -196,12 +218,15 @@ private class FakeHost(private val platform: ProtocolPlatform) : SessionHost {
private class FakeListener : FaceclawBleCommunicatorListener {
val phases = ArrayList<String>()
@Volatile var lastStatus = ""
val events = ArrayList<String>()
val finished = ArrayList<String>()
val batteries = ArrayList<String>()
val firmware = ArrayList<String>()
override fun onStateChange(phase: String?, status: String?) {
phases.add(phase ?: "")
lastStatus = status ?: ""
}
override fun onRingEvent(kind: String?, containerName: String?, eventType: Int, eventSource: Int, systemExitReasonCode: Int, frameId: Int, ringTick: Long, ringType: Int, ringAux: Int, ringSpeed: Int) {
@@ -226,7 +251,9 @@ private class FakeListener : FaceclawBleCommunicatorListener {
finished.add("$frameId:$outcome")
}
override fun onFirmwareInfo(leftVersion: String?, rightVersion: String?, extension: String?) {}
override fun onFirmwareInfo(leftVersion: String?, rightVersion: String?, extension: String?) {
firmware.add("$leftVersion|$rightVersion|$extension")
}
}
private class Session {
@@ -454,4 +481,214 @@ class GlassesSessionCoreTest {
assertContentEquals(first.message, second.message)
s.core.disconnect()
}
@Test
fun aCfwAckSlowerThanHalfASecondIsNotReplayed() {
val s = Session()
s.link.muted.add("image")
s.startAndAwaitLayout()
s.core.configureCompositorScreen(64, 32)
s.core.configureSurface("s", 0, 0, 64, 32, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
try {
s.submitFrame(3, 0x20, 1)
assertTrue(waitUntil(5_000) { s.link.cfwMessages().size == 1 })
// Well past the old 500 ms deadline, but inside the 3 s ack window.
s.platform.offsetMs += 1_500
sleepMs(400)
assertFalse(s.host.hasLog("CFW recovery"), s.host.logs().takeLast(10).toString())
assertEquals(1, s.link.cfwMessages().size)
// The late ack still completes the frame.
val first = s.link.writes.first { it.sid == CfwTransport.SID && it.kind == "image" }
val message = assertNotNull(s.core.monitor.withLock { s.core.inFlightMessages.firstOrNull { it.magic == first.magic } })
for (lens in 1..2) {
s.core.onNotification(LEFT, BleProtocol.NOTIFY_CHAR_UUID, cfwAck(first.magic, lens, message.message.size, message.cfwChecksum))
}
assertTrue(waitUntil(3_000) { s.displayedMatchesDesired() })
} finally {
s.core.close()
}
}
@Test
fun cfwAckDeadlinesFollowTheirOwnArmsWritesAndAcks() {
val core = Session().core
fun cfw(magic: Int, leftArm: Boolean) = OutboundMessage(
"image", "m$magic", CfwTransport.SID, 0, magic, byteArrayOf(1), CfwMessageWindow.ACK_STALL_MS, 0, leftArm)
val left = cfw(1, true)
val right = cfw(2, false)
core.monitor.withLock {
for (message in listOf(left, right)) {
message.sentAtMs = 1_000
message.ackDeadlineAtMs = 4_000
core.inFlightMessages.addLast(message)
}
core.lastArmCfwAckAtMs[0] = 2_000
core.lastArmWriteAtMs[1] = 5_000
core.refreshCfwAckDeadlinesLocked()
}
assertEquals(5_000L, left.ackDeadlineAtMs)
assertEquals(5_500L, right.ackDeadlineAtMs)
// Any CFW ack from an arm counts as that arm's progress, matched or not.
val before = core.now()
core.onNotification(LEFT, BleProtocol.NOTIFY_CHAR_UUID, cfwAck(77, 1, 1, 0))
assertTrue(core.monitor.withLock { core.lastArmCfwAckAtMs[0] } >= before)
assertEquals(0L, core.monitor.withLock { core.lastArmCfwAckAtMs[1] })
}
@Test
fun reconnectBackoffGrowsAndRestartsAfterAStableSession() {
val core = Session().core
core.monitor.withLock {
assertEquals(2_000L, core.scheduleReconnectLocked(false))
assertEquals(6_000L, core.scheduleReconnectLocked(false))
assertEquals(10_000L, core.scheduleReconnectLocked(false))
assertEquals(10_000L, core.scheduleReconnectLocked(false))
// A session that dies soon after coming up keeps backing off...
core.lastSessionReadyAtMs = core.now() - 1_000
assertEquals(10_000L, core.scheduleReconnectLocked(true))
// ...one that stayed up starts the schedule over.
core.lastSessionReadyAtMs = core.now() - ConnectionOptions.STABLE_SESSION_MS
assertEquals(2_000L, core.scheduleReconnectLocked(true))
assertEquals(6_000L, core.scheduleReconnectLocked(false))
}
}
@Test
fun unreachableArmIsNamedAndRetriedWithBackoff() {
val s = Session()
s.link.unreachable = LEFT
fun retryDelayMs() = s.core.monitor.withLock { s.core.reconnectAfterMs } - s.platform.elapsedRealtimeMs()
fun attempts() = s.core.monitor.withLock { s.core.consecutiveReconnects }
try {
assertTrue(s.core.start())
assertTrue(waitUntil(5_000) { attempts() == 1 }, s.host.logs().takeLast(10).toString())
assertTrue(s.host.hasLog("Transport failure: left arm not found"))
assertTrue(s.listener.lastStatus.startsWith("Can't reach the left arm"), s.listener.lastStatus)
assertTrue(retryDelayMs() in 1_000L..2_000L)
// Both arms are torn down, so the right arm doesn't sit connected on its own.
assertTrue(s.link.disconnected.containsAll(listOf(RIGHT, LEFT)))
s.platform.offsetMs += 2_000
assertTrue(waitUntil(5_000) { attempts() == 2 })
assertTrue(retryDelayMs() in 5_000L..6_000L)
s.platform.offsetMs += 6_000
assertTrue(waitUntil(5_000) { attempts() == 3 })
assertTrue(retryDelayMs() in 9_000L..10_000L)
// Once the arm answers again, the session comes up.
s.link.unreachable = null
s.platform.offsetMs += 10_000
assertTrue(waitUntil(5_000) { s.layoutCreated() }, s.host.logs().takeLast(10).toString())
} finally {
s.core.close()
}
}
@Test
fun stockFirmwareHaltsTheSessionBeforeAnyCustomFirmwareTraffic() {
val s = Session()
s.core.setRequiredFirmwareRevision(36)
s.link.firmwareExtension = ""
try {
assertTrue(s.core.start())
assertTrue(waitUntil(5_000) { s.listener.phases.contains("incompatible-firmware") }, s.host.logs().takeLast(10).toString())
assertEquals(listOf("$STOCK_VERSION|$STOCK_VERSION|"), s.listener.firmware)
assertTrue(s.host.hasLog("incompatible firmware"), s.host.logs().takeLast(10).toString())
// The settings reply precedes the layout, so stock never sees a private-stream message.
assertEquals(0, s.link.writes.count { it.kind == "create-layout" })
assertEquals(0, s.link.writes.count { it.sid == CfwTransport.SID })
assertTrue(s.link.disconnected.containsAll(listOf(RIGHT, LEFT)))
assertFalse(s.core.isSessionReady())
// Parked rather than retrying: no redial even well past the backoff schedule.
val connects = s.link.connects
s.platform.offsetMs += 60_000
s.core.interruptibleSleep.interrupt()
sleepMs(300)
assertEquals(connects, s.link.connects)
assertEquals("incompatible-firmware", s.listener.phases.last())
assertFalse(s.listener.phases.contains("retrying"))
} finally {
s.core.close()
}
}
@Test
fun anOlderFaceclawRevisionAlsoHalts() {
val s = Session()
s.core.setRequiredFirmwareRevision(36)
s.link.firmwareExtension = "Faceclaw/35"
try {
assertTrue(s.core.start())
assertTrue(waitUntil(5_000) { s.listener.phases.contains("incompatible-firmware") }, s.host.logs().takeLast(10).toString())
assertEquals(0, s.link.writes.count { it.kind == "create-layout" })
} finally {
s.core.close()
}
}
@Test
fun compatibleFirmwareIsCheckedAgainOnEverySession() {
val s = Session()
s.core.setRequiredFirmwareRevision(36)
s.link.firmwareExtension = "Faceclaw/36"
try {
s.startAndAwaitLayout()
assertTrue(s.core.monitor.withLock { s.core.customFirmwareDetected })
assertFalse(s.listener.phases.contains("incompatible-firmware"))
// The glasses come back on stock firmware (reflashed while the link was down):
// the new session's own settings query catches it before the layout.
s.link.firmwareExtension = ""
s.core.handleTransportFailure("test drop")
s.platform.offsetMs += 10_000
s.core.interruptibleSleep.interrupt()
assertTrue(waitUntil(5_000) { s.listener.phases.contains("incompatible-firmware") }, s.host.logs().takeLast(10).toString())
assertEquals(2, s.link.writes.count { it.kind == "battery" })
assertEquals(1, s.link.writes.count { it.kind == "enter-evenhub" })
assertEquals(0, s.link.writes.count { it.kind == "create-layout" })
} finally {
s.core.close()
}
}
@Test
fun customFirmwareEntersEvenHubOnEverySessionInsteadOfCreatingTheLayout() {
val s = Session()
s.core.setRequiredFirmwareRevision(37)
s.link.firmwareExtension = "Faceclaw/37"
try {
s.startAndAwaitLayout()
fun enters() = s.link.writes.filter { it.kind == "enter-evenhub" }
assertEquals(1, enters().size)
assertEquals(CfwTransport.SID, enters()[0].sid)
assertContentEquals(byteArrayOf(CFW_MSG_ENTER_EVENHUB.toByte()), enters()[0].message)
// The settings reply that identifies the firmware comes first.
assertTrue(s.link.writes.indexOfFirst { it.kind == "battery" } < s.link.writes.indexOf(enters()[0]))
// A reconnect may find the previous session's page still up. Mode 31
// ACKs in that state, so the session is entered the same way again.
s.core.handleTransportFailure("test drop")
assertTrue(waitUntil(5_000) { !s.layoutCreated() })
s.platform.offsetMs += 10_000
s.core.interruptibleSleep.interrupt()
assertTrue(waitUntil(5_000) { s.layoutCreated() && enters().size == 2 }, s.host.logs().takeLast(10).toString())
assertEquals(0, s.link.writes.count { it.kind == "create-layout" })
} finally {
s.core.close()
}
}
@Test
fun firmwareCompatibilityFollowsTheRevisionString() {
fun info(extension: String) = BleProtocol.FirmwareInfo(STOCK_VERSION, STOCK_VERSION, extension)
assertEquals(-1, info("").faceclawRevision())
assertEquals(-1, info("EVENCFW/22 img640").faceclawRevision())
assertEquals(-1, info("Faceclaw/abc").faceclawRevision())
assertEquals(-1, info("OtherCFW/36").faceclawRevision())
assertEquals(36, info(" Faceclaw/36 ").faceclawRevision())
assertFalse(info("").isCompatible(36))
assertFalse(info("Faceclaw/35").isCompatible(36))
assertTrue(info("Faceclaw/36").isCompatible(36))
assertTrue(info("Faceclaw/40").isCompatible(36))
assertTrue(info("Faceclaw/1").isCompatible(0))
assertFalse(info("EVENCFW/22").isCompatible(0))
}
}
@@ -43,7 +43,7 @@ class PreviewPackingTest {
compositor.configureSurface("app", 0, 0, 3, 2, 0, SurfaceCompositor.TRANSPARENCY_OPAQUE)
// One white 2x1 shell layer at (0,0), with no dimming or selection.
val header = listOf(1, 1, 0, 0, 2, 1, 256, 0, 0).flatMap { listOf(it.toByte(), (it shr 8).toByte()) }.toByteArray()
compositor.setShellScene(ArrayByteReader(header + byteArrayOf(-16, -16)))
compositor.setShellScene(ArrayByteReader(header + byteArrayOf(-16, -16) + DrawProtocol.word(0)))
val wire = compositor.applyAndComposite("app", ArrayByteReader(ByteArray(6) { 32 }), 0, 0, 3, 2, "first")
assertSame(wire.screenGray, wire.gray) // ShellScene.preview allocates its own pixels.
assertContentEquals(ByteArray(6) { 32 }, wire.screenGray)
@@ -212,6 +212,18 @@ class ProtocolTest {
assertTrue(CfwMessageWindow.canSend(listOf(eviction), upload))
}
@Test
fun cfwAckDeadlineWaitsWhileEitherWritesOrAcksProgress() {
// Sent at 1000 into an idle arm: 3 s from the send.
assertEquals(4_000L, CfwMessageWindow.ackDeadline(1_000, 1_000, 0))
// An ack from the arm restarts the 3 s ack window.
assertEquals(5_500L, CfwMessageWindow.ackDeadline(1_000, 1_000, 2_500))
// Writes still completing keep it waiting past the ack window, 500 ms at a time.
assertEquals(6_700L, CfwMessageWindow.ackDeadline(1_000, 6_200, 2_500))
// An ack from before the send doesn't shorten anything.
assertEquals(4_000L, CfwMessageWindow.ackDeadline(1_000, 1_000, 900))
}
@Test
fun orderedCompletionAndWholeWindowRecovery() {
val head = message()
@@ -97,6 +97,46 @@ class SharedGraphicsTest {
assertContentEquals(byteArrayOf(-56, -56, 0, -56, -56, 0), c.composite().gray)
}
@Test
fun surfaceIsCurrentOnlyAfterItsContentAndEveryChangeWasComposited() {
val c = SurfaceCompositor()
c.configureScreen(3, 2)
c.configureSurface("app", 0, 0, 3, 2, 0, 0)
assertFalse(c.isSurfaceCurrent("app", ""))
c.applyAndComposite("app", ArrayByteReader(ByteArray(6) { 200.toByte() }), 0, 0, 3, 2, "a")
assertTrue(c.isSurfaceCurrent("app", "a"))
assertFalse(c.isSurfaceCurrent("app", "b"))
assertFalse(c.isSurfaceCurrent("missing", "a"))
// A preview composite is not kept, so it does not apply pending changes.
c.setUnderlayDim(1, 128)
assertFalse(c.isSurfaceCurrent("app", "a"))
c.previewComposite()
assertFalse(c.isSurfaceCurrent("app", "a"))
c.composite()
assertTrue(c.isSurfaceCurrent("app", "a"))
for (change in listOf<() -> Unit>(
{ c.configureSurface("app", 1, 0, 3, 2, 0, 0) },
{ c.setSurfaceVisible("app", false) },
{ c.setSurfaceDepth("app", 2) },
{ c.setBlanked(false) },
{ c.removeSurface("other") },
)) {
change()
assertFalse(c.isSurfaceCurrent("app", "a"))
c.composite()
assertTrue(c.isSurfaceCurrent("app", "a"))
}
// Submitting without compositing retains the content but leaves it unapplied.
c.submitSurface("app", ArrayByteReader(ByteArray(6)), 0, 0, 3, 2, "b")
assertFalse(c.isSurfaceCurrent("app", "b"))
c.composite()
assertTrue(c.isSurfaceCurrent("app", "b"))
// A resize discards the retained content.
c.configureSurface("app", 0, 0, 2, 2, 0, 0)
c.composite()
assertFalse(c.isSurfaceCurrent("app", "b"))
}
@Test
fun pngChunksAndPixels() {
val png = SharedScreenshots.encode4BitGrayPng(byteArrayOf(0, 16, -1, 32, 48, 64), 3, 2)
@@ -0,0 +1,75 @@
package com.faceclaw.app
import kotlin.test.*
class VideoFrameScalerTest {
private val identity = ByteArray(256) { it.toByte() }
private fun scale(scaler: VideoFrameScaler, luma: ByteArray, lut: ByteArray = identity): IntArray {
val out = ByteArray(scaler.outputWidth * scaler.outputHeight)
scaler.scale(luma, 0, scaler.sourceWidth, 1, lut, out)
return IntArray(out.size) { out[it].toInt() and 0xff }
}
@Test fun twoToOneDownscaleAveragesEachBox() {
// 4x2 source -> 2x1: each output is the mean of a 2x2 box.
val luma = byteArrayOf(0, 100, 200.toByte(), 200.toByte(), 20, 80, 100, 100)
val scaler = VideoFrameScaler(4, 2, 0, 2, 1)
assertContentEquals(intArrayOf(50, 150), scale(scaler, luma))
}
@Test fun largeDownscaleStaysWithinTheTapBudget() {
// 100:1 horizontally still averages only MAX_TAPS samples, spread across the box.
val luma = ByteArray(200) { (if (it % 100 < 50) 0 else 200).toByte() }
val scaler = VideoFrameScaler(200, 1, 0, 2, 1)
assertContentEquals(intArrayOf(100, 100), scale(scaler, luma))
}
@Test fun upscaleIsNearestNeighbor() {
val scaler = VideoFrameScaler(2, 1, 0, 4, 2)
assertContentEquals(intArrayOf(10, 10, 90, 90, 10, 10, 90, 90), scale(scaler, byteArrayOf(10, 90)))
}
@Test fun offsetAndStridesSelectTheVisiblePlane() {
// A 2x2 visible crop at (1, 1) of a 4-wide plane, with interleaved samples (pixelStride 2).
val plane = ByteArray(40)
fun put(x: Int, y: Int, v: Int) { plane[(1 + y) * 10 + 1 + x * 2] = v.toByte() }
put(0, 0, 11); put(1, 0, 22); put(0, 1, 33); put(1, 1, 44)
val scaler = VideoFrameScaler(2, 2, 0, 2, 2)
val out = ByteArray(4)
scaler.scale(plane, 11, 10, 2, identity, out)
assertContentEquals(byteArrayOf(11, 22, 33, 44), out)
}
@Test fun rotationsMatchTheContainerClockwiseConvention() {
// Coded 3x2: 1 2 3
// 4 5 6
val luma = byteArrayOf(1, 2, 3, 4, 5, 6)
assertContentEquals(intArrayOf(4, 1, 5, 2, 6, 3), scale(VideoFrameScaler(3, 2, 90, 2, 3), luma))
assertContentEquals(intArrayOf(6, 5, 4, 3, 2, 1), scale(VideoFrameScaler(3, 2, 180, 3, 2), luma))
assertContentEquals(intArrayOf(3, 6, 2, 5, 1, 4), scale(VideoFrameScaler(3, 2, 270, 2, 3), luma))
}
@Test fun normalizeRotationSnapsToQuarterTurns() {
assertEquals(0, VideoFrameScaler.normalizeRotation(0))
assertEquals(90, VideoFrameScaler.normalizeRotation(90))
assertEquals(270, VideoFrameScaler.normalizeRotation(-90))
assertEquals(180, VideoFrameScaler.normalizeRotation(540))
assertEquals(0, VideoFrameScaler.normalizeRotation(359))
}
@Test fun limitedRangeStretchesAndMonochromeThresholds() {
val gray = VideoFrameScaler.toneLut(limitedRange = true, gamma = 1f, monochrome = false)
assertEquals(0, gray[16].toInt() and 0xff)
assertEquals(0, gray[0].toInt() and 0xff)
assertEquals(255, gray[235].toInt() and 0xff)
assertEquals(255, gray[255].toInt() and 0xff)
val mono = VideoFrameScaler.toneLut(limitedRange = false, gamma = 2.2f, monochrome = true)
assertEquals(0, mono[127].toInt() and 0xff)
assertEquals(255, mono[128].toInt() and 0xff)
// Gamma darkens midtones but leaves the ends alone.
val photo = VideoFrameScaler.toneLut(limitedRange = false, gamma = 2.2f, monochrome = false)
assertTrue((photo[128].toInt() and 0xff) < 64)
assertEquals(255, photo[255].toInt() and 0xff)
}
}