1 /****************************************************************************************
2  * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz>                                      *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #include "TestEngineController.h"
18 
19 #include "EngineController.h"
20 #include "core/support/Components.h"
21 
22 #include <QTest>
23 #include <QSignalSpy>
24 #include <ThreadWeaver/Job>
25 #include <ThreadWeaver/Queue>
26 
27 
28 QTEST_GUILESS_MAIN( TestEngineController )
29 
30 class CallSupportedMimeTypesJob : public QObject, public ThreadWeaver::Job
31 {
32     Q_OBJECT
33 
34     protected:
run(ThreadWeaver::JobPointer self,ThreadWeaver::Thread * thread)35         void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread) override
36         {
37             Q_UNUSED(self);
38             Q_UNUSED(thread);
39             EngineController *ec = Amarok::Components::engineController();
40             QVERIFY( ec );
41             QStringList types = ec->supportedMimeTypes();
42             QVERIFY( !types.isEmpty() );
43         }
44 
defaultBegin(const ThreadWeaver::JobPointer & self,ThreadWeaver::Thread * thread)45         void defaultBegin(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread) override
46         {
47             Q_EMIT started(self);
48             ThreadWeaver::Job::defaultBegin(self, thread);
49         }
50 
defaultEnd(const ThreadWeaver::JobPointer & self,ThreadWeaver::Thread * thread)51         void defaultEnd(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread) override
52         {
53             ThreadWeaver::Job::defaultEnd(self, thread);
54             if (!self->success()) {
55                 Q_EMIT failed(self);
56             }
57             Q_EMIT done(self);
58         }
59 
60     Q_SIGNALS:
61         /** This signal is emitted when this job is being processed by a thread. */
62         void started(ThreadWeaver::JobPointer);
63         /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */
64         void done(ThreadWeaver::JobPointer);
65         /** This job has failed.
66          * This signal is emitted when success() returns false after the job is executed. */
67         void failed(ThreadWeaver::JobPointer);
68 
69 };
70 
71 void
init()72 TestEngineController::init()
73 {
74     // the test depend on EngineController being used for the first time
75     QVERIFY( Amarok::Components::engineController() == 0 );
76     Amarok::Components::setEngineController( new EngineController() );
77 }
78 
79 void
cleanup()80 TestEngineController::cleanup()
81 {
82     // we cannot simply call WeaverInterface::finish(), it stops event loop
83     QSignalSpy spy( ThreadWeaver::Queue::instance(), &ThreadWeaver::Queue::finished );
84     if( !ThreadWeaver::Queue::instance()->isIdle() )
85         QVERIFY2( spy.wait( 5000 ), "threads did not finish in timeout" );
86 
87     delete Amarok::Components::setEngineController( 0 );
88 }
89 
90 void
testSupportedMimeTypesInMainThread()91 TestEngineController::testSupportedMimeTypesInMainThread()
92 {
93     EngineController *ec = Amarok::Components::engineController();
94     QVERIFY( ec );
95     QStringList types = ec->supportedMimeTypes();
96     QVERIFY( !types.isEmpty() );
97 }
98 
99 void
testSupportedMimeTypesInAnotherThread()100 TestEngineController::testSupportedMimeTypesInAnotherThread()
101 {
102     ThreadWeaver::JobPointer job = QSharedPointer<ThreadWeaver::Job>(new CallSupportedMimeTypesJob());
103     ThreadWeaver::Queue::instance()->enqueue( job );
104 }
105 
106 #include "TestEngineController.moc"
107