diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d6ecd118b33..3461c65356eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1538,6 +1538,7 @@ add_executable(mixxx-test src/test/mixxxtest.cpp src/test/mock_networkaccessmanager.cpp src/test/movinginterquartilemean_test.cpp + src/test/musicbrainzrecordingstasktest.cpp src/test/nativeeffects_test.cpp src/test/performancetimer_test.cpp src/test/playcountertest.cpp diff --git a/src/test/musicbrainzrecordingstasktest.cpp b/src/test/musicbrainzrecordingstasktest.cpp new file mode 100644 index 000000000000..0560b8cb17b0 --- /dev/null +++ b/src/test/musicbrainzrecordingstasktest.cpp @@ -0,0 +1,140 @@ +#include "test/musicbrainzrecordingstasktest.h" + +#include + +#include +#include + +#include "musicbrainz/web/musicbrainzrecordingstask.h" +#include "test/mixxxtest.h" +#include "test/mock_networkaccessmanager.h" +#include "util/compatibility.h" + +class MusicBrainzRecordingsTaskTest : public MixxxTest { + protected: + MusicBrainzRecordingsTaskTest() { + m_pReceiver.reset(new MockMusicBrainzReceiver()); + m_recordingIds = { + "416a273e-51b8-4b1c-8873-4c9b4ed54a0f", + "5e0aa7e4-01cb-441c-9fc2-d89e890cb981", + "5f6340ae-9cab-4f00-83d5-7ad00ac35f5b"}; + m_expectedParams = { + {"inc", "artists+artist-credits+releases+release-groups+media"}}; + + m_pMusicBrainzTask.reset(new mixxx::MusicBrainzRecordingsTask( + &m_network, + m_recordingIds, + m_pReceiver.data())); + QObject::connect(m_pMusicBrainzTask.data(), + &mixxx::MusicBrainzRecordingsTask::succeeded, + m_pReceiver.data(), + &MockMusicBrainzReceiver::slotMusicBrainzTaskSucceeded); + QObject::connect(m_pMusicBrainzTask.data(), + &mixxx::MusicBrainzRecordingsTask::failed, + m_pReceiver.data(), + &MockMusicBrainzReceiver::slotMusicBrainzTaskFailed); + QObject::connect(m_pMusicBrainzTask.data(), + &mixxx::MusicBrainzRecordingsTask::aborted, + m_pReceiver.data(), + &MockMusicBrainzReceiver::slotMusicBrainzTaskAborted); + QObject::connect(m_pMusicBrainzTask.data(), + &mixxx::MusicBrainzRecordingsTask::networkError, + m_pReceiver.data(), + &MockMusicBrainzReceiver::slotMusicBrainzTaskNetworkError); + }; + + QScopedPointer m_pReceiver; + QScopedPointer m_pMusicBrainzTask; + QList m_recordingIds; + QMap m_expectedParams; + MockNetworkAccessManager m_network; +}; + +void MockMusicBrainzReceiver::slotMusicBrainzTaskSucceeded( + const QList& guessedTrackReleases) { + Q_UNUSED(guessedTrackReleases); + qDebug() << "MockMusicBrainzReceiver::slotMusicBrainzTaskSucceeded"; + MocSucceeded(); +} + +void MockMusicBrainzReceiver::slotMusicBrainzTaskFailed( + const mixxx::network::WebResponse& response, + int errorCode, + const QString& errorMessage) { + Q_UNUSED(response); + Q_UNUSED(errorCode); + Q_UNUSED(errorMessage); + qDebug() << "MockMusicBrainzReceiver::slotMusicBrainzTaskFailed"; + MocFailed(); +} + +void MockMusicBrainzReceiver::slotMusicBrainzTaskAborted() { + qDebug() << "MockMusicBrainzReceiver::slotMusicBrainzTaskAborted"; + MocAborted(); +} + +void MockMusicBrainzReceiver::slotMusicBrainzTaskNetworkError( + QNetworkReply::NetworkError errorCode, + const QString& errorString, + const mixxx::network::WebResponseWithContent& responseWithContent) { + Q_UNUSED(errorCode); + Q_UNUSED(errorString); + Q_UNUSED(responseWithContent); + qDebug() << "MockMusicBrainzReceiver::slotMusicBrainzTaskNetworkError"; + MocNetworkError(); +} + +TEST_F(MusicBrainzRecordingsTaskTest, ClinetSideTimeout) { + EXPECT_CALL(*m_pReceiver.data(), MocNetworkError()).Times(1); + m_network.ExpectGet( + uuidToStringWithoutBraces(m_recordingIds[0]), + m_expectedParams, + 404, + QByteArray()); + m_pMusicBrainzTask->invokeStart(100); + + // Wait until the timeout happens + while (m_pMusicBrainzTask->isBusy()) { + application()->processEvents(); + } +} + +TEST_F(MusicBrainzRecordingsTaskTest, RespodsEmpty) { + EXPECT_CALL(*m_pReceiver.data(), MocFailed()).Times(1); + MockNetworkReply* pReply0 = m_network.ExpectGet( + uuidToStringWithoutBraces(m_recordingIds[0]), + m_expectedParams, + 200, + QByteArray()); + m_pMusicBrainzTask->invokeStart(10000); + + application()->processEvents(); + qDebug() << "pReply0->Done()" << pReply0; + pReply0->Done(); + MockNetworkReply* pReply1 = m_network.ExpectGet( + uuidToStringWithoutBraces(m_recordingIds[1]), + m_expectedParams, + 200, + QByteArray()); + + QTest::qSleep(1000); // millis + application()->processEvents(); + + qDebug() << "pReply1->Done()" << pReply1; + pReply1->Done(); + MockNetworkReply* pReply2 = m_network.ExpectGet( + uuidToStringWithoutBraces(m_recordingIds[2]), + m_expectedParams, + 200, + QByteArray()); + + QTest::qSleep(1000); // millis + application()->processEvents(); + + qDebug() << "pReply2->Done()" << pReply2; + pReply2->Done(); + + while (m_pMusicBrainzTask->isBusy()) { + application()->processEvents(); + } +} diff --git a/src/test/musicbrainzrecordingstasktest.h b/src/test/musicbrainzrecordingstasktest.h new file mode 100644 index 000000000000..fd97b8e1b7a2 --- /dev/null +++ b/src/test/musicbrainzrecordingstasktest.h @@ -0,0 +1,30 @@ +#include + +#include "gmock/gmock.h" +#include "musicbrainz/musicbrainz.h" +#include "network/webtask.h" + +class MockMusicBrainzReceiver : public QObject { + Q_OBJECT + public: + MockMusicBrainzReceiver() = default; + ~MockMusicBrainzReceiver() override = default; + + MOCK_METHOD(void, MocSucceeded, ()); + MOCK_METHOD(void, MocFailed, ()); + MOCK_METHOD(void, MocAborted, ()); + MOCK_METHOD(void, MocNetworkError, ()); + + public slots: + void slotMusicBrainzTaskSucceeded( + const QList& guessedTrackReleases); + void slotMusicBrainzTaskFailed( + const mixxx::network::WebResponse& response, + int errorCode, + const QString& errorMessage); + void slotMusicBrainzTaskAborted(); + void slotMusicBrainzTaskNetworkError( + QNetworkReply::NetworkError errorCode, + const QString& errorString, + const mixxx::network::WebResponseWithContent& responseWithContent); +};