]> git.saurik.com Git - wxWidgets.git/blob - tests/graphics/colour.cpp
ae603a701d42f11e8c0e0cafd744b94624a15c2b
[wxWidgets.git] / tests / graphics / colour.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/colour.cpp
3 // Purpose: wxColour unit test
4 // Author: Vadim Zeitlin
5 // Created: 2009-09-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/colour.h"
21 #include "asserthelper.h"
22
23 // ----------------------------------------------------------------------------
24 // helpers
25 // ----------------------------------------------------------------------------
26
27 // Check the colour components, with and without alpha.
28 //
29 // NB: These are macros and not functions to have correct line numbers in case
30 // of failure.
31 #define ASSERT_EQUAL_RGB(c, r, g, b) \
32 CPPUNIT_ASSERT_EQUAL( r, (int)c.Red() ); \
33 CPPUNIT_ASSERT_EQUAL( g, (int)c.Green() ); \
34 CPPUNIT_ASSERT_EQUAL( b, (int)c.Blue() )
35
36 #define ASSERT_EQUAL_RGBA(c, r, g, b, a) \
37 ASSERT_EQUAL_RGB(c, r, g, b); \
38 CPPUNIT_ASSERT_EQUAL( a, (int)c.Alpha() )
39
40 // ----------------------------------------------------------------------------
41 // test class
42 // ----------------------------------------------------------------------------
43
44 class ColourTestCase : public CppUnit::TestCase
45 {
46 public:
47 ColourTestCase() { }
48
49 private:
50 CPPUNIT_TEST_SUITE( ColourTestCase );
51 CPPUNIT_TEST( GetSetRGB );
52 CPPUNIT_TEST( FromString );
53 CPPUNIT_TEST_SUITE_END();
54
55 void GetSetRGB();
56 void FromString();
57
58 DECLARE_NO_COPY_CLASS(ColourTestCase)
59 };
60
61 // register in the unnamed registry so that these tests are run by default
62 CPPUNIT_TEST_SUITE_REGISTRATION( ColourTestCase );
63
64 // also include in its own registry so that these tests can be run alone
65 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ColourTestCase, "ColourTestCase" );
66
67 void ColourTestCase::GetSetRGB()
68 {
69 wxColour c;
70 c.SetRGB(0x123456);
71
72 CPPUNIT_ASSERT_EQUAL( 0x56, (int)c.Red() );
73 CPPUNIT_ASSERT_EQUAL( 0x34, (int)c.Green() );
74 CPPUNIT_ASSERT_EQUAL( 0x12, (int)c.Blue() );
75 CPPUNIT_ASSERT_EQUAL( wxALPHA_OPAQUE, c.Alpha() );
76
77 CPPUNIT_ASSERT_EQUAL( wxColour(0x123456), c );
78 CPPUNIT_ASSERT_EQUAL( 0x123456, c.GetRGB() );
79
80 c.SetRGBA(0xaabbccdd);
81
82 CPPUNIT_ASSERT_EQUAL( 0xdd, (int)c.Red() );
83 CPPUNIT_ASSERT_EQUAL( 0xcc, (int)c.Green() );
84 CPPUNIT_ASSERT_EQUAL( 0xbb, (int)c.Blue() );
85
86 // wxX11 doesn't support alpha at all currently.
87 #ifndef __WXX11__
88 CPPUNIT_ASSERT_EQUAL( 0xaa, (int)c.Alpha() );
89 #endif // __WXX11__
90
91 // FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
92 // into account: bug or feature?
93 //CPPUNIT_ASSERT_EQUAL( wxColour(0xaabbccdd), c );
94 CPPUNIT_ASSERT_EQUAL( 0xbbccdd, c.GetRGB() );
95 #ifndef __WXX11__
96 CPPUNIT_ASSERT_EQUAL( 0xaabbccdd, c.GetRGBA() );
97 #endif // __WXX11__
98 }
99
100 void ColourTestCase::FromString()
101 {
102 ASSERT_EQUAL_RGB( wxColour("rgb(11, 22, 33)"), 11, 22, 33 );
103 ASSERT_EQUAL_RGBA( wxColour("rgba(11, 22, 33, 0.5)"), 11, 22, 33, 128 );
104 ASSERT_EQUAL_RGBA( wxColour("rgba( 11, 22, 33, 0.5 )"), 11, 22, 33, 128 );
105
106 ASSERT_EQUAL_RGB( wxColour("#aabbcc"), 0xaa, 0xbb, 0xcc );
107
108 ASSERT_EQUAL_RGB( wxColour("red"), 0xff, 0, 0 );
109
110 wxColour col;
111 CPPUNIT_ASSERT( !wxFromString("rgb(1, 2)", &col) );
112 CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456)", &col) );
113 CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456, foo)", &col) );
114 }
115