From 17e3f08377c34d301401fbd3c40b525790fcd9a5 Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Fri, 26 Apr 2019 21:46:45 +0200 Subject: Add tests for PluginParameters in PositionSource Testing parameters in QGeoPositionInfoSourceFactory becomes implicit with this test. Task-number: QTBUG-66304 Change-Id: I3e9fbf99762e9a03e4c8cae3ff317ea36313e687 Reviewed-by: Alex Blasche --- tests/auto/auto.pro | 1 + tests/auto/declarative_core/tst_positionsource.qml | 131 +++++++++---- tests/auto/positionplugin/plugin.cpp | 47 ++++- tests/auto/positionplugin/plugin.json | 2 +- tests/auto/positionplugin/positionplugin.pro | 2 +- tests/auto/positionpluginV1/plugin.cpp | 212 +++++++++++++++++++++ tests/auto/positionpluginV1/plugin.json | 9 + tests/auto/positionpluginV1/positionpluginV1.pro | 12 ++ 8 files changed, 369 insertions(+), 47 deletions(-) create mode 100644 tests/auto/positionpluginV1/plugin.cpp create mode 100644 tests/auto/positionpluginV1/plugin.json create mode 100644 tests/auto/positionpluginV1/positionpluginV1.pro (limited to 'tests') diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 745e264b..05559d12 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -86,6 +86,7 @@ SUBDIRS += \ !android: SUBDIRS += \ positionplugin \ + positionpluginV1 \ positionplugintest \ qgeoareamonitor \ qgeopositioninfosource \ diff --git a/tests/auto/declarative_core/tst_positionsource.qml b/tests/auto/declarative_core/tst_positionsource.qml index a663f3ab..9b78936b 100644 --- a/tests/auto/declarative_core/tst_positionsource.qml +++ b/tests/auto/declarative_core/tst_positionsource.qml @@ -28,7 +28,7 @@ import QtQuick 2.0 import QtTest 1.0 -import QtPositioning 5.2 +import QtPositioning 5.14 TestCase { id: testCase @@ -106,6 +106,26 @@ TestCase { SignalSpy { id: directionValidSpy; target: testingSource.position; signalName: "directionValidChanged" } SignalSpy { id: directionSpy; target: testingSource.position; signalName: "directionChanged" } + PositionSource { + id: testingSourceWParams + name: "test.source" + updateInterval: 1000 + PluginParameter { + id: altitudeParameter + name: "test.source.altitude" + value: 42.42 + } + } + + SignalSpy { id: updateSpyWParams; target: testingSourceWParams; signalName: "positionChanged" } + SignalSpy { id: directionValidSpyWParams; target: testingSourceWParams.position; signalName: "directionValidChanged" } + SignalSpy { id: directionSpyWParams; target: testingSourceWParams.position; signalName: "directionChanged" } + + PositionSource { id: testingSourceV1; name: "test.source.v1"; updateInterval: 1000 } + SignalSpy { id: updateSpyV1; target: testingSourceV1; signalName: "positionChanged" } + SignalSpy { id: directionValidSpyV1; target: testingSourceV1.position; signalName: "directionValidChanged" } + SignalSpy { id: directionSpyV1; target: testingSourceV1.position; signalName: "directionChanged" } + function test_updateInterval() { testingSource.updateInterval = 1000; compare(testingSource.updateInterval, 1000); @@ -125,40 +145,81 @@ TestCase { } function test_updates() { - updateSpy.clear(); - - compare(directionValidSpy.count, 0) - compare(directionSpy.count, 0) - - testingSource.active = true; - - tryCompare(updateSpy, "count", 1, 1500); - compare(testingSource.position.coordinate.longitude, 0.1); - compare(testingSource.position.coordinate.latitude, 0.1); - compare(directionValidSpy.count, 1) - compare(directionSpy.count, 1) - fuzzyCompare(testingSource.position.direction, 45, 0.1) - verify(!testingSource.position.speedValid) - verify(isNaN(testingSource.position.speed)) - - tryCompare(updateSpy, "count", 2, 1500); - compare(testingSource.position.coordinate.longitude, 0.2); - compare(testingSource.position.coordinate.latitude, 0.2); - compare(directionValidSpy.count, 1) - compare(directionSpy.count, 2) - fuzzyCompare(testingSource.position.direction, 45, 0.1) - verify(testingSource.position.speedValid) - verify(testingSource.position.speed > 10000) - - testingSource.active = false; + updateSpyV1.clear(); + + compare(directionValidSpyV1.count, 0) + compare(directionSpyV1.count, 0) + + testingSourceV1.active = true; + + tryCompare(updateSpyV1, "count", 1, 1500); + compare(testingSourceV1.position.coordinate.longitude, 0.1); + compare(testingSourceV1.position.coordinate.latitude, 0.1); + compare(directionValidSpyV1.count, 1) + compare(directionSpyV1.count, 1) + fuzzyCompare(testingSourceV1.position.direction, 45, 0.1) + verify(!testingSourceV1.position.speedValid) + verify(isNaN(testingSourceV1.position.speed)) + + tryCompare(updateSpyV1, "count", 2, 1500); + compare(testingSourceV1.position.coordinate.longitude, 0.2); + compare(testingSourceV1.position.coordinate.latitude, 0.2); + compare(directionValidSpyV1.count, 1) + compare(directionSpyV1.count, 2) + fuzzyCompare(testingSourceV1.position.direction, 45, 0.1) + verify(testingSourceV1.position.speedValid) + verify(testingSourceV1.position.speed > 10000) + + testingSourceV1.active = false; + wait(2500); + compare(updateSpyV1.count, 2); + compare(testingSourceV1.position.coordinate.longitude, 0.2); + compare(testingSourceV1.position.coordinate.latitude, 0.2); + compare(directionValidSpyV1.count, 1) + compare(directionSpyV1.count, 2) + fuzzyCompare(testingSourceV1.position.direction, 45, 0.1) + verify(testingSourceV1.position.speedValid) + verify(testingSourceV1.position.speed > 10000) + } + + function test_updates_w_params() { + updateSpyWParams.clear(); + + compare(directionValidSpyWParams.count, 0) + compare(directionSpyWParams.count, 0) + + testingSourceWParams.active = true; + + tryCompare(updateSpyWParams, "count", 1, 1500); + compare(testingSourceWParams.position.coordinate.longitude, 0.1); + compare(testingSourceWParams.position.coordinate.latitude, 0.1); + compare(testingSourceWParams.position.coordinate.altitude, altitudeParameter.value); + compare(directionValidSpyWParams.count, 1) + compare(directionSpyWParams.count, 1) + fuzzyCompare(testingSourceWParams.position.direction, 45, 0.1) + verify(!testingSourceWParams.position.speedValid) + verify(isNaN(testingSourceWParams.position.speed)) + + tryCompare(updateSpyWParams, "count", 2, 1500); + compare(testingSourceWParams.position.coordinate.longitude, 0.2); + compare(testingSourceWParams.position.coordinate.latitude, 0.2); + compare(testingSourceWParams.position.coordinate.altitude, altitudeParameter.value); + compare(directionValidSpyWParams.count, 1) + compare(directionSpyWParams.count, 2) + fuzzyCompare(testingSourceWParams.position.direction, 45, 0.1) + verify(testingSourceWParams.position.speedValid) + verify(testingSourceWParams.position.speed > 10000) + + testingSourceWParams.active = false; wait(2500); - compare(updateSpy.count, 2); - compare(testingSource.position.coordinate.longitude, 0.2); - compare(testingSource.position.coordinate.latitude, 0.2); - compare(directionValidSpy.count, 1) - compare(directionSpy.count, 2) - fuzzyCompare(testingSource.position.direction, 45, 0.1) - verify(testingSource.position.speedValid) - verify(testingSource.position.speed > 10000) + compare(updateSpyWParams.count, 2); + compare(testingSourceWParams.position.coordinate.longitude, 0.2); + compare(testingSourceWParams.position.coordinate.latitude, 0.2); + compare(testingSourceWParams.position.coordinate.altitude, altitudeParameter.value); + compare(directionValidSpyWParams.count, 1) + compare(directionSpyWParams.count, 2) + fuzzyCompare(testingSourceWParams.position.direction, 45, 0.1) + verify(testingSourceWParams.position.speedValid) + verify(testingSourceWParams.position.speed > 10000) } } diff --git a/tests/auto/positionplugin/plugin.cpp b/tests/auto/positionplugin/plugin.cpp index a15a89a5..b6016b95 100644 --- a/tests/auto/positionplugin/plugin.cpp +++ b/tests/auto/positionplugin/plugin.cpp @@ -39,7 +39,7 @@ class DummySource : public QGeoPositionInfoSource Q_OBJECT public: - DummySource(QObject *parent=0); + DummySource(const QVariantMap ¶meters, QObject *parent=0); ~DummySource(); void startUpdates(); @@ -65,13 +65,19 @@ private slots: void doTimeout(); }; -DummySource::DummySource(QObject *parent) : +DummySource::DummySource(const QVariantMap ¶meters, QObject *parent) : QGeoPositionInfoSource(parent), timer(new QTimer(this)), timeoutTimer(new QTimer(this)), singleTimer(new QTimer(this)), lastPosition(QGeoCoordinate(0,0), QDateTime::currentDateTime()) { + if (parameters.contains(QStringLiteral("test.source.altitude"))) { + const qreal alti = parameters.value(QStringLiteral("test.source.altitude")).toReal(); + QGeoCoordinate crd = lastPosition.coordinate(); + crd.setAltitude(alti); + lastPosition.setCoordinate(crd); + } timer->setInterval(1000); connect(timer, SIGNAL(timeout()), this, SLOT(updatePosition())); @@ -157,7 +163,8 @@ void DummySource::updatePosition() const QDateTime now = QDateTime::currentDateTime(); QGeoCoordinate coord(lastPosition.coordinate().latitude() + 0.1, - lastPosition.coordinate().longitude() + 0.1); + lastPosition.coordinate().longitude() + 0.1, + lastPosition.coordinate().altitude()); QGeoPositionInfo info(coord, now); info.setAttribute(QGeoPositionInfo::Direction, lastPosition.coordinate().azimuthTo(coord)); @@ -179,35 +186,55 @@ void DummySource::doTimeout() } -class QGeoPositionInfoSourceFactoryTest : public QObject, public QGeoPositionInfoSourceFactory +class QGeoPositionInfoSourceFactoryTest : public QObject, public QGeoPositionInfoSourceFactoryV2 { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.qt.position.sourcefactory/5.0" FILE "plugin.json") - Q_INTERFACES(QGeoPositionInfoSourceFactory) + Q_INTERFACES(QGeoPositionInfoSourceFactoryV2) public: QGeoPositionInfoSource *positionInfoSource(QObject *parent); QGeoSatelliteInfoSource *satelliteInfoSource(QObject *parent); QGeoAreaMonitorSource *areaMonitor(QObject *parent); + + QGeoPositionInfoSource *positionInfoSourceWithParameters(QObject *parent, const QVariantMap ¶meters); + QGeoSatelliteInfoSource *satelliteInfoSourceWithParameters(QObject *parent, const QVariantMap ¶meters); + QGeoAreaMonitorSource *areaMonitorWithParameters(QObject *parent, const QVariantMap ¶meters); }; QGeoPositionInfoSource *QGeoPositionInfoSourceFactoryTest::positionInfoSource(QObject *parent) { - return new DummySource(parent); + return new DummySource(QVariantMap(), parent); } QGeoSatelliteInfoSource *QGeoPositionInfoSourceFactoryTest::satelliteInfoSource(QObject *parent) { - Q_UNUSED(parent); - // not implemented - return 0; + return satelliteInfoSourceWithParameters(parent, QVariantMap()); } QGeoAreaMonitorSource *QGeoPositionInfoSourceFactoryTest::areaMonitor(QObject* parent) +{ + return areaMonitorWithParameters(parent, QVariantMap()); +} + +QGeoPositionInfoSource *QGeoPositionInfoSourceFactoryTest::positionInfoSourceWithParameters(QObject *parent, const QVariantMap ¶meters) +{ + return new DummySource(parameters, parent); +} + +QGeoSatelliteInfoSource *QGeoPositionInfoSourceFactoryTest::satelliteInfoSourceWithParameters(QObject *parent, const QVariantMap ¶meters) +{ + Q_UNUSED(parent); + Q_UNUSED(parameters) + return nullptr; +} + +QGeoAreaMonitorSource *QGeoPositionInfoSourceFactoryTest::areaMonitorWithParameters(QObject *parent, const QVariantMap ¶meters) { Q_UNUSED(parent); - return 0; + Q_UNUSED(parameters) + return nullptr; } #include "plugin.moc" diff --git a/tests/auto/positionplugin/plugin.json b/tests/auto/positionplugin/plugin.json index 68acaded..a38d2a5a 100644 --- a/tests/auto/positionplugin/plugin.json +++ b/tests/auto/positionplugin/plugin.json @@ -4,6 +4,6 @@ "Position": true, "Satellite": false, "Monitor": false, - "Priority": 0, + "Priority": 1, "Testable": true } diff --git a/tests/auto/positionplugin/positionplugin.pro b/tests/auto/positionplugin/positionplugin.pro index dd04e7fb..9ccd030f 100644 --- a/tests/auto/positionplugin/positionplugin.pro +++ b/tests/auto/positionplugin/positionplugin.pro @@ -2,7 +2,7 @@ TARGET = qtposition_testplugin QT += positioning PLUGIN_TYPE = position -PLUGIN_CLASS_NAME = TestPositionPlugin +PLUGIN_CLASS_NAME = QGeoPositionInfoSourceFactoryTest PLUGIN_EXTENDS = - load(qt_plugin) diff --git a/tests/auto/positionpluginV1/plugin.cpp b/tests/auto/positionpluginV1/plugin.cpp new file mode 100644 index 00000000..bf8b8234 --- /dev/null +++ b/tests/auto/positionpluginV1/plugin.cpp @@ -0,0 +1,212 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include + +QT_USE_NAMESPACE + +class DummySource : public QGeoPositionInfoSource +{ + Q_OBJECT + +public: + DummySource(QObject *parent = nullptr); + ~DummySource(); + + void startUpdates(); + void stopUpdates(); + void requestUpdate(int timeout=5000); + + QGeoPositionInfo lastKnownPosition(bool fromSatellitePositioningMethodsOnly) const; + PositioningMethods supportedPositioningMethods() const; + + void setUpdateInterval(int msec); + int minimumUpdateInterval() const; + Error error() const; + +private: + QTimer *timer; + QTimer *timeoutTimer; + QTimer *singleTimer; + QGeoPositionInfo lastPosition; + QDateTime lastUpdateTime; + +private slots: + void updatePosition(); + void doTimeout(); +}; + +DummySource::DummySource(QObject *parent) : + QGeoPositionInfoSource(parent), + timer(new QTimer(this)), + timeoutTimer(new QTimer(this)), + singleTimer(new QTimer(this)), + lastPosition(QGeoCoordinate(0,0), QDateTime::currentDateTime()) +{ + timer->setInterval(1000); + connect(timer, SIGNAL(timeout()), + this, SLOT(updatePosition())); + connect(singleTimer, SIGNAL(timeout()), + this, SLOT(updatePosition())); + connect(timeoutTimer, SIGNAL(timeout()), + this, SLOT(doTimeout())); +} + +QGeoPositionInfoSource::Error DummySource::error() const +{ + return QGeoPositionInfoSource::NoError; +} + + +void DummySource::setUpdateInterval(int msec) +{ + if (msec == 0) { + timer->setInterval(1000); + } else if (msec < 1000) { + msec = 1000; + timer->setInterval(msec); + } else { + timer->setInterval(msec); + } + + QGeoPositionInfoSource::setUpdateInterval(msec); +} + +int DummySource::minimumUpdateInterval() const +{ + return 1000; +} + +QGeoPositionInfo DummySource::lastKnownPosition(bool fromSatellitePositioningMethodsOnly) const +{ + Q_UNUSED(fromSatellitePositioningMethodsOnly); + return lastPosition; +} + +QGeoPositionInfoSource::PositioningMethods DummySource::supportedPositioningMethods() const +{ + return QGeoPositionInfoSource::AllPositioningMethods; +} + +void DummySource::startUpdates() +{ + timer->start(); +} + +void DummySource::stopUpdates() +{ + timer->stop(); +} + +void DummySource::requestUpdate(int timeout) +{ + if (timeout == 0) + timeout = 5000; + if (timeout < 0) + timeout = 0; + + timeoutTimer->setInterval(timeout); + timeoutTimer->start(); + + if (timer->isActive()) { + timer->stop(); + timer->start(); + } + + singleTimer->setInterval(1000); + singleTimer->start(); +} + +DummySource::~DummySource() +{} + +void DummySource::updatePosition() +{ + timeoutTimer->stop(); + singleTimer->stop(); + + const QDateTime now = QDateTime::currentDateTime(); + + QGeoCoordinate coord(lastPosition.coordinate().latitude() + 0.1, + lastPosition.coordinate().longitude() + 0.1); + + QGeoPositionInfo info(coord, now); + info.setAttribute(QGeoPositionInfo::Direction, lastPosition.coordinate().azimuthTo(coord)); + if (lastUpdateTime.isValid()) { + double speed = lastPosition.coordinate().distanceTo(coord) / lastUpdateTime.msecsTo(now); + info.setAttribute(QGeoPositionInfo::GroundSpeed, 1000 * speed); + } + + lastUpdateTime = now; + lastPosition = info; + emit positionUpdated(info); +} + +void DummySource::doTimeout() +{ + timeoutTimer->stop(); + singleTimer->stop(); + emit updateTimeout(); +} + + +class QGeoPositionInfoSourceFactoryTestV1 : public QObject, public QGeoPositionInfoSourceFactory +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.qt.position.sourcefactory/5.0" + FILE "plugin.json") + Q_INTERFACES(QGeoPositionInfoSourceFactory) + +public: + QGeoPositionInfoSource *positionInfoSource(QObject *parent); + QGeoSatelliteInfoSource *satelliteInfoSource(QObject *parent); + QGeoAreaMonitorSource *areaMonitor(QObject *parent); +}; + +QGeoPositionInfoSource *QGeoPositionInfoSourceFactoryTestV1::positionInfoSource(QObject *parent) +{ + return new DummySource(parent); +} + +QGeoSatelliteInfoSource *QGeoPositionInfoSourceFactoryTestV1::satelliteInfoSource(QObject *parent) +{ + Q_UNUSED(parent); + return nullptr; +} + +QGeoAreaMonitorSource *QGeoPositionInfoSourceFactoryTestV1::areaMonitor(QObject* parent) +{ + Q_UNUSED(parent); + return nullptr; +} + +#include "plugin.moc" diff --git a/tests/auto/positionpluginV1/plugin.json b/tests/auto/positionpluginV1/plugin.json new file mode 100644 index 00000000..9acf27e7 --- /dev/null +++ b/tests/auto/positionpluginV1/plugin.json @@ -0,0 +1,9 @@ +{ + "Keys": ["test.source.v1"], + "Provider": "test.source.v1", + "Position": true, + "Satellite": false, + "Monitor": false, + "Priority": 0, + "Testable": true +} diff --git a/tests/auto/positionpluginV1/positionpluginV1.pro b/tests/auto/positionpluginV1/positionpluginV1.pro new file mode 100644 index 00000000..925a7e29 --- /dev/null +++ b/tests/auto/positionpluginV1/positionpluginV1.pro @@ -0,0 +1,12 @@ +TARGET = qtposition_testpluginv1 +QT += positioning + +PLUGIN_TYPE = position +PLUGIN_CLASS_NAME = QGeoPositionInfoSourceFactoryTestV1 +PLUGIN_EXTENDS = - +load(qt_plugin) + +SOURCES += plugin.cpp + +OTHER_FILES += \ + plugin.json -- cgit v1.2.1