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/private/loadproxydevicejob_p.h>
32#include <Qt3DInput/private/inputmanagers_p.h>
33#include <Qt3DInput/private/inputhandler_p.h>
34#include <Qt3DInput/private/physicaldeviceproxy_p.h>
35#include <Qt3DInput/private/qinputdeviceintegration_p.h>
36#include <Qt3DCore/private/qbackendnode_p.h>
37#include "qbackendnodetester.h"
38#include "testdeviceproxy.h"
39#include "testpostmanarbiter.h"
40
41class FakeInputDeviceIntegration : public Qt3DInput::QInputDeviceIntegration
42{
43public:
44 explicit FakeInputDeviceIntegration(const QString &name)
45 : Qt3DInput::QInputDeviceIntegration()
46 , m_name(name)
47 {}
48
49 QVector<Qt3DCore::QAspectJobPtr> jobsToExecute(qint64) override
50 {
51 return QVector<Qt3DCore::QAspectJobPtr>();
52 }
53
54 Qt3DInput::QAbstractPhysicalDevice *createPhysicalDevice(const QString &name) override
55 {
56 if (name == m_name)
57 return new TestPhysicalDevice();
58 return nullptr;
59 }
60
61 Qt3DInput::QAbstractPhysicalDeviceBackendNode *physicalDevice(Qt3DCore::QNodeId) const override
62 {
63 return nullptr;
64 }
65
66 QVector<Qt3DCore::QNodeId> physicalDevices() const override
67 {
68 return QVector<Qt3DCore::QNodeId>();
69 }
70
71 QStringList deviceNames() const override
72 {
73 return QStringList() << m_name;
74 }
75
76private:
77 void onInitialize() override {}
78 QString m_name;
79};
80
81class tst_LoadProxyDeviceJob : public QObject
82{
83 Q_OBJECT
84
85private Q_SLOTS:
86
87 void checkInitialState()
88 {
89 // GIVEN
90 Qt3DInput::Input::LoadProxyDeviceJob backendLoadProxyDeviceJob;
91
92 // THEN
93 QVERIFY(backendLoadProxyDeviceJob.inputHandler() == nullptr);
94 QVERIFY(backendLoadProxyDeviceJob.proxies().empty());
95 }
96
97 void checkJobCreatesDeviceWhenValid()
98 {
99 // GIVEN
100 Qt3DInput::Input::InputHandler inputHandler;
101 Qt3DInput::Input::PhysicalDeviceProxyManager *manager = inputHandler.physicalDeviceProxyManager();
102
103 FakeInputDeviceIntegration inputIntegration(QStringLiteral("TestProxy"));
104 inputHandler.addInputDeviceIntegration(inputIntegration: &inputIntegration);
105
106 // THEN
107 QCOMPARE(inputHandler.inputDeviceIntegrations().size(), 1);
108 QCOMPARE(inputHandler.inputDeviceIntegrations().first(), &inputIntegration);
109
110 // WHEN -> valid device name
111 {
112 // WHEN
113 TestProxy proxy;
114 TestArbiter arbiter;
115 Qt3DInput::Input::PhysicalDeviceProxy *backendProxy = manager->getOrCreateResource(id: proxy.id());
116
117 {
118 backendProxy->setManager(manager);
119 Qt3DCore::QBackendNodeTester backendNodeCreator;
120 backendNodeCreator.simulateInitializationSync(frontend: &proxy, backend: backendProxy);
121 Qt3DCore::QBackendNodePrivate::get(n: backendProxy)->setArbiter(&arbiter);
122 }
123
124 // THEN
125 QCOMPARE(manager->lookupResource(proxy.id()), backendProxy);
126 QCOMPARE(backendProxy->deviceName(), QStringLiteral("TestProxy"));
127 QVERIFY(backendProxy->physicalDeviceId().isNull());
128
129 const QVector<Qt3DCore::QNodeId> pendingProxies = manager->takePendingProxiesToLoad();
130 QCOMPARE(pendingProxies.size(), 1);
131 QCOMPARE(pendingProxies.first(), backendProxy->peerId());
132
133 // WHEN
134 Qt3DInput::Input::LoadProxyDeviceJob job;
135 job.setInputHandler(&inputHandler);
136 job.setProxiesToLoad(std::move(pendingProxies));
137
138 job.run();
139
140 // THEN -> PhysicalDeviceWrapper::setDevice should have been called
141 QVERIFY(!backendProxy->physicalDeviceId().isNull());
142 }
143
144 // WHEN -> invalid name
145 {
146 // WHEN
147 TestProxy proxy(QStringLiteral("NonExisting"));
148 TestArbiter arbiter;
149 Qt3DInput::Input::PhysicalDeviceProxy *backendProxy = manager->getOrCreateResource(id: proxy.id());
150
151 {
152 backendProxy->setManager(manager);
153 Qt3DCore::QBackendNodeTester backendNodeCreator;
154 backendNodeCreator.simulateInitializationSync(frontend: &proxy, backend: backendProxy);
155 Qt3DCore::QBackendNodePrivate::get(n: backendProxy)->setArbiter(&arbiter);
156 }
157
158 // THEN
159 QCOMPARE(manager->lookupResource(proxy.id()), backendProxy);
160 QCOMPARE(backendProxy->deviceName(), QStringLiteral("NonExisting"));
161
162 const QVector<Qt3DCore::QNodeId> pendingProxies = manager->takePendingProxiesToLoad();
163 QCOMPARE(pendingProxies.size(), 1);
164 QCOMPARE(pendingProxies.first(), backendProxy->peerId());
165
166 // WHEN
167 Qt3DInput::Input::LoadProxyDeviceJob job;
168 job.setInputHandler(&inputHandler);
169 job.setProxiesToLoad(std::move(pendingProxies));
170
171 job.run();
172
173 // THEN -> PhysicalDeviceWrapper::setDevice should not have been called
174 QCOMPARE(arbiter.events.count(), 0);
175 }
176 }
177
178};
179
180QTEST_MAIN(tst_LoadProxyDeviceJob)
181
182#include "tst_loadproxydevicejob.moc"
183

source code of qt3d/tests/auto/input/loadproxydevicejob/tst_loadproxydevicejob.cpp