1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 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 "testdevice.h" |
32 | |
33 | #include <Qt3DCore/private/qnode_p.h> |
34 | #include <Qt3DCore/private/qscene_p.h> |
35 | #include <Qt3DInput/private/buttonaxisinput_p.h> |
36 | #include <Qt3DInput/private/inputhandler_p.h> |
37 | #include <Qt3DInput/QButtonAxisInput> |
38 | |
39 | class tst_ButtonAxisInput: public Qt3DCore::QBackendNodeTester |
40 | { |
41 | Q_OBJECT |
42 | |
43 | private Q_SLOTS: |
44 | |
45 | void checkPeerPropertyMirroring() |
46 | { |
47 | // GIVEN |
48 | Qt3DInput::Input::ButtonAxisInput backendAxisInput; |
49 | Qt3DInput::QButtonAxisInput axisInput; |
50 | TestDevice sourceDevice; |
51 | |
52 | axisInput.setButtons(QVector<int>() << (1 << 8)); |
53 | axisInput.setScale(0.5f); |
54 | axisInput.setAcceleration(0.42f); |
55 | axisInput.setDeceleration(0.43f); |
56 | axisInput.setSourceDevice(&sourceDevice); |
57 | |
58 | // WHEN |
59 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
60 | |
61 | // THEN |
62 | QCOMPARE(backendAxisInput.peerId(), axisInput.id()); |
63 | QCOMPARE(backendAxisInput.isEnabled(), axisInput.isEnabled()); |
64 | QCOMPARE(backendAxisInput.buttons(), axisInput.buttons()); |
65 | QCOMPARE(backendAxisInput.scale(), axisInput.scale()); |
66 | QCOMPARE(backendAxisInput.acceleration(), axisInput.acceleration()); |
67 | QCOMPARE(backendAxisInput.deceleration(), axisInput.deceleration()); |
68 | QCOMPARE(backendAxisInput.sourceDevice(), sourceDevice.id()); |
69 | |
70 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
71 | QCOMPARE(backendAxisInput.lastUpdateTime(), 0); |
72 | } |
73 | |
74 | void checkInitialAndCleanedUpState() |
75 | { |
76 | // GIVEN |
77 | Qt3DInput::Input::ButtonAxisInput backendAxisInput; |
78 | |
79 | // THEN |
80 | QVERIFY(backendAxisInput.peerId().isNull()); |
81 | QCOMPARE(backendAxisInput.scale(), 0.0f); |
82 | QVERIFY(qIsInf(backendAxisInput.acceleration())); |
83 | QVERIFY(qIsInf(backendAxisInput.deceleration())); |
84 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
85 | QCOMPARE(backendAxisInput.lastUpdateTime(), 0); |
86 | QVERIFY(backendAxisInput.buttons().isEmpty()); |
87 | QCOMPARE(backendAxisInput.isEnabled(), false); |
88 | QCOMPARE(backendAxisInput.sourceDevice(), Qt3DCore::QNodeId()); |
89 | |
90 | // GIVEN |
91 | Qt3DInput::QButtonAxisInput axisInput; |
92 | TestDevice sourceDevice; |
93 | |
94 | axisInput.setButtons(QVector<int>() << (1 << 8)); |
95 | axisInput.setScale(0.5f); |
96 | axisInput.setSourceDevice(&sourceDevice); |
97 | |
98 | // WHEN |
99 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
100 | backendAxisInput.cleanup(); |
101 | |
102 | // THEN |
103 | QCOMPARE(backendAxisInput.scale(), 0.0f); |
104 | QVERIFY(qIsInf(backendAxisInput.acceleration())); |
105 | QVERIFY(qIsInf(backendAxisInput.deceleration())); |
106 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
107 | QCOMPARE(backendAxisInput.lastUpdateTime(), 0); |
108 | QVERIFY(backendAxisInput.buttons().isEmpty()); |
109 | QCOMPARE(backendAxisInput.isEnabled(), false); |
110 | QCOMPARE(backendAxisInput.sourceDevice(), Qt3DCore::QNodeId()); |
111 | } |
112 | |
113 | void checkPropertyChanges() |
114 | { |
115 | // GIVEN |
116 | Qt3DInput::QButtonAxisInput axisInput; |
117 | Qt3DInput::Input::ButtonAxisInput backendAxisInput; |
118 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
119 | |
120 | // WHEN |
121 | axisInput.setButtons(QVector<int>() << 64); |
122 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
123 | |
124 | // THEN |
125 | QCOMPARE(backendAxisInput.buttons(), QVector<int>() << 64); |
126 | |
127 | // WHEN |
128 | axisInput.setScale(0.5f); |
129 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
130 | |
131 | // THEN |
132 | QCOMPARE(backendAxisInput.scale(), 0.5f); |
133 | |
134 | // WHEN |
135 | axisInput.setEnabled(false); |
136 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
137 | |
138 | // THEN |
139 | QCOMPARE(backendAxisInput.isEnabled(), false); |
140 | |
141 | // WHEN |
142 | TestDevice device; |
143 | axisInput.setSourceDevice(&device); |
144 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
145 | |
146 | // THEN |
147 | QCOMPARE(backendAxisInput.sourceDevice(), device.id()); |
148 | |
149 | // WHEN |
150 | axisInput.setAcceleration(0.42f); |
151 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
152 | |
153 | // THEN |
154 | QCOMPARE(backendAxisInput.acceleration(), 0.42f); |
155 | |
156 | // WHEN |
157 | axisInput.setAcceleration(-0.42f); |
158 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
159 | |
160 | // THEN |
161 | QVERIFY(qIsInf(backendAxisInput.acceleration())); |
162 | |
163 | // WHEN |
164 | axisInput.setDeceleration(0.43f); |
165 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
166 | |
167 | // THEN |
168 | QCOMPARE(backendAxisInput.deceleration(), 0.43f); |
169 | |
170 | // WHEN |
171 | axisInput.setDeceleration(-0.43f); |
172 | backendAxisInput.syncFromFrontEnd(frontEnd: &axisInput, firstTime: false); |
173 | |
174 | // THEN |
175 | QVERIFY(qIsInf(backendAxisInput.deceleration())); |
176 | } |
177 | |
178 | void shouldProcessAndUpdateSpeedRatioOverTime() |
179 | { |
180 | const qint64 s = 1000000000; |
181 | |
182 | // GIVEN |
183 | TestDeviceIntegration deviceIntegration; |
184 | TestDevice *device = deviceIntegration.createPhysicalDevice(name: "keyboard" ); |
185 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(id: device->id()); |
186 | Qt3DInput::Input::InputHandler handler; |
187 | handler.addInputDeviceIntegration(inputIntegration: &deviceIntegration); |
188 | |
189 | Qt3DInput::Input::ButtonAxisInput backendAxisInput; |
190 | Qt3DInput::QButtonAxisInput axisInput; |
191 | axisInput.setEnabled(true); |
192 | axisInput.setButtons(QVector<int>() << Qt::Key_Space); |
193 | axisInput.setScale(-1.0f); |
194 | axisInput.setAcceleration(0.15f); |
195 | axisInput.setDeceleration(0.3f); |
196 | axisInput.setSourceDevice(device); |
197 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
198 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
199 | QCOMPARE(backendAxisInput.lastUpdateTime(), 0); |
200 | |
201 | // WHEN (accelerate) |
202 | deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Space, pressed: true); |
203 | |
204 | // WHEN |
205 | QCOMPARE(backendAxisInput.process(&handler, 30 * s), 0.0f); |
206 | |
207 | // THEN |
208 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
209 | QCOMPARE(backendAxisInput.lastUpdateTime(), 30 * s); |
210 | |
211 | // WHEN |
212 | QCOMPARE(backendAxisInput.process(&handler, 31 * s), -0.15f); |
213 | |
214 | // THEN |
215 | QCOMPARE(backendAxisInput.speedRatio(), 0.15f); |
216 | QCOMPARE(backendAxisInput.lastUpdateTime(), 31 * s); |
217 | |
218 | // WHEN |
219 | QCOMPARE(backendAxisInput.process(&handler, 32 * s), -0.3f); |
220 | |
221 | // THEN |
222 | QCOMPARE(backendAxisInput.speedRatio(), 0.3f); |
223 | QCOMPARE(backendAxisInput.lastUpdateTime(), 32 * s); |
224 | |
225 | // WHEN |
226 | QCOMPARE(backendAxisInput.process(&handler, 35 * s), -0.75f); |
227 | |
228 | // THEN |
229 | QCOMPARE(backendAxisInput.speedRatio(), 0.75f); |
230 | QCOMPARE(backendAxisInput.lastUpdateTime(), 35 * s); |
231 | |
232 | // WHEN |
233 | QCOMPARE(backendAxisInput.process(&handler, 37 * s), -1.0f); |
234 | |
235 | // THEN |
236 | QCOMPARE(backendAxisInput.speedRatio(), 1.0f); |
237 | QCOMPARE(backendAxisInput.lastUpdateTime(), 37 * s); |
238 | |
239 | // WHEN |
240 | QCOMPARE(backendAxisInput.process(&handler, 38 * s), -1.0f); |
241 | |
242 | // THEN |
243 | QCOMPARE(backendAxisInput.speedRatio(), 1.0f); |
244 | QCOMPARE(backendAxisInput.lastUpdateTime(), 38 * s); |
245 | |
246 | // WHEN |
247 | QCOMPARE(backendAxisInput.process(&handler, 42 * s), -1.0f); |
248 | |
249 | // THEN |
250 | QCOMPARE(backendAxisInput.speedRatio(), 1.0f); |
251 | QCOMPARE(backendAxisInput.lastUpdateTime(), 42 * s); |
252 | |
253 | |
254 | // WHEN (decelerate) |
255 | deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Space, pressed: false); |
256 | |
257 | // WHEN |
258 | QCOMPARE(backendAxisInput.process(&handler, 43 * s), -0.7f); |
259 | |
260 | // THEN |
261 | QCOMPARE(backendAxisInput.speedRatio(), 0.7f); |
262 | QCOMPARE(backendAxisInput.lastUpdateTime(), 43 * s); |
263 | |
264 | // WHEN |
265 | QCOMPARE(backendAxisInput.process(&handler, 45 * s), -0.1f); |
266 | |
267 | // THEN |
268 | QCOMPARE(backendAxisInput.speedRatio(), 0.1f); |
269 | QCOMPARE(backendAxisInput.lastUpdateTime(), 45 * s); |
270 | |
271 | // WHEN |
272 | QCOMPARE(backendAxisInput.process(&handler, 46 * s), 0.0f); |
273 | |
274 | // THEN |
275 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
276 | QCOMPARE(backendAxisInput.lastUpdateTime(), 0); |
277 | } |
278 | |
279 | void shouldNotProcessWhenDisabled() |
280 | { |
281 | const qint64 s = 1000000000; |
282 | |
283 | // GIVEN |
284 | TestDeviceIntegration deviceIntegration; |
285 | TestDevice *device = deviceIntegration.createPhysicalDevice(name: "keyboard" ); |
286 | TestDeviceBackendNode *deviceBackend = deviceIntegration.physicalDevice(id: device->id()); |
287 | Qt3DInput::Input::InputHandler handler; |
288 | handler.addInputDeviceIntegration(inputIntegration: &deviceIntegration); |
289 | |
290 | Qt3DInput::Input::ButtonAxisInput backendAxisInput; |
291 | Qt3DInput::QButtonAxisInput axisInput; |
292 | axisInput.setEnabled(false); |
293 | axisInput.setButtons(QVector<int>() << Qt::Key_Space); |
294 | axisInput.setScale(-1.0f); |
295 | axisInput.setAcceleration(0.15f); |
296 | axisInput.setDeceleration(0.3f); |
297 | axisInput.setSourceDevice(device); |
298 | simulateInitializationSync(frontend: &axisInput, backend: &backendAxisInput); |
299 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
300 | QCOMPARE(backendAxisInput.lastUpdateTime(), 0); |
301 | |
302 | // WHEN (accelerate) |
303 | deviceBackend->setButtonPressed(buttonIdentifier: Qt::Key_Space, pressed: true); |
304 | |
305 | // WHEN |
306 | QCOMPARE(backendAxisInput.process(&handler, 30 * s), 0.0f); |
307 | |
308 | // THEN |
309 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
310 | QCOMPARE(backendAxisInput.lastUpdateTime(), 0); |
311 | |
312 | // WHEN |
313 | QCOMPARE(backendAxisInput.process(&handler, 31 * s), 0.0f); |
314 | |
315 | // THEN |
316 | QCOMPARE(backendAxisInput.speedRatio(), 0.0f); |
317 | QCOMPARE(backendAxisInput.lastUpdateTime(), 0); |
318 | } |
319 | }; |
320 | |
321 | QTEST_APPLESS_MAIN(tst_ButtonAxisInput) |
322 | |
323 | #include "tst_buttonaxisinput.moc" |
324 | |