| 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 | #include <QDebug> |
| 33 | #include <QStringList> |
| 34 | |
| 35 | #include <private/qmediaserviceprovider_p.h> |
| 36 | #include <qmediaserviceproviderplugin.h> |
| 37 | #include <private/qmediapluginloader_p.h> |
| 38 | #include <qmediaobject.h> |
| 39 | #include <qmediaservice.h> |
| 40 | #include <qmediaplayer.h> |
| 41 | #include <qaudiorecorder.h> |
| 42 | #include <qcamera.h> |
| 43 | #include <qcamerainfo.h> |
| 44 | |
| 45 | QT_USE_NAMESPACE |
| 46 | |
| 47 | class MockMediaServiceProvider : public QMediaServiceProvider |
| 48 | { |
| 49 | QMediaService* requestService(const QByteArray &type, const QMediaServiceProviderHint &) |
| 50 | { |
| 51 | Q_UNUSED(type); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | void releaseService(QMediaService *service) |
| 56 | { |
| 57 | Q_UNUSED(service); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | class tst_QMediaServiceProvider : public QObject |
| 63 | { |
| 64 | Q_OBJECT |
| 65 | |
| 66 | public slots: |
| 67 | void initTestCase(); |
| 68 | |
| 69 | private slots: |
| 70 | void testDefaultProviderAvailable(); |
| 71 | void testObtainService(); |
| 72 | void testHasSupport(); |
| 73 | void testSupportedMimeTypes(); |
| 74 | void testProviderHints(); |
| 75 | void testDefaultDevice(); |
| 76 | void testAvailableDevices(); |
| 77 | void testCameraInfo(); |
| 78 | |
| 79 | private: |
| 80 | QObjectList plugins; |
| 81 | }; |
| 82 | |
| 83 | void tst_QMediaServiceProvider::initTestCase() |
| 84 | { |
| 85 | // QMediaPluginLoader::setStaticPlugins(QLatin1String("mediaservice"), plugins); |
| 86 | #if QT_CONFIG(library) |
| 87 | QCoreApplication::setLibraryPaths(QStringList() << QCoreApplication::applicationDirPath()); |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | void tst_QMediaServiceProvider::testDefaultProviderAvailable() |
| 92 | { |
| 93 | // Must always be a default provider available |
| 94 | QVERIFY(QMediaServiceProvider::defaultServiceProvider() != 0); |
| 95 | } |
| 96 | |
| 97 | void tst_QMediaServiceProvider::testObtainService() |
| 98 | { |
| 99 | QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider(); |
| 100 | |
| 101 | if (provider == 0) |
| 102 | QSKIP("No default provider" ); |
| 103 | |
| 104 | QMediaService *service = 0; |
| 105 | |
| 106 | // Player |
| 107 | service = provider->requestService(Q_MEDIASERVICE_MEDIAPLAYER); |
| 108 | QVERIFY(service != 0); |
| 109 | provider->releaseService(service); |
| 110 | } |
| 111 | |
| 112 | void tst_QMediaServiceProvider::testHasSupport() |
| 113 | { |
| 114 | MockMediaServiceProvider mockProvider; |
| 115 | QCOMPARE(mockProvider.hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "video/ogv" , QStringList()), |
| 116 | QMultimedia::MaybeSupported); |
| 117 | |
| 118 | QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider(); |
| 119 | |
| 120 | if (provider == 0) |
| 121 | QSKIP("No default provider" ); |
| 122 | |
| 123 | QCOMPARE(provider->hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "video/ogv" , QStringList()), |
| 124 | QMultimedia::MaybeSupported); |
| 125 | |
| 126 | QCOMPARE(provider->hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "audio/ogg" , QStringList()), |
| 127 | QMultimedia::ProbablySupported); |
| 128 | |
| 129 | //while the service returns PreferredService, provider should return ProbablySupported |
| 130 | QCOMPARE(provider->hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "audio/wav" , QStringList()), |
| 131 | QMultimedia::ProbablySupported); |
| 132 | |
| 133 | //even while all the plugins with "hasSupport" returned NotSupported, |
| 134 | //MockServicePlugin3 has no "hasSupport" interface, so MaybeSupported |
| 135 | QCOMPARE(provider->hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "video/avi" , |
| 136 | QStringList() << "mpeg4" ), |
| 137 | QMultimedia::MaybeSupported); |
| 138 | |
| 139 | QCOMPARE(provider->hasSupport(QByteArray("non existing service" ), "video/ogv" , QStringList()), |
| 140 | QMultimedia::NotSupported); |
| 141 | |
| 142 | QCOMPARE(QMediaPlayer::hasSupport("video/ogv" ), QMultimedia::MaybeSupported); |
| 143 | QCOMPARE(QMediaPlayer::hasSupport("audio/ogg" ), QMultimedia::ProbablySupported); |
| 144 | QCOMPARE(QMediaPlayer::hasSupport("audio/wav" ), QMultimedia::ProbablySupported); |
| 145 | |
| 146 | //test low latency flag support |
| 147 | QCOMPARE(QMediaPlayer::hasSupport("audio/wav" , QStringList(), QMediaPlayer::LowLatency), |
| 148 | QMultimedia::ProbablySupported); |
| 149 | //plugin1 probably supports audio/ogg, it checked because it doesn't provide features iface |
| 150 | QCOMPARE(QMediaPlayer::hasSupport("audio/ogg" , QStringList(), QMediaPlayer::LowLatency), |
| 151 | QMultimedia::ProbablySupported); |
| 152 | //Plugin4 is not checked here, sine it's known not support low latency |
| 153 | QCOMPARE(QMediaPlayer::hasSupport("video/quicktime" , QStringList(), QMediaPlayer::LowLatency), |
| 154 | QMultimedia::MaybeSupported); |
| 155 | |
| 156 | //test streaming flag support |
| 157 | QCOMPARE(QMediaPlayer::hasSupport("video/quicktime" , QStringList(), QMediaPlayer::StreamPlayback), |
| 158 | QMultimedia::ProbablySupported); |
| 159 | //Plugin2 is not checked here, sine it's known not support streaming |
| 160 | QCOMPARE(QMediaPlayer::hasSupport("audio/wav" , QStringList(), QMediaPlayer::StreamPlayback), |
| 161 | QMultimedia::MaybeSupported); |
| 162 | |
| 163 | //ensure the correct media player plugin is chosen for mime type |
| 164 | QMediaPlayer simplePlayer(0, QMediaPlayer::LowLatency); |
| 165 | QCOMPARE(simplePlayer.service()->objectName(), QLatin1String("MockServicePlugin2" )); |
| 166 | |
| 167 | QMediaPlayer mediaPlayer; |
| 168 | QVERIFY(mediaPlayer.service()->objectName() != QLatin1String("MockServicePlugin2" )); |
| 169 | |
| 170 | QMediaPlayer streamPlayer(0, QMediaPlayer::StreamPlayback); |
| 171 | QCOMPARE(streamPlayer.service()->objectName(), QLatin1String("MockServicePlugin4" )); |
| 172 | } |
| 173 | |
| 174 | void tst_QMediaServiceProvider::testSupportedMimeTypes() |
| 175 | { |
| 176 | QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider(); |
| 177 | |
| 178 | if (provider == 0) |
| 179 | QSKIP("No default provider" ); |
| 180 | |
| 181 | QVERIFY(provider->supportedMimeTypes(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER)).contains("audio/ogg" )); |
| 182 | QVERIFY(!provider->supportedMimeTypes(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER)).contains("audio/mp3" )); |
| 183 | } |
| 184 | |
| 185 | void tst_QMediaServiceProvider::testProviderHints() |
| 186 | { |
| 187 | { |
| 188 | QMediaServiceProviderHint hint; |
| 189 | QVERIFY(hint.isNull()); |
| 190 | QCOMPARE(hint.type(), QMediaServiceProviderHint::Null); |
| 191 | QVERIFY(hint.device().isEmpty()); |
| 192 | QCOMPARE(hint.cameraPosition(), QCamera::UnspecifiedPosition); |
| 193 | QVERIFY(hint.mimeType().isEmpty()); |
| 194 | QVERIFY(hint.codecs().isEmpty()); |
| 195 | QCOMPARE(hint.features(), 0); |
| 196 | } |
| 197 | |
| 198 | { |
| 199 | QByteArray deviceName(QByteArray("testDevice" )); |
| 200 | QMediaServiceProviderHint hint(deviceName); |
| 201 | QVERIFY(!hint.isNull()); |
| 202 | QCOMPARE(hint.type(), QMediaServiceProviderHint::Device); |
| 203 | QCOMPARE(hint.device(), deviceName); |
| 204 | QCOMPARE(hint.cameraPosition(), QCamera::UnspecifiedPosition); |
| 205 | QVERIFY(hint.mimeType().isEmpty()); |
| 206 | QVERIFY(hint.codecs().isEmpty()); |
| 207 | QCOMPARE(hint.features(), 0); |
| 208 | } |
| 209 | |
| 210 | { |
| 211 | QMediaServiceProviderHint hint(QCamera::FrontFace); |
| 212 | QVERIFY(!hint.isNull()); |
| 213 | QCOMPARE(hint.type(), QMediaServiceProviderHint::CameraPosition); |
| 214 | QVERIFY(hint.device().isEmpty()); |
| 215 | QCOMPARE(hint.cameraPosition(), QCamera::FrontFace); |
| 216 | QVERIFY(hint.mimeType().isEmpty()); |
| 217 | QVERIFY(hint.codecs().isEmpty()); |
| 218 | QCOMPARE(hint.features(), 0); |
| 219 | } |
| 220 | |
| 221 | { |
| 222 | QMediaServiceProviderHint hint(QMediaServiceProviderHint::LowLatencyPlayback); |
| 223 | QVERIFY(!hint.isNull()); |
| 224 | QCOMPARE(hint.type(), QMediaServiceProviderHint::SupportedFeatures); |
| 225 | QVERIFY(hint.device().isEmpty()); |
| 226 | QCOMPARE(hint.cameraPosition(), QCamera::UnspecifiedPosition); |
| 227 | QVERIFY(hint.mimeType().isEmpty()); |
| 228 | QVERIFY(hint.codecs().isEmpty()); |
| 229 | QCOMPARE(hint.features(), QMediaServiceProviderHint::LowLatencyPlayback); |
| 230 | } |
| 231 | |
| 232 | { |
| 233 | QMediaServiceProviderHint hint(QMediaServiceProviderHint::RecordingSupport); |
| 234 | QVERIFY(!hint.isNull()); |
| 235 | QCOMPARE(hint.type(), QMediaServiceProviderHint::SupportedFeatures); |
| 236 | QVERIFY(hint.device().isEmpty()); |
| 237 | QCOMPARE(hint.cameraPosition(), QCamera::UnspecifiedPosition); |
| 238 | QVERIFY(hint.mimeType().isEmpty()); |
| 239 | QVERIFY(hint.codecs().isEmpty()); |
| 240 | QCOMPARE(hint.features(), QMediaServiceProviderHint::RecordingSupport); |
| 241 | } |
| 242 | |
| 243 | { |
| 244 | QString mimeType(QLatin1String("video/ogg" )); |
| 245 | QStringList codecs; |
| 246 | codecs << "theora" << "vorbis" ; |
| 247 | |
| 248 | QMediaServiceProviderHint hint(mimeType,codecs); |
| 249 | QVERIFY(!hint.isNull()); |
| 250 | QCOMPARE(hint.type(), QMediaServiceProviderHint::ContentType); |
| 251 | QVERIFY(hint.device().isEmpty()); |
| 252 | QCOMPARE(hint.cameraPosition(), QCamera::UnspecifiedPosition); |
| 253 | QCOMPARE(hint.mimeType(), mimeType); |
| 254 | QCOMPARE(hint.codecs(), codecs); |
| 255 | |
| 256 | QMediaServiceProviderHint hint2(hint); |
| 257 | |
| 258 | QVERIFY(!hint2.isNull()); |
| 259 | QCOMPARE(hint2.type(), QMediaServiceProviderHint::ContentType); |
| 260 | QVERIFY(hint2.device().isEmpty()); |
| 261 | QCOMPARE(hint.cameraPosition(), QCamera::UnspecifiedPosition); |
| 262 | QCOMPARE(hint2.mimeType(), mimeType); |
| 263 | QCOMPARE(hint2.codecs(), codecs); |
| 264 | |
| 265 | QMediaServiceProviderHint hint3; |
| 266 | QVERIFY(hint3.isNull()); |
| 267 | hint3 = hint; |
| 268 | QVERIFY(!hint3.isNull()); |
| 269 | QCOMPARE(hint3.type(), QMediaServiceProviderHint::ContentType); |
| 270 | QVERIFY(hint3.device().isEmpty()); |
| 271 | QCOMPARE(hint.cameraPosition(), QCamera::UnspecifiedPosition); |
| 272 | QCOMPARE(hint3.mimeType(), mimeType); |
| 273 | QCOMPARE(hint3.codecs(), codecs); |
| 274 | |
| 275 | QCOMPARE(hint, hint2); |
| 276 | QCOMPARE(hint3, hint2); |
| 277 | |
| 278 | QMediaServiceProviderHint hint4(mimeType,codecs); |
| 279 | QCOMPARE(hint, hint4); |
| 280 | |
| 281 | QMediaServiceProviderHint hint5(mimeType,QStringList()); |
| 282 | QVERIFY(hint != hint5); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void tst_QMediaServiceProvider::testDefaultDevice() |
| 287 | { |
| 288 | QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider(); |
| 289 | |
| 290 | if (provider == 0) |
| 291 | QSKIP("No default provider" ); |
| 292 | |
| 293 | QCOMPARE(provider->defaultDevice(Q_MEDIASERVICE_AUDIOSOURCE), QByteArray("audiosource1" )); |
| 294 | QCOMPARE(provider->defaultDevice(Q_MEDIASERVICE_CAMERA), QByteArray("frontcamera" )); |
| 295 | } |
| 296 | |
| 297 | void tst_QMediaServiceProvider::testAvailableDevices() |
| 298 | { |
| 299 | QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider(); |
| 300 | |
| 301 | if (provider == 0) |
| 302 | QSKIP("No default provider" ); |
| 303 | |
| 304 | QList<QByteArray> devices = provider->devices(Q_MEDIASERVICE_AUDIOSOURCE); |
| 305 | QCOMPARE(devices.count(), 2); |
| 306 | QCOMPARE(devices.at(0), QByteArray("audiosource1" )); |
| 307 | QCOMPARE(devices.at(1), QByteArray("audiosource2" )); |
| 308 | |
| 309 | devices = provider->devices(Q_MEDIASERVICE_CAMERA); |
| 310 | QCOMPARE(devices.count(), 3); |
| 311 | QCOMPARE(devices.at(0), QByteArray("frontcamera" )); |
| 312 | QCOMPARE(devices.at(1), QByteArray("backcamera" )); |
| 313 | QCOMPARE(devices.at(2), QByteArray("somecamera" )); |
| 314 | } |
| 315 | |
| 316 | void tst_QMediaServiceProvider::testCameraInfo() |
| 317 | { |
| 318 | QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider(); |
| 319 | |
| 320 | if (provider == 0) |
| 321 | QSKIP("No default provider" ); |
| 322 | |
| 323 | QCOMPARE(provider->cameraPosition("backcamera" ), QCamera::BackFace); |
| 324 | QCOMPARE(provider->cameraOrientation("backcamera" ), 90); |
| 325 | QCOMPARE(provider->cameraPosition("frontcamera" ), QCamera::FrontFace); |
| 326 | QCOMPARE(provider->cameraOrientation("frontcamera" ), 270); |
| 327 | QCOMPARE(provider->cameraPosition("somecamera" ), QCamera::UnspecifiedPosition); |
| 328 | QCOMPARE(provider->cameraOrientation("somecamera" ), 0); |
| 329 | |
| 330 | { |
| 331 | QCamera camera; |
| 332 | QVERIFY(camera.service()); |
| 333 | QCOMPARE(camera.service()->objectName(), QLatin1String("MockServicePlugin3" )); |
| 334 | } |
| 335 | |
| 336 | { |
| 337 | QCamera camera(QCameraInfo::defaultCamera()); |
| 338 | QVERIFY(camera.service()); |
| 339 | QCOMPARE(camera.service()->objectName(), QLatin1String("MockServicePlugin3" )); |
| 340 | } |
| 341 | |
| 342 | { |
| 343 | QCamera camera(QCameraInfo::availableCameras().at(i: 0)); |
| 344 | QVERIFY(camera.service()); |
| 345 | QCOMPARE(camera.service()->objectName(), QLatin1String("MockServicePlugin3" )); |
| 346 | } |
| 347 | |
| 348 | { |
| 349 | QCamera camera(QCameraInfo::availableCameras().at(i: 1)); |
| 350 | QVERIFY(camera.service()); |
| 351 | QCOMPARE(camera.service()->objectName(), QLatin1String("MockServicePlugin5" )); |
| 352 | } |
| 353 | |
| 354 | { |
| 355 | QCamera camera(QCameraInfo::availableCameras().at(i: 2)); |
| 356 | QVERIFY(camera.service()); |
| 357 | QCOMPARE(camera.service()->objectName(), QLatin1String("MockServicePlugin5" )); |
| 358 | } |
| 359 | |
| 360 | { |
| 361 | QCamera camera(QCamera::FrontFace); |
| 362 | QVERIFY(camera.service()); |
| 363 | QCOMPARE(camera.service()->objectName(), QLatin1String("MockServicePlugin3" )); |
| 364 | } |
| 365 | |
| 366 | { |
| 367 | QCamera camera(QCamera::BackFace); |
| 368 | QVERIFY(camera.service()); |
| 369 | QCOMPARE(camera.service()->objectName(), QLatin1String("MockServicePlugin5" )); |
| 370 | } |
| 371 | |
| 372 | { |
| 373 | QCamera camera(QCamera::UnspecifiedPosition); |
| 374 | QVERIFY(camera.service()); |
| 375 | QCOMPARE(camera.service()->objectName(), QLatin1String("MockServicePlugin3" )); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | QTEST_MAIN(tst_QMediaServiceProvider) |
| 380 | |
| 381 | #include "tst_qmediaserviceprovider.moc" |
| 382 | |