1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSensors 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 "test_sensorimpl.h" |
30 | #include "test_sensor2impl.h" |
31 | #include <qsensorplugin.h> |
32 | #include <qsensorbackend.h> |
33 | #include <qsensormanager.h> |
34 | #include <QFile> |
35 | #include <QDebug> |
36 | #include <QTest> |
37 | |
38 | class TestSensorPlugin : public QObject, |
39 | public QSensorPluginInterface, |
40 | public QSensorChangesInterface, |
41 | public QSensorBackendFactory |
42 | { |
43 | Q_OBJECT |
44 | Q_PLUGIN_METADATA(IID "com.qt-project.Qt.QSensorPluginInterface/1.0" ) |
45 | Q_INTERFACES(QSensorPluginInterface QSensorChangesInterface) |
46 | public: |
47 | void registerSensors() override |
48 | { |
49 | static bool recursive = false; |
50 | QVERIFY2(!recursive, "Recursively called TestSensorPlugin::registerSensors!" ); |
51 | if (recursive) return; |
52 | recursive = true; |
53 | |
54 | |
55 | // This is bad code. It caused a crash due to recursively calling |
56 | // loadPlugins() in qsensormanager.cpp (because loadPlugins() did |
57 | // not set the pluginsLoaded flag soon enough). |
58 | (void)QSensor::defaultSensorForType(type: TestSensor::type); |
59 | |
60 | QSensorManager::registerBackend(type: TestSensor::type, identifier: testsensorimpl::id, factory: this); |
61 | QSensorManager::registerBackend(type: TestSensor::type, identifier: "test sensor 2" , factory: this); |
62 | QSensorManager::registerBackend(type: TestSensor2::type, identifier: testsensor2impl::id, factory: this); |
63 | } |
64 | |
65 | void sensorsChanged() override |
66 | { |
67 | // Register a new type on initial load |
68 | // This is testing the "don't emit availableSensorsChanged() too many times" functionality. |
69 | if (!QSensorManager::isBackendRegistered(type: TestSensor::type, identifier: "test sensor 3" )) |
70 | QSensorManager::registerBackend(type: TestSensor::type, identifier: "test sensor 3" , factory: this); |
71 | |
72 | // When a sensor of type "a random type" is registered, register another sensor. |
73 | // This is testing the "don't emit availableSensorsChanged() too many times" functionality. |
74 | if (!QSensor::defaultSensorForType(type: "a random type" ).isEmpty()) { |
75 | if (!QSensorManager::isBackendRegistered(type: "a random type 2" , identifier: "random.dynamic" )) |
76 | QSensorManager::registerBackend(type: "a random type 2" , identifier: "random.dynamic" , factory: this); |
77 | } else { |
78 | if (QSensorManager::isBackendRegistered(type: "a random type 2" , identifier: "random.dynamic" )) |
79 | QSensorManager::unregisterBackend(type: "a random type 2" , identifier: "random.dynamic" ); |
80 | } |
81 | } |
82 | |
83 | QSensorBackend *createBackend(QSensor *sensor) override |
84 | { |
85 | if (sensor->identifier() == testsensorimpl::id) { |
86 | return new testsensorimpl(sensor); |
87 | } |
88 | if (sensor->identifier() == testsensor2impl::id) { |
89 | return new testsensor2impl(sensor); |
90 | } |
91 | |
92 | qWarning() << "Can't create backend" << sensor->identifier(); |
93 | return 0; |
94 | } |
95 | }; |
96 | |
97 | Q_IMPORT_PLUGIN(TestSensorPlugin) |
98 | |
99 | #include "test_sensorplugin.moc" |
100 | |