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 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
33#include <qvideodeviceselectorcontrol.h>
34#include <qmediacontrol.h>
35#include <qmediaservice.h>
36
37#include <QtCore/qcoreapplication.h>
38
39QT_BEGIN_NAMESPACE
40
41class QtTestMediaService;
42
43class QtTestMediaControlA : public QMediaControl
44{
45 Q_OBJECT
46};
47
48#define QtTestMediaControlA_iid "com.nokia.QtTestMediaControlA"
49Q_MEDIA_DECLARE_CONTROL(QtTestMediaControlA, QtTestMediaControlA_iid)
50
51class QtTestMediaControlB : public QMediaControl
52{
53 Q_OBJECT
54};
55
56#define QtTestMediaControlB_iid "com.nokia.QtTestMediaControlB"
57Q_MEDIA_DECLARE_CONTROL(QtTestMediaControlB, QtTestMediaControlB_iid)
58
59
60class QtTestMediaControlC : public QMediaControl
61{
62 Q_OBJECT
63};
64
65#define QtTestMediaControlC_iid "com.nokia.QtTestMediaControlC"
66Q_MEDIA_DECLARE_CONTROL(QtTestMediaControlC, QtTestMediaControlA_iid) // Yes A.
67
68class QtTestMediaControlD : public QMediaControl
69{
70 Q_OBJECT
71};
72
73#define QtTestMediaControlD_iid "com.nokia.QtTestMediaControlD"
74Q_MEDIA_DECLARE_CONTROL(QtTestMediaControlD, QtTestMediaControlD_iid)
75
76//unimplemented service
77#define QtTestMediaControlE_iid "com.nokia.QtTestMediaControlF"
78class QtTestMediaControlE : public QMediaControl
79{
80 Q_OBJECT
81};
82
83/* implementation of child class by inheriting The QMediaService base class for media service implementations. */
84class QtTestMediaService : public QMediaService
85{
86 Q_OBJECT
87public:
88 int refA;
89 int refB;
90 int refC;
91 QtTestMediaControlA controlA;
92 QtTestMediaControlB controlB;
93 QtTestMediaControlC controlC;
94
95 //constructor
96 QtTestMediaService(): QMediaService(0), refA(0), refB(0), refC(0)
97 {
98 }
99
100 //requestControl() pure virtual function of QMediaService class.
101 QMediaControl *requestControl(const char *name)
102 {
103 if (strcmp(s1: name, QtTestMediaControlA_iid) == 0) {
104 refA += 1;
105
106 return &controlA;
107 } else if (strcmp(s1: name, QtTestMediaControlB_iid) == 0) {
108 refB += 1;
109
110 return &controlB;
111 } else if (strcmp(s1: name, QtTestMediaControlC_iid) == 0) {
112 refA += 1;
113
114 return &controlA;
115 } else {
116 return 0;
117 }
118 }
119
120 //releaseControl() pure virtual function of QMediaService class.
121 void releaseControl(QMediaControl *control)
122 {
123 if (control == &controlA)
124 refA -= 1;
125 else if (control == &controlB)
126 refB -= 1;
127 else if (control == &controlC)
128 refC -= 1;
129 }
130
131 //requestControl() function of QMediaService class.
132 using QMediaService::requestControl;
133};
134
135/* Test case implementation for QMediaService class which provides a common base class for media service implementations.*/
136class tst_QMediaService : public QObject
137{
138 Q_OBJECT
139private slots:
140 void tst_destructor();
141 void tst_releaseControl();
142 void tst_requestControl();
143 void tst_requestControlTemplate();
144
145 void initTestCase();
146
147 void control_iid();
148 void control();
149
150};
151
152/*MaemoAPI-1668 :destructor property test. */
153void tst_QMediaService::tst_destructor()
154{
155 QtTestMediaService *service = new QtTestMediaService;
156 delete service;
157}
158
159void tst_QMediaService::initTestCase()
160{
161}
162
163/*MaemoAPI-1669 :releaseControl() API property test. */
164void tst_QMediaService::tst_releaseControl()
165{
166 //test class instance creation
167 QtTestMediaService service;
168
169 //Get a pointer to the media control implementing interface and verify.
170 QMediaControl* controlA = service.requestControl(QtTestMediaControlA_iid);
171 QCOMPARE(controlA, &service.controlA);
172 service.releaseControl(control: controlA); //Controls must be returned to the service when no longer needed
173 QVERIFY(service.refA == 0);
174
175 //Get a pointer to the media control implementing interface and verify.
176 QMediaControl* controlB = service.requestControl(QtTestMediaControlB_iid);
177 QCOMPARE(controlB, &service.controlB);
178 service.releaseControl(control: controlB); //Controls must be returned to the service when no longer needed
179 QVERIFY(service.refB == 0);
180}
181
182/*MaemoAPI-1670 :requestControl() API property test. */
183void tst_QMediaService::tst_requestControl()
184{
185 //test class instance creation
186 QtTestMediaService service;
187
188 //Get a pointer to the media control implementing interface and verify.
189 QMediaControl* controlA = service.requestControl(QtTestMediaControlA_iid);
190 QCOMPARE(controlA, &service.controlA);
191 service.releaseControl(control: controlA); //Controls must be returned to the service when no longer needed
192
193 //Get a pointer to the media control implementing interface and verify.
194 QMediaControl* controlB = service.requestControl(QtTestMediaControlB_iid);
195 QCOMPARE(controlB, &service.controlB);
196 service.releaseControl(control: controlB); //Controls must be returned to the service when no longer needed
197
198 //If the service does not implement the control, a null pointer is returned instead.
199 QMediaControl* controlE = service.requestControl(QtTestMediaControlE_iid);
200 QVERIFY(!controlE); //should return null pointer
201 service.releaseControl(control: controlE); //Controls must be returned to the service when no longer needed
202
203 //If the service is unavailable a null pointer is returned instead.
204 QMediaControl* control = service.requestControl(name: "");
205 QVERIFY(!control); //should return null pointer
206 service.releaseControl(control); //Controls must be returned to the service when no longer needed
207}
208
209/*MaemoAPI-1671 :requestControl() API property test. */
210void tst_QMediaService::tst_requestControlTemplate()
211{
212 //test class instance creation
213 QtTestMediaService service;
214
215 //Get a pointer to the media control of type T implemented by a media service.
216 QtTestMediaControlA *controlA = service.requestControl<QtTestMediaControlA *>();
217 QCOMPARE(controlA, &service.controlA);
218 service.releaseControl(control: controlA);
219
220 //Get a pointer to the media control of type T implemented by a media service.
221 QtTestMediaControlB *controlB = service.requestControl<QtTestMediaControlB *>();
222 QCOMPARE(controlB, &service.controlB);
223 service.releaseControl(control: controlB);
224
225 QVERIFY(!service.requestControl<QtTestMediaControlC *>()); // Faulty implementation returns A.
226 QCOMPARE(service.refA, 0); // Verify the control was released.
227
228 QVERIFY(!service.requestControl<QtTestMediaControlD *>()); // No control of that type.
229}
230
231
232void tst_QMediaService::control_iid()
233{
234 const char *nullString = 0;
235
236 // Default implementation.
237 QCOMPARE(qmediacontrol_iid<QtTestMediaControlE *>(), nullString);
238
239 // Partial template.
240 QVERIFY(qstrcmp(qmediacontrol_iid<QtTestMediaControlA *>(), QtTestMediaControlA_iid) == 0);
241}
242
243void tst_QMediaService::control()
244{
245 QtTestMediaService service;
246
247 QtTestMediaControlA *controlA = service.requestControl<QtTestMediaControlA *>();
248 QCOMPARE(controlA, &service.controlA);
249 service.releaseControl(control: controlA);
250
251 QtTestMediaControlB *controlB = service.requestControl<QtTestMediaControlB *>();
252 QCOMPARE(controlB, &service.controlB);
253 service.releaseControl(control: controlB);
254
255 QVERIFY(!service.requestControl<QtTestMediaControlC *>()); // Faulty implementation returns A, but is wrong class
256 QCOMPARE(service.refA, 0); // Verify the control was released.
257
258 QVERIFY(!service.requestControl<QtTestMediaControlD *>()); // No control of that type.
259}
260
261QT_END_NAMESPACE
262
263QT_USE_NAMESPACE
264
265QTEST_MAIN(tst_QMediaService)
266
267#include "tst_qmediaservice.moc"
268

source code of qtmultimedia/tests/auto/unit/qmediaservice/tst_qmediaservice.cpp