summaryrefslogtreecommitdiffstats
path: root/tests/qtuitest/tst_qtuitestnamespace/tst_qtuitestnamespace.cpp
blob: 64b5a325dbe59f31e703f2cc2d3e804ef53743ce (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
/****************************************************************************
**
** 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 QtUiTest.
**
** $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 QT_NO_DEBUG
#define QT_NO_DEBUG 1
#endif

#include <QAction>
#include <QCoreApplication>
#include <QObject>
#include <QTest>

#include <qalternatestack_p.h>
#include <qtuitestconnectionmanager_p.h>
#include <qtuitestnamespace.h>

#include <QTime>
#include <QTimer>

class tst_QtUiTestNamespace : public QObject
{
    Q_OBJECT

private slots:
    void wait();
    void connectFirst();
    void connectFirstIsFirst();
    void connectFirstDestruction();
    void connectFirstInvalid();
    void connectFirstDefaultParam();
    void disconnectFirstInvalid();
    void disconnectFirstWildcards();
};

int      g_exit_code;
int      g_argc;
char**   g_argv;
bool     g_test_done;
QObject* g_test;

void run_test(QAlternateStack*,QVariant const&)
{
    g_exit_code = QTest::qExec(g_test, g_argc, g_argv);
    g_test_done = true;
}

int main(int argc, char** argv)
{
    QCoreApplication app(argc, argv);
    tst_QtUiTestNamespace test;
    g_test = &test;
    g_argc = argc;
    g_argv = argv;

    // Most QtUiTest code runs in an alternate stack, while the original
    // stack runs the event loop.
    // Try to replicate that behavior for this test.
    QAlternateStack stack;

    g_test_done = false;
    stack.start(run_test);
    while (!g_test_done) {
        app.processEvents(QEventLoop::WaitForMoreEvents);
    }
    return g_exit_code;
}

/*
    Event loop where 'exec' is a slot, for convenience.
*/
class TestEventLoop : public QEventLoop
{
    Q_OBJECT
public:
    TestEventLoop(QObject* parent =0)
        :   QEventLoop(parent),
            m_execCount(0),
            m_exitCount(0)
    {}

    int m_execCount;
    int m_exitCount;

public slots:
    int exec(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents)
    {
        ++m_execCount;
        int ret = QEventLoop::exec(flags);
        ++m_exitCount;
        return ret;
    }
};

/*
    Class for emitting a particular integer value.
*/
class Emitter : public QObject
{
    Q_OBJECT

public:
    Emitter(QObject* parent =0)
        : QObject(parent)
    {}

    void emitValue(int value)
    {
        emit valueChanged(value);
        emit valueChanged2(value);
        emit valueChangedWithDefault(value);
    }

    enum Values { DefaultValue = 1338 };

signals:
    void valueChanged(int);
    void valueChanged2(int);
    void valueChangedWithDefault(int = DefaultValue);
};

/*
    Class for receiving an emitted value.
*/
class Receiver : public QObject
{
    Q_OBJECT

public:
    Receiver(QObject* parent =0)
        : QObject(parent)
    {}

    QList<int> values;
    QList<int> values2;

    enum Values { DefaultValue = 1337 };

public slots:
    void receive(int value)
    { values << value; }

    void receive2(int value)
    { values2 << value; }

    void receiveMultipliedByTwo(int value)
    { values << 2*value; }

    void receiveDefault(int value = DefaultValue)
    { values << value; }

    void clear()
    { values.clear(); }

    void clear2()
    { values2.clear(); }
};

// Verify that \a actual is bounded by \a min and \a max.
#define QCOMPARE_BOUNDED(actual, min, max)                           \
    do {                                                             \
        QVERIFY2(actual >= min && actual <= max, qPrintable(QString( \
            "actual %1, expected to be in range %2 .. %3"            \
        ).arg(actual).arg(min).arg(max)));                           \
    } while(0)

/*
    The most important test ever written.

    Verify that we can wait() for an arbitrary amount of time, while processing
    events, without hanging if a nested event loop occurs.

    See bug 194361.
*/
void tst_QtUiTestNamespace::wait()
{
    {
        QTime t;
        t.start();

        QtUiTest::wait(1000);

        QCOMPARE_BOUNDED(t.elapsed(), 1000, 30000);
    }

    // OK, so we can wait when there are no nested event loops.
    // Big deal.  The real test is:  if we have a nested event loop, can we
    // avoid hanging.
    {
        TestEventLoop loop;

        QTime t;
        t.start();

        // The nested event loop will run for 2.5 seconds.
        // But we only want to wait for 1 second.
        // What on earth will happen... ???
        QTimer::singleShot(0,    &loop, SLOT(exec()));
        QTimer::singleShot(2500, &loop, SLOT(quit()));
        QtUiTest::wait(1000);

        // Verify the loop really did exec.
        QCOMPARE(loop.m_execCount, 1);
        // Verify the loop really hasn't exited yet.
        QCOMPARE(loop.m_exitCount, 0);
        // Verify that we've waited for about as long as we wanted to wait.
        QCOMPARE_BOUNDED(t.elapsed(), 1000, 30000);

        // OK, now check inner loop really does exit, to ensure we haven't
        // screwed things up by switching stacks.
        for (int i = 0; i < 5000 && !loop.m_exitCount; i+=100, QtUiTest::wait(100))
        {}
        QCOMPARE(loop.m_exitCount, 1);
        QCOMPARE_BOUNDED(t.elapsed(), 2500, 30000);
    }
}

/*
    Tests QtUiTest::connectFirst(), which works like QObject::connect but guarantees
    that the connection comes before all existing connections.
*/
void tst_QtUiTestNamespace::connectFirst()
{
    QtUiTestConnectionManager* cm = QtUiTestConnectionManager::instance();
    if (cm->m_connections.count())
        QSKIP("Test cannot proceed; previous test left connection manager in incorrect state", SkipAll);

    {
        // Basic connection
        Emitter  e;
        Receiver r;

        QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(receive(int))));
        QCOMPARE(cm->m_connections.count(), 1);

        e.emitValue(1024);
        e.emitValue(1025);
        QCOMPARE(r.values, QList<int>() << 1024 << 1025);
        r.values.clear();

        // Basic disconnection
        QVERIFY(QtUiTest::disconnectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(receive(int))));
        QCOMPARE(cm->m_connections.count(), 0);

        e.emitValue(1027);
        QCOMPARE(r.values, QList<int>());
    }

    {
        // Multiply-connected
        Emitter e1, e2;
        Receiver r1, r2;

        QVERIFY(QtUiTest::connectFirst(&e1, SIGNAL(valueChanged(int)), &r1, SLOT(receive(int))));
        QCOMPARE(cm->m_connections.count(), 1);

        QVERIFY(QtUiTest::connectFirst(&e1, SIGNAL(valueChanged(int)), &r1, SLOT(receive(int))));
        QCOMPARE(cm->m_connections.count(), 2);

        QVERIFY(QtUiTest::connectFirst(&e1, SIGNAL(valueChanged(int)), &r1, SLOT(receive(int))));
        QCOMPARE(cm->m_connections.count(), 3);

        // Connect to another object to make sure it doesn't just happen to work when all
        // connections are identical.
        QVERIFY(QtUiTest::connectFirst(&e2, SIGNAL(valueChanged2(int)), &r2, SLOT(receive2(int))));
        QCOMPARE(cm->m_connections.count(), 4);

        e1.emitValue(128);
        e1.emitValue(129);
        QCOMPARE(r1.values,  QList<int>() << 128 << 128 << 128 << 129 << 129 << 129);
        QCOMPARE(r1.values2, QList<int>());
        QCOMPARE(r2.values,  QList<int>());
        QCOMPARE(r2.values2, QList<int>());
        r1.values.clear();

        e2.emitValue(130);
        e2.emitValue(150);
        QCOMPARE(r1.values,  QList<int>());
        QCOMPARE(r1.values2, QList<int>());
        QCOMPARE(r2.values,  QList<int>());
        QCOMPARE(r2.values2, QList<int>() << 130 << 150);
        r2.values2.clear();

        // Disconnecting something which is multiply-connected
        QVERIFY(QtUiTest::disconnectFirst(&e1, SIGNAL(valueChanged(int)), &r1, SLOT(receive(int))));
        QCOMPARE(cm->m_connections.count(), 1);

        e1.emitValue(1280);
        e1.emitValue(1290);
        QCOMPARE(r1.values,  QList<int>());
        QCOMPARE(r1.values2, QList<int>());
        QCOMPARE(r2.values,  QList<int>());
        QCOMPARE(r2.values2, QList<int>());
        r1.values.clear();

        e2.emitValue(1300);
        e2.emitValue(1500);
        QCOMPARE(r1.values,  QList<int>());
        QCOMPARE(r1.values2, QList<int>());
        QCOMPARE(r2.values,  QList<int>());
        QCOMPARE(r2.values2, QList<int>() << 1300 << 1500);
        r2.values2.clear();

        QVERIFY(QtUiTest::disconnectFirst(&e2, SIGNAL(valueChanged2(int)), &r2, SLOT(receive2(int))));
        QCOMPARE(cm->m_connections.count(), 0);

        e2.emitValue(280);
        e2.emitValue(290);
        QCOMPARE(r1.values,  QList<int>());
        QCOMPARE(r1.values2, QList<int>());
        QCOMPARE(r2.values,  QList<int>());
        QCOMPARE(r2.values2, QList<int>());

    }

    {
        // Large number of connections
        QList<Emitter*>  emitters;
        QList<Receiver*> receivers;

        static const int LARGE = 1000;
        while (emitters.count() < LARGE) {
            emitters << new Emitter;
            receivers << new Receiver;
        }

        for (int i = 0; i < LARGE; ++i) {
            QVERIFY(QtUiTest::connectFirst(emitters.at(i), SIGNAL(valueChanged(int)), receivers.at(i), SLOT(receive(int))));
            QVERIFY(QtUiTest::connectFirst(emitters.at(i), SIGNAL(valueChanged(int)), receivers.at(i), SLOT(receive(int))));
            QVERIFY(QtUiTest::connectFirst(emitters.at(i), SIGNAL(valueChanged2(int)), receivers.at(LARGE-i-1), SLOT(receive2(int))));
            QVERIFY(QtUiTest::connectFirst(emitters.at(i), SIGNAL(valueChanged2(int)), receivers.at(LARGE-i-1), SLOT(receive2(int))));
            QCOMPARE(cm->m_connections.count(), (i+1)*4);
        }

        for (int i = 0; i < LARGE; ++i) {
            emitters.at(i)->emitValue(i);
        }

        for (int i = 0; i < LARGE; ++i) {
            // receiver at i should have received (twice):
            //  value: i
            //  value2: LARGE-i-1
            QCOMPARE( receivers.at(i)->values,  QList<int>() << i << i );
            QCOMPARE( receivers.at(i)->values2, QList<int>() << (LARGE-i-1) << (LARGE-i-1) );
            receivers.at(i)->values.clear();
            receivers.at(i)->values2.clear();
        }

        // Disconnect half of the objects.
        for (int i = 0; i < LARGE; i += 2) {
            QVERIFY(QtUiTest::disconnectFirst(emitters.at(i), SIGNAL(valueChanged(int)), 0, 0));
            QVERIFY(QtUiTest::disconnectFirst(emitters.at(i), SIGNAL(valueChanged2(int)), 0, 0));
        }
        QCOMPARE(cm->m_connections.count(), LARGE*2);

        for (int i = 0; i < LARGE; ++i) {
            emitters.at(i)->emitValue(i);
        }

        for (int i = 0; i < LARGE; ++i) {
            if (i % 2) {
                QCOMPARE( receivers.at(i)->values,  QList<int>() << i << i);
                receivers.at(i)->values.clear();
            } else {
                QCOMPARE( receivers.at(i)->values,  QList<int>() );
            }

            if (!(i % 2)) {
                QCOMPARE( receivers.at(i)->values2, QList<int>() << (LARGE-i-1) << (LARGE-i-1));
                receivers.at(i)->values2.clear();
            } else {
                QCOMPARE( receivers.at(i)->values2, QList<int>() );
            }
        }

        // Delete half of the objects.
        for (int i = 0; i < LARGE/2; ++i) {
            delete receivers.takeLast();
            delete emitters.takeLast();
        }
        // All the `2' connections have been severed
        QCOMPARE(cm->m_connections.count(), LARGE/2);

        for (int i = 0; i < LARGE/2; ++i) {
            emitters.at(i)->emitValue(i);
        }

        for (int i = 0; i < LARGE/2; ++i) {
            if (i % 2) {
                QCOMPARE( receivers.at(i)->values,  QList<int>() << i << i);
                receivers.at(i)->values.clear();
            } else {
                QCOMPARE( receivers.at(i)->values,  QList<int>() );
            }
            QCOMPARE( receivers.at(i)->values2, QList<int>() );
        }

        // Delete the rest.
        while (receivers.count()) {
            delete receivers.takeFirst();
            delete emitters.takeFirst();
        }
        QCOMPARE(cm->m_connections.count(), 0);
    }
}


void tst_QtUiTestNamespace::connectFirstIsFirst()
{
    QtUiTestConnectionManager* cm = QtUiTestConnectionManager::instance();
    if (cm->m_connections.count())
        QSKIP("Test cannot proceed; previous test left connection manager in incorrect state", SkipAll);

    // Tests that connectFirst really does what its name says: establishes a connection which is
    // activated _before_ all QObject::connect connections.
    Emitter  e;
    Receiver r;

    static const int LARGE = 200;

    QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(receive(int))));

    for (int i = 0; i < LARGE; ++i)
        QVERIFY(QObject::connect  (&e, SIGNAL(valueChanged(int)), &r, SLOT(receiveMultipliedByTwo(int))));

    QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(receive(int))));

    for (int i = 0; i < LARGE; ++i)
        QVERIFY(QObject::connect (&e, SIGNAL(valueChanged(int)), &r, SLOT(receiveMultipliedByTwo(int))));

    QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(receive(int))));

    e.emitValue(1);
    QCOMPARE(r.values.count(), LARGE*2+3);

    // Verify that the QtUiTest-connected signals came first
    for (int i = 0; i < 3; ++i)
        QCOMPARE(r.values.at(i), 1);
    for (int i = 0; i < LARGE*2; ++i)
        QCOMPARE(r.values.at(i+3), 2);
}

void tst_QtUiTestNamespace::connectFirstDestruction()
{
    QtUiTestConnectionManager* cm = QtUiTestConnectionManager::instance();
    if (cm->m_connections.count())
        QSKIP("Test cannot proceed; previous test left connection manager in incorrect state", SkipAll);

    {
        // Verify connections are destroyed when sender is destroyed
        Receiver r;
        {
            Emitter e;

            QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(receive(int))));
            QCOMPARE(cm->m_connections.count(), 1);
        }
        QCOMPARE(cm->m_connections.count(), 0);
    }

    {
        // Verify connections are destroyed when receiver is destroyed
        Emitter e;
        {
            Receiver r;

            QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(receive(int))));
            QCOMPARE(cm->m_connections.count(), 1);
        }
        QCOMPARE(cm->m_connections.count(), 0);
    }
}

void tst_QtUiTestNamespace::connectFirstInvalid()
{
    QtUiTestConnectionManager* cm = QtUiTestConnectionManager::instance();
    if (cm->m_connections.count())
        QSKIP("Test cannot proceed; previous test left connection manager in incorrect state", SkipAll);

    Emitter e;
    Receiver r;

    QVERIFY(!QtUiTest::connectFirst(0, 0, 0, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::connectFirst(&e, 0, 0, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::connectFirst(&e, 0, &r, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::connectFirst(0, 0, &r, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::connectFirst(&e, SIGNAL(notexist()), &r, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::connectFirst(&e, SIGNAL(valueChanged(int)), &r, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::connectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(notexist())));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::connectFirst(&e, SIGNAL(notexist()), &r, SLOT(receive(int))));
    QVERIFY(!cm->m_connections.count());
}

void tst_QtUiTestNamespace::disconnectFirstInvalid()
{
    QtUiTestConnectionManager* cm = QtUiTestConnectionManager::instance();
    if (cm->m_connections.count())
        QSKIP("Test cannot proceed; previous test left connection manager in incorrect state", SkipAll);

    Emitter e;
    Receiver r;

    QVERIFY(!QtUiTest::disconnectFirst(0, 0, 0, 0));
    QVERIFY(!cm->m_connections.count());

    // Not an error but there are no connections
    QVERIFY(!QtUiTest::disconnectFirst(&e, 0, 0, 0));
    QVERIFY(!cm->m_connections.count());

    // Not an error but there are no connections
    QVERIFY(!QtUiTest::disconnectFirst(&e, 0, &r, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::disconnectFirst(0, 0, &r, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::disconnectFirst(&e, SIGNAL(notexist()), &r, 0));
    QVERIFY(!cm->m_connections.count());

    // Not an error but there are no connections
    QVERIFY(!QtUiTest::disconnectFirst(&e, SIGNAL(valueChanged(int)), &r, 0));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::disconnectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(notexist())));
    QVERIFY(!cm->m_connections.count());

    QVERIFY(!QtUiTest::disconnectFirst(&e, SIGNAL(notexist2()), &r, SLOT(receive(int))));
    QVERIFY(!cm->m_connections.count());

    // Not an error but there are no connections
    QVERIFY(!QtUiTest::disconnectFirst(&e, SIGNAL(valueChanged(int)), &r, SLOT(receive(int))));
    QVERIFY(!cm->m_connections.count());
}

/*
    Tests QtUiTest::connectFirst() for signals/slots which have default parameters.
*/
void tst_QtUiTestNamespace::connectFirstDefaultParam()
{
    QtUiTestConnectionManager* cm = QtUiTestConnectionManager::instance();
    if (cm->m_connections.count())
        QSKIP("Test cannot proceed; previous test left connection manager in incorrect state", SkipAll);

    {   // x,x
        Emitter e;
        Receiver r;

        QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChangedWithDefault(int)), &r, SLOT(receiveDefault(int))));
        QCOMPARE(cm->m_connections.count(), 1);
        e.emitValue(-99);
        QCOMPARE(r.values, QList<int>() << -99);
    }

    {   // x,0
        Emitter e;
        Receiver r;

        QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChangedWithDefault(int)), &r, SLOT(receiveDefault())));
        QCOMPARE(cm->m_connections.count(), 1);
        e.emitValue(129);
        QCOMPARE(r.values, QList<int>() << r.DefaultValue);
    }

    {   // 0,x: can't work
        Emitter e;
        Receiver r;

        QVERIFY(!QtUiTest::connectFirst(&e, SIGNAL(valueChangedWithDefault()), &r, SLOT(receiveDefault(int))));
        QCOMPARE(cm->m_connections.count(), 0);
    }

    {   // 0,0
        Emitter e;
        Receiver r;

        QVERIFY(QtUiTest::connectFirst(&e, SIGNAL(valueChangedWithDefault()), &r, SLOT(receiveDefault())));
        QCOMPARE(cm->m_connections.count(), 1);
        e.emitValue(130);

        QEXPECT_FAIL("", "Bug 227908: signals with default parameters are discarded by QtUiTest::connectFirst()", Abort);

        QCOMPARE(r.values.count(), 1);
        QCOMPARE(r.values, QList<int>() << r.DefaultValue);
    }
}

void tst_QtUiTestNamespace::disconnectFirstWildcards()
{
    QtUiTestConnectionManager* cm = QtUiTestConnectionManager::instance();
    if (cm->m_connections.count())
        QSKIP("Test cannot proceed; previous test left connection manager in incorrect state", SkipAll);

#define INIT() \
    Receiver r1;  Emitter e1;   \
    Receiver r2;  Emitter e2a;  Emitter e2b;    \
    Receiver r3a; Receiver r3b; Emitter e3;     \
    Receiver r4a; Receiver r4b; Emitter e4;     \
\
    QVERIFY(QtUiTest::connectFirst(&e1,  SIGNAL(valueChanged(int)),  &r1,  SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e1,  SIGNAL(valueChanged(int)),  &r1,  SLOT(receive2(int))));   \
\
    QVERIFY(QtUiTest::connectFirst(&e2a, SIGNAL(valueChanged(int)),  &r2,  SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e2a, SIGNAL(valueChanged(int)),  &r2,  SLOT(receive2(int))));   \
    QVERIFY(QtUiTest::connectFirst(&e2b, SIGNAL(valueChanged(int)),  &r2,  SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e2b, SIGNAL(valueChanged(int)),  &r2,  SLOT(receive2(int))));   \
\
    QVERIFY(QtUiTest::connectFirst(&e3,  SIGNAL(valueChanged(int)),  &r3a, SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e3,  SIGNAL(valueChanged(int)),  &r3a, SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e3,  SIGNAL(valueChanged(int)),  &r3b, SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e3,  SIGNAL(valueChanged(int)),  &r3b, SLOT(receive2(int))));   \
\
    QVERIFY(QtUiTest::connectFirst(&e4,  SIGNAL(valueChanged(int)),  &r4a, SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e4,  SIGNAL(valueChanged(int)),  &r4a, SLOT(receive2(int))));   \
    QVERIFY(QtUiTest::connectFirst(&e4,  SIGNAL(valueChanged2(int)), &r4a, SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e4,  SIGNAL(valueChanged2(int)), &r4a, SLOT(receive2(int))));   \
    QVERIFY(QtUiTest::connectFirst(&e4,  SIGNAL(valueChanged(int)),  &r4b, SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e4,  SIGNAL(valueChanged(int)),  &r4b, SLOT(receive2(int))));   \
    QVERIFY(QtUiTest::connectFirst(&e4,  SIGNAL(valueChanged2(int)), &r4b, SLOT(receive(int))));    \
    QVERIFY(QtUiTest::connectFirst(&e4,  SIGNAL(valueChanged2(int)), &r4b, SLOT(receive2(int))));

    {
        INIT();

        // Verify connections for correctness
        QCOMPARE(cm->m_connections.count(), 18);

        e1. emitValue(10);
        e2a.emitValue(20);
        e2b.emitValue(21);
        e3. emitValue(30);
        e4. emitValue(40);
        QCOMPARE(r1.values,   QList<int>() << 10);
        QCOMPARE(r1.values2,  QList<int>() << 10);
        QCOMPARE(r2.values,   QList<int>() << 20 << 21);
        QCOMPARE(r2.values2,  QList<int>() << 20 << 21);
        QCOMPARE(r3a.values,  QList<int>() << 30 << 30);
        QCOMPARE(r3a.values2, QList<int>());
        QCOMPARE(r3b.values,  QList<int>() << 30);
        QCOMPARE(r3b.values2, QList<int>() << 30);
        QCOMPARE(r4a.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4a.values2, QList<int>() << 40 << 40);
        QCOMPARE(r4b.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4b.values2, QList<int>() << 40 << 40);
    }

    {
        INIT();

        // All of these disconnects should fail:
        //  0,0,0,0
        //  0,0,0,x
        //  0,0,x,0
        //  0,0,x,x
        //  0,x,0,0
        //  0,x,0,x
        //  0,x,x,0
        //  0,x,x,x
        QVERIFY(!QtUiTest::disconnectFirst(0, 0, 0, 0));
        QVERIFY(!QtUiTest::disconnectFirst(0, 0, 0, SLOT(receive(int))));
        QVERIFY(!QtUiTest::disconnectFirst(0, 0, &r1, 0));
        QVERIFY(!QtUiTest::disconnectFirst(0, 0, &r1, SLOT(receive(int))));
        QVERIFY(!QtUiTest::disconnectFirst(0, SIGNAL(valueChanged(int)), 0, 0));
        QVERIFY(!QtUiTest::disconnectFirst(0, SIGNAL(valueChanged2(int)), 0, SLOT(receive(int))));
        QVERIFY(!QtUiTest::disconnectFirst(0, SIGNAL(valueChanged(int)), &r1, 0));
        QVERIFY(!QtUiTest::disconnectFirst(0, SIGNAL(valueChanged2(int)), &r2, SLOT(receive2(int))));
        QCOMPARE(cm->m_connections.count(), 18);
    }

    {
        INIT();

        // Test x,0,0,0
        QVERIFY(QtUiTest::disconnectFirst(&e4,0,0,0));
        QCOMPARE(cm->m_connections.count(), 10);

        e1. emitValue(10);
        e2a.emitValue(20);
        e2b.emitValue(21);
        e3. emitValue(30);
        e4. emitValue(40);
        QCOMPARE(r1.values,   QList<int>() << 10);
        QCOMPARE(r1.values2,  QList<int>() << 10);
        QCOMPARE(r2.values,   QList<int>() << 20 << 21);
        QCOMPARE(r2.values2,  QList<int>() << 20 << 21);
        QCOMPARE(r3a.values,  QList<int>() << 30 << 30);
        QCOMPARE(r3a.values2, QList<int>());
        QCOMPARE(r3b.values,  QList<int>() << 30);
        QCOMPARE(r3b.values2, QList<int>() << 30);
        QCOMPARE(r4a.values,  QList<int>());
        QCOMPARE(r4a.values2, QList<int>());
        QCOMPARE(r4b.values,  QList<int>());
        QCOMPARE(r4b.values2, QList<int>());
    }

    {
        INIT();

        // Test x,0,0,x: should fail
        QVERIFY(!QtUiTest::disconnectFirst(&e4, 0, 0, SLOT(receive2(int))));
        QCOMPARE(cm->m_connections.count(), 18);
    }

    {
        INIT();

        // Test x,0,x,0
        QVERIFY(QtUiTest::disconnectFirst(&e4, 0, &r4a, 0));
        QCOMPARE(cm->m_connections.count(), 14);

        e1. emitValue(10);
        e2a.emitValue(20);
        e2b.emitValue(21);
        e3. emitValue(30);
        e4. emitValue(40);
        QCOMPARE(r1.values,   QList<int>() << 10);
        QCOMPARE(r1.values2,  QList<int>() << 10);
        QCOMPARE(r2.values,   QList<int>() << 20 << 21);
        QCOMPARE(r2.values2,  QList<int>() << 20 << 21);
        QCOMPARE(r3a.values,  QList<int>() << 30 << 30);
        QCOMPARE(r3a.values2, QList<int>());
        QCOMPARE(r3b.values,  QList<int>() << 30);
        QCOMPARE(r3b.values2, QList<int>() << 30);
        QCOMPARE(r4a.values,  QList<int>());
        QCOMPARE(r4a.values2, QList<int>());
        QCOMPARE(r4b.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4b.values2, QList<int>() << 40 << 40);
    }

    {
        INIT();

        // Test x,0,x,x
        QVERIFY(QtUiTest::disconnectFirst(&e4, 0, &r4a, SLOT(receive2(int))));

        QCOMPARE(cm->m_connections.count(), 16);

        e1. emitValue(10);
        e2a.emitValue(20);
        e2b.emitValue(21);
        e3. emitValue(30);
        e4. emitValue(40);
        QCOMPARE(r1.values,   QList<int>() << 10);
        QCOMPARE(r1.values2,  QList<int>() << 10);
        QCOMPARE(r2.values,   QList<int>() << 20 << 21);
        QCOMPARE(r2.values2,  QList<int>() << 20 << 21);
        QCOMPARE(r3a.values,  QList<int>() << 30 << 30);
        QCOMPARE(r3a.values2, QList<int>());
        QCOMPARE(r3b.values,  QList<int>() << 30);
        QCOMPARE(r3b.values2, QList<int>() << 30);
        QCOMPARE(r4a.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4a.values2, QList<int>());
        QCOMPARE(r4b.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4b.values2, QList<int>() << 40 << 40);
    }

    {
        INIT();

        // Test x,x,0,0
        QVERIFY(QtUiTest::disconnectFirst(&e4, SIGNAL(valueChanged(int)), 0, 0));
        QCOMPARE(cm->m_connections.count(), 14);

        e1. emitValue(10);
        e2a.emitValue(20);
        e2b.emitValue(21);
        e3. emitValue(30);
        e4. emitValue(40);
        QCOMPARE(r1.values,   QList<int>() << 10);
        QCOMPARE(r1.values2,  QList<int>() << 10);
        QCOMPARE(r2.values,   QList<int>() << 20 << 21);
        QCOMPARE(r2.values2,  QList<int>() << 20 << 21);
        QCOMPARE(r3a.values,  QList<int>() << 30 << 30);
        QCOMPARE(r3a.values2, QList<int>());
        QCOMPARE(r3b.values,  QList<int>() << 30);
        QCOMPARE(r3b.values2, QList<int>() << 30);
        QCOMPARE(r4a.values,  QList<int>() << 40);
        QCOMPARE(r4a.values2, QList<int>() << 40);
        QCOMPARE(r4b.values,  QList<int>() << 40);
        QCOMPARE(r4b.values2, QList<int>() << 40);
    }

    {
        INIT();

        // Test x,x,0,x: should fail
        QVERIFY(!QtUiTest::disconnectFirst(&e2a, SIGNAL(valueChanged(int)), 0, SLOT(receive(int))));

        QCOMPARE(cm->m_connections.count(), 18);
    }

    {
        INIT();

        // Test x,x,x,0
        QVERIFY(QtUiTest::disconnectFirst(&e1, SIGNAL(valueChanged(int)), &r1, 0));

        QCOMPARE(cm->m_connections.count(), 16);

        e1. emitValue(10);
        e2a.emitValue(20);
        e2b.emitValue(21);
        e3. emitValue(30);
        e4. emitValue(40);
        QCOMPARE(r1.values,   QList<int>());
        QCOMPARE(r1.values2,  QList<int>());
        QCOMPARE(r2.values,   QList<int>() << 20 << 21);
        QCOMPARE(r2.values2,  QList<int>() << 20 << 21);
        QCOMPARE(r3a.values,  QList<int>() << 30 << 30);
        QCOMPARE(r3a.values2, QList<int>());
        QCOMPARE(r3b.values,  QList<int>() << 30);
        QCOMPARE(r3b.values2, QList<int>() << 30);
        QCOMPARE(r4a.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4a.values2, QList<int>() << 40 << 40);
        QCOMPARE(r4b.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4b.values2, QList<int>() << 40 << 40);
    }

    {
        INIT();

        // Test x,x,x,x
        QVERIFY(QtUiTest::disconnectFirst(&e3, SIGNAL(valueChanged(int)), &r3a, SLOT(receive(int))));

        QCOMPARE(cm->m_connections.count(), 16);

        e1. emitValue(10);
        e2a.emitValue(20);
        e2b.emitValue(21);
        e3. emitValue(30);
        e4. emitValue(40);
        QCOMPARE(r1.values,   QList<int>() << 10);
        QCOMPARE(r1.values2,  QList<int>() << 10);
        QCOMPARE(r2.values,   QList<int>() << 20 << 21);
        QCOMPARE(r2.values2,  QList<int>() << 20 << 21);
        QCOMPARE(r3a.values,  QList<int>());
        QCOMPARE(r3a.values2, QList<int>());
        QCOMPARE(r3b.values,  QList<int>() << 30);
        QCOMPARE(r3b.values2, QList<int>() << 30);
        QCOMPARE(r4a.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4a.values2, QList<int>() << 40 << 40);
        QCOMPARE(r4b.values,  QList<int>() << 40 << 40);
        QCOMPARE(r4b.values2, QList<int>() << 40 << 40);
    }

}

#include "tst_qtuitestnamespace.moc"