Repository navigation
Expand file tree
/
Copy pathexample_code_viewer_test.dart
More file actions
132 lines (127 loc) · 4.71 KB
/
Copy pathexample_code_viewer_test.dart
File metadata and controls
132 lines (127 loc) · 4.71 KB
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
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:tdesign_flutter_example/base/example_code_viewer.dart';
import 'package:tdesign_flutter_example/config.dart';
import 'package:tdesign_flutter_example/provider/theme_mode_provider.dart';
import 'demo_page_test_utils.dart';
void main() {
testWidgets('所有公开分组的代码资源均已打包且非空', (tester) async {
await tester.runAsync(() async {
final manifest =
jsonDecode(await rootBundle.loadString('assets/code/manifest.json'))
as Map<String, dynamic>;
final groups = manifest['groups'] as Map<String, dynamic>;
expect(groups.length, greaterThanOrEqualTo(60));
var count = 0;
for (final modules in groups.values) {
for (final module in modules as List<dynamic>) {
for (final item in module['items'] as List<dynamic>) {
final key = item['assetKey'];
final code = await rootBundle.loadString('assets/code/$key.txt');
expect(code.trim(), isNotEmpty, reason: '$key');
count++;
}
}
}
expect(count, greaterThanOrEqualTo(370));
});
});
for (final page in [
(
name: 'fab',
entries: <String, String>{
'纯图标悬浮按钮': 'class FabIconOnlyExample',
'图标加文字悬浮按钮': 'class FabIconTextExample',
'可移动悬浮按钮': 'class FabDraggableExample',
'带自动收缩功能': 'class FabCollapsibleExample',
},
),
(
name: 'imageViewer',
entries: <String, String>{
'带操作图片预览': 'class ImageViewerWithActionsExample',
},
),
]) {
testWidgets('${page.name} 工具栏直接选择并展示示例代码', (tester) async {
await tester.runAsync(
() => preloadDemoCodeGroup(page.name == 'fab' ? 'fab' : 'image-viewer'),
);
tester.view.physicalSize = const Size(375, 812);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final model = exampleMap.values
.expand((items) => items)
.singleWhere((model) => model.name == page.name);
model.showAction = true;
await tester.pumpWidget(
ChangeNotifierProvider(
create: (_) => ThemeModeProvider(),
child: MaterialApp(
theme: TThemeBuilder.light(TThemeData.defaultData()),
home: Builder(
builder: (context) => model.pageBuilder(context, model),
),
),
),
);
await tester.pumpAndSettle();
for (final entry in page.entries.entries) {
await tester.tap(find.byIcon(TIcons.code));
await waitForDemoWidget(tester, find.text('选择示例代码'));
expect(find.text('选择示例代码'), findsOneWidget);
final option = find.descendant(
of: find.byType(BottomSheet),
matching: find.text(entry.key),
);
await tester.ensureVisible(option);
await tester.tap(option);
await waitForDemoWidget(tester, find.byType(Markdown));
final panel = find.byType(Markdown);
expect(panel, findsOneWidget);
expect(tester.widget<Markdown>(panel).data, contains(entry.value));
Navigator.of(tester.element(panel)).pop();
await tester.pumpAndSettle();
}
expect(tester.takeException(), isNull);
});
}
testWidgets('缺失分组与代码资源明确提示而不抛出异常', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Builder(
builder: (context) {
return Column(
children: [
TextButton(
onPressed: () =>
ExampleCodeViewer.showExamples(context, 'missing'),
child: const Text('group'),
),
TextButton(
onPressed: () =>
ExampleCodeViewer.showAsset(context, 'missing.txt'),
child: const Text('asset'),
),
],
);
},
),
),
);
for (final label in ['group', 'asset']) {
await tester.tap(find.text(label));
await waitForDemoWidget(tester, find.text('暂无演示代码'));
final message = find.text('暂无演示代码');
expect(message, findsOneWidget);
Navigator.of(tester.element(message)).pop();
await tester.pumpAndSettle();
}
expect(tester.takeException(), isNull);
});
}