zefyr

render_context_test.dart 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/editable_box.dart';
  7. import 'package:zefyr/src/widgets/render_context.dart';
  8. import 'package:zefyr/zefyr.dart';
  9. void main() {
  10. group('$ZefyrRenderContext', () {
  11. ZefyrRenderContext context;
  12. setUp(() {
  13. WidgetsFlutterBinding.ensureInitialized();
  14. context = new ZefyrRenderContext();
  15. });
  16. test('adds to dirty list first', () {
  17. var p = createParagraph(context);
  18. context.addBox(p);
  19. expect(context.dirty, isNotEmpty);
  20. expect(context.active, isEmpty);
  21. expect(context.dirty, contains(p));
  22. });
  23. test('removes from dirty list', () {
  24. var p = createParagraph(context);
  25. context.addBox(p);
  26. expect(context.dirty, isNotEmpty);
  27. context.removeBox(p);
  28. expect(context.dirty, isEmpty);
  29. });
  30. test('markDirty moves between active and dirty lists', () {
  31. var p = createParagraph(context);
  32. context.addBox(p);
  33. context.markDirty(p, false);
  34. expect(context.dirty, isEmpty);
  35. expect(context.active, isNotEmpty);
  36. context.markDirty(p, true);
  37. expect(context.dirty, isNotEmpty);
  38. expect(context.active, isEmpty);
  39. });
  40. test('finds paragraph for text offset', () {
  41. var p = createParagraph(context);
  42. context.addBox(p);
  43. expect(context.boxForTextOffset(0), isNull);
  44. context.markDirty(p, false);
  45. expect(context.boxForTextOffset(0), p);
  46. });
  47. testWidgets('notifyListeners is delayed to next frame', (tester) async {
  48. var focusNode = FocusNode();
  49. var controller = ZefyrController(new NotusDocument());
  50. var widget = MaterialApp(
  51. home: ZefyrScaffold(
  52. child: ZefyrEditor(
  53. controller: controller,
  54. focusNode: focusNode,
  55. ),
  56. ),
  57. );
  58. await tester.pumpWidget(widget);
  59. controller.replaceText(0, 0, 'test');
  60. var result = await tester.pumpAndSettle();
  61. expect(result, 2);
  62. });
  63. });
  64. }
  65. RenderEditableProxyBox createParagraph(ZefyrRenderContext viewport) {
  66. final doc = new NotusDocument();
  67. doc.insert(0, 'This House Is A Circus');
  68. final LineNode node = doc.root.children.first;
  69. final link = new LayerLink();
  70. final showCursor = new ValueNotifier<bool>(true);
  71. final selection = new TextSelection.collapsed(offset: 0);
  72. final selectionColor = Colors.blue;
  73. return new RenderEditableProxyBox(
  74. node: node,
  75. layerLink: link,
  76. renderContext: viewport,
  77. showCursor: showCursor,
  78. selection: selection,
  79. selectionColor: selectionColor,
  80. );
  81. }