File: TestMetaMultiTrack.cpp

package info (click to toggle)
amarok 3.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,168 kB
  • sloc: cpp: 195,056; xml: 4,322; ansic: 2,634; javascript: 673; ruby: 528; python: 507; sh: 252; makefile: 12
file content (179 lines) | stat: -rw-r--r-- 6,994 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/***************************************************************************
 *   Copyright (c) 2009 Sven Krohlas <sven@asbest-online.de>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "TestMetaMultiTrack.h"

#include "core/support/Components.h"
#include "EngineController.h"
#include "config-amarok-test.h"
#include "core-impl/collections/support/CollectionManager.h"
#include "core-impl/meta/multi/MultiTrack.h"
#include "core-impl/playlists/types/file/PlaylistFileSupport.h"

#include <QDir>
#include <QFileInfo>
#include <QSignalSpy>
#include <QTest>
#include <QTimer>

#include <ThreadWeaver/Queue>

QTEST_GUILESS_MAIN( TestMetaMultiTrack )

TestMetaMultiTrack::TestMetaMultiTrack()
    : m_testMultiTrack( nullptr )
{
}

void
TestMetaMultiTrack::tracksLoaded( Playlists::PlaylistPtr playlist )
{
    Q_EMIT tracksLoadedSignal( playlist );
}

void TestMetaMultiTrack::initTestCase()
{
    qRegisterMetaType<Meta::TrackPtr>( "Meta::TrackPtr" );

    //apparently the engine controller is needed somewhere, or we will get a crash...
    EngineController *controller = new EngineController();
    Amarok::Components::setEngineController( controller );

    /* Collection manager needs to be instantiated in the main thread, but
     * MetaProxy::Tracks used by playlist may trigger its creation in a different thread.
     * Pre-create it explicitly */
    CollectionManager::instance();

    const QString path = QStringLiteral( AMAROK_TEST_DIR ) + QStringLiteral("/data/playlists/test.pls");
    const QFileInfo file( QDir::toNativeSeparators( path ) );
    QVERIFY( file.exists() );
    const QString filePath = file.absoluteFilePath();
    m_playlist = Playlists::loadPlaylistFile( QUrl::fromLocalFile(filePath) ).data();
    QVERIFY( m_playlist ); // no playlist -> no test. that's life ;)
    subscribeTo( m_playlist );
    m_playlist->triggerTrackLoad();
    QSignalSpy spy( this, &TestMetaMultiTrack::tracksLoadedSignal );
    if( m_playlist->trackCount() < 0 )
        QVERIFY( spy.wait( 5000 ) );

    QCOMPARE( m_playlist->name(), QStringLiteral("test.pls") );
    QCOMPARE( m_playlist->trackCount(), 4 );

    // now wait for all MetaProxy::Tracks to actually load their real tracks:
    Meta::TrackList tracks = m_playlist->tracks();
    NotifyObserversWaiter wainter( QSet<Meta::TrackPtr> ( tracks.begin(), tracks.end() ) );
    QSignalSpy spyDone( &wainter, &NotifyObserversWaiter::done );
    QVERIFY( spyDone.wait( 5000 ) );
}

void
TestMetaMultiTrack::cleanupTestCase()
{
    // Wait for other jobs, like MetaProxys fetching meta data, to finish
    ThreadWeaver::Queue::instance()->finish();
}

void TestMetaMultiTrack::init()
{
    m_testMultiTrack = new Meta::MultiTrack( m_playlist );
}

void TestMetaMultiTrack::cleanup()
{
    delete m_testMultiTrack;
}

void TestMetaMultiTrack::testSources()
{
    QStringList sources = m_testMultiTrack->sources();
    QCOMPARE( sources.size(), 4 );
    QCOMPARE( sources.at( 0 ), QStringLiteral( "http://85.214.44.27:8000" ) );
    QCOMPARE( sources.at( 1 ), QStringLiteral( "http://217.20.121.40:8000" ) );
    QCOMPARE( sources.at( 2 ), QStringLiteral( "http://85.214.44.27:8100" ) );
    QCOMPARE( sources.at( 3 ), QStringLiteral( "http://85.214.44.27:8200" ) );
}

void TestMetaMultiTrack::testSetSourceCurrentNextUrl()
{
    QCOMPARE( m_testMultiTrack->current(), 0 );
    QCOMPARE( m_testMultiTrack->playableUrl(), QUrl(QStringLiteral("http://85.214.44.27:8000")) );
    QCOMPARE( m_testMultiTrack->nextUrl(), QUrl(QStringLiteral("http://217.20.121.40:8000")) );

    m_testMultiTrack->setSource( 1 );
    QCOMPARE( m_testMultiTrack->current(), 1 );
    QCOMPARE( m_testMultiTrack->playableUrl(), QUrl(QStringLiteral("http://217.20.121.40:8000")) );
    QCOMPARE( m_testMultiTrack->nextUrl(), QUrl(QStringLiteral("http://85.214.44.27:8100")) );

    m_testMultiTrack->setSource( 2 );
    QCOMPARE( m_testMultiTrack->current(), 2 );
    QCOMPARE( m_testMultiTrack->playableUrl(), QUrl(QStringLiteral("http://85.214.44.27:8100")) );
    QCOMPARE( m_testMultiTrack->nextUrl(), QUrl(QStringLiteral("http://85.214.44.27:8200")) );

    m_testMultiTrack->setSource( 3 );
    QCOMPARE( m_testMultiTrack->current(), 3 );
    QCOMPARE( m_testMultiTrack->playableUrl(), QUrl(QStringLiteral("http://85.214.44.27:8200")) );
    QCOMPARE( m_testMultiTrack->nextUrl(), QUrl() );

    m_testMultiTrack->setSource( 4 );
    QCOMPARE( m_testMultiTrack->current(), 3 );
    m_testMultiTrack->setSource( -1 );
    QCOMPARE( m_testMultiTrack->current(), 3 );
}

void TestMetaMultiTrack::testHasCapabilityInterface()
{
    QVERIFY( m_testMultiTrack->hasCapabilityInterface( Capabilities::Capability::MultiSource ) );
}

NotifyObserversWaiter::NotifyObserversWaiter( const QSet<Meta::TrackPtr> &tracks, QObject *parent )
    : QObject( parent )
    , m_tracks( tracks )
{
    // we need to filter already resolved tracks in the next event loop iteration because
    // the user wouldn't be able to get the done() signal yet.
    QTimer::singleShot( 0, this, &NotifyObserversWaiter::slotFilterResolved );
}

void
NotifyObserversWaiter::slotFilterResolved()
{
    QMutexLocker locker( &m_mutex );
    QMutableSetIterator<Meta::TrackPtr> it( m_tracks );
    while( it.hasNext() )
    {
        const Meta::TrackPtr &track = it.next();
        const MetaProxy::Track *proxyTrack = dynamic_cast<const MetaProxy::Track *>( track.data() );
        Q_ASSERT( proxyTrack );
        if( proxyTrack->isResolved() )
            it.remove();
        else
            subscribeTo( track );
    }
    if( m_tracks.isEmpty() )
        Q_EMIT done();
}

void
NotifyObserversWaiter::metadataChanged( const Meta::TrackPtr &track )
{
    QMutexLocker locker( &m_mutex );
    m_tracks.remove( track );
    if( m_tracks.isEmpty() )
        Q_EMIT done();
}