File indexing completed on 2024-05-12 05:40:46

0001 /***************************************************************************
0002  *  Copyright (C) 2019 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include <QtTest/QtTest>
0021 
0022 #include <QModelIndex>
0023 #include <QModelIndexList>
0024 #include <QtCore/QString>
0025 #include <memory>
0026 
0027 #include "controller/audiocontroller.h"
0028 #include "controller/audioplayercontroller.h"
0029 #include "rwidgets/customs/playerwidget.h"
0030 #include "model/musicmodel.h"
0031 #include "test_root_path.h"
0032 
0033 constexpr int TIME_SONG{36};
0034 
0035 class TestAudioPlayer : public QObject
0036 {
0037     Q_OBJECT
0038 public:
0039     TestAudioPlayer();
0040 
0041 private slots:
0042     void init();
0043 
0044     void mode();
0045 
0046     void property_audio();
0047 
0048     void addSongTest();
0049     void addSongTest_data();
0050 
0051     void playSong();
0052     void playSong_data();
0053 
0054     void playSongInLoop();
0055     void playSongInLoop_data();
0056 
0057 
0058     void playerWidget();
0059 
0060 private:
0061     std::unique_ptr<AudioPlayerController> m_audioController;
0062     std::unique_ptr<AudioController> m_audiosCtrl;
0063 };
0064 
0065 TestAudioPlayer::TestAudioPlayer() {}
0066 
0067 void TestAudioPlayer::init()
0068 {
0069     m_audioController.reset(new AudioPlayerController(0, "", nullptr));
0070     m_audioController->setLocalIsGm(true);
0071     m_audiosCtrl.reset(new AudioController(nullptr, nullptr));
0072     m_audiosCtrl->setLocalIsGM(true);
0073 }
0074 
0075 void TestAudioPlayer::playerWidget()
0076 {
0077     PlayerWidget playerWidget(m_audioController.get());
0078 
0079     QMenu menu;
0080     playerWidget.addActionsIntoMenu(&menu);
0081 
0082     auto listAct = menu.actions();
0083 
0084     /*for(auto act: listAct)
0085     {
0086         //act->trigger();
0087     }*/
0088 }
0089 
0090 void TestAudioPlayer::mode()
0091 {
0092     m_audioController->addSong(
0093         {QUrl("qrc:/music/07.mp3"),
0094          QUrl::fromUserInput(QString("file://%1/resources/%2").arg(tests::root_path, "break.mp3")),
0095          QUrl::fromUserInput(QString("file://%1/resources/%2").arg(tests::root_path, "quickFixes.mp3")),
0096          QUrl::fromUserInput(QString("file://%1/resources/%2").arg(tests::root_path, "taskFailed.mp3")),
0097          QUrl::fromUserInput(QString("file://%1/resources/%2").arg(tests::root_path, "taskCompleted.mp3")),
0098          QUrl::fromUserInput(QString("file://%1/resources/%2").arg(tests::root_path, "warning.mp3")),
0099          QUrl::fromUserInput(QString("file://%1/resources/%2").arg(tests::root_path, "error.mp3"))});
0100     auto model= m_audioController->model();
0101     m_audioController->setPlayingMode(AudioPlayerController::PlayingMode::NEXT);
0102     QSignalSpy spy(m_audioController.get(), &AudioPlayerController::stateChanged);
0103     m_audioController->setMedia(model->index(0, 0));
0104     QCOMPARE(model->indexOfCurrent(), 0);
0105     m_audioController->play();
0106     m_audioController->text();
0107 
0108     m_audioController->exportList(QString("file://%1/%2").arg(tests::root_path, "playlist.m3u"));
0109 
0110     m_audioController->mute();
0111     QVERIFY(m_audioController->muted());
0112 
0113     m_audioController->setVolume(20);
0114     QCOMPARE(m_audioController->volume(), 20);
0115 
0116     m_audioController->error();
0117 
0118     m_audioController->setVisible(true);
0119     m_audioController->setVisible(false);
0120     QVERIFY(!m_audioController->visible());
0121     m_audioController->setVisible(true);
0122     QVERIFY(m_audioController->visible());
0123 
0124     m_audioController->setTime(m_audioController->time() + 10);
0125     m_audioController->id();
0126     QCOMPARE(m_audioController->mode(), AudioPlayerController::PlayingMode::NEXT);
0127     spy.wait(TIME_SONG * 400);
0128 
0129     auto arg= spy.takeLast();
0130 
0131     QCOMPARE(arg[0].toInt(), AudioPlayerController::PlayingState);
0132     QCOMPARE(m_audioController->state(), AudioPlayerController::PlayingState);
0133 
0134     spy.clear();
0135     m_audioController->next();
0136     QVERIFY(model->indexOfCurrent() == 1);
0137     m_audioController->setPlayingMode(AudioPlayerController::PlayingMode::LOOP);
0138 
0139     spy.wait(TIME_SONG);
0140     spy.wait(TIME_SONG);
0141 
0142     arg= spy.takeLast();
0143 
0144     QCOMPARE(arg[0].toInt(), AudioPlayerController::PlayingState);
0145 
0146     m_audioController->next();
0147     QVERIFY(model->indexOfCurrent() == 1);
0148 
0149     spy.clear();
0150     m_audioController->pause();
0151     spy.wait(TIME_SONG);
0152 
0153     arg= spy.takeLast();
0154 
0155     QCOMPARE(arg[0].toInt(), AudioPlayerController::PausedState);
0156 
0157     m_audioController->setPlayingMode(AudioPlayerController::PlayingMode::UNIQUE);
0158     m_audioController->play();
0159 
0160     spy.wait(TIME_SONG);
0161     m_audioController->next();
0162     spy.wait(TIME_SONG);
0163 
0164     QCOMPARE(m_audioController->state(), AudioPlayerController::StoppedState);
0165 
0166     m_audioController->setPlayingMode(AudioPlayerController::PlayingMode::SHUFFLE);
0167     m_audioController->next();
0168 
0169     spy.wait(TIME_SONG * 10);
0170     m_audioController->next();
0171     spy.wait(TIME_SONG * 10);
0172     m_audioController->next();
0173 
0174     m_audioController->removeSong({model->index(0, 0)});
0175 
0176     m_audioController->nwNewSong("taskCompleted.mp3", 0);
0177     m_audioController->setLocalIsGm(false);
0178     m_audioController->nwNewSong("taskCompleted.mp3", 0);
0179 
0180     m_audioController->clear();
0181     m_audioController->next();
0182 
0183     m_audioController->loadPlayList(QString("file://%1/%2").arg(tests::root_path, "playlist.m3u"));
0184 }
0185 
0186 void TestAudioPlayer::property_audio()
0187 {
0188     QVERIFY(m_audiosCtrl->localIsGM());
0189     QVERIFY(m_audiosCtrl->firstController() != nullptr);
0190     QVERIFY(m_audiosCtrl->secondController() != nullptr);
0191     QVERIFY(m_audiosCtrl->thirdController() != nullptr);
0192 
0193     m_audiosCtrl->setLocalIsGM(true);
0194     m_audiosCtrl->setLocalIsGM(false);
0195 
0196     QVERIFY(!m_audiosCtrl->firstController()->localIsGm());
0197     QVERIFY(!m_audiosCtrl->secondController()->localIsGm());
0198     QVERIFY(!m_audiosCtrl->thirdController()->localIsGm());
0199 }
0200 
0201 void TestAudioPlayer::addSongTest()
0202 {
0203     QFETCH(QList<QUrl>, songs);
0204     QFETCH(int, expected);
0205 
0206     QVERIFY(m_audioController->localIsGm());
0207 
0208     m_audioController->addSong(songs);
0209 
0210     auto model= m_audioController->model();
0211     QCOMPARE(model->rowCount(), expected);
0212 }
0213 
0214 void TestAudioPlayer::addSongTest_data()
0215 {
0216     QTest::addColumn<QList<QUrl>>("songs");
0217     QTest::addColumn<int>("expected");
0218 
0219     QTest::addRow("list1") << QList<QUrl>() << 0;
0220     QTest::addRow("list2") << QList<QUrl>({QUrl("song")}) << 1;
0221     QTest::addRow("list3") << QList<QUrl>({QUrl("song1"), QUrl("song2")}) << 2;
0222     QTest::addRow("list4") << QList<QUrl>({QUrl("song1"), QUrl("song2"), QUrl("song3")}) << 3;
0223 }
0224 
0225 void TestAudioPlayer::playSong()
0226 {
0227     QFETCH(QList<QUrl>, songs);
0228     QFETCH(int, expected);
0229 
0230     m_audioController->addSong(songs);
0231 
0232     auto model= m_audioController->model();
0233     QCOMPARE(model->rowCount(), expected);
0234 
0235     QSignalSpy spy(m_audioController.get(), &AudioPlayerController::stateChanged);
0236     QSignalSpy spy2(m_audioController.get(), &AudioPlayerController::startPlayingSong);
0237     m_audioController->setMedia(model->index(0));
0238     m_audioController->play();
0239 
0240     spy.wait();
0241     spy2.wait();
0242     QCOMPARE(spy.count(), expected);
0243     QCOMPARE(spy2.count(), expected);
0244 }
0245 
0246 void TestAudioPlayer::playSong_data()
0247 {
0248     QTest::addColumn<QList<QUrl>>("songs");
0249     QTest::addColumn<int>("expected");
0250 
0251     QTest::addRow("list1") << QList<QUrl>({QUrl("qrc:/music/07.mp3")}) << 1;
0252 }
0253 
0254 void TestAudioPlayer::playSongInLoop()
0255 {
0256     QFETCH(QList<QUrl>, songs);
0257     QFETCH(int, expected);
0258 
0259     m_audioController->setPlayingMode(AudioPlayerController::LOOP);
0260 
0261     m_audioController->addSong(songs);
0262 
0263     auto model= m_audioController->model();
0264     QCOMPARE(model->rowCount(), expected);
0265 
0266     QSignalSpy spy(m_audioController.get(), &AudioPlayerController::stateChanged);
0267     QSignalSpy spy2(m_audioController.get(), &AudioPlayerController::startPlayingSong);
0268     m_audioController->setMedia(model->index(0));
0269     m_audioController->play();
0270 
0271     spy.wait((TIME_SONG + 10) * 1000);
0272     spy2.wait();
0273     QCOMPARE(spy.count(), expected + 2);
0274     QCOMPARE(spy2.count(), expected + 2);
0275 }
0276 void TestAudioPlayer::playSongInLoop_data()
0277 {
0278     QTest::addColumn<QList<QUrl>>("songs");
0279     QTest::addColumn<int>("expected");
0280 
0281     QTest::addRow("list1") << QList<QUrl>({QUrl("qrc:/music/07.mp3")}) << 1;
0282 }
0283 QTEST_MAIN(TestAudioPlayer);
0284 
0285 #include "tst_audioplayer.moc"