ime.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import 'package:appflowy_editor/appflowy_editor.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_test/flutter_test.dart';
  5. extension IME on WidgetTester {
  6. IMESimulator get ime => IMESimulator(this);
  7. }
  8. class IMESimulator {
  9. IMESimulator(this.tester) {
  10. client = findTextInputClient();
  11. }
  12. final WidgetTester tester;
  13. late final TextInputClient client;
  14. Future<void> insertText(String text) async {
  15. for (final c in text.characters) {
  16. await insertCharacter(c);
  17. }
  18. }
  19. Future<void> insertCharacter(String character) async {
  20. final value = client.currentTextEditingValue;
  21. if (value == null) {
  22. assert(false);
  23. return;
  24. }
  25. final text = value.text
  26. .replaceRange(value.selection.start, value.selection.end, character);
  27. final textEditingValue = TextEditingValue(
  28. text: text,
  29. selection: TextSelection.collapsed(
  30. offset: value.selection.baseOffset + 1,
  31. ),
  32. composing: TextRange.empty,
  33. );
  34. client.updateEditingValue(textEditingValue);
  35. await tester.pumpAndSettle();
  36. }
  37. TextInputClient findTextInputClient() {
  38. final finder = find.byType(KeyboardServiceWidget);
  39. final KeyboardServiceWidgetState state = tester.state(finder);
  40. return state.textInputService as TextInputClient;
  41. }
  42. }