We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / tests / rpc / main.cpp
1 /** ***** QxtRPCPeer loopback test ******/
2 #include <QxtRPCPeer>
3 #include <qxtfifo.h>
4 #include <QCoreApplication>
5 #include <QTest>
6 #include <QSignalSpy>
7 #include <QBuffer>
8 #include <QDebug>
9 #include <QByteArray>
10 #include <QTcpSocket>
11
12 class RPCTest: public QObject
13 {
14     Q_OBJECT
15 private:
16     QxtRPCPeer* peer;
17 signals:
18     void wave(QString);
19     void counterwave(QString);
20     void networkedwave(quint64,QString);
21
22
23 private slots:
24     void initTestCase()
25     {}
26
27
28
29     void loopback()
30     {
31         QxtFifo io;
32         QxtRPCPeer peer(&io);
33         QVERIFY2(peer.attachSignal (this, SIGNAL(  wave  ( QString ) ) ),"cannot attach signal");
34         QVERIFY2(peer.attachSlot (  SIGNAL(   wave (  QString  )   ),this, SIGNAL( counterwave(QString  )) ),"cannot attach slot");
35
36         QSignalSpy spy(this, SIGNAL(counterwave(QString)));
37         QSignalSpy spyr(&io, SIGNAL(readyRead()));
38
39         emit(wave("world"));
40
41         QCoreApplication::processEvents ();
42         QCoreApplication::processEvents ();
43
44         QVERIFY2 (spyr.count()> 0, "buffer not emitting readyRead" );
45
46         QVERIFY2 (spy.count()> 0, "no signal received" );
47         QVERIFY2 (spy.count()< 2, "wtf, two signals received?" );
48
49         QList<QVariant> arguments = spy.takeFirst();
50         QVERIFY2(arguments.at(0).toString()=="world","argument missmatch");
51     }
52     void directcall()
53     {
54         QxtFifo io;
55         QxtRPCPeer peer(&io);
56         QVERIFY2(peer.attachSlot (  SIGNAL(   wave (  QString  )   ),this, SIGNAL( counterwave(QString  )) ),"cannot attach slot");
57
58         QSignalSpy spy(this, SIGNAL(counterwave(QString)));
59         QSignalSpy spyr(&io, SIGNAL(readyRead()));
60         peer.call(SIGNAL(wave   ( QString   )  ),QString("world"));
61
62         QCoreApplication::processEvents ();
63         QCoreApplication::processEvents ();
64
65
66         QVERIFY2 (spyr.count()> 0, "buffer not emitting readyRead" );
67
68         QVERIFY2 (spy.count()> 0, "no signal received" );
69         QVERIFY2 (spy.count()< 2, "wtf, two signals received?" );
70
71         QList<QVariant> arguments = spy.takeFirst();
72         QVERIFY2(arguments.at(0).toString()=="world","argument missmatch");
73     }
74
75     void TcpServerIo()
76     {
77         QxtRPCPeer server(QxtRPCPeer::Server);
78         QVERIFY2(server.attachSlot (SIGNAL(wave(QString)),this,SIGNAL(networkedwave(quint64,QString))),"cannot attach slot");
79
80
81         QVERIFY(server.listen (QHostAddress::LocalHost, 23444));
82
83
84         QxtRPCPeer client(QxtRPCPeer::Client);
85         client.connect (QHostAddress::LocalHost, 23444);
86         QVERIFY(qobject_cast<QTcpSocket*>(client.socket())->waitForConnected ( 30000 ));
87
88
89         QSignalSpy spy(this, SIGNAL(networkedwave(quint64,QString)));
90         client.call(SIGNAL(wave(QString)),QString("world"));
91
92
93         QCoreApplication::processEvents ();
94         QCoreApplication::processEvents ();
95         QCoreApplication::processEvents ();
96
97         QVERIFY2 (spy.count()> 0, "no signal received" );
98         QVERIFY2 (spy.count()< 2, "wtf, two signals received?" );
99
100         QList<QVariant> arguments = spy.takeFirst();
101         QVERIFY2(arguments.at(1).toString()=="world","argument missmatch");
102     }
103     void cleanupTestCase()
104     {}
105 };
106
107 QTEST_MAIN(RPCTest)
108 #include "main.moc"