qtmobility/tests/auto/qmediaserviceprovider/tst_qmediaserviceprovider.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
    47 #include <qmediaserviceproviderplugin.h>
    47 #include <qmediaserviceproviderplugin.h>
    48 #include <qmediapluginloader_p.h>
    48 #include <qmediapluginloader_p.h>
    49 #include <qmediaobject.h>
    49 #include <qmediaobject.h>
    50 #include <qmediaservice.h>
    50 #include <qmediaservice.h>
    51 #include <qmediaplayer.h>
    51 #include <qmediaplayer.h>
    52 #include <experimental/qcamera.h>
       
    53 #include <qaudiocapturesource.h>
    52 #include <qaudiocapturesource.h>
    54 
    53 
    55 QTM_USE_NAMESPACE
    54 QTM_USE_NAMESPACE
    56 class MockMediaService : public QMediaService
    55 class MockMediaService : public QMediaService
    57 {
    56 {
    73     Q_INTERFACES(QtMobility::QMediaServiceSupportedDevicesInterface)
    72     Q_INTERFACES(QtMobility::QMediaServiceSupportedDevicesInterface)
    74 public:
    73 public:
    75     QStringList keys() const
    74     QStringList keys() const
    76     {
    75     {
    77         return QStringList() <<
    76         return QStringList() <<
    78                 QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER) <<
    77                 QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER);
    79                 QLatin1String(Q_MEDIASERVICE_CAMERA);
       
    80     }
    78     }
    81 
    79 
    82     QMediaService* create(QString const& key)
    80     QMediaService* create(QString const& key)
    83     {
    81     {
    84         if (keys().contains(key))
    82         if (keys().contains(key))
   110     }
   108     }
   111 
   109 
   112     QList<QByteArray> devices(const QByteArray &service) const
   110     QList<QByteArray> devices(const QByteArray &service) const
   113     {
   111     {
   114         QList<QByteArray> res;
   112         QList<QByteArray> res;
   115         if (service == QByteArray(Q_MEDIASERVICE_CAMERA))
       
   116             res << "camera1" << "camera2";        
       
   117         return res;
   113         return res;
   118     }
   114     }
   119 
   115 
   120     QString deviceDescription(const QByteArray &service, const QByteArray &device)
   116     QString deviceDescription(const QByteArray &service, const QByteArray &device)
   121     {
   117     {
   155     QtMedia::SupportEstimate hasSupport(const QString &mimeType, const QStringList& codecs) const
   151     QtMedia::SupportEstimate hasSupport(const QString &mimeType, const QStringList& codecs) const
   156     {
   152     {
   157         Q_UNUSED(codecs);
   153         Q_UNUSED(codecs);
   158 
   154 
   159         if (mimeType == "audio/wav")
   155         if (mimeType == "audio/wav")
   160             return QtMedia::PreferedService;        
   156             return QtMedia::PreferredService;
   161 
   157 
   162         return QtMedia::NotSupported;
   158         return QtMedia::NotSupported;
   163     }
   159     }
   164 
   160 
   165     QStringList supportedMimeTypes() const
   161     QStringList supportedMimeTypes() const
   185 public:
   181 public:
   186     QStringList keys() const
   182     QStringList keys() const
   187     {
   183     {
   188         return QStringList() <<
   184         return QStringList() <<
   189                QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER) <<
   185                QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER) <<
   190                QLatin1String(Q_MEDIASERVICE_CAMERA) <<
       
   191                QLatin1String(Q_MEDIASERVICE_AUDIOSOURCE);
   186                QLatin1String(Q_MEDIASERVICE_AUDIOSOURCE);
   192     }
   187     }
   193 
   188 
   194     QMediaService* create(QString const& key)
   189     QMediaService* create(QString const& key)
   195     {
   190     {
   205     }
   200     }
   206 
   201 
   207     QList<QByteArray> devices(const QByteArray &service) const
   202     QList<QByteArray> devices(const QByteArray &service) const
   208     {
   203     {
   209         QList<QByteArray> res;
   204         QList<QByteArray> res;
   210         if (service == QByteArray(Q_MEDIASERVICE_CAMERA))
   205         if (service == QByteArray(Q_MEDIASERVICE_AUDIOSOURCE))
   211             res << "camera3" << "camera4";
       
   212         else if (service == QByteArray(Q_MEDIASERVICE_AUDIOSOURCE))
       
   213             res << "audiosource1" << "audiosource2";
   206             res << "audiosource1" << "audiosource2";
   214 
   207 
   215         return res;
   208         return res;
   216     }
   209     }
   217 
   210 
   222         else
   215         else
   223             return QString();
   216             return QString();
   224     }
   217     }
   225 };
   218 };
   226 
   219 
       
   220 class MockServicePlugin4 : public QMediaServiceProviderPlugin,
       
   221                             public QMediaServiceSupportedFormatsInterface,
       
   222                             public QMediaServiceFeaturesInterface
       
   223 {
       
   224     Q_OBJECT
       
   225     Q_INTERFACES(QtMobility::QMediaServiceSupportedFormatsInterface)
       
   226     Q_INTERFACES(QtMobility::QMediaServiceFeaturesInterface)
       
   227 public:
       
   228     QStringList keys() const
       
   229     {
       
   230         return QStringList() << QLatin1String(Q_MEDIASERVICE_MEDIAPLAYER);
       
   231     }
       
   232 
       
   233     QMediaService* create(QString const& key)
       
   234     {
       
   235         if (keys().contains(key))
       
   236             return new MockMediaService("MockServicePlugin4");
       
   237         else
       
   238             return 0;
       
   239     }
       
   240 
       
   241     void release(QMediaService *service)
       
   242     {
       
   243         delete service;
       
   244     }
       
   245 
       
   246     QtMedia::SupportEstimate hasSupport(const QString &mimeType, const QStringList& codecs) const
       
   247     {
       
   248         if (codecs.contains(QLatin1String("jpeg2000")))
       
   249             return QtMedia::NotSupported;
       
   250 
       
   251         if (supportedMimeTypes().contains(mimeType))
       
   252             return QtMedia::ProbablySupported;
       
   253 
       
   254         return QtMedia::MaybeSupported;
       
   255     }
       
   256 
       
   257     QStringList supportedMimeTypes() const
       
   258     {
       
   259         return QStringList() << "video/mp4" << "video/quicktime";
       
   260     }
       
   261 
       
   262     QMediaServiceProviderHint::Features supportedFeatures(const QByteArray &service) const
       
   263     {
       
   264         if (service == QByteArray(Q_MEDIASERVICE_MEDIAPLAYER))
       
   265             return QMediaServiceProviderHint::StreamPlayback;
       
   266         else
       
   267             return 0;
       
   268     }
       
   269 };
       
   270 
   227 
   271 
   228 
   272 
   229 class MockMediaServiceProvider : public QMediaServiceProvider
   273 class MockMediaServiceProvider : public QMediaServiceProvider
   230 {
   274 {
   231     QMediaService* requestService(const QByteArray &type, const QMediaServiceProviderHint &)
   275     QMediaService* requestService(const QByteArray &type, const QMediaServiceProviderHint &)
   246     Q_OBJECT
   290     Q_OBJECT
   247 
   291 
   248 public slots:
   292 public slots:
   249     void initTestCase();
   293     void initTestCase();
   250 
   294 
   251 private slots:    
   295 private slots:
   252     void testDefaultProviderAvailable();
   296     void testDefaultProviderAvailable();
   253     void testObtainService();
   297     void testObtainService();
   254     void testHasSupport();
   298     void testHasSupport();
   255     void testSupportedMimeTypes();
   299     void testSupportedMimeTypes();
   256     void testDevices();
       
   257     void testProviderHints();
   300     void testProviderHints();
   258 
   301 
   259 private:
   302 private:
   260     QObjectList plugins;
   303     QObjectList plugins;
   261 };
   304 };
   263 void tst_QMediaServiceProvider::initTestCase()
   306 void tst_QMediaServiceProvider::initTestCase()
   264 {
   307 {
   265     plugins << new MockServicePlugin1;
   308     plugins << new MockServicePlugin1;
   266     plugins << new MockServicePlugin2;
   309     plugins << new MockServicePlugin2;
   267     plugins << new MockServicePlugin3;
   310     plugins << new MockServicePlugin3;
       
   311     plugins << new MockServicePlugin4;
   268 
   312 
   269     QMediaPluginLoader::setStaticPlugins(QLatin1String("/mediaservice"), plugins);
   313     QMediaPluginLoader::setStaticPlugins(QLatin1String("/mediaservice"), plugins);
   270 }
   314 }
   271 
   315 
   272 void tst_QMediaServiceProvider::testDefaultProviderAvailable()
   316 void tst_QMediaServiceProvider::testDefaultProviderAvailable()
   282     if (provider == 0)
   326     if (provider == 0)
   283         QSKIP("No default provider", SkipSingle);
   327         QSKIP("No default provider", SkipSingle);
   284 
   328 
   285     QMediaService *service = 0;
   329     QMediaService *service = 0;
   286 
   330 
   287     QTest::ignoreMessage(QtWarningMsg, "Load static plugins for \"/mediaservice/\" ");
       
   288     // Player
   331     // Player
   289     service = provider->requestService(Q_MEDIASERVICE_MEDIAPLAYER);
   332     service = provider->requestService(Q_MEDIASERVICE_MEDIAPLAYER);
   290     QVERIFY(service != 0);
   333     QVERIFY(service != 0);
   291     provider->releaseService(service);
   334     provider->releaseService(service);
   292 }
   335 }
   323 
   366 
   324     QCOMPARE(QMediaPlayer::hasSupport("video/ogv"), QtMedia::MaybeSupported);    
   367     QCOMPARE(QMediaPlayer::hasSupport("video/ogv"), QtMedia::MaybeSupported);    
   325     QCOMPARE(QMediaPlayer::hasSupport("audio/ogg"), QtMedia::ProbablySupported);
   368     QCOMPARE(QMediaPlayer::hasSupport("audio/ogg"), QtMedia::ProbablySupported);
   326     QCOMPARE(QMediaPlayer::hasSupport("audio/wav"), QtMedia::ProbablySupported);
   369     QCOMPARE(QMediaPlayer::hasSupport("audio/wav"), QtMedia::ProbablySupported);
   327 
   370 
       
   371     //test low latency flag support
       
   372     QCOMPARE(QMediaPlayer::hasSupport("audio/wav", QStringList(), QMediaPlayer::LowLatency),
       
   373              QtMedia::ProbablySupported);
       
   374     //plugin1 probably supports audio/ogg, it checked because it doesn't provide features iface
       
   375     QCOMPARE(QMediaPlayer::hasSupport("audio/ogg", QStringList(), QMediaPlayer::LowLatency),
       
   376              QtMedia::ProbablySupported);
       
   377     //Plugin4 is not checked here, sine it's known not support low latency
       
   378     QCOMPARE(QMediaPlayer::hasSupport("video/quicktime", QStringList(), QMediaPlayer::LowLatency),
       
   379              QtMedia::MaybeSupported);
       
   380 
       
   381     //test streaming flag support
       
   382     QCOMPARE(QMediaPlayer::hasSupport("video/quicktime", QStringList(), QMediaPlayer::StreamPlayback),
       
   383              QtMedia::ProbablySupported);
       
   384     //Plugin2 is not checked here, sine it's known not support streaming
       
   385     QCOMPARE(QMediaPlayer::hasSupport("audio/wav", QStringList(), QMediaPlayer::StreamPlayback),
       
   386              QtMedia::MaybeSupported);
       
   387 
   328     //ensure the correct media player plugin is choosen for mime type
   388     //ensure the correct media player plugin is choosen for mime type
   329     QMediaPlayer simplePlayer(0, QMediaPlayer::LowLatency);
   389     QMediaPlayer simplePlayer(0, QMediaPlayer::LowLatency);
   330     QCOMPARE(simplePlayer.service()->objectName(), QLatin1String("MockServicePlugin2"));
   390     QCOMPARE(simplePlayer.service()->objectName(), QLatin1String("MockServicePlugin2"));
   331 
   391 
   332     QMediaPlayer mediaPlayer;
   392     QMediaPlayer mediaPlayer;
   333     QVERIFY(mediaPlayer.service()->objectName() != QLatin1String("MockServicePlugin2"));
   393     QVERIFY(mediaPlayer.service()->objectName() != QLatin1String("MockServicePlugin2"));
       
   394 
       
   395     QMediaPlayer streamPlayer(0, QMediaPlayer::StreamPlayback);
       
   396     QCOMPARE(streamPlayer.service()->objectName(), QLatin1String("MockServicePlugin4"));
   334 }
   397 }
   335 
   398 
   336 void tst_QMediaServiceProvider::testSupportedMimeTypes()
   399 void tst_QMediaServiceProvider::testSupportedMimeTypes()
   337 {
   400 {
   338     QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
   401     QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
   341         QSKIP("No default provider", SkipSingle);
   404         QSKIP("No default provider", SkipSingle);
   342 
   405 
   343     QVERIFY(provider->supportedMimeTypes(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER)).contains("audio/ogg"));
   406     QVERIFY(provider->supportedMimeTypes(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER)).contains("audio/ogg"));
   344     QVERIFY(!provider->supportedMimeTypes(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER)).contains("audio/mp3"));
   407     QVERIFY(!provider->supportedMimeTypes(QByteArray(Q_MEDIASERVICE_MEDIAPLAYER)).contains("audio/mp3"));
   345 }
   408 }
   346 
       
   347 void tst_QMediaServiceProvider::testDevices()
       
   348 {
       
   349     MockMediaServiceProvider mockProvider;
       
   350     QVERIFY(mockProvider.devices(QByteArray(Q_MEDIASERVICE_CAMERA)).isEmpty());
       
   351     QVERIFY(mockProvider.deviceDescription(QByteArray(Q_MEDIASERVICE_CAMERA),
       
   352                                            QByteArray()).isEmpty());
       
   353 
       
   354     QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
       
   355 
       
   356     QList<QByteArray> cameraDevices = provider->devices(QByteArray(Q_MEDIASERVICE_CAMERA));
       
   357     QCOMPARE(cameraDevices.count(), 4);
       
   358     QVERIFY(cameraDevices.contains(QByteArray("camera1")));
       
   359     QVERIFY(cameraDevices.contains(QByteArray("camera2")));
       
   360     QVERIFY(cameraDevices.contains(QByteArray("camera3")));
       
   361     QVERIFY(cameraDevices.contains(QByteArray("camera4")));
       
   362 
       
   363     //ensure the right plugin is choosen for a device
       
   364     QCamera camera1(QByteArray("camera1"));
       
   365     QCOMPARE( camera1.service()->objectName(), QLatin1String("MockServicePlugin1") );
       
   366     QCamera camera2(QByteArray("camera2"));
       
   367     QCOMPARE( camera2.service()->objectName(), QLatin1String("MockServicePlugin1") );
       
   368     QCamera camera3(QByteArray("camera3"));
       
   369     QCOMPARE( camera3.service()->objectName(), QLatin1String("MockServicePlugin3") );
       
   370     QCamera camera4(QByteArray("camera4"));
       
   371     QCOMPARE( camera4.service()->objectName(), QLatin1String("MockServicePlugin3") );
       
   372 
       
   373     QList<QByteArray> audioSourceDevices = provider->devices(QByteArray(Q_MEDIASERVICE_AUDIOSOURCE));
       
   374     QCOMPARE(audioSourceDevices.count(), 2);
       
   375     QVERIFY(audioSourceDevices.contains(QByteArray("audiosource1")));
       
   376     QVERIFY(audioSourceDevices.contains(QByteArray("audiosource2")));
       
   377 
       
   378     QVERIFY(provider->devices(QByteArray("non existing service")).isEmpty());
       
   379 }
       
   380 
       
   381 
       
   382 
       
   383 
   409 
   384 void tst_QMediaServiceProvider::testProviderHints()
   410 void tst_QMediaServiceProvider::testProviderHints()
   385 {
   411 {
   386     {
   412     {
   387         QMediaServiceProviderHint hint;
   413         QMediaServiceProviderHint hint;