]>
git.saurik.com Git - wxWidgets.git/blob - tests/graphics/measuring.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/graphics/measuring.cpp
3 // Purpose: Tests for wxGraphicsRenderer::CreateMeasuringContext
4 // Author: Kevin Ollivier, Vadim Zeitlin (non wxGC parts)
6 // Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com>
7 // (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
23 #include "wx/window.h"
26 // wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11
27 #if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__)
28 #include "wx/graphics.h"
32 #include "wx/dcclient.h"
34 #include "wx/metafile.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class MeasuringTextTestCase
: public CppUnit::TestCase
43 MeasuringTextTestCase() { }
46 CPPUNIT_TEST_SUITE( MeasuringTextTestCase
);
47 CPPUNIT_TEST( DCGetTextExtent
);
48 CPPUNIT_TEST( LeadingAndDescent
);
49 CPPUNIT_TEST( WindowGetTextExtent
);
50 CPPUNIT_TEST( GetPartialTextExtent
);
52 CPPUNIT_TEST( GraphicsGetTextExtent
);
54 CPPUNIT_TEST_SUITE_END();
56 void DCGetTextExtent();
57 void LeadingAndDescent();
58 void WindowGetTextExtent();
60 void GetPartialTextExtent();
63 void GraphicsGetTextExtent();
66 DECLARE_NO_COPY_CLASS(MeasuringTextTestCase
)
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase
);
72 // also include in its own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase
, "MeasuringTextTestCase" );
75 // ----------------------------------------------------------------------------
76 // helper for XXXTextExtent() methods
77 // ----------------------------------------------------------------------------
80 struct GetTextExtentTester
82 // Constructor runs a couple of simple tests for GetTextExtent().
83 GetTextExtentTester(const T
& obj
)
85 // Test that getting the height only doesn't crash.
87 obj
.GetTextExtent("H", NULL
, &y
);
89 CPPUNIT_ASSERT( y
> 1 );
91 wxSize size
= obj
.GetTextExtent("Hello");
92 CPPUNIT_ASSERT( size
.x
> 1 );
93 CPPUNIT_ASSERT_EQUAL( y
, size
.y
);
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 void MeasuringTextTestCase::DCGetTextExtent()
103 wxClientDC
dc(wxTheApp
->GetTopWindow());
105 GetTextExtentTester
<wxClientDC
> testDC(dc
);
108 dc
.GetMultiLineTextExtent("Good\nbye", &w
, NULL
);
109 const wxSize sz
= dc
.GetTextExtent("Good");
110 CPPUNIT_ASSERT_EQUAL( sz
.x
, w
);
112 CPPUNIT_ASSERT( dc
.GetMultiLineTextExtent("Good\nbye").y
>= 2*sz
.y
);
114 // Test the functions with some other DC kinds also.
115 #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT
117 // wxPostScriptDC doesn't have any font set by default but its
118 // GetTextExtent() requires one to be set. This is probably a bug and we
119 // should set the default font in it implicitly but for now just work
121 psdc
.SetFont(*wxNORMAL_FONT
);
122 GetTextExtentTester
<wxPostScriptDC
> testPS(psdc
);
125 #if wxUSE_ENH_METAFILE
126 wxEnhMetaFileDC metadc
;
127 GetTextExtentTester
<wxEnhMetaFileDC
> testMF(metadc
);
131 void MeasuringTextTestCase::LeadingAndDescent()
133 wxClientDC
dc(wxTheApp
->GetTopWindow());
135 // Retrieving just the descent should work.
137 dc
.GetTextExtent("foo", NULL
, NULL
, &descent
);
138 CPPUNIT_ASSERT( descent
!= -17 );
140 // Same for external leading.
142 dc
.GetTextExtent("foo", NULL
, NULL
, NULL
, &leading
);
143 CPPUNIT_ASSERT( leading
!= -289 );
145 // And both should also work for the empty string as they retrieve the
146 // values valid for the entire font and not just this string.
149 dc
.GetTextExtent("", NULL
, NULL
, &descent2
, &leading2
);
151 CPPUNIT_ASSERT_EQUAL( descent
, descent2
);
152 CPPUNIT_ASSERT_EQUAL( leading
, leading2
);
155 void MeasuringTextTestCase::WindowGetTextExtent()
157 wxWindow
* const win
= wxTheApp
->GetTopWindow();
159 GetTextExtentTester
<wxWindow
> testWin(*win
);
162 void MeasuringTextTestCase::GetPartialTextExtent()
164 wxClientDC
dc(wxTheApp
->GetTopWindow());
167 CPPUNIT_ASSERT( dc
.GetPartialTextExtents("Hello", widths
) );
168 CPPUNIT_ASSERT_EQUAL( 5, widths
.size() );
169 CPPUNIT_ASSERT_EQUAL( widths
[0], dc
.GetTextExtent("H").x
);
170 CPPUNIT_ASSERT_EQUAL( widths
[4], dc
.GetTextExtent("Hello").x
);
175 void MeasuringTextTestCase::GraphicsGetTextExtent()
177 wxGraphicsRenderer
* renderer
= wxGraphicsRenderer::GetDefaultRenderer();
178 CPPUNIT_ASSERT(renderer
);
179 wxGraphicsContext
* context
= renderer
->CreateMeasuringContext();
180 CPPUNIT_ASSERT(context
);
181 wxFont
font(12, wxFONTFAMILY_DEFAULT
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
182 CPPUNIT_ASSERT(font
.IsOk());
183 context
->SetFont(font
, *wxBLACK
);
184 double width
, height
, descent
, externalLeading
= 0.0;
185 context
->GetTextExtent("x", &width
, &height
, &descent
, &externalLeading
);
187 // TODO: Determine a way to make these tests more robust.
188 CPPUNIT_ASSERT(width
> 0.0);
189 CPPUNIT_ASSERT(height
> 0.0);