]>
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 #if wxUSE_UIACTIONSIMULATOR
25 #include "wx/window.h"
28 #include "wx/uiaction.h"
29 #include "wx/vector.h"
34 // ----------------------------------------------------------------------------
35 // test window verifying the event generation
36 // ----------------------------------------------------------------------------
38 class KeyboardTestWindow
: public wxWindow
41 KeyboardTestWindow(wxWindow
*parent
)
42 : wxWindow(parent
, wxID_ANY
)
44 Connect(wxEVT_KEY_DOWN
, wxKeyEventHandler(KeyboardTestWindow::OnKeyDown
));
45 Connect(wxEVT_CHAR
, wxKeyEventHandler(KeyboardTestWindow::OnChar
));
46 Connect(wxEVT_KEY_UP
, wxKeyEventHandler(KeyboardTestWindow::OnKeyUp
));
49 unsigned GetKeyDownCount() const { return m_keyDownEvents
.size(); }
50 unsigned GetCharCount() const { return m_charEvents
.size(); }
51 unsigned GetKeyUpCount() const { return m_keyUpEvents
.size(); }
53 const wxKeyEvent
& GetKeyDownEvent(unsigned n
= 0) const
55 return m_keyDownEvents
[n
];
57 const wxKeyEvent
& GetCharEvent(unsigned n
= 0) const
59 return m_charEvents
[n
];
61 const wxKeyEvent
& GetKeyUpEvent(unsigned n
= 0) const
63 return m_keyUpEvents
[n
];
70 m_keyUpEvents
= wxVector
<wxKeyEvent
>();
74 void OnKeyDown(wxKeyEvent
& event
)
76 m_keyDownEvents
.push_back(event
);
80 void OnChar(wxKeyEvent
& event
)
82 m_charEvents
.push_back(event
);
86 void OnKeyUp(wxKeyEvent
& event
)
88 m_keyUpEvents
.push_back(event
);
92 wxVector
<wxKeyEvent
> m_keyDownEvents
,
97 wxDECLARE_NO_COPY_CLASS(KeyboardTestWindow
);
100 // Object describing the (main fields of) keyboard event.
103 KeyDesc(int keycode
, int mods
= 0)
104 : m_keycode(keycode
),
113 // Helper for ModKeyDown().
114 int GetModForKey(int keycode
)
118 case WXK_CONTROL
: return wxMOD_CONTROL
;
119 case WXK_SHIFT
: return wxMOD_SHIFT
;
120 case WXK_ALT
: return wxMOD_ALT
;
122 wxFAIL_MSG( "Unknown modifier key" );
128 // Helper function to allow writing just ModKeyDown(WXK_CONTROL) instead of
129 // more verbose KeyDesc(WXK_CONTROL, wxMOD_CONTROL).
130 KeyDesc
ModKeyDown(int keycode
)
132 return KeyDesc(keycode
, GetModForKey(keycode
));
135 // Another helper provided for symmetry with ModKeyDown() only.
136 KeyDesc
ModKeyUp(int keycode
)
138 return KeyDesc(keycode
);
141 // Verify that the event object corresponds to our idea of what it should be.
142 void TestEvent(int line
, const wxKeyEvent
& ev
, const KeyDesc
& desc
)
144 // Construct the message we'll display if an assert fails.
146 const wxEventType t
= ev
.GetEventType();
147 if ( t
== wxEVT_KEY_DOWN
)
149 else if ( t
== wxEVT_CHAR
)
151 else if ( t
== wxEVT_KEY_UP
)
154 CPPUNIT_FAIL( "unknown event type" );
156 msg
+= " event at line ";
157 msg
+= wxString::Format("%d", line
).mb_str();
160 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong key code in " + msg
,
165 if ( desc
.m_keycode
< WXK_START
)
167 // For Latin-1 our key code is the same as Unicode character value.
168 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong Unicode key in " + msg
,
169 (char)desc
.m_keycode
,
170 (char)ev
.GetUnicodeKey() );
174 // Key codes above WXK_START don't correspond to printable characters.
175 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong non-zero Unicode key in " + msg
,
177 (int)ev
.GetUnicodeKey() );
179 #endif // wxUSE_UNICODE
181 CPPUNIT_ASSERT_EQUAL_MESSAGE( "wrong modifiers in " + msg
,
186 // Call TestEvent() passing it the line number from where it was called: this
187 // is useful for interpreting the assert failure messages.
188 #define ASSERT_KEY_EVENT_IS( ev, desc ) TestEvent(__LINE__, ev, desc)
190 } // anonymous namespace
192 // --------------------------------------------------------------------------
194 // --------------------------------------------------------------------------
196 class KeyboardEventTestCase
: public CppUnit::TestCase
199 KeyboardEventTestCase() {}
201 virtual void setUp();
202 virtual void tearDown();
205 CPPUNIT_TEST_SUITE( KeyboardEventTestCase
);
206 CPPUNIT_TEST( NormalLetter
);
207 CPPUNIT_TEST( NormalSpecial
);
208 CPPUNIT_TEST( CtrlLetter
);
209 CPPUNIT_TEST( CtrlSpecial
);
210 CPPUNIT_TEST( ShiftLetter
);
211 CPPUNIT_TEST( ShiftSpecial
);
212 CPPUNIT_TEST_SUITE_END();
215 void NormalSpecial();
221 KeyboardTestWindow
*m_win
;
223 wxDECLARE_NO_COPY_CLASS(KeyboardEventTestCase
);
226 wxREGISTER_UNIT_TEST(KeyboardEvent
);
228 void KeyboardEventTestCase::setUp()
230 m_win
= new KeyboardTestWindow(wxTheApp
->GetTopWindow());
232 wxYield(); // needed to show the new window
234 // The window might get some key up events when it's being shown if the key
235 // was pressed when the program was started and released after the window
236 // was shown, e.g. this does happen in practice when launching the test
237 // from command line. Simply discard all the spurious events so far.
238 m_win
->ClearEvents();
241 void KeyboardEventTestCase::tearDown()
246 void KeyboardEventTestCase::NormalLetter()
248 wxUIActionSimulator sim
;
252 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyDownCount() );
253 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(), 'A' );
255 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
256 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(), 'a' );
258 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyUpCount() );
259 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(), 'A' );
262 void KeyboardEventTestCase::NormalSpecial()
264 wxUIActionSimulator sim
;
268 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyDownCount() );
269 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(), WXK_END
);
271 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
272 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(), WXK_END
);
274 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetKeyUpCount() );
275 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(), WXK_END
);
278 void KeyboardEventTestCase::CtrlLetter()
280 wxUIActionSimulator sim
;
281 sim
.Char('z', wxMOD_CONTROL
);
284 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
285 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
286 ModKeyDown(WXK_CONTROL
) );
287 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
288 KeyDesc('Z', wxMOD_CONTROL
) );
290 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
291 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
292 KeyDesc('\x1a', wxMOD_CONTROL
) );
294 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
295 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
296 KeyDesc('Z', wxMOD_CONTROL
) );
297 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
298 ModKeyUp(WXK_CONTROL
) );
301 void KeyboardEventTestCase::CtrlSpecial()
303 wxUIActionSimulator sim
;
304 sim
.Char(WXK_PAGEUP
, wxMOD_CONTROL
);
307 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
308 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
309 ModKeyDown(WXK_CONTROL
) );
310 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
311 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
313 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
314 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
315 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
317 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
318 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
319 KeyDesc(WXK_PAGEUP
, wxMOD_CONTROL
) );
320 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
321 ModKeyUp(WXK_CONTROL
) );
324 void KeyboardEventTestCase::ShiftLetter()
326 wxUIActionSimulator sim
;
327 sim
.Char('Q', wxMOD_SHIFT
);
330 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
331 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
332 ModKeyDown(WXK_SHIFT
) );
333 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
334 KeyDesc('Q', wxMOD_SHIFT
) );
336 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
337 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
338 KeyDesc('Q', wxMOD_SHIFT
) );
340 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
341 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
342 KeyDesc('Q', wxMOD_SHIFT
) );
343 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
344 ModKeyUp(WXK_SHIFT
) );
347 void KeyboardEventTestCase::ShiftSpecial()
349 wxUIActionSimulator sim
;
350 sim
.Char(WXK_TAB
, wxMOD_SHIFT
);
353 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyDownCount() );
354 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(0),
355 ModKeyDown(WXK_SHIFT
) );
356 ASSERT_KEY_EVENT_IS( m_win
->GetKeyDownEvent(1),
357 KeyDesc(WXK_TAB
, wxMOD_SHIFT
) );
359 CPPUNIT_ASSERT_EQUAL( 1, m_win
->GetCharCount() );
360 ASSERT_KEY_EVENT_IS( m_win
->GetCharEvent(),
361 KeyDesc(WXK_TAB
, wxMOD_SHIFT
) );
363 CPPUNIT_ASSERT_EQUAL( 2, m_win
->GetKeyUpCount() );
364 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(0),
365 KeyDesc(WXK_TAB
, wxMOD_SHIFT
) );
366 ASSERT_KEY_EVENT_IS( m_win
->GetKeyUpEvent(1),
367 ModKeyUp(WXK_SHIFT
) );
370 #endif // wxUSE_UIACTIONSIMULATOR