tests/auto/servicemetadata/tst_servicemetadata.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 #define private public
       
    44 #include <qserviceinterfacedescriptor.h>
       
    45 #include <qserviceinterfacedescriptor_p.h>
       
    46 #if !defined(Q_CC_MINGW)
       
    47     #include "../../../src/serviceframework/qserviceinterfacedescriptor.cpp"
       
    48 #endif
       
    49 #include "servicemetadata_p.h"
       
    50 #if !defined(Q_CC_MINGW)
       
    51     #include "../../../src/serviceframework/servicemetadata.cpp"
       
    52 #endif
       
    53 
       
    54 #if defined(Q_OS_SYMBIAN)
       
    55 # define TESTDATA_DIR "."
       
    56 #endif
       
    57 
       
    58 QTM_USE_NAMESPACE
       
    59 class ServiceMetadataTest: public QObject
       
    60 {
       
    61     Q_OBJECT
       
    62     
       
    63 private slots:
       
    64     void initTestCase();
       
    65     void parseInvalidServiceXML_oldXml();
       
    66     void parseValidServiceXML();
       
    67     void parseInvalidServiceXML_data();
       
    68     void parseInvalidServiceXML();
       
    69     void noCapability();
       
    70     void checkVersion_data();
       
    71     void checkVersion();
       
    72     void latestInterfaceVersion_data();
       
    73     void latestInterfaceVersion();
       
    74     void cleanupTestCase();
       
    75 
       
    76 private:
       
    77 	QDir dir;
       
    78 
       
    79 };
       
    80 
       
    81 void ServiceMetadataTest::initTestCase()
       
    82 {
       
    83     dir = QDir(TESTDATA_DIR "/testdata");
       
    84 }
       
    85 
       
    86 void ServiceMetadataTest::cleanupTestCase()
       
    87 {
       
    88 }
       
    89 
       
    90 void ServiceMetadataTest::parseInvalidServiceXML_oldXml()
       
    91 {
       
    92     ServiceMetaData parser(dir.absoluteFilePath("ServiceTestOld.xml"));
       
    93     QCOMPARE(parser.extractMetadata(), false);
       
    94 }
       
    95 
       
    96 void ServiceMetadataTest::parseValidServiceXML()
       
    97 {
       
    98     ServiceMetaData parser(dir.absoluteFilePath("ServiceTest.xml"));
       
    99     QCOMPARE(parser.extractMetadata(),true);
       
   100     const ServiceMetaDataResults data = parser.parseResults();
       
   101     QCOMPARE(data.name, QString("TestService"));
       
   102     QCOMPARE(data.description, QString("Test service description"));
       
   103     
       
   104     QCOMPARE(data.location, QString("C:/TestData/testservice.dll"));
       
   105 
       
   106     QList<QServiceInterfaceDescriptor> allInterfaces = data.interfaces;
       
   107     QServiceInterfaceDescriptor aInterface = allInterfaces.at(0);
       
   108     QCOMPARE(aInterface.interfaceName(),QString("com.nokia.qt.tests.IDownloader"));
       
   109     QCOMPARE(aInterface.majorVersion(), 1);
       
   110     QCOMPARE(aInterface.minorVersion(), 4);
       
   111     QVERIFY(aInterface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList().count() == 0);
       
   112     QCOMPARE(aInterface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), QString("Interface that provides download support"));
       
   113         
       
   114     aInterface = allInterfaces.at(1);
       
   115     QCOMPARE(aInterface.interfaceName(),QString("com.nokia.qt.tests.ILocation"));
       
   116     QCOMPARE(aInterface.majorVersion(), 1);
       
   117     QCOMPARE(aInterface.minorVersion(), 4);
       
   118     QVERIFY(aInterface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList().count() == 0);
       
   119     QCOMPARE(aInterface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), QString("Interface that provides location support"));
       
   120     
       
   121     aInterface = allInterfaces.at(2);
       
   122     QCOMPARE(aInterface.interfaceName(),QString("com.nokia.qt.tests.ISysInfo"));
       
   123     QCOMPARE(aInterface.majorVersion(), 2);
       
   124     QCOMPARE(aInterface.minorVersion(), 3);
       
   125     QVERIFY(aInterface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList().count() == 1);
       
   126     QVERIFY(aInterface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList().contains("ReadUserData"));
       
   127     QCOMPARE(aInterface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), QString("Interface that provides system information support"));
       
   128     
       
   129 
       
   130     aInterface = allInterfaces.at(3);
       
   131     QCOMPARE(aInterface.interfaceName(),QString("com.nokia.qt.tests.ISendMessage"));
       
   132     QCOMPARE(aInterface.majorVersion(), 3);
       
   133     QCOMPARE(aInterface.minorVersion(), 0);
       
   134     QStringList capabilities = aInterface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList();
       
   135     QVERIFY(capabilities.count() == 2);
       
   136     QVERIFY(capabilities.contains("ReadUserData"));
       
   137     QVERIFY(capabilities.contains("WriteUserData"));
       
   138     QCOMPARE(aInterface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), QString("Interface that provides message sending support"));
       
   139 
       
   140     aInterface = allInterfaces.at(4);
       
   141     QCOMPARE(aInterface.interfaceName(), QString("com.nokia.qt.tests.IReceiveMessage"));
       
   142     QCOMPARE(aInterface.majorVersion(), 1);
       
   143     QCOMPARE(aInterface.minorVersion(), 1);
       
   144     capabilities = aInterface.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList();
       
   145     QVERIFY(capabilities.count() == 3);
       
   146     QVERIFY(capabilities.contains("ReadUserData"));
       
   147     QVERIFY(capabilities.contains("WriteUserData"));
       
   148     QVERIFY(capabilities.contains("ExecUserData"));
       
   149     QCOMPARE(aInterface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(), QString("Interface that provides message receiving support"));
       
   150     QCOMPARE(aInterface.customAttribute("key1"), QString("value1"));
       
   151     QCOMPARE(aInterface.customAttribute("key2"), QString("value2"));
       
   152     QCOMPARE(aInterface.customAttribute("key3"), QString(""));
       
   153     QCOMPARE(aInterface.customAttribute("key4"), QString());
       
   154     
       
   155     ServiceMetaData parser1(dir.absoluteFilePath("WrongOrder.xml"));
       
   156     QCOMPARE(parser1.extractMetadata(),true);
       
   157     QList<QServiceInterfaceDescriptor> allInterfacesWrongOrder = parser1.parseResults().interfaces;
       
   158     foreach(const QServiceInterfaceDescriptor d, allInterfacesWrongOrder) {
       
   159         QCOMPARE(d.serviceName(), QString("ovi"));
       
   160         QCOMPARE(d.attribute(QServiceInterfaceDescriptor::Location).toString(), QString("C:/Nokia/ovi.dll"));
       
   161         QCOMPARE(d.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("Ovi Services"));
       
   162     }
       
   163 }
       
   164 
       
   165 void ServiceMetadataTest::parseInvalidServiceXML_data()
       
   166 {
       
   167     QTest::addColumn<QString>("fileName");
       
   168     QTest::addColumn<int>("expectedError");
       
   169 
       
   170     QTest::newRow("no such file") << "!#@!CSC" << (int)ServiceMetaData::SFW_ERROR_UNABLE_TO_OPEN_FILE;
       
   171 
       
   172     QTest::newRow("Test1.xml") << "Test1.xml" << (int)ServiceMetaData::SFW_ERROR_INVALID_XML_FILE;
       
   173     QTest::newRow("Test2.xml") << "Test2.xml" << (int)ServiceMetaData::SFW_ERROR_NO_SERVICE;
       
   174     QTest::newRow("Test3.xml") << "Test3.xml" << (int)ServiceMetaData::SFW_ERROR_PARSE_SERVICE;
       
   175     QTest::newRow("Test4.xml") << "Test4.xml" << (int)ServiceMetaData::SFW_ERROR_NO_INTERFACE_VERSION;
       
   176     QTest::newRow("Test5.xml") << "Test5.xml" << (int)ServiceMetaData::SFW_ERROR_NO_SERVICE_FILEPATH;
       
   177     QTest::newRow("Test7.xml") << "Test7.xml" << (int)ServiceMetaData::SFW_ERROR_PARSE_SERVICE;
       
   178     QTest::newRow("Test8.xml") << "Test8.xml" << (int)ServiceMetaData::SFW_ERROR_PARSE_INTERFACE;
       
   179     QTest::newRow("Test9.xml") << "Test9.xml" << (int)ServiceMetaData::SFW_ERROR_PARSE_SERVICE; ///check error code
       
   180     QTest::newRow("Test10.xml") << "Test10.xml" << (int)ServiceMetaData::SFW_ERROR_NO_SERVICE_INTERFACE;
       
   181     QTest::newRow("Test11.xml") << "Test11.xml" << (int)ServiceMetaData::SFW_ERROR_NO_INTERFACE_NAME;
       
   182     QTest::newRow("Test12.xml") << "Test12.xml" << (int)ServiceMetaData::SFW_ERROR_NO_SERVICE_FILEPATH;
       
   183     //QTest::newRow("Test8.xml") << "Test8.xml" << (int)ServiceMetaData::SFW_ERROR_NO_INTERFACE_NAME;
       
   184 
       
   185     QStringList badVersionXml;
       
   186     badVersionXml << "Test14.xml" << "Test15.xml" << "Test17.xml" << "Test18.xml";
       
   187     for (int i=0; i<badVersionXml.count(); i++)
       
   188         QTest::newRow(qPrintable(badVersionXml[i])) << badVersionXml[i] << (int)ServiceMetaData::SFW_ERROR_INVALID_VERSION;
       
   189 
       
   190     QTest::newRow("empty interface name") << "emptyInterfaceName.xml" << (int)ServiceMetaData::SFW_ERROR_NO_INTERFACE_NAME;
       
   191     QTest::newRow("empty service name") << "emptyServiceName.xml" << (int)ServiceMetaData::SFW_ERROR_NO_SERVICE_NAME;
       
   192     QTest::newRow("empty version") << "emptyVersion.xml" << (int)ServiceMetaData::SFW_ERROR_NO_INTERFACE_VERSION;
       
   193     QTest::newRow("duplicated interface A") << "Test13.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_INTERFACE;
       
   194     QTest::newRow("duplicated interface B") << "Test19.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_INTERFACE;
       
   195     QTest::newRow("duplicated service tag") << "DuplicatedServiceTag.xml" << (int)ServiceMetaData::SFW_ERROR_INVALID_XML_FILE;
       
   196     
       
   197     QTest::newRow("Test20.xml") << "Test20.xml" << (int)ServiceMetaData::SFW_ERROR_NO_SERVICE_NAME;
       
   198     QTest::newRow("Test21.xml") << "Test21.xml" << (int)ServiceMetaData::SFW_ERROR_NO_SERVICE_INTERFACE;
       
   199     QTest::newRow("Test22.xml") << "Test22.xml" << (int)ServiceMetaData::SFW_ERROR_NO_INTERFACE_NAME;
       
   200     //duplicated service name tag
       
   201     QTest::newRow("Test23.xml") << "Test23.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_TAG;
       
   202     //duplicated service description tag
       
   203     QTest::newRow("Test24.xml") << "Test24.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_TAG;
       
   204     //duplicated service filepath tag
       
   205     QTest::newRow("Test25.xml") << "Test25.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_TAG;
       
   206     //duplicated interface name tag
       
   207     QTest::newRow("Test26.xml") << "Test26.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_TAG;
       
   208     //duplicated interface version tag
       
   209     QTest::newRow("Test27.xml") << "Test27.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_TAG;
       
   210     //duplicated interface description tag
       
   211     QTest::newRow("Test28.xml") << "Test28.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_TAG;
       
   212     //duplicated interface capabilities tag
       
   213     QTest::newRow("Test29.xml") << "Test29.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_TAG;
       
   214     //missing key attribute for custom property tag
       
   215     QTest::newRow("Test30.xml") << "Test30.xml" << (int)ServiceMetaData::SFW_ERROR_INVALID_CUSTOM_TAG;
       
   216     //empty key attribute for custom property tag
       
   217     QTest::newRow("Test31.xml") << "Test31.xml" << (int)ServiceMetaData::SFW_ERROR_INVALID_CUSTOM_TAG;
       
   218     //same key exists twice
       
   219     QTest::newRow("Test32.xml") << "Test32.xml" << (int)ServiceMetaData::SFW_ERROR_DUPLICATED_CUSTOM_KEY;
       
   220 
       
   221 }
       
   222 
       
   223 void ServiceMetadataTest::parseInvalidServiceXML()
       
   224 {
       
   225     QFETCH(QString, fileName);
       
   226     QFETCH(int, expectedError);
       
   227 
       
   228     ServiceMetaData parser(dir.absoluteFilePath(fileName));
       
   229     QVERIFY(!parser.extractMetadata());
       
   230     QCOMPARE(parser.getLatestError(), expectedError);
       
   231 }
       
   232 
       
   233 void ServiceMetadataTest::noCapability()
       
   234 {
       
   235     ServiceMetaData parser(dir.absoluteFilePath("Test6.xml"));
       
   236     QVERIFY(parser.extractMetadata());
       
   237 }
       
   238 
       
   239 void ServiceMetadataTest::checkVersion_data()
       
   240 {
       
   241     QTest::addColumn<QString>("version");
       
   242     QTest::addColumn<bool>("result");
       
   243     QTest::addColumn<int>("major");
       
   244     QTest::addColumn<int>("minor");
       
   245 
       
   246     QTest::newRow("checkVersion_data():Invalid 1") << "" << false << -1 << -1;
       
   247     QTest::newRow("checkVersion_data():Invalid 2") << "0.3" << false << -1 << -1;
       
   248     QTest::newRow("checkVersion_data():Invalid 3") << "01.3" << false << -1 << -1;
       
   249     QTest::newRow("checkVersion_data():Invalid 4") << "1.03" << false << -1 << -1;
       
   250     QTest::newRow("checkVersion_data():Invalid 5") << "x.y" << false << -1 << -1;
       
   251     QTest::newRow("checkVersion_data():Invalid 6") << "23" << false << -1 << -1;
       
   252     QTest::newRow("checkVersion_data():Invalid 7") << "sdfsfs" << false << -1 << -1;
       
   253     QTest::newRow("checkVersion_data():Invalid 8") << "%#5346" << false << -1 << -1;
       
   254     QTest::newRow("checkVersion_data():Invalid 9") << ".66" << false << -1 << -1;
       
   255     QTest::newRow("checkVersion_data():Invalid 10") << "1.3.4" << false << -1 << -1;
       
   256     QTest::newRow("checkVersion_data():Invalid 11") << "1.a" << false << -1 << -1;
       
   257     QTest::newRow("checkVersion_data():Invalid 12") << "b.1" << false << -1 << -1;
       
   258     QTest::newRow("checkVersion_data():Invalid 13") << "3." << false << -1 << -1;
       
   259     QTest::newRow("checkVersion_data():Invalid 14") << "-1" << false << -1 << -1;
       
   260     QTest::newRow("checkVersion_data():Invalid 15") << "0.0" << false << -1 << -1;
       
   261     QTest::newRow("checkVersion_data():Invalid 16") << ".x" << false << -1 << -1;
       
   262     QTest::newRow("checkVersion_data():Invalid 17") << "x." << false << -1 << -1;
       
   263     QTest::newRow("checkVersion_data():Invalid 18") << "1. 0" << false << -1 << -1;
       
   264     QTest::newRow("checkVersion_data():Invalid 19") << "1  .0" << false << -1 << -1;
       
   265     QTest::newRow("checkVersion_data():Invalid 20") << "1  0" << false << -1 << -1;
       
   266     QTest::newRow("checkVersion_data():Invalid 21") << "1 . 0" << false << -1 << -1;
       
   267     QTest::newRow("checkVersion_data():Invalid 22") << " 1.5" << false << -1 << -1;
       
   268     QTest::newRow("checkVersion_data():Invalid 23") << "1.5 " << false << -1 << -1;
       
   269     QTest::newRow("checkVersion_data():Invalid 24") << " 1.5 " << false << -1 << -1;
       
   270     QTest::newRow("checkVersion_data():Invalid 25") << "1.5 1.6" << false << -1 << -1;
       
   271     QTest::newRow("checkVersion_data():Invalid 26") << "-1.0" << false << -1 << -1;
       
   272     QTest::newRow("checkVersion_data():Invalid 27") << "1.-1" << false << -1 << -1;
       
   273     QTest::newRow("checkVersion_data():Invalid 28") << "-5.-1" << false << -1 << -1;
       
   274     QTest::newRow("checkVersion_data():Invalid 29") << "4,8" << false << -1 << -1;
       
   275     QTest::newRow("checkVersion_data():Invalid 30") << "  " << false << -1 << -1;
       
   276     QTest::newRow("checkVersion_data():Invalid 31") << "1.9b" << false << -1 << -1;
       
   277 
       
   278     QTest::newRow("checkVersion_data():Valid 1") << "1.0" << true << 1 << 0;
       
   279     QTest::newRow("checkVersion_data():Valid 2") << "1.00" << true << 1 << 0;
       
   280     QTest::newRow("checkVersion_data():Valid 3") << "99.99" << true << 99 << 99;
       
   281     QTest::newRow("checkVersion_data():Valid 4") << "2.3" << true << 2 << 3;
       
   282     QTest::newRow("checkVersion_data():Valid 5") << "10.3" << true << 10 << 3;
       
   283     QTest::newRow("checkVersion_data():Valid 6") << "5.10" << true << 5 << 10;
       
   284     QTest::newRow("checkVersion_data():Valid 7") << "10.10" << true << 10 << 10;
       
   285 }
       
   286 
       
   287 void ServiceMetadataTest::checkVersion()
       
   288 {
       
   289     QFETCH(QString, version);
       
   290     QFETCH(bool, result);
       
   291     QFETCH(int, major);
       
   292     QFETCH(int, minor);
       
   293 
       
   294     ServiceMetaData parser(dir.absoluteFilePath("ServiceTest.xml"));
       
   295     QCOMPARE(parser.checkVersion(version), result);
       
   296     int majorVer;
       
   297     int minorVer;
       
   298     parser.transformVersion(version, &majorVer, &minorVer);
       
   299     QCOMPARE(majorVer, major);
       
   300     QCOMPARE(minorVer, minor);
       
   301 }
       
   302 
       
   303 void ServiceMetadataTest::latestInterfaceVersion_data()
       
   304 {
       
   305     QTest::addColumn<QString>("fileName");
       
   306     QTest::addColumn<int>("major");
       
   307     QTest::addColumn<int>("minor");
       
   308 
       
   309     //cases 1-3 contains services with more than one interface
       
   310     QTest::newRow("latestVersion_data() 1") << "latestVersion.xml" << 2 << 1; //latest version in middle
       
   311     QTest::newRow("latestVersion_data() 2") << "latestVersion2.xml" << 3 << 0; //latest version at end
       
   312     QTest::newRow("latestVersion_data() 3") << "latestVersion3.xml" << 5 << 7; //latest version at start
       
   313     QTest::newRow("latestVersion_data() 4") << "latestVersion4.xml" << 1 << 7; //only one version
       
   314 
       
   315 }
       
   316 
       
   317 void ServiceMetadataTest::latestInterfaceVersion()
       
   318 {
       
   319     QFETCH(QString, fileName);
       
   320     QFETCH(int, major);
       
   321     QFETCH(int, minor);
       
   322 
       
   323     ServiceMetaData parser(dir.absoluteFilePath(fileName));
       
   324     QCOMPARE(parser.extractMetadata(), true);
       
   325     QServiceInterfaceDescriptor interface = parser.latestInterfaceVersion("com.nokia.service.contacts");
       
   326     QCOMPARE(interface.majorVersion(), major);
       
   327     QCOMPARE(interface.minorVersion(), minor);
       
   328     QCOMPARE(interface.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString(),
       
   329             QString("Contacts management service")); //make sure we're getting the right interface
       
   330 }
       
   331 
       
   332 QTEST_MAIN(ServiceMetadataTest)
       
   333 #include "tst_servicemetadata.moc"