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"
32 #include "asserthelper.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 class MiscGUIFuncsTestCase
: public CppUnit
::TestCase
41 MiscGUIFuncsTestCase() { }
44 CPPUNIT_TEST_SUITE( MiscGUIFuncsTestCase
);
45 CPPUNIT_TEST( DisplaySize
);
46 CPPUNIT_TEST( URLDataObject
);
47 CPPUNIT_TEST( ParseFileDialogFilter
);
48 CPPUNIT_TEST( ClientToScreen
);
49 CPPUNIT_TEST( FindWindowAtPoint
);
50 CPPUNIT_TEST_SUITE_END();
54 void ParseFileDialogFilter();
55 void ClientToScreen();
56 void FindWindowAtPoint();
58 DECLARE_NO_COPY_CLASS(MiscGUIFuncsTestCase
)
61 // register in the unnamed registry so that these tests are run by default
62 CPPUNIT_TEST_SUITE_REGISTRATION( MiscGUIFuncsTestCase
);
64 // also include in its own registry so that these tests can be run alone
65 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscGUIFuncsTestCase
, "MiscGUIFuncsTestCase" );
67 void MiscGUIFuncsTestCase
::DisplaySize()
69 // test that different (almost) overloads return the same results
71 wxDisplaySize(&w
, &h
);
72 wxSize sz
= wxGetDisplaySize();
74 CPPUNIT_ASSERT_EQUAL( w
, sz
.x
);
75 CPPUNIT_ASSERT_EQUAL( h
, sz
.y
);
77 // test that passing NULL works as expected, e.g. doesn't crash
78 wxDisplaySize(NULL
, NULL
);
79 wxDisplaySize(&w
, NULL
);
80 wxDisplaySize(NULL
, &h
);
82 CPPUNIT_ASSERT_EQUAL( w
, sz
.x
);
83 CPPUNIT_ASSERT_EQUAL( h
, sz
.y
);
85 // test that display PPI is something reasonable
86 sz
= wxGetDisplayPPI();
87 CPPUNIT_ASSERT( sz
.x
< 1000 && sz
.y
< 1000 );
90 void MiscGUIFuncsTestCase
::URLDataObject()
92 // this tests for buffer overflow, see #11102
94 url
= "http://something.long.to.overwrite.plenty.memory.example.com";
95 wxURLDataObject
* const dobj
= new wxURLDataObject(url
);
96 CPPUNIT_ASSERT_EQUAL( url
, dobj
->GetURL() );
98 wxClipboardLocker lockClip
;
99 CPPUNIT_ASSERT( wxTheClipboard
->SetData(dobj
) );
100 wxTheClipboard
->Flush();
103 void MiscGUIFuncsTestCase
::ParseFileDialogFilter()
111 wxParseCommonDialogsFilter("Image files|*.jpg;*.png", descs
, filters
)
114 CPPUNIT_ASSERT_EQUAL( "Image files", descs
[0] );
115 CPPUNIT_ASSERT_EQUAL( "*.jpg;*.png", filters
[0] );
120 wxParseCommonDialogsFilter
122 "All files (*.*)|*.*|Python source (*.py)|*.py",
127 CPPUNIT_ASSERT_EQUAL( "*.*", filters
[0] );
128 CPPUNIT_ASSERT_EQUAL( "*.py", filters
[1] );
130 // Test some invalid ones too.
131 WX_ASSERT_FAILS_WITH_ASSERT
133 wxParseCommonDialogsFilter
135 "All files (*.*)|*.*|Python source (*.py)|*.py|",
141 void MiscGUIFuncsTestCase
::ClientToScreen()
143 wxWindow
* const tlw
= wxTheApp
->GetTopWindow();
144 CPPUNIT_ASSERT( tlw
);
147 p1
= new wxPanel(tlw
, wxID_ANY
, wxPoint(0, 0), wxSize(100, 50));
149 p2
= new wxPanel(tlw
, wxID_ANY
, wxPoint(0, 50), wxSize(100, 50));
151 b
= new wxWindow(p2
, wxID_ANY
, wxPoint(10, 10), wxSize(30, 10));
153 // We need this to realize the windows created above under wxGTK.
156 const wxPoint tlwOrig
= tlw
->ClientToScreen(wxPoint(0, 0));
160 tlwOrig
+ wxPoint(0, 50),
161 p2
->ClientToScreen(wxPoint(0, 0))
166 tlwOrig
+ wxPoint(10, 60),
167 b
->ClientToScreen(wxPoint(0, 0))
177 // This class is used as a test window here. We can't use a real wxButton
178 // because we can't create other windows as its children in wxGTK.
179 class TestButton
: public wxWindow
182 TestButton(wxWindow
* parent
, const wxString
& label
, const wxPoint
& pos
)
183 : wxWindow(parent
, wxID_ANY
, pos
, wxSize(100, 50))
189 // Helper function returning the label of the window at the given point or
190 // "NONE" if there is no window there.
191 wxString
GetLabelOfWindowAtPoint(wxWindow
* parent
, int x
, int y
)
194 win
= wxFindWindowAtPoint(parent
->ClientToScreen(wxPoint(x
, y
)));
195 return win ? win
->GetLabel() : wxString("NONE");
198 } // anonymous namespace
200 void MiscGUIFuncsTestCase
::FindWindowAtPoint()
202 wxWindow
* const parent
= wxTheApp
->GetTopWindow();
203 CPPUNIT_ASSERT( parent
);
205 // Set a label to allow distinguishing it from the other windows in the
206 // assertion messages.
207 parent
->SetLabel("parent");
209 CPPUNIT_ASSERT_EQUAL_MESSAGE
211 "No window for a point outside of the window",
213 GetLabelOfWindowAtPoint(parent
, 900, 900)
216 wxWindow
* btn1
= new TestButton(parent
, "1", wxPoint(10, 10));
217 CPPUNIT_ASSERT_EQUAL_MESSAGE
219 "Point over a child control corresponds to it",
221 GetLabelOfWindowAtPoint(parent
, 11, 11)
224 CPPUNIT_ASSERT_EQUAL_MESSAGE
226 "Point outside of any child control returns the TLW itself",
228 GetLabelOfWindowAtPoint(parent
, 5, 5)
231 wxWindow
* btn2
= new TestButton(parent
, "2", wxPoint(10, 90));
233 CPPUNIT_ASSERT_EQUAL_MESSAGE
235 "Point over a disabled child control still corresponds to it",
237 GetLabelOfWindowAtPoint(parent
, 11, 91)
241 CPPUNIT_ASSERT_EQUAL_MESSAGE
243 "Point over a hidden child control doesn't take it into account",
245 GetLabelOfWindowAtPoint(parent
, 11, 91)
249 wxWindow
* btn3
= new TestButton(btn2
, "3", wxPoint(0, 0));
251 CPPUNIT_ASSERT_EQUAL_MESSAGE
253 "Point over recursive disabled child controls corresponds to deepest child",
255 GetLabelOfWindowAtPoint(parent
, 11, 91)
259 wxDELETE(btn3
); // delete child before parent