Add a test to ensure that CreateMeasuringContext creates a valid context
[wxWidgets.git] / tests / graphics / measuring.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/clientsize.cpp
3 // Purpose: Client vs. window size handling unit test
4 // Author: Vaclav Slavik
5 // Created: 2008-02-12
6 // RCS-ID: $Id: clientsize.cpp 53740 2008-05-25 02:56:22Z VZ $
7 // Copyright: (c) 2008 Vaclav Slavik <vslavik@fastmail.fm>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #if wxUSE_GRAPHICS_CONTEXT
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/app.h"
24 #include "wx/font.h"
25 #include "wx/window.h"
26 #endif // WX_PRECOMP
27
28 #include "wx/graphics.h"
29 // ----------------------------------------------------------------------------
30 // test class
31 // ----------------------------------------------------------------------------
32
33 class MeasuringContextTestCase : public CppUnit::TestCase
34 {
35 public:
36 MeasuringContextTestCase() { }
37
38 virtual void setUp();
39 virtual void tearDown();
40
41 private:
42 CPPUNIT_TEST_SUITE( MeasuringContextTestCase );
43 CPPUNIT_TEST( GetTextExtent );
44 CPPUNIT_TEST_SUITE_END();
45
46 void GetTextExtent();
47
48 wxWindow *m_win;
49
50 DECLARE_NO_COPY_CLASS(MeasuringContextTestCase)
51 };
52
53 // register in the unnamed registry so that these tests are run by default
54 CPPUNIT_TEST_SUITE_REGISTRATION( MeasuringContextTestCase );
55
56 // also include in it's own registry so that these tests can be run alone
57 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MeasuringContextTestCase, "MeasuringContextTestCase" );
58
59 // ----------------------------------------------------------------------------
60 // test initialization
61 // ----------------------------------------------------------------------------
62
63 void MeasuringContextTestCase::setUp()
64 {
65 m_win = wxTheApp->GetTopWindow();
66 }
67
68 void MeasuringContextTestCase::tearDown()
69 {
70 m_win = NULL;
71 }
72
73 // ----------------------------------------------------------------------------
74 // tests themselves
75 // ----------------------------------------------------------------------------
76
77 void MeasuringContextTestCase::GetTextExtent()
78 {
79 wxGraphicsRenderer* renderer = wxGraphicsRenderer::GetDefaultRenderer();
80 CPPUNIT_ASSERT(renderer);
81 wxGraphicsContext* context = renderer->CreateMeasuringContext();
82 CPPUNIT_ASSERT(context);
83 wxFont font(12, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
84 CPPUNIT_ASSERT(font.IsOk());
85 context->SetFont(font, *wxBLACK);
86 double width, height, descent, externalLeading = 0.0;
87 context->GetTextExtent("x", &width, &height, &descent, &externalLeading);
88
89 // TODO: Determine a way to make these tests more robust.
90 CPPUNIT_ASSERT(width > 0.0);
91 CPPUNIT_ASSERT(height > 0.0);
92
93 }
94
95 #endif