From 22260646a9fa7dffbd506b6bdd2586ad3d4d3556 Mon Sep 17 00:00:00 2001 From: Lincoln Ramsay Date: Fri, 29 Jul 2011 16:52:27 +1000 Subject: Pull in the qsensor unit test from Mobility Change-Id: I72aa59c7253267b0dd4abccf71d37af0929b5e0b Reviewed-on: http://codereview.qt.nokia.com/2378 Reviewed-by: Qt Sanity Bot Reviewed-by: Lorn Potter --- tests/auto/qsensor/main.cpp | 76 +++ tests/auto/qsensor/qsensor.pro | 26 + tests/auto/qsensor/test_backends.cpp | 95 ++++ tests/auto/qsensor/test_backends.h | 144 +++++ tests/auto/qsensor/test_sensor.cpp | 61 ++ tests/auto/qsensor/test_sensor.h | 90 +++ tests/auto/qsensor/test_sensor2.cpp | 83 +++ tests/auto/qsensor/test_sensor2.h | 109 ++++ tests/auto/qsensor/test_sensor2_p.h | 69 +++ tests/auto/qsensor/test_sensor2impl.cpp | 71 +++ tests/auto/qsensor/test_sensor2impl.h | 62 ++ tests/auto/qsensor/test_sensor_p.h | 67 +++ tests/auto/qsensor/test_sensorimpl.cpp | 119 ++++ tests/auto/qsensor/test_sensorimpl.h | 67 +++ tests/auto/qsensor/test_sensorplugin.cpp | 122 ++++ tests/auto/qsensor/tst_qsensor.cpp | 942 +++++++++++++++++++++++++++++++ 16 files changed, 2203 insertions(+) create mode 100644 tests/auto/qsensor/main.cpp create mode 100644 tests/auto/qsensor/qsensor.pro create mode 100644 tests/auto/qsensor/test_backends.cpp create mode 100644 tests/auto/qsensor/test_backends.h create mode 100644 tests/auto/qsensor/test_sensor.cpp create mode 100644 tests/auto/qsensor/test_sensor.h create mode 100644 tests/auto/qsensor/test_sensor2.cpp create mode 100644 tests/auto/qsensor/test_sensor2.h create mode 100644 tests/auto/qsensor/test_sensor2_p.h create mode 100644 tests/auto/qsensor/test_sensor2impl.cpp create mode 100644 tests/auto/qsensor/test_sensor2impl.h create mode 100644 tests/auto/qsensor/test_sensor_p.h create mode 100644 tests/auto/qsensor/test_sensorimpl.cpp create mode 100644 tests/auto/qsensor/test_sensorimpl.h create mode 100644 tests/auto/qsensor/test_sensorplugin.cpp create mode 100644 tests/auto/qsensor/tst_qsensor.cpp (limited to 'tests/auto/qsensor') diff --git a/tests/auto/qsensor/main.cpp b/tests/auto/qsensor/main.cpp new file mode 100644 index 00000000..93945348 --- /dev/null +++ b/tests/auto/qsensor/main.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "gruesensorimpl.h" +#include +#include +#include +#include +#include + +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/qsensor/qsensor.pro b/tests/auto/qsensor/qsensor.pro new file mode 100644 index 00000000..c983c0c1 --- /dev/null +++ b/tests/auto/qsensor/qsensor.pro @@ -0,0 +1,26 @@ +TEMPLATE = app +TARGET = tst_qsensor + +CONFIG += testcase +QT += testlib sensors-private + +SOURCES += \ + tst_qsensor.cpp + +HEADERS += \ + test_sensor.h\ + test_sensor_p.h\ + test_sensorimpl.h\ + test_sensor2.h\ + test_sensor2_p.h\ + test_sensor2impl.h\ + test_backends.h + +SOURCES += \ + test_sensor.cpp\ + test_sensorimpl.cpp\ + test_sensor2.cpp\ + test_sensor2impl.cpp\ + test_sensorplugin.cpp\ + test_backends.cpp + diff --git a/tests/auto/qsensor/test_backends.cpp b/tests/auto/qsensor/test_backends.cpp new file mode 100644 index 00000000..b810db2b --- /dev/null +++ b/tests/auto/qsensor/test_backends.cpp @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "qsensorbackend.h" + +typedef QSensorBackend* (*CreateFunc) (QSensor *sensor); +class Record +{ +public: + QByteArray type; + CreateFunc func; +}; +static QList records; + +static bool registerTestBackend(const char *className, CreateFunc func) +{ + Record record; + record.type = className; + record.func = func; + records << record; + return true; +} + +#define REGISTER_TOO +#include "test_backends.h" +#include + +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); + } +} + diff --git a/tests/auto/qsensor/test_backends.h b/tests/auto/qsensor/test_backends.h new file mode 100644 index 00000000..289d59ba --- /dev/null +++ b/tests/auto/qsensor/test_backends.h @@ -0,0 +1,144 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEST_BACKENDS_H +#define TEST_BACKENDS_H + +#include + +void register_test_backends(); +void unregister_test_backends(); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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(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();\ + ReadingClass *reading = sensor.reading();\ + readingcode\ + } while (0); + +#endif diff --git a/tests/auto/qsensor/test_sensor.cpp b/tests/auto/qsensor/test_sensor.cpp new file mode 100644 index 00000000..0f9e2b05 --- /dev/null +++ b/tests/auto/qsensor/test_sensor.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "test_sensor.h" +#include "test_sensor_p.h" + +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.cpp" diff --git a/tests/auto/qsensor/test_sensor.h b/tests/auto/qsensor/test_sensor.h new file mode 100644 index 00000000..2d03dba8 --- /dev/null +++ b/tests/auto/qsensor/test_sensor.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEST_SENSOR_H +#define TEST_SENSOR_H + +#include + +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(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(QSensor::reading()); } + static const char *type; + + // used by the testSensorsChangedSignal test function + int sensorsChangedEmitted; +private slots: + void s_availableSensorsChanged() + { + sensorsChangedEmitted++; + } +}; + +#endif diff --git a/tests/auto/qsensor/test_sensor2.cpp b/tests/auto/qsensor/test_sensor2.cpp new file mode 100644 index 00000000..cf73a28a --- /dev/null +++ b/tests/auto/qsensor/test_sensor2.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "test_sensor2.h" +#include "test_sensor2_p.h" + +#undef IMPLEMENT_READING +#undef IMPLEMENT_READING_D + +#define IMPLEMENT_READING(classname)\ + IMPLEMENT_READING_D(classname, classname ## Private) + +#define IMPLEMENT_READING_D(classname, pclassname)\ + classname::classname(QObject *parent)\ + : QSensorReading(parent, new pclassname)\ + , d(d_ptr())\ + {}\ + classname::~classname() {}\ + void classname::copyValuesFrom(QSensorReading *_other)\ + {\ + /* No need to verify types, only called by QSensorBackend */\ + classname *other = static_cast(_other);\ + pclassname *my_ptr = static_cast(d_ptr()->data());\ + pclassname *other_ptr = static_cast(other->d_ptr()->data());\ + /* Do a direct copy of the private class */\ + *(my_ptr) = *(other_ptr);\ + } + +IMPLEMENT_READING(TestSensor2Reading) + +int TestSensor2Reading::test() const +{ + return d->test; +} + +void TestSensor2Reading::setTest(int test) +{ + d->test = test; +} + +// ===================================================================== + +char const * const TestSensor2::type("test sensor 2"); + +#include "moc_test_sensor2.cpp" diff --git a/tests/auto/qsensor/test_sensor2.h b/tests/auto/qsensor/test_sensor2.h new file mode 100644 index 00000000..a6775d6a --- /dev/null +++ b/tests/auto/qsensor/test_sensor2.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEST_SENSOR2_H +#define TEST_SENSOR2_H + +#include "qsensor.h" + +#undef DECLARE_READING +#undef DECLARE_READING_D + +template +class qTypedWrapper +{ +public: + qTypedWrapper(QScopedPointer *_ptr) + : ptr(_ptr) + { + } + + T *operator->() const + { + return static_cast(ptr->data()); + } + +private: + QScopedPointer *ptr; +}; + +#define DECLARE_READING(classname)\ + DECLARE_READING_D(classname, classname ## Private) + +#define DECLARE_READING_D(classname, pclassname)\ + public:\ + classname(QObject *parent = 0);\ + virtual ~classname();\ + void copyValuesFrom(QSensorReading *other);\ + private:\ + qTypedWrapper d; + +class TestSensor2ReadingPrivate; + +class TestSensor2Reading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(int test READ test) + DECLARE_READING(TestSensor2Reading) +public: + int test() const; + void setTest(int test); +}; + +class TestSensor2Filter : public QSensorFilter +{ +public: + virtual bool filter(TestSensor2Reading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class TestSensor2 : public QSensor +{ + Q_OBJECT +public: + explicit TestSensor2(QObject *parent = 0) : QSensor(TestSensor2::type, parent) {} + virtual ~TestSensor2() {} + TestSensor2Reading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +#endif diff --git a/tests/auto/qsensor/test_sensor2_p.h b/tests/auto/qsensor/test_sensor2_p.h new file mode 100644 index 00000000..564918f3 --- /dev/null +++ b/tests/auto/qsensor/test_sensor2_p.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEST_SENSOR2_P_H +#define TEST_SENSOR2_P_H + +// +// 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. +// + +#include "private/qsensor_p.h" + +class TestSensor2ReadingPrivate : public QSensorReadingPrivate +{ +public: + TestSensor2ReadingPrivate() + : test(0) + { + } + + int test; +}; + +#endif diff --git a/tests/auto/qsensor/test_sensor2impl.cpp b/tests/auto/qsensor/test_sensor2impl.cpp new file mode 100644 index 00000000..882a3059 --- /dev/null +++ b/tests/auto/qsensor/test_sensor2impl.cpp @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "test_sensor2impl.h" +#include +#include + +char const * const testsensor2impl::id("test sensor 2 impl"); + +testsensor2impl::testsensor2impl(QSensor *sensor) + : QSensorBackend(sensor) +{ + setReading(&m_reading); +} + +void testsensor2impl::start() +{ + QString doThis = sensor()->property("doThis").toString(); + if (doThis == "setOne") { + m_reading.setTimestamp(1); + m_reading.setTest(1); + newReadingAvailable(); + } else { + m_reading.setTimestamp(2); + m_reading.setTest(2); + newReadingAvailable(); + } +} + +void testsensor2impl::stop() +{ +} + diff --git a/tests/auto/qsensor/test_sensor2impl.h b/tests/auto/qsensor/test_sensor2impl.h new file mode 100644 index 00000000..c6a47f80 --- /dev/null +++ b/tests/auto/qsensor/test_sensor2impl.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEST_SENSOR2IMPL_H +#define TEST_SENSOR2IMPL_H + +#include +#include "test_sensor2.h" + +class testsensor2impl : public QSensorBackend +{ +public: + static char const * const id; + + testsensor2impl(QSensor *sensor); + + void start(); + void stop(); + +private: + TestSensor2Reading m_reading; +}; + +#endif diff --git a/tests/auto/qsensor/test_sensor_p.h b/tests/auto/qsensor/test_sensor_p.h new file mode 100644 index 00000000..7d1d1131 --- /dev/null +++ b/tests/auto/qsensor/test_sensor_p.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEST_SENSOR_P_H +#define TEST_SENSOR_P_H + +// +// 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; +}; + +#endif diff --git a/tests/auto/qsensor/test_sensorimpl.cpp b/tests/auto/qsensor/test_sensorimpl.cpp new file mode 100644 index 00000000..8ee864b9 --- /dev/null +++ b/tests/auto/qsensor/test_sensorimpl.cpp @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "test_sensorimpl.h" +#include + +const char *testsensorimpl::id("test sensor impl"); + +static testsensorimpl *exclusiveHandle = 0; + +testsensorimpl::testsensorimpl(QSensor *sensor) + : QSensorBackend(sensor) +{ + setReading(&m_reading); + setDescription("sensor description"); + addOutputRange(0, 1, 0.5); + addOutputRange(0, 2, 1); + QString doThis = sensor->property("doThis").toString(); + if (doThis == "rates(0)") { + setDataRates(0); + } else if (doThis == "rates(nodef)") { + TestSensor *acc = new TestSensor(this); + setDataRates(acc); + delete acc; + } else if (doThis == "rates") { + TestSensor *acc = new TestSensor(this); + acc->connectToBackend(); + setDataRates(acc); + delete acc; + } else { + addDataRate(100, 100); + } + reading(); +} + +testsensorimpl::~testsensorimpl() +{ + Q_ASSERT(exclusiveHandle != this); +} + +void testsensorimpl::start() +{ + QVariant _exclusive = sensor()->property("exclusive"); + bool exclusive = _exclusive.isValid()?_exclusive.toBool():false; + if (exclusive) { + if (!exclusiveHandle) { + exclusiveHandle = this; + } else { + // Hook up the busyChanged signal + connect(exclusiveHandle, SIGNAL(emitBusyChanged()), sensor(), SIGNAL(busyChanged())); + sensorBusy(); // report the busy condition + return; + } + } + + QString doThis = sensor()->property("doThis").toString(); + if (doThis == "stop") + sensorStopped(); + else if (doThis == "error") + sensorError(1); + else if (doThis == "setOne") { + m_reading.setTimestamp(1); + m_reading.setTest(1); + newReadingAvailable(); + } else { + m_reading.setTimestamp(2); + m_reading.setTest(2); + newReadingAvailable(); + } +} + +void testsensorimpl::stop() +{ + QVariant _exclusive = sensor()->property("exclusive"); + bool exclusive = _exclusive.isValid()?_exclusive.toBool():false; + if (exclusive && exclusiveHandle == this) { + exclusiveHandle = 0; + emit emitBusyChanged(); // notify any waiting instances that they can try to grab the sensor now + } +} + diff --git a/tests/auto/qsensor/test_sensorimpl.h b/tests/auto/qsensor/test_sensorimpl.h new file mode 100644 index 00000000..13c8af54 --- /dev/null +++ b/tests/auto/qsensor/test_sensorimpl.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TEST_SENSORIMPL_H +#define TEST_SENSORIMPL_H + +#include +#include "test_sensor.h" + +class testsensorimpl : public QSensorBackend +{ + Q_OBJECT +public: + static const char *id; + + testsensorimpl(QSensor *sensor); + ~testsensorimpl(); + + void start(); + void stop(); + +signals: + void emitBusyChanged(); + +private: + TestSensorReading m_reading; +}; + +#endif diff --git a/tests/auto/qsensor/test_sensorplugin.cpp b/tests/auto/qsensor/test_sensorplugin.cpp new file mode 100644 index 00000000..1f14129b --- /dev/null +++ b/tests/auto/qsensor/test_sensorplugin.cpp @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "test_sensorimpl.h" +#include "test_sensor2impl.h" +#include +#include +#include +#include +#include +#include + +class TestSensorPlugin : public QObject, + public QSensorPluginInterface, + public QSensorChangesInterface, + public QSensorBackendFactory +{ + Q_OBJECT + Q_INTERFACES(QSensorPluginInterface QSensorChangesInterface) +public: + void registerSensors() + { + static bool recursive = false; + QVERIFY2(!recursive, "Recursively called TestSensorPlugin::registerSensors!"); + if (recursive) return; + recursive = true; + + // This is bad code. It caused a crash due to recursively calling + // loadPlugins() in qsensormanager.cpp (because loadPlugins() did + // not set the pluginsLoaded flag soon enough). + (void)QSensor::defaultSensorForType(TestSensor::type); + + QSensorManager::registerBackend(TestSensor::type, testsensorimpl::id, this); + QSensorManager::registerBackend(TestSensor::type, "test sensor 2", this); + QSensorManager::registerBackend(TestSensor2::type, testsensor2impl::id, this); + } + + void sensorsChanged() + { + // Register a new type on initial load + // This is testing the "don't emit availableSensorsChanged() too many times" functionality. + if (!QSensorManager::isBackendRegistered(TestSensor::type, "test sensor 3")) + QSensorManager::registerBackend(TestSensor::type, "test sensor 3", this); + + // When a sensor of type "a random type" is registered, register another sensor. + // This is testing the "don't emit availableSensorsChanged() too many times" functionality. + if (!QSensor::defaultSensorForType("a random type").isEmpty()) { + if (!QSensorManager::isBackendRegistered("a random type 2", "random.dynamic")) + QSensorManager::registerBackend("a random type 2", "random.dynamic", this); + } else { + if (QSensorManager::isBackendRegistered("a random type 2", "random.dynamic")) + QSensorManager::unregisterBackend("a random type 2", "random.dynamic"); + } + } + + QSensorBackend *createBackend(QSensor *sensor) + { + if (sensor->identifier() == testsensorimpl::id) { + return new testsensorimpl(sensor); + } + if (sensor->identifier() == testsensor2impl::id) { + return new testsensor2impl(sensor); + } + + qWarning() << "Can't create backend" << sensor->identifier(); + return 0; + } +}; + +REGISTER_STATIC_PLUGIN_V2(TestSensorPlugin) + +class LegacySensorPlugin : public QSensorPluginInterface +{ +public: + void registerSensors() + { + qWarning() << "Loaded the LegacySensorPlugin"; + } +}; + +REGISTER_STATIC_PLUGIN_V1(LegacySensorPlugin) + +#include "test_sensorplugin.moc" + diff --git a/tests/auto/qsensor/tst_qsensor.cpp b/tests/auto/qsensor/tst_qsensor.cpp new file mode 100644 index 00000000..3198f80c --- /dev/null +++ b/tests/auto/qsensor/tst_qsensor.cpp @@ -0,0 +1,942 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//TESTED_COMPONENT=src/sensors + +#include +#include +#include +#include +#include +#include + +#include "qsensor.h" +#include "test_sensor.h" +#include "test_sensor2.h" +#include "test_sensorimpl.h" +#include "test_backends.h" + +// The unit test needs to change the behaviour of the library. It does this +// through an exported but undocumented function. +Q_SENSORS_EXPORT void sensors_unit_test_hook(int index); +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 &data) + { + QStringList list; + foreach (const QByteArray &str, data) { + list << QString::fromLatin1(str); + } + QString ret = QString("QList: (%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; + } +}; + +class MyFactory : public QSensorBackendFactory +{ + QSensorBackend *createBackend(QSensor * /*sensor*/) + { + return 0; + } +}; + +/* + Unit test for QSensor class. +*/ +class tst_QSensor : public QObject +{ + Q_OBJECT + +public: + tst_QSensor() + { + 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 expected; + expected << TestSensor::type << TestSensor2::type; + QList actual = QSensor::sensorTypes(); + qSort(actual); // The actual list is not in a defined order + QCOMPARE(actual, expected); + } + + void testSensorRegistered() + { + QList expected; + expected << "test sensor 2" << "test sensor 3" << testsensorimpl::id; + QList 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 expected; + QList 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); + quint64 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 actual = sensor.filters(); + QList expected = QList(); + 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() << filter; + QCOMPARE(actual, expected); + + MyFilter *filter2 = new MyFilter; + sensor.addFilter(filter2); + + actual = sensor.filters(); + expected = QList() << filter << filter2; + QCOMPARE(actual, expected); + + delete filter2; + + actual = sensor.filters(); + expected = QList() << filter; + QCOMPARE(actual, expected); + + sensor.removeFilter(filter); + + actual = sensor.filters(); + expected = QList(); + 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(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(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(0x1000)); + QCOMPARE(reading.tapDirection(), QTapReading::Undefined); + } + } + + void testDynamicDefaultsAndGenericHandling() + { + QByteArray expected; + QByteArray actual; + MyFactory factory; + + // The default for this type is null + expected = QByteArray(); + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + + // Register a bogus backend + QSensorManager::registerBackend("random", "generic.random", &factory); + + // The default for this type is the newly-registered backend + expected = "generic.random"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + + // Register a non-generic bogus backend + QSensorManager::registerBackend("random", "not.generic.random", &factory); + + // The default for this type is the newly-registered backend + expected = "not.generic.random"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + + // Unregister the non-generic bogus backend + QSensorManager::unregisterBackend("random", "not.generic.random"); + + // The default for this type is the generic backend + expected = "generic.random"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + + // Unregister a bogus backend + QSensorManager::unregisterBackend("random", "generic.random"); + + // The default for this type is null again + expected = QByteArray(); + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + + + // Now test out some more of the logic + // Register 2 backends and unregister the first. + QSensorManager::registerBackend("random", "random.1", &factory); + expected = "random.1"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + QSensorManager::registerBackend("random", "random.2", &factory); + expected = "random.1"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + QSensorManager::unregisterBackend("random", "random.1"); + expected = "random.2"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + QSensorManager::unregisterBackend("random", "random.2"); + expected = QByteArray(); + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + + // Now stick a generic backend into the mix and ensure the correct thing happens + QSensorManager::registerBackend("random", "random.1", &factory); + expected = "random.1"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + QSensorManager::registerBackend("random", "generic.random.2", &factory); + expected = "random.1"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + QSensorManager::registerBackend("random", "random.2", &factory); + expected = "random.1"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + QSensorManager::unregisterBackend("random", "random.1"); + expected = "random.2"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + QSensorManager::unregisterBackend("random", "generic.random.2"); + expected = "random.2"; + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + QSensorManager::unregisterBackend("random", "random.2"); + expected = QByteArray(); + actual = QSensor::defaultSensorForType("random"); + QCOMPARE(expected, actual); + } + + void testCreation2() + { + MyFactory factory; + + QSensorManager::registerBackend("random", "random.1", &factory); + QSensorManager::registerBackend("random", "random.2", &factory); + QSensor random("random"); + // This is a sensorlog, not a warning + //QTest::ignoreMessage(QtWarningMsg, "no suitable backend found for requested identifier \"\" and type \"random\" "); + random.connectToBackend(); + QVERIFY(!random.isConnectedToBackend()); + random.setIdentifier("random.3"); + // This is a sensorlog, not a warning + //QTest::ignoreMessage(QtWarningMsg, "no backend with identifier \"random.3\" for type \"random\" "); + random.connectToBackend(); + QVERIFY(!random.isConnectedToBackend()); + random.setIdentifier("random.1"); + random.connectToBackend(); + QVERIFY(!random.isConnectedToBackend()); + QSensorManager::unregisterBackend("random", "random.1"); + QSensorManager::unregisterBackend("random", "random.2"); + } + + void testSensorsChangedSignal() + { + TestSensor sensor; + MyFactory factory; + + // Register a bogus backend + sensor.sensorsChangedEmitted = 0; + QSensorManager::registerBackend("a random type", "a random id", &factory); + QCOMPARE(sensor.sensorsChangedEmitted, 1); + + // Register it again (creates a warning) + sensor.sensorsChangedEmitted = 0; + QTest::ignoreMessage(QtWarningMsg, "A backend with type \"a random type\" and identifier \"a random id\" has already been registered! "); + QSensorManager::registerBackend("a random type", "a random id", &factory); + QCOMPARE(sensor.sensorsChangedEmitted, 0); + + // Unregister a bogus backend + sensor.sensorsChangedEmitted = 0; + QSensorManager::unregisterBackend("a random type", "a random id"); + QCOMPARE(sensor.sensorsChangedEmitted, 1); + + // Unregister an unknown identifier + sensor.sensorsChangedEmitted = 0; + QTest::ignoreMessage(QtWarningMsg, "Identifier \"a random id\" is not registered "); + QSensorManager::unregisterBackend(TestSensor::type, "a random id"); + QCOMPARE(sensor.sensorsChangedEmitted, 0); + + // Unregister for an unknown type + sensor.sensorsChangedEmitted = 0; + QTest::ignoreMessage(QtWarningMsg, "No backends of type \"foo\" are registered "); + QSensorManager::unregisterBackend("foo", "bar"); + QCOMPARE(sensor.sensorsChangedEmitted, 0); + + // Make sure we've cleaned up the list of available types + QList expected; + expected << TestSensor::type << TestSensor2::type; + QList actual = QSensor::sensorTypes(); + qSort(actual); // The actual list is not in a defined order + QCOMPARE(actual, expected); + } + + 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 testReadingBC() + { + // QSensorReading changed in 1.0.1 due to QTMOBILITY-226 + // This test verifies that a backend built against the 1.0.0 + // version of qsensor.h still runs. + TestSensor2 sensor; + + 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 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()); + } + + // This test must be LAST or it will interfere with the other tests + void testLoadingPlugins() + { + // Go ahead and load the actual plugins (as a test that plugin loading works) + sensors_unit_test_hook(1); + + // Hmm... There's no real way to tell if this worked or not. + // If it doesn't work the unit test will probably crash. + // That's what it did on Symbian before plugin loading was fixed. + } +}; + +QTEST_MAIN(tst_QSensor) + +#include "tst_qsensor.moc" -- cgit v1.2.3