Merge: restore composer focus when the window returns

This commit is contained in:
2026-10-31 22:16:43 +00:00
4 changed files with 487 additions and 0 deletions
@@ -0,0 +1,198 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/composer_focus_restore.dart';
import 'chat_composer_bar_fixtures.dart';
final _desktop = TargetPlatformVariant.only(TargetPlatform.macOS);
final _phone = TargetPlatformVariant.only(TargetPlatform.android);
final _field = find.byKey(const Key('chat-composer-field'));
FocusNode _composerFocus(WidgetTester tester) => tester
.widget<EditableText>(
find.descendant(of: _field, matching: find.byType(EditableText)),
)
.focusNode;
TextEditingController _controller(WidgetTester tester) => tester
.widget<EditableText>(
find.descendant(of: _field, matching: find.byType(EditableText)),
)
.controller;
/// The window loses focus (web: window `blur`) and comes back (`focus`).
Future<void> _switchAway(
WidgetTester tester, {
Future<void> Function()? whileAway,
bool viaHidden = false,
}) async {
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
if (viaHidden) {
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
}
await tester.pump();
if (whileAway != null) await whileAway();
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
await tester.pump();
}
Future<ComposerBarGateway> _open(WidgetTester tester) async {
final gateway = ComposerBarGateway()..page = fixtureTurn();
addTearDown(gateway.close);
await tester.pumpWidget(composerThread(gateway));
await tester.pumpAndSettle();
return gateway;
}
Future<void> _typeWithCaret(WidgetTester tester) async {
await tester.showKeyboard(_field);
await tester.enterText(_field, 'hello world');
_controller(tester).selection = const TextSelection(
baseOffset: 2,
extentOffset: 5,
);
await tester.pump();
}
void main() {
testWidgets(
'focus and selection return to the composer when the window comes back',
(tester) async {
await _open(tester);
await _typeWithCaret(tester);
// The web engine closes the text connection on the editing element's
// blur, which the browser fires just before the window's blur; the
// framework has unfocused the field by the time `inactive` arrives, so
// FocusManager's own suspend/resume cannot bring it back.
_composerFocus(tester).unfocus();
await tester.pump();
expect(_composerFocus(tester).hasFocus, isFalse);
await _switchAway(tester);
expect(_composerFocus(tester).hasPrimaryFocus, isTrue);
expect(
_controller(tester).selection,
const TextSelection(baseOffset: 2, extentOffset: 5),
);
},
variant: _desktop,
);
testWidgets(
'switching tabs (inactive, hidden, inactive, resumed) restores too',
(tester) async {
await _open(tester);
await _typeWithCaret(tester);
_composerFocus(tester).unfocus();
await tester.pump();
await _switchAway(tester, viaHidden: true);
expect(_composerFocus(tester).hasPrimaryFocus, isTrue);
},
variant: _desktop,
);
testWidgets('nothing is restored when the composer was not focused', (
tester,
) async {
await _open(tester);
expect(_composerFocus(tester).hasFocus, isFalse);
await _switchAway(tester);
expect(_composerFocus(tester).hasFocus, isFalse);
}, variant: _desktop);
testWidgets('nothing is restored when another control holds focus', (
tester,
) async {
await _open(tester);
await _typeWithCaret(tester);
final other = FocusNode(debugLabel: 'other');
addTearDown(other.dispose);
await _switchAway(
tester,
whileAway: () async {
// A focusable outside the field takes focus while away.
other
.attach(tester.element(_field))
.reparent(parent: FocusManager.instance.rootScope);
other.requestFocus();
await tester.pump();
},
);
expect(other.hasPrimaryFocus, isTrue);
expect(_composerFocus(tester).hasFocus, isFalse);
}, variant: _desktop);
testWidgets('nothing is restored while a dialog covers the chat', (
tester,
) async {
await _open(tester);
await _typeWithCaret(tester);
await _switchAway(
tester,
whileAway: () async {
showDialog<void>(
context: tester.element(_field),
builder: (_) => const AlertDialog(content: Text('dialog')),
);
await tester.pumpAndSettle();
FocusManager.instance.primaryFocus?.unfocus();
await tester.pump();
},
);
expect(find.text('dialog'), findsOneWidget);
expect(_composerFocus(tester).hasFocus, isFalse);
}, variant: _desktop);
testWidgets('phones do not refocus (no unasked on-screen keyboard)', (
tester,
) async {
await _open(tester);
await _typeWithCaret(tester);
await _switchAway(
tester,
whileAway: () async {
_composerFocus(tester).unfocus();
await tester.pump();
},
);
expect(_composerFocus(tester).hasFocus, isFalse);
}, variant: _phone);
testWidgets('a blur well before the window left is the operator\'s own', (
tester,
) async {
var now = DateTime(2026);
final focus = FocusNode();
final restorer = ComposerFocusRestorer(
focus: focus,
canRestore: () => true,
enabled: true,
now: () => now,
);
addTearDown(() {
restorer.dispose();
focus.dispose();
});
await tester.pumpWidget(
MaterialApp(
home: Material(child: TextField(focusNode: focus)),
),
);
focus.requestFocus();
await tester.pump();
focus.unfocus();
await tester.pump();
now = now.add(composerBlurGrace * 2);
await _switchAway(tester);
expect(focus.hasFocus, isFalse);
focus.requestFocus();
await tester.pump();
focus.unfocus();
await tester.pump();
now = now.add(composerBlurGrace ~/ 2);
await _switchAway(tester);
expect(focus.hasPrimaryFocus, isTrue);
});
}