zefyr

controller_test.dart 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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(controller.document.toDelta(), Delta()..insert('More Words\n'));
  56. expect(controller.lastChangeSource, ChangeSource.remote);
  57. });
  58. test('replaceText', () {
  59. bool notified = false;
  60. controller.addListener(() {
  61. notified = true;
  62. });
  63. var selection = TextSelection.collapsed(offset: 5);
  64. controller.replaceText(0, 0, 'Words', selection: selection);
  65. expect(notified, isTrue);
  66. expect(controller.selection, selection);
  67. expect(controller.document.toDelta(), Delta()..insert('Words\n'));
  68. expect(controller.lastChangeSource, ChangeSource.local);
  69. });
  70. test('formatText', () {
  71. bool notified = false;
  72. controller.addListener(() {
  73. notified = true;
  74. });
  75. controller.replaceText(0, 0, 'Words');
  76. controller.formatText(0, 5, NotusAttribute.bold);
  77. expect(notified, isTrue);
  78. expect(
  79. controller.document.toDelta(),
  80. Delta()..insert('Words', NotusAttribute.bold.toJson())..insert('\n'),
  81. );
  82. expect(controller.lastChangeSource, ChangeSource.local);
  83. });
  84. test('formatText with toggled style enabled', () {
  85. bool notified = false;
  86. controller.addListener(() {
  87. notified = true;
  88. });
  89. controller.replaceText(0, 0, 'Words');
  90. controller.formatText(2, 0, NotusAttribute.bold);
  91. // Test that doing nothing does reset the toggledStyle.
  92. controller.replaceText(2, 0, '');
  93. controller.replaceText(2, 0, 'n');
  94. controller.formatText(3, 0, NotusAttribute.bold);
  95. controller.replaceText(3, 0, 'B');
  96. expect(notified, isTrue);
  97. expect(
  98. controller.document.toDelta(),
  99. Delta()
  100. ..insert('Won')
  101. ..insert('B', NotusAttribute.bold.toJson())
  102. ..insert('rds')
  103. ..insert('\n'),
  104. );
  105. expect(controller.lastChangeSource, ChangeSource.local);
  106. });
  107. test('insert text with toggled style unset', () {
  108. bool notified = false;
  109. controller.addListener(() {
  110. notified = true;
  111. });
  112. controller.replaceText(0, 0, 'Words');
  113. controller.formatText(1, 0, NotusAttribute.bold);
  114. controller.replaceText(1, 0, 'B');
  115. controller.formatText(2, 0, NotusAttribute.bold.unset);
  116. controller.replaceText(2, 0, 'u');
  117. expect(notified, isTrue);
  118. expect(
  119. controller.document.toDelta(),
  120. Delta()
  121. ..insert('W')
  122. ..insert('B', NotusAttribute.bold.toJson())
  123. ..insert('uords')
  124. ..insert('\n'),
  125. );
  126. expect(controller.lastChangeSource, ChangeSource.local);
  127. });
  128. test('formatSelection', () {
  129. bool notified = false;
  130. controller.addListener(() {
  131. notified = true;
  132. });
  133. var selection = TextSelection(baseOffset: 0, extentOffset: 5);
  134. controller.replaceText(0, 0, 'Words', selection: selection);
  135. controller.formatSelection(NotusAttribute.bold);
  136. expect(notified, isTrue);
  137. expect(
  138. controller.document.toDelta(),
  139. Delta()..insert('Words', NotusAttribute.bold.toJson())..insert('\n'),
  140. );
  141. expect(controller.lastChangeSource, ChangeSource.local);
  142. });
  143. test('getSelectionStyle', () {
  144. var selection = TextSelection.collapsed(offset: 3);
  145. controller.replaceText(0, 0, 'Words', selection: selection);
  146. controller.formatText(0, 5, NotusAttribute.bold);
  147. var result = controller.getSelectionStyle();
  148. expect(result.values, [NotusAttribute.bold]);
  149. });
  150. test('getSelectionStyle with toggled style', () {
  151. var selection = TextSelection.collapsed(offset: 3);
  152. controller.replaceText(0, 0, 'Words', selection: selection);
  153. controller.formatText(3, 0, NotusAttribute.bold);
  154. var result = controller.getSelectionStyle();
  155. expect(result.values, [NotusAttribute.bold]);
  156. });
  157. });
  158. }