summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorLincoln Ramsay <lincoln.ramsay@nokia.com>2011-08-23 11:24:22 +1000
committerQt by Nokia <qt-info@nokia.com>2011-08-25 07:59:15 +0200
commit7cd6a4db67cccad64e6e0965654b30353ab35d3b (patch)
tree8ece9bed05eb6a2626b2ba170cc172ffc637a005 /tests/auto
parent8b618a4bcc045c9298c219cad9b88902616134a3 (diff)
Add a unit test for QtmSensors.
As parts of the QtSensors unit test are re-used, they must be updated to include the correct headers. Change-Id: I189eb74fe312f74cfbe9e1fb605ac047bc9bdfbb Reviewed-on: http://codereview.qt.nokia.com/3349 Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Zsolt Simon <zsolt.simon@nokia.com>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/auto.pro2
-rw-r--r--tests/auto/qsensor/test_backends.cpp3
-rw-r--r--tests/auto/qsensor/test_backends.h22
-rw-r--r--tests/auto/qsensor/test_sensor.h2
-rw-r--r--tests/auto/qsensor/test_sensor2.h2
-rw-r--r--tests/auto/qsensor/test_sensor2_p.h2
-rw-r--r--tests/auto/qsensor/test_sensor2impl.cpp1
-rw-r--r--tests/auto/qsensor/test_sensor2impl.h2
-rw-r--r--tests/auto/qsensor/test_sensorimpl.h2
-rw-r--r--tests/auto/qsensor/test_sensorplugin.cpp6
-rw-r--r--tests/auto/qsensor/tst_qsensor.cpp1
-rw-r--r--tests/auto/qtmsensor/main.cpp76
-rw-r--r--tests/auto/qtmsensor/qtmsensor.pro36
-rw-r--r--tests/auto/qtmsensor/test_backends_qtm.cpp104
-rw-r--r--tests/auto/qtmsensor/test_backends_qtm.h151
-rw-r--r--tests/auto/qtmsensor/test_sensor_qtm.cpp64
-rw-r--r--tests/auto/qtmsensor/test_sensor_qtm.h94
-rw-r--r--tests/auto/qtmsensor/test_sensor_qtm_p.h71
-rw-r--r--tests/auto/qtmsensor/test_sensor_wrapper.cpp84
-rw-r--r--tests/auto/qtmsensor/test_sensor_wrapper.h14
-rw-r--r--tests/auto/qtmsensor/tst_qtmsensor.cpp766
21 files changed, 1483 insertions, 22 deletions
diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro
index b303c70b..788e2753 100644
--- a/tests/auto/auto.pro
+++ b/tests/auto/auto.pro
@@ -1,4 +1,4 @@
TEMPLATE = subdirs
SUBDIRS += qsensor
-
+SUBDIRS += qtmsensor
diff --git a/tests/auto/qsensor/test_backends.cpp b/tests/auto/qsensor/test_backends.cpp
index b091d2f8..f8ac2622 100644
--- a/tests/auto/qsensor/test_backends.cpp
+++ b/tests/auto/qsensor/test_backends.cpp
@@ -41,7 +41,8 @@
#include <QList>
-#include "qsensorbackend.h"
+#include <QtSensors/qsensorbackend.h>
+#include <QtSensors/qsensormanager.h>
typedef QSensorBackend* (*CreateFunc) (QSensor *sensor);
class Record
diff --git a/tests/auto/qsensor/test_backends.h b/tests/auto/qsensor/test_backends.h
index d58793da..376d7ca5 100644
--- a/tests/auto/qsensor/test_backends.h
+++ b/tests/auto/qsensor/test_backends.h
@@ -42,21 +42,21 @@
#ifndef TEST_BACKENDS_H
#define TEST_BACKENDS_H
-#include <qsensorbackend.h>
+#include <QtSensors/qsensorbackend.h>
void register_test_backends();
void unregister_test_backends();
-#include <qaccelerometer.h>
-#include <qambientlightsensor.h>
-#include <qcompass.h>
-#include <qgyroscope.h>
-#include <qlightsensor.h>
-#include <qmagnetometer.h>
-#include <qorientationsensor.h>
-#include <qproximitysensor.h>
-#include <qrotationsensor.h>
-#include <qtapsensor.h>
+#include <QtSensors/qaccelerometer.h>
+#include <QtSensors/qambientlightsensor.h>
+#include <QtSensors/qcompass.h>
+#include <QtSensors/qgyroscope.h>
+#include <QtSensors/qlightsensor.h>
+#include <QtSensors/qmagnetometer.h>
+#include <QtSensors/qorientationsensor.h>
+#include <QtSensors/qproximitysensor.h>
+#include <QtSensors/qrotationsensor.h>
+#include <QtSensors/qtapsensor.h>
#define PREPARE_SENSORINTERFACE_DECLS(SensorClass, ReadingClass, FilterClass, readingcode)\
class SensorClass ## _impl : public QSensorBackend\
diff --git a/tests/auto/qsensor/test_sensor.h b/tests/auto/qsensor/test_sensor.h
index cc7a278a..436af251 100644
--- a/tests/auto/qsensor/test_sensor.h
+++ b/tests/auto/qsensor/test_sensor.h
@@ -42,7 +42,7 @@
#ifndef TEST_SENSOR_H
#define TEST_SENSOR_H
-#include <qsensor.h>
+#include <QtSensors/qsensor.h>
class TestSensorReadingPrivate;
diff --git a/tests/auto/qsensor/test_sensor2.h b/tests/auto/qsensor/test_sensor2.h
index 93b20687..ba632b3e 100644
--- a/tests/auto/qsensor/test_sensor2.h
+++ b/tests/auto/qsensor/test_sensor2.h
@@ -42,7 +42,7 @@
#ifndef TEST_SENSOR2_H
#define TEST_SENSOR2_H
-#include "qsensor.h"
+#include <QtSensors/qsensor.h>
#undef DECLARE_READING
#undef DECLARE_READING_D
diff --git a/tests/auto/qsensor/test_sensor2_p.h b/tests/auto/qsensor/test_sensor2_p.h
index 0e84230d..87a9e637 100644
--- a/tests/auto/qsensor/test_sensor2_p.h
+++ b/tests/auto/qsensor/test_sensor2_p.h
@@ -53,7 +53,7 @@
// We mean it.
//
-#include "private/qsensor_p.h"
+#include <5.0.0/QtSensors/private/qsensor_p.h>
class TestSensor2ReadingPrivate : public QSensorReadingPrivate
{
diff --git a/tests/auto/qsensor/test_sensor2impl.cpp b/tests/auto/qsensor/test_sensor2impl.cpp
index 04ee6fb0..fcecef29 100644
--- a/tests/auto/qsensor/test_sensor2impl.cpp
+++ b/tests/auto/qsensor/test_sensor2impl.cpp
@@ -40,7 +40,6 @@
****************************************************************************/
#include "test_sensor2impl.h"
-#include <qaccelerometer.h>
#include <QDebug>
char const * const testsensor2impl::id("test sensor 2 impl");
diff --git a/tests/auto/qsensor/test_sensor2impl.h b/tests/auto/qsensor/test_sensor2impl.h
index 7fb0b348..ebd72192 100644
--- a/tests/auto/qsensor/test_sensor2impl.h
+++ b/tests/auto/qsensor/test_sensor2impl.h
@@ -42,7 +42,7 @@
#ifndef TEST_SENSOR2IMPL_H
#define TEST_SENSOR2IMPL_H
-#include <qsensorbackend.h>
+#include <QtSensors/qsensorbackend.h>
#include "test_sensor2.h"
class testsensor2impl : public QSensorBackend
diff --git a/tests/auto/qsensor/test_sensorimpl.h b/tests/auto/qsensor/test_sensorimpl.h
index 35afb7ee..d8fff884 100644
--- a/tests/auto/qsensor/test_sensorimpl.h
+++ b/tests/auto/qsensor/test_sensorimpl.h
@@ -42,7 +42,7 @@
#ifndef TEST_SENSORIMPL_H
#define TEST_SENSORIMPL_H
-#include <qsensorbackend.h>
+#include <QtSensors/qsensorbackend.h>
#include "test_sensor.h"
class testsensorimpl : public QSensorBackend
diff --git a/tests/auto/qsensor/test_sensorplugin.cpp b/tests/auto/qsensor/test_sensorplugin.cpp
index c501377a..9f2d69c8 100644
--- a/tests/auto/qsensor/test_sensorplugin.cpp
+++ b/tests/auto/qsensor/test_sensorplugin.cpp
@@ -41,9 +41,9 @@
#include "test_sensorimpl.h"
#include "test_sensor2impl.h"
-#include <qsensorplugin.h>
-#include <qsensorbackend.h>
-#include <qsensormanager.h>
+#include <QtSensors/qsensorplugin.h>
+#include <QtSensors/qsensorbackend.h>
+#include <QtSensors/qsensormanager.h>
#include <QFile>
#include <QDebug>
#include <QTest>
diff --git a/tests/auto/qsensor/tst_qsensor.cpp b/tests/auto/qsensor/tst_qsensor.cpp
index 0765750d..7807bc87 100644
--- a/tests/auto/qsensor/tst_qsensor.cpp
+++ b/tests/auto/qsensor/tst_qsensor.cpp
@@ -49,6 +49,7 @@
#include <QSignalSpy>
#include "qsensor.h"
+#include "qsensormanager.h"
#include "test_sensor.h"
#include "test_sensor2.h"
#include "test_sensorimpl.h"
diff --git a/tests/auto/qtmsensor/main.cpp b/tests/auto/qtmsensor/main.cpp
new file mode 100644
index 00000000..92e24755
--- /dev/null
+++ b/tests/auto/qtmsensor/main.cpp
@@ -0,0 +1,76 @@
+/****************************************************************************
+**
+** 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 QtSensors module 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 "gruesensorimpl.h"
+#include <qsensorplugin.h>
+#include <qsensorbackend.h>
+#include <qsensormanager.h>
+#include <QFile>
+#include <QDebug>
+
+class GrueSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory
+{
+ Q_OBJECT
+ Q_INTERFACES(QtMobility::QSensorPluginInterface)
+public:
+ void registerSensors()
+ {
+ qDebug() << "loaded the grue plugin";
+ QSensorManager::registerBackend(GrueSensor::type, gruesensorimpl::id, this);
+ }
+
+ QSensorBackend *createBackend(QSensor *sensor)
+ {
+ if (sensor->identifier() == gruesensorimpl::id) {
+ // Can't make this unless we have an ambient light sensor
+ if (!QSensor::defaultSensorForType(QAmbientLightSensor::type).isEmpty())
+ return new gruesensorimpl(sensor);
+ qDebug() << "can't make" << sensor->identifier() << "because no" << QAmbientLightSensor::type << "sensors exist";
+ }
+
+ return 0;
+ }
+};
+
+Q_EXPORT_PLUGIN2(libsensors_grueplugin, GrueSensorPlugin);
+
+#include "main.moc"
+
diff --git a/tests/auto/qtmsensor/qtmsensor.pro b/tests/auto/qtmsensor/qtmsensor.pro
new file mode 100644
index 00000000..78e41c18
--- /dev/null
+++ b/tests/auto/qtmsensor/qtmsensor.pro
@@ -0,0 +1,36 @@
+TEMPLATE = app
+TARGET = tst_qtmsensor
+
+CONFIG += testcase
+QT = core testlib qtmsensors-private
+
+SOURCES += \
+ tst_qtmsensor.cpp
+
+HEADERS += \
+ ../qsensor/test_sensor.h\
+ ../qsensor/test_sensor_p.h\
+ ../qsensor/test_sensorimpl.h\
+ ../qsensor/test_sensor2.h\
+ ../qsensor/test_sensor2_p.h\
+ ../qsensor/test_sensor2impl.h\
+ ../qsensor/test_backends.h
+
+SOURCES += \
+ ../qsensor/test_sensor.cpp\
+ ../qsensor/test_sensorimpl.cpp\
+ ../qsensor/test_sensor2.cpp\
+ ../qsensor/test_sensor2impl.cpp\
+ ../qsensor/test_sensorplugin.cpp\
+ ../qsensor/test_backends.cpp
+
+HEADERS += \
+ test_sensor_qtm.h \
+ test_sensor_qtm_p.h \
+ test_sensor_wrapper.h \
+ test_backends_qtm.h
+
+SOURCES += \
+ test_sensor_qtm.cpp \
+ test_sensor_wrapper.cpp \
+ test_backends_qtm.cpp
diff --git a/tests/auto/qtmsensor/test_backends_qtm.cpp b/tests/auto/qtmsensor/test_backends_qtm.cpp
new file mode 100644
index 00000000..2d98baa5
--- /dev/null
+++ b/tests/auto/qtmsensor/test_backends_qtm.cpp
@@ -0,0 +1,104 @@
+/****************************************************************************
+**
+** 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 QtSensors module 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 <QList>
+
+#include <private/qsensorbackend_p.h>
+#include <private/qsensormanager_p.h>
+
+QTM_BEGIN_NAMESPACE
+
+typedef QSensorBackend* (*CreateFunc) (QSensor *sensor);
+
+class Record
+{
+public:
+ QByteArray type;
+ CreateFunc func;
+};
+static QList<Record> records;
+
+static bool registerTestBackend(const char *className, CreateFunc func)
+{
+ Record record;
+ record.type = className;
+ record.func = func;
+ records << record;
+ return true;
+}
+
+QTM_END_NAMESPACE
+
+#define REGISTER_TOO
+#include "test_backends_qtm.h"
+#include <QDebug>
+
+QTM_BEGIN_NAMESPACE
+
+class BackendFactory : public QSensorBackendFactory
+{
+ QSensorBackend *createBackend(QSensor *sensor)
+ {
+ foreach (const Record &record, records) {
+ if (sensor->identifier() == record.type) {
+ return record.func(sensor);
+ }
+ }
+ return 0;
+ };
+};
+static BackendFactory factory;
+
+void register_test_backends()
+{
+ foreach (const Record &record, records) {
+ QSensorManager::registerBackend(record.type, record.type, &factory);
+ }
+}
+
+void unregister_test_backends()
+{
+ foreach (const Record &record, records) {
+ QSensorManager::unregisterBackend(record.type, record.type);
+ }
+}
+
+QTM_END_NAMESPACE
diff --git a/tests/auto/qtmsensor/test_backends_qtm.h b/tests/auto/qtmsensor/test_backends_qtm.h
new file mode 100644
index 00000000..9c42de5b
--- /dev/null
+++ b/tests/auto/qtmsensor/test_backends_qtm.h
@@ -0,0 +1,151 @@
+/****************************************************************************
+**
+** 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 QtSensors module 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 TEST_BACKENDS_H
+#define TEST_BACKENDS_H
+
+#include <private/qsensorbackend_p.h>
+
+#include <qaccelerometer.h>
+#include <qambientlightsensor.h>
+#include <qcompass.h>
+#include <qgyroscope.h>
+#include <qlightsensor.h>
+#include <qmagnetometer.h>
+#include <qorientationsensor.h>
+#include <qproximitysensor.h>
+#include <qrotationsensor.h>
+#include <qtapsensor.h>
+
+QTM_BEGIN_NAMESPACE
+
+void register_test_backends();
+void unregister_test_backends();
+
+#define PREPARE_SENSORINTERFACE_DECLS(SensorClass, ReadingClass, FilterClass, readingcode)\
+ class SensorClass ## _impl : public QSensorBackend\
+ {\
+ public:\
+ SensorClass ## _impl(QSensor *sensor);\
+ void start();\
+ void stop();\
+ };\
+ class SensorClass ## _testfilter : public FilterClass { bool filter(ReadingClass *); };
+
+#define PREPARE_SENSORINTERFACE_IMPLS(SensorClass, ReadingClass, FilterClass, readingcode)\
+ SensorClass ## _impl::SensorClass ##_impl(QSensor *sensor) : QSensorBackend(sensor) {}\
+ void SensorClass ## _impl::start() {\
+ ReadingClass *reading = setReading<ReadingClass>(0);\
+ readingcode\
+ newReadingAvailable();\
+ }\
+ void SensorClass ##_impl::stop() {}\
+ bool SensorClass ## _testfilter::filter(ReadingClass *) { return true; }\
+ static QSensorBackend *create_ ## SensorClass ## _impl(QSensor *sensor) { return new SensorClass ## _impl(sensor); }\
+ static bool registered_ ## SensorClass = registerTestBackend(#SensorClass, create_ ## SensorClass ## _impl);
+
+#ifdef REGISTER_TOO
+#define PREPARE_SENSORINTERFACE(SensorClass, ReadingClass, FilterClass, readingcode)\
+ PREPARE_SENSORINTERFACE_DECLS(SensorClass, ReadingClass, FilterClass, readingcode)\
+ PREPARE_SENSORINTERFACE_IMPLS(SensorClass, ReadingClass, FilterClass, readingcode)
+#else
+#define PREPARE_SENSORINTERFACE(SensorClass, ReadingClass, FilterClass, readingcode)\
+ PREPARE_SENSORINTERFACE_DECLS(SensorClass, ReadingClass, FilterClass, readingcode)
+#endif
+
+PREPARE_SENSORINTERFACE(QAccelerometer, QAccelerometerReading, QAccelerometerFilter, {
+ reading->setX(1.0);
+ reading->setY(1.0);
+ reading->setZ(1.0);
+})
+PREPARE_SENSORINTERFACE(QAmbientLightSensor, QAmbientLightReading, QAmbientLightFilter, {
+ reading->setLightLevel(QAmbientLightReading::Twilight);
+})
+PREPARE_SENSORINTERFACE(QCompass, QCompassReading, QCompassFilter, {
+ reading->setAzimuth(1.0);
+ reading->setCalibrationLevel(1.0);
+})
+PREPARE_SENSORINTERFACE(QGyroscope, QGyroscopeReading, QGyroscopeFilter, {
+ reading->setX(1.0);
+ reading->setY(1.0);
+ reading->setZ(1.0);
+})
+PREPARE_SENSORINTERFACE(QLightSensor, QLightReading, QLightFilter, {
+ reading->setLux(1.0);
+})
+PREPARE_SENSORINTERFACE(QMagnetometer, QMagnetometerReading, QMagnetometerFilter, {
+ reading->setX(1.0);
+ reading->setY(1.0);
+ reading->setZ(1.0);
+ reading->setCalibrationLevel(1.0);
+})
+PREPARE_SENSORINTERFACE(QOrientationSensor, QOrientationReading, QOrientationFilter, {
+ reading->setOrientation(QOrientationReading::LeftUp);
+})
+PREPARE_SENSORINTERFACE(QProximitySensor, QProximityReading, QProximityFilter, {
+ reading->setClose(true);
+})
+PREPARE_SENSORINTERFACE(QRotationSensor, QRotationReading, QRotationFilter, {
+ reading->setX(1.0);
+ reading->setY(1.0);
+ reading->setZ(1.0);
+})
+PREPARE_SENSORINTERFACE(QTapSensor, QTapReading, QTapFilter, {
+ reading->setTapDirection(QTapReading::Z_Both);
+ reading->setDoubleTap(true);
+})
+
+#define TEST_SENSORINTERFACE(SensorClass, ReadingClass, readingcode)\
+ do {\
+ SensorClass sensor;\
+ SensorClass ## _testfilter filter;\
+ sensor.addFilter(&filter);\
+ sensor.start();\
+ QVERIFY(sensor.isConnectedToBackend());\
+ QVERIFY(sensor.isActive());\
+ ReadingClass *reading = sensor.reading();\
+ QVERIFY(reading != 0);\
+ readingcode\
+ } while (0);
+
+#endif
+
+QTM_END_NAMESPACE
diff --git a/tests/auto/qtmsensor/test_sensor_qtm.cpp b/tests/auto/qtmsensor/test_sensor_qtm.cpp
new file mode 100644
index 00000000..ee77a7ae
--- /dev/null
+++ b/tests/auto/qtmsensor/test_sensor_qtm.cpp
@@ -0,0 +1,64 @@
+/****************************************************************************
+**
+** 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 QtSensors module 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 "test_sensor_qtm.h"
+#include "test_sensor_qtm_p.h"
+
+QTM_BEGIN_NAMESPACE
+
+IMPLEMENT_READING(TestSensorReading)
+
+int TestSensorReading::test() const
+{
+ return d->test;
+}
+
+void TestSensorReading::setTest(int test)
+{
+ d->test = test;
+}
+
+// =====================================================================
+
+const char *TestSensor::type("test sensor");
+
+#include "moc_test_sensor_qtm.cpp"
+QTM_END_NAMESPACE
diff --git a/tests/auto/qtmsensor/test_sensor_qtm.h b/tests/auto/qtmsensor/test_sensor_qtm.h
new file mode 100644
index 00000000..1946d3b1
--- /dev/null
+++ b/tests/auto/qtmsensor/test_sensor_qtm.h
@@ -0,0 +1,94 @@
+/****************************************************************************
+**
+** 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 QtSensors module 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 TEST_SENSOR_QTM_H
+#define TEST_SENSOR_QTM_H
+
+#include <qsensor.h>
+
+QTM_BEGIN_NAMESPACE
+
+class TestSensorReadingPrivate;
+
+class TestSensorReading : public QSensorReading
+{
+ Q_OBJECT
+ Q_PROPERTY(int test READ test)
+ DECLARE_READING(TestSensorReading)
+public:
+ int test() const;
+ void setTest(int test);
+};
+
+class TestSensorFilter : public QSensorFilter
+{
+public:
+ virtual bool filter(TestSensorReading *reading) = 0;
+private:
+ bool filter(QSensorReading *reading) { return filter(static_cast<TestSensorReading*>(reading)); }
+};
+
+class TestSensor : public QSensor
+{
+ Q_OBJECT
+public:
+ explicit TestSensor(QObject *parent = 0)
+ : QSensor(TestSensor::type, parent)
+ , sensorsChangedEmitted(0)
+ {
+ connect(this, SIGNAL(availableSensorsChanged()), this, SLOT(s_availableSensorsChanged()));
+ }
+ virtual ~TestSensor() {}
+ TestSensorReading *reading() const { return static_cast<TestSensorReading*>(QSensor::reading()); }
+ static const char *type;
+
+ // used by the testSensorsChangedSignal test function
+ int sensorsChangedEmitted;
+private slots:
+ void s_availableSensorsChanged()
+ {
+ sensorsChangedEmitted++;
+ }
+};
+
+QTM_END_NAMESPACE
+
+#endif
diff --git a/tests/auto/qtmsensor/test_sensor_qtm_p.h b/tests/auto/qtmsensor/test_sensor_qtm_p.h
new file mode 100644
index 00000000..145b48eb
--- /dev/null
+++ b/tests/auto/qtmsensor/test_sensor_qtm_p.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** 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 QtSensors module 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 TEST_SENSOR_QTM_P_H
+#define TEST_SENSOR_QTM_P_H
+
+QTM_BEGIN_NAMESPACE
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+class TestSensorReadingPrivate
+{
+public:
+ TestSensorReadingPrivate()
+ : test(0)
+ {
+ }
+
+ int test;
+};
+
+QTM_END_NAMESPACE
+
+#endif
diff --git a/tests/auto/qtmsensor/test_sensor_wrapper.cpp b/tests/auto/qtmsensor/test_sensor_wrapper.cpp
new file mode 100644
index 00000000..d236cc94
--- /dev/null
+++ b/tests/auto/qtmsensor/test_sensor_wrapper.cpp
@@ -0,0 +1,84 @@
+/****************************************************************************
+**
+** 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 QtSensors module 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 "test_sensor_wrapper.h"
+#include <private/qsensormanager_p.h>
+#include <QDebug>
+
+QTM_BEGIN_NAMESPACE
+
+class WrapperFactory : public QSensorPluginInterface,
+ public QSensorBackendFactory
+{
+public:
+ void registerSensors()
+ {
+ QList<QByteArray> types;
+ types << TestSensor::type;
+ foreach (const QByteArray &type, types) {
+ foreach (const QByteArray &identifier, NEW_NAMESPACE(QSensor)::sensorsForType(type)) {
+ //qDebug() << "Registerbackend" << type << identifier;
+ QSensorManager::registerBackend(type, identifier, this);
+ }
+ }
+ }
+
+ QSensorBackend *createBackend(QSensor *sensor)
+ {
+ if (sensor->identifier() == "test sensor impl") {
+ return new TestSensorWrapper(sensor);
+ }
+
+ qWarning() << "Can't create backend" << sensor->identifier();
+ return 0;
+ }
+};
+
+REGISTER_STATIC_PLUGIN_V1(WrapperFactory)
+
+IMPLEMENT_WRAPPER(TestSensor, TestSensorReading, {
+ //qDebug() << "fetchData" << sensor->reading()->timestamp();
+ m_reading.setTimestamp(sensor->reading()->timestamp());
+ m_reading.setTest(sensor->reading()->test());
+ newReadingAvailable();
+ })
+
+QTM_END_NAMESPACE
diff --git a/tests/auto/qtmsensor/test_sensor_wrapper.h b/tests/auto/qtmsensor/test_sensor_wrapper.h
new file mode 100644
index 00000000..adfc6c22
--- /dev/null
+++ b/tests/auto/qtmsensor/test_sensor_wrapper.h
@@ -0,0 +1,14 @@
+#ifndef TEST_SENSOR_WRAPPER_H
+#define TEST_SENSOR_WRAPPER_H
+
+#include <private/backendwrapper_p.h>
+#include "../qsensor/test_sensor.h"
+#include "test_sensor_qtm.h"
+
+QTM_BEGIN_NAMESPACE
+
+DECLARE_WRAPPER(TestSensor, TestSensorReading)
+
+QTM_END_NAMESPACE
+
+#endif
diff --git a/tests/auto/qtmsensor/tst_qtmsensor.cpp b/tests/auto/qtmsensor/tst_qtmsensor.cpp
new file mode 100644
index 00000000..40f90670
--- /dev/null
+++ b/tests/auto/qtmsensor/tst_qtmsensor.cpp
@@ -0,0 +1,766 @@
+/****************************************************************************
+**
+** 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 QtSensors module 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/sensors
+
+#include <QObject>
+#include <QTest>
+#include <QDebug>
+#include <QSettings>
+#include <QFile>
+#include <QSignalSpy>
+
+#include <qaccelerometer.h>
+#include <qambientlightsensor.h>
+#include <qcompass.h>
+#include <qgyroscope.h>
+#include <qlightsensor.h>
+#include <qmagnetometer.h>
+#include <qorientationsensor.h>
+#include <qproximitysensor.h>
+#include <qrotationsensor.h>
+#include <qtapsensor.h>
+
+#include "test_sensor_qtm.h"
+#include "test_backends_qtm.h"
+#include <private/qsensormanager_p.h>
+
+static char const * const testsensorimpl_id("test sensor impl");
+
+Q_SENSORS_EXPORT void sensors_unit_test_hook(int);
+
+QTM_BEGIN_NAMESPACE
+QTM_SENSORS_EXPORT void qtm_sensors_unit_test_hook(int);
+QTM_END_NAMESPACE
+
+QTM_USE_NAMESPACE
+
+bool operator==(const qoutputrange &orl1, const qoutputrange &orl2)
+{
+ return (orl1.minimum == orl2.minimum &&
+ orl1.maximum == orl2.maximum &&
+ orl1.accuracy == orl2.accuracy);
+}
+
+namespace QTest {
+ template<> char *toString(const qoutputrangelist &orl)
+ {
+ QStringList list;
+ foreach (const qoutputrange &item, orl) {
+ list << QString("%1-%2%3%4").arg(item.minimum).arg(item.maximum).arg(QString::fromWCharArray(L"\u00B1")).arg(item.accuracy);
+ }
+ QString ret = QString("qoutputrangelist: (%1)").arg(list.join("), ("));
+ return qstrdup(ret.toLatin1().data());
+ }
+ template<> char *toString(const QList<QByteArray> &data)
+ {
+ QStringList list;
+ foreach (const QByteArray &str, data) {
+ list << QString::fromLatin1(str);
+ }
+ QString ret = QString("QList<QByteArray>: (%1)").arg(list.join("), ("));
+ return qstrdup(ret.toLatin1().data());
+ }
+}
+
+
+class MyFilter : public TestSensorFilter { bool filter(TestSensorReading *) { return false; } };
+
+class ModFilter : public TestSensorFilter
+{
+ bool filter(TestSensorReading *reading)
+ {
+ reading->setTest(3);
+ return true;
+ }
+};
+
+/*
+ Unit test for QtmSensor class.
+*/
+class tst_QtmSensor : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_QtmSensor()
+ {
+ sensors_unit_test_hook(0); // change some flags the library uses
+ qtm_sensors_unit_test_hook(0); // change some flags the library uses
+ }
+
+private slots:
+ void initTestCase()
+ {
+ QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
+ settings.clear();
+ }
+
+ void cleanupTestCase()
+ {
+ QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
+ settings.clear();
+
+#ifdef WAIT_AT_END
+ QFile _stdin;
+ _stdin.open(1, QIODevice::ReadOnly);
+ _stdin.readLine();
+#endif
+ }
+
+ // This test MUST be first
+ void testRecursiveLoadPlugins()
+ {
+ TestSensor sensor;
+
+ // This confirms that legacy static plugins can still be registered
+ //QTest::ignoreMessage(QtWarningMsg, "Loaded the LegacySensorPlugin ");
+
+ // The logic for the test is in test_sensorplugin.cpp (which warns and aborts if the test fails)
+ (void)QSensor::sensorTypes();
+
+ // Checking that the availableSensorsChanged() signal was not emitted too many times while loading plugins.
+ QCOMPARE(sensor.sensorsChangedEmitted, 1);
+ }
+
+ void testTypeRegistered()
+ {
+ QList<QByteArray> expected;
+ expected << TestSensor::type;
+ QList<QByteArray> actual = QSensor::sensorTypes();
+ qSort(actual); // The actual list is not in a defined order
+ QCOMPARE(actual, expected);
+ }
+
+ void testSensorRegistered()
+ {
+ QList<QByteArray> expected;
+ expected << "test sensor 2" << "test sensor 3" << testsensorimpl_id;
+ QList<QByteArray> actual = QSensor::sensorsForType(TestSensor::type);
+ qSort(actual); // The actual list is not in a defined order
+ QCOMPARE(actual, expected);
+ }
+
+ void testSensorDefault()
+ {
+ QByteArray expected = testsensorimpl_id;
+ QByteArray actual = QSensor::defaultSensorForType(TestSensor::type);
+ QCOMPARE(actual, expected);
+ }
+
+ void testBadDefaultFromConfig()
+ {
+ QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
+ settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray("bogus id"));
+ settings.sync();
+
+ QByteArray expected = testsensorimpl_id;
+ QByteArray actual = QSensor::defaultSensorForType(TestSensor::type);
+ QCOMPARE(actual, expected);
+ }
+
+ void testGoodDefaultFromConfig()
+ {
+ QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
+ settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray(testsensorimpl_id));
+ settings.sync();
+
+ QByteArray expected = testsensorimpl_id;
+ QByteArray actual = QSensor::defaultSensorForType(TestSensor::type);
+ QCOMPARE(actual, expected);
+
+ settings.clear();
+ }
+
+ void testNoSensorsForType()
+ {
+ QList<QByteArray> expected;
+ QList<QByteArray> actual = QSensor::sensorsForType("bogus type");
+ QCOMPARE(actual, expected);
+ }
+
+ void testNoDefaultForType()
+ {
+ QByteArray expected;
+ QByteArray actual = QSensor::defaultSensorForType("bogus type");
+ QCOMPARE(actual, expected);
+ }
+
+ void testCreation()
+ {
+ TestSensor sensor;
+ sensor.connectToBackend();
+ QByteArray expected = testsensorimpl_id;
+ QByteArray actual = sensor.identifier();
+ QCOMPARE(actual, expected);
+ }
+
+ void testSetIdentifierFail()
+ {
+ TestSensor sensor;
+ sensor.setIdentifier(testsensorimpl_id);
+ sensor.connectToBackend();
+ QVERIFY(sensor.isConnectedToBackend());
+ QByteArray expected = testsensorimpl_id;
+ QByteArray actual = sensor.identifier();
+ QCOMPARE(actual, expected);
+
+ QTest::ignoreMessage(QtWarningMsg, "ERROR: Cannot call QSensor::setIdentifier while connected to a backend! ");
+ sensor.setIdentifier("dummy.accelerometer");
+ expected = testsensorimpl_id;
+ actual = sensor.identifier();
+ QCOMPARE(actual, expected);
+ }
+
+ void testBadDefaultCreation()
+ {
+ QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
+ settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray("test sensor 2"));
+ settings.sync();
+
+ TestSensor sensor;
+ QTest::ignoreMessage(QtWarningMsg, "Can't create backend \"test sensor 2\" ");
+ sensor.connectToBackend();
+ QByteArray expected = testsensorimpl_id;
+ QByteArray actual = sensor.identifier();
+ QCOMPARE(actual, expected);
+
+ settings.clear();
+ }
+
+ void testBadCreation()
+ {
+ QSensor sensor("bogus type");
+ sensor.connectToBackend();
+ QByteArray expected; // should be null
+ QByteArray actual = sensor.identifier();
+ QCOMPARE(actual, expected);
+ }
+
+ void testTimestamp()
+ {
+ TestSensor sensor;
+ sensor.connectToBackend();
+ QVERIFY(sensor.reading() != 0);
+ qtimestamp timestamp = sensor.reading()->timestamp();
+ QVERIFY(timestamp == qtimestamp());
+ sensor.setProperty("doThis", "setOne");
+ sensor.start();
+ timestamp = sensor.reading()->timestamp();
+ QVERIFY(timestamp == 1);
+ }
+
+ void testStart()
+ {
+ TestSensor sensor;
+ sensor.start();
+ QVERIFY(sensor.isActive());
+ sensor.start();
+ QVERIFY(sensor.isActive());
+ }
+
+ void testBadStart()
+ {
+ QSensor sensor("bogus type");
+ sensor.start();
+ QVERIFY(!sensor.isActive());
+ }
+
+ void testStop()
+ {
+ TestSensor sensor;
+ sensor.stop();
+ QVERIFY(!sensor.isActive());
+ sensor.start();
+ QVERIFY(sensor.isActive());
+ sensor.stop();
+ QVERIFY(!sensor.isActive());
+ }
+
+ void testMetaData()
+ {
+ TestSensor sensor;
+
+ {
+ bool actual = sensor.isConnectedToBackend();
+ bool expected = false;
+ QCOMPARE(actual, expected);
+ }
+
+ sensor.connectToBackend();
+
+ {
+ bool actual = sensor.isConnectedToBackend();
+ bool expected = true;
+ QCOMPARE(actual, expected);
+ }
+
+ {
+ QString actual = sensor.description();
+ QString expected = "sensor description";
+ QCOMPARE(actual, expected);
+ }
+
+ {
+ qoutputrangelist actual = sensor.outputRanges();
+ qoutputrangelist expected;
+ qoutputrange r; r.minimum = 0; r.maximum = 1; r.accuracy = 0.5;
+ expected << r;
+ r.minimum = 0; r.maximum = 2; r.accuracy = 1;
+ expected << r;
+ QCOMPARE(actual, expected);
+ }
+
+ {
+ int actual = sensor.outputRange();
+ int expected = -1;
+ QCOMPARE(actual, expected);
+
+ sensor.setOutputRange(0);
+
+ actual = sensor.outputRange();
+ expected = 0;
+ QCOMPARE(actual, expected);
+ }
+
+ {
+ qrangelist actual = sensor.availableDataRates();
+ qrangelist expected = qrangelist() << qrange(100,100);
+ QCOMPARE(actual, expected);
+ }
+
+ {
+ TestSensor sensor;
+ sensor.setProperty("doThis", "rates");
+ sensor.connectToBackend();
+ qrangelist actual = sensor.availableDataRates();
+ qrangelist expected = qrangelist() << qrange(100,100);
+ QCOMPARE(actual, expected);
+ }
+
+ {
+ TestSensor sensor;
+ sensor.setProperty("doThis", "rates(0)");
+ QTest::ignoreMessage(QtWarningMsg, "ERROR: Cannot call QSensorBackend::setDataRates with 0 ");
+ sensor.connectToBackend();
+ }
+
+ {
+ TestSensor sensor;
+ sensor.setProperty("doThis", "rates(nodef)");
+ QTest::ignoreMessage(QtWarningMsg, "ERROR: Cannot call QSensorBackend::setDataRates with an invalid sensor ");
+ sensor.connectToBackend();
+ }
+
+ {
+ int actual = sensor.dataRate();
+ int expected = 0;
+ QCOMPARE(actual, expected);
+
+ sensor.setDataRate(100);
+
+ actual = sensor.dataRate();
+ expected = 100;
+ QCOMPARE(actual, expected);
+ }
+
+ // Test the generic accessor functions
+ TestSensorReading *reading = sensor.reading();
+ QCOMPARE(reading->valueCount(), 1);
+ reading->setTest(1);
+ QCOMPARE(reading->test(), reading->value(0).toInt());
+ }
+
+ void testFilter()
+ {
+ TestSensor sensor;
+ sensor.connectToBackend();
+
+ QList<QSensorFilter*> actual = sensor.filters();
+ QList<QSensorFilter*> expected = QList<QSensorFilter*>();
+ QCOMPARE(actual, expected);
+
+ QTest::ignoreMessage(QtWarningMsg, "addFilter: passed a null filter! ");
+ sensor.addFilter(0);
+
+ QTest::ignoreMessage(QtWarningMsg, "removeFilter: passed a null filter! ");
+ sensor.removeFilter(0);
+
+ MyFilter *filter = new MyFilter;
+ sensor.addFilter(filter);
+
+ actual = sensor.filters();
+ expected = QList<QSensorFilter*>() << filter;
+ QCOMPARE(actual, expected);
+
+ MyFilter *filter2 = new MyFilter;
+ sensor.addFilter(filter2);
+
+ actual = sensor.filters();
+ expected = QList<QSensorFilter*>() << filter << filter2;
+ QCOMPARE(actual, expected);
+
+ delete filter2;
+
+ actual = sensor.filters();
+ expected = QList<QSensorFilter*>() << filter;
+ QCOMPARE(actual, expected);
+
+ sensor.removeFilter(filter);
+
+ actual = sensor.filters();
+ expected = QList<QSensorFilter*>();
+ QCOMPARE(actual, expected);
+
+ delete filter;
+ }
+
+ void testFilter2()
+ {
+ TestSensor sensor;
+ sensor.setProperty("doThis", "setOne");
+ TestSensorFilter *filter1 = new ModFilter;
+ TestSensorFilter *filter2 = new MyFilter;
+ sensor.addFilter(filter1);
+ sensor.start();
+ QCOMPARE(sensor.reading()->test(), 3);
+ sensor.stop();
+ sensor.reading()->setTest(1);
+ sensor.addFilter(filter2);
+ sensor.start();
+ QCOMPARE(sensor.reading()->test(), 1);
+ sensor.stop();
+ delete filter1;
+ delete filter2;
+ }
+
+ void testFilter3()
+ {
+ TestSensor sensor;
+ sensor.setProperty("doThis", "setOne");
+ QSignalSpy spy(&sensor, SIGNAL(readingChanged()));
+ sensor.start();
+ QCOMPARE(spy.count(), 1); // reading changes
+ sensor.stop();
+
+ TestSensorFilter *filter2 = new MyFilter;
+ sensor.addFilter(filter2);
+ sensor.start();
+ QCOMPARE(spy.count(), 1); // filter suppresses reading so it does not change
+ sensor.stop();
+ delete filter2;
+
+ TestSensorFilter *filter1 = new ModFilter;
+ sensor.addFilter(filter1);
+ sensor.start();
+ QCOMPARE(spy.count(), 2); // filter does not suppress reading
+ sensor.stop();
+ delete filter1;
+ }
+
+ void testStart2()
+ {
+ TestSensor sensor;
+ sensor.connectToBackend();
+
+ sensor.setProperty("doThis", "stop");
+ sensor.start();
+ QVERIFY(!sensor.isActive());
+ sensor.stop();
+
+ sensor.setProperty("doThis", "error");
+ sensor.start();
+ QVERIFY(sensor.error() == 1);
+ // Yes, this is non-intuitive but the sensor
+ // decides if an error is fatal or not.
+ // In this case our test sensor is reporting a
+ // non-fatal error so the sensor will start.
+ QVERIFY(sensor.isActive());
+ sensor.stop();
+
+ sensor.setProperty("doThis", "setOne");
+ sensor.start();
+ QCOMPARE(sensor.reading()->timestamp(), qtimestamp(1));
+ QCOMPARE(sensor.reading()->test(), 1);
+ sensor.stop();
+
+ sensor.setProperty("doThis", "setTwo");
+ sensor.start();
+ QCOMPARE(sensor.reading()->timestamp(), qtimestamp(2));
+ QCOMPARE(sensor.reading()->test(), 2);
+ sensor.stop();
+ }
+
+ void testSetBadDataRate()
+ {
+ TestSensor sensor;
+ sensor.connectToBackend();
+
+ QTest::ignoreMessage(QtWarningMsg, "setDataRate: 1 is not supported by the sensor. ");
+ sensor.setDataRate(1);
+ QCOMPARE(sensor.dataRate(), 0);
+
+ QTest::ignoreMessage(QtWarningMsg, "setDataRate: 1000 is not supported by the sensor. ");
+ sensor.setDataRate(1000);
+ QCOMPARE(sensor.dataRate(), 0);
+ }
+
+ void testSetBadDataRateWhenNotConnected()
+ {
+ TestSensor sensor;
+ sensor.setDataRate(0);
+ QCOMPARE(sensor.dataRate(), 0);
+ sensor.setDataRate(300);
+ QCOMPARE(sensor.dataRate(), 300);
+ sensor.setDataRate(350);
+ QTest::ignoreMessage(QtWarningMsg, "setDataRate: 350 is not supported by the sensor. ");
+ sensor.connectToBackend();
+ QCOMPARE(sensor.dataRate(), 0);
+ }
+
+ void testSetBadOutputRange()
+ {
+ TestSensor sensor;
+ sensor.connectToBackend();
+
+ sensor.setOutputRange(-1);
+ QCOMPARE(sensor.outputRange(), -1);
+ QTest::ignoreMessage(QtWarningMsg, "setOutputRange: 300 is not supported by the sensor. ");
+ sensor.setOutputRange(300);
+ QCOMPARE(sensor.outputRange(), -1);
+ }
+
+ void testSetBadOutputRangeWhenNotConnected()
+ {
+ TestSensor sensor;
+ sensor.setOutputRange(300);
+ QCOMPARE(sensor.outputRange(), 300);
+ sensor.setOutputRange(350);
+ QTest::ignoreMessage(QtWarningMsg, "setOutputRange: 350 is not supported by the sensor. ");
+ sensor.connectToBackend();
+ QCOMPARE(sensor.outputRange(), -1);
+ QTest::ignoreMessage(QtWarningMsg, "setOutputRange: -2 is not supported by the sensor. ");
+ sensor.setOutputRange(-2);
+ QCOMPARE(sensor.outputRange(), -1);
+ }
+
+ void testEnumHandling()
+ {
+ {
+ QAmbientLightReading reading;
+ for (int i = 0; i <= 6; i++) {
+ QAmbientLightReading::LightLevel setting = static_cast<QAmbientLightReading::LightLevel>(i);
+ QAmbientLightReading::LightLevel expected = setting;
+ if (i == 6)
+ expected = QAmbientLightReading::Undefined;
+ reading.setLightLevel(setting);
+ QCOMPARE(reading.lightLevel(), expected);
+ }
+ }
+
+ {
+ QOrientationReading reading;
+ for (int i = 0; i <= 7; i++) {
+ QOrientationReading::Orientation setting = static_cast<QOrientationReading::Orientation>(i);
+ QOrientationReading::Orientation expected = setting;
+ if (i == 7)
+ expected = QOrientationReading::Undefined;
+ reading.setOrientation(setting);
+ QCOMPARE(reading.orientation(), expected);
+ }
+ }
+
+ {
+ QTapReading reading;
+ reading.setTapDirection(QTapReading::Undefined);
+ QCOMPARE(reading.tapDirection(), QTapReading::Undefined);
+ reading.setTapDirection(QTapReading::X_Pos);
+ QCOMPARE(reading.tapDirection(), QTapReading::X_Pos);
+ reading.setTapDirection(QTapReading::X_Neg);
+ QCOMPARE(reading.tapDirection(), QTapReading::X_Neg);
+ reading.setTapDirection(QTapReading::Y_Pos);
+ QCOMPARE(reading.tapDirection(), QTapReading::Y_Pos);
+ reading.setTapDirection(QTapReading::Y_Neg);
+ QCOMPARE(reading.tapDirection(), QTapReading::Y_Neg);
+ reading.setTapDirection(QTapReading::Z_Pos);
+ QCOMPARE(reading.tapDirection(), QTapReading::Z_Pos);
+ reading.setTapDirection(QTapReading::Z_Neg);
+ QCOMPARE(reading.tapDirection(), QTapReading::Z_Neg);
+ // Directions can be ORed together
+ reading.setTapDirection(QTapReading::X_Both);
+ QCOMPARE(reading.tapDirection(), QTapReading::X_Both);
+ reading.setTapDirection(QTapReading::Y_Both);
+ QCOMPARE(reading.tapDirection(), QTapReading::Y_Both);
+ reading.setTapDirection(QTapReading::Z_Both);
+ QCOMPARE(reading.tapDirection(), QTapReading::Z_Both);
+ // You can't set just the Axis
+ reading.setTapDirection(QTapReading::X);
+ QCOMPARE(reading.tapDirection(), QTapReading::Undefined);
+ reading.setTapDirection(QTapReading::Y);
+ QCOMPARE(reading.tapDirection(), QTapReading::Undefined);
+ reading.setTapDirection(QTapReading::Z);
+ QCOMPARE(reading.tapDirection(), QTapReading::Undefined);
+ reading.setTapDirection(static_cast<QTapReading::TapDirection>(0x1000));
+ QCOMPARE(reading.tapDirection(), QTapReading::Undefined);
+ }
+ }
+
+ void testSetActive()
+ {
+ TestSensor sensor;
+ sensor.setActive(true);
+ // doesn't start till the event loop is hit
+ QVERIFY(!sensor.isActive());
+ // hit the event loop
+ QTest::qWait(0);
+ QVERIFY(sensor.isActive());
+ sensor.setActive(true);
+ QVERIFY(sensor.isActive());
+ // it does stop immediately
+ sensor.setActive(false);
+ QVERIFY(!sensor.isActive());
+ }
+
+ void testIsRegistered()
+ {
+ bool expected;
+ bool actual;
+
+ expected = true;
+ actual = QSensorManager::isBackendRegistered(TestSensor::type, testsensorimpl_id);
+ QCOMPARE(expected, actual);
+
+ expected = false;
+ actual = QSensorManager::isBackendRegistered(TestSensor::type, "random");
+ QCOMPARE(expected, actual);
+
+ expected = false;
+ actual = QSensorManager::isBackendRegistered("random", "random");
+ QCOMPARE(expected, actual);
+ }
+
+ void testAllTheInterfaces()
+ {
+ register_test_backends();
+
+ TEST_SENSORINTERFACE(QAccelerometer, QAccelerometerReading, {
+ QCOMPARE(reading->x(), 1.0);
+ QCOMPARE(reading->y(), 1.0);
+ QCOMPARE(reading->z(), 1.0);
+ })
+
+ TEST_SENSORINTERFACE(QAmbientLightSensor, QAmbientLightReading, {
+ QCOMPARE(reading->lightLevel(), QAmbientLightReading::Twilight);
+ })
+
+ TEST_SENSORINTERFACE(QCompass, QCompassReading, {
+ QCOMPARE(reading->azimuth(), 1.0);
+ QCOMPARE(reading->calibrationLevel(), 1.0);
+ })
+
+ TEST_SENSORINTERFACE(QGyroscope, QGyroscopeReading, {
+ QCOMPARE(reading->x(), 1.0);
+ QCOMPARE(reading->y(), 1.0);
+ QCOMPARE(reading->z(), 1.0);
+ })
+
+ TEST_SENSORINTERFACE(QLightSensor, QLightReading, {
+ QCOMPARE(reading->lux(), 1.0);
+ })
+
+ TEST_SENSORINTERFACE(QMagnetometer, QMagnetometerReading, {
+ QCOMPARE(reading->x(), 1.0);
+ QCOMPARE(reading->y(), 1.0);
+ QCOMPARE(reading->z(), 1.0);
+ QCOMPARE(reading->calibrationLevel(), 1.0);
+ })
+
+ TEST_SENSORINTERFACE(QOrientationSensor, QOrientationReading, {
+ QCOMPARE(reading->orientation(), QOrientationReading::LeftUp);
+ })
+
+ TEST_SENSORINTERFACE(QProximitySensor, QProximityReading, {
+ QCOMPARE(reading->close(), true);
+ })
+
+ TEST_SENSORINTERFACE(QRotationSensor, QRotationReading, {
+ QCOMPARE(reading->x(), 1.0);
+ QCOMPARE(reading->y(), 1.0);
+ QCOMPARE(reading->z(), 1.0);
+ })
+
+ TEST_SENSORINTERFACE(QTapSensor, QTapReading, {
+ QCOMPARE(reading->tapDirection(), QTapReading::Z_Both);
+ QCOMPARE(reading->isDoubleTap(), true);
+ })
+
+ unregister_test_backends();
+ }
+
+ void testBusyChanged()
+ {
+ // Start an exclusive sensor
+ TestSensor sensor1;
+ sensor1.setProperty("exclusive", true);
+ sensor1.start();
+ QVERIFY(sensor1.isActive());
+
+ // Try to start another one, sensor reports busy
+ TestSensor sensor2;
+ sensor2.setProperty("exclusive", true);
+ sensor2.start();
+ QVERIFY(sensor2.isBusy());
+ QVERIFY(!sensor2.isActive());
+
+ // Stopping the first instance causes the busyChanged signal to be emitted from the second instance
+ QSignalSpy spy(&sensor2, SIGNAL(busyChanged()));
+ sensor1.stop();
+ QCOMPARE(spy.count(), 1);
+
+ // Now we can start the second instance
+ sensor2.start();
+ QVERIFY(sensor2.isActive());
+ }
+};
+
+QTEST_MAIN(tst_QtmSensor)
+
+#include "tst_qtmsensor.moc"