wxwidgets/tests/font/fonttest.cpp
Vadim Zeitlin 7cc016b5fa Remove CppUnit boilerplate from wxFont unit test
No real changes, just remove the test case class which is not at all
necessary in this unit test.
2018-09-06 03:13:23 +02:00

315 lines
10 KiB
C++

///////////////////////////////////////////////////////////////////////////////
// Name: tests/font/fonttest.cpp
// Purpose: wxFont unit test
// Author: Francesco Montorsi
// Created: 16.3.09
// Copyright: (c) 2009 Francesco Montorsi
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif // WX_PRECOMP
#include "wx/font.h"
#include "asserthelper.h"
// ----------------------------------------------------------------------------
// local helpers
// ----------------------------------------------------------------------------
// Returns a point to an array of fonts and fills the output parameter with the
// number of elements in this array.
static const wxFont *GetTestFonts(unsigned& numFonts)
{
static const wxFont testfonts[] =
{
*wxNORMAL_FONT,
*wxSMALL_FONT,
*wxITALIC_FONT,
*wxSWISS_FONT,
wxFont(5, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)
};
numFonts = WXSIZEOF(testfonts);
return testfonts;
}
wxString DumpFont(const wxFont *font)
{
// dumps the internal properties of a wxFont in the same order they
// are checked by wxFontBase::operator==()
wxASSERT(font->IsOk());
wxString s;
s.Printf(wxS("%d-%d;%d-%d-%d-%d-%d-%s-%d"),
font->GetPointSize(),
font->GetPixelSize().x,
font->GetPixelSize().y,
font->GetFamily(),
font->GetStyle(),
font->GetWeight(),
font->GetUnderlined() ? 1 : 0,
font->GetFaceName(),
font->GetEncoding());
return s;
}
// ----------------------------------------------------------------------------
// the tests
// ----------------------------------------------------------------------------
TEST_CASE("wxFont::Construct", "[font][ctor]")
{
// 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.
CHECK( wxFont(10, wxFONTFAMILY_DEFAULT,
wxFONTSTYLE_NORMAL,
wxFONTWEIGHT_NORMAL).IsOk() );
#if WXWIN_COMPATIBILITY_3_0
// Disable the warning about deprecated wxNORMAL as we use it here
// intentionally.
#ifdef __VISUALC__
#pragma warning(push)
#pragma warning(disable:4996)
#endif
wxGCC_WARNING_SUPPRESS(deprecated-declarations)
// Tests relying on the soon-to-be-deprecated ctor taking ints and not
// wxFontXXX enum elements.
CHECK( wxFont(10, wxDEFAULT, wxNORMAL, wxNORMAL).IsOk() );
wxGCC_WARNING_RESTORE(deprecated-declarations)
#ifdef __VISUALC__
#pragma warning(pop)
#endif
#endif // WXWIN_COMPATIBILITY_3_0
}
TEST_CASE("wxFont::GetSet", "[font][getters]")
{
unsigned numFonts;
const wxFont *pf = GetTestFonts(numFonts);
for ( unsigned n = 0; n < numFonts; n++ )
{
wxFont test(*pf++);
// remember: getters can only be called when wxFont::IsOk() == true
CHECK( test.IsOk() );
// test Get/SetFaceName()
CHECK( !test.SetFaceName("a dummy face name") );
CHECK( !test.IsOk() );
// if the call to SetFaceName() below fails on your system/port,
// consider adding another branch to this #if
#if defined(__WXMSW__) || defined(__WXOSX__)
static const char *knownGoodFaceName = "Arial";
#else
static const char *knownGoodFaceName = "Monospace";
#endif
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)
);
CHECK( test.IsOk() );
// test Get/SetFamily()
test.SetFamily( wxFONTFAMILY_ROMAN );
CHECK( test.IsOk() );
// 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 )
CHECK( wxFONTFAMILY_ROMAN == family );
// test Get/SetEncoding()
//test.SetEncoding( wxFONTENCODING_KOI8 );
//CHECK( test.IsOk() );
//CHECK( wxFONTENCODING_KOI8 == test.GetEncoding() );
// test Get/SetPointSize()
test.SetPointSize(30);
CHECK( test.IsOk() );
CHECK( 30 == test.GetPointSize() );
// test Get/SetPixelSize()
test.SetPixelSize(wxSize(0,30));
CHECK( test.IsOk() );
CHECK( test.GetPixelSize().GetHeight() <= 30 );
// NOTE: the match found by SetPixelSize() may be not 100% precise; it
// only grants that a font smaller than the required height will
// be selected
// test Get/SetStyle()
test.SetStyle(wxFONTSTYLE_SLANT);
CHECK( test.IsOk() );
#ifdef __WXMSW__
// on wxMSW wxFONTSTYLE_SLANT==wxFONTSTYLE_ITALIC, so accept the latter
// as a valid value too.
if ( test.GetStyle() != wxFONTSTYLE_ITALIC )
#endif
CHECK( wxFONTSTYLE_SLANT == test.GetStyle() );
// test Get/SetUnderlined()
test.SetUnderlined(true);
CHECK( test.IsOk() );
CHECK( test.GetUnderlined() );
const wxFont fontBase = test.GetBaseFont();
CHECK( fontBase.IsOk() );
CHECK( !fontBase.GetUnderlined() );
CHECK( !fontBase.GetStrikethrough() );
CHECK( wxFONTWEIGHT_NORMAL == fontBase.GetWeight() );
CHECK( wxFONTSTYLE_NORMAL == fontBase.GetStyle() );
// test Get/SetStrikethrough()
test.SetStrikethrough(true);
CHECK( test.IsOk() );
CHECK( test.GetStrikethrough() );
// test Get/SetWeight()
test.SetWeight(wxFONTWEIGHT_BOLD);
CHECK( test.IsOk() );
CHECK( wxFONTWEIGHT_BOLD == test.GetWeight() );
}
}
TEST_CASE("wxFont::NativeFontInfo", "[font][fontinfo]")
{
unsigned numFonts;
const wxFont *pf = GetTestFonts(numFonts);
for ( unsigned n = 0; n < numFonts; n++ )
{
wxFont test(*pf++);
const wxString& nid = test.GetNativeFontInfoDesc();
CHECK( !nid.empty() );
// documented to be never empty
wxFont temp;
CHECK( temp.SetNativeFontInfo(nid) );
CHECK( temp.IsOk() );
WX_ASSERT_MESSAGE(
("Test #%u failed\ndump of test font: \"%s\"\ndump of temp font: \"%s\"", \
n, DumpFont(&test), DumpFont(&temp)),
temp == test );
}
// test that clearly invalid font info strings do not work
wxFont font;
CHECK( !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__)
CHECK( !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);
CHECK(font == wxFont(font));
CHECK(font == wxFont(*font.GetNativeFontInfo()));
CHECK(font == wxFont(font.GetNativeFontInfoDesc()));
font.SetUnderlined(false);
CHECK(font == wxFont(font));
CHECK(font == wxFont(*font.GetNativeFontInfo()));
CHECK(font == wxFont(font.GetNativeFontInfoDesc()));
font.SetUnderlined(true);
font.SetStrikethrough(false);
CHECK(font == wxFont(font));
CHECK(font == wxFont(*font.GetNativeFontInfo()));
CHECK(font == wxFont(font.GetNativeFontInfoDesc()));
// note: the GetNativeFontInfoUserDesc() doesn't preserve all attributes
// according to docs, so it is not tested.
}
TEST_CASE("wxFont::NativeFontInfoUserDesc", "[font][fontinfo]")
{
unsigned numFonts;
const wxFont *pf = GetTestFonts(numFonts);
for ( unsigned n = 0; n < numFonts; n++ )
{
wxFont test(*pf++);
const wxString& niud = test.GetNativeFontInfoUserDesc();
CHECK( !niud.empty() );
// documented to be never empty
wxFont temp2;
CHECK( temp2.SetNativeFontInfoUserDesc(niud) );
CHECK( temp2.IsOk() );
#ifdef __WXGTK__
// Pango saves/restores all font info in the user-friendly string:
WX_ASSERT_MESSAGE(
("Test #%u failed; native info user desc was \"%s\" for test and \"%s\" for temp2", \
n, niud, temp2.GetNativeFontInfoUserDesc()),
temp2 == test );
#else
// NOTE: as documented GetNativeFontInfoUserDesc/SetNativeFontInfoUserDesc
// are not granted to save/restore all font info.
// In fact e.g. the font family is not saved at all; test only those
// info which GetNativeFontInfoUserDesc() does indeed save:
CHECK( test.GetWeight() == temp2.GetWeight() );
CHECK( test.GetStyle() == temp2.GetStyle() );
// if the original face name was empty, it means that any face name (in
// this family) can be used for the new font so we shouldn't be
// surprised to find that they differ in this case
const wxString facename = test.GetFaceName();
if ( !facename.empty() )
{
CHECK( facename.Upper() == temp2.GetFaceName().Upper() );
}
CHECK( test.GetPointSize() == temp2.GetPointSize() );
CHECK( test.GetEncoding() == temp2.GetEncoding() );
#endif
}
}