0

I have custom QWidgets where all the painting is done manually (fillRect, drawRect, drawText, drawPixmap..) . There are up to 120 properties affecting what's going to be paint, how it's blinking and so on. Legacy spaghetti code.

When I'm changing something I would like to first test cover old functionality. Is there a recommended way how to cover QWidget::paintEvent(QPaintEvent* event) with unit tests? Do I have to mock QPainter, or is there a way for checking paintEvent output?

How you are covering paintEvetnt with tests?

Libor Tomsik
  • 668
  • 7
  • 24
  • You might take screenshots of your widget and compare them. IMO, it's the best and most precise way to test how a thing is rendered. – vahancho May 22 '19 at 08:07
  • @vahancho Is there a standard way doing so in QTestlib? Or pixel by pixel comparison by own method (or external library)? – Libor Tomsik May 22 '19 at 08:21
  • 2
    It's not that difficult as it seems. Store the baseline image of your current widget. When you change something, take a screenshot of the widget, e.g. with `QPixmap::grabWidget()` function. Convert pixmap to a `QImage` and compare it with the baseline image: `QImage::operator!=()`. This will only tell you whether they are different or not. In order to see the differences you need to write your own function of use some tool. – vahancho May 22 '19 at 08:28
  • unit testing UI is usually waste of time and pain in the .. . If you can extract widget logic this should be tested. – Marek R May 22 '19 at 09:46

1 Answers1

1

Finally storing and comparing images like:

auto configuredImage = widgetUnderTest->grab().toImage();
QImage sampleImage;      
sampleImage.load("src/Images/configuredImage.png");
QCOMPARE(configuredImage, sampleImage);
Libor Tomsik
  • 668
  • 7
  • 24