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 | |
30 | #include <QtTest/QTest> |
31 | #include <Qt3DInput/qmousedevice.h> |
32 | #include <Qt3DInput/private/qmousedevice_p.h> |
33 | #include <Qt3DInput/private/mousedevice_p.h> |
34 | #include "qbackendnodetester.h" |
35 | |
36 | class tst_MouseDevice : public Qt3DCore::QBackendNodeTester |
37 | { |
38 | Q_OBJECT |
39 | |
40 | private Q_SLOTS: |
41 | |
42 | void checkInitialState() |
43 | { |
44 | // GIVEN |
45 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
46 | |
47 | // THEN |
48 | QCOMPARE(backendMouseDevice.isEnabled(), false); |
49 | QVERIFY(backendMouseDevice.peerId().isNull()); |
50 | QVERIFY(backendMouseDevice.inputHandler() == nullptr); |
51 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
52 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
53 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
54 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
55 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, false); |
56 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
57 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
58 | QCOMPARE(backendMouseDevice.previousPos(), QPointF()); |
59 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
60 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
61 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
62 | } |
63 | |
64 | void checkInitializeFromPeer() |
65 | { |
66 | // GIVEN |
67 | Qt3DInput::QMouseDevice mouseDevice; |
68 | mouseDevice.setSensitivity(0.8f); |
69 | |
70 | { |
71 | // WHEN |
72 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
73 | simulateInitializationSync(frontend: &mouseDevice, backend: &backendMouseDevice); |
74 | |
75 | // THEN |
76 | QCOMPARE(backendMouseDevice.isEnabled(), true); |
77 | QCOMPARE(backendMouseDevice.peerId(), mouseDevice.id()); |
78 | QVERIFY(backendMouseDevice.inputHandler() == nullptr); |
79 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
80 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
81 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
82 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
83 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, false); |
84 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
85 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
86 | QCOMPARE(backendMouseDevice.previousPos(), QPointF()); |
87 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
88 | QCOMPARE(backendMouseDevice.sensitivity(), 0.8f); |
89 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
90 | } |
91 | { |
92 | // WHEN |
93 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
94 | mouseDevice.setEnabled(false); |
95 | simulateInitializationSync(frontend: &mouseDevice, backend: &backendMouseDevice); |
96 | |
97 | // THEN |
98 | QCOMPARE(backendMouseDevice.peerId(), mouseDevice.id()); |
99 | QCOMPARE(backendMouseDevice.isEnabled(), false); |
100 | } |
101 | } |
102 | |
103 | void checkMouseMoveStatesUpdates() |
104 | { |
105 | // GIVEN |
106 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
107 | |
108 | // WHEN |
109 | auto eventList = QList<QT_PREPEND_NAMESPACE(QMouseEvent)>() << QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseButtonPress, |
110 | QPointF(400.0f, 400.0f), |
111 | QPointF(400.0f, 400.0f), |
112 | QPointF(400.0f, 400.0f), |
113 | Qt::LeftButton, |
114 | Qt::LeftButton, |
115 | Qt::NoModifier); |
116 | backendMouseDevice.updateMouseEvents(events: eventList); |
117 | |
118 | // THEN |
119 | // Note: axis are only modified when moving (> 1 event) |
120 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
121 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
122 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
123 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
124 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, true); |
125 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
126 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
127 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(400.0f, 400.0f)); |
128 | QCOMPARE(backendMouseDevice.wasPressed(), true); |
129 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
130 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
131 | |
132 | // WHEN |
133 | eventList = QList<QT_PREPEND_NAMESPACE(QMouseEvent)>() << QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseMove, |
134 | QPointF(600.0f, 600.0f), |
135 | QPointF(600.0f, 600.0f), |
136 | QPointF(600.0f, 600.0f), |
137 | Qt::LeftButton, |
138 | Qt::LeftButton, |
139 | Qt::NoModifier); |
140 | backendMouseDevice.updateMouseEvents(events: eventList); |
141 | |
142 | // THEN |
143 | QCOMPARE(backendMouseDevice.mouseState().xAxis, (600.0f - 400.0f) * 0.1f); |
144 | QCOMPARE(backendMouseDevice.mouseState().yAxis, (400.0f - 600.0f) * 0.1f); |
145 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
146 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
147 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, true); |
148 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
149 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
150 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(600.0f, 600.0f)); |
151 | QCOMPARE(backendMouseDevice.wasPressed(), true); |
152 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
153 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
154 | |
155 | // WHEN |
156 | eventList = QList<QT_PREPEND_NAMESPACE(QMouseEvent)>() << QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseButtonRelease, |
157 | QPointF(800.0f, 800.0f), |
158 | QPointF(800.0f, 800.0f), |
159 | QPointF(800.0f, 800.0f), |
160 | Qt::LeftButton, |
161 | Qt::NoButton, |
162 | Qt::NoModifier); |
163 | backendMouseDevice.updateMouseEvents(events: eventList); |
164 | |
165 | // THEN |
166 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
167 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
168 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
169 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
170 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, false); |
171 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
172 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
173 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(800.0f, 800.0f)); |
174 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
175 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
176 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
177 | |
178 | // WHEN |
179 | eventList = QList<QT_PREPEND_NAMESPACE(QMouseEvent)>() << QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseMove, |
180 | QPointF(900.0f, 900.0f), |
181 | QPointF(900.0f, 900.0f), |
182 | QPointF(900.0f, 900.0f), |
183 | Qt::NoButton, |
184 | Qt::NoButton, |
185 | Qt::NoModifier); |
186 | |
187 | // THEN -> no axes update |
188 | backendMouseDevice.updateMouseEvents(events: eventList); |
189 | QCOMPARE(backendMouseDevice.mouseState().xAxis, 0.0f); |
190 | QCOMPARE(backendMouseDevice.mouseState().yAxis, 0.0f); |
191 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
192 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
193 | QCOMPARE(backendMouseDevice.mouseState().leftPressed, false); |
194 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
195 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
196 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(900.0f, 900.0f)); |
197 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
198 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
199 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), false); |
200 | |
201 | |
202 | // WHEN |
203 | eventList = QList<QT_PREPEND_NAMESPACE(QMouseEvent)>() << QT_PREPEND_NAMESPACE(QMouseEvent)(QMouseEvent::MouseMove, |
204 | QPointF(1000.0f, 1000.0f), |
205 | QPointF(1000.0f, 1000.0f), |
206 | QPointF(1000.0f, 1000.0f), |
207 | Qt::NoButton, |
208 | Qt::NoButton, |
209 | Qt::NoModifier); |
210 | |
211 | Qt3DInput::QMouseDevice mouseDevice; |
212 | mouseDevice.setUpdateAxesContinuously(true); |
213 | backendMouseDevice.syncFromFrontEnd(frontEnd: &mouseDevice, firstTime: false); |
214 | backendMouseDevice.updateMouseEvents(events: eventList); |
215 | |
216 | // THEN |
217 | QCOMPARE(backendMouseDevice.mouseState().xAxis, (1000.0f - 900.0f) * 0.1f); |
218 | QCOMPARE(backendMouseDevice.mouseState().yAxis, (900.0f - 1000.0f) * 0.1f); |
219 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
220 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
221 | QCOMPARE(backendMouseDevice.mouseState().leftPressed,false); |
222 | QCOMPARE(backendMouseDevice.mouseState().rightPressed, false); |
223 | QCOMPARE(backendMouseDevice.mouseState().centerPressed, false); |
224 | QCOMPARE(backendMouseDevice.previousPos(), QPointF(1000.0f, 1000.0f)); |
225 | QCOMPARE(backendMouseDevice.wasPressed(), false); |
226 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
227 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), true); |
228 | } |
229 | |
230 | void checkMouseWheelState() |
231 | { |
232 | // GIVEN |
233 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
234 | |
235 | // WHEN |
236 | auto eventList = QList<QT_PREPEND_NAMESPACE(QWheelEvent)>() << QT_PREPEND_NAMESPACE(QWheelEvent)(QPointF(500.0f, 500.0f), |
237 | 120, |
238 | Qt::NoButton, |
239 | Qt::NoModifier, |
240 | Qt::Vertical); |
241 | backendMouseDevice.updateWheelEvents(events: eventList); |
242 | |
243 | // THEN |
244 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
245 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.1f * 120); |
246 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
247 | |
248 | // WHEN |
249 | eventList = QList<QT_PREPEND_NAMESPACE(QWheelEvent)>() << QT_PREPEND_NAMESPACE(QWheelEvent)(QPointF(500.0f, 500.0f), |
250 | 120, |
251 | Qt::NoButton, |
252 | Qt::NoModifier, |
253 | Qt::Horizontal); |
254 | backendMouseDevice.updateWheelEvents(events: eventList); |
255 | |
256 | // THEN |
257 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.1f * 120); |
258 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
259 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
260 | |
261 | // WHEN |
262 | eventList = QList<QT_PREPEND_NAMESPACE(QWheelEvent)>() << QT_PREPEND_NAMESPACE(QWheelEvent)(QPointF(500.0f, 500.0f), |
263 | 0, |
264 | Qt::NoButton, |
265 | Qt::NoModifier, |
266 | Qt::Horizontal); |
267 | backendMouseDevice.updateWheelEvents(events: eventList); |
268 | |
269 | // THEN |
270 | QCOMPARE(backendMouseDevice.mouseState().wXAxis, 0.0f); |
271 | QCOMPARE(backendMouseDevice.mouseState().wYAxis, 0.0f); |
272 | QCOMPARE(backendMouseDevice.sensitivity(), 0.1f); |
273 | } |
274 | |
275 | void checkSceneChangeEvents() |
276 | { |
277 | // GIVEN |
278 | Qt3DInput::QMouseDevice mouseDevice; |
279 | Qt3DInput::Input::MouseDevice backendMouseDevice; |
280 | simulateInitializationSync(frontend: &mouseDevice, backend: &backendMouseDevice); |
281 | |
282 | { |
283 | // WHEN |
284 | const bool newValue = false; |
285 | mouseDevice.setEnabled(newValue); |
286 | backendMouseDevice.syncFromFrontEnd(frontEnd: &mouseDevice, firstTime: false); |
287 | |
288 | // THEN |
289 | QCOMPARE(backendMouseDevice.isEnabled(), newValue); |
290 | } |
291 | { |
292 | // WHEN |
293 | const float newValue = 99.0f; |
294 | mouseDevice.setSensitivity(newValue); |
295 | backendMouseDevice.syncFromFrontEnd(frontEnd: &mouseDevice, firstTime: false); |
296 | |
297 | // THEN |
298 | QCOMPARE(backendMouseDevice.sensitivity(), newValue); |
299 | } |
300 | { |
301 | // WHEN |
302 | const bool newValue = true; |
303 | mouseDevice.setUpdateAxesContinuously(newValue); |
304 | backendMouseDevice.syncFromFrontEnd(frontEnd: &mouseDevice, firstTime: false); |
305 | |
306 | // THEN |
307 | QCOMPARE(backendMouseDevice.updateAxesContinuously(), newValue); |
308 | } |
309 | } |
310 | |
311 | }; |
312 | |
313 | QTEST_MAIN(tst_MouseDevice) |
314 | |
315 | #include "tst_mousedevice.moc" |
316 | |