Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / tests / gmenumodel.c
1 #include <gio/gio.h>
2
3 #include "gdbus-sessionbus.h"
4
5 /* Markup printing {{{1 */
6
7 /* This used to be part of GLib, but it was removed before the stable
8  * release because it wasn't generally useful.  We want it here, though.
9  */
10 static void
11 indent_string (GString *string,
12                gint     indent)
13 {
14   while (indent--)
15     g_string_append_c (string, ' ');
16 }
17
18 static GString *
19 g_menu_markup_print_string (GString    *string,
20                             GMenuModel *model,
21                             gint        indent,
22                             gint        tabstop)
23 {
24   gboolean need_nl = FALSE;
25   gint i, n;
26
27   if G_UNLIKELY (string == NULL)
28     string = g_string_new (NULL);
29
30   n = g_menu_model_get_n_items (model);
31
32   for (i = 0; i < n; i++)
33     {
34       GMenuAttributeIter *attr_iter;
35       GMenuLinkIter *link_iter;
36       GString *contents;
37       GString *attrs;
38
39       attr_iter = g_menu_model_iterate_item_attributes (model, i);
40       link_iter = g_menu_model_iterate_item_links (model, i);
41       contents = g_string_new (NULL);
42       attrs = g_string_new (NULL);
43
44       while (g_menu_attribute_iter_next (attr_iter))
45         {
46           const char *name = g_menu_attribute_iter_get_name (attr_iter);
47           GVariant *value = g_menu_attribute_iter_get_value (attr_iter);
48
49           if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
50             {
51               gchar *str;
52               str = g_markup_printf_escaped (" %s='%s'", name, g_variant_get_string (value, NULL));
53               g_string_append (attrs, str);
54               g_free (str);
55             }
56
57           else
58             {
59               gchar *printed;
60               gchar *str;
61               const gchar *type;
62
63               printed = g_variant_print (value, TRUE);
64               type = g_variant_type_peek_string (g_variant_get_type (value));
65               str = g_markup_printf_escaped ("<attribute name='%s' type='%s'>%s</attribute>\n", name, type, printed);
66               indent_string (contents, indent + tabstop);
67               g_string_append (contents, str);
68               g_free (printed);
69               g_free (str);
70             }
71
72           g_variant_unref (value);
73         }
74       g_object_unref (attr_iter);
75
76       while (g_menu_link_iter_next (link_iter))
77         {
78           const gchar *name = g_menu_link_iter_get_name (link_iter);
79           GMenuModel *menu = g_menu_link_iter_get_value (link_iter);
80           gchar *str;
81
82           if (contents->str[0])
83             g_string_append_c (contents, '\n');
84
85           str = g_markup_printf_escaped ("<link name='%s'>\n", name);
86           indent_string (contents, indent + tabstop);
87           g_string_append (contents, str);
88           g_free (str);
89
90           g_menu_markup_print_string (contents, menu, indent + 2 * tabstop, tabstop);
91
92           indent_string (contents, indent + tabstop);
93           g_string_append (contents, "</link>\n");
94           g_object_unref (menu);
95         }
96       g_object_unref (link_iter);
97
98       if (contents->str[0])
99         {
100           indent_string (string, indent);
101           g_string_append_printf (string, "<item%s>\n", attrs->str);
102           g_string_append (string, contents->str);
103           indent_string (string, indent);
104           g_string_append (string, "</item>\n");
105           need_nl = TRUE;
106         }
107
108       else
109         {
110           if (need_nl)
111             g_string_append_c (string, '\n');
112
113           indent_string (string, indent);
114           g_string_append_printf (string, "<item%s/>\n", attrs->str);
115           need_nl = FALSE;
116         }
117
118       g_string_free (contents, TRUE);
119       g_string_free (attrs, TRUE);
120     }
121
122   return string;
123 }
124
125 /* TestItem {{{1 */
126
127 /* This utility struct is used by both the RandomMenu and MirrorMenu
128  * class implementations below.
129  */
130 typedef struct {
131   GHashTable *attributes;
132   GHashTable *links;
133 } TestItem;
134
135 static TestItem *
136 test_item_new (GHashTable *attributes,
137                GHashTable *links)
138 {
139   TestItem *item;
140
141   item = g_slice_new (TestItem);
142   item->attributes = g_hash_table_ref (attributes);
143   item->links = g_hash_table_ref (links);
144
145   return item;
146 }
147
148 static void
149 test_item_free (gpointer data)
150 {
151   TestItem *item = data;
152
153   g_hash_table_unref (item->attributes);
154   g_hash_table_unref (item->links);
155
156   g_slice_free (TestItem, item);
157 }
158
159 /* RandomMenu {{{1 */
160 #define MAX_ITEMS 5
161 #define TOP_ORDER 4
162
163 typedef struct {
164   GMenuModel parent_instance;
165
166   GSequence *items;
167   gint order;
168 } RandomMenu;
169
170 typedef GMenuModelClass RandomMenuClass;
171
172 static GType random_menu_get_type (void);
173 G_DEFINE_TYPE (RandomMenu, random_menu, G_TYPE_MENU_MODEL);
174
175 static gboolean
176 random_menu_is_mutable (GMenuModel *model)
177 {
178   return TRUE;
179 }
180
181 static gint
182 random_menu_get_n_items (GMenuModel *model)
183 {
184   RandomMenu *menu = (RandomMenu *) model;
185
186   return g_sequence_get_length (menu->items);
187 }
188
189 static void
190 random_menu_get_item_attributes (GMenuModel  *model,
191                                  gint         position,
192                                  GHashTable **table)
193 {
194   RandomMenu *menu = (RandomMenu *) model;
195   TestItem *item;
196
197   item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
198   *table = g_hash_table_ref (item->attributes);
199 }
200
201 static void
202 random_menu_get_item_links (GMenuModel  *model,
203                             gint         position,
204                             GHashTable **table)
205 {
206   RandomMenu *menu = (RandomMenu *) model;
207   TestItem *item;
208
209   item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
210   *table = g_hash_table_ref (item->links);
211 }
212
213 static void
214 random_menu_finalize (GObject *object)
215 {
216   RandomMenu *menu = (RandomMenu *) object;
217
218   g_sequence_free (menu->items);
219
220   G_OBJECT_CLASS (random_menu_parent_class)
221     ->finalize (object);
222 }
223
224 static void
225 random_menu_init (RandomMenu *menu)
226 {
227 }
228
229 static void
230 random_menu_class_init (GMenuModelClass *class)
231 {
232   GObjectClass *object_class = G_OBJECT_CLASS (class);
233
234   class->is_mutable = random_menu_is_mutable;
235   class->get_n_items = random_menu_get_n_items;
236   class->get_item_attributes = random_menu_get_item_attributes;
237   class->get_item_links = random_menu_get_item_links;
238
239   object_class->finalize = random_menu_finalize;
240 }
241
242 static RandomMenu * random_menu_new (GRand *rand, gint order);
243
244 static void
245 random_menu_change (RandomMenu *menu,
246                     GRand      *rand)
247 {
248   gint position, removes, adds;
249   GSequenceIter *point;
250   gint n_items;
251   gint i;
252
253   n_items = g_sequence_get_length (menu->items);
254
255   do
256     {
257       position = g_rand_int_range (rand, 0, n_items + 1);
258       removes = g_rand_int_range (rand, 0, n_items - position + 1);
259       adds = g_rand_int_range (rand, 0, MAX_ITEMS - (n_items - removes) + 1);
260     }
261   while (removes == 0 && adds == 0);
262
263   point = g_sequence_get_iter_at_pos (menu->items, position + removes);
264
265   if (removes)
266     {
267       GSequenceIter *start;
268
269       start = g_sequence_get_iter_at_pos (menu->items, position);
270       g_sequence_remove_range (start, point);
271     }
272
273   for (i = 0; i < adds; i++)
274     {
275       const gchar *label;
276       GHashTable *links;
277       GHashTable *attributes;
278
279       attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
280       links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_object_unref);
281
282       if (menu->order > 0 && g_rand_boolean (rand))
283         {
284           RandomMenu *child;
285           const gchar *subtype;
286
287           child = random_menu_new (rand, menu->order - 1);
288
289           if (g_rand_boolean (rand))
290             {
291               subtype = G_MENU_LINK_SECTION;
292               /* label some section headers */
293               if (g_rand_boolean (rand))
294                 label = "Section";
295               else
296                 label = NULL;
297             }
298           else
299             {
300               /* label all submenus */
301               subtype = G_MENU_LINK_SUBMENU;
302               label = "Submenu";
303             }
304
305           g_hash_table_insert (links, g_strdup (subtype), child);
306         }
307       else
308         /* label all terminals */
309         label = "Menu Item";
310
311       if (label)
312         g_hash_table_insert (attributes, g_strdup ("label"), g_variant_ref_sink (g_variant_new_string (label)));
313
314       g_sequence_insert_before (point, test_item_new (attributes, links));
315       g_hash_table_unref (links);
316       g_hash_table_unref (attributes);
317     }
318
319   g_menu_model_items_changed (G_MENU_MODEL (menu), position, removes, adds);
320 }
321
322 static RandomMenu *
323 random_menu_new (GRand *rand,
324                  gint   order)
325 {
326   RandomMenu *menu;
327
328   menu = g_object_new (random_menu_get_type (), NULL);
329   menu->items = g_sequence_new (test_item_free);
330   menu->order = order;
331
332   random_menu_change (menu, rand);
333
334   return menu;
335 }
336
337 /* MirrorMenu {{{1 */
338 typedef struct {
339   GMenuModel parent_instance;
340
341   GMenuModel *clone_of;
342   GSequence *items;
343   gulong handler_id;
344 } MirrorMenu;
345
346 typedef GMenuModelClass MirrorMenuClass;
347
348 static GType mirror_menu_get_type (void);
349 G_DEFINE_TYPE (MirrorMenu, mirror_menu, G_TYPE_MENU_MODEL);
350
351 static gboolean
352 mirror_menu_is_mutable (GMenuModel *model)
353 {
354   MirrorMenu *menu = (MirrorMenu *) model;
355
356   return menu->handler_id != 0;
357 }
358
359 static gint
360 mirror_menu_get_n_items (GMenuModel *model)
361 {
362   MirrorMenu *menu = (MirrorMenu *) model;
363
364   return g_sequence_get_length (menu->items);
365 }
366
367 static void
368 mirror_menu_get_item_attributes (GMenuModel  *model,
369                                  gint         position,
370                                  GHashTable **table)
371 {
372   MirrorMenu *menu = (MirrorMenu *) model;
373   TestItem *item;
374
375   item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
376   *table = g_hash_table_ref (item->attributes);
377 }
378
379 static void
380 mirror_menu_get_item_links (GMenuModel  *model,
381                             gint         position,
382                             GHashTable **table)
383 {
384   MirrorMenu *menu = (MirrorMenu *) model;
385   TestItem *item;
386
387   item = g_sequence_get (g_sequence_get_iter_at_pos (menu->items, position));
388   *table = g_hash_table_ref (item->links);
389 }
390
391 static void
392 mirror_menu_finalize (GObject *object)
393 {
394   MirrorMenu *menu = (MirrorMenu *) object;
395
396   if (menu->handler_id)
397     g_signal_handler_disconnect (menu->clone_of, menu->handler_id);
398
399   g_sequence_free (menu->items);
400   g_object_unref (menu->clone_of);
401
402   G_OBJECT_CLASS (mirror_menu_parent_class)
403     ->finalize (object);
404 }
405
406 static void
407 mirror_menu_init (MirrorMenu *menu)
408 {
409 }
410
411 static void
412 mirror_menu_class_init (GMenuModelClass *class)
413 {
414   GObjectClass *object_class = G_OBJECT_CLASS (class);
415
416   class->is_mutable = mirror_menu_is_mutable;
417   class->get_n_items = mirror_menu_get_n_items;
418   class->get_item_attributes = mirror_menu_get_item_attributes;
419   class->get_item_links = mirror_menu_get_item_links;
420
421   object_class->finalize = mirror_menu_finalize;
422 }
423
424 static MirrorMenu * mirror_menu_new (GMenuModel *clone_of);
425
426 static void
427 mirror_menu_changed (GMenuModel *model,
428                      gint        position,
429                      gint        removed,
430                      gint        added,
431                      gpointer    user_data)
432 {
433   MirrorMenu *menu = user_data;
434   GSequenceIter *point;
435   gint i;
436
437   g_assert (model == menu->clone_of);
438
439   point = g_sequence_get_iter_at_pos (menu->items, position + removed);
440
441   if (removed)
442     {
443       GSequenceIter *start;
444
445       start = g_sequence_get_iter_at_pos (menu->items, position);
446       g_sequence_remove_range (start, point);
447     }
448
449   for (i = position; i < position + added; i++)
450     {
451       GMenuAttributeIter *attr_iter;
452       GMenuLinkIter *link_iter;
453       GHashTable *links;
454       GHashTable *attributes;
455       const gchar *name;
456       GMenuModel *child;
457       GVariant *value;
458
459       attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_variant_unref);
460       links = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_object_unref);
461
462       attr_iter = g_menu_model_iterate_item_attributes (model, i);
463       while (g_menu_attribute_iter_get_next (attr_iter, &name, &value))
464         {
465           g_hash_table_insert (attributes, g_strdup (name), value);
466         }
467       g_object_unref (attr_iter);
468
469       link_iter = g_menu_model_iterate_item_links (model, i);
470       while (g_menu_link_iter_get_next (link_iter, &name, &child))
471         {
472           g_hash_table_insert (links, g_strdup (name), mirror_menu_new (child));
473           g_object_unref (child);
474         }
475       g_object_unref (link_iter);
476
477       g_sequence_insert_before (point, test_item_new (attributes, links));
478       g_hash_table_unref (attributes);
479       g_hash_table_unref (links);
480     }
481
482   g_menu_model_items_changed (G_MENU_MODEL (menu), position, removed, added);
483 }
484
485 static MirrorMenu *
486 mirror_menu_new (GMenuModel *clone_of)
487 {
488   MirrorMenu *menu;
489
490   menu = g_object_new (mirror_menu_get_type (), NULL);
491   menu->items = g_sequence_new (test_item_free);
492   menu->clone_of = g_object_ref (clone_of);
493
494   if (g_menu_model_is_mutable (clone_of))
495     menu->handler_id = g_signal_connect (clone_of, "items-changed", G_CALLBACK (mirror_menu_changed), menu);
496   mirror_menu_changed (clone_of, 0, 0, g_menu_model_get_n_items (clone_of), menu);
497
498   return menu;
499 }
500
501 /* check_menus_equal(), assert_menus_equal() {{{1 */
502 static gboolean
503 check_menus_equal (GMenuModel *a,
504                    GMenuModel *b)
505 {
506   gboolean equal = TRUE;
507   gint a_n, b_n;
508   gint i;
509
510   a_n = g_menu_model_get_n_items (a);
511   b_n = g_menu_model_get_n_items (b);
512
513   if (a_n != b_n)
514     return FALSE;
515
516   for (i = 0; i < a_n; i++)
517     {
518       GMenuAttributeIter *attr_iter;
519       GVariant *a_value, *b_value;
520       GMenuLinkIter *link_iter;
521       GMenuModel *a_menu, *b_menu;
522       const gchar *name;
523
524       attr_iter = g_menu_model_iterate_item_attributes (a, i);
525       while (g_menu_attribute_iter_get_next (attr_iter, &name, &a_value))
526         {
527           b_value = g_menu_model_get_item_attribute_value (b, i, name, NULL);
528           equal &= b_value && g_variant_equal (a_value, b_value);
529           if (b_value)
530             g_variant_unref (b_value);
531           g_variant_unref (a_value);
532         }
533       g_object_unref (attr_iter);
534
535       attr_iter = g_menu_model_iterate_item_attributes (b, i);
536       while (g_menu_attribute_iter_get_next (attr_iter, &name, &b_value))
537         {
538           a_value = g_menu_model_get_item_attribute_value (a, i, name, NULL);
539           equal &= a_value && g_variant_equal (a_value, b_value);
540           if (a_value)
541             g_variant_unref (a_value);
542           g_variant_unref (b_value);
543         }
544       g_object_unref (attr_iter);
545
546       link_iter = g_menu_model_iterate_item_links (a, i);
547       while (g_menu_link_iter_get_next (link_iter, &name, &a_menu))
548         {
549           b_menu = g_menu_model_get_item_link (b, i, name);
550           equal &= b_menu && check_menus_equal (a_menu, b_menu);
551           if (b_menu)
552             g_object_unref (b_menu);
553           g_object_unref (a_menu);
554         }
555       g_object_unref (link_iter);
556
557       link_iter = g_menu_model_iterate_item_links (b, i);
558       while (g_menu_link_iter_get_next (link_iter, &name, &b_menu))
559         {
560           a_menu = g_menu_model_get_item_link (a, i, name);
561           equal &= a_menu && check_menus_equal (a_menu, b_menu);
562           if (a_menu)
563             g_object_unref (a_menu);
564           g_object_unref (b_menu);
565         }
566       g_object_unref (link_iter);
567     }
568
569   return equal;
570 }
571
572 static void
573 assert_menus_equal (GMenuModel *a,
574                     GMenuModel *b)
575 {
576   if (!check_menus_equal (a, b))
577     {
578       GString *string;
579
580       string = g_string_new ("\n  <a>\n");
581       g_menu_markup_print_string (string, G_MENU_MODEL (a), 4, 2);
582       g_string_append (string, "  </a>\n\n-------------\n  <b>\n");
583       g_menu_markup_print_string (string, G_MENU_MODEL (b), 4, 2);
584       g_string_append (string, "  </b>\n");
585       g_error ("%s", string->str);
586     }
587 }
588
589 /* Test cases {{{1 */
590 static void
591 test_equality (void)
592 {
593   GRand *randa, *randb;
594   guint32 seed;
595   gint i;
596
597   seed = g_test_rand_int ();
598
599   randa = g_rand_new_with_seed (seed);
600   randb = g_rand_new_with_seed (seed);
601
602   for (i = 0; i < 500; i++)
603     {
604       RandomMenu *a, *b;
605
606       a = random_menu_new (randa, TOP_ORDER);
607       b = random_menu_new (randb, TOP_ORDER);
608       assert_menus_equal (G_MENU_MODEL (a), G_MENU_MODEL (b));
609       g_object_unref (b);
610       g_object_unref (a);
611     }
612
613   g_rand_int (randa);
614
615   for (i = 0; i < 500;)
616     {
617       RandomMenu *a, *b;
618
619       a = random_menu_new (randa, TOP_ORDER);
620       b = random_menu_new (randb, TOP_ORDER);
621       if (check_menus_equal (G_MENU_MODEL (a), G_MENU_MODEL (b)))
622         {
623           /* by chance, they may really be equal.  double check. */
624           GString *as, *bs;
625
626           as = g_menu_markup_print_string (NULL, G_MENU_MODEL (a), 4, 2);
627           bs = g_menu_markup_print_string (NULL, G_MENU_MODEL (b), 4, 2);
628           g_assert_cmpstr (as->str, ==, bs->str);
629           g_string_free (bs, TRUE);
630           g_string_free (as, TRUE);
631
632           /* we're here because randa and randb just generated equal
633            * menus.  they may do it again, so throw away randb and make
634            * a fresh one.
635            */
636           g_rand_free (randb);
637           randb = g_rand_new_with_seed (g_rand_int (randa));
638         }
639       else
640         /* make sure we get enough unequals (ie: no GRand failure) */
641         i++;
642
643       g_object_unref (b);
644       g_object_unref (a);
645     }
646
647   g_rand_free (randb);
648   g_rand_free (randa);
649 }
650
651 static void
652 test_random (void)
653 {
654   RandomMenu *random;
655   MirrorMenu *mirror;
656   GRand *rand;
657   gint i;
658
659   rand = g_rand_new_with_seed (g_test_rand_int ());
660   random = random_menu_new (rand, TOP_ORDER);
661   mirror = mirror_menu_new (G_MENU_MODEL (random));
662
663   for (i = 0; i < 500; i++)
664     {
665       assert_menus_equal (G_MENU_MODEL (random), G_MENU_MODEL (mirror));
666       random_menu_change (random, rand);
667     }
668
669   g_object_unref (mirror);
670   g_object_unref (random);
671
672   g_rand_free (rand);
673 }
674
675 struct roundtrip_state
676 {
677   RandomMenu *random;
678   MirrorMenu *proxy_mirror;
679   GDBusMenuModel *proxy;
680   GMainLoop *loop;
681   GRand *rand;
682   gint success;
683   gint count;
684 };
685
686 static gboolean
687 roundtrip_step (gpointer data)
688 {
689   struct roundtrip_state *state = data;
690
691   if (check_menus_equal (G_MENU_MODEL (state->random), G_MENU_MODEL (state->proxy)) &&
692       check_menus_equal (G_MENU_MODEL (state->random), G_MENU_MODEL (state->proxy_mirror)))
693     {
694       state->success++;
695       state->count = 0;
696
697       if (state->success < 100)
698         random_menu_change (state->random, state->rand);
699       else
700         g_main_loop_quit (state->loop);
701     }
702   else if (state->count == 100)
703     {
704       assert_menus_equal (G_MENU_MODEL (state->random), G_MENU_MODEL (state->proxy));
705       g_assert_not_reached ();
706     }
707   else
708     state->count++;
709
710   return G_SOURCE_CONTINUE;
711 }
712
713 static void
714 test_dbus_roundtrip (void)
715 {
716   struct roundtrip_state state;
717   GDBusConnection *bus;
718   guint export_id;
719   guint id;
720
721   bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
722
723   state.rand = g_rand_new_with_seed (g_test_rand_int ());
724
725   state.random = random_menu_new (state.rand, 2);
726   export_id = g_dbus_connection_export_menu_model (bus, "/", G_MENU_MODEL (state.random), NULL);
727   state.proxy = g_dbus_menu_model_get (bus, g_dbus_connection_get_unique_name (bus), "/");
728   state.proxy_mirror = mirror_menu_new (G_MENU_MODEL (state.proxy));
729   state.count = 0;
730   state.success = 0;
731
732   id = g_timeout_add (10, roundtrip_step, &state);
733
734   state.loop = g_main_loop_new (NULL, FALSE);
735   g_main_loop_run (state.loop);
736
737   g_main_loop_unref (state.loop);
738   g_source_remove (id);
739   g_object_unref (state.proxy);
740   g_dbus_connection_unexport_menu_model (bus, export_id);
741   g_object_unref (state.random);
742   g_object_unref (state.proxy_mirror);
743   g_rand_free (state.rand);
744   g_object_unref (bus);
745 }
746
747 static gint items_changed_count;
748
749 static void
750 items_changed (GMenuModel *model,
751                gint        position,
752                gint        removed,
753                gint        added,
754                gpointer    data)
755 {
756   items_changed_count++;
757 }
758
759 static gboolean
760 stop_loop (gpointer data)
761 {
762   GMainLoop *loop = data;
763
764   g_main_loop_quit (loop);
765
766   return G_SOURCE_REMOVE;
767 }
768
769 static void
770 test_dbus_subscriptions (void)
771 {
772   GDBusConnection *bus;
773   GMenu *menu;
774   GDBusMenuModel *proxy;
775   GMainLoop *loop;
776   GError *error = NULL;
777   guint export_id;
778
779   loop = g_main_loop_new (NULL, FALSE);
780
781   bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
782
783   menu = g_menu_new ();
784
785   export_id = g_dbus_connection_export_menu_model (bus, "/", G_MENU_MODEL (menu), &error);
786   g_assert_no_error (error);
787
788   proxy = g_dbus_menu_model_get (bus, g_dbus_connection_get_unique_name (bus), "/");
789   items_changed_count = 0;
790   g_signal_connect (proxy, "items-changed",
791                     G_CALLBACK (items_changed), NULL);
792
793   g_menu_append (menu, "item1", NULL);
794   g_menu_append (menu, "item2", NULL);
795   g_menu_append (menu, "item3", NULL);
796
797   g_assert_cmpint (items_changed_count, ==, 0);
798
799   g_timeout_add (100, stop_loop, loop);
800   g_main_loop_run (loop);
801
802   g_menu_model_get_n_items (G_MENU_MODEL (proxy));
803
804   g_timeout_add (100, stop_loop, loop);
805   g_main_loop_run (loop);
806
807   g_assert_cmpint (items_changed_count, ==, 1);
808   g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (proxy)), ==, 3);
809
810   g_timeout_add (100, stop_loop, loop);
811   g_main_loop_run (loop);
812
813   g_menu_append (menu, "item4", NULL);
814   g_menu_append (menu, "item5", NULL);
815   g_menu_append (menu, "item6", NULL);
816   g_menu_remove (menu, 0);
817   g_menu_remove (menu, 0);
818
819   g_timeout_add (200, stop_loop, loop);
820   g_main_loop_run (loop);
821
822   g_assert_cmpint (items_changed_count, ==, 6);
823
824   g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (proxy)), ==, 4);
825   g_object_unref (proxy);
826
827   g_timeout_add (100, stop_loop, loop);
828   g_main_loop_run (loop);
829
830   g_menu_remove (menu, 0);
831   g_menu_remove (menu, 0);
832
833   g_timeout_add (100, stop_loop, loop);
834   g_main_loop_run (loop);
835
836   g_assert_cmpint (items_changed_count, ==, 6);
837
838   g_dbus_connection_unexport_menu_model (bus, export_id);
839   g_object_unref (menu);
840
841   g_main_loop_unref (loop);
842 }
843
844 static gpointer
845 do_modify (gpointer data)
846 {
847   RandomMenu *menu = data;
848   GRand *rand;
849   gint i;
850
851   rand = g_rand_new_with_seed (g_test_rand_int ());
852
853   for (i = 0; i < 10000; i++)
854     {
855       random_menu_change (menu, rand);
856     }
857
858   return NULL;
859 }
860
861 static gpointer
862 do_export (gpointer data)
863 {
864   GMenuModel *menu = data;
865   gint i;
866   GDBusConnection *bus;
867   gchar *path;
868   GError *error = NULL;
869   guint id;
870
871   bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
872   path = g_strdup_printf ("/%p", data);
873
874   for (i = 0; i < 10000; i++)
875     {
876       id = g_dbus_connection_export_menu_model (bus, path, menu, &error);
877       g_assert_no_error (error);
878       g_dbus_connection_unexport_menu_model (bus, id);
879       while (g_main_context_iteration (NULL, FALSE));
880     }
881
882   g_free (path);
883
884   g_object_unref (bus);
885
886   return NULL;
887 }
888
889 static void
890 test_dbus_threaded (void)
891 {
892   RandomMenu *menu[10];
893   GThread *call[10];
894   GThread *export[10];
895   gint i;
896
897   for (i = 0; i < 10; i++)
898     {
899       menu[i] = random_menu_new (g_rand_new_with_seed (g_test_rand_int ()), 2);
900       call[i] = g_thread_new ("call", do_modify, menu[i]);
901       export[i] = g_thread_new ("export", do_export, menu[i]);
902     }
903
904   for (i = 0; i < 10; i++)
905     {
906       g_thread_join (call[i]);
907       g_thread_join (export[i]);
908     }
909
910   for (i = 0; i < 10; i++)
911     g_object_unref (menu[i]);
912 }
913
914 static void
915 test_attributes (void)
916 {
917   GMenu *menu;
918   GMenuItem *item;
919   GVariant *v;
920
921   menu = g_menu_new ();
922
923   item = g_menu_item_new ("test", NULL);
924   g_menu_item_set_attribute_value (item, "boolean", g_variant_new_boolean (FALSE));
925   g_menu_item_set_attribute_value (item, "string", g_variant_new_string ("bla"));
926   g_menu_item_set_attribute_value (item, "double", g_variant_new_double (1.5));
927   v = g_variant_new_parsed ("[('one', 1), ('two', %i), (%s, 3)]", 2, "three");
928   g_menu_item_set_attribute_value (item, "complex", v);
929   g_menu_item_set_attribute_value (item, "test-123", g_variant_new_string ("test-123"));
930
931   g_menu_append_item (menu, item);
932
933   g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (menu)), ==, 1);
934
935   v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "boolean", NULL);
936   g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN));
937   g_variant_unref (v);
938
939   v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "string", NULL);
940   g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING));
941   g_variant_unref (v);
942
943   v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "double", NULL);
944   g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE_DOUBLE));
945   g_variant_unref (v);
946
947   v = g_menu_model_get_item_attribute_value (G_MENU_MODEL (menu), 0, "complex", NULL);
948   g_assert (g_variant_is_of_type (v, G_VARIANT_TYPE("a(si)")));
949   g_variant_unref (v);
950
951   g_object_unref (menu);
952 }
953
954 static void
955 test_links (void)
956 {
957   GMenu *menu;
958   GMenuModel *m;
959   GMenuModel *x;
960   GMenuItem *item;
961
962   m = G_MENU_MODEL (g_menu_new ());
963   g_menu_append (G_MENU (m), "test", NULL);
964
965   menu = g_menu_new ();
966
967   item = g_menu_item_new ("test1", NULL);
968   g_menu_item_set_link (item, "section", m);
969   g_menu_append_item (menu, item);
970
971   item = g_menu_item_new ("test2", NULL);
972   g_menu_item_set_link (item, "submenu", m);
973   g_menu_append_item (menu, item);
974
975   item = g_menu_item_new ("test3", NULL);
976   g_menu_item_set_link (item, "wallet", m);
977   g_menu_append_item (menu, item);
978
979   item = g_menu_item_new ("test4", NULL);
980   g_menu_item_set_link (item, "purse", m);
981   g_menu_item_set_link (item, "purse", NULL);
982   g_menu_append_item (menu, item);
983
984   g_assert_cmpint (g_menu_model_get_n_items (G_MENU_MODEL (menu)), ==, 4);
985
986   x = g_menu_model_get_item_link (G_MENU_MODEL (menu), 0, "section");
987   g_assert (x == m);
988   g_object_unref (x);
989
990   x = g_menu_model_get_item_link (G_MENU_MODEL (menu), 1, "submenu");
991   g_assert (x == m);
992   g_object_unref (x);
993
994   x = g_menu_model_get_item_link (G_MENU_MODEL (menu), 2, "wallet");
995   g_assert (x == m);
996   g_object_unref (x);
997
998   x = g_menu_model_get_item_link (G_MENU_MODEL (menu), 3, "purse");
999   g_assert (x == NULL);
1000
1001   g_object_unref (m);
1002   g_object_unref (menu);
1003 }
1004
1005 static void
1006 test_mutable (void)
1007 {
1008   GMenu *menu;
1009
1010   menu = g_menu_new ();
1011   g_menu_append (menu, "test", "test");
1012
1013   g_assert (g_menu_model_is_mutable (G_MENU_MODEL (menu)));
1014   g_menu_freeze (menu);
1015   g_assert (!g_menu_model_is_mutable (G_MENU_MODEL (menu)));
1016
1017   g_object_unref (menu);
1018 }
1019
1020 /* Epilogue {{{1 */
1021 int
1022 main (int argc, char **argv)
1023 {
1024   gboolean ret;
1025
1026   g_test_init (&argc, &argv, NULL);
1027   g_type_init ();
1028
1029   g_unsetenv ("DISPLAY");
1030   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
1031
1032   session_bus_up ();
1033
1034   g_test_add_func ("/gmenu/equality", test_equality);
1035   g_test_add_func ("/gmenu/random", test_random);
1036   g_test_add_func ("/gmenu/dbus/roundtrip", test_dbus_roundtrip);
1037   g_test_add_func ("/gmenu/dbus/subscriptions", test_dbus_subscriptions);
1038   g_test_add_func ("/gmenu/dbus/threaded", test_dbus_threaded);
1039   g_test_add_func ("/gmenu/attributes", test_attributes);
1040   g_test_add_func ("/gmenu/links", test_links);
1041   g_test_add_func ("/gmenu/mutable", test_mutable);
1042
1043   ret = g_test_run ();
1044
1045   session_bus_down ();
1046
1047   return ret;
1048 }
1049 /* vim:set foldmethod=marker: */