1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/misctests.cpp
3 // Purpose: test miscellaneous GUI functions
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2008 Vadim Zeitlin
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
22 #include "wx/gdicmn.h"
23 #include "wx/filefn.h"
27 #include "wx/button.h"
28 #include "wx/clipbrd.h"
29 #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 wxWindow
* btn1
= new TestButton(parent
, "1", wxPoint(10, 10));
210 wxWindow
* btn2
= new TestButton(parent
, "2", wxPoint(10, 90));
211 wxWindow
* btn3
= new TestButton(btn2
, "3", wxPoint(20, 20));
213 // We need this to realize the windows created above under wxGTK.
216 CPPUNIT_ASSERT_EQUAL_MESSAGE
218 "No window for a point outside of the window",
220 GetLabelOfWindowAtPoint(parent
, 900, 900)
223 CPPUNIT_ASSERT_EQUAL_MESSAGE
225 "Point over a child control corresponds to it",
227 GetLabelOfWindowAtPoint(parent
, 11, 11)
230 CPPUNIT_ASSERT_EQUAL_MESSAGE
232 "Point outside of any child control returns the TLW itself",
234 GetLabelOfWindowAtPoint(parent
, 5, 5)
238 CPPUNIT_ASSERT_EQUAL_MESSAGE
240 "Point over a disabled child control still corresponds to it",
242 GetLabelOfWindowAtPoint(parent
, 11, 91)
246 CPPUNIT_ASSERT_EQUAL_MESSAGE
248 "Point over a hidden child control doesn't take it into account",
250 GetLabelOfWindowAtPoint(parent
, 11, 91)
254 CPPUNIT_ASSERT_EQUAL_MESSAGE
256 "Point over child control corresponds to the child",
258 GetLabelOfWindowAtPoint(parent
, 31, 111)
262 CPPUNIT_ASSERT_EQUAL_MESSAGE
264 "Point over disabled child controls still corresponds to this child",
266 GetLabelOfWindowAtPoint(parent
, 31, 111)
271 // btn3 was already deleted when its parent was