From 3f9da0137a022f3e1c6e3282387a75194f701c78 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 2 Nov 2011 18:42:18 +1000 Subject: Add unit tests for QNmeaPositionInfoSource. Change-Id: I8fa6cc3c6f0f8f2f5d3de6772a07d8d78bb77f3d Reviewed-by: Alex --- .../dummynmeapositioninfosource.pro | 15 + .../tst_dummynmeapositioninfosource.cpp | 163 +++++++ .../qnmeapositioninfosource.pro | 8 + .../qnmeapositioninfosource_realtime.pro | 17 + .../tst_qnmeapositioninfosource_realtime.cpp | 57 +++ .../qnmeapositioninfosource_realtime_generic.pro | 17 + ...st_qnmeapositioninfosource_realtime_generic.cpp | 77 +++ .../qnmeapositioninfosource_simulation.pro | 17 + .../tst_qnmeapositioninfosource_simulation.cpp | 56 +++ .../qnmeapositioninfosource_simulation_generic.pro | 17 + ..._qnmeapositioninfosource_simulation_generic.cpp | 61 +++ .../qnmeapositioninfosourceproxyfactory.cpp | 101 ++++ .../qnmeapositioninfosourceproxyfactory.h | 88 ++++ .../tst_qnmeapositioninfosource.cpp | 529 +++++++++++++++++++++ .../tst_qnmeapositioninfosource.h | 190 ++++++++ 15 files changed, 1413 insertions(+) create mode 100644 tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/dummynmeapositioninfosource.pro create mode 100644 tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/tst_dummynmeapositioninfosource.cpp create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource.pro create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime/qnmeapositioninfosource_realtime.pro create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime/tst_qnmeapositioninfosource_realtime.cpp create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime_generic/qnmeapositioninfosource_realtime_generic.pro create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime_generic/tst_qnmeapositioninfosource_realtime_generic.cpp create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation/qnmeapositioninfosource_simulation.pro create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation/tst_qnmeapositioninfosource_simulation.cpp create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation_generic/qnmeapositioninfosource_simulation_generic.pro create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation_generic/tst_qnmeapositioninfosource_simulation_generic.cpp create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosourceproxyfactory.cpp create mode 100644 tests/auto/qnmeapositioninfosource/qnmeapositioninfosourceproxyfactory.h create mode 100644 tests/auto/qnmeapositioninfosource/tst_qnmeapositioninfosource.cpp create mode 100644 tests/auto/qnmeapositioninfosource/tst_qnmeapositioninfosource.h (limited to 'tests/auto/qnmeapositioninfosource') diff --git a/tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/dummynmeapositioninfosource.pro b/tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/dummynmeapositioninfosource.pro new file mode 100644 index 00000000..ff57235d --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/dummynmeapositioninfosource.pro @@ -0,0 +1,15 @@ +TEMPLATE = app +CONFIG+=testcase +QT += network location testlib +TARGET = tst_dummynmeapositioninfosource + +INCLUDEPATH += .. + +HEADERS += ../../qgeocoordinate/qlocationtestutils_p.h \ + ../../qgeopositioninfosource/testqgeopositioninfosource_p.h \ + ../qnmeapositioninfosourceproxyfactory.h + +SOURCES += ../../qgeocoordinate/qlocationtestutils.cpp \ + ../../qgeopositioninfosource/testqgeopositioninfosource.cpp \ + ../qnmeapositioninfosourceproxyfactory.cpp \ + tst_dummynmeapositioninfosource.cpp diff --git a/tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/tst_dummynmeapositioninfosource.cpp b/tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/tst_dummynmeapositioninfosource.cpp new file mode 100644 index 00000000..8bcd9eb8 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/dummynmeapositioninfosource/tst_dummynmeapositioninfosource.cpp @@ -0,0 +1,163 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//TESTED_COMPONENT=src/location + +#include "qnmeapositioninfosourceproxyfactory.h" +#include "../qgeopositioninfosource/testqgeopositioninfosource_p.h" +#include "../qgeocoordinate/qlocationtestutils_p.h" + +#include +#include +#include + +QT_USE_NAMESPACE +Q_DECLARE_METATYPE(QNmeaPositionInfoSource::UpdateMode) +Q_DECLARE_METATYPE(QGeoPositionInfo) + +class DummyNmeaPositionInfoSource : public QNmeaPositionInfoSource +{ + Q_OBJECT + +public: + DummyNmeaPositionInfoSource(QNmeaPositionInfoSource::UpdateMode mode, QObject *parent = 0); + +protected: + virtual bool parsePosInfoFromNmeaData(const char *data, + int size, + QGeoPositionInfo *posInfo, + bool *hasFix); + +private: + int callCount; +}; + +DummyNmeaPositionInfoSource::DummyNmeaPositionInfoSource(QNmeaPositionInfoSource::UpdateMode mode, QObject *parent) : + QNmeaPositionInfoSource(mode, parent), + callCount(0) +{ +} + +bool DummyNmeaPositionInfoSource::parsePosInfoFromNmeaData(const char* data, + int size, + QGeoPositionInfo *posInfo, + bool *hasFix) +{ + Q_UNUSED(data); + Q_UNUSED(size); + + posInfo->setCoordinate(QGeoCoordinate(callCount * 1.0, callCount * 1.0, callCount * 1.0)); + posInfo->setTimestamp(QDateTime::currentDateTime().toUTC()); + *hasFix = true; + ++callCount; + + return true; +} + +class tst_DummyNmeaPositionInfoSource : public QObject +{ + Q_OBJECT + +public: + tst_DummyNmeaPositionInfoSource(); + +private slots: + void initTestCase(); + void testOverloadedParseFunction(); +}; + + +tst_DummyNmeaPositionInfoSource::tst_DummyNmeaPositionInfoSource() {} + +void tst_DummyNmeaPositionInfoSource::initTestCase() +{ + qRegisterMetaType(); +} + +void tst_DummyNmeaPositionInfoSource::testOverloadedParseFunction() +{ + DummyNmeaPositionInfoSource source(QNmeaPositionInfoSource::RealTimeMode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spy(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + + QGeoPositionInfo pos; + + proxy->source()->startUpdates(); + + proxy->feedBytes(QString("The parser converts\n").toAscii()); + + QTRY_VERIFY_WITH_TIMEOUT((spy.count() == 1), 10000); + pos = spy.at(0).at(0).value(); + + QVERIFY((pos.coordinate().latitude() == 0.0) + && (pos.coordinate().longitude() == 0.0) + && (pos.coordinate().altitude() == 0.0)); + + spy.clear(); + + proxy->feedBytes(QString("any data it receives\n").toAscii()); + + QTRY_VERIFY_WITH_TIMEOUT((spy.count() == 1), 10000); + pos = spy.at(0).at(0).value(); + + QVERIFY((pos.coordinate().latitude() == 1.0) + && (pos.coordinate().longitude() == 1.0) + && (pos.coordinate().altitude() == 1.0)); + + spy.clear(); + + proxy->feedBytes(QString("into positions\n").toAscii()); + + QTRY_VERIFY_WITH_TIMEOUT((spy.count() == 1), 10000); + pos = spy.at(0).at(0).value(); + + QVERIFY((pos.coordinate().latitude() == 2.0) + && (pos.coordinate().longitude() == 2.0) + && (pos.coordinate().altitude() == 2.0)); + + spy.clear(); +} + +#include "tst_dummynmeapositioninfosource.moc" + +QTEST_GUILESS_MAIN(tst_DummyNmeaPositionInfoSource); diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource.pro b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource.pro new file mode 100644 index 00000000..8c168d52 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource.pro @@ -0,0 +1,8 @@ +TEMPLATE = subdirs +SUBDIRS += \ + dummynmeapositioninfosource \ + qnmeapositioninfosource_realtime \ + qnmeapositioninfosource_simulation \ + qnmeapositioninfosource_realtime_generic \ + qnmeapositioninfosource_simulation_generic + diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime/qnmeapositioninfosource_realtime.pro b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime/qnmeapositioninfosource_realtime.pro new file mode 100644 index 00000000..5df263b3 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime/qnmeapositioninfosource_realtime.pro @@ -0,0 +1,17 @@ +TEMPLATE = app +CONFIG+=testcase +QT += network location testlib +TARGET = tst_qnmeapositioninfosource_realtime + +INCLUDEPATH += .. + +HEADERS += ../../qgeocoordinate/qlocationtestutils_p.h \ + ../../qgeopositioninfosource/testqgeopositioninfosource_p.h \ + ../qnmeapositioninfosourceproxyfactory.h \ + ../tst_qnmeapositioninfosource.h + +SOURCES += ../../qgeocoordinate/qlocationtestutils.cpp \ + ../../qgeopositioninfosource/testqgeopositioninfosource.cpp \ + ../qnmeapositioninfosourceproxyfactory.cpp \ + ../tst_qnmeapositioninfosource.cpp \ + tst_qnmeapositioninfosource_realtime.cpp diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime/tst_qnmeapositioninfosource_realtime.cpp b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime/tst_qnmeapositioninfosource_realtime.cpp new file mode 100644 index 00000000..090b2680 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime/tst_qnmeapositioninfosource_realtime.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//TESTED_COMPONENT=src/location + +#include "tst_qnmeapositioninfosource.h" + +class tst_QNmeaPositionInfoSource_RealTime : public tst_QNmeaPositionInfoSource +{ + Q_OBJECT + +public: + tst_QNmeaPositionInfoSource_RealTime() + : tst_QNmeaPositionInfoSource(QNmeaPositionInfoSource::RealTimeMode) {} +}; + +#include "tst_qnmeapositioninfosource_realtime.moc" + +QTEST_GUILESS_MAIN(tst_QNmeaPositionInfoSource_RealTime); diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime_generic/qnmeapositioninfosource_realtime_generic.pro b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime_generic/qnmeapositioninfosource_realtime_generic.pro new file mode 100644 index 00000000..1a5e95a1 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime_generic/qnmeapositioninfosource_realtime_generic.pro @@ -0,0 +1,17 @@ +TEMPLATE = app +CONFIG+=testcase +QT += network location testlib +TARGET = tst_qnmeapositioninfosource_realtime_generic + +INCLUDEPATH += .. + +HEADERS += ../../qgeocoordinate/qlocationtestutils_p.h \ + ../../qgeopositioninfosource/testqgeopositioninfosource_p.h \ + ../qnmeapositioninfosourceproxyfactory.h \ + ../tst_qnmeapositioninfosource.h + +SOURCES += ../../qgeocoordinate/qlocationtestutils.cpp \ + ../../qgeopositioninfosource/testqgeopositioninfosource.cpp \ + ../qnmeapositioninfosourceproxyfactory.cpp \ + ../tst_qnmeapositioninfosource.cpp \ + tst_qnmeapositioninfosource_realtime_generic.cpp diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime_generic/tst_qnmeapositioninfosource_realtime_generic.cpp b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime_generic/tst_qnmeapositioninfosource_realtime_generic.cpp new file mode 100644 index 00000000..d2d7aa7d --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_realtime_generic/tst_qnmeapositioninfosource_realtime_generic.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//TESTED_COMPONENT=src/location + +#include "tst_qnmeapositioninfosource.h" + +class tst_QNmeaPositionInfoSource_RealTime_Generic : public TestQGeoPositionInfoSource +{ + Q_OBJECT + +public: + tst_QNmeaPositionInfoSource_RealTime_Generic() + { + m_factory = new QNmeaPositionInfoSourceProxyFactory; + } + + ~tst_QNmeaPositionInfoSource_RealTime_Generic() + { + delete m_factory; + } + +protected: + QGeoPositionInfoSource *createTestSource() + { + QNmeaPositionInfoSource *source = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::RealTimeMode); + QNmeaPositionInfoSourceProxy *proxy = static_cast(m_factory->createProxy(source)); + Feeder *feeder = new Feeder(source); + feeder->start(proxy); + return source; + } + +private: + QNmeaPositionInfoSourceProxyFactory *m_factory; +}; + +#include "tst_qnmeapositioninfosource_realtime_generic.moc" + +QTEST_GUILESS_MAIN(tst_QNmeaPositionInfoSource_RealTime_Generic); diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation/qnmeapositioninfosource_simulation.pro b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation/qnmeapositioninfosource_simulation.pro new file mode 100644 index 00000000..78cfd812 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation/qnmeapositioninfosource_simulation.pro @@ -0,0 +1,17 @@ +TEMPLATE = app +CONFIG+=testcase +QT += network location testlib +TARGET = tst_qnmeapositioninfosource_simulation + +INCLUDEPATH += .. + +HEADERS += ../../qgeocoordinate/qlocationtestutils_p.h \ + ../../qgeopositioninfosource/testqgeopositioninfosource_p.h \ + ../qnmeapositioninfosourceproxyfactory.h \ + ../tst_qnmeapositioninfosource.h + +SOURCES += ../../qgeocoordinate/qlocationtestutils.cpp \ + ../../qgeopositioninfosource/testqgeopositioninfosource.cpp \ + ../qnmeapositioninfosourceproxyfactory.cpp \ + ../tst_qnmeapositioninfosource.cpp \ + tst_qnmeapositioninfosource_simulation.cpp diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation/tst_qnmeapositioninfosource_simulation.cpp b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation/tst_qnmeapositioninfosource_simulation.cpp new file mode 100644 index 00000000..f86f7fa0 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation/tst_qnmeapositioninfosource_simulation.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//TESTED_COMPONENT=src/location + +#include "tst_qnmeapositioninfosource.h" + +class tst_QNmeaPositionInfoSource_Simulation : public tst_QNmeaPositionInfoSource +{ + Q_OBJECT +public: + tst_QNmeaPositionInfoSource_Simulation() + : tst_QNmeaPositionInfoSource(QNmeaPositionInfoSource::SimulationMode) {} +}; + +#include "tst_qnmeapositioninfosource_simulation.moc" + +QTEST_GUILESS_MAIN(tst_QNmeaPositionInfoSource_Simulation); diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation_generic/qnmeapositioninfosource_simulation_generic.pro b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation_generic/qnmeapositioninfosource_simulation_generic.pro new file mode 100644 index 00000000..99c0d6d3 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation_generic/qnmeapositioninfosource_simulation_generic.pro @@ -0,0 +1,17 @@ +TEMPLATE = app +CONFIG+=testcase +QT += network location testlib +TARGET = tst_qnmeapositioninfosource_simulation_generic + +INCLUDEPATH += .. + +HEADERS += ../../qgeocoordinate/qlocationtestutils_p.h \ + ../../qgeopositioninfosource/testqgeopositioninfosource_p.h \ + ../qnmeapositioninfosourceproxyfactory.h \ + ../tst_qnmeapositioninfosource.h + +SOURCES += ../../qgeocoordinate/qlocationtestutils.cpp \ + ../../qgeopositioninfosource/testqgeopositioninfosource.cpp \ + ../qnmeapositioninfosourceproxyfactory.cpp \ + ../tst_qnmeapositioninfosource.cpp \ + tst_qnmeapositioninfosource_simulation_generic.cpp diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation_generic/tst_qnmeapositioninfosource_simulation_generic.cpp b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation_generic/tst_qnmeapositioninfosource_simulation_generic.cpp new file mode 100644 index 00000000..caccd3d6 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosource_simulation_generic/tst_qnmeapositioninfosource_simulation_generic.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//TESTED_COMPONENT=src/location + +#include "tst_qnmeapositioninfosource.h" + +class tst_QNmeaPositionInfoSource_Simulation_Generic : public TestQGeoPositionInfoSource +{ + Q_OBJECT + +protected: + QGeoPositionInfoSource *createTestSource() + { + QNmeaPositionInfoSource *source = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::SimulationMode); + source->setDevice(new UnlimitedNmeaStream(source)); + return source; + } +}; + +#include "tst_qnmeapositioninfosource_simulation_generic.moc" + +QTEST_GUILESS_MAIN(tst_QNmeaPositionInfoSource_Simulation_Generic); diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosourceproxyfactory.cpp b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosourceproxyfactory.cpp new file mode 100644 index 00000000..c7dfffd9 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosourceproxyfactory.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnmeapositioninfosourceproxyfactory.h" +#include "../qgeocoordinate/qlocationtestutils_p.h" + +#include +#include +#include + + +QNmeaPositionInfoSourceProxy::QNmeaPositionInfoSourceProxy(QNmeaPositionInfoSource *source, QIODevice *outDevice) + : m_source(source), + m_outDevice(outDevice) +{ +} + +QNmeaPositionInfoSourceProxy::~QNmeaPositionInfoSourceProxy() +{ + m_outDevice->close(); + delete m_outDevice; +} + +QGeoPositionInfoSource *QNmeaPositionInfoSourceProxy::source() const +{ + return m_source; +} + +void QNmeaPositionInfoSourceProxy::feedUpdate(const QDateTime &dt) +{ + m_outDevice->write(QLocationTestUtils::createRmcSentence(dt).toAscii()); +} + +void QNmeaPositionInfoSourceProxy::feedBytes(const QByteArray &bytes) +{ + m_outDevice->write(bytes); +} + + +QNmeaPositionInfoSourceProxyFactory::QNmeaPositionInfoSourceProxyFactory() + : m_server(new QTcpServer(this)) +{ + bool b = m_server->listen(QHostAddress::LocalHost); + Q_ASSERT(b); +} + +QNmeaPositionInfoSourceProxy *QNmeaPositionInfoSourceProxyFactory::createProxy(QNmeaPositionInfoSource *source) +{ + QTcpSocket *client = new QTcpSocket; + client->connectToHost(m_server->serverAddress(), m_server->serverPort()); + //qDebug() << "listening on" << m_server->serverAddress() << m_server->serverPort(); + bool b = m_server->waitForNewConnection(5000); + Q_ASSERT(b); + b = client->waitForConnected(); + Q_ASSERT(b); + + //QNmeaPositionInfoSource *source = new QNmeaPositionInfoSource(m_mode); + source->setDevice(m_server->nextPendingConnection()); + Q_ASSERT(source->device() != 0); + QNmeaPositionInfoSourceProxy *proxy = new QNmeaPositionInfoSourceProxy(source, client); + proxy->setParent(source); + return proxy; +} diff --git a/tests/auto/qnmeapositioninfosource/qnmeapositioninfosourceproxyfactory.h b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosourceproxyfactory.h new file mode 100644 index 00000000..2105f35c --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/qnmeapositioninfosourceproxyfactory.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNMEAPOSITIONINFOSOURCEPROXYFACTORY_H +#define QNMEAPOSITIONINFOSOURCEPROXYFACTORY_H + +#include + +#include + +QT_USE_NAMESPACE + +class QTcpServer; +class QIODevice; +class QNmeaPositionInfoSource; + +class QNmeaPositionInfoSourceProxy : public QObject +{ + Q_OBJECT +public: + QNmeaPositionInfoSourceProxy(QNmeaPositionInfoSource *source, QIODevice *outDevice); + ~QNmeaPositionInfoSourceProxy(); + + QGeoPositionInfoSource *source() const; + + void feedUpdate(const QDateTime &dt); + + void feedBytes(const QByteArray &bytes); + + int updateIntervalErrorMargin() const { return 50; } + +private: + QNmeaPositionInfoSource *m_source; + QIODevice *m_outDevice; +}; + +class QNmeaPositionInfoSourceProxyFactory : public QObject +{ + Q_OBJECT +public: + QNmeaPositionInfoSourceProxyFactory(); + + // proxy is created as child of source + QNmeaPositionInfoSourceProxy *createProxy(QNmeaPositionInfoSource *source); + +private: + QTcpServer *m_server; +}; + +#endif diff --git a/tests/auto/qnmeapositioninfosource/tst_qnmeapositioninfosource.cpp b/tests/auto/qnmeapositioninfosource/tst_qnmeapositioninfosource.cpp new file mode 100644 index 00000000..0a371008 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/tst_qnmeapositioninfosource.cpp @@ -0,0 +1,529 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//TESTED_COMPONENT=src/location + +#include "tst_qnmeapositioninfosource.h" + +tst_QNmeaPositionInfoSource::tst_QNmeaPositionInfoSource(QNmeaPositionInfoSource::UpdateMode mode, QObject *parent) + : QObject(parent), + m_mode(mode) +{ +} + +void tst_QNmeaPositionInfoSource::initTestCase() +{ + qRegisterMetaType(); + qRegisterMetaType(); +} + +void tst_QNmeaPositionInfoSource::constructor() +{ + QObject o; + QNmeaPositionInfoSource source(m_mode, &o); + QCOMPARE(source.updateMode(), m_mode); + QCOMPARE(source.parent(), &o); +} + +void tst_QNmeaPositionInfoSource::supportedPositioningMethods() +{ + QNmeaPositionInfoSource source(m_mode); + QCOMPARE(source.supportedPositioningMethods(), QNmeaPositionInfoSource::SatellitePositioningMethods); +} + +void tst_QNmeaPositionInfoSource::minimumUpdateInterval() +{ + QNmeaPositionInfoSource source(m_mode); + QCOMPARE(source.minimumUpdateInterval(), 100); +} + +void tst_QNmeaPositionInfoSource::setUpdateInterval_delayedUpdate() +{ + // If an update interval is set, and an update is not available at a + // particular interval, the source should emit the next update as soon + // as it becomes available + + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spyUpdate(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + proxy->source()->setUpdateInterval(500); + proxy->source()->startUpdates(); + + QTest::qWait(600); + QDateTime now = QDateTime::currentDateTime(); + proxy->feedUpdate(now); + QTRY_COMPARE(spyUpdate.count(), 1); + + // should have gotten the update immediately, and not have needed to + // wait until the next interval + QVERIFY(now.time().msecsTo(QDateTime::currentDateTime().time()) < 200); +} + +void tst_QNmeaPositionInfoSource::lastKnownPosition() +{ + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QCOMPARE(proxy->source()->lastKnownPosition(), QGeoPositionInfo()); + + // source may need requestUpdate() or startUpdates() to be called to + // trigger reading of data channel + QSignalSpy spyTimeout(proxy->source(), SIGNAL(updateTimeout())); + proxy->source()->requestUpdate(proxy->source()->minimumUpdateInterval()); + QTRY_COMPARE(spyTimeout.count(), 1); + + // If an update is received and startUpdates() or requestUpdate() hasn't + // been called, it should still be available through lastKnownPosition() + QDateTime dt = QDateTime::currentDateTime().toUTC(); + proxy->feedUpdate(dt); + QTRY_COMPARE(proxy->source()->lastKnownPosition().timestamp(), dt); + + QList dateTimes = createDateTimes(5); + for (int i=0; isource()->requestUpdate(); + proxy->feedUpdate(dateTimes[i]); + QTRY_COMPARE(proxy->source()->lastKnownPosition().timestamp(), dateTimes[i]); + } + + proxy->source()->startUpdates(); + dateTimes = createDateTimes(5); + for (int i=0; ifeedUpdate(dateTimes[i]); + QTRY_COMPARE(proxy->source()->lastKnownPosition().timestamp(), dateTimes[i]); + } +} + +void tst_QNmeaPositionInfoSource::beginWithBufferedData() +{ + // In SimulationMode, data stored in the QIODevice is read when + // startUpdates() or requestUpdate() is called. + // In RealTimeMode, all existing data in the QIODevice is ignored - + // only new data will be read. + + QFETCH(QList, dateTimes); + QFETCH(UpdateTriggerMethod, trigger); + + QByteArray bytes; + for (int i=0; i().timestamp(), dateTimes[i]); + } else if (trigger == RequestUpdatesMethod) { + QTRY_COMPARE(spy.count(), 1); + QCOMPARE(spy.at(0).at(0).value().timestamp(), dateTimes.first()); + } + } +} + +void tst_QNmeaPositionInfoSource::beginWithBufferedData_data() +{ + QTest::addColumn >("dateTimes"); + QTest::addColumn("trigger"); + + QList dateTimes; + dateTimes << QDateTime::currentDateTime().toUTC(); + + QTest::newRow("startUpdates(), 1 update in buffer") << dateTimes << StartUpdatesMethod; + QTest::newRow("requestUpdate(), 1 update in buffer") << dateTimes << RequestUpdatesMethod; + + for (int i=1; i<3; i++) + dateTimes << dateTimes[0].addDays(i); + QTest::newRow("startUpdates(), multiple updates in buffer") << dateTimes << StartUpdatesMethod; + QTest::newRow("requestUpdate(), multiple updates in buffer") << dateTimes << RequestUpdatesMethod; +} + +void tst_QNmeaPositionInfoSource::startUpdates() +{ + QFETCH(QList, dateTimes); + + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spyUpdate(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + proxy->source()->startUpdates(); + + for (int i=0; ifeedUpdate(dateTimes[i]); + QTRY_COMPARE(spyUpdate.count(), dateTimes.count()); +} + +void tst_QNmeaPositionInfoSource::startUpdates_data() +{ + QTest::addColumn >("dateTimes"); + + QTest::newRow("1 update") << createDateTimes(1); + QTest::newRow("2 updates") << createDateTimes(2); + QTest::newRow("10 updates") << createDateTimes(10); +} + +void tst_QNmeaPositionInfoSource::startUpdates_withTimeout() +{ + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spyUpdate(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + QSignalSpy spyTimeout(proxy->source(), SIGNAL(updateTimeout())); + + proxy->source()->setUpdateInterval(1000); + proxy->source()->startUpdates(); + + QDateTime dt = QDateTime::currentDateTime().toUTC(); + + if (m_mode == QNmeaPositionInfoSource::SimulationMode) { + // the first sentence primes the simulation + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt).toLatin1()); + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt.addMSecs(50)).toLatin1()); + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt.addSecs(1)).toLatin1()); + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt.addSecs(2)).toLatin1()); + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt.addSecs(9)).toLatin1()); + + int i = 0; + + for (int j = 0; j < 3; ++j) { + i = 0; + for (; i < 12; ++i) { + QTest::qWait(100); + if ((spyUpdate.count() == 1) && (spyTimeout.count() == 0)) + break; + } + QVERIFY((spyUpdate.count() == 1) && (spyTimeout.count() == 0)); + spyUpdate.clear(); + for (; i < 10; ++i) { + QTest::qWait(100); + } + } + + i = 0; + for (; i < 72; ++i) { + QTest::qWait(100); + if ((spyUpdate.count() == 0) && (spyTimeout.count() == 1)) + break; + } + QVERIFY((spyUpdate.count() == 0) && (spyTimeout.count() == 1)); + spyTimeout.clear(); + + for (; i < 72; ++i) { + QTest::qWait(100); + if ((spyUpdate.count() == 1) && (spyTimeout.count() == 0)) + break; + } + QVERIFY((spyUpdate.count() == 1) && (spyTimeout.count() == 0)); + + } else { + QTest::qWait(900); + // dt + 900 + QVERIFY(spyUpdate.count() == 0 && spyTimeout.count() == 0); + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt.addSecs(1)).toLatin1()); + QTest::qWait(200); + // dt + 1100 + QVERIFY(spyUpdate.count() == 1 && spyTimeout.count() == 0); + spyUpdate.clear(); + + QTest::qWait(800); + // dt + 1900 + QVERIFY(spyUpdate.count() == 0 && spyTimeout.count() == 0); + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt.addSecs(2)).toLatin1()); + QTest::qWait(200); + // dt + 2100 + QVERIFY(spyUpdate.count() == 1 && spyTimeout.count() == 0); + spyUpdate.clear(); + + QTest::qWait(800); + // dt + 2900 + QVERIFY(spyUpdate.count() == 0 && spyTimeout.count() == 0); + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt.addSecs(3)).toLatin1()); + QTest::qWait(200); + // dt + 3100 + QVERIFY(spyUpdate.count() == 1 && spyTimeout.count() == 0); + spyUpdate.clear(); + + QTest::qWait(3800); + // dt + 6900 + QVERIFY(spyUpdate.count() == 0 && spyTimeout.count() == 1); + spyTimeout.clear(); + + proxy->feedBytes(QLocationTestUtils::createRmcSentence(dt.addSecs(7)).toLatin1()); + QTest::qWait(200); + // dt + 7100 + QVERIFY(spyUpdate.count() == 1 && spyTimeout.count() == 0); + spyUpdate.clear(); + } +} + +void tst_QNmeaPositionInfoSource::startUpdates_expectLatestUpdateOnly() +{ + // If startUpdates() is called and an interval has been set, if multiple' + // updates are in the buffer, only the latest update should be emitted + + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spyUpdate(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + proxy->source()->setUpdateInterval(500); + proxy->source()->startUpdates(); + + QList dateTimes = createDateTimes(3); + for (int i=0; ifeedUpdate(dateTimes[i]); + + QTRY_COMPARE(spyUpdate.count(), 1); + QCOMPARE(spyUpdate[0][0].value().timestamp(), dateTimes.last()); +} + +void tst_QNmeaPositionInfoSource::startUpdates_waitForValidDateTime() +{ + // Tests that the class does not emit an update until it receives a + // sentences with a valid date *and* time. All sentences before this + // should be ignored, and any sentences received after this that do + // not have a date should use the known date. + + QFETCH(QByteArray, bytes); + QFETCH(QList, dateTimes); + + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spy(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + proxy->source()->startUpdates(); + + proxy->feedBytes(bytes); + QTRY_COMPARE(spy.count(), dateTimes.count()); + + for (int i=0; i().timestamp(), dateTimes[i]); +} + +void tst_QNmeaPositionInfoSource::startUpdates_waitForValidDateTime_data() +{ + QTest::addColumn("bytes"); + QTest::addColumn >("dateTimes"); + + QDateTime dt = QDateTime::currentDateTime().toUTC(); + QByteArray bytes; + + // should only receive RMC sentence and the GGA sentence *after* it + bytes += QLocationTestUtils::createGgaSentence(dt.addSecs(1).time()).toLatin1(); + bytes += QLocationTestUtils::createRmcSentence(dt.addSecs(2)).toLatin1(); + bytes += QLocationTestUtils::createGgaSentence(dt.addSecs(3).time()).toLatin1(); + QTest::newRow("Feed GGA,RMC,GGA; expect RMC, second GGA only") + << bytes << (QList() << dt.addSecs(2) << dt.addSecs(3)); + + // should not receive ZDA (has no coordinates) but should get the GGA + // sentence after it since it got the date/time from ZDA + bytes.clear(); + bytes += QLocationTestUtils::createGgaSentence(dt.addSecs(1).time()).toLatin1(); + bytes += QLocationTestUtils::createZdaSentence(dt.addSecs(2)).toLatin1(); + bytes += QLocationTestUtils::createGgaSentence(dt.addSecs(3).time()).toLatin1(); + QTest::newRow("Feed GGA,ZDA,GGA; expect second GGA only") + << bytes << (QList() << dt.addSecs(3)); + + if (m_mode == QNmeaPositionInfoSource::SimulationMode) { + // In sim m_mode, should ignore sentence with a date/time before the known date/time + // (in real time m_mode, everything is passed on regardless) + bytes.clear(); + bytes += QLocationTestUtils::createRmcSentence(dt.addSecs(1)).toLatin1(); + bytes += QLocationTestUtils::createRmcSentence(dt.addSecs(-2)).toLatin1(); + bytes += QLocationTestUtils::createRmcSentence(dt.addSecs(2)).toLatin1(); + QTest::newRow("Feed good RMC, RMC with bad date/time, good RMC; expect first and third RMC only") + << bytes << (QList() << dt.addSecs(1) << dt.addSecs(2)); + } +} + +void tst_QNmeaPositionInfoSource::requestUpdate_waitForValidDateTime() +{ + QFETCH(QByteArray, bytes); + QFETCH(QList, dateTimes); + + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spy(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + proxy->source()->requestUpdate(); + + proxy->feedBytes(bytes); + QTRY_COMPARE(spy.count(), 1); + QCOMPARE(spy[0][0].value().timestamp(), dateTimes[0]); +} + +void tst_QNmeaPositionInfoSource::requestUpdate_waitForValidDateTime_data() +{ + startUpdates_waitForValidDateTime_data(); +} + +void tst_QNmeaPositionInfoSource::requestUpdate() +{ + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spyUpdate(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + QSignalSpy spyTimeout(proxy->source(), SIGNAL(updateTimeout())); + QDateTime dt; + + proxy->source()->requestUpdate(100); + QTRY_COMPARE(spyTimeout.count(), 1); + spyTimeout.clear(); + + dt = QDateTime::currentDateTime().toUTC(); + proxy->feedUpdate(dt); + proxy->source()->requestUpdate(); + QTRY_COMPARE(spyUpdate.count(), 1); + QCOMPARE(spyUpdate[0][0].value().timestamp(), dt); + QCOMPARE(spyTimeout.count(), 0); + spyUpdate.clear(); + + // delay the update and expect it to be emitted after 300ms + dt = QDateTime::currentDateTime().toUTC(); + proxy->source()->requestUpdate(1000); + QTest::qWait(300); + proxy->feedUpdate(dt); + QTRY_COMPARE(spyUpdate.count(), 1); + QCOMPARE(spyUpdate[0][0].value().timestamp(), dt); + QCOMPARE(spyTimeout.count(), 0); + spyUpdate.clear(); + + // delay the update and expect updateTimeout() to be emitted + dt = QDateTime::currentDateTime().toUTC(); + proxy->source()->requestUpdate(500); + QTest::qWait(1000); + proxy->feedUpdate(dt); + QCOMPARE(spyTimeout.count(), 1); + QCOMPARE(spyUpdate.count(), 0); + spyUpdate.clear(); +} + +void tst_QNmeaPositionInfoSource::requestUpdate_after_start() +{ + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spyUpdate(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + QSignalSpy spyTimeout(proxy->source(), SIGNAL(updateTimeout())); + + // Start updates with 500ms interval and requestUpdate() with 100ms + // timeout. Feed an update, and it should be emitted immediately due to + // the requestUpdate(). The update should not be emitted again after that + // (i.e. the startUpdates() interval should not cause it to be re-emitted). + + QDateTime dt = QDateTime::currentDateTime().toUTC(); + proxy->source()->setUpdateInterval(500); + proxy->source()->startUpdates(); + proxy->source()->requestUpdate(100); + proxy->feedUpdate(dt); + QTRY_COMPARE(spyUpdate.count(), 1); + QCOMPARE(spyUpdate[0][0].value().timestamp(), dt); + QCOMPARE(spyTimeout.count(), 0); + spyUpdate.clear(); + + // Update has been emitted for requestUpdate(), shouldn't be emitted for startUpdates() + QTest::qWait(1000); + QCOMPARE(spyUpdate.count(), 0); +} + +void tst_QNmeaPositionInfoSource::testWithBadNmea() +{ + QFETCH(QByteArray, bytes); + QFETCH(QList, dateTimes); + QFETCH(UpdateTriggerMethod, trigger); + + QNmeaPositionInfoSource source(m_mode); + QNmeaPositionInfoSourceProxyFactory factory; + QNmeaPositionInfoSourceProxy *proxy = static_cast(factory.createProxy(&source)); + + QSignalSpy spy(proxy->source(), SIGNAL(positionUpdated(QGeoPositionInfo))); + if (trigger == StartUpdatesMethod) + proxy->source()->startUpdates(); + else + proxy->source()->requestUpdate(); + + proxy->feedBytes(bytes); + QTRY_COMPARE(spy.count(), dateTimes.count()); + for (int i=0; i().timestamp(), dateTimes[i]); +} + +void tst_QNmeaPositionInfoSource::testWithBadNmea_data() +{ + QTest::addColumn("bytes"); + QTest::addColumn >("dateTimes"); + QTest::addColumn("trigger"); + + QDateTime firstDateTime = QDateTime::currentDateTime().toUTC(); + QByteArray bad = QLocationTestUtils::createRmcSentence(firstDateTime.addSecs(1)).toLatin1(); + bad = bad.mid(bad.length()/2); + QDateTime lastDateTime = firstDateTime.addSecs(2); + + QByteArray bytes; + bytes += QLocationTestUtils::createRmcSentence(firstDateTime).toLatin1(); + bytes += bad; + bytes += QLocationTestUtils::createRmcSentence(lastDateTime).toLatin1(); + QTest::newRow("requestUpdate(), bad second sentence") << bytes + << (QList() << firstDateTime) << RequestUpdatesMethod; + QTest::newRow("startUpdates(), bad second sentence") << bytes + << (QList() << firstDateTime << lastDateTime) << StartUpdatesMethod; +} diff --git a/tests/auto/qnmeapositioninfosource/tst_qnmeapositioninfosource.h b/tests/auto/qnmeapositioninfosource/tst_qnmeapositioninfosource.h new file mode 100644 index 00000000..a4a30a63 --- /dev/null +++ b/tests/auto/qnmeapositioninfosource/tst_qnmeapositioninfosource.h @@ -0,0 +1,190 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnmeapositioninfosourceproxyfactory.h" +#include "../qgeopositioninfosource/testqgeopositioninfosource_p.h" +#include "../qgeocoordinate/qlocationtestutils_p.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +QT_USE_NAMESPACE +Q_DECLARE_METATYPE(QNmeaPositionInfoSource::UpdateMode) +Q_DECLARE_METATYPE(QGeoPositionInfo) +Q_DECLARE_METATYPE(QList) + +class tst_QNmeaPositionInfoSource : public QObject +{ + Q_OBJECT + +public: + enum UpdateTriggerMethod + { + StartUpdatesMethod, + RequestUpdatesMethod + }; + + tst_QNmeaPositionInfoSource(QNmeaPositionInfoSource::UpdateMode mode, QObject *parent = 0); + +private: + QList createDateTimes(int count) const + { + QList dateTimes; + QDateTime dt = QDateTime::currentDateTime().toUTC(); + int interval = 100; + for (int i=0; isetInterval(proxy->source()->minimumUpdateInterval()*2); + timer->start(); + } + +public slots: + void timeout() + { + m_proxy->feedBytes(QLocationTestUtils::createRmcSentence(QDateTime::currentDateTime()).toAscii()); + } + +private: + QNmeaPositionInfoSourceProxy *m_proxy; +}; + +//--------------------------------------------------- + + +class UnlimitedNmeaStream : public QIODevice +{ + Q_OBJECT + +public: + UnlimitedNmeaStream(QObject *parent) : QIODevice(parent) {} + +protected: + qint64 readData(char *data, qint64 maxSize) + { + QByteArray bytes = QLocationTestUtils::createRmcSentence(QDateTime::currentDateTime()).toLatin1(); + qint64 sz = qMin(qint64(bytes.size()), maxSize); + memcpy(data, bytes.constData(), sz); + return sz; + } + + qint64 writeData(const char *, qint64) + { + return -1; + } + + qint64 bytesAvailable() const + { + return 1024 + QIODevice::bytesAvailable(); + } +}; -- cgit v1.2.1