1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Copyright 2019-2020 Kai Pastor
*
* This file is part of OpenOrienteering.
*
* OpenOrienteering is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenOrienteering is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtTest>
#include <QDir> // IWYU pragma: keep
#include <QFileInfo> // IWYU pragma: keep
#include <QObject>
#include <QSignalSpy> // IWYU pragma: keep
#include <QStandardPaths> // IWYU pragma: keep
#include <QStaticPlugin> // IWYU pragma: keep
#include <QTemporaryFile> // IWYU pragma: keep
#include "test_config.h" // IWYU pragma: keep
#include "util/backports.h" // IWYU pragma: keep
namespace OpenOrienteering {}
using namespace OpenOrienteering;
#ifdef QT_POSITIONING_LIB
#include <QGeoPositionInfoSource>
#endif
#ifdef MAPPER_USE_FAKE_POSITION_PLUGIN
#include <QGeoCoordinate> // IWYU pragma: keep
#include <QNmeaPositionInfoSource> // IWYU pragma: keep
#include "sensors/fake_position_plugin.h"
#include "sensors/fake_position_source.h"
Q_IMPORT_PLUGIN(FakePositionPlugin)
#endif
#ifdef MAPPER_USE_NMEA_POSITION_PLUGIN
#include <QNmeaPositionInfoSource> // IWYU pragma: keep
#include "sensors/nmea_position_plugin.h"
Q_IMPORT_PLUGIN(NmeaPositionPlugin)
#endif
#ifdef MAPPER_USE_POWERSHELL_POSITION_PLUGIN
#include "sensors/powershell_position_source.h"
Q_IMPORT_PLUGIN(PowershellPositionPlugin)
#endif
class SensorsTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
QDir::addSearchPath(QStringLiteral("testdata"), QDir(QString::fromUtf8(MAPPER_TEST_SOURCE_DIR)).absoluteFilePath(QStringLiteral("data")));
}
#if defined(MAPPER_USE_FAKE_POSITION_PLUGIN)
void fakePositionSourcePluginTest()
{
auto sources = QGeoPositionInfoSource::availableSources();
QVERIFY(sources.contains(QStringLiteral("Fake position")));
}
void fakePositionSourceSimulatedTest()
{
auto const reference = QGeoCoordinate{10, 20, 100};
FakePositionSource::setReferencePoint(reference);
auto* source = QGeoPositionInfoSource::createSource(QStringLiteral("Fake position"), this);
QVERIFY(source);
QCOMPARE(int(source->error()), int(QGeoPositionInfoSource::NoError));
QSignalSpy source_spy(source, &QGeoPositionInfoSource::positionUpdated);
QVERIFY(source_spy.isValid());
source->startUpdates();
QCOMPARE(int(source->error()), int(QGeoPositionInfoSource::NoError));
QVERIFY(source_spy.wait());
auto last = source->lastKnownPosition(true);
QVERIFY(last.isValid());
QVERIFY(qAbs(last.coordinate().latitude() - reference.latitude()) < 0.002);
QVERIFY(qAbs(last.coordinate().longitude() - reference.longitude()) < 0.002);
QVERIFY(qAbs(last.coordinate().altitude() - reference.altitude()) < 20);
source->stopUpdates();
delete source;
}
#endif // MAPPER_USE_FAKE_POSITION_PLUGIN
#if defined(MAPPER_USE_NMEA_POSITION_PLUGIN)
void nmeaPositionSourcePluginTest()
{
auto nmea_position_source = QStringLiteral("NMEA (OpenOrienteering)");
auto sources = QGeoPositionInfoSource::availableSources();
QVERIFY(sources.contains(nmea_position_source));
}
void nmeaPositionSourceSimulatedTest()
{
auto nmea_position_source = QStringLiteral("NMEA (OpenOrienteering)");
auto* source = QGeoPositionInfoSource::createSource(nmea_position_source, this);
#if !defined(Q_OS_LINUX) && !defined(Q_OS_MACOS)
QVERIFY(!source || source->error() != QGeoPositionInfoSource::NoError);
QSKIP("\"unsupported platform");
#endif
QVERIFY(source);
QVERIFY(qobject_cast<QNmeaPositionInfoSource*>(source));
QCOMPARE(int(source->error()), int(QGeoPositionInfoSource::NoError));
QSignalSpy source_spy(source, &QGeoPositionInfoSource::positionUpdated);
QVERIFY(source_spy.isValid());
auto test_file = QFileInfo(QStringLiteral("testdata:sensors/nmea.txt"));
QVERIFY(test_file.exists());
qputenv("QT_NMEA_SERIAL_PORT", test_file.absoluteFilePath().toUtf8());
source->startUpdates();
QCOMPARE(int(source->error()), int(QGeoPositionInfoSource::NoError));
QVERIFY(source_spy.wait());
auto last = source->lastKnownPosition(true);
QVERIFY(last.isValid());
QCOMPARE(int(last.coordinate().latitude()), -30);
QCOMPARE(int(last.coordinate().longitude()), 139);
source->stopUpdates();
QTemporaryFile unreadable_file;
QVERIFY(unreadable_file.open());
unreadable_file.close();
unreadable_file.setPermissions({});
qputenv("QT_NMEA_SERIAL_PORT", unreadable_file.fileName().toUtf8());
source->startUpdates();
QCOMPARE(int(source->error()), int(QGeoPositionInfoSource::AccessError));
delete source;
}
#endif // MAPPER_USE_NMEA_POSITION_PLUGIN
#if defined(MAPPER_USE_POWERSHELL_POSITION_PLUGIN)
void powershellPositionSourcePluginTest()
{
auto powershell_position_source = QStringLiteral("Windows");
auto sources = QGeoPositionInfoSource::availableSources();
QVERIFY(sources.contains(powershell_position_source)); // JSON file properties
}
void powershellPositionSourceLiveTest()
{
auto powershell_path = QStandardPaths::findExecutable(QStringLiteral("powershell.exe"));
#if !defined(Q_OS_WIN)
if (powershell_path.isEmpty())
QSKIP("Powershell not available");
#endif
QVERIFY(!powershell_path.isEmpty());
auto powershell_position_source = QStringLiteral("Windows");
auto* source = QGeoPositionInfoSource::createSource(powershell_position_source, this);
QCOMPARE(source->error(), QGeoPositionInfoSource::NoError);
QSignalSpy source_spy(source, &QGeoPositionInfoSource::positionUpdated);
source->startUpdates();
if (source_spy.wait(2000))
return;
switch(source->error())
{
case QGeoPositionInfoSource::NoError:
QWARN("startUpdates(): QGeoPositionInfoSource::NoError"); // and no position yet!
break;
case QGeoPositionInfoSource::AccessError:
QWARN("startUpdates(): QGeoPositionInfoSource::AccessError");
break;
case QGeoPositionInfoSource::ClosedError:
QFAIL("startUpdates(): QGeoPositionInfoSource::ClosedError");
break;
default:
QFAIL("startUpdates(): unknown error");
break;
}
}
void powershellPositionSourceSimulatedTest()
{
auto setup_function = [](QProcess& process, QByteArray&, QByteArray&) -> QGeoPositionInfoSource::Error {
auto test_file = QFileInfo(QStringLiteral("testdata:sensors/powershell_position.txt"));
if (!test_file.exists())
return QGeoPositionInfoSource::UnknownSourceError;
#if defined(Q_OS_WIN)
process.setProgram(QStandardPaths::findExecutable(QStringLiteral("cmd")));
process.setArguments({QStringLiteral("/C"), QStringLiteral("type"), QDir::toNativeSeparators(test_file.absoluteFilePath())});
#else
process.setProgram(QStringLiteral("/bin/cat"));
process.setArguments({test_file.absoluteFilePath()});
#endif
return QGeoPositionInfoSource::NoError;
};
PowershellPositionSource source(*setup_function);
QCOMPARE(int(source.error()), int(QGeoPositionInfoSource::NoError));
QSignalSpy source_spy(&source, &QGeoPositionInfoSource::positionUpdated);
QVERIFY(source_spy.isValid());
source.startUpdates();
QCOMPARE(int(source.error()), int(QGeoPositionInfoSource::NoError));
QVERIFY(source_spy.wait());
auto last = source.lastKnownPosition(true);
QVERIFY(last.isValid());
QCOMPARE(int(last.coordinate().latitude()), 50);
QCOMPARE(int(last.coordinate().longitude()), 8);
source.stopUpdates();
}
#endif // MAPPER_USE_POWERSHELL_POSITION_PLUGIN
};
/*
* We don't need a real GUI window.
*
* But we discovered QTBUG-58768 macOS: Crash when using QPrinter
* while running with "minimal" platform plugin.
*/
#ifndef Q_OS_MACOS
namespace {
auto Q_DECL_UNUSED qpa_selected = qputenv("QT_QPA_PLATFORM", "minimal"); // clazy:exclude=non-pod-global-static
}
#endif
QTEST_MAIN(SensorsTest)
#include "sensors_t.moc" // IWYU pragma: keep
|