Using colors

Using the Java Color class for specifying colors can be too tedious for testing purposes. When writing a test that checks that a given table cell is red, we do not always want to specify the exact color (#FF0000 in RGB), we just want to say "red" and have our test run even if the color chosen by the developers is #DD1111.

To this end, UISpec4J proposes three different means for checking a color:

  • Use a "user-friendly" color name such as "red" or "blue". In this case the tool will check that the color is a shade of red or blue. The authorized names are those of the predefined Color objects defined in the Java Color class.
  • Use a specific color value such as "FF0021". In this case the tool will check the exact color.
  • Use a Color object such as Color.red or a custom made Color. This is especially useful in Unit tests, when we want to use a static Color instance used both by the production code and the test. In this case the tool will also check the exact color.

This is why all color-related methods such as Table.backgroundEquals use Object arguments - you can provide them either String or Color objects.

Behind the scenes, UISpec4J basically computes a distance between expected and actual colors by converting them in the HSB (Hue/Saturation/Brightness) color space. Results may not always be accurate since colors can be subject to one's judgement, but in such cases you can always stick to more formal definitions - although as a result your tests will be more sensible to little cosmetic changes.