[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / tests / appmonitor.c
1 #include <gio/gio.h>
2 #include <gstdio.h>
3
4 static gboolean
5 create_app (gpointer data)
6 {
7   const gchar *path = data;
8   gchar *file;
9   GError *error = NULL;
10   const gchar *contents = 
11     "[Desktop Entry]\n"
12     "Name=Application\n"
13     "Version=1.0\n"
14     "Type=Application\n"
15     "Exec=true\n";
16
17   file = g_build_filename (path, "app.desktop", NULL);
18
19   g_file_set_contents (file, contents, -1, &error);
20   g_assert_no_error (error);
21
22   g_free (file);
23
24   return G_SOURCE_REMOVE;
25 }
26
27 static gboolean
28 delete_app (gpointer data)
29 {
30   const gchar *path = data;
31   gchar *file;
32
33   file = g_build_filename (path, "app.desktop", NULL);
34
35   g_remove (file);
36
37   g_free (file);
38
39   return G_SOURCE_REMOVE;
40 }
41
42 static gboolean changed_fired;
43
44 static void
45 changed_cb (GAppInfoMonitor *monitor, GMainLoop *loop)
46 {
47   changed_fired = TRUE;
48   g_main_loop_quit (loop);
49 }
50
51 static gboolean
52 quit_loop (gpointer data)
53 {
54   GMainLoop *loop = data;
55
56   g_main_loop_quit (loop);
57
58   return G_SOURCE_REMOVE;
59 }
60
61 static void
62 test_app_monitor (void)
63 {
64   gchar *path;
65   GAppInfoMonitor *monitor;
66   GMainLoop *loop;
67
68   path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
69   g_mkdir (path, 0755);
70
71   /* FIXME: this shouldn't be required */
72   g_list_free_full (g_app_info_get_all (), g_object_unref);
73
74   monitor = g_app_info_monitor_get ();
75   loop = g_main_loop_new (NULL, FALSE);
76
77   g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), loop);
78
79   g_idle_add (create_app, path);
80   g_timeout_add_seconds (3, quit_loop, loop);
81
82   g_main_loop_run (loop);
83   g_assert (changed_fired);
84   changed_fired = FALSE;
85
86   /* FIXME: this shouldn't be required */
87   g_list_free_full (g_app_info_get_all (), g_object_unref);
88
89   g_idle_add (delete_app, path);
90   g_timeout_add_seconds (3, quit_loop, loop);
91
92   g_main_loop_run (loop);
93   g_assert (changed_fired);
94
95   g_main_loop_unref (loop);
96
97   g_object_unref (monitor);
98   g_free (path);
99 }
100
101 int
102 main (int argc, char *argv[])
103 {
104   gchar *path;
105
106   path = g_mkdtemp (g_strdup ("app_monitor_XXXXXX"));
107   g_setenv ("XDG_DATA_DIRS", path, TRUE);
108   g_setenv ("XDG_DATA_HOME", path, TRUE);
109
110   g_test_init (&argc, &argv, NULL);
111
112   g_test_add_func ("/monitor/app", test_app_monitor);
113
114   return g_test_run ();
115 }