387 lines
15 KiB
C
387 lines
15 KiB
C
/*
|
|
* Copyright 2016 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/idle.h>
|
|
#include <core/playlist.h>
|
|
#include <core/settings.h>
|
|
#include <core/tags/tags.h>
|
|
#include <tests/test.h>
|
|
|
|
|
|
static inline struct playlist *__test_pl_favorites(void)
|
|
{ return playlist_lookup(PL_SYSTEM, "Favorites"); }
|
|
static inline struct playlist *__test_pl_hidden(void)
|
|
{ return playlist_lookup(PL_SYSTEM, "Hidden"); }
|
|
static inline struct playlist *__test_pl_queued(void)
|
|
{ return playlist_lookup(PL_SYSTEM, "Queued Tracks"); }
|
|
static inline struct playlist *__test_pl_collection(void)
|
|
{ return playlist_lookup(PL_SYSTEM, "Collection"); }
|
|
static inline struct playlist *__test_pl_history(void)
|
|
{ return playlist_lookup(PL_SYSTEM, "History"); }
|
|
static inline struct playlist *__test_pl_unplayed(void)
|
|
{ return playlist_lookup(PL_SYSTEM, "Unplayed"); }
|
|
static inline struct playlist *__test_pl_most_played(void)
|
|
{ return playlist_lookup(PL_SYSTEM, "Most Played"); }
|
|
static inline struct playlist *__test_pl_least_played(void)
|
|
{ return playlist_lookup(PL_SYSTEM, "Least Played"); }
|
|
|
|
|
|
static void test_init()
|
|
{
|
|
struct library *library = library_find("tests/Music");
|
|
struct playlist *playlist;
|
|
unsigned int i;
|
|
|
|
g_assert(playlist_current() == __test_pl_collection());
|
|
|
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
|
|
playlist = playlist_get(PL_SYSTEM, i);
|
|
g_assert_nonnull(playlist);
|
|
g_assert(playlist_lookup(PL_SYSTEM, playlist->pl_name) == playlist);
|
|
g_assert_cmpuint(playlist->pl_id, ==, i);
|
|
g_assert_false(playlist_select(playlist));
|
|
|
|
if (i == SYS_PL_QUEUED || i == SYS_PL_HISTORY) {
|
|
g_assert_cmpuint(
|
|
g_slist_length(playlist->pl_queue.q_sort), ==, 0);
|
|
} else
|
|
g_assert_cmpuint(
|
|
g_slist_length(playlist->pl_queue.q_sort), ==, 3);
|
|
}
|
|
|
|
/* Add tracks to the collection. */
|
|
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");
|
|
pl_system_new_track(track_get(0));
|
|
pl_system_new_track(track_get(1));
|
|
pl_system_new_track(track_get(2));
|
|
|
|
g_assert_null(playlist_new(PL_SYSTEM, "New Playlist"));
|
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
|
|
playlist = playlist_get(PL_SYSTEM, i);
|
|
|
|
if (i == SYS_PL_COLLECTION || i == SYS_PL_UNPLAYED)
|
|
g_assert_cmpuint(playlist_size(playlist), ==, 3);
|
|
else
|
|
g_assert_cmpuint(playlist_size(playlist), ==, 0);
|
|
}
|
|
}
|
|
|
|
static void test_random()
|
|
{
|
|
struct playlist *playlist;
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
|
|
playlist = playlist_get(PL_SYSTEM, i);
|
|
|
|
g_assert_false(playlist_get_random(playlist_get(PL_SYSTEM, i)));
|
|
playlist_set_random(playlist, true);
|
|
if (i == SYS_PL_HISTORY)
|
|
g_assert_false(playlist_get_random(playlist));
|
|
else
|
|
g_assert_true(playlist_get_random(playlist));
|
|
|
|
playlist_set_random(playlist, false);
|
|
g_assert_false(playlist_get_random(playlist));
|
|
}
|
|
}
|
|
|
|
static void test_sort()
|
|
{
|
|
struct playlist *playlist;
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
|
|
playlist = playlist_get(PL_SYSTEM, i);
|
|
playlist_clear_sort(playlist);
|
|
|
|
if (i == SYS_PL_HISTORY) {
|
|
g_assert_false(playlist_sort(playlist, COMPARE_TRACK));
|
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 0);
|
|
g_assert_false(playlist_sort(playlist, COMPARE_YEAR));
|
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 0);
|
|
} else {
|
|
g_assert_true(playlist_sort(playlist, COMPARE_TRACK));
|
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 1);
|
|
g_assert_true(playlist_sort(playlist, COMPARE_YEAR));
|
|
g_assert_cmpuint(g_slist_length(playlist->pl_queue.q_sort), ==, 2);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void test_played()
|
|
{
|
|
g_assert_cmpuint(playlist_size(__test_pl_unplayed()), ==, 3);
|
|
g_assert_cmpuint(playlist_size(__test_pl_most_played()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_least_played()), ==, 0);
|
|
|
|
track_played(track_get(0));
|
|
playlist_played(track_get(0));
|
|
while (idle_run_task()) {}
|
|
|
|
g_assert_cmpuint(playlist_size(__test_pl_unplayed()), ==, 2);
|
|
g_assert_cmpuint(playlist_size(__test_pl_most_played()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_least_played()), ==, 1);
|
|
|
|
g_assert_true(playlist_has(__test_pl_least_played(), track_get(0)));
|
|
g_assert_true(playlist_has(__test_pl_unplayed(), track_get(1)));
|
|
g_assert_true(playlist_has(__test_pl_unplayed(), track_get(2)));
|
|
|
|
track_played(track_get(0));
|
|
track_played(track_get(0));
|
|
track_played(track_get(1));
|
|
playlist_played(track_get(0));
|
|
playlist_played(track_get(1));
|
|
while (idle_run_task()) {}
|
|
|
|
g_assert_cmpuint(playlist_size(__test_pl_unplayed()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_most_played()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_least_played()), ==, 1);
|
|
|
|
g_assert_true(playlist_has(__test_pl_most_played(), track_get(0)));
|
|
g_assert_true(playlist_has(__test_pl_least_played(), track_get(1)));
|
|
g_assert_true(playlist_has(__test_pl_unplayed(), track_get(2)));
|
|
}
|
|
|
|
static void test_add()
|
|
{
|
|
struct playlist *playlist;
|
|
unsigned int i;
|
|
|
|
g_assert_true( playlist_add(__test_pl_favorites(), track_get(0)));
|
|
g_assert_false(playlist_add(__test_pl_favorites(), track_get(0)));
|
|
g_assert_true( playlist_add(__test_pl_queued(), track_get(0)));
|
|
g_assert_false(playlist_add(__test_pl_queued(), track_get(0)));
|
|
g_assert_true( playlist_add(__test_pl_history(), track_get(0)));
|
|
g_assert_true( playlist_add(__test_pl_history(), track_get(0)));
|
|
|
|
g_assert_cmpuint(playlist_size(__test_pl_favorites()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_history()), ==, 2);
|
|
|
|
g_assert_true(playlist_has(__test_pl_favorites(), track_get(0)));
|
|
g_assert_true(playlist_has(__test_pl_queued(), track_get(0)));
|
|
g_assert_true(playlist_has(__test_pl_history(), track_get(0)));
|
|
|
|
g_assert_true( playlist_add(__test_pl_hidden(), track_get(0)));
|
|
g_assert_true( playlist_add(__test_pl_hidden(), track_get(1)));
|
|
g_assert_true( playlist_add(__test_pl_hidden(), track_get(2)));
|
|
g_assert_false(playlist_add(__test_pl_collection(), track_get(0)));
|
|
g_assert_false(playlist_add(__test_pl_collection(), track_get(1)));
|
|
g_assert_false(playlist_add(__test_pl_collection(), track_get(2)));
|
|
g_assert_false(playlist_add(__test_pl_most_played(), track_get(0)));
|
|
g_assert_false(playlist_add(__test_pl_least_played(), track_get(1)));
|
|
g_assert_false(playlist_add(__test_pl_unplayed(), track_get(2)));
|
|
|
|
g_assert_cmpuint(playlist_size(__test_pl_hidden()), ==, 3);
|
|
g_assert_cmpuint(playlist_size(__test_pl_collection()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_most_played()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_least_played()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_unplayed()), ==, 0);
|
|
|
|
g_assert_true( playlist_has(__test_pl_hidden(), track_get(0)));
|
|
g_assert_true( playlist_has(__test_pl_hidden(), track_get(1)));
|
|
g_assert_true( playlist_has(__test_pl_hidden(), track_get(2)));
|
|
g_assert_false(playlist_has(__test_pl_collection(), track_get(0)));
|
|
g_assert_false(playlist_has(__test_pl_collection(), track_get(1)));
|
|
g_assert_false(playlist_has(__test_pl_collection(), track_get(2)));
|
|
g_assert_false(playlist_has(__test_pl_most_played(), track_get(0)));
|
|
g_assert_false(playlist_has(__test_pl_least_played(), track_get(1)));
|
|
g_assert_false(playlist_has(__test_pl_unplayed(), track_get(2)));
|
|
|
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
|
|
playlist = playlist_get(PL_SYSTEM, i);
|
|
|
|
switch (i) {
|
|
case SYS_PL_FAVORITES:
|
|
case SYS_PL_HIDDEN:
|
|
case SYS_PL_QUEUED:
|
|
g_assert_true( playlist_select(playlist));
|
|
g_assert_false(playlist_select(playlist));
|
|
g_assert(playlist_current() == playlist);
|
|
break;
|
|
default:
|
|
g_assert_false(playlist_select(playlist));
|
|
g_assert(playlist_current() != playlist);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void test_remove()
|
|
{
|
|
struct playlist *playlist;
|
|
unsigned int i;
|
|
|
|
g_assert_true( playlist_remove(__test_pl_favorites(), track_get(0)));
|
|
g_assert_false(playlist_remove(__test_pl_favorites(), track_get(0)));
|
|
g_assert_true( playlist_remove(__test_pl_queued(), track_get(0)));
|
|
g_assert_false(playlist_remove(__test_pl_queued(), track_get(0)));
|
|
g_assert_false(playlist_remove(__test_pl_history(), track_get(0)));
|
|
|
|
g_assert_cmpuint(playlist_size(__test_pl_favorites()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_history()), ==, 2);
|
|
|
|
g_assert_false(playlist_has(__test_pl_favorites(), track_get(0)));
|
|
g_assert_false(playlist_has(__test_pl_queued(), track_get(0)));
|
|
g_assert_true( playlist_has(__test_pl_history(), track_get(0)));
|
|
|
|
g_assert_true( playlist_remove(__test_pl_hidden(), track_get(0)));
|
|
g_assert_true( playlist_remove(__test_pl_hidden(), track_get(1)));
|
|
g_assert_true( playlist_remove(__test_pl_hidden(), track_get(2)));
|
|
g_assert_false(playlist_remove(__test_pl_most_played(), track_get(0)));
|
|
g_assert_false(playlist_remove(__test_pl_least_played(), track_get(1)));
|
|
g_assert_false(playlist_remove(__test_pl_unplayed(), track_get(2)));
|
|
|
|
g_assert_cmpuint(playlist_size(__test_pl_hidden()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_collection()), ==, 3);
|
|
g_assert_cmpuint(playlist_size(__test_pl_most_played()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_least_played()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_unplayed()), ==, 1);
|
|
|
|
g_assert_false(playlist_has(__test_pl_hidden(), track_get(0)));
|
|
g_assert_false(playlist_has(__test_pl_hidden(), track_get(1)));
|
|
g_assert_false(playlist_has(__test_pl_hidden(), track_get(2)));
|
|
g_assert_true( playlist_has(__test_pl_collection(), track_get(0)));
|
|
g_assert_true( playlist_has(__test_pl_collection(), track_get(1)));
|
|
g_assert_true( playlist_has(__test_pl_collection(), track_get(2)));
|
|
g_assert_true( playlist_has(__test_pl_most_played(), track_get(0)));
|
|
g_assert_true( playlist_has(__test_pl_least_played(), track_get(1)));
|
|
g_assert_true( playlist_has(__test_pl_unplayed(), track_get(2)));
|
|
|
|
g_assert_true(playlist_remove(__test_pl_collection(), track_get(0)));
|
|
g_assert_cmpuint(playlist_size(__test_pl_hidden()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_collection()), ==, 2);
|
|
g_assert_true (playlist_has(__test_pl_hidden(), track_get(0)));
|
|
g_assert_false(playlist_has(__test_pl_collection(), track_get(0)));
|
|
g_assert_true(playlist_remove(__test_pl_hidden(), track_get(0)));
|
|
|
|
g_assert_true(playlist_select(__test_pl_least_played()));
|
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++) {
|
|
playlist = playlist_get(PL_SYSTEM, i);
|
|
|
|
switch (i) {
|
|
case SYS_PL_FAVORITES:
|
|
case SYS_PL_HIDDEN:
|
|
case SYS_PL_QUEUED:
|
|
case SYS_PL_HISTORY:
|
|
g_assert_false(playlist_select(playlist));
|
|
g_assert(playlist_current() != playlist);
|
|
break;
|
|
default:
|
|
g_assert_true( playlist_select(playlist));
|
|
g_assert_false(playlist_select(playlist));
|
|
g_assert(playlist_current() == playlist);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void test_delete()
|
|
{
|
|
unsigned int i;
|
|
|
|
playlist_add(__test_pl_favorites(), track_get(0));
|
|
playlist_add(__test_pl_hidden(), track_get(1));
|
|
playlist_add(__test_pl_hidden(), track_get(2));
|
|
playlist_add(__test_pl_queued(), track_get(0));
|
|
playlist_add(__test_pl_history(), track_get(0));
|
|
|
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++)
|
|
g_assert_false(playlist_delete(playlist_get(PL_SYSTEM, i)));
|
|
|
|
g_assert_cmpuint(playlist_size(__test_pl_favorites()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_hidden()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 0);
|
|
g_assert_cmpuint(playlist_size(__test_pl_collection()), ==, 3);
|
|
g_assert_cmpuint(playlist_size(__test_pl_history()), ==, 3);
|
|
g_assert_cmpuint(playlist_size(__test_pl_unplayed()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_most_played()), ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_least_played()), ==, 1);
|
|
}
|
|
|
|
static void test_next()
|
|
{
|
|
playlist_select(__test_pl_collection());
|
|
g_assert_cmpuint(playlist_next()->tr_track, ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_collection()), ==, 3);
|
|
g_assert_cmpuint(playlist_next()->tr_track, ==, 2);
|
|
g_assert_cmpuint(playlist_size(__test_pl_collection()), ==, 3);
|
|
g_assert_cmpuint(playlist_next()->tr_track, ==, 3);
|
|
g_assert_cmpuint(playlist_size(__test_pl_collection()), ==, 3);
|
|
g_assert_cmpuint(playlist_next()->tr_track, ==, 1);
|
|
g_assert_cmpuint(playlist_size(__test_pl_collection()), ==, 3);
|
|
|
|
playlist_add(__test_pl_queued(), track_get(0));
|
|
playlist_add(__test_pl_queued(), track_get(1));
|
|
g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 2);
|
|
g_assert(playlist_current() == __test_pl_queued());
|
|
|
|
g_assert(playlist_next() == track_get(0));
|
|
g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 1);
|
|
g_assert(playlist_current() == __test_pl_queued());
|
|
|
|
g_assert(playlist_next() == track_get(1));
|
|
g_assert_cmpuint(playlist_size(__test_pl_queued()), ==, 0);
|
|
g_assert(playlist_current() == __test_pl_collection());
|
|
|
|
queue_clear(&__test_pl_history()->pl_queue);
|
|
g_assert_cmpuint(playlist_size(__test_pl_history()), ==, 0);
|
|
g_assert_true(playlist_add(__test_pl_history(), track_get(0)));
|
|
g_assert_true(playlist_add(__test_pl_history(), track_get(1)));
|
|
g_assert_true(playlist_add(__test_pl_history(), track_get(2)));
|
|
g_assert_cmpuint(playlist_size(__test_pl_history()), ==, 3);
|
|
g_assert(playlist_prev() == track_get(1));
|
|
g_assert(playlist_prev() == track_get(0));
|
|
g_assert(playlist_prev() == track_get(2));
|
|
g_assert_cmpuint(playlist_size(__test_pl_history()), ==, 3);
|
|
|
|
}
|
|
|
|
static void test_delete_tracks()
|
|
{
|
|
unsigned int i;
|
|
|
|
g_assert_true(playlist_add(__test_pl_favorites(), track_get(0)));
|
|
g_assert_true(playlist_add(__test_pl_hidden(), track_get(1)));
|
|
g_assert_true(playlist_add(__test_pl_queued(), track_get(0)));
|
|
|
|
pl_system_delete_track(track_get(0));
|
|
pl_system_delete_track(track_get(1));
|
|
pl_system_delete_track(track_get(2));
|
|
|
|
for (i = 0; i < SYS_PL_NUM_PLAYLISTS; i++)
|
|
g_assert_cmpuint(playlist_size(playlist_get(PL_SYSTEM, i)), ==, 0);
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int ret;
|
|
|
|
idle_init_sync();
|
|
settings_init();
|
|
tags_init();
|
|
playlist_init(NULL, NULL);
|
|
while (idle_run_task()) {};
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
g_test_add_func("/Core/Playlists/System/Init", test_init);
|
|
g_test_add_func("/Core/Playlists/System/Random", test_random);
|
|
g_test_add_func("/Core/Playlists/System/Sort", test_sort);
|
|
g_test_add_func("/Core/Playlists/System/Played", test_played);
|
|
g_test_add_func("/Core/Playlists/System/Add Tracks", test_add);
|
|
g_test_add_func("/Core/Playlists/System/Remove Tracks", test_remove);
|
|
g_test_add_func("/Core/Playlists/System/Delete", test_delete);
|
|
g_test_add_func("/Core/Playlists/System/Next", test_next);
|
|
g_test_add_func("/Core/PLaylists/System/Delete Tracks", test_delete_tracks);
|
|
ret = g_test_run();
|
|
|
|
playlist_deinit();
|
|
tags_deinit();
|
|
settings_deinit();
|
|
idle_deinit();
|
|
return ret;
|
|
}
|