10 setup (Fixture
*fixture
,
11 gconstpointer user_data
)
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
);
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
);
31 GOutputStream
*stream
;
37 create_file_idle (gpointer data
)
39 MonitorData
*d
= data
;
42 g_assert (d
->state
== 0);
44 d
->stream
= (GOutputStream
*)g_file_create (d
->file
, 0, NULL
, &error
);
45 g_assert_no_error (error
);
49 return G_SOURCE_REMOVE
;
53 write_file_idle (gpointer data
)
55 MonitorData
*d
= data
;
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
);
68 return G_SOURCE_REMOVE
;
72 delete_file_idle (gpointer data
)
74 MonitorData
*d
= data
;
77 g_assert (d
->state
== 4);
79 g_file_delete (d
->file
, NULL
, &error
);
80 g_assert_no_error (error
);
84 return G_SOURCE_REMOVE
;
88 stop_loop_idle (gpointer data
)
90 MonitorData
*d
= data
;
92 g_assert (d
->state
== 6);
95 g_main_loop_quit (d
->loop
);
97 return G_SOURCE_REMOVE
;
101 changed_cb (GFileMonitor
*monitor
,
104 GFileMonitorEvent event
,
107 MonitorData
*d
= data
;
112 g_assert (event
== G_FILE_MONITOR_EVENT_CREATED
);
114 g_idle_add (write_file_idle
, data
);
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
)
122 g_idle_add (delete_file_idle
, data
);
126 g_assert (event
== G_FILE_MONITOR_EVENT_DELETED
);
128 g_idle_add (stop_loop_idle
, data
);
131 g_assert_not_reached ();
137 file_changed_cb (GFileMonitor
*monitor
,
140 GFileMonitorEvent event
,
148 g_assert (event
== G_FILE_MONITOR_EVENT_CREATED
);
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
)
158 g_assert (event
== G_FILE_MONITOR_EVENT_DELETED
);
162 g_assert_not_reached ();
168 test_directory_monitor (Fixture
*fixture
,
169 gconstpointer user_data
)
173 GFileMonitor
*dir_monitor
;
174 GFileMonitor
*file_monitor
;
175 GError
*error
= NULL
;
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
);
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 ();