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 ); | |
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 VZ |
56 | void DCGetTextExtent(); |
57 | void WindowGetTextExtent(); | |
58 | ||
59 | void GetPartialTextExtent(); | |
60 | ||
61 | #ifdef TEST_GC | |
62 | void GraphicsGetTextExtent(); | |
63 | #endif // TEST_GC | |
64 | ||
65 | DECLARE_NO_COPY_CLASS(MeasuringTextTestCase) | |
f806b2b4 KO |
66 | }; |
67 | ||
68 | // register in the unnamed registry so that these tests are run by default | |
8cd79b7a | 69 | CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringTextTestCase ); |
f806b2b4 | 70 | |
e3778b4d | 71 | // also include in its own registry so that these tests can be run alone |
8cd79b7a | 72 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringTextTestCase, "MeasuringTextTestCase" ); |
f806b2b4 | 73 | |
f806b2b4 | 74 | // ---------------------------------------------------------------------------- |
c7619cf1 | 75 | // helper for XXXTextExtent() methods |
f806b2b4 KO |
76 | // ---------------------------------------------------------------------------- |
77 | ||
8cd79b7a | 78 | template <typename T> |
c7619cf1 | 79 | struct GetTextExtentTester |
8cd79b7a | 80 | { |
c7619cf1 VZ |
81 | // Constructor runs a couple of simple tests for GetTextExtent(). |
82 | GetTextExtentTester(const T& obj) | |
83 | { | |
84 | // Test that getting the height only doesn't crash. | |
85 | int y; | |
86 | obj.GetTextExtent("H", NULL, &y); | |
87 | ||
88 | CPPUNIT_ASSERT( y > 1 ); | |
89 | ||
90 | wxSize size = obj.GetTextExtent("Hello"); | |
91 | CPPUNIT_ASSERT( size.x > 1 ); | |
92 | CPPUNIT_ASSERT_EQUAL( y, size.y ); | |
93 | } | |
94 | }; | |
8cd79b7a | 95 | |
c7619cf1 VZ |
96 | // ---------------------------------------------------------------------------- |
97 | // tests themselves | |
98 | // ---------------------------------------------------------------------------- | |
8cd79b7a VZ |
99 | |
100 | void MeasuringTextTestCase::DCGetTextExtent() | |
101 | { | |
102 | wxClientDC dc(wxTheApp->GetTopWindow()); | |
103 | ||
c7619cf1 | 104 | GetTextExtentTester<wxClientDC> testDC(dc); |
bb996f28 VZ |
105 | |
106 | int w; | |
107 | dc.GetMultiLineTextExtent("Good\nbye", &w, NULL); | |
108 | const wxSize sz = dc.GetTextExtent("Good"); | |
109 | CPPUNIT_ASSERT_EQUAL( sz.x, w ); | |
110 | ||
111 | CPPUNIT_ASSERT( dc.GetMultiLineTextExtent("Good\nbye").y >= 2*sz.y ); | |
1bce253a VZ |
112 | |
113 | // Test the functions with some other DC kinds also. | |
114 | #if wxUSE_PRINTING_ARCHITECTURE && wxUSE_POSTSCRIPT | |
115 | wxPostScriptDC psdc; | |
116 | // wxPostScriptDC doesn't have any font set by default but its | |
117 | // GetTextExtent() requires one to be set. This is probably a bug and we | |
118 | // should set the default font in it implicitly but for now just work | |
119 | // around it. | |
120 | psdc.SetFont(*wxNORMAL_FONT); | |
c7619cf1 | 121 | GetTextExtentTester<wxPostScriptDC> testPS(psdc); |
1bce253a VZ |
122 | #endif |
123 | ||
124 | #if wxUSE_ENH_METAFILE | |
125 | wxEnhMetaFileDC metadc; | |
c7619cf1 | 126 | GetTextExtentTester<wxEnhMetaFileDC> testMF(metadc); |
1bce253a | 127 | #endif |
8cd79b7a VZ |
128 | } |
129 | ||
130 | void MeasuringTextTestCase::WindowGetTextExtent() | |
131 | { | |
132 | wxWindow* const win = wxTheApp->GetTopWindow(); | |
133 | ||
c7619cf1 | 134 | GetTextExtentTester<wxWindow> testWin(*win); |
8cd79b7a VZ |
135 | } |
136 | ||
137 | void MeasuringTextTestCase::GetPartialTextExtent() | |
138 | { | |
139 | wxClientDC dc(wxTheApp->GetTopWindow()); | |
140 | ||
141 | wxArrayInt widths; | |
142 | CPPUNIT_ASSERT( dc.GetPartialTextExtents("Hello", widths) ); | |
143 | CPPUNIT_ASSERT_EQUAL( 5, widths.size() ); | |
144 | CPPUNIT_ASSERT_EQUAL( widths[0], dc.GetTextExtent("H").x ); | |
145 | CPPUNIT_ASSERT_EQUAL( widths[4], dc.GetTextExtent("Hello").x ); | |
146 | } | |
147 | ||
148 | #ifdef TEST_GC | |
149 | ||
150 | void MeasuringTextTestCase::GraphicsGetTextExtent() | |
f806b2b4 KO |
151 | { |
152 | wxGraphicsRenderer* renderer = wxGraphicsRenderer::GetDefaultRenderer(); | |
153 | CPPUNIT_ASSERT(renderer); | |
154 | wxGraphicsContext* context = renderer->CreateMeasuringContext(); | |
155 | CPPUNIT_ASSERT(context); | |
156 | wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL); | |
157 | CPPUNIT_ASSERT(font.IsOk()); | |
158 | context->SetFont(font, *wxBLACK); | |
159 | double width, height, descent, externalLeading = 0.0; | |
160 | context->GetTextExtent("x", &width, &height, &descent, &externalLeading); | |
161 | ||
162 | // TODO: Determine a way to make these tests more robust. | |
163 | CPPUNIT_ASSERT(width > 0.0); | |
164 | CPPUNIT_ASSERT(height > 0.0); | |
165 | ||
166 | } | |
167 | ||
8cd79b7a | 168 | #endif // TEST_GC |