| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 Paul Lemire <paul.lemire350@gmail.com> |
| 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 | |
| 30 | #include <QtTest/QTest> |
| 31 | #include <Qt3DRender/qfilterkey.h> |
| 32 | #include <Qt3DRender/private/qfilterkey_p.h> |
| 33 | #include <QObject> |
| 34 | #include <QSignalSpy> |
| 35 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
| 36 | #include <Qt3DCore/qnodecreatedchange.h> |
| 37 | #include "testpostmanarbiter.h" |
| 38 | |
| 39 | class tst_QFilterKey : public QObject |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | |
| 43 | private Q_SLOTS: |
| 44 | |
| 45 | void checkDefaultConstruction() |
| 46 | { |
| 47 | // GIVEN |
| 48 | Qt3DRender::QFilterKey filterKey; |
| 49 | |
| 50 | // THEN |
| 51 | QCOMPARE(filterKey.value(), QVariant()); |
| 52 | QCOMPARE(filterKey.name(), QString()); |
| 53 | } |
| 54 | |
| 55 | void checkPropertyChanges() |
| 56 | { |
| 57 | // GIVEN |
| 58 | Qt3DRender::QFilterKey filterKey; |
| 59 | |
| 60 | { |
| 61 | // WHEN |
| 62 | QSignalSpy spy(&filterKey, SIGNAL(valueChanged(QVariant))); |
| 63 | const QVariant newValue = QVariant("Tim" ); |
| 64 | filterKey.setValue(newValue); |
| 65 | |
| 66 | // THEN |
| 67 | QVERIFY(spy.isValid()); |
| 68 | QCOMPARE(filterKey.value(), newValue); |
| 69 | QCOMPARE(spy.count(), 1); |
| 70 | |
| 71 | // WHEN |
| 72 | spy.clear(); |
| 73 | filterKey.setValue(newValue); |
| 74 | |
| 75 | // THEN |
| 76 | QCOMPARE(filterKey.value(), newValue); |
| 77 | QCOMPARE(spy.count(), 0); |
| 78 | } |
| 79 | { |
| 80 | // WHEN |
| 81 | QSignalSpy spy(&filterKey, SIGNAL(nameChanged(QString))); |
| 82 | const QString newValue = QStringLiteral("Darius" ); |
| 83 | filterKey.setName(newValue); |
| 84 | |
| 85 | // THEN |
| 86 | QVERIFY(spy.isValid()); |
| 87 | QCOMPARE(filterKey.name(), newValue); |
| 88 | QCOMPARE(spy.count(), 1); |
| 89 | |
| 90 | // WHEN |
| 91 | spy.clear(); |
| 92 | filterKey.setName(newValue); |
| 93 | |
| 94 | // THEN |
| 95 | QCOMPARE(filterKey.name(), newValue); |
| 96 | QCOMPARE(spy.count(), 0); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void checkCreationData() |
| 101 | { |
| 102 | // GIVEN |
| 103 | Qt3DRender::QFilterKey filterKey; |
| 104 | |
| 105 | filterKey.setValue(QVariant(QStringLiteral("Taylor" ))); |
| 106 | filterKey.setName(QStringLiteral("Craig" )); |
| 107 | |
| 108 | // WHEN |
| 109 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
| 110 | |
| 111 | { |
| 112 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&filterKey); |
| 113 | creationChanges = creationChangeGenerator.creationChanges(); |
| 114 | } |
| 115 | |
| 116 | // THEN |
| 117 | { |
| 118 | QCOMPARE(creationChanges.size(), 1); |
| 119 | |
| 120 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QFilterKeyData>>(src: creationChanges.first()); |
| 121 | const Qt3DRender::QFilterKeyData cloneData = creationChangeData->data; |
| 122 | |
| 123 | QCOMPARE(filterKey.value(), cloneData.value); |
| 124 | QCOMPARE(filterKey.name(), cloneData.name); |
| 125 | QCOMPARE(filterKey.id(), creationChangeData->subjectId()); |
| 126 | QCOMPARE(filterKey.isEnabled(), true); |
| 127 | QCOMPARE(filterKey.isEnabled(), creationChangeData->isNodeEnabled()); |
| 128 | QCOMPARE(filterKey.metaObject(), creationChangeData->metaObject()); |
| 129 | } |
| 130 | |
| 131 | // WHEN |
| 132 | filterKey.setEnabled(false); |
| 133 | |
| 134 | { |
| 135 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&filterKey); |
| 136 | creationChanges = creationChangeGenerator.creationChanges(); |
| 137 | } |
| 138 | |
| 139 | // THEN |
| 140 | { |
| 141 | QCOMPARE(creationChanges.size(), 1); |
| 142 | |
| 143 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QFilterKeyData>>(src: creationChanges.first()); |
| 144 | const Qt3DRender::QFilterKeyData cloneData = creationChangeData->data; |
| 145 | |
| 146 | QCOMPARE(filterKey.value(), cloneData.value); |
| 147 | QCOMPARE(filterKey.name(), cloneData.name); |
| 148 | QCOMPARE(filterKey.id(), creationChangeData->subjectId()); |
| 149 | QCOMPARE(filterKey.isEnabled(), false); |
| 150 | QCOMPARE(filterKey.isEnabled(), creationChangeData->isNodeEnabled()); |
| 151 | QCOMPARE(filterKey.metaObject(), creationChangeData->metaObject()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void checkValueUpdate() |
| 156 | { |
| 157 | // GIVEN |
| 158 | TestArbiter arbiter; |
| 159 | Qt3DRender::QFilterKey filterKey; |
| 160 | arbiter.setArbiterOnNode(&filterKey); |
| 161 | |
| 162 | { |
| 163 | // WHEN |
| 164 | filterKey.setValue(QVariant(427)); |
| 165 | QCoreApplication::processEvents(); |
| 166 | |
| 167 | // THEN |
| 168 | QCOMPARE(arbiter.events.size(), 0); |
| 169 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 170 | QCOMPARE(arbiter.dirtyNodes.front(), &filterKey); |
| 171 | |
| 172 | arbiter.dirtyNodes.clear(); |
| 173 | } |
| 174 | |
| 175 | { |
| 176 | // WHEN |
| 177 | filterKey.setValue(QVariant(427)); |
| 178 | QCoreApplication::processEvents(); |
| 179 | |
| 180 | // THEN |
| 181 | QCOMPARE(arbiter.events.size(), 0); |
| 182 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | void checkNameUpdate() |
| 188 | { |
| 189 | // GIVEN |
| 190 | TestArbiter arbiter; |
| 191 | Qt3DRender::QFilterKey filterKey; |
| 192 | arbiter.setArbiterOnNode(&filterKey); |
| 193 | |
| 194 | { |
| 195 | // WHEN |
| 196 | filterKey.setName(QStringLiteral("Easton" )); |
| 197 | QCoreApplication::processEvents(); |
| 198 | |
| 199 | // THEN |
| 200 | QCOMPARE(arbiter.events.size(), 0); |
| 201 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 202 | QCOMPARE(arbiter.dirtyNodes.front(), &filterKey); |
| 203 | |
| 204 | arbiter.dirtyNodes.clear(); |
| 205 | } |
| 206 | |
| 207 | { |
| 208 | // WHEN |
| 209 | filterKey.setName(QStringLiteral("Easton" )); |
| 210 | QCoreApplication::processEvents(); |
| 211 | |
| 212 | // THEN |
| 213 | QCOMPARE(arbiter.events.size(), 0); |
| 214 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 215 | } |
| 216 | |
| 217 | } |
| 218 | |
| 219 | }; |
| 220 | |
| 221 | QTEST_MAIN(tst_QFilterKey) |
| 222 | |
| 223 | #include "tst_qfilterkey.moc" |
| 224 | |