testsuite: add widget refcount test case

Testing toplevels and popovers.
This commit is contained in:
Timm Bäder 2017-09-26 17:04:51 +02:00 committed by Matthias Clasen
parent 0f10b170f4
commit e5d6b493c0
2 changed files with 102 additions and 0 deletions

View File

@ -66,6 +66,7 @@ tests = [
['displayclose'],
['revealer-size'],
['widgetorder'],
['widget-refcount'],
]
# Tests that are expected to fail

View File

@ -0,0 +1,101 @@
#include <gtk/gtk.h>
static void
check_finalized (gpointer data,
GObject *where_the_object_was)
{
gboolean *did_finalize = (gboolean *)data;
*did_finalize = TRUE;
}
static void
popover (void)
{
GtkWidget *button = gtk_menu_button_new ();
GtkWidget *p = gtk_popover_new ();
gboolean finalized = FALSE;
gtk_menu_button_set_popover (GTK_MENU_BUTTON (button), p);
/* GtkButton is a normal widget and thus floating */
g_assert (g_object_is_floating (button));
/* GtkPopver sinks itself */
g_assert (!g_object_is_floating (p));
g_object_weak_ref (G_OBJECT (p), check_finalized, &finalized);
g_object_ref_sink (button);
g_object_unref (button);
/* We do NOT unref p since the only reference held to it gets
* removed when the button gets disposed. */
g_assert (finalized);
}
static void
popover2 (void)
{
GtkWidget *button = gtk_menu_button_new ();
GtkWidget *p = gtk_popover_new ();
gboolean finalized = FALSE;
gtk_menu_button_set_popover (GTK_MENU_BUTTON (button), p);
g_assert (g_object_is_floating (button));
g_assert (!g_object_is_floating (p));
g_object_weak_ref (G_OBJECT (p), check_finalized, &finalized);
g_object_ref_sink (button);
gtk_menu_button_set_popover (GTK_MENU_BUTTON (button), NULL);
g_assert (finalized);
g_object_unref (button);
}
static void
filechooserwidget (void)
{
/* We use GtkFileChooserWidget simply because it's a complex widget, that's it. */
GtkWidget *w = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
gboolean finalized = FALSE;
g_assert (g_object_is_floating (w));
g_object_ref_sink (w);
g_object_weak_ref (G_OBJECT (w), check_finalized, &finalized);
g_object_unref (w);
g_assert (finalized);
}
static void
window (void)
{
GtkWidget *w = gtk_window_new ();
gboolean finalized = FALSE;
/* GTK holds a ref */
g_assert (!g_object_is_floating (w));
g_object_weak_ref (G_OBJECT (w), check_finalized, &finalized);
gtk_window_destroy (GTK_WINDOW (w));
g_assert (finalized);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
gtk_init ();
g_test_add_func ("/gtk/widget-refcount/popover", popover);
g_test_add_func ("/gtk/widget-refcount/popover2", popover2);
g_test_add_func ("/gtk/widget-refcount/filechoosewidget", filechooserwidget);
g_test_add_func ("/gtk/widget-refcount/window", window);
return g_test_run ();
}