1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | //TESTED_COMPONENT=src/multimedia |
30 | |
31 | #include <QtTest/QtTest> |
32 | #include <QDebug> |
33 | #include <QTimer> |
34 | #include <QtCore/QMap> |
35 | |
36 | #include <qmediaobject.h> |
37 | #include <qmediacontrol.h> |
38 | #include <qmediaservice.h> |
39 | #include <qradiotunercontrol.h> |
40 | #include <qradiotuner.h> |
41 | |
42 | #include "mockmediaserviceprovider.h" |
43 | #include "mockmediaservice.h" |
44 | #include "mockradiotunercontrol.h" |
45 | #include "mockavailabilitycontrol.h" |
46 | |
47 | QT_USE_NAMESPACE |
48 | |
49 | class tst_QRadioTuner: public QObject |
50 | { |
51 | Q_OBJECT |
52 | |
53 | public slots: |
54 | void initTestCase(); |
55 | void cleanupTestCase(); |
56 | void init(); |
57 | |
58 | private slots: |
59 | void testNullService(); |
60 | void testNullControl(); |
61 | void testBand(); |
62 | void testFrequency(); |
63 | void testMute(); |
64 | void testSearch(); |
65 | void testVolume(); |
66 | void testSignal(); |
67 | void testStereo(); |
68 | void testSearchAllStations(); |
69 | void errorSignal(); |
70 | |
71 | private: |
72 | MockRadioTunerControl *mock; |
73 | MockAvailabilityControl *mockAvailability; |
74 | MockMediaService *service; |
75 | MockMediaServiceProvider *provider; |
76 | QRadioTuner *radio; |
77 | }; |
78 | |
79 | void tst_QRadioTuner::initTestCase() |
80 | { |
81 | qRegisterMetaType<QRadioTuner::State>(typeName: "QRadioTuner::State" ); |
82 | qRegisterMetaType<QRadioTuner::Band>(typeName: "QRadioTuner::Band" ); |
83 | |
84 | mock = new MockRadioTunerControl(this); |
85 | mockAvailability = new MockAvailabilityControl(QMultimedia::Available); |
86 | |
87 | QMap<QString, QMediaControl *> map; |
88 | map.insert(QRadioTunerControl_iid, avalue: mock); |
89 | map.insert(QMediaAvailabilityControl_iid, avalue: mockAvailability); |
90 | |
91 | service = new MockMediaService(this, map); |
92 | provider = new MockMediaServiceProvider(service); |
93 | QMediaServiceProvider::setDefaultServiceProvider(provider); |
94 | |
95 | radio = new QRadioTuner; |
96 | QVERIFY(radio->service() != 0); |
97 | QVERIFY(radio->isAvailable()); |
98 | QVERIFY(radio->availability() == QMultimedia::Available); |
99 | |
100 | QSignalSpy stateSpy(radio, SIGNAL(stateChanged(QRadioTuner::State))); |
101 | |
102 | QCOMPARE(radio->state(), QRadioTuner::StoppedState); |
103 | radio->start(); |
104 | QVERIFY(radio->availability() == QMultimedia::Available); |
105 | QCOMPARE(radio->state(), QRadioTuner::ActiveState); |
106 | |
107 | QCOMPARE(stateSpy.count(), 1); |
108 | QCOMPARE(stateSpy.first()[0].value<QRadioTuner::State>(), QRadioTuner::ActiveState); |
109 | } |
110 | |
111 | void tst_QRadioTuner::cleanupTestCase() |
112 | { |
113 | QVERIFY(radio->error() == QRadioTuner::NoError); |
114 | QVERIFY(radio->errorString().isEmpty()); |
115 | |
116 | QSignalSpy stateSpy(radio, SIGNAL(stateChanged(QRadioTuner::State))); |
117 | |
118 | radio->stop(); |
119 | QVERIFY(radio->availability() == QMultimedia::Available); |
120 | QCOMPARE(radio->state(), QRadioTuner::StoppedState); |
121 | QCOMPARE(stateSpy.count(), 1); |
122 | |
123 | QCOMPARE(stateSpy.first()[0].value<QRadioTuner::State>(), QRadioTuner::StoppedState); |
124 | |
125 | delete radio; |
126 | delete service; |
127 | delete provider; |
128 | delete mockAvailability; |
129 | } |
130 | |
131 | void tst_QRadioTuner::init() |
132 | { |
133 | QMediaServiceProvider::setDefaultServiceProvider(provider); |
134 | } |
135 | |
136 | void tst_QRadioTuner::testNullService() |
137 | { |
138 | const QPair<int, int> nullRange(0, 0); |
139 | |
140 | MockMediaServiceProvider provider(0); |
141 | QMediaServiceProvider::setDefaultServiceProvider(&provider); |
142 | |
143 | QRadioTuner radio; |
144 | QVERIFY(!radio.isAvailable()); |
145 | radio.start(); |
146 | QCOMPARE(radio.error(), QRadioTuner::ResourceError); |
147 | QCOMPARE(radio.errorString(), QString()); |
148 | QCOMPARE(radio.band(), QRadioTuner::FM); |
149 | QCOMPARE(radio.isBandSupported(QRadioTuner::AM), false); |
150 | QCOMPARE(radio.isBandSupported(QRadioTuner::FM), false); |
151 | QCOMPARE(radio.frequency(), 0); |
152 | QCOMPARE(radio.frequencyStep(QRadioTuner::AM), 0); |
153 | QCOMPARE(radio.frequencyStep(QRadioTuner::FM), 0); |
154 | QCOMPARE(radio.frequencyRange(QRadioTuner::AM), nullRange); |
155 | QCOMPARE(radio.frequencyRange(QRadioTuner::FM), nullRange); |
156 | QCOMPARE(radio.isStereo(), false); |
157 | QCOMPARE(radio.stereoMode(), QRadioTuner::Auto); |
158 | QCOMPARE(radio.signalStrength(), 0); |
159 | QCOMPARE(radio.volume(), 0); |
160 | QCOMPARE(radio.isMuted(), false); |
161 | QCOMPARE(radio.isSearching(), false); |
162 | radio.stop(); |
163 | } |
164 | |
165 | void tst_QRadioTuner::testNullControl() |
166 | { |
167 | const QPair<int, int> nullRange(0, 0); |
168 | |
169 | MockMediaService service(0, 0); |
170 | MockMediaServiceProvider provider(&service); |
171 | QMediaServiceProvider::setDefaultServiceProvider(&provider); |
172 | QRadioTuner radio; |
173 | QVERIFY(!radio.isAvailable()); |
174 | radio.start(); |
175 | |
176 | QCOMPARE(radio.error(), QRadioTuner::ResourceError); |
177 | QCOMPARE(radio.errorString(), QString()); |
178 | |
179 | QCOMPARE(radio.band(), QRadioTuner::FM); |
180 | QCOMPARE(radio.isBandSupported(QRadioTuner::AM), false); |
181 | QCOMPARE(radio.isBandSupported(QRadioTuner::FM), false); |
182 | QCOMPARE(radio.frequency(), 0); |
183 | QCOMPARE(radio.frequencyStep(QRadioTuner::AM), 0); |
184 | QCOMPARE(radio.frequencyStep(QRadioTuner::FM), 0); |
185 | QCOMPARE(radio.frequencyRange(QRadioTuner::AM), nullRange); |
186 | QCOMPARE(radio.frequencyRange(QRadioTuner::FM), nullRange); |
187 | { |
188 | QSignalSpy bandSpy(&radio, SIGNAL(bandChanged(QRadioTuner::Band))); |
189 | QSignalSpy frequencySpy(&radio, SIGNAL(frequencyChanged(int))); |
190 | |
191 | radio.setFrequency(107500); |
192 | QCOMPARE(radio.band(), QRadioTuner::FM); |
193 | QCOMPARE(radio.frequency(), 0); |
194 | QCOMPARE(bandSpy.count(), 0); |
195 | QCOMPARE(frequencySpy.count(), 0); |
196 | |
197 | radio.setBand(QRadioTuner::AM); |
198 | QCOMPARE(radio.band(), QRadioTuner::FM); |
199 | QCOMPARE(radio.frequency(), 0); |
200 | QCOMPARE(bandSpy.count(), 0); |
201 | QCOMPARE(frequencySpy.count(), 0); |
202 | } |
203 | QCOMPARE(radio.isStereo(), false); |
204 | QCOMPARE(radio.stereoMode(), QRadioTuner::Auto); |
205 | |
206 | radio.setStereoMode(QRadioTuner::ForceStereo); |
207 | QCOMPARE(radio.stereoMode(), QRadioTuner::Auto); |
208 | |
209 | QCOMPARE(radio.signalStrength(), 0); |
210 | |
211 | QCOMPARE(radio.volume(), 0); |
212 | QCOMPARE(radio.isMuted(), false); |
213 | { |
214 | QSignalSpy volumeSpy(&radio, SIGNAL(volumeChanged(int))); |
215 | QSignalSpy muteSpy(&radio, SIGNAL(mutedChanged(bool))); |
216 | |
217 | radio.setVolume(76); |
218 | QCOMPARE(radio.volume(), 0); |
219 | QCOMPARE(volumeSpy.count(), 0); |
220 | |
221 | radio.setMuted(true); |
222 | QCOMPARE(radio.isMuted(), false); |
223 | QCOMPARE(muteSpy.count(), 0); |
224 | } |
225 | QCOMPARE(radio.isSearching(), false); |
226 | { |
227 | QSignalSpy spy(&radio, SIGNAL(searchingChanged(bool))); |
228 | |
229 | radio.searchBackward(); |
230 | QCOMPARE(radio.isSearching(), false); |
231 | QCOMPARE(spy.count(), 0); |
232 | |
233 | radio.searchForward(); |
234 | QCOMPARE(radio.isSearching(), false); |
235 | QCOMPARE(spy.count(), 0); |
236 | |
237 | radio.searchAllStations(); |
238 | QCOMPARE(radio.isSearching(), false); |
239 | QCOMPARE(spy.count(), 0); |
240 | |
241 | radio.cancelSearch(); |
242 | QCOMPARE(radio.isSearching(), false); |
243 | QCOMPARE(spy.count(), 0); |
244 | } |
245 | |
246 | radio.stop(); |
247 | } |
248 | |
249 | void tst_QRadioTuner::testBand() |
250 | { |
251 | QVERIFY(radio->isBandSupported(QRadioTuner::FM)); |
252 | QVERIFY(!radio->isBandSupported(QRadioTuner::SW)); |
253 | |
254 | if(radio->isBandSupported(b: QRadioTuner::AM)) { |
255 | QSignalSpy readSignal(radio, SIGNAL(bandChanged(QRadioTuner::Band))); |
256 | radio->setBand(QRadioTuner::AM); |
257 | QTestEventLoop::instance().enterLoop(secs: 1); |
258 | QVERIFY(radio->band() == QRadioTuner::AM); |
259 | QVERIFY(readSignal.count() == 1); |
260 | } |
261 | } |
262 | |
263 | void tst_QRadioTuner::testFrequency() |
264 | { |
265 | QSignalSpy readSignal(radio, SIGNAL(frequencyChanged(int))); |
266 | radio->setFrequency(104500000); |
267 | QTestEventLoop::instance().enterLoop(secs: 1); |
268 | QVERIFY(radio->frequency() == 104500000); |
269 | QVERIFY(readSignal.count() == 1); |
270 | |
271 | QVERIFY(radio->frequencyStep(QRadioTuner::FM) == 1); |
272 | QPair<int,int> test = radio->frequencyRange(band: QRadioTuner::FM); |
273 | QVERIFY(test.first == 1); |
274 | QVERIFY(test.second == 2); |
275 | } |
276 | |
277 | void tst_QRadioTuner::testMute() |
278 | { |
279 | QSignalSpy readSignal(radio, SIGNAL(mutedChanged(bool))); |
280 | radio->setMuted(true); |
281 | QTestEventLoop::instance().enterLoop(secs: 1); |
282 | QVERIFY(radio->isMuted()); |
283 | QVERIFY(readSignal.count() == 1); |
284 | } |
285 | |
286 | void tst_QRadioTuner::testSearch() |
287 | { |
288 | QSignalSpy readSignal(radio, SIGNAL(searchingChanged(bool))); |
289 | QVERIFY(!radio->isSearching()); |
290 | |
291 | radio->searchForward(); |
292 | QTestEventLoop::instance().enterLoop(secs: 1); |
293 | QVERIFY(radio->isSearching()); |
294 | QVERIFY(readSignal.count() == 1); |
295 | |
296 | radio->cancelSearch(); |
297 | QTestEventLoop::instance().enterLoop(secs: 1); |
298 | QVERIFY(!radio->isSearching()); |
299 | QVERIFY(readSignal.count() == 2); |
300 | |
301 | radio->searchBackward(); |
302 | QTestEventLoop::instance().enterLoop(secs: 1); |
303 | QVERIFY(radio->isSearching()); |
304 | QVERIFY(readSignal.count() == 3); |
305 | |
306 | radio->cancelSearch(); |
307 | QVERIFY(!radio->isSearching()); |
308 | } |
309 | |
310 | void tst_QRadioTuner::testVolume() |
311 | { |
312 | QVERIFY(radio->volume() == 100); |
313 | QSignalSpy readSignal(radio, SIGNAL(volumeChanged(int))); |
314 | radio->setVolume(50); |
315 | QTestEventLoop::instance().enterLoop(secs: 1); |
316 | QVERIFY(radio->volume() == 50); |
317 | QVERIFY(readSignal.count() == 1); |
318 | } |
319 | |
320 | void tst_QRadioTuner::testSignal() |
321 | { |
322 | QVERIFY(radio->signalStrength() == 0); |
323 | // There is no set of this only a get, do nothing else. |
324 | } |
325 | |
326 | void tst_QRadioTuner::testStereo() |
327 | { |
328 | /* no set function to toggle stereo status; |
329 | cannot emit stereoStatusChanged() signal */ |
330 | |
331 | QVERIFY(radio->isStereo()); |
332 | radio->setStereoMode(QRadioTuner::ForceMono); |
333 | QVERIFY(radio->stereoMode() == QRadioTuner::ForceMono); |
334 | } |
335 | |
336 | void tst_QRadioTuner::testSearchAllStations() |
337 | { |
338 | QSignalSpy foundSpy(radio, SIGNAL(stationFound(int,QString))); |
339 | QSignalSpy completeSpy(radio, SIGNAL(searchingChanged(bool))); |
340 | radio->searchAllStations(searchMode: QRadioTuner::SearchGetStationId); |
341 | QTestEventLoop::instance().enterLoop(secs: 1); |
342 | QCOMPARE(radio->frequency(), 103100000 ); |
343 | QCOMPARE(foundSpy.count(), 3); |
344 | QVERIFY(qvariant_cast<int>(foundSpy.at(2).at(0)) == 103100000 ); |
345 | QVERIFY(qvariant_cast<QString>(foundSpy.at(2).at(1)) == QString("MockProgramPI3" ) ); |
346 | QCOMPARE(completeSpy.count(), 2); |
347 | } |
348 | |
349 | // QRadioTuner's errorsignal |
350 | void tst_QRadioTuner::errorSignal() |
351 | { |
352 | qRegisterMetaType<QRadioTuner::Error>(typeName: "QRadioTuner::Error" ); |
353 | QObject obj; |
354 | MockRadioTunerControl dctrl(&obj); |
355 | MockMediaService service(&obj, &dctrl); |
356 | MockMediaServiceProvider provider(&service); |
357 | QMediaServiceProvider::setDefaultServiceProvider(&provider); |
358 | QRadioTuner radio; |
359 | QSignalSpy spy(&radio, SIGNAL(error(QRadioTuner::Error))); |
360 | QVERIFY(radio.service() != 0); |
361 | QVERIFY(radio.isAvailable()); |
362 | radio.start(); |
363 | radio.setBand(QRadioTuner::FM); |
364 | QVERIFY(spy.count() == 1); |
365 | QVERIFY(qvariant_cast<QRadioTuner::Error>(spy.at(0).at(0)) == QRadioTuner::NoError); |
366 | QVERIFY(radio.error() == QRadioTuner::NoError); |
367 | QVERIFY(radio.error() != QRadioTuner::OpenError); |
368 | QVERIFY(radio.errorString().isEmpty()); |
369 | spy.clear(); |
370 | |
371 | /* emits QRadioTuner::OutOfRangeError if band is set to FM2 or LW |
372 | and frequency set to >= 148500000 */ |
373 | |
374 | radio.setBand(QRadioTuner::LW); |
375 | radio.setBand(QRadioTuner::FM2); |
376 | radio.setFrequency(148500000); |
377 | QVERIFY(spy.count() == 3); |
378 | QVERIFY(qvariant_cast<QRadioTuner::Error>(spy.at(0).at(0)) == QRadioTuner::OutOfRangeError); |
379 | QVERIFY(qvariant_cast<QRadioTuner::Error>(spy.at(1).at(0)) == QRadioTuner::OutOfRangeError); |
380 | QVERIFY(qvariant_cast<QRadioTuner::Error>(spy.at(2).at(0)) == QRadioTuner::OutOfRangeError); |
381 | QVERIFY(radio.error() == QRadioTuner::OutOfRangeError); |
382 | QVERIFY2(!radio.errorString().isEmpty(), "band and range not supported" ); |
383 | spy.clear(); |
384 | radio.stop(); |
385 | } |
386 | |
387 | QTEST_GUILESS_MAIN(tst_QRadioTuner) |
388 | #include "tst_qradiotuner.moc" |
389 | |