Add some more cases to the app-id unit tests
[glib.git] / gio / tests / monitor.c
blob67be3a4405575fa87856d1244ac9be302078bf42
1 #include <gio/gio.h>
3 typedef struct {
4 GFile *file;
5 GOutputStream *stream;
6 GMainLoop *loop;
7 gint state;
8 } MonitorData;
10 static gboolean
11 create_file_idle (gpointer data)
13 MonitorData *d = data;
14 GError *error = NULL;
16 g_assert (d->state == 0);
18 d->stream = (GOutputStream*)g_file_create (d->file, 0, NULL, &error);
19 g_assert_no_error (error);
21 d->state = 1;
23 return G_SOURCE_REMOVE;
26 static gboolean
27 write_file_idle (gpointer data)
29 MonitorData *d = data;
30 GError *error = NULL;
32 g_assert (d->state == 2);
34 g_output_stream_write (d->stream, "abcd", 4, NULL, &error);
35 g_assert_no_error (error);
36 g_object_unref (d->stream);
37 d->stream = NULL;
39 d->state = 3;
42 return G_SOURCE_REMOVE;
45 static gboolean
46 delete_file_idle (gpointer data)
48 MonitorData *d = data;
49 GError *error = NULL;
51 g_assert (d->state == 4);
53 g_file_delete (d->file, NULL, &error);
54 g_assert_no_error (error);
56 d->state = 5;
58 return G_SOURCE_REMOVE;
61 static void
62 changed_cb (GFileMonitor *monitor,
63 GFile *file,
64 GFile *other_file,
65 GFileMonitorEvent event,
66 gpointer data)
68 MonitorData *d = data;
70 switch (d->state)
72 case 1:
73 g_assert (event == G_FILE_MONITOR_EVENT_CREATED);
74 d->state = 2;
75 g_idle_add (write_file_idle, data);
76 break;
77 case 3:
78 g_assert (event == G_FILE_MONITOR_EVENT_CHANGED ||
79 event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
80 if (event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
82 d->state = 4;
83 g_idle_add (delete_file_idle, data);
85 break;
86 case 5:
87 g_assert (event == G_FILE_MONITOR_EVENT_DELETED);
88 d->state = 6;
89 if (d->loop)
90 g_main_loop_quit (d->loop);
91 break;
92 default:
93 g_assert_not_reached ();
94 break;
98 static void
99 file_changed_cb (GFileMonitor *monitor,
100 GFile *file,
101 GFile *other_file,
102 GFileMonitorEvent event,
103 gpointer data)
105 gint *state = data;
107 switch (*state)
109 case 0:
110 g_assert (event == G_FILE_MONITOR_EVENT_CREATED);
111 *state = 1;
112 break;
113 case 1:
114 g_assert (event == G_FILE_MONITOR_EVENT_CHANGED ||
115 event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
116 if (event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
117 *state = 2;
118 break;
119 case 2:
120 g_assert (event == G_FILE_MONITOR_EVENT_DELETED);
121 *state = 3;
122 break;
123 default:
124 g_assert_not_reached ();
125 break;
129 static void
130 test_directory_monitor (void)
132 gchar *path;
133 GFile *file;
134 GFile *child;
135 GFileMonitor *dir_monitor;
136 GFileMonitor *file_monitor;
137 GError *error = NULL;
138 MonitorData data;
139 gint state;
140 GMainLoop *loop;
142 path = g_mkdtemp (g_strdup ("file_monitor_XXXXXX"));
143 file = g_file_new_for_path (path);
144 dir_monitor = g_file_monitor_directory (file, 0, NULL, &error);
145 g_assert_no_error (error);
147 child = g_file_get_child (file, "test-file");
148 file_monitor = g_file_monitor_file (child, 0, NULL, &error);
149 g_assert_no_error (error);
151 loop = g_main_loop_new (NULL, FALSE);
153 g_signal_connect (dir_monitor, "changed", G_CALLBACK (changed_cb), &data);
154 g_signal_connect (file_monitor, "changed", G_CALLBACK (file_changed_cb), &state);
156 data.loop = loop;
157 data.file = child;
158 data.state = 0;
159 state = 0;
161 g_idle_add (create_file_idle, &data);
163 g_main_loop_run (loop);
165 g_assert_cmpint (data.state, ==, 6);
166 g_assert_cmpint (state, ==, 3);
168 g_main_loop_unref (loop);
169 g_object_unref (dir_monitor);
170 g_object_unref (file_monitor);
171 g_object_unref (child);
172 g_object_unref (file);
173 g_free (path);
177 main (int argc, char *argv[])
179 g_test_init (&argc, &argv, NULL);
181 g_test_add_func ("/monitor/directory", test_directory_monitor);
183 return g_test_run ();