markerlist: implement GESMarkerList
[platform/upstream/gst-editing-services.git] / tests / check / ges / markerlist.c
1 /* GStreamer Editing Services
2  *
3  * Copyright (C) <2019> Mathieu Duponchelle <mathieu@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "test-utils.h"
22 #include <ges/ges.h>
23 #include <gst/check/gstcheck.h>
24
25 GST_START_TEST (test_add)
26 {
27   GESMarkerList *markerlist;
28   GESMarker *marker;
29   ges_init ();
30
31   markerlist = ges_marker_list_new ();
32   marker = ges_marker_list_add (markerlist, 42);
33
34   ASSERT_OBJECT_REFCOUNT (marker, "marker list", 1);
35
36   g_object_ref (marker);
37
38   ASSERT_OBJECT_REFCOUNT (marker, "marker list + local ref", 2);
39
40   g_object_unref (markerlist);
41
42   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
43
44   g_object_unref (marker);
45
46   ges_deinit ();
47 }
48
49 GST_END_TEST;
50
51 GST_START_TEST (test_remove)
52 {
53   GESMarkerList *markerlist;
54   GESMarker *marker;
55   ges_init ();
56
57   markerlist = ges_marker_list_new ();
58   marker = ges_marker_list_add (markerlist, 42);
59
60   g_object_ref (marker);
61
62   fail_unless_equals_int (ges_marker_list_size (markerlist), 1);
63
64   fail_unless (ges_marker_list_remove (markerlist, marker));
65   fail_unless_equals_int (ges_marker_list_size (markerlist), 0);
66
67   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
68
69   fail_if (ges_marker_list_remove (markerlist, marker));
70
71   g_object_unref (marker);
72
73   g_object_unref (markerlist);
74
75   ges_deinit ();
76 }
77
78 GST_END_TEST;
79
80
81 static void
82 marker_added_cb (GESMarkerList * mlist, GstClockTime position,
83     GESMarker * marker, gboolean * called)
84 {
85   fail_unless_equals_int (position, 42);
86
87   ASSERT_OBJECT_REFCOUNT (marker, "local ref + signal", 2);
88   *called = TRUE;
89 }
90
91 GST_START_TEST (test_signal_marker_added)
92 {
93   GESMarkerList *mlist;
94   GESMarker *marker;
95   gboolean called = FALSE;
96
97   ges_init ();
98
99   mlist = ges_marker_list_new ();
100   g_signal_connect (mlist, "marker-added", G_CALLBACK (marker_added_cb),
101       &called);
102   marker = ges_marker_list_add (mlist, 42);
103   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
104   fail_unless (called == TRUE);
105   g_signal_handlers_disconnect_by_func (mlist, marker_added_cb, &called);
106
107   g_object_unref (mlist);
108
109   ges_deinit ();
110 }
111
112 GST_END_TEST;
113
114
115 static void
116 marker_removed_cb (GESMarkerList * mlist, GESMarker * marker, gboolean * called)
117 {
118   ASSERT_OBJECT_REFCOUNT (marker, "local ref + signal", 2);
119   *called = TRUE;
120 }
121
122 GST_START_TEST (test_signal_marker_removed)
123 {
124   GESMarkerList *mlist;
125   GESMarker *marker;
126   gboolean called = FALSE;
127
128   ges_init ();
129
130   mlist = ges_marker_list_new ();
131   marker = ges_marker_list_add (mlist, 42);
132
133   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
134
135   g_signal_connect (mlist, "marker-removed", G_CALLBACK (marker_removed_cb),
136       &called);
137
138   fail_unless (ges_marker_list_remove (mlist, marker));
139
140   fail_unless (called == TRUE);
141
142   g_signal_handlers_disconnect_by_func (mlist, marker_removed_cb, &called);
143
144   g_object_unref (mlist);
145
146   ges_deinit ();
147 }
148
149 GST_END_TEST;
150
151
152 static void
153 marker_moved_cb (GESMarkerList * mlist, GstClockTime position,
154     GESMarker * marker, gboolean * called)
155 {
156   fail_unless_equals_int (position, 42);
157
158   ASSERT_OBJECT_REFCOUNT (marker, "local ref + signal", 2);
159   *called = TRUE;
160 }
161
162 GST_START_TEST (test_signal_marker_moved)
163 {
164   GESMarkerList *mlist;
165   GESMarker *marker;
166   gboolean called = FALSE;
167
168   ges_init ();
169
170   mlist = ges_marker_list_new ();
171   g_signal_connect (mlist, "marker-moved", G_CALLBACK (marker_moved_cb),
172       &called);
173
174   marker = ges_marker_list_add (mlist, 10);
175   ASSERT_OBJECT_REFCOUNT (marker, "local ref", 1);
176
177   fail_unless (ges_marker_list_move (mlist, marker, 42));
178
179   fail_unless (called == TRUE);
180
181   g_signal_handlers_disconnect_by_func (mlist, marker_moved_cb, &called);
182
183   g_object_unref (mlist);
184
185   ges_deinit ();
186 }
187
188 GST_END_TEST;
189
190
191 GST_START_TEST (test_get_markers)
192 {
193   GESMarkerList *markerlist;
194   GESMarker *marker1, *marker2, *marker3, *marker4;
195   GList *markers;
196
197   ges_init ();
198
199   markerlist = ges_marker_list_new ();
200   marker1 = ges_marker_list_add (markerlist, 0);
201   marker2 = ges_marker_list_add (markerlist, 10);
202   marker3 = ges_marker_list_add (markerlist, 20);
203   marker4 = ges_marker_list_add (markerlist, 30);
204
205   markers = ges_marker_list_get_markers (markerlist);
206
207   ASSERT_OBJECT_REFCOUNT (marker1, "local ref + markers", 2);
208   ASSERT_OBJECT_REFCOUNT (marker2, "local ref + markers", 2);
209   ASSERT_OBJECT_REFCOUNT (marker3, "local ref + markers", 2);
210   ASSERT_OBJECT_REFCOUNT (marker4, "local ref + markers", 2);
211
212   fail_unless (g_list_index (markers, marker1) == 0);
213   fail_unless (g_list_index (markers, marker2) == 1);
214   fail_unless (g_list_index (markers, marker3) == 2);
215   fail_unless (g_list_index (markers, marker4) == 3);
216
217   g_list_foreach (markers, (GFunc) gst_object_unref, NULL);
218   g_list_free (markers);
219
220   g_object_unref (markerlist);
221
222   ges_deinit ();
223 }
224
225 GST_END_TEST;
226
227
228 GST_START_TEST (test_move_marker)
229 {
230   GESMarkerList *markerlist;
231   GESMarker *marker_a, *marker_b;
232   GstClockTime position;
233   GList *range;
234
235   ges_init ();
236
237   markerlist = ges_marker_list_new ();
238
239   marker_a = ges_marker_list_add (markerlist, 10);
240   marker_b = ges_marker_list_add (markerlist, 30);
241
242   fail_unless (ges_marker_list_move (markerlist, marker_a, 20));
243
244   g_object_get (marker_a, "position", &position, NULL);
245   fail_unless_equals_int (position, 20);
246
247   range = ges_marker_list_get_markers (markerlist);
248
249   fail_unless (g_list_index (range, marker_a) == 0);
250   fail_unless (g_list_index (range, marker_b) == 1);
251
252   g_list_foreach (range, (GFunc) gst_object_unref, NULL);
253   g_list_free (range);
254
255   fail_unless (ges_marker_list_move (markerlist, marker_a, 35));
256
257   range = ges_marker_list_get_markers (markerlist);
258
259   fail_unless (g_list_index (range, marker_b) == 0);
260   fail_unless (g_list_index (range, marker_a) == 1);
261
262   g_list_foreach (range, (GFunc) gst_object_unref, NULL);
263   g_list_free (range);
264
265   fail_unless (ges_marker_list_move (markerlist, marker_a, 30));
266
267   g_object_get (marker_a, "position", &position, NULL);
268   fail_unless_equals_int (position, 30);
269
270   g_object_get (marker_b, "position", &position, NULL);
271   fail_unless_equals_int (position, 30);
272
273   fail_unless_equals_int (ges_marker_list_size (markerlist), 2);
274
275   ges_marker_list_remove (markerlist, marker_a);
276
277   fail_unless (!ges_marker_list_move (markerlist, marker_a, 20));
278
279   g_object_unref (markerlist);
280
281   ges_deinit ();
282 }
283
284 GST_END_TEST;
285
286 GST_START_TEST (test_serialize_deserialize)
287 {
288   GESMarkerList *markerlist1, *markerlist2;
289   gchar *metas1, *metas2;
290   GESTimeline *timeline1, *timeline2;
291
292   ges_init ();
293
294   timeline1 = ges_timeline_new_audio_video ();
295
296   markerlist1 = ges_marker_list_new ();
297   ges_marker_list_add (markerlist1, 0);
298   ges_marker_list_add (markerlist1, 10);
299
300   ASSERT_OBJECT_REFCOUNT (markerlist1, "local ref", 1);
301
302   fail_unless (ges_meta_container_set_marker_list (GES_META_CONTAINER
303           (timeline1), "ges-test", markerlist1));
304
305   ASSERT_OBJECT_REFCOUNT (markerlist1, "GstStructure + local ref", 2);
306
307   markerlist2 =
308       ges_meta_container_get_marker_list (GES_META_CONTAINER (timeline1),
309       "ges-test");
310
311   fail_unless (markerlist1 == markerlist2);
312
313   ASSERT_OBJECT_REFCOUNT (markerlist1, "GstStructure + getter + local ref", 3);
314
315   g_object_unref (markerlist2);
316
317   ASSERT_OBJECT_REFCOUNT (markerlist1, "GstStructure + local ref", 2);
318
319   timeline2 = ges_timeline_new_audio_video ();
320
321   metas1 = ges_meta_container_metas_to_string (GES_META_CONTAINER (timeline1));
322   ges_meta_container_add_metas_from_string (GES_META_CONTAINER (timeline2),
323       metas1);
324   metas2 = ges_meta_container_metas_to_string (GES_META_CONTAINER (timeline2));
325
326   fail_unless_equals_string (metas1, metas2);
327
328   g_free (metas1);
329   g_free (metas2);
330
331   fail_unless (ges_meta_container_set_marker_list (GES_META_CONTAINER
332           (timeline1), "ges-test", NULL));
333
334   ASSERT_OBJECT_REFCOUNT (markerlist1, "local ref", 1);
335
336   g_object_unref (markerlist1);
337   g_object_unref (timeline1);
338   g_object_unref (timeline2);
339
340   ges_deinit ();
341 }
342
343 GST_END_TEST;
344
345 static Suite *
346 ges_suite (void)
347 {
348   Suite *s = suite_create ("ges-marker-list");
349   TCase *tc_chain = tcase_create ("markerlist");
350
351   suite_add_tcase (s, tc_chain);
352
353   tcase_add_test (tc_chain, test_add);
354   tcase_add_test (tc_chain, test_remove);
355   tcase_add_test (tc_chain, test_signal_marker_added);
356   tcase_add_test (tc_chain, test_signal_marker_removed);
357   tcase_add_test (tc_chain, test_signal_marker_moved);
358   tcase_add_test (tc_chain, test_get_markers);
359   tcase_add_test (tc_chain, test_move_marker);
360   tcase_add_test (tc_chain, test_serialize_deserialize);
361
362   return s;
363 }
364
365 GST_CHECK_MAIN (ges);