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