Meson: Group all glib tests into a single dict
[glib.git] / gio / tests / appmonitor.c
blob30ea108fb35eb937d5611c999e1bb7a96802823f
1 #include <gio/gio.h>
2 #include <gstdio.h>
4 typedef struct
6 gchar *data_dir;
7 gchar *applications_dir;
8 } Fixture;
10 static void
11 setup (Fixture *fixture,
12 gconstpointer user_data)
14 GError *error = NULL;
16 fixture->data_dir = g_dir_make_tmp ("gio-test-app-monitor_XXXXXX", &error);
17 g_assert_no_error (error);
19 fixture->applications_dir = g_build_filename (fixture->data_dir, "applications", NULL);
20 g_assert_cmpint (g_mkdir (fixture->applications_dir, 0755), ==, 0);
22 g_setenv ("XDG_DATA_DIRS", fixture->data_dir, TRUE);
23 g_setenv ("XDG_DATA_HOME", fixture->data_dir, TRUE);
25 g_test_message ("Using data directory: %s", fixture->data_dir);
28 static void
29 teardown (Fixture *fixture,
30 gconstpointer user_data)
32 g_assert_cmpint (g_rmdir (fixture->applications_dir), ==, 0);
33 g_clear_pointer (&fixture->applications_dir, g_free);
35 g_assert_cmpint (g_rmdir (fixture->data_dir), ==, 0);
36 g_clear_pointer (&fixture->data_dir, g_free);
39 static gboolean
40 create_app (gpointer data)
42 const gchar *path = data;
43 GError *error = NULL;
44 const gchar *contents =
45 "[Desktop Entry]\n"
46 "Name=Application\n"
47 "Version=1.0\n"
48 "Type=Application\n"
49 "Exec=true\n";
51 g_file_set_contents (path, contents, -1, &error);
52 g_assert_no_error (error);
54 return G_SOURCE_REMOVE;
57 static void
58 delete_app (gpointer data)
60 const gchar *path = data;
62 g_remove (path);
65 static gboolean changed_fired;
67 static void
68 changed_cb (GAppInfoMonitor *monitor, GMainLoop *loop)
70 changed_fired = TRUE;
71 g_main_loop_quit (loop);
74 static gboolean
75 quit_loop (gpointer data)
77 GMainLoop *loop = data;
79 if (g_main_loop_is_running (loop))
80 g_main_loop_quit (loop);
82 return G_SOURCE_REMOVE;
85 static void
86 test_app_monitor (Fixture *fixture,
87 gconstpointer user_data)
89 gchar *app_path;
90 GAppInfoMonitor *monitor;
91 GMainLoop *loop;
93 app_path = g_build_filename (fixture->applications_dir, "app.desktop", NULL);
95 /* FIXME: this shouldn't be required */
96 g_list_free_full (g_app_info_get_all (), g_object_unref);
98 monitor = g_app_info_monitor_get ();
99 loop = g_main_loop_new (NULL, FALSE);
101 g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), loop);
103 g_idle_add (create_app, app_path);
104 g_timeout_add_seconds (3, quit_loop, loop);
106 g_main_loop_run (loop);
107 g_assert (changed_fired);
108 changed_fired = FALSE;
110 /* FIXME: this shouldn't be required */
111 g_list_free_full (g_app_info_get_all (), g_object_unref);
113 g_timeout_add_seconds (3, quit_loop, loop);
115 delete_app (app_path);
117 g_main_loop_run (loop);
119 g_assert (changed_fired);
121 g_main_loop_unref (loop);
122 g_remove (app_path);
124 g_object_unref (monitor);
126 g_free (app_path);
130 main (int argc, char *argv[])
132 g_test_init (&argc, &argv, NULL);
134 g_test_add ("/monitor/app", Fixture, NULL, setup, test_app_monitor, teardown);
136 return g_test_run ();