A phone app is driven by taps, and a tap says where it landed. A TV app is driven by a D-pad: up, down, left, right, OK and Back. Where a press goes depends on which element has focus, and the app decides that. So I split the checking of Yonto Player's TV app into three layers, each kept to the questions it can answer.
What a unit test can see
Most of the app is not about focus at all. Adapters, repositories, view models and settings persistence have unit tests, and the rule in the repo is that a new adapter, a repository method or a view model behaviour comes with one. The core tests run on the plain JVM against fakes and a local mock web server, and the test task points the JVM's proxies at one that refuses everything except loopback, which blocks the usual ways a unit test could reach the internet.
Logic that sits behind a screen, such as a form's validation or what gets saved, goes in plain Kotlin next to it, so it can be tested without drawing anything.
What Robolectric adds
For the screens themselves I use Robolectric with the Compose test rule. It renders a real screen on the JVM, so a test can assert that a node is there or a list shows the right cards.
Two things make those tests honest about a television. First, Robolectric is not a TV by default. Compose's default for how far a list scrolls to bring the focused item into view picks its TV behaviour only when the device declares the FEATURE_LEANBACK system feature, so a test class that renders a screen declares it:
// excerpt of the pattern, in a test's setUp
shadowOf(composeTestRule.activity.packageManager)
.setSystemFeature(PackageManager.FEATURE_LEANBACK, true)
Without that line, a test that checks positions describes a different platform.
Second, key events can be sent straight to the activity, so a test can press OK without a device. A press is a down, some repeats and an up, and the test builds those itself. A simplified sketch:
// Simplified sketch of how a screen test presses OK.
private fun ok(downTime: Long, eventTime: Long, action: Int, repeat: Int = 0) {
rule.runOnUiThread {
rule.activity.dispatchKeyEvent(
KeyEvent(downTime, eventTime, action, KeyEvent.KEYCODE_DPAD_CENTER, repeat)
)
}
rule.waitForIdle()
}
// a held press: down, two repeats, up
ok(1_000, 1_000, KeyEvent.ACTION_DOWN)
ok(1_000, 1_500, KeyEvent.ACTION_DOWN, repeat = 1)
ok(1_000, 1_550, KeyEvent.ACTION_DOWN, repeat = 2)
ok(1_000, 1_600, KeyEvent.ACTION_UP)
That is enough for a test to check that a held OK on the player's transport bar selects its control once.
What needs a device
Robolectric draws a screen, but not the way a television does. Text widths are not real under Robolectric, and there is no real window or input pipeline. So these questions go to an emulator or a TV:
- Focus order. Where the remote lands when a screen opens, which control Down reaches from a row of cards, and whether Back returns to the card you left.
- Long press. A card's one secondary action is a long press, and nothing draws a long press, so a screen that has one says so in a hint line. Long presses and focus are checked on a device.
- Scrolling. A scrolling column only moves as far as focus can go, so whether a field and the line under it stay in view is a thing to look at.
- The player's controls. Which keys the bar takes, which go to the picture, and what Back does when the bar is up.
To send keys to an emulator, adb shell input keyevent KEYCODE_DPAD_DOWN is enough for a press. A held OK is input keyevent --duration 3000 DPAD_CENTER, which sends the down, one long-press repeat about 400 ms later and the up. A held D-pad is different: a remote repeats a held arrow about every 50 ms, which input keyevent does not do. For that the repo has a small helper, hold-key, that injects the events itself through a short class run on the device as the shell user. It sends a down, a repeat every interval after the first 400 ms, then an up, and a shorter interval as a third argument stands in for a slow machine that cannot draw a frame between repeats. It is how a held arrow through a long list can be tried on an emulator.
Saying where focus starts and returns
Everything above leans on one design rule: each screen says where focus starts. A screen requests focus on its primary element, and there are no on-screen Back buttons, so the first control is the one that matters. A simplified sketch of the shape:
// Simplified sketch: a screen puts focus on its primary control.
@Composable
fun TitleScreen(onPlay: () -> Unit) {
val playFocus = remember { FocusRequester() }
LaunchedEffect(Unit) { playFocus.requestFocusWhenAttached() }
Button(onClick = onPlay, modifier = Modifier.focusRequester(playFocus)) {
Text("Play")
}
}
requestFocusWhenAttached is a small helper that asks again, frame after frame for up to 30 frames, until the answer is yes, because a control inside a lazy list may not be attached on the first frame. A one-shot decision such as initial focus lives at the screen level, since an effect inside a lazy item runs again every time that item scrolls back into view.
The return path matters as much as the start. When a dialog closes, or Back comes back from a screen, the remote has to go to somewhere real: the card you left, the button that opened a list, the row that opened a page. A screen names the control it returns to. That also makes the screens testable: "where does focus start" and "where does it come back to" have a definite answer that a device run can check.
A smoke test that is a script
"Does it start, show Home, find a title and play it" is the same question each time, so it is a script instead of someone's walk through the app. It drives an emulator through adb and runs in a minute or two. For the TV it opens Home, presses OK on the hero's Play, waits for playback and presses Back to return to Home.
Each step waits for what the screen says, not for a fixed time. A simplified sketch of the TV flow:
# Simplified sketch of a scripted smoke flow for a TV.
def tv_flow(d):
d.wait_for("Play", 60) # Home is up
d.wait_for("Browse")
d.key("DPAD_CENTER") # Home opens with focus on the hero's Play
d.wait_for_playback(audio_only=True) # the TV publishes no media session, so audio must keep running
d.shot("playing")
d.key("BACK")
d.wait_for("Browse") # back on Home
d.shot("back home")
It clears the app's data first, so it starts as a first launch, and it takes a screenshot per step, plus one named failure when a step fails. The exit code is 0 or 1 with one line saying what it was waiting for, so a person or a script can read the result without opening an image. Playback is checked through the system: the phone's media session position has to move, and on the TV the audio track has to keep running.
It is a check that the basics hold, and not a review. A pass on the television shows that audio runs, not that the picture is right, so the screenshot of the step is what tells you how it looks. What a script cannot judge, how a screen looks or what is confusing, stays with a person, or an agent walking the app, and anything that walk finds on a path the smoke covers becomes a new step.
The split
State, view models, validation and key handling you can express as events go to a unit or Robolectric test. Focus order, long press, scrolling and the player's controls go to an emulator or a TV. "Does it still start and play" goes to the smoke script, run on a build first.
If you build for Android TV, the part I would take from this is the focus rule. Give every screen a primary element that takes focus when it opens, and a named place where focus returns. Then both the unit tests and the device runs have something definite to assert.
Read more
The developer docs on testing and debugging cover running plugins on a laptop against the same engine as the TV, and the developer docs describe the plugin contract. The documentation covers what the app does, and the plugin SDK and command-line tool are in the plugins repository.
Top comments (1)
the FEATURE_LEANBACK shadow is the line most ppl miss, without it every bring-into-view assert is basically a phone test in a tv costume. the 30 frame requestFocus retry makes sense for lazy items tho, id just log when it gives up, because a silent give-up on a slow box looks exactly like a focus bug