PlaylistTests.cpp 12.5 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "Tests.h"

#include "Playlist.h"
#include "Media.h"

class Playlists : public Tests
{
protected:
    std::shared_ptr<Playlist> pl;
    std::shared_ptr<Media> m;

    virtual void SetUp() override
    {
        Tests::SetUp();
        pl = std::static_pointer_cast<Playlist>( ml->createPlaylist( "test playlist" ) );
    }
};

TEST_F( Playlists, Create )
{
    ASSERT_NE( nullptr, pl );
    ASSERT_NE( 0u, pl->id() );
    ASSERT_EQ( "test playlist", pl->name() );
50
    ASSERT_NE( 0u, pl->creationDate() );
51 52
}

53 54 55
TEST_F( Playlists, CreateDuplicate )
{
    auto p = ml->createPlaylist(pl->name());
56 57
    ASSERT_NE( nullptr, p );
    ASSERT_NE( p->id(), pl->id() );
58 59
}

60 61 62 63 64 65
TEST_F( Playlists, Fetch )
{
    auto pl2 = ml->playlist( pl->id() );
    ASSERT_NE( nullptr, pl2 );
    ASSERT_EQ( pl->id(), pl2->id() );

66
    auto playlists = ml->playlists( nullptr )->all();
67 68 69 70 71 72 73 74 75
    ASSERT_EQ( 1u, playlists.size() );
    ASSERT_EQ( pl->id(), playlists[0]->id() );
}


TEST_F( Playlists, DeletePlaylist )
{
    auto res = ml->deletePlaylist( pl->id() );
    ASSERT_TRUE( res );
76
    auto playlists = ml->playlists( nullptr )->all();
77 78 79 80 81 82 83 84 85 86 87 88 89
    ASSERT_EQ( 0u, playlists.size() );
}

TEST_F( Playlists, SetName )
{
    ASSERT_EQ( "test playlist", pl->name() );
    auto newName = "new name";
    auto res = pl->setName( newName );
    ASSERT_TRUE( res );
    ASSERT_EQ( newName, pl->name() );

    Reload();

90
    pl = std::static_pointer_cast<Playlist>( ml->playlist( pl->id() ) );
91 92 93 94 95 96 97 98 99 100
    ASSERT_EQ( newName, pl->name() );
}

TEST_F( Playlists, FetchAll )
{
    pl->setName( "pl 1" );
    ml->createPlaylist( "pl 2" );
    ml->createPlaylist( "pl 3" );
    ml->createPlaylist( "pl 4" );

101
    auto playlists = ml->playlists( nullptr )->all();
102 103 104 105 106 107 108 109 110 111
    ASSERT_EQ( 4u, playlists.size() );
    for ( auto& p : playlists )
    {
        auto name = std::string{ "pl " } + std::to_string( p->id() );
        ASSERT_EQ( name, p->name() );
    }
}

TEST_F( Playlists, Add )
{
112
    auto m = ml->addMedia( "file.mkv" );
113
    auto res = pl->append( *m );
114
    ASSERT_TRUE( res );
115
    auto media = pl->media()->all();
116 117 118 119 120 121 122 123
    ASSERT_EQ( 1u, media.size() );
    ASSERT_EQ( m->id(), media[0]->id() );
}

TEST_F( Playlists, Append )
{
    for ( auto i = 0; i < 5; ++i )
    {
124
        auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
125
        ASSERT_NE( nullptr, m );
126
        pl->append( *m );
127
    }
128
    auto media = pl->media()->all();
129 130 131 132 133 134 135 136 137 138 139 140
    ASSERT_EQ( 5u, media.size() );
    for ( auto i = 0u; i < media.size(); ++i )
    {
        auto name = "media" + std::to_string( i ) + ".mkv";
        ASSERT_EQ( media[i]->title(), name );
    }
}

TEST_F( Playlists, Insert )
{
    for ( auto i = 1; i < 4; ++i )
    {
141
        auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
142
        ASSERT_NE( nullptr, m );
143
        auto res = pl->append( *m );
144 145 146
        ASSERT_TRUE( res );
    }
    // [<1,1>,<2,2>,<3,3>]
147
    auto firstMedia = ml->addMedia( "first.mkv" );
148

149
    pl->add( *firstMedia, 1 );
150
    // [<4,1>,<1,2>,<2,3>,<3,4>]
151
    auto middleMedia = ml->addMedia( "middle.mkv" );
152
    pl->add( *middleMedia, 3 );
153
    // [<4,1>,<1,2>,<5,3>,<2,4>,<3,5>]
154
    auto media = pl->media()->all();
155 156 157 158 159 160 161 162 163 164 165 166 167
    ASSERT_EQ( 5u, media.size() );

    ASSERT_EQ( 4u, media[0]->id() );
    ASSERT_EQ( 1u, media[1]->id() );
    ASSERT_EQ( 5u, media[2]->id() );
    ASSERT_EQ( 2u, media[3]->id() );
    ASSERT_EQ( 3u, media[4]->id() );
}

TEST_F( Playlists, Move )
{
    for ( auto i = 1; i < 6; ++i )
    {
168
        auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
169
        ASSERT_NE( nullptr, m );
170
        auto res = pl->append( *m );
171 172 173 174 175
        ASSERT_TRUE( res );
    }
    // [<1,1>,<2,2>,<3,3>,<4,4>,<5,5>]
    pl->move( 5, 1 );
    // [<5,1>,<1,2>,<2,3>,<3,4>,<4,5>]
176
    auto media = pl->media()->all();
177 178 179 180 181 182 183 184 185 186 187 188 189
    ASSERT_EQ( 5u, media.size() );

    ASSERT_EQ( 5u, media[0]->id() );
    ASSERT_EQ( 1u, media[1]->id() );
    ASSERT_EQ( 2u, media[2]->id() );
    ASSERT_EQ( 3u, media[3]->id() );
    ASSERT_EQ( 4u, media[4]->id() );
}

TEST_F( Playlists, Remove )
{
    for ( auto i = 1; i < 6; ++i )
    {
190
        auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
191
        ASSERT_NE( nullptr, m );
192
        auto res = pl->append( *m );
193 194 195
        ASSERT_TRUE( res );
    }
    // [<1,1>,<2,2>,<3,3>,<4,4>,<5,5>]
196
    auto media = pl->media()->all();
197 198 199 200 201
    ASSERT_EQ( 5u, media.size() );

    pl->remove( 3 );
    // [<1,1>,<2,2>,<4,4>,<5,5>]

202
    media = pl->media()->all();
203 204 205 206 207 208 209 210 211 212 213 214
    ASSERT_EQ( 4u, media.size() );

    ASSERT_EQ( 1u, media[0]->id() );
    ASSERT_EQ( 2u, media[1]->id() );
    ASSERT_EQ( 4u, media[2]->id() );
    ASSERT_EQ( 5u, media[3]->id() );
}

TEST_F( Playlists, DeleteFile )
{
    for ( auto i = 1; i < 6; ++i )
    {
215
        auto m = ml->addMedia( "media" + std::to_string( i ) + ".mkv" );
216
        ASSERT_NE( nullptr, m );
217
        auto res = pl->append( *m );
218 219 220
        ASSERT_TRUE( res );
    }
    // [<1,1>,<2,2>,<3,3>,<4,4>,<5,5>]
221
    auto media = pl->media()->all();
222 223 224 225 226 227 228 229 230 231
    ASSERT_EQ( 5u, media.size() );
    auto m = std::static_pointer_cast<Media>( media[2] );
    auto fs = m->files();
    ASSERT_EQ( 1u, fs.size() );
    m->removeFile( static_cast<File&>( *fs[0] ) );
    // This should trigger the Media removal, which should in
    // turn trigger the playlist item removal
    // So we should now have
    // [<1,1>,<2,2>,<4,4>,<5,5>]

232
    media = pl->media()->all();
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    ASSERT_EQ( 4u, media.size() );

    ASSERT_EQ( 1u, media[0]->id() );
    ASSERT_EQ( 2u, media[1]->id() );
    ASSERT_EQ( 4u, media[2]->id() );
    ASSERT_EQ( 5u, media[3]->id() );

    // Ensure we don't delete an empty playlist:
    auto ms = ml->files();
    for ( auto &mptr : ms )
    {
        auto m = std::static_pointer_cast<Media>( mptr );
        auto fs = m->files();
        ASSERT_EQ( 1u, fs.size() );
        m->removeFile( static_cast<File&>( *fs[0] ) );
    }
249
    media = pl->media()->all();
250
    ASSERT_EQ( 0u, media.size() );
251 252
    auto pl2 = ml->playlist( pl->id() );
    ASSERT_NE( nullptr, pl2 );
253 254
}

255 256 257 258 259
TEST_F( Playlists, Search )
{
    ml->createPlaylist( "playlist 2" );
    ml->createPlaylist( "laylist 3" );

260
    auto playlists = ml->searchPlaylists( "play", nullptr )->all();
261 262 263
    ASSERT_EQ( 2u, playlists.size() );
}

264 265 266 267
TEST_F( Playlists, SearchAndSort )
{
    auto pl2 = ml->createPlaylist( "playlist 2" );

268
    auto playlists = ml->searchPlaylists( "play", nullptr )->all();
269 270 271 272
    ASSERT_EQ( 2u, playlists.size() );
    ASSERT_EQ( pl2->id(), playlists[0]->id() );
    ASSERT_EQ( pl->id(), playlists[1]->id() );

273 274
    QueryParameters params = { SortingCriteria::Default, true };
    playlists = ml->searchPlaylists( "play", &params )->all();
275 276 277 278 279
    ASSERT_EQ( 2u, playlists.size() );
    ASSERT_EQ( pl->id(), playlists[0]->id() );
    ASSERT_EQ( pl2->id(), playlists[1]->id() );
}

280 281 282
TEST_F( Playlists, SearchAfterDelete )
{
    auto pl = ml->createPlaylist( "sea otters greatest hits" );
283
    auto pls = ml->searchPlaylists( "sea otters", nullptr )->all();
284 285 286 287
    ASSERT_EQ( 1u, pls.size() );

    ml->deletePlaylist( pl->id() );

288
    pls = ml->searchPlaylists( "sea otters", nullptr )->all();
289 290 291 292 293 294
    ASSERT_EQ( 0u, pls.size() );
}

TEST_F( Playlists, SearchAfterUpdate )
{
    auto pl = ml->createPlaylist( "sea otters greatest hits" );
295
    auto pls = ml->searchPlaylists( "sea otters", nullptr )->all();
296 297 298 299
    ASSERT_EQ( 1u, pls.size() );

    pl->setName( "pangolins are cool too" );

300
    pls = ml->searchPlaylists( "sea otters", nullptr )->all();
301 302
    ASSERT_EQ( 0u, pls.size() );

303
    pls = ml->searchPlaylists( "pangolins", nullptr )->all();
304 305
    ASSERT_EQ( 1u, pls.size() );
}
306 307 308 309 310

TEST_F( Playlists, Sort )
{
    auto pl2 = ml->createPlaylist( "A playlist" );

311
    auto pls = ml->playlists( nullptr )->all();
312 313 314 315
    ASSERT_EQ( 2u, pls.size() );
    ASSERT_EQ( pl2->id(), pls[0]->id() );
    ASSERT_EQ( pl->id(), pls[1]->id() );

316 317
    QueryParameters params { SortingCriteria::Default, true };
    pls = ml->playlists( &params )->all();
318 319 320 321
    ASSERT_EQ( 2u, pls.size() );
    ASSERT_EQ( pl2->id(), pls[1]->id() );
    ASSERT_EQ( pl->id(), pls[0]->id() );
}
322 323 324

TEST_F( Playlists, AddDuplicate )
{
325
    auto m = ml->addMedia( "file.mkv" );
326
    auto res = pl->append( *m );
327
    ASSERT_TRUE( res );
328
    res = pl->append( *m );
329 330 331 332 333
    ASSERT_TRUE( res );
    auto media = pl->media()->all();
    ASSERT_EQ( 2u, media.size() );
    ASSERT_EQ( m->id(), media[0]->id() );
    ASSERT_EQ( m->id(), media[1]->id() );
334 335 336

    auto count = pl->media()->count();
    ASSERT_EQ( count, media.size() );
337
}
338 339 340

TEST_F( Playlists, SearchMedia )
{
341
    auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "m1.mp3", IMedia::Type::Audio ) );
342 343 344
    m1->setTitleBuffered( "otter" );
    m1->save();

345
    auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "m2.mp3", IMedia::Type::Audio ) );
346 347 348 349
    // Won't match since it's not on the beginning of a word
    m2->setTitleBuffered( "ENOOTTER" );
    m2->save();

350
    auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "m3.mp3", IMedia::Type::Audio ) );
351 352 353
    m3->setTitleBuffered( "otter otter otter" );
    m3->save();

354 355
    pl->append( *m1 );
    pl->append( *m2 );
356 357 358 359 360 361 362 363

    auto media = ml->searchMedia( "otter", nullptr )->all();
    ASSERT_EQ( 2u, media.size() );

    media = pl->searchMedia( "otter", nullptr )->all();
    ASSERT_EQ( 1u, media.size() );
    ASSERT_EQ( m1->id(), media[0]->id() );
}
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

TEST_F( Playlists, ReinsertMedia )
{
    auto m1 = ml->addMedia( "http://sea.otters/fluffy.mkv" );
    auto m2 = ml->addMedia( "file:///cute_otters_holding_hands.mp4" );
    auto m3 = ml->addMedia( "media.mp3" );
    pl->append( *m1 );
    pl->append( *m2 );
    pl->append( *m3 );

    auto media = pl->media()->all();
    ASSERT_EQ( 3u, media.size() );
    ASSERT_EQ( m1->id(), media[0]->id() );
    ASSERT_EQ( m2->id(), media[1]->id() );
    ASSERT_EQ( m3->id(), media[2]->id() );

    // Ensure the media we fetch are recreated by checking their ids before/after
    auto m1Id = m1->id();
    auto m2Id = m2->id();

    ml->deleteMedia( m1->id() );
    ml->deleteMedia( m2->id() );

    Reload();
388
    pl = std::static_pointer_cast<Playlist>( ml->playlist( pl->id() ) );
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421

    m1 = ml->addMedia( "http://sea.otters/fluffy.mkv" );
    m2 = ml->addMedia( "file:///cute_otters_holding_hands.mp4" );

    media = pl->media()->all();
    ASSERT_EQ( 3u, media.size() );
    ASSERT_EQ( m1->id(), media[0]->id() );
    ASSERT_EQ( m2->id(), media[1]->id() );
    ASSERT_EQ( m3->id(), media[2]->id() );
    ASSERT_NE( m1->id(), m1Id );
    ASSERT_NE( m2->id(), m2Id );
}

TEST_F( Playlists, RemoveMedia )
{
    auto m1 = ml->addMedia( "http://sea.otters/fluffy.mkv" );
    auto m2 = ml->addMedia( "file:///cute_otters_holding_hands.mp4" );
    auto m3 = ml->addMedia( "media.mp3" );
    pl->append( *m1 );
    pl->append( *m2 );
    pl->append( *m3 );

    auto media = pl->media()->all();
    ASSERT_EQ( 3u, media.size() );
    ASSERT_EQ( m1->id(), media[0]->id() );
    ASSERT_EQ( m2->id(), media[1]->id() );
    ASSERT_EQ( m3->id(), media[2]->id() );

    ml->deleteMedia( m1->id() );
    ml->deleteMedia( m2->id() );

    Reload();

422
    pl = std::static_pointer_cast<Playlist>( ml->playlist( pl->id() ) );
423 424 425 426 427

    media = pl->media()->all();
    ASSERT_EQ( 1u, media.size() );
    ASSERT_EQ( m3->id(), media[0]->id() );
}
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

TEST_F( Playlists, ClearContent )
{
    auto m1 = ml->addMedia( "seaotter.mkv" );
    auto m2 = ml->addMedia( "fluffyfurball.mp4" );
    auto pl2 = ml->createPlaylist( "playlist 2" );

    pl->append( *m1 );
    pl2->append( *m2 );

    ASSERT_EQ( 1u, pl->media()->count() );
    ASSERT_EQ( 1u, pl2->media()->count() );

    pl->clearContent();

    ASSERT_EQ( 0u, pl->media()->count() );
    ASSERT_EQ( 1u, pl2->media()->count() );
}