Fix crash in wxDC::GetMultiLineTextExtent() after last commit.
[wxWidgets.git] / 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)
5 // Created: 2008-02-12
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com>
8 // (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ----------------------------------------------------------------------------
12 // headers
13 // ----------------------------------------------------------------------------
14
15 #include "testprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #include "wx/font.h"
24 #include "wx/window.h"
25 #endif // WX_PRECOMP
26
27 // wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11
28 #if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__)
29 #include "wx/graphics.h"
30 #define TEST_GC
31 #endif
32
33 #include "wx/dcclient.h"
34
35 // ----------------------------------------------------------------------------
36 // test class
37 // ----------------------------------------------------------------------------
38
39 class MeasuringTextTestCase : public CppUnit::TestCase
40 {
41 public:
42 MeasuringTextTestCase() { }
43
44 private:
45 CPPUNIT_TEST_SUITE( MeasuringTextTestCase );
46 CPPUNIT_TEST( DCGetTextExtent );
47 CPPUNIT_TEST( WindowGetTextExtent );
48 CPPUNIT_TEST( GetPartialTextExtent );
49 #ifdef TEST_GC
50 CPPUNIT_TEST( GraphicsGetTextExtent );
51 #endif // TEST_GC
52 CPPUNIT_TEST_SUITE_END();
53
54 template <typename T>
55 void DoTestGetTextExtent(const T& obj);
56
57 void DCGetTextExtent();
58 void WindowGetTextExtent();
59
60 void GetPartialTextExtent();
61
62 #ifdef TEST_GC
63 void GraphicsGetTextExtent();
64 #endif // TEST_GC
65
66 DECLARE_NO_COPY_CLASS(MeasuringTextTestCase)
67 };
68
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase );
71
72 // also include in its own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" );
74
75 // ----------------------------------------------------------------------------
76 // tests themselves
77 // ----------------------------------------------------------------------------
78
79 template <typename T>
80 void MeasuringTextTestCase::DoTestGetTextExtent(const T& obj)
81 {
82 // Test that getting the height only doesn't crash.
83 int y;
84 obj.GetTextExtent("H", NULL, &y);
85
86 CPPUNIT_ASSERT( y > 1 );
87
88 wxSize size = obj.GetTextExtent("Hello");
89 CPPUNIT_ASSERT( size.x > 1 );
90 CPPUNIT_ASSERT_EQUAL( y, size.y );
91 }
92
93 void MeasuringTextTestCase::DCGetTextExtent()
94 {
95 wxClientDC dc(wxTheApp->GetTopWindow());
96
97 DoTestGetTextExtent(dc);
98
99 int w;
100 dc.GetMultiLineTextExtent("Good\nbye", &w, NULL);
101 const wxSize sz = dc.GetTextExtent("Good");
102 CPPUNIT_ASSERT_EQUAL( sz.x, w );
103
104 CPPUNIT_ASSERT( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y );
105 }
106
107 void MeasuringTextTestCase::WindowGetTextExtent()
108 {
109 wxWindow* const win = wxTheApp->GetTopWindow();
110
111 DoTestGetTextExtent(*win);
112 }
113
114 void MeasuringTextTestCase::GetPartialTextExtent()
115 {
116 wxClientDC dc(wxTheApp->GetTopWindow());
117
118 wxArrayInt widths;
119 CPPUNIT_ASSERT( dc.GetPartialTextExtents("Hello", widths) );
120 CPPUNIT_ASSERT_EQUAL( 5, widths.size() );
121 CPPUNIT_ASSERT_EQUAL( widths[0], dc.GetTextExtent("H").x );
122 CPPUNIT_ASSERT_EQUAL( widths[4], dc.GetTextExtent("Hello").x );
123 }
124
125 #ifdef TEST_GC
126
127 void MeasuringTextTestCase::GraphicsGetTextExtent()
128 {
129 wxGraphicsRenderer* renderer = wxGraphicsRenderer::GetDefaultRenderer();
130 CPPUNIT_ASSERT(renderer);
131 wxGraphicsContext* context = renderer->CreateMeasuringContext();
132 CPPUNIT_ASSERT(context);
133 wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
134 CPPUNIT_ASSERT(font.IsOk());
135 context->SetFont(font, *wxBLACK);
136 double width, height, descent, externalLeading = 0.0;
137 context->GetTextExtent("x", &width, &height, &descent, &externalLeading);
138
139 // TODO: Determine a way to make these tests more robust.
140 CPPUNIT_ASSERT(width > 0.0);
141 CPPUNIT_ASSERT(height > 0.0);
142
143 }
144
145 #endif // TEST_GC