| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 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:LGPL$ |
| 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 Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | |
| 41 | #include <QtTest/QTest> |
| 42 | #include <Qt3DRender/qwaitfence.h> |
| 43 | #include <Qt3DRender/private/qwaitfence_p.h> |
| 44 | #include <QObject> |
| 45 | #include <QSignalSpy> |
| 46 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
| 47 | #include <Qt3DCore/qnodecreatedchange.h> |
| 48 | #include "testpostmanarbiter.h" |
| 49 | |
| 50 | class tst_QWaitFence : public QObject |
| 51 | { |
| 52 | Q_OBJECT |
| 53 | |
| 54 | private Q_SLOTS: |
| 55 | |
| 56 | void initTestCase() |
| 57 | { |
| 58 | qRegisterMetaType<Qt3DRender::QWaitFence::HandleType>(typeName: "HandleType" ); |
| 59 | } |
| 60 | |
| 61 | void checkDefaultConstruction() |
| 62 | { |
| 63 | // GIVEN |
| 64 | Qt3DRender::QWaitFence waitFence; |
| 65 | |
| 66 | // THEN |
| 67 | QCOMPARE(waitFence.handleType(), Qt3DRender::QWaitFence::NoHandle); |
| 68 | QCOMPARE(waitFence.handle(), QVariant()); |
| 69 | QCOMPARE(waitFence.waitOnCPU(), false); |
| 70 | QCOMPARE(waitFence.timeout(), quint64(-1)); |
| 71 | } |
| 72 | |
| 73 | void checkPropertyChanges() |
| 74 | { |
| 75 | // GIVEN |
| 76 | Qt3DRender::QWaitFence waitFence; |
| 77 | |
| 78 | { |
| 79 | // WHEN |
| 80 | QSignalSpy spy(&waitFence, SIGNAL(handleTypeChanged(HandleType))); |
| 81 | const Qt3DRender::QWaitFence::HandleType newValue = Qt3DRender::QWaitFence::OpenGLFenceId; |
| 82 | waitFence.setHandleType(newValue); |
| 83 | |
| 84 | // THEN |
| 85 | QVERIFY(spy.isValid()); |
| 86 | QCOMPARE(waitFence.handleType(), newValue); |
| 87 | QCOMPARE(spy.count(), 1); |
| 88 | |
| 89 | // WHEN |
| 90 | spy.clear(); |
| 91 | waitFence.setHandleType(newValue); |
| 92 | |
| 93 | // THEN |
| 94 | QCOMPARE(waitFence.handleType(), newValue); |
| 95 | QCOMPARE(spy.count(), 0); |
| 96 | } |
| 97 | { |
| 98 | // WHEN |
| 99 | QSignalSpy spy(&waitFence, SIGNAL(handleChanged(QVariant))); |
| 100 | const QVariant newValue(883); |
| 101 | waitFence.setHandle(newValue); |
| 102 | |
| 103 | // THEN |
| 104 | QVERIFY(spy.isValid()); |
| 105 | QCOMPARE(waitFence.handle(), newValue); |
| 106 | QCOMPARE(spy.count(), 1); |
| 107 | |
| 108 | // WHEN |
| 109 | spy.clear(); |
| 110 | waitFence.setHandle(newValue); |
| 111 | |
| 112 | // THEN |
| 113 | QCOMPARE(waitFence.handle(), newValue); |
| 114 | QCOMPARE(spy.count(), 0); |
| 115 | } |
| 116 | { |
| 117 | // WHEN |
| 118 | QSignalSpy spy(&waitFence, SIGNAL(waitOnCPUChanged(bool))); |
| 119 | const bool newValue = true; |
| 120 | waitFence.setWaitOnCPU(newValue); |
| 121 | |
| 122 | // THEN |
| 123 | QVERIFY(spy.isValid()); |
| 124 | QCOMPARE(waitFence.waitOnCPU(), newValue); |
| 125 | QCOMPARE(spy.count(), 1); |
| 126 | |
| 127 | // WHEN |
| 128 | spy.clear(); |
| 129 | waitFence.setWaitOnCPU(newValue); |
| 130 | |
| 131 | // THEN |
| 132 | QCOMPARE(waitFence.waitOnCPU(), newValue); |
| 133 | QCOMPARE(spy.count(), 0); |
| 134 | } |
| 135 | { |
| 136 | // WHEN |
| 137 | QSignalSpy spy(&waitFence, SIGNAL(timeoutChanged(quint64))); |
| 138 | const quint64 newValue = 984; |
| 139 | waitFence.setTimeout(newValue); |
| 140 | |
| 141 | // THEN |
| 142 | QVERIFY(spy.isValid()); |
| 143 | QCOMPARE(waitFence.timeout(), newValue); |
| 144 | QCOMPARE(spy.count(), 1); |
| 145 | |
| 146 | // WHEN |
| 147 | spy.clear(); |
| 148 | waitFence.setTimeout(newValue); |
| 149 | |
| 150 | // THEN |
| 151 | QCOMPARE(waitFence.timeout(), newValue); |
| 152 | QCOMPARE(spy.count(), 0); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void checkCreationData() |
| 157 | { |
| 158 | // GIVEN |
| 159 | Qt3DRender::QWaitFence waitFence; |
| 160 | |
| 161 | waitFence.setHandleType(Qt3DRender::QWaitFence::OpenGLFenceId); |
| 162 | waitFence.setHandle(QVariant(1200)); |
| 163 | waitFence.setWaitOnCPU(true); |
| 164 | waitFence.setTimeout(1584); |
| 165 | |
| 166 | // WHEN |
| 167 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; |
| 168 | |
| 169 | { |
| 170 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&waitFence); |
| 171 | creationChanges = creationChangeGenerator.creationChanges(); |
| 172 | } |
| 173 | |
| 174 | // THEN |
| 175 | { |
| 176 | QCOMPARE(creationChanges.size(), 1); |
| 177 | |
| 178 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QWaitFenceData>>(src: creationChanges.first()); |
| 179 | const Qt3DRender::QWaitFenceData cloneData = creationChangeData->data; |
| 180 | |
| 181 | QCOMPARE(waitFence.handleType(), cloneData.handleType); |
| 182 | QCOMPARE(waitFence.handle(), cloneData.handle); |
| 183 | QCOMPARE(waitFence.waitOnCPU(), cloneData.waitOnCPU); |
| 184 | QCOMPARE(waitFence.timeout(), cloneData.timeout); |
| 185 | QCOMPARE(waitFence.id(), creationChangeData->subjectId()); |
| 186 | QCOMPARE(waitFence.isEnabled(), true); |
| 187 | QCOMPARE(waitFence.isEnabled(), creationChangeData->isNodeEnabled()); |
| 188 | QCOMPARE(waitFence.metaObject(), creationChangeData->metaObject()); |
| 189 | } |
| 190 | |
| 191 | // WHEN |
| 192 | waitFence.setEnabled(false); |
| 193 | |
| 194 | { |
| 195 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&waitFence); |
| 196 | creationChanges = creationChangeGenerator.creationChanges(); |
| 197 | } |
| 198 | |
| 199 | // THEN |
| 200 | { |
| 201 | QCOMPARE(creationChanges.size(), 1); |
| 202 | |
| 203 | const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QWaitFenceData>>(src: creationChanges.first()); |
| 204 | const Qt3DRender::QWaitFenceData cloneData = creationChangeData->data; |
| 205 | |
| 206 | QCOMPARE(waitFence.handleType(), cloneData.handleType); |
| 207 | QCOMPARE(waitFence.handle(), cloneData.handle); |
| 208 | QCOMPARE(waitFence.waitOnCPU(), cloneData.waitOnCPU); |
| 209 | QCOMPARE(waitFence.timeout(), cloneData.timeout); |
| 210 | QCOMPARE(waitFence.id(), creationChangeData->subjectId()); |
| 211 | QCOMPARE(waitFence.isEnabled(), false); |
| 212 | QCOMPARE(waitFence.isEnabled(), creationChangeData->isNodeEnabled()); |
| 213 | QCOMPARE(waitFence.metaObject(), creationChangeData->metaObject()); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void checkHandleTypeUpdate() |
| 218 | { |
| 219 | // GIVEN |
| 220 | TestArbiter arbiter; |
| 221 | Qt3DRender::QWaitFence waitFence; |
| 222 | arbiter.setArbiterOnNode(&waitFence); |
| 223 | |
| 224 | { |
| 225 | // WHEN |
| 226 | waitFence.setHandleType(Qt3DRender::QWaitFence::OpenGLFenceId); |
| 227 | QCoreApplication::processEvents(); |
| 228 | |
| 229 | // THEN |
| 230 | QCOMPARE(arbiter.events.size(), 0); |
| 231 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 232 | QCOMPARE(arbiter.dirtyNodes.front(), &waitFence); |
| 233 | |
| 234 | arbiter.dirtyNodes.clear(); |
| 235 | } |
| 236 | |
| 237 | { |
| 238 | // WHEN |
| 239 | waitFence.setHandleType(Qt3DRender::QWaitFence::OpenGLFenceId); |
| 240 | QCoreApplication::processEvents(); |
| 241 | |
| 242 | // THEN |
| 243 | QCOMPARE(arbiter.events.size(), 0); |
| 244 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void checkHandleUpdate() |
| 249 | { |
| 250 | // GIVEN |
| 251 | TestArbiter arbiter; |
| 252 | Qt3DRender::QWaitFence waitFence; |
| 253 | arbiter.setArbiterOnNode(&waitFence); |
| 254 | |
| 255 | { |
| 256 | // WHEN |
| 257 | waitFence.setHandle(QVariant(883)); |
| 258 | QCoreApplication::processEvents(); |
| 259 | |
| 260 | // THEN |
| 261 | QCOMPARE(arbiter.events.size(), 0); |
| 262 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 263 | QCOMPARE(arbiter.dirtyNodes.front(), &waitFence); |
| 264 | |
| 265 | arbiter.dirtyNodes.clear(); |
| 266 | } |
| 267 | |
| 268 | { |
| 269 | // WHEN |
| 270 | waitFence.setHandle(QVariant(883)); |
| 271 | QCoreApplication::processEvents(); |
| 272 | |
| 273 | // THEN |
| 274 | QCOMPARE(arbiter.events.size(), 0); |
| 275 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void checkWaitOnCPUUpdate() |
| 280 | { |
| 281 | // GIVEN |
| 282 | TestArbiter arbiter; |
| 283 | Qt3DRender::QWaitFence waitFence; |
| 284 | arbiter.setArbiterOnNode(&waitFence); |
| 285 | |
| 286 | { |
| 287 | // WHEN |
| 288 | waitFence.setWaitOnCPU(true); |
| 289 | QCoreApplication::processEvents(); |
| 290 | |
| 291 | // THEN |
| 292 | QCOMPARE(arbiter.events.size(), 0); |
| 293 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 294 | QCOMPARE(arbiter.dirtyNodes.front(), &waitFence); |
| 295 | |
| 296 | arbiter.dirtyNodes.clear(); |
| 297 | } |
| 298 | |
| 299 | { |
| 300 | // WHEN |
| 301 | waitFence.setWaitOnCPU(true); |
| 302 | QCoreApplication::processEvents(); |
| 303 | |
| 304 | // THEN |
| 305 | QCOMPARE(arbiter.events.size(), 0); |
| 306 | QCOMPARE(arbiter.events.size(), 0); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void checkTimeoutUpdate() |
| 311 | { |
| 312 | // GIVEN |
| 313 | TestArbiter arbiter; |
| 314 | Qt3DRender::QWaitFence waitFence; |
| 315 | arbiter.setArbiterOnNode(&waitFence); |
| 316 | |
| 317 | { |
| 318 | // WHEN |
| 319 | waitFence.setTimeout(quint64(600)); |
| 320 | QCoreApplication::processEvents(); |
| 321 | |
| 322 | // THEN |
| 323 | QCOMPARE(arbiter.events.size(), 0); |
| 324 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
| 325 | QCOMPARE(arbiter.dirtyNodes.front(), &waitFence); |
| 326 | |
| 327 | arbiter.dirtyNodes.clear(); |
| 328 | } |
| 329 | |
| 330 | { |
| 331 | // WHEN |
| 332 | waitFence.setTimeout(quint64(600)); |
| 333 | QCoreApplication::processEvents(); |
| 334 | |
| 335 | // THEN |
| 336 | QCOMPARE(arbiter.events.size(), 0); |
| 337 | QCOMPARE(arbiter.dirtyNodes.size(), 0); |
| 338 | } |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | QTEST_MAIN(tst_QWaitFence) |
| 343 | |
| 344 | #include "tst_qwaitfence.moc" |
| 345 | |