import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'chat_composer_bar_fixtures.dart'; /// These tests drive a desktop keyboard, where Enter sends; on a phone the /// on-screen keyboard's Return breaks the line instead. final _desktop = TargetPlatformVariant.only(TargetPlatform.linux); void main() { for (final shortcut in ['enter', 'numpad', 'control', 'meta', 'software']) { testWidgets('$shortcut sends on Material', (tester) async { final gateway = ComposerBarGateway()..page = fixtureTurn(); addTearDown(gateway.close); await tester.pumpWidget(composerThread(gateway)); await tester.pumpAndSettle(); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, ' message '); await tester.pump(); final modifier = switch (shortcut) { 'control' => LogicalKeyboardKey.controlLeft, 'meta' => LogicalKeyboardKey.metaLeft, _ => null, }; if (modifier != null) await tester.sendKeyDownEvent(modifier); if (shortcut == 'software') { await tester.testTextInput.receiveAction(TextInputAction.send); } else { await tester.sendKeyEvent( shortcut == 'numpad' ? LogicalKeyboardKey.numpadEnter : LogicalKeyboardKey.enter, ); } if (modifier != null) await tester.sendKeyUpEvent(modifier); await tester.pumpAndSettle(); expect(gateway.promptBodies.single['text'], 'message'); expect(fieldText(tester), isEmpty); final editor = tester.widget( find.descendant(of: field, matching: find.byType(EditableText)), ); expect(editor.focusNode.hasFocus, isTrue); expect(editor.textInputAction, TextInputAction.send); }, variant: _desktop); } testWidgets('Shift+Enter replaces only selected text (Material)', ( tester, ) async { final gateway = ComposerBarGateway()..page = fixtureTurn(); addTearDown(gateway.close); await tester.pumpWidget(composerThread(gateway)); await tester.pumpAndSettle(); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, 'abcd'); await tester.pump(); final editor = tester.widget( find.descendant(of: field, matching: find.byType(EditableText)), ); editor.controller.selection = const TextSelection( baseOffset: 1, extentOffset: 3, ); await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); await tester.pump(); expect(fieldText(tester), 'a\nd'); expect( editor.controller.selection, const TextSelection.collapsed(offset: 2), ); expect(gateway.promptBodies, isEmpty); }, variant: _desktop); testWidgets( 'Enter confirms IME composition without submitting (Material)', (tester) async { final gateway = ComposerBarGateway()..page = fixtureTurn(); addTearDown(gateway.close); await tester.pumpWidget(composerThread(gateway)); await tester.pumpAndSettle(); await tester.showKeyboard(find.byKey(const Key('chat-composer-field'))); tester.testTextInput.updateEditingValue( const TextEditingValue( text: '仮', selection: TextSelection.collapsed(offset: 1), composing: TextRange(start: 0, end: 1), ), ); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pump(); expect(gateway.promptBodies, isEmpty); tester.testTextInput.updateEditingValue( const TextEditingValue( text: '仮', selection: TextSelection.collapsed(offset: 1), ), ); await tester.pump(); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pumpAndSettle(); expect(gateway.promptBodies.single['text'], '仮'); }, variant: _desktop, ); testWidgets( 'empty Enter, repeat and duplicate software submit send nothing extra', (tester) async { final gateway = ComposerBarGateway()..page = fixtureTurn(); final reply = Completer>(); gateway.prompt = (_, _) => reply.future; addTearDown(gateway.close); await tester.pumpWidget(composerThread(gateway)); await tester.pumpAndSettle(); final field = find.byKey(const Key('chat-composer-field')); await tester.enterText(field, ' '); await tester.sendKeyEvent(LogicalKeyboardKey.enter); await tester.pump(); expect(gateway.promptBodies, isEmpty); await tester.enterText(field, 'one message'); await tester.pump(); await tester.sendKeyDownEvent(LogicalKeyboardKey.enter); await tester.pump(); await tester.sendKeyRepeatEvent(LogicalKeyboardKey.enter); await tester.testTextInput.receiveAction(TextInputAction.send); await tester.sendKeyUpEvent(LogicalKeyboardKey.enter); await tester.pump(); expect(gateway.promptBodies, hasLength(1)); reply.complete({}); await tester.pumpAndSettle(); }, variant: _desktop, ); }