| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | //TESTED_COMPONENT=src/serviceframework |
| 30 | |
| 31 | #include <qservicemanager.h> |
| 32 | #include <private/qserviceinterfacedescriptor_p.h> |
| 33 | #include "sampleservice/sampleserviceplugin.h" |
| 34 | #include "../qsfwtestutil.h" |
| 35 | |
| 36 | #include <QtTest/QtTest> |
| 37 | #include <QtCore> |
| 38 | #include <QSettings> |
| 39 | #include <QFileInfo> |
| 40 | #include <QDir> |
| 41 | #include <QPair> |
| 42 | |
| 43 | #define PRINT_ERR(a) qPrintable(QString("error = %1").arg(a.error())) |
| 44 | |
| 45 | typedef QList<QServiceInterfaceDescriptor> ServiceInterfaceDescriptorList; |
| 46 | Q_DECLARE_METATYPE(QServiceFilter) |
| 47 | Q_DECLARE_METATYPE(QServiceInterfaceDescriptor) |
| 48 | Q_DECLARE_METATYPE(ServiceInterfaceDescriptorList) |
| 49 | |
| 50 | Q_DECLARE_METATYPE(QSet<QString>) |
| 51 | Q_DECLARE_METATYPE(QList<QByteArray>) |
| 52 | Q_DECLARE_METATYPE(QService::Scope) |
| 53 | |
| 54 | QT_BEGIN_NAMESPACE |
| 55 | typedef QHash<QServiceInterfaceDescriptor::Attribute, QVariant> DescriptorAttributes; |
| 56 | QT_END_NAMESPACE |
| 57 | |
| 58 | QT_USE_NAMESPACE |
| 59 | static DescriptorAttributes defaultDescriptorAttributes() |
| 60 | { |
| 61 | DescriptorAttributes props; |
| 62 | //props[QServiceInterfaceDescriptor::Capabilities] = QStringList(); |
| 63 | props[QServiceInterfaceDescriptor::Location] = ""; |
| 64 | props[QServiceInterfaceDescriptor::ServiceDescription] = ""; |
| 65 | props[QServiceInterfaceDescriptor::InterfaceDescription] = ""; |
| 66 | props[QServiceInterfaceDescriptor::ServiceType] = QService::Plugin; |
| 67 | return props; |
| 68 | } |
| 69 | static const DescriptorAttributes DEFAULT_DESCRIPTOR_PROPERTIES = defaultDescriptorAttributes(); |
| 70 | |
| 71 | // Helper function for debugging. Useful e.g. for checking what is difference between |
| 72 | // two descriptors (in addition to attributes printed below, the |
| 73 | // QServiceInterfaceDescriptor::== operator also compares |
| 74 | // attributes. |
| 75 | /*static void printDescriptor (const QServiceInterfaceDescriptor &desc) { |
| 76 | qDebug("***QServiceInterfaceDescriptor printed:"); |
| 77 | qDebug() << "***majorVersion:" << desc.majorVersion(); |
| 78 | qDebug() << "***minorVersion:" << desc.minorVersion(); |
| 79 | qDebug() << "***interfaceName:" << desc.interfaceName(); |
| 80 | qDebug() << "***serviceName:" << desc.serviceName(); |
| 81 | qDebug() << "***customAttributes:" << desc.customAttributes(); |
| 82 | qDebug() << "***attributes:" << desc.attribute(QServiceInterfaceDescriptor::Capabilities) << |
| 83 | desc.attribute(QServiceInterfaceDescriptor::Location) << |
| 84 | desc.attribute(QServiceInterfaceDescriptor::ServiceDescription) << |
| 85 | desc.attribute(QServiceInterfaceDescriptor::InterfaceDescription) << |
| 86 | desc.attribute(QServiceInterfaceDescriptor::ServiceType); |
| 87 | qDebug() << "***isValid(): " << desc.isValid(); |
| 88 | qDebug() << "***scope (user:0, system:1): " << desc.scope(); |
| 89 | }*/ |
| 90 | |
| 91 | class ServicesListener : public QObject |
| 92 | { |
| 93 | Q_OBJECT |
| 94 | public slots: |
| 95 | void serviceAdded(const QString &name , QService::Scope scope) { |
| 96 | params.append(qMakePair(name, scope)); |
| 97 | } |
| 98 | void serviceRemoved(const QString &name, QService::Scope scope) { |
| 99 | params.append(qMakePair(name, scope)); |
| 100 | } |
| 101 | public: |
| 102 | QList<QPair<QString, QService::Scope> > params; |
| 103 | }; |
| 104 | |
| 105 | |
| 106 | class tst_QServiceManager: public QObject |
| 107 | { |
| 108 | Q_OBJECT |
| 109 | |
| 110 | private: |
| 111 | inline QString xmlTestDataPath(const QString &xmlFileName) const |
| 112 | { return m_xmlDirectory + QLatin1Char('/') + xmlFileName; } |
| 113 | |
| 114 | QByteArray createServiceXml(const QString &serviceName, const QByteArray &interfaceXml, const QString &path, const QString &description = QString()) const |
| 115 | { |
| 116 | QString xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"; |
| 117 | xml += "<service>\n"; |
| 118 | xml += "<name>" + serviceName + "</name>\n"; |
| 119 | xml += "<filepath>" + path + "</filepath>\n"; |
| 120 | xml += "<description>" + description + "</description>\n"; |
| 121 | xml += interfaceXml; |
| 122 | xml += "</service>\n"; |
| 123 | return xml.toLatin1(); |
| 124 | } |
| 125 | |
| 126 | QByteArray createServiceXml(const QString &serviceName, const QList<QServiceInterfaceDescriptor> &descriptors) const |
| 127 | { |
| 128 | Q_ASSERT(descriptors.count() > 0); |
| 129 | return createServiceXml(serviceName, createInterfaceXml(descriptors), |
| 130 | descriptors[0].attribute(QServiceInterfaceDescriptor::Location).toString(), |
| 131 | descriptors[0].attribute(QServiceInterfaceDescriptor::ServiceDescription).toString()); |
| 132 | } |
| 133 | |
| 134 | QByteArray createInterfaceXml(const QList<QServiceInterfaceDescriptor> &descriptors) const |
| 135 | { |
| 136 | QByteArray interfacesXml; |
| 137 | foreach (const QServiceInterfaceDescriptor &desc, descriptors) { |
| 138 | QString version = QString("%1.%2").arg(desc.majorVersion()).arg(desc.minorVersion()); |
| 139 | interfacesXml += createInterfaceXml(desc.interfaceName(), version, |
| 140 | desc.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString()); |
| 141 | } |
| 142 | return interfacesXml; |
| 143 | } |
| 144 | |
| 145 | QByteArray createInterfaceXml(const QString &name, const QString &version = "1.0", const QString &description = QString()) const |
| 146 | { |
| 147 | QString xml = "<interface>\n"; |
| 148 | xml += "<name>" + name + "</name>\n"; |
| 149 | xml += "<version>" + version + "</version>\n"; |
| 150 | xml += " <description>" + description + "</description>\n"; |
| 151 | xml += "</interface>\n"; |
| 152 | return xml.toLatin1(); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | QServiceInterfaceDescriptor createDescriptor(const QString &interfaceName, int major, int minor, const QString &serviceName, const DescriptorAttributes &attributes = DescriptorAttributes(), QService::Scope scope = QService::UserScope) const |
| 157 | { |
| 158 | QString version = QString("%1.%2").arg(major).arg(minor); |
| 159 | |
| 160 | QServiceInterfaceDescriptorPrivate *priv = new QServiceInterfaceDescriptorPrivate; |
| 161 | priv->serviceName = serviceName; |
| 162 | priv->interfaceName = interfaceName; |
| 163 | priv->major = major; |
| 164 | priv->minor = minor; |
| 165 | priv->scope = scope; |
| 166 | |
| 167 | priv->attributes = attributes; |
| 168 | foreach (QServiceInterfaceDescriptor::Attribute key, DEFAULT_DESCRIPTOR_PROPERTIES.keys()) { |
| 169 | if (!priv->attributes.contains(key)) |
| 170 | priv->attributes[key] = DEFAULT_DESCRIPTOR_PROPERTIES[key]; |
| 171 | } |
| 172 | |
| 173 | QServiceInterfaceDescriptor desc; |
| 174 | QServiceInterfaceDescriptorPrivate::setPrivate(&desc, priv); |
| 175 | return desc; |
| 176 | } |
| 177 | |
| 178 | void deleteTestDatabasesAndWaitUntilDone() |
| 179 | { |
| 180 | QSfwTestUtil::removeTempUserDb(); |
| 181 | QSfwTestUtil::removeTempSystemDb(); |
| 182 | |
| 183 | QTRY_VERIFY(!QFile::exists(QSfwTestUtil::tempUserDbDir())); |
| 184 | QTRY_VERIFY(!QFile::exists(QSfwTestUtil::tempSystemDbDir())); |
| 185 | } |
| 186 | |
| 187 | private slots: |
| 188 | void initTestCase(); |
| 189 | void cleanupTestCase(); |
| 190 | void init(); |
| 191 | |
| 192 | void constructor(); |
| 193 | void constructor_scope(); |
| 194 | void constructor_scope_data(); |
| 195 | |
| 196 | void findServices(); |
| 197 | void findServices_data(); |
| 198 | |
| 199 | void findServices_scope(); |
| 200 | void findServices_scope_data(); |
| 201 | |
| 202 | void findInterfaces_filter(); |
| 203 | void findInterfaces_filter_data(); |
| 204 | |
| 205 | void findInterfaces_scope(); |
| 206 | void findInterfaces_scope_data(); |
| 207 | |
| 208 | void loadInterface_string(); |
| 209 | |
| 210 | void loadInterface_descriptor(); |
| 211 | void loadInterface_descriptor_data(); |
| 212 | |
| 213 | void loadInterface_testLoadedObjectAttributes(); |
| 214 | |
| 215 | void loadLocalTypedInterface(); |
| 216 | |
| 217 | void addService(); |
| 218 | void addService_data(); |
| 219 | |
| 220 | void addService_testInvalidServiceXml(); |
| 221 | void addService_testPluginLoading(); |
| 222 | void addService_testPluginLoading_data(); |
| 223 | void addService_testInstallService(); |
| 224 | |
| 225 | void removeService(); |
| 226 | |
| 227 | void setInterfaceDefault_strings(); |
| 228 | void setInterfaceDefault_strings_multipleInterfaces(); |
| 229 | |
| 230 | void setInterfaceDefault_descriptor(); |
| 231 | void setInterfaceDefault_descriptor_data(); |
| 232 | |
| 233 | void interfaceDefault(); |
| 234 | |
| 235 | void serviceAdded(); |
| 236 | void serviceAdded_data(); |
| 237 | |
| 238 | void serviceRemoved(); |
| 239 | void serviceRemoved_data(); |
| 240 | |
| 241 | private: |
| 242 | QString m_xmlDirectory; |
| 243 | QString m_pluginsDirectory; |
| 244 | QStringList m_validPluginNames; |
| 245 | QStringList m_validPluginPaths; |
| 246 | }; |
| 247 | |
| 248 | static const char *plugins[] = |
| 249 | {"tst_sfw_sampleserviceplugin", "tst_sfw_sampleserviceplugin2", "tst_sfw_testservice2plugin"}; |
| 250 | |
| 251 | static const char *expectedPluginClassNames[] = |
| 252 | {"SampleServicePluginClass", 0, "TestService"}; |
| 253 | |
| 254 | static inline QByteArray msgCannotLoadLibrary(const QString &name, const QString &error) |
| 255 | { |
| 256 | QByteArray result = "Unable to load '"; |
| 257 | result += name.toLocal8Bit(); |
| 258 | result += "': "; |
| 259 | result += error.toLocal8Bit(); |
| 260 | return result; |
| 261 | } |
| 262 | |
| 263 | void tst_QServiceManager::initTestCase() |
| 264 | { |
| 265 | qRegisterMetaType<QService::Scope>("QService::Scope"); |
| 266 | |
| 267 | QSfwTestUtil::setupTempUserDb(); |
| 268 | QSfwTestUtil::setupTempSystemDb(); |
| 269 | |
| 270 | m_xmlDirectory = QFINDTESTDATA("xml"); |
| 271 | QVERIFY2(!m_xmlDirectory.isEmpty(), "Unable to locate XML test data"); |
| 272 | m_pluginsDirectory = QFINDTESTDATA("plugins"); |
| 273 | QVERIFY2(!m_pluginsDirectory.isEmpty(), "Unable to locate plugins"); |
| 274 | |
| 275 | // Example XML files specify plugins as 'plugins/foo', so, add parent directory. |
| 276 | const QFileInfo pluginsDirectory(m_pluginsDirectory); |
| 277 | QCoreApplication::addLibraryPath(pluginsDirectory.absolutePath()); |
| 278 | QCoreApplication::addLibraryPath(pluginsDirectory.absoluteFilePath()); |
| 279 | |
| 280 | // Determine plugin and let QLibrary resolve the full path of the libraries |
| 281 | // '/path/foo' -> '/path/libfoo.so' or '/path/foo.dll' |
| 282 | const size_t pluginCount = sizeof(plugins) / sizeof(const char *); |
| 283 | for (size_t i = 0; i < pluginCount; ++i) { |
| 284 | const QString pluginName = QLatin1String(plugins[i]); |
| 285 | const QString pluginPathIn = m_pluginsDirectory + QLatin1Char('/') + pluginName; |
| 286 | QLibrary lib(pluginPathIn); |
| 287 | QVERIFY2(lib.load(), msgCannotLoadLibrary(pluginPathIn, lib.errorString()).constData()); |
| 288 | lib.unload(); |
| 289 | m_validPluginNames << pluginName; |
| 290 | m_validPluginPaths << lib.fileName(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void tst_QServiceManager::init() |
| 295 | { |
| 296 | QSfwTestUtil::removeTempUserDb(); |
| 297 | QSfwTestUtil::removeTempSystemDb(); |
| 298 | QSettings settings("com.nokia.qt.serviceframework.tests", "SampleServicePlugin"); |
| 299 | settings.setValue("installed", false); |
| 300 | } |
| 301 | |
| 302 | void tst_QServiceManager::cleanupTestCase() |
| 303 | { |
| 304 | QSfwTestUtil::removeTempUserDb(); |
| 305 | QSfwTestUtil::removeTempSystemDb(); |
| 306 | //process deferred delete events |
| 307 | //QServiceManager::loadInterface makes use of deleteLater() when |
| 308 | //cleaning up service objects and their respective QPluginLoader |
| 309 | //we want to force the testcase to run the cleanup code |
| 310 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 311 | QCoreApplication::processEvents(); |
| 312 | } |
| 313 | |
| 314 | void tst_QServiceManager::constructor() |
| 315 | { |
| 316 | QObject o; |
| 317 | QServiceManager mgr(&o); |
| 318 | QCOMPARE(mgr.scope(), QService::UserScope); |
| 319 | QCOMPARE(mgr.parent(), &o); |
| 320 | } |
| 321 | |
| 322 | void tst_QServiceManager::constructor_scope() |
| 323 | { |
| 324 | QFETCH(QService::Scope, scope); |
| 325 | |
| 326 | QObject o; |
| 327 | QServiceManager mgr(scope, &o); |
| 328 | QCOMPARE(mgr.scope(), scope); |
| 329 | QCOMPARE(mgr.parent(), &o); |
| 330 | } |
| 331 | |
| 332 | void tst_QServiceManager::constructor_scope_data() |
| 333 | { |
| 334 | QTest::addColumn<QService::Scope>("scope"); |
| 335 | |
| 336 | QTest::newRow("user") << QService::UserScope; |
| 337 | QTest::newRow("system") << QService::SystemScope; |
| 338 | } |
| 339 | |
| 340 | void tst_QServiceManager::findServices() |
| 341 | { |
| 342 | QFETCH(QList<QByteArray>, xmlBlocks); |
| 343 | QFETCH(QStringList, interfaceNames); |
| 344 | QFETCH(QSet<QString>, searchByInterfaceResult); |
| 345 | QFETCH(QSet<QString>, searchAllResult); |
| 346 | |
| 347 | QServiceManager mgr; |
| 348 | QServiceFilter wildcardFilter; |
| 349 | |
| 350 | // Check that nothing is found neither with default search or interface-search |
| 351 | QVERIFY(mgr.findServices().isEmpty()); |
| 352 | foreach (const QString &serviceInterface, interfaceNames) |
| 353 | QVERIFY(mgr.findServices(serviceInterface).isEmpty()); |
| 354 | QCOMPARE(mgr.findInterfaces(wildcardFilter).count(), 0); |
| 355 | |
| 356 | // Add all services from the xmlBlocks list |
| 357 | foreach (const QByteArray &xml, xmlBlocks) { |
| 358 | QBuffer buffer; |
| 359 | buffer.setData(xml); |
| 360 | QVERIFY2(mgr.addService(&buffer), PRINT_ERR(mgr)); |
| 361 | } |
| 362 | // Check that all services are found with default search |
| 363 | QCOMPARE(mgr.findServices().toSet(), searchAllResult); |
| 364 | // Check that all services are found based on interface search |
| 365 | foreach (const QString &serviceInterface, interfaceNames) |
| 366 | QCOMPARE(mgr.findServices(serviceInterface).toSet(), searchByInterfaceResult); |
| 367 | |
| 368 | // Check that nothing is found with empty interface |
| 369 | QCOMPARE(mgr.findServices("com.invalid.interface") , QStringList()); |
| 370 | } |
| 371 | |
| 372 | void tst_QServiceManager::findServices_data() |
| 373 | { |
| 374 | QTest::addColumn< QList<QByteArray> >("xmlBlocks"); |
| 375 | QTest::addColumn<QStringList>("interfaceNames"); |
| 376 | QTest::addColumn< QSet<QString> >("searchByInterfaceResult"); |
| 377 | QTest::addColumn< QSet<QString> >("searchAllResult"); |
| 378 | |
| 379 | QStringList interfaces; |
| 380 | interfaces << "com.nokia.qt.TestInterfaceA"; |
| 381 | interfaces << "com.nokia.qt.TestInterfaceB"; |
| 382 | QByteArray interfacesXml; |
| 383 | for (int i=0; i<interfaces.count(); i++) |
| 384 | interfacesXml += "\n" + createInterfaceXml(interfaces[i]); |
| 385 | |
| 386 | QTest::newRow("one service") |
| 387 | << (QList<QByteArray>() << createServiceXml("SomeTestService", interfacesXml, m_validPluginNames.first())) |
| 388 | << interfaces |
| 389 | << (QSet<QString>() << "SomeTestService") |
| 390 | << (QSet<QString>() << "SomeTestService"); |
| 391 | |
| 392 | QTest::newRow("multiple services with same interfaces") |
| 393 | << (QList<QByteArray>() << createServiceXml("SomeTestService", interfacesXml, m_validPluginNames[0]) |
| 394 | << createServiceXml("SomeSimilarTestService", interfacesXml, m_validPluginNames[1])) |
| 395 | << interfaces |
| 396 | << (QSet<QString>() << "SomeTestService" << "SomeSimilarTestService") |
| 397 | << (QSet<QString>() << "SomeTestService" << "SomeSimilarTestService"); |
| 398 | |
| 399 | QStringList interfaces2; |
| 400 | interfaces2 << "com.nokia.qt.TestInterfaceY"; |
| 401 | interfaces2 << "com.nokia.qt.TestInterfaceZ"; |
| 402 | QByteArray interfacesXml2; |
| 403 | for (int i=0; i<interfaces2.count(); i++) |
| 404 | interfacesXml2 += "\n" + createInterfaceXml(interfaces2[i]); |
| 405 | QTest::newRow("multiple services with different interfaces") |
| 406 | << (QList<QByteArray>() << createServiceXml("SomeTestService", interfacesXml, m_validPluginNames[0]) |
| 407 | << createServiceXml("TestServiceWithOtherInterfaces", interfacesXml2, m_validPluginNames[1])) |
| 408 | << interfaces2 |
| 409 | << (QSet<QString>() << "TestServiceWithOtherInterfaces") |
| 410 | << (QSet<QString>() << "SomeTestService" << "TestServiceWithOtherInterfaces"); |
| 411 | } |
| 412 | |
| 413 | void tst_QServiceManager::findServices_scope() |
| 414 | { |
| 415 | QFETCH(QService::Scope, scope_add); |
| 416 | QFETCH(QService::Scope, scope_find); |
| 417 | QFETCH(bool, expectFound); |
| 418 | |
| 419 | QByteArray xml = createServiceXml("SomeTestService", |
| 420 | createInterfaceXml("com.nokia.qt.TestInterface"), m_validPluginNames[0]); |
| 421 | QBuffer buffer(&xml); |
| 422 | |
| 423 | QServiceManager mgrUser(QService::UserScope); |
| 424 | QServiceManager mgrSystem(QService::SystemScope); |
| 425 | |
| 426 | QServiceManager &mgrAdd = scope_add == QService::UserScope ? mgrUser : mgrSystem; |
| 427 | QServiceManager &mgrFind = scope_find == QService::UserScope ? mgrUser : mgrSystem; |
| 428 | |
| 429 | QVERIFY2(mgrAdd.addService(&buffer), PRINT_ERR(mgrAdd)); |
| 430 | QStringList result = mgrFind.findServices(); |
| 431 | QCOMPARE(!result.isEmpty(), expectFound); |
| 432 | } |
| 433 | |
| 434 | void tst_QServiceManager::findServices_scope_data() |
| 435 | { |
| 436 | QTest::addColumn<QService::Scope>("scope_add"); |
| 437 | QTest::addColumn<QService::Scope>("scope_find"); |
| 438 | QTest::addColumn<bool>("expectFound"); |
| 439 | |
| 440 | QTest::newRow("user scope") |
| 441 | << QService::UserScope << QService::UserScope << true; |
| 442 | QTest::newRow("system scope") |
| 443 | << QService::SystemScope << QService::SystemScope << true; |
| 444 | |
| 445 | QTest::newRow("user scope - add, system scope - find") |
| 446 | << QService::UserScope << QService::SystemScope << false; |
| 447 | QTest::newRow("system scope - add, user scope - find") |
| 448 | << QService::SystemScope << QService::UserScope << true; |
| 449 | } |
| 450 | |
| 451 | void tst_QServiceManager::findInterfaces_filter() |
| 452 | { |
| 453 | QFETCH(QByteArray, xml); |
| 454 | QFETCH(QServiceFilter, filter); |
| 455 | QFETCH(QList<QServiceInterfaceDescriptor>, expectedInterfaces); |
| 456 | |
| 457 | QServiceManager mgr; |
| 458 | |
| 459 | QBuffer buffer(&xml); |
| 460 | QVERIFY2(mgr.addService(&buffer), PRINT_ERR(mgr)); |
| 461 | |
| 462 | QList<QServiceInterfaceDescriptor> result = mgr.findInterfaces(filter); |
| 463 | |
| 464 | qDebug() << "Wanted" << expectedInterfaces; |
| 465 | |
| 466 | qDebug() << "Got" << result; |
| 467 | |
| 468 | QCOMPARE(result.toSet(), expectedInterfaces.toSet()); |
| 469 | } |
| 470 | |
| 471 | void tst_QServiceManager::findInterfaces_filter_data() |
| 472 | { |
| 473 | QTest::addColumn<QByteArray>("xml"); |
| 474 | QTest::addColumn<QServiceFilter>("filter"); |
| 475 | QTest::addColumn<ServiceInterfaceDescriptorList>("expectedInterfaces"); |
| 476 | |
| 477 | QString serviceName = "SomeTestService"; |
| 478 | DescriptorAttributes attributes; |
| 479 | attributes[QServiceInterfaceDescriptor::Location] = m_validPluginNames.first(); |
| 480 | //attributes[QServiceInterfaceDescriptor::ServiceType] = QService::Plugin; |
| 481 | |
| 482 | QList<QServiceInterfaceDescriptor> descriptors; |
| 483 | descriptors << createDescriptor("com.nokia.qt.TestInterfaceA", 1, 0, serviceName, attributes); |
| 484 | descriptors << createDescriptor("com.nokia.qt.TestInterfaceB", 1, 0, serviceName, attributes); |
| 485 | descriptors << createDescriptor("com.nokia.qt.TestInterfaceB", 2, 0, serviceName, attributes); |
| 486 | descriptors << createDescriptor("com.nokia.qt.TestInterfaceB", 2, 3, serviceName, attributes); |
| 487 | |
| 488 | QByteArray serviceXml = createServiceXml(serviceName, descriptors); |
| 489 | QServiceFilter filter; |
| 490 | |
| 491 | QTest::newRow("empty/wildcard filter") |
| 492 | << serviceXml |
| 493 | << QServiceFilter() |
| 494 | << descriptors; |
| 495 | |
| 496 | filter = QServiceFilter(); |
| 497 | filter.setInterface("com.nokia.qt.TestInterfaceA"); |
| 498 | QTest::newRow("by interface name (A)") |
| 499 | << serviceXml |
| 500 | << filter |
| 501 | << descriptors.mid(0, 1); |
| 502 | |
| 503 | filter = QServiceFilter(); |
| 504 | filter.setInterface("com.nokia.qt.TestInterfaceB"); |
| 505 | QTest::newRow("by interface name (B)") |
| 506 | << serviceXml |
| 507 | << filter |
| 508 | << descriptors.mid(1); |
| 509 | |
| 510 | filter = QServiceFilter(); |
| 511 | filter.setServiceName(serviceName); |
| 512 | QTest::newRow("by service name, should find all") |
| 513 | << serviceXml |
| 514 | << filter |
| 515 | << descriptors; |
| 516 | |
| 517 | filter = QServiceFilter(); |
| 518 | filter.setInterface("com.invalid.interface"); |
| 519 | QTest::newRow("by non-existing interface name") |
| 520 | << serviceXml |
| 521 | << filter |
| 522 | << ServiceInterfaceDescriptorList(); |
| 523 | |
| 524 | filter = QServiceFilter(); |
| 525 | filter.setServiceName("InvalidServiceName"); |
| 526 | QTest::newRow("by non-existing service name") |
| 527 | << serviceXml |
| 528 | << filter |
| 529 | << ServiceInterfaceDescriptorList(); |
| 530 | |
| 531 | //version lookup testing for existing interface |
| 532 | //valid from first version onwards |
| 533 | filter = QServiceFilter(); |
| 534 | filter.setInterface("com.nokia.qt.TestInterfaceB", "1.0"); |
| 535 | QTest::newRow("by version name 1.0 DefaultMatch, should find all B interfaces") |
| 536 | << serviceXml |
| 537 | << filter |
| 538 | << descriptors.mid(1); |
| 539 | |
| 540 | filter = QServiceFilter(); |
| 541 | filter.setInterface("com.nokia.qt.TestInterfaceB", "1.0", QServiceFilter::MinimumVersionMatch); |
| 542 | QTest::newRow("by version name 1.0 MinimumMatch, should find all B interfaces") |
| 543 | << serviceXml |
| 544 | << filter |
| 545 | << descriptors.mid(1); |
| 546 | |
| 547 | filter = QServiceFilter(); |
| 548 | filter.setInterface("com.nokia.qt.TestInterfaceB", "1.0", QServiceFilter::ExactVersionMatch); |
| 549 | QTest::newRow("by version name 1.0 ExactMatch, find B 1.0 only") |
| 550 | << serviceXml |
| 551 | << filter |
| 552 | << descriptors.mid(1, 1); |
| 553 | |
| 554 | //valid with exact version match |
| 555 | filter = QServiceFilter(); |
| 556 | filter.setInterface("com.nokia.qt.TestInterfaceB", "2.0"); |
| 557 | QTest::newRow("by version name 2.0 DefaultMatch, find B 2.0+") |
| 558 | << serviceXml |
| 559 | << filter |
| 560 | << descriptors.mid(2); |
| 561 | |
| 562 | filter = QServiceFilter(); |
| 563 | filter.setInterface("com.nokia.qt.TestInterfaceB", "2.0", QServiceFilter::MinimumVersionMatch); |
| 564 | QTest::newRow("by version name 2.0 MinimumMatch, find B 2.0+") |
| 565 | << serviceXml |
| 566 | << filter |
| 567 | << descriptors.mid(2); |
| 568 | |
| 569 | filter = QServiceFilter(); |
| 570 | filter.setInterface("com.nokia.qt.TestInterfaceB", "2.0", QServiceFilter::ExactVersionMatch); |
| 571 | QTest::newRow("by version name 2.0 ExactMatch, find B 2.0") |
| 572 | << serviceXml |
| 573 | << filter |
| 574 | << descriptors.mid(2, 1); |
| 575 | |
| 576 | //valid but not exact version match |
| 577 | filter = QServiceFilter(); |
| 578 | filter.setInterface("com.nokia.qt.TestInterfaceB", "1.9"); |
| 579 | QTest::newRow("by version name 1.9 DefaultMatch, find B 1.9+") |
| 580 | << serviceXml |
| 581 | << filter |
| 582 | << descriptors.mid(2); |
| 583 | |
| 584 | filter = QServiceFilter(); |
| 585 | filter.setInterface("com.nokia.qt.TestInterfaceB", "1.9", QServiceFilter::MinimumVersionMatch); |
| 586 | QTest::newRow("by version name 1.9 MinimumMatch, find B 1.9+") |
| 587 | << serviceXml |
| 588 | << filter |
| 589 | << descriptors.mid(2); |
| 590 | |
| 591 | filter = QServiceFilter(); |
| 592 | filter.setInterface("com.nokia.qt.TestInterfaceB", "1.9", QServiceFilter::ExactVersionMatch); |
| 593 | QTest::newRow("by version name 1.9 ExactMatch") |
| 594 | << serviceXml |
| 595 | << filter |
| 596 | << ServiceInterfaceDescriptorList(); |
| 597 | |
| 598 | //version doesn't exist yet |
| 599 | filter = QServiceFilter(); |
| 600 | filter.setInterface("com.nokia.qt.TestInterfaceB", "3.9"); |
| 601 | QTest::newRow("by version name 3.9 DefaultMatch") |
| 602 | << serviceXml |
| 603 | << filter |
| 604 | << ServiceInterfaceDescriptorList(); |
| 605 | |
| 606 | filter = QServiceFilter(); |
| 607 | filter.setInterface("com.nokia.qt.TestInterfaceB", "3.9", QServiceFilter::MinimumVersionMatch); |
| 608 | QTest::newRow("by version name 3.9 MinimumMatch") |
| 609 | << serviceXml |
| 610 | << filter |
| 611 | << ServiceInterfaceDescriptorList(); |
| 612 | |
| 613 | filter = QServiceFilter(); |
| 614 | filter.setInterface("com.nokia.qt.TestInterfaceB", "3.9", QServiceFilter::ExactVersionMatch); |
| 615 | QTest::newRow("by version name 3.9 ExactMatch") |
| 616 | << serviceXml |
| 617 | << filter |
| 618 | << ServiceInterfaceDescriptorList(); |
| 619 | |
| 620 | //invalid version tag 1 -> match anything |
| 621 | filter = QServiceFilter(); |
| 622 | filter.setInterface("com.nokia.qt.TestInterfaceB", "x3.9"); |
| 623 | QTest::newRow("by version name x3.9 DefaultMatch") |
| 624 | << serviceXml<< filter |
| 625 | << descriptors; |
| 626 | |
| 627 | filter = QServiceFilter(); |
| 628 | filter.setInterface("com.nokia.qt.TestInterfaceB", "x3.9", QServiceFilter::MinimumVersionMatch); |
| 629 | QTest::newRow("by version name x3.9 MinimumMatch") |
| 630 | << serviceXml |
| 631 | << filter |
| 632 | << descriptors; |
| 633 | |
| 634 | filter = QServiceFilter(); |
| 635 | filter.setInterface("com.nokia.qt.TestInterfaceB", "x3.9", QServiceFilter::ExactVersionMatch); |
| 636 | QTest::newRow("by version name x3.9 ExactMatch") |
| 637 | << serviceXml |
| 638 | << filter |
| 639 | << descriptors; |
| 640 | |
| 641 | //envalid/empty version tag |
| 642 | filter = QServiceFilter(); |
| 643 | filter.setInterface("com.nokia.qt.TestInterfaceB", ""); |
| 644 | QTest::newRow("by empty version string DefaultMatch") |
| 645 | << serviceXml |
| 646 | << filter |
| 647 | << descriptors.mid(1); |
| 648 | |
| 649 | filter = QServiceFilter(); |
| 650 | filter.setInterface("com.nokia.qt.TestInterfaceB", "", QServiceFilter::MinimumVersionMatch); |
| 651 | QTest::newRow("by empty version string MinimumMatch") |
| 652 | << serviceXml |
| 653 | << filter |
| 654 | << descriptors.mid(1); |
| 655 | |
| 656 | filter = QServiceFilter(); |
| 657 | filter.setInterface("com.nokia.qt.TestInterfaceB", "", QServiceFilter::ExactVersionMatch); //what's the result of this? |
| 658 | QTest::newRow("by empty version string ExactMatch") |
| 659 | << serviceXml |
| 660 | << filter |
| 661 | << descriptors.mid(1); |
| 662 | |
| 663 | //invalid version tag 2 |
| 664 | filter = QServiceFilter(); |
| 665 | filter.setInterface("com.nokia.qt.TestInterfaceB", "abc"); |
| 666 | QTest::newRow("by version name abc DefaultMatch") |
| 667 | << serviceXml<< filter |
| 668 | << descriptors; |
| 669 | |
| 670 | filter = QServiceFilter(); |
| 671 | filter.setInterface("com.nokia.qt.TestInterfaceB", "abc", QServiceFilter::MinimumVersionMatch); |
| 672 | QTest::newRow("by version name abc MinimumMatch") |
| 673 | << serviceXml<< filter |
| 674 | << descriptors; |
| 675 | |
| 676 | filter = QServiceFilter(); |
| 677 | filter.setInterface("com.nokia.qt.TestInterfaceB", "abc", QServiceFilter::ExactVersionMatch); |
| 678 | QTest::newRow("by version name abc ExactMatch") |
| 679 | << serviceXml |
| 680 | << filter |
| 681 | << descriptors; |
| 682 | } |
| 683 | |
| 684 | void tst_QServiceManager::findInterfaces_scope() |
| 685 | { |
| 686 | QFETCH(QService::Scope, scope_add); |
| 687 | QFETCH(QService::Scope, scope_find); |
| 688 | QFETCH(bool, expectFound); |
| 689 | |
| 690 | QByteArray xml = createServiceXml("SomeTestService", |
| 691 | createInterfaceXml("com.nokia.qt.TestInterface"), m_validPluginNames[0]); |
| 692 | QBuffer buffer(&xml); |
| 693 | |
| 694 | QServiceManager mgrUser(QService::UserScope); |
| 695 | QServiceManager mgrSystem(QService::SystemScope); |
| 696 | |
| 697 | QServiceManager &mgrAdd = scope_add == QService::UserScope ? mgrUser : mgrSystem; |
| 698 | QServiceManager &mgrFind = scope_find == QService::UserScope ? mgrUser : mgrSystem; |
| 699 | |
| 700 | QList<QServiceInterfaceDescriptor> result = mgrFind.findInterfaces(QString()); |
| 701 | QVERIFY(result.isEmpty()); |
| 702 | |
| 703 | QVERIFY2(mgrAdd.addService(&buffer), PRINT_ERR(mgrAdd)); |
| 704 | result = mgrFind.findInterfaces("SomeTestService"); |
| 705 | QCOMPARE(!result.isEmpty(), expectFound); |
| 706 | |
| 707 | result = mgrFind.findInterfaces(QString()); |
| 708 | if (expectFound) |
| 709 | QVERIFY(result.count() == 1); |
| 710 | else |
| 711 | QVERIFY(result.isEmpty()); |
| 712 | |
| 713 | result = mgrFind.findInterfaces("NonExistingService"); |
| 714 | QVERIFY(result.isEmpty()); |
| 715 | } |
| 716 | |
| 717 | void tst_QServiceManager::findInterfaces_scope_data() |
| 718 | { |
| 719 | findServices_scope_data(); |
| 720 | } |
| 721 | |
| 722 | |
| 723 | void tst_QServiceManager::loadInterface_string() |
| 724 | { |
| 725 | // The sampleservice.xml and sampleservice2.xml services in |
| 726 | // tests/sampleserviceplugin and tests/sampleserviceplugin2 implement a |
| 727 | // common interface, "com.nokia.qt.TestInterfaceA". If both are |
| 728 | // registered, loadInterface(QString) should return the correct one |
| 729 | // depending on which is set as the default. |
| 730 | |
| 731 | // Real servicenames and classnames |
| 732 | QString serviceA = "SampleService"; |
| 733 | QString serviceAClassName = "SampleServicePluginClass"; |
| 734 | QString serviceB = "SampleService2"; |
| 735 | QString serviceBClassName = "SampleServicePluginClass2"; |
| 736 | |
| 737 | QObject *obj = 0; |
| 738 | QServiceManager mgr; |
| 739 | QString commonInterface = "com.nokia.qt.TestInterfaceA"; |
| 740 | |
| 741 | // Add first service. Adds the service described in |
| 742 | // c/Private/<uid3 of this executable>/plugins/xmldata/sampleservice.xml |
| 743 | QVERIFY2(mgr.addService(xmlTestDataPath("sampleservice.xml")), PRINT_ERR(mgr)); |
| 744 | |
| 745 | obj = mgr.loadInterface(commonInterface); |
| 746 | QVERIFY(obj != 0); |
| 747 | QCOMPARE(QString(obj->metaObject()->className()), serviceAClassName); |
| 748 | delete obj; |
| 749 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 750 | QCoreApplication::processEvents(); |
| 751 | |
| 752 | // Add first service. Adds the service described in |
| 753 | // c/Private/<uid3 of this executable>/plugins/xmldata/sampleservice2.xml |
| 754 | QVERIFY2(mgr.addService(xmlTestDataPath("sampleservice2.xml")), PRINT_ERR(mgr)); |
| 755 | |
| 756 | // if first service is set as default, it should be returned |
| 757 | QVERIFY(mgr.setInterfaceDefault(serviceA, commonInterface)); |
| 758 | obj = mgr.loadInterface(commonInterface); |
| 759 | QVERIFY(obj != 0); |
| 760 | QCOMPARE(QString(obj->metaObject()->className()), serviceAClassName); |
| 761 | delete obj; |
| 762 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 763 | QCoreApplication::processEvents(); |
| 764 | |
| 765 | // if second service is set as default, it should be returned |
| 766 | QVERIFY(mgr.setInterfaceDefault(serviceB, commonInterface)); |
| 767 | obj = mgr.loadInterface(commonInterface); |
| 768 | QVERIFY(obj != 0); |
| 769 | QCOMPARE(QString(obj->metaObject()->className()), serviceBClassName); |
| 770 | delete obj; |
| 771 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 772 | QCoreApplication::processEvents(); |
| 773 | } |
| 774 | |
| 775 | void tst_QServiceManager::loadInterface_descriptor() |
| 776 | { |
| 777 | QFETCH(QServiceInterfaceDescriptor, descriptor); |
| 778 | QFETCH(QString, className); |
| 779 | |
| 780 | QObject* obj; |
| 781 | { |
| 782 | QServiceManager mgr; |
| 783 | obj = mgr.loadInterface(descriptor); |
| 784 | QVERIFY(obj != 0); |
| 785 | QCOMPARE(QString::fromLatin1(obj->metaObject()->className()), className); |
| 786 | } |
| 787 | |
| 788 | QVERIFY(obj != 0); |
| 789 | |
| 790 | delete obj; |
| 791 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 792 | QCoreApplication::processEvents(); |
| 793 | } |
| 794 | |
| 795 | void tst_QServiceManager::loadInterface_descriptor_data() |
| 796 | { |
| 797 | QTest::addColumn<QServiceInterfaceDescriptor>("descriptor"); |
| 798 | QTest::addColumn<QString>("className"); |
| 799 | |
| 800 | for (int i = 0; i < m_validPluginNames.size(); ++i) { |
| 801 | if (const char *expectedClassName = expectedPluginClassNames[i]) { |
| 802 | QServiceInterfaceDescriptor descriptor; |
| 803 | QServiceInterfaceDescriptorPrivate *priv = new QServiceInterfaceDescriptorPrivate; |
| 804 | priv->interfaceName = "com.nokia.qt.TestInterfaceA"; // needed by service plugin implementation |
| 805 | priv->attributes[QServiceInterfaceDescriptor::Location] = m_validPluginPaths.at(i); |
| 806 | QServiceInterfaceDescriptorPrivate::setPrivate(&descriptor, priv); |
| 807 | QTest::newRow(qPrintable(m_validPluginNames.at(i))) |
| 808 | << descriptor |
| 809 | << QString(QLatin1String(expectedClassName)); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | void tst_QServiceManager::loadInterface_testLoadedObjectAttributes() |
| 815 | { |
| 816 | QServiceInterfaceDescriptor descriptor; |
| 817 | QServiceInterfaceDescriptorPrivate *priv = new QServiceInterfaceDescriptorPrivate; |
| 818 | priv->interfaceName = "com.nokia.qt.TestInterfaceA"; // needed by service plugin implementation |
| 819 | priv->attributes[QServiceInterfaceDescriptor::Location] = m_validPluginPaths.at(2); |
| 820 | |
| 821 | QServiceInterfaceDescriptorPrivate::setPrivate(&descriptor, priv); |
| 822 | |
| 823 | QServiceManager mgr; |
| 824 | QObject *obj = mgr.loadInterface(descriptor); |
| 825 | QVERIFY(obj != 0); |
| 826 | |
| 827 | bool invokeOk = false; |
| 828 | QString name; |
| 829 | |
| 830 | // check attributes |
| 831 | QMetaProperty nameProperty = obj->metaObject()->property(obj->metaObject()->indexOfProperty("name")); |
| 832 | QVERIFY(nameProperty.isValid()); |
| 833 | QVERIFY(nameProperty.write(obj, "A service name")); |
| 834 | QCOMPARE(nameProperty.read(obj), QVariant("A service name")); |
| 835 | |
| 836 | // check signals |
| 837 | QVERIFY(obj->metaObject()->indexOfSignal("someSignal()") >= 0); |
| 838 | QSignalSpy spy(obj, SIGNAL(someSignal())); |
| 839 | invokeOk = QMetaObject::invokeMethod(obj, "someSignal"); |
| 840 | QVERIFY(invokeOk); |
| 841 | QVERIFY(spy.count() == 1); |
| 842 | |
| 843 | // check slots |
| 844 | invokeOk = QMetaObject::invokeMethod(obj, "callSlot"); |
| 845 | QVERIFY(invokeOk); |
| 846 | invokeOk = QMetaObject::invokeMethod(obj, "callSlotAndSetName", Q_ARG(QString, "test string")); |
| 847 | QVERIFY(invokeOk); |
| 848 | invokeOk = QMetaObject::invokeMethod(obj, "callSlotAndReturnName", Q_RETURN_ARG(QString, name)); |
| 849 | QVERIFY(invokeOk); |
| 850 | QCOMPARE(name, QString("test string")); |
| 851 | |
| 852 | // check invokables |
| 853 | invokeOk = QMetaObject::invokeMethod(obj, "callInvokable"); |
| 854 | QVERIFY(invokeOk); |
| 855 | |
| 856 | // call method on a returned object |
| 857 | QObject *embeddedObj = 0; |
| 858 | int value = 0; |
| 859 | invokeOk = QMetaObject::invokeMethod(obj, "embeddedTestService", Q_RETURN_ARG(QObject*, embeddedObj)); |
| 860 | QVERIFY(invokeOk); |
| 861 | invokeOk = QMetaObject::invokeMethod(embeddedObj, "callWithInt", Q_RETURN_ARG(int, value), Q_ARG(int, 10)); |
| 862 | QVERIFY(invokeOk); |
| 863 | QCOMPARE(value, 10); |
| 864 | |
| 865 | // call a method that is not invokable via meta system |
| 866 | invokeOk = QMetaObject::invokeMethod(embeddedObj, "callNormalMethod"); |
| 867 | QVERIFY(!invokeOk); |
| 868 | |
| 869 | delete obj; |
| 870 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 871 | QCoreApplication::processEvents(); |
| 872 | } |
| 873 | |
| 874 | void tst_QServiceManager::loadLocalTypedInterface() |
| 875 | { |
| 876 | QServiceManager mgr; |
| 877 | |
| 878 | QServiceInterfaceDescriptor descriptor; |
| 879 | QServiceInterfaceDescriptorPrivate *priv = new QServiceInterfaceDescriptorPrivate; |
| 880 | priv->interfaceName = "com.nokia.qt.TestInterfaceA"; // needed by service plugin implementation |
| 881 | priv->attributes[QServiceInterfaceDescriptor::Location] = m_validPluginPaths.at(0); |
| 882 | QServiceInterfaceDescriptorPrivate::setPrivate(&descriptor, priv); |
| 883 | |
| 884 | //use manual descriptor -> avoid database involvement |
| 885 | SampleServicePluginClass *plugin = 0; |
| 886 | plugin = mgr.loadLocalTypedInterface<SampleServicePluginClass>(descriptor); |
| 887 | |
| 888 | QVERIFY(plugin != 0); |
| 889 | |
| 890 | delete plugin; |
| 891 | plugin = 0; |
| 892 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 893 | QCoreApplication::processEvents(); |
| 894 | |
| 895 | //use database descriptor |
| 896 | QFile file1(xmlTestDataPath("sampleservice.xml")); |
| 897 | QVERIFY2(file1.exists(), qPrintable(QString("%1: %2").arg(xmlTestDataPath("sampleservice.xml")).arg(file1.errorString()) )); |
| 898 | QVERIFY2(mgr.addService(&file1), PRINT_ERR(mgr)); |
| 899 | |
| 900 | QCOMPARE(mgr.findServices("com.nokia.qt.TestInterfaceA"), QStringList("SampleService")); |
| 901 | QCOMPARE(mgr.findServices("com.nokia.qt.TestInterfaceB"), QStringList("SampleService")); |
| 902 | QCOMPARE(mgr.findServices("com.nokia.qt.TestInterfaceC"), QStringList("SampleService")); |
| 903 | QList<QServiceInterfaceDescriptor> ifaces = mgr.findInterfaces("SampleService"); |
| 904 | QList<SampleServicePluginClass*> serviceObjects; |
| 905 | QVERIFY(ifaces.count() == 3); |
| 906 | for (int i = 0; i<ifaces.count(); i++) { |
| 907 | plugin = mgr.loadLocalTypedInterface<SampleServicePluginClass>(ifaces.at(i)); |
| 908 | |
| 909 | if (ifaces.at(i).interfaceName() == "com.nokia.qt.TestInterfaceC") { |
| 910 | QVERIFY(plugin == 0); |
| 911 | } else { |
| 912 | QVERIFY(plugin != 0); |
| 913 | plugin->testSlotOne(); |
| 914 | serviceObjects.append(plugin); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | //test for a bug where two service instances from same plugin |
| 919 | //caused a crash when the first instance was deleted and |
| 920 | //the second instance called |
| 921 | QVERIFY(serviceObjects.count() == 2); |
| 922 | delete serviceObjects.takeFirst(); |
| 923 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 924 | QCoreApplication::processEvents(); |
| 925 | |
| 926 | plugin = serviceObjects.takeFirst(); |
| 927 | plugin->testSlotOne(); |
| 928 | delete plugin; |
| 929 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 930 | QCoreApplication::processEvents(); |
| 931 | |
| 932 | |
| 933 | //use default lookup |
| 934 | plugin = mgr.loadLocalTypedInterface<SampleServicePluginClass>("com.nokia.qt.TestInterfaceA"); |
| 935 | QVERIFY(plugin != 0); |
| 936 | |
| 937 | delete plugin; |
| 938 | QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); |
| 939 | QCoreApplication::processEvents(); |
| 940 | plugin = 0; |
| 941 | |
| 942 | //use totally wrong but QObject based template class type |
| 943 | QFile *w = mgr.loadLocalTypedInterface<QFile>("com.nokia.qt.TestInterfaceA"); |
| 944 | QVERIFY(!w); |
| 945 | |
| 946 | //use non QObject based template class type |
| 947 | //doesn't compile and should never compile |
| 948 | //QString* s = mgr.loadLocalTypedInterface<QString>("com.nokia.qt.TestInterfaceA", &context, &session); |
| 949 | //QVERIFY(!s); |
| 950 | } |
| 951 | |
| 952 | #define TST_QSERVICEMANAGER_ADD_SERVICE(paramType, file) { \ |
| 953 | if (paramType == "QString") \ |
| 954 | QVERIFY2(mgr.addService(file->fileName()), PRINT_ERR(mgr)); \ |
| 955 | else if (paramType == "QIODevice") \ |
| 956 | QVERIFY2(mgr.addService(file), PRINT_ERR(mgr)); \ |
| 957 | else \ |
| 958 | QFAIL("tst_QServiceManager::addService(): Bad test parameter"); \ |
| 959 | } |
| 960 | |
| 961 | void tst_QServiceManager::addService() |
| 962 | { |
| 963 | QFETCH(QString, paramType); |
| 964 | |
| 965 | QServiceManager mgr; |
| 966 | |
| 967 | QString commonInterface = "com.qt.serviceframework.tests.CommonInterface"; |
| 968 | QByteArray xmlA = createServiceXml("ServiceA", createInterfaceXml(commonInterface), m_validPluginNames[0]); |
| 969 | QByteArray xmlB = createServiceXml("ServiceB", createInterfaceXml(commonInterface), m_validPluginNames[1]); |
| 970 | |
| 971 | QTemporaryFile *tempFileA = new QTemporaryFile(this); |
| 972 | QVERIFY2(tempFileA->open(), "Can't open temp file A"); |
| 973 | tempFileA->write(xmlA); |
| 974 | tempFileA->seek(0); |
| 975 | QTemporaryFile *tempFileB = new QTemporaryFile(this); |
| 976 | QVERIFY2(tempFileB->open(), "Can't open temp file B"); |
| 977 | tempFileB->write(xmlB); |
| 978 | tempFileB->seek(0); |
| 979 | |
| 980 | TST_QSERVICEMANAGER_ADD_SERVICE(paramType, tempFileA); |
| 981 | QCOMPARE(mgr.findServices(), QStringList("ServiceA")); |
| 982 | |
| 983 | // the service should be automatically set as the default for its |
| 984 | // implemented interfaces since it was the first service added for them |
| 985 | QCOMPARE(mgr.interfaceDefault(commonInterface).serviceName(), QString("ServiceA")); |
| 986 | QCOMPARE(mgr.interfaceDefault(commonInterface).serviceName(), QString("ServiceA")); |
| 987 | |
| 988 | // add second service |
| 989 | TST_QSERVICEMANAGER_ADD_SERVICE(paramType, tempFileB); |
| 990 | QStringList result = mgr.findServices(); |
| 991 | QCOMPARE(result.count(), 2); |
| 992 | QVERIFY(result.contains("ServiceA")); |
| 993 | QVERIFY(result.contains("ServiceB")); |
| 994 | |
| 995 | // the default does not change once ServiceB is added |
| 996 | QCOMPARE(mgr.interfaceDefault(commonInterface).serviceName(), QString("ServiceA")); |
| 997 | QCOMPARE(mgr.interfaceDefault(commonInterface).serviceName(), QString("ServiceA")); |
| 998 | |
| 999 | delete tempFileA; |
| 1000 | delete tempFileB; |
| 1001 | } |
| 1002 | |
| 1003 | void tst_QServiceManager::addService_data() |
| 1004 | { |
| 1005 | QTest::addColumn<QString>("paramType"); |
| 1006 | |
| 1007 | QTest::newRow("string") << "QString"; |
| 1008 | QTest::newRow("iodevice") << "QIODevice"; |
| 1009 | } |
| 1010 | |
| 1011 | void tst_QServiceManager::addService_testInvalidServiceXml() |
| 1012 | { |
| 1013 | QBuffer buffer; |
| 1014 | QServiceManager mgr; |
| 1015 | |
| 1016 | QVERIFY2(!mgr.addService(&buffer), PRINT_ERR(mgr)); |
| 1017 | |
| 1018 | // a service with no interfaces |
| 1019 | QString xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"; |
| 1020 | xml += "<service><name>SomeService</name><filepath>" + m_validPluginNames.first() + "</filepath>\n"; |
| 1021 | xml += "</service>\n"; |
| 1022 | buffer.close(); |
| 1023 | buffer.setData(xml.toLatin1()); |
| 1024 | QVERIFY(!mgr.addService(&buffer)); |
| 1025 | |
| 1026 | QVERIFY2(mgr.findServices().isEmpty(), PRINT_ERR(mgr)); |
| 1027 | } |
| 1028 | |
| 1029 | void tst_QServiceManager::addService_testPluginLoading() |
| 1030 | { |
| 1031 | QFETCH(QString, pluginPath); |
| 1032 | QFETCH(bool, isAdded); |
| 1033 | |
| 1034 | QServiceManager mgr; |
| 1035 | |
| 1036 | QByteArray xml = createServiceXml("SomeService", createInterfaceXml("com.qt.serviceframework.Interface"), pluginPath); |
| 1037 | QBuffer buffer(&xml); |
| 1038 | QVERIFY2(mgr.addService(&buffer) == isAdded, PRINT_ERR(mgr)); |
| 1039 | |
| 1040 | // the service should not be added if the service plugin cannot be loaded |
| 1041 | if (!isAdded) |
| 1042 | QVERIFY(mgr.findServices().isEmpty()); |
| 1043 | } |
| 1044 | |
| 1045 | void tst_QServiceManager::addService_testPluginLoading_data() |
| 1046 | { |
| 1047 | QTest::addColumn<QString>("pluginPath"); |
| 1048 | QTest::addColumn<bool>("isAdded"); |
| 1049 | |
| 1050 | QTest::newRow("valid path") << m_validPluginNames.first() << true; |
| 1051 | QTest::newRow("invalid path") << "no_such_plugin" << false; |
| 1052 | } |
| 1053 | |
| 1054 | void tst_QServiceManager::addService_testInstallService() |
| 1055 | { |
| 1056 | QSettings settings("com.nokia.qt.serviceframework.tests", "SampleServicePlugin"); |
| 1057 | QCOMPARE(settings.value("installed").toBool(), false); |
| 1058 | |
| 1059 | QServiceManager mgr; |
| 1060 | QVERIFY2(mgr.addService(xmlTestDataPath("sampleservice.xml")), PRINT_ERR(mgr)); |
| 1061 | QCOMPARE(mgr.findServices(), QStringList("SampleService")); |
| 1062 | |
| 1063 | // test installService() was called on the plugin |
| 1064 | QCOMPARE(settings.value("installed").toBool(), true); |
| 1065 | } |
| 1066 | |
| 1067 | void tst_QServiceManager::removeService() |
| 1068 | { |
| 1069 | QServiceManager mgr; |
| 1070 | |
| 1071 | QVERIFY(!mgr.removeService("NonExistentService")); |
| 1072 | |
| 1073 | QSettings settings("com.nokia.qt.serviceframework.tests", "SampleServicePlugin"); |
| 1074 | QCOMPARE(settings.value("installed").toBool(), false); |
| 1075 | |
| 1076 | qDebug() << "open" << xmlTestDataPath("sampleservice.xml"); |
| 1077 | QVERIFY2(mgr.addService(xmlTestDataPath("sampleservice.xml")), PRINT_ERR(mgr)); |
| 1078 | QCOMPARE(mgr.findServices("com.nokia.qt.TestInterfaceA"), QStringList("SampleService")); |
| 1079 | QCOMPARE(settings.value("installed").toBool(), true); |
| 1080 | |
| 1081 | QVERIFY(mgr.removeService("SampleService")); |
| 1082 | QVERIFY(mgr.findServices().isEmpty()); |
| 1083 | QCOMPARE(mgr.findServices("com.nokia.qt.TestInterfaceA"), QStringList()); |
| 1084 | QCOMPARE(settings.value("installed").toBool(), false); |
| 1085 | |
| 1086 | // add it again, should still work |
| 1087 | QVERIFY2(mgr.addService(xmlTestDataPath("sampleservice.xml")), PRINT_ERR(mgr)); |
| 1088 | QCOMPARE(mgr.findServices("com.nokia.qt.TestInterfaceA"), QStringList("SampleService")); |
| 1089 | QCOMPARE(settings.value("installed").toBool(), true); |
| 1090 | } |
| 1091 | |
| 1092 | void tst_QServiceManager::setInterfaceDefault_strings() |
| 1093 | { |
| 1094 | QServiceManager mgr; |
| 1095 | QString interfaceName = "com.nokia.qt.serviceframework.tests.AnInterface"; |
| 1096 | DescriptorAttributes attributes; |
| 1097 | QServiceInterfaceDescriptor descriptor; |
| 1098 | QByteArray xml; |
| 1099 | |
| 1100 | attributes[QServiceInterfaceDescriptor::Location] = m_validPluginNames[0]; |
| 1101 | descriptor = createDescriptor(interfaceName, 1, 0, "ServiceA", attributes); |
| 1102 | xml = createServiceXml("ServiceA", |
| 1103 | createInterfaceXml(QList<QServiceInterfaceDescriptor>() << descriptor), |
| 1104 | attributes[QServiceInterfaceDescriptor::Location].toString()); |
| 1105 | QBuffer buffer(&xml); |
| 1106 | |
| 1107 | // fails if the specified interface hasn't been registered |
| 1108 | QCOMPARE(mgr.setInterfaceDefault("ServiceA", interfaceName), false); |
| 1109 | |
| 1110 | // now it works |
| 1111 | QVERIFY2(mgr.addService(&buffer), PRINT_ERR(mgr)); |
| 1112 | QCOMPARE(mgr.setInterfaceDefault("ServiceA", interfaceName), true); |
| 1113 | QCOMPARE(mgr.interfaceDefault(interfaceName), descriptor); |
| 1114 | |
| 1115 | // replace the default with another service |
| 1116 | attributes[QServiceInterfaceDescriptor::Location] = m_validPluginNames[1]; |
| 1117 | descriptor = createDescriptor(interfaceName, 1, 0, "ServiceB", attributes); |
| 1118 | xml = createServiceXml("ServiceB", |
| 1119 | createInterfaceXml(QList<QServiceInterfaceDescriptor>() << descriptor), |
| 1120 | attributes[QServiceInterfaceDescriptor::Location].toString()); |
| 1121 | buffer.close(); |
| 1122 | buffer.setData(xml); |
| 1123 | QVERIFY2(mgr.addService(&buffer), PRINT_ERR(mgr)); |
| 1124 | QCOMPARE(mgr.setInterfaceDefault("ServiceB", interfaceName), true); |
| 1125 | QCOMPARE(mgr.interfaceDefault(interfaceName), descriptor); |
| 1126 | |
| 1127 | // bad arguments |
| 1128 | QCOMPARE(mgr.setInterfaceDefault("", ""), false); |
| 1129 | QCOMPARE(mgr.setInterfaceDefault("blah", "blah"), false); |
| 1130 | QCOMPARE(mgr.setInterfaceDefault("SampleService", ""), false); |
| 1131 | } |
| 1132 | |
| 1133 | void tst_QServiceManager::setInterfaceDefault_strings_multipleInterfaces() |
| 1134 | { |
| 1135 | QServiceManager mgr; |
| 1136 | QString interfaceName = "com.nokia.qt.serviceframework.tests.AnInterface"; |
| 1137 | DescriptorAttributes attributes; |
| 1138 | QServiceInterfaceDescriptor descriptor; |
| 1139 | QByteArray xml; |
| 1140 | |
| 1141 | // if there are multiple interfaces, the default should be the latest version |
| 1142 | attributes[QServiceInterfaceDescriptor::Location] = m_validPluginNames[0]; |
| 1143 | QList<QServiceInterfaceDescriptor> descriptorList; |
| 1144 | descriptorList << createDescriptor(interfaceName, 1, 0, "ServiceC", attributes) |
| 1145 | << createDescriptor(interfaceName, 1, 8, "ServiceC", attributes) |
| 1146 | << createDescriptor(interfaceName, 1, 3, "ServiceC", attributes); |
| 1147 | xml = createServiceXml("ServiceC", createInterfaceXml(descriptorList), |
| 1148 | attributes[QServiceInterfaceDescriptor::Location].toString()); |
| 1149 | QBuffer buffer(&xml); |
| 1150 | QVERIFY2(mgr.addService(&buffer), PRINT_ERR(mgr)); |
| 1151 | QCOMPARE(mgr.setInterfaceDefault("ServiceC", interfaceName), true); |
| 1152 | QCOMPARE(mgr.interfaceDefault(interfaceName), descriptorList[1]); |
| 1153 | } |
| 1154 | |
| 1155 | void tst_QServiceManager::setInterfaceDefault_descriptor() |
| 1156 | { |
| 1157 | QFETCH(QService::Scope, scope_add); |
| 1158 | QFETCH(QService::Scope, scope_find); |
| 1159 | QFETCH(bool, expectFound); |
| 1160 | |
| 1161 | QServiceManager mgr(scope_add); |
| 1162 | QServiceInterfaceDescriptor desc; |
| 1163 | |
| 1164 | QString interfaceName = "com.nokia.qt.serviceframework.TestInterface"; |
| 1165 | DescriptorAttributes attributes; |
| 1166 | attributes[QServiceInterfaceDescriptor::Location] = m_validPluginNames.first(); |
| 1167 | |
| 1168 | QCOMPARE(mgr.setInterfaceDefault(desc), false); |
| 1169 | |
| 1170 | desc = createDescriptor(interfaceName, 1, 0, "SomeService", attributes, |
| 1171 | scope_add); |
| 1172 | |
| 1173 | // fails if the specified interface hasn't been registered |
| 1174 | QCOMPARE(mgr.setInterfaceDefault(desc), false); |
| 1175 | |
| 1176 | // now it works |
| 1177 | QByteArray xml = createServiceXml("SomeService", |
| 1178 | createInterfaceXml(QList<QServiceInterfaceDescriptor>() << desc), m_validPluginNames.first()); |
| 1179 | QBuffer buffer(&xml); |
| 1180 | QVERIFY2(mgr.addService(&buffer), PRINT_ERR(mgr)); |
| 1181 | QCOMPARE(mgr.setInterfaceDefault(desc), true); |
| 1182 | |
| 1183 | QCOMPARE(mgr.interfaceDefault(interfaceName), desc); |
| 1184 | |
| 1185 | QServiceManager mgrWithOtherScope(scope_find); |
| 1186 | QCOMPARE(mgrWithOtherScope.interfaceDefault(interfaceName).isValid(), expectFound); |
| 1187 | } |
| 1188 | |
| 1189 | void tst_QServiceManager::setInterfaceDefault_descriptor_data() |
| 1190 | { |
| 1191 | QTest::addColumn<QService::Scope>("scope_add"); |
| 1192 | QTest::addColumn<QService::Scope>("scope_find"); |
| 1193 | QTest::addColumn<bool>("expectFound"); |
| 1194 | |
| 1195 | QTest::newRow("user scope") |
| 1196 | << QService::UserScope << QService::UserScope << true; |
| 1197 | QTest::newRow("system scope") |
| 1198 | << QService::SystemScope << QService::SystemScope << true; |
| 1199 | |
| 1200 | QTest::newRow("user scope - add, system scope - find") |
| 1201 | << QService::UserScope << QService::SystemScope << false; |
| 1202 | QTest::newRow("system scope - add, user scope - find") |
| 1203 | << QService::SystemScope << QService::UserScope << true; |
| 1204 | } |
| 1205 | |
| 1206 | void tst_QServiceManager::interfaceDefault() |
| 1207 | { |
| 1208 | QServiceManager mgr; |
| 1209 | QVERIFY(!mgr.interfaceDefault("").isValid()); |
| 1210 | } |
| 1211 | |
| 1212 | void tst_QServiceManager::serviceAdded() |
| 1213 | { |
| 1214 | QFETCH(QByteArray, xml); |
| 1215 | QFETCH(QString, serviceName); |
| 1216 | QFETCH(QService::Scope, scope_modify); |
| 1217 | QFETCH(QService::Scope, scope_listen); |
| 1218 | QFETCH(bool, expectSignal); |
| 1219 | |
| 1220 | QBuffer buffer; |
| 1221 | buffer.setData(xml); |
| 1222 | QServiceManager mgr_modify(scope_modify); |
| 1223 | QServiceManager mgr_listen(scope_listen); |
| 1224 | |
| 1225 | // ensure mgr.connectNotify() is called |
| 1226 | ServicesListener *listener = new ServicesListener; |
| 1227 | connect(&mgr_listen, SIGNAL(serviceAdded(QString,QService::Scope)), |
| 1228 | listener, SLOT(serviceAdded(QString,QService::Scope))); |
| 1229 | |
| 1230 | QSignalSpy spyAdd(&mgr_listen, SIGNAL(serviceAdded(QString,QService::Scope))); |
| 1231 | QVERIFY2(mgr_modify.addService(&buffer), PRINT_ERR(mgr_modify)); |
| 1232 | |
| 1233 | if (!expectSignal) { |
| 1234 | QTest::qWait(2000); |
| 1235 | QCOMPARE(spyAdd.count(), 0); |
| 1236 | } else { |
| 1237 | QTRY_COMPARE(spyAdd.count(), 1); |
| 1238 | } |
| 1239 | |
| 1240 | if (expectSignal) { |
| 1241 | QCOMPARE(spyAdd.at(0).at(0).toString(), serviceName); |
| 1242 | QCOMPARE( listener->params.at(0).second , scope_modify); |
| 1243 | } |
| 1244 | listener->params.clear(); |
| 1245 | |
| 1246 | // Pause between file changes so they are detected separately |
| 1247 | QTest::qWait(2000); |
| 1248 | |
| 1249 | QSignalSpy spyRemove(&mgr_listen, SIGNAL(serviceRemoved(QString,QService::Scope))); |
| 1250 | QVERIFY(mgr_modify.removeService(serviceName)); |
| 1251 | |
| 1252 | if (!expectSignal) { |
| 1253 | QTest::qWait(2000); |
| 1254 | QCOMPARE(spyRemove.count(), 0); |
| 1255 | } else { |
| 1256 | QTRY_COMPARE(spyRemove.count(), 1); |
| 1257 | } |
| 1258 | |
| 1259 | #if !defined (Q_OS_WIN) |
| 1260 | // on win, cannot delete the database while it is in use |
| 1261 | // try it again after deleting the database |
| 1262 | deleteTestDatabasesAndWaitUntilDone(); |
| 1263 | #else |
| 1264 | // Pause between file changes so they are detected separately |
| 1265 | QTest::qWait(2000); |
| 1266 | #endif |
| 1267 | |
| 1268 | spyAdd.clear(); |
| 1269 | buffer.seek(0); |
| 1270 | QVERIFY2(mgr_modify.addService(&buffer), PRINT_ERR(mgr_modify)); |
| 1271 | if (!expectSignal) { |
| 1272 | QTest::qWait(2000); |
| 1273 | QCOMPARE(spyAdd.count(), 0); |
| 1274 | } else { |
| 1275 | QTRY_COMPARE(spyAdd.count(), 1); |
| 1276 | } |
| 1277 | if (expectSignal) { |
| 1278 | QCOMPARE(spyAdd.at(0).at(0).toString(), serviceName); |
| 1279 | QCOMPARE(listener->params.at(0).second, scope_modify); |
| 1280 | } |
| 1281 | |
| 1282 | // ensure mgr.disconnectNotify() is called |
| 1283 | disconnect(&mgr_listen, SIGNAL(serviceRemoved(QString,QService::Scope)), |
| 1284 | listener, SLOT(serviceRemoved(QString,QService::Scope))); |
| 1285 | |
| 1286 | delete listener; |
| 1287 | } |
| 1288 | |
| 1289 | void tst_QServiceManager::serviceAdded_data() |
| 1290 | { |
| 1291 | QTest::addColumn<QByteArray>("xml"); |
| 1292 | QTest::addColumn<QString>("serviceName"); |
| 1293 | QTest::addColumn<QService::Scope>("scope_modify"); |
| 1294 | QTest::addColumn<QService::Scope>("scope_listen"); |
| 1295 | QTest::addColumn<bool>("expectSignal"); |
| 1296 | |
| 1297 | QFile file1(xmlTestDataPath("sampleservice.xml")); |
| 1298 | QVERIFY(file1.open(QIODevice::ReadOnly)); |
| 1299 | QFile file2(xmlTestDataPath("testserviceplugin.xml")); |
| 1300 | QVERIFY(file2.open(QIODevice::ReadOnly)); |
| 1301 | |
| 1302 | QByteArray file1Data = file1.readAll(); |
| 1303 | |
| 1304 | QTest::newRow("SampleService, user scope") << file1Data << "SampleService" |
| 1305 | << QService::UserScope << QService::UserScope << true; |
| 1306 | QTest::newRow("TestService, user scope") << file2.readAll() << "TestService" |
| 1307 | << QService::UserScope << QService::UserScope << true; |
| 1308 | |
| 1309 | QTest::newRow("system scope") << file1Data << "SampleService" |
| 1310 | << QService::SystemScope << QService::SystemScope << true; |
| 1311 | QTest::newRow("modify as user, listen in system") << file1Data << "SampleService" |
| 1312 | << QService::UserScope << QService::SystemScope << false; |
| 1313 | QTest::newRow("modify as system, listen in user") << file1Data << "SampleService" |
| 1314 | << QService::SystemScope << QService::UserScope << true; |
| 1315 | } |
| 1316 | |
| 1317 | void tst_QServiceManager::serviceRemoved() |
| 1318 | { |
| 1319 | QFETCH(QByteArray, xml); |
| 1320 | QFETCH(QString, serviceName); |
| 1321 | QFETCH(QService::Scope, scope_modify); |
| 1322 | QFETCH(QService::Scope, scope_listen); |
| 1323 | QFETCH(bool, expectSignal); |
| 1324 | |
| 1325 | QBuffer buffer; |
| 1326 | buffer.setData(xml); |
| 1327 | QServiceManager mgr_modify(scope_modify); |
| 1328 | QServiceManager mgr_verify(scope_listen); |
| 1329 | |
| 1330 | // ensure mgr.connectNotify() is called |
| 1331 | ServicesListener *listenerVerify = new ServicesListener; |
| 1332 | connect(&mgr_verify, SIGNAL(serviceAdded(QString,QService::Scope)), |
| 1333 | listenerVerify, SLOT(serviceAdded(QString,QService::Scope))); |
| 1334 | |
| 1335 | QSignalSpy spyAddMod(&mgr_verify, SIGNAL(serviceAdded(QString,QService::Scope))); |
| 1336 | QVERIFY2(mgr_modify.addService(&buffer), PRINT_ERR(mgr_modify)); |
| 1337 | |
| 1338 | if (!expectSignal) { |
| 1339 | QTest::qWait(2000); |
| 1340 | QCOMPARE(spyAddMod.count(), 0); |
| 1341 | } else { |
| 1342 | QTRY_COMPARE(spyAddMod.count(), 1); |
| 1343 | } |
| 1344 | |
| 1345 | // Pause between file changes so they are detected separately |
| 1346 | QTest::qWait(2000); |
| 1347 | |
| 1348 | QServiceManager mgr_listen(scope_listen); |
| 1349 | QSignalSpy spyAdd(&mgr_listen, SIGNAL(serviceAdded(QString,QService::Scope))); |
| 1350 | |
| 1351 | // ensure mgr.connectNotify() is called |
| 1352 | ServicesListener *listener = new ServicesListener; |
| 1353 | connect(&mgr_listen, SIGNAL(serviceRemoved(QString,QService::Scope)), |
| 1354 | listener, SLOT(serviceRemoved(QString,QService::Scope))); |
| 1355 | |
| 1356 | QSignalSpy spyRemove(&mgr_listen, SIGNAL(serviceRemoved(QString,QService::Scope))); |
| 1357 | QVERIFY(mgr_modify.removeService(serviceName)); |
| 1358 | |
| 1359 | if (!expectSignal) { |
| 1360 | QTest::qWait(2000); |
| 1361 | QCOMPARE(spyRemove.count(), 0); |
| 1362 | } else { |
| 1363 | QTRY_COMPARE(spyRemove.count(), 1); |
| 1364 | } |
| 1365 | if (expectSignal) { |
| 1366 | QCOMPARE(spyRemove.at(0).at(0).toString(), serviceName); |
| 1367 | QCOMPARE(listener->params.at(0).second , scope_modify); |
| 1368 | } |
| 1369 | listener->params.clear(); |
| 1370 | |
| 1371 | #if !defined (Q_OS_WIN) |
| 1372 | // on win, cannot delete the database while it is in use |
| 1373 | // try it again after deleting the database |
| 1374 | deleteTestDatabasesAndWaitUntilDone(); |
| 1375 | #else |
| 1376 | // Pause between file changes so they are detected separately |
| 1377 | QTest::qWait(2000); |
| 1378 | #endif |
| 1379 | spyAdd.clear(); |
| 1380 | buffer.seek(0); |
| 1381 | QVERIFY2(mgr_modify.addService(&buffer), PRINT_ERR(mgr_modify)); |
| 1382 | if (!expectSignal) { |
| 1383 | QTest::qWait(2000); |
| 1384 | QCOMPARE(spyAdd.count(), 0); |
| 1385 | } else { |
| 1386 | QTRY_COMPARE(spyAdd.count(), 1); |
| 1387 | } |
| 1388 | |
| 1389 | spyRemove.clear(); |
| 1390 | |
| 1391 | // Pause between file changes so they are detected separately |
| 1392 | QTest::qWait(2000); |
| 1393 | |
| 1394 | QVERIFY(mgr_modify.removeService(serviceName)); |
| 1395 | if (!expectSignal) { |
| 1396 | QTest::qWait(2000); |
| 1397 | QCOMPARE(spyRemove.count(), 0); |
| 1398 | } else { |
| 1399 | QTRY_COMPARE(spyRemove.count(), 1); |
| 1400 | } |
| 1401 | if (expectSignal) { |
| 1402 | QCOMPARE(spyRemove.at(0).at(0).toString(), serviceName); |
| 1403 | QCOMPARE(listener->params.at(0).second , scope_modify); |
| 1404 | } |
| 1405 | |
| 1406 | // ensure mgr.disconnectNotify() is called |
| 1407 | disconnect(&mgr_listen, SIGNAL(serviceRemoved(QString,QService::Scope)), |
| 1408 | listener, SLOT(serviceRemoved(QString,QService::Scope))); |
| 1409 | disconnect(&mgr_verify, SIGNAL(serviceRemoved(QString,QService::Scope)), |
| 1410 | listenerVerify, SLOT(serviceRemoved(QString,QService::Scope))); |
| 1411 | |
| 1412 | |
| 1413 | delete listener; |
| 1414 | delete listenerVerify; |
| 1415 | } |
| 1416 | |
| 1417 | void tst_QServiceManager::serviceRemoved_data() |
| 1418 | { |
| 1419 | serviceAdded_data(); |
| 1420 | } |
| 1421 | |
| 1422 | QTEST_MAIN(tst_QServiceManager) |
| 1423 | |
| 1424 | #include "tst_qservicemanager.moc" |