summaryrefslogtreecommitdiff
path: root/testsuite/gtk/widget-refcount.c
blob: cebd30982a84a976f839256c5adcbee8724fca5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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_true (g_object_is_floating (button));
  /* GtkPopver sinks itself */
  g_assert_true (!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_true (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_true (g_object_is_floating (button));
  g_assert_true (!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_true (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_true (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_true (finalized);
}

static void
window (void)
{
  GtkWidget *w = gtk_window_new ();
  gboolean finalized = FALSE;

  /* GTK holds a ref */
  g_assert_true (!g_object_is_floating (w));
  g_object_weak_ref (G_OBJECT (w), check_finalized, &finalized);

  gtk_window_destroy (GTK_WINDOW (w));

  g_assert_true (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 ();
}