| 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/QAbstractAxisInput> |
| 9 | #include <Qt3DInput/QAbstractPhysicalDevice> |
| 10 | #include <Qt3DInput/private/qabstractaxisinput_p.h> |
| 11 | |
| 12 | #include "testdevice.h" |
| 13 | #include <testarbiter.h> |
| 14 | |
| 15 | class DummyAxisInput : public Qt3DInput::QAbstractAxisInput |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | public: |
| 19 | explicit DummyAxisInput(QNode *parent = nullptr) |
| 20 | : Qt3DInput::QAbstractAxisInput(*new Qt3DInput::QAbstractAxisInputPrivate, parent) |
| 21 | { |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | class tst_QAbstractAxisInput: public QObject |
| 26 | { |
| 27 | Q_OBJECT |
| 28 | public: |
| 29 | tst_QAbstractAxisInput() |
| 30 | { |
| 31 | qRegisterMetaType<Qt3DInput::QAbstractPhysicalDevice*>("Qt3DInput::QAbstractPhysicalDevice*"); |
| 32 | qRegisterMetaType<Qt3DCore::QNode*>("Qt3DCore::QNode*"); |
| 33 | } |
| 34 | |
| 35 | private Q_SLOTS: |
| 36 | void checkPropertyUpdates() |
| 37 | { |
| 38 | // GIVEN |
| 39 | TestArbiter arbiter; |
| 40 | QScopedPointer<Qt3DInput::QAbstractAxisInput> axisInput(new DummyAxisInput()); |
| 41 | arbiter.setArbiterOnNode(axisInput.data()); |
| 42 | |
| 43 | // WHEN |
| 44 | TestDevice *device = new TestDevice(axisInput.data()); |
| 45 | QCoreApplication::processEvents(); |
| 46 | arbiter.clear(); |
| 47 | |
| 48 | axisInput->setSourceDevice(device); |
| 49 | |
| 50 | // THEN |
| 51 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 52 | QCOMPARE(arbiter.dirtyNodes().front(), axisInput.data()); |
| 53 | |
| 54 | arbiter.clear(); |
| 55 | } |
| 56 | |
| 57 | void checkSourceDeviceBookkeeping() |
| 58 | { |
| 59 | // GIVEN |
| 60 | QScopedPointer<Qt3DInput::QAbstractAxisInput> axisInput(new DummyAxisInput); |
| 61 | { |
| 62 | // WHEN |
| 63 | Qt3DInput::QAbstractPhysicalDevice device; |
| 64 | axisInput->setSourceDevice(&device); |
| 65 | |
| 66 | // THEN |
| 67 | QCOMPARE(device.parent(), axisInput.data()); |
| 68 | QCOMPARE(axisInput->sourceDevice(), &device); |
| 69 | } |
| 70 | // THEN (Should not crash and effect be unset) |
| 71 | QVERIFY(axisInput->sourceDevice() == nullptr); |
| 72 | |
| 73 | { |
| 74 | // WHEN |
| 75 | DummyAxisInput someOtherAxisInput; |
| 76 | QScopedPointer<Qt3DInput::QAbstractPhysicalDevice> device(new Qt3DInput::QAbstractPhysicalDevice(&someOtherAxisInput)); |
| 77 | axisInput->setSourceDevice(device.data()); |
| 78 | |
| 79 | // THEN |
| 80 | QCOMPARE(device->parent(), &someOtherAxisInput); |
| 81 | QCOMPARE(axisInput->sourceDevice(), device.data()); |
| 82 | |
| 83 | // WHEN |
| 84 | axisInput.reset(); |
| 85 | device.reset(); |
| 86 | |
| 87 | // THEN Should not crash when the device is destroyed (tests for failed removal of destruction helper) |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | QTEST_MAIN(tst_QAbstractAxisInput) |
| 93 | |
| 94 | #include "tst_qabstractaxisinput.moc" |