From e550638823e39551e4931cc7486c79826f2dc9b1 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 31 Aug 2016 08:08:26 -0400 Subject: [PATCH] gui/playlists/library: Write unit test for adding file paths This functionality needs to be tested better. I can't really test the dialog, since it runs in the main thread, but I can test adding library paths with the selected directory. Signed-off-by: Anna Schumaker --- gui/ocarina.c | 2 - gui/playlist.c | 2 + gui/playlists/library.c | 49 +++++++++++---------- include/gui/playlist.h | 2 +- include/gui/playlists/library.h | 3 ++ tests/gui/CMakeLists.txt | 2 + tests/gui/playlists/.gitignore | 1 + tests/gui/playlists/CMakeLists.txt | 7 +++ tests/gui/playlists/library.c | 70 ++++++++++++++++++++++++++++++ 9 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 tests/gui/playlists/.gitignore create mode 100644 tests/gui/playlists/CMakeLists.txt create mode 100644 tests/gui/playlists/library.c diff --git a/gui/ocarina.c b/gui/ocarina.c index 59dd86b3..ac8d6f26 100644 --- a/gui/ocarina.c +++ b/gui/ocarina.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -108,7 +107,6 @@ static void __ocarina_startup(GApplication *application, gpointer data) gui_treeview_init(); gui_sidebar_init(); gui_view_init(); - gui_pl_library_init(); gui_playlist_init(); gui_audio_init(); diff --git a/gui/playlist.c b/gui/playlist.c index 41c0fc70..9ce34c64 100644 --- a/gui/playlist.c +++ b/gui/playlist.c @@ -182,6 +182,8 @@ void gui_playlist_init() "audio-x-generic"); idle_schedule(IDLE_SYNC, __gui_playlist_init_idle, NULL); + + gui_pl_library_init(); } const gchar *gui_playlist_cur() diff --git a/gui/playlists/library.c b/gui/playlists/library.c index 69c55e83..7a2d2e96 100644 --- a/gui/playlists/library.c +++ b/gui/playlists/library.c @@ -8,27 +8,12 @@ #include #include -static void __gui_pl_library_add(GtkFileChooser *chooser) -{ - gchar *filename = gtk_file_chooser_get_filename(chooser); - struct playlist *playlist; - struct library *library; - - if (playlist_new(PL_LIBRARY, filename)) { - library = library_lookup(filename); - playlist = playlist_get(PL_LIBRARY, library->li_path); - gui_playlist_add_library(playlist); - } - - gui_idle_enable(); - g_free(filename); -} - void __gui_pl_library_choose(GtkButton *button, gpointer data) { GtkFileFilter *filter; + const gchar *music; GtkWidget *dialog; - const gchar *path; + gchar *path; gint res; filter = gtk_file_filter_new(); @@ -42,14 +27,17 @@ void __gui_pl_library_choose(GtkButton *button, gpointer data) gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT); gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter); - path = g_get_user_special_dir(G_USER_DIRECTORY_MUSIC); - if (path) + music = g_get_user_special_dir(G_USER_DIRECTORY_MUSIC); + if (music) gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), - path); + music); res = gtk_dialog_run(GTK_DIALOG(dialog)); - if (res == GTK_RESPONSE_ACCEPT) - __gui_pl_library_add(GTK_FILE_CHOOSER(dialog)); + if (res == GTK_RESPONSE_ACCEPT) { + path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + gui_pl_library_add(path); + g_free(path); + } gtk_widget_destroy(dialog); } @@ -64,8 +52,10 @@ static bool __gui_pl_library_init_idle() gui_playlist_add_library(playlist); } +#ifndef CONFIG_TESTING if (library_db_get()->db_size == 0) __gui_pl_library_choose(NULL, NULL); +#endif /* CONFIG_TESTING */ return true; } @@ -74,3 +64,18 @@ void gui_pl_library_init() { idle_schedule(IDLE_SYNC, __gui_pl_library_init_idle, NULL); } + +bool gui_pl_library_add(const gchar *filename) +{ + struct playlist *playlist; + struct library *library; + + if (playlist_new(PL_LIBRARY, filename)) { + library = library_lookup(filename); + playlist = playlist_get(PL_LIBRARY, library->li_path); + gui_playlist_add_library(playlist); + gui_idle_enable(); + return true; + } + return false; +} diff --git a/include/gui/playlist.h b/include/gui/playlist.h index 5a8a0d47..31bb9ba8 100644 --- a/include/gui/playlist.h +++ b/include/gui/playlist.h @@ -3,8 +3,8 @@ */ #ifndef OCARINA_GUI_PLAYLIST_H #define OCARINA_GUI_PLAYLIST_H - #include +#include /* Called to initialize the GUI playlist code. */ void gui_playlist_init(); diff --git a/include/gui/playlists/library.h b/include/gui/playlists/library.h index c1a19f8d..0c2c0fe8 100644 --- a/include/gui/playlists/library.h +++ b/include/gui/playlists/library.h @@ -7,4 +7,7 @@ /* Called to initialize GUI library playlists. */ void gui_pl_library_init(); +/* Called to add a library path. */ +bool gui_pl_library_add(const gchar *); + #endif /* OCARINA_GUI_PLAYLISTS_LIBRARY_H */ diff --git a/tests/gui/CMakeLists.txt b/tests/gui/CMakeLists.txt index 3ea62242..eacb8163 100644 --- a/tests/gui/CMakeLists.txt +++ b/tests/gui/CMakeLists.txt @@ -11,6 +11,8 @@ gui_unit_test(Model) gui_unit_test(Filter) gui_unit_test(Treeview) gui_unit_test(Sidebar) + +add_subdirectory(playlists/) gui_unit_test(View) gui_unit_test(Playlist) gui_unit_test(Audio) diff --git a/tests/gui/playlists/.gitignore b/tests/gui/playlists/.gitignore new file mode 100644 index 00000000..14d6358d --- /dev/null +++ b/tests/gui/playlists/.gitignore @@ -0,0 +1 @@ +library diff --git a/tests/gui/playlists/CMakeLists.txt b/tests/gui/playlists/CMakeLists.txt new file mode 100644 index 00000000..2193ad4d --- /dev/null +++ b/tests/gui/playlists/CMakeLists.txt @@ -0,0 +1,7 @@ +Include(../../UnitTest.cmake) + +function(gui_playlist_unit_test name) + unit_test(Gui/Playlists ${name} guilib ${corefiles}) +endfunction() + +gui_playlist_unit_test(Library) diff --git a/tests/gui/playlists/library.c b/tests/gui/playlists/library.c new file mode 100644 index 00000000..cfc718c7 --- /dev/null +++ b/tests/gui/playlists/library.c @@ -0,0 +1,70 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +struct core_init_data init_data = { + .playlist_ops = &playlist_ops, +}; + +static void test_library() +{ + GtkTreeModel *model = gui_sidebar_model(); + struct playlist *playlist; + GtkTreeIter iter; + + gui_sidebar_iter_first(&iter); + gui_sidebar_iter_find(&iter, "Library", PL_MAX_TYPE); + g_assert_false(gtk_tree_model_iter_has_child(model, &iter)); + + g_assert_cmpuint(library_db_get()->db_size, ==, 0); + g_assert_null(playlist_get(PL_LIBRARY, "tests/Music/Hyrule Symphony")); + + g_assert_true(gui_pl_library_add("tests/Music/Hyrule Symphony")); + playlist = playlist_get(PL_LIBRARY, "tests/Music/Hyrule Symphony"); + g_assert_cmpuint(library_db_get()->db_size, ==, 1); + g_assert_cmpuint(playlist_size(playlist->pl_type, + playlist->pl_name), ==, 0); + g_assert_true(gtk_tree_model_iter_has_child(model, &iter)); + g_assert_cmpuint(gtk_tree_model_iter_n_children(model, &iter), ==, 1); + + while (idle_run_task()) {} + g_assert_cmpuint(library_db_get()->db_size, ==, 1); + g_assert_cmpuint(playlist_size(playlist->pl_type, + playlist->pl_name), ==, 13); + g_assert_true(gtk_tree_model_iter_has_child(model, &iter)); + g_assert_cmpuint(gtk_tree_model_iter_n_children(model, &iter), ==, 1); +} + +int main(int argc, char **argv) +{ + int ret; + + gtk_init(&argc, NULL); + core_init(&argc, &argv, &init_data); + gui_builder_init("share/ocarina/ocarina.ui"); + gui_model_init(); + gui_filter_init(); + gui_treeview_init(); + gui_sidebar_init(); + gui_playlist_init(); + while (idle_run_task()) {} + + g_test_init(&argc, &argv, NULL); + g_test_add_func("/Gui/Playlists/Library", test_library); + ret = g_test_run(); + + core_deinit(); + gui_treeview_deinit(); + gui_filter_deinit(); + gui_model_deinit(); + gui_builder_deinit(); + return ret; +}