diff --git a/gui/collection.c b/gui/collection.c index d05b3fbb..5b8b4deb 100644 --- a/gui/collection.c +++ b/gui/collection.c @@ -2,11 +2,11 @@ * Copyright 2015 (c) Anna Schumaker. */ #include -#include #include #include #include #include +#include #include enum collection_sidebar_columns { @@ -52,7 +52,7 @@ void __collection_activated(GtkTreeView *treeview, GtkTreePath *path, return; collection_update(library); - gui_collection_idle_enable(); + gui_idle_enable(); } void __collection_toggled(GtkCheckMenuItem *check, gpointer data) @@ -146,7 +146,7 @@ void __collection_add(GtkButton *button, GtkFileChooser *chooser) gtk_tree_store_insert_before(GTK_TREE_STORE(c_model), &iter, NULL, &last); __collection_set_library(&iter, collection_add(filename)); - gui_collection_idle_enable(); + gui_idle_enable(); g_free(filename); } @@ -168,19 +168,6 @@ void __collection_selection_changed(GtkTreeSelection *selection, } } -static gboolean __collection_on_idle(gpointer data) -{ - GtkProgressBar *progress = GTK_PROGRESS_BAR(data); - - if (idle_run_task()) { - gtk_progress_bar_set_fraction(progress, idle_progress()); - return gtk_widget_is_visible(gui_builder_widget("o_window")); - } else { - gtk_widget_hide(GTK_WIDGET(progress)); - return G_SOURCE_REMOVE; - } -} - static void *__collection_init(struct queue *queue) { return gui_queue_alloc(queue, "Collection", GQ_CAN_RANDOM); @@ -233,15 +220,7 @@ void gui_collection_init() gui_sidebar_on_select, NULL, NULL); gui_sidebar_set_size(gui_queue(collection_get_queue())); - gui_collection_idle_enable(); -} - -void gui_collection_idle_enable() -{ - GtkWidget *progress = gui_builder_widget("o_idle_progress"); - - gtk_widget_show(progress); - g_idle_add(__collection_on_idle, progress); + gui_idle_enable(); } struct queue_ops collection_ops = { diff --git a/gui/idle.c b/gui/idle.c new file mode 100644 index 00000000..79dc6208 --- /dev/null +++ b/gui/idle.c @@ -0,0 +1,27 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#include +#include +#include + +static gboolean __on_idle(gpointer data) +{ + GtkProgressBar *progress = GTK_PROGRESS_BAR(data); + + if (idle_run_task()) { + gtk_progress_bar_set_fraction(progress, idle_progress()); + return gtk_widget_is_visible(gui_builder_widget("o_window")); + } else { + gtk_widget_hide(GTK_WIDGET(progress)); + return G_SOURCE_REMOVE; + } +} + +void gui_idle_enable() +{ + GtkWidget *progress = gui_builder_widget("o_idle_progress"); + + gtk_widget_show(progress); + g_idle_add(__on_idle, progress); +} diff --git a/include/gui/collection.h b/include/gui/collection.h index 4cfaf641..05760ed5 100644 --- a/include/gui/collection.h +++ b/include/gui/collection.h @@ -7,9 +7,6 @@ /* Called to initialize the GUI collection code. */ void gui_collection_init(); -/* Called to enable processing idle queue tasks. */ -void gui_collection_idle_enable(); - /* Collection operations passed to core_init() */ extern struct queue_ops collection_ops; diff --git a/include/gui/idle.h b/include/gui/idle.h new file mode 100644 index 00000000..0101751a --- /dev/null +++ b/include/gui/idle.h @@ -0,0 +1,10 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#ifndef OCARINA_GUI_IDLE_H +#define OCARINA_GUI_IDLE_H + +/* Called to enable processing idle queue tasks. */ +void gui_idle_enable(); + +#endif /* OCARINA_GUI_IDLE_H */ diff --git a/tests/gui/.gitignore b/tests/gui/.gitignore index 13d7ae52..0e060262 100644 --- a/tests/gui/.gitignore +++ b/tests/gui/.gitignore @@ -4,6 +4,7 @@ model view queue window +idle sidebar collection playlist diff --git a/tests/gui/Sconscript b/tests/gui/Sconscript index 694e46f3..a70d6d5b 100644 --- a/tests/gui/Sconscript +++ b/tests/gui/Sconscript @@ -30,6 +30,7 @@ res += [ GuiTest("model") ] res += [ GuiTest("view") ] res += [ GuiTest("queue") ] res += [ GuiTest("window") ] +res += [ GuiTest("idle") ] res += [ GuiTest("sidebar") ] res += [ GuiTest("collection") ] res += [ GuiTest("playlist") ] diff --git a/tests/gui/idle.c b/tests/gui/idle.c new file mode 100644 index 00000000..caac2a62 --- /dev/null +++ b/tests/gui/idle.c @@ -0,0 +1,74 @@ +/* + * Copyright 2016 (c) Anna Schumaker. + */ +#define TEST_NEED_AUDIO +#define TEST_NEED_COLLECTION +#define TEST_NEED_PLAYLIST +#define TEST_NEED_SIDEBAR +#include +#include +#include +#include +#include +#include + +static const unsigned int N = 100; +static unsigned int cur = -1; +static bool func_passed = false; +struct core_init_data init_data; +static GMainLoop *main_loop; + +static void inc_cur(void *data) +{ + unsigned int expected = GPOINTER_TO_INT(data); + cur++; + func_passed = (cur == expected); +} + +static int test_on_idle() +{ + g_main_loop_quit(main_loop); + return G_SOURCE_CONTINUE; +} + +static void test_idle() +{ + GtkProgressBar *progress; + GtkWindow *window; + unsigned int i; + float fraction; + int argc = 0; + + gtk_init(&argc, NULL); + gui_builder_init("share/ocarina/ocarina6.glade"); + core_init(&argc, NULL, &init_data); + + main_loop = g_main_loop_new(NULL, FALSE); + window = GTK_WINDOW(gui_builder_widget("o_window")); + g_idle_add(test_on_idle, window); + + progress = GTK_PROGRESS_BAR(gui_builder_widget("o_idle_progress")); + test_equal(gtk_widget_is_visible(GTK_WIDGET(progress)), false); + + for (i = 0; i < N; i++) + idle_schedule(inc_cur, GINT_TO_POINTER(i)); + + gui_idle_enable(); + + for (i = 0; i < N; i++) { + test_loop_equal(gtk_widget_is_visible(GTK_WIDGET(progress)), true, i); + g_main_loop_run(main_loop); + test_loop_equal(idle_progress(), ((i + 1) / (float)N), i); + test_loop_equal(func_passed, (bool)true, i); + if (i != (N - 1)) { + fraction = gtk_progress_bar_get_fraction(progress); + test_loop_equal(fraction, idle_progress(), i); + } + } test_loop_passed(); + + test_equal(gtk_widget_is_visible(GTK_WIDGET(progress)), false); +} + +DECLARE_UNIT_TESTS( + UNIT_TEST("Idle Progress Bar", test_idle), +);