1 | #include <gio/gio.h> |
2 | #include <gstdio.h> |
3 | |
4 | typedef struct |
5 | { |
6 | gchar *applications_dir; |
7 | } Fixture; |
8 | |
9 | static void |
10 | setup (Fixture *fixture, |
11 | gconstpointer user_data) |
12 | { |
13 | fixture->applications_dir = g_build_filename (first_element: 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 (format: "Using data directory: %s" , g_get_user_data_dir ()); |
17 | } |
18 | |
19 | static void |
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 |
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 (filename: path, contents, length: -1, error: &error); |
40 | g_assert_no_error (error); |
41 | |
42 | return G_SOURCE_REMOVE; |
43 | } |
44 | |
45 | static void |
46 | delete_app (gpointer data) |
47 | { |
48 | const gchar *path = data; |
49 | |
50 | g_remove (filename: path); |
51 | } |
52 | |
53 | static gboolean changed_fired; |
54 | |
55 | static void |
56 | changed_cb (GAppInfoMonitor *monitor, GMainLoop *loop) |
57 | { |
58 | changed_fired = TRUE; |
59 | g_main_loop_quit (loop); |
60 | } |
61 | |
62 | static gboolean |
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 |
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 (first_element: fixture->applications_dir, "app.desktop" , NULL); |
82 | |
83 | /* FIXME: this shouldn't be required */ |
84 | g_list_free_full (list: g_app_info_get_all (), free_func: 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 (function: create_app, data: app_path); |
92 | g_timeout_add_seconds (interval: 3, function: quit_loop, data: 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 (list: g_app_info_get_all (), free_func: g_object_unref); |
100 | |
101 | g_timeout_add_seconds (interval: 3, function: quit_loop, data: loop); |
102 | |
103 | delete_app (data: 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 (filename: app_path); |
111 | |
112 | g_object_unref (object: monitor); |
113 | |
114 | g_free (mem: app_path); |
115 | } |
116 | |
117 | int |
118 | main (int argc, char *argv[]) |
119 | { |
120 | g_test_init (argc: &argc, argv: &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 | |