ime.dart 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 = findDeltaTextInputClient();
  11. }
  12. final WidgetTester tester;
  13. late final DeltaTextInputClient 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 deltas = [
  26. TextEditingDeltaInsertion(
  27. textInserted: character,
  28. oldText: value.text.replaceRange(
  29. value.selection.start,
  30. value.selection.end,
  31. '',
  32. ),
  33. insertionOffset: value.selection.baseOffset,
  34. selection: TextSelection.collapsed(
  35. offset: value.selection.baseOffset + 1,
  36. ),
  37. composing: TextRange.empty,
  38. ),
  39. ];
  40. client.updateEditingValueWithDeltas(deltas);
  41. await tester.pumpAndSettle();
  42. }
  43. DeltaTextInputClient findDeltaTextInputClient() {
  44. final finder = find.byType(KeyboardServiceWidget);
  45. final KeyboardServiceWidgetState state = tester.state(finder);
  46. return state.textInputService as DeltaTextInputClient;
  47. }
  48. }