qthighway/tests/auto/xqservice/tst_xqservicechannel/src/tst_xqservicechannel.cpp
changeset 18 1b485afba084
parent 16 19b186e43276
child 28 19321a443c34
equal deleted inserted replaced
16:19b186e43276 18:1b485afba084
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:                                                         
       
    19 *
       
    20 */
       
    21 
       
    22 #include <QtTest/QtTest>
       
    23 #include <QtCore/qthread.h>
       
    24 #include <QtCore/qcoreevent.h>
       
    25 #include "xqservicechannel.h"
       
    26 #include "xqsharablefile.h"
       
    27 
       
    28 const QString ConstReturnValue("ReturnValue=OK");
       
    29 
       
    30 class TestChannel: public XQServiceChannel 
       
    31 {
       
    32     Q_OBJECT
       
    33 
       
    34 public:
       
    35     TestChannel(const QString& ch, QObject *parent = 0) 
       
    36         : XQServiceChannel(ch,true,parent),count(0)
       
    37         {
       
    38         }
       
    39     
       
    40     ~TestChannel() 
       
    41         {
       
    42         }
       
    43     
       
    44     QVariant receive(const QString& msg, const QByteArray &data);
       
    45     
       
    46 signals:
       
    47     void received();
       
    48       
       
    49 public:
       
    50     QString lastMsg;
       
    51     QByteArray lastData;
       
    52     int count;
       
    53 };
       
    54 
       
    55 QVariant TestChannel::receive(const QString& msg, const QByteArray &data)
       
    56 {
       
    57     lastMsg = msg;
       
    58     lastData = data;
       
    59     ++count;
       
    60     return QVariant(ConstReturnValue);
       
    61 }
       
    62 
       
    63 class ServerThread : public QThread
       
    64 {
       
    65     Q_OBJECT
       
    66 
       
    67 public:
       
    68     ServerThread(const QString ch,QObject *parent = 0) : QThread(parent), tstChannel(NULL), channel(ch)
       
    69     {
       
    70     }
       
    71     
       
    72     ~ServerThread() 
       
    73     {
       
    74     }
       
    75 
       
    76 public slots:
       
    77 
       
    78 signals:
       
    79     void serverThreadStarted();
       
    80       
       
    81 protected:
       
    82     void run();
       
    83 
       
    84 public:
       
    85     QString channel;
       
    86     TestChannel* tstChannel;
       
    87 };
       
    88 
       
    89 void ServerThread::run()
       
    90 {
       
    91     tstChannel = new TestChannel(channel);
       
    92     bool ret = tstChannel->connectChannel();
       
    93     emit serverThreadStarted();
       
    94     exec();
       
    95     delete tstChannel;
       
    96 }
       
    97 
       
    98 class tst_XQServiceChannel : public QObject, public XQServiceRequestCompletedAsync
       
    99 {
       
   100     Q_OBJECT
       
   101 public:
       
   102     tst_XQServiceChannel() {}
       
   103     ~tst_XQServiceChannel() {}
       
   104 
       
   105 private slots:
       
   106     void initTestCase();
       
   107     void init();
       
   108 
       
   109     void send_data();
       
   110     void send();
       
   111 
       
   112     void sendLocally_data();
       
   113     void sendLocally();
       
   114 
       
   115     void cleanup();
       
   116     void cleanupTestCase();
       
   117     
       
   118 public slots:
       
   119     void signalSeen();
       
   120 
       
   121 public:
       
   122     void requestCompletedAsync(const QVariant &retValue);
       
   123     void requestErrorAsync(int err);
       
   124     
       
   125 signals:
       
   126     void received();
       
   127     void sendLocally(const QString& ch, const QString& msg,
       
   128                             const QByteArray &data);    
       
   129 protected:
       
   130 
       
   131 private:
       
   132     ServerThread* startThreadChannel(const QString &ch);
       
   133     void stopThreadChannel(ServerThread* st);
       
   134 
       
   135     ServerThread *serverThread;
       
   136 
       
   137     QEventLoop *eventLoop;
       
   138     bool signalAlreadySeen;
       
   139     TestChannel* tstChannelLocally;
       
   140     
       
   141     void expectSignal(QObject *object, const char *signal);
       
   142     bool waitForSignal(int timeout = 1000);
       
   143     QVariant lastRetValue ;
       
   144 };
       
   145 
       
   146 void tst_XQServiceChannel::initTestCase()
       
   147 {
       
   148     serverThread = startThreadChannel(QLatin1String("TestChannel")) ;
       
   149     tstChannelLocally = new TestChannel(QLatin1String("TestChannelLocally"));
       
   150     QVERIFY(tstChannelLocally->connectChannel());
       
   151     connect(tstChannelLocally, SIGNAL(received()),
       
   152             this, SIGNAL(received()));
       
   153 }
       
   154 
       
   155 void tst_XQServiceChannel::cleanupTestCase()
       
   156 {
       
   157    stopThreadChannel(serverThread) ;
       
   158 }
       
   159 
       
   160 void tst_XQServiceChannel::init()
       
   161 {
       
   162 }
       
   163 
       
   164 void tst_XQServiceChannel::cleanup()
       
   165 {
       
   166 }
       
   167 
       
   168 void tst_XQServiceChannel::send_data()
       
   169 {
       
   170     QString currentChannel;
       
   171 
       
   172     if (qstrcmp(QTest::currentTestFunction(), "sendLocally") == 0){
       
   173         currentChannel = "TestChannelLocally";
       
   174     }
       
   175     else {
       
   176         currentChannel = "TestChannel";
       
   177     }
       
   178 
       
   179     QTest::addColumn<QString>("chan");
       
   180     QTest::addColumn<QString>("msg");
       
   181     QTest::addColumn<QByteArray>("data");
       
   182 
       
   183     QTest::newRow("simple") 
       
   184         << currentChannel 
       
   185         << "message()" 
       
   186         << QByteArray();
       
   187 
       
   188     QTest::newRow("withargs")
       
   189         << currentChannel 
       
   190         << "message(QString,int,QByteArray)"
       
   191         << QByteArray(32, 'A');
       
   192 
       
   193     QTest::newRow("longmessage")
       
   194         << currentChannel
       
   195         << (QLatin1String("message(QString,int,QByteArray)") + QString(1024, QChar('X')))
       
   196         << QByteArray(32, 'A');
       
   197 
       
   198     QTest::newRow("longdata-1024")
       
   199         << currentChannel 
       
   200         << "message(QString,int,QByteArray)"
       
   201         << QByteArray(1024, 'A');
       
   202 
       
   203     QTest::newRow("longdata-2048")
       
   204         << currentChannel 
       
   205         << "message(QString,int,QByteArray)"
       
   206         << QByteArray(2048, 'A');
       
   207 
       
   208     QTest::newRow("longdata-4096")
       
   209         << currentChannel 
       
   210         << "message(QString,int,QByteArray)"
       
   211         << QByteArray(4096, 'A');
       
   212     
       
   213     QTest::newRow("longdata-64K")
       
   214         << currentChannel 
       
   215         << "message(QString,int,QByteArray)"
       
   216         << QByteArray(65535, 'A');
       
   217 }
       
   218 
       
   219 void tst_XQServiceChannel::send()
       
   220 {
       
   221     QFETCH(QString, chan);
       
   222     QFETCH(QString, msg);
       
   223     QFETCH(QByteArray, data);
       
   224 
       
   225     serverThread->tstChannel->count = 0;
       
   226 
       
   227     // We expect to see the received() signal if the message is delivered.
       
   228     expectSignal(this, SIGNAL(received()));
       
   229 
       
   230     QVariant ret; // not used for asyn
       
   231     
       
   232     // Send the message.
       
   233     QVERIFY(XQServiceChannel::send(chan, msg, data, ret, false,this));
       
   234 
       
   235     // Wait until the message is delivered or a timeout occurs.
       
   236     QVERIFY(waitForSignal());
       
   237 
       
   238     // Check that the correct values were delivered.
       
   239     QCOMPARE(lastRetValue.toString(), ConstReturnValue);
       
   240     QCOMPARE(serverThread->tstChannel->lastMsg, msg);
       
   241     QCOMPARE(serverThread->tstChannel->lastData, data);
       
   242     QCOMPARE(serverThread->tstChannel->count, 1);
       
   243 }
       
   244 
       
   245 void tst_XQServiceChannel::sendLocally_data()
       
   246 {
       
   247     send_data();
       
   248 }
       
   249 
       
   250 void tst_XQServiceChannel::sendLocally()
       
   251 {
       
   252     QFETCH(QString, chan);
       
   253     QFETCH(QString, msg);
       
   254     QFETCH(QByteArray, data);
       
   255     
       
   256     tstChannelLocally->count = 0;
       
   257     
       
   258     // Send the message.
       
   259     XQSharableFile dummy;
       
   260     QVariant ret = XQServiceChannel::sendLocally(chan, msg, data, dummy);
       
   261     
       
   262     // Check that the correct values were delivered.
       
   263     QCOMPARE(ret.toString(), ConstReturnValue);
       
   264     QCOMPARE(tstChannelLocally->lastMsg, msg);
       
   265     QCOMPARE(tstChannelLocally->lastData, data);
       
   266     QCOMPARE(tstChannelLocally->count, 1);
       
   267 
       
   268 }
       
   269 
       
   270 void tst_XQServiceChannel::expectSignal(QObject *object, const char *signal)
       
   271 {
       
   272     signalAlreadySeen = false;
       
   273     connect(object, signal, this, SLOT(signalSeen()));
       
   274 }
       
   275 
       
   276 bool tst_XQServiceChannel::waitForSignal(int timeout)
       
   277 {
       
   278     if (signalAlreadySeen)
       
   279         return true;
       
   280     QEventLoop loop;
       
   281     eventLoop = &loop;
       
   282     QTimer::singleShot(timeout, eventLoop, SLOT(quit()));
       
   283     loop.exec();
       
   284     eventLoop = 0;
       
   285     return signalAlreadySeen;
       
   286 }
       
   287 
       
   288 void tst_XQServiceChannel::signalSeen()
       
   289 {
       
   290     signalAlreadySeen = true;
       
   291     if (eventLoop)
       
   292         eventLoop->quit();
       
   293 }
       
   294 
       
   295 ServerThread* tst_XQServiceChannel::startThreadChannel(const QString &ch)
       
   296 {
       
   297     ServerThread* st = new ServerThread(ch, this);
       
   298     expectSignal(st, SIGNAL(serverThreadStarted()));
       
   299     st->start();
       
   300 
       
   301     QTest::qVerify(waitForSignal(), "startThreadChannel", "", __FILE__, __LINE__);
       
   302     
       
   303     disconnect(st, SIGNAL(serverThreadStarted()), this, SLOT(signalSeen()));
       
   304     connect(st->tstChannel, SIGNAL(received()),
       
   305             this, SIGNAL(received()));
       
   306 
       
   307     eventLoop = 0;
       
   308     signalAlreadySeen = false;
       
   309     return st;
       
   310 }
       
   311 
       
   312 void tst_XQServiceChannel::stopThreadChannel(ServerThread* st)
       
   313 {
       
   314     st->quit();
       
   315     st->wait();
       
   316     delete st;
       
   317 }
       
   318 
       
   319 void tst_XQServiceChannel::requestCompletedAsync(const QVariant &retValue)
       
   320 {
       
   321     lastRetValue = retValue;
       
   322     emit received();
       
   323 }
       
   324 
       
   325 void tst_XQServiceChannel::requestErrorAsync(int err)
       
   326 {
       
   327     //nothing to emit for error here
       
   328 }
       
   329 
       
   330 QTEST_MAIN(tst_XQServiceChannel)
       
   331 
       
   332 #include "tst_xqservicechannel.moc"