tracker-0.16.2/tests/libtracker-miner/tracker-crawler-test.c

No issues found

  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 <libtracker-miner/tracker-miner.h>
 21 
 22 typedef struct CrawlerTest CrawlerTest;
 23 
 24 struct CrawlerTest {
 25 	GMainLoop *main_loop;
 26 	guint directories_found;
 27 	guint directories_ignored;
 28 	guint files_found;
 29 	guint files_ignored;
 30 	gboolean interrupted;
 31 
 32 	/* signals statistics */
 33 	guint n_check_directory;
 34 	guint n_check_directory_contents;
 35 	guint n_check_file;
 36 };
 37 
 38 static void
 39 crawler_finished_cb (TrackerCrawler *crawler,
 40                      gboolean        interrupted,
 41                      gpointer        user_data)
 42 {
 43 	CrawlerTest *test = user_data;
 44 
 45 	test->interrupted = interrupted;
 46 
 47 	if (test->main_loop) {
 48 		g_main_loop_quit (test->main_loop);
 49 	}
 50 }
 51 
 52 static void
 53 crawler_directory_crawled_cb (TrackerCrawler *crawler,
 54                               GFile          *directory,
 55                               GNode          *tree,
 56                               guint           directories_found,
 57                               guint           directories_ignored,
 58                               guint           files_found,
 59                               guint           files_ignored,
 60                               gpointer        user_data)
 61 {
 62 	CrawlerTest *test = user_data;
 63 
 64 	test->directories_found = directories_found;
 65 	test->directories_ignored = directories_ignored;
 66 	test->files_found = files_found;
 67 	test->files_ignored = files_ignored;
 68 
 69 	g_assert_cmpint (g_node_n_nodes (tree, G_TRAVERSE_ALL), ==, directories_found + files_found);
 70 }
 71 
 72 static gboolean
 73 crawler_check_directory_cb (TrackerCrawler *crawler,
 74 			    GFile          *file,
 75 			    gpointer        user_data)
 76 {
 77 	CrawlerTest *test = user_data;
 78 
 79 	test->n_check_directory++;
 80 
 81 	return TRUE;
 82 }
 83 
 84 static gboolean
 85 crawler_check_file_cb (TrackerCrawler *crawler,
 86 		       GFile          *file,
 87 		       gpointer        user_data)
 88 {
 89 	CrawlerTest *test = user_data;
 90 
 91 	test->n_check_file++;
 92 
 93 	return TRUE;
 94 }
 95 
 96 static gboolean
 97 crawler_check_directory_contents_cb (TrackerCrawler *crawler,
 98 				     GFile          *file,
 99 				     GList          *contents,
100 				     gpointer        user_data)
101 {
102 	CrawlerTest *test = user_data;
103 
104 	test->n_check_directory_contents++;
105 
106 	return TRUE;
107 }
108 
109 static void
110 test_crawler_crawl (void)
111 {
112 	TrackerCrawler *crawler;
113 	CrawlerTest test = { 0 };
114 	gboolean started;
115 	GFile *file;
116 
117 	test.main_loop = g_main_loop_new (NULL, FALSE);
118 
119 	crawler = tracker_crawler_new ();
120 	g_signal_connect (crawler, "finished",
121 			  G_CALLBACK (crawler_finished_cb), &test);
122 
123 	file = g_file_new_for_path (TEST_DATA_DIR);
124 
125 	started = tracker_crawler_start (crawler, file, TRUE);
126 
127 	g_assert_cmpint (started, ==, 1);
128 
129 	g_main_loop_run (test.main_loop);
130 
131 	g_assert_cmpint (test.interrupted, ==, 0);
132 
133 	g_main_loop_unref (test.main_loop);
134 	g_object_unref (crawler);
135 	g_object_unref (file);
136 }
137 
138 static void
139 test_crawler_crawl_interrupted (void)
140 {
141 	TrackerCrawler *crawler;
142 	CrawlerTest test = { 0 };
143 	gboolean started;
144 	GFile *file;
145 
146 	crawler = tracker_crawler_new ();
147 	g_signal_connect (crawler, "finished",
148 			  G_CALLBACK (crawler_finished_cb), &test);
149 
150 	file = g_file_new_for_path (TEST_DATA_DIR);
151 
152 	started = tracker_crawler_start (crawler, file, TRUE);
153 
154 	g_assert_cmpint (started, ==, 1);
155 
156 	tracker_crawler_stop (crawler);
157 
158 	g_assert_cmpint (test.interrupted, ==, 1);
159 
160 	g_object_unref (crawler);
161 	g_object_unref (file);
162 }
163 
164 static void
165 test_crawler_crawl_nonexisting (void)
166 {
167 	TrackerCrawler *crawler;
168 	GFile *file;
169 	gboolean started;
170 
171 	crawler = tracker_crawler_new ();
172 	file = g_file_new_for_path (TEST_DATA_DIR "-idontexist");
173 
174 	started = tracker_crawler_start (crawler, file, TRUE);
175 
176 	g_assert_cmpint (started, ==, 0);
177 
178 	g_object_unref (crawler);
179 	g_object_unref (file);
180 }
181 
182 static void
183 test_crawler_crawl_recursive (void)
184 {
185 	TrackerCrawler *crawler;
186 	CrawlerTest test = { 0 };
187 	GFile *file;
188 
189 	test.main_loop = g_main_loop_new (NULL, FALSE);
190 
191 	crawler = tracker_crawler_new ();
192 	g_signal_connect (crawler, "finished",
193 			  G_CALLBACK (crawler_finished_cb), &test);
194 	g_signal_connect (crawler, "directory-crawled",
195 			  G_CALLBACK (crawler_directory_crawled_cb), &test);
196 
197 	file = g_file_new_for_path (TEST_DATA_DIR);
198 
199 	tracker_crawler_start (crawler, file, TRUE);
200 
201 	g_main_loop_run (test.main_loop);
202 
203 	/* There are 4 directories and 5 (2 hidden) files */
204 	g_assert_cmpint (test.directories_found, ==, 4);
205 	g_assert_cmpint (test.directories_ignored, ==, 0);
206 	g_assert_cmpint (test.files_found, ==, 5);
207 	g_assert_cmpint (test.files_ignored, ==, 0);
208 
209 	g_main_loop_unref (test.main_loop);
210 	g_object_unref (crawler);
211 	g_object_unref (file);
212 }
213 
214 static void
215 test_crawler_crawl_non_recursive (void)
216 {
217 	TrackerCrawler *crawler;
218 	CrawlerTest test = { 0 };
219 	GFile *file;
220 
221 	test.main_loop = g_main_loop_new (NULL, FALSE);
222 
223 	crawler = tracker_crawler_new ();
224 	g_signal_connect (crawler, "finished",
225 			  G_CALLBACK (crawler_finished_cb), &test);
226 	g_signal_connect (crawler, "directory-crawled",
227 			  G_CALLBACK (crawler_directory_crawled_cb), &test);
228 
229 	file = g_file_new_for_path (TEST_DATA_DIR);
230 
231 	tracker_crawler_start (crawler, file, FALSE);
232 
233 	g_main_loop_run (test.main_loop);
234 
235 	/* There are 3 directories (including parent) and 1 file in toplevel dir */
236 	g_assert_cmpint (test.directories_found, ==, 3);
237 	g_assert_cmpint (test.directories_ignored, ==, 0);
238 	g_assert_cmpint (test.files_found, ==, 1);
239 	g_assert_cmpint (test.files_ignored, ==, 0);
240 
241 	g_main_loop_unref (test.main_loop);
242 	g_object_unref (crawler);
243 	g_object_unref (file);
244 }
245 
246 static void
247 test_crawler_crawl_n_signals (void)
248 {
249 	TrackerCrawler *crawler;
250 	CrawlerTest test = { 0 };
251 	GFile *file;
252 
253 	test.main_loop = g_main_loop_new (NULL, FALSE);
254 
255 	crawler = tracker_crawler_new ();
256 	g_signal_connect (crawler, "finished",
257 			  G_CALLBACK (crawler_finished_cb), &test);
258 	g_signal_connect (crawler, "directory-crawled",
259 			  G_CALLBACK (crawler_directory_crawled_cb), &test);
260 	g_signal_connect (crawler, "check-directory",
261 			  G_CALLBACK (crawler_check_directory_cb), &test);
262 	g_signal_connect (crawler, "check-directory-contents",
263 			  G_CALLBACK (crawler_check_directory_contents_cb), &test);
264 	g_signal_connect (crawler, "check-file",
265 			  G_CALLBACK (crawler_check_file_cb), &test);
266 
267 	file = g_file_new_for_path (TEST_DATA_DIR);
268 
269 	tracker_crawler_start (crawler, file, TRUE);
270 
271 	g_main_loop_run (test.main_loop);
272 
273 	g_assert_cmpint (test.directories_found, ==, test.n_check_directory);
274 	g_assert_cmpint (test.directories_found, ==, test.n_check_directory_contents);
275 	g_assert_cmpint (test.files_found, ==, test.n_check_file);
276 
277 	g_main_loop_unref (test.main_loop);
278 	g_object_unref (crawler);
279 	g_object_unref (file);
280 }
281 
282 static void
283 test_crawler_crawl_n_signals_non_recursive (void)
284 {
285 	TrackerCrawler *crawler;
286 	CrawlerTest test = { 0 };
287 	GFile *file;
288 
289 	test.main_loop = g_main_loop_new (NULL, FALSE);
290 
291 	crawler = tracker_crawler_new ();
292 	g_signal_connect (crawler, "finished",
293 			  G_CALLBACK (crawler_finished_cb), &test);
294 	g_signal_connect (crawler, "directory-crawled",
295 			  G_CALLBACK (crawler_directory_crawled_cb), &test);
296 	g_signal_connect (crawler, "check-directory",
297 			  G_CALLBACK (crawler_check_directory_cb), &test);
298 	g_signal_connect (crawler, "check-directory-contents",
299 			  G_CALLBACK (crawler_check_directory_contents_cb), &test);
300 	g_signal_connect (crawler, "check-file",
301 			  G_CALLBACK (crawler_check_file_cb), &test);
302 
303 	file = g_file_new_for_path (TEST_DATA_DIR);
304 
305 	tracker_crawler_start (crawler, file, FALSE);
306 
307 	g_main_loop_run (test.main_loop);
308 
309 	g_assert_cmpint (test.directories_found, ==, test.n_check_directory);
310 	g_assert_cmpint (1, ==, test.n_check_directory_contents);
311 	g_assert_cmpint (test.files_found, ==, test.n_check_file);
312 
313 	g_main_loop_unref (test.main_loop);
314 	g_object_unref (crawler);
315 	g_object_unref (file);
316 }
317 
318 int
319 main (int    argc,
320       char **argv)
321 {
322 	g_test_init (&argc, &argv, NULL);
323 
324 	g_test_message ("Testing filesystem crawler");
325 
326 	g_test_add_func ("/libtracker-miner/tracker-crawler/crawl",
327 	                 test_crawler_crawl);
328 	g_test_add_func ("/libtracker-miner/tracker-crawler/crawl-interrupted",
329 	                 test_crawler_crawl_interrupted);
330 	g_test_add_func ("/libtracker-miner/tracker-crawler/crawl-nonexisting",
331 	                 test_crawler_crawl_nonexisting);
332 
333 	g_test_add_func ("/libtracker-miner/tracker-crawler/crawl-recursive",
334 	                 test_crawler_crawl_recursive);
335 	g_test_add_func ("/libtracker-miner/tracker-crawler/crawl-non-recursive",
336 	                 test_crawler_crawl_non_recursive);
337 
338 	g_test_add_func ("/libtracker-miner/tracker-crawler/crawl-n-signals",
339 	                 test_crawler_crawl_n_signals);
340 	g_test_add_func ("/libtracker-miner/tracker-crawler/crawl-n-signals-non-recursive",
341 	                 test_crawler_crawl_n_signals_non_recursive);
342 
343 	return g_test_run ();
344 }