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 <Qt3DCore/private/qnode_p.h>
31#include <Qt3DCore/private/qscene_p.h>
32#include <Qt3DCore/private/qnodecreatedchangegenerator_p.h>
33
34#include <Qt3DInput/QAction>
35#include <Qt3DInput/QActionInput>
36#include <Qt3DInput/private/qaction_p.h>
37#include <Qt3DInput/private/qactioninput_p.h>
38
39#include "testpostmanarbiter.h"
40
41// We need to call QNode::clone which is protected
42// We need to call QAction::sceneChangeEvent which is protected
43// So we sublcass QNode instead of QObject
44class tst_QAction: public Qt3DInput::QAction
45{
46 Q_OBJECT
47public:
48 tst_QAction()
49 {
50 }
51
52private Q_SLOTS:
53
54 void checkCloning_data()
55 {
56 QTest::addColumn<Qt3DInput::QAction *>(name: "action");
57
58 Qt3DInput::QAction *defaultConstructed = new Qt3DInput::QAction();
59 QTest::newRow(dataTag: "defaultConstructed") << defaultConstructed;
60
61 Qt3DInput::QAction *namedaction = new Qt3DInput::QAction();
62 QTest::newRow(dataTag: "namedAction") << namedaction;
63
64 Qt3DInput::QAction *namedactionWithInputs = new Qt3DInput::QAction();
65 Qt3DInput::QActionInput *actionInput1 = new Qt3DInput::QActionInput();
66 Qt3DInput::QActionInput *actionInput2 = new Qt3DInput::QActionInput();
67 Qt3DInput::QActionInput *actionInput3 = new Qt3DInput::QActionInput();
68 namedactionWithInputs->addInput(input: actionInput1);
69 namedactionWithInputs->addInput(input: actionInput2);
70 namedactionWithInputs->addInput(input: actionInput3);
71 QTest::newRow(dataTag: "namedActionWithInputs") << namedactionWithInputs;
72 }
73
74 void checkCloning()
75 {
76 // GIVEN
77 QFETCH(Qt3DInput::QAction *, action);
78
79 // WHEN
80 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(action);
81 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges();
82
83 // THEN
84 QCOMPARE(creationChanges.size(), 1 + action->inputs().size());
85
86 const Qt3DCore::QNodeCreatedChangePtr<Qt3DInput::QActionData> creationChangeData =
87 qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QActionData>>(src: creationChanges.first());
88 const Qt3DInput::QActionData &cloneActionData = creationChangeData->data;
89
90 // THEN
91 QCOMPARE(creationChangeData->subjectId(), action->id());
92 QCOMPARE(creationChangeData->isNodeEnabled(), action->isEnabled());
93 QCOMPARE(creationChangeData->metaObject(), action->metaObject());
94 QCOMPARE(creationChangeData->parentId(), action->parentNode() ? action->parentNode()->id() : Qt3DCore::QNodeId());
95 QCOMPARE(cloneActionData.inputIds.size(), action->inputs().size());
96
97 const QVector<Qt3DInput::QAbstractActionInput *> &inputs = action->inputs();
98 for (int i = 0, m = inputs.size(); i < m; ++i)
99 QCOMPARE(cloneActionData.inputIds.at(i), inputs.at(i)->id());
100 }
101
102 void checkPropertyUpdates()
103 {
104 // GIVEN
105 TestArbiter arbiter;
106 QScopedPointer<Qt3DInput::QAction> action(new Qt3DInput::QAction());
107 arbiter.setArbiterOnNode(action.data());
108
109 // WHEN
110 Qt3DInput::QActionInput *input = new Qt3DInput::QActionInput();
111 action->addInput(input);
112 QCoreApplication::processEvents();
113
114 // THEN
115 QCOMPARE(arbiter.events.size(), 0);
116 QCOMPARE(arbiter.dirtyNodes.size(), 1);
117 QCOMPARE(arbiter.dirtyNodes.front(), action.data());
118
119 arbiter.dirtyNodes.clear();
120
121 // WHEN
122 action->removeInput(input);
123 QCoreApplication::processEvents();
124
125 // THEN
126 QCOMPARE(arbiter.events.size(), 0);
127 QCOMPARE(arbiter.dirtyNodes.size(), 1);
128 QCOMPARE(arbiter.dirtyNodes.front(), action.data());
129
130 arbiter.events.clear();
131 }
132
133 void checkActionInputBookkeeping()
134 {
135 // GIVEN
136 QScopedPointer<Qt3DInput::QAction> action(new Qt3DInput::QAction);
137 {
138 // WHEN
139 Qt3DInput::QActionInput input;
140 action->addInput(input: &input);
141
142 // THEN
143 QCOMPARE(input.parent(), action.data());
144 QCOMPARE(action->inputs().size(), 1);
145 }
146 // THEN (Should not crash and parameter be unset)
147 QVERIFY(action->inputs().empty());
148
149 {
150 // WHEN
151 Qt3DInput::QAction someOtherAction;
152 QScopedPointer<Qt3DInput::QActionInput> input(new Qt3DInput::QActionInput(&someOtherAction));
153 action->addInput(input: input.data());
154
155 // THEN
156 QCOMPARE(input->parent(), &someOtherAction);
157 QCOMPARE(action->inputs().size(), 1);
158
159 // WHEN
160 action.reset();
161 input.reset();
162
163 // THEN Should not crash when the input is destroyed (tests for failed removal of destruction helper)
164 }
165 }
166};
167
168QTEST_MAIN(tst_QAction)
169
170#include "tst_qaction.moc"
171

source code of qt3d/tests/auto/input/qaction/tst_qaction.cpp