diff options
author | Richard Moe Gustavsen <richard.gustavsen@nokia.com> | 2010-03-24 12:19:38 (GMT) |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@nokia.com> | 2010-04-07 10:35:56 (GMT) |
commit | 600ec57606ef0c02a51f9875e0a201ff71d09c19 (patch) | |
tree | 9e45099154ed07949b9568a285934c729d5c29f8 /tests | |
parent | 757e9af177544eaf6b66641ea724716ccd4b2701 (diff) | |
download | Qt-600ec57606ef0c02a51f9875e0a201ff71d09c19.zip Qt-600ec57606ef0c02a51f9875e0a201ff71d09c19.tar.gz Qt-600ec57606ef0c02a51f9875e0a201ff71d09c19.tar.bz2 |
Autotest: added more tests to macnativeevents
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/macnativeevents/expectedeventlist.cpp | 169 | ||||
-rw-r--r-- | tests/auto/macnativeevents/expectedeventlist.h | 70 | ||||
-rw-r--r-- | tests/auto/macnativeevents/macnativeevents.pro | 6 | ||||
-rw-r--r-- | tests/auto/macnativeevents/nativeeventlist.cpp (renamed from tests/auto/macnativeevents/qnativeplayer.cpp) | 76 | ||||
-rw-r--r-- | tests/auto/macnativeevents/nativeeventlist.h (renamed from tests/auto/macnativeevents/qnativeplayer.h) | 29 | ||||
-rw-r--r-- | tests/auto/macnativeevents/qnativeevents.cpp (renamed from tests/auto/macnativeevents/qnativeinput.cpp) | 2 | ||||
-rw-r--r-- | tests/auto/macnativeevents/qnativeevents.h (renamed from tests/auto/macnativeevents/qnativeinput.h) | 0 | ||||
-rw-r--r-- | tests/auto/macnativeevents/qnativeevents_mac.cpp (renamed from tests/auto/macnativeevents/qnativeinput_mac.cpp) | 2 | ||||
-rw-r--r-- | tests/auto/macnativeevents/tst_macnativeevents.cpp | 124 |
9 files changed, 391 insertions, 87 deletions
diff --git a/tests/auto/macnativeevents/expectedeventlist.cpp b/tests/auto/macnativeevents/expectedeventlist.cpp new file mode 100644 index 0000000..48cfe1f --- /dev/null +++ b/tests/auto/macnativeevents/expectedeventlist.cpp @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "expectedeventlist.h" +#include <QDebug> +#include <QCoreApplication> +#include <QAbstractEventDispatcher> +#include <QtTest/QtTest> + +ExpectedEventList::ExpectedEventList(QObject *target) + : QObject(target), eventCount(0) +{ + target->installEventFilter(this); +} + +ExpectedEventList::~ExpectedEventList() +{ + qDeleteAll(eventList); +} + +void ExpectedEventList::append(QEvent *e) +{ + eventList.append(e); + ++eventCount; +} + +void ExpectedEventList::timerEvent(QTimerEvent *) +{ + timer.stop(); + QAbstractEventDispatcher::instance()->interrupt(); +} + +bool ExpectedEventList::waitForAllEvents(int maxEventWaitTime) +{ + if (eventList.isEmpty()) + return true; + + int eventCount = eventList.size(); + timer.start(maxEventWaitTime, this); + + while (timer.isActive()) { + QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents); + if (eventList.isEmpty()) + return true; + + if (eventCount < eventList.size()){ + eventCount = eventList.size(); + timer.start(maxEventWaitTime, this); + } + } + + int eventListNr = eventCount - eventList.size() + 1; + qWarning() << "Stopped waiting for expected event nr" << eventListNr; + return false; +} + +void ExpectedEventList::compareMouseEvents(QEvent *expected, QEvent *received) +{ + QMouseEvent *e1 = static_cast<QMouseEvent *>(expected); + QMouseEvent *e2 = static_cast<QMouseEvent *>(received); + if (e1->pos() == e2->pos() + && (e1->globalPos() == e2->globalPos()) + && (e1->button() == e2->button()) + && (e1->buttons() == e2->buttons()) + && (e1->modifiers() == e2->modifiers())) + return; // equal + + int eventListNr = eventCount - eventList.size(); + qWarning() << "Expected event" << eventListNr << "about to fail:"; + qWarning() << "Expected:" << e1; + qWarning() << "Received:" << e2; + QCOMPARE(e1->pos(), e2->pos()); + QCOMPARE(e1->globalPos(), e2->globalPos()); + QCOMPARE(e1->button(), e2->button()); + QCOMPARE(e1->buttons(), e2->buttons()); + QCOMPARE(e1->modifiers(), e2->modifiers()); +} + +void ExpectedEventList::compareKeyEvents(QEvent *event1, QEvent *event2) +{ + QKeyEvent *e1 = static_cast<QKeyEvent *>(event1); + QKeyEvent *e2 = static_cast<QKeyEvent *>(event2); + Q_UNUSED(e1); + Q_UNUSED(e2); +} + +bool ExpectedEventList::eventFilter(QObject *, QEvent *received) +{ + if (eventList.isEmpty()) + return false; + + QEvent *expected = eventList.first(); + if (expected->type() == received->type()) { + eventList.removeFirst(); + switch (received->type()) { + case QEvent::MouseButtonPress: + case QEvent::MouseButtonRelease: + case QEvent::MouseMove: + case QEvent::MouseButtonDblClick: + case QEvent::NonClientAreaMouseButtonPress: + case QEvent::NonClientAreaMouseButtonRelease: + case QEvent::NonClientAreaMouseButtonDblClick: + case QEvent::NonClientAreaMouseMove: { + compareMouseEvents(expected, received); + break; + } + case QEvent::KeyPress: { + break; + } + case QEvent::KeyRelease: { + break; + } + case QEvent::Resize: { + break; + } + case QEvent::WindowActivate: { + break; + } + case QEvent::WindowDeactivate: { + break; + } + default: + break; + } + if (eventList.isEmpty()) + QAbstractEventDispatcher::instance()->interrupt(); + } + + return false; +} + diff --git a/tests/auto/macnativeevents/expectedeventlist.h b/tests/auto/macnativeevents/expectedeventlist.h new file mode 100644 index 0000000..3bdbb67 --- /dev/null +++ b/tests/auto/macnativeevents/expectedeventlist.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef EVENTFILTER +#define EVENTFILTER + +#include <QWidget> +#include <QList> +#include <QEvent> +#include <QBasicTimer> + +class ExpectedEventList : public QObject +{ + QList<QEvent *> eventList; + QBasicTimer timer; + int eventCount; + void timerEvent(QTimerEvent *); + +public: + ExpectedEventList(QObject *target); + ~ExpectedEventList(); + void append(QEvent *e); + bool waitForAllEvents(int timeoutPerEvent = 2000); + bool eventFilter(QObject *obj, QEvent *event); + +private: + void compareMouseEvents(QEvent *event1, QEvent *event2); + void compareKeyEvents(QEvent *event1, QEvent *event2); +}; + +#endif + diff --git a/tests/auto/macnativeevents/macnativeevents.pro b/tests/auto/macnativeevents/macnativeevents.pro index a0293d4..af34942 100644 --- a/tests/auto/macnativeevents/macnativeevents.pro +++ b/tests/auto/macnativeevents/macnativeevents.pro @@ -8,9 +8,9 @@ DEPENDPATH += . INCLUDEPATH += . LIBS += -framework Carbon -HEADERS += qnativeinput.h qnativeplayer.h -SOURCES += qnativeinput.cpp qnativeplayer.cpp qnativeinput_mac.cpp - +HEADERS += qnativeevents.h nativeeventlist.h expectedeventlist.h +SOURCES += qnativeevents.cpp qnativeevents_mac.cpp +SOURCES += expectedeventlist.cpp nativeeventlist.cpp SOURCES += tst_macnativeevents.cpp requires(mac) diff --git a/tests/auto/macnativeevents/qnativeplayer.cpp b/tests/auto/macnativeevents/nativeeventlist.cpp index 92298ef..614e9d0 100644 --- a/tests/auto/macnativeevents/qnativeplayer.cpp +++ b/tests/auto/macnativeevents/nativeeventlist.cpp @@ -39,22 +39,23 @@ ** ****************************************************************************/ -#include "qnativeplayer.h" +#include "nativeeventlist.h" -QNativePlayer::QNativePlayer() +NativeEventList::NativeEventList(int defaultWaitMs) + : playbackMultiplier(1.0) + , currIndex(-1) + , wait(false) + , defaultWaitMs(defaultWaitMs) { - currIndex = -1; - playbackMultiplier = 1.0; - wait = false; } -QNativePlayer::~QNativePlayer() +NativeEventList::~NativeEventList() { for (int i=0; i<eventList.size(); i++) delete eventList.takeAt(i).second; } -void QNativePlayer::sendNextEvent() +void NativeEventList::sendNextEvent() { QNativeEvent *e = eventList.at(currIndex).second; if (e) @@ -62,7 +63,7 @@ void QNativePlayer::sendNextEvent() waitNextEvent(); } -void QNativePlayer::waitNextEvent() +void NativeEventList::waitNextEvent() { if (++currIndex >= eventList.size()){ emit done(); @@ -74,12 +75,22 @@ void QNativePlayer::waitNextEvent() QTimer::singleShot(interval * playbackMultiplier, this, SLOT(sendNextEvent())); } -void QNativePlayer::append(int waitMs, QNativeEvent *event) +void NativeEventList::append(QNativeEvent *event) +{ + eventList.append(QPair<int, QNativeEvent *>(defaultWaitMs, event)); +} + +void NativeEventList::append(int waitMs, QNativeEvent *event) { eventList.append(QPair<int, QNativeEvent *>(waitMs, event)); } -void QNativePlayer::play(Playback playback) +void NativeEventList::append(int waitMs) +{ + eventList.append(QPair<int, QNativeEvent *>(waitMs, 0)); +} + +void NativeEventList::play(Playback playback) { waitNextEvent(); @@ -88,52 +99,9 @@ void QNativePlayer::play(Playback playback) QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents); } -void QNativePlayer::stop() +void NativeEventList::stop() { wait = false; QAbstractEventDispatcher::instance()->interrupt(); } -// ************************************************************************ - -QEventOutputList::QEventOutputList() -{ - wait = true; -} - -QEventOutputList::~QEventOutputList() -{ - qDeleteAll(*this); -} - -bool QEventOutputList::waitUntilEmpty(int maxEventWaitTime) -{ - int currSize = size(); - QTime time; - time.restart(); - while (wait){ - QCoreApplication::processEvents(QEventLoop::AllEvents, 50); - - if (isEmpty()){ - return true; - } - else if (currSize == size()){ - if (time.elapsed() > maxEventWaitTime){ - return false; - } - } - else{ - currSize = size(); - time.restart(); - } - } - return false; -} - -void QEventOutputList::sleep(int sleepTime) -{ - QTime time; - time.restart(); - while (time.elapsed() < sleepTime) - QCoreApplication::processEvents(QEventLoop::AllEvents, 50); -} diff --git a/tests/auto/macnativeevents/qnativeplayer.h b/tests/auto/macnativeevents/nativeeventlist.h index 61ee162..886ab78 100644 --- a/tests/auto/macnativeevents/qnativeplayer.h +++ b/tests/auto/macnativeevents/nativeeventlist.h @@ -43,19 +43,22 @@ #define Q_NATIVE_PLAYBACK #include <QtCore> -#include "qnativeinput.h" +#include "qnativeevents.h" -class QNativePlayer : public QObject +class NativeEventList : public QObject { Q_OBJECT; public: enum Playback {ReturnImmediately, WaitUntilFinished}; - QNativePlayer(); - ~QNativePlayer(); + NativeEventList(int defaultWaitMs = 20); + ~NativeEventList(); + void append(QNativeEvent *event); void append(int waitMs, QNativeEvent *event = 0); + void append(int waitMs); + void play(Playback playback = WaitUntilFinished); void stop(); float playbackMultiplier; @@ -66,27 +69,13 @@ signals: private slots: void sendNextEvent(); - private: +private: void waitNextEvent(); QList<QPair<int, QNativeEvent *> > eventList; int currIndex; bool wait; + int defaultWaitMs; }; -// ****************************************************************** - -class QEventOutputList : public QList<QEvent *> -{ -public: - QEventOutputList(); - ~QEventOutputList(); - bool waitUntilEmpty(int maxEventWaitTime = 1000); - bool wait; - - // Useful method. Just sleep and process events: - static void sleep(int sleepTime); -}; - - #endif diff --git a/tests/auto/macnativeevents/qnativeinput.cpp b/tests/auto/macnativeevents/qnativeevents.cpp index c9462a6..cb4b82e 100644 --- a/tests/auto/macnativeevents/qnativeinput.cpp +++ b/tests/auto/macnativeevents/qnativeevents.cpp @@ -39,7 +39,7 @@ ** ****************************************************************************/ -#include "qnativeinput.h" +#include "qnativeevents.h" QNativeInput::QNativeInput(bool subscribe) { diff --git a/tests/auto/macnativeevents/qnativeinput.h b/tests/auto/macnativeevents/qnativeevents.h index a98e4e4..a98e4e4 100644 --- a/tests/auto/macnativeevents/qnativeinput.h +++ b/tests/auto/macnativeevents/qnativeevents.h diff --git a/tests/auto/macnativeevents/qnativeinput_mac.cpp b/tests/auto/macnativeevents/qnativeevents_mac.cpp index 143a633..6c04bf3 100644 --- a/tests/auto/macnativeevents/qnativeinput_mac.cpp +++ b/tests/auto/macnativeevents/qnativeevents_mac.cpp @@ -39,7 +39,7 @@ ** ****************************************************************************/ -#include "qnativeinput.h" +#include "qnativeevents.h" #include <Carbon/Carbon.h> #include <QtCore> diff --git a/tests/auto/macnativeevents/tst_macnativeevents.cpp b/tests/auto/macnativeevents/tst_macnativeevents.cpp index ccadd54..c6da3ff 100644 --- a/tests/auto/macnativeevents/tst_macnativeevents.cpp +++ b/tests/auto/macnativeevents/tst_macnativeevents.cpp @@ -44,8 +44,9 @@ #include <QPushButton> #include <QtTest/QtTest> -#include "qnativeinput.h" -#include "qnativeplayer.h" +#include "qnativeevents.h" +#include "nativeeventlist.h" +#include "expectedeventlist.h" #ifdef Q_OS_MAC @@ -55,19 +56,126 @@ class tst_MacNativeEvents : public QObject { Q_OBJECT private slots: - void testLeftMousePressRelease(); + void testMouseMoveLocation(); + void testPushButtonPressRelease(); + void testMouseLeftDoubleClick(); + void stressTestMouseLeftDoubleClick(); + void testMouseDragInside(); }; -void tst_MacNativeEvents::testLeftMousePressRelease() +void tst_MacNativeEvents::testMouseMoveLocation() { + QWidget w; + w.setMouseTracking(true); + w.show(); + QPoint p = w.geometry().center(); + + NativeEventList native; + native.append(new QNativeMouseMoveEvent(p, Qt::NoModifier)); + + ExpectedEventList expected(&w); + expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p), p, Qt::NoButton, Qt::NoButton, Qt::NoModifier)); + + native.play(); + QVERIFY2(expected.waitForAllEvents(500), "the test did not receive all expected events!"); +} + +void tst_MacNativeEvents::testPushButtonPressRelease() +{ + // Check that a native mouse press and release generates the + // same qevents on a pushbutton: QPushButton w("click me"); w.show(); QPoint p = w.geometry().center(); - QNativePlayer player; - player.append(50, new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier)); - player.append(50, new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); - player.play(); + NativeEventList native; + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier)); + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); + + ExpectedEventList expected(&w); + expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); + + native.play(); + QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); +} + +void tst_MacNativeEvents::testMouseLeftDoubleClick() +{ + // Check that a native double click makes + // the test widget receive a press-release-click-release: + QWidget w; + w.show(); + QPoint p = w.geometry().center(); + + NativeEventList native; + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier)); + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 2, Qt::NoModifier)); + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); + + ExpectedEventList expected(&w); + expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseButtonDblClick, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); + + native.play(); + QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); +} + +void tst_MacNativeEvents::stressTestMouseLeftDoubleClick() +{ + // Check that multiple, fast, double clicks makes + // the test widget receive correct click events + QWidget w; + w.show(); + QPoint p = w.geometry().center(); + + NativeEventList native; + ExpectedEventList expected(&w); + + for (int i=0; i<10; ++i){ + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 1, Qt::NoModifier)); + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 2, Qt::NoModifier)); + native.append(new QNativeMouseButtonEvent(p, Qt::LeftButton, 0, Qt::NoModifier)); + + expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseButtonDblClick, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p), p, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); + } + + native.play(); + QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); +} + +void tst_MacNativeEvents::testMouseDragInside() +{ + // Check that a mouse drag inside a widget + // will cause press-move-release events to be delivered + QWidget w; + w.show(); + QPoint p1 = w.geometry().center(); + QPoint p2 = p1 - QPoint(10, 0); + QPoint p3 = p1 - QPoint(20, 0); + QPoint p4 = p1 - QPoint(30, 0); + + NativeEventList native; + native.append(new QNativeMouseButtonEvent(p1, Qt::LeftButton, 1, Qt::NoModifier)); + native.append(new QNativeMouseDragEvent(p2, Qt::LeftButton, Qt::NoModifier)); + native.append(new QNativeMouseDragEvent(p3, Qt::LeftButton, Qt::NoModifier)); + native.append(new QNativeMouseButtonEvent(p4, Qt::LeftButton, 0, Qt::NoModifier)); + + ExpectedEventList expected(&w); + expected.append(new QMouseEvent(QEvent::MouseButtonPress, w.mapFromGlobal(p1), p1, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p2), p2, Qt::NoButton, Qt::LeftButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseMove, w.mapFromGlobal(p3), p3, Qt::NoButton, Qt::LeftButton, Qt::NoModifier)); + expected.append(new QMouseEvent(QEvent::MouseButtonRelease, w.mapFromGlobal(p4), p4, Qt::LeftButton, Qt::NoButton, Qt::NoModifier)); + + native.play(); + QVERIFY2(expected.waitForAllEvents(), "the test did not receive all expected events!"); } #include "tst_macnativeevents.moc" |