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 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 class MiscGUIFuncsTestCase
: public CppUnit
::TestCase
39 MiscGUIFuncsTestCase() { }
42 CPPUNIT_TEST_SUITE( MiscGUIFuncsTestCase
);
43 CPPUNIT_TEST( DisplaySize
);
44 CPPUNIT_TEST( URLDataObject
);
45 CPPUNIT_TEST( ParseFileDialogFilter
);
46 CPPUNIT_TEST( FindWindowAtPoint
);
47 CPPUNIT_TEST_SUITE_END();
51 void ParseFileDialogFilter();
52 void FindWindowAtPoint();
54 DECLARE_NO_COPY_CLASS(MiscGUIFuncsTestCase
)
57 // register in the unnamed registry so that these tests are run by default
58 CPPUNIT_TEST_SUITE_REGISTRATION( MiscGUIFuncsTestCase
);
60 // also include in its own registry so that these tests can be run alone
61 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscGUIFuncsTestCase
, "MiscGUIFuncsTestCase" );
63 void MiscGUIFuncsTestCase
::DisplaySize()
65 // test that different (almost) overloads return the same results
67 wxDisplaySize(&w
, &h
);
68 wxSize sz
= wxGetDisplaySize();
70 CPPUNIT_ASSERT_EQUAL( w
, sz
.x
);
71 CPPUNIT_ASSERT_EQUAL( h
, sz
.y
);
73 // test that passing NULL works as expected, e.g. doesn't crash
74 wxDisplaySize(NULL
, NULL
);
75 wxDisplaySize(&w
, NULL
);
76 wxDisplaySize(NULL
, &h
);
78 CPPUNIT_ASSERT_EQUAL( w
, sz
.x
);
79 CPPUNIT_ASSERT_EQUAL( h
, sz
.y
);
81 // test that display PPI is something reasonable
82 sz
= wxGetDisplayPPI();
83 CPPUNIT_ASSERT( sz
.x
< 1000 && sz
.y
< 1000 );
86 void MiscGUIFuncsTestCase
::URLDataObject()
88 // this tests for buffer overflow, see #11102
90 url
= "http://something.long.to.overwrite.plenty.memory.example.com";
91 wxURLDataObject
* const dobj
= new wxURLDataObject(url
);
92 CPPUNIT_ASSERT_EQUAL( url
, dobj
->GetURL() );
94 wxClipboardLocker lockClip
;
95 CPPUNIT_ASSERT( wxTheClipboard
->SetData(dobj
) );
96 wxTheClipboard
->Flush();
99 void MiscGUIFuncsTestCase
::ParseFileDialogFilter()
107 wxParseCommonDialogsFilter("Image files|*.jpg;*.png", descs
, filters
)
110 CPPUNIT_ASSERT_EQUAL( "Image files", descs
[0] );
111 CPPUNIT_ASSERT_EQUAL( "*.jpg;*.png", filters
[0] );
116 wxParseCommonDialogsFilter
118 "All files (*.*)|*.*|Python source (*.py)|*.py",
123 CPPUNIT_ASSERT_EQUAL( "*.*", filters
[0] );
124 CPPUNIT_ASSERT_EQUAL( "*.py", filters
[1] );
126 // Test some invalid ones too.
127 WX_ASSERT_FAILS_WITH_ASSERT
129 wxParseCommonDialogsFilter
131 "All files (*.*)|*.*|Python source (*.py)|*.py|",
140 // This class is used as a test window here. We can't use a real wxButton
141 // because we can't create other windows as its children in wxGTK.
142 class TestButton
: public wxWindow
145 TestButton(wxWindow
* parent
, const wxString
& label
, const wxPoint
& pos
)
146 : wxWindow(parent
, wxID_ANY
, pos
, wxSize(100, 50))
152 // Helper function returning the label of the window at the given point or
153 // "NONE" if there is no window there.
154 wxString
GetLabelOfWindowAtPoint(wxWindow
* parent
, int x
, int y
)
157 win
= wxFindWindowAtPoint(parent
->ClientToScreen(wxPoint(x
, y
)));
158 return win ? win
->GetLabel() : wxString("NONE");
161 } // anonymous namespace
163 void MiscGUIFuncsTestCase
::FindWindowAtPoint()
165 wxWindow
* const parent
= wxTheApp
->GetTopWindow();
166 CPPUNIT_ASSERT( parent
);
168 // Set a label to allow distinguishing it from the other windows in the
169 // assertion messages.
170 parent
->SetLabel("parent");
172 CPPUNIT_ASSERT_EQUAL_MESSAGE
174 "No window for a point outside of the window",
176 GetLabelOfWindowAtPoint(parent
, 900, 900)
179 wxWindow
* btn1
= new TestButton(parent
, "1", wxPoint(10, 10));
180 CPPUNIT_ASSERT_EQUAL_MESSAGE
182 "Point over a child control corresponds to it",
184 GetLabelOfWindowAtPoint(parent
, 11, 11)
187 CPPUNIT_ASSERT_EQUAL_MESSAGE
189 "Point outside of any child control returns the TLW itself",
191 GetLabelOfWindowAtPoint(parent
, 5, 5)
194 wxWindow
* btn2
= new TestButton(parent
, "2", wxPoint(10, 90));
196 CPPUNIT_ASSERT_EQUAL_MESSAGE
198 "Point over a disabled child control still corresponds to it",
200 GetLabelOfWindowAtPoint(parent
, 11, 91)
204 CPPUNIT_ASSERT_EQUAL_MESSAGE
206 "Point over a hidden child control doesn't take it into account",
208 GetLabelOfWindowAtPoint(parent
, 11, 91)
212 wxWindow
* btn3
= new TestButton(btn2
, "3", wxPoint(0, 0));
214 CPPUNIT_ASSERT_EQUAL_MESSAGE
216 "Point over recursive disabled child controls corresponds to deepest child",
218 GetLabelOfWindowAtPoint(parent
, 11, 91)
222 wxDELETE(btn3
); // delete child before parent