1 #include <gio/gio.h>
2 #include <gstdio.h>
3
4 typedef struct
5 {
6 gchar *applications_dir;
7 } Fixture;
8
9 static void
setup(Fixture * fixture,gconstpointer user_data)10 setup (Fixture *fixture,
11 gconstpointer user_data)
12 {
13 fixture->applications_dir = g_build_filename (g_get_user_data_dir (), "applications", NULL);
14 g_assert_no_errno (g_mkdir_with_parents (fixture->applications_dir, 0755));
15
16 g_test_message ("Using data directory: %s", g_get_user_data_dir ());
17 }
18
19 static void
teardown(Fixture * fixture,gconstpointer user_data)20 teardown (Fixture *fixture,
21 gconstpointer user_data)
22 {
23 g_assert_no_errno (g_rmdir (fixture->applications_dir));
24 g_clear_pointer (&fixture->applications_dir, g_free);
25 }
26
27 static gboolean
create_app(gpointer data)28 create_app (gpointer data)
29 {
30 const gchar *path = data;
31 GError *error = NULL;
32 const gchar *contents =
33 "[Desktop Entry]\n"
34 "Name=Application\n"
35 "Version=1.0\n"
36 "Type=Application\n"
37 "Exec=true\n";
38
39 g_file_set_contents (path, contents, -1, &error);
40 g_assert_no_error (error);
41
42 return G_SOURCE_REMOVE;
43 }
44
45 static void
delete_app(gpointer data)46 delete_app (gpointer data)
47 {
48 const gchar *path = data;
49
50 g_remove (path);
51 }
52
53 static gboolean changed_fired;
54
55 static void
changed_cb(GAppInfoMonitor * monitor,GMainLoop * loop)56 changed_cb (GAppInfoMonitor *monitor, GMainLoop *loop)
57 {
58 changed_fired = TRUE;
59 g_main_loop_quit (loop);
60 }
61
62 static gboolean
quit_loop(gpointer data)63 quit_loop (gpointer data)
64 {
65 GMainLoop *loop = data;
66
67 if (g_main_loop_is_running (loop))
68 g_main_loop_quit (loop);
69
70 return G_SOURCE_REMOVE;
71 }
72
73 static void
test_app_monitor(Fixture * fixture,gconstpointer user_data)74 test_app_monitor (Fixture *fixture,
75 gconstpointer user_data)
76 {
77 gchar *app_path;
78 GAppInfoMonitor *monitor;
79 GMainLoop *loop;
80
81 app_path = g_build_filename (fixture->applications_dir, "app.desktop", NULL);
82
83 /* FIXME: this shouldn't be required */
84 g_list_free_full (g_app_info_get_all (), g_object_unref);
85
86 monitor = g_app_info_monitor_get ();
87 loop = g_main_loop_new (NULL, FALSE);
88
89 g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), loop);
90
91 g_idle_add (create_app, app_path);
92 g_timeout_add_seconds (3, quit_loop, loop);
93
94 g_main_loop_run (loop);
95 g_assert (changed_fired);
96 changed_fired = FALSE;
97
98 /* FIXME: this shouldn't be required */
99 g_list_free_full (g_app_info_get_all (), g_object_unref);
100
101 g_timeout_add_seconds (3, quit_loop, loop);
102
103 delete_app (app_path);
104
105 g_main_loop_run (loop);
106
107 g_assert (changed_fired);
108
109 g_main_loop_unref (loop);
110 g_remove (app_path);
111
112 g_object_unref (monitor);
113
114 g_free (app_path);
115 }
116
117 int
main(int argc,char * argv[])118 main (int argc, char *argv[])
119 {
120 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
121
122 g_test_add ("/monitor/app", Fixture, NULL, setup, test_app_monitor, teardown);
123
124 return g_test_run ();
125 }
126