tests/auto/qmediaserviceprovider/tst_qmediaserviceprovider.cpp
changeset 0 876b1a06bc25
child 5 603d3f8b6302
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 
       
    42 #include <QtTest/QtTest>
       
    43 #include <QDebug>
       
    44 #include <QStringList>
       
    45 
       
    46 #include <qmediaserviceprovider.h>
       
    47 #include <qmediaserviceproviderplugin.h>
       
    48 #include <qmediapluginloader_p.h>
       
    49 #include <qmediaobject.h>
       
    50 #include <qmediaservice.h>
       
    51 #include <qmediaplayer.h>
       
    52 #include <qaudiocapturesource.h>
       
    53 
       
    54 QT_USE_NAMESPACE
       
    55 class MockMediaService : public QMediaService
       
    56 {
       
    57     Q_OBJECT
       
    58 public:
       
    59     MockMediaService(const QString& name, QObject *parent = 0) : QMediaService(parent)
       
    60     { setObjectName(name); }
       
    61     ~MockMediaService() {}
       
    62 
       
    63     QMediaControl* requestControl(const char *) {return 0;}
       
    64     void releaseControl(QMediaControl *) {}
       
    65 };
       
    66 
       
    67 class MockServicePlugin1 : public QMediaServiceProviderPlugin,
       
    68                            public QMediaServiceSupportedFormatsInterface,
       
    69                            public QMediaServiceSupportedDevicesInterface
       
    70 {
       
    71     Q_OBJECT
       
    72     Q_INTERFACES(QMediaServiceSupportedFormatsInterface)
       
    73     Q_INTERFACES(QMediaServiceSupportedDevicesInterface)
       
    74 public:
       
    75     QStringList keys() const
       
    76     {
       
    77         return QStringList() <<
       
    78                 QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER);
       
    79     }
       
    80 
       
    81     QMediaService* create(QString const& key)
       
    82     {
       
    83         if (keys().contains(key))
       
    84             return new MockMediaService("MockServicePlugin1");
       
    85         else
       
    86             return 0;
       
    87     }
       
    88 
       
    89     void release(QMediaService *service)
       
    90     {
       
    91         delete service;
       
    92     }
       
    93 
       
    94     QtMultimediaKit::SupportEstimate hasSupport(const QString &mimeType, const QStringList& codecs) const
       
    95     {
       
    96         if (codecs.contains(QLatin1String("mpeg4")))
       
    97             return QtMultimediaKit::NotSupported;
       
    98 
       
    99         if (mimeType == "audio/ogg") {
       
   100             return QtMultimediaKit::ProbablySupported;
       
   101         }
       
   102 
       
   103         return QtMultimediaKit::MaybeSupported;
       
   104     }
       
   105 
       
   106     QStringList supportedMimeTypes() const
       
   107     {
       
   108         return QStringList("audio/ogg");
       
   109     }
       
   110 
       
   111     QList<QByteArray> devices(const QByteArray &service) const
       
   112     {
       
   113         QList<QByteArray> res;
       
   114         return res;
       
   115     }
       
   116 
       
   117     QString deviceDescription(const QByteArray &service, const QByteArray &device)
       
   118     {
       
   119         if (devices(service).contains(device))
       
   120             return QString(device)+" description";
       
   121         else
       
   122             return QString();
       
   123     }
       
   124 };
       
   125 
       
   126 class MockServicePlugin2 : public QMediaServiceProviderPlugin,
       
   127                             public QMediaServiceSupportedFormatsInterface,
       
   128                             public QMediaServiceFeaturesInterface
       
   129 {
       
   130     Q_OBJECT
       
   131     Q_INTERFACES(QMediaServiceSupportedFormatsInterface)
       
   132     Q_INTERFACES(QMediaServiceFeaturesInterface)
       
   133 public:
       
   134     QStringList keys() const
       
   135     {
       
   136         return QStringList() << QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER);
       
   137     }
       
   138 
       
   139     QMediaService* create(QString const& key)
       
   140     {
       
   141         if (keys().contains(key))
       
   142             return new MockMediaService("MockServicePlugin2");
       
   143         else
       
   144             return 0;
       
   145     }
       
   146 
       
   147     void release(QMediaService *service)
       
   148     {
       
   149         delete service;
       
   150     }
       
   151 
       
   152     QtMultimediaKit::SupportEstimate hasSupport(const QString &mimeType, const QStringList& codecs) const
       
   153     {
       
   154         Q_UNUSED(codecs);
       
   155 
       
   156         if (mimeType == "audio/wav")
       
   157             return QtMultimediaKit::PreferredService;
       
   158 
       
   159         return QtMultimediaKit::NotSupported;
       
   160     }
       
   161 
       
   162     QStringList supportedMimeTypes() const
       
   163     {
       
   164         return QStringList("audio/wav");
       
   165     }
       
   166 
       
   167     QMediaServiceProviderHint::Features supportedFeatures(const QByteArray &service) const
       
   168     {
       
   169         if (service == QByteArray(Q_MEDIASERVICE_MEDIAPLAYER))
       
   170             return QMediaServiceProviderHint::LowLatencyPlayback;
       
   171         else
       
   172             return 0;
       
   173     }
       
   174 };
       
   175 
       
   176 
       
   177 class MockServicePlugin3 : public QMediaServiceProviderPlugin,
       
   178                             public QMediaServiceSupportedDevicesInterface
       
   179 {
       
   180     Q_OBJECT
       
   181     Q_INTERFACES(QMediaServiceSupportedDevicesInterface)
       
   182 public:
       
   183     QStringList keys() const
       
   184     {
       
   185         return QStringList() <<
       
   186                QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER) <<
       
   187                QLatin1String(Q_MEDIASERVICE_AUDIOSOURCE);
       
   188     }
       
   189 
       
   190     QMediaService* create(QString const& key)
       
   191     {
       
   192         if (keys().contains(key))
       
   193             return new MockMediaService("MockServicePlugin3");
       
   194         else
       
   195             return 0;
       
   196     }
       
   197 
       
   198     void release(QMediaService *service)
       
   199     {
       
   200         delete service;
       
   201     }
       
   202 
       
   203     QList<QByteArray> devices(const QByteArray &service) const
       
   204     {
       
   205         QList<QByteArray> res;
       
   206         if (service == QByteArray(Q_MEDIASERVICE_AUDIOSOURCE))
       
   207             res << "audiosource1" << "audiosource2";
       
   208 
       
   209         return res;
       
   210     }
       
   211 
       
   212     QString deviceDescription(const QByteArray &service, const QByteArray &device)
       
   213     {
       
   214         if (devices(service).contains(device))
       
   215             return QString(device)+" description";
       
   216         else
       
   217             return QString();
       
   218     }
       
   219 };
       
   220 
       
   221 class MockServicePlugin4 : public QMediaServiceProviderPlugin,
       
   222                             public QMediaServiceSupportedFormatsInterface,
       
   223                             public QMediaServiceFeaturesInterface
       
   224 {
       
   225     Q_OBJECT
       
   226     Q_INTERFACES(QMediaServiceSupportedFormatsInterface)
       
   227     Q_INTERFACES(QMediaServiceFeaturesInterface)
       
   228 public:
       
   229     QStringList keys() const
       
   230     {
       
   231         return QStringList() << QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER);
       
   232     }
       
   233 
       
   234     QMediaService* create(QString const& key)
       
   235     {
       
   236         if (keys().contains(key))
       
   237             return new MockMediaService("MockServicePlugin4");
       
   238         else
       
   239             return 0;
       
   240     }
       
   241 
       
   242     void release(QMediaService *service)
       
   243     {
       
   244         delete service;
       
   245     }
       
   246 
       
   247     QtMultimediaKit::SupportEstimate hasSupport(const QString &mimeType, const QStringList& codecs) const
       
   248     {
       
   249         if (codecs.contains(QLatin1String("jpeg2000")))
       
   250             return QtMultimediaKit::NotSupported;
       
   251 
       
   252         if (supportedMimeTypes().contains(mimeType))
       
   253             return QtMultimediaKit::ProbablySupported;
       
   254 
       
   255         return QtMultimediaKit::MaybeSupported;
       
   256     }
       
   257 
       
   258     QStringList supportedMimeTypes() const
       
   259     {
       
   260         return QStringList() << "video/mp4" << "video/quicktime";
       
   261     }
       
   262 
       
   263     QMediaServiceProviderHint::Features supportedFeatures(const QByteArray &service) const
       
   264     {
       
   265         if (service == QByteArray(Q_MEDIASERVICE_MEDIAPLAYER))
       
   266             return QMediaServiceProviderHint::StreamPlayback;
       
   267         else
       
   268             return 0;
       
   269     }
       
   270 };
       
   271 
       
   272 
       
   273 
       
   274 class MockMediaServiceProvider : public QMediaServiceProvider
       
   275 {
       
   276     QMediaService* requestService(const QByteArray &type, const QMediaServiceProviderHint &)
       
   277     {
       
   278         Q_UNUSED(type);
       
   279         return 0;
       
   280     }
       
   281 
       
   282     void releaseService(QMediaService *service)
       
   283     {
       
   284         Q_UNUSED(service);
       
   285     }
       
   286 };
       
   287 
       
   288 
       
   289 class tst_QMediaServiceProvider : public QObject
       
   290 {
       
   291     Q_OBJECT
       
   292 
       
   293 public slots:
       
   294     void initTestCase();
       
   295 
       
   296 private slots:
       
   297     void testDefaultProviderAvailable();
       
   298     void testObtainService();
       
   299     void testHasSupport();
       
   300     void testSupportedMimeTypes();
       
   301     void testProviderHints();
       
   302 
       
   303 private:
       
   304     QObjectList plugins;
       
   305 };
       
   306 
       
   307 void tst_QMediaServiceProvider::initTestCase()
       
   308 {
       
   309     plugins << new MockServicePlugin1;
       
   310     plugins << new MockServicePlugin2;
       
   311     plugins << new MockServicePlugin3;
       
   312     plugins << new MockServicePlugin4;
       
   313 
       
   314     QMediaPluginLoader::setStaticPlugins(QLatin1String("/mediaservice"), plugins);
       
   315 }
       
   316 
       
   317 void tst_QMediaServiceProvider::testDefaultProviderAvailable()
       
   318 {
       
   319     // Must always be a default provider available    
       
   320     QVERIFY(QMediaServiceProvider::defaultServiceProvider() != 0);
       
   321 }
       
   322 
       
   323 void tst_QMediaServiceProvider::testObtainService()
       
   324 {
       
   325     QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
       
   326 
       
   327     if (provider == 0)
       
   328         QSKIP("No default provider", SkipSingle);
       
   329 
       
   330     QMediaService *service = 0;
       
   331 
       
   332     // Player
       
   333     service = provider->requestService(Q_MEDIASERVICE_MEDIAPLAYER);
       
   334     QVERIFY(service != 0);
       
   335     provider->releaseService(service);
       
   336 }
       
   337 
       
   338 void tst_QMediaServiceProvider::testHasSupport()
       
   339 {
       
   340     MockMediaServiceProvider mockProvider;
       
   341     QCOMPARE(mockProvider.hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "video/ogv", QStringList()),
       
   342              QtMultimediaKit::MaybeSupported);
       
   343 
       
   344     QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
       
   345 
       
   346     if (provider == 0)
       
   347         QSKIP("No default provider", SkipSingle);
       
   348 
       
   349     QCOMPARE(provider->hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "video/ogv", QStringList()),
       
   350              QtMultimediaKit::MaybeSupported);
       
   351 
       
   352     QCOMPARE(provider->hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "audio/ogg", QStringList()),
       
   353              QtMultimediaKit::ProbablySupported);
       
   354 
       
   355     //while the service returns PreferredService, provider should return ProbablySupported
       
   356     QCOMPARE(provider->hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "audio/wav", QStringList()),
       
   357              QtMultimediaKit::ProbablySupported);
       
   358 
       
   359     //even while all the plugins with "hasSupport" returned NotSupported,
       
   360     //MockServicePlugin3 has no "hasSupport" interface, so MaybeSupported
       
   361     QCOMPARE(provider->hasSupport(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER), "video/avi",
       
   362                                   QStringList() << "mpeg4"),
       
   363              QtMultimediaKit::MaybeSupported);
       
   364 
       
   365     QCOMPARE(provider->hasSupport(QByteArray("non existing service"), "video/ogv", QStringList()),
       
   366              QtMultimediaKit::NotSupported);
       
   367 
       
   368     QCOMPARE(QMediaPlayer::hasSupport("video/ogv"), QtMultimediaKit::MaybeSupported);
       
   369     QCOMPARE(QMediaPlayer::hasSupport("audio/ogg"), QtMultimediaKit::ProbablySupported);
       
   370     QCOMPARE(QMediaPlayer::hasSupport("audio/wav"), QtMultimediaKit::ProbablySupported);
       
   371 
       
   372     //test low latency flag support
       
   373     QCOMPARE(QMediaPlayer::hasSupport("audio/wav", QStringList(), QMediaPlayer::LowLatency),
       
   374              QtMultimediaKit::ProbablySupported);
       
   375     //plugin1 probably supports audio/ogg, it checked because it doesn't provide features iface
       
   376     QCOMPARE(QMediaPlayer::hasSupport("audio/ogg", QStringList(), QMediaPlayer::LowLatency),
       
   377              QtMultimediaKit::ProbablySupported);
       
   378     //Plugin4 is not checked here, sine it's known not support low latency
       
   379     QCOMPARE(QMediaPlayer::hasSupport("video/quicktime", QStringList(), QMediaPlayer::LowLatency),
       
   380              QtMultimediaKit::MaybeSupported);
       
   381 
       
   382     //test streaming flag support
       
   383     QCOMPARE(QMediaPlayer::hasSupport("video/quicktime", QStringList(), QMediaPlayer::StreamPlayback),
       
   384              QtMultimediaKit::ProbablySupported);
       
   385     //Plugin2 is not checked here, sine it's known not support streaming
       
   386     QCOMPARE(QMediaPlayer::hasSupport("audio/wav", QStringList(), QMediaPlayer::StreamPlayback),
       
   387              QtMultimediaKit::MaybeSupported);
       
   388 
       
   389     //ensure the correct media player plugin is choosen for mime type
       
   390     QMediaPlayer simplePlayer(0, QMediaPlayer::LowLatency);
       
   391     QCOMPARE(simplePlayer.service()->objectName(), QLatin1String("MockServicePlugin2"));
       
   392 
       
   393     QMediaPlayer mediaPlayer;
       
   394     QVERIFY(mediaPlayer.service()->objectName() != QLatin1String("MockServicePlugin2"));
       
   395 
       
   396     QMediaPlayer streamPlayer(0, QMediaPlayer::StreamPlayback);
       
   397     QCOMPARE(streamPlayer.service()->objectName(), QLatin1String("MockServicePlugin4"));
       
   398 }
       
   399 
       
   400 void tst_QMediaServiceProvider::testSupportedMimeTypes()
       
   401 {
       
   402     QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
       
   403 
       
   404     if (provider == 0)
       
   405         QSKIP("No default provider", SkipSingle);
       
   406 
       
   407     QVERIFY(provider->supportedMimeTypes(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER)).contains("audio/ogg"));
       
   408     QVERIFY(!provider->supportedMimeTypes(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER)).contains("audio/mp3"));
       
   409 }
       
   410 
       
   411 void tst_QMediaServiceProvider::testProviderHints()
       
   412 {
       
   413     {
       
   414         QMediaServiceProviderHint hint;
       
   415         QVERIFY(hint.isNull());
       
   416         QCOMPARE(hint.type(), QMediaServiceProviderHint::Null);
       
   417         QVERIFY(hint.device().isEmpty());
       
   418         QVERIFY(hint.mimeType().isEmpty());
       
   419         QVERIFY(hint.codecs().isEmpty());
       
   420         QCOMPARE(hint.features(), 0);
       
   421     }
       
   422 
       
   423     {
       
   424         QByteArray deviceName(QByteArray("testDevice"));
       
   425         QMediaServiceProviderHint hint(deviceName);
       
   426         QVERIFY(!hint.isNull());
       
   427         QCOMPARE(hint.type(), QMediaServiceProviderHint::Device);
       
   428         QCOMPARE(hint.device(), deviceName);
       
   429         QVERIFY(hint.mimeType().isEmpty());
       
   430         QVERIFY(hint.codecs().isEmpty());
       
   431         QCOMPARE(hint.features(), 0);
       
   432     }
       
   433 
       
   434     {
       
   435         QMediaServiceProviderHint hint(QMediaServiceProviderHint::LowLatencyPlayback);
       
   436         QVERIFY(!hint.isNull());
       
   437         QCOMPARE(hint.type(), QMediaServiceProviderHint::SupportedFeatures);
       
   438         QVERIFY(hint.device().isEmpty());
       
   439         QVERIFY(hint.mimeType().isEmpty());
       
   440         QVERIFY(hint.codecs().isEmpty());
       
   441         QCOMPARE(hint.features(), QMediaServiceProviderHint::LowLatencyPlayback);
       
   442     }
       
   443 
       
   444     {
       
   445         QString mimeType(QLatin1String("video/ogg"));
       
   446         QStringList codecs;
       
   447         codecs << "theora" << "vorbis";
       
   448 
       
   449         QMediaServiceProviderHint hint(mimeType,codecs);
       
   450         QVERIFY(!hint.isNull());
       
   451         QCOMPARE(hint.type(), QMediaServiceProviderHint::ContentType);
       
   452         QVERIFY(hint.device().isEmpty());
       
   453         QCOMPARE(hint.mimeType(), mimeType);
       
   454         QCOMPARE(hint.codecs(), codecs);
       
   455 
       
   456         QMediaServiceProviderHint hint2(hint);
       
   457 
       
   458         QVERIFY(!hint2.isNull());
       
   459         QCOMPARE(hint2.type(), QMediaServiceProviderHint::ContentType);
       
   460         QVERIFY(hint2.device().isEmpty());
       
   461         QCOMPARE(hint2.mimeType(), mimeType);
       
   462         QCOMPARE(hint2.codecs(), codecs);
       
   463 
       
   464         QMediaServiceProviderHint hint3;
       
   465         QVERIFY(hint3.isNull());
       
   466         hint3 = hint;
       
   467         QVERIFY(!hint3.isNull());
       
   468         QCOMPARE(hint3.type(), QMediaServiceProviderHint::ContentType);
       
   469         QVERIFY(hint3.device().isEmpty());
       
   470         QCOMPARE(hint3.mimeType(), mimeType);
       
   471         QCOMPARE(hint3.codecs(), codecs);
       
   472 
       
   473         QCOMPARE(hint, hint2);
       
   474         QCOMPARE(hint3, hint2);
       
   475 
       
   476         QMediaServiceProviderHint hint4(mimeType,codecs);
       
   477         QCOMPARE(hint, hint4);
       
   478 
       
   479         QMediaServiceProviderHint hint5(mimeType,QStringList());
       
   480         QVERIFY(hint != hint5);
       
   481     }
       
   482 }
       
   483 
       
   484 QTEST_MAIN(tst_QMediaServiceProvider)
       
   485 
       
   486 #include "tst_qmediaserviceprovider.moc"