tests/auto/qcopchannel/tst_qcopchannel.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #if defined(Q_WS_QWS) && !defined(QT_NO_PROCESS)
       
    46 
       
    47 //TESTED_CLASS=
       
    48 //TESTED_FILES=
       
    49 
       
    50 #include <QCopChannel>
       
    51 #include <QProcess>
       
    52 #include "../../shared/util.h"
       
    53 
       
    54 class tst_QCopChannel : public QObject
       
    55 {
       
    56     Q_OBJECT
       
    57 
       
    58 public:
       
    59     tst_QCopChannel() {}
       
    60     virtual ~tst_QCopChannel() {}
       
    61 
       
    62 private slots:
       
    63     void channel();
       
    64     void isRegistered();
       
    65     void sendreceivemp();
       
    66     void sendreceivesp();
       
    67 protected:
       
    68     void testSend(const QString& channel, const QString& msg, const QByteArray& data=QByteArray());
       
    69 };
       
    70 
       
    71 class tst_SendQCopProcess : public QProcess
       
    72 {
       
    73     Q_OBJECT
       
    74 public:
       
    75     tst_SendQCopProcess( QObject* par )
       
    76 	: QProcess( par )
       
    77     {
       
    78     }
       
    79 
       
    80 signals:
       
    81     void messageSent();
       
    82 };
       
    83 
       
    84 void tst_QCopChannel::channel()
       
    85 {
       
    86     QCopChannel channel1("channel1");
       
    87     QCOMPARE(channel1.channel(), QString("channel1"));
       
    88 }
       
    89 
       
    90 void tst_QCopChannel::isRegistered()
       
    91 {
       
    92     QVERIFY(!QCopChannel::isRegistered("foo"));
       
    93 
       
    94     const QString channelName("registered/channel");
       
    95     QCopChannel *channel = new QCopChannel(channelName);
       
    96     QVERIFY(QCopChannel::isRegistered(channelName));
       
    97 
       
    98     delete channel;
       
    99     QVERIFY(!QCopChannel::isRegistered(channelName));
       
   100 }
       
   101 
       
   102 void tst_QCopChannel::sendreceivemp()
       
   103 {
       
   104     const QString channelName("tst_QcopChannel::send()");
       
   105     QCopChannel *channel = new QCopChannel(channelName);
       
   106     QSignalSpy spy(channel, SIGNAL(received(const QString&, const QByteArray&)));
       
   107 
       
   108     testSend("foo", "msg");
       
   109     QApplication::processEvents();
       
   110     QCOMPARE(spy.count(), 0);
       
   111 
       
   112     testSend(channelName, "msg", "data");
       
   113     QApplication::processEvents();
       
   114     QTRY_COMPARE(spy.count(), 1);
       
   115 
       
   116     QList<QVariant> args = spy.takeFirst();
       
   117     QCOMPARE(args.at(0).toString(), QString("msg"));
       
   118     QCOMPARE(args.at(1).toByteArray(), QByteArray("data"));
       
   119 
       
   120     QCOMPARE(spy.count(), 0);
       
   121 
       
   122     delete channel;
       
   123     testSend(channelName, "msg2");
       
   124     QApplication::processEvents();
       
   125     QCOMPARE(spy.count(), 0);
       
   126 }
       
   127 
       
   128 void tst_QCopChannel::sendreceivesp()
       
   129 {
       
   130     const QString channelName("tst_QcopChannel::send()");
       
   131     QCopChannel *channel = new QCopChannel(channelName);
       
   132     QSignalSpy spy(channel, SIGNAL(received(const QString&, const QByteArray&)));
       
   133     QCopChannel::send("foo", "msg");
       
   134     QApplication::processEvents();
       
   135     QCOMPARE(spy.count(), 0);
       
   136     QCopChannel::send(channelName, "msg", "data");
       
   137     QApplication::processEvents();
       
   138     QTRY_COMPARE(spy.count(), 1);
       
   139 
       
   140     QList<QVariant> args = spy.takeFirst();
       
   141     QCOMPARE(args.at(0).toString(), QString("msg"));
       
   142     QCOMPARE(args.at(1).toByteArray(), QByteArray("data"));
       
   143 
       
   144     QCOMPARE(spy.count(), 0);
       
   145 
       
   146     delete channel;
       
   147     QCopChannel::send(channelName, "msg2");
       
   148     QApplication::processEvents();
       
   149     QCOMPARE(spy.count(), 0);
       
   150 }
       
   151 
       
   152 void tst_QCopChannel::testSend( const QString& channel, const QString& msg, const QByteArray& data )
       
   153 {
       
   154     QProcess proc;
       
   155     QStringList args;
       
   156     args << channel << msg;
       
   157     if( !data.isEmpty() )
       
   158 	args << data;
       
   159     proc.start( "testSend/testSend", args );
       
   160 
       
   161     QTest::qWait(100);
       
   162 
       
   163     QVERIFY(proc.state() == QProcess::NotRunning || proc.waitForFinished());
       
   164     QCOMPARE(proc.exitStatus(), QProcess::NormalExit);
       
   165     QVERIFY(proc.readAll() == "done"); // sanity check
       
   166 }
       
   167 
       
   168 QTEST_MAIN(tst_QCopChannel)
       
   169 
       
   170 #include "tst_qcopchannel.moc"
       
   171 
       
   172 #else // Q_WS_QWS
       
   173 QTEST_NOOP_MAIN
       
   174 #endif