toc: Make structures opaque and clean up function names and fields a bit
[platform/upstream/gstreamer.git] / tests / check / gst / gsttocsetter.c
1 /* GStreamer GstTocSetter interface unit tests
2  * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <gst/check/gstcheck.h>
21 #include <gst/gst.h>
22 #include <string.h>
23
24 #define ENTRY_ED1       "/edition1"
25 #define ENTRY_ED2       "/edition2"
26 #define ENTRY_ED3       "test-edition"
27
28 #define ENTRY_CH1       "/edition1/chapter1"
29 #define ENTRY_CH2       "/edition1/chapter2"
30 #define ENTRY_CH3       "/edition2/chapter3"
31 #define ENTRY_CH4       "/test-chapter"
32
33 #define ENTRY_SUB1      "/edition2/chapter3/subchapter1"
34
35 #define ENTRY_TAG       "EntryTag"
36 #define TOC_TAG         "TocTag"
37
38 #define CHECK_TOC_ENTRY(entry_c,type_c,uid_c)                            \
39 {                                                                        \
40   GstTagList *tags;                                                      \
41   gchar *tag_c;                                                          \
42                                                                          \
43   fail_unless_equals_string (gst_toc_entry_get_uid (entry_c), uid_c);    \
44   fail_unless (gst_toc_entry_get_entry_type (entry_c) == type_c);        \
45                                                                          \
46   tags = gst_toc_entry_get_tags (entry_c);                               \
47   fail_unless (tags != NULL);                                            \
48   fail_unless (gst_tag_list_get_string (tags,                            \
49                GST_TAG_TITLE, &tag_c));                                  \
50   fail_unless_equals_string (tag_c, ENTRY_TAG);                          \
51   g_free (tag_c);                                                        \
52 }
53
54 #define CHECK_TOC(toc_t)                                                 \
55 {                                                                        \
56   GstTocEntry *entry_t, *subentry_t;                                     \
57   GstTagList *tags;                                                      \
58   GList *entries, *subentries, *subsubentries;                           \
59   gchar *tag_t;                                                          \
60                                                                          \
61   /* check TOC */                                                        \
62   tags = gst_toc_get_tags (toc_t);                                       \
63   fail_unless (tags != NULL);                                            \
64   fail_unless (gst_tag_list_get_string (tags,                            \
65                GST_TAG_TITLE, &tag_t));                                  \
66   fail_unless_equals_string (tag_t, TOC_TAG);                            \
67   g_free (tag_t);                                                        \
68                                                                          \
69   entries = gst_toc_get_entries (toc_t);                                 \
70   fail_unless_equals_int (g_list_length (entries), 2);                   \
71   /* check edition1 */                                                   \
72   entry_t = g_list_nth_data (entries, 0);                                \
73   fail_if (entry_t == NULL);                                             \
74   subentries = gst_toc_entry_get_sub_entries (entry_t);                  \
75   fail_unless_equals_int (g_list_length (subentries), 2);                \
76   CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);      \
77   /* check chapter1 */                                                   \
78   subentry_t = g_list_nth_data (subentries, 0);                          \
79   fail_if (subentry_t == NULL);                                          \
80   subsubentries = gst_toc_entry_get_sub_entries (subentry_t);            \
81   fail_unless_equals_int (g_list_length (subsubentries), 0);             \
82   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);   \
83   /* check chapter2 */                                                   \
84   subentry_t = g_list_nth_data (subentries, 1);                          \
85   fail_if (subentry_t == NULL);                                          \
86   subsubentries = gst_toc_entry_get_sub_entries (subentry_t);            \
87   fail_unless_equals_int (g_list_length (subsubentries), 0);             \
88   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);   \
89   /* check edition2 */                                                   \
90   entry_t = g_list_nth_data (entries, 1);                                \
91   fail_if (entry_t == NULL);                                             \
92   subentries = gst_toc_entry_get_sub_entries (entry_t);                  \
93   fail_unless_equals_int (g_list_length (subentries), 1);                \
94   CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);      \
95   /* check chapter3 */                                                   \
96   subentry_t = g_list_nth_data (subentries, 0);                          \
97   fail_if (subentry_t == NULL);                                          \
98   subsubentries = gst_toc_entry_get_sub_entries (subentry_t);            \
99   fail_unless_equals_int (g_list_length (subsubentries), 1);             \
100   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);   \
101   /* check subchapter1 */                                                \
102   subentry_t = g_list_nth_data (subentries, 0);                          \
103   fail_if (subentry_t == NULL);                                          \
104   subsubentries = gst_toc_entry_get_sub_entries (subentry_t);            \
105   fail_unless_equals_int (g_list_length (subsubentries), 0);             \
106   CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);  \
107 }
108
109 /* some minimal GstTocSetter object */
110 #define GST_TYPE_DUMMY_ENC gst_dummy_enc_get_type()
111
112 typedef GstElement GstDummyEnc;
113 typedef GstElementClass GstDummyEncClass;
114
115 GType gst_dummy_enc_get_type (void);
116 G_DEFINE_TYPE_WITH_CODE (GstDummyEnc, gst_dummy_enc,
117     GST_TYPE_ELEMENT, G_IMPLEMENT_INTERFACE (GST_TYPE_TOC_SETTER, NULL));
118
119 static void
120 gst_dummy_enc_class_init (GstDummyEncClass * klass)
121 {
122 }
123
124 static void
125 gst_dummy_enc_init (GstDummyEnc * enc)
126 {
127 }
128
129 static GstToc *
130 create_toc (void)
131 {
132   GstToc *toc;
133   GstTocEntry *ed, *ch, *subch;
134   GstTagList *tags;
135
136   toc = gst_toc_new ();
137   tags = gst_tag_list_new_empty ();
138   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, TOC_TAG, NULL);
139   gst_toc_set_tags (toc, tags);
140
141   /* create edition1 */
142   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
143   tags = gst_tag_list_new_empty ();
144   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
145   gst_toc_entry_set_tags (ed, tags);
146
147   /* append chapter1 to edition1 */
148   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);
149   tags = gst_tag_list_new_empty ();
150   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
151   gst_toc_entry_set_tags (ch, tags);
152
153   gst_toc_entry_append_sub_entry (ed, ch);
154
155   /* append chapter2 to edition1 */
156   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);
157   tags = gst_tag_list_new_empty ();
158   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
159   gst_toc_entry_set_tags (ch, tags);
160
161   gst_toc_entry_append_sub_entry (ed, ch);
162
163   /* append edition1 to the TOC */
164   gst_toc_append_entry (toc, ed);
165
166   /* create edition2 */
167   ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);
168   tags = gst_tag_list_new_empty ();
169   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
170   gst_toc_entry_set_tags (ed, tags);
171
172   /* create chapter3 */
173   ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);
174   tags = gst_tag_list_new_empty ();
175   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
176   gst_toc_entry_set_tags (ch, tags);
177
178   /* create subchapter1 */
179   subch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
180   tags = gst_tag_list_new_empty ();
181   gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
182   gst_toc_entry_set_tags (subch, tags);
183
184   /* append subchapter1 to chapter3 */
185   gst_toc_entry_append_sub_entry (ch, subch);
186
187   /* append chapter3 to edition2 */
188   gst_toc_entry_append_sub_entry (ed, ch);
189
190   /* finally append edition2 to the TOC */
191   gst_toc_append_entry (toc, ed);
192
193   return toc;
194 }
195
196 GST_START_TEST (test_set)
197 {
198   GstToc *toc;
199 #if 0
200   GstTocEntry *entry, *ed;
201 #endif
202   GstTocSetter *setter;
203   GstElement *enc;
204
205   enc = g_object_new (GST_TYPE_DUMMY_ENC, NULL);
206   fail_unless (enc != NULL);
207
208   setter = GST_TOC_SETTER (enc);
209
210   toc = create_toc ();
211   fail_unless (toc != NULL);
212
213   gst_toc_setter_set_toc (setter, toc);
214
215   gst_toc_unref (toc);
216   toc = gst_toc_setter_get_toc (setter);
217
218   CHECK_TOC (toc);
219
220 #if 0
221   /* test entry adding into the root TOC */
222   entry = g_list_last (toc->entries)->data;
223   toc->entries = g_list_remove (toc->entries, entry);
224
225   gst_toc_setter_set_toc (setter, toc);
226   gst_toc_setter_add_toc_entry (setter, "0", entry);
227
228   gst_toc_unref (toc);
229   gst_toc_entry_unref (entry);
230   toc = gst_toc_setter_get_toc (setter);
231
232   CHECK_TOC (toc);
233 #endif
234
235 #if 0
236   /* test entry adding into the arbitrary entry */
237   entry = gst_toc_find_entry (toc, ENTRY_CH2);
238   fail_if (entry == NULL);
239
240   ed = toc->entries->data;
241   ed->subentries = g_list_remove (ed->subentries, entry);
242
243   gst_toc_setter_add_toc_entry (setter, ed->uid, entry);
244
245   CHECK_TOC (toc);
246 #endif
247
248   gst_toc_unref (toc);
249   gst_toc_setter_reset (setter);
250   toc = gst_toc_setter_get_toc (setter);
251
252   fail_unless (toc == NULL);
253
254   g_object_unref (enc);
255 }
256
257 GST_END_TEST static int spin_and_wait = 1;
258 static int threads_running = 0;
259
260 #define THREADS_TEST_SECONDS 1.5
261
262 static gpointer
263 test_threads_thread_func1 (gpointer data)
264 {
265   GstToc *toc;
266   GstTocSetter *setter = GST_TOC_SETTER (data);
267   GTimer *timer;
268
269   toc = create_toc ();
270   timer = g_timer_new ();
271
272   g_atomic_int_inc (&threads_running);
273   while (g_atomic_int_get (&spin_and_wait))
274     g_usleep (0);
275
276   GST_INFO ("Go!");
277   g_timer_start (timer);
278
279   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
280     gst_toc_setter_set_toc (setter, toc);
281
282   gst_toc_unref (toc);
283   g_timer_destroy (timer);
284   GST_INFO ("Done");
285
286   return NULL;
287 }
288
289 static gpointer
290 test_threads_thread_func2 (gpointer data)
291 {
292   GstToc *toc;
293   GstTocSetter *setter = GST_TOC_SETTER (data);
294   GTimer *timer;
295
296   toc = create_toc ();
297   timer = g_timer_new ();
298
299   g_atomic_int_inc (&threads_running);
300   while (g_atomic_int_get (&spin_and_wait))
301     g_usleep (0);
302
303   GST_INFO ("Go!");
304   g_timer_start (timer);
305
306   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
307     gst_toc_setter_set_toc (setter, toc);
308
309   gst_toc_unref (toc);
310   g_timer_destroy (timer);
311   GST_INFO ("Done");
312
313   return NULL;
314 }
315
316 static gpointer
317 test_threads_thread_func3 (gpointer data)
318 {
319   GstTocSetter *setter = GST_TOC_SETTER (data);
320   GTimer *timer;
321
322   timer = g_timer_new ();
323
324   g_atomic_int_inc (&threads_running);
325   while (g_atomic_int_get (&spin_and_wait))
326     g_usleep (0);
327
328   GST_INFO ("Go!");
329   g_timer_start (timer);
330
331   while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS) {
332     gst_toc_setter_reset (setter);
333   }
334
335   g_timer_destroy (timer);
336   GST_INFO ("Done");
337
338   return NULL;
339 }
340
341 GST_START_TEST (test_threads)
342 {
343   GstTocSetter *setter;
344   GThread *threads[3];
345
346   setter = GST_TOC_SETTER (g_object_new (GST_TYPE_DUMMY_ENC, NULL));
347
348   spin_and_wait = TRUE;
349   threads[0] = g_thread_create (test_threads_thread_func1, setter, TRUE, NULL);
350   threads[1] = g_thread_create (test_threads_thread_func2, setter, TRUE, NULL);
351   threads[2] = g_thread_create (test_threads_thread_func3, setter, TRUE, NULL);
352
353   while (g_atomic_int_get (&threads_running) < 3)
354     g_usleep (10);
355
356   g_atomic_int_set (&spin_and_wait, FALSE);
357
358   g_thread_join (threads[0]);
359   g_thread_join (threads[1]);
360   g_thread_join (threads[2]);
361
362   g_object_unref (G_OBJECT (setter));
363 }
364
365 GST_END_TEST static Suite *
366 gst_toc_setter_suite (void)
367 {
368   Suite *s = suite_create ("GstTocSetter");
369   TCase *tc_chain = tcase_create ("general");
370
371   suite_add_tcase (s, tc_chain);
372   tcase_add_test (tc_chain, test_set);
373   tcase_add_test (tc_chain, test_threads);
374
375   return s;
376 }
377
378 GST_CHECK_MAIN (gst_toc_setter);