diff options
author | Doris Verria <doris.verria@qt.io> | 2021-02-10 16:54:25 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2021-02-17 08:26:08 +0000 |
commit | 59b04ded0fe5fe5bbc3e2c29d7cd426a78600e59 (patch) | |
tree | e3d6200ab6d289f0676d6ad913abd0f493ad6451 | |
parent | 03f3201a24514f43e781bf59026ca5039fd9ab21 (diff) |
Remove the notifyInterval functionality from QMediaSource
As a step towards removing QMediaSource from the
hierarchy, remove its notifyInterval functionality and implement it
in the child classes QMediaPlayer and QMediaRecorder instead.
Change-Id: I17aa778fc306881f9e08ab4decbc2d2f3928c686
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r-- | src/multimedia/playback/qmediaplayer.cpp | 88 | ||||
-rw-r--r-- | src/multimedia/playback/qmediaplayer.h | 9 | ||||
-rw-r--r-- | src/multimedia/qmediasource.cpp | 103 | ||||
-rw-r--r-- | src/multimedia/qmediasource.h | 10 | ||||
-rw-r--r-- | src/multimedia/qmediasource_p.h | 7 | ||||
-rw-r--r-- | src/multimedia/recording/qmediarecorder.cpp | 66 | ||||
-rw-r--r-- | src/multimedia/recording/qmediarecorder.h | 7 | ||||
-rw-r--r-- | src/multimedia/recording/qmediarecorder_p.h | 2 | ||||
-rw-r--r-- | tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp | 227 | ||||
-rw-r--r-- | tests/auto/unit/multimedia/qmediasource/tst_qmediasource.cpp | 227 |
10 files changed, 398 insertions, 348 deletions
diff --git a/src/multimedia/playback/qmediaplayer.cpp b/src/multimedia/playback/qmediaplayer.cpp index cf2823748..dc639c2c4 100644 --- a/src/multimedia/playback/qmediaplayer.cpp +++ b/src/multimedia/playback/qmediaplayer.cpp @@ -101,6 +101,7 @@ class QMediaPlayerPrivate : public QMediaSourcePrivate Q_DECLARE_NON_CONST_PUBLIC(QMediaPlayer) public: + QMediaPlayerPrivate() : notifyTimer(nullptr) {} QMediaPlatformPlayerInterface *playerInterface = nullptr; QMediaPlayerControl* control = nullptr; QString errorString; @@ -125,8 +126,31 @@ public: void _q_stateChanged(QMediaPlayer::State state); void _q_mediaStatusChanged(QMediaPlayer::MediaStatus status); void _q_error(int error, const QString &errorString); + void _q_notify(); + + QTimer* notifyTimer; + QSet<int> notifyProperties; }; +void QMediaPlayerPrivate::_q_notify() +{ + Q_Q(QMediaPlayer); + + const QMetaObject* m = q->metaObject(); + + // QTBUG-57045 + // we create a copy of notifyProperties container to ensure that if a property is removed + // from the original container as a result of invoking propertyChanged signal, the iterator + // won't become invalidated + QSet<int> properties = notifyProperties; + + for (int pi : qAsConst(properties)) { + QMetaProperty p = m->property(pi); + p.notifySignal().invoke( + q, QGenericArgument(p.metaType().name(), p.read(q).data())); + } +} + void QMediaPlayerPrivate::_q_stateChanged(QMediaPlayer::State ps) { Q_Q(QMediaPlayer); @@ -264,6 +288,10 @@ QMediaPlayer::QMediaPlayer(QObject *parent): { Q_D(QMediaPlayer); + d->notifyTimer = new QTimer(this); + d->notifyTimer->setInterval(1000); + connect(d->notifyTimer, SIGNAL(timeout()), SLOT(_q_notify())); + d->playerInterface = QMediaPlatformIntegration::instance()->createPlayerInterface(); if (!d->playerInterface) { qWarning() << "QPlatformMediaPlayerInterface not implemented!"; @@ -316,6 +344,66 @@ QMediaPlayer::~QMediaPlayer() delete d->playerInterface; } +int QMediaPlayer::notifyInterval() const +{ + return d_func()->notifyTimer->interval(); +} + +void QMediaPlayer::setNotifyInterval(int milliSeconds) +{ + Q_D(QMediaPlayer); + + if (d->notifyTimer->interval() != milliSeconds) { + d->notifyTimer->setInterval(milliSeconds); + + emit notifyIntervalChanged(milliSeconds); + } +} + +/*! + Watch the property \a name. The property's notify signal will be emitted + once every \c notifyInterval milliseconds. + + \sa notifyInterval +*/ + +void QMediaPlayer::addPropertyWatch(QByteArray const &name) +{ + Q_D(QMediaPlayer); + + const QMetaObject* m = metaObject(); + + int index = m->indexOfProperty(name.constData()); + + if (index != -1 && m->property(index).hasNotifySignal()) { + d->notifyProperties.insert(index); + + if (!d->notifyTimer->isActive()) + d->notifyTimer->start(); + } +} + +/*! + Remove property \a name from the list of properties whose changes are + regularly signaled. + + \sa notifyInterval +*/ + +void QMediaPlayer::removePropertyWatch(QByteArray const &name) +{ + Q_D(QMediaPlayer); + + int index = metaObject()->indexOfProperty(name.constData()); + + if (index != -1) { + d->notifyProperties.remove(index); + + if (d->notifyProperties.isEmpty()) + d->notifyTimer->stop(); + } +} + QUrl QMediaPlayer::media() const { Q_D(const QMediaPlayer); diff --git a/src/multimedia/playback/qmediaplayer.h b/src/multimedia/playback/qmediaplayer.h index 591383c30..791580253 100644 --- a/src/multimedia/playback/qmediaplayer.h +++ b/src/multimedia/playback/qmediaplayer.h @@ -55,6 +55,7 @@ class QMediaPlayerPrivate; class Q_MULTIMEDIA_EXPORT QMediaPlayer : public QMediaSource { Q_OBJECT + Q_PROPERTY(int notifyInterval READ notifyInterval WRITE setNotifyInterval NOTIFY notifyIntervalChanged) Q_PROPERTY(QUrl media READ media WRITE setMedia NOTIFY mediaChanged) Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged) Q_PROPERTY(qint64 position READ position WRITE setPosition NOTIFY positionChanged) @@ -112,6 +113,11 @@ public: // bool enableLowLatencyPlayback(bool tryEnable); // bool isLowLatencyPlaybackEnabled() const; + int notifyInterval() const; + void setNotifyInterval(int milliSeconds); + void addPropertyWatch(QByteArray const &name); + void removePropertyWatch(QByteArray const &name); + bool setAudioOutput(const QAudioDeviceInfo &device); QAudioDeviceInfo audioOutput() const; @@ -179,6 +185,8 @@ public Q_SLOTS: void setMedia(const QUrl &media, QIODevice *stream = nullptr); Q_SIGNALS: + void notifyIntervalChanged(int milliSeconds); + void mediaChanged(const QUrl &media); void stateChanged(QMediaPlayer::State newState); @@ -208,6 +216,7 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_stateChanged(QMediaPlayer::State)) Q_PRIVATE_SLOT(d_func(), void _q_mediaStatusChanged(QMediaPlayer::MediaStatus)) Q_PRIVATE_SLOT(d_func(), void _q_error(int, const QString &)) + Q_PRIVATE_SLOT(d_func(), void _q_notify()) }; QT_END_NAMESPACE diff --git a/src/multimedia/qmediasource.cpp b/src/multimedia/qmediasource.cpp index f807e823e..5015f7e77 100644 --- a/src/multimedia/qmediasource.cpp +++ b/src/multimedia/qmediasource.cpp @@ -48,25 +48,6 @@ QT_BEGIN_NAMESPACE -void QMediaSourcePrivate::_q_notify() -{ - Q_Q(QMediaSource); - - const QMetaObject* m = q->metaObject(); - - // QTBUG-57045 - // we create a copy of notifyProperties container to ensure that if a property is removed - // from the original container as a result of invoking propertyChanged signal, the iterator - // won't become invalidated - QSet<int> properties = notifyProperties; - - for (int pi : qAsConst(properties)) { - QMetaProperty p = m->property(pi); - p.notifySignal().invoke( - q, QGenericArgument(p.metaType().name(), p.read(q).data())); - } -} - /*! \class QMediaSource @@ -133,22 +114,6 @@ QMediaService* QMediaSource::service() const return d_func()->service; } -int QMediaSource::notifyInterval() const -{ - return d_func()->notifyTimer->interval(); -} - -void QMediaSource::setNotifyInterval(int milliSeconds) -{ - Q_D(QMediaSource); - - if (d->notifyTimer->interval() != milliSeconds) { - d->notifyTimer->setInterval(milliSeconds); - - emit notifyIntervalChanged(milliSeconds); - } -} - /*! Bind \a object to this QMediaSource instance. @@ -211,10 +176,6 @@ QMediaSource::QMediaSource(QObject *parent, QMediaService *service) { Q_D(QMediaSource); - d->notifyTimer = new QTimer(this); - d->notifyTimer->setInterval(1000); - connect(d->notifyTimer, SIGNAL(timeout()), SLOT(_q_notify())); - d->service = service; } @@ -227,74 +188,10 @@ QMediaSource::QMediaSource(QMediaSourcePrivate &dd, QObject *parent, QMediaServi { Q_D(QMediaSource); - d->notifyTimer = new QTimer(this); - d->notifyTimer->setInterval(1000); - connect(d->notifyTimer, SIGNAL(timeout()), SLOT(_q_notify())); - d->service = service; } /*! - Watch the property \a name. The property's notify signal will be emitted - once every \c notifyInterval milliseconds. - - \sa notifyInterval -*/ - -void QMediaSource::addPropertyWatch(QByteArray const &name) -{ - Q_D(QMediaSource); - - const QMetaObject* m = metaObject(); - - int index = m->indexOfProperty(name.constData()); - - if (index != -1 && m->property(index).hasNotifySignal()) { - d->notifyProperties.insert(index); - - if (!d->notifyTimer->isActive()) - d->notifyTimer->start(); - } -} - -/*! - Remove property \a name from the list of properties whose changes are - regularly signaled. - - \sa notifyInterval -*/ - -void QMediaSource::removePropertyWatch(QByteArray const &name) -{ - Q_D(QMediaSource); - - int index = metaObject()->indexOfProperty(name.constData()); - - if (index != -1) { - d->notifyProperties.remove(index); - - if (d->notifyProperties.isEmpty()) - d->notifyTimer->stop(); - } -} - -/*! - \property QMediaSource::notifyInterval - - The interval at which notifiable properties will update. - - The interval is expressed in milliseconds, the default value is 1000. - - \sa addPropertyWatch(), removePropertyWatch() -*/ - -/*! - \fn void QMediaSource::notifyIntervalChanged(int milliseconds) - - Signal a change in the notify interval period to \a milliseconds. -*/ - -/*! Returns the value associated with a meta-data \a key. See the list of predefined \l {QMediaMetaData}{meta-data keys}. diff --git a/src/multimedia/qmediasource.h b/src/multimedia/qmediasource.h index 8f376eeaf..42db044f1 100644 --- a/src/multimedia/qmediasource.h +++ b/src/multimedia/qmediasource.h @@ -57,7 +57,6 @@ class QMediaSourcePrivate; class Q_MULTIMEDIA_EXPORT QMediaSource : public QObject { Q_OBJECT - Q_PROPERTY(int notifyInterval READ notifyInterval WRITE setNotifyInterval NOTIFY notifyIntervalChanged) public: ~QMediaSource(); @@ -69,29 +68,20 @@ public: virtual QMediaService* service() const; - int notifyInterval() const; - void setNotifyInterval(int milliSeconds); - bool bind(QMediaSink *); void unbind(QMediaSink *); virtual QMediaMetaData metaData() const; Q_SIGNALS: - void notifyIntervalChanged(int milliSeconds); - void metaDataChanged(); protected: QMediaSource(QObject *parent, QMediaService *service); QMediaSource(QMediaSourcePrivate &dd, QObject *parent, QMediaService *service); - void addPropertyWatch(QByteArray const &name); - void removePropertyWatch(QByteArray const &name); - private: Q_DECLARE_PRIVATE(QMediaSource) - Q_PRIVATE_SLOT(d_func(), void _q_notify()) }; diff --git a/src/multimedia/qmediasource_p.h b/src/multimedia/qmediasource_p.h index e6000e11e..756c86955 100644 --- a/src/multimedia/qmediasource_p.h +++ b/src/multimedia/qmediasource_p.h @@ -70,15 +70,10 @@ class QMediaSourcePrivate : public QObjectPrivate Q_DECLARE_PUBLIC(QMediaSource) public: - QMediaSourcePrivate() : service(nullptr), notifyTimer(nullptr) {} + QMediaSourcePrivate() : service(nullptr) {} virtual ~QMediaSourcePrivate() {} - void _q_notify(); - QMediaService *service; - - QTimer* notifyTimer; - QSet<int> notifyProperties; }; QT_END_NAMESPACE diff --git a/src/multimedia/recording/qmediarecorder.cpp b/src/multimedia/recording/qmediarecorder.cpp index 2fa14fd4e..50b77ab94 100644 --- a/src/multimedia/recording/qmediarecorder.cpp +++ b/src/multimedia/recording/qmediarecorder.cpp @@ -238,6 +238,66 @@ QMediaRecorder::~QMediaRecorder() delete d_ptr; } +int QMediaRecorder::notifyInterval() const +{ + return d_func()->notifyTimer->interval(); +} + +void QMediaRecorder::setNotifyInterval(int milliSeconds) +{ + Q_D(QMediaRecorder); + + if (d->notifyTimer->interval() != milliSeconds) { + d->notifyTimer->setInterval(milliSeconds); + + emit notifyIntervalChanged(milliSeconds); + } +} + +/*! + Watch the property \a name. The property's notify signal will be emitted + once every \c notifyInterval milliseconds. + + \sa notifyInterval +*/ + +void QMediaRecorder::addPropertyWatch(QByteArray const &name) +{ + Q_D(QMediaRecorder); + + const QMetaObject* m = metaObject(); + + int index = m->indexOfProperty(name.constData()); + + if (index != -1 && m->property(index).hasNotifySignal()) { + d->notifyProperties.insert(index); + + if (!d->notifyTimer->isActive()) + d->notifyTimer->start(); + } +} + +/*! + Remove property \a name from the list of properties whose changes are + regularly signaled. + + \sa notifyInterval +*/ + +void QMediaRecorder::removePropertyWatch(QByteArray const &name) +{ + Q_D(QMediaRecorder); + + int index = metaObject()->indexOfProperty(name.constData()); + + if (index != -1) { + d->notifyProperties.remove(index); + + if (d->notifyProperties.isEmpty()) + d->notifyTimer->stop(); + } +} + /*! Returns the QMediaSource instance that this QMediaRecorder is bound too, or 0 otherwise. @@ -281,7 +341,7 @@ bool QMediaRecorder::setMediaSource(QMediaSource *object) this, SLOT(_q_error(int,QString))); } - disconnect(d->mediaSource, SIGNAL(notifyIntervalChanged(int)), this, SLOT(_q_updateNotifyInterval(int))); + disconnect(this, SIGNAL(notifyIntervalChanged(int)), this, SLOT(_q_updateNotifyInterval(int))); QMediaService *service = d->mediaSource->service(); @@ -300,8 +360,8 @@ bool QMediaRecorder::setMediaSource(QMediaSource *object) if (d->mediaSource) { QMediaService *service = d->mediaSource->service(); - d->notifyTimer->setInterval(d->mediaSource->notifyInterval()); - connect(d->mediaSource, SIGNAL(notifyIntervalChanged(int)), SLOT(_q_updateNotifyInterval(int))); + d->notifyTimer->setInterval(notifyInterval()); + connect(this, SIGNAL(notifyIntervalChanged(int)), SLOT(_q_updateNotifyInterval(int))); if (service) { d->control = qobject_cast<QMediaRecorderControl*>(service->requestControl(QMediaRecorderControl_iid)); diff --git a/src/multimedia/recording/qmediarecorder.h b/src/multimedia/recording/qmediarecorder.h index 303dd3dea..844abf966 100644 --- a/src/multimedia/recording/qmediarecorder.h +++ b/src/multimedia/recording/qmediarecorder.h @@ -72,6 +72,7 @@ class Q_MULTIMEDIA_EXPORT QMediaRecorder : public QObject, public QMediaSink Q_ENUMS(State) Q_ENUMS(Status) Q_ENUMS(Error) + Q_PROPERTY(int notifyInterval READ notifyInterval WRITE setNotifyInterval NOTIFY notifyIntervalChanged) Q_PROPERTY(QMediaRecorder::State state READ state NOTIFY stateChanged) Q_PROPERTY(QMediaRecorder::Status status READ status NOTIFY statusChanged) Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged) @@ -118,6 +119,11 @@ public: explicit QMediaRecorder(QMediaSource *mediaSource, QObject *parent = nullptr); ~QMediaRecorder(); + int notifyInterval() const; + void setNotifyInterval(int milliSeconds); + void addPropertyWatch(QByteArray const &name); + void removePropertyWatch(QByteArray const &name); + QMediaSource *mediaSource() const override; QObject *asObject() override { return this; } @@ -159,6 +165,7 @@ public Q_SLOTS: bool setAudioInput(const QAudioDeviceInfo &device); Q_SIGNALS: + void notifyIntervalChanged(int milliSeconds); void stateChanged(QMediaRecorder::State state); void statusChanged(QMediaRecorder::Status status); void durationChanged(qint64 duration); diff --git a/src/multimedia/recording/qmediarecorder_p.h b/src/multimedia/recording/qmediarecorder_p.h index 94d5d80c9..44b3e74b3 100644 --- a/src/multimedia/recording/qmediarecorder_p.h +++ b/src/multimedia/recording/qmediarecorder_p.h @@ -81,6 +81,8 @@ public: QTimer* notifyTimer = nullptr; + QSet<int> notifyProperties; + QMediaRecorder::State state = QMediaRecorder::StoppedState; QMediaRecorder::Error error = QMediaRecorder::NoError; QString errorString; diff --git a/tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp b/tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp index 40f1e7dec..9c778fe11 100644 --- a/tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp +++ b/tests/auto/unit/multimedia/qmediaplayer/tst_qmediaplayer.cpp @@ -79,6 +79,11 @@ public slots: void cleanup(); private slots: + void propertyWatch(); + void notifySignals_data(); + void notifySignals(); + void notifyInterval_data(); + void notifyInterval(); void testNullService_data(); void testNullService(); void testValid(); @@ -126,6 +131,7 @@ private slots: void testCustomAudioRole(); private: + void setupNotifyTests(); void setupCommonTestData(); QMockIntegration *mockIntegration; @@ -133,6 +139,227 @@ private: QMediaPlayer *player; }; +class QtTestMediaPlayer : public QMediaPlayer +{ + Q_OBJECT + Q_PROPERTY(int a READ a WRITE setA NOTIFY aChanged) + Q_PROPERTY(int b READ b WRITE setB NOTIFY bChanged) + Q_PROPERTY(int c READ c WRITE setC NOTIFY cChanged) + Q_PROPERTY(int d READ d WRITE setD) +public: + QtTestMediaPlayer() : QMediaPlayer() {} + + using QMediaPlayer::addPropertyWatch; + using QMediaPlayer::removePropertyWatch; + + [[nodiscard]] int a() const { return m_a; } + void setA(int a) { m_a = a; } + + [[nodiscard]] int b() const { return m_b; } + void setB(int b) { m_b = b; } + + [[nodiscard]] int c() const { return m_c; } + void setC(int c) { m_c = c; } + + [[nodiscard]] int d() const { return m_d; } + void setD(int d) { m_d = d; } + +Q_SIGNALS: + void aChanged(int a); + void bChanged(int b); + void cChanged(int c); + +private: + int m_a = 0; + int m_b = 0; + int m_c = 0; + int m_d = 0; +}; + +void tst_QMediaPlayer::propertyWatch() +{ + QtTestMediaPlayer object; + object.setNotifyInterval(0); + + QEventLoop loop; + connect(&object, SIGNAL(aChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&object, SIGNAL(bChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&object, SIGNAL(cChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); + + QSignalSpy aSpy(&object, SIGNAL(aChanged(int))); + QSignalSpy bSpy(&object, SIGNAL(bChanged(int))); + QSignalSpy cSpy(&object, SIGNAL(cChanged(int))); + + QTestEventLoop::instance().enterLoop(1); + + QCOMPARE(aSpy.count(), 0); + QCOMPARE(bSpy.count(), 0); + QCOMPARE(cSpy.count(), 0); + + int aCount = 0; + int bCount = 0; + int cCount = 0; + + object.addPropertyWatch("a"); + + QTestEventLoop::instance().enterLoop(1); + + QVERIFY(aSpy.count() > aCount); + QCOMPARE(bSpy.count(), 0); + QCOMPARE(cSpy.count(), 0); + QCOMPARE(aSpy.last().value(0).toInt(), 0); + + aCount = aSpy.count(); + + object.setA(54); + object.setB(342); + object.setC(233); + + QTestEventLoop::instance().enterLoop(1); + + QVERIFY(aSpy.count() > aCount); + QCOMPARE(bSpy.count(), 0); + QCOMPARE(cSpy.count(), 0); + QCOMPARE(aSpy.last().value(0).toInt(), 54); + + aCount = aSpy.count(); + + object.addPropertyWatch("b"); + object.addPropertyWatch("d"); + object.removePropertyWatch("e"); + object.setA(43); + object.setB(235); + object.setC(90); + + QTestEventLoop::instance().enterLoop(1); + + QVERIFY(aSpy.count() > aCount); + QVERIFY(bSpy.count() > bCount); + QCOMPARE(cSpy.count(), 0); + QCOMPARE(aSpy.last().value(0).toInt(), 43); + QCOMPARE(bSpy.last().value(0).toInt(), 235); + + aCount = aSpy.count(); + bCount = bSpy.count(); + + object.removePropertyWatch("a"); + object.addPropertyWatch("c"); + object.addPropertyWatch("e"); + + QTestEventLoop::instance().enterLoop(1); + + QCOMPARE(aSpy.count(), aCount); + QVERIFY(bSpy.count() > bCount); + QVERIFY(cSpy.count() > cCount); + QCOMPARE(bSpy.last().value(0).toInt(), 235); + QCOMPARE(cSpy.last().value(0).toInt(), 90); + + bCount = bSpy.count(); + cCount = cSpy.count(); + + object.setA(435); + object.setC(9845); + + QTestEventLoop::instance().enterLoop(1); + + QCOMPARE(aSpy.count(), aCount); + QVERIFY(bSpy.count() > bCount); + QVERIFY(cSpy.count() > cCount); + QCOMPARE(bSpy.last().value(0).toInt(), 235); + QCOMPARE(cSpy.last().value(0).toInt(), 9845); + + bCount = bSpy.count(); + cCount = cSpy.count(); + + object.setA(8432); + object.setB(324); + object.setC(443); + object.removePropertyWatch("c"); + object.removePropertyWatch("d"); + + QTestEventLoop::instance().enterLoop(1); + + QCOMPARE(aSpy.count(), aCount); + QVERIFY(bSpy.count() > bCount); + QCOMPARE(cSpy.count(), cCount); + QCOMPARE(bSpy.last().value(0).toInt(), 324); + QCOMPARE(cSpy.last().value(0).toInt(), 9845); + + bCount = bSpy.count(); + + object.removePropertyWatch("b"); + + QTestEventLoop::instance().enterLoop(1); + + QCOMPARE(aSpy.count(), aCount); + QCOMPARE(bSpy.count(), bCount); + QCOMPARE(cSpy.count(), cCount); +} + +void tst_QMediaPlayer::setupNotifyTests() +{ + QTest::addColumn<int>("interval"); + QTest::addColumn<int>("count"); + + QTest::newRow("single 750ms") + << 750 + << 1; + QTest::newRow("single 600ms") + << 600 + << 1; + QTest::newRow("x3 300ms") + << 300 + << 3; + QTest::newRow("x5 180ms") + << 180 + << 5; +} + +void tst_QMediaPlayer::notifySignals_data() +{ + setupNotifyTests(); +} + +void tst_QMediaPlayer::notifySignals() +{ + QFETCH(int, interval); + QFETCH(int, count); + + QtTestMediaPlayer object; + QSignalSpy spy(&object, SIGNAL(aChanged(int))); + + object.setNotifyInterval(interval); + object.addPropertyWatch("a"); + + QElapsedTimer timer; + timer.start(); + + QTRY_COMPARE(spy.count(), count); +} + +void tst_QMediaPlayer::notifyInterval_data() +{ + setupNotifyTests(); +} + +void tst_QMediaPlayer::notifyInterval() +{ + QFETCH(int, interval); + + QtTestMediaPlayer object; + QSignalSpy spy(&object, SIGNAL(notifyIntervalChanged(int))); + + object.setNotifyInterval(interval); + QCOMPARE(object.notifyInterval(), interval); + QCOMPARE(spy.count(), 1); + QCOMPARE(spy.last().value(0).toInt(), interval); + + object.setNotifyInterval(interval); + QCOMPARE(object.notifyInterval(), interval); + QCOMPARE(spy.count(), 1); +} + + void tst_QMediaPlayer::setupCommonTestData() { QTest::addColumn<bool>("valid"); diff --git a/tests/auto/unit/multimedia/qmediasource/tst_qmediasource.cpp b/tests/auto/unit/multimedia/qmediasource/tst_qmediasource.cpp index fb581acfa..08452b8fd 100644 --- a/tests/auto/unit/multimedia/qmediasource/tst_qmediasource.cpp +++ b/tests/auto/unit/multimedia/qmediasource/tst_qmediasource.cpp @@ -30,8 +30,6 @@ #include <QtTest/QtTest> -#include <QtCore/qtimer.h> - #include <QtMultimedia/qmediametadata.h> #include <qmediasource.h> #include <qmediaservice.h> @@ -64,240 +62,17 @@ class tst_QMediaSource : public QObject Q_OBJECT private slots: - void propertyWatch(); - void notifySignals_data(); - void notifySignals(); - void notifyInterval_data(); - void notifyInterval(); - void availability(); - void service(); - -private: - void setupNotifyTests(); }; - class QtTestMediaObject : public QMediaSource { Q_OBJECT - Q_PROPERTY(int a READ a WRITE setA NOTIFY aChanged) - Q_PROPERTY(int b READ b WRITE setB NOTIFY bChanged) - Q_PROPERTY(int c READ c WRITE setC NOTIFY cChanged) - Q_PROPERTY(int d READ d WRITE setD) + public: QtTestMediaObject(QMediaService *service = nullptr): QMediaSource(nullptr, service) {} - - using QMediaSource::addPropertyWatch; - using QMediaSource::removePropertyWatch; - - [[nodiscard]] int a() const { return m_a; } - void setA(int a) { m_a = a; } - - [[nodiscard]] int b() const { return m_b; } - void setB(int b) { m_b = b; } - - [[nodiscard]] int c() const { return m_c; } - void setC(int c) { m_c = c; } - - [[nodiscard]] int d() const { return m_d; } - void setD(int d) { m_d = d; } - -Q_SIGNALS: - void aChanged(int a); - void bChanged(int b); - void cChanged(int c); - -private: - int m_a = 0; - int m_b = 0; - int m_c = 0; - int m_d = 0; }; -void tst_QMediaSource::propertyWatch() -{ - QtTestMediaObject object; - object.setNotifyInterval(0); - - QEventLoop loop; - connect(&object, SIGNAL(aChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); - connect(&object, SIGNAL(bChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); - connect(&object, SIGNAL(cChanged(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); - - QSignalSpy aSpy(&object, SIGNAL(aChanged(int))); - QSignalSpy bSpy(&object, SIGNAL(bChanged(int))); - QSignalSpy cSpy(&object, SIGNAL(cChanged(int))); - - QTestEventLoop::instance().enterLoop(1); - - QCOMPARE(aSpy.count(), 0); - QCOMPARE(bSpy.count(), 0); - QCOMPARE(cSpy.count(), 0); - - int aCount = 0; - int bCount = 0; - int cCount = 0; - - object.addPropertyWatch("a"); - - QTestEventLoop::instance().enterLoop(1); - - QVERIFY(aSpy.count() > aCount); - QCOMPARE(bSpy.count(), 0); - QCOMPARE(cSpy.count(), 0); - QCOMPARE(aSpy.last().value(0).toInt(), 0); - - aCount = aSpy.count(); - - object.setA(54); - object.setB(342); - object.setC(233); - - QTestEventLoop::instance().enterLoop(1); - - QVERIFY(aSpy.count() > aCount); - QCOMPARE(bSpy.count(), 0); - QCOMPARE(cSpy.count(), 0); - QCOMPARE(aSpy.last().value(0).toInt(), 54); - - aCount = aSpy.count(); - - object.addPropertyWatch("b"); - object.addPropertyWatch("d"); - object.removePropertyWatch("e"); - object.setA(43); - object.setB(235); - object.setC(90); - - QTestEventLoop::instance().enterLoop(1); - - QVERIFY(aSpy.count() > aCount); - QVERIFY(bSpy.count() > bCount); - QCOMPARE(cSpy.count(), 0); - QCOMPARE(aSpy.last().value(0).toInt(), 43); - QCOMPARE(bSpy.last().value(0).toInt(), 235); - - aCount = aSpy.count(); - bCount = bSpy.count(); - - object.removePropertyWatch("a"); - object.addPropertyWatch("c"); - object.addPropertyWatch("e"); - - QTestEventLoop::instance().enterLoop(1); - - QCOMPARE(aSpy.count(), aCount); - QVERIFY(bSpy.count() > bCount); - QVERIFY(cSpy.count() > cCount); - QCOMPARE(bSpy.last().value(0).toInt(), 235); - QCOMPARE(cSpy.last().value(0).toInt(), 90); - - bCount = bSpy.count(); - cCount = cSpy.count(); - - object.setA(435); - object.setC(9845); - - QTestEventLoop::instance().enterLoop(1); - - QCOMPARE(aSpy.count(), aCount); - QVERIFY(bSpy.count() > bCount); - QVERIFY(cSpy.count() > cCount); - QCOMPARE(bSpy.last().value(0).toInt(), 235); - QCOMPARE(cSpy.last().value(0).toInt(), 9845); - - bCount = bSpy.count(); - cCount = cSpy.count(); - - object.setA(8432); - object.setB(324); - object.setC(443); - object.removePropertyWatch("c"); - object.removePropertyWatch("d"); - - QTestEventLoop::instance().enterLoop(1); - - QCOMPARE(aSpy.count(), aCount); - QVERIFY(bSpy.count() > bCount); - QCOMPARE(cSpy.count(), cCount); - QCOMPARE(bSpy.last().value(0).toInt(), 324); - QCOMPARE(cSpy.last().value(0).toInt(), 9845); - - bCount = bSpy.count(); - - object.removePropertyWatch("b"); - - QTestEventLoop::instance().enterLoop(1); - - QCOMPARE(aSpy.count(), aCount); - QCOMPARE(bSpy.count(), bCount); - QCOMPARE(cSpy.count(), cCount); -} - -void tst_QMediaSource::setupNotifyTests() -{ - QTest::addColumn<int>("interval"); - QTest::addColumn<int>("count"); - - QTest::newRow("single 750ms") - << 750 - << 1; - QTest::newRow("single 600ms") - << 600 - << 1; - QTest::newRow("x3 300ms") - << 300 - << 3; - QTest::newRow("x5 180ms") - << 180 - << 5; -} - -void tst_QMediaSource::notifySignals_data() -{ - setupNotifyTests(); -} - -void tst_QMediaSource::notifySignals() -{ - QFETCH(int, interval); - QFETCH(int, count); - - QtTestMediaObject object; - QSignalSpy spy(&object, SIGNAL(aChanged(int))); - - object.setNotifyInterval(interval); - object.addPropertyWatch("a"); - - QElapsedTimer timer; - timer.start(); - - QTRY_COMPARE(spy.count(), count); -} - -void tst_QMediaSource::notifyInterval_data() -{ - setupNotifyTests(); -} - -void tst_QMediaSource::notifyInterval() -{ - QFETCH(int, interval); - - QtTestMediaObject object; - QSignalSpy spy(&object, SIGNAL(notifyIntervalChanged(int))); - - object.setNotifyInterval(interval); - QCOMPARE(object.notifyInterval(), interval); - QCOMPARE(spy.count(), 1); - QCOMPARE(spy.last().value(0).toInt(), interval); - - object.setNotifyInterval(interval); - QCOMPARE(object.notifyInterval(), interval); - QCOMPARE(spy.count(), 1); -} - void tst_QMediaSource::availability() { { |