Parcourir la source

Added tests for ZefyrScope

Anatoly Pulyaevskiy il y a 6 ans
Parent
révision
55c88b7c76

+ 0
- 107
packages/zefyr/test/widgets/editable_text_scope_test.dart Voir le fichier

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

+ 75
- 0
packages/zefyr/test/widgets/scope_test.dart Voir le fichier

@@ -0,0 +1,75 @@
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
+
5
+import 'package:flutter/material.dart';
6
+import 'package:flutter_test/flutter_test.dart';
7
+import 'package:zefyr/zefyr.dart';
8
+
9
+void main() {
10
+  group('$ZefyrScope', () {
11
+    ZefyrScope scope;
12
+
13
+    setUp(() {
14
+      WidgetsFlutterBinding.ensureInitialized();
15
+      final doc = NotusDocument();
16
+      scope = ZefyrScope.editable(
17
+        controller: ZefyrController(doc),
18
+        imageDelegate: ZefyrDefaultImageDelegate(),
19
+        focusNode: FocusNode(),
20
+        focusScope: FocusScopeNode(),
21
+      );
22
+    });
23
+
24
+    test('it notifies on image delegate update', () {
25
+      bool notified = false;
26
+      scope.addListener(() {
27
+        notified = true;
28
+      });
29
+      final delegate = ZefyrDefaultImageDelegate();
30
+      scope.imageDelegate = delegate;
31
+      expect(notified, isTrue);
32
+      notified = false;
33
+      scope.imageDelegate = delegate;
34
+      expect(notified, isFalse);
35
+    });
36
+
37
+    test('it notifies on controller update', () {
38
+      bool notified = false;
39
+      scope.addListener(() {
40
+        notified = true;
41
+      });
42
+      final controller = ZefyrController(NotusDocument());
43
+      scope.controller = controller;
44
+      expect(notified, isTrue);
45
+      notified = false;
46
+      scope.controller = controller;
47
+      expect(notified, isFalse);
48
+    });
49
+
50
+    test('it notifies on focus node update', () {
51
+      bool notified = false;
52
+      scope.addListener(() {
53
+        notified = true;
54
+      });
55
+      final focusNode = FocusNode();
56
+      scope.focusNode = focusNode;
57
+      expect(notified, isTrue);
58
+      notified = false;
59
+      scope.focusNode = focusNode;
60
+      expect(notified, isFalse);
61
+    });
62
+
63
+    test('it notifies on selection changes but not text changes', () {
64
+      bool notified = false;
65
+      scope.addListener(() {
66
+        notified = true;
67
+      });
68
+
69
+      scope.controller.replaceText(0, 0, 'Hello');
70
+      expect(notified, isFalse);
71
+      scope.controller.updateSelection(TextSelection.collapsed(offset: 4));
72
+      expect(notified, isTrue);
73
+    });
74
+  });
75
+}