1 /*
2 * Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
3 *
4 * Author: Carlos Garnacho <carlos@lanedo.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
20 */
21 #include <string.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24
25 #include <glib.h>
26 #include <glib/gstdio.h>
27
28 #include <libtracker-miner/tracker-miner-enums.h>
29 #include <libtracker-miner/tracker-file-notifier.h>
30
31 typedef struct {
32 gint op;
33 gchar *path;
34 gchar *other_path;
35 } FilesystemOperation;
36
37 /* Fixture struct */
38 typedef struct {
39 GFile *test_file;
40 gchar *test_path;
41
42 TrackerIndexingTree *indexing_tree;
43 GMainLoop *main_loop;
44
45 /* The file notifier to test */
46 TrackerFileNotifier *notifier;
47
48 guint expire_timeout_id;
49 gboolean expect_finished;
50
51 FilesystemOperation *expect_results;
52 guint expect_n_results;
53
54 GList *ops;
55 } TestCommonContext;
56
57 typedef enum {
58 OPERATION_CREATE,
59 OPERATION_UPDATE,
60 OPERATION_DELETE,
61 OPERATION_MOVE
62 } OperationType;
63
64 #if GLIB_MINOR_VERSION < 30
65 gchar *
66 g_mkdtemp (gchar *tmpl)
67 {
68 return mkdtemp (tmpl);
69 }
70 #endif
71
72 #define test_add(path,fun) \
73 g_test_add (path, \
74 TestCommonContext, \
75 NULL, \
76 test_common_context_setup, \
77 fun, \
78 test_common_context_teardown)
79
80 static void
81 filesystem_operation_free (FilesystemOperation *op)
82 {
83 g_free (op->path);
84 g_free (op->other_path);
85 g_free (op);
86 }
87
88 static void
89 perform_file_operation (TestCommonContext *fixture,
90 gchar *command,
91 gchar *filename,
92 gchar *other_filename)
93 {
94 gchar *path, *other_path, *call;
95
96 path = g_build_filename (fixture->test_path, filename, NULL);
97
98 if (other_filename) {
99 other_path = g_build_filename (fixture->test_path, filename, NULL);
100 call = g_strdup_printf ("%s %s %s", command, path, other_path);
101 g_free (other_path);
102 } else {
103 call = g_strdup_printf ("%s %s", command, path);
104 }
105
106 system (call);
ignoring return value of 'system', declared with attribute warn_unused_result
(emitted by gcc)
107
108 g_free (call);
109 g_free (path);
110 }
111
112 #define CREATE_FOLDER(fixture,p) perform_file_operation((fixture),"mkdir",(p),NULL)
113 #define CREATE_UPDATE_FILE(fixture,p) perform_file_operation((fixture),"touch",(p),NULL)
114 #define DELETE_FILE(fixture,p) perform_file_operation((fixture),"rm",(p),NULL)
115 #define DELETE_FOLDER(fixture,p) perform_file_operation((fixture),"rm -rf",(p),NULL)
116
117 static void
118 file_notifier_file_created_cb (TrackerFileNotifier *notifier,
119 GFile *file,
120 gpointer user_data)
121 {
122 TestCommonContext *fixture = user_data;
123 FilesystemOperation *op;
124
125 op = g_new0 (FilesystemOperation, 1);
126 op->op = OPERATION_CREATE;
127 op->path = g_file_get_relative_path (fixture->test_file , file);
128
129 fixture->ops = g_list_prepend (fixture->ops, op);
130
131 if (!fixture->expect_finished &&
132 fixture->expect_n_results == g_list_length (fixture->ops)) {
133 g_main_loop_quit (fixture->main_loop);
134 }
135 }
136
137 static void
138 file_notifier_file_updated_cb (TrackerFileNotifier *notifier,
139 GFile *file,
140 gboolean attributes_only,
141 gpointer user_data)
142 {
143 TestCommonContext *fixture = user_data;
144 FilesystemOperation *op;
145
146 op = g_new0 (FilesystemOperation, 1);
147 op->op = OPERATION_UPDATE;
148 op->path = g_file_get_relative_path (fixture->test_file , file);
149
150 fixture->ops = g_list_prepend (fixture->ops, op);
151
152 if (!fixture->expect_finished &&
153 fixture->expect_n_results == g_list_length (fixture->ops)) {
154 g_main_loop_quit (fixture->main_loop);
155 }
156 }
157
158 static void
159 file_notifier_file_deleted_cb (TrackerFileNotifier *notifier,
160 GFile *file,
161 gpointer user_data)
162 {
163 TestCommonContext *fixture = user_data;
164 FilesystemOperation *op;
165 guint i;
166
167 op = g_new0 (FilesystemOperation, 1);
168 op->op = OPERATION_DELETE;
169 op->path = g_file_get_relative_path (fixture->test_file , file);
170
171 for (i = 0; i < fixture->expect_n_results; i++) {
172 if (fixture->expect_results[i].op == op->op &&
173 g_strcmp0 (fixture->expect_results[i].path, op->path) != 0 &&
174 g_str_has_prefix (op->path, fixture->expect_results[i].path)) {
175 /* Deleted file is the child of a directory
176 * that's expected to be deleted.
177 */
178 filesystem_operation_free (op);
179 return;
180 }
181 }
182
183 fixture->ops = g_list_prepend (fixture->ops, op);
184
185 if (!fixture->expect_finished &&
186 fixture->expect_n_results == g_list_length (fixture->ops)) {
187 g_main_loop_quit (fixture->main_loop);
188 }
189 }
190
191 static void
192 file_notifier_file_moved_cb (TrackerFileNotifier *notifier,
193 GFile *file,
194 GFile *other_file,
195 gpointer user_data)
196 {
197 TestCommonContext *fixture = user_data;
198 FilesystemOperation *op;
199
200 op = g_new0 (FilesystemOperation, 1);
201 op->op = OPERATION_MOVE;
202 op->path = g_file_get_relative_path (fixture->test_file , file);
203 op->other_path = g_file_get_relative_path (fixture->test_file ,
204 other_file);
205
206 fixture->ops = g_list_prepend (fixture->ops, op);
207
208 if (!fixture->expect_finished &&
209 fixture->expect_n_results == g_list_length (fixture->ops)) {
210 g_main_loop_quit (fixture->main_loop);
211 }
212 }
213
214 static void
215 file_notifier_finished_cb (TrackerFileNotifier *notifier,
216 gpointer user_data)
217 {
218 TestCommonContext *fixture = user_data;
219
220 if (fixture->expect_finished) {
221 g_main_loop_quit (fixture->main_loop);
222 };
223 }
224
225 static void
226 test_common_context_index_dir (TestCommonContext *fixture,
227 const gchar *filename,
228 TrackerDirectoryFlags flags)
229 {
230 GFile *file;
231 gchar *path;
232
233 path = g_build_filename (fixture->test_path, filename, NULL);
234 file = g_file_new_for_path (path);
235 g_free (path);
236
237 tracker_indexing_tree_add (fixture->indexing_tree, file, flags);
238 g_object_unref (file);
239 }
240
241 static void
242 test_common_context_remove_dir (TestCommonContext *fixture,
243 const gchar *filename)
244 {
245 GFile *file;
246 gchar *path;
247
248 path = g_build_filename (fixture->test_path, filename, NULL);
249 file = g_file_new_for_path (path);
250 g_free (path);
251
252 tracker_indexing_tree_remove (fixture->indexing_tree, file);
253 g_object_unref (file);
254 }
255
256 static void
257 test_common_context_setup (TestCommonContext *fixture,
258 gconstpointer data)
259 {
260 fixture->test_path = g_build_filename (g_get_tmp_dir (),
261 "tracker-test-XXXXXX",
262 NULL);
263 fixture->test_path = g_mkdtemp (fixture->test_path);
264 fixture->test_file = g_file_new_for_path (fixture->test_path);
265
266 fixture->ops = NULL;
267
268 /* Create basic folders within the test location */
269 CREATE_FOLDER (fixture, "recursive");
270 CREATE_FOLDER (fixture, "non-recursive");
271 CREATE_FOLDER (fixture, "non-indexed");
272
273 fixture->indexing_tree = tracker_indexing_tree_new ();
274 tracker_indexing_tree_set_filter_hidden (fixture->indexing_tree, TRUE);
275
276 fixture->main_loop = g_main_loop_new (NULL, FALSE);
277 fixture->notifier = tracker_file_notifier_new (fixture->indexing_tree);
278
279 g_signal_connect (fixture->notifier, "file-created",
280 G_CALLBACK (file_notifier_file_created_cb), fixture);
281 g_signal_connect (fixture->notifier, "file-updated",
282 G_CALLBACK (file_notifier_file_updated_cb), fixture);
283 g_signal_connect (fixture->notifier, "file-deleted",
284 G_CALLBACK (file_notifier_file_deleted_cb), fixture);
285 g_signal_connect (fixture->notifier, "file-moved",
286 G_CALLBACK (file_notifier_file_moved_cb), fixture);
287 g_signal_connect (fixture->notifier, "finished",
288 G_CALLBACK (file_notifier_finished_cb), fixture);
289 }
290
291 static void
292 test_common_context_teardown (TestCommonContext *fixture,
293 gconstpointer data)
294 {
295 DELETE_FOLDER (fixture, NULL);
296
297 g_list_foreach (fixture->ops, (GFunc) filesystem_operation_free, NULL);
298 g_list_free (fixture->ops);
299
300 if (fixture->notifier) {
301 g_object_unref (fixture->notifier);
302 }
303
304 if (fixture->indexing_tree) {
305 g_object_unref (fixture->indexing_tree);
306 }
307
308 if (fixture->test_file) {
309 g_object_unref (fixture->test_file);
310 }
311
312 if (fixture->test_path) {
313 g_free (fixture->test_path);
314 }
315
316 }
317
318 static gboolean
319 timeout_expired_cb (gpointer user_data)
320 {
321 TestCommonContext *fixture = user_data;
322
323 fixture->expire_timeout_id = 0;
324 g_main_loop_quit (fixture->main_loop);
325
326 return FALSE;
327 }
328
329 static void
330 test_common_context_expect_results (TestCommonContext *fixture,
331 FilesystemOperation *results,
332 guint n_results,
333 guint max_timeout,
334 gboolean expect_finished)
335 {
336 GList *ops;
337 guint i, id;
338
339 fixture->expect_finished = expect_finished;
340 fixture->expect_n_results = n_results;
341 fixture->expect_results = results;
342
343 if (fixture->expect_n_results != g_list_length (fixture->ops)) {
344 if (max_timeout != 0) {
345 id = g_timeout_add_seconds (max_timeout,
346 (GSourceFunc) timeout_expired_cb,
347 fixture);
348 }
349
350 g_main_loop_run (fixture->main_loop);
351
352 if (max_timeout != 0) {
353 g_source_remove (id);
354 }
355 }
356
357 for (i = 0; i < n_results; i++) {
358 gboolean matched = FALSE;
359
360 ops = fixture->ops;
361
362 while (ops) {
363 FilesystemOperation *op = ops->data;
364
365 if (op->op == results[i].op &&
366 g_strcmp0 (op->path, results[i].path) == 0 &&
367 g_strcmp0 (op->other_path, results[i].other_path) == 0) {
368 filesystem_operation_free (op);
369 fixture->ops = g_list_delete_link (fixture->ops, ops);
370 matched = TRUE;
371 break;
372 }
373
374 ops = ops->next;
375 }
376
377 if (!matched) {
378 if (results[i].op == OPERATION_MOVE) {
379 g_critical ("Expected operation %d on %s (-> %s) didn't happen",
380 results[i].op, results[i].path,
381 results[i].other_path);
382 } else {
383 g_critical ("Expected operation %d on %s didn't happen",
384 results[i].op, results[i].path);
385 }
386 }
387 }
388
389 ops = fixture->ops;
390
391 while (ops) {
392 FilesystemOperation *op = ops->data;
393
394 if (op->op == OPERATION_MOVE) {
395 g_critical ("Unexpected operation %d on %s (-> %s) happened",
396 op->op, op->path,
397 op->other_path);
398 } else {
399 g_critical ("Unexpected operation %d on %s happened",
400 op->op, op->path);
401 }
402 }
403
404 g_assert_cmpint (g_list_length (fixture->ops), ==, 0);
405 }
406
407 static void
408 test_file_notifier_crawling_non_recursive (TestCommonContext *fixture,
409 gconstpointer data)
410 {
411 FilesystemOperation expected_results[] = {
412 { OPERATION_CREATE, "non-recursive", NULL },
413 { OPERATION_CREATE, "non-recursive/folder", NULL },
414 { OPERATION_CREATE, "non-recursive/bbb", NULL },
415 };
416
417 CREATE_FOLDER (fixture, "non-recursive/folder");
418 CREATE_UPDATE_FILE (fixture, "non-recursive/folder/aaa");
419 CREATE_UPDATE_FILE (fixture, "non-recursive/bbb");
420
421 test_common_context_index_dir (fixture, "non-recursive",
422 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
423
424 tracker_file_notifier_start (fixture->notifier);
425
426 test_common_context_expect_results (fixture, expected_results,
427 G_N_ELEMENTS (expected_results),
428 2, TRUE);
429
430 tracker_file_notifier_stop (fixture->notifier);
431 }
432
433 static void
434 test_file_notifier_crawling_recursive (TestCommonContext *fixture,
435 gconstpointer data)
436 {
437 FilesystemOperation expected_results[] = {
438 { OPERATION_CREATE, "recursive", NULL },
439 { OPERATION_CREATE, "recursive/folder", NULL },
440 { OPERATION_CREATE, "recursive/folder/aaa", NULL },
441 { OPERATION_CREATE, "recursive/bbb", NULL },
442 };
443
444 CREATE_FOLDER (fixture, "recursive/folder");
445 CREATE_UPDATE_FILE (fixture, "recursive/folder/aaa");
446 CREATE_UPDATE_FILE (fixture, "recursive/bbb");
447
448 test_common_context_index_dir (fixture, "recursive",
449 TRACKER_DIRECTORY_FLAG_RECURSE |
450 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
451
452 tracker_file_notifier_start (fixture->notifier);
453
454 test_common_context_expect_results (fixture, expected_results,
455 G_N_ELEMENTS (expected_results),
456 2, TRUE);
457
458 tracker_file_notifier_stop (fixture->notifier);
459 }
460
461 static void
462 test_file_notifier_crawling_non_recursive_within_recursive (TestCommonContext *fixture,
463 gconstpointer data)
464 {
465 FilesystemOperation expected_results[] = {
466 { OPERATION_CREATE, "recursive", NULL },
467 { OPERATION_CREATE, "recursive/folder", NULL },
468 { OPERATION_CREATE, "recursive/folder/aaa", NULL },
469 { OPERATION_CREATE, "recursive/bbb", NULL },
470 { OPERATION_CREATE, "recursive/folder/non-recursive", NULL },
471 { OPERATION_CREATE, "recursive/folder/non-recursive/ccc", NULL },
472 { OPERATION_CREATE, "recursive/folder/non-recursive/folder", NULL },
473 };
474
475 CREATE_FOLDER (fixture, "recursive/folder");
476 CREATE_UPDATE_FILE (fixture, "recursive/folder/aaa");
477 CREATE_UPDATE_FILE (fixture, "recursive/bbb");
478 CREATE_FOLDER (fixture, "recursive/folder/non-recursive");
479 CREATE_UPDATE_FILE (fixture, "recursive/folder/non-recursive/ccc");
480 CREATE_FOLDER (fixture, "recursive/folder/non-recursive/folder");
481 CREATE_UPDATE_FILE (fixture, "recursive/folder/non-recursive/folder/ddd");
482
483 test_common_context_index_dir (fixture, "recursive",
484 TRACKER_DIRECTORY_FLAG_RECURSE |
485 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
486 test_common_context_index_dir (fixture, "recursive/folder/non-recursive",
487 TRACKER_DIRECTORY_FLAG_NONE |
488 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
489
490 tracker_file_notifier_start (fixture->notifier);
491
492 test_common_context_expect_results (fixture, expected_results,
493 G_N_ELEMENTS (expected_results),
494 2, TRUE);
495
496 tracker_file_notifier_stop (fixture->notifier);
497 }
498
499 static void
500 test_file_notifier_crawling_recursive_within_non_recursive (TestCommonContext *fixture,
501 gconstpointer data)
502 {
503 FilesystemOperation expected_results[] = {
504 { OPERATION_CREATE, "non-recursive", NULL },
505 { OPERATION_CREATE, "non-recursive/folder", NULL },
506 { OPERATION_CREATE, "non-recursive/bbb", NULL },
507 { OPERATION_CREATE, "non-recursive/folder/recursive", NULL },
508 { OPERATION_CREATE, "non-recursive/folder/recursive/ccc", NULL },
509 { OPERATION_CREATE, "non-recursive/folder/recursive/folder", NULL },
510 { OPERATION_CREATE, "non-recursive/folder/recursive/folder/ddd", NULL },
511 };
512
513 CREATE_FOLDER (fixture, "non-recursive/folder");
514 CREATE_UPDATE_FILE (fixture, "non-recursive/folder/aaa");
515 CREATE_UPDATE_FILE (fixture, "non-recursive/bbb");
516 CREATE_FOLDER (fixture, "non-recursive/folder/recursive");
517 CREATE_UPDATE_FILE (fixture, "non-recursive/folder/recursive/ccc");
518 CREATE_FOLDER (fixture, "non-recursive/folder/recursive/folder");
519 CREATE_UPDATE_FILE (fixture, "non-recursive/folder/recursive/folder/ddd");
520
521 test_common_context_index_dir (fixture, "non-recursive/folder/recursive",
522 TRACKER_DIRECTORY_FLAG_RECURSE |
523 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
524 test_common_context_index_dir (fixture, "non-recursive",
525 TRACKER_DIRECTORY_FLAG_NONE |
526 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
527
528 tracker_file_notifier_start (fixture->notifier);
529
530 test_common_context_expect_results (fixture, expected_results,
531 G_N_ELEMENTS (expected_results),
532 2, TRUE);
533
534 tracker_file_notifier_stop (fixture->notifier);
535 }
536
537 static void
538 test_file_notifier_crawling_ignore_within_recursive (TestCommonContext *fixture,
539 gconstpointer data)
540 {
541 FilesystemOperation expected_results[] = {
542 { OPERATION_CREATE, "recursive", NULL },
543 { OPERATION_CREATE, "recursive/folder", NULL },
544 { OPERATION_CREATE, "recursive/folder/aaa", NULL },
545 { OPERATION_CREATE, "recursive/bbb", NULL },
546 { OPERATION_DELETE, "recursive/folder/ignore", NULL }
547 };
548
549 CREATE_FOLDER (fixture, "recursive/folder");
550 CREATE_UPDATE_FILE (fixture, "recursive/folder/aaa");
551 CREATE_UPDATE_FILE (fixture, "recursive/bbb");
552 CREATE_FOLDER (fixture, "recursive/folder/ignore");
553 CREATE_UPDATE_FILE (fixture, "recursive/folder/ignore/ccc");
554 CREATE_FOLDER (fixture, "recursive/folder/ignore/folder");
555 CREATE_UPDATE_FILE (fixture, "recursive/folder/ignore/folder/ddd");
556
557 test_common_context_index_dir (fixture, "recursive",
558 TRACKER_DIRECTORY_FLAG_RECURSE |
559 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
560 test_common_context_index_dir (fixture, "recursive/folder/ignore",
561 TRACKER_DIRECTORY_FLAG_IGNORE |
562 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
563
564 tracker_file_notifier_start (fixture->notifier);
565
566 test_common_context_expect_results (fixture, expected_results,
567 G_N_ELEMENTS (expected_results),
568 2, TRUE);
569
570 tracker_file_notifier_stop (fixture->notifier);
571 }
572
573 static void
574 test_file_notifier_changes_remove_non_recursive (TestCommonContext *fixture,
575 gconstpointer data)
576 {
577 FilesystemOperation expected_results[] = {
578 { OPERATION_DELETE, "non-recursive", NULL }
579 };
580
581 test_file_notifier_crawling_non_recursive (fixture, data);
582
583 test_common_context_remove_dir (fixture, "non-recursive");
584 tracker_file_notifier_start (fixture->notifier);
585 test_common_context_expect_results (fixture, expected_results,
586 G_N_ELEMENTS (expected_results),
587 1, FALSE);
588 tracker_file_notifier_stop (fixture->notifier);
589 }
590
591 static void
592 test_file_notifier_changes_remove_recursive (TestCommonContext *fixture,
593 gconstpointer data)
594 {
595 FilesystemOperation expected_results[] = {
596 { OPERATION_DELETE, "recursive", NULL }
597 };
598
599 test_file_notifier_crawling_recursive (fixture, data);
600
601 test_common_context_remove_dir (fixture, "recursive");
602 tracker_file_notifier_start (fixture->notifier);
603 test_common_context_expect_results (fixture, expected_results,
604 G_N_ELEMENTS (expected_results),
605 1, FALSE);
606 tracker_file_notifier_stop (fixture->notifier);
607 }
608
609 static void
610 test_file_notifier_changes_remove_ignore (TestCommonContext *fixture,
611 gconstpointer data)
612 {
613 FilesystemOperation expected_results[] = {
614 { OPERATION_CREATE, "recursive/folder/ignore", NULL },
615 { OPERATION_CREATE, "recursive/folder/ignore/ccc", NULL },
616 { OPERATION_CREATE, "recursive/folder/ignore/folder", NULL },
617 { OPERATION_CREATE, "recursive/folder/ignore/folder/ddd", NULL }
618 };
619 FilesystemOperation expected_results2[] = {
620 { OPERATION_DELETE, "recursive/folder/ignore", NULL }
621 };
622
623 /* Start off from ignore test case */
624 test_file_notifier_crawling_ignore_within_recursive (fixture, data);
625
626 /* Remove ignored folder */
627 test_common_context_remove_dir (fixture, "recursive/folder/ignore");
628 tracker_file_notifier_start (fixture->notifier);
629 test_common_context_expect_results (fixture, expected_results,
630 G_N_ELEMENTS (expected_results),
631 1, FALSE);
632 tracker_file_notifier_stop (fixture->notifier);
633
634 /* And add it back */
635 fixture->expect_n_results = G_N_ELEMENTS (expected_results2);
636 test_common_context_index_dir (fixture, "recursive/folder/ignore",
637 TRACKER_DIRECTORY_FLAG_IGNORE);
638 tracker_file_notifier_start (fixture->notifier);
639 test_common_context_expect_results (fixture, expected_results2,
640 G_N_ELEMENTS (expected_results2),
641 1, FALSE);
642 tracker_file_notifier_stop (fixture->notifier);
643 }
644
645 static void
646 test_file_notifier_monitor_updates_non_recursive (TestCommonContext *fixture,
647 gconstpointer data)
648 {
649 FilesystemOperation expected_results[] = {
650 { OPERATION_CREATE, "non-recursive", NULL },
651 { OPERATION_CREATE, "non-recursive/folder", NULL },
652 { OPERATION_CREATE, "non-recursive/bbb", NULL }
653 };
654 FilesystemOperation expected_results2[] = {
655 { OPERATION_UPDATE, "non-recursive/bbb", NULL },
656 { OPERATION_CREATE, "non-recursive/ccc", NULL }
657 };
658 FilesystemOperation expected_results3[] = {
659 { OPERATION_DELETE, "non-recursive/folder", NULL },
660 { OPERATION_DELETE, "non-recursive/ccc", NULL }
661 };
662
663 CREATE_FOLDER (fixture, "non-recursive/folder");
664 CREATE_UPDATE_FILE (fixture, "non-recursive/bbb");
665
666 test_common_context_index_dir (fixture, "non-recursive",
667 TRACKER_DIRECTORY_FLAG_MONITOR |
668 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
669
670 tracker_file_notifier_start (fixture->notifier);
671 test_common_context_expect_results (fixture, expected_results,
672 G_N_ELEMENTS (expected_results),
673 2, TRUE);
674 tracker_file_notifier_stop (fixture->notifier);
675
676 /* Perform file updates */
677 tracker_file_notifier_start (fixture->notifier);
678 CREATE_UPDATE_FILE (fixture, "non-recursive/folder/aaa");
679 CREATE_UPDATE_FILE (fixture, "non-recursive/bbb");
680 CREATE_UPDATE_FILE (fixture, "non-recursive/ccc");
681 test_common_context_expect_results (fixture, expected_results2,
682 G_N_ELEMENTS (expected_results2),
683 3, FALSE);
684
685 DELETE_FILE (fixture, "non-recursive/ccc");
686 DELETE_FOLDER (fixture, "non-recursive/folder");
687 test_common_context_expect_results (fixture, expected_results3,
688 G_N_ELEMENTS (expected_results3),
689 3, FALSE);
690 tracker_file_notifier_stop (fixture->notifier);
691 }
692
693 static void
694 test_file_notifier_monitor_updates_recursive (TestCommonContext *fixture,
695 gconstpointer data)
696 {
697 FilesystemOperation expected_results[] = {
698 { OPERATION_CREATE, "recursive", NULL },
699 { OPERATION_CREATE, "recursive/bbb", NULL }
700 };
701 FilesystemOperation expected_results2[] = {
702 { OPERATION_CREATE, "recursive/folder", NULL },
703 { OPERATION_CREATE, "recursive/folder/aaa", NULL },
704 { OPERATION_UPDATE, "recursive/bbb", NULL },
705 };
706 FilesystemOperation expected_results3[] = {
707 { OPERATION_DELETE, "recursive/folder", NULL },
708 { OPERATION_DELETE, "recursive/bbb", NULL }
709 };
710
711 CREATE_UPDATE_FILE (fixture, "recursive/bbb");
712
713 test_common_context_index_dir (fixture, "recursive",
714 TRACKER_DIRECTORY_FLAG_RECURSE |
715 TRACKER_DIRECTORY_FLAG_MONITOR |
716 TRACKER_DIRECTORY_FLAG_CHECK_MTIME);
717
718 tracker_file_notifier_start (fixture->notifier);
719 test_common_context_expect_results (fixture, expected_results,
720 G_N_ELEMENTS (expected_results),
721 2, TRUE);
722 tracker_file_notifier_stop (fixture->notifier);
723
724 /* Perform file updates */
725 tracker_file_notifier_start (fixture->notifier);
726 CREATE_FOLDER (fixture, "recursive/folder");
727 CREATE_UPDATE_FILE (fixture, "recursive/folder/aaa");
728 CREATE_UPDATE_FILE (fixture, "recursive/bbb");
729 test_common_context_expect_results (fixture, expected_results2,
730 G_N_ELEMENTS (expected_results2),
731 5, FALSE);
732
733 DELETE_FILE (fixture, "recursive/bbb");
734 DELETE_FOLDER (fixture, "recursive/folder");
735 test_common_context_expect_results (fixture, expected_results3,
736 G_N_ELEMENTS (expected_results3),
737 5, FALSE);
738 tracker_file_notifier_stop (fixture->notifier);
739 }
740
741 gint
742 main (gint argc,
743 gchar **argv)
744 {
745 g_test_init (&argc, &argv, NULL);
746
747 g_test_message ("Testing file notifier");
748
749 /* Crawling */
750 test_add ("/libtracker-miner/file-notifier/crawling-non-recursive",
751 test_file_notifier_crawling_non_recursive);
752 test_add ("/libtracker-miner/file-notifier/crawling-recursive",
753 test_file_notifier_crawling_recursive);
754 test_add ("/libtracker-miner/file-notifier/crawling-non-recursive-within-recursive",
755 test_file_notifier_crawling_non_recursive_within_recursive);
756 test_add ("/libtracker-miner/file-notifier/crawling-recursive-within-non-recursive",
757 test_file_notifier_crawling_recursive_within_non_recursive);
758 test_add ("/libtracker-miner/file-notifier/crawling-ignore-within-recursive",
759 test_file_notifier_crawling_ignore_within_recursive);
760
761 /* Config changes */
762 test_add ("/libtracker-miner/file-notifier/changes-remove-non-recursive",
763 test_file_notifier_changes_remove_non_recursive);
764 test_add ("/libtracker-miner/file-notifier/changes-remove-recursive",
765 test_file_notifier_changes_remove_recursive);
766 test_add ("/libtracker-miner/file-notifier/changes-remove-ignore",
767 test_file_notifier_changes_remove_ignore);
768
769 /* Monitoring */
770 test_add ("/libtracker-miner/file-notifier/monitor-updates-non-recursive",
771 test_file_notifier_monitor_updates_non_recursive);
772 test_add ("/libtracker-miner/file-notifier/monitor-updates-recursive",
773 test_file_notifier_monitor_updates_recursive);
774
775 return g_test_run ();
776 }