| 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/qnode_p.h> |
| 6 | #include <Qt3DCore/private/qscene_p.h> |
| 7 | |
| 8 | #include <Qt3DInput/QLogicalDevice> |
| 9 | #include <Qt3DInput/private/qlogicaldevice_p.h> |
| 10 | #include <Qt3DInput/QAxis> |
| 11 | #include <Qt3DInput/QAction> |
| 12 | |
| 13 | #include "testarbiter.h" |
| 14 | |
| 15 | class tst_QLogicalDevice: public QObject |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | public: |
| 19 | tst_QLogicalDevice() |
| 20 | { |
| 21 | qRegisterMetaType<Qt3DCore::QNode *>(); |
| 22 | } |
| 23 | |
| 24 | private Q_SLOTS: |
| 25 | |
| 26 | void checkPropertyUpdates() |
| 27 | { |
| 28 | // GIVEN |
| 29 | TestArbiter arbiter; |
| 30 | QScopedPointer<Qt3DInput::QLogicalDevice> logicalDevice(new Qt3DInput::QLogicalDevice()); |
| 31 | arbiter.setArbiterOnNode(logicalDevice.data()); |
| 32 | |
| 33 | // WHEN |
| 34 | Qt3DInput::QAction *action = new Qt3DInput::QAction(logicalDevice.data()); |
| 35 | QCoreApplication::processEvents(); |
| 36 | arbiter.clear(); |
| 37 | |
| 38 | logicalDevice->addAction(action); |
| 39 | QCoreApplication::processEvents(); |
| 40 | |
| 41 | // THEN |
| 42 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 43 | QCOMPARE(arbiter.dirtyNodes().front(), logicalDevice.data()); |
| 44 | |
| 45 | arbiter.clear(); |
| 46 | |
| 47 | // WHEN |
| 48 | logicalDevice->removeAction(action); |
| 49 | QCoreApplication::processEvents(); |
| 50 | |
| 51 | // THEN |
| 52 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 53 | QCOMPARE(arbiter.dirtyNodes().front(), logicalDevice.data()); |
| 54 | |
| 55 | arbiter.clear(); |
| 56 | |
| 57 | // WHEN |
| 58 | Qt3DInput::QAxis *axis = new Qt3DInput::QAxis(logicalDevice.data()); |
| 59 | QCoreApplication::processEvents(); |
| 60 | arbiter.clear(); |
| 61 | |
| 62 | logicalDevice->addAxis(axis); |
| 63 | QCoreApplication::processEvents(); |
| 64 | |
| 65 | // THEN |
| 66 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 67 | QCOMPARE(arbiter.dirtyNodes().front(), logicalDevice.data()); |
| 68 | |
| 69 | arbiter.clear(); |
| 70 | |
| 71 | // WHEN |
| 72 | logicalDevice->removeAxis(axis); |
| 73 | QCoreApplication::processEvents(); |
| 74 | |
| 75 | // THEN |
| 76 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 77 | QCOMPARE(arbiter.dirtyNodes().front(), logicalDevice.data()); |
| 78 | |
| 79 | arbiter.clear(); |
| 80 | } |
| 81 | |
| 82 | void checkAxisBookkeeping() |
| 83 | { |
| 84 | // GIVEN |
| 85 | QScopedPointer<Qt3DInput::QLogicalDevice> device(new Qt3DInput::QLogicalDevice); |
| 86 | { |
| 87 | // WHEN |
| 88 | Qt3DInput::QAxis axis; |
| 89 | device->addAxis(&axis); |
| 90 | |
| 91 | // THEN |
| 92 | QCOMPARE(axis.parent(), device.data()); |
| 93 | QCOMPARE(device->axes().size(), 1); |
| 94 | } |
| 95 | // THEN (Should not crash and parameter be unset) |
| 96 | QVERIFY(device->axes().empty()); |
| 97 | |
| 98 | { |
| 99 | // WHEN |
| 100 | Qt3DInput::QLogicalDevice someOtherDevice; |
| 101 | QScopedPointer<Qt3DInput::QAxis> axis(new Qt3DInput::QAxis(&someOtherDevice)); |
| 102 | device->addAxis(axis.data()); |
| 103 | |
| 104 | // THEN |
| 105 | QCOMPARE(axis->parent(), &someOtherDevice); |
| 106 | QCOMPARE(device->axes().size(), 1); |
| 107 | |
| 108 | // WHEN |
| 109 | device.reset(); |
| 110 | axis.reset(); |
| 111 | |
| 112 | // THEN Should not crash when the axis is destroyed (tests for failed removal of destruction helper) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void checkActionBookkeeping() |
| 117 | { |
| 118 | // GIVEN |
| 119 | QScopedPointer<Qt3DInput::QLogicalDevice> device(new Qt3DInput::QLogicalDevice); |
| 120 | { |
| 121 | // WHEN |
| 122 | Qt3DInput::QAction action; |
| 123 | device->addAction(&action); |
| 124 | |
| 125 | // THEN |
| 126 | QCOMPARE(action.parent(), device.data()); |
| 127 | QCOMPARE(device->actions().size(), 1); |
| 128 | } |
| 129 | // THEN (Should not crash and parameter be unset) |
| 130 | QVERIFY(device->actions().empty()); |
| 131 | |
| 132 | { |
| 133 | // WHEN |
| 134 | Qt3DInput::QLogicalDevice someOtherDevice; |
| 135 | QScopedPointer<Qt3DInput::QAction> action(new Qt3DInput::QAction(&someOtherDevice)); |
| 136 | device->addAction(action.data()); |
| 137 | |
| 138 | // THEN |
| 139 | QCOMPARE(action->parent(), &someOtherDevice); |
| 140 | QCOMPARE(device->actions().size(), 1); |
| 141 | |
| 142 | // WHEN |
| 143 | device.reset(); |
| 144 | action.reset(); |
| 145 | |
| 146 | // THEN Should not crash when the action is destroyed (tests for failed removal of destruction helper) |
| 147 | } |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | QTEST_MAIN(tst_QLogicalDevice) |
| 152 | |
| 153 | #include "tst_qlogicaldevice.moc" |