]>
git.saurik.com Git - wxWidgets.git/blob - tests/events/keyboard.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/events/keyboard.cpp
3 // Purpose: Test keyboard events
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2010 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
20 // FIXME: As all the other tests involving wxUIActionSimulator, this one is
21 // broken under OS X, the test window siply never gets any events.
22 #if wxUSE_UIACTIONSIMULATOR && !defined(__WXOSX__)
27 #include "wx/window.h"
30 #include "wx/uiaction.h"
31 #include "wx/vector.h"
36 // ----------------------------------------------------------------------------
37 // test window verifying the event generation
38 // ----------------------------------------------------------------------------
40 class KeyboardTestWindow
: public wxWindow
43 KeyboardTestWindow(wxWindow
*parent
)
44 : wxWindow(parent
, wxID_ANY
, wxPoint(0, 0), parent
->GetClientSize())
46 Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(KeyboardTestWindow::OnKeyDown
));
47 Connect(wxEVT_CHAR
, wxKeyEventHandler(KeyboardTestWindow::OnChar
));
48 Connect(wxEVT_KEY_UP
, wxKeyEventHandler(KeyboardTestWindow::OnKeyUp
));
51 unsigned GetKeyDownCount() const { return m_keyDownEvents
.size(); }
52 unsigned GetCharCount() const { return m_charEvents
.size(); }
53 unsigned GetKeyUpCount() const { return m_keyUpEvents
.size(); }
55 const wxKeyEvent
& GetKeyDownEvent(unsigned n
= 0) const
57 return m_keyDownEvents
[n
];
59 const wxKeyEvent
& GetCharEvent(unsigned n
= 0) const
61 return m_charEvents
[n
];
63 const wxKeyEvent
& GetKeyUpEvent(unsigned n
= 0) const
65 return m_keyUpEvents
[n
];
72 m_keyUpEvents
= wxVector
<wxKeyEvent
>();
76 void OnKeyDown(wxKeyEvent
& event
)
78 m_keyDownEvents
.push_back(event
);
82 void OnChar(wxKeyEvent
& event
)
84 m_charEvents
.push_back(event
);
88 void OnKeyUp(wxKeyEvent
& event
)
90 m_keyUpEvents
.push_back(event
);
94 wxVector
<wxKeyEvent
> m_keyDownEvents
,
99 wxDECLARE_NO_COPY_CLASS(KeyboardTestWindow
);
102 // Object describing the (main fields of) keyboard event.
105 KeyDesc(int keycode
, int mods
= 0)
106 : m_keycode(keycode
),
115 // Helper for ModKeyDown().
116 int GetModForKey(int keycode
)
120 case WXK_CONTROL
: return wxMOD_CONTROL
;
121 case WXK_SHIFT
: return wxMOD_SHIFT
;
122 case WXK_ALT
: return wxMOD_ALT
;
124 wxFAIL_MSG( "Unknown modifier key" );
130 // Helper function to allow writing just ModKeyDown(WXK_CONTROL) instead of
131 // more verbose KeyDesc(WXK_CONTROL, wxMOD_CONTROL).
132 KeyDesc
ModKeyDown(int keycode
)
134 return KeyDesc(keycode
, GetModForKey(keycode
));
137 // Another helper provided for symmetry with ModKeyDown() only.
138 KeyDesc
ModKeyUp(int keycode
)
140 return KeyDesc(keycode
);
143 // Verify that the event object corresponds to our idea of what it should be.
144 void TestEvent(int line
, const wxKeyEvent
& ev
, const KeyDesc
& desc
)
146 // Construct the message we'll display if an assert fails.
148 const wxEventType t
= ev
.GetEventType();
149 if ( t
== wxEVT_KEY_DOWN
)
151 else if ( t
== wxEVT_CHAR
)
153 else if ( t
== wxEVT_KEY_UP
)
156 CPPUNIT_FAIL( "unknown event type" );
158 msg
+= " event at line ";
159 msg
+= wxString::Format("%d", line
).mb_str();
162 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong key code in " + msg
,
167 if ( desc
.m_keycode
< WXK_START
)
169 // For Latin-1 our key code is the same as Unicode character value.
170 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong Unicode key in " + msg
,
171 (char)desc
.m_keycode
,
172 (char)ev
.GetUnicodeKey() );
176 // Key codes above WXK_START don't correspond to printable characters.
177 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong non-zero Unicode key in " + msg
,
179 (int)ev
.GetUnicodeKey() );
181 #endif // wxUSE_UNICODE
183 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong modifiers in " + msg
,
188 // Call TestEvent() passing it the line number from where it was called: this
189 // is useful for interpreting the assert failure messages.
190 #define ASSERT_KEY_EVENT_IS( ev, desc ) TestEvent(__LINE__, ev, desc)
192 } // anonymous namespace
194 // --------------------------------------------------------------------------
196 // --------------------------------------------------------------------------
198 class KeyboardEventTestCase
: public CppUnit::TestCase
201 KeyboardEventTestCase() {}
203 virtual void setUp();
204 virtual void tearDown();
207 CPPUNIT_TEST_SUITE( KeyboardEventTestCase
);
208 CPPUNIT_TEST( NormalLetter
);
209 CPPUNIT_TEST( NormalSpecial
);
210 CPPUNIT_TEST( CtrlLetter
);
211 CPPUNIT_TEST( CtrlSpecial
);
212 CPPUNIT_TEST( ShiftLetter
);
213 CPPUNIT_TEST( ShiftSpecial
);
214 CPPUNIT_TEST_SUITE_END();
217 void NormalSpecial();
223 KeyboardTestWindow
*m_win
;
225 wxDECLARE_NO_COPY_CLASS(KeyboardEventTestCase
);
228 wxREGISTER_UNIT_TEST(KeyboardEvent
);
230 void KeyboardEventTestCase::setUp()
232 m_win
= new KeyboardTestWindow(wxTheApp
->GetTopWindow());
234 wxYield(); // needed to show the new window
236 // The window might get some key up events when it's being shown if the key
237 // was pressed when the program was started and released after the window
238 // was shown, e.g. this does happen in practice when launching the test
239 // from command line. Simply discard all the spurious events so far.
240 m_win
->ClearEvents();
243 void KeyboardEventTestCase::tearDown()
248 void KeyboardEventTestCase::NormalLetter()
250 wxUIActionSimulator sim
;
254 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyDownCount() );
255 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(), 'A' );
257 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
258 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(), 'a' );
260 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyUpCount() );
261 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(), 'A' );
264 void KeyboardEventTestCase::NormalSpecial()
266 wxUIActionSimulator sim
;
270 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyDownCount() );
271 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(), WXK_END
);
273 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
274 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(), WXK_END
);
276 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyUpCount() );
277 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(), WXK_END
);
280 void KeyboardEventTestCase::CtrlLetter()
282 wxUIActionSimulator sim
;
283 sim
.Char('z', wxMOD_CONTROL
);
286 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
287 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
288 ModKeyDown(WXK_CONTROL
) );
289 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
290 KeyDesc('Z', wxMOD_CONTROL
) );
292 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
293 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
294 KeyDesc('\x1a', wxMOD_CONTROL
) );
296 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
297 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
298 KeyDesc('Z', wxMOD_CONTROL
) );
299 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
300 ModKeyUp(WXK_CONTROL
) );
303 void KeyboardEventTestCase::CtrlSpecial()
305 wxUIActionSimulator sim
;
306 sim
.Char(WXK_PAGEUP
, wxMOD_CONTROL
);
309 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
310 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
311 ModKeyDown(WXK_CONTROL
) );
312 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
313 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
315 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
316 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
317 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
319 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
320 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
321 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
322 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
323 ModKeyUp(WXK_CONTROL
) );
326 void KeyboardEventTestCase::ShiftLetter()
328 wxUIActionSimulator sim
;
329 sim
.Char('Q', wxMOD_SHIFT
);
332 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
333 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
334 ModKeyDown(WXK_SHIFT
) );
335 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
336 KeyDesc('Q', wxMOD_SHIFT
) );
338 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
339 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
340 KeyDesc('Q', wxMOD_SHIFT
) );
342 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
343 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
344 KeyDesc('Q', wxMOD_SHIFT
) );
345 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
346 ModKeyUp(WXK_SHIFT
) );
349 void KeyboardEventTestCase::ShiftSpecial()
351 wxUIActionSimulator sim
;
352 sim
.Char(WXK_F3
, wxMOD_SHIFT
);
355 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
356 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
357 ModKeyDown(WXK_SHIFT
) );
358 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
359 KeyDesc(WXK_F3
, wxMOD_SHIFT
) );
361 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
362 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
363 KeyDesc(WXK_F3
, wxMOD_SHIFT
) );
365 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
366 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
367 KeyDesc(WXK_F3
, wxMOD_SHIFT
) );
368 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
369 ModKeyUp(WXK_SHIFT
) );
372 #endif // wxUSE_UIACTIONSIMULATOR