123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- // Copyright (c) 2018, the Zefyr project authors. Please see the AUTHORS file
- // for details. All rights reserved. Use of this source code is governed by a
- // BSD-style license that can be found in the LICENSE file.
- import 'package:flutter/services.dart';
- import 'package:flutter_test/flutter_test.dart';
- import 'package:quill_delta/quill_delta.dart';
- import 'package:zefyr/zefyr.dart';
-
- void main() {
- group('$ZefyrController', () {
- ZefyrController controller;
-
- setUp(() {
- var doc = NotusDocument();
- controller = ZefyrController(doc);
- });
-
- test('dispose', () {
- controller.dispose();
- expect(controller.document.isClosed, isTrue);
- });
-
- test('selection', () {
- bool notified = false;
- controller.addListener(() {
- notified = true;
- });
- controller.updateSelection(TextSelection.collapsed(offset: 0));
- expect(notified, isTrue);
- expect(controller.selection, TextSelection.collapsed(offset: 0));
- expect(controller.lastChangeSource, ChangeSource.remote);
- });
-
- test('compose', () {
- bool notified = false;
- controller.addListener(() {
- notified = true;
- });
- var selection = TextSelection.collapsed(offset: 5);
- var change = Delta()..insert('Words');
- controller.compose(change, selection: selection);
- expect(notified, isTrue);
- expect(controller.selection, selection);
- expect(controller.document.toDelta(), Delta()..insert('Words\n'));
- expect(controller.lastChangeSource, ChangeSource.remote);
- });
-
- test('compose and transform position', () {
- bool notified = false;
- controller.addListener(() {
- notified = true;
- });
- var selection = TextSelection.collapsed(offset: 5);
- var change = Delta()..insert('Words');
- controller.compose(change, selection: selection);
- var change2 = Delta()..insert('More ');
- controller.compose(change2);
- expect(notified, isTrue);
- var expectedSelection = TextSelection.collapsed(offset: 10);
- expect(controller.selection, expectedSelection);
- expect(
- controller.document.toDelta(), Delta()..insert('More Words\n'));
- expect(controller.lastChangeSource, ChangeSource.remote);
- });
-
- test('replaceText', () {
- bool notified = false;
- controller.addListener(() {
- notified = true;
- });
- var selection = TextSelection.collapsed(offset: 5);
- controller.replaceText(0, 0, 'Words', selection: selection);
- expect(notified, isTrue);
- expect(controller.selection, selection);
- expect(controller.document.toDelta(), Delta()..insert('Words\n'));
- expect(controller.lastChangeSource, ChangeSource.local);
- });
-
- test('formatText', () {
- bool notified = false;
- controller.addListener(() {
- notified = true;
- });
- controller.replaceText(0, 0, 'Words');
- controller.formatText(0, 5, NotusAttribute.bold);
- expect(notified, isTrue);
- expect(
- controller.document.toDelta(),
- Delta()
- ..insert('Words', NotusAttribute.bold.toJson())
- ..insert('\n'),
- );
- expect(controller.lastChangeSource, ChangeSource.local);
- });
-
- test('formatSelection', () {
- bool notified = false;
- controller.addListener(() {
- notified = true;
- });
- var selection = TextSelection(baseOffset: 0, extentOffset: 5);
- controller.replaceText(0, 0, 'Words', selection: selection);
- controller.formatSelection(NotusAttribute.bold);
- expect(notified, isTrue);
- expect(
- controller.document.toDelta(),
- Delta()
- ..insert('Words', NotusAttribute.bold.toJson())
- ..insert('\n'),
- );
- expect(controller.lastChangeSource, ChangeSource.local);
- });
-
- test('getSelectionStyle', () {
- var selection = TextSelection.collapsed(offset: 3);
- controller.replaceText(0, 0, 'Words', selection: selection);
- controller.formatText(0, 5, NotusAttribute.bold);
- var result = controller.getSelectionStyle();
- expect(result.values, [NotusAttribute.bold]);
- });
- });
- }
|