From fe687e760e2e33911dfc16e640b4a112ba245bb2 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 18 Jan 2011 09:36:24 -0500 Subject: [PATCH] Add testcases for opening display More precisely, test that we can successfully fail to open a display... --- gdk/tests/Makefile.am | 8 +++-- gdk/tests/display.c | 74 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 gdk/tests/display.c diff --git a/gdk/tests/Makefile.am b/gdk/tests/Makefile.am index 5930f4ca33..5cadc6d35c 100644 --- a/gdk/tests/Makefile.am +++ b/gdk/tests/Makefile.am @@ -24,8 +24,12 @@ gdk_color_SOURCES = gdk-color.c gdk_color_LDADD = $(progs_ldadd) TEST_PROGS += encoding -encoding_SOURCES = encoding.c -encoding_LDADD = $(progs_ldadd) +encoding_SOURCES = encoding.c +encoding_LDADD = $(progs_ldadd) + +TEST_PROGS += display +display_SOURCES = display.c +display_LDADD = $(progs_ldadd) CLEANFILES = \ cairosurface.png \ diff --git a/gdk/tests/display.c b/gdk/tests/display.c new file mode 100644 index 0000000000..79fbcfd254 --- /dev/null +++ b/gdk/tests/display.c @@ -0,0 +1,74 @@ +#include + +#include + +static void +test_unset_display (void) +{ + if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) + { + GdkDisplayManager *manager; + + g_unsetenv ("DISPLAY"); + + g_assert (!gdk_init_check (NULL, NULL)); + manager = gdk_display_manager_get (); + g_assert (manager != NULL); + g_assert (gdk_display_manager_get_default_display (manager) == NULL); + + exit (0); + } + g_test_trap_assert_passed (); + + if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) + { + g_unsetenv ("DISPLAY"); + + gdk_init (NULL, NULL); + + exit (0); + } + g_test_trap_assert_failed (); + g_test_trap_assert_stderr ("*cannot open display*"); +} + +static void +test_bad_display (void) +{ + if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) + { + GdkDisplayManager *manager; + + g_setenv ("DISPLAY", "poo", TRUE); + + g_assert (!gdk_init_check (NULL, NULL)); + manager = gdk_display_manager_get (); + g_assert (manager != NULL); + g_assert (gdk_display_manager_get_default_display (manager) == NULL); + + exit (0); + } + g_test_trap_assert_passed (); + + if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) + { + g_setenv ("DISPLAY", "poo", TRUE); + + gdk_init (NULL, NULL); + + exit (0); + } + g_test_trap_assert_failed (); + g_test_trap_assert_stderr ("*cannot open display*"); +} + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/display/unset-display", test_unset_display); + g_test_add_func ("/display/bad-display", test_bad_display); + + return g_test_run (); +}