tests/auto/macnativeevents/expectedeventlist.cpp
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "expectedeventlist.h"
       
    43 #include <QDebug>
       
    44 #include <QCoreApplication>
       
    45 #include <QAbstractEventDispatcher>
       
    46 #include <QtTest/QtTest>
       
    47 
       
    48 ExpectedEventList::ExpectedEventList(QObject *target)
       
    49     : QObject(target), eventCount(0)
       
    50 {
       
    51     target->installEventFilter(this);
       
    52     debug = !qgetenv("NATIVEDEBUG").isEmpty();
       
    53 }
       
    54 
       
    55 ExpectedEventList::~ExpectedEventList()
       
    56 {
       
    57     qDeleteAll(eventList);
       
    58 }
       
    59 
       
    60 void ExpectedEventList::append(QEvent *e)
       
    61 {
       
    62     eventList.append(e);
       
    63     ++eventCount;
       
    64 }
       
    65 
       
    66 void ExpectedEventList::timerEvent(QTimerEvent *)
       
    67 {
       
    68     timer.stop();
       
    69     QAbstractEventDispatcher::instance()->interrupt();
       
    70 }
       
    71 
       
    72 bool ExpectedEventList::waitForAllEvents(int maxEventWaitTime)
       
    73 {
       
    74     if (eventList.isEmpty())
       
    75         return true;
       
    76 
       
    77     int eventCount = eventList.size();
       
    78     timer.start(maxEventWaitTime, this);
       
    79 
       
    80     while (timer.isActive()) {
       
    81         QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
       
    82         if (eventList.isEmpty())
       
    83             return true;
       
    84 
       
    85         if (eventCount < eventList.size()){
       
    86             eventCount = eventList.size();
       
    87             timer.start(maxEventWaitTime, this);
       
    88         }
       
    89     }
       
    90 
       
    91     int eventListNr = eventCount - eventList.size() + 1;
       
    92     qWarning() << "Stopped waiting for expected event nr" << eventListNr;
       
    93     return false;
       
    94 }
       
    95 
       
    96 void ExpectedEventList::compareMouseEvents(QEvent *received, QEvent *expected)
       
    97 {
       
    98     QMouseEvent *e1 = static_cast<QMouseEvent *>(received);
       
    99     QMouseEvent *e2 = static_cast<QMouseEvent *>(expected);
       
   100 
       
   101     // Do a manual check first to be able to write more sensible
       
   102     // debug output if we know we're going to fail:
       
   103     if (e1->pos() == e2->pos()
       
   104             && (e1->globalPos() == e2->globalPos())
       
   105             && (e1->button() == e2->button())
       
   106             && (e1->buttons() == e2->buttons())
       
   107             && (e1->modifiers() == e2->modifiers()))
       
   108         return; // equal
       
   109 
       
   110     // INVARIANT: The two events are not equal. So we fail. Depending
       
   111     // on whether debug mode is no or not, we let QTest fail. Otherwise
       
   112     // we let the test continue for debugging puposes.
       
   113     int eventListNr = eventCount - eventList.size();
       
   114     if (!debug) {
       
   115         qWarning() << "Expected event" << eventListNr << "differs from received event:";
       
   116         QCOMPARE(e1->pos(), e2->pos());
       
   117         QCOMPARE(e1->globalPos(), e2->globalPos());
       
   118         QCOMPARE(e1->button(), e2->button());
       
   119         QCOMPARE(e1->buttons(), e2->buttons());
       
   120         QCOMPARE(e1->modifiers(), e2->modifiers());
       
   121     } else {
       
   122         qWarning() << "*** FAIL *** : Expected event" << eventListNr << "differs from received event:";
       
   123         qWarning() << "Received:" << e1 << e1->globalPos();
       
   124         qWarning() << "Expected:" << e2 << e2->globalPos();
       
   125     }
       
   126 }
       
   127 
       
   128 void ExpectedEventList::compareKeyEvents(QEvent *received, QEvent *expected)
       
   129 {
       
   130     QKeyEvent *e1 = static_cast<QKeyEvent *>(received);
       
   131     QKeyEvent *e2 = static_cast<QKeyEvent *>(expected);
       
   132 
       
   133     // Do a manual check first to be able to write more sensible
       
   134     // debug output if we know we're going to fail:
       
   135     if (e1->key() == e2->key()
       
   136             && (e1->modifiers() == e2->modifiers())
       
   137             && (e1->count() == e2->count())
       
   138             && (e1->isAutoRepeat() == e2->isAutoRepeat()))
       
   139         return; // equal
       
   140 
       
   141     // INVARIANT: The two events are not equal. So we fail. Depending
       
   142     // on whether debug mode is no or not, we let QTest fail. Otherwise
       
   143     // we let the test continue for debugging puposes.
       
   144     int eventListNr = eventCount - eventList.size();
       
   145     if (!debug) {
       
   146         qWarning() << "Expected event" << eventListNr << "differs from received event:";
       
   147         QCOMPARE(e1->key(), e2->key());
       
   148         QCOMPARE(e1->modifiers(), e2->modifiers());
       
   149         QCOMPARE(e1->count(), e2->count());
       
   150         QCOMPARE(e1->isAutoRepeat(), e2->isAutoRepeat());
       
   151     } else {
       
   152         qWarning() << "*** FAIL *** : Expected event" << eventListNr << "differs from received event:";
       
   153         qWarning() << "Received:" << e1 << e1->key();
       
   154         qWarning() << "Expected:" << e2 << e2->key();
       
   155     }
       
   156 }
       
   157 
       
   158 bool ExpectedEventList::eventFilter(QObject *, QEvent *received)
       
   159 {
       
   160     if (debug)
       
   161         qDebug() << received;
       
   162     if (eventList.isEmpty())
       
   163         return false;
       
   164 
       
   165     QEvent *expected = eventList.first();
       
   166     if (expected->type() == received->type()) {
       
   167         eventList.removeFirst();
       
   168         switch (received->type()) {
       
   169             case QEvent::MouseButtonPress:
       
   170             case QEvent::MouseButtonRelease:
       
   171             case QEvent::MouseMove:
       
   172             case QEvent::MouseButtonDblClick:
       
   173             case QEvent::NonClientAreaMouseButtonPress:
       
   174             case QEvent::NonClientAreaMouseButtonRelease:
       
   175             case QEvent::NonClientAreaMouseButtonDblClick:
       
   176             case QEvent::NonClientAreaMouseMove: {
       
   177                 compareMouseEvents(received, expected);
       
   178                 break;
       
   179             }
       
   180             case QEvent::KeyPress:
       
   181             case QEvent::KeyRelease: {
       
   182                 compareKeyEvents(received, expected);
       
   183                 break;
       
   184             }
       
   185             case QEvent::Resize: {
       
   186                 break;
       
   187             }
       
   188             case QEvent::WindowActivate: {
       
   189                 break;
       
   190             }
       
   191             case QEvent::WindowDeactivate: {
       
   192                 break;
       
   193             }
       
   194             default:
       
   195                 break;
       
   196         }
       
   197         if (eventList.isEmpty())
       
   198             QAbstractEventDispatcher::instance()->interrupt();
       
   199     }
       
   200 
       
   201     return false;
       
   202 }
       
   203