tests/auto/qserviceinterfacedescriptor/tst_qserviceinterfacedescriptor.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 Qt Mobility Components.
       
     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 <QtTest/QtTest>
       
    42 #include <QtCore>
       
    43 #ifndef QT_NO_DEBUG_STREAM
       
    44 #include <QDebug>
       
    45 #endif
       
    46 #include <qserviceinterfacedescriptor.h>
       
    47 #include <qserviceinterfacedescriptor_p.h>
       
    48 
       
    49 QTM_USE_NAMESPACE
       
    50 class tst_QServiceInterfaceDescriptor: public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53     
       
    54 private slots:
       
    55     void initTestCase();
       
    56     void cleanupTestCase();
       
    57     void comparison();
       
    58 #ifndef QT_NO_DATASTREAM
       
    59     void testStreamOperators();
       
    60 #endif
       
    61 #ifndef QT_NO_DEBUG_STREAM
       
    62     void testDebugStream();
       
    63 #endif
       
    64     void destructor();
       
    65 };
       
    66 
       
    67 void tst_QServiceInterfaceDescriptor::initTestCase()
       
    68 {
       
    69 }
       
    70 
       
    71 void tst_QServiceInterfaceDescriptor::comparison()
       
    72 {
       
    73     QServiceInterfaceDescriptor desc;
       
    74     QVERIFY(desc.majorVersion() == -1);
       
    75     QVERIFY(desc.minorVersion() == -1);
       
    76     QVERIFY(desc.serviceName().isEmpty());
       
    77     QVERIFY(desc.interfaceName().isEmpty());
       
    78     QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::Capabilities).isValid());
       
    79     QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::Location).isValid());
       
    80     QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::InterfaceDescription).isValid());
       
    81     QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::ServiceDescription).isValid());
       
    82     QVERIFY(desc.scope() == QService::UserScope);
       
    83     QVERIFY(!desc.isValid());
       
    84 
       
    85     QServiceInterfaceDescriptor copy(desc);
       
    86     QVERIFY(copy.majorVersion() == -1);
       
    87     QVERIFY(copy.minorVersion() == -1);
       
    88     QVERIFY(copy.serviceName().isEmpty());
       
    89     QVERIFY(copy.interfaceName().isEmpty());
       
    90     QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::Capabilities).isValid());
       
    91     QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::Location).isValid());
       
    92     QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::InterfaceDescription).isValid());
       
    93     QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::ServiceDescription).isValid());
       
    94     QVERIFY(copy.scope() == QService::UserScope);
       
    95     QVERIFY(!copy.isValid());
       
    96 
       
    97     QVERIFY(desc == copy);
       
    98 
       
    99     QServiceInterfaceDescriptor valid;
       
   100     QServiceInterfaceDescriptorPrivate *d = new QServiceInterfaceDescriptorPrivate();
       
   101     QServiceInterfaceDescriptorPrivate::setPrivate(&valid, d);
       
   102     d->serviceName = "name";
       
   103     d->interfaceName = "interface";
       
   104     d->major = 3;
       
   105     d->minor = 1;
       
   106     d->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("mydescription"));
       
   107     d->customAttributes.insert(QString("ckey"), QString("cvalue"));
       
   108     d->scope = QService::SystemScope;
       
   109 
       
   110     QCOMPARE(valid.interfaceName(), QString("interface"));
       
   111     QCOMPARE(valid.serviceName(), QString("name"));
       
   112     QCOMPARE(valid.majorVersion(), 3);
       
   113     QCOMPARE(valid.minorVersion(), 1);
       
   114     QCOMPARE(valid.customAttribute("ckey"), QString("cvalue"));
       
   115     QCOMPARE(valid.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
       
   116     QCOMPARE(valid.attribute(QServiceInterfaceDescriptor::Location).toString(), QString(""));
       
   117     QCOMPARE(valid.scope(), QService::SystemScope);
       
   118     QVERIFY(valid.isValid());
       
   119 
       
   120     QVERIFY(valid != desc);
       
   121     QVERIFY(desc != valid);
       
   122 
       
   123     //test copy constructor
       
   124     QServiceInterfaceDescriptor validCopy(valid);
       
   125     QVERIFY(valid==validCopy);
       
   126     QVERIFY(validCopy==valid);
       
   127 
       
   128     QServiceInterfaceDescriptorPrivate::getPrivate(&validCopy)->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue"));
       
   129     QVERIFY(valid!=validCopy);
       
   130     QVERIFY(validCopy!=valid);
       
   131 
       
   132     QCOMPARE(validCopy.interfaceName(), QString("interface"));
       
   133     QCOMPARE(validCopy.serviceName(), QString("name"));
       
   134     QCOMPARE(validCopy.majorVersion(), 3);
       
   135     QCOMPARE(validCopy.minorVersion(), 1);
       
   136     QCOMPARE(validCopy.attribute(QServiceInterfaceDescriptor::Location).toString(), QString("myValue"));
       
   137     QCOMPARE(validCopy.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
       
   138     QCOMPARE(validCopy.customAttribute("ckey"),QString("cvalue"));
       
   139     QCOMPARE(validCopy.scope(), QService::SystemScope);
       
   140     QVERIFY(validCopy.isValid());
       
   141 
       
   142     //test assignment operator
       
   143     QServiceInterfaceDescriptor validCopy2 = valid;
       
   144     QVERIFY(valid==validCopy2);
       
   145     QVERIFY(validCopy2==valid);
       
   146 
       
   147     QServiceInterfaceDescriptorPrivate::getPrivate(&validCopy2)->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue2"));
       
   148     QVERIFY(valid!=validCopy2);
       
   149     QVERIFY(validCopy2!=valid);
       
   150 
       
   151     QCOMPARE(validCopy2.interfaceName(), QString("interface"));
       
   152     QCOMPARE(validCopy2.serviceName(), QString("name"));
       
   153     QCOMPARE(validCopy2.majorVersion(), 3);
       
   154     QCOMPARE(validCopy2.minorVersion(), 1);
       
   155     QCOMPARE(validCopy2.attribute(QServiceInterfaceDescriptor::Location).toString(), QString("myValue2"));
       
   156     QCOMPARE(validCopy2.customAttribute("ckey"),QString("cvalue"));
       
   157     QCOMPARE(validCopy2.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
       
   158     QCOMPARE(validCopy2.scope(), QService::SystemScope);
       
   159     QVERIFY(validCopy2.isValid());
       
   160 
       
   161     //test customAttributes
       
   162     d->customAttributes.insert(QString("ckey"), QString("cvalue"));
       
   163     d->customAttributes.insert(QString("ckey1"), QString("cvalue1"));
       
   164     d->customAttributes.insert(QString("ckey2"), QString("cvalue2"));
       
   165     QStringList customAttributes = valid.customAttributes();
       
   166     QVERIFY(customAttributes.contains("ckey"));
       
   167     QVERIFY(customAttributes.contains("ckey1"));
       
   168     QVERIFY(customAttributes.contains("ckey2"));
       
   169     QCOMPARE(customAttributes.count(), 3);
       
   170 }
       
   171 
       
   172 #ifndef QT_NO_DATASTREAM
       
   173 void tst_QServiceInterfaceDescriptor::testStreamOperators()
       
   174 {
       
   175     QByteArray byteArray;
       
   176     QBuffer buffer(&byteArray);
       
   177     buffer.open(QIODevice::ReadWrite);
       
   178     QDataStream stream(&buffer);
       
   179 
       
   180 
       
   181     QServiceInterfaceDescriptor empty;
       
   182     QVERIFY(!empty.isValid());
       
   183 
       
   184     //stream invalid into invalid
       
   185     QServiceInterfaceDescriptor invalid;
       
   186     QVERIFY(!invalid.isValid());
       
   187     QVERIFY(invalid == empty);
       
   188     buffer.seek(0);
       
   189     stream << empty;
       
   190     buffer.seek(0);
       
   191     stream >> invalid;
       
   192     QVERIFY(invalid == empty);
       
   193 
       
   194     //stream invalid into valid
       
   195     QServiceInterfaceDescriptor valid;
       
   196     QServiceInterfaceDescriptorPrivate *d = new QServiceInterfaceDescriptorPrivate();
       
   197     QServiceInterfaceDescriptorPrivate::setPrivate(&valid, d);
       
   198     d->serviceName = "name";
       
   199     d->interfaceName = "interface";
       
   200     d->major = 3;
       
   201     d->minor = 1;
       
   202     d->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue"));
       
   203     d->attributes.insert(QServiceInterfaceDescriptor::Capabilities, QStringList() << "val1" << "val2");
       
   204     d->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("This is the service description"));
       
   205     d->attributes.insert(QServiceInterfaceDescriptor::InterfaceDescription, QString("This is the interface description"));
       
   206     d->customAttributes.insert(QString("key1"), QString("value1"));
       
   207     d->customAttributes.insert(QString("abcd"), QString("efgh"));
       
   208     d->customAttributes.insert(QString("empty"), QString(""));
       
   209     d->scope = QService::SystemScope;
       
   210     QVERIFY(valid.isValid());
       
   211     QServiceInterfaceDescriptor validref = valid;
       
   212     QVERIFY(validref == valid);
       
   213     QVERIFY(!(validref!=valid));
       
   214     QVERIFY(empty!=validref);
       
   215 
       
   216     buffer.seek(0);
       
   217     stream << empty;
       
   218     buffer.seek(0);
       
   219     stream >> validref;
       
   220     QVERIFY(empty == validref);
       
   221     QVERIFY(!(empty!=validref));
       
   222     QVERIFY(validref != valid);
       
   223     
       
   224     //stream valid into invalid
       
   225     QServiceInterfaceDescriptor invalid2;
       
   226     QVERIFY(!invalid2.isValid());
       
   227     validref = valid;
       
   228     QVERIFY(validref == valid);
       
   229     QVERIFY(validref != invalid2);
       
   230 
       
   231     buffer.seek(0);
       
   232     stream << validref;
       
   233     buffer.seek(0);
       
   234     stream >> invalid2;
       
   235     QVERIFY(invalid2 == validref);
       
   236     QVERIFY(!(invalid2 != validref));
       
   237     QVERIFY(invalid2.isValid());
       
   238     QVERIFY(invalid2.interfaceName() == QString("interface"));
       
   239     QVERIFY(invalid2.serviceName() == QString("name"));
       
   240     QVERIFY(invalid2.majorVersion() == 3);
       
   241     QVERIFY(invalid2.minorVersion() == 1);
       
   242     QVERIFY(invalid2.attribute(QServiceInterfaceDescriptor::Location).toString() == QString("myValue"));
       
   243     QVERIFY(invalid2.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList() == (QStringList() << "val1" << "val2"));
       
   244     QVERIFY(invalid2.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString() == QString("This is the service description"));
       
   245     QVERIFY(invalid2.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString() == QString("This is the interface description"));
       
   246     QCOMPARE(invalid2.customAttribute("key1"), QString("value1"));
       
   247     QCOMPARE(invalid2.customAttribute("abcd"), QString("efgh"));
       
   248     QCOMPARE(invalid2.customAttribute("notvalid"), QString());
       
   249     QVERIFY(invalid2.customAttribute("notvalid").isEmpty());
       
   250     QVERIFY(invalid2.customAttribute("notvalid").isNull());
       
   251     QCOMPARE(invalid2.customAttribute("empty"), QString(""));
       
   252     QVERIFY(invalid2.customAttribute("empty").isEmpty());
       
   253     QVERIFY(!invalid2.customAttribute("empty").isNull());
       
   254     QCOMPARE(invalid2.scope(), QService::SystemScope);
       
   255 
       
   256     //stream valid into valid
       
   257     QServiceInterfaceDescriptor valid2;
       
   258     QServiceInterfaceDescriptorPrivate *d2 = new QServiceInterfaceDescriptorPrivate();
       
   259     QServiceInterfaceDescriptorPrivate::setPrivate(&valid2, d2);
       
   260     d2->serviceName = "name2";
       
   261     d2->interfaceName = "interface2";
       
   262     d2->major = 5;
       
   263     d2->minor = 6;
       
   264     d2->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue1"));
       
   265     d2->attributes.insert(QServiceInterfaceDescriptor::Capabilities, QStringList() << "val3" << "val4");
       
   266     d2->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("This is the second service description"));
       
   267     d2->attributes.insert(QServiceInterfaceDescriptor::InterfaceDescription, QString("This is the second interface description"));
       
   268     d2->customAttributes.insert(QString("key1"), QString("value2"));
       
   269     d2->customAttributes.insert(QString("abcd1"), QString("efgh"));
       
   270     d2->customAttributes.insert(QString("empty"), QString(""));
       
   271     d2->scope = QService::UserScope;
       
   272     QVERIFY(valid2.isValid());
       
   273     QCOMPARE(valid2.customAttribute("key1"), QString("value2"));
       
   274     QCOMPARE(valid2.customAttribute("abcd1"), QString("efgh"));
       
   275     QCOMPARE(valid2.customAttribute("abcd"), QString());
       
   276     QVERIFY(valid2.customAttribute("abcd").isEmpty());
       
   277     QVERIFY(valid2.customAttribute("abcd").isNull());
       
   278     QCOMPARE(valid2.customAttribute("empty"), QString(""));
       
   279     QVERIFY(valid2.customAttribute("empty").isEmpty());
       
   280     QVERIFY(!valid2.customAttribute("empty").isNull());
       
   281 
       
   282 
       
   283     QVERIFY(valid2 != valid);
       
   284     QVERIFY(!(valid2 == valid));
       
   285 
       
   286     buffer.seek(0);
       
   287     stream << valid;
       
   288     buffer.seek(0);
       
   289     stream >> valid2;
       
   290     QVERIFY(valid2 == valid);
       
   291     QVERIFY(!(valid2 != valid));
       
   292     QVERIFY(valid2.isValid());
       
   293     QVERIFY(valid2.interfaceName() == QString("interface"));
       
   294     QVERIFY(valid2.serviceName() == QString("name"));
       
   295     QVERIFY(valid2.majorVersion() == 3);
       
   296     QVERIFY(valid2.minorVersion() == 1);
       
   297     QVERIFY(valid2.attribute(QServiceInterfaceDescriptor::Location).toString() == QString("myValue"));
       
   298     QVERIFY(valid2.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList() == (QStringList() << "val1" << "val2"));
       
   299     QVERIFY(valid2.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString() == QString("This is the service description"));
       
   300     QVERIFY(valid2.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString() == QString("This is the interface description"));
       
   301     QCOMPARE(valid2.customAttribute("key1"), QString("value1"));
       
   302     QCOMPARE(valid2.customAttribute("abcd"), QString("efgh"));
       
   303     QCOMPARE(valid2.customAttribute("notvalid"), QString());
       
   304     QVERIFY(valid2.customAttribute("notvalid").isEmpty());
       
   305     QVERIFY(valid2.customAttribute("notvalid").isNull());
       
   306     QCOMPARE(valid2.customAttribute("empty"), QString(""));
       
   307     QVERIFY(valid2.customAttribute("empty").isEmpty());
       
   308     QVERIFY(!valid2.customAttribute("empty").isNull());
       
   309     QCOMPARE(valid2.customAttribute("abcd1"), QString());
       
   310     QCOMPARE(valid2.scope(), QService::SystemScope);
       
   311 }
       
   312 #endif //QT_NO_DATASTREAM
       
   313 
       
   314 #ifndef QT_NO_DEBUG_STREAM
       
   315 static QByteArray msg;
       
   316 static QtMsgType type;
       
   317 
       
   318 static void customMsgHandler(QtMsgType t, const char* m)
       
   319 {
       
   320     msg = m;
       
   321     type = t;
       
   322 
       
   323 }
       
   324 
       
   325 void tst_QServiceInterfaceDescriptor::testDebugStream()
       
   326 {
       
   327     QServiceInterfaceDescriptor valid2;
       
   328     QServiceInterfaceDescriptorPrivate *d2 = new QServiceInterfaceDescriptorPrivate();
       
   329     QServiceInterfaceDescriptorPrivate::setPrivate(&valid2, d2);
       
   330     d2->serviceName = "name2";
       
   331     d2->interfaceName = "interface2";
       
   332     d2->major = 5;
       
   333     d2->minor = 6;
       
   334     d2->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue1"));
       
   335     d2->attributes.insert(QServiceInterfaceDescriptor::Capabilities, QStringList() << "val3" << "val4");
       
   336     d2->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("This is the second service description"));
       
   337     d2->attributes.insert(QServiceInterfaceDescriptor::InterfaceDescription, QString("This is the second interface description"));
       
   338     QVERIFY(valid2.isValid());
       
   339 
       
   340     QServiceInterfaceDescriptor invalid;
       
   341 
       
   342     qInstallMsgHandler(customMsgHandler);
       
   343     qDebug() << valid2 << invalid;
       
   344     QCOMPARE(type, QtDebugMsg);
       
   345     QCOMPARE(QString::fromLatin1(msg.data()),QString::fromLatin1("QServiceInterfaceDescriptor(service=\"name2\", interface=\"interface2 5.6\") QServiceInterfaceDescriptor(invalid) "));
       
   346     qInstallMsgHandler(0);
       
   347 }
       
   348 #endif
       
   349 
       
   350 void tst_QServiceInterfaceDescriptor::destructor()
       
   351 {
       
   352     //test destructor if descriptor is invalid
       
   353     QServiceInterfaceDescriptor* invalid = new QServiceInterfaceDescriptor();
       
   354     delete invalid;
       
   355 
       
   356     //test destructor if descriptor is valid
       
   357     QServiceInterfaceDescriptor* valid = new QServiceInterfaceDescriptor();
       
   358     QServiceInterfaceDescriptorPrivate *d = new QServiceInterfaceDescriptorPrivate();
       
   359     QServiceInterfaceDescriptorPrivate::setPrivate(valid, d);
       
   360     d->serviceName = "name";
       
   361     d->interfaceName = "interface";
       
   362     d->major = 3;
       
   363     d->minor = 1;
       
   364     d->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue"));
       
   365     d->attributes.insert(QServiceInterfaceDescriptor::Capabilities, QStringList() << "val1" << "val2");
       
   366     d->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("This is the service description"));
       
   367     d->attributes.insert(QServiceInterfaceDescriptor::InterfaceDescription, QString("This is the interface description"));
       
   368     d->customAttributes.insert("ckey", "cvalue");
       
   369     QVERIFY(valid->isValid());
       
   370     delete valid;
       
   371 }
       
   372 
       
   373 void tst_QServiceInterfaceDescriptor::cleanupTestCase()
       
   374 {
       
   375 }
       
   376 
       
   377 QTEST_MAIN(tst_QServiceInterfaceDescriptor)
       
   378 #include "tst_qserviceinterfacedescriptor.moc"