| 1 | // Copyright (C) 2016 Paul Lemire <[email protected]> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | // TODO Remove in Qt6 |
| 5 | #include <QtCore/qcompilerdetection.h> |
| 6 | QT_WARNING_DISABLE_DEPRECATED |
| 7 | |
| 8 | #include <QtTest/QTest> |
| 9 | #include <Qt3DRender/qtexture.h> |
| 10 | #include <Qt3DRender/private/qtexture_p.h> |
| 11 | #include <QObject> |
| 12 | #include <QSignalSpy> |
| 13 | #include "testarbiter.h" |
| 14 | |
| 15 | class tst_QTextureLoader : public QObject |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | |
| 19 | private Q_SLOTS: |
| 20 | |
| 21 | void checkDefaultConstruction() |
| 22 | { |
| 23 | // GIVEN |
| 24 | Qt3DRender::QTextureLoader textureLoader; |
| 25 | |
| 26 | // THEN |
| 27 | QCOMPARE(textureLoader.source(), QUrl()); |
| 28 | QCOMPARE(textureLoader.isMirrored(), true); |
| 29 | QCOMPARE(textureLoader.target(), Qt3DRender::QTextureLoader::TargetAutomatic); |
| 30 | QCOMPARE(textureLoader.wrapMode()->x(), Qt3DRender::QTextureWrapMode::Repeat); |
| 31 | QCOMPARE(textureLoader.wrapMode()->y(), Qt3DRender::QTextureWrapMode::Repeat); |
| 32 | QCOMPARE(textureLoader.magnificationFilter(), Qt3DRender::QTextureLoader::Linear); |
| 33 | QCOMPARE(textureLoader.minificationFilter(), Qt3DRender::QTextureLoader::LinearMipMapLinear); |
| 34 | QCOMPARE(textureLoader.generateMipMaps(), true); |
| 35 | QCOMPARE(textureLoader.maximumAnisotropy(), 16.0f); |
| 36 | } |
| 37 | |
| 38 | void checkPropertyChanges() |
| 39 | { |
| 40 | // GIVEN |
| 41 | Qt3DRender::QTextureLoader textureLoader; |
| 42 | |
| 43 | { |
| 44 | // WHEN |
| 45 | QSignalSpy spy(&textureLoader, SIGNAL(sourceChanged(QUrl))); |
| 46 | const QUrl newValue(QStringLiteral("http://msn.com")); |
| 47 | textureLoader.setSource(newValue); |
| 48 | |
| 49 | // THEN |
| 50 | QVERIFY(spy.isValid()); |
| 51 | QCOMPARE(textureLoader.source(), newValue); |
| 52 | QCOMPARE(spy.size(), 1); |
| 53 | |
| 54 | // WHEN |
| 55 | spy.clear(); |
| 56 | textureLoader.setSource(newValue); |
| 57 | |
| 58 | // THEN |
| 59 | QCOMPARE(textureLoader.source(), newValue); |
| 60 | QCOMPARE(spy.size(), 0); |
| 61 | } |
| 62 | { |
| 63 | // WHEN |
| 64 | QSignalSpy spy(&textureLoader, SIGNAL(mirroredChanged(bool))); |
| 65 | const bool newValue = false; |
| 66 | textureLoader.setMirrored(newValue); |
| 67 | |
| 68 | // THEN |
| 69 | QVERIFY(spy.isValid()); |
| 70 | QCOMPARE(textureLoader.isMirrored(), newValue); |
| 71 | QCOMPARE(spy.size(), 1); |
| 72 | |
| 73 | // WHEN |
| 74 | spy.clear(); |
| 75 | textureLoader.setMirrored(newValue); |
| 76 | |
| 77 | // THEN |
| 78 | QCOMPARE(textureLoader.isMirrored(), newValue); |
| 79 | QCOMPARE(spy.size(), 0); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void checkSourceUpdate() |
| 84 | { |
| 85 | // GIVEN |
| 86 | TestArbiter arbiter; |
| 87 | Qt3DRender::QTextureLoader textureLoader; |
| 88 | arbiter.setArbiterOnNode(&textureLoader); |
| 89 | |
| 90 | { |
| 91 | // WHEN |
| 92 | textureLoader.setSource(QUrl(QStringLiteral("Gary"))); |
| 93 | QCoreApplication::processEvents(); |
| 94 | |
| 95 | // THEN |
| 96 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 97 | QCOMPARE(arbiter.dirtyNodes().front(), &textureLoader); |
| 98 | |
| 99 | Qt3DRender::QAbstractTexturePrivate *d = dynamic_cast<Qt3DRender::QAbstractTexturePrivate *>(Qt3DRender::QAbstractTexturePrivate::get(&textureLoader)); |
| 100 | const auto generator = qSharedPointerCast<Qt3DRender::QTextureFromSourceGenerator>(d->dataFunctor()); |
| 101 | QVERIFY(generator); |
| 102 | QCOMPARE(generator->url(), QUrl(QStringLiteral("Gary"))); |
| 103 | |
| 104 | arbiter.clear(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void checkMirroredUpdate() |
| 109 | { |
| 110 | // GIVEN |
| 111 | TestArbiter arbiter; |
| 112 | Qt3DRender::QTextureLoader textureLoader; |
| 113 | arbiter.setArbiterOnNode(&textureLoader); |
| 114 | |
| 115 | { |
| 116 | // WHEN |
| 117 | textureLoader.setMirrored(false); |
| 118 | QCoreApplication::processEvents(); |
| 119 | |
| 120 | // THEN |
| 121 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 122 | QCOMPARE(arbiter.dirtyNodes().front(), &textureLoader); |
| 123 | |
| 124 | Qt3DRender::QAbstractTexturePrivate *d = dynamic_cast<Qt3DRender::QAbstractTexturePrivate *>(Qt3DRender::QAbstractTexturePrivate::get(&textureLoader)); |
| 125 | const auto generator = qSharedPointerCast<Qt3DRender::QTextureFromSourceGenerator>(d->dataFunctor()); |
| 126 | QVERIFY(generator); |
| 127 | QCOMPARE(generator->isMirrored(), false); |
| 128 | |
| 129 | arbiter.clear(); |
| 130 | } |
| 131 | |
| 132 | { |
| 133 | // WHEN |
| 134 | textureLoader.setMirrored(false); |
| 135 | QCoreApplication::processEvents(); |
| 136 | |
| 137 | // THEN |
| 138 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | }; |
| 144 | |
| 145 | QTEST_MAIN(tst_QTextureLoader) |
| 146 | |
| 147 | #include "tst_qtextureloader.moc" |