| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DCore/private/qservicelocator_p.h> |
| 6 | #include <Qt3DCore/private/qopenglinformationservice_p.h> |
| 7 | #include <Qt3DCore/private/qsysteminformationservice_p.h> |
| 8 | |
| 9 | #include <QScopedPointer> |
| 10 | |
| 11 | using namespace Qt3DCore; |
| 12 | |
| 13 | class DummyService : public QAbstractServiceProvider |
| 14 | { |
| 15 | public: |
| 16 | DummyService() |
| 17 | : QAbstractServiceProvider(QServiceLocator::UserService + 1, |
| 18 | QStringLiteral("Dummy Service")) |
| 19 | {} |
| 20 | |
| 21 | int data() const { return 10; } |
| 22 | float moreData() const { return 3.14159f; } |
| 23 | }; |
| 24 | |
| 25 | |
| 26 | class DummySystemInfoService : public QSystemInformationService |
| 27 | { |
| 28 | public: |
| 29 | DummySystemInfoService() |
| 30 | : QSystemInformationService(nullptr, QStringLiteral("Dummy System Information Service")) |
| 31 | {} |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | class tst_QServiceLocator : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | public: |
| 40 | tst_QServiceLocator() : QObject() {} |
| 41 | ~tst_QServiceLocator() {} |
| 42 | |
| 43 | private slots: |
| 44 | void construction(); |
| 45 | void defaultServices(); |
| 46 | void addRemoveDefaultService(); |
| 47 | void addRemoveUserService(); |
| 48 | }; |
| 49 | |
| 50 | void tst_QServiceLocator::construction() |
| 51 | { |
| 52 | QServiceLocator locator; |
| 53 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount); |
| 54 | } |
| 55 | |
| 56 | void tst_QServiceLocator::defaultServices() |
| 57 | { |
| 58 | QServiceLocator locator; |
| 59 | QOpenGLInformationService *glInfo = locator.openGLInformation(); |
| 60 | QVERIFY(glInfo != nullptr); |
| 61 | QVERIFY(glInfo->description() == QStringLiteral("Null OpenGL Information Service")); |
| 62 | |
| 63 | QSystemInformationService *sysInfo = locator.systemInformation(); |
| 64 | QVERIFY(sysInfo != nullptr); |
| 65 | QVERIFY(sysInfo->description() == QStringLiteral("Default System Information Service")); |
| 66 | QVERIFY(sysInfo->threadPoolThreadCount() != 0); |
| 67 | } |
| 68 | |
| 69 | void tst_QServiceLocator::addRemoveDefaultService() |
| 70 | { |
| 71 | // Create locator and register a custom service that replaces a default service |
| 72 | QScopedPointer<DummySystemInfoService> dummy(new DummySystemInfoService); |
| 73 | QServiceLocator locator; |
| 74 | locator.registerServiceProvider(QServiceLocator::SystemInformation, dummy.data()); |
| 75 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount); |
| 76 | |
| 77 | // Get the service from the locator and check it works as expected |
| 78 | QSystemInformationService *service = locator.systemInformation(); |
| 79 | QVERIFY(service == dummy.data()); |
| 80 | |
| 81 | // Ensure the other default services work |
| 82 | QOpenGLInformationService *glInfo = locator.openGLInformation(); |
| 83 | QVERIFY(glInfo != nullptr); |
| 84 | QVERIFY(glInfo->description() == QStringLiteral("Null OpenGL Information Service")); |
| 85 | QVERIFY(glInfo->format() == QSurfaceFormat()); |
| 86 | |
| 87 | // Remove custom service |
| 88 | locator.unregisterServiceProvider(QServiceLocator::SystemInformation); |
| 89 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount); |
| 90 | |
| 91 | // Check the dummy service still exists |
| 92 | QVERIFY(!dummy.isNull()); |
| 93 | } |
| 94 | |
| 95 | void tst_QServiceLocator::addRemoveUserService() |
| 96 | { |
| 97 | // Create locator and register a custom service |
| 98 | QScopedPointer<DummyService> dummy(new DummyService); |
| 99 | QServiceLocator locator; |
| 100 | locator.registerServiceProvider(dummy->type(), dummy.data()); |
| 101 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount + 1); |
| 102 | |
| 103 | // Get the service from the locator and check it works as expected |
| 104 | DummyService *service = locator.service<DummyService>(dummy->type()); |
| 105 | QVERIFY(service == dummy.data()); |
| 106 | QVERIFY(service->data() == 10); |
| 107 | QVERIFY(qFuzzyCompare(service->moreData(), 3.14159f)); |
| 108 | |
| 109 | // Ensure the default services work |
| 110 | QSystemInformationService *sysInfo = locator.systemInformation(); |
| 111 | QVERIFY(sysInfo != nullptr); |
| 112 | QVERIFY(sysInfo->description() == QStringLiteral("Default System Information Service")); |
| 113 | QVERIFY(sysInfo->threadPoolThreadCount() != 0); |
| 114 | |
| 115 | // Remove custom service |
| 116 | locator.unregisterServiceProvider(dummy->type()); |
| 117 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount); |
| 118 | |
| 119 | // Check the dummy service still exists |
| 120 | QVERIFY(dummy->data() == 10); |
| 121 | } |
| 122 | |
| 123 | QTEST_MAIN(tst_QServiceLocator) |
| 124 | |
| 125 | #include "tst_qservicelocator.moc" |