zefyr

selection_test.dart 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_test/flutter_test.dart';
  6. import 'package:zefyr/src/widgets/rich_text.dart';
  7. import 'package:zefyr/zefyr.dart';
  8. import '../testing.dart';
  9. void main() {
  10. group('$ZefyrSelectionOverlay', () {
  11. testWidgets('double tap caret shows toolbar', (tester) async {
  12. final editor = new EditorSandBox(tester: tester);
  13. await editor.tapEditor();
  14. RenderEditableParagraph renderObject =
  15. tester.firstRenderObject(find.byType(EditableRichText));
  16. var offset = renderObject.localToGlobal(Offset.zero);
  17. offset += Offset(5.0, 5.0);
  18. await tester.tapAt(offset);
  19. await tester.pumpAndSettle();
  20. await tester.tapAt(offset);
  21. await tester.pumpAndSettle();
  22. expect(find.text('Paste'), findsOneWidget);
  23. });
  24. testWidgets('hides when editor lost focus', (tester) async {
  25. final editor = new EditorSandBox(tester: tester);
  26. await editor.tapEditor();
  27. await editor.updateSelection(base: 0, extent: 5);
  28. expect(editor.findSelectionHandle(), findsNWidgets(2));
  29. await editor.unfocus();
  30. expect(editor.findSelectionHandle(), findsNothing);
  31. });
  32. testWidgets('tap on padding area finds closest paragraph', (tester) async {
  33. final editor = new EditorSandBox(tester: tester);
  34. await editor.tapEditor();
  35. editor.controller
  36. .updateSelection(new TextSelection.collapsed(offset: 10));
  37. await tester.pumpAndSettle();
  38. expect(editor.controller.selection.extentOffset, 10);
  39. RenderEditableParagraph renderObject =
  40. tester.firstRenderObject(find.byType(EditableRichText));
  41. var offset = renderObject.localToGlobal(Offset.zero);
  42. offset += Offset(-5.0, 5.0);
  43. await tester.tapAt(offset);
  44. await tester.pumpAndSettle();
  45. expect(editor.controller.selection.isCollapsed, isTrue);
  46. expect(editor.controller.selection.extentOffset, 0);
  47. });
  48. testWidgets('tap on empty space finds closest paragraph', (tester) async {
  49. final editor = new EditorSandBox(tester: tester);
  50. await editor.tapEditor();
  51. editor.controller.replaceText(10, 1, '\n',
  52. selection: new TextSelection.collapsed(offset: 0));
  53. await tester.pumpAndSettle();
  54. expect(editor.controller.document.toPlainText(),
  55. 'This House\nIs A Circus\n');
  56. expect(editor.controller.selection.extentOffset, 0);
  57. RenderBox renderObject =
  58. tester.firstRenderObject(find.byType(ZefyrEditableText));
  59. var offset = renderObject.localToGlobal(Offset.zero);
  60. offset += Offset(50.0, renderObject.size.height - 5.0);
  61. await tester.tapAt(offset);
  62. await tester.pumpAndSettle();
  63. expect(editor.controller.selection.isCollapsed, isTrue);
  64. expect(editor.controller.selection.extentOffset,
  65. 13); // Note that this is probably too fragile.
  66. offset = renderObject.localToGlobal(Offset.zero) + Offset(50.0, 1.0);
  67. await tester.tapAt(offset);
  68. await tester.pumpAndSettle();
  69. expect(editor.controller.selection.isCollapsed, isTrue);
  70. expect(editor.controller.selection.extentOffset,
  71. 2); // Note that this is probably too fragile.
  72. });
  73. });
  74. }