1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/graphics/measuring.cpp
// Purpose: Tests for wxGraphicsRenderer::CreateMeasuringContext
// Author: Kevin Ollivier, Vadim Zeitlin (non wxGC parts)
// Created: 2008-02-12
// Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com>
// (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/font.h"
#include "wx/window.h"
#endif // WX_PRECOMP
// wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11
#if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__)
#include "wx/graphics.h"
#define TEST_GC
#endif
#include "wx/dcclient.h"
#include "wx/dcps.h"
#include "wx/metafile.h"
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class MeasuringTextTestCase : public CppUnit::TestCase
{
public:
MeasuringTextTestCase() { }
private:
CPPUNIT_TEST_SUITE( MeasuringTextTestCase );
CPPUNIT_TEST( DCGetTextExtent );
CPPUNIT_TEST( LeadingAndDescent );
CPPUNIT_TEST( WindowGetTextExtent );
CPPUNIT_TEST( GetPartialTextExtent );
#ifdef TEST_GC
CPPUNIT_TEST( GraphicsGetTextExtent );
#endif // TEST_GC
CPPUNIT_TEST_SUITE_END();
void DCGetTextExtent();
void LeadingAndDescent();
void WindowGetTextExtent();
void GetPartialTextExtent();
#ifdef TEST_GC
void GraphicsGetTextExtent();
#endif // TEST_GC
DECLARE_NO_COPY_CLASS(MeasuringTextTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" );
// ----------------------------------------------------------------------------
// helper for XXXTextExtent() methods
// ----------------------------------------------------------------------------
template <typename T>
struct GetTextExtentTester
{
// Constructor runs a couple of simple tests for GetTextExtent().
GetTextExtentTester(const T& obj)
{
// Test that getting the height only doesn't crash.
int y;
obj.GetTextExtent("H", NULL, &y);
CPPUNIT_ASSERT( y > 1 );
wxSize size = obj.GetTextExtent("Hello");
CPPUNIT_ASSERT( size.x > 1 );
CPPUNIT_ASSERT_EQUAL( y, size.y );
}
};
// ----------------------------------------------------------------------------
// tests themselves
// ----------------------------------------------------------------------------
void MeasuringTextTestCase::DCGetTextExtent()
{
wxClientDC dc(wxTheApp->GetTopWindow());
GetTextExtentTester<wxClientDC> testDC(dc);
int w;
dc.GetMultiLineTextExtent("Good\nbye", &w, NULL);
const wxSize sz = dc.GetTextExtent("Good");
CPPUNIT_ASSERT_EQUAL( sz.x, w );
CPPUNIT_ASSERT( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y );
// Test the functions with some other DC kinds also.
#if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
wxPostScriptDC psdc;
// wxPostScriptDC doesn't have any font set by default but its
// GetTextExtent() requires one to be set. This is probably a bug and we
// should set the default font in it implicitly but for now just work
// around it.
psdc.SetFont(*wxNORMAL_FONT);
GetTextExtentTester<wxPostScriptDC> testPS(psdc);
#endif
#if wxUSE_ENH_METAFILE
wxEnhMetaFileDC metadc;
GetTextExtentTester<wxEnhMetaFileDC> testMF(metadc);
#endif
}
void MeasuringTextTestCase::LeadingAndDescent()
{
wxClientDC dc(wxTheApp->GetTopWindow());
// Retrieving just the descent should work.
int descent = -17;
dc.GetTextExtent("foo", NULL, NULL, &descent);
CPPUNIT_ASSERT( descent != -17 );
// Same for external leading.
int leading = -289;
dc.GetTextExtent("foo", NULL, NULL, NULL, &leading);
CPPUNIT_ASSERT( leading != -289 );
// And both should also work for the empty string as they retrieve the
// values valid for the entire font and not just this string.
int descent2,
leading2;
dc.GetTextExtent("", NULL, NULL, &descent2, &leading2);
CPPUNIT_ASSERT_EQUAL( descent, descent2 );
CPPUNIT_ASSERT_EQUAL( leading, leading2 );
}
void MeasuringTextTestCase::WindowGetTextExtent()
{
wxWindow* const win = wxTheApp->GetTopWindow();
GetTextExtentTester<wxWindow> testWin(*win);
}
void MeasuringTextTestCase::GetPartialTextExtent()
{
wxClientDC dc(wxTheApp->GetTopWindow());
wxArrayInt widths;
CPPUNIT_ASSERT( dc.GetPartialTextExtents("Hello", widths) );
CPPUNIT_ASSERT_EQUAL( 5, widths.size() );
CPPUNIT_ASSERT_EQUAL( widths[0], dc.GetTextExtent("H").x );
CPPUNIT_ASSERT_EQUAL( widths[4], dc.GetTextExtent("Hello").x );
}
#ifdef TEST_GC
void MeasuringTextTestCase::GraphicsGetTextExtent()
{
wxGraphicsRenderer* renderer = wxGraphicsRenderer::GetDefaultRenderer();
CPPUNIT_ASSERT(renderer);
wxGraphicsContext* context = renderer->CreateMeasuringContext();
CPPUNIT_ASSERT(context);
wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
CPPUNIT_ASSERT(font.IsOk());
context->SetFont(font, *wxBLACK);
double width, height, descent, externalLeading = 0.0;
context->GetTextExtent("x", &width, &height, &descent, &externalLeading);
// TODO: Determine a way to make these tests more robust.
CPPUNIT_ASSERT(width > 0.0);
CPPUNIT_ASSERT(height > 0.0);
}
#endif // TEST_GC
|