File: main.cpp

package info (click to toggle)
medialibrary 0.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,820 kB
  • sloc: cpp: 49,393; sql: 26,604; ansic: 3,236; sh: 46; python: 36; makefile: 4
file content (299 lines) | stat: -rw-r--r-- 9,636 bytes parent folder | download
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015-2019 Hugo Beauzée-Luyssen, Videolabs, VideoLAN
 *
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
 *
 * This program 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 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 *****************************************************************************/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "Tester.h"
#include "medialibrary/filesystem/IDirectory.h"
#include "medialibrary/filesystem/IFile.h"
#include "utils/Filename.h"
#include "utils/Directory.h"
#include "Playlist.h"
#include "Media.h"
#include "File.h"
#include "Subscription.h"

#include <vlcpp/vlc.hpp>

static void Parse( Tests* T )
{
    T->m_cb->waitForParsingComplete();

    T->runChecks();
}

static void ParseTwice( Tests* T )
{
    T->m_cb->waitForParsingComplete();

    T->runChecks();

    if ( T->input.IsArray() )
    {
        T->m_cb->prepareForRemoval( T->input.Size() );
        for ( auto i = 0u; i < T->input.Size(); ++i )
        {
            auto samplesDir = Tests::Directory + "samples/" + T->input[i].GetString();
            samplesDir = utils::fs::toAbsolute( samplesDir );
            T->m_ml->removeRoot( utils::file::toMrl( samplesDir ) );
        }
        ASSERT_TRUE( T->m_cb->waitForRemovalComplete() );
        T->m_cb->reinit();
        for ( auto i = 0u; i < T->input.Size(); ++i )
        {
            auto samplesDir = Tests::Directory + "samples/" + T->input[i].GetString();
            samplesDir = utils::fs::toAbsolute( samplesDir );
            T->m_ml->discover( utils::file::toMrl( samplesDir ) );
        }
    }
    else

    if ( T->subscriptions.IsArray() )
    {
        auto service = T->m_ml->service( IService::Type::Podcast );
        auto subscriptions = service->subscriptions( nullptr )->all();
        for ( const auto& c : subscriptions )
            T->m_ml->removeSubscription( c->id() );
        /* Ensure we don't wait for a discovery that will not come */
        T->m_cb->onDiscoveryCompleted();
        for ( auto i = 0u; i < T->subscriptions.Size(); ++i )
        {
            auto& sub = T->subscriptions[i];
            ASSERT_TRUE( sub.HasMember( "service" ) && sub.HasMember( "mrl" ) );
            T->addSubscription( static_cast<IService::Type>( sub["service"].GetUint() ),
                    sub["mrl"].GetString() );
        }
    }

    T->m_cb->waitForParsingComplete();

    T->runChecks();
}

static void RunResumeTests( ResumeTests* T )
{
    T->m_cb->waitForDiscoveryComplete();
    auto testMl = static_cast<MediaLibraryResumeTest*>( T->m_ml.get() );
    testMl->forceParserStart();
    T->m_cb->waitForParsingComplete();

    T->runChecks();
}

static void Rescan( ResumeTests* T )
{
    T->m_cb->waitForDiscoveryComplete();
    auto testMl = static_cast<MediaLibraryResumeTest*>( T->m_ml.get() );
    testMl->forceParserStart();
    T->m_cb->waitForParsingComplete();

    T->m_cb->reinit();
    T->m_ml->forceRescan();
    T->m_cb->waitForParsingComplete();

    T->runChecks();
}

static void RunRefreshTests( RefreshTests* T )
{
    T->m_cb->waitForDiscoveryComplete();
    T->m_cb->waitForParsingComplete();

    T->runChecks();

    T->m_cb->reinit();
    T->forceRefresh();

    T->m_cb->waitForParsingComplete();

    T->runChecks();
}

static void ReplaceVlcInstance( Tests* T )
{
    VLC::Instance inst{ 0, nullptr };
    T->m_ml->setExternalLibvlcInstance( inst.get() );
    /* Replacing the instance will stop the discoverer so let's resume it */
    T->m_ml->reload();
    T->m_cb->waitForParsingComplete();

    T->runChecks();
}

static void RunBackupRestorePlaylist( BackupRestorePlaylistTests* T )
{
    auto samplesFolder = std::string{ SRC_DIR "/test/samples/samples/playlist/tracks" };
    ASSERT_TRUE( utils::fs::isDirectory( samplesFolder ) );
    samplesFolder = utils::fs::toAbsolute( samplesFolder );
    T->m_ml->discover( utils::file::toMrl( samplesFolder ) );
    T->m_cb->waitForParsingComplete();
    // Now we should have discovered some media

    auto media = T->m_ml->audioFiles( nullptr )->all();
    ASSERT_EQ( 3u, media.size() );

    auto pl1 = std::static_pointer_cast<Playlist>( T->m_ml->createPlaylist( "Exported Playlist 1" ) );
    auto m1 = media[0];
    auto m2 = media[1];
    auto m3 = T->m_ml->addExternalMedia( "http://example.org/sea&ottér.avi", -1 );
    pl1->append( *m1 );
    pl1->append( *m2 );
    pl1->append( *m3 );

    auto pl2 = std::static_pointer_cast<Playlist>( T->m_ml->createPlaylist( "Exported Playlist <2>" ) );
    pl2->append( *m3 );
    pl2->append( *m2 );
    pl2->append( *m1 );

    auto backup = Playlist::backupPlaylists( static_cast<MediaLibrary*>( T->m_ml.get() ),
                                             Settings::DbModelVersion );
    ASSERT_TRUE( std::get<0>( backup ) );

    T->m_cb->prepareForPlaylistReload();
    T->m_ml->clearDatabase( true );

    T->m_cb->waitForPlaylistReload();

    auto playlists = T->m_ml->playlists( PlaylistType::All, nullptr )->all();
    ASSERT_EQ( 2u, playlists.size() );
    auto playlist1 = playlists[0];
    media = playlist1->media( nullptr )->all();
    ASSERT_EQ( 3u, media.size() );
    ASSERT_EQ( "Exported Playlist 1", playlist1->name() );

    auto playlist2 = playlists[1];
    media = playlist2->media( nullptr )->all();
    ASSERT_EQ( 3u, media.size() );
    ASSERT_EQ( "Exported Playlist <2>", playlist2->name() );

    /*
     * Since the folder isn't discovered yet, the media won't be preparsed and
     * won't have their duration or title.
     * However if we discover those media again, the media should be analyzed and
     * converted back to internal media, meaning they'll recover their titles
     * and duration among other information.
     */
    T->m_cb->reinit();
    T->m_ml->discover( utils::file::toMrl( samplesFolder ) );
    T->m_cb->waitForParsingComplete();
    media = playlist1->media( nullptr )->all();
    ASSERT_EQ( m1->title(), media[0]->title() );
    ASSERT_EQ( m2->title(), media[1]->title() );
    ASSERT_EQ( m3->title(), media[2]->title() );

    media = playlist2->media( nullptr )->all();
    ASSERT_EQ( m3->title(), media[0]->title() );
    ASSERT_EQ( m2->title(), media[1]->title() );
    ASSERT_EQ( m1->title(), media[2]->title() );
}

static void ReplaceExternalMediaByPlaylist( ReplaceExternalMediaByPlaylistTests* T )
{
    /*
     * This test was initialized with a playlist inserted as an external media.
     * From there, just run the same test as the ParseTwice suite to check if we
     * can recover properly and insert the playlist as an actual playlist
     */
    ParseTwice( T );
    /*
     * Initially the playlist was inserted with the same MRL as the media, ensure
     * this is not the case anymore
     */
    auto mlPtr = static_cast<MediaLibrary*>( T->m_ml.get() );
    auto file = File::fromMrl( mlPtr, T->playlistMrl );
    ASSERT_NON_NULL( file );
    ASSERT_EQ( IFile::Type::Playlist, file->type() );
    auto res = file->destroy();
    ASSERT_TRUE( res );
    /* Ensure there's no other file with the same MRL */
    file = File::fromMrl( mlPtr, T->playlistMrl );
    ASSERT_EQ( nullptr, file );
    file = File::fromExternalMrl( mlPtr, T->playlistMrl );
    ASSERT_EQ( nullptr, file );
}

#define RUN_TEST( Type, Func ) \
    try \
    { \
        auto T = std::make_unique<Type>(); \
        T->SetUp( testType, testName ); \
        Func( T.get() ); \
        T->TearDown(); \
        return 0; \
    } catch ( const TestFailed& tf ) { \
        fprintf( stderr, "Test %s.%s failed: %s\n", testType.c_str(), \
                 testName.c_str(), tf.what() ); \
        return 2; \
    }

int main(int ac, char** av)
{
    std::string testType;
    std::string testName;
    if ( ac != 3 )
    {
        fprintf( stderr, "usage: %s <test type> <test name>\n", av[0] );
        return 1;
    }
    testType = av[1];
    testName = av[2];
    if ( testType == "Parse" )
    {
        RUN_TEST( Tests, Parse );
    }
    else if ( testType == "ParseTwice" )
    {
        RUN_TEST( Tests, ParseTwice );
    }
    else if ( testType == "Resume" )
    {
        RUN_TEST( ResumeTests, RunResumeTests );
    }
    else if ( testType == "Rescan" )
    {
        RUN_TEST( ResumeTests, Rescan );
    }
    else if ( testType == "Refresh" )
    {
        RUN_TEST( RefreshTests, RunRefreshTests );
    }
    else if ( testType == "ReplaceVlcInstance" )
    {
        RUN_TEST( Tests, ReplaceVlcInstance );
    }
    else if ( testType == "BackupRestorePlaylist" )
    {
        RUN_TEST( BackupRestorePlaylistTests, RunBackupRestorePlaylist );
    }
    else if ( testType == "ReplaceExternalMediaByPlaylist" )
    {
        RUN_TEST( ReplaceExternalMediaByPlaylistTests, ReplaceExternalMediaByPlaylist );
    }
    else
    {
        assert( !"Invalid test type" );
        return 1;
    }
    return 1;
}