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

source code of qt3d/tests/auto/input/qactioninput/tst_qactioninput.cpp