qthighway/tests/auto/xqservice/ut_xqservicechannel/src/ut_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>
       
    23 #include <QThread>
       
    24 
       
    25 #include "xqservicechannel.h"
       
    26 
       
    27 #include "ut_xqservicechannel_threadstorage.h"
       
    28 
       
    29 
       
    30 static QThreadStorage<XQServiceChannelStorage *> testServiceStorage;
       
    31 
       
    32 XQServiceChannelStorage* testStorage()
       
    33 {
       
    34     if (!testServiceStorage.hasLocalData()) {
       
    35         testServiceStorage.setLocalData(new XQServiceChannelStorage());
       
    36     }
       
    37     
       
    38     return testServiceStorage.localData() ;
       
    39 }
       
    40 
       
    41 class testChannel  : public XQServiceChannel
       
    42 {
       
    43     Q_OBJECT
       
    44 public:
       
    45     explicit testChannel(const QString& channel, bool isServer, QObject *parent=0);
       
    46     virtual ~testChannel();
       
    47 
       
    48 public slots:
       
    49     QVariant receive(const QString& msg, const QByteArray &data);
       
    50     
       
    51 signals:
       
    52     void received();
       
    53 
       
    54 public:
       
    55     
       
    56 public:
       
    57     void clear();
       
    58     
       
    59     QString lastMsg;
       
    60     QByteArray lastData;
       
    61     int count;
       
    62 
       
    63 };
       
    64 
       
    65 testChannel::testChannel(const QString& channel, bool isServer, QObject *parent)
       
    66 : XQServiceChannel(channel,isServer,parent)
       
    67 {
       
    68 }
       
    69 
       
    70 testChannel::~testChannel()
       
    71 {
       
    72 }
       
    73 
       
    74 QVariant testChannel::receive(const QString& msg, const QByteArray &data)
       
    75 {
       
    76 lastMsg = msg;
       
    77 lastData = data;
       
    78 count++;
       
    79 return QVariant();
       
    80 }
       
    81 
       
    82 void testChannel::clear()
       
    83 {
       
    84 lastMsg.clear();
       
    85 lastData.clear();
       
    86 count=0;
       
    87 }
       
    88 
       
    89 class ut_XQServiceChannel : public QObject
       
    90 {
       
    91     Q_OBJECT
       
    92     
       
    93 public:
       
    94     ut_XQServiceChannel() {}
       
    95     ~ut_XQServiceChannel() {}
       
    96 
       
    97 private slots:
       
    98     void initTestCase();
       
    99     void init();
       
   100 
       
   101     void connectFail();
       
   102     
       
   103     void sendFail();
       
   104 
       
   105     void returnValue();
       
   106 
       
   107     void testSendLocally();
       
   108 
       
   109     void cleanup();
       
   110     void cleanupTestCase();
       
   111     
       
   112 public slots:
       
   113     
       
   114 signals:
       
   115 
       
   116 public:
       
   117 
       
   118 private:
       
   119 };
       
   120 
       
   121 void ut_XQServiceChannel::initTestCase()
       
   122 {
       
   123 }
       
   124 
       
   125 void ut_XQServiceChannel::cleanupTestCase()
       
   126 {
       
   127 }
       
   128 
       
   129 void ut_XQServiceChannel::init()
       
   130 {
       
   131     testStorage()->resetAll();
       
   132 }
       
   133 
       
   134 void ut_XQServiceChannel::cleanup()
       
   135 {
       
   136 }
       
   137 
       
   138 void ut_XQServiceChannel::connectFail()
       
   139 {
       
   140     XQServiceChannel* tstChannel;
       
   141     
       
   142     QString channel("com.nokia.test.Receiver");
       
   143     //fail in connect
       
   144     testStorage()->failConnect = true;
       
   145     
       
   146     tstChannel = new XQServiceChannel(channel,true );
       
   147     QCOMPARE(channel,tstChannel->channel());
       
   148     QVERIFY(!tstChannel->connectChannel());
       
   149     delete tstChannel; 
       
   150 
       
   151     //OK
       
   152     testStorage()->failConnect = false;
       
   153     tstChannel = new XQServiceChannel(channel,true );
       
   154     QCOMPARE(channel,tstChannel->channel());
       
   155     QVERIFY(tstChannel->connectChannel());
       
   156     delete tstChannel; 
       
   157 }
       
   158 
       
   159 void ut_XQServiceChannel::sendFail()
       
   160 {
       
   161     QByteArray data;
       
   162     QVariant retData;
       
   163     QString channel("com.nokia.test.Receiver");
       
   164     
       
   165     testStorage()->failConnect = true;
       
   166     //fail in connection
       
   167     QVERIFY(!XQServiceChannel::send(channel,QString("test(QString)"), data, retData,false)); //sync
       
   168     
       
   169     testStorage()->failConnect = false;
       
   170     testStorage()->failSend = true;
       
   171     //fail in send
       
   172     QVERIFY(!XQServiceChannel::send(channel,QString("test(QString)"), data, retData,false)); //sync
       
   173 
       
   174     testStorage()->failSend = false;
       
   175     //send ok
       
   176     QVERIFY(XQServiceChannel::send(channel,QString("test(QString)"), data, retData,false)); //sync
       
   177 }
       
   178 
       
   179 void ut_XQServiceChannel::returnValue()
       
   180 {
       
   181     QString channel("com.nokia.test.Receiver");
       
   182 //TODO: test return value
       
   183     testChannel* tstChannel = new testChannel(channel,true );
       
   184     QCOMPARE(channel,tstChannel->channel());
       
   185     QVERIFY(tstChannel->connectChannel());
       
   186 
       
   187     delete tstChannel; 
       
   188 }
       
   189 
       
   190 void ut_XQServiceChannel::testSendLocally()
       
   191 {
       
   192     QString channel("com.nokia.test.Receiver");
       
   193     
       
   194     testChannel* tstChannel1 = new testChannel(channel+"_1",true );
       
   195     QCOMPARE(channel+"_1",tstChannel1->channel());
       
   196     QVERIFY(tstChannel1->connectChannel());
       
   197 
       
   198     testChannel* tstChannel2 = new testChannel(channel+"_2",true );
       
   199     QCOMPARE(channel+"_2",tstChannel2->channel());
       
   200     QVERIFY(tstChannel2->connectChannel());
       
   201 
       
   202     testChannel* tstChannel3 = new testChannel(channel+"_3",true );
       
   203     QCOMPARE(channel+"_3",tstChannel3->channel());
       
   204     QVERIFY(tstChannel3->connectChannel());
       
   205 
       
   206     QByteArray data(200,'A');
       
   207     QVariant retData;
       
   208     QString msg("test(QString)");
       
   209     tstChannel1->clear();
       
   210     tstChannel2->clear();
       
   211     tstChannel3->clear();
       
   212     QVERIFY(XQServiceChannel::send(channel+"_1",msg, data, retData,false)); //sync
       
   213     QCOMPARE(tstChannel1->count,1);
       
   214     QCOMPARE(tstChannel1->lastMsg,msg);
       
   215     QCOMPARE(tstChannel1->lastData,data);
       
   216     QCOMPARE(tstChannel2->count,0);
       
   217     QCOMPARE(tstChannel3->count,0);
       
   218     tstChannel1->clear();
       
   219 
       
   220     QVERIFY(XQServiceChannel::send(channel+"_2",msg, data, retData,false)); //sync
       
   221     QCOMPARE(tstChannel2->count,1);
       
   222     QCOMPARE(tstChannel2->lastMsg,msg);
       
   223     QCOMPARE(tstChannel2->lastData,data);
       
   224     QCOMPARE(tstChannel1->count,0);
       
   225     QCOMPARE(tstChannel3->count,0);
       
   226     tstChannel2->clear();
       
   227 
       
   228     QVERIFY(XQServiceChannel::send(channel+"_3",msg, data, retData,false)); //sync
       
   229     QCOMPARE(tstChannel3->count,1);
       
   230     QCOMPARE(tstChannel3->lastMsg,msg);
       
   231     QCOMPARE(tstChannel3->lastData,data);
       
   232     QCOMPARE(tstChannel1->count,0);
       
   233     QCOMPARE(tstChannel2->count,0);
       
   234     tstChannel3->clear();
       
   235 
       
   236     delete tstChannel1; 
       
   237     delete tstChannel2; 
       
   238     delete tstChannel3; 
       
   239 }
       
   240 
       
   241 QTEST_MAIN(ut_XQServiceChannel)
       
   242 
       
   243 #include "ut_xqservicechannel.moc"