toc: put toc directly into event/message/query structure
[platform/upstream/gstreamer.git] / tests / check / gst / gsttoc.c
1 /* GStreamer
2  *
3  * unit test for GstToc
4  *
5  * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
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 /*  -------  TOC  -------
24  *           /  \
25  *   edition1    edition2
26  *   |           |
27  *   -chapter1   -chapter3
28  *   -chapter2    |
29  *                -subchapter1
30  */
31
32 #include <gst/check/gstcheck.h>
33
34 #define ENTRY_ED1       "/edition1"
35 #define ENTRY_ED2       "/edition2"
36 #define ENTRY_ED3       "test-edition"
37
38 #define ENTRY_CH1       "/edition1/chapter1"
39 #define ENTRY_CH2       "/edition1/chapter2"
40 #define ENTRY_CH3       "/edition2/chapter3"
41 #define ENTRY_CH4       "/test-chapter"
42
43 #define ENTRY_SUB1      "/edition2/chapter3/subchapter1"
44
45 #define ENTRY_TAG       "EntryTag"
46 #define TOC_TAG         "TocTag"
47
48 #define TEST_UID        "129537542"
49 #define INFO_NAME       "gst-toc-check"
50 #define INFO_FIELD      "info-test"
51 #define INFO_TEXT_EN    "info-text-entry"
52 #define INFO_TEXT_TOC   "info-text-toc"
53
54 #define CHECK_TOC_ENTRY(entry_c,type_c,uid_c)                            \
55 {                                                                        \
56   gchar *tag_c;                                                          \
57   const GValue *val;                                                     \
58   GstStructure *struct_c;                                                \
59                                                                          \
60   fail_unless_equals_string (entry_c->uid, uid_c);                       \
61   fail_unless (entry_c->type == type_c);                                 \
62   fail_unless (entry_c->tags != NULL);                                   \
63   fail_unless (entry_c->pads == NULL);                                   \
64                                                                          \
65   fail_unless (entry_c->info != NULL);                                   \
66   gst_structure_get (entry_c->info, INFO_NAME, GST_TYPE_STRUCTURE,       \
67       &struct_c, NULL);                                                  \
68   fail_unless (struct_c != NULL);                                        \
69   val = gst_structure_get_value (struct_c, INFO_FIELD);                  \
70   fail_unless (val != NULL);                                             \
71   fail_unless_equals_string (g_value_get_string (val), INFO_TEXT_EN);    \
72                                                                          \
73   fail_unless (gst_tag_list_get_string (entry_c->tags,                   \
74       GST_TAG_TITLE, &tag_c));                                           \
75   fail_unless_equals_string (tag_c, ENTRY_TAG);                          \
76   g_free (tag_c);                                                        \
77   gst_structure_free (struct_c);                                         \
78 }
79
80 #define CHECK_TOC(toc_t)                                                 \
81 {                                                                        \
82   GstTocEntry *entry_t, *subentry_t;                                     \
83   gchar *tag_t;                                                          \
84   const GValue *val;                                                     \
85   GstStructure *struct_toc;                                              \
86                                                                          \
87   /* check TOC */                                                        \
88   fail_unless (g_list_length (toc_t->entries) == 2);                     \
89   fail_unless (toc_t->tags != NULL);                                     \
90   fail_unless (gst_tag_list_get_string (toc_t->tags,                     \
91       GST_TAG_TITLE, &tag_t));                                           \
92   fail_unless_equals_string (tag_t, TOC_TAG);                            \
93   g_free (tag_t);                                                        \
94                                                                          \
95   fail_unless (toc_t->info != NULL);                                     \
96   gst_structure_get (toc_t->info, INFO_NAME, GST_TYPE_STRUCTURE,         \
97       &struct_toc, NULL);                                                \
98   fail_unless (struct_toc != NULL);                                      \
99   val = gst_structure_get_value (struct_toc, INFO_FIELD);                \
100   fail_unless (val != NULL);                                             \
101   fail_unless_equals_string (g_value_get_string (val), INFO_TEXT_TOC);   \
102   gst_structure_free (struct_toc);                                       \
103                                                                          \
104   /* check edition1 */                                                   \
105   entry_t = g_list_nth_data (toc_t->entries, 0);                         \
106   fail_if (entry_t == NULL);                                             \
107   fail_unless (g_list_length (entry_t->subentries) == 2);                \
108   CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);      \
109   /* check chapter1 */                                                   \
110   subentry_t = g_list_nth_data (entry_t->subentries, 0);                 \
111   fail_if (subentry_t == NULL);                                          \
112   fail_unless (g_list_length (subentry_t->subentries) == 0);             \
113   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);   \
114   /* check chapter2 */                                                   \
115   subentry_t = g_list_nth_data (entry_t->subentries, 1);                 \
116   fail_if (subentry_t == NULL);                                          \
117   fail_unless (g_list_length (subentry_t->subentries) == 0);             \
118   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);   \
119   /* check edition2 */                                                   \
120   entry_t = g_list_nth_data (toc_t->entries, 1);                         \
121   fail_if (entry_t == NULL);                                             \
122   fail_unless (g_list_length (entry_t->subentries) == 1);                \
123   CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);      \
124   /* check chapter3 */                                                   \
125   subentry_t = g_list_nth_data (entry_t->subentries, 0);                 \
126   fail_if (subentry_t == NULL);                                          \
127   fail_unless (g_list_length (subentry_t->subentries) == 1);             \
128   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);   \
129   /* check subchapter1 */                                                \
130   subentry_t = g_list_nth_data (subentry_t->subentries, 0);              \
131   fail_if (subentry_t == NULL);                                          \
132   fail_unless (g_list_length (subentry_t->subentries) == 0);             \
133   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);  \
134 }
135
136 /* This whole test is a bit pointless now that we just stuff a ref of
137  * the original TOC into the message/query/event */
138 GST_START_TEST (test_serializing)
139 {
140   GstStructure *structure;
141   GstToc *toc, *test_toc = NULL;
142   GstTocEntry *ed, *ch, *subch;
143   GstEvent *event;
144   GstMessage *message;
145   GstQuery *query;
146   gboolean updated;
147   gchar *uid;
148   gint64 start = -1, stop = -1;
149
150   toc = gst_toc_new ();
151   fail_if (toc == NULL);
152   gst_tag_list_add (toc->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
153       TOC_TAG, NULL);
154   structure =
155       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_TOC,
156       NULL);
157   gst_structure_set (toc->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
158   gst_structure_free (structure);
159
160   /* create edition1 */
161   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
162   fail_if (ed == NULL);
163   gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
164       ENTRY_TAG, NULL);
165   structure =
166       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
167       NULL);
168   gst_structure_set (ed->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
169   gst_structure_free (structure);
170
171   CHECK_TOC_ENTRY (ed, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
172
173   /* append chapter1 to edition1 */
174   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);
175   fail_if (ch == NULL);
176   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
177       ENTRY_TAG, NULL);
178   structure =
179       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
180       NULL);
181   gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
182   gst_structure_free (structure);
183
184   CHECK_TOC_ENTRY (ch, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);
185
186   ed->subentries = g_list_append (ed->subentries, ch);
187   fail_unless (g_list_length (ed->subentries) == 1);
188
189   /* append chapter2 to edition1 */
190   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);
191   fail_if (ch == NULL);
192   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
193       ENTRY_TAG, NULL);
194   structure =
195       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
196       NULL);
197   gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
198   gst_structure_free (structure);
199
200   CHECK_TOC_ENTRY (ch, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);
201
202   ed->subentries = g_list_append (ed->subentries, ch);
203   fail_unless (g_list_length (ed->subentries) == 2);
204
205   /* append edition1 to the TOC */
206   toc->entries = g_list_append (toc->entries, ed);
207   fail_unless (g_list_length (toc->entries) == 1);
208
209   /* test gst_toc_entry_find() */
210   ed = NULL;
211   ed = gst_toc_find_entry (toc, ENTRY_ED1);
212
213   fail_if (ed == NULL);
214
215   CHECK_TOC_ENTRY (ed, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
216
217   /* test info GstStructure */
218   gst_toc_entry_set_start_stop (ch, 100, 1000);
219   fail_if (!gst_toc_entry_get_start_stop (ch, &start, &stop));
220   fail_unless (start == 100);
221   fail_unless (stop == 1000);
222
223   /* create edition2 */
224   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);
225   fail_if (ed == NULL);
226   gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
227       ENTRY_TAG, NULL);
228   structure =
229       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
230       NULL);
231   gst_structure_set (ed->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
232   gst_structure_free (structure);
233
234   CHECK_TOC_ENTRY (ed, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);
235
236   /* create chapter3 */
237   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);
238   fail_if (ch == NULL);
239   gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
240       ENTRY_TAG, NULL);
241   structure =
242       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
243       NULL);
244   gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
245   gst_structure_free (structure);
246
247   CHECK_TOC_ENTRY (ch, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);
248
249   /* create subchapter1 */
250   subch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
251   fail_if (subch == NULL);
252   gst_tag_list_add (subch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
253       ENTRY_TAG, NULL);
254   structure =
255       gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
256       NULL);
257   gst_structure_set (subch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure,
258       NULL);
259   gst_structure_free (structure);
260
261   CHECK_TOC_ENTRY (subch, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
262
263   /* append subchapter1 to chapter3 */
264   ch->subentries = g_list_append (ch->subentries, subch);
265   fail_unless (g_list_length (ch->subentries) == 1);
266
267   /* append chapter3 to edition2 */
268   ed->subentries = g_list_append (ed->subentries, ch);
269   fail_unless (g_list_length (ed->subentries) == 1);
270
271   /* finally append edition2 to the TOC */
272   toc->entries = g_list_append (toc->entries, ed);
273   fail_unless (g_list_length (toc->entries) == 2);
274
275   /* test gst_toc_copy() */
276   test_toc = gst_toc_copy (toc);
277   fail_if (test_toc == NULL);
278   CHECK_TOC (test_toc);
279   gst_toc_unref (test_toc);
280   test_toc = NULL;
281
282   /* check TOC event handling */
283   event = gst_event_new_toc (toc, TRUE);
284   fail_if (event == NULL);
285   fail_unless (event->type == GST_EVENT_TOC);
286   ASSERT_MINI_OBJECT_REFCOUNT (GST_MINI_OBJECT (event), "GstEvent", 1);
287
288   gst_event_parse_toc (event, &test_toc, &updated);
289   fail_unless (updated == TRUE);
290   fail_if (test_toc == NULL);
291   CHECK_TOC (test_toc);
292   gst_toc_unref (test_toc);
293   gst_event_unref (event);
294   updated = FALSE;
295   test_toc = NULL;
296
297   /* check TOC message handling */
298   message = gst_message_new_toc (NULL, toc, TRUE);
299   fail_if (message == NULL);
300   fail_unless (message->type == GST_MESSAGE_TOC);
301   ASSERT_MINI_OBJECT_REFCOUNT (GST_MINI_OBJECT (message), "GstMessage", 1);
302
303   gst_message_parse_toc (message, &test_toc, &updated);
304   fail_unless (updated == TRUE);
305   fail_if (test_toc == NULL);
306   CHECK_TOC (test_toc);
307   gst_toc_unref (test_toc);
308   gst_message_unref (message);
309   test_toc = NULL;
310
311   /* check TOC select event handling */
312   event = gst_event_new_toc_select (TEST_UID);
313   fail_if (event == NULL);
314   fail_unless (event->type == GST_EVENT_TOC_SELECT);
315   ASSERT_MINI_OBJECT_REFCOUNT (GST_MINI_OBJECT (event), "GstEvent", 1);
316
317   gst_event_parse_toc_select (event, &uid);
318   fail_unless_equals_string (uid, TEST_UID);
319   gst_event_unref (event);
320   g_free (uid);
321
322   /* check TOC query handling */
323   query = gst_query_new_toc ();
324   fail_if (query == NULL);
325   gst_query_set_toc (query, toc, TEST_UID);
326   fail_unless (query->type == GST_QUERY_TOC);
327   ASSERT_MINI_OBJECT_REFCOUNT (GST_MINI_OBJECT (query), "GstQuery", 1);
328
329   gst_query_parse_toc (query, &test_toc, &uid);
330   fail_unless_equals_string (uid, TEST_UID);
331   fail_if (test_toc == NULL);
332   CHECK_TOC (test_toc);
333   gst_toc_unref (test_toc);
334   gst_query_unref (query);
335   g_free (uid);
336
337   /* FIXME: toc validation / verification should probably be done on the fly
338    * while creating it, and not when putting the toc in events or messages ? */
339 #if 0
340   /* that's wrong code, we should fail */
341   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH4);
342   toc->entries = g_list_prepend (toc->entries, ch);
343   ASSERT_CRITICAL (message = gst_message_new_toc (NULL, toc, TRUE));
344
345   /* and yet another one */
346   toc->entries = g_list_remove (toc->entries, ch);
347   gst_toc_entry_unref (ch);
348   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED3);
349   ch = (GstTocEntry *) (toc->entries->data);
350   ch->subentries = g_list_prepend (ch->subentries, ed);
351   ASSERT_WARNING (message = gst_message_new_toc (NULL, toc, TRUE));
352 #endif
353
354   gst_toc_unref (toc);
355 }
356
357 GST_END_TEST;
358
359 static Suite *
360 gst_toc_suite (void)
361 {
362   Suite *s = suite_create ("GstToc");
363   TCase *tc_chain = tcase_create ("general");
364
365   suite_add_tcase (s, tc_chain);
366   tcase_add_test (tc_chain, test_serializing);
367
368   return s;
369 }
370
371 GST_CHECK_MAIN (gst_toc);