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/axis_p.h>
36#include <Qt3DInput/private/qabstractaxisinput_p.h>
37#include <Qt3DInput/QAnalogAxisInput>
38#include <Qt3DInput/QAxis>
39#include <Qt3DCore/private/qbackendnode_p.h>
40#include "testpostmanarbiter.h"
41
42class DummyAxisInput : public Qt3DInput::QAbstractAxisInput
43{
44 Q_OBJECT
45public:
46 DummyAxisInput(Qt3DCore::QNode *parent = nullptr)
47 : Qt3DInput::QAbstractAxisInput(*new Qt3DInput::QAbstractAxisInputPrivate, parent)
48 {}
49};
50
51class tst_Axis: public Qt3DCore::QBackendNodeTester
52{
53 Q_OBJECT
54
55private Q_SLOTS:
56
57 void checkPeerPropertyMirroring()
58 {
59 // GIVEN
60 Qt3DInput::Input::Axis backendAxis;
61 Qt3DInput::QAxis axis;
62 Qt3DInput::QAnalogAxisInput axisInput;
63
64 axis.addInput(input: &axisInput);
65
66 // WHEN
67 simulateInitializationSync(frontend: &axis, backend: &backendAxis);
68
69 // THEN
70 QCOMPARE(backendAxis.peerId(), axis.id());
71 QCOMPARE(backendAxis.isEnabled(), axis.isEnabled());
72 QCOMPARE(backendAxis.inputs().size(), axis.inputs().size());
73
74 const int inputsCount = backendAxis.inputs().size();
75 if (inputsCount > 0) {
76 for (int i = 0; i < inputsCount; ++i)
77 QCOMPARE(backendAxis.inputs().at(i), axis.inputs().at(i)->id());
78 }
79 }
80
81 void checkInitialAndCleanedUpState()
82 {
83 // GIVEN
84 Qt3DInput::Input::Axis backendAxis;
85
86 // THEN
87 QVERIFY(backendAxis.peerId().isNull());
88 QCOMPARE(backendAxis.axisValue(), 0.0f);
89 QCOMPARE(backendAxis.isEnabled(), false);
90 QCOMPARE(backendAxis.inputs().size(), 0);
91
92 // GIVEN
93 Qt3DInput::QAxis axis;
94 Qt3DInput::QAnalogAxisInput axisInput;
95
96 axis.addInput(input: &axisInput);
97
98 // WHEN
99 simulateInitializationSync(frontend: &axis, backend: &backendAxis);
100 backendAxis.setAxisValue(883.0f);
101 backendAxis.cleanup();
102
103 // THEN
104 QCOMPARE(backendAxis.axisValue(), 0.0f);
105 QCOMPARE(backendAxis.isEnabled(), false);
106 QCOMPARE(backendAxis.inputs().size(), 0);
107 }
108
109 void checkPropertyChanges()
110 {
111 // GIVEN
112 Qt3DInput::QAxis axis;
113 Qt3DInput::Input::Axis backendAxis;
114 simulateInitializationSync(frontend: &axis, backend: &backendAxis);
115
116 // WHEN
117 axis.setEnabled(false);
118 backendAxis.syncFromFrontEnd(frontEnd: &axis, firstTime: false);
119
120 // THEN
121 QCOMPARE(backendAxis.isEnabled(), false);
122
123 // WHEN
124 DummyAxisInput input;
125 const Qt3DCore::QNodeId inputId = input.id();
126 axis.addInput(input: &input);
127 backendAxis.syncFromFrontEnd(frontEnd: &axis, firstTime: false);
128
129 // THEN
130 QCOMPARE(backendAxis.inputs().size(), 1);
131 QCOMPARE(backendAxis.inputs().first(), inputId);
132
133 // WHEN
134 axis.removeInput(input: &input);
135 backendAxis.syncFromFrontEnd(frontEnd: &axis, firstTime: false);
136
137 // THEN
138 QCOMPARE(backendAxis.inputs().size(), 0);
139 }
140
141 void shouldNotChangeValueWhenDisabled()
142 {
143 // GIVEN
144 TestArbiter arbiter;
145 Qt3DInput::Input::Axis backendAxis;
146 Qt3DCore::QBackendNodePrivate::get(n: &backendAxis)->setArbiter(&arbiter);
147 backendAxis.setEnabled(false);
148
149 // WHEN
150 backendAxis.setAxisValue(454.0f);
151
152 // THEN
153 QCOMPARE(backendAxis.axisValue(), 0.0f);
154 QCOMPARE(arbiter.events.count(), 0);
155 }
156};
157
158QTEST_APPLESS_MAIN(tst_Axis)
159
160#include "tst_axis.moc"
161

source code of qt3d/tests/auto/input/axis/tst_axis.cpp