| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2015 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:GPL-EXCEPT$ | 
| 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 General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #include <QtTest/QTest> | 
| 30 | #include <qbackendnodetester.h> | 
| 31 | #include <Qt3DCore/private/qnode_p.h> | 
| 32 | #include <Qt3DCore/private/qscene_p.h> | 
| 33 | #include <Qt3DCore/qpropertynodeaddedchange.h> | 
| 34 | #include <Qt3DCore/qpropertynoderemovedchange.h> | 
| 35 | #include <Qt3DInput/private/action_p.h> | 
| 36 | #include <Qt3DInput/QActionInput> | 
| 37 | #include <Qt3DInput/QAction> | 
| 38 | #include <Qt3DCore/private/qbackendnode_p.h> | 
| 39 | #include "testpostmanarbiter.h" | 
| 40 |  | 
| 41 | class DummyActionInput : public Qt3DInput::QActionInput | 
| 42 | { | 
| 43 |     Q_OBJECT | 
| 44 | public: | 
| 45 |     DummyActionInput(Qt3DCore::QNode *parent = nullptr) | 
| 46 |         : Qt3DInput::QActionInput(parent) | 
| 47 |     {} | 
| 48 | }; | 
| 49 |  | 
| 50 | class tst_Action : public Qt3DCore::QBackendNodeTester | 
| 51 | { | 
| 52 |     Q_OBJECT | 
| 53 |  | 
| 54 | private Q_SLOTS: | 
| 55 |  | 
| 56 |     void checkPeerPropertyMirroring() | 
| 57 |     { | 
| 58 |         // GIVEN | 
| 59 |         Qt3DInput::Input::Action backendAction; | 
| 60 |         Qt3DInput::QAction action; | 
| 61 |         Qt3DInput::QActionInput actionInput; | 
| 62 |  | 
| 63 |         action.addInput(input: &actionInput); | 
| 64 |  | 
| 65 |         // WHEN | 
| 66 |         simulateInitializationSync(frontend: &action, backend: &backendAction); | 
| 67 |  | 
| 68 |         // THEN | 
| 69 |         QCOMPARE(backendAction.peerId(), action.id()); | 
| 70 |         QCOMPARE(backendAction.isEnabled(), action.isEnabled()); | 
| 71 |         QCOMPARE(backendAction.inputs().size(), action.inputs().size()); | 
| 72 |  | 
| 73 |         const int inputsCount = backendAction.inputs().size(); | 
| 74 |         if (inputsCount > 0) { | 
| 75 |             for (int i = 0; i < inputsCount; ++i) | 
| 76 |                 QCOMPARE(backendAction.inputs().at(i), action.inputs().at(i)->id()); | 
| 77 |         } | 
| 78 |     } | 
| 79 |  | 
| 80 |     void checkInitialAndCleanedUpState() | 
| 81 |     { | 
| 82 |         // GIVEN | 
| 83 |         Qt3DInput::Input::Action backendAction; | 
| 84 |  | 
| 85 |         // THEN | 
| 86 |         QVERIFY(backendAction.peerId().isNull()); | 
| 87 |         QCOMPARE(backendAction.actionTriggered(), false); | 
| 88 |         QCOMPARE(backendAction.isEnabled(), false); | 
| 89 |         QCOMPARE(backendAction.inputs().size(), 0); | 
| 90 |  | 
| 91 |         // GIVEN | 
| 92 |         Qt3DInput::QAction action; | 
| 93 |         Qt3DInput::QActionInput axisInput; | 
| 94 |  | 
| 95 |         action.addInput(input: &axisInput); | 
| 96 |  | 
| 97 |         // WHEN | 
| 98 |         simulateInitializationSync(frontend: &action, backend: &backendAction); | 
| 99 |         backendAction.setActionTriggered(true); | 
| 100 |         backendAction.cleanup(); | 
| 101 |  | 
| 102 |         // THEN | 
| 103 |         QCOMPARE(backendAction.actionTriggered(), false); | 
| 104 |         QCOMPARE(backendAction.isEnabled(), false); | 
| 105 |         QCOMPARE(backendAction.inputs().size(), 0); | 
| 106 |     } | 
| 107 |  | 
| 108 |     void checkPropertyChanges() | 
| 109 |     { | 
| 110 |         // GIVEN | 
| 111 |         Qt3DInput::QAction action; | 
| 112 |         Qt3DInput::Input::Action backendAction; | 
| 113 |         simulateInitializationSync(frontend: &action, backend: &backendAction); | 
| 114 |  | 
| 115 |         // WHEN | 
| 116 |         action.setEnabled(false); | 
| 117 |         backendAction.syncFromFrontEnd(frontEnd: &action, firstTime: false); | 
| 118 |  | 
| 119 |         // THEN | 
| 120 |         QCOMPARE(backendAction.isEnabled(), false); | 
| 121 |  | 
| 122 |         // WHEN | 
| 123 |         DummyActionInput input; | 
| 124 |         const Qt3DCore::QNodeId inputId = input.id(); | 
| 125 |         action.addInput(input: &input); | 
| 126 |         backendAction.syncFromFrontEnd(frontEnd: &action, firstTime: false); | 
| 127 |  | 
| 128 |         // THEN | 
| 129 |         QCOMPARE(backendAction.inputs().size(), 1); | 
| 130 |         QCOMPARE(backendAction.inputs().first(), inputId); | 
| 131 |  | 
| 132 |         // WHEN | 
| 133 |         action.removeInput(input: &input); | 
| 134 |         backendAction.syncFromFrontEnd(frontEnd: &action, firstTime: false); | 
| 135 |  | 
| 136 |         // THEN | 
| 137 |         QCOMPARE(backendAction.inputs().size(), 0); | 
| 138 |     } | 
| 139 |  | 
| 140 |     void shouldNotActivateWhenDisabled() | 
| 141 |     { | 
| 142 |         // GIVEN | 
| 143 |         TestArbiter arbiter; | 
| 144 |         Qt3DInput::Input::Action backendAction; | 
| 145 |         backendAction.setEnabled(false); | 
| 146 |         Qt3DCore::QBackendNodePrivate::get(n: &backendAction)->setArbiter(&arbiter); | 
| 147 |  | 
| 148 |         // WHEN | 
| 149 |         backendAction.setActionTriggered(true); | 
| 150 |  | 
| 151 |         // THEN | 
| 152 |         QVERIFY(!backendAction.actionTriggered()); | 
| 153 |         QCOMPARE(arbiter.events.count(), 0); | 
| 154 |     } | 
| 155 | }; | 
| 156 |  | 
| 157 | QTEST_APPLESS_MAIN(tst_Action) | 
| 158 |  | 
| 159 | #include "tst_action.moc" | 
| 160 |  |