2009-04-15 18:38:53 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2009-06-20 19:33:55 -04:00
|
|
|
// Name: tests/graphics/measuring.cpp
|
|
|
|
// Purpose: Tests for wxGraphicsRenderer::CreateMeasuringContext
|
2012-10-17 18:35:49 -04:00
|
|
|
// Author: Kevin Ollivier, Vadim Zeitlin (non wxGC parts)
|
2009-04-15 18:38:53 -04:00
|
|
|
// Created: 2008-02-12
|
2009-06-20 19:33:55 -04:00
|
|
|
// Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com>
|
2012-10-17 18:35:49 -04:00
|
|
|
// (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
|
2009-04-15 18:38:53 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// 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
|
|
|
|
|
2012-10-17 18:35:49 -04:00
|
|
|
// 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"
|
2012-10-27 08:27:56 -04:00
|
|
|
#include "wx/dcps.h"
|
|
|
|
#include "wx/metafile.h"
|
2012-10-17 18:35:49 -04:00
|
|
|
|
2009-04-15 18:38:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2012-10-17 18:35:49 -04:00
|
|
|
class MeasuringTextTestCase : public CppUnit::TestCase
|
2009-04-15 18:38:53 -04:00
|
|
|
{
|
|
|
|
public:
|
2012-10-17 18:35:49 -04:00
|
|
|
MeasuringTextTestCase() { }
|
2009-04-15 18:38:53 -04:00
|
|
|
|
|
|
|
private:
|
2012-10-17 18:35:49 -04:00
|
|
|
CPPUNIT_TEST_SUITE( MeasuringTextTestCase );
|
|
|
|
CPPUNIT_TEST( DCGetTextExtent );
|
2013-07-09 21:28:18 -04:00
|
|
|
CPPUNIT_TEST( LeadingAndDescent );
|
2012-10-17 18:35:49 -04:00
|
|
|
CPPUNIT_TEST( WindowGetTextExtent );
|
|
|
|
CPPUNIT_TEST( GetPartialTextExtent );
|
|
|
|
#ifdef TEST_GC
|
|
|
|
CPPUNIT_TEST( GraphicsGetTextExtent );
|
|
|
|
#endif // TEST_GC
|
2009-04-15 18:38:53 -04:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
2012-10-17 18:35:49 -04:00
|
|
|
void DCGetTextExtent();
|
2013-07-09 21:28:18 -04:00
|
|
|
void LeadingAndDescent();
|
2012-10-17 18:35:49 -04:00
|
|
|
void WindowGetTextExtent();
|
|
|
|
|
|
|
|
void GetPartialTextExtent();
|
|
|
|
|
|
|
|
#ifdef TEST_GC
|
|
|
|
void GraphicsGetTextExtent();
|
|
|
|
#endif // TEST_GC
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(MeasuringTextTestCase);
|
2009-04-15 18:38:53 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
2012-10-17 18:35:49 -04:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase );
|
2009-04-15 18:38:53 -04:00
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2012-10-17 18:35:49 -04:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" );
|
2009-04-15 18:38:53 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2012-11-12 10:50:13 -05:00
|
|
|
// helper for XXXTextExtent() methods
|
2009-04-15 18:38:53 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2012-10-17 18:35:49 -04:00
|
|
|
template <typename T>
|
2012-11-12 10:50:13 -05:00
|
|
|
struct GetTextExtentTester
|
2012-10-17 18:35:49 -04:00
|
|
|
{
|
2012-11-12 10:50:13 -05:00
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
};
|
2012-10-17 18:35:49 -04:00
|
|
|
|
2012-11-12 10:50:13 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// tests themselves
|
|
|
|
// ----------------------------------------------------------------------------
|
2012-10-17 18:35:49 -04:00
|
|
|
|
|
|
|
void MeasuringTextTestCase::DCGetTextExtent()
|
|
|
|
{
|
|
|
|
wxClientDC dc(wxTheApp->GetTopWindow());
|
|
|
|
|
2012-11-12 10:50:13 -05:00
|
|
|
GetTextExtentTester<wxClientDC> testDC(dc);
|
2012-10-17 19:06:07 -04:00
|
|
|
|
|
|
|
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 );
|
2012-10-27 08:27:56 -04:00
|
|
|
|
|
|
|
// 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);
|
2012-11-12 10:50:13 -05:00
|
|
|
GetTextExtentTester<wxPostScriptDC> testPS(psdc);
|
2012-10-27 08:27:56 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if wxUSE_ENH_METAFILE
|
|
|
|
wxEnhMetaFileDC metadc;
|
2012-11-12 10:50:13 -05:00
|
|
|
GetTextExtentTester<wxEnhMetaFileDC> testMF(metadc);
|
2012-10-27 08:27:56 -04:00
|
|
|
#endif
|
2012-10-17 18:35:49 -04:00
|
|
|
}
|
|
|
|
|
2013-07-09 21:28:18 -04:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2012-10-17 18:35:49 -04:00
|
|
|
void MeasuringTextTestCase::WindowGetTextExtent()
|
|
|
|
{
|
|
|
|
wxWindow* const win = wxTheApp->GetTopWindow();
|
|
|
|
|
2012-11-12 10:50:13 -05:00
|
|
|
GetTextExtentTester<wxWindow> testWin(*win);
|
2012-10-17 18:35:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2009-04-15 18:38:53 -04:00
|
|
|
{
|
|
|
|
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);
|
2017-10-01 12:41:49 -04:00
|
|
|
delete context;
|
2009-04-15 18:38:53 -04:00
|
|
|
|
|
|
|
// TODO: Determine a way to make these tests more robust.
|
|
|
|
CPPUNIT_ASSERT(width > 0.0);
|
|
|
|
CPPUNIT_ASSERT(height > 0.0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-17 18:35:49 -04:00
|
|
|
#endif // TEST_GC
|