Browse Source

Fix toggling toolbar between two editors (#229)

Anatoly Pulyaevskiy 5 years ago
parent
commit
af80faea11
No account linked to committer's email address

+ 1
- 1
packages/zefyr/lib/src/widgets/editor.dart View File

@@ -90,7 +90,7 @@ class _ZefyrEditorState extends State<ZefyrEditor> {
90 90
 
91 91
   void hideToolbar() {
92 92
     if (_toolbarKey == null) return;
93
-    _scaffold.hideToolbar();
93
+    _scaffold.hideToolbar(buildToolbar);
94 94
     _toolbarKey = null;
95 95
   }
96 96
 

+ 5
- 0
packages/zefyr/lib/src/widgets/mode.dart View File

@@ -58,4 +58,9 @@ class ZefyrMode {
58 58
 
59 59
   @override
60 60
   int get hashCode => hash3(canEdit, canSelect, canFormat);
61
+
62
+  @override
63
+  String toString() {
64
+    return 'ZefyrMode(canEdit: $canEdit, canSelect: $canSelect, canFormat: $canFormat)';
65
+  }
61 66
 }

+ 2
- 2
packages/zefyr/lib/src/widgets/scaffold.dart View File

@@ -27,8 +27,8 @@ class ZefyrScaffoldState extends State<ZefyrScaffold> {
27 27
     });
28 28
   }
29 29
 
30
-  void hideToolbar() {
31
-    if (_toolbarBuilder != null) {
30
+  void hideToolbar(WidgetBuilder builder) {
31
+    if (_toolbarBuilder == builder) {
32 32
       setState(() {
33 33
         _toolbarBuilder = null;
34 34
       });

+ 94
- 3
packages/zefyr/test/testing.dart View File

@@ -119,9 +119,9 @@ class EditorSandBox {
119 119
 class _ZefyrSandbox extends StatefulWidget {
120 120
   const _ZefyrSandbox({
121 121
     Key key,
122
-    this.controller,
123
-    this.focusNode,
124
-    this.autofocus,
122
+    @required this.controller,
123
+    @required this.focusNode,
124
+    this.autofocus = false,
125 125
     this.imageDelegate,
126 126
   }) : super(key: key);
127 127
   final ZefyrController controller;
@@ -153,3 +153,94 @@ class _ZefyrSandboxState extends State<_ZefyrSandbox> {
153 153
     });
154 154
   }
155 155
 }
156
+
157
+class MultiEditorSandbox {
158
+  final WidgetTester tester;
159
+  final Key firstEditorKey;
160
+  final Key secondEditorKey;
161
+  final FocusNode firstFocusNode;
162
+  final FocusNode secondFocusNode;
163
+  final Widget widget;
164
+
165
+  factory MultiEditorSandbox({@required WidgetTester tester}) {
166
+    final firstEditorKey = UniqueKey();
167
+    final secondEditorKey = UniqueKey();
168
+    final firstFocusNode = FocusNode();
169
+    final secondFocusNode = FocusNode();
170
+    Widget first = _ZefyrSandbox(
171
+      key: firstEditorKey,
172
+      controller: ZefyrController(NotusDocument.fromDelta(delta)),
173
+      focusNode: firstFocusNode,
174
+    );
175
+    Widget second = _ZefyrSandbox(
176
+      key: secondEditorKey,
177
+      controller: ZefyrController(NotusDocument.fromDelta(delta)),
178
+      focusNode: secondFocusNode,
179
+    );
180
+
181
+    Widget widget = MaterialApp(
182
+      home: Scaffold(
183
+        body: ZefyrScaffold(
184
+          child: Column(
185
+            children: <Widget>[
186
+              SizedBox(height: 100, child: first),
187
+              SizedBox(height: 10),
188
+              SizedBox(height: 100, child: second),
189
+            ],
190
+          ),
191
+        ),
192
+      ),
193
+    );
194
+
195
+    return MultiEditorSandbox._(
196
+      tester: tester,
197
+      widget: widget,
198
+      firstEditorKey: firstEditorKey,
199
+      secondEditorKey: secondEditorKey,
200
+      firstFocusNode: firstFocusNode,
201
+      secondFocusNode: secondFocusNode,
202
+    );
203
+  }
204
+
205
+  MultiEditorSandbox._({
206
+    @required this.tester,
207
+    @required this.widget,
208
+    @required this.firstEditorKey,
209
+    @required this.secondEditorKey,
210
+    @required this.firstFocusNode,
211
+    @required this.secondFocusNode,
212
+  });
213
+
214
+  Future<void> pump() async {
215
+    await tester.pumpWidget(widget);
216
+  }
217
+
218
+  Future<void> tapFirstEditor() async {
219
+    await tester.tap(find.byKey(firstEditorKey).first);
220
+    await tester.pumpAndSettle();
221
+  }
222
+
223
+  Future<void> tapSecondEditor() async {
224
+    await tester.tap(find.byKey(secondEditorKey).first);
225
+    await tester.pumpAndSettle();
226
+  }
227
+
228
+  ZefyrEditor findFirstEditor() {
229
+    return tester.widget(find.descendant(
230
+      of: find.byKey(firstEditorKey),
231
+      matching: find.byType(ZefyrEditor),
232
+    ));
233
+  }
234
+
235
+  ZefyrEditor findSecondEditor() {
236
+    return tester.widget(find.descendant(
237
+      of: find.byKey(secondEditorKey),
238
+      matching: find.byType(ZefyrEditor),
239
+    ));
240
+  }
241
+
242
+  Future<void> tapButtonWithIcon(IconData icon) async {
243
+    await tester.tap(find.widgetWithIcon(ZefyrButton, icon));
244
+    await tester.pumpAndSettle();
245
+  }
246
+}

+ 28
- 0
packages/zefyr/test/widgets/editor_test.dart View File

@@ -46,5 +46,33 @@ void main() {
46 46
       ZefyrEditor widget = tester.widget(find.byType(ZefyrEditor));
47 47
       expect(widget.mode, ZefyrMode.view);
48 48
     });
49
+
50
+    testWidgets('toggle toolbar between two editors', (tester) async {
51
+      final sandbox = MultiEditorSandbox(tester: tester);
52
+      await sandbox.pump();
53
+      await sandbox.tapFirstEditor();
54
+      expect(sandbox.firstFocusNode.hasFocus, isTrue);
55
+      expect(sandbox.secondFocusNode.hasFocus, isFalse);
56
+      expect(find.byIcon(Icons.format_list_bulleted), findsOneWidget);
57
+
58
+      await sandbox.tapButtonWithIcon(Icons.format_list_bulleted);
59
+      ZefyrEditor widget = sandbox.findFirstEditor();
60
+      Node line = widget.controller.document.root.children.first;
61
+      expect(line, isInstanceOf<BlockNode>());
62
+      BlockNode block = line;
63
+      expect(block.style.contains(NotusAttribute.block.bulletList), isTrue);
64
+
65
+      await sandbox.tapSecondEditor();
66
+      expect(sandbox.firstFocusNode.hasFocus, isFalse);
67
+      expect(sandbox.secondFocusNode.hasFocus, isTrue);
68
+      expect(find.byIcon(Icons.format_list_bulleted), findsOneWidget);
69
+
70
+      await sandbox.tapButtonWithIcon(Icons.format_list_bulleted);
71
+      widget = sandbox.findSecondEditor();
72
+      line = widget.controller.document.root.children.first;
73
+      expect(line, isInstanceOf<BlockNode>());
74
+      block = line;
75
+      expect(block.style.contains(NotusAttribute.block.bulletList), isTrue);
76
+    });
49 77
   });
50 78
 }