File indexing completed on 2024-04-14 04:43:08

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz>                                      *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "TestEngineController.h"
0018 
0019 #include "EngineController.h"
0020 #include "core/support/Components.h"
0021 
0022 #include <QTest>
0023 #include <QSignalSpy>
0024 #include <ThreadWeaver/Job>
0025 #include <ThreadWeaver/Queue>
0026 
0027 
0028 QTEST_GUILESS_MAIN( TestEngineController )
0029 
0030 class CallSupportedMimeTypesJob : public QObject, public ThreadWeaver::Job
0031 {
0032     Q_OBJECT
0033 
0034     protected:
0035         void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread) override
0036         {
0037             Q_UNUSED(self);
0038             Q_UNUSED(thread);
0039             EngineController *ec = Amarok::Components::engineController();
0040             QVERIFY( ec );
0041             QStringList types = ec->supportedMimeTypes();
0042             QVERIFY( !types.isEmpty() );
0043         }
0044 
0045         void defaultBegin(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread) override
0046         {
0047             Q_EMIT started(self);
0048             ThreadWeaver::Job::defaultBegin(self, thread);
0049         }
0050 
0051         void defaultEnd(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread) override
0052         {
0053             ThreadWeaver::Job::defaultEnd(self, thread);
0054             if (!self->success()) {
0055                 Q_EMIT failed(self);
0056             }
0057             Q_EMIT done(self);
0058         }
0059 
0060     Q_SIGNALS:
0061         /** This signal is emitted when this job is being processed by a thread. */
0062         void started(ThreadWeaver::JobPointer);
0063         /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */
0064         void done(ThreadWeaver::JobPointer);
0065         /** This job has failed.
0066          * This signal is emitted when success() returns false after the job is executed. */
0067         void failed(ThreadWeaver::JobPointer);
0068 
0069 };
0070 
0071 void
0072 TestEngineController::init()
0073 {
0074     // the test depend on EngineController being used for the first time
0075     QVERIFY( Amarok::Components::engineController() == nullptr );
0076     Amarok::Components::setEngineController( new EngineController() );
0077 }
0078 
0079 void
0080 TestEngineController::cleanup()
0081 {
0082     // we cannot simply call WeaverInterface::finish(), it stops event loop
0083     QSignalSpy spy( ThreadWeaver::Queue::instance(), &ThreadWeaver::Queue::finished );
0084     if( !ThreadWeaver::Queue::instance()->isIdle() )
0085         QVERIFY2( spy.wait( 5000 ), "threads did not finish in timeout" );
0086 
0087     delete Amarok::Components::setEngineController( nullptr );
0088 }
0089 
0090 void
0091 TestEngineController::testSupportedMimeTypesInMainThread()
0092 {
0093     EngineController *ec = Amarok::Components::engineController();
0094     QVERIFY( ec );
0095     QStringList types = ec->supportedMimeTypes();
0096     QVERIFY( !types.isEmpty() );
0097 }
0098 
0099 void
0100 TestEngineController::testSupportedMimeTypesInAnotherThread()
0101 {
0102     ThreadWeaver::JobPointer job = QSharedPointer<ThreadWeaver::Job>(new CallSupportedMimeTypesJob());
0103     ThreadWeaver::Queue::instance()->enqueue( job );
0104 }
0105 
0106 #include "TestEngineController.moc"