Send chat messages with Enter and use Shift+Enter for newlines
This commit is contained in:
@@ -91,7 +91,7 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('Enter inserts a newline; Ctrl+Enter sends', (tester) async {
|
||||
testWidgets('Shift+Enter inserts a newline; Enter sends', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
composerThread(gateway, newOperationId: nextOperation),
|
||||
);
|
||||
@@ -101,23 +101,15 @@ void main() {
|
||||
await tester.enterText(field, 'line one');
|
||||
await tester.pump();
|
||||
|
||||
// A hardware Enter is left to the IME and never submits; the engine then
|
||||
// inserts the newline as an editing value.
|
||||
// Shift+Enter inserts a newline without triggering the keyboard Send action.
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
|
||||
await tester.pump();
|
||||
expect(gateway.promptBodies, isEmpty);
|
||||
tester.testTextInput.updateEditingValue(
|
||||
const TextEditingValue(
|
||||
text: 'line one\n',
|
||||
selection: TextSelection.collapsed(offset: 9),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
expect(fieldText(tester), 'line one\n');
|
||||
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.promptBodies.single['text'], 'line one');
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
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';
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
for (final shortcut in ['enter', 'numpad', 'control', 'meta', 'software']) {
|
||||
testWidgets(
|
||||
'$shortcut sends on ${cupertino ? 'Cupertino' : 'Material'}',
|
||||
(tester) async {
|
||||
final gateway = ComposerBarGateway()..page = fixtureTurn();
|
||||
addTearDown(gateway.close);
|
||||
await tester.pumpWidget(
|
||||
composerThread(gateway, cupertino: cupertino),
|
||||
);
|
||||
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<EditableText>(
|
||||
find.descendant(of: field, matching: find.byType(EditableText)),
|
||||
);
|
||||
expect(editor.focusNode.hasFocus, isTrue);
|
||||
expect(editor.textInputAction, TextInputAction.send);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets('Shift+Enter replaces only selected text ($cupertino)', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = ComposerBarGateway()..page = fixtureTurn();
|
||||
addTearDown(gateway.close);
|
||||
await tester.pumpWidget(composerThread(gateway, cupertino: cupertino));
|
||||
await tester.pumpAndSettle();
|
||||
final field = find.byKey(const Key('chat-composer-field'));
|
||||
await tester.enterText(field, 'abcd');
|
||||
await tester.pump();
|
||||
final editor = tester.widget<EditableText>(
|
||||
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);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'Enter confirms IME composition without submitting ($cupertino)',
|
||||
(tester) async {
|
||||
final gateway = ComposerBarGateway()..page = fixtureTurn();
|
||||
addTearDown(gateway.close);
|
||||
await tester.pumpWidget(composerThread(gateway, cupertino: cupertino));
|
||||
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'], '仮');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'empty Enter, repeat and duplicate software submit send nothing extra',
|
||||
(tester) async {
|
||||
final gateway = ComposerBarGateway()..page = fixtureTurn();
|
||||
final reply = Completer<Map<String, dynamic>>();
|
||||
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();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user