| 1 | // Copyright (C) 2017 Paul Lemire <[email protected]> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include <QtTest/QTest> |
| 6 | #include <Qt3DAnimation/qlerpclipblend.h> |
| 7 | #include <Qt3DAnimation/qanimationcliploader.h> |
| 8 | #include <Qt3DAnimation/private/qlerpclipblend_p.h> |
| 9 | #include <QObject> |
| 10 | #include <QSignalSpy> |
| 11 | #include "testarbiter.h" |
| 12 | |
| 13 | class tst_QLerpClipBlend : public QObject |
| 14 | { |
| 15 | Q_OBJECT |
| 16 | |
| 17 | private Q_SLOTS: |
| 18 | void initTestCase() |
| 19 | { |
| 20 | qRegisterMetaType<Qt3DAnimation::QAbstractClipBlendNode*>(); |
| 21 | } |
| 22 | |
| 23 | void checkDefaultConstruction() |
| 24 | { |
| 25 | // GIVEN |
| 26 | Qt3DAnimation::QLerpClipBlend lerpBlend; |
| 27 | |
| 28 | // THEN |
| 29 | QCOMPARE(lerpBlend.blendFactor(), 0.0f); |
| 30 | QCOMPARE(lerpBlend.startClip(), static_cast<Qt3DAnimation::QAbstractClipBlendNode *>(nullptr)); |
| 31 | QCOMPARE(lerpBlend.endClip(), static_cast<Qt3DAnimation::QAbstractClipBlendNode *>(nullptr)); |
| 32 | } |
| 33 | |
| 34 | void checkPropertyChanges() |
| 35 | { |
| 36 | // GIVEN |
| 37 | Qt3DAnimation::QLerpClipBlend lerpBlend; |
| 38 | |
| 39 | { |
| 40 | // WHEN |
| 41 | QSignalSpy spy(&lerpBlend, SIGNAL(blendFactorChanged(float))); |
| 42 | const float newValue = 0.5f; |
| 43 | lerpBlend.setBlendFactor(newValue); |
| 44 | |
| 45 | // THEN |
| 46 | QVERIFY(spy.isValid()); |
| 47 | QCOMPARE(lerpBlend.blendFactor(), newValue); |
| 48 | QCOMPARE(spy.size(), 1); |
| 49 | |
| 50 | // WHEN |
| 51 | spy.clear(); |
| 52 | lerpBlend.setBlendFactor(newValue); |
| 53 | |
| 54 | // THEN |
| 55 | QCOMPARE(lerpBlend.blendFactor(), newValue); |
| 56 | QCOMPARE(spy.size(), 0); |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | // WHEN |
| 61 | QSignalSpy spy(&lerpBlend, SIGNAL(startClipChanged(Qt3DAnimation::QAbstractClipBlendNode*))); |
| 62 | auto newValue = new Qt3DAnimation::QLerpClipBlend(); |
| 63 | lerpBlend.setStartClip(newValue); |
| 64 | |
| 65 | // THEN |
| 66 | QVERIFY(spy.isValid()); |
| 67 | QCOMPARE(lerpBlend.startClip(), newValue); |
| 68 | QCOMPARE(spy.size(), 1); |
| 69 | |
| 70 | // WHEN |
| 71 | spy.clear(); |
| 72 | lerpBlend.setStartClip(newValue); |
| 73 | |
| 74 | // THEN |
| 75 | QCOMPARE(lerpBlend.startClip(), newValue); |
| 76 | QCOMPARE(spy.size(), 0); |
| 77 | } |
| 78 | |
| 79 | { |
| 80 | // WHEN |
| 81 | QSignalSpy spy(&lerpBlend, SIGNAL(endClipChanged(Qt3DAnimation::QAbstractClipBlendNode*))); |
| 82 | auto newValue = new Qt3DAnimation::QLerpClipBlend(); |
| 83 | lerpBlend.setEndClip(newValue); |
| 84 | |
| 85 | // THEN |
| 86 | QVERIFY(spy.isValid()); |
| 87 | QCOMPARE(lerpBlend.endClip(), newValue); |
| 88 | QCOMPARE(spy.size(), 1); |
| 89 | |
| 90 | // WHEN |
| 91 | spy.clear(); |
| 92 | lerpBlend.setEndClip(newValue); |
| 93 | |
| 94 | // THEN |
| 95 | QCOMPARE(lerpBlend.endClip(), newValue); |
| 96 | QCOMPARE(spy.size(), 0); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void checkBlendFactorUpdate() |
| 101 | { |
| 102 | // GIVEN |
| 103 | TestArbiter arbiter; |
| 104 | Qt3DAnimation::QLerpClipBlend lerpBlend; |
| 105 | arbiter.setArbiterOnNode(&lerpBlend); |
| 106 | |
| 107 | { |
| 108 | // WHEN |
| 109 | lerpBlend.setBlendFactor(0.4f); |
| 110 | |
| 111 | // THEN |
| 112 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 113 | QCOMPARE(arbiter.dirtyNodes().front(), &lerpBlend); |
| 114 | |
| 115 | arbiter.clear(); |
| 116 | } |
| 117 | |
| 118 | { |
| 119 | // WHEN |
| 120 | lerpBlend.setBlendFactor(0.4f); |
| 121 | |
| 122 | // THEN |
| 123 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | |
| 128 | void checkStartClipUpdate() |
| 129 | { |
| 130 | // GIVEN |
| 131 | TestArbiter arbiter; |
| 132 | Qt3DAnimation::QLerpClipBlend lerpBlend; |
| 133 | arbiter.setArbiterOnNode(&lerpBlend); |
| 134 | auto startClip = new Qt3DAnimation::QLerpClipBlend(); |
| 135 | |
| 136 | { |
| 137 | // WHEN |
| 138 | lerpBlend.setStartClip(startClip); |
| 139 | |
| 140 | // THEN |
| 141 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 142 | QCOMPARE(arbiter.dirtyNodes().front(), &lerpBlend); |
| 143 | |
| 144 | arbiter.clear(); |
| 145 | } |
| 146 | |
| 147 | { |
| 148 | // WHEN |
| 149 | lerpBlend.setStartClip(startClip); |
| 150 | |
| 151 | // THEN |
| 152 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void checkEndClipUpdate() |
| 157 | { |
| 158 | // GIVEN |
| 159 | TestArbiter arbiter; |
| 160 | Qt3DAnimation::QLerpClipBlend lerpBlend; |
| 161 | arbiter.setArbiterOnNode(&lerpBlend); |
| 162 | auto endClip = new Qt3DAnimation::QLerpClipBlend(); |
| 163 | |
| 164 | { |
| 165 | // WHEN |
| 166 | lerpBlend.setEndClip(endClip); |
| 167 | |
| 168 | // THEN |
| 169 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 170 | QCOMPARE(arbiter.dirtyNodes().front(), &lerpBlend); |
| 171 | |
| 172 | arbiter.clear(); |
| 173 | } |
| 174 | |
| 175 | { |
| 176 | // WHEN |
| 177 | lerpBlend.setEndClip(endClip); |
| 178 | |
| 179 | // THEN |
| 180 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void checkStartClipBookkeeping() |
| 185 | { |
| 186 | // GIVEN |
| 187 | QScopedPointer<Qt3DAnimation::QLerpClipBlend> lerpBlend(new Qt3DAnimation::QLerpClipBlend); |
| 188 | { |
| 189 | // WHEN |
| 190 | Qt3DAnimation::QLerpClipBlend clip; |
| 191 | lerpBlend->setStartClip(&clip); |
| 192 | |
| 193 | // THEN |
| 194 | QCOMPARE(clip.parent(), lerpBlend.data()); |
| 195 | QCOMPARE(lerpBlend->startClip(), &clip); |
| 196 | } |
| 197 | // THEN (Should not crash and clip be unset) |
| 198 | QVERIFY(lerpBlend->startClip() == nullptr); |
| 199 | |
| 200 | { |
| 201 | // WHEN |
| 202 | Qt3DAnimation::QLerpClipBlend someOtherLerpBlend; |
| 203 | QScopedPointer<Qt3DAnimation::QLerpClipBlend> clip(new Qt3DAnimation::QLerpClipBlend(&someOtherLerpBlend)); |
| 204 | lerpBlend->setStartClip(clip.data()); |
| 205 | |
| 206 | // THEN |
| 207 | QCOMPARE(clip->parent(), &someOtherLerpBlend); |
| 208 | QCOMPARE(lerpBlend->startClip(), clip.data()); |
| 209 | |
| 210 | // WHEN |
| 211 | lerpBlend.reset(); |
| 212 | clip.reset(); |
| 213 | |
| 214 | // THEN Should not crash when the effect is destroyed (tests for failed removal of destruction helper) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void checkEndClipBookkeeping() |
| 219 | { |
| 220 | // GIVEN |
| 221 | QScopedPointer<Qt3DAnimation::QLerpClipBlend> lerpBlend(new Qt3DAnimation::QLerpClipBlend); |
| 222 | { |
| 223 | // WHEN |
| 224 | Qt3DAnimation::QLerpClipBlend clip; |
| 225 | lerpBlend->setEndClip(&clip); |
| 226 | |
| 227 | // THEN |
| 228 | QCOMPARE(clip.parent(), lerpBlend.data()); |
| 229 | QCOMPARE(lerpBlend->endClip(), &clip); |
| 230 | } |
| 231 | // THEN (Should not crash and clip be unset) |
| 232 | QVERIFY(lerpBlend->endClip() == nullptr); |
| 233 | |
| 234 | { |
| 235 | // WHEN |
| 236 | Qt3DAnimation::QLerpClipBlend someOtherLerpBlend; |
| 237 | QScopedPointer<Qt3DAnimation::QLerpClipBlend> clip(new Qt3DAnimation::QLerpClipBlend(&someOtherLerpBlend)); |
| 238 | lerpBlend->setEndClip(clip.data()); |
| 239 | |
| 240 | // THEN |
| 241 | QCOMPARE(clip->parent(), &someOtherLerpBlend); |
| 242 | QCOMPARE(lerpBlend->endClip(), clip.data()); |
| 243 | |
| 244 | // WHEN |
| 245 | lerpBlend.reset(); |
| 246 | clip.reset(); |
| 247 | |
| 248 | // THEN Should not crash when the effect is destroyed (tests for failed removal of destruction helper) |
| 249 | } |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | QTEST_MAIN(tst_QLerpClipBlend) |
| 254 | |
| 255 | #include "tst_qlerpclipblend.moc" |