| 1 | /**************************************************************************** |
|---|---|
| 2 | ** |
| 3 | ** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D 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 | #include <QtTest/QtTest> |
| 30 | #include <Qt3DCore/private/qservicelocator_p.h> |
| 31 | #include <Qt3DCore/private/qopenglinformationservice_p.h> |
| 32 | #include <Qt3DCore/private/qsysteminformationservice_p.h> |
| 33 | |
| 34 | #include <QScopedPointer> |
| 35 | |
| 36 | using namespace Qt3DCore; |
| 37 | |
| 38 | class DummyService : public QAbstractServiceProvider |
| 39 | { |
| 40 | public: |
| 41 | DummyService() |
| 42 | : QAbstractServiceProvider(QServiceLocator::UserService + 1, |
| 43 | QStringLiteral("Dummy Service")) |
| 44 | {} |
| 45 | |
| 46 | int data() const { return 10; } |
| 47 | float moreData() const { return 3.14159f; } |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | class DummySystemInfoService : public QSystemInformationService |
| 52 | { |
| 53 | public: |
| 54 | DummySystemInfoService() |
| 55 | : QSystemInformationService(nullptr, QStringLiteral("Dummy System Information Service")) |
| 56 | {} |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | class tst_QServiceLocator : public QObject |
| 61 | { |
| 62 | Q_OBJECT |
| 63 | |
| 64 | public: |
| 65 | tst_QServiceLocator() : QObject() {} |
| 66 | ~tst_QServiceLocator() {} |
| 67 | |
| 68 | private slots: |
| 69 | void construction(); |
| 70 | void defaultServices(); |
| 71 | void addRemoveDefaultService(); |
| 72 | void addRemoveUserService(); |
| 73 | }; |
| 74 | |
| 75 | void tst_QServiceLocator::construction() |
| 76 | { |
| 77 | QServiceLocator locator; |
| 78 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount); |
| 79 | } |
| 80 | |
| 81 | void tst_QServiceLocator::defaultServices() |
| 82 | { |
| 83 | QServiceLocator locator; |
| 84 | QOpenGLInformationService *glInfo = locator.openGLInformation(); |
| 85 | QVERIFY(glInfo != nullptr); |
| 86 | QVERIFY(glInfo->description() == QStringLiteral("Null OpenGL Information Service")); |
| 87 | |
| 88 | QSystemInformationService *sysInfo = locator.systemInformation(); |
| 89 | QVERIFY(sysInfo != nullptr); |
| 90 | QVERIFY(sysInfo->description() == QStringLiteral("Default System Information Service")); |
| 91 | QVERIFY(sysInfo->threadPoolThreadCount() != 0); |
| 92 | } |
| 93 | |
| 94 | void tst_QServiceLocator::addRemoveDefaultService() |
| 95 | { |
| 96 | // Create locator and register a custom service that replaces a default service |
| 97 | QScopedPointer<DummySystemInfoService> dummy(new DummySystemInfoService); |
| 98 | QServiceLocator locator; |
| 99 | locator.registerServiceProvider(serviceType: QServiceLocator::SystemInformation, provider: dummy.data()); |
| 100 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount); |
| 101 | |
| 102 | // Get the service from the locator and check it works as expected |
| 103 | QSystemInformationService *service = locator.systemInformation(); |
| 104 | QVERIFY(service == dummy.data()); |
| 105 | |
| 106 | // Ensure the other default services work |
| 107 | QOpenGLInformationService *glInfo = locator.openGLInformation(); |
| 108 | QVERIFY(glInfo != nullptr); |
| 109 | QVERIFY(glInfo->description() == QStringLiteral("Null OpenGL Information Service")); |
| 110 | QVERIFY(glInfo->format() == QSurfaceFormat()); |
| 111 | |
| 112 | // Remove custom service |
| 113 | locator.unregisterServiceProvider(serviceType: QServiceLocator::SystemInformation); |
| 114 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount); |
| 115 | |
| 116 | // Check the dummy service still exists |
| 117 | QVERIFY(!dummy.isNull()); |
| 118 | } |
| 119 | |
| 120 | void tst_QServiceLocator::addRemoveUserService() |
| 121 | { |
| 122 | // Create locator and register a custom service |
| 123 | QScopedPointer<DummyService> dummy(new DummyService); |
| 124 | QServiceLocator locator; |
| 125 | locator.registerServiceProvider(serviceType: dummy->type(), provider: dummy.data()); |
| 126 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount + 1); |
| 127 | |
| 128 | // Get the service from the locator and check it works as expected |
| 129 | DummyService *service = locator.service<DummyService>(serviceType: dummy->type()); |
| 130 | QVERIFY(service == dummy.data()); |
| 131 | QVERIFY(service->data() == 10); |
| 132 | QVERIFY(qFuzzyCompare(service->moreData(), 3.14159f)); |
| 133 | |
| 134 | // Ensure the default services work |
| 135 | QSystemInformationService *sysInfo = locator.systemInformation(); |
| 136 | QVERIFY(sysInfo != nullptr); |
| 137 | QVERIFY(sysInfo->description() == QStringLiteral("Default System Information Service")); |
| 138 | QVERIFY(sysInfo->threadPoolThreadCount() != 0); |
| 139 | |
| 140 | // Remove custom service |
| 141 | locator.unregisterServiceProvider(serviceType: dummy->type()); |
| 142 | QVERIFY(locator.serviceCount() == QServiceLocator::DefaultServiceCount); |
| 143 | |
| 144 | // Check the dummy service still exists |
| 145 | QVERIFY(dummy->data() == 10); |
| 146 | } |
| 147 | |
| 148 | QTEST_MAIN(tst_QServiceLocator) |
| 149 | |
| 150 | #include "tst_qservicelocator.moc" |
| 151 |
