142 lines
5.3 KiB
Dart
142 lines
5.3 KiB
Dart
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();
|
|
},
|
|
);
|
|
}
|