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 | |
33 | #include <Qt3DInput/QAbstractAxisInput> |
34 | #include <Qt3DInput/QAbstractPhysicalDevice> |
35 | #include <Qt3DInput/private/qabstractaxisinput_p.h> |
36 | |
37 | #include "testpostmanarbiter.h" |
38 | #include "testdevice.h" |
39 | |
40 | class DummyAxisInput : public Qt3DInput::QAbstractAxisInput |
41 | { |
42 | Q_OBJECT |
43 | public: |
44 | explicit DummyAxisInput(QNode *parent = nullptr) |
45 | : Qt3DInput::QAbstractAxisInput(*new Qt3DInput::QAbstractAxisInputPrivate, parent) |
46 | { |
47 | } |
48 | }; |
49 | |
50 | class tst_QAbstractAxisInput: public QObject |
51 | { |
52 | Q_OBJECT |
53 | public: |
54 | tst_QAbstractAxisInput() |
55 | { |
56 | qRegisterMetaType<Qt3DInput::QAbstractPhysicalDevice*>(typeName: "Qt3DInput::QAbstractPhysicalDevice*" ); |
57 | qRegisterMetaType<Qt3DCore::QNode*>(typeName: "Qt3DCore::QNode*" ); |
58 | } |
59 | |
60 | private Q_SLOTS: |
61 | void checkPropertyUpdates() |
62 | { |
63 | // GIVEN |
64 | TestArbiter arbiter; |
65 | QScopedPointer<Qt3DInput::QAbstractAxisInput> axisInput(new DummyAxisInput()); |
66 | arbiter.setArbiterOnNode(axisInput.data()); |
67 | |
68 | // WHEN |
69 | TestDevice *device = new TestDevice(axisInput.data()); |
70 | QCoreApplication::processEvents(); |
71 | arbiter.events.clear(); |
72 | |
73 | axisInput->setSourceDevice(device); |
74 | |
75 | // THEN |
76 | QCOMPARE(arbiter.dirtyNodes.size(), 1); |
77 | QCOMPARE(arbiter.dirtyNodes.front(), axisInput.data()); |
78 | |
79 | arbiter.dirtyNodes.clear(); |
80 | } |
81 | |
82 | void checkSourceDeviceBookkeeping() |
83 | { |
84 | // GIVEN |
85 | QScopedPointer<Qt3DInput::QAbstractAxisInput> axisInput(new DummyAxisInput); |
86 | { |
87 | // WHEN |
88 | Qt3DInput::QAbstractPhysicalDevice device; |
89 | axisInput->setSourceDevice(&device); |
90 | |
91 | // THEN |
92 | QCOMPARE(device.parent(), axisInput.data()); |
93 | QCOMPARE(axisInput->sourceDevice(), &device); |
94 | } |
95 | // THEN (Should not crash and effect be unset) |
96 | QVERIFY(axisInput->sourceDevice() == nullptr); |
97 | |
98 | { |
99 | // WHEN |
100 | DummyAxisInput someOtherAxisInput; |
101 | QScopedPointer<Qt3DInput::QAbstractPhysicalDevice> device(new Qt3DInput::QAbstractPhysicalDevice(&someOtherAxisInput)); |
102 | axisInput->setSourceDevice(device.data()); |
103 | |
104 | // THEN |
105 | QCOMPARE(device->parent(), &someOtherAxisInput); |
106 | QCOMPARE(axisInput->sourceDevice(), device.data()); |
107 | |
108 | // WHEN |
109 | axisInput.reset(); |
110 | device.reset(); |
111 | |
112 | // THEN Should not crash when the device is destroyed (tests for failed removal of destruction helper) |
113 | } |
114 | } |
115 | }; |
116 | |
117 | QTEST_MAIN(tst_QAbstractAxisInput) |
118 | |
119 | #include "tst_qabstractaxisinput.moc" |
120 | |