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/QAxis> |
35 | #include <Qt3DInput/QAnalogAxisInput> |
36 | #include <Qt3DInput/private/qaxis_p.h> |
37 | |
38 | #include "testpostmanarbiter.h" |
39 | |
40 | class tst_QAxis: public QObject |
41 | { |
42 | Q_OBJECT |
43 | public: |
44 | tst_QAxis() |
45 | { |
46 | } |
47 | |
48 | private Q_SLOTS: |
49 | |
50 | void checkCloning_data() |
51 | { |
52 | QTest::addColumn<Qt3DInput::QAxis *>(name: "axis" ); |
53 | |
54 | Qt3DInput::QAxis *defaultConstructed = new Qt3DInput::QAxis(); |
55 | QTest::newRow(dataTag: "defaultConstructed" ) << defaultConstructed; |
56 | |
57 | Qt3DInput::QAxis *namedAxis = new Qt3DInput::QAxis(); |
58 | QTest::newRow(dataTag: "namedAxis" ) << namedAxis; |
59 | |
60 | Qt3DInput::QAxis *namedAxisWithInputs = new Qt3DInput::QAxis(); |
61 | Qt3DInput::QAbstractAxisInput *axisInput1 = new Qt3DInput::QAnalogAxisInput(); |
62 | Qt3DInput::QAbstractAxisInput *axisInput2 = new Qt3DInput::QAnalogAxisInput(); |
63 | Qt3DInput::QAbstractAxisInput *axisInput3 = new Qt3DInput::QAnalogAxisInput(); |
64 | namedAxisWithInputs->addInput(input: axisInput1); |
65 | namedAxisWithInputs->addInput(input: axisInput2); |
66 | namedAxisWithInputs->addInput(input: axisInput3); |
67 | QTest::newRow(dataTag: "namedAxisWithInputs" ) << namedAxisWithInputs; |
68 | } |
69 | |
70 | void checkCloning() |
71 | { |
72 | // GIVEN |
73 | QFETCH(Qt3DInput::QAxis *, axis); |
74 | |
75 | // WHEN |
76 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(axis); |
77 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); |
78 | |
79 | // THEN |
80 | QCOMPARE(creationChanges.size(), 1 + axis->inputs().size()); |
81 | |
82 | const Qt3DCore::QNodeCreatedChangePtr<Qt3DInput::QAxisData> creationChangeData = |
83 | qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QAxisData>>(src: creationChanges.first()); |
84 | const Qt3DInput::QAxisData &cloneData = creationChangeData->data; |
85 | |
86 | // THEN |
87 | QCOMPARE(axis->id(), creationChangeData->subjectId()); |
88 | QCOMPARE(axis->isEnabled(), creationChangeData->isNodeEnabled()); |
89 | QCOMPARE(axis->metaObject(), creationChangeData->metaObject()); |
90 | QCOMPARE(axis->inputs().count(), cloneData.inputIds.count()); |
91 | |
92 | for (int i = 0, m = axis->inputs().count(); i < m; ++i) |
93 | QCOMPARE(axis->inputs().at(i)->id(), cloneData.inputIds.at(i)); |
94 | } |
95 | |
96 | void checkPropertyUpdates() |
97 | { |
98 | // GIVEN |
99 | TestArbiter arbiter; |
100 | QScopedPointer<Qt3DInput::QAxis> axis(new Qt3DInput::QAxis()); |
101 | arbiter.setArbiterOnNode(axis.data()); |
102 | |
103 | // WHEN |
104 | Qt3DInput::QAbstractAxisInput *input = new Qt3DInput::QAnalogAxisInput(); |
105 | axis->addInput(input); |
106 | QCoreApplication::processEvents(); |
107 | |
108 | // THEN |
109 | QCOMPARE(arbiter.events.size(), 0); |
110 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
111 | QCOMPARE(arbiter.dirtyNodes.front(), axis.data()); |
112 | |
113 | arbiter.dirtyNodes.clear(); |
114 | |
115 | // WHEN |
116 | axis->removeInput(input); |
117 | QCoreApplication::processEvents(); |
118 | |
119 | // THEN |
120 | QCOMPARE(arbiter.events.size(), 0); |
121 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
122 | QCOMPARE(arbiter.dirtyNodes.front(), axis.data()); |
123 | |
124 | arbiter.events.clear(); |
125 | } |
126 | |
127 | void checkAxisInputBookkeeping() |
128 | { |
129 | // GIVEN |
130 | QScopedPointer<Qt3DInput::QAxis> axis(new Qt3DInput::QAxis); |
131 | { |
132 | // WHEN |
133 | Qt3DInput::QAnalogAxisInput input; |
134 | axis->addInput(input: &input); |
135 | |
136 | // THEN |
137 | QCOMPARE(input.parent(), axis.data()); |
138 | QCOMPARE(axis->inputs().size(), 1); |
139 | } |
140 | // THEN (Should not crash and parameter be unset) |
141 | QVERIFY(axis->inputs().empty()); |
142 | |
143 | { |
144 | // WHEN |
145 | Qt3DInput::QAxis someOtherAxis; |
146 | QScopedPointer<Qt3DInput::QAbstractAxisInput> input(new Qt3DInput::QAnalogAxisInput(&someOtherAxis)); |
147 | axis->addInput(input: input.data()); |
148 | |
149 | // THEN |
150 | QCOMPARE(input->parent(), &someOtherAxis); |
151 | QCOMPARE(axis->inputs().size(), 1); |
152 | |
153 | // WHEN |
154 | axis.reset(); |
155 | input.reset(); |
156 | |
157 | // THEN Should not crash when the input is destroyed (tests for failed removal of destruction helper) |
158 | } |
159 | } |
160 | }; |
161 | |
162 | QTEST_MAIN(tst_QAxis) |
163 | |
164 | #include "tst_qaxis.moc" |
165 | |