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/QKeyboardDevice> |
35 | #include <Qt3DInput/QKeyboardHandler> |
36 | #include <Qt3DInput/private/qkeyboardhandler_p.h> |
37 | |
38 | #include "testpostmanarbiter.h" |
39 | |
40 | class tst_QKeyboardHandler : public QObject |
41 | { |
42 | Q_OBJECT |
43 | public: |
44 | tst_QKeyboardHandler() |
45 | { |
46 | qRegisterMetaType<Qt3DInput::QKeyboardDevice*>(typeName: "Qt3DInput::QKeyboardDevice*" ); |
47 | } |
48 | |
49 | private Q_SLOTS: |
50 | void checkCloning_data() |
51 | { |
52 | QTest::addColumn<Qt3DInput::QKeyboardHandler *>(name: "keyboardHandler" ); |
53 | |
54 | auto defaultConstructed = new Qt3DInput::QKeyboardHandler; |
55 | QTest::newRow(dataTag: "defaultConstructed" ) << defaultConstructed; |
56 | |
57 | auto handlerWithDevice = new Qt3DInput::QKeyboardHandler; |
58 | handlerWithDevice->setSourceDevice(new Qt3DInput::QKeyboardDevice); |
59 | QTest::newRow(dataTag: "handlerWithDevice" ) << handlerWithDevice; |
60 | |
61 | auto handlerWithDeviceAndFocus = new Qt3DInput::QKeyboardHandler; |
62 | handlerWithDeviceAndFocus->setSourceDevice(new Qt3DInput::QKeyboardDevice); |
63 | handlerWithDeviceAndFocus->setFocus(true); |
64 | QTest::newRow(dataTag: "handlerWithDeviceAndFocus" ) << handlerWithDeviceAndFocus; |
65 | } |
66 | |
67 | void checkCloning() |
68 | { |
69 | // GIVEN |
70 | QFETCH(Qt3DInput::QKeyboardHandler *, keyboardHandler); |
71 | |
72 | // WHEN |
73 | Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(keyboardHandler); |
74 | QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges = creationChangeGenerator.creationChanges(); |
75 | |
76 | // THEN |
77 | QCOMPARE(creationChanges.size(), 1 + (keyboardHandler->sourceDevice() ? 1 : 0)); |
78 | |
79 | auto creationChangeData = |
80 | qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DInput::QKeyboardHandlerData>>(src: creationChanges.first()); |
81 | const Qt3DInput::QKeyboardHandlerData &cloneData = creationChangeData->data; |
82 | QCOMPARE(keyboardHandler->id(), creationChangeData->subjectId()); |
83 | QCOMPARE(keyboardHandler->isEnabled(), creationChangeData->isNodeEnabled()); |
84 | QCOMPARE(keyboardHandler->metaObject(), creationChangeData->metaObject()); |
85 | QCOMPARE(keyboardHandler->focus(), cloneData.focus); |
86 | QCOMPARE(keyboardHandler->sourceDevice() ? keyboardHandler->sourceDevice()->id() : Qt3DCore::QNodeId(), cloneData.keyboardDeviceId); |
87 | } |
88 | |
89 | void checkPropertyUpdates() |
90 | { |
91 | // GIVEN |
92 | TestArbiter arbiter; |
93 | QScopedPointer<Qt3DInput::QKeyboardHandler> keyboardHandler(new Qt3DInput::QKeyboardHandler); |
94 | arbiter.setArbiterOnNode(keyboardHandler.data()); |
95 | |
96 | // WHEN |
97 | keyboardHandler->setFocus(true); |
98 | |
99 | // THEN |
100 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
101 | QCOMPARE(arbiter.dirtyNodes.front(), keyboardHandler.data()); |
102 | |
103 | arbiter.dirtyNodes.clear(); |
104 | |
105 | // WHEN |
106 | auto device = new Qt3DInput::QKeyboardDevice(keyboardHandler.data()); |
107 | QCoreApplication::processEvents(); |
108 | arbiter.events.clear(); |
109 | |
110 | keyboardHandler->setSourceDevice(device); |
111 | |
112 | // THEN |
113 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
114 | QCOMPARE(arbiter.dirtyNodes.front(), keyboardHandler.data()); |
115 | |
116 | arbiter.dirtyNodes.clear(); |
117 | } |
118 | |
119 | void checkSourceDeviceBookkeeping() |
120 | { |
121 | // GIVEN |
122 | QScopedPointer<Qt3DInput::QKeyboardHandler> keyboardHandler(new Qt3DInput::QKeyboardHandler); |
123 | { |
124 | // WHEN |
125 | Qt3DInput::QKeyboardDevice device; |
126 | keyboardHandler->setSourceDevice(&device); |
127 | |
128 | // THEN |
129 | QCOMPARE(device.parent(), keyboardHandler.data()); |
130 | QCOMPARE(keyboardHandler->sourceDevice(), &device); |
131 | } |
132 | // THEN (Should not crash and effect be unset) |
133 | QVERIFY(keyboardHandler->sourceDevice() == nullptr); |
134 | |
135 | { |
136 | // WHEN |
137 | Qt3DInput::QKeyboardHandler someOtherKeyboardHandler; |
138 | QScopedPointer<Qt3DInput::QKeyboardDevice> device(new Qt3DInput::QKeyboardDevice(&someOtherKeyboardHandler)); |
139 | keyboardHandler->setSourceDevice(device.data()); |
140 | |
141 | // THEN |
142 | QCOMPARE(device->parent(), &someOtherKeyboardHandler); |
143 | QCOMPARE(keyboardHandler->sourceDevice(), device.data()); |
144 | |
145 | // WHEN |
146 | keyboardHandler.reset(); |
147 | device.reset(); |
148 | |
149 | // THEN Should not crash when the device is destroyed (tests for failed removal of destruction helper) |
150 | } |
151 | } |
152 | }; |
153 | |
154 | QTEST_MAIN(tst_QKeyboardHandler) |
155 | |
156 | #include "tst_qkeyboardhandler.moc" |
157 | |