MediaTests.cpp 14.3 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
TEST_F( Medias, Fetch )
62
{
63
    auto f = ml->addFile( "media.avi" );
64
65
    auto f2 = ml->media( f->id() );
    ASSERT_EQ( f->id(), f2->id() );
66
    ASSERT_EQ( f, f2 );
67
68

    // Flush cache and fetch from DB
69
    Reload();
70

71
    f2 = std::static_pointer_cast<Media>( ml->media( f->id() ) );
72
    ASSERT_EQ( f->id(), f2->id() );
73
}
74

75
TEST_F( Medias, Duration )
76
{
77
    auto f = ml->addFile( "media.avi" );
78
    ASSERT_EQ( f->duration(), -1 );
79

80
81
82
83
    // Use a value that checks we're using a 64bits value
    int64_t d = int64_t(1) << 40;

    f->setDuration( d );
84
    f->save();
85
    ASSERT_EQ( f->duration(), d );
86
87
88

    Reload();

89
    auto f2 = ml->media( f->id() );
90
    ASSERT_EQ( f2->duration(), d );
91
}
92

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
93
TEST_F( Medias, Thumbnail )
94
{
95
    auto f = ml->addFile( "media.avi" );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
96
    ASSERT_EQ( f->thumbnail(), "" );
97

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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
100
    f->setThumbnail( newThumbnail );
101
    f->save();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
102
    ASSERT_EQ( f->thumbnail(), newThumbnail );
103
104
105

    Reload();

106
    auto f2 = ml->media( f->id() );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
107
    ASSERT_EQ( f2->thumbnail(), newThumbnail );
108
}
109
110
111

TEST_F( Medias, PlayCount )
{
112
    auto f = ml->addFile( "media.avi" );
113
114
115
116
117
118
    ASSERT_EQ( 0, f->playCount() );
    f->increasePlayCount();
    ASSERT_EQ( 1, f->playCount() );

    Reload();

119
    f = std::static_pointer_cast<Media>( ml->media( f->id() ) );
120
121
    ASSERT_EQ( 1, f->playCount() );
}
122
123
124
125

TEST_F( Medias, Progress )
{
    auto f = ml->addFile( "media.avi" );
126
127
128
    ASSERT_EQ( 0, f->metadata( Media::MetadataType::Progress ).integer() );
    f->setMetadata( Media::MetadataType::Progress, 123 );
    ASSERT_EQ( 123, f->metadata( Media::MetadataType::Progress ).integer() );
129
130
131
132

    Reload();

    f = ml->media( f->id() );
133
    ASSERT_EQ( 123, f->metadata( Media::MetadataType::Progress ).integer() );
134
135
136
137
138
}

TEST_F( Medias, Rating )
{
    auto f = ml->addFile( "media.avi" );
139
140
141
    ASSERT_FALSE( f->metadata( Media::MetadataType::Rating ).isSet() );
    f->setMetadata( Media::MetadataType::Rating, 12345 );
    ASSERT_EQ( 12345, f->metadata( Media::MetadataType::Rating ).integer() );
142
143
144
145

    Reload();

    f = ml->media( f->id() );
146
    ASSERT_EQ( 12345, f->metadata( Media::MetadataType::Rating ).integer() );
147
}
148
149
150
151
152
153
154

TEST_F( Medias, Search )
{
    for ( auto i = 1u; i <= 10u; ++i )
    {
        auto m = ml->addFile( "track " + std::to_string( i ) + ".mp3" );
    }
155
    auto media = ml->searchMedia( "tra" ).others;
156
157
    ASSERT_EQ( 10u, media.size() );

158
    media = ml->searchMedia( "track 1" ).others;
159
160
    ASSERT_EQ( 2u, media.size() );

161
    media = ml->searchMedia( "grouik" ).others;
162
163
    ASSERT_EQ( 0u, media.size() );

164
    media = ml->searchMedia( "rack" ).others;
165
166
167
168
169
170
171
    ASSERT_EQ( 0u, media.size() );
}

TEST_F( Medias, SearchAfterEdit )
{
    auto m = ml->addFile( "media.mp3" );

172
    auto media = ml->searchMedia( "media" ).others;
173
174
175
176
177
    ASSERT_EQ( 1u, media.size() );

    m->setTitle( "otters are awesome" );
    m->save();

178
    media = ml->searchMedia( "media" ).others;
179
180
    ASSERT_EQ( 0u, media.size() );

181
    media = ml->searchMedia( "otters" ).others;
182
183
184
185
186
187
188
    ASSERT_EQ( 1u, media.size() );
}

TEST_F( Medias, SearchAfterDelete )
{
    auto m = ml->addFile( "media.mp3" );

189
    auto media = ml->searchMedia( "media" ).others;
190
191
192
193
194
    ASSERT_EQ( 1u, media.size() );

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

195
    media = ml->searchMedia( "media" ).others;
196
197
    ASSERT_EQ( 0u, media.size() );
}
198
199
200
201

TEST_F( Medias, SearchByLabel )
{
    auto m = ml->addFile( "media.mkv" );
202
    auto media = ml->searchMedia( "otter" ).others;
203
204
205
206
207
    ASSERT_EQ( 0u, media.size() );

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

208
    media = ml->searchMedia( "otter" ).others;
209
210
211
212
213
    ASSERT_EQ( 1u, media.size() );

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

214
    media = ml->searchMedia( "otter" ).others;
215
216
    ASSERT_EQ( 1u, media.size() );

217
    media = ml->searchMedia( "pangolin" ).others;
218
219
220
221
    ASSERT_EQ( 1u, media.size() );

    m->removeLabel( l );

222
    media = ml->searchMedia( "otter" ).others;
223
224
    ASSERT_EQ( 0u, media.size() );

225
    media = ml->searchMedia( "pangolin" ).others;
226
227
228
229
    ASSERT_EQ( 1u, media.size() );

    m->addLabel( l );

230
    media = ml->searchMedia( "otter" ).others;
231
232
    ASSERT_EQ( 1u, media.size() );

233
    media = ml->searchMedia( "pangolin" ).others;
234
235
236
237
    ASSERT_EQ( 1u, media.size() );

    ml->deleteLabel( l );

238
    media = ml->searchMedia( "otter" ).others;
239
240
    ASSERT_EQ( 0u, media.size() );

241
    media = ml->searchMedia( "pangolin" ).others;
242
243
    ASSERT_EQ( 1u, media.size() );
}
244
245
246
247
248
249
250

TEST_F( Medias, SearchTracks )
{
    auto a = ml->createAlbum( "album" );
    for ( auto i = 1u; i <= 10u; ++i )
    {
       auto m = ml->addFile( "track " + std::to_string( i ) + ".mp3" );
251
       a->addTrack( m, i, 1, 0, 0 );
252
253
254
255
256
257
258
259
260
261
262
263
264
    }
    auto tracks = ml->searchMedia( "tra" ).tracks;
    ASSERT_EQ( 10u, tracks.size() );

    tracks = ml->searchMedia( "track 1" ).tracks;
    ASSERT_EQ( 2u, tracks.size() );

    tracks = ml->searchMedia( "grouik" ).tracks;
    ASSERT_EQ( 0u, tracks.size() );

    tracks = ml->searchMedia( "rack" ).tracks;
    ASSERT_EQ( 0u, tracks.size() );
}
265
266
267
268
269
270
271
272
273
274
275
276
277
278

TEST_F( Medias, Favorite )
{
    auto m = ml->addFile( "media.mkv" );
    ASSERT_FALSE( m->isFavorite() );

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

    Reload();

    m = ml->media( m->id() );
    ASSERT_TRUE( m->isFavorite() );
}
279
280
281
282
283
284
285
286
287
288
289
290
291
292

TEST_F( Medias, History )
{
    auto m = ml->addFile( "media.mkv" );

    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() );

293
    compat::this_thread::sleep_for( std::chrono::seconds{ 1 } );
294
295
296
297
298
299
300
301
302
    auto m2 = ml->addFile( "media.mkv" );
    m2->increasePlayCount();
    m2->save();

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

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
TEST_F( Medias, ClearHistory )
{
    auto m = ml->addFile( "media.mkv" );

    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() );
}

327
328
329
330
331
332
333
334
335
336
337
338
339
340
TEST_F( Medias, SetReleaseDate )
{
    auto m = ml->addFile( "movie.mkv" );

    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 );
}
341
342
343
344
345

TEST_F( Medias, SortByAlpha )
{
    auto m1 = ml->addFile( "media1.mp3" );
    m1->setTitle( "Abcd" );
346
    m1->setType( Media::Type::Audio );
347
348
349
350
    m1->save();

    auto m2 = ml->addFile( "media2.mp3" );
    m2->setTitle( "Zyxw" );
351
    m2->setType( Media::Type::Audio );
352
353
    m2->save();

354
355
    auto m3 = ml->addFile( "media3.mp3" );
    m3->setTitle( "afterA-beforeZ" );
356
    m3->setType( Media::Type::Audio );
357
358
    m3->save();

359
    auto media = ml->audioFiles( SortingCriteria::Alpha, false );
360
    ASSERT_EQ( 3u, media.size() );
361
    ASSERT_EQ( m1->id(), media[0]->id() );
362
363
    ASSERT_EQ( m3->id(), media[1]->id() );
    ASSERT_EQ( m2->id(), media[2]->id() );
364

365
    media = ml->audioFiles( SortingCriteria::Alpha, true );
366
    ASSERT_EQ( 3u, media.size() );
367
    ASSERT_EQ( m2->id(), media[0]->id() );
368
369
    ASSERT_EQ( m3->id(), media[1]->id() );
    ASSERT_EQ( m1->id(), media[2]->id() );
370
371
372
373
374
375
376
}

TEST_F( Medias, SortByLastModifDate )
{
    auto file1 = std::make_shared<mock::NoopFile>( "media.mkv" );
    file1->setLastModificationDate( 666 );
    auto m1 = ml->addFile( *file1 );
377
    m1->setType( Media::Type::Video );
378
    m1->save();
379
380
381
382

    auto file2 = std::make_shared<mock::NoopFile>( "media2.mkv" );
    file2->setLastModificationDate( 111 );
    auto m2 = ml->addFile( *file2 );
383
    m2->setType( Media::Type::Video );
384
    m2->save();
385

386
    auto media = ml->videoFiles( SortingCriteria::LastModificationDate, false );
387
388
389
390
    ASSERT_EQ( 2u, media.size() );
    ASSERT_EQ( m2->id(), media[0]->id() );
    ASSERT_EQ( m1->id(), media[1]->id() );

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

397
398
399
400
401
TEST_F( Medias, SortByFileSize )
{
    auto file1 = std::make_shared<mock::NoopFile>( "media.mkv" );
    file1->setSize( 666 );
    auto m1 = ml->addFile( *file1 );
402
    m1->setType( Media::Type::Video );
403
    m1->save();
404
405
406
407

    auto file2 = std::make_shared<mock::NoopFile>( "media2.mkv" );
    file2->setSize( 111 );
    auto m2 = ml->addFile( *file2 );
408
    m2->setType( Media::Type::Video );
409
    m2->save();
410
411
412
413
414
415
416
417
418
419
420
421

    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() );
}

422
423
424
TEST_F( Medias, SetType )
{
    auto m1 = ml->addFile( "media1.mp3" );
425
    ASSERT_EQ( IMedia::Type::Unknown, m1->type() );
426

427
    m1->setType( IMedia::Type::Video );
428
429
    m1->save();

430
    ASSERT_EQ( IMedia::Type::Video, m1->type() );
431
432
433
434

    Reload();

    auto m2 = ml->media( m1->id() );
435
    ASSERT_EQ( IMedia::Type::Video, m2->type() );
436
}
437

438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
TEST_F( Medias, Metadata )
{
    auto m = ml->addFile( "media.mp3" );

    {
        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 )
{
    auto m = ml->addFile( "media.mp3" );
    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() );
}

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
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 );
        fsMock->addFolder( "/a/mnt/" );
        auto device = fsMock->addDevice( RemovableDeviceMountpoint, RemovableDeviceUuid );
        device->setRemovable( true );
        fsMock->addFile( RemovableDeviceMountpoint + "removablefile.mp3" );
        Reload();
    }

    virtual void InstantiateMediaLibrary() override
    {
        ml.reset( new MediaLibraryWithoutParser );
    }

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

const std::string FetchMedia::RemovableDeviceUuid = "{fake-removable-device}";
const std::string FetchMedia::RemovableDeviceMountpoint = "/a/mnt/fake-device/";

TEST_F( FetchMedia, FetchNonRemovable )
{
    cbMock->prepareForWait();
    ml->discover( mock::FileSystemFactory::Root );
    bool discovered = cbMock->wait();
    ASSERT_TRUE( discovered );

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

TEST_F( FetchMedia, FetchRemovable )
{
    cbMock->prepareForWait();
    ml->discover( mock::FileSystemFactory::Root );
    bool discovered = cbMock->wait();
    ASSERT_TRUE( discovered );

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

TEST_F( FetchMedia, FetchRemovableUnplugged )
{
    cbMock->prepareForWait();
    ml->discover( mock::FileSystemFactory::Root );
    bool discovered = cbMock->wait();
    ASSERT_TRUE( discovered );

    fsMock->unmountDevice( RemovableDeviceUuid );

    cbMock->prepareForReload();
    Reload();
    bool reloaded = cbMock->wait();
    ASSERT_TRUE( reloaded );

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