tests/auto/qsensor/tst_qsensor.cpp
changeset 0 876b1a06bc25
child 5 603d3f8b6302
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <QObject>
       
    42 #include <QTest>
       
    43 #include <QDebug>
       
    44 #include <QSettings>
       
    45 
       
    46 #include "qsensor.h"
       
    47 #include "test_sensor.h"
       
    48 #include "test_sensorimpl.h"
       
    49 
       
    50 QTM_USE_NAMESPACE
       
    51 
       
    52         class MyFilter : public TestSensorFilter
       
    53 {
       
    54     bool filter(TestSensorReading *reading)
       
    55     {
       
    56         return reading->test();
       
    57     }
       
    58 };
       
    59 
       
    60 /*
       
    61     Unit test for QSensor class.
       
    62 */
       
    63 class tst_QSensor : public QObject
       
    64 {
       
    65     Q_OBJECT
       
    66 
       
    67 public:
       
    68     tst_QSensor()
       
    69     {
       
    70     }
       
    71 
       
    72 private slots:
       
    73     void initTestCase()
       
    74     {
       
    75         QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
       
    76         settings.clear();
       
    77     }
       
    78 
       
    79     void cleanupTestCase()
       
    80     {
       
    81         QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
       
    82         settings.clear();
       
    83     }
       
    84 
       
    85 
       
    86     void testTypeRegistered()
       
    87     {
       
    88         QList<QByteArray> expected;
       
    89         expected << "QAccelerometer" << TestSensor::type;
       
    90         QList<QByteArray> actual = QSensor::sensorTypes();
       
    91 
       
    92         for (int i = 0; i < expected.size(); ++i) {
       
    93             QVERIFY2(actual.contains(expected.at(i)),expected.at(i)+" not present");
       
    94         }
       
    95     }
       
    96 
       
    97     void testSensorRegistered()
       
    98     {
       
    99         QList<QByteArray> expected;
       
   100         expected << testsensorimpl::id << "test sensor 2";
       
   101         QList<QByteArray> actual = QSensor::sensorsForType(TestSensor::type);
       
   102         QCOMPARE(actual, expected);
       
   103     }
       
   104 
       
   105     void testSensorDefault()
       
   106     {
       
   107         QByteArray expected = testsensorimpl::id;
       
   108         QByteArray actual = QSensor::defaultSensorForType(TestSensor::type);
       
   109         QCOMPARE(actual, expected);
       
   110     }
       
   111 
       
   112     void testBadDefaultFromConfig()
       
   113     {
       
   114         QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
       
   115         settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray("bogus id"));
       
   116         settings.sync();
       
   117 
       
   118         QByteArray expected = testsensorimpl::id;
       
   119         QByteArray actual = QSensor::defaultSensorForType(TestSensor::type);
       
   120         QCOMPARE(actual, expected);
       
   121     }
       
   122 
       
   123     void testGoodDefaultFromConfig()
       
   124     {
       
   125         QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
       
   126         settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray(testsensorimpl::id));
       
   127         settings.sync();
       
   128 
       
   129         QByteArray expected = testsensorimpl::id;
       
   130         QByteArray actual = QSensor::defaultSensorForType(TestSensor::type);
       
   131         QCOMPARE(actual, expected);
       
   132     }
       
   133 
       
   134     void testNoSensorsForType()
       
   135     {
       
   136         QList<QByteArray> expected;
       
   137         QList<QByteArray> actual = QSensor::sensorsForType("bogus type");
       
   138         QCOMPARE(actual, expected);
       
   139     }
       
   140 
       
   141     void testNoDefaultForType()
       
   142     {
       
   143         QByteArray expected;
       
   144         QByteArray actual = QSensor::defaultSensorForType("bogus type");
       
   145         QCOMPARE(actual, expected);
       
   146     }
       
   147 
       
   148     void testCreation()
       
   149     {
       
   150         TestSensor sensor;
       
   151         sensor.connectToBackend();
       
   152         QByteArray expected = testsensorimpl::id;
       
   153         QByteArray actual = sensor.identifier();
       
   154         QCOMPARE(actual, expected);
       
   155     }
       
   156 
       
   157     void testBadDefaultCreation()
       
   158     {
       
   159         QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
       
   160         settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray("test sensor 2"));
       
   161         settings.sync();
       
   162 
       
   163         TestSensor sensor;
       
   164         QTest::ignoreMessage(QtWarningMsg, "Can't create backend \"test sensor 2\" ");
       
   165         sensor.connectToBackend();
       
   166         QByteArray expected = testsensorimpl::id;
       
   167         QByteArray actual = sensor.identifier();
       
   168         QCOMPARE(actual, expected);
       
   169     }
       
   170 
       
   171     void testBadCreation()
       
   172     {
       
   173         QSensor sensor("bogus type");
       
   174         sensor.connectToBackend();
       
   175         QByteArray expected; // should be null
       
   176         QByteArray actual = sensor.identifier();
       
   177         QCOMPARE(actual, expected);
       
   178     }
       
   179 
       
   180     void resetSettings()
       
   181     {
       
   182         QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors"));
       
   183         settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray(testsensorimpl::id));
       
   184         settings.sync();
       
   185     }
       
   186 
       
   187     void testTimestamp()
       
   188     {
       
   189         TestSensor sensor;
       
   190         sensor.connectToBackend();
       
   191         QVERIFY(sensor.reading() != 0);
       
   192         quint64 timestamp = sensor.reading()->timestamp();
       
   193         QVERIFY(timestamp == qtimestamp());
       
   194     }
       
   195 
       
   196     void testStart()
       
   197     {
       
   198         TestSensor sensor;
       
   199         sensor.start();
       
   200         QVERIFY(sensor.isActive());
       
   201         sensor.start();
       
   202         QVERIFY(sensor.isActive());
       
   203     }
       
   204 
       
   205     void testBadStart()
       
   206     {
       
   207         QSensor sensor("bogus type");
       
   208         sensor.start();
       
   209         QVERIFY(!sensor.isActive());
       
   210     }
       
   211 
       
   212     void testStop()
       
   213     {
       
   214         TestSensor sensor;
       
   215         sensor.stop();
       
   216         QVERIFY(!sensor.isActive());
       
   217         sensor.start();
       
   218         QVERIFY(sensor.isActive());
       
   219         sensor.stop();
       
   220         QVERIFY(!sensor.isActive());
       
   221     }
       
   222 
       
   223     void testMetaData()
       
   224     {
       
   225         TestSensor sensor;
       
   226         sensor.connectToBackend();
       
   227 
       
   228         QString actual = sensor.description();
       
   229         QString expected = "sensor description";
       
   230         QCOMPARE(actual, expected);
       
   231         sensor.outputRange();
       
   232         sensor.setOutputRange(1);
       
   233         sensor.outputRanges();
       
   234         sensor.availableDataRates();
       
   235         sensor.setDataRate(100);
       
   236         sensor.dataRate();
       
   237         sensor.isBusy();
       
   238         sensor.error();
       
   239         sensor.isConnectedToBackend();
       
   240 
       
   241         TestSensorReading *reading = sensor.reading();
       
   242         reading->test();
       
   243         QCOMPARE(reading->valueCount(), 1);
       
   244         reading->value(0).toBool();
       
   245     }
       
   246 
       
   247     void testMetaData2()
       
   248     {
       
   249         TestSensor sensor;
       
   250         sensor.setProperty("doThis", "rates(0)");
       
   251         QTest::ignoreMessage(QtWarningMsg, "ERROR: Cannot call QSensorBackend::setDataRates with 0 ");
       
   252         sensor.connectToBackend();
       
   253     }
       
   254 
       
   255     void testMetaData3()
       
   256     {
       
   257         TestSensor sensor;
       
   258         sensor.setProperty("doThis", "rates");
       
   259         sensor.connectToBackend();
       
   260 
       
   261         sensor.availableDataRates();
       
   262     }
       
   263 
       
   264     void testFilter()
       
   265     {
       
   266         TestSensor sensor;
       
   267         sensor.connectToBackend();
       
   268 
       
   269         MyFilter filter;
       
   270         sensor.addFilter(&filter);
       
   271         sensor.removeFilter(&filter);
       
   272         sensor.addFilter(&filter);
       
   273     }
       
   274 
       
   275     void testStart2()
       
   276     {
       
   277         TestSensor sensor;
       
   278         sensor.connectToBackend();
       
   279 
       
   280         sensor.setProperty("doThis", "busy");
       
   281         sensor.start();
       
   282         QVERIFY(!sensor.isActive());
       
   283 
       
   284         sensor.setProperty("doThis", "stop");
       
   285         sensor.start();
       
   286         QVERIFY(!sensor.isActive());
       
   287 
       
   288         sensor.setProperty("doThis", "error");
       
   289         sensor.start();
       
   290         QVERIFY(sensor.error() == 1);
       
   291         QVERIFY(sensor.isActive());
       
   292 
       
   293         MyFilter filter;
       
   294         sensor.addFilter(&filter);
       
   295         sensor.setProperty("doThis", "setFalse");
       
   296         sensor.start();
       
   297         QVERIFY(sensor.isActive());
       
   298 
       
   299         sensor.setProperty("doThis", "setTrue");
       
   300         sensor.start();
       
   301         QVERIFY(sensor.isActive());
       
   302 
       
   303     }
       
   304 
       
   305     void testSetBadRate()
       
   306     {
       
   307         TestSensor sensor;
       
   308         sensor.connectToBackend();
       
   309 
       
   310         QTest::ignoreMessage(QtWarningMsg, "setDataRate: rate 300 is not supported by the sensor. ");
       
   311         sensor.setDataRate(300);
       
   312     }
       
   313 };
       
   314 
       
   315 QTEST_MAIN(tst_QSensor)
       
   316 
       
   317 #include "tst_qsensor.moc"