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 <qradiodatacontrol.h>
40#include <qradiodata.h>
41#include <qradiotuner.h>
42
43#include "mockmediaserviceprovider.h"
44#include "mockmediaservice.h"
45#include "mockavailabilitycontrol.h"
46#include "mockradiotunercontrol.h"
47#include "mockradiodatacontrol.h"
48
49QT_USE_NAMESPACE
50
51class tst_QRadioData: public QObject
52{
53 Q_OBJECT
54
55public slots:
56 void initTestCase();
57 void cleanupTestCase();
58
59private slots:
60 void testNullService();
61 void testNullControl();
62 void testAlternativeFrequencies();
63 void testRadioDataUpdates();
64
65private:
66 MockAvailabilityControl *mockAvailability;
67 MockRadioTunerControl *mockTuner;
68 MockRadioDataControl *mockData;
69 MockMediaService *service;
70 MockMediaServiceProvider *provider;
71 QRadioTuner * radio;
72 QRadioData *radioData;
73};
74
75void tst_QRadioData::initTestCase()
76{
77 qRegisterMetaType<QRadioData::ProgramType>(typeName: "QRadioData::ProgramType");
78
79 mockAvailability = new MockAvailabilityControl(QMultimedia::Available);
80 mockTuner = new MockRadioTunerControl(this);
81 mockData = new MockRadioDataControl(this);
82
83 QMap<QString, QMediaControl *> map;
84 map.insert(QRadioTunerControl_iid, avalue: mockTuner);
85 map.insert(QRadioDataControl_iid, avalue: mockData);
86 map.insert(QMediaAvailabilityControl_iid, avalue: mockAvailability);
87
88 service = new MockMediaService(this, map);
89 provider = new MockMediaServiceProvider(service);
90 QMediaServiceProvider::setDefaultServiceProvider(provider);
91
92 radio = new QRadioTuner;
93 radioData = radio->radioData();
94
95 QVERIFY(radioData->availability() == QMultimedia::Available);
96}
97
98void tst_QRadioData::cleanupTestCase()
99{
100 QVERIFY(radioData->error() == QRadioData::NoError);
101 QVERIFY(radioData->errorString().isEmpty());
102
103 delete radio;
104 delete service;
105 delete provider;
106 delete mockAvailability;
107}
108
109void tst_QRadioData::testNullService()
110{
111 const QPair<int, int> nullRange(0, 0);
112
113 MockMediaServiceProvider nullProvider(0);
114 QMediaServiceProvider::setDefaultServiceProvider(&nullProvider);
115 QRadioTuner radio;
116 QRadioData *nullRadioData = radio.radioData();
117
118 QVERIFY(nullRadioData == 0);
119
120 QRadioData radioData(&radio);
121
122 QCOMPARE(radioData.error(), QRadioData::ResourceError);
123 QCOMPARE(radioData.errorString(), QString());
124 QCOMPARE(radioData.stationId(), QString());
125 QCOMPARE(radioData.programType(), QRadioData::Undefined);
126 QCOMPARE(radioData.programTypeName(), QString());
127 QCOMPARE(radioData.stationName(), QString());
128 QCOMPARE(radioData.radioText(), QString());
129 QCOMPARE(radioData.isAlternativeFrequenciesEnabled(), false);
130
131}
132
133void tst_QRadioData::testNullControl()
134{
135 const QPair<int, int> nullRange(0, 0);
136
137 MockMediaService service(0, 0);
138 MockMediaServiceProvider provider(&service);
139 QMediaServiceProvider::setDefaultServiceProvider(&provider);
140 QRadioTuner radio;
141 QRadioData *radioData = radio.radioData();
142 QCOMPARE(radioData->error(), QRadioData::ResourceError);
143 QCOMPARE(radioData->errorString(), QString());
144
145 QCOMPARE(radioData->stationId(), QString());
146 QCOMPARE(radioData->programType(), QRadioData::Undefined);
147 QCOMPARE(radioData->programTypeName(), QString());
148 QCOMPARE(radioData->stationName(), QString());
149 QCOMPARE(radioData->radioText(), QString());
150 QCOMPARE(radioData->isAlternativeFrequenciesEnabled(), false);
151 {
152 QSignalSpy spy(radioData, SIGNAL(alternativeFrequenciesEnabledChanged(bool)));
153
154 radioData->setAlternativeFrequenciesEnabled(true);
155 QCOMPARE(radioData->isAlternativeFrequenciesEnabled(), false);
156 QCOMPARE(spy.count(), 0);
157 }
158}
159
160void tst_QRadioData::testAlternativeFrequencies()
161{
162 QSignalSpy readSignal(radioData, SIGNAL(alternativeFrequenciesEnabledChanged(bool)));
163 radioData->setAlternativeFrequenciesEnabled(true);
164 QTestEventLoop::instance().enterLoop(secs: 1);
165 QVERIFY(radioData->isAlternativeFrequenciesEnabled() == true);
166 QVERIFY(readSignal.count() == 1);
167}
168
169void tst_QRadioData::testRadioDataUpdates()
170{
171 QSignalSpy rtSpy(radioData, SIGNAL(radioTextChanged(QString)));
172 QSignalSpy ptyPTYSpy(radioData, SIGNAL(programTypeChanged(QRadioData::ProgramType)));
173 QSignalSpy ptynSpy(radioData, SIGNAL(programTypeNameChanged(QString)));
174 QSignalSpy piSpy(radioData, SIGNAL(stationIdChanged(QString)));
175 QSignalSpy psSpy(radioData, SIGNAL(stationNameChanged(QString)));
176 mockData->forceRT(text: "Mock Radio Text");
177 mockData->forceProgramType(pty: static_cast<int>(QRadioData::Sport));
178 mockData->forcePTYN(ptyn: "Mock Programme Type Name");
179 mockData->forcePI(pi: "Mock Programme Identification");
180 mockData->forcePS(ps: "Mock Programme Service");
181 QTestEventLoop::instance().enterLoop(secs: 1);
182 QVERIFY(rtSpy.count() == 1);
183 QVERIFY(ptyPTYSpy.count() == 1);
184 QVERIFY(ptynSpy.count() == 1);
185 QVERIFY(piSpy.count() == 1);
186 QVERIFY(psSpy.count() == 1);
187 QCOMPARE(radioData->radioText(), QString("Mock Radio Text"));
188 QCOMPARE(radioData->programType(), QRadioData::Sport);
189 QCOMPARE(radioData->programTypeName(), QString("Mock Programme Type Name"));
190 QCOMPARE(radioData->stationId(), QString("Mock Programme Identification"));
191 QCOMPARE(radioData->stationName(), QString("Mock Programme Service"));
192}
193
194QTEST_GUILESS_MAIN(tst_QRadioData)
195#include "tst_qradiodata.moc"
196

source code of qtmultimedia/tests/auto/unit/qradiodata/tst_qradiodata.cpp