1 /*
2  * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 #include <string.h>
21 #include <unistd.h>
22 
23 #include <glib.h>
24 #include <glib/gstdio.h>
25 
26 /* Special case, the monitor header is not normally exported */
27 #include <libtracker-miner/tracker-monitor.h>
28 
29 /* -------------- COMMON FOR ALL FILE EVENT TESTS ----------------- */
30 
31 #define TEST_TIMEOUT 5 /* seconds */
32 
33 typedef enum {
34 	MONITOR_SIGNAL_NONE                   = 0,
35 	MONITOR_SIGNAL_ITEM_CREATED           = 1 << 0,
36 	MONITOR_SIGNAL_ITEM_UPDATED           = 1 << 1,
37 	MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED = 1 << 2,
38 	MONITOR_SIGNAL_ITEM_DELETED           = 1 << 3,
39 	MONITOR_SIGNAL_ITEM_MOVED_FROM        = 1 << 4,
40 	MONITOR_SIGNAL_ITEM_MOVED_TO          = 1 << 5
41 } MonitorSignal;
42 
43 /* Fixture object type */
44 typedef struct {
45 	TrackerMonitor *monitor;
46 	GFile *monitored_directory_file;
47 	gchar *monitored_directory;
48 	gchar *not_monitored_directory;
49 	GHashTable *events;
50 	GMainLoop *main_loop;
51 } TrackerMonitorTestFixture;
52 
53 static void
add_event(GHashTable * events,GFile * file,MonitorSignal new_event)54 add_event (GHashTable    *events,
55            GFile         *file,
56            MonitorSignal  new_event)
57 {
58 	gpointer previous_file;
59 	gpointer previous_mask;
60 
61 	/* Lookup file in HT */
62 	if (g_hash_table_lookup_extended (events,
63 	                                  file,
64 	                                  &previous_file,
65 	                                  &previous_mask)) {
66 		guint mask;
67 
68 		mask = GPOINTER_TO_UINT (previous_mask);
69 		mask |= new_event;
70 		g_hash_table_replace (events,
71 		                      g_object_ref (previous_file),
72 		                      GUINT_TO_POINTER (mask));
73 	}
74 }
75 
76 static void
test_monitor_events_created_cb(TrackerMonitor * monitor,GFile * file,gboolean is_directory,gpointer user_data)77 test_monitor_events_created_cb (TrackerMonitor *monitor,
78                                 GFile          *file,
79                                 gboolean        is_directory,
80                                 gpointer        user_data)
81 {
82 	gchar *path;
83 
84 	g_assert (file != NULL);
85 	path = g_file_get_path (file);
86 	g_assert (path != NULL);
87 
88 	g_debug ("***** '%s' (%s) (CREATED)",
89 	         path,
90 	         is_directory ? "DIR" : "FILE");
91 
92 	g_free (path);
93 
94 	add_event ((GHashTable *) user_data,
95 	           file,
96 	           MONITOR_SIGNAL_ITEM_CREATED);
97 }
98 
99 static void
test_monitor_events_updated_cb(TrackerMonitor * monitor,GFile * file,gboolean is_directory,gpointer user_data)100 test_monitor_events_updated_cb (TrackerMonitor *monitor,
101                                 GFile          *file,
102                                 gboolean        is_directory,
103                                 gpointer        user_data)
104 {
105 	gchar *path;
106 
107 	g_assert (file != NULL);
108 	path = g_file_get_path (file);
109 	g_assert (path != NULL);
110 
111 	g_debug ("***** '%s' (%s) (UPDATED)",
112 	         path,
113 	         is_directory ? "DIR" : "FILE");
114 
115 	g_free (path);
116 
117 	add_event ((GHashTable *) user_data,
118 	           file,
119 	           MONITOR_SIGNAL_ITEM_UPDATED);
120 }
121 
122 static void
test_monitor_events_attribute_updated_cb(TrackerMonitor * monitor,GFile * file,gboolean is_directory,gpointer user_data)123 test_monitor_events_attribute_updated_cb (TrackerMonitor *monitor,
124                                           GFile          *file,
125                                           gboolean        is_directory,
126                                           gpointer        user_data)
127 {
128 	gchar *path;
129 
130 	g_assert (file != NULL);
131 	path = g_file_get_path (file);
132 	g_assert (path != NULL);
133 
134 	g_debug ("***** '%s' (%s) (ATRIBUTE UPDATED)",
135 	         path,
136 	         is_directory ? "DIR" : "FILE");
137 
138 	g_free (path);
139 
140 	add_event ((GHashTable *) user_data,
141 	           file,
142 	           MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED);
143 }
144 
145 static void
test_monitor_events_deleted_cb(TrackerMonitor * monitor,GFile * file,gboolean is_directory,gpointer user_data)146 test_monitor_events_deleted_cb (TrackerMonitor *monitor,
147                                 GFile          *file,
148                                 gboolean        is_directory,
149                                 gpointer        user_data)
150 {
151 	gchar *path;
152 
153 	g_assert (file != NULL);
154 	path = g_file_get_path (file);
155 	g_assert (path != NULL);
156 
157 	g_debug ("***** '%s' (%s) (DELETED)",
158 	         path,
159 	         is_directory ? "DIR" : "FILE");
160 
161 	g_free (path);
162 
163 	add_event ((GHashTable *) user_data,
164 	           file,
165 	           MONITOR_SIGNAL_ITEM_DELETED);
166 }
167 
168 static void
test_monitor_events_moved_cb(TrackerMonitor * monitor,GFile * file,GFile * other_file,gboolean is_directory,gboolean is_source_monitored,gpointer user_data)169 test_monitor_events_moved_cb (TrackerMonitor *monitor,
170                               GFile          *file,
171                               GFile          *other_file,
172                               gboolean        is_directory,
173                               gboolean        is_source_monitored,
174                               gpointer        user_data)
175 {
176 	gchar *path;
177 	gchar *other_path;
178 
179 	g_assert (file != NULL);
180 	path = g_file_get_path (other_file);
181 	other_path = g_file_get_path (other_file);
182 
183 	g_debug ("***** '%s'->'%s' (%s) (MOVED) (source %smonitored)",
184 	         path,
185 	         other_path,
186 	         is_directory ? "DIR" : "FILE",
187 	         is_source_monitored ? "" : "not ");
188 
189 	g_free (other_path);
190 	g_free (path);
191 
192 	/* Add event to the files */
193 	add_event ((GHashTable *) user_data,
194 	           file,
195 	           MONITOR_SIGNAL_ITEM_MOVED_FROM);
196 	add_event ((GHashTable *) user_data,
197 	           other_file,
198 	           MONITOR_SIGNAL_ITEM_MOVED_TO);
199 }
200 
201 static void
test_monitor_common_setup(TrackerMonitorTestFixture * fixture,gconstpointer data)202 test_monitor_common_setup (TrackerMonitorTestFixture *fixture,
203                            gconstpointer              data)
204 {
205 	gchar *basename;
206 
207 	/* Create HT to store received events */
208 	fixture->events = g_hash_table_new_full (g_file_hash,
209 	                                         (GEqualFunc) g_file_equal,
210 	                                         NULL,
211 	                                         NULL);
212 
213 	/* Create and setup the tracker monitor */
214 	fixture->monitor = tracker_monitor_new ();
215 	g_assert (fixture->monitor != NULL);
216 
217 	g_signal_connect (fixture->monitor, "item-created",
218 	                  G_CALLBACK (test_monitor_events_created_cb),
219 	                  fixture->events);
220 	g_signal_connect (fixture->monitor, "item-updated",
221 	                  G_CALLBACK (test_monitor_events_updated_cb),
222 	                  fixture->events);
223 	g_signal_connect (fixture->monitor, "item-attribute-updated",
224 	                  G_CALLBACK (test_monitor_events_attribute_updated_cb),
225 	                  fixture->events);
226 	g_signal_connect (fixture->monitor, "item-deleted",
227 	                  G_CALLBACK (test_monitor_events_deleted_cb),
228 	                  fixture->events);
229 	g_signal_connect (fixture->monitor, "item-moved",
230 	                  G_CALLBACK (test_monitor_events_moved_cb),
231 	                  fixture->events);
232 
233 	/* Initially, set it disabled */
234 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
235 
236 	/* Create a temp directory to monitor in the test */
237 	basename = g_strdup_printf ("monitor-test-%d", getpid ());
238 	fixture->monitored_directory = g_build_path (G_DIR_SEPARATOR_S, g_get_tmp_dir (), basename, NULL);
239 	fixture->monitored_directory_file = g_file_new_for_path (fixture->monitored_directory);
240 	g_assert (fixture->monitored_directory_file != NULL);
241 	g_assert_cmpint (g_file_make_directory_with_parents (fixture->monitored_directory_file, NULL, NULL), ==, TRUE);
242 	g_free (basename);
243 	g_assert_cmpint (tracker_monitor_add (fixture->monitor, fixture->monitored_directory_file), ==, TRUE);
244 	g_assert_cmpint (tracker_monitor_get_count (fixture->monitor), ==, 1);
245 
246 	/* Setup also not-monitored directory */
247 	fixture->not_monitored_directory = g_strdup (g_get_tmp_dir ());
248 
249 	/* Create new main loop */
250 	fixture->main_loop = g_main_loop_new (NULL, FALSE);
251 	g_assert (fixture->main_loop != NULL);
252 }
253 
254 static void
test_monitor_common_teardown(TrackerMonitorTestFixture * fixture,gconstpointer data)255 test_monitor_common_teardown (TrackerMonitorTestFixture *fixture,
256                               gconstpointer              data)
257 {
258 	/* Remove the main loop */
259 	g_main_loop_unref (fixture->main_loop);
260 
261 	/* Cleanup monitor */
262 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, fixture->monitored_directory_file), ==, TRUE);
263 	g_assert_cmpint (tracker_monitor_get_count (fixture->monitor), ==, 0);
264 
265 	/* Destroy monitor */
266 	g_assert (fixture->monitor != NULL);
267 	g_object_unref (fixture->monitor);
268 
269 	/* Remove the HT of events */
270 	g_hash_table_destroy (fixture->events);
271 
272 	/* Remove base test directories */
273 	g_assert (fixture->monitored_directory_file != NULL);
274 	g_assert (fixture->monitored_directory != NULL);
275 	g_assert_cmpint (g_file_delete (fixture->monitored_directory_file, NULL, NULL), ==, TRUE);
276 	g_object_unref (fixture->monitored_directory_file);
277 	g_free (fixture->monitored_directory);
278 
279 	g_assert (fixture->not_monitored_directory != NULL);
280 	g_free (fixture->not_monitored_directory);
281 }
282 
283 static void
create_directory(const gchar * parent,const gchar * directory_name,GFile ** outfile)284 create_directory (const gchar  *parent,
285                   const gchar  *directory_name,
286                   GFile       **outfile)
287 {
288 	GFile *dirfile;
289 	gchar *path;
290 
291 	path = g_build_path (G_DIR_SEPARATOR_S, parent, directory_name, NULL);
292 	dirfile = g_file_new_for_path (path);
293 	g_assert (dirfile != NULL);
294 	g_assert_cmpint (g_file_make_directory_with_parents (dirfile, NULL, NULL), ==, TRUE);
295 	if (outfile) {
296 		*outfile = dirfile;
297 	} else {
298 		g_object_unref (dirfile);
299 	}
300 	g_free (path);
301 }
302 
303 static void
set_file_contents(const gchar * directory,const gchar * filename,const gchar * contents,GFile ** outfile)304 set_file_contents (const gchar  *directory,
305                    const gchar  *filename,
306                    const gchar  *contents,
307                    GFile       **outfile)
308 {
309 	FILE *file;
310 	size_t length;
311 	gchar *file_path;
312 
313 	g_assert (directory != NULL);
314 	g_assert (filename != NULL);
315 	g_assert (contents != NULL);
316 
317 	file_path = g_build_filename (directory, filename, NULL);
318 
319 	file = g_fopen (file_path, "wb");
320 	g_assert (file != NULL);
321 	length = strlen (contents);
322 	g_assert_cmpint (fwrite (contents, 1, length, file), >=, length);
323 	g_assert_cmpint (fflush (file), ==, 0);
324 	g_assert_cmpint (fclose (file), !=, EOF);
325 
326 	if (outfile) {
327 		*outfile = g_file_new_for_path (file_path);
328 	}
329 	g_free (file_path);
330 }
331 
332 static void
set_file_permissions(const gchar * directory,const gchar * filename,gint permissions)333 set_file_permissions (const gchar  *directory,
334                       const gchar  *filename,
335                       gint          permissions)
336 {
337 	gchar *file_path;
338 
339 	g_assert (directory != NULL);
340 	g_assert (filename != NULL);
341 
342 	file_path = g_build_filename (directory, filename, NULL);
343 	g_assert_cmpint (g_chmod (file_path, permissions), ==, 0);
344 	g_free (file_path);
345 }
346 
347 static void
print_file_events_cb(gpointer key,gpointer value,gpointer user_data)348 print_file_events_cb (gpointer key,
349                       gpointer value,
350                       gpointer user_data)
351 {
352 	GFile *file;
353 	guint events;
354 	gchar *uri;
355 
356 	file = key;
357 	events = GPOINTER_TO_UINT (value);
358 	uri = g_file_get_uri (file);
359 
360 	g_print ("Signals received for '%s': \n"
361 	         "   CREATED:           %s\n"
362 	         "   UPDATED:           %s\n"
363 	         "   ATTRIBUTE UPDATED: %s\n"
364 	         "   DELETED:           %s\n"
365 	         "   MOVED_FROM:        %s\n"
366 	         "   MOVED_TO:          %s\n",
367 	         uri,
368 	         events & MONITOR_SIGNAL_ITEM_CREATED ? "yes" : "no",
369 	         events & MONITOR_SIGNAL_ITEM_UPDATED ? "yes" : "no",
370 	         events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED ? "yes" : "no",
371 	         events & MONITOR_SIGNAL_ITEM_DELETED ? "yes" : "no",
372 	         events & MONITOR_SIGNAL_ITEM_MOVED_FROM ? "yes" : "no",
373 	         events & MONITOR_SIGNAL_ITEM_MOVED_TO ? "yes" : "no");
374 
375 	g_free (uri);
376 }
377 
378 static gboolean
timeout_cb(gpointer data)379 timeout_cb (gpointer data)
380 {
381 	g_main_loop_quit ((GMainLoop *) data);
382 	return FALSE;
383 }
384 
385 static void
events_wait(TrackerMonitorTestFixture * fixture)386 events_wait (TrackerMonitorTestFixture *fixture)
387 {
388 	/* Setup timeout to stop the main loop after some seconds */
389 	g_timeout_add_seconds (TEST_TIMEOUT, timeout_cb, fixture->main_loop);
390 	g_debug ("Waiting %u seconds for monitor events...", TEST_TIMEOUT);
391 	g_main_loop_run (fixture->main_loop);
392 
393 	/* Print signals received for each file */
394 	g_hash_table_foreach (fixture->events, print_file_events_cb, NULL);
395 }
396 
397 /* ----------------------------- FILE EVENT TESTS --------------------------------- */
398 
399 static void
test_monitor_file_event_created(TrackerMonitorTestFixture * fixture,gconstpointer data)400 test_monitor_file_event_created (TrackerMonitorTestFixture *fixture,
401                                  gconstpointer              data)
402 {
403 	GFile *test_file;
404 	guint file_events;
405 
406 	/* Set up environment */
407 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
408 
409 	/* Create file to test with */
410 	set_file_contents (fixture->monitored_directory, "created.txt", "foo", &test_file);
411 	g_assert (test_file != NULL);
412 	g_hash_table_insert (fixture->events,
413 	                     g_object_ref (test_file),
414 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
415 
416 	/* Wait for events */
417 	events_wait (fixture);
418 
419 	/* Get events in the file */
420 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, test_file));
421 
422 	/* Fail if we didn't get the CREATE signal */
423 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), >, 0);
424 
425 	/* Fail if we got a MOVE or DELETE signal */
426 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
427 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
428 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
429 	/* We may get an UPDATE due to the way the file is created */
430 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
431 
432 	/* Cleanup environment */
433 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
434 
435 	/* Remove the test file */
436 	g_assert_cmpint (g_file_delete (test_file, NULL, NULL), ==, TRUE);
437 	g_object_unref (test_file);
438 }
439 
440 static void
test_monitor_file_event_updated(TrackerMonitorTestFixture * fixture,gconstpointer data)441 test_monitor_file_event_updated (TrackerMonitorTestFixture *fixture,
442                                  gconstpointer              data)
443 {
444 	GFile *test_file;
445 	guint file_events;
446 
447 	/* Create file to test with, before setting up environment */
448 	set_file_contents (fixture->monitored_directory, "created.txt", "foo", NULL);
449 
450 	/* Set up environment */
451 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
452 
453 	/* Now, trigger update of the already created file */
454 	set_file_contents (fixture->monitored_directory, "created.txt", "barrrr", &test_file);
455 	g_assert (test_file != NULL);
456 	g_hash_table_insert (fixture->events,
457 	                     g_object_ref (test_file),
458 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
459 
460 	/* Wait for events */
461 	events_wait (fixture);
462 
463 	/* Get events in the file */
464 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, test_file));
465 
466 	/* Fail if we didn't get the UPDATE signal */
467 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), >, 0);
468 
469 	/* Fail if we got a CREATE, MOVE or DELETE signal */
470 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
471 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
472 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
473 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
474 
475 	/* Cleanup environment */
476 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
477 
478 	/* Remove the test file */
479 	g_assert_cmpint (g_file_delete (test_file, NULL, NULL), ==, TRUE);
480 	g_object_unref (test_file);
481 }
482 
483 static void
test_monitor_file_event_attribute_updated(TrackerMonitorTestFixture * fixture,gconstpointer data)484 test_monitor_file_event_attribute_updated (TrackerMonitorTestFixture *fixture,
485                                            gconstpointer              data)
486 {
487 	GFile *test_file;
488 	guint file_events;
489 
490 	/* Create file to test with, before setting up environment */
491 	set_file_contents (fixture->monitored_directory, "created.txt", "foo", &test_file);
492 	g_assert (test_file != NULL);
493 
494 	/* Set up environment */
495 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
496 
497 	/* Now, trigger attribute update of the already created file */
498 	set_file_permissions (fixture->monitored_directory,
499 	                      "created.txt",
500 	                      S_IRWXU);
501 
502 	g_hash_table_insert (fixture->events,
503 	                     g_object_ref (test_file),
504 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
505 
506 	/* Wait for events */
507 	events_wait (fixture);
508 
509 	/* Get events in the file */
510 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, test_file));
511 
512 	/* Fail if we didn't get the ATTRIBUTE UPDATE signal */
513 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), >, 0);
514 
515 	/* Fail if we got a UPDATE, CREATE, MOVE or DELETE signal */
516 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
517 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
518 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
519 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
520 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
521 
522 	/* Cleanup environment */
523 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
524 
525 	/* Remove the test file */
526 	g_assert_cmpint (g_file_delete (test_file, NULL, NULL), ==, TRUE);
527 	g_object_unref (test_file);
528 }
529 
530 static void
test_monitor_file_event_deleted(TrackerMonitorTestFixture * fixture,gconstpointer data)531 test_monitor_file_event_deleted (TrackerMonitorTestFixture *fixture,
532                                  gconstpointer              data)
533 {
534 	GFile *test_file;
535 	guint file_events;
536 
537 	/* Create file to test with, before setting up environment */
538 	set_file_contents (fixture->monitored_directory, "created.txt", "foo", &test_file);
539 	g_assert (test_file != NULL);
540 
541 	/* Set up environment */
542 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
543 
544 	/* Now, remove file */
545 	g_assert_cmpint (g_file_delete (test_file, NULL, NULL), ==, TRUE);
546 	g_hash_table_insert (fixture->events,
547 	                     g_object_ref (test_file),
548 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
549 
550 	/* Wait for events */
551 	events_wait (fixture);
552 
553 	/* Get events in the file */
554 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, test_file));
555 
556 	/* Fail if we didn't get the DELETED signal */
557 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), >, 0);
558 
559 	/* Fail if we got a CREATE, UDPATE or MOVE signal */
560 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
561 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
562 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
563 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
564 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
565 
566 	/* Cleanup environment */
567 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
568 	g_object_unref (test_file);
569 }
570 
571 static void
test_monitor_file_event_moved_to_monitored(TrackerMonitorTestFixture * fixture,gconstpointer data)572 test_monitor_file_event_moved_to_monitored (TrackerMonitorTestFixture *fixture,
573                                             gconstpointer              data)
574 {
575 	GFile *source_file;
576 	gchar *source_path;
577 	GFile *dest_file;
578 	gchar *dest_path;
579 	guint file_events;
580 
581 	/* Create file to test with, before setting up environment */
582 	set_file_contents (fixture->monitored_directory, "created.txt", "foo", &source_file);
583 	g_assert (source_file != NULL);
584 
585 	/* Set up environment */
586 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
587 
588 	/* Now, rename the file */
589 	source_path = g_file_get_path (source_file);
590 	dest_path = g_build_filename (fixture->monitored_directory, "renamed.txt", NULL);
591 	dest_file = g_file_new_for_path (dest_path);
592 	g_assert (dest_file != NULL);
593 
594 	g_assert_cmpint (g_rename (source_path, dest_path), ==, 0);
595 
596 	g_hash_table_insert (fixture->events,
597 	                     g_object_ref (source_file),
598 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
599 	g_hash_table_insert (fixture->events,
600 	                     g_object_ref (dest_file),
601 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
602 
603 	/* Wait for events */
604 	events_wait (fixture);
605 
606 	/* Get events in the source file */
607 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_file));
608 	/* Fail if we didn't get the MOVED_FROM signal */
609 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), >, 0);
610 	/* Fail if we got a CREATE, UPDATE, DELETE or MOVE_TO signal */
611 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
612 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
613 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
614 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
615 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
616 
617 	/* Get events in the dest file */
618 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, dest_file));
619 	/* Fail if we didn't get the MOVED_TO signal */
620 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), >, 0);
621 	/* Fail if we got a CREATE, DELETE or MOVE_FROM signal (UPDATE may actually be possible) */
622 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
623 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
624 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
625 
626 	/* Cleanup environment */
627 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
628 	g_assert_cmpint (g_file_delete (dest_file, NULL, NULL), ==, TRUE);
629 	g_object_unref (source_file);
630 	g_object_unref (dest_file);
631 	g_free (source_path);
632 	g_free (dest_path);
633 }
634 
635 static void
test_monitor_file_event_moved_to_not_monitored(TrackerMonitorTestFixture * fixture,gconstpointer data)636 test_monitor_file_event_moved_to_not_monitored (TrackerMonitorTestFixture *fixture,
637                                                 gconstpointer              data)
638 {
639 	GFile *source_file;
640 	gchar *source_path;
641 	GFile *dest_file;
642 	gchar *dest_path;
643 	guint file_events;
644 
645 	/* Create file to test with, before setting up environment */
646 	set_file_contents (fixture->monitored_directory, "created.txt", "foo", &source_file);
647 	g_assert (source_file != NULL);
648 
649 	/* Set up environment */
650 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
651 
652 	/* Now, rename the file */
653 	source_path = g_file_get_path (source_file);
654 	dest_path = g_build_filename (fixture->not_monitored_directory, "out.txt", NULL);
655 	dest_file = g_file_new_for_path (dest_path);
656 	g_assert (dest_file != NULL);
657 
658 	g_assert_cmpint (g_rename (source_path, dest_path), ==, 0);
659 
660 	g_hash_table_insert (fixture->events,
661 	                     g_object_ref (source_file),
662 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
663 	g_hash_table_insert (fixture->events,
664 	                     g_object_ref (dest_file),
665 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
666 
667 	/* Wait for events */
668 	events_wait (fixture);
669 
670 	/* Get events in the source file */
671 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_file));
672 	/* Fail if we didn't get the DELETED signal */
673 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), >, 0);
674 	/* Fail if we got a CREATE, UPDATE or MOVE signal */
675 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
676 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
677 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
678 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
679 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
680 
681 	/* Get events in the dest file */
682 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, dest_file));
683 	/* Fail if we got a CREATE, UPDATE, DELETE or MOVE signal */
684 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
685 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
686 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
687 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
688 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
689 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
690 
691 	/* Cleanup environment */
692 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
693 	g_assert_cmpint (g_file_delete (dest_file, NULL, NULL), ==, TRUE);
694 	g_object_unref (source_file);
695 	g_object_unref (dest_file);
696 	g_free (source_path);
697 	g_free (dest_path);
698 }
699 
700 static void
test_monitor_file_event_moved_from_not_monitored(TrackerMonitorTestFixture * fixture,gconstpointer data)701 test_monitor_file_event_moved_from_not_monitored (TrackerMonitorTestFixture *fixture,
702                                                   gconstpointer              data)
703 {
704 	GFile *source_file;
705 	gchar *source_path;
706 	GFile *dest_file;
707 	gchar *dest_path;
708 	guint file_events;
709 
710 	/* Create file to test with, before setting up environment */
711 	set_file_contents (fixture->not_monitored_directory, "created.txt", "foo", &source_file);
712 	g_assert (source_file != NULL);
713 
714 	/* Set up environment */
715 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
716 
717 	/* Now, rename the file */
718 	source_path = g_file_get_path (source_file);
719 	dest_path = g_build_filename (fixture->monitored_directory, "in.txt", NULL);
720 	dest_file = g_file_new_for_path (dest_path);
721 	g_assert (dest_file != NULL);
722 
723 	g_assert_cmpint (g_rename (source_path, dest_path), ==, 0);
724 
725 	g_hash_table_insert (fixture->events,
726 	                     g_object_ref (source_file),
727 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
728 	g_hash_table_insert (fixture->events,
729 	                     g_object_ref (dest_file),
730 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
731 
732 	/* Wait for events */
733 	events_wait (fixture);
734 
735 	/* Get events in the source file */
736 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_file));
737 	/* Fail if we got a CREATE, UPDATE, DELETE or MOVE signal */
738 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
739 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
740 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
741 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
742 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
743 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
744 
745 	/* Get events in the dest file */
746 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, dest_file));
747 	/* Fail if we didn't get the CREATED signal */
748 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), >, 0);
749 	/* Fail if we got a DELETE, UPDATE or MOVE signal */
750 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
751 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
752 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
753 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
754 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
755 
756 	/* Cleanup environment */
757 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
758 	g_assert_cmpint (g_file_delete (dest_file, NULL, NULL), ==, TRUE);
759 	g_object_unref (source_file);
760 	g_object_unref (dest_file);
761 	g_free (source_path);
762 	g_free (dest_path);
763 }
764 
765 /* ----------------------------- DIRECTORY EVENT TESTS --------------------------------- */
766 
767 static void
test_monitor_directory_event_created(TrackerMonitorTestFixture * fixture,gconstpointer data)768 test_monitor_directory_event_created (TrackerMonitorTestFixture *fixture,
769                                       gconstpointer              data)
770 {
771 	GFile *test_dir;
772 	guint file_events;
773 
774 	/* Set up environment */
775 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
776 
777 	/* Create directory to test with */
778 	create_directory (fixture->monitored_directory, "foo", &test_dir);
779 	g_assert (test_dir != NULL);
780 	g_hash_table_insert (fixture->events,
781 	                     g_object_ref (test_dir),
782 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
783 
784 	/* Wait for events */
785 	events_wait (fixture);
786 
787 	/* Get events in the file */
788 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, test_dir));
789 
790 	/* Fail if we didn't get the CREATE signal */
791 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), >, 0);
792 
793 	/* Fail if we got a MOVE or DELETE signal (update may actually happen) */
794 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
795 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
796 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
797 
798 	/* Cleanup environment */
799 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
800 
801 	/* Remove the test dir */
802 	g_assert_cmpint (g_file_delete (test_dir, NULL, NULL), ==, TRUE);
803 	g_object_unref (test_dir);
804 }
805 
806 static void
test_monitor_directory_event_deleted(TrackerMonitorTestFixture * fixture,gconstpointer data)807 test_monitor_directory_event_deleted (TrackerMonitorTestFixture *fixture,
808 				      gconstpointer              data)
809 {
810 	GFile *source_dir;
811 	gchar *source_path;
812 	guint file_events;
813 
814 	/* Create directory to test with in a monitored place,
815 	 * before setting up the environment */
816 	create_directory (fixture->monitored_directory, "foo", &source_dir);
817 	source_path = g_file_get_path (source_dir);
818 	g_assert (source_dir != NULL);
819 
820 	/* Set to monitor the new dir also */
821 	g_assert_cmpint (tracker_monitor_add (fixture->monitor, source_dir), ==, TRUE);
822 
823 	/* Set up environment */
824 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
825 
826 	/* Now, delete the directory  */
827 	g_assert_cmpint (g_file_delete (source_dir, NULL, NULL), ==, TRUE);
828 
829 	g_hash_table_insert (fixture->events,
830 	                     g_object_ref (source_dir),
831 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
832 
833 	/* Wait for events */
834 	events_wait (fixture);
835 
836 	/* Get events in the source dir */
837 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_dir));
838 	/* Fail if we didn't get DELETED signal */
839 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), >, 0);
840 	/* Fail if we got a CREATEd, UPDATED, MOVED_FROM or MOVED_TO signal */
841 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
842 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
843 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
844 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
845 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
846 
847 	/* Cleanup environment */
848 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
849 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, source_dir), ==, TRUE);
850 	g_object_unref (source_dir);
851 	g_free (source_path);
852 }
853 
854 
855 static void
test_monitor_directory_event_moved_to_monitored(TrackerMonitorTestFixture * fixture,gconstpointer data)856 test_monitor_directory_event_moved_to_monitored (TrackerMonitorTestFixture *fixture,
857                                                  gconstpointer              data)
858 {
859 	GFile *source_dir;
860 	gchar *source_path;
861 	GFile *dest_dir;
862 	gchar *dest_path;
863 	GFile *file_in_source_dir;
864 	GFile *file_in_dest_dir;
865 	gchar *file_in_dest_dir_path;
866 	guint file_events;
867 
868 	/* Create directory to test with, before setting up the environment */
869 	create_directory (fixture->monitored_directory, "foo", &source_dir);
870 	source_path = g_file_get_path (source_dir);
871 	g_assert (source_dir != NULL);
872 
873 	/* Add some file to the new dir */
874 	set_file_contents (source_path, "lalala.txt", "whatever", &file_in_source_dir);
875 
876 	/* Set up environment */
877 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
878 
879 	/* Set to monitor the new dir also */
880 	g_assert_cmpint (tracker_monitor_add (fixture->monitor, source_dir), ==, TRUE);
881 
882 	/* Get final path of the file */
883 	file_in_dest_dir_path = g_build_path (G_DIR_SEPARATOR_S,
884 	                                      fixture->monitored_directory,
885 	                                      "renamed",
886 	                                      "lalala.txt",
887 	                                      NULL);
888 	file_in_dest_dir = g_file_new_for_path (file_in_dest_dir_path);
889 	g_assert (file_in_dest_dir != NULL);
890 
891 	/* Now, rename the directory */
892 	dest_dir = g_file_get_parent (file_in_dest_dir);
893 	dest_path = g_file_get_path (dest_dir);
894 
895 	g_assert_cmpint (g_rename (source_path, dest_path), ==, 0);
896 
897 	g_hash_table_insert (fixture->events,
898 	                     g_object_ref (source_dir),
899 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
900 	g_hash_table_insert (fixture->events,
901 	                     g_object_ref (dest_dir),
902 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
903 	g_hash_table_insert (fixture->events,
904 	                     g_object_ref (file_in_dest_dir),
905 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
906 	g_hash_table_insert (fixture->events,
907 	                     g_object_ref (file_in_source_dir),
908 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
909 
910 	/* Wait for events */
911 	events_wait (fixture);
912 
913 	/* Get events in the source dir */
914 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_dir));
915 	/* Fail if we didn't get the MOVED_FROM signal */
916 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), >, 0);
917 	/* Fail if we got a CREATE, UPDATE, DELETE or MOVE_TO signal */
918 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
919 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
920 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
921 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
922 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
923 
924 	/* Get events in the dest file */
925 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, dest_dir));
926 	/* Fail if we didn't get the MOVED_TO signal */
927 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), >, 0);
928 	/* Fail if we got a CREATE, DELETE or MOVE_FROM signal (UPDATE may actually be possible) */
929 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
930 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
931 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
932 
933 	/* Get events in the file in source dir */
934 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, file_in_source_dir));
935 	/* Fail if we got ANY signal */
936 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
937 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
938 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
939 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
940 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
941 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
942 
943 	/* Get events in the file in dest dir */
944 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, file_in_dest_dir));
945 	/* Fail if we got ANY signal */
946 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
947 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
948 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
949 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
950 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
951 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
952 
953 
954 	/* Cleanup environment */
955 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
956 	/* Since tracker 0.9.33, monitors are NOT moved to the new location directly when the
957 	 * directory is moved, that is done by the upper layers.
958 	 * Note that monitor is now in dest_dir */
959 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, source_dir), ==, TRUE);
960 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, dest_dir), !=, TRUE);
961 	g_assert_cmpint (g_file_delete (file_in_dest_dir, NULL, NULL), ==, TRUE);
962 	g_assert_cmpint (g_file_delete (dest_dir, NULL, NULL), ==, TRUE);
963 	g_object_unref (source_dir);
964 	g_object_unref (file_in_source_dir);
965 	g_object_unref (dest_dir);
966 	g_object_unref (file_in_dest_dir);
967 	g_free (source_path);
968 	g_free (file_in_dest_dir_path);
969 	g_free (dest_path);
970 }
971 
972 /* Same test as before, BUT, creating a new file in the directory while it's being monitored.
973  * In this case, GIO dumps an extra DELETE event after the MOVE
974  */
975 static void
test_monitor_directory_event_moved_to_monitored_after_file_create(TrackerMonitorTestFixture * fixture,gconstpointer data)976 test_monitor_directory_event_moved_to_monitored_after_file_create (TrackerMonitorTestFixture *fixture,
977                                                                    gconstpointer              data)
978 {
979 	GFile *source_dir;
980 	gchar *source_path;
981 	GFile *dest_dir;
982 	gchar *dest_path;
983 	GFile *file_in_source_dir;
984 	GFile *file_in_dest_dir;
985 	gchar *file_in_dest_dir_path;
986 	guint file_events;
987 
988 	/* Create directory to test with, before setting up the environment */
989 	create_directory (fixture->monitored_directory, "foo", &source_dir);
990 	source_path = g_file_get_path (source_dir);
991 	g_assert (source_dir != NULL);
992 
993 	/* Set up environment */
994 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
995 
996 	/* Set to monitor the new dir also */
997 	g_assert_cmpint (tracker_monitor_add (fixture->monitor, source_dir), ==, TRUE);
998 
999 	/* Add some file to the new dir, WHILE ALREADY MONITORING */
1000 	set_file_contents (source_path, "lalala.txt", "whatever", &file_in_source_dir);
1001 
1002 	/* Ignore the events thus far */
1003 	events_wait (fixture);
1004 	g_hash_table_remove_all (fixture->events);
1005 
1006 	/* Get final path of the file */
1007 	file_in_dest_dir_path = g_build_path (G_DIR_SEPARATOR_S,
1008 	                                      fixture->monitored_directory,
1009 	                                      "renamed",
1010 	                                      "lalala.txt",
1011 	                                      NULL);
1012 	file_in_dest_dir = g_file_new_for_path (file_in_dest_dir_path);
1013 	g_assert (file_in_dest_dir != NULL);
1014 
1015 	/* Now, rename the directory */
1016 	dest_dir = g_file_get_parent (file_in_dest_dir);
1017 	dest_path = g_file_get_path (dest_dir);
1018 
1019 	g_assert_cmpint (g_rename (source_path, dest_path), ==, 0);
1020 
1021 	g_hash_table_insert (fixture->events,
1022 	                     g_object_ref (source_dir),
1023 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1024 	g_hash_table_insert (fixture->events,
1025 	                     g_object_ref (dest_dir),
1026 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1027 	g_hash_table_insert (fixture->events,
1028 	                     g_object_ref (file_in_dest_dir),
1029 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1030 	g_hash_table_insert (fixture->events,
1031 	                     g_object_ref (file_in_source_dir),
1032 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1033 
1034 	/* Wait for events */
1035 	events_wait (fixture);
1036 
1037 	/* Get events in the source dir */
1038 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_dir));
1039 	/* Fail if we didn't get the MOVED_FROM signal */
1040 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), >, 0);
1041 	/* Fail if we got a CREATE, UPDATE, DELETE or MOVE_TO signal */
1042 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1043 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
1044 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1045 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1046 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1047 
1048 	/* Get events in the dest file */
1049 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, dest_dir));
1050 	/* Fail if we didn't get the MOVED_TO signal */
1051 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), >, 0);
1052 	/* Fail if we got a CREATE, DELETE or MOVE_FROM signal (UPDATE may actually be possible) */
1053 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1054 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1055 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1056 
1057 	/* Get events in the file in source dir */
1058 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, file_in_source_dir));
1059 	/* Fail if we got ANY signal != CREATED/UPDATED (we created the file while monitoring) */
1060 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1061 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1062 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1063 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1064 
1065 	/* Get events in the file in dest dir */
1066 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, file_in_dest_dir));
1067 	/* Fail if we got ANY signal */
1068 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1069 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
1070 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1071 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1072 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1073 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1074 
1075 
1076 	/* Cleanup environment */
1077 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
1078 	/* Since tracker 0.9.33, monitors are NOT moved to the new location directly when the
1079 	 * directory is moved, that is done by the upper layers.
1080 	 * Note that monitor is now in dest_dir */
1081 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, source_dir), ==, TRUE);
1082 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, dest_dir), !=, TRUE);
1083 	g_assert_cmpint (g_file_delete (file_in_dest_dir, NULL, NULL), ==, TRUE);
1084 	g_assert_cmpint (g_file_delete (dest_dir, NULL, NULL), ==, TRUE);
1085 	g_object_unref (source_dir);
1086 	g_object_unref (file_in_source_dir);
1087 	g_object_unref (dest_dir);
1088 	g_object_unref (file_in_dest_dir);
1089 	g_free (source_path);
1090 	g_free (file_in_dest_dir_path);
1091 	g_free (dest_path);
1092 }
1093 
1094 /* Same test as before, BUT, updating an existing file in the directory while it's being monitored.
1095  * In this case, GIO dumps an extra DELETE event after the MOVE
1096  */
1097 static void
test_monitor_directory_event_moved_to_monitored_after_file_update(TrackerMonitorTestFixture * fixture,gconstpointer data)1098 test_monitor_directory_event_moved_to_monitored_after_file_update (TrackerMonitorTestFixture *fixture,
1099                                                                    gconstpointer              data)
1100 {
1101 	GFile *source_dir;
1102 	gchar *source_path;
1103 	GFile *dest_dir;
1104 	gchar *dest_path;
1105 	GFile *file_in_source_dir;
1106 	GFile *file_in_dest_dir;
1107 	gchar *file_in_dest_dir_path;
1108 	guint file_events;
1109 
1110 	/* Create directory to test with, before setting up the environment */
1111 	create_directory (fixture->monitored_directory, "foo", &source_dir);
1112 	source_path = g_file_get_path (source_dir);
1113 	g_assert (source_dir != NULL);
1114 
1115 	/* Add some file to the new dir */
1116 	set_file_contents (source_path, "lalala.txt", "whatever", &file_in_source_dir);
1117 
1118 	/* Set up environment */
1119 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
1120 
1121 	/* Set to monitor the new dir also */
1122 	g_assert_cmpint (tracker_monitor_add (fixture->monitor, source_dir), ==, TRUE);
1123 
1124 	/* Ignore the events thus far */
1125 	events_wait (fixture);
1126 	g_hash_table_remove_all (fixture->events);
1127 
1128 	/* Get final path of the file */
1129 	file_in_dest_dir_path = g_build_path (G_DIR_SEPARATOR_S,
1130 	                                      fixture->monitored_directory,
1131 	                                      "renamed",
1132 	                                      "lalala.txt",
1133 	                                      NULL);
1134 	file_in_dest_dir = g_file_new_for_path (file_in_dest_dir_path);
1135 	g_assert (file_in_dest_dir != NULL);
1136 
1137 	/* Update file contents */
1138 	set_file_contents (source_path, "lalala.txt", "hohoho", &file_in_source_dir);
1139 
1140 	/* Now, rename the directory */
1141 	dest_dir = g_file_get_parent (file_in_dest_dir);
1142 	dest_path = g_file_get_path (dest_dir);
1143 
1144 	g_assert_cmpint (g_rename (source_path, dest_path), ==, 0);
1145 
1146 	g_hash_table_insert (fixture->events,
1147 	                     g_object_ref (source_dir),
1148 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1149 	g_hash_table_insert (fixture->events,
1150 	                     g_object_ref (dest_dir),
1151 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1152 	g_hash_table_insert (fixture->events,
1153 	                     g_object_ref (file_in_dest_dir),
1154 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1155 	g_hash_table_insert (fixture->events,
1156 	                     g_object_ref (file_in_source_dir),
1157 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1158 
1159 	/* Wait for events */
1160 	events_wait (fixture);
1161 
1162 	/* Get events in the source dir */
1163 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_dir));
1164 	/* Fail if we didn't get the MOVED_FROM signal */
1165 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), >, 0);
1166 	/* Fail if we got a CREATE, UPDATE, DELETE or MOVE_TO signal */
1167 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1168 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
1169 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1170 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1171 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1172 
1173 	/* Get events in the dest file */
1174 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, dest_dir));
1175 	/* Fail if we didn't get the MOVED_TO signal */
1176 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), >, 0);
1177 	/* Fail if we got a CREATE, DELETE or MOVE_FROM signal (UPDATE may actually be possible) */
1178 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1179 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1180 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1181 
1182 	/* Get events in the file in source dir */
1183 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, file_in_source_dir));
1184 	/* Fail if we didn't get the UPDATE signal */
1185 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), >=, 0);
1186 	/* Fail if we got ANY signal */
1187 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1188 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1189 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1190 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1191 
1192 	/* Get events in the file in dest dir */
1193 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, file_in_dest_dir));
1194 	/* Fail if we got ANY signal */
1195 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1196 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
1197 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1198 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1199 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1200 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1201 
1202 
1203 	/* Cleanup environment */
1204 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
1205 	/* Since tracker 0.9.33, monitors are NOT moved to the new location directly when the
1206 	 * directory is moved, that is done by the upper layers.
1207 	 * Note that monitor is now in dest_dir */
1208 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, source_dir), ==, TRUE);
1209 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, dest_dir), !=, TRUE);
1210 	g_assert_cmpint (g_file_delete (file_in_dest_dir, NULL, NULL), ==, TRUE);
1211 	g_assert_cmpint (g_file_delete (dest_dir, NULL, NULL), ==, TRUE);
1212 	g_object_unref (source_dir);
1213 	g_object_unref (file_in_source_dir);
1214 	g_object_unref (dest_dir);
1215 	g_object_unref (file_in_dest_dir);
1216 	g_free (source_path);
1217 	g_free (file_in_dest_dir_path);
1218 	g_free (dest_path);
1219 }
1220 
1221 static void
test_monitor_directory_event_moved_to_not_monitored(TrackerMonitorTestFixture * fixture,gconstpointer data)1222 test_monitor_directory_event_moved_to_not_monitored (TrackerMonitorTestFixture *fixture,
1223 						     gconstpointer              data)
1224 {
1225 	GFile *source_dir;
1226 	gchar *source_path;
1227 	GFile *dest_dir;
1228 	gchar *dest_path;
1229 	guint file_events;
1230 
1231 	/* Create directory to test with, before setting up the environment */
1232 	create_directory (fixture->monitored_directory, "foo", &source_dir);
1233 	source_path = g_file_get_path (source_dir);
1234 	g_assert (source_dir != NULL);
1235 
1236 	/* Set to monitor the new dir also */
1237 	g_assert_cmpint (tracker_monitor_add (fixture->monitor, source_dir), ==, TRUE);
1238 
1239 	/* Set up environment */
1240 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
1241 
1242 	/* Now, rename the directory */
1243 	dest_path = g_build_path (G_DIR_SEPARATOR_S, fixture->not_monitored_directory, "foo", NULL);
1244 	dest_dir = g_file_new_for_path (dest_path);
1245 
1246 	g_assert_cmpint (g_rename (source_path, dest_path), ==, 0);
1247 
1248 	g_hash_table_insert (fixture->events,
1249 	                     g_object_ref (source_dir),
1250 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1251 	g_hash_table_insert (fixture->events,
1252 	                     g_object_ref (dest_dir),
1253 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1254 
1255 	/* Wait for events */
1256 	events_wait (fixture);
1257 
1258 	/* Get events in the source dir */
1259 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_dir));
1260 	/* Fail if we didn't get the DELETED signal */
1261 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), >, 0);
1262 	/* Fail if we got a CREATE, UPDATE, MOVE_FROM or MOVE_TO signal */
1263 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1264 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
1265 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1266 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1267 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1268 
1269 	/* Get events in the dest file */
1270 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, dest_dir));
1271 	/* Fail if we got any signal */
1272 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1273 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
1274 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1275 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1276 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1277 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1278 
1279 	/* Cleanup environment */
1280 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
1281 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, source_dir), ==, TRUE);
1282 	/* Note that monitor is NOT in dest_dir, so FAIL if we could remove it */
1283 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, dest_dir), !=, TRUE);
1284 	g_assert_cmpint (g_file_delete (dest_dir, NULL, NULL), ==, TRUE);
1285 	g_object_unref (source_dir);
1286 	g_object_unref (dest_dir);
1287 	g_free (source_path);
1288 	g_free (dest_path);
1289 }
1290 
1291 static void
test_monitor_directory_event_moved_from_not_monitored(TrackerMonitorTestFixture * fixture,gconstpointer data)1292 test_monitor_directory_event_moved_from_not_monitored (TrackerMonitorTestFixture *fixture,
1293 						       gconstpointer              data)
1294 {
1295 	GFile *source_dir;
1296 	gchar *source_path;
1297 	GFile *dest_dir;
1298 	gchar *dest_path;
1299 	guint file_events;
1300 
1301 	/* Create directory to test with in a not-monitored place,
1302 	 * before setting up the environment */
1303 	create_directory (fixture->not_monitored_directory, "foo", &source_dir);
1304 	source_path = g_file_get_path (source_dir);
1305 	g_assert (source_dir != NULL);
1306 
1307 	/* Set up environment */
1308 	tracker_monitor_set_enabled (fixture->monitor, TRUE);
1309 
1310 	/* Now, rename the directory to somewhere monitored */
1311 	dest_path = g_build_path (G_DIR_SEPARATOR_S, fixture->monitored_directory, "foo", NULL);
1312 	dest_dir = g_file_new_for_path (dest_path);
1313 
1314 	g_assert_cmpint (g_rename (source_path, dest_path), ==, 0);
1315 
1316 	g_hash_table_insert (fixture->events,
1317 	                     g_object_ref (source_dir),
1318 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1319 	g_hash_table_insert (fixture->events,
1320 	                     g_object_ref (dest_dir),
1321 	                     GUINT_TO_POINTER (MONITOR_SIGNAL_NONE));
1322 
1323 	/* Wait for events */
1324 	events_wait (fixture);
1325 
1326 	/* Get events in the source dir */
1327 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, source_dir));
1328 	/* Fail if we got any signal */
1329 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), ==, 0);
1330 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
1331 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1332 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1333 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1334 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1335 
1336 	/* Get events in the dest file */
1337 	file_events = GPOINTER_TO_UINT (g_hash_table_lookup (fixture->events, dest_dir));
1338 	/* Fail if we didn't get the CREATED signal */
1339 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_CREATED), >, 0);
1340 	/* Fail if we got a CREATE, UPDATE, MOVE_FROM or MOVE_TO signal */
1341 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_UPDATED), ==, 0);
1342 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_ATTRIBUTE_UPDATED), ==, 0);
1343 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_FROM), ==, 0);
1344 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_MOVED_TO), ==, 0);
1345 	g_assert_cmpuint ((file_events & MONITOR_SIGNAL_ITEM_DELETED), ==, 0);
1346 
1347 	/* Cleanup environment */
1348 	tracker_monitor_set_enabled (fixture->monitor, FALSE);
1349 	/* Note that monitor is now in dest_dir, BUT TrackerMonitor should
1350 	 * NOT add automatically a new monitor, so FAIL if we can remove it */
1351 	g_assert_cmpint (tracker_monitor_remove (fixture->monitor, dest_dir), !=, TRUE);
1352 	g_assert_cmpint (g_file_delete (dest_dir, NULL, NULL), ==, TRUE);
1353 	g_object_unref (source_dir);
1354 	g_object_unref (dest_dir);
1355 	g_free (source_path);
1356 	g_free (dest_path);
1357 }
1358 
1359 /* ----------------------------- BASIC API TESTS --------------------------------- */
1360 
1361 static void
test_monitor_basic(void)1362 test_monitor_basic (void)
1363 {
1364 	TrackerMonitor *monitor;
1365 	gchar *basename;
1366 	gchar *path_for_monitor;
1367 	GFile *file_for_monitor;
1368 	GFile *file_for_tmp;
1369 
1370 	/* Setup directories */
1371 	basename = g_strdup_printf ("monitor-test-%d", getpid ());
1372 	path_for_monitor = g_build_path (G_DIR_SEPARATOR_S, g_get_tmp_dir (), basename, NULL);
1373 	g_free (basename);
1374 	g_assert_cmpint (g_mkdir_with_parents (path_for_monitor, 00755), ==, 0);
1375 
1376 	file_for_monitor = g_file_new_for_path (path_for_monitor);
1377 	g_assert (G_IS_FILE (file_for_monitor));
1378 
1379 	file_for_tmp = g_file_new_for_path (g_get_tmp_dir ());
1380 	g_assert (G_IS_FILE (file_for_tmp));
1381 
1382 	/* Create a monitor */
1383 	monitor = tracker_monitor_new ();
1384 	g_assert (monitor != NULL);
1385 
1386 	/* Test general API with monitors enabled */
1387 	tracker_monitor_set_enabled (monitor, TRUE);
1388 	g_assert_cmpint (tracker_monitor_get_enabled (monitor), ==, TRUE);
1389 
1390 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
1391 	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE);
1392 	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE); /* Test double add on purpose */
1393 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 1);
1394 	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, TRUE);
1395 	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, TRUE);
1396 	g_assert_cmpint (tracker_monitor_remove (monitor, file_for_monitor), ==, TRUE);
1397 	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
1398 	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
1399 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
1400 
1401 	tracker_monitor_add (monitor, file_for_monitor);
1402 	tracker_monitor_add (monitor, file_for_tmp);
1403 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 2);
1404 	g_assert_cmpint (tracker_monitor_remove_recursively (monitor, file_for_tmp), ==, TRUE);
1405 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
1406 
1407 	/* Test general API with monitors disabled */
1408 	tracker_monitor_set_enabled (monitor, FALSE);
1409 	g_assert_cmpint (tracker_monitor_get_enabled (monitor), ==, FALSE);
1410 
1411 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
1412 	g_assert_cmpint (tracker_monitor_add (monitor, file_for_monitor), ==, TRUE);
1413 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 1);
1414 	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
1415 	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
1416 	g_assert_cmpint (tracker_monitor_remove (monitor, file_for_monitor), ==, TRUE);
1417 	g_assert_cmpint (tracker_monitor_is_watched (monitor, file_for_monitor), ==, FALSE);
1418 	g_assert_cmpint (tracker_monitor_is_watched_by_string (monitor, path_for_monitor), ==, FALSE);
1419 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
1420 
1421 	tracker_monitor_add (monitor, file_for_monitor);
1422 	tracker_monitor_add (monitor, file_for_tmp);
1423 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 2);
1424 	g_assert_cmpint (tracker_monitor_remove_recursively (monitor, file_for_tmp), ==, TRUE);
1425 	g_assert_cmpint (tracker_monitor_get_count (monitor), ==, 0);
1426 
1427 	/* Cleanup */
1428 	g_assert_cmpint (g_rmdir (path_for_monitor), ==, 0);
1429 	g_assert (file_for_tmp != NULL);
1430 	g_object_unref (file_for_tmp);
1431 	g_assert (file_for_monitor != NULL);
1432 	g_object_unref (file_for_monitor);
1433 	g_assert (path_for_monitor != NULL);
1434 	g_free (path_for_monitor);
1435 	g_assert (monitor != NULL);
1436 	g_object_unref (monitor);
1437 }
1438 
1439 gint
main(gint argc,gchar ** argv)1440 main (gint    argc,
1441       gchar **argv)
1442 {
1443 	g_test_init (&argc, &argv, NULL);
1444 
1445 	g_test_message ("Testing filesystem monitor");
1446 
1447 	/* Basic API tests */
1448 	g_test_add_func ("/libtracker-miner/tracker-monitor/basic",
1449 	                 test_monitor_basic);
1450 
1451 	/* File Event tests */
1452 	g_test_add ("/libtracker-miner/tracker-monitor/file-event/created",
1453 	            TrackerMonitorTestFixture,
1454 	            NULL,
1455 	            test_monitor_common_setup,
1456 	            test_monitor_file_event_created,
1457 	            test_monitor_common_teardown);
1458 	g_test_add ("/libtracker-miner/tracker-monitor/file-event/updated",
1459 	            TrackerMonitorTestFixture,
1460 	            NULL,
1461 	            test_monitor_common_setup,
1462 	            test_monitor_file_event_updated,
1463 	            test_monitor_common_teardown);
1464 	g_test_add ("/libtracker-miner/tracker-monitor/file-event/attribute-updated",
1465 	            TrackerMonitorTestFixture,
1466 	            NULL,
1467 	            test_monitor_common_setup,
1468 	            test_monitor_file_event_attribute_updated,
1469 	            test_monitor_common_teardown);
1470 	g_test_add ("/libtracker-miner/tracker-monitor/file-event/deleted",
1471 	            TrackerMonitorTestFixture,
1472 	            NULL,
1473 	            test_monitor_common_setup,
1474 	            test_monitor_file_event_deleted,
1475 	            test_monitor_common_teardown);
1476 	g_test_add ("/libtracker-miner/tracker-monitor/file-event/moved/to-monitored",
1477 	            TrackerMonitorTestFixture,
1478 	            NULL,
1479 	            test_monitor_common_setup,
1480 	            test_monitor_file_event_moved_to_monitored,
1481 	            test_monitor_common_teardown);
1482 	g_test_add ("/libtracker-miner/tracker-monitor/file-event/moved/to-not-monitored",
1483 	            TrackerMonitorTestFixture,
1484 	            NULL,
1485 	            test_monitor_common_setup,
1486 	            test_monitor_file_event_moved_to_not_monitored,
1487 	            test_monitor_common_teardown);
1488 	g_test_add ("/libtracker-miner/tracker-monitor/file-event/moved/from-not-monitored",
1489 	            TrackerMonitorTestFixture,
1490 	            NULL,
1491 	            test_monitor_common_setup,
1492 	            test_monitor_file_event_moved_from_not_monitored,
1493 	            test_monitor_common_teardown);
1494 
1495 	/* Directory Event tests */
1496 	g_test_add ("/libtracker-miner/tracker-monitor/directory-event/created",
1497 	            TrackerMonitorTestFixture,
1498 	            NULL,
1499 	            test_monitor_common_setup,
1500 	            test_monitor_directory_event_created,
1501 	            test_monitor_common_teardown);
1502 	g_test_add ("/libtracker-miner/tracker-monitor/directory-event/deleted",
1503 	            TrackerMonitorTestFixture,
1504 	            NULL,
1505 	            test_monitor_common_setup,
1506 	            test_monitor_directory_event_deleted,
1507 	            test_monitor_common_teardown);
1508 	g_test_add ("/libtracker-miner/tracker-monitor/directory-event/moved/to-monitored",
1509 	            TrackerMonitorTestFixture,
1510 	            NULL,
1511 	            test_monitor_common_setup,
1512 	            test_monitor_directory_event_moved_to_monitored,
1513 	            test_monitor_common_teardown);
1514 	g_test_add ("/libtracker-miner/tracker-monitor/directory-event/moved/to-monitored-after-file-create",
1515 	            TrackerMonitorTestFixture,
1516 	            NULL,
1517 	            test_monitor_common_setup,
1518 	            test_monitor_directory_event_moved_to_monitored_after_file_create,
1519 	            test_monitor_common_teardown);
1520 	g_test_add ("/libtracker-miner/tracker-monitor/directory-event/moved/to-monitored-after-file-update",
1521 	            TrackerMonitorTestFixture,
1522 	            NULL,
1523 	            test_monitor_common_setup,
1524 	            test_monitor_directory_event_moved_to_monitored_after_file_update,
1525 	            test_monitor_common_teardown);
1526 	g_test_add ("/libtracker-miner/tracker-monitor/directory-event/moved/to-not-monitored",
1527 	            TrackerMonitorTestFixture,
1528 	            NULL,
1529 	            test_monitor_common_setup,
1530 		    test_monitor_directory_event_moved_to_not_monitored,
1531 	            test_monitor_common_teardown);
1532 	g_test_add ("/libtracker-miner/tracker-monitor/directory-event/moved/from-not-monitored",
1533 	            TrackerMonitorTestFixture,
1534 	            NULL,
1535 	            test_monitor_common_setup,
1536 		    test_monitor_directory_event_moved_from_not_monitored,
1537 	            test_monitor_common_teardown);
1538 
1539 	return g_test_run ();
1540 }
1541