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