From cf0ff2f31a3c066d9156efcea63118ae444da430 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Fri, 30 Apr 2010 12:44:35 +0200 Subject: Autotest: check that we receive key events on toplevel widgets Autotest add to test db322e8aec1cae Reviewed-by: denis --- tests/auto/macnativeevents/expectedeventlist.cpp | 43 ++++++++++++++++++---- tests/auto/macnativeevents/tst_macnativeevents.cpp | 23 ++++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/tests/auto/macnativeevents/expectedeventlist.cpp b/tests/auto/macnativeevents/expectedeventlist.cpp index b1fb9a6..0679dcb 100644 --- a/tests/auto/macnativeevents/expectedeventlist.cpp +++ b/tests/auto/macnativeevents/expectedeventlist.cpp @@ -97,6 +97,9 @@ void ExpectedEventList::compareMouseEvents(QEvent *received, QEvent *expected) { QMouseEvent *e1 = static_cast(received); QMouseEvent *e2 = static_cast(expected); + + // Do a manual check first to be able to write more sensible + // debug output if we know we're going to fail: if (e1->pos() == e2->pos() && (e1->globalPos() == e2->globalPos()) && (e1->button() == e2->button()) @@ -104,6 +107,9 @@ void ExpectedEventList::compareMouseEvents(QEvent *received, QEvent *expected) && (e1->modifiers() == e2->modifiers())) return; // equal + // INVARIANT: The two events are not equal. So we fail. Depending + // on whether debug mode is no or not, we let QTest fail. Otherwise + // we let the test continue for debugging puposes. int eventListNr = eventCount - eventList.size(); if (!debug) { qWarning() << "Expected event" << eventListNr << "differs from received event:"; @@ -119,12 +125,34 @@ void ExpectedEventList::compareMouseEvents(QEvent *received, QEvent *expected) } } -void ExpectedEventList::compareKeyEvents(QEvent *event1, QEvent *event2) +void ExpectedEventList::compareKeyEvents(QEvent *received, QEvent *expected) { - QKeyEvent *e1 = static_cast(event1); - QKeyEvent *e2 = static_cast(event2); - Q_UNUSED(e1); - Q_UNUSED(e2); + QKeyEvent *e1 = static_cast(received); + QKeyEvent *e2 = static_cast(expected); + + // Do a manual check first to be able to write more sensible + // debug output if we know we're going to fail: + if (e1->key() == e2->key() + && (e1->modifiers() == e2->modifiers()) + && (e1->count() == e2->count()) + && (e1->isAutoRepeat() == e2->isAutoRepeat())) + return; // equal + + // INVARIANT: The two events are not equal. So we fail. Depending + // on whether debug mode is no or not, we let QTest fail. Otherwise + // we let the test continue for debugging puposes. + int eventListNr = eventCount - eventList.size(); + if (!debug) { + qWarning() << "Expected event" << eventListNr << "differs from received event:"; + QCOMPARE(e1->key(), e2->key()); + QCOMPARE(e1->modifiers(), e2->modifiers()); + QCOMPARE(e1->count(), e2->count()); + QCOMPARE(e1->isAutoRepeat(), e2->isAutoRepeat()); + } else { + qWarning() << "*** FAIL *** : Expected event" << eventListNr << "differs from received event:"; + qWarning() << "Received:" << e1 << e1->key(); + qWarning() << "Expected:" << e2 << e2->key(); + } } bool ExpectedEventList::eventFilter(QObject *, QEvent *received) @@ -149,10 +177,9 @@ bool ExpectedEventList::eventFilter(QObject *, QEvent *received) compareMouseEvents(received, expected); break; } - case QEvent::KeyPress: { - break; - } + case QEvent::KeyPress: case QEvent::KeyRelease: { + compareKeyEvents(received, expected); break; } case QEvent::Resize: { diff --git a/tests/auto/macnativeevents/tst_macnativeevents.cpp b/tests/auto/macnativeevents/tst_macnativeevents.cpp index 70a14f5..ffd0596 100644 --- a/tests/auto/macnativeevents/tst_macnativeevents.cpp +++ b/tests/auto/macnativeevents/tst_macnativeevents.cpp @@ -67,6 +67,7 @@ private slots: void testDragWindow(); void testMouseEnter(); void testChildDialogInFrontOfModalParent(); + void testKeyPressOnToplevel(); }; void tst_MacNativeEvents::testMouseMoveLocation() @@ -307,6 +308,28 @@ void tst_MacNativeEvents::testChildDialogInFrontOfModalParent() QVERIFY(!child.isVisible()); } +void tst_MacNativeEvents::testKeyPressOnToplevel() +{ + // Check that we receive keyevents for + // toplevel widgets. For leagacy reasons, and according to Qt on + // other platforms (carbon port + linux), we should get these events + // even when the focus policy is set to Qt::NoFocus when there is no + // other focus widget on screen: + QWidget w; + w.show(); + + NativeEventList native; + native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, true, Qt::NoModifier)); + native.append(new QNativeKeyEvent(QNativeKeyEvent::Key_A, false, Qt::NoModifier)); + + ExpectedEventList expected(&w); + expected.append(new QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier)); + expected.append(new QKeyEvent(QEvent::KeyRelease, Qt::Key_A, Qt::NoModifier)); + + native.play(); + QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); +} + #include "tst_macnativeevents.moc" QTEST_MAIN(tst_MacNativeEvents) -- cgit v0.12