|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/gestures.dart'; |
| 6 | +import 'package:flutter/widgets.dart'; |
| 7 | +import 'package:flutter_markdown/flutter_markdown.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | + |
| 10 | +import 'utils.dart'; |
| 11 | + |
| 12 | +void main() => defineTests(); |
| 13 | + |
| 14 | +void defineTests() { |
| 15 | + group( |
| 16 | + 'structure', |
| 17 | + () { |
| 18 | + testWidgets( |
| 19 | + 'footnote is detected and handle correctly', |
| 20 | + (WidgetTester tester) async { |
| 21 | + const String data = 'Foo[^a]\n[^a]: Bar'; |
| 22 | + await tester.pumpWidget( |
| 23 | + boilerplate( |
| 24 | + const MarkdownBody( |
| 25 | + data: data, |
| 26 | + ), |
| 27 | + ), |
| 28 | + ); |
| 29 | + |
| 30 | + final Iterable<Widget> widgets = tester.allWidgets; |
| 31 | + expectTextStrings(widgets, <String>[ |
| 32 | + 'Foo1', |
| 33 | + '1.', |
| 34 | + 'Bar ↩', |
| 35 | + ]); |
| 36 | + }, |
| 37 | + ); |
| 38 | + |
| 39 | + testWidgets( |
| 40 | + 'footnote is detected and handle correctly for selectable markdown', |
| 41 | + (WidgetTester tester) async { |
| 42 | + const String data = 'Foo[^a]\n[^a]: Bar'; |
| 43 | + await tester.pumpWidget( |
| 44 | + boilerplate( |
| 45 | + const MarkdownBody( |
| 46 | + data: data, |
| 47 | + selectable: true, |
| 48 | + ), |
| 49 | + ), |
| 50 | + ); |
| 51 | + |
| 52 | + final Iterable<Widget> widgets = tester.allWidgets; |
| 53 | + expectTextStrings(widgets, <String>[ |
| 54 | + 'Foo1', |
| 55 | + '1.', |
| 56 | + 'Bar ↩', |
| 57 | + ]); |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + testWidgets( |
| 62 | + 'ignore footnotes without description', |
| 63 | + (WidgetTester tester) async { |
| 64 | + const String data = 'Foo[^1] Bar[^2]\n[^1]: Bar'; |
| 65 | + await tester.pumpWidget( |
| 66 | + boilerplate( |
| 67 | + const MarkdownBody( |
| 68 | + data: data, |
| 69 | + ), |
| 70 | + ), |
| 71 | + ); |
| 72 | + |
| 73 | + final Iterable<Widget> widgets = tester.allWidgets; |
| 74 | + expectTextStrings(widgets, <String>[ |
| 75 | + 'Foo1 Bar[^2]', |
| 76 | + '1.', |
| 77 | + 'Bar ↩', |
| 78 | + ]); |
| 79 | + }, |
| 80 | + ); |
| 81 | + testWidgets( |
| 82 | + 'ignore superscripts and footnotes order', |
| 83 | + (WidgetTester tester) async { |
| 84 | + const String data = '[^2]: Bar \n [^1]: Foo \n Foo[^f] Bar[^b]'; |
| 85 | + await tester.pumpWidget( |
| 86 | + boilerplate( |
| 87 | + const MarkdownBody( |
| 88 | + data: data, |
| 89 | + ), |
| 90 | + ), |
| 91 | + ); |
| 92 | + |
| 93 | + final Iterable<Widget> widgets = tester.allWidgets; |
| 94 | + expectTextStrings(widgets, <String>[ |
| 95 | + 'Foo1 Bar2', |
| 96 | + '1.', |
| 97 | + 'Foo ↩', |
| 98 | + '2.', |
| 99 | + 'Bar ↩', |
| 100 | + ]); |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + testWidgets( |
| 105 | + 'handle two digits superscript', |
| 106 | + (WidgetTester tester) async { |
| 107 | + const String data = ''' |
| 108 | +1[^1] 2[^2] 3[^3] 4[^4] 5[^5] 6[^6] 7[^7] 8[^8] 9[^9] 10[^10] |
| 109 | +[^1]:1 |
| 110 | +[^2]:2 |
| 111 | +[^3]:3 |
| 112 | +[^4]:4 |
| 113 | +[^5]:5 |
| 114 | +[^6]:6 |
| 115 | +[^7]:7 |
| 116 | +[^8]:8 |
| 117 | +[^9]:9 |
| 118 | +[^10]:10 |
| 119 | +'''; |
| 120 | + await tester.pumpWidget( |
| 121 | + boilerplate( |
| 122 | + const MarkdownBody( |
| 123 | + data: data, |
| 124 | + ), |
| 125 | + ), |
| 126 | + ); |
| 127 | + |
| 128 | + final Iterable<Widget> widgets = tester.allWidgets; |
| 129 | + expectTextStrings(widgets, <String>[ |
| 130 | + '11 22 33 44 55 66 77 88 99 1010', |
| 131 | + '1.', |
| 132 | + '1 ↩', |
| 133 | + '2.', |
| 134 | + '2 ↩', |
| 135 | + '3.', |
| 136 | + '3 ↩', |
| 137 | + '4.', |
| 138 | + '4 ↩', |
| 139 | + '5.', |
| 140 | + '5 ↩', |
| 141 | + '6.', |
| 142 | + '6 ↩', |
| 143 | + '7.', |
| 144 | + '7 ↩', |
| 145 | + '8.', |
| 146 | + '8 ↩', |
| 147 | + '9.', |
| 148 | + '9 ↩', |
| 149 | + '10.', |
| 150 | + '10 ↩', |
| 151 | + ]); |
| 152 | + }, |
| 153 | + ); |
| 154 | + }, |
| 155 | + ); |
| 156 | + |
| 157 | + group( |
| 158 | + 'superscript textstyle replacing', |
| 159 | + () { |
| 160 | + testWidgets( |
| 161 | + 'superscript has correct fontfeature', |
| 162 | + (WidgetTester tester) async { |
| 163 | + const String data = 'Foo[^a]\n[^a]: Bar'; |
| 164 | + await tester.pumpWidget( |
| 165 | + boilerplate( |
| 166 | + const MarkdownBody( |
| 167 | + data: data, |
| 168 | + ), |
| 169 | + ), |
| 170 | + ); |
| 171 | + |
| 172 | + final Iterable<Widget> widgets = tester.allWidgets; |
| 173 | + final RichText richText = widgets |
| 174 | + .firstWhere((Widget widget) => widget is RichText) as RichText; |
| 175 | + |
| 176 | + final TextSpan span = richText.text as TextSpan; |
| 177 | + final List<InlineSpan>? children = span.children; |
| 178 | + |
| 179 | + expect(children, isNotNull); |
| 180 | + expect(children!.length, 2); |
| 181 | + expect(children[1].style, isNotNull); |
| 182 | + expect(children[1].style!.fontFeatures?.length, 1); |
| 183 | + expect(children[1].style!.fontFeatures?.first.feature, 'sups'); |
| 184 | + }, |
| 185 | + ); |
| 186 | + |
| 187 | + testWidgets( |
| 188 | + 'superscript index has the same font style like text', |
| 189 | + (WidgetTester tester) async { |
| 190 | + const String data = '# Foo[^a]\n[^a]: Bar'; |
| 191 | + await tester.pumpWidget( |
| 192 | + boilerplate( |
| 193 | + const MarkdownBody( |
| 194 | + data: data, |
| 195 | + ), |
| 196 | + ), |
| 197 | + ); |
| 198 | + |
| 199 | + final Iterable<Widget> widgets = tester.allWidgets; |
| 200 | + final RichText richText = widgets |
| 201 | + .firstWhere((Widget widget) => widget is RichText) as RichText; |
| 202 | + |
| 203 | + final TextSpan span = richText.text as TextSpan; |
| 204 | + final List<InlineSpan>? children = span.children; |
| 205 | + |
| 206 | + expect(children![0].style, isNotNull); |
| 207 | + expect(children[1].style!.fontSize, children[0].style!.fontSize); |
| 208 | + expect(children[1].style!.fontFamily, children[0].style!.fontFamily); |
| 209 | + expect(children[1].style!.fontStyle, children[0].style!.fontStyle); |
| 210 | + expect(children[1].style!.fontSize, children[0].style!.fontSize); |
| 211 | + }, |
| 212 | + ); |
| 213 | + |
| 214 | + testWidgets( |
| 215 | + 'link is correctly copied to new superscript index', |
| 216 | + (WidgetTester tester) async { |
| 217 | + final List<MarkdownLink> linkTapResults = <MarkdownLink>[]; |
| 218 | + const String data = 'Foo[^a]\n[^a]: Bar'; |
| 219 | + await tester.pumpWidget( |
| 220 | + boilerplate( |
| 221 | + MarkdownBody( |
| 222 | + data: data, |
| 223 | + onTapLink: (String text, String? href, String title) => |
| 224 | + linkTapResults.add(MarkdownLink(text, href, title)), |
| 225 | + ), |
| 226 | + ), |
| 227 | + ); |
| 228 | + |
| 229 | + final Iterable<Widget> widgets = tester.allWidgets; |
| 230 | + final RichText richText = widgets |
| 231 | + .firstWhere((Widget widget) => widget is RichText) as RichText; |
| 232 | + |
| 233 | + final TextSpan span = richText.text as TextSpan; |
| 234 | + |
| 235 | + final List<Type> gestureRecognizerTypes = <Type>[]; |
| 236 | + span.visitChildren((InlineSpan inlineSpan) { |
| 237 | + if (inlineSpan is TextSpan) { |
| 238 | + final TapGestureRecognizer? recognizer = |
| 239 | + inlineSpan.recognizer as TapGestureRecognizer?; |
| 240 | + gestureRecognizerTypes.add(recognizer?.runtimeType ?? Null); |
| 241 | + if (recognizer != null) { |
| 242 | + recognizer.onTap!(); |
| 243 | + } |
| 244 | + } |
| 245 | + return true; |
| 246 | + }); |
| 247 | + |
| 248 | + expect(span.children!.length, 2); |
| 249 | + expect( |
| 250 | + gestureRecognizerTypes, |
| 251 | + orderedEquals(<Type>[Null, TapGestureRecognizer]), |
| 252 | + ); |
| 253 | + expectLinkTap(linkTapResults[0], const MarkdownLink('1', '#fn-a')); |
| 254 | + }, |
| 255 | + ); |
| 256 | + }, |
| 257 | + ); |
| 258 | +} |
0 commit comments