| 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/qaspectfactory_p.h> |
| 6 | #include <Qt3DCore/QAbstractAspect> |
| 7 | |
| 8 | using namespace QT_PREPEND_NAMESPACE(Qt3DCore); |
| 9 | |
| 10 | #define FAKE_ASPECT(ClassName) \ |
| 11 | class ClassName : public QAbstractAspect \ |
| 12 | { \ |
| 13 | Q_OBJECT \ |
| 14 | public: \ |
| 15 | explicit ClassName(QObject *parent = 0) \ |
| 16 | : QAbstractAspect(parent) {} \ |
| 17 | \ |
| 18 | private: \ |
| 19 | void onRegistered() override {} \ |
| 20 | void onEngineStartup() override {} \ |
| 21 | void onEngineShutdown() override {} \ |
| 22 | \ |
| 23 | std::vector<QAspectJobPtr> jobsToExecute(qint64) override \ |
| 24 | { \ |
| 25 | return std::vector<QAspectJobPtr>(); \ |
| 26 | } \ |
| 27 | }; |
| 28 | |
| 29 | FAKE_ASPECT(DefaultFakeAspect) |
| 30 | FAKE_ASPECT(AnotherFakeAspect) |
| 31 | |
| 32 | QT3D_REGISTER_ASPECT("default", DefaultFakeAspect) |
| 33 | |
| 34 | class tst_QAspectFactory : public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | private Q_SLOTS: |
| 38 | void shoulHaveDefaultState() |
| 39 | { |
| 40 | // GIVEN |
| 41 | QAspectFactory factory; |
| 42 | |
| 43 | // THEN |
| 44 | QCOMPARE(factory.availableFactories().size(), 2); |
| 45 | // order is not deterministic |
| 46 | QVERIFY(factory.availableFactories().contains(QLatin1String("core"))); |
| 47 | QVERIFY(factory.availableFactories().contains(QLatin1String("default"))); |
| 48 | |
| 49 | // WHEN |
| 50 | QAbstractAspect *aspect = factory.createAspect(QLatin1String("default")); |
| 51 | |
| 52 | // THEN |
| 53 | QVERIFY(qobject_cast<DefaultFakeAspect*>(aspect) != nullptr); |
| 54 | QVERIFY(aspect->parent() == nullptr); |
| 55 | } |
| 56 | |
| 57 | void shouldKnowAspectNames() |
| 58 | { |
| 59 | // GIVEN |
| 60 | QAspectFactory factory; |
| 61 | |
| 62 | // WHEN |
| 63 | DefaultFakeAspect fake; |
| 64 | AnotherFakeAspect missing; |
| 65 | |
| 66 | // THEN |
| 67 | QCOMPARE(factory.aspectName(&fake), QLatin1String("default")); |
| 68 | QCOMPARE(factory.aspectName(&missing), QLatin1String()); |
| 69 | } |
| 70 | |
| 71 | void shouldGracefulyHandleMissingFactories() |
| 72 | { |
| 73 | // GIVEN |
| 74 | QAspectFactory factory; |
| 75 | |
| 76 | // WHEN |
| 77 | QAbstractAspect *aspect = factory.createAspect(QLatin1String("missing"), this); |
| 78 | |
| 79 | // THEN |
| 80 | QVERIFY(qobject_cast<AnotherFakeAspect*>(aspect) == nullptr); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | QTEST_MAIN(tst_QAspectFactory) |
| 85 | |
| 86 | #include "tst_qaspectfactory.moc" |