1
+ import 'package:flutter/material.dart' ;
2
+ import 'package:flutter_html/flutter_html.dart' ;
3
+ import 'package:flutter_html_svg/flutter_html_svg.dart' ;
4
+ import 'package:flutter_test/flutter_test.dart' ;
5
+ import 'package:meta/meta.dart' ;
6
+
7
+ void main () {
8
+ group ("custom image data uri matcher" , () {
9
+ CustomRenderMatcher matcher = svgDataUriMatcher (encoding: null , mime: 'image/svg+xml' );
10
+ testImgSrcMatcher (
11
+ "matches an svg data uri with base64 encoding" ,
12
+ matcher,
13
+ imgSrc:
14
+ 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB2aWV3Qm94PSIwIDAgMzAgMjAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxjaXJjbGUgY3g9IjE1IiBjeT0iMTAiIHI9IjEwIiBmaWxsPSJncmVlbiIvPgo8L3N2Zz4=' ,
15
+ shouldMatch: true ,
16
+ );
17
+ testImgSrcMatcher (
18
+ "matches an svg data uri without specified encoding" ,
19
+ matcher,
20
+ imgSrc:
21
+ 'data:image/svg+xml,%3C?xml version="1.0" encoding="UTF-8"?%3E%3Csvg viewBox="0 0 30 20" xmlns="http://www.w3.org/2000/svg"%3E%3Ccircle cx="15" cy="10" r="10" fill="green"/%3E%3C/svg%3E' ,
22
+ shouldMatch: true ,
23
+ );
24
+ testImgSrcMatcher (
25
+ "matches base64 data uri without data" ,
26
+ matcher,
27
+ imgSrc: 'data:image/svg+xml;base64,' ,
28
+ shouldMatch: true ,
29
+ );
30
+ testImgSrcMatcher (
31
+ "doesn't match non-base64 image data uri" ,
32
+ matcher,
33
+ imgSrc:
34
+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' ,
35
+ shouldMatch: false ,
36
+ );
37
+ testImgSrcMatcher (
38
+ "doesn't match different mime data uri" ,
39
+ matcher,
40
+ imgSrc: 'data:text/plain;base64,' ,
41
+ shouldMatch: false ,
42
+ );
43
+ testImgSrcMatcher (
44
+ "doesn't non-data schema" ,
45
+ matcher,
46
+ imgSrc: 'http:' ,
47
+ shouldMatch: false ,
48
+ );
49
+ testImgSrcMatcher (
50
+ "doesn't match null" ,
51
+ matcher,
52
+ imgSrc: null ,
53
+ shouldMatch: false ,
54
+ );
55
+ testImgSrcMatcher (
56
+ "doesn't match empty" ,
57
+ matcher,
58
+ imgSrc: '' ,
59
+ shouldMatch: false ,
60
+ );
61
+ });
62
+ }
63
+
64
+ String _fakeElement (String ? src) {
65
+ return """
66
+ <img src="$src " />
67
+ """ ;
68
+ }
69
+
70
+ @isTest
71
+ void testImgSrcMatcher (
72
+ String name,
73
+ CustomRenderMatcher matcher, {
74
+ required String ? imgSrc,
75
+ required bool shouldMatch,
76
+ }) {
77
+ testWidgets (name, (WidgetTester tester) async {
78
+ await tester.pumpWidget (
79
+ TestApp (
80
+ Html (
81
+ data: _fakeElement (imgSrc),
82
+ customRenders: {
83
+ matcher: CustomRender .widget (
84
+ widget: (RenderContext context, _) {
85
+ return Text ("Success" );
86
+ },
87
+ ),
88
+ },
89
+ ),
90
+ ),
91
+ );
92
+ await expectLater (find.text ("Success" ), shouldMatch ? findsOneWidget : findsNothing);
93
+ });
94
+ }
95
+
96
+ class TestApp extends StatelessWidget {
97
+ final Widget body;
98
+
99
+ TestApp (this .body);
100
+
101
+ @override
102
+ Widget build (BuildContext context) {
103
+ return MaterialApp (
104
+ home: Scaffold (
105
+ body: body,
106
+ appBar: AppBar (title: Text ('flutter_html' )),
107
+ ),
108
+ );
109
+ }
110
+ }
0 commit comments