mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-11-10 10:50:10 +00:00
Added unit tests for Gtk+ testing utilities.
* gtk/tests/testing.c: added a sample test program that tests Gtk+ test utility functions. some g_test_* related portions are disabled and need to be enabled once Gtk+ depends on a new glib with the GLib testing framework integrated. * gtk/tests/Makefile.am: new subdirectory to include quick Gtk+ tests. added exemplary test rules to run tests inside Xvfb. this needs to depend on gtester for full fledged testing. * gtk/Makefile.am: build gtk+/gtk/tests, define -DGTK_ENABLE_BROKEN when collecting _get_type functions to catch e.g. gtk_text_get_type(). * configure.in: create gtk/tests/Makefile.in and gtk/tests/Makefile. svn path=/trunk/; revision=19011
This commit is contained in:
parent
936d27a475
commit
3cf208ef5e
@ -1786,6 +1786,7 @@ gtk/makefile.msc
|
||||
gtk/gtkversion.h
|
||||
gtk/gtk-win32.rc
|
||||
gtk/theme-bits/Makefile
|
||||
gtk/tests/Makefile
|
||||
gtk/xdgmime/Makefile
|
||||
modules/Makefile
|
||||
modules/input/Makefile
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Makefile.am for gtk+/gtk
|
||||
|
||||
SUBDIRS=theme-bits
|
||||
SUBDIRS = theme-bits . tests
|
||||
|
||||
if OS_UNIX
|
||||
SUBDIRS += xdgmime
|
||||
@ -822,7 +822,7 @@ gtktypebuiltins.c: @REBUILD@ $(gtk_public_h_sources) Makefile
|
||||
|
||||
gtktypefuncs.c: @REBUILD@ $(top_srcdir)/gtk/*.h $(top_srcdir)/gdk/*.h Makefile
|
||||
echo '#include <gtk/gtk.h>' > xgen-gtfsrc.c && \
|
||||
${CPP} $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) xgen-gtfsrc.c | \
|
||||
${CPP} $(DEFS) $(INCLUDES) -DGTK_ENABLE_BROKEN $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) xgen-gtfsrc.c | \
|
||||
egrep -o '\<g[td]k_[a-zA-Z0-9_]+_get_type\>' | sort | uniq | \
|
||||
sed '{ s/^/vgt=/; s/$$/();/; }' > xgen-gtf \
|
||||
&& cp xgen-gtf $@ && rm -f xgen-gtf
|
||||
|
52
gtk/tests/Makefile.am
Normal file
52
gtk/tests/Makefile.am
Normal file
@ -0,0 +1,52 @@
|
||||
INCLUDES = \
|
||||
-I$(top_srcdir) \
|
||||
-I$(top_builddir)/gdk \
|
||||
-I$(top_srcdir)/gdk \
|
||||
-DGDK_PIXBUF_DISABLE_DEPRECATED \
|
||||
-DGDK_DISABLE_DEPRECATED \
|
||||
-DGTK_DISABLE_DEPRECATED \
|
||||
$(GTK_DEBUG_FLAGS) \
|
||||
$(GTK_DEP_CFLAGS)
|
||||
|
||||
DEPS = $(top_builddir)/gdk-pixbuf/libgdk_pixbuf-$(GTK_API_VERSION).la \
|
||||
$(top_builddir)/gdk/$(gdktargetlib) \
|
||||
$(top_builddir)/gtk/$(gtktargetlib)
|
||||
|
||||
progs_ldadd = $(top_builddir)/gdk-pixbuf/libgdk_pixbuf-$(GTK_API_VERSION).la \
|
||||
$(top_builddir)/gdk/$(gdktargetlib) \
|
||||
$(top_builddir)/gtk/$(gtktargetlib)
|
||||
|
||||
GTESTER = gtester
|
||||
|
||||
TEST_PROGS =
|
||||
noinst_PROGRAMS = $(TEST_PROGS)
|
||||
|
||||
|
||||
TEST_PROGS += testing
|
||||
testing_SOURCES = testing.c
|
||||
testing_LDADD = $(progs_ldadd)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Xvfb based test rules
|
||||
XVFB = Xvfb
|
||||
test-xvfb:
|
||||
${XVFB} -help 2>/dev/null 1>&2 \
|
||||
&& XID=`for id in \`seq 101 199\` ; do test -e /tmp/.X$$id-lock || { echo $$id; exit 0; }; done; exit 1` \
|
||||
&& { ${XVFB} :$$XID -nolisten tcp -auth /dev/null >/dev/null 2>&1 & \
|
||||
trap "kill -15 $$! " 0 HUP INT QUIT TRAP USR1 PIPE TERM ; } \
|
||||
|| { echo Failed to start Xvfb environment for testing; false; } \
|
||||
&& set -e ; for prg in ${TEST_PROGS} ; do DISPLAY=:$$XID ./$$prg ; done
|
||||
|
||||
# exemplary unit test rules
|
||||
test:
|
||||
set -e ; for prg in ${TEST_PROGS} ; do ./$$prg ; done
|
||||
######### ${GTESTER} --verbose ${TEST_PROGS}
|
||||
test-report:
|
||||
$(MAKE) test
|
||||
######### ${GTESTER} --verbose -k -o testreport.xml ${TEST_PROGS}
|
||||
.PHONY: test test-report
|
||||
check-local: test
|
||||
|
231
gtk/tests/testing.c
Normal file
231
gtk/tests/testing.c
Normal file
@ -0,0 +1,231 @@
|
||||
/* Gtk+ testing utilities
|
||||
* Copyright (C) 2007 Tim Janik
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
#include <gtk/gtk.h>
|
||||
#include <gdk/gdkkeysyms.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
/* --- test functions --- */
|
||||
static void
|
||||
test_button_clicks (void)
|
||||
{
|
||||
int a = 0, b = 0, c = 0;
|
||||
GtkWidget *window = gtk_test_display_button_window ("Test Window",
|
||||
"Test: gtk_test_widget_click",
|
||||
"IgnoreMe1", &a,
|
||||
"ClickMe", &b,
|
||||
"IgnoreMe2", &c,
|
||||
NULL);
|
||||
GtkWidget *button = gtk_test_find_widget (window, "*Click*", GTK_TYPE_BUTTON);
|
||||
gboolean simsuccess;
|
||||
g_assert (button != NULL);
|
||||
simsuccess = gtk_test_widget_click (button, 1, 0);
|
||||
g_assert (simsuccess == TRUE);
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
g_assert (a == 0 && b > 0 && c == 0);
|
||||
gtk_widget_destroy (window); // FIXME: use generic teardown
|
||||
}
|
||||
|
||||
static void
|
||||
test_button_keys (void)
|
||||
{
|
||||
int a = 0, b = 0, c = 0;
|
||||
GtkWidget *window = gtk_test_display_button_window ("Test Window",
|
||||
"Test: gtk_test_widget_send_key",
|
||||
"IgnoreMe1", &a,
|
||||
"ClickMe", &b,
|
||||
"IgnoreMe2", &c,
|
||||
NULL);
|
||||
GtkWidget *button = gtk_test_find_widget (window, "*Click*", GTK_TYPE_BUTTON);
|
||||
gboolean simsuccess;
|
||||
g_assert (button != NULL);
|
||||
gtk_widget_grab_focus (button);
|
||||
g_assert (GTK_WIDGET_HAS_FOCUS (button));
|
||||
simsuccess = gtk_test_widget_send_key (button, GDK_Return, 0);
|
||||
g_assert (simsuccess == TRUE);
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
g_assert (a == 0 && b > 0 && c == 0);
|
||||
gtk_widget_destroy (window); // FIXME: use generic teardown
|
||||
}
|
||||
|
||||
static void
|
||||
test_slider_ranges (void)
|
||||
{
|
||||
GtkWidget *window = gtk_test_create_simple_window ("Test Window", "Test: gtk_test_warp_slider");
|
||||
GtkWidget *hscale = gtk_hscale_new_with_range (-50, +50, 5);
|
||||
gtk_container_add (GTK_CONTAINER (GTK_BIN (window)->child), hscale);
|
||||
gtk_widget_show (hscale);
|
||||
gtk_widget_show_now (window);
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
gtk_test_slider_set_perc (hscale, 0.0);
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
g_assert (gtk_test_slider_get_value (hscale) == -50);
|
||||
gtk_test_slider_set_perc (hscale, 50.0);
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
g_assert (fabs (gtk_test_slider_get_value (hscale)) < 0.0001);
|
||||
gtk_test_slider_set_perc (hscale, 100.0);
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
g_assert (gtk_test_slider_get_value (hscale) == +50.0);
|
||||
gtk_widget_destroy (window); // FIXME: use generic teardown
|
||||
}
|
||||
|
||||
static void
|
||||
test_text_access (void)
|
||||
{
|
||||
const int N_WIDGETS = 4;
|
||||
GtkWidget *widgets[N_WIDGETS];
|
||||
int i = 0;
|
||||
widgets[i++] = gtk_test_create_widget (GTK_TYPE_LABEL, NULL);
|
||||
widgets[i++] = gtk_test_create_widget (GTK_TYPE_ENTRY, NULL);
|
||||
widgets[i++] = gtk_test_create_widget (GTK_TYPE_TEXT_VIEW, NULL);
|
||||
widgets[i++] = gtk_test_create_widget (g_type_from_name ("GtkText"), NULL);
|
||||
g_assert (i == N_WIDGETS);
|
||||
for (i = 0; i < N_WIDGETS; i++)
|
||||
gtk_test_text_set (widgets[i], "foobar");
|
||||
for (i = 0; i < N_WIDGETS; i++)
|
||||
{
|
||||
gchar *text = gtk_test_text_get (widgets[i]);
|
||||
g_assert (strcmp (text, "foobar") == 0);
|
||||
g_free (text);
|
||||
}
|
||||
for (i = 0; i < N_WIDGETS; i++)
|
||||
gtk_test_text_set (widgets[i], "");
|
||||
for (i = 0; i < N_WIDGETS; i++)
|
||||
{
|
||||
gchar *text = gtk_test_text_get (widgets[i]);
|
||||
g_assert (strcmp (text, "") == 0);
|
||||
g_free (text);
|
||||
}
|
||||
for (i = 0; i < N_WIDGETS; i++)
|
||||
gtk_widget_destroy (widgets[i]); // FIXME: use generic teardown
|
||||
}
|
||||
|
||||
static void
|
||||
test_xserver_sync (void)
|
||||
{
|
||||
GtkWidget *window = gtk_test_create_simple_window ("Test Window", "Test: test_xserver_sync");
|
||||
GtkWidget *darea = gtk_drawing_area_new ();
|
||||
GTimer *gtimer = g_timer_new();
|
||||
gint sync_is_slower = 0, repeat = 5;
|
||||
gtk_widget_set_size_request (darea, 320, 200);
|
||||
gtk_container_add (GTK_CONTAINER (GTK_BIN (window)->child), darea);
|
||||
gtk_widget_show (darea);
|
||||
gtk_widget_show_now (window);
|
||||
while (repeat--)
|
||||
{
|
||||
gint i, many = 100;
|
||||
double nosync_time, sync_time;
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
/* run a number of consecutive drawing requests, just using drawing queue */
|
||||
g_timer_start (gtimer);
|
||||
for (i = 0; i < many; i++)
|
||||
{
|
||||
gdk_draw_line (darea->window, darea->style->black_gc, 0, 0, 320, 200);
|
||||
gdk_draw_line (darea->window, darea->style->black_gc, 320, 0, 0, 200);
|
||||
}
|
||||
g_timer_stop (gtimer);
|
||||
nosync_time = g_timer_elapsed (gtimer, NULL);
|
||||
gdk_flush();
|
||||
while (gtk_events_pending ())
|
||||
gtk_main_iteration ();
|
||||
g_timer_start (gtimer);
|
||||
/* run a number of consecutive drawing requests with intermediate drawing syncs */
|
||||
for (i = 0; i < many; i++)
|
||||
{
|
||||
gdk_draw_line (darea->window, darea->style->black_gc, 0, 0, 320, 200);
|
||||
gdk_draw_line (darea->window, darea->style->black_gc, 320, 0, 0, 200);
|
||||
gtk_test_xserver_render_sync (darea->window);
|
||||
}
|
||||
g_timer_stop (gtimer);
|
||||
sync_time = g_timer_elapsed (gtimer, NULL);
|
||||
sync_is_slower += sync_time > nosync_time * 1.5;
|
||||
}
|
||||
g_timer_destroy (gtimer);
|
||||
g_assert (sync_is_slower > 0);
|
||||
gtk_widget_destroy (window); // FIXME: use generic teardown
|
||||
}
|
||||
|
||||
static void
|
||||
test_spin_button_arrows (void)
|
||||
{
|
||||
GtkWidget *window = gtk_test_create_simple_window ("Test Window", "Test: test_spin_button_arrows");
|
||||
GtkWidget *spinner = gtk_spin_button_new_with_range (0, 100, 5);
|
||||
gboolean simsuccess;
|
||||
double oldval, newval;
|
||||
gtk_container_add (GTK_CONTAINER (GTK_BIN (window)->child), spinner);
|
||||
gtk_widget_show (spinner);
|
||||
gtk_widget_show_now (window);
|
||||
gtk_test_slider_set_perc (spinner, 0);
|
||||
/* check initial spinner value */
|
||||
oldval = gtk_test_slider_get_value (spinner);
|
||||
g_assert (oldval == 0);
|
||||
/* check simple increment */
|
||||
simsuccess = gtk_test_spin_button_click (GTK_SPIN_BUTTON (spinner), 1, TRUE);
|
||||
g_assert (simsuccess == TRUE);
|
||||
while (gtk_events_pending ()) /* let spin button timeout/idle handlers update */
|
||||
gtk_main_iteration ();
|
||||
newval = gtk_test_slider_get_value (spinner);
|
||||
g_assert (newval > oldval);
|
||||
/* check maximum warp */
|
||||
simsuccess = gtk_test_spin_button_click (GTK_SPIN_BUTTON (spinner), 3, TRUE);
|
||||
g_assert (simsuccess == TRUE);
|
||||
while (gtk_events_pending ()) /* let spin button timeout/idle handlers update */
|
||||
gtk_main_iteration ();
|
||||
oldval = gtk_test_slider_get_value (spinner);
|
||||
g_assert (oldval == 100);
|
||||
/* check simple decrement */
|
||||
oldval = gtk_test_slider_get_value (spinner);
|
||||
simsuccess = gtk_test_spin_button_click (GTK_SPIN_BUTTON (spinner), 1, FALSE);
|
||||
g_assert (simsuccess == TRUE);
|
||||
while (gtk_events_pending ()) /* let spin button timeout/idle handlers update */
|
||||
gtk_main_iteration ();
|
||||
newval = gtk_test_slider_get_value (spinner);
|
||||
g_assert (newval < oldval);
|
||||
/* check minimum warp */
|
||||
simsuccess = gtk_test_spin_button_click (GTK_SPIN_BUTTON (spinner), 3, FALSE);
|
||||
g_assert (simsuccess == TRUE);
|
||||
while (gtk_events_pending ()) /* let spin button timeout/idle handlers update */
|
||||
gtk_main_iteration ();
|
||||
oldval = gtk_test_slider_get_value (spinner);
|
||||
g_assert (oldval == 0);
|
||||
/* shutdown */
|
||||
gtk_widget_destroy (window); // FIXME: use generic teardown
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
gtk_test_init (&argc, &argv);
|
||||
gtk_test_register_all_types();
|
||||
test_text_access();
|
||||
test_button_clicks();
|
||||
test_button_keys();
|
||||
test_slider_ranges();
|
||||
test_xserver_sync();
|
||||
test_spin_button_arrows();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user