diff --git a/libs/test/CMakeLists.txt b/libs/test/CMakeLists.txt index 43708d60..92869a2c 100644 --- a/libs/test/CMakeLists.txt +++ b/libs/test/CMakeLists.txt @@ -1,23 +1,23 @@ enable_testing() include(CTest) SET(QT_USE_QTMAIN TRUE) SET(QT_USE_QTTEST TRUE) function(pmc_test TESTNAME) - kde4_add_executable(${TESTNAME} ${TESTNAME}.cpp fakemediavalidator.cpp) + kde4_add_executable(${TESTNAME} ${TESTNAME}.cpp) add_test(pmc-libs-${TESTNAME} ${TESTNAME}) target_link_libraries(${TESTNAME} plasmamediacentertest ${KDE4_KDECORE_LIBRARY} ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARIES} ${QT_QTGUI_LIBRARIES} ${QT_QTNETWORK_LIBRARIES} ${QT_QTXML_LIBRARIES} ${QT_QTDECLARATIVE_LIBRARIES} - ${TAGLIB_LIBRARIES} + ${TAGLIB_LIBRARIES} -lmockcpp ) endfunction(pmc_test) pmc_test(medialibrarytest) pmc_test(singletonfactorytest) pmc_test(lastfmimagefetchertest) pmc_test(pmcmediatest) pmc_test(mediatest) pmc_test(mediacentertest) pmc_test(itemcachetest) diff --git a/libs/test/fakemediavalidator.cpp b/libs/test/fakemediavalidator.cpp deleted file mode 100644 index 9b2feca6..00000000 --- a/libs/test/fakemediavalidator.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/*********************************************************************************** - * Copyright 2014 Shantanu Tushar * - * * - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU Lesser General Public * - * License as published by the Free Software Foundation; either * - * version 2.1 of the License, or (at your option) any later version. * - * * - * This library is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * - * Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public * - * License along with this library. If not, see . * - ***********************************************************************************/ - -#include "fakemediavalidator.h" - -bool FakeMediaValidator::fileWithUrlExists(const QString& url) -{ - Q_UNUSED(url) - return true; -} diff --git a/libs/test/fakemediavalidator.h b/libs/test/fakemediavalidator.h deleted file mode 100644 index 201973c4..00000000 --- a/libs/test/fakemediavalidator.h +++ /dev/null @@ -1,31 +0,0 @@ -/*********************************************************************************** - * Copyright 2014 Shantanu Tushar * - * * - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU Lesser General Public * - * License as published by the Free Software Foundation; either * - * version 2.1 of the License, or (at your option) any later version. * - * * - * This library is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * - * Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public * - * License along with this library. If not, see . * - ***********************************************************************************/ - -#ifndef FAKEMEDIAVALIDATOR_H -#define FAKEMEDIAVALIDATOR_H - -#include - -class FakeMediaValidator : public MediaValidator -{ - Q_OBJECT -public: - virtual bool fileWithUrlExists(const QString& url); -}; - -#endif // FAKEMEDIAVALIDATOR_H diff --git a/libs/test/medialibrarytest.cpp b/libs/test/medialibrarytest.cpp index 08af2395..bb5efff6 100644 --- a/libs/test/medialibrarytest.cpp +++ b/libs/test/medialibrarytest.cpp @@ -1,280 +1,293 @@ /*********************************************************************************** * Copyright 2014 Shantanu Tushar * * * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library. If not, see . * ***********************************************************************************/ #include "medialibrarytest.h" -#include "fakemediavalidator.h" #include "testhelpers.h" #include #include #include #include #include #include +#include +#include +#include + QTEST_KDEMAIN(MediaLibraryTest, NoGUI); void MediaLibraryTest::initTestCase() { } void MediaLibraryTest::cleanupTestCase() { // Called after the last testfunction was executed } void MediaLibraryTest::init() { } void MediaLibraryTest::cleanup() { } QHash MediaLibraryTest::createTestMediaData() const { QHash data; data.insert(MediaCenter::MediaUrlRole, "/foo/bar"); data.insert(Qt::DisplayRole, "Title"); data.insert(MediaCenter::MediaTypeRole, "audio"); data.insert(Qt::DecorationRole, "smiley"); data.insert(MediaCenter::DurationRole, 100); data.insert(MediaCenter::CreatedAtRole, QDateTime::currentDateTimeUtc()); return data; } QHash< int, QVariant > MediaLibraryTest::createTestMediaDataWithAlbumArtist() const { QHash data = createTestMediaData(); data.insert(MediaCenter::AlbumRole, "album"); data.insert(MediaCenter::ArtistRole, "artist"); return data; } void MediaLibraryTest::addsNewMediaAndItsMetadata() { - FakeMediaValidator validator; - MediaLibrary mediaLibrary(&validator); + MockObject validator; + MOCK_METHOD(validator, fileWithUrlExists).stubs().will(returnValue(true)); + + MediaLibrary mediaLibrary(validator); mediaLibrary.start(); QSignalSpy newMediaSpy(&mediaLibrary, SIGNAL(newMedia(QList< QSharedPointer >))); QVERIFY2(newMediaSpy.isValid(), "Could not listen to signal newMedia"); QSignalSpy newAlbumSpy(&mediaLibrary, SIGNAL(newAlbums(QList< QSharedPointer >))); QVERIFY2(newAlbumSpy.isValid(), "Could not listen to signal newAlbums"); QSignalSpy newArtistSpy(&mediaLibrary, SIGNAL(newArtists(QList< QSharedPointer >))); QVERIFY2(newArtistSpy.isValid(), "Could not listen to signal newArtists"); QHash data = createTestMediaDataWithAlbumArtist(); mediaLibrary.updateMedia(data); waitForSignal(&newMediaSpy); waitForSignal(&newAlbumSpy); waitForSignal(&newArtistSpy); QCOMPARE(newMediaSpy.size(), 1); QList > returnedMedia = newMediaSpy.takeFirst().first().value< QList > >(); QCOMPARE(returnedMedia.size(), 1); QSharedPointer media = returnedMedia.first(); QCOMPARE(media->url(), data.value(MediaCenter::MediaUrlRole).toString()); QCOMPARE(media->title(), data.value(Qt::DisplayRole).toString()); QCOMPARE(media->type(), data.value(MediaCenter::MediaTypeRole).toString()); QCOMPARE(media->thumbnail(), data.value(Qt::DecorationRole).toString()); QCOMPARE(media->album(), data.value(MediaCenter::AlbumRole).toString()); QCOMPARE(media->artist(), data.value(MediaCenter::ArtistRole).toString()); QCOMPARE(media->duration(), data.value(MediaCenter::DurationRole).toInt()); QCOMPARE(media->createdAt(), data.value(MediaCenter::CreatedAtRole).toDateTime()); QCOMPARE(newAlbumSpy.size(), 1); QList > returnedAlbum = newAlbumSpy.takeFirst().first().value< QList > >(); QCOMPARE(returnedAlbum.size(), 1); QSharedPointer album = returnedAlbum.first(); QCOMPARE(album->name(), data.value(MediaCenter::AlbumRole).toString()); QCOMPARE(album->albumArtist(), data.value(MediaCenter::ArtistRole).toString()); QCOMPARE(newArtistSpy.size(), 1); QList > returnedArtist = newArtistSpy.takeFirst().first().value< QList > >(); QCOMPARE(returnedArtist.size(), 1); QSharedPointer artist = returnedArtist.first(); QCOMPARE(artist->name(), data.value(MediaCenter::ArtistRole).toString()); } void MediaLibraryTest::shouldEmitUpdatedForMediaInsteadOfNewMediaWhenDataUpdated() { - FakeMediaValidator validator; - MediaLibrary mediaLibrary(&validator); + MockObject validator; + MOCK_METHOD(validator, fileWithUrlExists).stubs().will(returnValue(true)); + + MediaLibrary mediaLibrary(validator); mediaLibrary.start(); QHash data = createTestMediaDataWithAlbumArtist(); QSignalSpy newMediaSpy(&mediaLibrary, SIGNAL(newMedia(QList< QSharedPointer >))); QVERIFY2(newMediaSpy.isValid(), "Could not listen to signal newMedia"); mediaLibrary.updateMedia(data); waitForSignal(&newMediaSpy); QCOMPARE(newMediaSpy.size(), 1); QList > returnedMedia = newMediaSpy.takeFirst().first().value< QList > >(); QCOMPARE(returnedMedia.size(), 1); QSharedPointer pmcMedia = returnedMedia.at(0); QSignalSpy mediaUpdatedSpy(pmcMedia.data(), SIGNAL(updated())); data.insert(Qt::DisplayRole, "New title"); mediaLibrary.updateMedia(data); //Should not emit newMedia waitForSignal(&newMediaSpy); QCOMPARE(newMediaSpy.size(), 0); //Should emit updated for the PmcMedia QCOMPARE(mediaUpdatedSpy.size(), 1); } void MediaLibraryTest::shouldNotEmitUpdatedWhenNothingUpdated() { - FakeMediaValidator validator; - MediaLibrary mediaLibrary(&validator); + MockObject validator; + MOCK_METHOD(validator, fileWithUrlExists).stubs().will(returnValue(true)); + + MediaLibrary mediaLibrary(validator); mediaLibrary.start(); QHash data = createTestMediaDataWithAlbumArtist(); QSignalSpy newMediaSpy(&mediaLibrary, SIGNAL(newMedia(QList< QSharedPointer >))); QVERIFY2(newMediaSpy.isValid(), "Could not listen to signal newMedia"); mediaLibrary.updateMedia(data); waitForSignal(&newMediaSpy); QCOMPARE(newMediaSpy.size(), 1); QList > returnedMedia = newMediaSpy.takeFirst().first().value< QList > >(); QCOMPARE(returnedMedia.size(), 1); QSharedPointer pmcMedia = returnedMedia.at(0); QSignalSpy mediaUpdatedSpy(pmcMedia.data(), SIGNAL(updated())); mediaLibrary.updateMedia(data); //Should not emit newMedia waitForSignal(&newMediaSpy); QCOMPARE(newMediaSpy.size(), 0); //Should not emit updated for the PmcMedia QCOMPARE(mediaUpdatedSpy.size(), 0); } void MediaLibraryTest::shouldEmitUpdatedWhenAlbumOrArtistChanged() { - FakeMediaValidator validator; - MediaLibrary mediaLibrary(&validator); + MockObject validator; + MOCK_METHOD(validator, fileWithUrlExists).stubs().will(returnValue(true)); + + MediaLibrary mediaLibrary(validator); mediaLibrary.start(); QHash data = createTestMediaDataWithAlbumArtist(); QSignalSpy newMediaSpy(&mediaLibrary, SIGNAL(newMedia(QList< QSharedPointer >))); QVERIFY2(newMediaSpy.isValid(), "Could not listen to signal newMedia"); mediaLibrary.updateMedia(data); waitForSignal(&newMediaSpy); QCOMPARE(newMediaSpy.size(), 1); QList > returnedMedia = newMediaSpy.takeFirst().first().value< QList > >(); QCOMPARE(returnedMedia.size(), 1); QSharedPointer pmcMedia = returnedMedia.at(0); QSignalSpy mediaUpdatedSpy(pmcMedia.data(), SIGNAL(updated())); data.insert(MediaCenter::AlbumRole, "another_album"); mediaLibrary.updateMedia(data); //Should not emit newMedia waitForSignal(&newMediaSpy); QCOMPARE(newMediaSpy.size(), 0); //Should emit updated for the PmcMedia QCOMPARE(mediaUpdatedSpy.size(), 1); mediaUpdatedSpy.clear(); data.insert(MediaCenter::ArtistRole, "another_artist"); mediaLibrary.updateMedia(data); //Should not emit newMedia waitForSignal(&newMediaSpy); QCOMPARE(newMediaSpy.size(), 0); //Should emit updated for the PmcMedia QCOMPARE(mediaUpdatedSpy.size(), 1); } void MediaLibraryTest::shouldNotAddMediaForNonExistentFile() { MediaLibrary mediaLibrary; mediaLibrary.start(); QSignalSpy newMediaSpy(&mediaLibrary, SIGNAL(newMedia(QList< QSharedPointer >))); QVERIFY2(newMediaSpy.isValid(), "Could not listen to signal newMedia"); QHash data = createTestMediaDataWithAlbumArtist(); mediaLibrary.updateMedia(data); waitForSignal(&newMediaSpy, 6000); QCOMPARE(newMediaSpy.size(), 0); } void MediaLibraryTest::shouldCleanupEntriesForNonExistentMedia() { - FakeMediaValidator validator; - MediaLibrary *mediaLibrary = new MediaLibrary(&validator); + MockObject validator; + MOCK_METHOD(validator, fileWithUrlExists).stubs().will(returnValue(true)); + + MediaLibrary *mediaLibrary = new MediaLibrary(validator); mediaLibrary->start(); QSignalSpy newMediaSpy(mediaLibrary, SIGNAL(newMedia(QList< QSharedPointer >))); QVERIFY2(newMediaSpy.isValid(), "Could not listen to signal newMedia"); QHash data = createTestMediaDataWithAlbumArtist(); mediaLibrary->updateMedia(data); waitForSignal(&newMediaSpy); QCOMPARE(newMediaSpy.size(), 1); QList > returnedMedia = newMediaSpy.takeFirst().first().value< QList > >(); QCOMPARE(returnedMedia.size(), 1); delete mediaLibrary; mediaLibrary = new MediaLibrary(); QSignalSpy anotherNewMediaSpy(mediaLibrary, SIGNAL(newMedia(QList< QSharedPointer >))); QVERIFY2(anotherNewMediaSpy.isValid(), "Could not listen to signal newMedia"); mediaLibrary->start(); waitForSignal(&anotherNewMediaSpy); QCOMPARE(anotherNewMediaSpy.size(), 0); delete mediaLibrary; }