summaryrefslogtreecommitdiff
path: root/tests/check/gst/gsttoc.c
blob: 3788b1657034501eec7bb65a14eaec474f799863 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* GStreamer
 *
 * unit test for GstToc
 *
 * Copyright (C) 2010, 2012 Alexander Saprykin <xelfium@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*  -------  TOC  -------
 *           /  \
 *   edition1    edition2
 *   |           |
 *   -chapter1   -chapter3
 *   -chapter2    |
 *                -subchapter1
 */

#include <gst/check/gstcheck.h>

#define ENTRY_ED1       "/edition1"
#define ENTRY_ED2       "/edition2"
#define ENTRY_ED3       "test-edition"

#define ENTRY_CH1       "/edition1/chapter1"
#define ENTRY_CH2       "/edition1/chapter2"
#define ENTRY_CH3       "/edition2/chapter3"
#define ENTRY_CH4       "/test-chapter"

#define ENTRY_SUB1      "/edition2/chapter3/subchapter1"

#define ENTRY_TAG       "EntryTag"
#define TOC_TAG         "TocTag"

#define TEST_UID        "129537542"
#define INFO_NAME	"gst-toc-check"
#define INFO_FIELD      "info-test"
#define INFO_TEXT_EN    "info-text-entry"
#define INFO_TEXT_TOC   "info-text-toc"

#define CHECK_TOC_ENTRY(entry_c,type_c,uid_c)                            \
{                                                                        \
  gchar *tag_c;                                                          \
  const GValue *val;                                                     \
  GstStructure *struct_c;                                                \
                                                                         \
  fail_unless_equals_string (entry_c->uid, uid_c);                       \
  fail_unless (entry_c->type == type_c);                                 \
  fail_unless (entry_c->tags != NULL);                                   \
  fail_unless (entry_c->pads == NULL);                                   \
                                                                         \
  fail_unless (entry_c->info != NULL);                                   \
  gst_structure_get (entry_c->info, INFO_NAME, GST_TYPE_STRUCTURE,       \
      &struct_c, NULL);                                                  \
  fail_unless (struct_c != NULL);                                        \
  val = gst_structure_get_value (struct_c, INFO_FIELD);                  \
  fail_unless (val != NULL);                                             \
  fail_unless_equals_string (g_value_get_string (val), INFO_TEXT_EN);    \
                                                                         \
  fail_unless (gst_tag_list_get_string (entry_c->tags,                   \
      GST_TAG_TITLE, &tag_c));                                           \
  fail_unless_equals_string (tag_c, ENTRY_TAG);                          \
  g_free (tag_c);                                                        \
  gst_structure_free (struct_c);                                         \
}

#define CHECK_TOC(toc_t)                                                 \
{                                                                        \
  GstTocEntry *entry_t, *subentry_t;                                     \
  gchar *tag_t;                                                          \
  const GValue *val;                                                     \
  GstStructure *struct_toc;                                              \
                                                                         \
  /* check TOC */                                                        \
  fail_unless (g_list_length (toc_t->entries) == 2);                     \
  fail_unless (toc_t->tags != NULL);                                     \
  fail_unless (gst_tag_list_get_string (toc_t->tags,                     \
      GST_TAG_TITLE, &tag_t));                                           \
  fail_unless_equals_string (tag_t, TOC_TAG);                            \
  g_free (tag_t);                                                        \
                                                                         \
  fail_unless (toc_t->info != NULL);                                     \
  gst_structure_get (toc_t->info, INFO_NAME, GST_TYPE_STRUCTURE,         \
      &struct_toc, NULL);                                                \
  fail_unless (struct_toc != NULL);                                      \
  val = gst_structure_get_value (struct_toc, INFO_FIELD);                \
  fail_unless (val != NULL);                                             \
  fail_unless_equals_string (g_value_get_string (val), INFO_TEXT_TOC);   \
  gst_structure_free (struct_toc);                                       \
                                                                         \
  /* check edition1 */                                                   \
  entry_t = g_list_nth_data (toc_t->entries, 0);                         \
  fail_if (entry_t == NULL);                                             \
  fail_unless (g_list_length (entry_t->subentries) == 2);                \
  CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);      \
  /* check chapter1 */                                                   \
  subentry_t = g_list_nth_data (entry_t->subentries, 0);                 \
  fail_if (subentry_t == NULL);                                          \
  fail_unless (g_list_length (subentry_t->subentries) == 0);             \
  CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);   \
  /* check chapter2 */                                                   \
  subentry_t = g_list_nth_data (entry_t->subentries, 1);                 \
  fail_if (subentry_t == NULL);                                          \
  fail_unless (g_list_length (subentry_t->subentries) == 0);             \
  CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);   \
  /* check edition2 */                                                   \
  entry_t = g_list_nth_data (toc_t->entries, 1);                         \
  fail_if (entry_t == NULL);                                             \
  fail_unless (g_list_length (entry_t->subentries) == 1);                \
  CHECK_TOC_ENTRY (entry_t, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);      \
  /* check chapter3 */                                                   \
  subentry_t = g_list_nth_data (entry_t->subentries, 0);                 \
  fail_if (subentry_t == NULL);                                          \
  fail_unless (g_list_length (subentry_t->subentries) == 1);             \
  CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);   \
  /* check subchapter1 */                                                \
  subentry_t = g_list_nth_data (subentry_t->subentries, 0);              \
  fail_if (subentry_t == NULL);                                          \
  fail_unless (g_list_length (subentry_t->subentries) == 0);             \
  CHECK_TOC_ENTRY (subentry_t, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);  \
}

GST_START_TEST (test_serializing)
{
  GstStructure *structure;
  GstToc *toc, *test_toc = NULL;
  GstTocEntry *ed, *ch, *subch;
  GstEvent *event;
  GstMessage *message;
  GstQuery *query;
  gboolean updated;
  gchar *uid;
  gint64 start = -1, stop = -1;

  toc = gst_toc_new ();
  fail_if (toc == NULL);
  gst_tag_list_add (toc->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
      TOC_TAG, NULL);
  structure =
      gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_TOC,
      NULL);
  gst_structure_set (toc->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
  gst_structure_free (structure);

  /* create edition1 */
  ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);
  fail_if (ed == NULL);
  gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
      ENTRY_TAG, NULL);
  structure =
      gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
      NULL);
  gst_structure_set (ed->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
  gst_structure_free (structure);

  CHECK_TOC_ENTRY (ed, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);

  /* append chapter1 to edition1 */
  ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);
  fail_if (ch == NULL);
  gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
      ENTRY_TAG, NULL);
  structure =
      gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
      NULL);
  gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
  gst_structure_free (structure);

  CHECK_TOC_ENTRY (ch, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH1);

  ed->subentries = g_list_append (ed->subentries, ch);
  fail_unless (g_list_length (ed->subentries) == 1);

  /* append chapter2 to edition1 */
  ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);
  fail_if (ch == NULL);
  gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
      ENTRY_TAG, NULL);
  structure =
      gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
      NULL);
  gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
  gst_structure_free (structure);

  CHECK_TOC_ENTRY (ch, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH2);

  ed->subentries = g_list_append (ed->subentries, ch);
  fail_unless (g_list_length (ed->subentries) == 2);

  /* append edition1 to the TOC */
  toc->entries = g_list_append (toc->entries, ed);
  fail_unless (g_list_length (toc->entries) == 1);

  /* test gst_toc_entry_find() */
  ed = NULL;
  ed = gst_toc_find_entry (toc, ENTRY_ED1);

  fail_if (ed == NULL);

  CHECK_TOC_ENTRY (ed, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED1);

  /* test info GstStructure */
  gst_toc_entry_set_start_stop (ch, 100, 1000);
  fail_if (!gst_toc_entry_get_start_stop (ch, &start, &stop));
  fail_unless (start == 100);
  fail_unless (stop == 1000);

  /* create edition2 */
  ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);
  fail_if (ed == NULL);
  gst_tag_list_add (ed->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
      ENTRY_TAG, NULL);
  structure =
      gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
      NULL);
  gst_structure_set (ed->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
  gst_structure_free (structure);

  CHECK_TOC_ENTRY (ed, GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED2);

  /* create chapter3 */
  ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);
  fail_if (ch == NULL);
  gst_tag_list_add (ch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
      ENTRY_TAG, NULL);
  structure =
      gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
      NULL);
  gst_structure_set (ch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure, NULL);
  gst_structure_free (structure);

  CHECK_TOC_ENTRY (ch, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH3);

  /* create subchapter1 */
  subch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);
  fail_if (subch == NULL);
  gst_tag_list_add (subch->tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE,
      ENTRY_TAG, NULL);
  structure =
      gst_structure_new (INFO_NAME, INFO_FIELD, G_TYPE_STRING, INFO_TEXT_EN,
      NULL);
  gst_structure_set (subch->info, INFO_NAME, GST_TYPE_STRUCTURE, structure,
      NULL);
  gst_structure_free (structure);

  CHECK_TOC_ENTRY (subch, GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_SUB1);

  /* append subchapter1 to chapter3 */
  ch->subentries = g_list_append (ch->subentries, subch);
  fail_unless (g_list_length (ch->subentries) == 1);

  /* append chapter3 to edition2 */
  ed->subentries = g_list_append (ed->subentries, ch);
  fail_unless (g_list_length (ed->subentries) == 1);

  /* finally append edition2 to the TOC */
  toc->entries = g_list_append (toc->entries, ed);
  fail_unless (g_list_length (toc->entries) == 2);

  /* test gst_toc_copy() */
  test_toc = gst_toc_copy (toc);
  fail_if (test_toc == NULL);
  CHECK_TOC (test_toc);
  gst_toc_free (test_toc);
  test_toc = NULL;

  /* check TOC event handling */
  event = gst_event_new_toc (toc, TRUE);
  fail_if (event == NULL);
  fail_if (event->structure == NULL);
  fail_unless (event->type == GST_EVENT_TOC);
  ASSERT_MINI_OBJECT_REFCOUNT (GST_MINI_OBJECT (event), "GstEvent", 1);

  gst_event_parse_toc (event, &test_toc, &updated);
  fail_unless (updated == TRUE);
  fail_if (test_toc == NULL);
  CHECK_TOC (test_toc);
  gst_toc_free (test_toc);
  gst_event_unref (event);
  updated = FALSE;
  test_toc = NULL;

  /* check TOC message handling */
  message = gst_message_new_toc (NULL, toc, TRUE);
  fail_if (message == NULL);
  fail_if (event->structure == NULL);
  fail_unless (message->type == GST_MESSAGE_TOC);
  ASSERT_MINI_OBJECT_REFCOUNT (GST_MINI_OBJECT (message), "GstMessage", 1);

  gst_message_parse_toc (message, &test_toc, &updated);
  fail_unless (updated == TRUE);
  fail_if (test_toc == NULL);
  CHECK_TOC (test_toc);
  gst_toc_free (test_toc);
  gst_message_unref (message);
  test_toc = NULL;

  /* check TOC select event handling */
  event = gst_event_new_toc_select (TEST_UID);
  fail_if (event == NULL);
  fail_if (event->structure == NULL);
  fail_unless (event->type == GST_EVENT_TOC_SELECT);
  ASSERT_MINI_OBJECT_REFCOUNT (GST_MINI_OBJECT (event), "GstEvent", 1);

  gst_event_parse_toc_select (event, &uid);
  fail_unless_equals_string (uid, TEST_UID);
  gst_event_unref (event);
  g_free (uid);

  /* check TOC query handling */
  query = gst_query_new_toc ();
  fail_if (query == NULL);
  gst_query_set_toc (query, toc, TEST_UID);
  fail_if (query->structure == NULL);
  fail_unless (query->type == GST_QUERY_TOC);
  ASSERT_MINI_OBJECT_REFCOUNT (GST_MINI_OBJECT (query), "GstQuery", 1);

  gst_query_parse_toc (query, &test_toc, &uid);
  fail_unless_equals_string (uid, TEST_UID);
  fail_if (test_toc == NULL);
  CHECK_TOC (test_toc);
  gst_toc_free (test_toc);
  gst_query_unref (query);
  g_free (uid);

  /* that's wrong code, we should fail */
  ch = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, ENTRY_CH4);
  toc->entries = g_list_prepend (toc->entries, ch);
  ASSERT_CRITICAL (message = gst_message_new_toc (NULL, toc, TRUE));

  /* and yet another one */
  toc->entries = g_list_remove (toc->entries, ch);
  gst_toc_entry_free (ch);
  ed = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, ENTRY_ED3);
  ch = (GstTocEntry *) (toc->entries->data);
  ch->subentries = g_list_prepend (ch->subentries, ed);
  ASSERT_WARNING (message = gst_message_new_toc (NULL, toc, TRUE));

  gst_toc_free (toc);
}

GST_END_TEST;

static Suite *
gst_toc_suite (void)
{
  Suite *s = suite_create ("GstToc");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_serializing);

  return s;
}

GST_CHECK_MAIN (gst_toc);