]> git.saurik.com Git - wxWidgets.git/blob - tests/misc/guifuncs.cpp
Disable Unity global menu while native modal dialogs are shown in wxGTK.
[wxWidgets.git] / tests / misc / guifuncs.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/misctests.cpp
3 // Purpose: test miscellaneous GUI functions
4 // Author: Vadim Zeitlin
5 // Created: 2008-09-22
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/defs.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/gdicmn.h"
24 #include "wx/filefn.h"
25 #endif // !PCH
26
27 #include "wx/app.h"
28 #include "wx/button.h"
29 #include "wx/clipbrd.h"
30 #include "wx/dataobj.h"
31 #include "wx/panel.h"
32
33 #include "asserthelper.h"
34
35 // ----------------------------------------------------------------------------
36 // test class
37 // ----------------------------------------------------------------------------
38
39 class MiscGUIFuncsTestCase : public CppUnit::TestCase
40 {
41 public:
42 MiscGUIFuncsTestCase() { }
43
44 private:
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();
52
53 void DisplaySize();
54 void URLDataObject();
55 void ParseFileDialogFilter();
56 void ClientToScreen();
57 void FindWindowAtPoint();
58
59 DECLARE_NO_COPY_CLASS(MiscGUIFuncsTestCase)
60 };
61
62 // register in the unnamed registry so that these tests are run by default
63 CPPUNIT_TEST_SUITE_REGISTRATION( MiscGUIFuncsTestCase );
64
65 // also include in its own registry so that these tests can be run alone
66 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscGUIFuncsTestCase, "MiscGUIFuncsTestCase" );
67
68 void MiscGUIFuncsTestCase::DisplaySize()
69 {
70 // test that different (almost) overloads return the same results
71 int w, h;
72 wxDisplaySize(&w, &h);
73 wxSize sz = wxGetDisplaySize();
74
75 CPPUNIT_ASSERT_EQUAL( w, sz.x );
76 CPPUNIT_ASSERT_EQUAL( h, sz.y );
77
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);
82
83 CPPUNIT_ASSERT_EQUAL( w, sz.x );
84 CPPUNIT_ASSERT_EQUAL( h, sz.y );
85
86 // test that display PPI is something reasonable
87 sz = wxGetDisplayPPI();
88 CPPUNIT_ASSERT( sz.x < 1000 && sz.y < 1000 );
89 }
90
91 void MiscGUIFuncsTestCase::URLDataObject()
92 {
93 // this tests for buffer overflow, see #11102
94 const char * const
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() );
98
99 wxClipboardLocker lockClip;
100 CPPUNIT_ASSERT( wxTheClipboard->SetData(dobj) );
101 wxTheClipboard->Flush();
102 }
103
104 void MiscGUIFuncsTestCase::ParseFileDialogFilter()
105 {
106 wxArrayString descs,
107 filters;
108
109 CPPUNIT_ASSERT_EQUAL
110 (
111 1,
112 wxParseCommonDialogsFilter("Image files|*.jpg;*.png", descs, filters)
113 );
114
115 CPPUNIT_ASSERT_EQUAL( "Image files", descs[0] );
116 CPPUNIT_ASSERT_EQUAL( "*.jpg;*.png", filters[0] );
117
118 CPPUNIT_ASSERT_EQUAL
119 (
120 2,
121 wxParseCommonDialogsFilter
122 (
123 "All files (*.*)|*.*|Python source (*.py)|*.py",
124 descs, filters
125 )
126 );
127
128 CPPUNIT_ASSERT_EQUAL( "*.*", filters[0] );
129 CPPUNIT_ASSERT_EQUAL( "*.py", filters[1] );
130
131 // Test some invalid ones too.
132 WX_ASSERT_FAILS_WITH_ASSERT
133 (
134 wxParseCommonDialogsFilter
135 (
136 "All files (*.*)|*.*|Python source (*.py)|*.py|",
137 descs, filters
138 )
139 );
140 }
141
142 void MiscGUIFuncsTestCase::ClientToScreen()
143 {
144 wxWindow* const tlw = wxTheApp->GetTopWindow();
145 CPPUNIT_ASSERT( tlw );
146
147 wxPanel* const
148 p1 = new wxPanel(tlw, wxID_ANY, wxPoint(0, 0), wxSize(100, 50));
149 wxPanel* const
150 p2 = new wxPanel(tlw, wxID_ANY, wxPoint(0, 50), wxSize(100, 50));
151 wxWindow* const
152 b = new wxWindow(p2, wxID_ANY, wxPoint(10, 10), wxSize(30, 10));
153
154 // We need this to realize the windows created above under wxGTK.
155 wxYield();
156
157 const wxPoint tlwOrig = tlw->ClientToScreen(wxPoint(0, 0));
158
159 CPPUNIT_ASSERT_EQUAL
160 (
161 tlwOrig + wxPoint(0, 50),
162 p2->ClientToScreen(wxPoint(0, 0))
163 );
164
165 CPPUNIT_ASSERT_EQUAL
166 (
167 tlwOrig + wxPoint(10, 60),
168 b->ClientToScreen(wxPoint(0, 0))
169 );
170
171 p1->Destroy();
172 p2->Destroy();
173 }
174
175 namespace
176 {
177
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
181 {
182 public:
183 TestButton(wxWindow* parent, const wxString& label, const wxPoint& pos)
184 : wxWindow(parent, wxID_ANY, pos, wxSize(100, 50))
185 {
186 SetLabel(label);
187 }
188 };
189
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)
193 {
194 wxWindow* const
195 win = wxFindWindowAtPoint(parent->ClientToScreen(wxPoint(x, y)));
196 return win ? win->GetLabel() : wxString("NONE");
197 }
198
199 } // anonymous namespace
200
201 void MiscGUIFuncsTestCase::FindWindowAtPoint()
202 {
203 wxWindow* const parent = wxTheApp->GetTopWindow();
204 CPPUNIT_ASSERT( parent );
205
206 // Set a label to allow distinguishing it from the other windows in the
207 // assertion messages.
208 parent->SetLabel("parent");
209
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));
213
214 // We need this to realize the windows created above under wxGTK.
215 wxYield();
216
217 CPPUNIT_ASSERT_EQUAL_MESSAGE
218 (
219 "No window for a point outside of the window",
220 "NONE",
221 GetLabelOfWindowAtPoint(parent, 900, 900)
222 );
223
224 CPPUNIT_ASSERT_EQUAL_MESSAGE
225 (
226 "Point over a child control corresponds to it",
227 btn1->GetLabel(),
228 GetLabelOfWindowAtPoint(parent, 11, 11)
229 );
230
231 CPPUNIT_ASSERT_EQUAL_MESSAGE
232 (
233 "Point outside of any child control returns the TLW itself",
234 parent->GetLabel(),
235 GetLabelOfWindowAtPoint(parent, 5, 5)
236 );
237
238 btn2->Disable();
239 CPPUNIT_ASSERT_EQUAL_MESSAGE
240 (
241 "Point over a disabled child control still corresponds to it",
242 btn2->GetLabel(),
243 GetLabelOfWindowAtPoint(parent, 11, 91)
244 );
245
246 btn2->Hide();
247 CPPUNIT_ASSERT_EQUAL_MESSAGE
248 (
249 "Point over a hidden child control doesn't take it into account",
250 parent->GetLabel(),
251 GetLabelOfWindowAtPoint(parent, 11, 91)
252 );
253
254 btn2->Show();
255 CPPUNIT_ASSERT_EQUAL_MESSAGE
256 (
257 "Point over child control corresponds to the child",
258 btn3->GetLabel(),
259 GetLabelOfWindowAtPoint(parent, 31, 111)
260 );
261
262 btn3->Disable();
263 CPPUNIT_ASSERT_EQUAL_MESSAGE
264 (
265 "Point over disabled child controls still corresponds to this child",
266 btn3->GetLabel(),
267 GetLabelOfWindowAtPoint(parent, 31, 111)
268 );
269
270 btn1->Destroy();
271 btn2->Destroy();
272 // btn3 was already deleted when its parent was
273 }