menu tests: keep mirror of proxy
[glib.git] / gio / tests / gmenumodel.c
blob12e6124ddc08879fa447b36993e6773a19b37a0e
1 #include <gio/gio.h>
3 /* TestItem {{{1 */
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;
13 static TestItem *
14 test_item_new (GHashTable *attributes,
15 GHashTable *links)
17 TestItem *item;
19 item = g_slice_new (TestItem);
20 item->attributes = g_hash_table_ref (attributes);
21 item->links = g_hash_table_ref (links);
23 return item;
26 static void
27 test_item_free (gpointer data)
29 TestItem *item = data;
31 g_hash_table_unref (item->attributes);
32 g_hash_table_unref (item->links);
34 g_slice_free (TestItem, item);
37 /* RandomMenu {{{1 */
38 #define MAX_ITEMS 5
39 #define TOP_ORDER 4
41 typedef struct {
42 GMenuModel parent_instance;
44 GSequence *items;
45 gint order;
46 } RandomMenu;
48 typedef GMenuModelClass RandomMenuClass;
50 static GType random_menu_get_type (void);
51 G_DEFINE_TYPE (RandomMenu, random_menu, G_TYPE_MENU_MODEL);
53 static gboolean
54 random_menu_is_mutable (GMenuModel *model)
56 return TRUE;
59 static gint
60 random_menu_get_n_items (GMenuModel *model)
62 RandomMenu *menu = (RandomMenu *) model;
64 return g_sequence_get_length (menu->items);
67 static void
68 random_menu_get_item_attributes (GMenuModel *model,
69 gint position,
70 GHashTable **table)
72 RandomMenu *menu = (RandomMenu *) model;
73 TestItem *item;
75 item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
76 *table = g_hash_table_ref (item->attributes);
79 static void
80 random_menu_get_item_links (GMenuModel *model,
81 gint position,
82 GHashTable **table)
84 RandomMenu *menu = (RandomMenu *) model;
85 TestItem *item;
87 item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
88 *table = g_hash_table_ref (item->links);
91 static void
92 random_menu_finalize (GObject *object)
94 RandomMenu *menu = (RandomMenu *) object;
96 g_sequence_free (menu->items);
98 G_OBJECT_CLASS (random_menu_parent_class)
99 ->finalize (object);
102 static void
103 random_menu_init (RandomMenu *menu)
107 static void
108 random_menu_class_init (GMenuModelClass *class)
110 GObjectClass *object_class = G_OBJECT_CLASS (class);
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;
117 object_class->finalize = random_menu_finalize;
120 static RandomMenu * random_menu_new (GRand *rand, gint order);
122 static void
123 random_menu_change (RandomMenu *menu,
124 GRand *rand)
126 gint position, removes, adds;
127 GSequenceIter *point;
128 gint n_items;
129 gint i;
131 n_items = g_sequence_get_length (menu->items);
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);
139 while (removes == 0 && adds == 0);
141 point = g_sequence_get_iter_at_pos (menu->items, position + removes);
143 if (removes)
145 GSequenceIter *start;
147 start = g_sequence_get_iter_at_pos (menu->items, position);
148 g_sequence_remove_range (start, point);
151 for (i = 0; i < adds; i++)
153 const gchar *label;
154 GHashTable *links;
155 GHashTable *attributes;
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);
160 if (menu->order > 0 && g_rand_boolean (rand))
162 RandomMenu *child;
163 const gchar *subtype;
165 child = random_menu_new (rand, menu->order - 1);
167 if (g_rand_boolean (rand))
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;
176 else
178 /* label all submenus */
179 subtype = G_MENU_LINK_SUBMENU;
180 label = "Submenu";
183 g_hash_table_insert (links, g_strdup (subtype), child);
185 else
186 /* label all terminals */
187 label = "Menu Item";
189 if (label)
190 g_hash_table_insert (attributes, g_strdup ("label"), g_variant_ref_sink (g_variant_new_string (label)));
192 g_sequence_insert_before (point, test_item_new (attributes, links));
193 g_hash_table_unref (links);
194 g_hash_table_unref (attributes);
197 g_menu_model_items_changed (G_MENU_MODEL (menu), position, removes, adds);
200 static RandomMenu *
201 random_menu_new (GRand *rand,
202 gint order)
204 RandomMenu *menu;
206 menu = g_object_new (random_menu_get_type (), NULL);
207 menu->items = g_sequence_new (test_item_free);
208 menu->order = order;
210 random_menu_change (menu, rand);
212 return menu;
215 /* MirrorMenu {{{1 */
216 typedef struct {
217 GMenuModel parent_instance;
219 GMenuModel *clone_of;
220 GSequence *items;
221 gulong handler_id;
222 } MirrorMenu;
224 typedef GMenuModelClass MirrorMenuClass;
226 static GType mirror_menu_get_type (void);
227 G_DEFINE_TYPE (MirrorMenu, mirror_menu, G_TYPE_MENU_MODEL);
229 static gboolean
230 mirror_menu_is_mutable (GMenuModel *model)
232 MirrorMenu *menu = (MirrorMenu *) model;
234 return menu->handler_id != 0;
237 static gint
238 mirror_menu_get_n_items (GMenuModel *model)
240 MirrorMenu *menu = (MirrorMenu *) model;
242 return g_sequence_get_length (menu->items);
245 static void
246 mirror_menu_get_item_attributes (GMenuModel *model,
247 gint position,
248 GHashTable **table)
250 MirrorMenu *menu = (MirrorMenu *) model;
251 TestItem *item;
253 item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
254 *table = g_hash_table_ref (item->attributes);
257 static void
258 mirror_menu_get_item_links (GMenuModel *model,
259 gint position,
260 GHashTable **table)
262 MirrorMenu *menu = (MirrorMenu *) model;
263 TestItem *item;
265 item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
266 *table = g_hash_table_ref (item->links);
269 static void
270 mirror_menu_finalize (GObject *object)
272 MirrorMenu *menu = (MirrorMenu *) object;
274 if (menu->handler_id)
275 g_signal_handler_disconnect (menu->clone_of, menu->handler_id);
277 g_sequence_free (menu->items);
278 g_object_unref (menu->clone_of);
280 G_OBJECT_CLASS (mirror_menu_parent_class)
281 ->finalize (object);
284 static void
285 mirror_menu_init (MirrorMenu *menu)
289 static void
290 mirror_menu_class_init (GMenuModelClass *class)
292 GObjectClass *object_class = G_OBJECT_CLASS (class);
294 class->is_mutable = mirror_menu_is_mutable;
295 class->get_n_items = mirror_menu_get_n_items;
296 class->get_item_attributes = mirror_menu_get_item_attributes;
297 class->get_item_links = mirror_menu_get_item_links;
299 object_class->finalize = mirror_menu_finalize;
302 static MirrorMenu * mirror_menu_new (GMenuModel *clone_of);
304 static void
305 mirror_menu_changed (GMenuModel *model,
306 gint position,
307 gint removed,
308 gint added,
309 gpointer user_data)
311 MirrorMenu *menu = user_data;
312 GSequenceIter *point;
313 gint i;
315 g_assert (model == menu->clone_of);
317 point = g_sequence_get_iter_at_pos (menu->items, position + removed);
319 if (removed)
321 GSequenceIter *start;
323 start = g_sequence_get_iter_at_pos (menu->items, position);
324 g_sequence_remove_range (start, point);
327 for (i = position; i < position + added; i++)
329 GMenuAttributeIter *attr_iter;
330 GMenuLinkIter *link_iter;
331 GHashTable *links;
332 GHashTable *attributes;
333 const gchar *name;
334 GMenuModel *child;
335 GVariant *value;
337 attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
338 links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_object_unref);
340 attr_iter = g_menu_model_iterate_item_attributes (model, i);
341 while (g_menu_attribute_iter_get_next (attr_iter, &name, &value))
343 g_hash_table_insert (attributes, g_strdup (name), value);
345 g_object_unref (attr_iter);
347 link_iter = g_menu_model_iterate_item_links (model, i);
348 while (g_menu_link_iter_get_next (link_iter, &name, &child))
350 g_hash_table_insert (links, g_strdup (name), mirror_menu_new (child));
351 g_object_unref (child);
353 g_object_unref (link_iter);
355 g_sequence_insert_before (point, test_item_new (attributes, links));
356 g_hash_table_unref (attributes);
357 g_hash_table_unref (links);
360 g_menu_model_items_changed (G_MENU_MODEL (menu), position, removed, added);
363 static MirrorMenu *
364 mirror_menu_new (GMenuModel *clone_of)
366 MirrorMenu *menu;
368 menu = g_object_new (mirror_menu_get_type (), NULL);
369 menu->items = g_sequence_new (test_item_free);
370 menu->clone_of = g_object_ref (clone_of);
372 if (g_menu_model_is_mutable (clone_of))
373 menu->handler_id = g_signal_connect (clone_of, "items-changed", G_CALLBACK (mirror_menu_changed), menu);
374 mirror_menu_changed (clone_of, 0, 0, g_menu_model_get_n_items (clone_of), menu);
376 return menu;
379 /* check_menus_equal(), assert_menus_equal() {{{1 */
380 static gboolean
381 check_menus_equal (GMenuModel *a,
382 GMenuModel *b)
384 gboolean equal = TRUE;
385 gint a_n, b_n;
386 gint i;
388 a_n = g_menu_model_get_n_items (a);
389 b_n = g_menu_model_get_n_items (b);
391 if (a_n != b_n)
392 return FALSE;
394 for (i = 0; i < a_n; i++)
396 GMenuAttributeIter *attr_iter;
397 GVariant *a_value, *b_value;
398 GMenuLinkIter *link_iter;
399 GMenuModel *a_menu, *b_menu;
400 const gchar *name;
402 attr_iter = g_menu_model_iterate_item_attributes (a, i);
403 while (g_menu_attribute_iter_get_next (attr_iter, &name, &a_value))
405 b_value = g_menu_model_get_item_attribute_value (b, i, name, NULL);
406 equal &= b_value && g_variant_equal (a_value, b_value);
407 if (b_value)
408 g_variant_unref (b_value);
409 g_variant_unref (a_value);
411 g_object_unref (attr_iter);
413 attr_iter = g_menu_model_iterate_item_attributes (b, i);
414 while (g_menu_attribute_iter_get_next (attr_iter, &name, &b_value))
416 a_value = g_menu_model_get_item_attribute_value (a, i, name, NULL);
417 equal &= a_value && g_variant_equal (a_value, b_value);
418 if (a_value)
419 g_variant_unref (a_value);
420 g_variant_unref (b_value);
422 g_object_unref (attr_iter);
424 link_iter = g_menu_model_iterate_item_links (a, i);
425 while (g_menu_link_iter_get_next (link_iter, &name, &a_menu))
427 b_menu = g_menu_model_get_item_link (b, i, name);
428 equal &= b_menu && check_menus_equal (a_menu, b_menu);
429 if (b_menu)
430 g_object_unref (b_menu);
431 g_object_unref (a_menu);
433 g_object_unref (link_iter);
435 link_iter = g_menu_model_iterate_item_links (b, i);
436 while (g_menu_link_iter_get_next (link_iter, &name, &b_menu))
438 a_menu = g_menu_model_get_item_link (a, i, name);
439 equal &= a_menu && check_menus_equal (a_menu, b_menu);
440 if (a_menu)
441 g_object_unref (a_menu);
442 g_object_unref (b_menu);
444 g_object_unref (link_iter);
447 return equal;
450 static void
451 assert_menus_equal (GMenuModel *a,
452 GMenuModel *b)
454 if (!check_menus_equal (a, b))
456 GString *string;
458 string = g_string_new ("\n <a>\n");
459 g_menu_markup_print_string (string, G_MENU_MODEL (a), 4, 2);
460 g_string_append (string, " </a>\n\n-------------\n <b>\n");
461 g_menu_markup_print_string (string, G_MENU_MODEL (b), 4, 2);
462 g_string_append (string, " </b>\n");
463 g_error ("%s", string->str);
467 /* Test cases {{{1 */
468 static void
469 test_equality (void)
471 GRand *randa, *randb;
472 guint32 seed;
473 gint i;
475 seed = g_test_rand_int ();
477 randa = g_rand_new_with_seed (seed);
478 randb = g_rand_new_with_seed (seed);
480 for (i = 0; i < 500; i++)
482 RandomMenu *a, *b;
484 a = random_menu_new (randa, TOP_ORDER);
485 b = random_menu_new (randb, TOP_ORDER);
486 assert_menus_equal (G_MENU_MODEL (a), G_MENU_MODEL (b));
487 g_object_unref (b);
488 g_object_unref (a);
491 g_rand_int (randa);
493 for (i = 0; i < 500;)
495 RandomMenu *a, *b;
497 a = random_menu_new (randa, TOP_ORDER);
498 b = random_menu_new (randb, TOP_ORDER);
499 if (check_menus_equal (G_MENU_MODEL (a), G_MENU_MODEL (b)))
501 /* by chance, they may really be equal. double check. */
502 GString *as, *bs;
504 as = g_menu_markup_print_string (NULL, G_MENU_MODEL (a), 4, 2);
505 bs = g_menu_markup_print_string (NULL, G_MENU_MODEL (b), 4, 2);
506 g_assert_cmpstr (as->str, ==, bs->str);
507 g_string_free (bs, TRUE);
508 g_string_free (as, TRUE);
510 else
511 /* make sure we get enough unequals (ie: no GRand failure) */
512 i++;
514 g_object_unref (b);
515 g_object_unref (a);
518 g_rand_free (randb);
519 g_rand_free (randa);
522 static void
523 test_random (void)
525 RandomMenu *random;
526 MirrorMenu *mirror;
527 GRand *rand;
528 gint i;
530 rand = g_rand_new_with_seed (g_test_rand_int ());
531 random = random_menu_new (rand, TOP_ORDER);
532 mirror = mirror_menu_new (G_MENU_MODEL (random));
534 for (i = 0; i < 500; i++)
536 assert_menus_equal (G_MENU_MODEL (random), G_MENU_MODEL (mirror));
537 random_menu_change (random, rand);
540 g_object_unref (mirror);
541 g_object_unref (random);
543 g_rand_free (rand);
546 struct roundtrip_state
548 RandomMenu *random;
549 MirrorMenu *proxy_mirror;
550 GMenuProxy *proxy;
551 GMainLoop *loop;
552 GRand *rand;
553 gint success;
554 gint count;
557 static gboolean
558 roundtrip_step (gpointer data)
560 struct roundtrip_state *state = data;
562 if (check_menus_equal (G_MENU_MODEL (state->random), G_MENU_MODEL (state->proxy)) &&
563 check_menus_equal (G_MENU_MODEL (state->random), G_MENU_MODEL (state->proxy_mirror)))
565 state->success++;
566 state->count = 0;
568 if (state->success < 100)
569 random_menu_change (state->random, state->rand);
570 else
571 g_main_loop_quit (state->loop);
573 else if (state->count == 100)
575 assert_menus_equal (G_MENU_MODEL (state->random), G_MENU_MODEL (state->proxy));
576 g_assert_not_reached ();
578 else
579 state->count++;
581 return G_SOURCE_CONTINUE;
584 static void
585 test_dbus_roundtrip (void)
587 struct roundtrip_state state;
588 GDBusConnection *bus;
589 guint id;
591 bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
593 state.rand = g_rand_new_with_seed (g_test_rand_int ());
595 state.random = random_menu_new (state.rand, 2);
596 g_menu_model_dbus_export_start (bus, "/", G_MENU_MODEL (state.random), NULL);
597 g_assert (g_menu_model_dbus_export_query (G_MENU_MODEL (state.random), NULL, NULL));
598 state.proxy = g_menu_proxy_get (bus, g_dbus_connection_get_unique_name (bus), "/");
599 state.proxy_mirror = mirror_menu_new (G_MENU_MODEL (state.proxy));
600 state.count = 0;
601 state.success = 0;
603 id = g_timeout_add (10, roundtrip_step, &state);
605 state.loop = g_main_loop_new (NULL, FALSE);
606 g_main_loop_run (state.loop);
608 g_main_loop_unref (state.loop);
609 g_source_remove (id);
610 g_object_unref (state.proxy);
611 g_menu_model_dbus_export_stop (G_MENU_MODEL (state.random));
612 g_assert (!g_menu_model_dbus_export_query (G_MENU_MODEL (state.random), NULL, NULL));
613 g_object_unref (state.random);
614 g_object_unref (state.proxy_mirror);
615 g_rand_free (state.rand);
616 g_object_unref (bus);
619 static gint items_changed_count;
621 static void
622 items_changed (GMenuModel *model,
623 gint position,
624 gint removed,
625 gint added,
626 gpointer data)
628 items_changed_count++;
631 static gboolean
632 stop_loop (gpointer data)
634 GMainLoop *loop = data;
636 g_main_loop_quit (loop);
638 return G_SOURCE_REMOVE;
641 static void
642 test_dbus_subscriptions (void)
644 GDBusConnection *bus;
645 GMenu *menu;
646 GMenuProxy *proxy;
647 GMainLoop *loop;
648 GError *error = NULL;
650 loop = g_main_loop_new (NULL, FALSE);
652 bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
654 menu = g_menu_new ();
656 g_menu_model_dbus_export_start (bus, "/", G_MENU_MODEL (menu), &error);
657 g_assert_no_error (error);
659 proxy = g_menu_proxy_get (bus, g_dbus_connection_get_unique_name (bus), "/");
660 items_changed_count = 0;
661 g_signal_connect (proxy, "items-changed",
662 G_CALLBACK (items_changed), NULL);
664 g_menu_append (menu, "item1", NULL);
665 g_menu_append (menu, "item2", NULL);
666 g_menu_append (menu, "item3", NULL);
668 g_assert_cmpint (items_changed_count, ==, 0);
670 g_timeout_add (100, stop_loop, loop);
671 g_main_loop_run (loop);
673 g_menu_model_get_n_items (G_MENU_MODEL (proxy));
675 g_timeout_add (100, stop_loop, loop);
676 g_main_loop_run (loop);
678 g_assert_cmpint (items_changed_count, ==, 1);
679 g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (proxy)), ==, 3);
681 g_timeout_add (100, stop_loop, loop);
682 g_main_loop_run (loop);
684 g_menu_append (menu, "item4", NULL);
685 g_menu_append (menu, "item5", NULL);
686 g_menu_append (menu, "item6", NULL);
687 g_menu_remove (menu, 0);
688 g_menu_remove (menu, 0);
690 g_timeout_add (200, stop_loop, loop);
691 g_main_loop_run (loop);
693 g_assert_cmpint (items_changed_count, ==, 6);
695 g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (proxy)), ==, 4);
696 g_object_unref (proxy);
698 g_timeout_add (100, stop_loop, loop);
699 g_main_loop_run (loop);
701 g_menu_remove (menu, 0);
702 g_menu_remove (menu, 0);
704 g_timeout_add (100, stop_loop, loop);
705 g_main_loop_run (loop);
707 g_assert_cmpint (items_changed_count, ==, 6);
709 g_menu_model_dbus_export_stop (G_MENU_MODEL (menu));
710 g_object_unref (menu);
712 g_main_loop_unref (loop);
715 static void
716 start_element (GMarkupParseContext *context,
717 const gchar *element_name,
718 const gchar **attribute_names,
719 const gchar **attribute_values,
720 gpointer user_data,
721 GError **error)
723 if (g_strcmp0 (element_name, "menu") == 0)
724 g_menu_markup_parser_start_menu (context, "domain", NULL);
727 static void
728 end_element (GMarkupParseContext *context,
729 const gchar *element_name,
730 gpointer user_data,
731 GError **error)
733 GMenu **menu = user_data;
735 if (g_strcmp0 (element_name, "menu") == 0)
736 *menu = g_menu_markup_parser_end_menu (context);
739 static GMenuModel *
740 parse_menu_string (const gchar *string, GError **error)
742 const GMarkupParser parser = {
743 start_element, end_element, NULL, NULL, NULL
745 GMarkupParseContext *context;
746 GMenuModel *menu = NULL;
748 context = g_markup_parse_context_new (&parser, 0, &menu, NULL);
749 g_markup_parse_context_parse (context, string, -1, error);
750 g_markup_parse_context_free (context);
752 return menu;
755 static gchar *
756 menu_to_string (GMenuModel *menu)
758 GString *s;
760 s = g_string_new ("<menu>\n");
761 g_menu_markup_print_string (s, menu, 2, 2);
762 g_string_append (s, "</menu>\n");
764 return g_string_free (s, FALSE);
767 static void
768 test_markup_roundtrip (void)
770 const gchar data[] =
771 "<menu id='edit-menu'>\n"
772 " <section>\n"
773 " <item action='undo'>\n"
774 " <attribute name='label' translatable='yes' context='Stock label'>'_Undo'</attribute>\n"
775 " </item>\n"
776 " <item label='Redo' action='redo'/>\n"
777 " </section>\n"
778 " <section></section>\n"
779 " <section label='Copy &amp; Paste'>\n"
780 " <item label='Cut' action='cut'/>\n"
781 " <item label='Copy' action='copy'/>\n"
782 " <item label='Paste' action='paste'/>\n"
783 " </section>\n"
784 " <section>\n"
785 " <item label='Bold' action='bold'/>\n"
786 " <submenu label='Language'>\n"
787 " <item label='Latin' action='lang' target='latin'/>\n"
788 " <item label='Greek' action='lang' target='greek'/>\n"
789 " <item label='Urdu' action='lang' target='urdu'/>\n"
790 " </submenu>\n"
791 " <item name='test unusual attributes'>\n"
792 " <attribute name='action' type='s'>'quite-some-action'</attribute>\n"
793 " <attribute name='target' type='i'>36</attribute>\n"
794 " <attribute name='chocolate-thunda' type='as'>['a','b']</attribute>\n"
795 " <attribute name='thing1' type='g'>'s(uu)'</attribute>\n"
796 " <attribute name='icon' type='s'>'small blue thing'</attribute>\n"
797 " </item>\n"
798 " </section>\n"
799 "</menu>\n";
800 GError *error = NULL;
801 GMenuModel *a;
802 GMenuModel *b;
803 gchar *s;
804 gchar *s2;
806 a = parse_menu_string (data, &error);
807 g_assert_no_error (error);
808 g_assert (G_IS_MENU_MODEL (a));
810 /* normalized representation */
811 s = menu_to_string (a);
813 b = parse_menu_string (s, &error);
814 g_assert_no_error (error);
815 g_assert (G_IS_MENU_MODEL (b));
817 assert_menus_equal (G_MENU_MODEL (a), G_MENU_MODEL (b));
819 s2 = menu_to_string (b);
821 g_assert_cmpstr (s, ==, s2);
823 g_object_unref (a);
824 g_object_unref (b);
825 g_free (s);
826 g_free (s2);
829 static void
830 test_attributes (void)
832 GMenu *menu;
833 GMenuItem *item;
834 GVariant *v;
836 menu = g_menu_new ();
838 item = g_menu_item_new ("test", NULL);
839 g_menu_item_set_attribute_value (item, "boolean", g_variant_new_boolean (FALSE));
840 g_menu_item_set_attribute_value (item, "string", g_variant_new_string ("bla"));
841 g_menu_item_set_attribute_value (item, "double", g_variant_new_double (1.5));
842 v = g_variant_new_parsed ("[('one', 1), ('two', %i), (%s, 3)]", 2, "three");
843 g_menu_item_set_attribute_value (item, "complex", v);
844 g_menu_item_set_attribute_value (item, "test-123", g_variant_new_string ("test-123"));
846 g_menu_append_item (menu, item);
848 g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (menu)), ==, 1);
850 v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "boolean", NULL);
851 g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN));
852 g_variant_unref (v);
854 v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "string", NULL);
855 g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING));
856 g_variant_unref (v);
858 v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "double", NULL);
859 g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_DOUBLE));
860 g_variant_unref (v);
862 v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "complex", NULL);
863 g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE("a(si)")));
864 g_variant_unref (v);
866 g_object_unref (menu);
869 static void
870 test_links (void)
872 GMenu *menu;
873 GMenuModel *m;
874 GMenuModel *x;
875 GMenuItem *item;
877 m = G_MENU_MODEL (g_menu_new ());
878 g_menu_append (G_MENU (m), "test", NULL);
880 menu = g_menu_new ();
882 item = g_menu_item_new ("test1", NULL);
883 g_menu_item_set_link (item, "section", m);
884 g_menu_append_item (menu, item);
886 item = g_menu_item_new ("test2", NULL);
887 g_menu_item_set_link (item, "submenu", m);
888 g_menu_append_item (menu, item);
890 item = g_menu_item_new ("test3", NULL);
891 g_menu_item_set_link (item, "wallet", m);
892 g_menu_append_item (menu, item);
894 item = g_menu_item_new ("test4", NULL);
895 g_menu_item_set_link (item, "purse", m);
896 g_menu_item_set_link (item, "purse", NULL);
897 g_menu_append_item (menu, item);
899 g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (menu)), ==, 4);
901 x = g_menu_model_get_item_link (G_MENU_MODEL (menu), 0, "section");
902 g_assert (x == m);
903 g_object_unref (x);
905 x = g_menu_model_get_item_link (G_MENU_MODEL (menu), 1, "submenu");
906 g_assert (x == m);
907 g_object_unref (x);
909 x = g_menu_model_get_item_link (G_MENU_MODEL (menu), 2, "wallet");
910 g_assert (x == m);
911 g_object_unref (x);
913 x = g_menu_model_get_item_link (G_MENU_MODEL (menu), 3, "purse");
914 g_assert (x == NULL);
916 g_object_unref (m);
917 g_object_unref (menu);
920 static void
921 test_mutable (void)
923 GMenu *menu;
925 menu = g_menu_new ();
926 g_menu_append (menu, "test", "test");
928 g_assert (g_menu_model_is_mutable (G_MENU_MODEL (menu)));
929 g_menu_freeze (menu);
930 g_assert (!g_menu_model_is_mutable (G_MENU_MODEL (menu)));
932 g_object_unref (menu);
935 static void
936 test_misc (void)
938 /* trying to use most of the GMenu api for constructing the
939 * same menu two different ways
941 GMenu *a, *m, *m2;
942 GMenuModel *b;
943 GMenuItem *item;
944 const gchar *s;
946 a = g_menu_new ();
947 item = g_menu_item_new ("test1", "action1::target1");
948 g_menu_prepend_item (a, item);
949 g_object_unref (item);
951 m = g_menu_new ();
952 g_menu_prepend (m, "test2a", "action2");
953 g_menu_append (m, "test2c", NULL);
954 g_menu_insert (m, 1, "test2b", NULL);
956 item = g_menu_item_new_submenu ("test2", G_MENU_MODEL (m));
957 g_menu_append_item (a, item);
958 g_object_unref (item);
959 g_object_unref (m);
961 m = g_menu_new ();
963 m2 = g_menu_new ();
964 g_menu_append (m2, "x", NULL);
965 g_menu_prepend_section (m, "test3a", G_MENU_MODEL (m2));
966 g_object_unref (m2);
968 item = g_menu_item_new_section ("test3", G_MENU_MODEL (m));
969 g_menu_insert_item (a, -1, item);
970 g_object_unref (item);
971 g_object_unref (m);
973 s = ""
974 "<menu>"
975 " <item target='target1' action='action1' label='test1'/>"
976 " <item label='test2'>"
977 " <link name='submenu'>"
978 " <item action='action2' label='test2a'/>"
979 " <item label='test2b'/>"
980 " <item label='test2c'/>"
981 " </link>"
982 " </item>"
983 " <item label='test3'>"
984 " <link name='section'>"
985 " <item label='test3a'>"
986 " <link name='section'>"
987 " <item label='x'/>"
988 " </link>"
989 " </item>"
990 " </link>"
991 " </item>"
992 "</menu>";
994 b = parse_menu_string (s, NULL);
996 assert_menus_equal (G_MENU_MODEL (a), G_MENU_MODEL (b));
997 g_object_unref (a);
998 g_object_unref (b);
1001 /* Epilogue {{{1 */
1003 main (int argc, char **argv)
1005 g_test_init (&argc, &argv, NULL);
1007 g_type_init ();
1009 g_test_add_func ("/gmenu/equality", test_equality);
1010 g_test_add_func ("/gmenu/random", test_random);
1011 g_test_add_func ("/gmenu/dbus/roundtrip", test_dbus_roundtrip);
1012 g_test_add_func ("/gmenu/dbus/subscriptions", test_dbus_subscriptions);
1013 g_test_add_func ("/gmenu/markup/roundtrip", test_markup_roundtrip);
1014 g_test_add_func ("/gmenu/attributes", test_attributes);
1015 g_test_add_func ("/gmenu/links", test_links);
1016 g_test_add_func ("/gmenu/mutable", test_mutable);
1017 g_test_add_func ("/gmenu/misc", test_misc);
1019 return g_test_run ();
1021 /* vim:set foldmethod=marker: */