zefyr

testing.dart 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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:quill_delta/quill_delta.dart';
  7. import 'package:zefyr/src/widgets/selection.dart';
  8. import 'package:zefyr/zefyr.dart';
  9. var delta = new Delta()..insert('This House Is A Circus\n');
  10. class EditorSandBox {
  11. final WidgetTester tester;
  12. final FocusNode focusNode;
  13. final NotusDocument document;
  14. final ZefyrController controller;
  15. final Widget widget;
  16. factory EditorSandBox({
  17. @required WidgetTester tester,
  18. FocusNode focusNode,
  19. NotusDocument document,
  20. ZefyrThemeData theme,
  21. }) {
  22. focusNode ??= FocusNode();
  23. document ??= NotusDocument.fromDelta(delta);
  24. var controller = ZefyrController(document);
  25. Widget widget = _ZefyrSandbox(controller: controller, focusNode: focusNode);
  26. if (theme != null) {
  27. widget = ZefyrTheme(data: theme, child: widget);
  28. }
  29. widget = MaterialApp(
  30. home: ZefyrScaffold(child: widget),
  31. );
  32. return EditorSandBox._(tester, focusNode, document, controller, widget);
  33. }
  34. EditorSandBox._(
  35. this.tester, this.focusNode, this.document, this.controller, this.widget);
  36. TextSelection get selection => controller.selection;
  37. Future<void> unfocus() {
  38. focusNode.unfocus();
  39. return tester.pumpAndSettle();
  40. }
  41. Future<void> updateSelection({int base, int extent}) {
  42. controller.updateSelection(
  43. new TextSelection(baseOffset: base, extentOffset: extent),
  44. );
  45. return tester.pumpAndSettle();
  46. }
  47. Future<void> disable() {
  48. _ZefyrSandboxState state = tester.state(find.byType(_ZefyrSandbox));
  49. state.disable();
  50. return tester.pumpAndSettle();
  51. }
  52. Future<void> tapEditor() async {
  53. await tester.pumpWidget(widget);
  54. await tester.tap(find.byType(ZefyrParagraph).first);
  55. await tester.pumpAndSettle();
  56. expect(focusNode.hasFocus, isTrue);
  57. }
  58. Future<void> tapHideKeyboardButton() async {
  59. await tapButtonWithIcon(Icons.keyboard_hide);
  60. }
  61. Future<void> tapButtonWithIcon(IconData icon) async {
  62. await tester.tap(find.widgetWithIcon(ZefyrButton, icon));
  63. await tester.pumpAndSettle();
  64. }
  65. Future<void> tapButtonWithText(String text) async {
  66. await tester.tap(find.widgetWithText(ZefyrButton, text));
  67. await tester.pumpAndSettle();
  68. }
  69. RawZefyrButton findButtonWithIcon(IconData icon) {
  70. RawZefyrButton button =
  71. tester.widget(find.widgetWithIcon(RawZefyrButton, icon));
  72. return button;
  73. }
  74. RawZefyrButton findButtonWithText(String text) {
  75. RawZefyrButton button =
  76. tester.widget(find.widgetWithText(RawZefyrButton, text));
  77. return button;
  78. }
  79. Finder findSelectionHandle() {
  80. return find.descendant(
  81. of: find.byType(SelectionHandleDriver),
  82. matching: find.byType(Positioned));
  83. }
  84. }
  85. class _ZefyrSandbox extends StatefulWidget {
  86. const _ZefyrSandbox({Key key, this.controller, this.focusNode})
  87. : super(key: key);
  88. final ZefyrController controller;
  89. final FocusNode focusNode;
  90. @override
  91. _ZefyrSandboxState createState() => _ZefyrSandboxState();
  92. }
  93. class _ZefyrSandboxState extends State<_ZefyrSandbox> {
  94. bool _enabled = true;
  95. @override
  96. Widget build(BuildContext context) {
  97. return new ZefyrEditor(
  98. controller: widget.controller,
  99. focusNode: widget.focusNode,
  100. enabled: _enabled,
  101. );
  102. }
  103. void disable() {
  104. setState(() {
  105. _enabled = false;
  106. });
  107. }
  108. }