Files
plezy/test/focus/focus_navigation_intent_test.dart
T
edde746 e3703892b3 fix(player): keep a keyboard Enter out of focus navigation
Pressing Enter over the player put the whole app into keyboard mode and
dropped focus onto Play/Pause, even with Video Player Navigation off. Two
independent paths did it. InputModeTracker promoted on any key satisfying
isNavigationKey, a set that unioned activation, dismissal and the menu key
with the arrows and consulted no setting at all; separately the surface's
Select handler always asked the chrome for focus. Escape had the same effect,
which on desktop reads as the mouse cursor vanishing mid-playback.

Both now ask one predicate. eventRequestsFocusNavigation decides whether the
app switches to keyboard mode and whether a key may hand focus to the chrome,
so the two cannot disagree and focus can never land on a control while focus
chrome is still suppressed. Activation and dismissal act on what already has
focus, so they answer no; Tab, the menu key, a remote's OK or BACK, and an
arrow that will really traverse answer yes. The one input the predicate cannot
read off the event, whether the focused feature owns arrow keys, rides on the
node as DirectionalShortcutFocusNode instead of on a subtree, so every sheet,
prompt and OSD button stays an ordinary traversal target with nothing to
re-enable.

playerDirectionalNavigationEnabled and videoPlayerNavigationPreference replace
five hand-copied pref-or-isTV expressions and a screen-level cache that
disagreed with the live getter after a toggle. Services whose input is
synthesized past HardwareKeyboard announce themselves through
InputModeTracker.reportNonPointerInput rather than two static callbacks and
three copies of a highlight-strategy write. That registration is now
identity-guarded: the bootstrap-to-app tree swap disposed the outgoing tracker
after the incoming one initialised and cleared both callbacks, so gamepad and
companion remote input had stopped switching to keyboard mode entirely.

Falling out of the same rule: a companion heartbeat no longer flips an idle
desktop host into keyboard mode, analog-stick drift promotes only past the
deadzone that actually navigates, Enter keeps toggling playback once the
chrome is up, Tab both reaches and traverses the OSD, and the player surface
claims the remote from mount rather than only when the chrome starts hidden,
so the first key on a desktop route is a playback shortcut instead of the
screen node's chrome-raising self-heal.

isNavigationKey becomes isReservedControlKey, since its real meaning is a
shell key rather than a text character and the old name is what invited the
conflation. The unreachable PlayerChromeFocusTarget.timeline goes with it.
2026-08-07 13:23:53 +02:00

150 lines
6.0 KiB
Dart

import 'dart:ui' show KeyEventDeviceType;
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart' show FocusNode;
import 'package:plezy/focus/dpad_navigator.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:plezy/focus/focus_navigation_intent.dart';
import 'package:plezy/utils/platform_detector.dart';
/// Executable spec for the one predicate that decides both whether the app
/// switches to keyboard mode and whether a key may hand focus to the player
/// chrome. Every row is a key the app can actually receive; the table is the
/// contract, so widening it is a deliberate edit rather than an accident.
///
/// `WidgetTester.sendKeyEvent` always reports `deviceType == keyboard`, so rows
/// that vary the device construct the event directly.
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
KeyDownEvent down(LogicalKeyboardKey key, {KeyEventDeviceType deviceType = KeyEventDeviceType.keyboard}) {
return KeyDownEvent(
logicalKey: key,
physicalKey: PhysicalKeyboardKey.f13,
timeStamp: Duration.zero,
deviceType: deviceType,
);
}
tearDown(() => TvDetectionService.debugSetAppleTVOverride(null));
group('off TV', () {
setUp(() => TvDetectionService.debugSetAppleTVOverride(false));
final promotes = <String, KeyEvent>{
'tab': down(LogicalKeyboardKey.tab),
'contextMenu': down(LogicalKeyboardKey.contextMenu),
'gameButtonX': down(LogicalKeyboardKey.gameButtonX),
'select': down(LogicalKeyboardKey.select),
'select reported as a keyboard device (tvOS engine shape)': down(
LogicalKeyboardKey.select,
deviceType: KeyEventDeviceType.keyboard,
),
'gameButtonA': down(LogicalKeyboardKey.gameButtonA),
'gameButtonB': down(LogicalKeyboardKey.gameButtonB),
'goBack': down(LogicalKeyboardKey.goBack),
'enter from a gamepad': down(LogicalKeyboardKey.enter, deviceType: KeyEventDeviceType.gamepad),
'escape from a d-pad': down(LogicalKeyboardKey.escape, deviceType: KeyEventDeviceType.directionalPad),
};
final ignores = <String, KeyEvent>{
'enter from a physical keyboard': down(LogicalKeyboardKey.enter),
'numpadEnter from a physical keyboard': down(LogicalKeyboardKey.numpadEnter),
'escape from a physical keyboard': down(LogicalKeyboardKey.escape),
'browserBack (media keyboards emit it)': down(LogicalKeyboardKey.browserBack),
'space': down(LogicalKeyboardKey.space),
'a letter': down(LogicalKeyboardKey.keyF),
'mediaPlayPause': down(LogicalKeyboardKey.mediaPlayPause),
};
promotes.forEach((name, event) {
test('$name requests focus navigation', () {
expect(eventRequestsFocusNavigation(event), isTrue);
});
});
ignores.forEach((name, event) {
test('$name does not request focus navigation', () {
expect(eventRequestsFocusNavigation(event), isFalse);
});
});
test('only key-down counts', () {
const up = KeyUpEvent(
logicalKey: LogicalKeyboardKey.tab,
physicalKey: PhysicalKeyboardKey.tab,
timeStamp: Duration.zero,
);
const repeat = KeyRepeatEvent(
logicalKey: LogicalKeyboardKey.arrowDown,
physicalKey: PhysicalKeyboardKey.arrowDown,
timeStamp: Duration.zero,
);
expect(eventRequestsFocusNavigation(up), isFalse);
expect(eventRequestsFocusNavigation(repeat), isFalse);
});
});
group('on TV', () {
setUp(() => TvDetectionService.debugSetAppleTVOverride(true));
test('a remote OK that arrives as a keyboard enter still requests navigation', () {
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.enter)), isTrue);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.numpadEnter)), isTrue);
});
test('escape from a physical keyboard still only dismisses', () {
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.escape)), isFalse);
});
});
group('arrows ask the focused node', () {
setUp(() => TvDetectionService.debugSetAppleTVOverride(false));
test('an ordinary node means arrows traverse', () {
final node = FocusNode(debugLabel: 'plain');
addTearDown(node.dispose);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.arrowDown), focused: node), isTrue);
});
test('a node that owns arrows suppresses promotion', () {
final node = DirectionalShortcutFocusNode(debugLabel: 'surface', consumesDirectionalKeys: (_) => true);
addTearDown(node.dispose);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.arrowDown), focused: node), isFalse);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.arrowRight), focused: node), isFalse);
});
test('a node can own one axis and leave the other as traversal', () {
final node = DirectionalShortcutFocusNode(
debugLabel: 'surface',
consumesDirectionalKeys: (key) => key.isLeftKey || key.isRightKey,
);
addTearDown(node.dispose);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.arrowLeft), focused: node), isFalse);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.arrowUp), focused: node), isTrue);
});
test('a node that has stopped owning arrows lets them traverse again', () {
var owns = true;
final node = DirectionalShortcutFocusNode(debugLabel: 'surface', consumesDirectionalKeys: (_) => owns);
addTearDown(node.dispose);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.arrowDown), focused: node), isFalse);
owns = false;
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.arrowDown), focused: node), isTrue);
});
test('select is unaffected by who owns arrows', () {
final node = DirectionalShortcutFocusNode(debugLabel: 'surface', consumesDirectionalKeys: (_) => true);
addTearDown(node.dispose);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.select), focused: node), isTrue);
expect(eventRequestsFocusNavigation(down(LogicalKeyboardKey.enter), focused: node), isFalse);
});
});
}