Skip to content

Debloat your Android TV, install apps, browse and transfer files on it, all from your phone. Just 3 MB, no tracking, no root, no PC.

Remote repeat: time the repeats on a clock the tests can set

jesse-agent-23292715cd93409
diff --git a/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt b/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt
index 2d2771b..44b15fd 100644
--- a/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt
+++ b/app/src/main/java/app/tellyscope/ui/RemoteScreen.kt
@@ -173,13 +173,18 @@ internal const val REPEAT_EVERY_MS = 50L
* One press, then repeats while [held] for keys that repeat. Stops at the first key the TV didn't
* take (refused, dropped, or no TV selected).
*/
-internal suspend fun repeatWhileHeld(key: RemoteKey, held: () -> Boolean, press: suspend (RemoteKey) -> Boolean) {
+internal suspend fun repeatWhileHeld(
+ key: RemoteKey,
+ held: () -> Boolean,
+ nowMs: () -> Long = { System.nanoTime() / 1_000_000 },
+ press: suspend (RemoteKey) -> Boolean,
+) {
if (!press(key) || !key.repeats) return
delay(REPEAT_AFTER_MS)
- var last = System.nanoTime()
+ var last = nowMs()
while (held() && press(key)) {
- delay((REPEAT_EVERY_MS - (System.nanoTime() - last) / 1_000_000).coerceAtLeast(0))
- last = System.nanoTime()
+ delay((REPEAT_EVERY_MS - (nowMs() - last)).coerceAtLeast(0))
+ last = nowMs()
}
}
diff --git a/app/src/test/java/app/tellyscope/RemoteRepeatTest.kt b/app/src/test/java/app/tellyscope/RemoteRepeatTest.kt
index 477e767..2ebfb31 100644
--- a/app/src/test/java/app/tellyscope/RemoteRepeatTest.kt
+++ b/app/src/test/java/app/tellyscope/RemoteRepeatTest.kt
@@ -33,7 +33,9 @@ class RemoteRepeatTest {
@Test fun aKeyTheTvTakesAtOnceRepeatsAtAndroidsRate() = runTest {
var presses = 0
// Held for one second after the first repeat: the TV answering at once must not skip a list.
- repeatWhileHeld(RemoteKey.DOWN, held = { testScheduler.currentTime < 400 + 1000 }) { presses++; true }
+ // The test clock, not the real one: a stall on the machine running the test must not add a press.
+ val clock = { testScheduler.currentTime }
+ repeatWhileHeld(RemoteKey.DOWN, held = { clock() < 400 + 1000 }, nowMs = clock) { presses++; true }
assertEquals(1 + 1000 / REPEAT_EVERY_MS.toInt(), presses)
}
@@ -45,7 +47,8 @@ class RemoteRepeatTest {
@Test fun heldBackspaceRepeatsUntilReleased() = runTest {
var presses = 0
- repeatWhileHeld(RemoteKey.DEL, held = { testScheduler.currentTime < 400 + 500 }) { presses++; true }
+ val clock = { testScheduler.currentTime }
+ repeatWhileHeld(RemoteKey.DEL, held = { clock() < 400 + 500 }, nowMs = clock) { presses++; true }
assertEquals(1 + 500 / REPEAT_EVERY_MS.toInt(), presses)
}