-
Notifications
You must be signed in to change notification settings - Fork 29.4k
Description
Is there an existing issue for this?
- I have searched the existing issues
- I have read the guide to filing a bug
Use case
I've been developing a text widget package and found it really hard to test InlineSpans.
- Finders are only for widgets, so we need to get a TextSpan from the Text widget and call
visitChildren()on it to find children. - There is no way to get the position of an InlineSpan. A workaround is to get the center position of text or a position where a target span is supposed to be located.
Here is an example of the second bullet point:
flutter/packages/flutter/test/painting/text_span_test.dart
Lines 335 to 351 in ca384b8
| child: Center( | |
| child: Text.rich( | |
| TextSpan( | |
| text: 'xxxxx', | |
| children: <InlineSpan>[ | |
| TextSpan( | |
| text: 'yyyyy', | |
| mouseCursor: SystemMouseCursors.forbidden, | |
| ), | |
| TextSpan( | |
| text: 'xxxxx', | |
| ), | |
| ], | |
| ), | |
| textAlign: TextAlign.center, | |
| ), | |
| ), |
flutter/packages/flutter/test/painting/text_span_test.dart
Lines 355 to 365 in ca384b8
| final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); | |
| await gesture.addPointer(); | |
| await gesture.moveTo(tester.getCenter(find.byType(RichText)) - const Offset(40, 0)); | |
| expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); | |
| await gesture.moveTo(tester.getCenter(find.byType(RichText))); | |
| expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.forbidden); | |
| await gesture.moveTo(tester.getCenter(find.byType(RichText)) + const Offset(40, 0)); | |
| expect(RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1), SystemMouseCursors.basic); |
In this test, the TextSpan with "yyyyy" is definitely in the center because it has been placed between the other two spans, both with the same text "xxxxx". It is necessary to add or subtract some magic number (which is the number of pixels you guess from the width of strings) to move the mouse pointer to a span before or after the one in the center. It is possible but becomes much more difficult when you want to test more complex InlineSpans.
Proposal
It would be great if Flutter could provide some ways to test InlineSpans, similar to how we do when we test widgets. If it is not feasible, it'd still be better to at least have a way to get the position of an InlineSpan.