]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/font/fonttest.cpp
Support using GetTextExtent() with empty string to get descent in wxOSX.
[wxWidgets.git] / tests / font / fonttest.cpp
index 5f2c171c7f783633083f171758362c24895514de..bf1940320700a4179c80e6a052edcc6f13c52a44 100644 (file)
@@ -26,6 +26,8 @@
 
 #include "wx/font.h"
 
 
 #include "wx/font.h"
 
+#include "asserthelper.h"
+
 // ----------------------------------------------------------------------------
 // test class
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // test class
 // ----------------------------------------------------------------------------
@@ -37,11 +39,13 @@ public:
 
 private:
     CPPUNIT_TEST_SUITE( FontTestCase );
 
 private:
     CPPUNIT_TEST_SUITE( FontTestCase );
+        CPPUNIT_TEST( Construct );
         CPPUNIT_TEST( GetSet );
         CPPUNIT_TEST( NativeFontInfo );
         CPPUNIT_TEST( NativeFontInfoUserDesc );
     CPPUNIT_TEST_SUITE_END();
 
         CPPUNIT_TEST( GetSet );
         CPPUNIT_TEST( NativeFontInfo );
         CPPUNIT_TEST( NativeFontInfoUserDesc );
     CPPUNIT_TEST_SUITE_END();
 
+    void Construct();
     void GetSet();
     void NativeFontInfo();
     void NativeFontInfoUserDesc();
     void GetSet();
     void NativeFontInfo();
     void NativeFontInfoUserDesc();
@@ -68,7 +72,7 @@ private:
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase );
 
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( FontTestCase );
 
-// 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( FontTestCase, "FontTestCase" );
 
 wxString DumpFont(const wxFont *font)
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( FontTestCase, "FontTestCase" );
 
 wxString DumpFont(const wxFont *font)
@@ -93,11 +97,28 @@ wxString DumpFont(const wxFont *font)
     return s;
 }
 
     return s;
 }
 
+void FontTestCase::Construct()
+{
+    // The main purpose of this test is to verify that the font ctors below
+    // compile because it's easy to introduce ambiguities due to the number of
+    // overloaded wxFont ctors.
+
+    CPPUNIT_ASSERT( wxFont(10, wxFONTFAMILY_DEFAULT,
+                               wxFONTSTYLE_NORMAL,
+                               wxFONTWEIGHT_NORMAL).IsOk() );
+
+#if FUTURE_WXWIN_COMPATIBILITY_3_0
+    // Tests relying on the soon-to-be-deprecated ctor taking ints and not
+    // wxFontXXX enum elements.
+    CPPUNIT_ASSERT( wxFont(10, wxDEFAULT, wxNORMAL, wxNORMAL).IsOk() );
+#endif // FUTURE_WXWIN_COMPATIBILITY_3_0
+}
+
 void FontTestCase::GetSet()
 {
     unsigned numFonts;
     const wxFont *pf = GetTestFonts(numFonts);
 void FontTestCase::GetSet()
 {
     unsigned numFonts;
     const wxFont *pf = GetTestFonts(numFonts);
-    for ( size_t n = 0; n < numFonts; n++ )
+    for ( unsigned n = 0; n < numFonts; n++ )
     {
         wxFont test(*pf++);
 
     {
         wxFont test(*pf++);
 
@@ -114,10 +135,16 @@ void FontTestCase::GetSet()
 #if defined(__WXMSW__) || defined(__WXOSX__)
         static const char *knownGoodFaceName = "Arial";
 #else
 #if defined(__WXMSW__) || defined(__WXOSX__)
         static const char *knownGoodFaceName = "Arial";
 #else
-        static const char *knownGoodFaceName = "Fixed";
+        static const char *knownGoodFaceName = "Monospace";
 #endif
 
 #endif
 
-        CPPUNIT_ASSERT( test.SetFaceName(knownGoodFaceName) );
+        WX_ASSERT_MESSAGE
+        (
+            ("failed to set face name \"%s\" for test font #%u\n"
+             "(this failure is harmless if this face name is not "
+             "available on this system)", knownGoodFaceName, n),
+            test.SetFaceName(knownGoodFaceName)
+        );
         CPPUNIT_ASSERT( test.IsOk() );
 
 
         CPPUNIT_ASSERT( test.IsOk() );
 
 
@@ -125,10 +152,13 @@ void FontTestCase::GetSet()
 
         test.SetFamily( wxFONTFAMILY_ROMAN );
         CPPUNIT_ASSERT( test.IsOk() );
 
         test.SetFamily( wxFONTFAMILY_ROMAN );
         CPPUNIT_ASSERT( test.IsOk() );
-        CPPUNIT_ASSERT( wxFONTFAMILY_ROMAN == test.GetFamily() ||
-                        wxFONTFAMILY_UNKNOWN == test.GetFamily() );
-            // note that there is always the possibility that GetFamily() returns
-            // wxFONTFAMILY_UNKNOWN so that we consider it as a valid return value
+
+        // note that there is always the possibility that GetFamily() returns
+        // wxFONTFAMILY_DEFAULT (meaning "unknown" in this case) so that we
+        // consider it as a valid return value
+        const wxFontFamily family = test.GetFamily();
+        if ( family != wxFONTFAMILY_DEFAULT )
+            CPPUNIT_ASSERT_EQUAL( wxFONTFAMILY_ROMAN, family );
 
 
         // test Get/SetEncoding()
 
 
         // test Get/SetEncoding()
@@ -173,6 +203,12 @@ void FontTestCase::GetSet()
         CPPUNIT_ASSERT( test.IsOk() );
         CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() );
 
         CPPUNIT_ASSERT( test.IsOk() );
         CPPUNIT_ASSERT_EQUAL( true, test.GetUnderlined() );
 
+        // test Get/SetStrikethrough()
+
+        test.SetStrikethrough(true);
+        CPPUNIT_ASSERT( test.IsOk() );
+        CPPUNIT_ASSERT_EQUAL( true, test.GetStrikethrough() );
+
 
         // test Get/SetWeight()
 
 
         // test Get/SetWeight()
 
@@ -206,7 +242,34 @@ void FontTestCase::NativeFontInfo()
     // test that clearly invalid font info strings do not work
     wxFont font;
     CPPUNIT_ASSERT( !font.SetNativeFontInfo("") );
     // test that clearly invalid font info strings do not work
     wxFont font;
     CPPUNIT_ASSERT( !font.SetNativeFontInfo("") );
+
+    // pango_font_description_from_string() used by wxFont in wxGTK and wxX11
+    // never returns an error at all so this assertion fails there -- and as it
+    // doesn't seem to be possible to do anything about it maybe we should
+    // change wxMSW and other ports to also accept any strings?
+#if !defined(__WXGTK__) && !defined(__WXX11__)
     CPPUNIT_ASSERT( !font.SetNativeFontInfo("bloordyblop") );
     CPPUNIT_ASSERT( !font.SetNativeFontInfo("bloordyblop") );
+#endif
+
+    // Pango font description doesn't have 'underlined' and 'strikethrough'
+    // attributes, so wxNativeFontInfo implements these itself. Test if these
+    // are properly preserved by wxNativeFontInfo or its string description.
+    font.SetUnderlined(true);
+    font.SetStrikethrough(true);
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(font));
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(*font.GetNativeFontInfo()));
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(font.GetNativeFontInfoDesc()));
+    font.SetUnderlined(false);
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(font));
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(*font.GetNativeFontInfo()));
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(font.GetNativeFontInfoDesc()));
+    font.SetUnderlined(true);
+    font.SetStrikethrough(false);
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(font));
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(*font.GetNativeFontInfo()));
+    CPPUNIT_ASSERT_EQUAL(font, wxFont(font.GetNativeFontInfoDesc()));
+    // note: the GetNativeFontInfoUserDesc() doesn't preserve all attributes
+    // according to docs, so it is not tested.
 }
 
 void FontTestCase::NativeFontInfoUserDesc()
 }
 
 void FontTestCase::NativeFontInfoUserDesc()