Add some more cases to the app-id unit tests
[glib.git] / gio / tests / appmonitor.c
blob34661d88b1d964c49feb6b46d27ad944b46c9124
1 #include <gio/gio.h>
2 #include <gstdio.h>
4 static gboolean
5 create_app (gpointer data)
7 const gchar *path = data;
8 GError *error = NULL;
9 const gchar *contents =
10 "[Desktop Entry]\n"
11 "Name=Application\n"
12 "Version=1.0\n"
13 "Type=Application\n"
14 "Exec=true\n";
16 g_file_set_contents (path, contents, -1, &error);
17 g_assert_no_error (error);
19 return G_SOURCE_REMOVE;
22 static void
23 delete_app (gpointer data)
25 const gchar *path = data;
27 g_remove (path);
30 static gboolean changed_fired;
32 static void
33 changed_cb (GAppInfoMonitor *monitor, GMainLoop *loop)
35 changed_fired = TRUE;
36 g_main_loop_quit (loop);
39 static gboolean
40 quit_loop (gpointer data)
42 GMainLoop *loop = data;
44 if (g_main_loop_is_running (loop))
45 g_main_loop_quit (loop);
47 return G_SOURCE_REMOVE;
50 static void
51 test_app_monitor (void)
53 gchar *path, *app_path;
54 GAppInfoMonitor *monitor;
55 GMainLoop *loop;
57 path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
58 g_mkdir (path, 0755);
60 app_path = g_build_filename (path, "app.desktop", NULL);
62 /* FIXME: this shouldn't be required */
63 g_list_free_full (g_app_info_get_all (), g_object_unref);
65 monitor = g_app_info_monitor_get ();
66 loop = g_main_loop_new (NULL, FALSE);
68 g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), loop);
70 g_idle_add (create_app, app_path);
71 g_timeout_add_seconds (3, quit_loop, loop);
73 g_main_loop_run (loop);
74 g_assert (changed_fired);
75 changed_fired = FALSE;
77 /* FIXME: this shouldn't be required */
78 g_list_free_full (g_app_info_get_all (), g_object_unref);
80 g_timeout_add_seconds (3, quit_loop, loop);
82 delete_app (app_path);
84 g_main_loop_run (loop);
86 g_assert (changed_fired);
88 g_main_loop_unref (loop);
89 g_remove (app_path);
91 g_object_unref (monitor);
93 g_free (path);
94 g_free (app_path);
97 int
98 main (int argc, char *argv[])
100 gchar *path;
102 path = g_mkdtemp (g_strdup ("app_monitor_XXXXXX"));
103 g_setenv ("XDG_DATA_DIRS", path, TRUE);
104 g_setenv ("XDG_DATA_HOME", path, TRUE);
106 g_test_init (&argc, &argv, NULL);
108 g_test_add_func ("/monitor/app", test_app_monitor);
110 return g_test_run ();