How can I test a TextPainter?

802 views Asked by At

I've written a class to handle custom text-wrapping but I'm not sure how to test it. I'm trying to use Flutter's testWidgets method to create an instance of my outer class, pass it some text, and then evaluate the results. There's a public class PrecisionTextOverflow whose build method is like so. The _PrecisionTextPainter handles the operation of parsing and painting the text.:

Widget build(BuildContext context) {
    return new Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        new CustomPaint(
          painter: new _PrecisionTextPainter(
              mainText,
              mainTextStyle,
              mainAlignment,
              secondaryText,
              secondaryTextStyle,
              secondaryAlignment,
              lineWidth,
              linesToWrap),
        )
      ],
    );
  }

Since _PrecisionTextPainter uses TextPainters rather than Text widgets to paint the actual text it seems like the Finder "find text" method won't work for me.

I need a way to test what it is my _PrecisionTextPainter has painted. How can I access its fields or the resulting painted text?

1

There are 1 answers

2
Ian Hickson On BEST ANSWER

We unfortunately don't have a good way to test painting currently.

It's high on our list of things we want to do. You can track progress here: https://github.com/flutter/flutter/issues/3227