ocarina/tests/gui/idle.c

77 lines
1.8 KiB
C

/*
* Copyright 2016 (c) Anna Schumaker.
*/
#define TEST_NEED_AUDIO
#define TEST_NEED_COLLECTION
#define TEST_NEED_PLAYLIST
#define TEST_NEED_SIDEBAR
#include <core/core.h>
#include <core/idle.h>
#include <gui/builder.h>
#include <gui/idle.h>
#include <tests/gui.h>
#include <tests/test.h>
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 bool inc_cur(void *data)
{
unsigned int expected = GPOINTER_TO_INT(data);
cur++;
func_passed = (cur == expected);
return true;
}
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);
while (idle_run_task()) {}
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(IDLE_SYNC, 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),
);