udapted vi.po
[rhythmbox.git] / tests / test-rhythmdb-property-model.c
blob181768a5c02bead020c8f1e3b97d6c4e241b0e08
2 #include <check.h>
3 #include <gtk/gtk.h>
4 #include "test-utils.h"
5 #include "rhythmdb-query-model.h"
6 #include "rhythmdb-property-model.h"
8 #include "rb-debug.h"
9 #include "rb-file-helpers.h"
10 #include "rb-util.h"
12 static int
13 _get_property_count (RhythmDBPropertyModel *model, const char *artist)
15 GtkTreeIter iter;
16 int count;
18 if (rhythmdb_property_model_iter_from_string (model, artist, &iter) == FALSE) {
19 return 0;
22 gtk_tree_model_get (GTK_TREE_MODEL (model), &iter,
23 RHYTHMDB_PROPERTY_MODEL_COLUMN_NUMBER, &count, -1);
24 return count;
27 /* tests property models attached to static query models */
28 START_TEST (test_rhythmdb_property_model_static)
30 RhythmDBQueryModel *model;
31 RhythmDBQueryModel *model2;
32 RhythmDBPropertyModel *propmodel;
33 RhythmDBEntry *a, *b;
34 GtkTreeIter iter;
36 start_test_case ();
38 /* setup */
39 model = rhythmdb_query_model_new_empty (db);
40 g_object_set (model, "show-hidden", FALSE, NULL);
41 propmodel = rhythmdb_property_model_new (db, RHYTHMDB_PROP_ARTIST);
42 g_object_set (propmodel, "query-model", model, NULL);
44 /* create test entries */
45 a = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///a.ogg");
46 b = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///b.ogg");
47 rhythmdb_commit (db);
49 /* set artist values */
50 set_entry_string (db, a, RHYTHMDB_PROP_ARTIST, "x");
51 set_entry_string (db, b, RHYTHMDB_PROP_ARTIST, "y");
52 rhythmdb_commit (db);
54 end_step ();
56 /* add to model */
57 set_waiting_signal (G_OBJECT (propmodel), "row-inserted");
58 rhythmdb_query_model_add_entry (model, a, -1);
59 wait_for_signal ();
60 set_waiting_signal (G_OBJECT (propmodel), "row-inserted");
61 rhythmdb_query_model_add_entry (model, b, -1);
62 wait_for_signal ();
63 fail_unless (rhythmdb_query_model_entry_to_iter (model, a, &iter));
64 fail_unless (rhythmdb_query_model_entry_to_iter (model, b, &iter));
65 /*fail_unless (_get_property_count (propmodel, _("All")) == 2);*/
66 fail_unless (_get_property_count (propmodel, "x") == 1);
67 fail_unless (_get_property_count (propmodel, "y") == 1);
69 end_step ();
71 /* change one */
72 set_waiting_signal (G_OBJECT (propmodel), "row-deleted");
73 set_entry_string (db, a, RHYTHMDB_PROP_ARTIST, "y");
74 rhythmdb_commit (db);
75 wait_for_signal ();
76 fail_unless (_get_property_count (propmodel, "x") == 0);
77 fail_unless (_get_property_count (propmodel, "y") == 2);
79 end_step ();
81 /* hide it */
82 set_entry_hidden (db, a, TRUE);
83 rhythmdb_commit (db);
84 /*fail_unless (_get_property_count (propmodel, _("All")) == 1);*/
85 fail_unless (_get_property_count (propmodel, "x") == 0);
86 fail_unless (_get_property_count (propmodel, "y") == 1);
88 end_step ();
90 /* change back */
91 set_entry_string (db, a, RHYTHMDB_PROP_ARTIST, "x");
92 rhythmdb_commit (db);
93 fail_unless (_get_property_count (propmodel, "x") == 0);
94 fail_unless (_get_property_count (propmodel, "y") == 1);
96 end_step ();
98 /* unhide */
99 set_waiting_signal (G_OBJECT (propmodel), "row-inserted");
100 set_entry_hidden (db, a, FALSE);
101 rhythmdb_commit (db);
102 wait_for_signal ();
104 /*fail_unless (_get_property_count (propmodel, _("All")) == 2);*/
105 fail_unless (_get_property_count (propmodel, "x") == 1);
106 fail_unless (_get_property_count (propmodel, "y") == 1);
108 end_step ();
110 /* remove one */
111 set_waiting_signal (G_OBJECT (propmodel), "pre-row-deletion");
112 rhythmdb_query_model_remove_entry (model, a);
113 wait_for_signal ();
114 /*fail_unless (_get_property_count (propmodel, _("All")) == 1);*/
115 fail_unless (_get_property_count (propmodel, "x") == 0);
116 fail_unless (_get_property_count (propmodel, "y") == 1);
118 end_step ();
120 /* switch model */
121 model2 = rhythmdb_query_model_new_empty (db);
122 g_object_set (model2, "show-hidden", FALSE, NULL);
123 rhythmdb_query_model_add_entry (model2, a, -1);
124 rhythmdb_query_model_add_entry (model2, b, -1);
125 set_waiting_signal (G_OBJECT (propmodel), "row-inserted");
126 g_object_set (propmodel, "query-model", model2, NULL);
127 wait_for_signal ();
129 fail_unless (_get_property_count (propmodel, "x") == 1);
130 fail_unless (_get_property_count (propmodel, "y") == 1);
132 end_step ();
134 /* delete an entry */
135 set_waiting_signal (G_OBJECT (db), "entry_deleted");
136 rhythmdb_entry_delete (db, a);
137 rhythmdb_commit (db);
138 wait_for_signal ();
139 fail_unless (_get_property_count (propmodel, "x") == 0);
140 fail_unless (_get_property_count (propmodel, "y") == 1);
142 end_step ();
144 /* and the other */
145 set_waiting_signal (G_OBJECT (propmodel), "row-deleted");
146 rhythmdb_entry_delete (db, b);
147 rhythmdb_commit (db);
148 wait_for_signal ();
149 fail_unless (_get_property_count (propmodel, "x") == 0);
150 fail_unless (_get_property_count (propmodel, "y") == 0);
152 end_test_case ();
154 g_object_unref (model);
155 g_object_unref (model2);
156 g_object_unref (propmodel);
158 END_TEST
160 /* tests property models attached to query models with an actual query */
161 START_TEST (test_rhythmdb_property_model_query)
163 RhythmDBQueryModel *model;
164 RhythmDBQueryModel *model2;
165 RhythmDBPropertyModel *propmodel;
166 RhythmDBEntry *a, *b;
167 GPtrArray *query;
169 start_test_case ();
171 /* setup */
172 query = rhythmdb_query_parse (db,
173 RHYTHMDB_QUERY_PROP_EQUALS,
174 RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
175 RHYTHMDB_QUERY_PROP_LIKE,
176 RHYTHMDB_PROP_ARTIST, "x",
177 RHYTHMDB_QUERY_END);
179 model = rhythmdb_query_model_new (db, query, (GCompareDataFunc)rhythmdb_query_model_location_sort_func, NULL, NULL, FALSE);
180 rhythmdb_query_free (query);
182 query = rhythmdb_query_parse (db,
183 RHYTHMDB_QUERY_PROP_EQUALS,
184 RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
185 RHYTHMDB_QUERY_PROP_LIKE,
186 RHYTHMDB_PROP_ARTIST, "y",
187 RHYTHMDB_QUERY_END);
188 model2 = rhythmdb_query_model_new (db, query, (GCompareDataFunc)rhythmdb_query_model_location_sort_func, NULL, NULL, FALSE);
189 rhythmdb_query_free (query);
192 propmodel = rhythmdb_property_model_new (db, RHYTHMDB_PROP_ARTIST);
193 g_object_set (propmodel, "query-model", model, NULL);
195 end_step ();
197 /* create test entries */
198 set_waiting_signal (G_OBJECT (db), "entry_added");
199 a = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///a.ogg");
200 b = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///b.ogg");
201 set_entry_string (db, a, RHYTHMDB_PROP_ARTIST, "x");
202 set_entry_string (db, b, RHYTHMDB_PROP_ARTIST, "y");
203 rhythmdb_commit (db);
204 wait_for_signal ();
206 fail_unless (_get_property_count (propmodel, "x") == 1);
207 fail_unless (_get_property_count (propmodel, "y") == 0);
209 end_step ();
211 /* change b so it matches the query */
212 set_entry_string (db, b, RHYTHMDB_PROP_ARTIST, "x");
213 rhythmdb_commit (db);
214 fail_unless (_get_property_count (propmodel, "x") == 2);
215 fail_unless (_get_property_count (propmodel, "y") == 0);
217 end_step ();
219 /* change b again */
220 set_entry_string (db, b, RHYTHMDB_PROP_ARTIST, "xx");
221 rhythmdb_commit (db);
222 fail_unless (_get_property_count (propmodel, "x") == 1);
223 fail_unless (_get_property_count (propmodel, "xx") == 1);
224 fail_unless (_get_property_count (propmodel, "y") == 0);
226 end_step ();
228 /* hide a */
229 set_entry_hidden (db, a, TRUE);
230 rhythmdb_commit (db);
231 fail_unless (_get_property_count (propmodel, "x") == 0);
232 fail_unless (_get_property_count (propmodel, "xx") == 1);
234 end_step ();
236 /* change a */
237 set_entry_string (db, a, RHYTHMDB_PROP_ARTIST, "xx");
238 rhythmdb_commit (db);
239 fail_unless (_get_property_count (propmodel, "x") == 0);
240 fail_unless (_get_property_count (propmodel, "xx") == 1);
242 end_step ();
244 /* unhide a */
245 set_entry_hidden (db, a, FALSE);
246 rhythmdb_commit (db);
247 fail_unless (_get_property_count (propmodel, "x") == 0);
248 fail_unless (_get_property_count (propmodel, "xx") == 2);
250 end_step ();
252 /* change a -> y */
253 set_entry_string (db, a, RHYTHMDB_PROP_ARTIST, "y");
254 rhythmdb_commit (db);
255 fail_unless (_get_property_count (propmodel, "x") == 0);
256 fail_unless (_get_property_count (propmodel, "xx") == 1);
257 fail_unless (_get_property_count (propmodel, "y") == 0);
259 end_step ();
261 /* switch to model2 */
262 g_object_set (propmodel, "query-model", model2, NULL);
263 fail_unless (_get_property_count (propmodel, "x") == 0);
264 fail_unless (_get_property_count (propmodel, "y") == 1);
265 fail_unless (_get_property_count (propmodel, "xx") == 0);
267 end_step ();
269 /* change a -> x */
270 set_entry_string (db, a, RHYTHMDB_PROP_ARTIST, "x");
271 rhythmdb_commit (db);
272 fail_unless (_get_property_count (propmodel, "x") == 0);
273 fail_unless (_get_property_count (propmodel, "xx") == 0);
274 fail_unless (_get_property_count (propmodel, "y") == 0);
276 end_step ();
278 rhythmdb_entry_delete (db, a);
279 rhythmdb_entry_delete (db, b);
280 rhythmdb_commit (db);
282 end_test_case ();
284 g_object_unref (model);
285 g_object_unref (model2);
286 g_object_unref (propmodel);
288 END_TEST
290 /* tests property models attached to chained query models */
291 START_TEST (test_rhythmdb_property_model_query_chain)
293 RhythmDBQueryModel *base_model;
294 RhythmDBQueryModel *model;
295 RhythmDBPropertyModel *propmodel;
296 RhythmDBQuery *query;
297 RhythmDBEntry *a, *b;
299 start_test_case ();
301 /* setup */
302 query = rhythmdb_query_parse (db,
303 RHYTHMDB_QUERY_PROP_EQUALS,
304 RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
305 RHYTHMDB_QUERY_PROP_LIKE,
306 RHYTHMDB_PROP_SEARCH_MATCH, "y",
307 RHYTHMDB_QUERY_END);
309 base_model = rhythmdb_query_model_new (db, query, (GCompareDataFunc)rhythmdb_query_model_location_sort_func, NULL, NULL, FALSE);
310 rhythmdb_query_free (query);
312 query = rhythmdb_query_parse (db,
313 RHYTHMDB_QUERY_PROP_EQUALS,
314 RHYTHMDB_PROP_TYPE, RHYTHMDB_ENTRY_TYPE_SONG,
315 RHYTHMDB_QUERY_PROP_EQUALS,
316 RHYTHMDB_PROP_TRACK_NUMBER, 1,
317 RHYTHMDB_QUERY_END);
318 model = rhythmdb_query_model_new (db, query, (GCompareDataFunc)rhythmdb_query_model_location_sort_func, NULL, NULL, FALSE);
319 rhythmdb_query_free (query);
321 rhythmdb_query_model_chain (model, base_model, TRUE);
323 propmodel = rhythmdb_property_model_new (db, RHYTHMDB_PROP_ALBUM);
324 g_object_set (propmodel, "query-model", model, NULL);
326 /* create test entries */
327 set_waiting_signal (G_OBJECT (db), "entry_added");
328 a = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///a.ogg");
329 set_entry_string (db, a, RHYTHMDB_PROP_ALBUM, "x");
330 set_entry_ulong (db, a, RHYTHMDB_PROP_TRACK_NUMBER, 1);
331 rhythmdb_commit (db);
332 wait_for_signal ();
334 set_waiting_signal (G_OBJECT (db), "entry_added");
335 b = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///b.ogg");
336 set_entry_string (db, b, RHYTHMDB_PROP_ALBUM, "y");
337 set_entry_ulong (db, b, RHYTHMDB_PROP_TRACK_NUMBER, 1);
338 rhythmdb_commit (db);
339 wait_for_signal ();
341 fail_unless (_get_property_count (propmodel, "x") == 0);
342 fail_unless (_get_property_count (propmodel, "y") == 1);
344 end_step ();
346 /* change entry a so it matches the child query */
347 set_entry_string (db, a, RHYTHMDB_PROP_ALBUM, "yy");
348 rhythmdb_commit (db);
350 fail_unless (_get_property_count (propmodel, "x") == 0);
351 fail_unless (_get_property_count (propmodel, "y") == 1);
352 fail_unless (_get_property_count (propmodel, "yy") == 1);
354 end_step ();
356 /* change entry a again */
357 set_entry_string (db, a, RHYTHMDB_PROP_ALBUM, "y");
358 rhythmdb_commit (db);
360 fail_unless (_get_property_count (propmodel, "y") == 2);
361 fail_unless (_get_property_count (propmodel, "yy") == 0);
363 end_step ();
365 /* change entry b again */
366 set_entry_string (db, b, RHYTHMDB_PROP_ALBUM, "z");
367 rhythmdb_commit (db);
369 fail_unless (_get_property_count (propmodel, "y") == 1);
370 fail_unless (_get_property_count (propmodel, "z") == 0);
372 end_step ();
374 rhythmdb_entry_delete (db, a);
375 rhythmdb_entry_delete (db, b);
376 rhythmdb_commit (db);
378 end_test_case ();
380 g_object_unref (model);
381 g_object_unref (base_model);
382 g_object_unref (propmodel);
384 END_TEST
387 static Suite *
388 rhythmdb_property_model_suite (void)
390 Suite *s = suite_create ("rhythmdb-property-model");
391 TCase *tc_chain = tcase_create ("rhythmdb-property-model-core");
392 TCase *tc_bugs = tcase_create ("rhythmdb-property-model-bugs");
394 suite_add_tcase (s, tc_chain);
395 tcase_add_checked_fixture (tc_chain, test_rhythmdb_setup, test_rhythmdb_shutdown);
396 suite_add_tcase (s, tc_bugs);
397 tcase_add_checked_fixture (tc_bugs, test_rhythmdb_setup, test_rhythmdb_shutdown);
399 /* test core functionality */
400 tcase_add_test (tc_chain, test_rhythmdb_property_model_static);
401 tcase_add_test (tc_chain, test_rhythmdb_property_model_query);
402 tcase_add_test (tc_chain, test_rhythmdb_property_model_query_chain);
404 /* tests for breakable bug fixes */
405 /* tcase_add_test (tc_bugs, test_hidden_chain_filter);*/
407 return s;
411 main (int argc, char **argv)
413 int ret;
414 SRunner *sr;
415 Suite *s;
417 /* init stuff */
418 rb_profile_start ("rhythmdb-property-model test suite");
420 g_thread_init (NULL);
421 rb_threads_init ();
422 gtk_set_locale ();
423 gtk_init (&argc, &argv);
424 gnome_vfs_init ();
425 rb_debug_init (TRUE);
426 rb_refstring_system_init ();
427 rb_file_helpers_init ();
430 GDK_THREADS_ENTER ();
432 /* setup tests */
433 s = rhythmdb_property_model_suite ();
434 sr = srunner_create (s);
435 srunner_run_all (sr, CK_NORMAL);
436 ret = srunner_ntests_failed (sr);
437 srunner_free (sr);
440 rb_file_helpers_shutdown ();
441 rb_refstring_system_shutdown ();
442 gnome_vfs_shutdown ();
444 rb_profile_end ("rhythmdb-property-model test suite");
445 return ret;