|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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 << TestSensor::type; |
|
90 QList<QByteArray> actual = QSensor::sensorTypes(); |
|
91 QCOMPARE(actual, expected); |
|
92 } |
|
93 |
|
94 void testSensorRegistered() |
|
95 { |
|
96 QList<QByteArray> expected; |
|
97 expected << testsensorimpl::id << "test sensor 2"; |
|
98 QList<QByteArray> actual = QSensor::sensorsForType(TestSensor::type); |
|
99 QCOMPARE(actual, expected); |
|
100 } |
|
101 |
|
102 void testSensorDefault() |
|
103 { |
|
104 QByteArray expected = testsensorimpl::id; |
|
105 QByteArray actual = QSensor::defaultSensorForType(TestSensor::type); |
|
106 QCOMPARE(actual, expected); |
|
107 } |
|
108 |
|
109 void testBadDefaultFromConfig() |
|
110 { |
|
111 QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); |
|
112 settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray("bogus id")); |
|
113 settings.sync(); |
|
114 |
|
115 QByteArray expected = testsensorimpl::id; |
|
116 QByteArray actual = QSensor::defaultSensorForType(TestSensor::type); |
|
117 QCOMPARE(actual, expected); |
|
118 } |
|
119 |
|
120 void testGoodDefaultFromConfig() |
|
121 { |
|
122 QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); |
|
123 settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray(testsensorimpl::id)); |
|
124 settings.sync(); |
|
125 |
|
126 QByteArray expected = testsensorimpl::id; |
|
127 QByteArray actual = QSensor::defaultSensorForType(TestSensor::type); |
|
128 QCOMPARE(actual, expected); |
|
129 } |
|
130 |
|
131 void testNoSensorsForType() |
|
132 { |
|
133 QList<QByteArray> expected; |
|
134 QList<QByteArray> actual = QSensor::sensorsForType("bogus type"); |
|
135 QCOMPARE(actual, expected); |
|
136 } |
|
137 |
|
138 void testNoDefaultForType() |
|
139 { |
|
140 QByteArray expected; |
|
141 QByteArray actual = QSensor::defaultSensorForType("bogus type"); |
|
142 QCOMPARE(actual, expected); |
|
143 } |
|
144 |
|
145 void testCreation() |
|
146 { |
|
147 TestSensor sensor; |
|
148 sensor.connectToBackend(); |
|
149 QByteArray expected = testsensorimpl::id; |
|
150 QByteArray actual = sensor.identifier(); |
|
151 QCOMPARE(actual, expected); |
|
152 } |
|
153 |
|
154 void testBadDefaultCreation() |
|
155 { |
|
156 QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); |
|
157 settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray("test sensor 2")); |
|
158 settings.sync(); |
|
159 |
|
160 TestSensor sensor; |
|
161 QTest::ignoreMessage(QtWarningMsg, "Can't create backend \"test sensor 2\" "); |
|
162 sensor.connectToBackend(); |
|
163 QByteArray expected = testsensorimpl::id; |
|
164 QByteArray actual = sensor.identifier(); |
|
165 QCOMPARE(actual, expected); |
|
166 } |
|
167 |
|
168 void testBadCreation() |
|
169 { |
|
170 QSensor sensor("bogus type"); |
|
171 sensor.connectToBackend(); |
|
172 QByteArray expected; // should be null |
|
173 QByteArray actual = sensor.identifier(); |
|
174 QCOMPARE(actual, expected); |
|
175 } |
|
176 |
|
177 void resetSettings() |
|
178 { |
|
179 QSettings settings(QLatin1String("Nokia"), QLatin1String("Sensors")); |
|
180 settings.setValue(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(TestSensor::type)), QByteArray(testsensorimpl::id)); |
|
181 settings.sync(); |
|
182 } |
|
183 |
|
184 void testTimestamp() |
|
185 { |
|
186 TestSensor sensor; |
|
187 sensor.connectToBackend(); |
|
188 QVERIFY(sensor.reading() != 0); |
|
189 quint64 timestamp = sensor.reading()->timestamp(); |
|
190 QVERIFY(timestamp == qtimestamp()); |
|
191 } |
|
192 |
|
193 void testStart() |
|
194 { |
|
195 TestSensor sensor; |
|
196 sensor.start(); |
|
197 QVERIFY(sensor.isActive()); |
|
198 sensor.start(); |
|
199 QVERIFY(sensor.isActive()); |
|
200 } |
|
201 |
|
202 void testBadStart() |
|
203 { |
|
204 QSensor sensor("bogus type"); |
|
205 sensor.start(); |
|
206 QVERIFY(!sensor.isActive()); |
|
207 } |
|
208 |
|
209 void testStop() |
|
210 { |
|
211 TestSensor sensor; |
|
212 sensor.stop(); |
|
213 QVERIFY(!sensor.isActive()); |
|
214 sensor.start(); |
|
215 QVERIFY(sensor.isActive()); |
|
216 sensor.stop(); |
|
217 QVERIFY(!sensor.isActive()); |
|
218 } |
|
219 |
|
220 void testMetaData() |
|
221 { |
|
222 TestSensor sensor; |
|
223 sensor.connectToBackend(); |
|
224 |
|
225 QString actual = sensor.description(); |
|
226 QString expected = "sensor description"; |
|
227 QCOMPARE(actual, expected); |
|
228 sensor.outputRange(); |
|
229 sensor.setOutputRange(1); |
|
230 sensor.outputRanges(); |
|
231 sensor.availableDataRates(); |
|
232 sensor.setDataRate(100); |
|
233 sensor.dataRate(); |
|
234 sensor.isBusy(); |
|
235 sensor.error(); |
|
236 sensor.isConnectedToBackend(); |
|
237 |
|
238 TestSensorReading *reading = sensor.reading(); |
|
239 reading->test(); |
|
240 QCOMPARE(reading->valueCount(), 1); |
|
241 reading->value(0).toBool(); |
|
242 } |
|
243 |
|
244 void testMetaData2() |
|
245 { |
|
246 TestSensor sensor; |
|
247 sensor.setProperty("doThis", "rates(0)"); |
|
248 QTest::ignoreMessage(QtWarningMsg, "ERROR: Cannot call QSensorBackend::setDataRates with 0 "); |
|
249 QTest::ignoreMessage(QtWarningMsg, "\"test sensor impl\" backend does not support any data rates. It cannot be used. "); |
|
250 QTest::ignoreMessage(QtWarningMsg, "\"test sensor impl\" backend did not supply default data rate. "); |
|
251 sensor.connectToBackend(); |
|
252 } |
|
253 |
|
254 void testMetaData3() |
|
255 { |
|
256 TestSensor sensor; |
|
257 sensor.setProperty("doThis", "rates"); |
|
258 sensor.connectToBackend(); |
|
259 |
|
260 sensor.availableDataRates(); |
|
261 } |
|
262 |
|
263 void testFilter() |
|
264 { |
|
265 TestSensor sensor; |
|
266 sensor.connectToBackend(); |
|
267 |
|
268 MyFilter filter; |
|
269 sensor.addFilter(&filter); |
|
270 sensor.removeFilter(&filter); |
|
271 sensor.addFilter(&filter); |
|
272 } |
|
273 |
|
274 void testStart2() |
|
275 { |
|
276 TestSensor sensor; |
|
277 sensor.connectToBackend(); |
|
278 |
|
279 sensor.setProperty("doThis", "busy"); |
|
280 sensor.start(); |
|
281 QVERIFY(!sensor.isActive()); |
|
282 |
|
283 sensor.setProperty("doThis", "stop"); |
|
284 sensor.start(); |
|
285 QVERIFY(!sensor.isActive()); |
|
286 |
|
287 sensor.setProperty("doThis", "error"); |
|
288 sensor.start(); |
|
289 QVERIFY(sensor.error() == 1); |
|
290 QVERIFY(sensor.isActive()); |
|
291 |
|
292 MyFilter filter; |
|
293 sensor.addFilter(&filter); |
|
294 sensor.setProperty("doThis", "setFalse"); |
|
295 sensor.start(); |
|
296 QVERIFY(sensor.isActive()); |
|
297 |
|
298 sensor.setProperty("doThis", "setTrue"); |
|
299 sensor.start(); |
|
300 QVERIFY(sensor.isActive()); |
|
301 |
|
302 } |
|
303 |
|
304 void testSetBadRate() |
|
305 { |
|
306 TestSensor sensor; |
|
307 sensor.connectToBackend(); |
|
308 |
|
309 QTest::ignoreMessage(QtWarningMsg, "setDataRate: rate 300 is not supported by the sensor. "); |
|
310 sensor.setDataRate(300); |
|
311 } |
|
312 }; |
|
313 |
|
314 QTEST_MAIN(tst_QSensor) |
|
315 |
|
316 #include "tst_qsensor.moc" |