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/QAbstractPhysicalDevice>
35#include <Qt3DInput/QButtonAxisInput>
36#include <Qt3DInput/private/qbuttonaxisinput_p.h>
37
38#include "testpostmanarbiter.h"
39#include "testdevice.h"
40
41class tst_QButtonAxisInput: public QObject
42{
43 Q_OBJECT
44public:
45 tst_QButtonAxisInput()
46 {
47 qRegisterMetaType<Qt3DInput::QAbstractPhysicalDevice*>(typeName: "Qt3DInput::QAbstractPhysicalDevice*");
48 }
49
50private Q_SLOTS:
51 void shouldHaveDefaultState()
52 {
53 // GIVEN
54 Qt3DInput::QButtonAxisInput axisInput;
55
56 // THEN
57 QVERIFY(axisInput.buttons().isEmpty());
58 QCOMPARE(axisInput.scale(), 1.0f);
59 QCOMPARE(axisInput.acceleration(), -1.0f);
60 QCOMPARE(axisInput.deceleration(), -1.0f);
61 }
62
63 void checkCloning_data()
64 {
65 QTest::addColumn<Qt3DInput::QButtonAxisInput *>(name: "axisInput");
66
67 Qt3DInput::QButtonAxisInput *defaultConstructed = new Qt3DInput::QButtonAxisInput();
68 QTest::newRow(dataTag: "defaultConstructed") << defaultConstructed;
69
70 Qt3DInput::QButtonAxisInput *axisInputWithKeys = new Qt3DInput::QButtonAxisInput();
71 axisInputWithKeys->setButtons(QVector<int>() << ((1 << 1) | (1 << 5)));
72 axisInputWithKeys->setScale(327.0f);
73 QTest::newRow(dataTag: "axisInputWithKeys") << axisInputWithKeys;
74
75 Qt3DInput::QButtonAxisInput *axisInputWithKeysAndSourceDevice = new Qt3DInput::QButtonAxisInput();
76 TestDevice *device = new TestDevice();
77 axisInputWithKeysAndSourceDevice->setButtons(QVector<int>() << ((1 << 1) | (1 << 5)));
78 axisInputWithKeysAndSourceDevice->setSourceDevice(device);
79 axisInputWithKeysAndSourceDevice->setScale(355.0f);
80 QTest::newRow(dataTag: "axisInputWithKeysAndSourceDevice") << axisInputWithKeysAndSourceDevice;
81
82 Qt3DInput::QButtonAxisInput *axisInputWithAcceleration = new Qt3DInput::QButtonAxisInput();
83 axisInputWithAcceleration->setAcceleration(41.0f);
84 axisInputWithAcceleration->setDeceleration(42.0f);
85 QTest::newRow(dataTag: "axisInputWithAcceleration") << axisInputWithAcceleration;
86 }
87
88 void checkCloning()
89 {
90 // GIVEN
91 QFETCH(Qt3DInput::QButtonAxisInput *, axisInput);
92
93 // WHEN
94 Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(axisInput);
95 QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges();
96
97 // THEN
98 QCOMPARE(creationChanges.size(), 1 + (axisInput->sourceDevice() ? 1 : 0));
99
100 const Qt3DCore::QNodeCreatedChangePtr<Qt3DInput::QButtonAxisInputData> creationChangeData =
101 qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QButtonAxisInputData>>(src: creationChanges.first());
102 const Qt3DInput::QButtonAxisInputData &cloneData = creationChangeData->data;
103 QCOMPARE(axisInput->id(), creationChangeData->subjectId());
104 QCOMPARE(axisInput->isEnabled(), creationChangeData->isNodeEnabled());
105 QCOMPARE(axisInput->metaObject(), creationChangeData->metaObject());
106 QCOMPARE(axisInput->buttons(), cloneData.buttons);
107 QCOMPARE(axisInput->scale(), cloneData.scale);
108 QCOMPARE(axisInput->acceleration(), cloneData.acceleration);
109 QCOMPARE(axisInput->deceleration(), cloneData.deceleration);
110 QCOMPARE(axisInput->sourceDevice() ? axisInput->sourceDevice()->id() : Qt3DCore::QNodeId(), cloneData.sourceDeviceId);
111 }
112
113 void checkPropertyUpdates()
114 {
115 // GIVEN
116 TestArbiter arbiter;
117 QScopedPointer<Qt3DInput::QButtonAxisInput> axisInput(new Qt3DInput::QButtonAxisInput());
118 arbiter.setArbiterOnNode(axisInput.data());
119
120 // WHEN
121 QVector<int> buttons = QVector<int>() << 555;
122 axisInput->setButtons(buttons);
123
124 // THEN
125 QCOMPARE(arbiter.dirtyNodes.size(), 1);
126 QCOMPARE(arbiter.dirtyNodes.front(), axisInput.data());
127
128 arbiter.dirtyNodes.clear();
129
130 // WHEN
131 axisInput->setScale(1340.0f);
132
133 // THEN
134 QCOMPARE(arbiter.dirtyNodes.size(), 1);
135 QCOMPARE(arbiter.dirtyNodes.front(), axisInput.data());
136
137 arbiter.dirtyNodes.clear();
138
139 // WHEN
140 TestDevice *device = new TestDevice(axisInput.data());
141
142 axisInput->setSourceDevice(device);
143
144 // THEN
145 QCOMPARE(arbiter.dirtyNodes.size(), 1);
146 QCOMPARE(arbiter.dirtyNodes.front(), axisInput.data());
147
148 arbiter.dirtyNodes.clear();
149
150 // WHEN
151 axisInput->setAcceleration(42.0f);
152
153 // THEN
154 QCOMPARE(arbiter.dirtyNodes.size(), 1);
155 QCOMPARE(arbiter.dirtyNodes.front(), axisInput.data());
156
157 arbiter.dirtyNodes.clear();
158
159 // WHEN
160 axisInput->setDeceleration(43.0f);
161
162 // THEN
163 QCOMPARE(arbiter.dirtyNodes.size(), 1);
164 QCOMPARE(arbiter.dirtyNodes.front(), axisInput.data());
165
166 arbiter.dirtyNodes.clear();
167 }
168};
169
170QTEST_MAIN(tst_QButtonAxisInput)
171
172#include "tst_qbuttonaxisinput.moc"
173

source code of qt3d/tests/auto/input/qbuttonaxisinput/tst_qbuttonaxisinput.cpp