on xenial help sign in
work around a conflict between Q_GLOBAL_STATIC and qmlRegisterSingletonType
325be33Michael Zanetti
tests/mocks/Unity/InputInfo/CMakeLists.txt+1
@@ -17,6 +17,7 @@ add_definitions(-DUNITY_MOCKS)
17set(MockInputInfo_SOURCES
17set(MockInputInfo_SOURCES
18 plugin.cpp
18 plugin.cpp
19 qinputdeviceinfo_mock.cpp
19 qinputdeviceinfo_mock.cpp
20+ mockcontroller.cpp
20 ${CMAKE_SOURCE_DIR}/plugins/Unity/InputInfo/qinputinfo.cpp
21 ${CMAKE_SOURCE_DIR}/plugins/Unity/InputInfo/qinputinfo.cpp
21 ${CMAKE_SOURCE_DIR}/plugins/Unity/InputInfo/qdeclarativeinputdevicemodel.cpp
22 ${CMAKE_SOURCE_DIR}/plugins/Unity/InputInfo/qdeclarativeinputdevicemodel.cpp
22)
23)
tests/mocks/Unity/InputInfo/mockcontroller.cppnew file+19
@@ -0,0 +1,19 @@
1+#include "mockcontroller.h"
2+
3+#include "qinputdeviceinfo_mock_p.h"
4+
5+MockController::MockController(QObject *parent):
6+ QObject(parent)
7+{
8+
9+}
10+
11+QInputDevice *MockController::addMockDevice(const QString &devicePath, QInputDevice::InputType type)
12+{
13+ return QInputDeviceManagerPrivate::instance()->addMockDevice(devicePath, type);
14+}
15+
16+void MockController::removeDevice(const QString &devicePath)
17+{
18+ QInputDeviceManagerPrivate::instance()->removeDevice(devicePath);
19+}
tests/mocks/Unity/InputInfo/mockcontroller.hnew file+34
@@ -0,0 +1,34 @@
1+/*
2+ * Copyright 2015 Canonical Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU Lesser General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU Lesser General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU Lesser General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef MOCKCONTROLLER_H
18+#define MOCKCONTROLLER_H
19+
20+#include <QObject>
21+#include "qinputinfo.h"
22+
23+class MockController: public QObject
24+{
25+ Q_OBJECT
26+public:
27+ MockController(QObject *parent = 0);
28+ ~MockController() = default;
29+
30+ Q_INVOKABLE QInputDevice* addMockDevice(const QString &devicePath, QInputDevice::InputType type);
31+ Q_INVOKABLE void removeDevice(const QString &devicePath);
32+};
33+
34+#endif
tests/mocks/Unity/InputInfo/plugin.cpp+8−4
@@ -15,20 +15,20 @@
15 */
15 */
16
16
17// Qt
17// Qt
18−#include <QtQml/qqml.h>
18+#include <QtQml>
19
19
20// self
20// self
21#include "plugin.h"
21#include "plugin.h"
22
22
23// local
23// local
24#include "qdeclarativeinputdevicemodel_p.h"
24#include "qdeclarativeinputdevicemodel_p.h"
25−#include "qinputdeviceinfo_mock_p.h"
25+#include "mockcontroller.h"
26
26
27static QObject *backendProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
27static QObject *backendProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
28{
28{
29 Q_UNUSED(engine)
29 Q_UNUSED(engine)
30 Q_UNUSED(scriptEngine)
30 Q_UNUSED(scriptEngine)
31− return QInputDeviceManagerPrivate::instance();
31+ return new MockController(engine);
32}
32}
33
33
34void InputInfoPlugin::registerTypes(const char *uri)
34void InputInfoPlugin::registerTypes(const char *uri)
@@ -38,5 +38,9 @@ void InputInfoPlugin::registerTypes(const char *uri)
38 qmlRegisterType<QDeclarativeInputDeviceModel>(uri, major, minor, "InputDeviceModel");
38 qmlRegisterType<QDeclarativeInputDeviceModel>(uri, major, minor, "InputDeviceModel");
39 qmlRegisterType<QInputDevice>(uri, major, minor, "InputInfo");
39 qmlRegisterType<QInputDevice>(uri, major, minor, "InputInfo");
40
40
41+ // We can't register the MockInputDeviceBackend directly because QML wants to delete singletons on its own
42+ // Given that MockInputDeviceBackend is a Q_GLOBAL_STATIC it will also be cleaned up by other Qt internals
43+ // This leads to a double-free on shutdown. So let's add a proxy to control the MockBackend through QML:
44+ // MockController
41− qmlRegisterSingletonType<QInputDeviceManagerPrivate>(uri, major, minor, "MockInputDeviceBackend", backendProvider);
45+ qmlRegisterSingletonType<MockController>(uri, major, minor, "MockInputDeviceBackend", backendProvider);
42}
46}
tests/mocks/Unity/InputInfo/qinputdeviceinfo_mock.cpp+16−41
@@ -1,43 +1,18 @@
1−/****************************************************************************
1+/*
2−**
3−** Copyright (C) 2014 Canonical, Ltd. and/or its subsidiary(-ies).
2+ * Copyright 2015 Canonical Ltd.
4−** Contact: http://www.qt-project.org/legal
3+ *
5−**
4+ * This program is free software; you can redistribute it and/or modify
6−** This file is part of the QtSystems module of the Qt Toolkit.
5+ * it under the terms of the GNU Lesser General Public License as published by
7−**
6+ * the Free Software Foundation; version 3.
8−** $QT_BEGIN_LICENSE:LGPL$
7+ *
9−** Commercial License Usage
8+ * This program is distributed in the hope that it will be useful,
10−** Licensees holding valid commercial Qt licenses may use this file in
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11−** accordance with the commercial license agreement provided with the
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12−** Software or, alternatively, in accordance with the terms contained in
11+ * GNU Lesser General Public License for more details.
13−** a written agreement between you and Digia. For licensing terms and
12+ *
14−** conditions see http://qt.digia.com/licensing. For further information
13+ * You should have received a copy of the GNU Lesser General Public License
15−** use the contact form at http://qt.digia.com/contact-us.
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16−**
15+ */
17−** GNU Lesser General Public License Usage
18−** Alternatively, this file may be used under the terms of the GNU Lesser
19−** General Public License version 2.1 as published by the Free Software
20−** Foundation and appearing in the file LICENSE.LGPL included in the
21−** packaging of this file. Please review the following information to
22−** ensure the GNU Lesser General Public License version 2.1 requirements
23−** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24−**
25−** In addition, as a special exception, Digia gives you certain additional
26−** rights. These rights are described in the Digia Qt LGPL Exception
27−** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28−**
29−** GNU General Public License Usage
30−** Alternatively, this file may be used under the terms of the GNU
31−** General Public License version 3.0 as published by the Free Software
32−** Foundation and appearing in the file LICENSE.GPL included in the
33−** packaging of this file. Please review the following information to
34−** ensure the GNU General Public License version 3.0 requirements will be
35−** met: http://www.gnu.org/copyleft/gpl.html.
36−**
37−**
38−** $QT_END_LICENSE$
39−**
40−****************************************************************************/
41
16
42#include "qinputdeviceinfo_mock_p.h"
17#include "qinputdeviceinfo_mock_p.h"
43
18
@@ -62,7 +37,7 @@ QInputDevice *QInputDeviceManagerPrivate::addMockDevice(const QString &devicePat
62 inputDevice->setName("Mock Device " + devicePath);
37 inputDevice->setName("Mock Device " + devicePath);
63 inputDevice->setType(type);
38 inputDevice->setType(type);
64 deviceMap.insert(devicePath, inputDevice);
39 deviceMap.insert(devicePath, inputDevice);
65− qDebug() << "Adding mock input device" << devicePath << type;
40+ qDebug() << "Adding mock input device" << devicePath << type << inputDevice << this;
66 Q_EMIT deviceAdded(devicePath);
41 Q_EMIT deviceAdded(devicePath);
67 return inputDevice;
42 return inputDevice;
68}
43}
tests/mocks/Unity/InputInfo/qinputdeviceinfo_mock_p.h+15−40
@@ -1,43 +1,18 @@
1−/****************************************************************************
1+/*
2−**
3−** Copyright (C) 2014 Canonical, Ltd. and/or its subsidiary(-ies).
2+ * Copyright 2015 Canonical Ltd.
4−** Contact: http://www.qt-project.org/legal
3+ *
5−**
4+ * This program is free software; you can redistribute it and/or modify
6−** This file is part of the QtSystems module of the Qt Toolkit.
5+ * it under the terms of the GNU Lesser General Public License as published by
7−**
6+ * the Free Software Foundation; version 3.
8−** $QT_BEGIN_LICENSE:LGPL$
7+ *
9−** Commercial License Usage
8+ * This program is distributed in the hope that it will be useful,
10−** Licensees holding valid commercial Qt licenses may use this file in
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11−** accordance with the commercial license agreement provided with the
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12−** Software or, alternatively, in accordance with the terms contained in
11+ * GNU Lesser General Public License for more details.
13−** a written agreement between you and Digia. For licensing terms and
12+ *
14−** conditions see http://qt.digia.com/licensing. For further information
13+ * You should have received a copy of the GNU Lesser General Public License
15−** use the contact form at http://qt.digia.com/contact-us.
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
16−**
15+ */
17−** GNU Lesser General Public License Usage
18−** Alternatively, this file may be used under the terms of the GNU Lesser
19−** General Public License version 2.1 as published by the Free Software
20−** Foundation and appearing in the file LICENSE.LGPL included in the
21−** packaging of this file. Please review the following information to
22−** ensure the GNU Lesser General Public License version 2.1 requirements
23−** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24−**
25−** In addition, as a special exception, Digia gives you certain additional
26−** rights. These rights are described in the Digia Qt LGPL Exception
27−** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28−**
29−** GNU General Public License Usage
30−** Alternatively, this file may be used under the terms of the GNU
31−** General Public License version 3.0 as published by the Free Software
32−** Foundation and appearing in the file LICENSE.GPL included in the
33−** packaging of this file. Please review the following information to
34−** ensure the GNU General Public License version 3.0 requirements will be
35−** met: http://www.gnu.org/copyleft/gpl.html.
36−**
37−**
38−** $QT_END_LICENSE$
39−**
40−****************************************************************************/
41
16
42#ifndef QINPUTDEVICEINFO_MOCK_H
17#ifndef QINPUTDEVICEINFO_MOCK_H
43#define QINPUTDEVICEINFO_MOCK_H
18#define QINPUTDEVICEINFO_MOCK_H