1

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?

mbt
  • 155
  • 10
  • I don’t understand what you are trying to test. There is no point testing drawing commands; we know what they do. And who will reference the reference image in any case? In iOS you might use a UI test to take a screenshot and look to see if the interface is “right” using your eyes and brain, I suppose. But basically I’m not grasping the point of the test. – matt Nov 05 '19 at 14:44
  • Obviously I'm not trying to test the Cocoa drawing commands theirselves, I want to check whether my code produces the right images in different cases. It's a snapshot, nothing special, but I can't do it in NS* world because of the two problems I exposed. – mbt Nov 05 '19 at 21:38
  • NSBitmapImageRep has colorSpaceName in it's initializer (draw to it's buffer) + one can use NSImage imageWithSize:flipped:drawingHandler: which can easily create all variations (1x, 2x, 3x). Maybe this can help with research. If your app doesn't need color profiles than your code should compare histogram or something from cifilter. – Marek H Nov 06 '19 at 09:11

0 Answers0