MediaTests.cpp 18.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * 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.
 *****************************************************************************/

23
24
25
26
#if HAVE_CONFIG_H
# include "config.h"
#endif

27
#include "Tests.h"
28

29
#include "medialibrary/IMediaLibrary.h"
30
#include "File.h"
31
#include "Media.h"
32
33
34
#include "Artist.h"
#include "Album.h"
#include "AlbumTrack.h"
35
#include "mocks/FileSystem.h"
36
#include "mocks/DiscovererCbMock.h"
37
#include "compat/Thread.h"
38

39
class Medias : public Tests
40
41
42
43
{
};


44
TEST_F( Medias, Init )
45
46
47
48
{
    // only test for correct test fixture behavior
}

49
TEST_F( Medias, Create )
50
{
51
52
53
54
55
56
57
58
    auto m = ml->addFile( "media.avi" );
    ASSERT_NE( m, nullptr );

    ASSERT_EQ( m->playCount(), 0 );
    ASSERT_EQ( m->albumTrack(), nullptr );
    ASSERT_EQ( m->showEpisode(), nullptr );
    ASSERT_EQ( m->duration(), -1 );
    ASSERT_NE( 0u, m->insertionDate() );
59
60
61
62
63

    auto files = m->files();
    ASSERT_EQ( 1u, files.size() );
    auto f = files[0];
    ASSERT_FALSE( f->isExternal() );
64
    ASSERT_EQ( File::Type::Main, f->type() );
65
66
}

67
TEST_F( Medias, Fetch )
68
{
69
    auto f = ml->addMedia( "media.avi" );
70
71
    auto f2 = ml->media( f->id() );
    ASSERT_EQ( f->id(), f2->id() );
72
    ASSERT_EQ( f, f2 );
73
74

    // Flush cache and fetch from DB
75
    Reload();
76

77
    f2 = std::static_pointer_cast<Media>( ml->media( f->id() ) );
78
    ASSERT_EQ( f->id(), f2->id() );
79
}
80

81
TEST_F( Medias, Duration )
82
{
83
    auto f = std::static_pointer_cast<Media>( ml->addMedia( "media.avi" ) );
84
    ASSERT_EQ( f->duration(), -1 );
85

86
87
88
89
    // Use a value that checks we're using a 64bits value
    int64_t d = int64_t(1) << 40;

    f->setDuration( d );
90
    f->save();
91
    ASSERT_EQ( f->duration(), d );
92
93
94

    Reload();

95
    auto f2 = ml->media( f->id() );
96
    ASSERT_EQ( f2->duration(), d );
97
}
98

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
99
TEST_F( Medias, Thumbnail )
100
{
101
    auto f = std::static_pointer_cast<Media>( ml->addMedia( "media.avi" ) );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
102
    ASSERT_EQ( f->thumbnail(), "" );
103

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
104
    std::string newThumbnail( "/path/to/thumbnail" );
105

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
106
    f->setThumbnail( newThumbnail );
107
    f->save();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
108
    ASSERT_EQ( f->thumbnail(), newThumbnail );
109
110
111

    Reload();

112
    auto f2 = ml->media( f->id() );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
113
    ASSERT_EQ( f2->thumbnail(), newThumbnail );
114
}
115
116
117

TEST_F( Medias, PlayCount )
{
118
    auto f = std::static_pointer_cast<Media>( ml->addMedia( "media.avi" ) );
119
120
121
122
123
124
    ASSERT_EQ( 0, f->playCount() );
    f->increasePlayCount();
    ASSERT_EQ( 1, f->playCount() );

    Reload();

125
    f = std::static_pointer_cast<Media>( ml->media( f->id() ) );
126
127
    ASSERT_EQ( 1, f->playCount() );
}
128
129
130

TEST_F( Medias, Progress )
{
131
    auto f = std::static_pointer_cast<Media>( ml->addMedia( "media.avi" ) );
132
133
134
    ASSERT_EQ( 0, f->metadata( Media::MetadataType::Progress ).integer() );
    f->setMetadata( Media::MetadataType::Progress, 123 );
    ASSERT_EQ( 123, f->metadata( Media::MetadataType::Progress ).integer() );
135
    ASSERT_TRUE( f->metadata( Media::MetadataType::Progress ).isSet() );
136
137
138
139

    Reload();

    f = ml->media( f->id() );
140
    ASSERT_EQ( 123, f->metadata( Media::MetadataType::Progress ).integer() );
141
142
143
144
}

TEST_F( Medias, Rating )
{
145
    auto f = std::static_pointer_cast<Media>( ml->addMedia( "media.avi" ) );
146
147
148
    ASSERT_FALSE( f->metadata( Media::MetadataType::Rating ).isSet() );
    f->setMetadata( Media::MetadataType::Rating, 12345 );
    ASSERT_EQ( 12345, f->metadata( Media::MetadataType::Rating ).integer() );
149
    ASSERT_TRUE( f->metadata( Media::MetadataType::Rating ).isSet() );
150
151
152
153

    Reload();

    f = ml->media( f->id() );
154
    ASSERT_EQ( 12345, f->metadata( Media::MetadataType::Rating ).integer() );
155
}
156
157
158
159
160

TEST_F( Medias, Search )
{
    for ( auto i = 1u; i <= 10u; ++i )
    {
161
        ml->addMedia( "track " + std::to_string( i ) + ".mp3" );
162
    }
163
    auto media = ml->searchMedia( "tra", SortingCriteria::Default, false ).others;
164
165
    ASSERT_EQ( 10u, media.size() );

166
    media = ml->searchMedia( "track 1", SortingCriteria::Default, false ).others;
167
168
    ASSERT_EQ( 2u, media.size() );

169
    media = ml->searchMedia( "grouik", SortingCriteria::Default, false ).others;
170
171
    ASSERT_EQ( 0u, media.size() );

172
    media = ml->searchMedia( "rack", SortingCriteria::Default, false ).others;
173
174
175
    ASSERT_EQ( 0u, media.size() );
}

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
TEST_F( Medias, SearchAndSort )
{
    for ( auto i = 1u; i <= 3u; ++i )
    {
        auto m = std::static_pointer_cast<Media>(
                    ml->addMedia( "track " + std::to_string( i ) + ".mp3" ) );
        m->setDuration( 3 - i );
        m->save();
    }
    ml->addMedia( "this pattern doesn't match.mp3" );

    auto media = ml->searchMedia( "tra", SortingCriteria::Default, false ).others;
    ASSERT_EQ( 3u, media.size() );
    ASSERT_EQ( media[0]->title(), "track 1.mp3" );
    ASSERT_EQ( media[1]->title(), "track 2.mp3" );
    ASSERT_EQ( media[2]->title(), "track 3.mp3" );

    media = ml->searchMedia( "tra", SortingCriteria::Duration, false ).others;
    ASSERT_EQ( 3u, media.size() );
    ASSERT_EQ( media[0]->title(), "track 3.mp3" );
    ASSERT_EQ( media[1]->title(), "track 2.mp3" );
    ASSERT_EQ( media[2]->title(), "track 1.mp3" );
}

200
201
TEST_F( Medias, SearchAfterEdit )
{
202
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "media.mp3" ) );
203

204
    auto media = ml->searchMedia( "media", SortingCriteria::Default, false ).others;
205
206
    ASSERT_EQ( 1u, media.size() );

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
207
    m->setTitleBuffered( "otters are awesome" );
208
209
    m->save();

210
    media = ml->searchMedia( "media", SortingCriteria::Default, false ).others;
211
212
    ASSERT_EQ( 0u, media.size() );

213
    media = ml->searchMedia( "otters", SortingCriteria::Default, false ).others;
214
215
216
217
218
    ASSERT_EQ( 1u, media.size() );
}

TEST_F( Medias, SearchAfterDelete )
{
219
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "media.mp3" ) );
220

221
    auto media = ml->searchMedia( "media", SortingCriteria::Default, false ).others;
222
223
224
225
226
    ASSERT_EQ( 1u, media.size() );

    auto f = m->files()[0];
    m->removeFile( static_cast<File&>( *f ) );

227
    media = ml->searchMedia( "media", SortingCriteria::Default, false ).others;
228
229
    ASSERT_EQ( 0u, media.size() );
}
230
231
232

TEST_F( Medias, SearchByLabel )
{
233
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "media.mkv" ) );
234
    auto media = ml->searchMedia( "otter", SortingCriteria::Default, false ).others;
235
236
237
238
239
    ASSERT_EQ( 0u, media.size() );

    auto l = ml->createLabel( "otter" );
    m->addLabel( l );

240
    media = ml->searchMedia( "otter", SortingCriteria::Default, false ).others;
241
242
243
244
245
    ASSERT_EQ( 1u, media.size() );

    auto l2 = ml->createLabel( "pangolins" );
    m->addLabel( l2 );

246
    media = ml->searchMedia( "otter", SortingCriteria::Default, false ).others;
247
248
    ASSERT_EQ( 1u, media.size() );

249
    media = ml->searchMedia( "pangolin", SortingCriteria::Default, false ).others;
250
251
252
253
    ASSERT_EQ( 1u, media.size() );

    m->removeLabel( l );

254
    media = ml->searchMedia( "otter", SortingCriteria::Default, false ).others;
255
256
    ASSERT_EQ( 0u, media.size() );

257
    media = ml->searchMedia( "pangolin", SortingCriteria::Default, false ).others;
258
259
260
261
    ASSERT_EQ( 1u, media.size() );

    m->addLabel( l );

262
    media = ml->searchMedia( "otter", SortingCriteria::Default, false ).others;
263
264
    ASSERT_EQ( 1u, media.size() );

265
    media = ml->searchMedia( "pangolin", SortingCriteria::Default, false ).others;
266
267
268
269
    ASSERT_EQ( 1u, media.size() );

    ml->deleteLabel( l );

270
    media = ml->searchMedia( "otter", SortingCriteria::Default, false ).others;
271
272
    ASSERT_EQ( 0u, media.size() );

273
    media = ml->searchMedia( "pangolin", SortingCriteria::Default, false ).others;
274
275
    ASSERT_EQ( 1u, media.size() );
}
276
277
278
279
280
281

TEST_F( Medias, SearchTracks )
{
    auto a = ml->createAlbum( "album" );
    for ( auto i = 1u; i <= 10u; ++i )
    {
282
       auto m = std::static_pointer_cast<Media>( ml->addMedia( "track " + std::to_string( i ) + ".mp3" ) );
283
       a->addTrack( m, i, 1, 0, 0 );
284
    }
285
    auto tracks = ml->searchMedia( "tra", SortingCriteria::Default, false ).tracks;
286
287
    ASSERT_EQ( 10u, tracks.size() );

288
    tracks = ml->searchMedia( "track 1", SortingCriteria::Default, false ).tracks;
289
290
    ASSERT_EQ( 2u, tracks.size() );

291
    tracks = ml->searchMedia( "grouik", SortingCriteria::Default, false ).tracks;
292
293
    ASSERT_EQ( 0u, tracks.size() );

294
    tracks = ml->searchMedia( "rack", SortingCriteria::Default, false ).tracks;
295
296
    ASSERT_EQ( 0u, tracks.size() );
}
297
298
299

TEST_F( Medias, Favorite )
{
300
    auto m = ml->addMedia( "media.mkv" );
301
302
303
304
305
306
307
308
309
310
    ASSERT_FALSE( m->isFavorite() );

    m->setFavorite( true );
    ASSERT_TRUE( m->isFavorite() );

    Reload();

    m = ml->media( m->id() );
    ASSERT_TRUE( m->isFavorite() );
}
311
312
313

TEST_F( Medias, History )
{
314
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "media.mkv" ) );
315
316
317
318
319
320
321
322
323
324

    auto history = ml->lastMediaPlayed();
    ASSERT_EQ( 0u, history.size() );

    m->increasePlayCount();
    m->save();
    history = ml->lastMediaPlayed();
    ASSERT_EQ( 1u, history.size() );
    ASSERT_EQ( m->id(), history[0]->id() );

325
    compat::this_thread::sleep_for( std::chrono::seconds{ 1 } );
326
    auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "media2.mkv" ) );
327
328
329
330
331
332
333
    m2->increasePlayCount();

    history = ml->lastMediaPlayed();
    ASSERT_EQ( 2u, history.size() );
    ASSERT_EQ( m2->id(), history[0]->id() );
    ASSERT_EQ( m->id(), history[1]->id() );
}
334

335
336
TEST_F( Medias, ClearHistory )
{
337
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "media.mkv" ) );
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

    auto history = ml->lastMediaPlayed();
    ASSERT_EQ( 0u, history.size() );

    m->increasePlayCount();
    m->save();
    history = ml->lastMediaPlayed();
    ASSERT_EQ( 1u, history.size() );

    ASSERT_TRUE( ml->clearHistory() );

    history = ml->lastMediaPlayed();
    ASSERT_EQ( 0u, history.size() );

    Reload();

    history = ml->lastMediaPlayed();
    ASSERT_EQ( 0u, history.size() );
}

358
359
TEST_F( Medias, SetReleaseDate )
{
360
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "movie.mkv" ) );
361
362
363
364
365
366
367
368
369
370
371

    ASSERT_EQ( m->releaseDate(), 0u );
    m->setReleaseDate( 1234 );
    m->save();
    ASSERT_EQ( m->releaseDate(), 1234u );

    Reload();

    auto m2 = ml->media( m->id() );
    ASSERT_EQ( m2->releaseDate(), 1234u );
}
372
373
374

TEST_F( Medias, SortByAlpha )
{
375
    auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "media1.mp3" ) );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
376
    m1->setTitleBuffered( "Abcd" );
377
    m1->setType( Media::Type::Audio );
378
379
    m1->save();

380
    auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "media2.mp3" ) );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
381
    m2->setTitleBuffered( "Zyxw" );
382
    m2->setType( Media::Type::Audio );
383
384
    m2->save();

385
    auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "media3.mp3" ) );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
386
    m3->setTitleBuffered( "afterA-beforeZ" );
387
    m3->setType( Media::Type::Audio );
388
389
    m3->save();

390
    auto media = ml->audioFiles( SortingCriteria::Alpha, false );
391
    ASSERT_EQ( 3u, media.size() );
392
    ASSERT_EQ( m1->id(), media[0]->id() );
393
394
    ASSERT_EQ( m3->id(), media[1]->id() );
    ASSERT_EQ( m2->id(), media[2]->id() );
395

396
    media = ml->audioFiles( SortingCriteria::Alpha, true );
397
    ASSERT_EQ( 3u, media.size() );
398
    ASSERT_EQ( m2->id(), media[0]->id() );
399
400
    ASSERT_EQ( m3->id(), media[1]->id() );
    ASSERT_EQ( m1->id(), media[2]->id() );
401
402
403
404
405
406
}

TEST_F( Medias, SortByLastModifDate )
{
    auto file1 = std::make_shared<mock::NoopFile>( "media.mkv" );
    file1->setLastModificationDate( 666 );
407
    auto m1 = ml->addFile( file1 );
408
    m1->setType( Media::Type::Video );
409
    m1->save();
410
411
412

    auto file2 = std::make_shared<mock::NoopFile>( "media2.mkv" );
    file2->setLastModificationDate( 111 );
413
    auto m2 = ml->addFile( file2 );
414
    m2->setType( Media::Type::Video );
415
    m2->save();
416

417
    auto media = ml->videoFiles( SortingCriteria::LastModificationDate, false );
418
419
420
421
    ASSERT_EQ( 2u, media.size() );
    ASSERT_EQ( m2->id(), media[0]->id() );
    ASSERT_EQ( m1->id(), media[1]->id() );

422
    media = ml->videoFiles( SortingCriteria::LastModificationDate, true );
423
424
425
426
    ASSERT_EQ( 2u, media.size() );
    ASSERT_EQ( m2->id(), media[1]->id() );
    ASSERT_EQ( m1->id(), media[0]->id() );
}
427

428
429
430
431
TEST_F( Medias, SortByFileSize )
{
    auto file1 = std::make_shared<mock::NoopFile>( "media.mkv" );
    file1->setSize( 666 );
432
    auto m1 = ml->addFile( file1 );
433
    m1->setType( Media::Type::Video );
434
    m1->save();
435
436
437

    auto file2 = std::make_shared<mock::NoopFile>( "media2.mkv" );
    file2->setSize( 111 );
438
    auto m2 = ml->addFile( file2 );
439
    m2->setType( Media::Type::Video );
440
    m2->save();
441
442
443
444
445
446
447
448
449
450
451
452

    auto media = ml->videoFiles( SortingCriteria::FileSize, false );
    ASSERT_EQ( 2u, media.size() );
    ASSERT_EQ( m2->id(), media[0]->id() );
    ASSERT_EQ( m1->id(), media[1]->id() );

    media = ml->videoFiles( SortingCriteria::FileSize, true );
    ASSERT_EQ( 2u, media.size() );
    ASSERT_EQ( m2->id(), media[1]->id() );
    ASSERT_EQ( m1->id(), media[0]->id() );
}

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
TEST_F( Medias, SortByFilename )
{
    auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "zzzzz.mp3" ) );
    m1->setType( Media::Type::Video );
    m1->setTitle( "aaaaa" );
    m1->save();

    auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "aaaaa.mp3" ) );
    m2->setType( Media::Type::Video );
    m2->setTitle( "zzzzz" );
    m2->save();

    auto media = ml->videoFiles( SortingCriteria::Filename, false );
    ASSERT_EQ( 2u, media.size() );
    ASSERT_EQ( m2->id(), media[0]->id() );
    ASSERT_EQ( m1->id(), media[1]->id() );

    media = ml->videoFiles( SortingCriteria::LastModificationDate, true );
    ASSERT_EQ( 2u, media.size() );
    ASSERT_EQ( m2->id(), media[1]->id() );
    ASSERT_EQ( m1->id(), media[0]->id() );
}

476
477
TEST_F( Medias, SetType )
{
478
    auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "media1.mp3" ) );
479
    ASSERT_EQ( IMedia::Type::External, m1->type() );
480

481
    m1->setType( IMedia::Type::Video );
482
483
    m1->save();

484
    ASSERT_EQ( IMedia::Type::Video, m1->type() );
485
486
487
488

    Reload();

    auto m2 = ml->media( m1->id() );
489
    ASSERT_EQ( IMedia::Type::Video, m2->type() );
490
}
491

492
493
TEST_F( Medias, Metadata )
{
494
    auto m = ml->addMedia( "media.mp3" );
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517

    {
        const auto& md = m->metadata( Media::MetadataType::Speed );
        ASSERT_FALSE( md.isSet() );
    }

    auto res = m->setMetadata( Media::MetadataType::Speed, "foo" );
    ASSERT_TRUE( res );

    {
        const auto& md = m->metadata( Media::MetadataType::Speed );
        ASSERT_EQ( "foo", md.str() );
    }

    Reload();

    m = ml->media( m->id() );
    const auto& md = m->metadata( Media::MetadataType::Speed );
    ASSERT_EQ( "foo", md.str() );
}

TEST_F( Medias, MetadataOverride )
{
518
    auto m = ml->addMedia( "media.mp3" );
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
    auto res = m->setMetadata( Media::MetadataType::Speed, "foo" );
    ASSERT_TRUE( res );

    m->setMetadata( Media::MetadataType::Speed, "otter" );
    {
        const auto& md = m->metadata( Media::MetadataType::Speed );
        ASSERT_EQ( "otter", md.str() );
    }

    Reload();

    m = ml->media( m->id() );
    const auto& md = m->metadata( Media::MetadataType::Speed );
    ASSERT_EQ( "otter", md.str() );
}

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
TEST_F( Medias, ExternalMrl )
{
    auto m = ml->addMedia( "https://foo.bar/sea-otters.mkv" );
    ASSERT_NE( nullptr, m );

    ASSERT_EQ( m->title(), "sea-otters.mkv" );

    // External files shouldn't appear in listings
    auto videos = ml->videoFiles( medialibrary::SortingCriteria::Default, false );
    ASSERT_EQ( 0u, videos.size() );

    auto audios = ml->audioFiles( medialibrary::SortingCriteria::Default, false );
    ASSERT_EQ( 0u, audios.size() );

    Reload();

    auto m2 = ml->media( "https://foo.bar/sea-otters.mkv" );
    ASSERT_NE( nullptr, m2 );
    ASSERT_EQ( m->id(), m2->id() );
554
555
556
557
558
559

    auto files = m2->files();
    ASSERT_EQ( 1u, files.size() );
    auto f = files[0];
    ASSERT_TRUE( f->isExternal() );
    ASSERT_EQ( File::Type::Main, f->type() );
560
561
}

562
563
564
565
566
567
568
569
TEST_F( Medias, DuplicatedExternalMrl )
{
    auto m = ml->addMedia( "http://foo.bar" );
    auto m2 = ml->addMedia( "http://foo.bar" );
    ASSERT_NE( nullptr, m );
    ASSERT_EQ( nullptr, m2 );
}

570
571
572
573
574
575
576
577
578
579
580
581
582
583
TEST_F( Medias, SetTitle )
{
    auto m = ml->addMedia( "media" );
    ASSERT_EQ( "media", m->title() );
    auto res = m->setTitle( "sea otters" );
    ASSERT_TRUE( res );
    ASSERT_EQ( "sea otters", m->title() );

    Reload();

    m = ml->media( m->id() );
    ASSERT_EQ( "sea otters", m->title() );
}

584
585
586
587
588
589
590
591
592
593
594
595
596
class FetchMedia : public Tests
{
protected:
    static const std::string RemovableDeviceUuid;
    static const std::string RemovableDeviceMountpoint;
    std::shared_ptr<mock::FileSystemFactory> fsMock;
    std::unique_ptr<mock::WaitForDiscoveryComplete> cbMock;

    virtual void SetUp() override
    {
        unlink( "test.db" );
        fsMock.reset( new mock::FileSystemFactory );
        cbMock.reset( new mock::WaitForDiscoveryComplete );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
597
        fsMock->addFolder( "file:///a/mnt/" );
598
599
600
601
602
603
604
605
        auto device = fsMock->addDevice( RemovableDeviceMountpoint, RemovableDeviceUuid );
        device->setRemovable( true );
        fsMock->addFile( RemovableDeviceMountpoint + "removablefile.mp3" );
        Reload();
    }

    virtual void InstantiateMediaLibrary() override
    {
606
        ml.reset( new MediaLibraryWithDiscoverer );
607
608
609
610
611
612
613
614
615
    }

    virtual void Reload()
    {
        Tests::Reload( fsMock, cbMock.get() );
    }
};

const std::string FetchMedia::RemovableDeviceUuid = "{fake-removable-device}";
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
616
const std::string FetchMedia::RemovableDeviceMountpoint = "file:///a/mnt/fake-device/";
617
618
619
620

TEST_F( FetchMedia, FetchNonRemovable )
{
    ml->discover( mock::FileSystemFactory::Root );
621
    bool discovered = cbMock->waitDiscovery();
622
623
624
625
626
627
628
629
630
    ASSERT_TRUE( discovered );

    auto m = ml->media( mock::FileSystemFactory::SubFolder + "subfile.mp4" );
    ASSERT_NE( nullptr, m );
}

TEST_F( FetchMedia, FetchRemovable )
{
    ml->discover( mock::FileSystemFactory::Root );
631
    bool discovered = cbMock->waitDiscovery();
632
633
634
635
636
637
638
639
640
    ASSERT_TRUE( discovered );

    auto m = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
    ASSERT_NE( nullptr, m );
}

TEST_F( FetchMedia, FetchRemovableUnplugged )
{
    ml->discover( mock::FileSystemFactory::Root );
641
    bool discovered = cbMock->waitDiscovery();
642
643
644
645
646
    ASSERT_TRUE( discovered );

    fsMock->unmountDevice( RemovableDeviceUuid );

    Reload();
647
    bool reloaded = cbMock->waitReload();
648
649
650
651
652
    ASSERT_TRUE( reloaded );

    auto m = ml->media( RemovableDeviceMountpoint + "removablefile.mp3" );
    ASSERT_EQ( nullptr, m );
}