| 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 <qbackendnodetester.h> |
| 6 | #include <Qt3DCore/private/qnode_p.h> |
| 7 | #include <Qt3DCore/private/qscene_p.h> |
| 8 | #include <Qt3DInput/private/axis_p.h> |
| 9 | #include <Qt3DInput/private/qabstractaxisinput_p.h> |
| 10 | #include <Qt3DInput/QAnalogAxisInput> |
| 11 | #include <Qt3DInput/QAxis> |
| 12 | #include <Qt3DCore/private/qbackendnode_p.h> |
| 13 | #include "testarbiter.h" |
| 14 | |
| 15 | class DummyAxisInput : public Qt3DInput::QAbstractAxisInput |
| 16 | { |
| 17 | Q_OBJECT |
| 18 | public: |
| 19 | DummyAxisInput(Qt3DCore::QNode *parent = nullptr) |
| 20 | : Qt3DInput::QAbstractAxisInput(*new Qt3DInput::QAbstractAxisInputPrivate, parent) |
| 21 | {} |
| 22 | }; |
| 23 | |
| 24 | class tst_Axis: public Qt3DCore::QBackendNodeTester |
| 25 | { |
| 26 | Q_OBJECT |
| 27 | |
| 28 | private Q_SLOTS: |
| 29 | |
| 30 | void checkPeerPropertyMirroring() |
| 31 | { |
| 32 | // GIVEN |
| 33 | Qt3DInput::Input::Axis backendAxis; |
| 34 | Qt3DInput::QAxis axis; |
| 35 | Qt3DInput::QAnalogAxisInput axisInput; |
| 36 | |
| 37 | axis.addInput(&axisInput); |
| 38 | |
| 39 | // WHEN |
| 40 | simulateInitializationSync(&axis, &backendAxis); |
| 41 | |
| 42 | // THEN |
| 43 | QCOMPARE(backendAxis.peerId(), axis.id()); |
| 44 | QCOMPARE(backendAxis.isEnabled(), axis.isEnabled()); |
| 45 | QCOMPARE(backendAxis.inputs().size(), axis.inputs().size()); |
| 46 | |
| 47 | const int inputsCount = backendAxis.inputs().size(); |
| 48 | if (inputsCount > 0) { |
| 49 | for (int i = 0; i < inputsCount; ++i) |
| 50 | QCOMPARE(backendAxis.inputs().at(i), axis.inputs().at(i)->id()); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void checkInitialAndCleanedUpState() |
| 55 | { |
| 56 | // GIVEN |
| 57 | Qt3DInput::Input::Axis backendAxis; |
| 58 | |
| 59 | // THEN |
| 60 | QVERIFY(backendAxis.peerId().isNull()); |
| 61 | QCOMPARE(backendAxis.axisValue(), 0.0f); |
| 62 | QCOMPARE(backendAxis.isEnabled(), false); |
| 63 | QCOMPARE(backendAxis.inputs().size(), 0); |
| 64 | |
| 65 | // GIVEN |
| 66 | Qt3DInput::QAxis axis; |
| 67 | Qt3DInput::QAnalogAxisInput axisInput; |
| 68 | |
| 69 | axis.addInput(&axisInput); |
| 70 | |
| 71 | // WHEN |
| 72 | simulateInitializationSync(&axis, &backendAxis); |
| 73 | backendAxis.setAxisValue(883.0f); |
| 74 | backendAxis.cleanup(); |
| 75 | |
| 76 | // THEN |
| 77 | QCOMPARE(backendAxis.axisValue(), 0.0f); |
| 78 | QCOMPARE(backendAxis.isEnabled(), false); |
| 79 | QCOMPARE(backendAxis.inputs().size(), 0); |
| 80 | } |
| 81 | |
| 82 | void checkPropertyChanges() |
| 83 | { |
| 84 | // GIVEN |
| 85 | Qt3DInput::QAxis axis; |
| 86 | Qt3DInput::Input::Axis backendAxis; |
| 87 | simulateInitializationSync(&axis, &backendAxis); |
| 88 | |
| 89 | // WHEN |
| 90 | axis.setEnabled(false); |
| 91 | backendAxis.syncFromFrontEnd(&axis, false); |
| 92 | |
| 93 | // THEN |
| 94 | QCOMPARE(backendAxis.isEnabled(), false); |
| 95 | |
| 96 | // WHEN |
| 97 | DummyAxisInput input; |
| 98 | const Qt3DCore::QNodeId inputId = input.id(); |
| 99 | axis.addInput(&input); |
| 100 | backendAxis.syncFromFrontEnd(&axis, false); |
| 101 | |
| 102 | // THEN |
| 103 | QCOMPARE(backendAxis.inputs().size(), 1); |
| 104 | QCOMPARE(backendAxis.inputs().first(), inputId); |
| 105 | |
| 106 | // WHEN |
| 107 | axis.removeInput(&input); |
| 108 | backendAxis.syncFromFrontEnd(&axis, false); |
| 109 | |
| 110 | // THEN |
| 111 | QCOMPARE(backendAxis.inputs().size(), 0); |
| 112 | } |
| 113 | |
| 114 | void shouldNotChangeValueWhenDisabled() |
| 115 | { |
| 116 | // GIVEN |
| 117 | Qt3DInput::Input::Axis backendAxis; |
| 118 | backendAxis.setEnabled(false); |
| 119 | |
| 120 | // WHEN |
| 121 | backendAxis.setAxisValue(454.0f); |
| 122 | |
| 123 | // THEN |
| 124 | QCOMPARE(backendAxis.axisValue(), 0.0f); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | QTEST_APPLESS_MAIN(tst_Axis) |
| 129 | |
| 130 | #include "tst_axis.moc" |