tests/gui: Add a generic way to run a main loop

I've had to code this in several places, so creating a generic function
is long overdue.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-19 08:53:37 -04:00
parent 90b80fc8a7
commit f9238c34e4
4 changed files with 44 additions and 38 deletions

30
include/tests/gui.h Normal file
View File

@ -0,0 +1,30 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#ifndef OCARINA_TESTS_GUI_H
#define OCARINA_TESTS_GUI_H
static GMainLoop *__gui_test_main_loop;
static int __gui_test_on_idle(gpointer data)
{
g_main_loop_quit(__gui_test_main_loop);
return G_SOURCE_CONTINUE;
}
static void gui_test_init()
{
__gui_test_main_loop = g_main_loop_new(NULL, FALSE);
g_idle_add(__gui_test_on_idle, NULL);
}
static void gui_test_deinit()
{
g_main_loop_unref(__gui_test_main_loop);
}
static void gui_test_main_loop()
{
g_main_loop_run(__gui_test_main_loop);
}
#endif /* OCARINA_TESTS_GUI_H */

View File

@ -9,11 +9,11 @@
#include <gui/view.h>
#include <gui/window.h>
#include <tests/test.h>
#include <tests/gui.h>
static const unsigned int N = 100;
static unsigned int cur = -1;
struct core_init_data init_data;
static GMainLoop *main_loop;
static bool inc_cur(void *data)
{
@ -22,21 +22,12 @@ static bool inc_cur(void *data)
return true;
}
static int test_on_idle()
{
g_main_loop_quit(main_loop);
return G_SOURCE_CONTINUE;
}
static void test_idle()
{
GtkProgressBar *progress;
unsigned int i;
float fraction;
main_loop = g_main_loop_new(NULL, FALSE);
g_idle_add(test_on_idle, gui_window());
progress = GTK_PROGRESS_BAR(gui_builder_widget("o_idle_progress"));
g_assert_false(gtk_widget_is_visible(GTK_WIDGET(progress)));
@ -47,7 +38,7 @@ static void test_idle()
for (i = 0; i < N; i++) {
g_assert_true(gtk_widget_is_visible(GTK_WIDGET(progress)));
g_main_loop_run(main_loop);
gui_test_main_loop();
g_assert_cmpfloat(idle_progress(), ==, (float)(i + 1) / N);
if (i != (N - 1)) {
fraction = gtk_progress_bar_get_fraction(progress);
@ -66,12 +57,14 @@ int main(int argc, char **argv)
core_init(&argc, NULL, &init_data);
gui_builder_init("share/ocarina/ocarina.ui");
gui_view_init();
gui_test_init();
while (idle_run_task()) {}
g_test_init(&argc, &argv, NULL);
g_test_add_func("/Gui/Idle", test_idle);
ret = g_test_run();
gui_test_deinit();
gui_builder_deinit();
core_deinit();
return ret;

View File

@ -10,6 +10,7 @@
#include <gui/model.h>
#include <gui/view.h>
#include <tests/test.h>
#include <tests/gui.h>
const gchar *QUEUE_SETTINGS[Q_MODEL_N_COLUMNS] = {
[Q_MODEL_TRACK_NR] = "gui.queue.track",
@ -25,7 +26,6 @@ const gchar *QUEUE_SETTINGS[Q_MODEL_N_COLUMNS] = {
[Q_MODEL_FONT] = "gui.queue.font",
};
GMainLoop *main_loop;
unsigned int load_count = 0;
static void test_load(struct track *track) { load_count++; }
@ -42,12 +42,6 @@ struct core_init_data init_data = {
.audio_ops = &test_audio_ops,
};
static int test_on_idle(gpointer data)
{
g_main_loop_quit(main_loop);
return G_SOURCE_CONTINUE;
}
static void test_treeview()
{
GtkTreeViewColumn *col;
@ -63,8 +57,6 @@ static void test_treeview()
filter = GTK_TREE_MODEL(gui_view_get_filter());
treeview = GTK_TREE_VIEW(gui_builder_widget("o_treeview"));
main_loop = g_main_loop_new(NULL, FALSE);
g_idle_add(test_on_idle, NULL);
for (i = 0; i < Q_MODEL_N_COLUMNS; i++)
g_assert_false(settings_has(QUEUE_SETTINGS[i]));
@ -76,7 +68,7 @@ static void test_treeview()
gtk_tree_view_column_set_fixed_width(col, (i + 2) * 10);
}
g_main_loop_run(main_loop);
gui_test_main_loop();
for (i = 0; i < Q_MODEL_N_COLUMNS; i++) {
bool has = (i < Q_MODEL_LAST_PLAY);
g_assert(settings_has(QUEUE_SETTINGS[i]) == has);
@ -107,6 +99,7 @@ static void test_treeview()
gtk_tree_path_free(path);
gui_view_set_queue(NULL);
gui_test_deinit();
}
int main(int argc, char **argv)
@ -116,6 +109,7 @@ int main(int argc, char **argv)
gui_builder_init("share/ocarina/ocarina.ui");
gui_queue_model_init();
gui_view_init();
gui_test_init();
while (idle_run_task()) {};
g_test_init(&argc, &argv, NULL);

View File

@ -3,29 +3,18 @@
*/
#include <core/settings.h>
#include <gui/window.h>
#include <tests/gui.h>
#include <tests/test.h>
GMainLoop *main_loop;
static int test_on_idle(gpointer data)
{
g_main_loop_quit(main_loop);
return G_SOURCE_CONTINUE;
}
static void test_window()
{
GtkWindow *window;
main_loop = g_main_loop_new(NULL, FALSE);
window = gui_window();
g_idle_add(test_on_idle, window);
GtkWindow *window = gui_window();
g_assert_false(settings_has("gui.window.width"));
g_assert_false(settings_has("gui.window.height"));
g_assert_cmpstr(gtk_window_get_title(window), ==, "Ocarina " CONFIG_VERSION);
g_main_loop_run(main_loop);
gui_test_main_loop();
g_assert_true(settings_has("gui.window.width"));
g_assert_true(settings_has("gui.window.height"));
@ -33,11 +22,9 @@ static void test_window()
settings_set("gui.window.height", 600);
gui_window_init("share/ocarina/ocarina.png");
g_main_loop_run(main_loop);
gui_test_main_loop();
g_assert_cmpuint(settings_get("gui.window.width"), ==, 800);
g_assert_cmpuint(settings_get("gui.window.height"), ==, 600);
g_main_loop_unref(main_loop);
}
int main(int argc, char **argv)
@ -48,11 +35,13 @@ int main(int argc, char **argv)
gtk_init(&argc, NULL);
gui_builder_init("share/ocarina/ocarina.ui");
gui_window_init("share/ocarina/ocarina.png");
gui_test_init();
g_test_init(&argc, &argv, NULL);
g_test_add_func("/Gui/Window", test_window);
ret = g_test_run();
gui_test_deinit();
gui_window_deinit();
gui_builder_deinit();
settings_deinit();