]>
git.saurik.com Git - wxWidgets.git/blob - tests/events/keyboard.cpp
8e28c60cda758838bc9097db8bee61830c9d99d4
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/keyboard.cpp
3 // Purpose: Test keyboard events
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2010 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
19 // FIXME: As all the other tests involving wxUIActionSimulator, this one is
20 // broken under OS X, the test window siply never gets any events.
21 #if wxUSE_UIACTIONSIMULATOR && !defined(__WXOSX__)
26 #include "wx/window.h"
29 #include "wx/uiaction.h"
30 #include "wx/vector.h"
35 // ----------------------------------------------------------------------------
36 // test window verifying the event generation
37 // ----------------------------------------------------------------------------
39 class KeyboardTestWindow
: public wxWindow
42 KeyboardTestWindow(wxWindow
*parent
)
43 : wxWindow(parent
, wxID_ANY
, wxPoint(0, 0), parent
->GetClientSize())
45 Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(KeyboardTestWindow::OnKeyDown
));
46 Connect(wxEVT_CHAR
, wxKeyEventHandler(KeyboardTestWindow::OnChar
));
47 Connect(wxEVT_KEY_UP
, wxKeyEventHandler(KeyboardTestWindow::OnKeyUp
));
50 unsigned GetKeyDownCount() const { return m_keyDownEvents
.size(); }
51 unsigned GetCharCount() const { return m_charEvents
.size(); }
52 unsigned GetKeyUpCount() const { return m_keyUpEvents
.size(); }
54 const wxKeyEvent
& GetKeyDownEvent(unsigned n
= 0) const
56 return m_keyDownEvents
[n
];
58 const wxKeyEvent
& GetCharEvent(unsigned n
= 0) const
60 return m_charEvents
[n
];
62 const wxKeyEvent
& GetKeyUpEvent(unsigned n
= 0) const
64 return m_keyUpEvents
[n
];
71 m_keyUpEvents
= wxVector
<wxKeyEvent
>();
75 void OnKeyDown(wxKeyEvent
& event
)
77 m_keyDownEvents
.push_back(event
);
81 void OnChar(wxKeyEvent
& event
)
83 m_charEvents
.push_back(event
);
87 void OnKeyUp(wxKeyEvent
& event
)
89 m_keyUpEvents
.push_back(event
);
93 wxVector
<wxKeyEvent
> m_keyDownEvents
,
98 wxDECLARE_NO_COPY_CLASS(KeyboardTestWindow
);
101 // Object describing the (main fields of) keyboard event.
104 KeyDesc(int keycode
, int mods
= 0)
105 : m_keycode(keycode
),
114 // Helper for ModKeyDown().
115 int GetModForKey(int keycode
)
119 case WXK_CONTROL
: return wxMOD_CONTROL
;
120 case WXK_SHIFT
: return wxMOD_SHIFT
;
121 case WXK_ALT
: return wxMOD_ALT
;
123 wxFAIL_MSG( "Unknown modifier key" );
129 // Helper function to allow writing just ModKeyDown(WXK_CONTROL) instead of
130 // more verbose KeyDesc(WXK_CONTROL, wxMOD_CONTROL).
131 KeyDesc
ModKeyDown(int keycode
)
133 return KeyDesc(keycode
, GetModForKey(keycode
));
136 // Another helper provided for symmetry with ModKeyDown() only.
137 KeyDesc
ModKeyUp(int keycode
)
139 return KeyDesc(keycode
);
142 // Verify that the event object corresponds to our idea of what it should be.
143 void TestEvent(int line
, const wxKeyEvent
& ev
, const KeyDesc
& desc
)
145 // Construct the message we'll display if an assert fails.
147 const wxEventType t
= ev
.GetEventType();
148 if ( t
== wxEVT_KEY_DOWN
)
150 else if ( t
== wxEVT_CHAR
)
152 else if ( t
== wxEVT_KEY_UP
)
155 CPPUNIT_FAIL( "unknown event type" );
157 msg
+= " event at line ";
158 msg
+= wxString::Format("%d", line
).mb_str();
161 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong key code in " + msg
,
166 if ( desc
.m_keycode
< WXK_START
)
168 // For Latin-1 our key code is the same as Unicode character value.
169 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong Unicode key in " + msg
,
170 (char)desc
.m_keycode
,
171 (char)ev
.GetUnicodeKey() );
175 // Key codes above WXK_START don't correspond to printable characters.
176 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong non-zero Unicode key in " + msg
,
178 (int)ev
.GetUnicodeKey() );
180 #endif // wxUSE_UNICODE
182 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong modifiers in " + msg
,
187 // Call TestEvent() passing it the line number from where it was called: this
188 // is useful for interpreting the assert failure messages.
189 #define ASSERT_KEY_EVENT_IS( ev, desc ) TestEvent(__LINE__, ev, desc)
191 } // anonymous namespace
193 // --------------------------------------------------------------------------
195 // --------------------------------------------------------------------------
197 class KeyboardEventTestCase
: public CppUnit::TestCase
200 KeyboardEventTestCase() {}
202 virtual void setUp();
203 virtual void tearDown();
206 CPPUNIT_TEST_SUITE( KeyboardEventTestCase
);
207 CPPUNIT_TEST( NormalLetter
);
208 CPPUNIT_TEST( NormalSpecial
);
209 CPPUNIT_TEST( CtrlLetter
);
210 CPPUNIT_TEST( CtrlSpecial
);
211 CPPUNIT_TEST( ShiftLetter
);
212 CPPUNIT_TEST( ShiftSpecial
);
213 CPPUNIT_TEST_SUITE_END();
216 void NormalSpecial();
222 KeyboardTestWindow
*m_win
;
224 wxDECLARE_NO_COPY_CLASS(KeyboardEventTestCase
);
227 wxREGISTER_UNIT_TEST(KeyboardEvent
);
229 void KeyboardEventTestCase::setUp()
231 m_win
= new KeyboardTestWindow(wxTheApp
->GetTopWindow());
233 wxYield(); // needed to show the new window
235 // The window might get some key up events when it's being shown if the key
236 // was pressed when the program was started and released after the window
237 // was shown, e.g. this does happen in practice when launching the test
238 // from command line. Simply discard all the spurious events so far.
239 m_win
->ClearEvents();
242 void KeyboardEventTestCase::tearDown()
247 void KeyboardEventTestCase::NormalLetter()
249 wxUIActionSimulator sim
;
253 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyDownCount() );
254 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(), 'A' );
256 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
257 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(), 'a' );
259 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyUpCount() );
260 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(), 'A' );
263 void KeyboardEventTestCase::NormalSpecial()
265 wxUIActionSimulator sim
;
269 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyDownCount() );
270 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(), WXK_END
);
272 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
273 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(), WXK_END
);
275 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyUpCount() );
276 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(), WXK_END
);
279 void KeyboardEventTestCase::CtrlLetter()
281 wxUIActionSimulator sim
;
282 sim
.Char('z', wxMOD_CONTROL
);
285 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
286 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
287 ModKeyDown(WXK_CONTROL
) );
288 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
289 KeyDesc('Z', wxMOD_CONTROL
) );
291 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
292 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
293 KeyDesc('\x1a', wxMOD_CONTROL
) );
295 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
296 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
297 KeyDesc('Z', wxMOD_CONTROL
) );
298 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
299 ModKeyUp(WXK_CONTROL
) );
302 void KeyboardEventTestCase::CtrlSpecial()
304 wxUIActionSimulator sim
;
305 sim
.Char(WXK_PAGEUP
, wxMOD_CONTROL
);
308 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
309 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
310 ModKeyDown(WXK_CONTROL
) );
311 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
312 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
314 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
315 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
316 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
318 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
319 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
320 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
321 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
322 ModKeyUp(WXK_CONTROL
) );
325 void KeyboardEventTestCase::ShiftLetter()
327 wxUIActionSimulator sim
;
328 sim
.Char('Q', wxMOD_SHIFT
);
331 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
332 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
333 ModKeyDown(WXK_SHIFT
) );
334 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
335 KeyDesc('Q', wxMOD_SHIFT
) );
337 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
338 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
339 KeyDesc('Q', wxMOD_SHIFT
) );
341 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
342 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
343 KeyDesc('Q', wxMOD_SHIFT
) );
344 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
345 ModKeyUp(WXK_SHIFT
) );
348 void KeyboardEventTestCase::ShiftSpecial()
350 wxUIActionSimulator sim
;
351 sim
.Char(WXK_F3
, wxMOD_SHIFT
);
354 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
355 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
356 ModKeyDown(WXK_SHIFT
) );
357 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
358 KeyDesc(WXK_F3
, wxMOD_SHIFT
) );
360 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
361 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
362 KeyDesc(WXK_F3
, wxMOD_SHIFT
) );
364 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
365 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
366 KeyDesc(WXK_F3
, wxMOD_SHIFT
) );
367 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
368 ModKeyUp(WXK_SHIFT
) );
371 #endif // wxUSE_UIACTIONSIMULATOR