Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / monitor.c
blob4d64fa82668c8afd3091d90dee364ba75cbd4372
1 #include <gio/gio.h>
2 #include <gstdio.h>
4 typedef struct
6 gchar *tmp_dir;
7 } Fixture;
9 static void
10 setup (Fixture *fixture,
11 gconstpointer user_data)
13 GError *error = NULL;
15 fixture->tmp_dir = g_dir_make_tmp ("gio-test-file-monitor_XXXXXX", &error);
16 g_assert_no_error (error);
18 g_test_message ("Using temporary directory: %s", fixture->tmp_dir);
21 static void
22 teardown (Fixture *fixture,
23 gconstpointer user_data)
25 g_assert_cmpint (g_rmdir (fixture->tmp_dir), ==, 0);
26 g_clear_pointer (&fixture->tmp_dir, g_free);
29 typedef struct {
30 GFile *file;
31 GOutputStream *stream;
32 GMainLoop *loop;
33 gint state;
34 } MonitorData;
36 static gboolean
37 create_file_idle (gpointer data)
39 MonitorData *d = data;
40 GError *error = NULL;
42 g_assert (d->state == 0);
44 d->stream = (GOutputStream*)g_file_create (d->file, 0, NULL, &error);
45 g_assert_no_error (error);
47 d->state = 1;
49 return G_SOURCE_REMOVE;
52 static gboolean
53 write_file_idle (gpointer data)
55 MonitorData *d = data;
56 GError *error = NULL;
58 g_assert (d->state == 2);
60 g_output_stream_write (d->stream, "abcd", 4, NULL, &error);
61 g_assert_no_error (error);
62 g_object_unref (d->stream);
63 d->stream = NULL;
65 d->state = 3;
68 return G_SOURCE_REMOVE;
71 static gboolean
72 delete_file_idle (gpointer data)
74 MonitorData *d = data;
75 GError *error = NULL;
77 g_assert (d->state == 4);
79 g_file_delete (d->file, NULL, &error);
80 g_assert_no_error (error);
82 d->state = 5;
84 return G_SOURCE_REMOVE;
87 static gboolean
88 stop_loop_idle (gpointer data)
90 MonitorData *d = data;
92 g_assert (d->state == 6);
94 if (d->loop)
95 g_main_loop_quit (d->loop);
97 return G_SOURCE_REMOVE;
100 static void
101 changed_cb (GFileMonitor *monitor,
102 GFile *file,
103 GFile *other_file,
104 GFileMonitorEvent event,
105 gpointer data)
107 MonitorData *d = data;
109 switch (d->state)
111 case 1:
112 g_assert (event == G_FILE_MONITOR_EVENT_CREATED);
113 d->state = 2;
114 g_idle_add (write_file_idle, data);
115 break;
116 case 3:
117 g_assert (event == G_FILE_MONITOR_EVENT_CHANGED ||
118 event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
119 if (event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
121 d->state = 4;
122 g_idle_add (delete_file_idle, data);
124 break;
125 case 5:
126 g_assert (event == G_FILE_MONITOR_EVENT_DELETED);
127 d->state = 6;
128 g_idle_add (stop_loop_idle, data);
129 break;
130 default:
131 g_assert_not_reached ();
132 break;
136 static void
137 file_changed_cb (GFileMonitor *monitor,
138 GFile *file,
139 GFile *other_file,
140 GFileMonitorEvent event,
141 gpointer data)
143 gint *state = data;
145 switch (*state)
147 case 0:
148 g_assert (event == G_FILE_MONITOR_EVENT_CREATED);
149 *state = 1;
150 break;
151 case 1:
152 g_assert (event == G_FILE_MONITOR_EVENT_CHANGED ||
153 event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
154 if (event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
155 *state = 2;
156 break;
157 case 2:
158 g_assert (event == G_FILE_MONITOR_EVENT_DELETED);
159 *state = 3;
160 break;
161 default:
162 g_assert_not_reached ();
163 break;
167 static void
168 test_directory_monitor (Fixture *fixture,
169 gconstpointer user_data)
171 GFile *file;
172 GFile *child;
173 GFileMonitor *dir_monitor;
174 GFileMonitor *file_monitor;
175 GError *error = NULL;
176 MonitorData data;
177 gint state;
178 GMainLoop *loop;
180 file = g_file_new_for_path (fixture->tmp_dir);
181 dir_monitor = g_file_monitor_directory (file, 0, NULL, &error);
182 g_assert_no_error (error);
184 child = g_file_get_child (file, "test-file");
185 file_monitor = g_file_monitor_file (child, 0, NULL, &error);
186 g_assert_no_error (error);
188 loop = g_main_loop_new (NULL, FALSE);
190 g_signal_connect (dir_monitor, "changed", G_CALLBACK (changed_cb), &data);
191 g_signal_connect (file_monitor, "changed", G_CALLBACK (file_changed_cb), &state);
193 data.loop = loop;
194 data.file = child;
195 data.state = 0;
196 state = 0;
198 g_idle_add (create_file_idle, &data);
200 g_main_loop_run (loop);
202 g_assert_cmpint (data.state, ==, 6);
203 g_assert_cmpint (state, ==, 3);
205 g_main_loop_unref (loop);
206 g_object_unref (dir_monitor);
207 g_object_unref (file_monitor);
208 g_object_unref (child);
209 g_object_unref (file);
213 main (int argc, char *argv[])
215 g_test_init (&argc, &argv, NULL);
217 g_test_add ("/monitor/directory", Fixture, NULL, setup, test_directory_monitor, teardown);
219 return g_test_run ();