summaryrefslogtreecommitdiffstats
path: root/src/remote/slave/lib/qtuitestnamespace.cpp
blob: 3c0604bf103367b0185a63b1086ee03abc59dee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of QtSystemTest.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include "qtuitestnamespace.h"

#include "qeventwatcher_p.h"
#include "qtuitestconnectionmanager_p.h"
#include "qtuitestwidgets_p.h"

#include <QtSystemTest/QstDebug>
#include <QEventLoop>
#include <QKeyEvent>
#include <QKeySequence>
#include <QPointer>
#include <QTimer>

/*
    A simple auto pointer class which deletes the pointed-to QObject
    later via deleteLater()
*/
template <typename T>
class QDelayedAutoPointer
{
public:
    inline QDelayedAutoPointer(T* thing)
        : raw(thing)
    {}
    inline ~QDelayedAutoPointer()
    {
        QObject::disconnect(raw, 0, 0, 0);
        raw->deleteLater();
    }

    inline T* operator->()
    { return raw; }

    inline operator T*()
    { return raw; }

private:
    T* raw;
};

/*
    A helper class to encapsulate generation of input events
*/
struct QtUiTestInput
{
    virtual ~QtUiTestInput() {}
    virtual void post() const =0;
    virtual QString toString() const =0;
};


namespace QtUiTest {
    QString toString(QObject* obj)
    {
        if (!obj) return "QObject(0x0)";
        return QString("%1(0x%2 \"%3\")")
            .arg(obj->metaObject()->className())
            .arg(QString::number(qptrdiff(obj), 16))
            .arg(obj->objectName())
        ;
    }

    QString toString(QEvent::Type type)
    {
#define DO(A) if (type == QEvent::A) return #A
        DO(KeyPress);
        DO(KeyRelease);
        DO(MouseButtonPress);
        DO(MouseButtonRelease);
        DO(Show);
        DO(Hide);
        DO(FocusIn);
        DO(FocusOut);
        DO(WindowBlocked);
#undef  DO
        return QString::number(int(type));
    }

    QString toString(QList<QEvent::Type> const& types)
    {
        QString ret;
        QString sep;
        foreach (QEvent::Type type, types) {
            ret += sep + toString(type);
            sep = ",";
        }
        return ret;
    }

    QString toString(Qt::Key key)
    { return QKeySequence(key).toString(); }

    QString toString(const QPoint& pos)
    { return QString("(%1,%2)").arg(pos.x()).arg(pos.y()); }

    QString toString(Qt::MouseButtons const& buttons)
    {
        QStringList ret;
#define DO(A) if (buttons & Qt::A) ret << #A
        DO(LeftButton);
        DO(RightButton);
        DO(MidButton);
        DO(XButton1);
        DO(XButton2);
#undef  DO
        return ret.join(",");
    }

    bool keyClick(QObject*, QList<QEvent::Type> const&, int, Qt::KeyboardModifiers, QtUiTest::InputOption);
    bool mouseClick(QObject*, QList<QEvent::Type> const&, const QPoint&, Qt::MouseButtons, QtUiTest::InputOption);
    bool mouseDClick(QObject*, QList<QEvent::Type> const&, const QPoint&, Qt::MouseButtons, QtUiTest::InputOption);
    bool inputWithEvent(QObject*, QList<QEventWatcherFilter*> const&, QtUiTestInput const&);
    bool inputWithSignal(QObject*, const QByteArray&, QtUiTestInput const&);
};

struct QtUiTestKeyClick : public QtUiTestInput
{
    QtUiTestKeyClick(Qt::Key key, Qt::KeyboardModifiers modifiers, QtUiTest::InputOption options)
        : m_key(key), m_modifiers(modifiers), m_options(options)
    {}

    virtual void post() const
    { QtUiTest::keyClick(m_key, m_modifiers, m_options); }

    virtual QString toString() const
    { return QString("Key click \"%1\"").arg(QtUiTest::toString(m_key)); }

    Qt::Key                m_key;
    Qt::KeyboardModifiers  m_modifiers;
    QtUiTest::InputOption  m_options;
};

struct QtUiTestMouseClick : public QtUiTestInput
{
    QtUiTestMouseClick(const QPoint& pos, Qt::MouseButtons buttons, QtUiTest::InputOption options)
        : m_pos(pos), m_buttons(buttons), m_options(options)
    {}

    virtual void post() const
    { QtUiTest::mouseClick(m_pos, m_buttons, m_options); }

    virtual QString toString() const
    {
        return QString("Mouse click \"%1\" at %2")
            .arg(QtUiTest::toString(m_buttons))
            .arg(QtUiTest::toString(m_pos))
        ;
    }

    const QPoint&          m_pos;
    Qt::MouseButtons       m_buttons;
    QtUiTest::InputOption  m_options;
};

struct QtUiTestMouseDClick : public QtUiTestInput
{
    QtUiTestMouseDClick(const QPoint& pos, Qt::MouseButtons buttons, QtUiTest::InputOption options)
        : m_pos(pos), m_buttons(buttons), m_options(options)
    {}

    virtual void post() const
    { QtUiTest::mouseDClick(m_pos, m_buttons, m_options); }

    virtual QString toString() const
    {
        return QString("Mouse click \"%1\" at %2")
            .arg(QtUiTest::toString(m_buttons))
            .arg(QtUiTest::toString(m_pos))
        ;
    }

    const QPoint&          m_pos;
    Qt::MouseButtons       m_buttons;
    QtUiTest::InputOption  m_options;
};

/*
    Filter which implements watching for events of a specific type.
*/
class QEventWatcherTypeFilter : public QEventWatcherFilter
{
public:
    QEventWatcherTypeFilter(QEvent::Type type)
        : m_type(type)
    {}

protected:
    virtual bool accept(QObject*,QEvent* e) const
    { return e->type() == m_type; }

    virtual QString toString() const
    { return QString("event of type %1").arg(QtUiTest::toString(m_type)); }

private:
    QEvent::Type m_type;
};

/*
    Filter which implements watching for specific key events.
*/

class QEventWatcherKeyFilter : public QEventWatcherTypeFilter
{
public:
    QEventWatcherKeyFilter(Qt::Key key, QEvent::Type type)
        : QEventWatcherTypeFilter(type)
        , m_key(key)
    {}

    static QEvent::Type keyPressType()
    { static int ret = QEvent::registerEventType(); return QEvent::Type(ret); }

    static QEvent::Type keyReleaseType()
    { static int ret = QEvent::registerEventType(); return QEvent::Type(ret); }

protected:
    virtual bool accept(QObject* o, QEvent* e) const
    {
        if (!QEventWatcherTypeFilter::accept(o,e))
            return false;
        if (e->type() != QEvent::KeyPress && e->type() != QEvent::KeyRelease)
            return false;
        return static_cast<QKeyEvent*>(e)->key() == m_key;
    }

    virtual QString toString() const
    {
        return QString("%1 (key:%2)")
            .arg(QEventWatcherTypeFilter::toString())
            .arg(QtUiTest::toString(m_key));
    }

private:
    Qt::Key m_key;
};


/*!
    \preliminary
    \namespace QtUiTest
    \inpublicgroup QtUiTestModule

    \brief The QtUiTest namespace provides the plugin interfaces used for
    customizing the behaviour of QtUiTest.
*/

/*!
    \fn T QtUiTest::qtuitest_cast_helper(QObject* object,T dummy)
    \internal
*/

/*!
    \relates QtUiTest
    \fn T qtuitest_cast(const QObject *object)

    Casts \a object to the specified QtUiTest test widget interface \c{T}.

    If \a object already implements \c{T}, it is simply casted and returned.
    Otherwise, QtUiTest will attempt to find or create a test widget to
    wrap \a object, using all loaded QtUiTest::WidgetFactory plugins.
    If a test widget cannot be created to wrap \a object, 0 is returned.

    In either case, the returned value must not be deleted by the caller.
*/


/*!
    \enum QtUiTest::InputOption

    This enum type specifies the options to be used when simulating key
    and mouse events.

    \value NoOptions no options.
    \value KeyRepeat when simulating key press events, simulate auto-repeat
                     key press events. The default is to simulate regular key
                     press events.
*/

/*!
    \enum QtUiTest::Key

    This enum provides mappings for high-level conceptual keys to platform-specific
    values of Qt::Key.

    \value Key_Activate         Key used to activate generic UI elements.
    \value Key_ActivateButton   Key used to activate buttons.
    \value Key_Select           Key used to select an item from lists.
*/

/*!
    Set or clear the specified \a option for subsequent simulated input
    events.  The option is set if \a on is true, otherwise it is cleared.
*/
void QtUiTest::setInputOption(QtUiTest::InputOption option, bool on)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->setInputOption(option, on);
}

/*!
    Returns true if \a option is currently set.
*/
bool QtUiTest::testInputOption(QtUiTest::InputOption option)
{
    QTT_TRACE_FUNCTION();
    return QtUiTestWidgets::instance()->testInputOption(option);
}

/*!
    Simulate a mouse press event at the co-ordinates given by \a pos,
    for the specified \a buttons.  \a options are applied to the simulated
    event.

    \a pos is interpreted as local co-ordinates for the currently active
    window in this application.
*/
void QtUiTest::mousePress(const QPoint& pos, Qt::MouseButtons buttons,
            QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->mousePress(pos, buttons, options);
}

void QtUiTest::touchPress(const QVariantList &points)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->touchPress(points);
}

void QtUiTest::touchUpdate(const QVariantList &points)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->touchUpdate(points);
}

void QtUiTest::touchRelease(const QVariantList &points)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->touchRelease(points);
}

/*!
    Simulate a mouse release event at the global co-ordinates given by \a pos,
    for the specified \a buttons.  \a options are applied to the simulated
    event.

    \a pos is interpreted as local co-ordinates for the currently active
    window in this application.
*/
void QtUiTest::mouseRelease(const QPoint& pos, Qt::MouseButtons buttons,
            QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->mouseRelease(pos, buttons, options);
}

/*!
    Simulate a mouse click event at the global co-ordinates given by \a pos,
    for the specified \a buttons.  \a options are applied to the simulated
    event.

    \a pos is interpreted as local co-ordinates for the currently active
    window in this application.
*/
void QtUiTest::mouseClick(const QPoint& pos, Qt::MouseButtons buttons,
            QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->mouseClick(pos, buttons, options);
}

/*!
    Simulate a mouse double-click event at the global co-ordinates given by \a pos,
    for the specified \a buttons.  \a options are applied to the simulated
    event.

    \a pos is interpreted as local co-ordinates for the currently active
    window in this application.
*/
void QtUiTest::mouseDClick(const QPoint& pos, Qt::MouseButtons buttons,
            QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->mouseDClick(pos, buttons, options);
}

/*!
    \overload
    Simulate a mouse click event.
    Returns true if the event appears to be delivered to \a object within maximumUiTimeout()
    milliseconds.
    If it does not, the errorString() will be set accordingly.
*/
bool QtUiTest::mouseClick(QObject* object, const QPoint& pos, Qt::MouseButtons buttons,
        QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    return mouseClick(object,
        QList<QEvent::Type>() << QEvent::MouseButtonRelease << QEvent::Hide << QEvent::WindowBlocked,
        pos, buttons, options
    );
}

/*!
    \overload
    Simulate a mouse double-click event.
    Returns true if the event appears to be delivered to \a object within maximumUiTimeout()
    milliseconds.
    If it does not, the errorString() will be set accordingly.
*/
bool QtUiTest::mouseDClick(QObject* object, const QPoint& pos, Qt::MouseButtons buttons,
        QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    return mouseDClick(object,
        QList<QEvent::Type>() << QEvent::MouseButtonRelease << QEvent::Hide << QEvent::WindowBlocked,
        pos, buttons, options
    );
}

/*!
    Simulate moving the mouse pointer to the global co-ordinates given by \a pos.
    \a options are applied to the simulated event.
*/
void QtUiTest::mouseMove(const QPoint& pos, QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->mouseMove(pos, options);
}

/*!
    \internal
*/
bool QtUiTest::mouseClick(QObject* object, QList<QEvent::Type> const& types, const QPoint& pos,
        Qt::MouseButtons buttons, QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QList<QEventWatcherFilter*> filters;
    foreach (QEvent::Type type, types) {
        filters << new QEventWatcherTypeFilter(type);
    }
    return inputWithEvent(object, filters, QtUiTestMouseClick(pos, buttons, options));
}

/*!
    \internal
*/
bool QtUiTest::mouseDClick(QObject* object, QList<QEvent::Type> const& types, const QPoint& pos,
        Qt::MouseButtons buttons, QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QList<QEventWatcherFilter*> filters;
    foreach (QEvent::Type type, types) {
        filters << new QEventWatcherTypeFilter(type);
    }
    return inputWithEvent(object, filters, QtUiTestMouseDClick(pos, buttons, options));
}

/*!
    \overload
    Simulate a mouse click event.
    Returns true if the event causes \a object to emit \a signal within maximumUiTimeout()
    milliseconds.
    If it does not, the errorString() will be set accordingly.
*/
bool QtUiTest::mouseClick(QObject* object, const QByteArray& signal, const QPoint& pos,
        Qt::MouseButtons buttons, QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    return inputWithSignal(object, signal, QtUiTestMouseClick(pos, buttons, options));
}

/*!
    \overload
    Simulate a mouse double-click event.
    Returns true if the event causes \a object to emit \a signal within maximumUiTimeout()
    milliseconds.
    If it does not, the errorString() will be set accordingly.
*/
bool QtUiTest::mouseDClick(QObject* object, const QByteArray& signal, const QPoint& pos,
        Qt::MouseButtons buttons, QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    return inputWithSignal(object, signal, QtUiTestMouseDClick(pos, buttons, options));
}

/*!
    Simulate a key press event, using the given \a key and \a modifiers.
    \a key must be a valid Qt::Key or QtUiTest::Key.
    \a options are applied to the simulated event.
*/
void QtUiTest::keyPress(int key, Qt::KeyboardModifiers modifiers,
        QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->keyPress(static_cast<Qt::Key>(key), modifiers, options);
}

/*!
    Simulate a key release event, using the given \a key and \a modifiers.
    \a key must be a valid Qt::Key or QtUiTest::Key.
    \a options are applied to the simulated event.
*/
void QtUiTest::keyRelease(int key, Qt::KeyboardModifiers modifiers,
        QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->keyRelease(static_cast<Qt::Key>(key), modifiers, options);
}

/*!
    Simulate a key click event, using the given \a key and \a modifiers.
    \a key must be a valid Qt::Key or QtUiTest::Key.
    \a options are applied to the simulated event.
*/
void QtUiTest::keyClick(int key, Qt::KeyboardModifiers modifiers,
        QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QtUiTestWidgets::instance()->keyClick(static_cast<Qt::Key>(key), modifiers, options);
}

/*!
    \overload
    Simulate a key click event.
    Returns true if the event appears to be delivered to \a object within maximumUiTimeout()
    milliseconds.
    If it does not, the errorString() will be set accordingly.
*/
bool QtUiTest::keyClick(QObject* object, int key, Qt::KeyboardModifiers modifiers,
        QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    return keyClick(object,
        QList<QEvent::Type>() << QEventWatcherKeyFilter::keyReleaseType()
            << QEvent::Hide << QEvent::WindowBlocked << QEvent::Show,
        key, modifiers, options
    );
}

/*!
    \internal
*/
bool QtUiTest::inputWithEvent(QObject* object, QList<QEventWatcherFilter*> const& filters,
        QtUiTestInput const& event)
{
    QTT_TRACE_FUNCTION();
    QPointer<QObject> sender = object;
    QDelayedAutoPointer<QEventWatcher> w = new QEventWatcher;
    w->addObject(sender);
    foreach (QEventWatcherFilter* filter, filters)
        w->addFilter(filter);

    event.post();

    if (!w->count() && !QtUiTest::waitForSignal(w, SIGNAL(event(QObject*,int)))) {
        setErrorString(QString(
            "%1 was expected to result in %2 receiving an event matching one of the following, "
            "but it didn't:\n%3")
            .arg(event.toString())
            .arg(toString(sender))
            .arg(w->toString()));
        return false;
    }
    return true;
}

/*!
    \internal
*/
bool QtUiTest::inputWithSignal(QObject* object, const QByteArray& signal,
        QtUiTestInput const& event)
{
    QTT_TRACE_FUNCTION();
    if (signal.isEmpty()) return false;
    QPointer<QObject> sender = object;

    QTimer dummy;
    dummy.setInterval(1000);
    if (!QtUiTest::connectFirst(sender, signal, &dummy, SLOT(start()))) {
        setErrorString(QString("Object %1 has no signal %2").arg(toString(sender)).arg(&signal.constData()[1]));
        return false;
    }

    // Ensure connectNotify is called
    if (!QObject::connect(sender, signal, &dummy, SLOT(start())))
        Q_ASSERT(0);

    event.post();

    if (!dummy.isActive() && !QtUiTest::waitForSignal(sender, signal)) {
        setErrorString(QString(
            "%1 was expected to result in %2 emitting the signal %3, "
            "but it didn't.")
            .arg(event.toString())
            .arg(toString(sender))
            .arg(&signal.constData()[1]));
        return false;
    }
    return true;
}

/*!
    \internal
    \overload
    Simulate a key click event.
    Returns true if \a object receives any event of the given \a types within maximumUiTimeout()
    milliseconds.
    If it does not, the errorString() will be set accordingly.
*/
bool QtUiTest::keyClick(QObject* object, QList<QEvent::Type> const& types, int key,
        Qt::KeyboardModifiers modifiers, QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    QList<QEventWatcherFilter*> filters;
    foreach (QEvent::Type type, types) {
        // These cases result in waiting for specific key events rather than just "any key press".
        if (type == QEventWatcherKeyFilter::keyPressType()) {
            filters << new QEventWatcherKeyFilter(Qt::Key(key), QEvent::KeyPress);
        } else if (type == QEventWatcherKeyFilter::keyReleaseType()) {
            filters << new QEventWatcherKeyFilter(Qt::Key(key), QEvent::KeyRelease);
        } else {
            filters << new QEventWatcherTypeFilter(type);
        }
    }
    return inputWithEvent(object, filters, QtUiTestKeyClick(Qt::Key(key), modifiers, options));
}

/*!
    \overload
    Simulate a key click event.
    Returns true if \a object emits \a signal within maximumUiTimeout()
    milliseconds.
    If it does not, the errorString() will be set accordingly.
*/
bool QtUiTest::keyClick(QObject* object, const QByteArray& signal, int key, Qt::KeyboardModifiers modifiers,
        QtUiTest::InputOption options)
{
    QTT_TRACE_FUNCTION();
    return inputWithSignal(object, signal, QtUiTestKeyClick(Qt::Key(key), modifiers, options));
}

/*!
    Returns the maximum amount of time, in milliseconds, the user interface is allowed to take
    to generate some response to a user's action.

    This value is useful to determine how long test widgets should wait for certain events to occur
    after simulating key/mouse events.  The value may be device-specific.
*/
int QtUiTest::maximumUiTimeout()
{ return 2000; }

/*!
    Returns the Qt::Key corresponding to \a c.

    This function is commonly used in conjunction with keyClick() to enter
    a string of characters using the keypad.

    Example:
    \code
    using namespace QtUiTest;
    QString text = "hello world";
    // ...
    foreach (QChar c, text) {
        keyClick( asciiToKey(c.toLatin1()), asciiToModifiers(c.toLatin1()) );
    }
    \endcode
*/
// Now implemented in qasciikey.cpp

/*!
    Returns any Qt::KeyboardModifiers which would be required to input \a c.

    This function is commonly used in conjunction with keyClick() to enter
    a string of characters using the keypad.

    Example:
    \code
    using namespace QtUiTest;
    QString text = "hello world";
    // ...
    foreach (QChar c, text) {
        keyClick( asciiToKey(c.toLatin1()), asciiToModifiers(c.toLatin1()) );
    }
    \endcode
*/
Qt::KeyboardModifiers QtUiTest::asciiToModifiers(char c)
{
    QTT_TRACE_FUNCTION();
    Qt::KeyboardModifiers ret = Qt::NoModifier;
    if (QChar(c).isUpper()) ret |= Qt::ShiftModifier;
    return ret;
}

/*!
    Returns a human-readable error string describing the last error which
    occurred while accessing a testwidget.

    The error string is used to report directly to a tester any unexpected
    errors.  The string will typically be used as a test failure message.

    \sa setErrorString()
*/
QString QtUiTest::errorString()
{
    return QtUiTestWidgets::instance()->errorString();
}

/*!
    Returns a QDebug object which can be used as an output stream for debugging
    information. The debug information is sent to the test runner for output.
    This ensures a consistent handling of debugging information on different
    test platforms.

    Example:
    \code
    QtUiTest::debug() << "This is a debug message";
    \endcode
*/
QDebug QtUiTest::debug()
{
    return QtUiTestWidgets::instance()->debug();
}

/*!
    \internal
*/
QString QtUiTest::debugOutput()
{
    return QtUiTestWidgets::instance()->debugOutput();
}

/*!
    Sets the human-readable \a error string describing the last error which
    occurred while accessing a testwidget.

    \sa errorString()
*/
void QtUiTest::setErrorString(const QString& error)
{
    QtUiTestWidgets::instance()->setErrorString(error);
}

/*!
    \internal
    Returns a test widget wrapper for \a object which implements
    \a interface.
*/
QObject* QtUiTest::testWidget(QObject* object, const char* interface)
{
    return QtUiTestWidgets::instance()->testWidget(object, interface);
}

/*!
    Causes the process to wait for \a ms milliseconds. While waiting, events will be processed.
*/
void QtUiTest::wait(int ms)
{
    QTT_TRACE_FUNCTION();
    QEventLoop loop;
    QTimer::singleShot(ms, &loop, SLOT(quit()));
    loop.exec();
}

#include <QDebug>

/*!
    Causes the process to wait for \a ms milliseconds or until \a signal is
    emitted from \a object, whichever comes first.

    While waiting, events will be processed.

    Returns true if \a signal was emitted from \a object before timing out.
    When false is returned, errorString() is set accordingly.

    If \a connectionType specifies a direct connection, this function will return
    immediately when the signal occurs, possibly before some objects have
    received the signal.  If \a connectionType specifies a queued connection, this
    function will return once the event loop has run following the emit.
*/
bool QtUiTest::waitForSignal(QObject* object, const char* signal, int ms, Qt::ConnectionType connectionType)
{
    QTT_TRACE_FUNCTION();
    if (ms < 0) return false;
    if (!signal || !signal[0]) return false;

    QPointer<QObject> sender = object;

    // Dummy variable to detect signal emission.
    QTimer dummy;
    dummy.setInterval(1000);
    if (!QtUiTest::connectFirst(sender, signal, &dummy, SLOT(start())))
        return false;

    // Ensure connectNotify is called
    if (!QObject::connect(sender, signal, &dummy, SLOT(start())))
        Q_ASSERT(0);

    QEventLoop loop;
    if (!QObject::connect(sender, signal, &loop, SLOT(quit())))
        return false;
    QTimer::singleShot(ms, &loop, SLOT(quit()));

    loop.exec();

    return dummy.isActive();
}

/*!
    Causes the process to wait for \a ms milliseconds or until an event of
    any of the given \a types is received by \a object, whichever comes first.

    While waiting, events will be processed.

    Returns true if the event was received by \a object before timing out.
    When false is returned, errorString() is set accordingly.

    If \a connectionType specifies a direct connection, this function will return
    immediately before the event is processed by \a object.
    If \a connectionType specifies a queued connection, this function will return
    once the event loop has run following the processing of the event.
*/
bool QtUiTest::waitForEvent(QObject* object, QList<QEvent::Type> const& types, int ms, Qt::ConnectionType connectionType)
{
    QTT_TRACE_FUNCTION();
    QPointer<QObject> sender = object;
    QDelayedAutoPointer<QEventWatcher> w = new QEventWatcher;
    w->setObjectName("qtuitest_waitForEvent_watcher");
    w->addObject(sender);
    foreach (QEvent::Type type, types)
        w->addFilter(new QEventWatcherTypeFilter(type));

    if (!QtUiTest::waitForSignal(w, SIGNAL(event(QObject*,int)), ms, connectionType)) {
        setErrorString(QString("Object %1 was expected to receive an event of type(s) %2 within "
            "%3 milliseconds, but it didn't.")
            .arg(toString(sender))
            .arg(toString(types))
            .arg(ms)
        );
        return false;
    }
    return true;
}

/*!
    \overload
    Waits for an event of the given \a type.
*/
bool QtUiTest::waitForEvent(QObject* object, QEvent::Type type, int ms, Qt::ConnectionType connectionType)
{
    QTT_TRACE_FUNCTION();
    return waitForEvent(object, QList<QEvent::Type>() << type, ms, connectionType);
}

/*!
    Creates a connection from the \a signal in the \a sender object to
    the \a method in the \a receiver object. Returns true if the connection succeeds;
    otherwise returns false.

    This function behaves similarly to QObject::connect() with the following
    important differences.
    \list
        \o The connection is guaranteed to be activated before
           any connections made with QObject::connect().
        \o The connection type is always Qt::DirectConnection.
        \o The connection cannot be disconnected using QObject::disconnect()
           (QtUiTest::disconnectFirst() must be used instead).
        \o The connection does not affect the return value of QObject::receivers().
        \o While \a method is being executed, the return value of
           QObject::sender() is undefined.
        \o QObject::connectNotify() is not called on the sending object.
    \endlist

    This function is primarily used in conjunction with QtUiTestRecorder to
    ensure events are recorded in the correct order.

    Note that this function cannot be used in a program which uses QSignalSpy.

    \sa QObject::connect(), QtUiTest::disconnectFirst()
*/
bool QtUiTest::connectFirst(const QObject* sender,   const char* signal,
                            const QObject* receiver, const char* method)
{
    QTT_TRACE_FUNCTION();
    // On failure, we use QObject::connect to get the same error message as
    // we normally would.
    if (sender == 0 || receiver == 0 || signal == 0 || method == 0) {
        return QObject::connect(sender,signal,receiver,method);
    }
    if (qstrlen(signal) < 1 || qstrlen(method) < 1) {
        return QObject::connect(sender,signal,receiver,method);
    }

    const QMetaObject* const senderMo = sender->metaObject();

    QByteArray normalSignal = QByteArray::fromRawData(signal+1, qstrlen(signal)-1);
    int signal_index = senderMo->indexOfSignal(normalSignal);

    if (signal_index < 0) {
        // See if we can find the signal after normalizing.
        normalSignal = QMetaObject::normalizedSignature(normalSignal);
        signal_index = senderMo->indexOfSignal(normalSignal);
    }
    if (signal_index < 0) {
        // Nope, bail out.
        return QObject::connect(sender,signal,receiver,method);
    }

    const QMetaObject* const receiverMo = receiver->metaObject();

    QByteArray normalMethod = QByteArray::fromRawData(method+1, qstrlen(method)-1);
    int method_index = receiverMo->indexOfMethod(normalMethod);

    if (method_index < 0) {
        // See if we can find the method after normalizing.
        normalMethod = QMetaObject::normalizedSignature(normalMethod);
        method_index = senderMo->indexOfMethod(normalMethod);
    }
    if (method_index < 0) {
        // Nope, bail out.
        return QObject::connect(sender,signal,receiver,method);
    }

    // Ensure signal and slot are compatible.
    if (!QMetaObject::checkConnectArgs(normalSignal.constData(), normalMethod.constData())) {
        return QObject::connect(sender,signal,receiver,method);
    }

    // If we get here, then everything is valid.
    QtUiTestConnectionManager::instance()->connect(sender, signal_index, receiver, method_index);
    return true;
}

/*!
    Disconnects \a signal in object \a sender from \a method in object \a receiver. Returns true if the connection is successfully broken; otherwise returns false.

    The connection must have been established with QtUiTest::connectFirst().

    Passing null arguments has the same wildcard effects as documented in QObject::disconnect().

    If the same connection has been established multiple times, disconnectFirst() will disconnect
    all instances of the connection.  There is no way to disconnect a single instance of a
    connection.  This behavior matches QObject::disconnect().

    \sa QObject::disconnect(), QtUiTest::connectFirst()
*/
bool QtUiTest::disconnectFirst(const QObject* sender,   const char* signal,
                               const QObject* receiver, const char* method)
{
    QTT_TRACE_FUNCTION();
    // On failure, we use QObject::disconnect to get the same error message as
    // we normally would.
    if (sender == 0) {
        return QObject::disconnect(sender,signal,receiver,method);
    }

    const QMetaObject* const senderMo = sender->metaObject();

    QByteArray normalSignal = (signal)
        ? QByteArray::fromRawData(signal+1, qstrlen(signal)-1)
        : QByteArray();
    int signal_index = (signal) ? senderMo->indexOfSignal(normalSignal) : -1;
    if (signal && (signal_index < 0)) {
        // See if we can find the signal after normalizing.
        normalSignal = QMetaObject::normalizedSignature(signal);
        signal_index = senderMo->indexOfSignal(normalSignal);
    }
    if (signal && (signal_index < 0)) {
        // Nope, bail out.
        return QObject::disconnect(sender,signal,receiver,method);
    }

    if (method && !receiver) {
        return QObject::disconnect(sender,signal,receiver,method);
    }

    const QMetaObject* const receiverMo = (receiver) ? receiver->metaObject() : 0;

    QByteArray normalMethod = (method)
        ? QByteArray::fromRawData(method+1, qstrlen(method)-1)
        : QByteArray();
    int method_index = (method) ? receiverMo->indexOfMethod(normalMethod) : -1;

    if (method && (method_index < 0)) {
        // See if we can find the method after normalizing.
        normalMethod = QMetaObject::normalizedSignature(method);
        method_index = senderMo->indexOfMethod(normalMethod);
    }
    if (method && (method_index < 0)) {
        // Nope, bail out.
        return QObject::disconnect(sender,signal,receiver,method);
    }

    return QtUiTestConnectionManager::instance()->disconnect(sender,signal_index,receiver,method_index);
}