tests/auto/qdbusreply/tst_qdbusreply.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 #include <qcoreapplication.h>
       
    42 #include <qdebug.h>
       
    43 #include <qvariant.h>
       
    44 
       
    45 #include <QtTest/QtTest>
       
    46 
       
    47 #include <QtDBus>
       
    48 
       
    49 typedef QMap<int,QString> IntStringMap;
       
    50 Q_DECLARE_METATYPE(IntStringMap)
       
    51 
       
    52 struct MyStruct
       
    53 {
       
    54     int i;
       
    55     QString s;
       
    56 
       
    57     MyStruct() : i(1), s("String") { }
       
    58     bool operator==(const MyStruct &other) const
       
    59     { return i == other.i && s == other.s; }
       
    60 };
       
    61 Q_DECLARE_METATYPE(MyStruct)
       
    62 
       
    63 QDBusArgument &operator<<(QDBusArgument &arg, const MyStruct &ms)
       
    64 {
       
    65     arg.beginStructure();
       
    66     arg << ms.i << ms.s;
       
    67     arg.endStructure();
       
    68     return arg;
       
    69 }
       
    70 
       
    71 const QDBusArgument &operator>>(const QDBusArgument &arg, MyStruct &ms)
       
    72 {
       
    73     arg.beginStructure();
       
    74     arg >> ms.i >> ms.s;
       
    75     arg.endStructure();
       
    76     return arg;
       
    77 }
       
    78 
       
    79 class TypesInterface;
       
    80 class tst_QDBusReply: public QObject
       
    81 {
       
    82     Q_OBJECT
       
    83     QDBusInterface *iface;
       
    84     TypesInterface *adaptor;
       
    85 public:
       
    86     tst_QDBusReply();
       
    87 
       
    88 private slots:
       
    89     void initTestCase()
       
    90     {
       
    91         qDBusRegisterMetaType<IntStringMap>();
       
    92         qDBusRegisterMetaType<MyStruct>();
       
    93     }
       
    94 
       
    95     void init();
       
    96     void simpleTypes();
       
    97     void complexTypes();
       
    98     void wrongTypes();
       
    99 };
       
   100 
       
   101 class TypesInterface: public QDBusAbstractAdaptor
       
   102 {
       
   103     Q_OBJECT
       
   104     Q_CLASSINFO("D-Bus Interface", "com.trolltech.Qt.Autotests.TypesInterface")
       
   105 public:
       
   106     TypesInterface(QObject *parent)
       
   107         : QDBusAbstractAdaptor(parent)
       
   108     { }
       
   109 
       
   110 public slots:
       
   111     bool retrieveBool()
       
   112     {
       
   113         return true;
       
   114     }
       
   115 
       
   116     uchar retrieveUChar()
       
   117     {
       
   118         return 'A';
       
   119     }
       
   120 
       
   121     short retrieveShort()
       
   122     {
       
   123         return -47;
       
   124     }
       
   125 
       
   126     ushort retrieveUShort()
       
   127     {
       
   128         return 42U;
       
   129     }
       
   130 
       
   131     int retrieveInt()
       
   132     {
       
   133         return -470000;
       
   134     }
       
   135 
       
   136     uint retrieveUInt()
       
   137     {
       
   138         return 42424242;
       
   139     }
       
   140 
       
   141     qlonglong retrieveLongLong()
       
   142     {
       
   143         return -(Q_INT64_C(1) << 32);
       
   144     }
       
   145 
       
   146     qulonglong retrieveULongLong()
       
   147     {
       
   148         return Q_INT64_C(1) << 32;
       
   149     }
       
   150 
       
   151     double retrieveDouble()
       
   152     {
       
   153         return 1.5;
       
   154     }
       
   155 
       
   156     QString retrieveString()
       
   157     {
       
   158         return "This string you should see";
       
   159     }
       
   160 
       
   161     QDBusObjectPath retrieveObjectPath()
       
   162     {
       
   163         return QDBusObjectPath("/");
       
   164     }
       
   165 
       
   166     QDBusSignature retrieveSignature()
       
   167     {
       
   168         return QDBusSignature("g");
       
   169     }
       
   170 
       
   171     QDBusVariant retrieveVariant()
       
   172     {
       
   173         return QDBusVariant(retrieveString());
       
   174     }
       
   175 
       
   176     QStringList retrieveStringList()
       
   177     {
       
   178         return QStringList() << "one" << "two";
       
   179     }
       
   180 
       
   181     QByteArray retrieveByteArray()
       
   182     {
       
   183         return "Hello, World";
       
   184     }
       
   185 
       
   186     QVariantList retrieveList()
       
   187     {
       
   188         return QVariantList() << retrieveInt() << retrieveString()
       
   189                               << retrieveByteArray();
       
   190     }
       
   191 
       
   192     QList<QDBusObjectPath> retrieveObjectPathList()
       
   193     {
       
   194         return QList<QDBusObjectPath>() << QDBusObjectPath("/") << QDBusObjectPath("/foo");
       
   195     }
       
   196 
       
   197     QVariantMap retrieveMap()
       
   198     {
       
   199         QVariantMap map;
       
   200         map["one"] = 1;
       
   201         map["two"] = 2U;
       
   202         map["string"] = retrieveString();
       
   203         map["stringlist"] = retrieveStringList();
       
   204         return map;
       
   205     }
       
   206 
       
   207     IntStringMap retrieveIntStringMap()
       
   208     {
       
   209         IntStringMap map;
       
   210         map[1] = "1";
       
   211         map[2] = "2";
       
   212         map[-1231456] = "foo";
       
   213         return map;
       
   214     }
       
   215 
       
   216     MyStruct retrieveStruct()
       
   217     {
       
   218         return MyStruct();
       
   219     }
       
   220 };
       
   221 
       
   222 tst_QDBusReply::tst_QDBusReply()
       
   223 {
       
   224     adaptor = new TypesInterface(this);
       
   225     QDBusConnection::sessionBus().registerObject("/", this);
       
   226 
       
   227     iface = new QDBusInterface(QDBusConnection::sessionBus().baseService(), "/",
       
   228                                "com.trolltech.Qt.Autotests.TypesInterface",
       
   229                                QDBusConnection::sessionBus(),
       
   230                                this);
       
   231 }
       
   232 
       
   233 void tst_QDBusReply::init()
       
   234 {
       
   235     QVERIFY(iface);
       
   236     QVERIFY(iface->isValid());
       
   237 }
       
   238 
       
   239 void tst_QDBusReply::simpleTypes()
       
   240 {
       
   241     QDBusReply<bool> rbool = iface->call(QDBus::BlockWithGui, "retrieveBool");
       
   242     QVERIFY(rbool.isValid());
       
   243     QCOMPARE(rbool.value(), adaptor->retrieveBool());
       
   244 
       
   245     QDBusReply<uchar> ruchar = iface->call(QDBus::BlockWithGui, "retrieveUChar");
       
   246     QVERIFY(ruchar.isValid());
       
   247     QCOMPARE(ruchar.value(), adaptor->retrieveUChar());
       
   248 
       
   249     QDBusReply<short> rshort = iface->call(QDBus::BlockWithGui, "retrieveShort");
       
   250     QVERIFY(rshort.isValid());
       
   251     QCOMPARE(rshort.value(), adaptor->retrieveShort());
       
   252 
       
   253     QDBusReply<ushort> rushort = iface->call(QDBus::BlockWithGui, "retrieveUShort");
       
   254     QVERIFY(rushort.isValid());
       
   255     QCOMPARE(rushort.value(), adaptor->retrieveUShort());
       
   256 
       
   257     QDBusReply<int> rint = iface->call(QDBus::BlockWithGui, "retrieveInt");
       
   258     QVERIFY(rint.isValid());
       
   259     QCOMPARE(rint.value(), adaptor->retrieveInt());
       
   260 
       
   261     QDBusReply<uint> ruint = iface->call(QDBus::BlockWithGui, "retrieveUInt");
       
   262     QVERIFY(ruint.isValid());
       
   263     QCOMPARE(ruint.value(), adaptor->retrieveUInt());
       
   264 
       
   265     QDBusReply<qlonglong> rqlonglong = iface->call(QDBus::BlockWithGui, "retrieveLongLong");
       
   266     QVERIFY(rqlonglong.isValid());
       
   267     QCOMPARE(rqlonglong.value(), adaptor->retrieveLongLong());
       
   268 
       
   269     QDBusReply<qulonglong> rqulonglong = iface->call(QDBus::BlockWithGui, "retrieveULongLong");
       
   270     QVERIFY(rqulonglong.isValid());
       
   271     QCOMPARE(rqulonglong.value(), adaptor->retrieveULongLong());
       
   272 
       
   273     QDBusReply<double> rdouble = iface->call(QDBus::BlockWithGui, "retrieveDouble");
       
   274     QVERIFY(rdouble.isValid());
       
   275     QCOMPARE(rdouble.value(), adaptor->retrieveDouble());
       
   276 
       
   277     QDBusReply<QString> rstring = iface->call(QDBus::BlockWithGui, "retrieveString");
       
   278     QVERIFY(rstring.isValid());
       
   279     QCOMPARE(rstring.value(), adaptor->retrieveString());
       
   280 
       
   281     QDBusReply<QDBusObjectPath> robjectpath = iface->call(QDBus::BlockWithGui, "retrieveObjectPath");
       
   282     QVERIFY(robjectpath.isValid());
       
   283     QCOMPARE(robjectpath.value().path(), adaptor->retrieveObjectPath().path());
       
   284 
       
   285     QDBusReply<QDBusSignature> rsignature = iface->call(QDBus::BlockWithGui, "retrieveSignature");
       
   286     QVERIFY(rsignature.isValid());
       
   287     QCOMPARE(rsignature.value().signature(), adaptor->retrieveSignature().signature());
       
   288 
       
   289     QDBusReply<QDBusVariant> rdbusvariant = iface->call(QDBus::BlockWithGui, "retrieveVariant");
       
   290     QVERIFY(rdbusvariant.isValid());
       
   291     QCOMPARE(rdbusvariant.value().variant(), adaptor->retrieveVariant().variant());
       
   292 
       
   293     QDBusReply<QVariant> rvariant = iface->call(QDBus::BlockWithGui, "retrieveVariant");
       
   294     QVERIFY(rvariant.isValid());
       
   295     QCOMPARE(rvariant.value(), adaptor->retrieveVariant().variant());
       
   296 
       
   297     QDBusReply<QByteArray> rbytearray = iface->call(QDBus::BlockWithGui, "retrieveByteArray");
       
   298     QVERIFY(rbytearray.isValid());
       
   299     QCOMPARE(rbytearray.value(), adaptor->retrieveByteArray());
       
   300 
       
   301     QDBusReply<QStringList> rstringlist = iface->call(QDBus::BlockWithGui, "retrieveStringList");
       
   302     QVERIFY(rstringlist.isValid());
       
   303     QCOMPARE(rstringlist.value(), adaptor->retrieveStringList());
       
   304 }
       
   305 
       
   306 void tst_QDBusReply::complexTypes()
       
   307 {
       
   308     QDBusReply<QVariantList> rlist = iface->call(QDBus::BlockWithGui, "retrieveList");
       
   309     QVERIFY(rlist.isValid());
       
   310     QCOMPARE(rlist.value(), adaptor->retrieveList());
       
   311 
       
   312     QDBusReply<QList<QDBusObjectPath> > rolist = iface->call(QDBus::BlockWithGui, "retrieveObjectPathList");
       
   313     QVERIFY(rolist.isValid());
       
   314     QCOMPARE(rolist.value(), adaptor->retrieveObjectPathList());
       
   315 
       
   316     QDBusReply<QVariantMap> rmap = iface->call(QDBus::BlockWithGui, "retrieveMap");
       
   317     QVERIFY(rmap.isValid());
       
   318     QCOMPARE(rmap.value(), adaptor->retrieveMap());
       
   319 
       
   320     QDBusReply<IntStringMap> rismap = iface->call(QDBus::BlockWithGui, "retrieveIntStringMap");
       
   321     QVERIFY(rismap.isValid());
       
   322     QCOMPARE(rismap.value(), adaptor->retrieveIntStringMap());
       
   323 
       
   324     QDBusReply<MyStruct> rstruct = iface->call(QDBus::BlockWithGui, "retrieveStruct");
       
   325     QVERIFY(rstruct.isValid());
       
   326     QCOMPARE(rstruct.value(), adaptor->retrieveStruct());
       
   327 }
       
   328 
       
   329 void tst_QDBusReply::wrongTypes()
       
   330 {
       
   331     QDBusReply<bool> rbool = iface->call(QDBus::BlockWithGui, "retrieveInt");
       
   332     QVERIFY(!rbool.isValid());
       
   333 
       
   334     rbool = iface->call(QDBus::BlockWithGui, "retrieveShort");
       
   335     QVERIFY(!rbool.isValid());
       
   336 
       
   337     rbool = iface->call(QDBus::BlockWithGui, "retrieveStruct");
       
   338     QVERIFY(!rbool.isValid());
       
   339 
       
   340     QDBusReply<short> rshort = iface->call(QDBus::BlockWithGui, "retrieveInt");
       
   341     QVERIFY(!rshort.isValid());
       
   342 
       
   343     rshort = iface->call(QDBus::BlockWithGui, "retrieveBool");
       
   344     QVERIFY(!rshort.isValid());
       
   345 
       
   346     rshort = iface->call(QDBus::BlockWithGui, "retrieveStruct");
       
   347     QVERIFY(!rshort.isValid());
       
   348 
       
   349     QDBusReply<MyStruct> rstruct = iface->call(QDBus::BlockWithGui, "retrieveInt");
       
   350     QVERIFY(!rstruct.isValid());
       
   351 
       
   352     rstruct = iface->call(QDBus::BlockWithGui, "retrieveShort");
       
   353     QVERIFY(!rstruct.isValid());
       
   354 
       
   355     rstruct = iface->call(QDBus::BlockWithGui, "retrieveIntStringMap");
       
   356     QVERIFY(!rstruct.isValid());
       
   357 }
       
   358 
       
   359 QTEST_MAIN(tst_QDBusReply)
       
   360 
       
   361 #include "tst_qdbusreply.moc"