Debian Codegraph @

testsuite/gtk/gtkmenu.c

raw
1 #include <gtk/gtk.h>
2
3 /* TestItem {{{1 */
4
5 /* This utility struct is used by both the RandomMenu and MirrorMenu
6 * class implementations below.
7 */
8 typedef struct {
9 GHashTable *attributes;
10 GHashTable *links;
11 } TestItem;
12
13 static TestItem *
14 test_item_new (GHashTable *attributes,
15 GHashTable *links)
16 {
17 TestItem *item;
18
19 item = g_slice_new (TestItem);
20 item->attributes = g_hash_table_ref (attributes);
21 item->links = g_hash_table_ref (links);
22
23 return item;
24 }
25
26 static void
27 test_item_free (gpointer data)
28 {
29 TestItem *item = data;
30
31 g_hash_table_unref (item->attributes);
32 g_hash_table_unref (item->links);
33
34 g_slice_free (TestItem, item);
35 }
36
37 /* RandomMenu {{{1 */
38 #define MAX_ITEMS 10
39 #define TOP_ORDER 4
40
41 typedef struct {
42 GMenuModel parent_instance;
43
44 GSequence *items;
45 gint order;
46 } RandomMenu;
47
48 typedef GMenuModelClass RandomMenuClass;
49
50 static GType random_menu_get_type (void);
51 G_DEFINE_TYPE (RandomMenu, random_menu, G_TYPE_MENU_MODEL);
52
53 static gboolean
54 random_menu_is_mutable (GMenuModel *model)
55 {
56 return TRUE;
57 }
58
59 static gint
60 random_menu_get_n_items (GMenuModel *model)
61 {
62 RandomMenu *menu = (RandomMenu *) model;
63
64 return g_sequence_get_length (menu->items);
65 }
66
67 static void
68 random_menu_get_item_attributes (GMenuModel *model,
69 gint position,
70 GHashTable **table)
71 {
72 RandomMenu *menu = (RandomMenu *) model;
73 TestItem *item;
74
75 item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
76 *table = g_hash_table_ref (item->attributes);
77 }
78
79 static void
80 random_menu_get_item_links (GMenuModel *model,
81 gint position,
82 GHashTable **table)
83 {
84 RandomMenu *menu = (RandomMenu *) model;
85 TestItem *item;
86
87 item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
88 *table = g_hash_table_ref (item->links);
89 }
90
91 static void
92 random_menu_finalize (GObject *object)
93 {
94 RandomMenu *menu = (RandomMenu *) object;
95
96 g_sequence_free (menu->items);
97
98 G_OBJECT_CLASS (random_menu_parent_class)
99 ->finalize (object);
100 }
101
102 static void
103 random_menu_init (RandomMenu *menu)
104 {
105 }
106
107 static void
108 random_menu_class_init (GMenuModelClass *class)
109 {
110 GObjectClass *object_class = G_OBJECT_CLASS (class);
111
112 class->is_mutable = random_menu_is_mutable;
113 class->get_n_items = random_menu_get_n_items;
114 class->get_item_attributes = random_menu_get_item_attributes;
115 class->get_item_links = random_menu_get_item_links;
116
117 object_class->finalize = random_menu_finalize;
118 }
119
120 static RandomMenu * random_menu_new (GRand *rand, gint order);
121
122 static void
123 random_menu_change (RandomMenu *menu,
124 GRand *rand)
125 {
126 gint position, removes, adds;
127 GSequenceIter *point;
128 gint n_items;
129 gint i;
130
131 n_items = g_sequence_get_length (menu->items);
132
133 do
134 {
135 position = g_rand_int_range (rand, 0, n_items + 1);
136 removes = g_rand_int_range (rand, 0, n_items - position + 1);
137 adds = g_rand_int_range (rand, 0, MAX_ITEMS - (n_items - removes) + 1);
138 }
139 while (removes == 0 && adds == 0);
140
141 point = g_sequence_get_iter_at_pos (menu->items, position + removes);
142
143 if (removes)
144 {
145 GSequenceIter *start;
146
147 start = g_sequence_get_iter_at_pos (menu->items, position);
148 g_sequence_remove_range (start, point);
149 }
150
151 for (i = 0; i < adds; i++)
152 {
153 const gchar *label;
154 GHashTable *links;
155 GHashTable *attributes;
156
157 attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
158 links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_object_unref);
159
160 if (menu->order > 0 && g_rand_boolean (rand))
161 {
162 RandomMenu *child;
163 const gchar *subtype;
164
165 child = random_menu_new (rand, menu->order - 1);
166
167 if (g_rand_boolean (rand))
168 {
169 subtype = G_MENU_LINK_SECTION;
170 /* label some section headers */
171 if (g_rand_boolean (rand))
172 label = "Section";
173 else
174 label = NULL;
175 }
176 else
177 {
178 /* label all submenus */
179 subtype = G_MENU_LINK_SUBMENU;
180 label = "Submenu";
181 }
182
183 g_hash_table_insert (links, g_strdup (subtype), child);
184 }
185 else
186 /* label all terminals */
187 label = "Menu Item";
188
189 if (label)
190 g_hash_table_insert (attributes, g_strdup ("label"), g_variant_ref_sink (g_variant_new_string (label)));
191
192 g_sequence_insert_before (point, test_item_new (attributes, links));
193 g_hash_table_unref (links);
194 g_hash_table_unref (attributes);
195 }
196
197 g_menu_model_items_changed (G_MENU_MODEL (menu), position, removes, adds);
198 }
199
200 static RandomMenu *
201 random_menu_new (GRand *rand,
202 gint order)
203 {
204 RandomMenu *menu;
205
206 menu = g_object_new (random_menu_get_type (), NULL);
207 menu->items = g_sequence_new (test_item_free);
208 menu->order = order;
209
210 random_menu_change (menu, rand);
211
212 return menu;
213 }
214
215 /* Test cases {{{1 */
216
217 static void assert_menu_equality (GtkContainer *container, GMenuModel *model);
218
219 static const gchar *
220 get_label (GtkMenuItem *item)
221 {
222 GList *children = gtk_container_get_children (GTK_CONTAINER (item));
223 const gchar *label = NULL;
224
225 while (children)
226 {
227 if (GTK_IS_CONTAINER (children->data))
228 children = g_list_concat (children, gtk_container_get_children (children->data));
229 else if (GTK_IS_LABEL (children->data))
230 label = gtk_label_get_text (children->data);
231
232 children = g_list_delete_link (children, children);
233 }
234
235 return label;
236 }
237
238 /* a bit complicated with the separators...
239 *
240 * with_separators are if subsections of this GMenuModel should have
241 * separators inserted between them (ie: in the same sense as the
242 * 'with_separators' argument to gtk_menu_shell_bind_model().
243 *
244 * needs_separator is true if this particular section needs to have a
245 * separator before it in the case that it is non-empty. this will be
246 * defined for all subsections of a with_separators menu (except the
247 * first) or in case section_header is non-%NULL.
248 *
249 * section_header is the label that must be inside that separator, if it
250 * exists. section_header is only non-%NULL if needs_separator is also
251 * TRUE.
252 */
253 static void
254 assert_section_equality (GSList **children,
255 gboolean with_separators,
256 gboolean needs_separator,
257 const gchar *section_header,
258 GMenuModel *model)
259 {
260 gboolean has_separator;
261 GSList *our_children;
262 gint i, n;
263
264 /* Assuming that we have the possibility of showing a separator, there
265 * are two valid situations:
266 *
267 * - we have a separator and we have other children
268 *
269 * - we have no separator and no children
270 *
271 * If we see a separator, we suppose that it is ours and that we will
272 * encounter children. In the case that we have no children, the
273 * separator may not be ours but may rather belong to a later section.
274 *
275 * We therefore keep our own copy of the children GSList. If we
276 * encounter children, we will delete the links that this section is
277 * responsible for and update the pass-by-reference value. Otherwise,
278 * we will leave everything alone and let the separator be accounted
279 * for by a following section.
280 */
281 our_children = *children;
282 if (needs_separator && GTK_IS_SEPARATOR_MENU_ITEM (our_children->data))
283 {
284 /* We accounted for the separator, at least for now, so remove it
285 * from the list.
286 *
287 * We will check later if we should have actually had a separator
288 * and compare the result to has_separator.
289 */
290 our_children = our_children->next;
291 has_separator = TRUE;
292 }
293 else
294 has_separator = FALSE;
295
296 /* Now, iterate the model checking that the items in the GSList line
297 * up with our expectations. */
298 n = g_menu_model_get_n_items (model);
299 for (i = 0; i < n; i++)
300 {
301 GMenuModel *subsection;
302 GMenuModel *submenu;
303 gchar *label = NULL;
304
305 subsection = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION);
306 submenu = g_menu_model_get_item_link (model, i, G_MENU_LINK_SUBMENU);
307 g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
308
309 if (subsection)
310 {
311 g_assert (!submenu);
312 assert_section_equality (&our_children,
313 FALSE, /* with_separators */
314 label || (with_separators && i > 0), /* needs_separator */
315 label, /* section_header */
316 subsection);
317 g_object_unref (subsection);
318 }
319 else
320 {
321 GtkWidget *submenu_widget;
322 GtkMenuItem *item;
323
324 /* This is a normal item. Make sure the label is right. */
325 item = our_children->data;
326 our_children = g_slist_remove (our_children, item);
327
328 /* get_label() returns "" when it ought to return NULL */
329 g_assert_cmpstr (get_label (item), ==, label ? label : "");
330 submenu_widget = gtk_menu_item_get_submenu (item);
331
332 if (submenu)
333 {
334 g_assert (submenu_widget != NULL);
335 assert_menu_equality (GTK_CONTAINER (submenu_widget), submenu);
336 g_object_unref (submenu);
337 }
338 else
339 g_assert (!submenu_widget);
340 }
341
342 g_free (label);
343 }
344
345 /* If we found a separator but visited no children then the separator
346 * was not for us. Patch that up.
347 */
348 if (has_separator && our_children == (*children)->next)
349 {
350 /* Rewind our_children to put the separator we tentatively
351 * consumed back into the list.
352 */
353 our_children = *children;
354 has_separator = FALSE;
355 }
356
357 if (our_children == *children)
358 /* If we had no children then we didn't really need a separator. */
359 needs_separator = FALSE;
360
361 g_assert (needs_separator == has_separator);
362
363 if (has_separator)
364 {
365 GtkWidget *contents;
366 const gchar *label;
367
368 /* We needed and had a separator and we visited a child.
369 *
370 * Make sure that separator was valid.
371 */
372 contents = gtk_bin_get_child ((*children)->data);
373 if (GTK_IS_LABEL (contents))
374 label = gtk_label_get_label (GTK_LABEL (contents));
375 else
376 label = "";
377
378 /* get_label() returns "" when it ought to return NULL */
379 g_assert_cmpstr (label, ==, section_header ? section_header : "");
380
381 /* our_children has already gone (possibly far) past *children, so
382 * we need to free up the link that we left behind for the
383 * separator in case we wanted to rewind.
384 */
385 g_slist_free_1 (*children);
386 }
387
388 *children = our_children;
389 }
390
391 /* We want to use a GSList here instead of a GList because the ->prev
392 * pointer updates cause trouble with the way we speculatively deal with
393 * separators by skipping over them and coming back to clean up later.
394 */
395 static void
396 get_children_into_slist (GtkWidget *widget,
397 gpointer user_data)
398 {
399 GSList **list_ptr = user_data;
400
401 *list_ptr = g_slist_prepend (*list_ptr, widget);
402 }
403
404 static void
405 assert_menu_equality (GtkContainer *container,
406 GMenuModel *model)
407 {
408 GSList *children = NULL;
409
410 gtk_container_foreach (container, get_children_into_slist, &children);
411 children = g_slist_reverse (children);
412
413 assert_section_equality (&children, TRUE, FALSE, NULL, model);
414 g_assert (children == NULL);
415 }
416
417 static void
418 test_bind_menu (void)
419 {
420 RandomMenu *model;
421 GtkWidget *menu;
422 GRand *rand;
423 gint i;
424
425 gtk_init (0, 0);
426
427 rand = g_rand_new_with_seed (g_test_rand_int ());
428 model = random_menu_new (rand, TOP_ORDER);
429 menu = gtk_menu_new_from_model (G_MENU_MODEL (model));
430 g_object_ref_sink (menu);
431 assert_menu_equality (GTK_CONTAINER (menu), G_MENU_MODEL (model));
432 for (i = 0; i < 100; i++)
433 {
434 random_menu_change (model, rand);
435 while (g_main_context_iteration (NULL, FALSE));
436 assert_menu_equality (GTK_CONTAINER (menu), G_MENU_MODEL (model));
437 }
438 g_object_unref (model);
439 g_object_unref (menu);
440 g_rand_free (rand);
441 }
442 /* Epilogue {{{1 */
443 int
444 main (int argc, char **argv)
445 {
446 g_test_init (&argc, &argv, NULL);
447
448 g_test_add_func ("/gmenu/bind", test_bind_menu);
449
450 return g_test_run ();
451 }
452 /* vim:set foldmethod=marker: */