ocarina/tests/core/tags/album.c

203 lines
6.3 KiB
C

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/idle.h>
#include <core/tags/album.h>
#include <core/tags/genre.h>
#include <tests/test.h>
static void test_verify_empty(struct album *album)
{
const struct db_ops *album_ops = test_album_ops();
g_assert_cmpstr(album->al_name, ==, "");
g_assert_null(album->al_tokens[0]);
g_assert_null(album->al_alts[0]);
g_assert(album->al_artist == artist_get(0));
g_assert(album->al_genre == genre_get(0));
g_assert_cmpuint(album->al_year, ==, 0);
g_assert_cmpstr_free(album_ops->dbe_key(&album->al_dbe), ==, "0/0/0/");
}
static void test_verify_hyrule(struct album *album, struct artist *artist,
struct genre *genre)
{
const struct db_ops *album_ops = test_album_ops();
gchar *key;
g_assert_cmpstr(album->al_name, ==, "Hyrule Symphony");
g_assert_cmpstr(album->al_tokens[0], ==, "hyrule");
g_assert_cmpstr(album->al_tokens[1], ==, "symphony");
g_assert_null(album->al_tokens[2]);
g_assert_null(album->al_alts[0]);
g_assert(album->al_artist == artist);
g_assert(album->al_genre == genre);
g_assert_cmpuint(album->al_year, ==, 1998);
key = album_ops->dbe_key(&album->al_dbe);
if (artist && genre)
g_assert_cmpstr_free(key, ==, "0/0/1998/Hyrule Symphony");
else
g_assert_cmpstr_free(key, ==, "1998/Hyrule Symphony");
}
static void test_album()
{
const struct db_ops *album_ops = test_album_ops();
struct album *album;
struct artist *koji;
struct genre *genre;
struct file f;
idle_init();
koji = artist_find("Koji Kondo");
genre = genre_find("Video Game Music");
album = ALBUM(album_ops->dbe_alloc("0/0/1998/Hyrule Symphony"));
test_verify_hyrule(album, koji, genre);
g_assert_true( album_match_token(album, "hyrule"));
g_assert_true( album_match_token(album, "symphony"));
g_assert_false(album_match_token(album, "skyward"));
file_init(&f, "album_tag", 1);
file_open(&f, OPEN_WRITE);
file_writef(&f, "0 0 0 \n");
album_ops->dbe_write(&f, &album->al_dbe);
file_close(&f);
album_ops->dbe_free(&album->al_dbe);
file_open(&f, OPEN_READ);
album = ALBUM(album_ops->dbe_read(&f));
test_verify_empty(album);
album_ops->dbe_free(&album->al_dbe);
album = ALBUM(album_ops->dbe_read(&f));
file_close(&f);
test_verify_hyrule(album, koji, genre);
album_ops->dbe_free(&album->al_dbe);
}
static void test_album_compare()
{
const struct db_ops *album_ops = test_album_ops();
struct album *twilight, *skyward;
twilight = ALBUM(album_ops->dbe_alloc("2006/Twilight Princess"));
skyward = ALBUM(album_ops->dbe_alloc("2011/skyward sword"));
g_assert_cmpint(album_compare(twilight, twilight), ==, 0);
g_assert_cmpint(album_compare(twilight, skyward), ==, 1);
g_assert_cmpint(album_compare(skyward, twilight), ==, -1);
g_assert_cmpint(album_compare_year(twilight, twilight), ==, 0);
g_assert_cmpint(album_compare_year(twilight, skyward), ==, -5);
g_assert_cmpint(album_compare_year(skyward, twilight), ==, 5);
skyward->al_year = 2006;
g_assert_cmpint(album_compare_year(twilight, twilight), ==, 0);
g_assert_cmpint(album_compare_year(twilight, skyward), ==, 1);
g_assert_cmpint(album_compare_year(skyward, twilight), ==, -1);
album_ops->dbe_free(&twilight->al_dbe);
album_ops->dbe_free(&skyward->al_dbe);
}
static void test_album_db()
{
const struct db_ops *album_ops = test_album_ops();
struct database album_db;
struct artist *koji;
struct genre *genre;
struct album *album;
koji = artist_find("Koji Kondo");
genre = genre_find("Video Game Music");
album = album_find(koji, genre, "Hyrule Symphony", 1998);
test_verify_hyrule(album, koji, genre);
g_assert(album_find(koji, genre, "Hyrule Symphony", 1998) == album);
g_assert(album_get(0) == album);
g_assert_null(album_get(1));
db_init(&album_db, "album.db", false, album_ops, 0);
db_load(&album_db);
g_assert_cmpuint(album_db.db_size, ==, 1);
db_deinit(&album_db);
}
#ifdef CONFIG_TESTING_ARTWORK
static void test_album_artwork()
{
struct album *ocarina, *majora, *wind, *ocarina_3d;
gchar *o_path, *m_path, *w_path;
struct artist *koji;
struct genre *genre;
const gchar *cache;
idle_deinit();
idle_init();
cache = g_get_user_cache_dir();
o_path = g_build_filename(cache, "ocarina-test", "core", "tags", "album", "1998", "Ocarina of Time.jpg", NULL);
m_path = g_build_filename(cache, "ocarina-test", "core", "tags", "album", "2000", "Majora%27s Mask.jpg", NULL);
w_path = g_build_filename(cache, "ocarina-test", "core", "tags", "album", "0", "Wind Waker.jpg", NULL);
koji = artist_find("Koji Kondo");
genre = genre_find("Video Game Music");
ocarina = album_find(koji, genre, "Ocarina of Time", 1998);
majora = album_find(koji, genre, "Majora's Mask", 2000);
wind = album_find(koji, genre, "Wind Waker", 0);
ocarina->al_artist = koji;
g_assert_false(album_artwork_exists(ocarina));
g_assert_false(album_artwork_exists(majora));
g_assert_false(album_artwork_exists(wind));
g_assert_null(album_artwork_path(ocarina));
g_assert_null(album_artwork_path(majora));
g_assert_null(album_artwork_path(wind));
while (idle_run_task()) {}
g_assert_true(album_artwork_exists(ocarina));
g_assert_true(album_artwork_exists(majora));
g_assert_true(album_artwork_exists(wind));
g_assert_cmpstr_free(album_artwork_path(ocarina), ==, o_path);
g_assert_cmpstr_free(album_artwork_path(majora), ==, m_path);
g_assert_cmpstr_free(album_artwork_path(wind), ==, w_path);
/* Import the original Ocarina of Time album art. */
ocarina_3d = album_find(koji, genre, "Ocarina of Time 3D", 2011);
g_assert_false(album_artwork_exists(ocarina_3d));
album_artwork_import(ocarina_3d, NULL);
g_assert_false(album_artwork_exists(ocarina_3d));
album_artwork_import(ocarina_3d, o_path);
g_assert_true(album_artwork_exists(ocarina_3d));
g_free(o_path);
g_free(m_path);
g_free(w_path);
}
#endif /* CONFIG_TESTING_ARTWORK */
int main(int argc, char **argv)
{
int ret;
idle_init();
artist_db_init();
genre_db_init();
album_db_init();
g_test_init(&argc, &argv, NULL);
g_test_add_func("/Core/Tags/Album", test_album);
g_test_add_func("/Core/Tags/Album/Comparison", test_album_compare);
g_test_add_func("/Core/Tags/Album/Database", test_album_db);
#ifdef CONFIG_TESTING_ARTWORK
g_test_add_func("/Core/Tags/Album/Artwork", test_album_artwork);
#endif /* CONFIG_TESTING_ARTWORK */
ret = g_test_run();
album_db_deinit();
artist_db_deinit();
idle_deinit();
return ret;
}