zefyr

editable_text_scope_test.dart 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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('$ZefyrEditableTextScope', () {
  11. setUp(() {
  12. WidgetsFlutterBinding.ensureInitialized();
  13. });
  14. test('updateShouldNotify for rendering context changes', () {
  15. var context = new ZefyrRenderContext();
  16. var paragraph1 = createParagraph(context);
  17. var paragraph2 = createParagraph(context);
  18. context.addBox(paragraph1);
  19. context.markDirty(paragraph1, false);
  20. var widget1 = createScope(renderingContext: context);
  21. var widget2 = createScope(renderingContext: context);
  22. expect(widget2.updateShouldNotify(widget1), isFalse);
  23. context.addBox(paragraph2);
  24. context.markDirty(paragraph2, false);
  25. widget2 = createScope(renderingContext: context);
  26. expect(widget2.updateShouldNotify(widget1), isTrue);
  27. });
  28. test('updateShouldNotify for selection changes', () {
  29. var context = new ZefyrRenderContext();
  30. var selection = new TextSelection.collapsed(offset: 0);
  31. var widget1 =
  32. createScope(renderingContext: context, selection: selection);
  33. var widget2 =
  34. createScope(renderingContext: context, selection: selection);
  35. expect(widget2.updateShouldNotify(widget1), isFalse);
  36. selection = new TextSelection.collapsed(offset: 1);
  37. widget2 = createScope(renderingContext: context, selection: selection);
  38. expect(widget2.updateShouldNotify(widget1), isTrue);
  39. });
  40. test('updateShouldNotify for showCursor changes', () {
  41. var context = new ZefyrRenderContext();
  42. var showCursor = new ValueNotifier<bool>(true);
  43. var widget1 =
  44. createScope(renderingContext: context, showCursor: showCursor);
  45. var widget2 =
  46. createScope(renderingContext: context, showCursor: showCursor);
  47. expect(widget2.updateShouldNotify(widget1), isFalse);
  48. showCursor = new ValueNotifier<bool>(true);
  49. widget2 = createScope(renderingContext: context, showCursor: showCursor);
  50. expect(widget2.updateShouldNotify(widget1), isTrue);
  51. });
  52. test('updateShouldNotify for imageDelegate changes', () {
  53. var context = new ZefyrRenderContext();
  54. var delegate = new ZefyrDefaultImageDelegate();
  55. var widget1 =
  56. createScope(renderingContext: context, imageDelegate: delegate);
  57. var widget2 =
  58. createScope(renderingContext: context, imageDelegate: delegate);
  59. expect(widget2.updateShouldNotify(widget1), isFalse);
  60. delegate = new ZefyrDefaultImageDelegate();
  61. widget2 = createScope(renderingContext: context, imageDelegate: delegate);
  62. expect(widget2.updateShouldNotify(widget1), isTrue);
  63. });
  64. });
  65. }
  66. ZefyrEditableTextScope createScope({
  67. @required ZefyrRenderContext renderingContext,
  68. TextSelection selection,
  69. ValueNotifier<bool> showCursor,
  70. ZefyrImageDelegate imageDelegate,
  71. }) {
  72. return ZefyrEditableTextScope(
  73. renderContext: renderingContext,
  74. selection: selection,
  75. showCursor: showCursor,
  76. imageDelegate: imageDelegate,
  77. child: null,
  78. );
  79. }
  80. RenderEditableProxyBox createParagraph(ZefyrRenderContext context) {
  81. final doc = new NotusDocument();
  82. doc.insert(0, 'This House Is A Circus');
  83. final link = new LayerLink();
  84. final showCursor = new ValueNotifier<bool>(true);
  85. final selection = new TextSelection.collapsed(offset: 0);
  86. final selectionColor = Colors.blue;
  87. return new RenderEditableProxyBox(
  88. node: doc.root.children.first,
  89. layerLink: link,
  90. renderContext: context,
  91. showCursor: showCursor,
  92. selection: selection,
  93. selectionColor: selectionColor,
  94. );
  95. }