151c446635
Implements issue #6: Store playlists as queues Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
186 lines
6.4 KiB
C
186 lines
6.4 KiB
C
/*
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/filter.h>
|
|
#include <core/idle.h>
|
|
#include <core/playlist.h>
|
|
#include <core/tags/tags.h>
|
|
#include <tests/test.h>
|
|
|
|
static void test_init()
|
|
{
|
|
struct queue *q = playlist_get_queue(PL_MOST_PLAYED);
|
|
struct library *library;
|
|
GSList *list;
|
|
|
|
filter_init();
|
|
tags_init();
|
|
playlist_init(NULL);
|
|
while (idle_run_task()) {};
|
|
|
|
test_equal((void *)playlist_get_queue(PL_FAVORITED), NULL);
|
|
test_equal((void *)playlist_get_queue(PL_HIDDEN), NULL);
|
|
test_not_equal((void *)playlist_get_queue(PL_UNPLAYED), NULL);
|
|
test_not_equal((void *)playlist_get_queue(PL_MOST_PLAYED), NULL);
|
|
test_not_equal((void *)playlist_get_queue(PL_LEAST_PLAYED), NULL);
|
|
|
|
test_not_equal((void *)q, NULL);
|
|
test_equal(queue_has_flag(q, Q_ENABLED), (bool)true);
|
|
test_equal(queue_has_flag(q, Q_REPEAT), (bool)true);
|
|
test_equal(queue_has_flag(q, Q_NO_SORT), (bool)true);
|
|
test_equal(queue_size(q), 0);
|
|
|
|
list = q->q_sort;
|
|
test_equal(g_slist_length(q->q_sort), 3);
|
|
test_equal(GPOINTER_TO_INT(list->data), COMPARE_ARTIST);
|
|
list = g_slist_next(list);
|
|
test_equal(GPOINTER_TO_INT(list->data), COMPARE_YEAR);
|
|
list = g_slist_next(list);
|
|
test_equal(GPOINTER_TO_INT(list->data), COMPARE_TRACK);
|
|
|
|
/* Add tracks to the collection. */
|
|
library = library_find("tests/Music");
|
|
track_add(library, "tests/Music/Hyrule Symphony/01 - Title Theme.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/02 - Kokiri Forest.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/03 - Hyrule Field.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/04 - Hyrule Castle.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/05 - Lon Lon Ranch.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/06 - Kakariko Village.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/07 - Death Mountain.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/08 - Zora's Domain.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/09 - Gerudo Valley.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/10 - Ganondorf.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/11 - Princess Zelda.ogg");
|
|
track_add(library, "tests/Music/Hyrule Symphony/12 - Ocarina Medley.ogg");
|
|
track_add(library,
|
|
"tests/Music/Hyrule Symphony/13 - The Legend of Zelda Medley.ogg");
|
|
}
|
|
|
|
static void test_add()
|
|
{
|
|
struct queue *q = playlist_get_queue(PL_FAVORITED);
|
|
struct track *track = track_get(0);
|
|
|
|
test_equal(playlist_add(PL_FAVORITED, NULL), (bool)false);
|
|
test_equal(playlist_has(PL_FAVORITED, NULL), (bool)false);
|
|
|
|
test_equal(playlist_has(PL_FAVORITED, track), (bool)false);
|
|
test_equal(playlist_add(PL_FAVORITED, track), (bool)true);
|
|
test_equal(playlist_has(PL_FAVORITED, track), (bool)true);
|
|
q = playlist_get_queue(PL_FAVORITED);
|
|
test_not_equal((void *)q, NULL);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(playlist_add(PL_FAVORITED, track), (bool)false);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(playlist_add(PL_FAVORITED, track_get(1)), (bool)true);
|
|
test_equal(queue_size(q), 2);
|
|
|
|
track = track_get(2);
|
|
playlist_select(PL_HIDDEN);
|
|
q = playlist_get_queue(PL_HIDDEN);
|
|
test_equal((void *)q, NULL);
|
|
test_equal(playlist_has(PL_HIDDEN, track), (bool)false);
|
|
test_equal(playlist_add(PL_HIDDEN, track), (bool)true);
|
|
test_equal(playlist_has(PL_HIDDEN, track), (bool)true);
|
|
q = playlist_get_queue(PL_HIDDEN);
|
|
test_not_equal((void *)q, NULL);
|
|
test_equal(queue_size(q), 1);
|
|
}
|
|
|
|
static void test_remove()
|
|
{
|
|
struct queue *q = playlist_get_queue(PL_FAVORITED);
|
|
struct track *track = track_get(0);
|
|
|
|
/* The important thing here is that we don't crash */
|
|
test_equal(playlist_remove(PL_FAVORITED, NULL), (bool)false);
|
|
|
|
playlist_select(PL_FAVORITED);
|
|
test_equal(queue_size(q), 2);
|
|
test_equal(playlist_has(PL_FAVORITED, track), (bool)true);
|
|
test_equal(playlist_remove(PL_FAVORITED, track), (bool)true);
|
|
test_equal(playlist_has(PL_FAVORITED, track), (bool)false);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(playlist_remove(PL_FAVORITED, track), (bool)false);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(playlist_remove(PL_FAVORITED, track_get(1)), (bool)true);
|
|
test_equal(queue_size(q), 0);
|
|
|
|
q = playlist_get_queue(PL_HIDDEN);
|
|
track = track_get(2);
|
|
playlist_select(PL_HIDDEN);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(playlist_has(PL_HIDDEN, track), (bool)true);
|
|
test_equal(playlist_remove(PL_HIDDEN, track), (bool)true);
|
|
test_equal(playlist_has(PL_HIDDEN, track), (bool)false);
|
|
test_equal(queue_size(q), 0);
|
|
}
|
|
|
|
static void test_dynamic()
|
|
{
|
|
struct queue *q = playlist_get_queue(PL_UNPLAYED);
|
|
struct db_entry *dbe, *next;
|
|
unsigned int i, average = 0;
|
|
struct track *track;
|
|
|
|
/* Set play count to (track_number - 1) (average = 6) */
|
|
db_for_each(dbe, next, track_db_get()) {
|
|
for (i = 1; i < TRACK(dbe)->tr_track; i++)
|
|
track_played(TRACK(dbe));
|
|
average += TRACK(dbe)->tr_count;
|
|
}
|
|
average /= track_db_get()->db_size;
|
|
|
|
/* Only one unplayed track (tr_track == 1) */
|
|
playlist_select(PL_UNPLAYED);
|
|
test_equal(playlist_add(PL_UNPLAYED, track_get(1)), (bool)false);
|
|
test_equal(playlist_remove(PL_UNPLAYED, queue_at(q, 0)), (bool)false);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(queue_at(q, 0)->tr_track, 1);
|
|
test_equal(queue_at(q, 0)->tr_count, 0);
|
|
|
|
/* Six tracks have tr_count <= average && tr_count > 0 */
|
|
playlist_select(PL_LEAST_PLAYED);
|
|
test_equal(playlist_add(PL_LEAST_PLAYED, track_get(0)), (bool)false);
|
|
test_equal(queue_size(q), 6);
|
|
for (i = 0; i < queue_size(q); i++) {
|
|
track = queue_at(q, i);
|
|
test_loop_equal(track->tr_track, i + 2, i);
|
|
test_loop_equal(track->tr_count, i + 1, i);
|
|
test_loop_equal(playlist_remove(PL_LEAST_PLAYED, track),
|
|
(bool)false, i);
|
|
} test_loop_passed();
|
|
|
|
/* Six tracks have tr_count > average */
|
|
playlist_select(PL_MOST_PLAYED);
|
|
test_equal(playlist_add(PL_MOST_PLAYED, track_get(0)), (bool)false);
|
|
test_equal(queue_size(q), 6);
|
|
for (i = 0; i < queue_size(q); i++) {
|
|
track = queue_at(q, i);
|
|
test_loop_equal(track->tr_track, i + 8, i);
|
|
test_loop_equal(track->tr_count, i + 7, i);
|
|
test_loop_equal(playlist_remove(PL_MOST_PLAYED, track),
|
|
(bool)false, i);
|
|
} test_loop_passed();
|
|
}
|
|
|
|
static void test_deinit()
|
|
{
|
|
struct queue *q = playlist_get_queue(PL_UNPLAYED);
|
|
|
|
playlist_deinit();
|
|
tags_deinit();
|
|
filter_deinit();
|
|
|
|
test_equal(queue_size(q), 0);
|
|
test_equal((void *)q->q_sort, NULL);
|
|
}
|
|
|
|
DECLARE_UNIT_TESTS(
|
|
UNIT_TEST("Playlist Initialization", test_init),
|
|
UNIT_TEST("Playlist Add Track", test_add),
|
|
UNIT_TEST("Playlist Remove Track", test_remove),
|
|
UNIT_TEST("Playlist Dynamic Generation", test_dynamic),
|
|
UNIT_TEST("Playlist Deinit", test_deinit),
|
|
);
|