tests/benchmarks/events/main.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 #include <QtCore>
       
    42 
       
    43 #include <qtest.h>
       
    44 #include <qtesteventloop.h>
       
    45 
       
    46 class PingPong : public QObject
       
    47 {
       
    48 public:
       
    49     void setPeer(QObject *peer);
       
    50 
       
    51 protected:
       
    52     bool event(QEvent *e);
       
    53 
       
    54 private:
       
    55     QObject *m_peer;
       
    56     int m_counter;
       
    57 };
       
    58 
       
    59 void PingPong::setPeer(QObject *peer)
       
    60 {
       
    61     m_peer = peer;
       
    62     m_counter = 100;
       
    63 }
       
    64 
       
    65 bool PingPong::event(QEvent *)
       
    66 {
       
    67     --m_counter;
       
    68     if (m_counter > 0) {
       
    69         QEvent *e = new QEvent(QEvent::User);
       
    70         QCoreApplication::postEvent(m_peer, e);
       
    71     } else {
       
    72         QCoreApplication::quit();
       
    73     }
       
    74     return true;
       
    75 }
       
    76 
       
    77 class EventTester : public QObject
       
    78 {
       
    79 public:
       
    80     int foo(int bar);
       
    81 
       
    82 protected:
       
    83     bool event(QEvent *e);
       
    84 };
       
    85 
       
    86 bool EventTester::event(QEvent *e)
       
    87 {
       
    88     if (e->type() == QEvent::User+1)
       
    89         return foo(e->type()) != 0;
       
    90     return false;
       
    91 }
       
    92 
       
    93 int EventTester::foo(int bar)
       
    94 {
       
    95     return bar + 1;
       
    96 }
       
    97 
       
    98 class EventsBench : public QObject
       
    99 {
       
   100     Q_OBJECT
       
   101 
       
   102 private slots:
       
   103     void initTestCase();
       
   104     void cleanupTestCase();
       
   105 
       
   106     void noEvent();
       
   107     void sendEvent_data();
       
   108     void sendEvent();
       
   109     void postEvent_data();
       
   110     void postEvent();
       
   111 };
       
   112 
       
   113 void EventsBench::initTestCase()
       
   114 {
       
   115 }
       
   116 
       
   117 void EventsBench::cleanupTestCase()
       
   118 {
       
   119 }
       
   120 
       
   121 void EventsBench::noEvent()
       
   122 {
       
   123     EventTester tst;
       
   124     int val = 0;
       
   125     QBENCHMARK {
       
   126         val += tst.foo(1);
       
   127     }
       
   128 }
       
   129 
       
   130 void EventsBench::sendEvent_data()
       
   131 {
       
   132     QTest::addColumn<bool>("filterEvents");
       
   133     QTest::newRow("no eventfilter") << false;
       
   134     QTest::newRow("eventfilter") << true;
       
   135 }
       
   136 
       
   137 void EventsBench::sendEvent()
       
   138 {
       
   139     QFETCH(bool, filterEvents);
       
   140     EventTester tst;
       
   141     if (filterEvents)
       
   142         tst.installEventFilter(this);
       
   143     QEvent evt(QEvent::Type(QEvent::User+1));
       
   144     QBENCHMARK {
       
   145         QCoreApplication::sendEvent(&tst, &evt);
       
   146     }
       
   147 }
       
   148 
       
   149 void EventsBench::postEvent_data()
       
   150 {
       
   151     QTest::addColumn<bool>("filterEvents");
       
   152     QTest::newRow("no eventfilter") << false;
       
   153     QTest::newRow("eventfilter") << true;
       
   154 }
       
   155 
       
   156 void EventsBench::postEvent()
       
   157 {
       
   158     QFETCH(bool, filterEvents);
       
   159     PingPong ping;
       
   160     PingPong pong;
       
   161     ping.setPeer(&pong);
       
   162     pong.setPeer(&ping);
       
   163     if (filterEvents) {
       
   164         ping.installEventFilter(this);
       
   165         pong.installEventFilter(this);
       
   166     }
       
   167     QEvent *e = new QEvent(QEvent::User);
       
   168     QBENCHMARK {
       
   169         QCoreApplication::postEvent(&ping, e);
       
   170         QTestEventLoop::instance().enterLoop( 61 );
       
   171     }
       
   172 }
       
   173 
       
   174 QTEST_MAIN(EventsBench)
       
   175 
       
   176 #include "main.moc"