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 | |
32 | #include <Qt3DCore/private/qnode_p.h> |
33 | #include <Qt3DCore/private/qscene_p.h> |
34 | |
35 | #include <Qt3DInput/private/inputhandler_p.h> |
36 | #include <Qt3DInput/private/inputmanagers_p.h> |
37 | #include <Qt3DInput/QKeyboardDevice> |
38 | #include <Qt3DInput/private/keyboarddevice_p.h> |
39 | #include <Qt3DInput/QKeyboardHandler> |
40 | #include <Qt3DInput/private/keyboardhandler_p.h> |
41 | |
42 | class tst_KeyboardHandler : public Qt3DCore::QBackendNodeTester |
43 | { |
44 | Q_OBJECT |
45 | |
46 | private Q_SLOTS: |
47 | |
48 | void checkPeerPropertyMirroring() |
49 | { |
50 | // GIVEN |
51 | Qt3DInput::Input::InputHandler inputHandler; |
52 | |
53 | auto keyboardDevice = new Qt3DInput::QKeyboardDevice; |
54 | auto backendKeyboardDevice = inputHandler.keyboardDeviceManager()->getOrCreateResource(id: keyboardDevice->id()); |
55 | backendKeyboardDevice->setInputHandler(&inputHandler); |
56 | simulateInitializationSync(frontend: keyboardDevice, backend: backendKeyboardDevice); |
57 | |
58 | Qt3DInput::QKeyboardHandler keyboardHandler; |
59 | auto backendKeyboardHandler = inputHandler.keyboardInputManager()->getOrCreateResource(id: keyboardHandler.id()); |
60 | backendKeyboardHandler->setInputHandler(&inputHandler); |
61 | |
62 | keyboardHandler.setFocus(true); |
63 | keyboardHandler.setSourceDevice(keyboardDevice); |
64 | |
65 | // WHEN |
66 | simulateInitializationSync(frontend: &keyboardHandler, backend: backendKeyboardHandler); |
67 | |
68 | // THEN |
69 | QCOMPARE(backendKeyboardHandler->peerId(), keyboardHandler.id()); |
70 | QCOMPARE(backendKeyboardHandler->isEnabled(), keyboardHandler.isEnabled()); |
71 | QCOMPARE(backendKeyboardDevice->lastKeyboardInputRequester(), backendKeyboardHandler->peerId()); |
72 | QCOMPARE(backendKeyboardHandler->keyboardDevice(), keyboardDevice->id()); |
73 | } |
74 | |
75 | void checkInitialState() |
76 | { |
77 | // GIVEN |
78 | Qt3DInput::Input::KeyboardHandler backendKeyboardHandler; |
79 | |
80 | // THEN |
81 | QVERIFY(backendKeyboardHandler.peerId().isNull()); |
82 | QCOMPARE(backendKeyboardHandler.isEnabled(), false); |
83 | QCOMPARE(backendKeyboardHandler.focus(), false); |
84 | QCOMPARE(backendKeyboardHandler.keyboardDevice(), Qt3DCore::QNodeId()); |
85 | } |
86 | |
87 | void checkPropertyChanges() |
88 | { |
89 | // GIVEN |
90 | Qt3DInput::Input::InputHandler inputHandler; |
91 | |
92 | Qt3DInput::QKeyboardHandler keyboardHandler; |
93 | keyboardHandler.setEnabled(false); |
94 | Qt3DInput::Input::KeyboardHandler backendKeyboardHandler; |
95 | backendKeyboardHandler.setInputHandler(&inputHandler); |
96 | |
97 | Qt3DInput::QKeyboardDevice device; |
98 | Qt3DInput::Input::KeyboardDevice *backendKeyboardDevice = |
99 | inputHandler.keyboardDeviceManager()->getOrCreateResource(id: device.id()); |
100 | backendKeyboardDevice->setInputHandler(&inputHandler); |
101 | |
102 | simulateInitializationSync(frontend: &keyboardHandler, backend: &backendKeyboardHandler); |
103 | |
104 | // WHEN |
105 | keyboardHandler.setSourceDevice(&device); |
106 | backendKeyboardHandler.syncFromFrontEnd(frontEnd: &keyboardHandler, firstTime: false); |
107 | |
108 | // THEN |
109 | QCOMPARE(backendKeyboardHandler.keyboardDevice(), device.id()); |
110 | |
111 | // WHEN (still disabled, nothing should happen) |
112 | keyboardHandler.setFocus(true); |
113 | backendKeyboardHandler.syncFromFrontEnd(frontEnd: &keyboardHandler, firstTime: false); |
114 | |
115 | // THEN |
116 | QVERIFY(backendKeyboardDevice->lastKeyboardInputRequester().isNull()); |
117 | |
118 | // WHEN |
119 | keyboardHandler.setEnabled(true); |
120 | backendKeyboardHandler.syncFromFrontEnd(frontEnd: &keyboardHandler, firstTime: false); |
121 | |
122 | // THEN |
123 | QCOMPARE(backendKeyboardHandler.isEnabled(), true); |
124 | |
125 | // WHEN (now enabled, should request focus) |
126 | keyboardHandler.setFocus(true); |
127 | backendKeyboardHandler.syncFromFrontEnd(frontEnd: &keyboardHandler, firstTime: false); |
128 | |
129 | // THEN |
130 | QCOMPARE(backendKeyboardDevice->lastKeyboardInputRequester(), backendKeyboardHandler.peerId()); |
131 | } |
132 | }; |
133 | |
134 | QTEST_APPLESS_MAIN(tst_KeyboardHandler) |
135 | |
136 | #include "tst_keyboardhandler.moc" |
137 | |