MediaTests.cpp 7.34 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 "IMediaLibrary.h"
26
#include "File.h"
27
#include "Media.h"
28
29
30
#include "Artist.h"
#include "Album.h"
#include "AlbumTrack.h"
31

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


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

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

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

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

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

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

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

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

    Reload();

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

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

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

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

    Reload();

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

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

    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
145
146

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

    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 );
    f->save();
    ASSERT_EQ( 12345, f->rating() );

    Reload();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    m->removeLabel( l );

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

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

    m->addLabel( l );

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

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

    ml->deleteLabel( l );

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

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

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() );
}
264
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 );
    m->save();
    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
293
294
295
296
297
298
299
300
301
302

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