zefyr

editable_text_scope_test.dart 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. });
  53. }
  54. ZefyrEditableTextScope createScope({
  55. @required ZefyrRenderContext renderingContext,
  56. TextSelection selection,
  57. ValueNotifier<bool> showCursor,
  58. }) {
  59. return ZefyrEditableTextScope(
  60. renderContext: renderingContext,
  61. selection: selection,
  62. showCursor: showCursor,
  63. child: null,
  64. );
  65. }
  66. RenderEditableProxyBox createParagraph(ZefyrRenderContext context) {
  67. final doc = new NotusDocument();
  68. doc.insert(0, 'This House Is A Circus');
  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: doc.root.children.first,
  75. layerLink: link,
  76. renderContext: context,
  77. showCursor: showCursor,
  78. selection: selection,
  79. selectionColor: selectionColor,
  80. );
  81. }