Correctly handle null windows or widgets in keyClick()

Allow passing of null windows or widgets to keyClick().
In that case route the event through the normal Qt event
processing. This allows e.g. shortcuts to catch the
key event.

Change-Id: Ic9455ea9be5164918b1c0bccbd58dd32eae74ff1
Reviewed-by: Jason McDonald <jason.mcdonald@nokia.com>
This commit is contained in:
Lars Knoll 2011-10-21 11:01:21 +02:00 committed by Qt by Nokia
parent 79fb588030
commit 5761d8546f
2 changed files with 13 additions and 0 deletions

View File

@ -85,8 +85,13 @@ namespace QTest
QString text, Qt::KeyboardModifiers modifier, int delay=-1)
{
QTEST_ASSERT(qApp);
if (!window)
window = QGuiApplication::activeWindow();
QTEST_ASSERT(window);
if (action == Click) {
sendKeyEvent(Press, window, code, text, modifier, delay);
sendKeyEvent(Release, window, code, text, modifier, delay);
@ -183,6 +188,12 @@ namespace QTest
if (!widget)
widget = QWidget::keyboardGrabber();
if (!widget) {
QWindow *window = QGuiApplication::focusWindow();
if (window) {
sendKeyEvent(action, window, code, text, modifier, delay);
return;
}
if (QWidget *apw = QApplication::activePopupWidget())
widget = apw->focusWidget() ? apw->focusWidget() : apw;
else

View File

@ -1249,10 +1249,12 @@ void tst_QMenuBar::taskQTBUG4965_escapeEaten()
menubar.setActiveAction(first);
QTRY_VERIFY(menu.isVisible());
QCOMPARE(menubar.activeAction(), first);
QTest::qWaitForWindowShown(&menu);
QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_Escape);
QVERIFY(!menu.isVisible());
QTRY_VERIFY(menubar.hasFocus());
QCOMPARE(menubar.activeAction(), first);
QTest::qWait(200);
QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_Escape);
QVERIFY(!menubar.activeAction());
QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_Escape); //now the action should be triggered