mardi 5 novembre 2019

How to make robust tests of bitmaps made by NS* Cocoa classes?

I am writing some tests for the Swift code of my Mac application.

One of the cases I need to test is:

  1. have a NSAttributedString with attachments

  2. each attachment is made by a bitmap (1x or 2x), colorised by the code with a custom color

  3. draw the attributed string and verify that the output is identical to a reference image

Another test case could be:

  1. have a NSView

  2. draw some stuff in the view

  3. verify that the bitmap of the view is identical to a reference image

There are 2 problems:

  1. Depending on the Mac hardware that is running the tests, Cocoa will work with 1x or 2x images. This forces to have 2 reference images for each test case, that is obviously an impractical solution.

  2. Again depending on the Mac hardware, the generated NSImage objects will take the color profile of the screen. This makes the tests fail if they run on another Mac, or on the same Mac but attached to a different display.

I tried to go deeper and use CG* functions to do my drawings, providing the size of the images and the color space. Tests written this way are stable, but I can't replace all the NS* code with CG*, because that would mean not testing the integration with NS* classes, that, at the end, is what I need to test.

It would be nice to run the tests in an environment that has a virtual screen, so that NS* classes will behave always in the same way, but I can't find if and how it is possible.

So, the final question is: how is it possible to test code that involves NS* classes and that produces bitmaps?

Aucun commentaire:

Enregistrer un commentaire