forked from asjqkkkk/markdown_widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget_test.dart
242 lines (232 loc) · 7.25 KB
/
widget_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:markdown_widget/markdown_widget.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'test_markdowns/network_image_mock.dart';
import 'widget_visitor_test.dart';
import 'package:path/path.dart' as p;
void main() {
const testMarkdown = '''
| align left | centered | align right |
| :-- | :-: | --: |
| a | b | c |
# a
## askdljakl
### akslfjkl
### akslfjkl
### akslfjkl
## askdljakl
### akslfjkl
### akslfjkl
### akslfjkl
## askdljakl
### akslfjkl
### akslfjkl
### akslfjkl
## askdljakl
### akslfjkl
### akslfjkl
### akslfjkl
## askdljakl
### akslfjkl
### akslfjkl
### akslfjkl
### akslfjklasjf22
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf
### akslfjklasjf33
- asdasd
- asdasda
- asdasdas
1. asdasdasd
2. asdasdasd
3. asdasdasd''';
testWidgets('test toc widget', (tester) async {
final tocController = TocController();
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Directionality(
textDirection: TextDirection.ltr,
child: TocWidget(
controller: tocController,
itemBuilder: (data) {
if (data.index == 0) return Container();
return null;
},
)),
),
));
final list = List.generate(10, (index) {
final heading = HeadingNode(H1Config(), WidgetVisitor());
heading.accept(TextNode(text: "$index"));
return Toc(
node: heading,
widgetIndex: index,
selfIndex: index,
);
});
tocController.setTocList(list);
print(tocController.tocList);
tocController.jumpToIndexCallback = (i) {
print('jumpToIndexCallback:$i');
};
tocController.onIndexChanged(5);
tocController.jumpToIndex(2);
await tester.scrollUntilVisible(
find.text('8'), // what you want to find // widget you want to scroll
200);
final gesture = await tester.startGesture(Offset(0, 300));
gesture.up();
tocController.setTocList([list.removeLast()]);
tocController.dispose();
});
testWidgets('test markdown widget', (tester) async {
final tocController = TocController();
tocController.jumpToIndexCallback = (i) {
print('jumpToIndexCallback :$i');
};
VisibilityDetectorController.instance.updateInterval = Duration.zero;
String text = '';
late StateSetter setter;
late BuildContext ctx;
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Directionality(
textDirection: TextDirection.ltr,
child: StatefulBuilder(builder: (context, callback) {
ctx = context;
setter = callback;
return MarkdownWidget(
data: text,
tocController: tocController,
config: MarkdownConfig(configs: [
TableConfig(wrapper: (child) => Container(child: child))
]),
);
})),
),
));
tocController.jumpToIndex(0);
setter(() {
text = testMarkdown;
});
await tester.scrollUntilVisible(find.text('akslfjklasjf22'), 50);
tocController.jumpToIndex(0);
await tester.scrollUntilVisible(find.text('akslfjklasjf33'), 50);
final widget = tester.firstWidget(find.byWidgetPredicate(
(widget) => widget is NotificationListener<UserScrollNotification>))
as NotificationListener<UserScrollNotification>;
widget.onNotification?.call(UserScrollNotification(
metrics: FixedScrollMetrics(
minScrollExtent: 0,
maxScrollExtent: 1,
pixels: 1,
viewportDimension: 1,
axisDirection: AxisDirection.down,
devicePixelRatio: 1),
context: ctx,
direction: ScrollDirection.forward));
});
testWidgets('test markdown block', (tester) async {
VisibilityDetectorController.instance.updateInterval = Duration.zero;
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: StatefulBuilder(builder: (context, callback) {
return SingleChildScrollView(
child: MarkdownBlock(data: testMarkdown),
);
}),
),
));
});
testWidgets('test other widgets', (tester) async {
final jsonList = getTestJsonList();
for (var json in jsonList) {
final content = json['markdown'];
final widgets = testMarkdownGenerator(content);
for (var widget in widgets) {
await mockNetworkImagesFor(() async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Directionality(
textDirection: TextDirection.ltr, child: widget)),
));
});
}
}
});
testWidgets('test for asset file', (tester) async {
VisibilityDetectorController.instance.updateInterval = Duration.zero;
final current = Directory.current;
final jsonPath = p.join(current.path, 'example', 'assets', 'editor.md');
File jsonFile = File(jsonPath);
final content = jsonFile.readAsStringSync();
await mockNetworkImagesFor(() async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Directionality(
textDirection: TextDirection.ltr,
child: MarkdownWidget(
data: content,
config: MarkdownConfig.defaultConfig.copy(configs: [
BlockquoteConfig(),
ListConfig(),
TableConfig(),
LinkConfig(),
ImgConfig(),
CheckBoxConfig(),
]),
markdownGenerator: MarkdownGenerator(generators: [
SpanNodeGeneratorWithTag(
tag: 'test',
generator: (e, config, visitor) {
return TextNode(text: e.textContent);
})
]),
))),
));
});
});
testWidgets('MCheckBox test', (tester) async {
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: Directionality(
textDirection: TextDirection.ltr,
child: ListView(
children: [
MCheckBox(checked: false),
MCheckBox(checked: true),
],
))),
));
});
testWidgets('test ImageViewer iconButton pressed', (tester) async {
await mockNetworkImagesFor(() async {
await tester.pumpWidget(MaterialApp(
home: ImageViewer(child: Container(width: 100, height: 100))));
});
final buttons = tester
.widgetList(find.byWidgetPredicate((widget) => widget is IconButton));
for (var button in buttons) {
(button as IconButton).onPressed?.call();
}
});
testWidgets('test ImageViewer gesture taped', (tester) async {
await mockNetworkImagesFor(() async {
await tester.pumpWidget(MaterialApp(
home: ImageViewer(child: Container(width: 100, height: 100))));
});
await (await tester.startGesture(Offset(50, 50))).up();
});
}