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 changed_cb (GFileMonitor
*monitor
,
91 GFileMonitorEvent event
,
94 MonitorData
*d
= data
;
99 g_assert (event
== G_FILE_MONITOR_EVENT_CREATED
);
101 g_idle_add (write_file_idle
, data
);
104 g_assert (event
== G_FILE_MONITOR_EVENT_CHANGED
||
105 event
== G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
);
106 if (event
== G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
)
109 g_idle_add (delete_file_idle
, data
);
113 g_assert (event
== G_FILE_MONITOR_EVENT_DELETED
);
116 g_main_loop_quit (d
->loop
);
119 g_assert_not_reached ();
125 file_changed_cb (GFileMonitor
*monitor
,
128 GFileMonitorEvent event
,
136 g_assert (event
== G_FILE_MONITOR_EVENT_CREATED
);
140 g_assert (event
== G_FILE_MONITOR_EVENT_CHANGED
||
141 event
== G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
);
142 if (event
== G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT
)
146 g_assert (event
== G_FILE_MONITOR_EVENT_DELETED
);
150 g_assert_not_reached ();
156 test_directory_monitor (Fixture
*fixture
,
157 gconstpointer user_data
)
161 GFileMonitor
*dir_monitor
;
162 GFileMonitor
*file_monitor
;
163 GError
*error
= NULL
;
168 file
= g_file_new_for_path (fixture
->tmp_dir
);
169 dir_monitor
= g_file_monitor_directory (file
, 0, NULL
, &error
);
170 g_assert_no_error (error
);
172 child
= g_file_get_child (file
, "test-file");
173 file_monitor
= g_file_monitor_file (child
, 0, NULL
, &error
);
174 g_assert_no_error (error
);
176 loop
= g_main_loop_new (NULL
, FALSE
);
178 g_signal_connect (dir_monitor
, "changed", G_CALLBACK (changed_cb
), &data
);
179 g_signal_connect (file_monitor
, "changed", G_CALLBACK (file_changed_cb
), &state
);
186 g_idle_add (create_file_idle
, &data
);
188 g_main_loop_run (loop
);
190 g_assert_cmpint (data
.state
, ==, 6);
191 g_assert_cmpint (state
, ==, 3);
193 g_main_loop_unref (loop
);
194 g_object_unref (dir_monitor
);
195 g_object_unref (file_monitor
);
196 g_object_unref (child
);
197 g_object_unref (file
);
201 main (int argc
, char *argv
[])
203 g_test_init (&argc
, &argv
, NULL
);
205 g_test_add ("/monitor/directory", Fixture
, NULL
, setup
, test_directory_monitor
, teardown
);
207 return g_test_run ();