// Purpose: wxColour unit test
// Author: Vadim Zeitlin
// Created: 2009-09-19
-// RCS-ID: $Id$
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
#endif
#include "wx/colour.h"
+#include "asserthelper.h"
// ----------------------------------------------------------------------------
-// helper functions
+// helpers
// ----------------------------------------------------------------------------
-namespace
-{
- // by default colour components values are output incorrectly because they
- // are unsigned chars, define a small helper struct which formats them in
- // a more useful way
- struct ColourChannel
- {
- ColourChannel(unsigned char value) : m_value(value) { }
-
- unsigned char m_value;
- };
-
- std::ostream& operator<<(std::ostream& os, const ColourChannel& cc)
- {
- os.width(2);
- os.fill('0');
- os << static_cast<int>(cc.m_value);
- return os;
- }
-} // anonymous namespace
-
-// this operator is needed to use CPPUNIT_ASSERT_EQUAL with wxColour objects
-std::ostream& operator<<(std::ostream& os, const wxColour& c)
-{
- os << std::hex << std::noshowbase
- << "("
- << ColourChannel(c.Red()) << ", "
- << ColourChannel(c.Green()) << ", "
- << ColourChannel(c.Blue());
-
- if ( const unsigned char a = c.Alpha() )
- {
- os << ", " << ColourChannel(c.Alpha());
- }
+// Check the colour components, with and without alpha.
+//
+// NB: These are macros and not functions to have correct line numbers in case
+// of failure.
+#define ASSERT_EQUAL_RGB(c, r, g, b) \
+ CPPUNIT_ASSERT_EQUAL( r, (int)c.Red() ); \
+ CPPUNIT_ASSERT_EQUAL( g, (int)c.Green() ); \
+ CPPUNIT_ASSERT_EQUAL( b, (int)c.Blue() )
- os << ")";
-
- return os;
-}
+#define ASSERT_EQUAL_RGBA(c, r, g, b, a) \
+ ASSERT_EQUAL_RGB(c, r, g, b); \
+ CPPUNIT_ASSERT_EQUAL( a, (int)c.Alpha() )
// ----------------------------------------------------------------------------
// test class
private:
CPPUNIT_TEST_SUITE( ColourTestCase );
CPPUNIT_TEST( GetSetRGB );
+ CPPUNIT_TEST( FromString );
CPPUNIT_TEST_SUITE_END();
void GetSetRGB();
+ void FromString();
DECLARE_NO_COPY_CLASS(ColourTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( ColourTestCase );
-// also include in it's own registry so that these tests can be run alone
+// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ColourTestCase, "ColourTestCase" );
void ColourTestCase::GetSetRGB()
CPPUNIT_ASSERT_EQUAL( 0xdd, (int)c.Red() );
CPPUNIT_ASSERT_EQUAL( 0xcc, (int)c.Green() );
CPPUNIT_ASSERT_EQUAL( 0xbb, (int)c.Blue() );
+
+ // wxX11 doesn't support alpha at all currently.
+#ifndef __WXX11__
CPPUNIT_ASSERT_EQUAL( 0xaa, (int)c.Alpha() );
+#endif // __WXX11__
// FIXME: at least under wxGTK wxColour ctor doesn't take alpha channel
// into account: bug or feature?
//CPPUNIT_ASSERT_EQUAL( wxColour(0xaabbccdd), c );
CPPUNIT_ASSERT_EQUAL( 0xbbccdd, c.GetRGB() );
+#ifndef __WXX11__
CPPUNIT_ASSERT_EQUAL( 0xaabbccdd, c.GetRGBA() );
+#endif // __WXX11__
+}
+
+void ColourTestCase::FromString()
+{
+ ASSERT_EQUAL_RGB( wxColour("rgb(11, 22, 33)"), 11, 22, 33 );
+ ASSERT_EQUAL_RGBA( wxColour("rgba(11, 22, 33, 0.5)"), 11, 22, 33, 128 );
+ ASSERT_EQUAL_RGBA( wxColour("rgba( 11, 22, 33, 0.5 )"), 11, 22, 33, 128 );
+
+ ASSERT_EQUAL_RGB( wxColour("#aabbcc"), 0xaa, 0xbb, 0xcc );
+
+ ASSERT_EQUAL_RGB( wxColour("red"), 0xff, 0, 0 );
+
+ wxColour col;
+ CPPUNIT_ASSERT( !wxFromString("rgb(1, 2)", &col) );
+ CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456)", &col) );
+ CPPUNIT_ASSERT( !wxFromString("rgba(1, 2, 3.456, foo)", &col) );
}