11 create_file_idle (gpointer data
)
13 MonitorData
*d
= data
;
16 g_assert (d
->state
== 0);
18 d
->stream
= (GOutputStream
*)g_file_create (d
->file
, 0, NULL
, &error
);
19 g_assert_no_error (error
);
23 return G_SOURCE_REMOVE
;
27 write_file_idle (gpointer data
)
29 MonitorData
*d
= data
;
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
);
42 return G_SOURCE_REMOVE
;
46 delete_file_idle (gpointer data
)
48 MonitorData
*d
= data
;
51 g_assert (d
->state
== 4);
53 g_file_delete (d
->file
, NULL
, &error
);
54 g_assert_no_error (error
);
58 return G_SOURCE_REMOVE
;
62 changed_cb (GFileMonitor
*monitor
,
65 GFileMonitorEvent event
,
68 MonitorData
*d
= data
;
73 g_assert (event
== G_FILE_MONITOR_EVENT_CREATED
);
75 g_idle_add (write_file_idle
, data
);
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
)
83 g_idle_add (delete_file_idle
, data
);
87 g_assert (event
== G_FILE_MONITOR_EVENT_DELETED
);
90 g_main_loop_quit (d
->loop
);
93 g_assert_not_reached ();
99 file_changed_cb (GFileMonitor
*monitor
,
102 GFileMonitorEvent event
,
110 g_assert (event
== G_FILE_MONITOR_EVENT_CREATED
);
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
)
120 g_assert (event
== G_FILE_MONITOR_EVENT_DELETED
);
124 g_assert_not_reached ();
130 test_directory_monitor (void)
135 GFileMonitor
*dir_monitor
;
136 GFileMonitor
*file_monitor
;
137 GError
*error
= NULL
;
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
);
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
);
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 ();