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