zefyr

controller_test.dart 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/services.dart';
  5. import 'package:flutter_test/flutter_test.dart';
  6. import 'package:quill_delta/quill_delta.dart';
  7. import 'package:zefyr/zefyr.dart';
  8. void main() {
  9. group('$ZefyrController', () {
  10. ZefyrController controller;
  11. setUp(() {
  12. var doc = NotusDocument();
  13. controller = ZefyrController(doc);
  14. });
  15. test('dispose', () {
  16. controller.dispose();
  17. expect(controller.document.isClosed, isTrue);
  18. });
  19. test('selection', () {
  20. bool notified = false;
  21. controller.addListener(() {
  22. notified = true;
  23. });
  24. controller.updateSelection(TextSelection.collapsed(offset: 0));
  25. expect(notified, isTrue);
  26. expect(controller.selection, TextSelection.collapsed(offset: 0));
  27. expect(controller.lastChangeSource, ChangeSource.remote);
  28. });
  29. test('compose', () {
  30. bool notified = false;
  31. controller.addListener(() {
  32. notified = true;
  33. });
  34. var selection = TextSelection.collapsed(offset: 5);
  35. var change = Delta()..insert('Words');
  36. controller.compose(change, selection: selection);
  37. expect(notified, isTrue);
  38. expect(controller.selection, selection);
  39. expect(controller.document.toDelta(), Delta()..insert('Words\n'));
  40. expect(controller.lastChangeSource, ChangeSource.remote);
  41. });
  42. test('compose and transform position', () {
  43. bool notified = false;
  44. controller.addListener(() {
  45. notified = true;
  46. });
  47. var selection = TextSelection.collapsed(offset: 5);
  48. var change = Delta()..insert('Words');
  49. controller.compose(change, selection: selection);
  50. var change2 = Delta()..insert('More ');
  51. controller.compose(change2);
  52. expect(notified, isTrue);
  53. var expectedSelection = TextSelection.collapsed(offset: 10);
  54. expect(controller.selection, expectedSelection);
  55. expect(
  56. controller.document.toDelta(), Delta()..insert('More Words\n'));
  57. expect(controller.lastChangeSource, ChangeSource.remote);
  58. });
  59. test('replaceText', () {
  60. bool notified = false;
  61. controller.addListener(() {
  62. notified = true;
  63. });
  64. var selection = TextSelection.collapsed(offset: 5);
  65. controller.replaceText(0, 0, 'Words', selection: selection);
  66. expect(notified, isTrue);
  67. expect(controller.selection, selection);
  68. expect(controller.document.toDelta(), Delta()..insert('Words\n'));
  69. expect(controller.lastChangeSource, ChangeSource.local);
  70. });
  71. test('formatText', () {
  72. bool notified = false;
  73. controller.addListener(() {
  74. notified = true;
  75. });
  76. controller.replaceText(0, 0, 'Words');
  77. controller.formatText(0, 5, NotusAttribute.bold);
  78. expect(notified, isTrue);
  79. expect(
  80. controller.document.toDelta(),
  81. Delta()
  82. ..insert('Words', NotusAttribute.bold.toJson())
  83. ..insert('\n'),
  84. );
  85. expect(controller.lastChangeSource, ChangeSource.local);
  86. });
  87. test('formatSelection', () {
  88. bool notified = false;
  89. controller.addListener(() {
  90. notified = true;
  91. });
  92. var selection = TextSelection(baseOffset: 0, extentOffset: 5);
  93. controller.replaceText(0, 0, 'Words', selection: selection);
  94. controller.formatSelection(NotusAttribute.bold);
  95. expect(notified, isTrue);
  96. expect(
  97. controller.document.toDelta(),
  98. Delta()
  99. ..insert('Words', NotusAttribute.bold.toJson())
  100. ..insert('\n'),
  101. );
  102. expect(controller.lastChangeSource, ChangeSource.local);
  103. });
  104. test('getSelectionStyle', () {
  105. var selection = TextSelection.collapsed(offset: 3);
  106. controller.replaceText(0, 0, 'Words', selection: selection);
  107. controller.formatText(0, 5, NotusAttribute.bold);
  108. var result = controller.getSelectionStyle();
  109. expect(result.values, [NotusAttribute.bold]);
  110. });
  111. });
  112. }