How to test a method that draws to the canvas?

MWB :

How do I test a method that draws something to the canvas. For example, for an android app, I have the following code:

@Override
public void draw(Canvas canvas) {
    canvas.drawRect(rect, paint);
}

This of course is a very simple example, but still: how would I test that the correct Rect has been drawn? In other words, how to check if the UI is displaying the correct things.

I suppose I could check all my pixels on my screen to see if they have the right properties, but I guess that is a very naive approach :)

GhostCat salutes Monica C. :

Basically you don't test functionality that you didn't implement yourself in unit tests.

You write unit tests to ensure that the expected calls happen. And that the expected parameters are passed down to such library methods.

Then, later on, you might do manual testing of end to end functionality.

Edit: it really depends on your goal. If you want to get to maximum code respectively "feature" coverage with unit tests, then your whole design needs to be prepared for that.

Sure, sometimes it is easy to use the actual functionality behind that "library call" (when you can test the results easily). But having a real canvas, and doing "real drawing" probably requires you to run quite a bit of "environment" for any kind of testing. So, I personally would try to isolate all my logic, so it becomes easy to test, and then make sure that I have efficient means to later test the end result.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130698&siteId=1