163 lines
4.6 KiB
C
163 lines
4.6 KiB
C
/*
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/collection.h>
|
|
#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();
|
|
GSList *list;
|
|
|
|
filter_init();
|
|
tags_init();
|
|
collection_init(NULL);
|
|
playlist_init(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. */
|
|
collection_add("tests/Music/Hyrule Symphony/");
|
|
while (idle_run_task());
|
|
}
|
|
|
|
static void test_add()
|
|
{
|
|
struct queue *c = collection_get_queue();
|
|
struct queue *q = playlist_get_queue();
|
|
struct track *track = track_get(0);
|
|
|
|
playlist_add(PL_FAVORITED, NULL);
|
|
test_equal(playlist_has(PL_FAVORITED, NULL), (bool)false);
|
|
|
|
test_equal(playlist_has(PL_FAVORITED, track), (bool)false);
|
|
playlist_add(PL_FAVORITED, track);
|
|
test_equal(playlist_has(PL_FAVORITED, track), (bool)true);
|
|
test_equal(queue_size(q), 1);
|
|
playlist_add(PL_FAVORITED, track);
|
|
test_equal(queue_size(q), 1);
|
|
playlist_add(PL_FAVORITED, track_get(1));
|
|
test_equal(queue_size(q), 2);
|
|
|
|
track = track_get(2);
|
|
playlist_select(PL_BANNED);
|
|
test_equal(queue_size(q), 0);
|
|
test_equal(queue_size(c), 13);
|
|
test_equal(playlist_has(PL_BANNED, track), (bool)false);
|
|
playlist_add(PL_BANNED, track);
|
|
test_equal(playlist_has(PL_BANNED, track), (bool)true);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(queue_size(c), 12);
|
|
|
|
/* Check playlist_fixup_collection() while we're here. */
|
|
queue_add(c, track);
|
|
test_equal(queue_size(c), 13);
|
|
playlist_fixup_collection();
|
|
test_equal(queue_size(c), 12);
|
|
}
|
|
|
|
static void test_remove()
|
|
{
|
|
struct queue *c = collection_get_queue();
|
|
struct queue *q = playlist_get_queue();
|
|
struct track *track = track_get(0);
|
|
|
|
/* The important thing here is that we don't crash */
|
|
playlist_remove(PL_FAVORITED, NULL);
|
|
|
|
playlist_select(PL_FAVORITED);
|
|
test_equal(queue_size(q), 2);
|
|
test_equal(playlist_has(PL_FAVORITED, track), (bool)true);
|
|
playlist_remove(PL_FAVORITED, track);
|
|
test_equal(playlist_has(PL_FAVORITED, track), (bool)false);
|
|
test_equal(queue_size(q), 1);
|
|
|
|
playlist_remove(PL_FAVORITED, track);
|
|
test_equal(queue_size(q), 1);
|
|
playlist_remove(PL_FAVORITED, track_get(1));
|
|
test_equal(queue_size(q), 0);
|
|
|
|
track = track_get(2);
|
|
playlist_select(PL_BANNED);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(queue_size(c), 12);
|
|
test_equal(playlist_has(PL_BANNED, track), (bool)true);
|
|
playlist_remove(PL_BANNED, track);
|
|
test_equal(playlist_has(PL_BANNED, track), (bool)false);
|
|
test_equal(queue_size(q), 0);
|
|
test_equal(queue_size(c), 13);
|
|
}
|
|
|
|
static void test_dynamic()
|
|
{
|
|
struct queue *q = playlist_get_queue();
|
|
struct db_entry *track, *next;
|
|
unsigned int i, average = 0;
|
|
|
|
/* Set play count to (track_number - 1) (average = 6) */
|
|
db_for_each(track, next, track_db_get()) {
|
|
TRACK(track)->tr_count = TRACK(track)->tr_track - 1;
|
|
average += TRACK(track)->tr_count;
|
|
}
|
|
average /= track_db_get()->db_size;
|
|
|
|
/* Only one unplayed track (tr_track == 1) */
|
|
playlist_select(PL_UNPLAYED);
|
|
test_equal(queue_size(q), 1);
|
|
test_equal(queue_at(q, 0)->tr_track, 1);
|
|
test_equal(queue_at(q, 0)->tr_count, 0);
|
|
|
|
/* Five tracks have tr_count < average && tr_count > 0 */
|
|
playlist_select(PL_LEAST_PLAYED);
|
|
test_equal(queue_size(q), 5);
|
|
for (i = 0; i < queue_size(q); i++) {
|
|
test_loop_equal(queue_at(q, i)->tr_track, i + 2, i);
|
|
test_loop_equal(queue_at(q, i)->tr_count, i + 1, i);
|
|
} test_loop_passed();
|
|
|
|
/* Six tracks have tr_count > average */
|
|
playlist_select(PL_MOST_PLAYED);
|
|
test_equal(queue_size(q), 6);
|
|
for (i = 0; i < queue_size(q); i++) {
|
|
test_loop_equal(queue_at(q, i)->tr_track, i + 8, i);
|
|
test_loop_equal(queue_at(q, i)->tr_count, i + 7, i);
|
|
} test_loop_passed();
|
|
}
|
|
|
|
static void test_deinit()
|
|
{
|
|
struct queue *q = playlist_get_queue();
|
|
|
|
playlist_deinit();
|
|
collection_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),
|
|
);
|