src/testlib/qtestevent.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 QtTest module 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 #ifndef QTESTEVENT_H
       
    43 #define QTESTEVENT_H
       
    44 
       
    45 #if 0
       
    46 // inform syncqt
       
    47 #pragma qt_no_master_include
       
    48 #endif
       
    49 
       
    50 #include <QtTest/qtest_global.h>
       
    51 #ifdef QT_GUI_LIB
       
    52 #include <QtTest/qtestkeyboard.h>
       
    53 #include <QtTest/qtestmouse.h>
       
    54 #endif
       
    55 #include <QtTest/qtestsystem.h>
       
    56 
       
    57 #include <QtCore/qlist.h>
       
    58 
       
    59 #include <stdlib.h>
       
    60 
       
    61 QT_BEGIN_HEADER
       
    62 
       
    63 QT_BEGIN_NAMESPACE
       
    64 
       
    65 QT_MODULE(Test)
       
    66 
       
    67 class QTestEvent
       
    68 {
       
    69 public:
       
    70     virtual void simulate(QWidget *w) = 0;
       
    71     virtual QTestEvent *clone() const = 0;
       
    72 
       
    73     virtual ~QTestEvent() {}
       
    74 };
       
    75 
       
    76 #ifdef QT_GUI_LIB
       
    77 class QTestKeyEvent: public QTestEvent
       
    78 {
       
    79 public:
       
    80     inline QTestKeyEvent(QTest::KeyAction action, Qt::Key key, Qt::KeyboardModifiers modifiers, int delay)
       
    81         : _action(action), _delay(delay), _modifiers(modifiers), _ascii(0), _key(key) {}
       
    82     inline QTestKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers, int delay)
       
    83         : _action(action), _delay(delay), _modifiers(modifiers),
       
    84           _ascii(ascii), _key(Qt::Key_unknown) {}
       
    85     inline QTestEvent *clone() const { return new QTestKeyEvent(*this); }
       
    86 
       
    87     inline void simulate(QWidget *w)
       
    88     {
       
    89         if (_ascii == 0)
       
    90             QTest::keyEvent(_action, w, _key, _modifiers, _delay);
       
    91         else
       
    92             QTest::keyEvent(_action, w, _ascii, _modifiers, _delay);
       
    93     }
       
    94 
       
    95 protected:
       
    96     QTest::KeyAction _action;
       
    97     int _delay;
       
    98     Qt::KeyboardModifiers _modifiers;
       
    99     char _ascii;
       
   100     Qt::Key _key;
       
   101 };
       
   102 
       
   103 class QTestKeyClicksEvent: public QTestEvent
       
   104 {
       
   105 public:
       
   106     inline QTestKeyClicksEvent(const QString &keys, Qt::KeyboardModifiers modifiers, int delay)
       
   107         : _keys(keys), _modifiers(modifiers), _delay(delay) {}
       
   108     inline QTestEvent *clone() const { return new QTestKeyClicksEvent(*this); }
       
   109 
       
   110     inline void simulate(QWidget *w)
       
   111     {
       
   112         QTest::keyClicks(w, _keys, _modifiers, _delay);
       
   113     }
       
   114 
       
   115 private:
       
   116     QString _keys;
       
   117     Qt::KeyboardModifiers _modifiers;
       
   118     int _delay;
       
   119 };
       
   120 
       
   121 class QTestMouseEvent: public QTestEvent
       
   122 {
       
   123 public:
       
   124     inline QTestMouseEvent(QTest::MouseAction action, Qt::MouseButton button,
       
   125             Qt::KeyboardModifiers modifiers, QPoint position, int delay)
       
   126         : _action(action), _button(button), _modifiers(modifiers), _pos(position), _delay(delay) {}
       
   127     inline QTestEvent *clone() const { return new QTestMouseEvent(*this); }
       
   128 
       
   129     inline void simulate(QWidget *w)
       
   130     {
       
   131         QTest::mouseEvent(_action, w, _button, _modifiers, _pos, _delay);
       
   132     }
       
   133 
       
   134 private:
       
   135     QTest::MouseAction _action;
       
   136     Qt::MouseButton _button;
       
   137     Qt::KeyboardModifiers _modifiers;
       
   138     QPoint _pos;
       
   139     int _delay;
       
   140 };
       
   141 #endif //QT_GUI_LIB
       
   142 
       
   143 
       
   144 class QTestDelayEvent: public QTestEvent
       
   145 {
       
   146 public:
       
   147     inline QTestDelayEvent(int msecs): _delay(msecs) {}
       
   148     inline QTestEvent *clone() const { return new QTestDelayEvent(*this); }
       
   149 
       
   150     inline void simulate(QWidget * /*w*/) { QTest::qWait(_delay); }
       
   151 
       
   152 private:
       
   153     int _delay;
       
   154 };
       
   155 
       
   156 class QTestEventList: public QList<QTestEvent *>
       
   157 {
       
   158 public:
       
   159     inline QTestEventList() {}
       
   160     inline QTestEventList(const QTestEventList &other): QList<QTestEvent *>()
       
   161     { for (int i = 0; i < other.count(); ++i) append(other.at(i)->clone()); }
       
   162     inline ~QTestEventList()
       
   163     { clear(); }
       
   164     inline void clear()
       
   165     { qDeleteAll(*this); QList<QTestEvent *>::clear(); }
       
   166 
       
   167 #ifdef QT_GUI_LIB
       
   168     inline void addKeyClick(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   169     { addKeyEvent(QTest::Click, qtKey, modifiers, msecs); }
       
   170     inline void addKeyPress(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   171     { addKeyEvent(QTest::Press, qtKey, modifiers, msecs); }
       
   172     inline void addKeyRelease(Qt::Key qtKey, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   173     { addKeyEvent(QTest::Release, qtKey, modifiers, msecs); }
       
   174     inline void addKeyEvent(QTest::KeyAction action, Qt::Key qtKey,
       
   175                             Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   176     { append(new QTestKeyEvent(action, qtKey, modifiers, msecs)); }
       
   177 
       
   178     inline void addKeyClick(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   179     { addKeyEvent(QTest::Click, ascii, modifiers, msecs); }
       
   180     inline void addKeyPress(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   181     { addKeyEvent(QTest::Press, ascii, modifiers, msecs); }
       
   182     inline void addKeyRelease(char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   183     { addKeyEvent(QTest::Release, ascii, modifiers, msecs); }
       
   184     inline void addKeyClicks(const QString &keys, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   185     { append(new QTestKeyClicksEvent(keys, modifiers, msecs)); }
       
   186     inline void addKeyEvent(QTest::KeyAction action, char ascii, Qt::KeyboardModifiers modifiers = Qt::NoModifier, int msecs = -1)
       
   187     { append(new QTestKeyEvent(action, ascii, modifiers, msecs)); }
       
   188 
       
   189     inline void addMousePress(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
       
   190                               QPoint pos = QPoint(), int delay=-1)
       
   191     { append(new QTestMouseEvent(QTest::MousePress, button, stateKey, pos, delay)); }
       
   192     inline void addMouseRelease(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
       
   193                                 QPoint pos = QPoint(), int delay=-1)
       
   194     { append(new QTestMouseEvent(QTest::MouseRelease, button, stateKey, pos, delay)); }
       
   195     inline void addMouseClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
       
   196                               QPoint pos = QPoint(), int delay=-1)
       
   197     { append(new QTestMouseEvent(QTest::MouseClick, button, stateKey, pos, delay)); }
       
   198     inline void addMouseDClick(Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
       
   199                             QPoint pos = QPoint(), int delay=-1)
       
   200     { append(new QTestMouseEvent(QTest::MouseDClick, button, stateKey, pos, delay)); }
       
   201     inline void addMouseMove(QPoint pos = QPoint(), int delay=-1)
       
   202     { append(new QTestMouseEvent(QTest::MouseMove, Qt::NoButton, 0, pos, delay)); }
       
   203 #endif //QT_GUI_LIB
       
   204 
       
   205     inline void addDelay(int msecs)
       
   206     { append(new QTestDelayEvent(msecs)); }
       
   207 
       
   208     inline void simulate(QWidget *w)
       
   209     {
       
   210         for (int i = 0; i < count(); ++i)
       
   211             at(i)->simulate(w);
       
   212     }
       
   213 };
       
   214 
       
   215 QT_END_NAMESPACE
       
   216 
       
   217 Q_DECLARE_METATYPE(QTestEventList)
       
   218 
       
   219 QT_END_HEADER
       
   220 
       
   221 #endif