Mocking JUCE graphics for unit testing?

#1

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()?

Thanks!

0 Likes

#2

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…

0 Likes

#3

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

0 Likes

#4

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

0 Likes

#5

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

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

0 Likes