1 /* GStreamer GstTocSetter interface unit tests
2 * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
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 #define ENTRY_ED1 "/edition1"
25 #define ENTRY_ED2 "/edition2"
26 #define ENTRY_ED3 "test-edition"
28 #define ENTRY_CH1 "/edition1/chapter1"
29 #define ENTRY_CH2 "/edition1/chapter2"
30 #define ENTRY_CH3 "/edition2/chapter3"
31 #define ENTRY_CH4 "/test-chapter"
33 #define ENTRY_SUB1 "/edition2/chapter3/subchapter1"
35 #define ENTRY_TAG "EntryTag"
36 #define TOC_TAG "TocTag"
39 CHECK_TOC_ENTRY (GstTocEntry * entry_c, GstTocEntryType type_c,
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);
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);
56 CHECK_TOC (GstToc * toc_t)
58 GstTocEntry *entry_t, *subentry_t;
60 GList *entries, *subentries, *subsubentries;
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);
73 entries = gst_toc_get_entries (toc_t);
74 fail_unless_equals_int (g_list_length (entries), 2);
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);
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);
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);
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);
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 (subentries, 0);
107 fail_if (subentry_t == NULL);
108 GST_ERROR ("%s", gst_toc_entry_get_uid (subentry_t));
109 subsubentries = gst_toc_entry_get_sub_entries (subentry_t);
110 GST_ERROR ("%p", subsubentries);
111 fail_unless_equals_int (g_list_length (subsubentries), 0);
112 CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
115 /* some minimal GstTocSetter object */
116 #define GST_TYPE_DUMMY_ENC gst_dummy_enc_get_type()
118 typedef GstElement GstDummyEnc;
119 typedef GstElementClass GstDummyEncClass;
121 GType gst_dummy_enc_get_type (void);
122 G_DEFINE_TYPE_WITH_CODE (GstDummyEnc, gst_dummy_enc,
123 GST_TYPE_ELEMENT, G_IMPLEMENT_INTERFACE (GST_TYPE_TOC_SETTER, NULL));
126 gst_dummy_enc_class_init (GstDummyEncClass * klass)
131 gst_dummy_enc_init (GstDummyEnc * enc)
139 GstTocEntry *ed, *ch, *subch;
142 toc = gst_toc_new ();
143 tags = gst_tag_list_new_empty ();
144 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, TOC_TAG, NULL);
145 gst_toc_set_tags (toc, tags);
147 /* create edition1 */
148 ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
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 (ed, tags);
153 /* append chapter1 to edition1 */
154 ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);
155 tags = gst_tag_list_new_empty ();
156 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
157 gst_toc_entry_set_tags (ch, tags);
159 gst_toc_entry_append_sub_entry (ed, ch);
161 /* append chapter2 to edition1 */
162 ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);
163 tags = gst_tag_list_new_empty ();
164 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
165 gst_toc_entry_set_tags (ch, tags);
167 gst_toc_entry_append_sub_entry (ed, ch);
169 /* append edition1 to the TOC */
170 gst_toc_append_entry (toc, ed);
172 /* create edition2 */
173 ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);
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 (ed, tags);
178 /* create chapter3 */
179 ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);
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 (ch, tags);
184 /* create subchapter1 */
185 subch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
186 tags = gst_tag_list_new_empty ();
187 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, ENTRY_TAG, NULL);
188 gst_toc_entry_set_tags (subch, tags);
190 /* append subchapter1 to chapter3 */
191 gst_toc_entry_append_sub_entry (ch, subch);
193 /* append chapter3 to edition2 */
194 gst_toc_entry_append_sub_entry (ed, ch);
196 /* finally append edition2 to the TOC */
197 gst_toc_append_entry (toc, ed);
202 GST_START_TEST (test_set)
206 GstTocEntry *entry, *ed;
208 GstTocSetter *setter;
211 enc = g_object_new (GST_TYPE_DUMMY_ENC, NULL);
212 fail_unless (enc != NULL);
214 setter = GST_TOC_SETTER (enc);
217 fail_unless (toc != NULL);
219 gst_toc_setter_set_toc (setter, toc);
222 toc = gst_toc_setter_get_toc (setter);
227 /* test entry adding into the root TOC */
228 entry = g_list_last (toc->entries)->data;
229 toc->entries = g_list_remove (toc->entries, entry);
231 gst_toc_setter_set_toc (setter, toc);
232 gst_toc_setter_add_toc_entry (setter, "0", entry);
235 gst_toc_entry_unref (entry);
236 toc = gst_toc_setter_get_toc (setter);
242 /* test entry adding into the arbitrary entry */
243 entry = gst_toc_find_entry (toc, ENTRY_CH2);
244 fail_if (entry == NULL);
246 ed = toc->entries->data;
247 ed->subentries = g_list_remove (ed->subentries, entry);
249 gst_toc_setter_add_toc_entry (setter, ed->uid, entry);
255 gst_toc_setter_reset (setter);
256 toc = gst_toc_setter_get_toc (setter);
258 fail_unless (toc == NULL);
260 g_object_unref (enc);
263 GST_END_TEST static int spin_and_wait = 1;
264 static int threads_running = 0;
266 #define THREADS_TEST_SECONDS 1.5
269 test_threads_thread_func1 (gpointer data)
272 GstTocSetter *setter = GST_TOC_SETTER (data);
276 timer = g_timer_new ();
278 g_atomic_int_inc (&threads_running);
279 while (g_atomic_int_get (&spin_and_wait))
283 g_timer_start (timer);
285 while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
286 gst_toc_setter_set_toc (setter, toc);
289 g_timer_destroy (timer);
296 test_threads_thread_func2 (gpointer data)
299 GstTocSetter *setter = GST_TOC_SETTER (data);
303 timer = g_timer_new ();
305 g_atomic_int_inc (&threads_running);
306 while (g_atomic_int_get (&spin_and_wait))
310 g_timer_start (timer);
312 while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS)
313 gst_toc_setter_set_toc (setter, toc);
316 g_timer_destroy (timer);
323 test_threads_thread_func3 (gpointer data)
325 GstTocSetter *setter = GST_TOC_SETTER (data);
328 timer = g_timer_new ();
330 g_atomic_int_inc (&threads_running);
331 while (g_atomic_int_get (&spin_and_wait))
335 g_timer_start (timer);
337 while (g_timer_elapsed (timer, NULL) < THREADS_TEST_SECONDS) {
338 gst_toc_setter_reset (setter);
341 g_timer_destroy (timer);
347 GST_START_TEST (test_threads)
349 GstTocSetter *setter;
352 setter = GST_TOC_SETTER (g_object_new (GST_TYPE_DUMMY_ENC, NULL));
354 spin_and_wait = TRUE;
355 threads[0] = g_thread_create (test_threads_thread_func1, setter, TRUE, NULL);
356 threads[1] = g_thread_create (test_threads_thread_func2, setter, TRUE, NULL);
357 threads[2] = g_thread_create (test_threads_thread_func3, setter, TRUE, NULL);
359 while (g_atomic_int_get (&threads_running) < 3)
362 g_atomic_int_set (&spin_and_wait, FALSE);
364 g_thread_join (threads[0]);
365 g_thread_join (threads[1]);
366 g_thread_join (threads[2]);
368 g_object_unref (G_OBJECT (setter));
371 GST_END_TEST static Suite *
372 gst_toc_setter_suite (void)
374 Suite *s = suite_create ("GstTocSetter");
375 TCase *tc_chain = tcase_create ("general");
377 suite_add_tcase (s, tc_chain);
378 tcase_add_test (tc_chain, test_set);
379 tcase_add_test (tc_chain, test_threads);
384 GST_CHECK_MAIN (gst_toc_setter);