Mocking JUCE graphics for unit testing?

Let’s say I am writing custom JUCE components and I’d like to unit-test them. For example, I have a component that just draws a rectangle filled with a colour.

Now I’d like to write a unit test checking that it indeed draws a rectangle filled with a colour. How would I do this? Is there a way I can mock juce::Graphics and substitute it with a class that checks what’s being drawn by the component in paint()?


You can either call Image Component::createComponentSnapshot().

Or if you want to call paint yourself, you can create a Graphics, that draws into an image:

Image img (PixelFormat::RGBA, 800, 600, true);
Graphics g (img);
component->paint (g);

EDIT: or did you mean, how to compare the image with the expected image?
I am not aware of that kind of image processing being available in JUCE. Would be a nice addition though…

The interesting figures would be probably the RMS of pixels, number of not equal pixels…

Nice! Yes, calling paint() myself seems to be the easiest way. Why did I not think about this? Thanks!

You could roll your own LowLevelGraphicsContext instead of using an Image.

See this old thread - Where can I get the GUITest class?

Perhaps @gbevin has gotten closer to making that code available.