2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <gst/check/gstcheck.h>
24 /* multiple artists are possible (unfixed) */
25 #define UTAG GST_TAG_ARTIST
26 #define UNFIXED1 "Britney Spears"
27 #define UNFIXED2 "Evanescence"
28 #define UNFIXED3 "AC/DC"
29 #define UNFIXED4 "The Prodigy"
31 /* license is fixed */
32 #define FTAG GST_TAG_LICENSE
33 #define FIXED1 "Lesser General Public License"
34 #define FIXED2 "Microsoft End User License Agreement"
35 #define FIXED3 "Mozilla Public License"
36 #define FIXED4 "Public Domain"
38 /* checks that a tag contains the given values and not more values */
40 check_tags (const GstTagList * list, const gchar * tag, const gchar * value,
47 va_start (args, value);
48 while (value != NULL) {
49 fail_unless (gst_tag_list_get_string_index (list, tag, i, &str));
50 fail_unless (strcmp (value, str) == 0);
53 value = va_arg (args, gchar *);
56 fail_unless (i == gst_tag_list_get_tag_size (list, tag));
61 check_tags_empty (const GstTagList * list)
63 GST_DEBUG ("taglist: %" GST_PTR_FORMAT, list);
64 fail_unless ((list == NULL) || (gst_tag_list_is_empty (list)));
67 #define NEW_LIST_FIXED(mode) \
69 if (list) gst_tag_list_free (list); \
70 list = gst_tag_list_new (); \
71 gst_tag_list_add (list, mode, FTAG, FIXED1, FTAG, FIXED2, \
72 FTAG, FIXED3, FTAG, FIXED4, NULL); \
76 #define NEW_LIST_UNFIXED(mode) \
78 if (list) gst_tag_list_free (list); \
79 list = gst_tag_list_new (); \
80 gst_tag_list_add (list, mode, UTAG, UNFIXED1, UTAG, UNFIXED2, \
81 UTAG, UNFIXED3, UTAG, UNFIXED4, NULL); \
85 #define NEW_LISTS_FIXED(mode) \
87 if (list) gst_tag_list_free (list); \
88 list = gst_tag_list_new (); \
89 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, FTAG, FIXED1, \
90 FTAG, FIXED2, NULL); \
91 if (list2) gst_tag_list_free (list2); \
92 list2 = gst_tag_list_new (); \
93 gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, FTAG, FIXED3, \
94 FTAG, FIXED4, NULL); \
95 if (merge) gst_tag_list_free (merge); \
96 merge = gst_tag_list_merge (list, list2, mode); \
100 #define NEW_LISTS_UNFIXED(mode) \
102 if (list) gst_tag_list_free (list); \
103 list = gst_tag_list_new (); \
104 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, UTAG, UNFIXED1, \
105 UTAG, UNFIXED2, NULL); \
106 if (list2) gst_tag_list_free (list2); \
107 list2 = gst_tag_list_new (); \
108 gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, UTAG, UNFIXED3,\
109 UTAG, UNFIXED4, NULL); \
110 if (merge) gst_tag_list_free (merge); \
111 merge = gst_tag_list_merge (list, list2, mode); \
115 #define NEW_LISTS_EMPTY1(mode) \
117 if (list) gst_tag_list_free (list); \
119 if (list2) gst_tag_list_free (list2); \
120 list2 = gst_tag_list_new (); \
121 gst_tag_list_add (list2, GST_TAG_MERGE_APPEND, FTAG, FIXED3, \
122 FTAG, FIXED4, NULL); \
123 if (merge) gst_tag_list_free (merge); \
124 merge = gst_tag_list_merge (list, list2, mode); \
128 #define NEW_LISTS_EMPTY2(mode) \
130 if (list) gst_tag_list_free (list); \
131 list = gst_tag_list_new (); \
132 gst_tag_list_add (list, GST_TAG_MERGE_APPEND, FTAG, FIXED1, \
133 FTAG, FIXED2, NULL); \
134 if (list2) gst_tag_list_free (list2); \
136 if (merge) gst_tag_list_free (merge); \
137 merge = gst_tag_list_merge (list, list2, mode); \
142 GST_START_TEST (test_basics)
144 /* make sure the assumptions work */
145 fail_unless (gst_tag_is_fixed (FTAG));
146 fail_unless (!gst_tag_is_fixed (UTAG));
147 /* we check string here only */
148 fail_unless (gst_tag_get_type (FTAG) == G_TYPE_STRING);
149 fail_unless (gst_tag_get_type (UTAG) == G_TYPE_STRING);
153 GST_START_TEST (test_add)
155 GstTagList *list = NULL;
157 /* check additions */
159 NEW_LIST_UNFIXED (GST_TAG_MERGE_REPLACE_ALL);
160 check_tags (list, UTAG, UNFIXED4, NULL);
161 NEW_LIST_UNFIXED (GST_TAG_MERGE_REPLACE);
162 check_tags (list, UTAG, UNFIXED4, NULL);
163 NEW_LIST_UNFIXED (GST_TAG_MERGE_PREPEND);
164 check_tags (list, UTAG, UNFIXED4, UNFIXED3, UNFIXED2, UNFIXED1, NULL);
165 NEW_LIST_UNFIXED (GST_TAG_MERGE_APPEND);
166 check_tags (list, UTAG, UNFIXED1, UNFIXED2, UNFIXED3, UNFIXED4, NULL);
167 NEW_LIST_UNFIXED (GST_TAG_MERGE_KEEP);
168 check_tags (list, UTAG, UNFIXED1, NULL);
169 NEW_LIST_UNFIXED (GST_TAG_MERGE_KEEP_ALL);
170 check_tags (list, UTAG, NULL);
173 NEW_LIST_FIXED (GST_TAG_MERGE_REPLACE_ALL);
174 check_tags (list, FTAG, FIXED4, NULL);
175 NEW_LIST_FIXED (GST_TAG_MERGE_REPLACE);
176 check_tags (list, FTAG, FIXED4, NULL);
177 NEW_LIST_FIXED (GST_TAG_MERGE_PREPEND);
178 check_tags (list, FTAG, FIXED4, NULL);
179 NEW_LIST_FIXED (GST_TAG_MERGE_APPEND);
180 check_tags (list, FTAG, FIXED1, NULL);
181 NEW_LIST_FIXED (GST_TAG_MERGE_KEEP);
182 check_tags (list, FTAG, FIXED1, NULL);
183 NEW_LIST_FIXED (GST_TAG_MERGE_KEEP_ALL);
184 check_tags (list, FTAG, NULL);
188 gst_tag_list_free (list);
192 GST_START_TEST (test_merge)
194 GstTagList *list = NULL, *list2 = NULL, *merge = NULL;
198 GST_DEBUG ("unfixed");
199 NEW_LISTS_UNFIXED (GST_TAG_MERGE_REPLACE_ALL);
200 check_tags (merge, UTAG, UNFIXED3, UNFIXED4, NULL);
201 NEW_LISTS_UNFIXED (GST_TAG_MERGE_REPLACE);
202 check_tags (merge, UTAG, UNFIXED3, UNFIXED4, NULL);
203 NEW_LISTS_UNFIXED (GST_TAG_MERGE_PREPEND);
204 check_tags (merge, UTAG, UNFIXED3, UNFIXED4, UNFIXED1, UNFIXED2, NULL);
205 NEW_LISTS_UNFIXED (GST_TAG_MERGE_APPEND);
206 check_tags (merge, UTAG, UNFIXED1, UNFIXED2, UNFIXED3, UNFIXED4, NULL);
207 NEW_LISTS_UNFIXED (GST_TAG_MERGE_KEEP);
208 check_tags (merge, UTAG, UNFIXED1, UNFIXED2, NULL);
209 NEW_LISTS_UNFIXED (GST_TAG_MERGE_KEEP_ALL);
210 check_tags (merge, UTAG, UNFIXED1, UNFIXED2, NULL);
214 NEW_LISTS_FIXED (GST_TAG_MERGE_REPLACE_ALL);
215 check_tags (merge, FTAG, FIXED3, NULL);
216 NEW_LISTS_FIXED (GST_TAG_MERGE_REPLACE);
217 check_tags (merge, FTAG, FIXED3, NULL);
218 NEW_LISTS_FIXED (GST_TAG_MERGE_PREPEND);
219 check_tags (merge, FTAG, FIXED3, NULL);
220 NEW_LISTS_FIXED (GST_TAG_MERGE_APPEND);
221 check_tags (merge, FTAG, FIXED1, NULL);
222 NEW_LISTS_FIXED (GST_TAG_MERGE_KEEP);
223 check_tags (merge, FTAG, FIXED1, NULL);
224 NEW_LISTS_FIXED (GST_TAG_MERGE_KEEP_ALL);
225 check_tags (merge, FTAG, FIXED1, NULL);
227 /* first list empty */
228 GST_DEBUG ("first empty");
229 NEW_LISTS_EMPTY1 (GST_TAG_MERGE_REPLACE_ALL);
230 check_tags (merge, FTAG, FIXED3, NULL);
231 NEW_LISTS_EMPTY1 (GST_TAG_MERGE_REPLACE);
232 check_tags (merge, FTAG, FIXED3, NULL);
233 NEW_LISTS_EMPTY1 (GST_TAG_MERGE_PREPEND);
234 check_tags (merge, FTAG, FIXED3, NULL);
235 NEW_LISTS_EMPTY1 (GST_TAG_MERGE_APPEND);
236 check_tags (merge, FTAG, FIXED3, NULL);
237 NEW_LISTS_EMPTY1 (GST_TAG_MERGE_KEEP);
238 check_tags (merge, FTAG, FIXED3, NULL);
239 NEW_LISTS_EMPTY1 (GST_TAG_MERGE_KEEP_ALL);
240 check_tags_empty (merge);
242 /* second list empty */
243 GST_DEBUG ("second empty");
244 NEW_LISTS_EMPTY2 (GST_TAG_MERGE_REPLACE_ALL);
245 check_tags_empty (merge);
246 NEW_LISTS_EMPTY2 (GST_TAG_MERGE_REPLACE);
247 check_tags (merge, FTAG, FIXED1, NULL);
248 NEW_LISTS_EMPTY2 (GST_TAG_MERGE_PREPEND);
249 check_tags (merge, FTAG, FIXED1, NULL);
250 NEW_LISTS_EMPTY2 (GST_TAG_MERGE_APPEND);
251 check_tags (merge, FTAG, FIXED1, NULL);
252 NEW_LISTS_EMPTY2 (GST_TAG_MERGE_KEEP);
253 check_tags (merge, FTAG, FIXED1, NULL);
254 NEW_LISTS_EMPTY2 (GST_TAG_MERGE_KEEP_ALL);
255 check_tags (merge, FTAG, FIXED1, NULL);
259 gst_tag_list_free (list);
261 gst_tag_list_free (list2);
263 gst_tag_list_free (merge);
267 GST_START_TEST (test_date_tags)
269 GstTagList *tag_list, *tag_list2;
273 date = g_date_new_dmy (14, 10, 2005);
274 tag_list = gst_tag_list_new ();
275 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, GST_TAG_DATE, date, NULL);
277 str = gst_structure_to_string (tag_list);
278 fail_if (str == NULL);
279 fail_if (strstr (str, "2005-10-14") == NULL);
281 tag_list2 = gst_structure_from_string (str, NULL);
282 fail_if (tag_list2 == NULL);
283 fail_if (!gst_tag_list_get_date (tag_list2, GST_TAG_DATE, &date2));
284 gst_tag_list_free (tag_list2);
287 fail_if (g_date_compare (date, date2) != 0);
288 fail_if (g_date_get_day (date) != 14);
289 fail_if (g_date_get_month (date) != 10);
290 fail_if (g_date_get_year (date) != 2005);
291 fail_if (g_date_get_day (date2) != 14);
292 fail_if (g_date_get_month (date2) != 10);
293 fail_if (g_date_get_year (date2) != 2005);
296 gst_tag_list_free (tag_list);
302 GST_START_TEST (test_type)
306 taglist = gst_tag_list_new ();
307 fail_unless (GST_IS_TAG_LIST (taglist));
308 fail_unless (gst_is_tag_list (taglist));
309 gst_tag_list_free (taglist);
311 /* this isn't okay */
312 ASSERT_CRITICAL (fail_if (gst_is_tag_list (NULL)));
314 /* this however should be fine */
315 fail_if (GST_IS_TAG_LIST (NULL));
317 /* check gst_tag_list_is_empty */
318 ASSERT_CRITICAL (gst_tag_list_is_empty (NULL));
319 taglist = gst_tag_list_new ();
320 fail_unless (gst_tag_list_is_empty (taglist));
321 gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "JD", NULL);
322 fail_if (gst_tag_list_is_empty (taglist));
323 gst_tag_list_free (taglist);
328 GST_START_TEST (test_set_non_utf8_string)
331 guint8 foobar[2] = { 0xff, 0x00 }; /* not UTF-8 */
333 taglist = gst_tag_list_new ();
334 fail_unless (taglist != NULL);
336 ASSERT_WARNING (gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND,
337 GST_TAG_ARTIST, (gchar *) foobar, NULL));
339 /* That string field with a non-UTF8 string should not have been added */
340 fail_unless (gst_tag_list_is_empty (taglist));
342 gst_tag_list_free (taglist);
347 GST_START_TEST (test_buffer_tags)
350 GstBuffer *buf1, *buf2;
352 tags = gst_tag_list_new ();
353 buf1 = gst_buffer_new_and_alloc (222);
354 buf2 = gst_buffer_new_and_alloc (100);
355 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_IMAGE, buf1,
356 GST_TAG_PREVIEW_IMAGE, buf2, NULL);
357 gst_buffer_unref (buf1);
358 gst_buffer_unref (buf2);
361 fail_if (!gst_tag_list_get_buffer (tags, GST_TAG_IMAGE, &buf1));
362 gst_buffer_unref (buf1);
363 fail_if (!gst_tag_list_get_buffer (tags, GST_TAG_PREVIEW_IMAGE, &buf2));
364 gst_buffer_unref (buf2);
366 fail_if (gst_tag_list_get_buffer_index (tags, GST_TAG_IMAGE, 1, &buf1));
367 fail_if (gst_tag_list_get_buffer_index (tags, GST_TAG_IMAGE, 2, &buf1));
368 fail_if (gst_tag_list_get_buffer_index (tags, GST_TAG_PREVIEW_IMAGE, 1,
370 fail_if (gst_tag_list_get_buffer_index (tags, GST_TAG_PREVIEW_IMAGE, 2,
373 fail_if (!gst_tag_list_get_buffer_index (tags, GST_TAG_IMAGE, 0, &buf1));
374 fail_if (!gst_tag_list_get_buffer_index (tags, GST_TAG_PREVIEW_IMAGE, 0,
376 fail_unless_equals_int (gst_buffer_get_size (buf1), 222);
377 fail_unless_equals_int (gst_buffer_get_size (buf2), 100);
379 gst_buffer_unref (buf1);
380 gst_buffer_unref (buf2);
382 gst_tag_list_free (tags);
387 GST_START_TEST (test_empty_tags)
391 /* only get g_warnings() with git */
392 if (GST_VERSION_NANO != 1)
395 tags = gst_tag_list_new ();
396 ASSERT_WARNING (gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
397 GST_TAG_ARTIST, NULL, NULL));
398 ASSERT_WARNING (gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
399 GST_TAG_ARTIST, "", NULL));
400 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "xyz", NULL);
401 gst_tag_list_free (tags);
406 GST_START_TEST (test_new_full)
409 gchar *artist, *title;
413 tags = gst_tag_list_new_full (GST_TAG_ARTIST, "Arty Ist",
414 GST_TAG_TRACK_NUMBER, 9, GST_TAG_TRACK_GAIN, 4.242, GST_TAG_TITLE,
417 fail_unless (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &artist));
418 fail_unless_equals_string (artist, "Arty Ist");
419 fail_unless (gst_tag_list_get_string (tags, GST_TAG_TITLE, &title));
420 fail_unless_equals_string (title, "Title!");
421 fail_unless (gst_tag_list_get_uint (tags, GST_TAG_TRACK_NUMBER, &track_num));
422 fail_unless_equals_int (track_num, 9);
423 fail_unless (gst_tag_list_get_double (tags, GST_TAG_TRACK_GAIN, &track_gain));
424 fail_unless_equals_float (track_gain, 4.242);
425 fail_unless (tags != NULL);
427 gst_tag_list_free (tags);
434 GST_START_TEST (test_merge_strings_with_comma)
437 gchar *artists = NULL;
439 tags = gst_tag_list_new ();
440 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Foo", NULL);
441 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Bar", NULL);
442 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, "Yay", NULL);
443 gst_tag_list_get_string (tags, GST_TAG_ARTIST, &artists);
444 fail_unless (artists != NULL);
445 /* can't check for exact string since the comma separator is i18n-ed */
446 fail_unless (strstr (artists, "Foo") != NULL);
447 fail_unless (strstr (artists, "Bar") != NULL);
448 fail_unless (strstr (artists, "Yay") != NULL);
450 gst_tag_list_free (tags);
458 Suite *s = suite_create ("GstTag");
459 TCase *tc_chain = tcase_create ("general");
461 suite_add_tcase (s, tc_chain);
462 tcase_add_test (tc_chain, test_basics);
463 tcase_add_test (tc_chain, test_add);
464 tcase_add_test (tc_chain, test_merge);
465 tcase_add_test (tc_chain, test_merge_strings_with_comma);
466 tcase_add_test (tc_chain, test_date_tags);
467 tcase_add_test (tc_chain, test_type);
468 tcase_add_test (tc_chain, test_set_non_utf8_string);
469 tcase_add_test (tc_chain, test_buffer_tags);
470 tcase_add_test (tc_chain, test_empty_tags);
471 tcase_add_test (tc_chain, test_new_full);
476 GST_CHECK_MAIN (gst_tag);