]>
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 |
2fc9c1ea | 6 | // Copyright: (c) 2008 Kevin Ollivier <kevino@theolliviers.com> |
8cd79b7a | 7 | // (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org> |
f806b2b4 KO |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | // ---------------------------------------------------------------------------- | |
11 | // headers | |
12 | // ---------------------------------------------------------------------------- | |
13 | ||
14 | #include "testprec.h" | |
15 | ||
f806b2b4 KO |
16 | #ifdef __BORLANDC__ |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/app.h" | |
22 | #include "wx/font.h" | |
23 | #include "wx/window.h" | |
24 | #endif // WX_PRECOMP | |
25 | ||
8cd79b7a VZ |
26 | // wxCairoRenderer::CreateMeasuringContext() is not implement for wxX11 |
27 | #if wxUSE_GRAPHICS_CONTEXT && !defined(__WXX11__) | |
28 | #include "wx/graphics.h" | |
29 | #define TEST_GC | |
30 | #endif | |
31 | ||
32 | #include "wx/dcclient.h" | |
1bce253a VZ |
33 | #include "wx/dcps.h" |
34 | #include "wx/metafile.h" | |
8cd79b7a | 35 | |
f806b2b4 KO |
36 | // ---------------------------------------------------------------------------- |
37 | // test class | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
8cd79b7a | 40 | class MeasuringTextTestCase : public CppUnit::TestCase |
f806b2b4 KO |
41 | { |
42 | public: | |
8cd79b7a | 43 | MeasuringTextTestCase() { } |
f806b2b4 | 44 | |
f806b2b4 | 45 | private: |
8cd79b7a VZ |
46 | CPPUNIT_TEST_SUITE( MeasuringTextTestCase ); |
47 | CPPUNIT_TEST( DCGetTextExtent ); | |
0646084e | 48 | CPPUNIT_TEST( LeadingAndDescent ); |
8cd79b7a VZ |
49 | CPPUNIT_TEST( WindowGetTextExtent ); |
50 | CPPUNIT_TEST( GetPartialTextExtent ); | |
51 | #ifdef TEST_GC | |
52 | CPPUNIT_TEST( GraphicsGetTextExtent ); | |
53 | #endif // TEST_GC | |
f806b2b4 KO |
54 | CPPUNIT_TEST_SUITE_END(); |
55 | ||
8cd79b7a | 56 | void DCGetTextExtent(); |
0646084e | 57 | void LeadingAndDescent(); |
8cd79b7a VZ |
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) | |
f806b2b4 KO |
67 | }; |
68 | ||
69 | // register in the unnamed registry so that these tests are run by default | |
8cd79b7a | 70 | CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase ); |
f806b2b4 | 71 | |
e3778b4d | 72 | // also include in its own registry so that these tests can be run alone |
8cd79b7a | 73 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" ); |
f806b2b4 | 74 | |
f806b2b4 | 75 | // ---------------------------------------------------------------------------- |
c7619cf1 | 76 | // helper for XXXTextExtent() methods |
f806b2b4 KO |
77 | // ---------------------------------------------------------------------------- |
78 | ||
8cd79b7a | 79 | template <typename T> |
c7619cf1 | 80 | struct GetTextExtentTester |
8cd79b7a | 81 | { |
c7619cf1 VZ |
82 | // Constructor runs a couple of simple tests for GetTextExtent(). |
83 | GetTextExtentTester(const T& obj) | |
84 | { | |
85 | // Test that getting the height only doesn't crash. | |
86 | int y; | |
87 | obj.GetTextExtent("H", NULL, &y); | |
88 | ||
89 | CPPUNIT_ASSERT( y > 1 ); | |
90 | ||
91 | wxSize size = obj.GetTextExtent("Hello"); | |
92 | CPPUNIT_ASSERT( size.x > 1 ); | |
93 | CPPUNIT_ASSERT_EQUAL( y, size.y ); | |
94 | } | |
95 | }; | |
8cd79b7a | 96 | |
c7619cf1 VZ |
97 | // ---------------------------------------------------------------------------- |
98 | // tests themselves | |
99 | // ---------------------------------------------------------------------------- | |
8cd79b7a VZ |
100 | |
101 | void MeasuringTextTestCase::DCGetTextExtent() | |
102 | { | |
103 | wxClientDC dc(wxTheApp->GetTopWindow()); | |
104 | ||
c7619cf1 | 105 | GetTextExtentTester<wxClientDC> testDC(dc); |
bb996f28 VZ |
106 | |
107 | int w; | |
108 | dc.GetMultiLineTextExtent("Good\nbye", &w, NULL); | |
109 | const wxSize sz = dc.GetTextExtent("Good"); | |
110 | CPPUNIT_ASSERT_EQUAL( sz.x, w ); | |
111 | ||
112 | CPPUNIT_ASSERT( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y ); | |
1bce253a VZ |
113 | |
114 | // Test the functions with some other DC kinds also. | |
115 | #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT | |
116 | wxPostScriptDC psdc; | |
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 | |
120 | // around it. | |
121 | psdc.SetFont(*wxNORMAL_FONT); | |
c7619cf1 | 122 | GetTextExtentTester<wxPostScriptDC> testPS(psdc); |
1bce253a VZ |
123 | #endif |
124 | ||
125 | #if wxUSE_ENH_METAFILE | |
126 | wxEnhMetaFileDC metadc; | |
c7619cf1 | 127 | GetTextExtentTester<wxEnhMetaFileDC> testMF(metadc); |
1bce253a | 128 | #endif |
8cd79b7a VZ |
129 | } |
130 | ||
0646084e VZ |
131 | void MeasuringTextTestCase::LeadingAndDescent() |
132 | { | |
133 | wxClientDC dc(wxTheApp->GetTopWindow()); | |
134 | ||
135 | // Retrieving just the descent should work. | |
136 | int descent = -17; | |
137 | dc.GetTextExtent("foo", NULL, NULL, &descent); | |
138 | CPPUNIT_ASSERT( descent != -17 ); | |
139 | ||
140 | // Same for external leading. | |
141 | int leading = -289; | |
142 | dc.GetTextExtent("foo", NULL, NULL, NULL, &leading); | |
143 | CPPUNIT_ASSERT( leading != -289 ); | |
144 | ||
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. | |
147 | int descent2, | |
148 | leading2; | |
149 | dc.GetTextExtent("", NULL, NULL, &descent2, &leading2); | |
150 | ||
151 | CPPUNIT_ASSERT_EQUAL( descent, descent2 ); | |
152 | CPPUNIT_ASSERT_EQUAL( leading, leading2 ); | |
153 | } | |
154 | ||
8cd79b7a VZ |
155 | void MeasuringTextTestCase::WindowGetTextExtent() |
156 | { | |
157 | wxWindow* const win = wxTheApp->GetTopWindow(); | |
158 | ||
c7619cf1 | 159 | GetTextExtentTester<wxWindow> testWin(*win); |
8cd79b7a VZ |
160 | } |
161 | ||
162 | void MeasuringTextTestCase::GetPartialTextExtent() | |
163 | { | |
164 | wxClientDC dc(wxTheApp->GetTopWindow()); | |
165 | ||
166 | wxArrayInt widths; | |
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 ); | |
171 | } | |
172 | ||
173 | #ifdef TEST_GC | |
174 | ||
175 | void MeasuringTextTestCase::GraphicsGetTextExtent() | |
f806b2b4 KO |
176 | { |
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); | |
186 | ||
187 | // TODO: Determine a way to make these tests more robust. | |
188 | CPPUNIT_ASSERT(width > 0.0); | |
189 | CPPUNIT_ASSERT(height > 0.0); | |
190 | ||
191 | } | |
192 | ||
8cd79b7a | 193 | #endif // TEST_GC |