add-child.c 12.5 KB
Newer Older
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <glib.h>
#include <glib-object.h>

#include <gladeui/glade-app.h>

typedef void (* AssertParentedFunc) (GObject *parent, GObject *child);

typedef struct {
  GType parent_type;
  GType child_type;
  AssertParentedFunc func;
} TestData;

/* Avoid warnings from GVFS-RemoteVolumeMonitor */
static gboolean
ignore_gvfs_warning (const gchar *log_domain,
		     GLogLevelFlags log_level,
		     const gchar *message,
		     gpointer user_data)
{
  if (g_strcmp0 (log_domain, "GVFS-RemoteVolumeMonitor") == 0)
    return FALSE;

  return TRUE;
}

static gboolean
main_loop_quit_cb (gpointer data)
{
  gtk_main_quit ();

  return FALSE;
}

static void
check_finalized (gpointer data,
		 GObject *where_the_object_was)
{
  gboolean *did_finalize = (gboolean *)data;

  *did_finalize = TRUE;
}

static void
test_add_child (gconstpointer data)
{
  TestData *test = (TestData *)data;
  GladeWidgetAdaptor *parent_adaptor;
  GladeWidgetAdaptor *child_adaptor;
  GladeWidget        *gparent, *gchild;
  GObject            *parent, *child;
  gboolean            parent_finalized = FALSE;
  gboolean            child_finalized = FALSE;
  gboolean            gparent_finalized = FALSE;
  gboolean            gchild_finalized = FALSE;

  g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL);

  parent_adaptor = glade_widget_adaptor_get_by_type (test->parent_type);
  child_adaptor  = glade_widget_adaptor_get_by_type (test->child_type);

  gparent = glade_widget_adaptor_create_widget (parent_adaptor, FALSE, NULL);
  gchild  = glade_widget_adaptor_create_widget (child_adaptor, FALSE, NULL);

  parent = glade_widget_get_object (gparent);
  child  = glade_widget_get_object (gchild);

  glade_widget_add_child (gparent, gchild, FALSE);

  /* Pass ownership to the parent */
  g_object_unref (gchild);

  g_assert (glade_widget_get_parent (gchild) == gparent);

  if (test->func)
    test->func (parent, child);

  /* filechoosers hold a reference until an async operation is complete */
  if (GTK_IS_FILE_CHOOSER (parent) || GTK_IS_FILE_CHOOSER (child))
    {
      g_timeout_add (2000, main_loop_quit_cb, NULL);
      gtk_main();
    }
  /* Our plugin code adds an idle when cell renderers are created */
  else if (GTK_IS_CELL_RENDERER (child))
    {
      g_timeout_add (50, main_loop_quit_cb, NULL);
      gtk_main();
    }

  /* Unreffing the parent should finalize the parent and child runtime objects */
  g_object_weak_ref (G_OBJECT (gparent), check_finalized, &gparent_finalized);
  g_object_weak_ref (G_OBJECT (gchild),  check_finalized, &gchild_finalized);
  g_object_weak_ref (G_OBJECT (parent), check_finalized, &parent_finalized);
  g_object_weak_ref (G_OBJECT (child),  check_finalized, &child_finalized);

  g_object_unref (gparent);

  g_assert (gparent_finalized);
  g_assert (gchild_finalized);
  g_assert (parent_finalized);
  g_assert (child_finalized);
}

static void
add_test (GType parent_type,
	  GType child_type,
	  AssertParentedFunc func)
{
  gchar *test_path;
  TestData *data = g_new (TestData, 1);

  test_path = g_strdup_printf ("/AddChild/%s/%s",
			       g_type_name (parent_type),
			       g_type_name (child_type));

  data->parent_type = parent_type;
  data->child_type  = child_type;
  data->func        = func;

  g_test_add_data_func_full (test_path, data, test_add_child, g_free);
  g_free (test_path);
}

static void
assert_widget_parented (GObject *parent,
			GObject *child)
{
  g_assert (gtk_widget_get_parent (GTK_WIDGET (child)) == GTK_WIDGET (parent));
}

static void
assert_submenu (GObject *parent,
		GObject *child)
{
  if (GTK_IS_MENU_ITEM (parent))
    g_assert (gtk_menu_item_get_submenu (GTK_MENU_ITEM (parent)) == GTK_WIDGET (child));
  else if (GTK_IS_MENU_TOOL_BUTTON (parent))
    g_assert (gtk_menu_tool_button_get_menu (GTK_MENU_TOOL_BUTTON (parent)) == GTK_WIDGET (child));
  else
    g_assert_not_reached ();
}

static void
assert_cell_parented (GObject *parent,
		      GObject *child)
{
  GList *cells;

  cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (parent));
  g_assert (g_list_find (cells, child) != NULL);
  g_list_free (cells);
}

static void
assert_column_parented (GObject *parent,
			GObject *child)
{
  g_assert (gtk_tree_view_get_column (GTK_TREE_VIEW (parent), 0) == GTK_TREE_VIEW_COLUMN (child));
}

162
163
164
/* Ignore deprecated classes, we test them regardless */
G_GNUC_BEGIN_IGNORE_DEPRECATIONS

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
static void
add_child_widgets (GType parent_type)
{
  add_test (parent_type, GTK_TYPE_IMAGE,               assert_widget_parented);
  add_test (parent_type, GTK_TYPE_LABEL,               assert_widget_parented);
  add_test (parent_type, GTK_TYPE_ACCEL_LABEL,         assert_widget_parented);
  add_test (parent_type, GTK_TYPE_ENTRY,               assert_widget_parented);
  add_test (parent_type, GTK_TYPE_SEARCH_ENTRY,        assert_widget_parented);
  add_test (parent_type, GTK_TYPE_SPIN_BUTTON,         assert_widget_parented);
  add_test (parent_type, GTK_TYPE_SWITCH,              assert_widget_parented);
  add_test (parent_type, GTK_TYPE_SEPARATOR,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_ARROW,               assert_widget_parented);
  add_test (parent_type, GTK_TYPE_DRAWING_AREA,        assert_widget_parented);
  add_test (parent_type, GTK_TYPE_SPINNER,             assert_widget_parented);
  add_test (parent_type, GTK_TYPE_LEVEL_BAR,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_PROGRESS_BAR,        assert_widget_parented);
  add_test (parent_type, GTK_TYPE_SCALE,               assert_widget_parented);
  add_test (parent_type, GTK_TYPE_SCROLLBAR,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_BUTTON,              assert_widget_parented);
  add_test (parent_type, GTK_TYPE_TOGGLE_BUTTON,       assert_widget_parented);
  add_test (parent_type, GTK_TYPE_CHECK_BUTTON,        assert_widget_parented);
  add_test (parent_type, GTK_TYPE_RADIO_BUTTON,        assert_widget_parented);
  add_test (parent_type, GTK_TYPE_LINK_BUTTON,         assert_widget_parented);
  add_test (parent_type, GTK_TYPE_MENU_BUTTON,         assert_widget_parented);

190
191
  add_test (parent_type, GTK_TYPE_COMBO_BOX,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_COMBO_BOX_TEXT,      assert_widget_parented);
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  add_test (parent_type, GTK_TYPE_SCALE_BUTTON,        assert_widget_parented);
  add_test (parent_type, GTK_TYPE_VOLUME_BUTTON,       assert_widget_parented);
  add_test (parent_type, GTK_TYPE_FONT_BUTTON,         assert_widget_parented);
  add_test (parent_type, GTK_TYPE_COLOR_BUTTON,        assert_widget_parented);

  /* FIXME: FileChooserButton leaks a GTask which will crash in the following test */
  /* add_test (parent_type, GTK_TYPE_FILE_CHOOSER_BUTTON, assert_widget_parented); */
  add_test (parent_type, GTK_TYPE_APP_CHOOSER_BUTTON,  assert_widget_parented);
  add_test (parent_type, GTK_TYPE_TEXT_VIEW,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_TREE_VIEW,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_ICON_VIEW,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_CALENDAR,            assert_widget_parented);
  add_test (parent_type, GTK_TYPE_BOX,                 assert_widget_parented);
  add_test (parent_type, GTK_TYPE_NOTEBOOK,            assert_widget_parented);
  add_test (parent_type, GTK_TYPE_FRAME,               assert_widget_parented);
  add_test (parent_type, GTK_TYPE_ASPECT_FRAME,        assert_widget_parented);
  add_test (parent_type, GTK_TYPE_OVERLAY,             assert_widget_parented);
  add_test (parent_type, GTK_TYPE_MENU_BAR,            assert_widget_parented);
  add_test (parent_type, GTK_TYPE_TOOLBAR,             assert_widget_parented);
  add_test (parent_type, GTK_TYPE_TOOL_PALETTE,        assert_widget_parented);
  add_test (parent_type, GTK_TYPE_PANED,               assert_widget_parented);
  add_test (parent_type, GTK_TYPE_BUTTON_BOX,          assert_widget_parented);
  add_test (parent_type, GTK_TYPE_LAYOUT,              assert_widget_parented);
  add_test (parent_type, GTK_TYPE_FIXED,               assert_widget_parented);
  add_test (parent_type, GTK_TYPE_EVENT_BOX,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_EXPANDER,            assert_widget_parented);
  add_test (parent_type, GTK_TYPE_VIEWPORT,            assert_widget_parented);
  add_test (parent_type, GTK_TYPE_ALIGNMENT,           assert_widget_parented);
  add_test (parent_type, GTK_TYPE_GRID,                assert_widget_parented);
  add_test (parent_type, GTK_TYPE_SCROLLED_WINDOW,     assert_widget_parented);
  add_test (parent_type, GTK_TYPE_INFO_BAR,            assert_widget_parented);
  add_test (parent_type, GTK_TYPE_STATUSBAR,           assert_widget_parented);
}

static void
add_child_cells (GType parent_type)
{
  add_test (parent_type, GTK_TYPE_CELL_RENDERER_TEXT, assert_cell_parented);
  add_test (parent_type, GTK_TYPE_CELL_RENDERER_ACCEL, assert_cell_parented);
  add_test (parent_type, GTK_TYPE_CELL_RENDERER_TOGGLE, assert_cell_parented);
  add_test (parent_type, GTK_TYPE_CELL_RENDERER_COMBO, assert_cell_parented);
  add_test (parent_type, GTK_TYPE_CELL_RENDERER_SPIN, assert_cell_parented);
  add_test (parent_type, GTK_TYPE_CELL_RENDERER_PIXBUF, assert_cell_parented);
  add_test (parent_type, GTK_TYPE_CELL_RENDERER_PROGRESS, assert_cell_parented);
  add_test (parent_type, GTK_TYPE_CELL_RENDERER_SPINNER, assert_cell_parented);
}

int
main (int   argc,
      char *argv[])
{
  gtk_test_init (&argc, &argv, NULL);

  glade_init ();
  glade_app_get ();

  /* Normal GtkContainer / GtkWidget parenting */
  add_child_widgets (GTK_TYPE_WINDOW);
  add_child_widgets (GTK_TYPE_BOX);
  add_child_widgets (GTK_TYPE_GRID);
  add_child_widgets (GTK_TYPE_NOTEBOOK);
  add_child_widgets (GTK_TYPE_OVERLAY);
  add_child_widgets (GTK_TYPE_PANED);
  add_child_widgets (GTK_TYPE_BUTTON_BOX);
  add_child_widgets (GTK_TYPE_LAYOUT);
  add_child_widgets (GTK_TYPE_FIXED);
  add_child_widgets (GTK_TYPE_EVENT_BOX);
  add_child_widgets (GTK_TYPE_EXPANDER);
  add_child_widgets (GTK_TYPE_VIEWPORT);
  add_child_widgets (GTK_TYPE_ALIGNMENT);

  /* Actions */
  add_test (GTK_TYPE_ACTION_GROUP, GTK_TYPE_ACTION, NULL);
  add_test (GTK_TYPE_ACTION_GROUP, GTK_TYPE_TOGGLE_ACTION, NULL);
  add_test (GTK_TYPE_ACTION_GROUP, GTK_TYPE_RADIO_ACTION, NULL);
  add_test (GTK_TYPE_ACTION_GROUP, GTK_TYPE_RECENT_ACTION, NULL);

  /* Menus */
  add_test (GTK_TYPE_MENU_BAR, GTK_TYPE_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU_BAR, GTK_TYPE_IMAGE_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU_BAR, GTK_TYPE_CHECK_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU_BAR, GTK_TYPE_RADIO_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU_BAR, GTK_TYPE_SEPARATOR_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU, GTK_TYPE_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU, GTK_TYPE_IMAGE_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU, GTK_TYPE_CHECK_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU, GTK_TYPE_RADIO_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU, GTK_TYPE_SEPARATOR_MENU_ITEM, assert_widget_parented);
  add_test (GTK_TYPE_MENU_ITEM, GTK_TYPE_MENU, assert_submenu);
  add_test (GTK_TYPE_IMAGE_MENU_ITEM, GTK_TYPE_MENU, assert_submenu);
  add_test (GTK_TYPE_CHECK_MENU_ITEM, GTK_TYPE_MENU, assert_submenu);
  add_test (GTK_TYPE_RADIO_MENU_ITEM, GTK_TYPE_MENU, assert_submenu);

  /* Toolbars / ToolPalette */
  add_test (GTK_TYPE_TOOLBAR, GTK_TYPE_TOOL_BUTTON, assert_widget_parented);
  add_test (GTK_TYPE_TOOLBAR, GTK_TYPE_TOGGLE_TOOL_BUTTON, assert_widget_parented);
  add_test (GTK_TYPE_TOOLBAR, GTK_TYPE_RADIO_TOOL_BUTTON, assert_widget_parented);
  add_test (GTK_TYPE_TOOLBAR, GTK_TYPE_MENU_TOOL_BUTTON, assert_widget_parented);
  add_test (GTK_TYPE_TOOL_ITEM_GROUP, GTK_TYPE_TOOL_BUTTON, assert_widget_parented);
  add_test (GTK_TYPE_TOOL_ITEM_GROUP, GTK_TYPE_TOGGLE_TOOL_BUTTON, assert_widget_parented);
  add_test (GTK_TYPE_TOOL_ITEM_GROUP, GTK_TYPE_RADIO_TOOL_BUTTON, assert_widget_parented);
  add_test (GTK_TYPE_TOOL_ITEM_GROUP, GTK_TYPE_MENU_TOOL_BUTTON, assert_widget_parented);
  add_test (GTK_TYPE_TOOL_PALETTE, GTK_TYPE_TOOL_ITEM_GROUP, assert_widget_parented);
  add_test (GTK_TYPE_MENU_TOOL_BUTTON, GTK_TYPE_MENU, assert_submenu);

  /* Cell layouts */
298
299
  /* FIXME: GtkTreeViewColumn gives a critical when creating it */
  /* add_test (GTK_TYPE_TREE_VIEW, GTK_TYPE_TREE_VIEW_COLUMN, assert_column_parented); */
300
301
  add_child_cells (GTK_TYPE_TREE_VIEW_COLUMN);
  add_child_cells (GTK_TYPE_ICON_VIEW);
302
  add_child_cells (GTK_TYPE_COMBO_BOX);
303
304
305
306
307
308

  /* TextTag */
  add_test (GTK_TYPE_TEXT_TAG_TABLE, GTK_TYPE_TEXT_TAG, NULL);

  return g_test_run ();
}