Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / check / libs / xmpwriter.c
1 /* GStreamer
2  *
3  * unit tests for xmp config library
4  *
5  * Copyright (C) 2011 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/check/gstcheck.h>
28
29 #include <gst/tag/tag.h>
30 #include <gst/tag/xmpwriter.h>
31
32 #include <string.h>
33
34 #define TEST_ELEMENT_TYPE (test_element_get_type())
35
36 typedef struct TestElement TestElement;
37 typedef struct TestElementClass TestElementClass;
38
39 struct TestElement
40 {
41   GstElement parent;
42 };
43
44 struct TestElementClass
45 {
46   GstElementClass parent_class;
47 };
48
49 GType test_element_get_type (void);
50
51 static void init_interface (GType type);
52
53 GST_BOILERPLATE_FULL (TestElement, test_element, GstElement, GST_TYPE_ELEMENT,
54     init_interface);
55
56 static void
57 init_interface (GType type)
58 {
59   static const GInterfaceInfo tagxmpwriter_info = {
60     NULL,
61     NULL,
62     NULL,
63   };
64
65   g_type_add_interface_static (type, GST_TYPE_TAG_XMP_WRITER,
66       &tagxmpwriter_info);
67 }
68
69 static void
70 test_element_base_init (gpointer klass)
71 {
72 }
73
74 static void
75 test_element_class_init (TestElementClass * klass)
76 {
77 }
78
79 static void
80 test_element_init (TestElement * this, TestElementClass * klass)
81 {
82 }
83
84 static void
85 tag_list_equals (GstTagList * taglist, GstTagList * taglist2)
86 {
87   const gchar *name_sent, *name_recv;
88   const GValue *value_sent, *value_recv;
89   gboolean found;
90   gint comparison;
91   gint n_recv;
92   gint n_sent;
93   gint i, j;
94
95   /* verify tags */
96   fail_unless (taglist2 != NULL);
97   n_recv = gst_structure_n_fields (taglist2);
98   n_sent = gst_structure_n_fields (taglist);
99   fail_unless (n_recv == n_sent);
100   fail_unless (n_sent > 0);
101
102   /* FIXME: compare taglist values */
103   for (i = 0; i < n_sent; i++) {
104     name_sent = gst_structure_nth_field_name (taglist, i);
105     value_sent = gst_structure_get_value (taglist, name_sent);
106     found = FALSE;
107     for (j = 0; j < n_recv; j++) {
108       name_recv = gst_structure_nth_field_name (taglist2, j);
109       if (!strcmp (name_sent, name_recv)) {
110         value_recv = gst_structure_get_value (taglist2, name_recv);
111         comparison = gst_value_compare (value_sent, value_recv);
112         if (comparison != GST_VALUE_EQUAL) {
113           gchar *vs = g_strdup_value_contents (value_sent);
114           gchar *vr = g_strdup_value_contents (value_recv);
115           GST_DEBUG ("sent = %s:'%s', recv = %s:'%s'",
116               G_VALUE_TYPE_NAME (value_sent), vs,
117               G_VALUE_TYPE_NAME (value_recv), vr);
118           g_free (vs);
119           g_free (vr);
120         }
121         if (comparison != GST_VALUE_EQUAL &&
122             G_VALUE_HOLDS (value_sent, G_TYPE_DOUBLE)) {
123           gdouble vs;
124           gdouble vr;
125
126           /* add some tolerance for doubles */
127           vs = g_value_get_double (value_sent);
128           vr = g_value_get_double (value_recv);
129           if (vr >= vs - 0.001 && vr <= vs + 0.001)
130             comparison = GST_VALUE_EQUAL;
131         }
132         fail_unless (comparison == GST_VALUE_EQUAL,
133             "tag item %s has been received with different type or value",
134             name_sent);
135         found = TRUE;
136         break;
137       }
138     }
139     fail_unless (found, "tag item %s is lost", name_sent);
140   }
141 }
142
143 static gboolean
144 gst_buffer_equals (GstBuffer * buf_a, GstBuffer * buf_b)
145 {
146   if (GST_BUFFER_SIZE (buf_a) != GST_BUFFER_SIZE (buf_b))
147     return FALSE;
148
149   return memcmp (GST_BUFFER_DATA (buf_a), GST_BUFFER_DATA (buf_b),
150       GST_BUFFER_SIZE (buf_a)) == 0;
151 }
152
153 static GstTagList *
154 create_taglist (void)
155 {
156   return gst_tag_list_new_full (GST_TAG_ARTIST, "artist",
157       GST_TAG_TITLE, "title", GST_TAG_COPYRIGHT, "copyright", NULL);
158 }
159
160 GST_START_TEST (test_no_xmp)
161 {
162   GstTagList *taglist = create_taglist ();
163   GstElement *test_element =
164       (GstElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
165
166   gst_tag_xmp_writer_remove_all_schemas (GST_TAG_XMP_WRITER (test_element));
167
168   fail_unless (gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER
169           (test_element), taglist, TRUE) == NULL);
170
171   gst_object_unref (test_element);
172   gst_tag_list_free (taglist);
173 }
174
175 GST_END_TEST;
176
177
178 GST_START_TEST (test_default)
179 {
180   GstTagList *taglist = create_taglist ();
181   GstElement *test_element =
182       (GstElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
183   GstBuffer *buf;
184   GstBuffer *buf2;
185
186   buf =
187       gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER
188       (test_element), taglist, TRUE);
189   buf2 = gst_tag_list_to_xmp_buffer (taglist, TRUE);
190   fail_unless (gst_buffer_equals (buf, buf2));
191
192   gst_object_unref (test_element);
193   gst_buffer_unref (buf);
194   gst_buffer_unref (buf2);
195   gst_tag_list_free (taglist);
196 }
197
198 GST_END_TEST;
199
200
201 GST_START_TEST (test_disable)
202 {
203   GstTagList *taglist;
204   GstTagList *taglist2;
205   GstElement *test_element =
206       (GstElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
207   GstBuffer *buf;
208   const gchar *str;
209
210   taglist = gst_tag_list_new_full (GST_TAG_ARTIST, "artist", NULL);
211
212   /* add a tag that is mapped on xmp schema (as of Mar, 21th 2011) */
213   gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_USER_RATING, 5,
214       NULL);
215
216   buf =
217       gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER
218       (test_element), taglist, TRUE);
219   taglist2 = gst_tag_list_from_xmp_buffer (buf);
220   tag_list_equals (taglist, taglist2);
221   gst_tag_list_free (taglist2);
222   gst_buffer_unref (buf);
223
224   gst_tag_xmp_writer_remove_schema (GST_TAG_XMP_WRITER (test_element), "xap");
225   buf =
226       gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER
227       (test_element), taglist, TRUE);
228   taglist2 = gst_tag_list_from_xmp_buffer (buf);
229
230   /* artist should be there, but rating shouldn't */
231   fail_unless (gst_tag_list_peek_string_index (taglist2, GST_TAG_ARTIST, 0,
232           &str));
233   fail_unless (gst_tag_list_get_value_index (taglist2, GST_TAG_USER_RATING,
234           0) == NULL);
235
236   gst_tag_list_free (taglist2);
237   gst_buffer_unref (buf);
238
239   gst_object_unref (test_element);
240   gst_tag_list_free (taglist);
241 }
242
243 GST_END_TEST;
244
245
246 static Suite *
247 xmp_config_suite (void)
248 {
249   Suite *s = suite_create ("xmpconfig interface");
250   TCase *tc_chain = tcase_create ("configuration");
251
252   suite_add_tcase (s, tc_chain);
253   tcase_add_test (tc_chain, test_no_xmp);
254   tcase_add_test (tc_chain, test_default);
255   tcase_add_test (tc_chain, test_disable);
256
257   return s;
258 }
259
260 int
261 main (int argc, char **argv)
262 {
263   int nf;
264
265   Suite *s = xmp_config_suite ();
266   SRunner *sr = srunner_create (s);
267
268   gst_check_init (&argc, &argv);
269
270   srunner_run_all (sr, CK_NORMAL);
271   nf = srunner_ntests_failed (sr);
272   srunner_free (sr);
273
274   return nf;
275 }