MediaTests.cpp 8.96 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
#include "Tests.h"
24

25
#include "medialibrary/IMediaLibrary.h"
26
#include "File.h"
27
#include "Media.h"
28
29
30
#include "Artist.h"
#include "Album.h"
#include "AlbumTrack.h"
31
#include "mocks/FileSystem.h"
32

33
class Medias : public Tests
34
35
36
37
{
};


38
TEST_F( Medias, Init )
39
40
41
42
{
    // only test for correct test fixture behavior
}

43
TEST_F( Medias, Create )
44
{
45
46
47
48
49
50
51
52
    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() );
53
54
}

55
TEST_F( Medias, Fetch )
56
{
57
    auto f = ml->addFile( "media.avi" );
58
59
    auto f2 = ml->media( f->id() );
    ASSERT_EQ( f->id(), f2->id() );
60
    ASSERT_EQ( f, f2 );
61
62

    // Flush cache and fetch from DB
63
    Reload();
64

65
    f2 = std::static_pointer_cast<Media>( ml->media( f->id() ) );
66
    ASSERT_EQ( f->id(), f2->id() );
67
}
68

69
TEST_F( Medias, Duration )
70
{
71
    auto f = ml->addFile( "media.avi" );
72
    ASSERT_EQ( f->duration(), -1 );
73

74
75
76
77
    // Use a value that checks we're using a 64bits value
    int64_t d = int64_t(1) << 40;

    f->setDuration( d );
78
    f->save();
79
    ASSERT_EQ( f->duration(), d );
80
81
82

    Reload();

83
    auto f2 = ml->media( f->id() );
84
    ASSERT_EQ( f2->duration(), d );
85
}
86

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
87
TEST_F( Medias, Thumbnail )
88
{
89
    auto f = ml->addFile( "media.avi" );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
90
    ASSERT_EQ( f->thumbnail(), "" );
91

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

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
94
    f->setThumbnail( newThumbnail );
95
    f->save();
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
96
    ASSERT_EQ( f->thumbnail(), newThumbnail );
97
98
99

    Reload();

100
    auto f2 = ml->media( f->id() );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
101
    ASSERT_EQ( f2->thumbnail(), newThumbnail );
102
}
103
104
105

TEST_F( Medias, PlayCount )
{
106
    auto f = ml->addFile( "media.avi" );
107
108
109
110
111
112
    ASSERT_EQ( 0, f->playCount() );
    f->increasePlayCount();
    ASSERT_EQ( 1, f->playCount() );

    Reload();

113
    f = std::static_pointer_cast<Media>( ml->media( f->id() ) );
114
115
    ASSERT_EQ( 1, f->playCount() );
}
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

TEST_F( Medias, Progress )
{
    auto f = ml->addFile( "media.avi" );
    ASSERT_EQ( .0f, f->progress() );
    f->setProgress( 123.0f );
    // Check that a non-sensical value is ignored
    ASSERT_EQ( .0f, f->progress() );
    f->setProgress( 0.666f );
    ASSERT_EQ( .666f, f->progress() );

    Reload();

    f = ml->media( f->id() );
    ASSERT_EQ( .666f, f->progress() );
}

TEST_F( Medias, Rating )
{
    auto f = ml->addFile( "media.avi" );
    ASSERT_EQ( -1, f->rating() );
    f->setRating( 12345 );
    ASSERT_EQ( 12345, f->rating() );

    Reload();

    f = ml->media( f->id() );
    ASSERT_EQ( 12345, f->rating() );
}
145
146
147
148
149
150
151

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

155
    media = ml->searchMedia( "track 1" ).others;
156
157
    ASSERT_EQ( 2u, media.size() );

158
    media = ml->searchMedia( "grouik" ).others;
159
160
    ASSERT_EQ( 0u, media.size() );

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

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

169
    auto media = ml->searchMedia( "media" ).others;
170
171
172
173
174
    ASSERT_EQ( 1u, media.size() );

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

175
    media = ml->searchMedia( "media" ).others;
176
177
    ASSERT_EQ( 0u, media.size() );

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

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

186
    auto media = ml->searchMedia( "media" ).others;
187
188
189
190
191
    ASSERT_EQ( 1u, media.size() );

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

192
    media = ml->searchMedia( "media" ).others;
193
194
    ASSERT_EQ( 0u, media.size() );
}
195
196
197
198

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

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

205
    media = ml->searchMedia( "otter" ).others;
206
207
208
209
210
    ASSERT_EQ( 1u, media.size() );

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

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

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

    m->removeLabel( l );

219
    media = ml->searchMedia( "otter" ).others;
220
221
    ASSERT_EQ( 0u, media.size() );

222
    media = ml->searchMedia( "pangolin" ).others;
223
224
225
226
    ASSERT_EQ( 1u, media.size() );

    m->addLabel( l );

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

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

    ml->deleteLabel( l );

235
    media = ml->searchMedia( "otter" ).others;
236
237
    ASSERT_EQ( 0u, media.size() );

238
    media = ml->searchMedia( "pangolin" ).others;
239
240
    ASSERT_EQ( 1u, media.size() );
}
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

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" );
       a->addTrack( m, i, 1 );
    }
    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() );
}
262
263
264
265
266
267
268
269
270
271
272
273
274
275

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() );
}
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

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

    std::this_thread::sleep_for( std::chrono::seconds{ 1 } );
    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() );
}
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

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 );
}
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

TEST_F( Medias, SortByAlpha )
{
    auto m1 = ml->addFile( "media1.mp3" );
    m1->setTitle( "Abcd" );
    m1->save();

    auto m2 = ml->addFile( "media2.mp3" );
    m2->setTitle( "Zyxw" );
    m2->save();

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

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

TEST_F( Medias, SortByLastModifDate )
{
    auto file1 = std::make_shared<mock::NoopFile>( "media.mkv" );
    file1->setLastModificationDate( 666 );
    auto m1 = ml->addFile( *file1 );

    auto file2 = std::make_shared<mock::NoopFile>( "media2.mkv" );
    file2->setLastModificationDate( 111 );
    auto m2 = ml->addFile( *file2 );

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

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