[gtk/window-destroy: 71/74] testsuite: add widget refcount test case



commit 2e4b1e38a7ca16539842be6fe5e19d413ce569ba
Author: Timm Bäder <mail baedert org>
Date:   Tue Sep 26 17:04:51 2017 +0200

    testsuite: add widget refcount test case
    
    Testing toplevels and popovers.

 testsuite/gtk/meson.build       |   1 +
 testsuite/gtk/widget-refcount.c | 101 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+)
---
diff --git a/testsuite/gtk/meson.build b/testsuite/gtk/meson.build
index b7440b5529..9df9b0c0d8 100644
--- a/testsuite/gtk/meson.build
+++ b/testsuite/gtk/meson.build
@@ -66,6 +66,7 @@ tests = [
   ['displayclose'],
   ['revealer-size'],
   ['widgetorder'],
+  ['widget-refcount'],
 ]
 
 # Tests that are expected to fail
diff --git a/testsuite/gtk/widget-refcount.c b/testsuite/gtk/widget-refcount.c
new file mode 100644
index 0000000000..d4fc5d8be6
--- /dev/null
+++ b/testsuite/gtk/widget-refcount.c
@@ -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 ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]