1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/misctests.cpp
3 // Purpose: test miscellaneous GUI functions
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
23 #include "wx/gdicmn.h"
24 #include "wx/filefn.h"
28 #include "wx/button.h"
29 #include "wx/clipbrd.h"
30 #include "wx/dataobj.h"
33 #include "asserthelper.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class MiscGUIFuncsTestCase
: public CppUnit::TestCase
42 MiscGUIFuncsTestCase() { }
45 CPPUNIT_TEST_SUITE( MiscGUIFuncsTestCase
);
46 CPPUNIT_TEST( DisplaySize
);
47 CPPUNIT_TEST( URLDataObject
);
48 CPPUNIT_TEST( ParseFileDialogFilter
);
49 CPPUNIT_TEST( ClientToScreen
);
50 CPPUNIT_TEST( FindWindowAtPoint
);
51 CPPUNIT_TEST_SUITE_END();
55 void ParseFileDialogFilter();
56 void ClientToScreen();
57 void FindWindowAtPoint();
59 DECLARE_NO_COPY_CLASS(MiscGUIFuncsTestCase
)
62 // register in the unnamed registry so that these tests are run by default
63 CPPUNIT_TEST_SUITE_REGISTRATION( MiscGUIFuncsTestCase
);
65 // also include in its own registry so that these tests can be run alone
66 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscGUIFuncsTestCase
, "MiscGUIFuncsTestCase" );
68 void MiscGUIFuncsTestCase::DisplaySize()
70 // test that different (almost) overloads return the same results
72 wxDisplaySize(&w
, &h
);
73 wxSize sz
= wxGetDisplaySize();
75 CPPUNIT_ASSERT_EQUAL( w
, sz
.x
);
76 CPPUNIT_ASSERT_EQUAL( h
, sz
.y
);
78 // test that passing NULL works as expected, e.g. doesn't crash
79 wxDisplaySize(NULL
, NULL
);
80 wxDisplaySize(&w
, NULL
);
81 wxDisplaySize(NULL
, &h
);
83 CPPUNIT_ASSERT_EQUAL( w
, sz
.x
);
84 CPPUNIT_ASSERT_EQUAL( h
, sz
.y
);
86 // test that display PPI is something reasonable
87 sz
= wxGetDisplayPPI();
88 CPPUNIT_ASSERT( sz
.x
< 1000 && sz
.y
< 1000 );
91 void MiscGUIFuncsTestCase::URLDataObject()
93 // this tests for buffer overflow, see #11102
95 url
= "http://something.long.to.overwrite.plenty.memory.example.com";
96 wxURLDataObject
* const dobj
= new wxURLDataObject(url
);
97 CPPUNIT_ASSERT_EQUAL( url
, dobj
->GetURL() );
99 wxClipboardLocker lockClip
;
100 CPPUNIT_ASSERT( wxTheClipboard
->SetData(dobj
) );
101 wxTheClipboard
->Flush();
104 void MiscGUIFuncsTestCase::ParseFileDialogFilter()
112 wxParseCommonDialogsFilter("Image files|*.jpg;*.png", descs
, filters
)
115 CPPUNIT_ASSERT_EQUAL( "Image files", descs
[0] );
116 CPPUNIT_ASSERT_EQUAL( "*.jpg;*.png", filters
[0] );
121 wxParseCommonDialogsFilter
123 "All files (*.*)|*.*|Python source (*.py)|*.py",
128 CPPUNIT_ASSERT_EQUAL( "*.*", filters
[0] );
129 CPPUNIT_ASSERT_EQUAL( "*.py", filters
[1] );
131 // Test some invalid ones too.
132 WX_ASSERT_FAILS_WITH_ASSERT
134 wxParseCommonDialogsFilter
136 "All files (*.*)|*.*|Python source (*.py)|*.py|",
142 void MiscGUIFuncsTestCase::ClientToScreen()
144 wxWindow
* const tlw
= wxTheApp
->GetTopWindow();
145 CPPUNIT_ASSERT( tlw
);
148 p1
= new wxPanel(tlw
, wxID_ANY
, wxPoint(0, 0), wxSize(100, 50));
150 p2
= new wxPanel(tlw
, wxID_ANY
, wxPoint(0, 50), wxSize(100, 50));
152 b
= new wxWindow(p2
, wxID_ANY
, wxPoint(10, 10), wxSize(30, 10));
154 // We need this to realize the windows created above under wxGTK.
157 const wxPoint tlwOrig
= tlw
->ClientToScreen(wxPoint(0, 0));
161 tlwOrig
+ wxPoint(0, 50),
162 p2
->ClientToScreen(wxPoint(0, 0))
167 tlwOrig
+ wxPoint(10, 60),
168 b
->ClientToScreen(wxPoint(0, 0))
178 // This class is used as a test window here. We can't use a real wxButton
179 // because we can't create other windows as its children in wxGTK.
180 class TestButton
: public wxWindow
183 TestButton(wxWindow
* parent
, const wxString
& label
, const wxPoint
& pos
)
184 : wxWindow(parent
, wxID_ANY
, pos
, wxSize(100, 50))
190 // Helper function returning the label of the window at the given point or
191 // "NONE" if there is no window there.
192 wxString
GetLabelOfWindowAtPoint(wxWindow
* parent
, int x
, int y
)
195 win
= wxFindWindowAtPoint(parent
->ClientToScreen(wxPoint(x
, y
)));
196 return win
? win
->GetLabel() : wxString("NONE");
199 } // anonymous namespace
201 void MiscGUIFuncsTestCase::FindWindowAtPoint()
203 wxWindow
* const parent
= wxTheApp
->GetTopWindow();
204 CPPUNIT_ASSERT( parent
);
206 // Set a label to allow distinguishing it from the other windows in the
207 // assertion messages.
208 parent
->SetLabel("parent");
210 wxWindow
* btn1
= new TestButton(parent
, "1", wxPoint(10, 10));
211 wxWindow
* btn2
= new TestButton(parent
, "2", wxPoint(10, 90));
212 wxWindow
* btn3
= new TestButton(btn2
, "3", wxPoint(20, 20));
214 // We need this to realize the windows created above under wxGTK.
217 CPPUNIT_ASSERT_EQUAL_MESSAGE
219 "No window for a point outside of the window",
221 GetLabelOfWindowAtPoint(parent
, 900, 900)
224 CPPUNIT_ASSERT_EQUAL_MESSAGE
226 "Point over a child control corresponds to it",
228 GetLabelOfWindowAtPoint(parent
, 11, 11)
231 CPPUNIT_ASSERT_EQUAL_MESSAGE
233 "Point outside of any child control returns the TLW itself",
235 GetLabelOfWindowAtPoint(parent
, 5, 5)
239 CPPUNIT_ASSERT_EQUAL_MESSAGE
241 "Point over a disabled child control still corresponds to it",
243 GetLabelOfWindowAtPoint(parent
, 11, 91)
247 CPPUNIT_ASSERT_EQUAL_MESSAGE
249 "Point over a hidden child control doesn't take it into account",
251 GetLabelOfWindowAtPoint(parent
, 11, 91)
255 CPPUNIT_ASSERT_EQUAL_MESSAGE
257 "Point over child control corresponds to the child",
259 GetLabelOfWindowAtPoint(parent
, 31, 111)
263 CPPUNIT_ASSERT_EQUAL_MESSAGE
265 "Point over disabled child controls still corresponds to this child",
267 GetLabelOfWindowAtPoint(parent
, 31, 111)
272 // btn3 was already deleted when its parent was