tests: fix indentation
[platform/upstream/gstreamer.git] / tests / check / libs / gstharness.c
1 /*
2  * Tests and examples of GstHarness
3  *
4  * Copyright (C) 2015 Havard Graff <havard@pexip.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23 #include <gst/check/gstharness.h>
24
25 static void
26 create_destroy_element_harness (gpointer data, gpointer user_data)
27 {
28   GstElement *element = user_data;
29   GstHarness *h = gst_harness_new_with_element (element, NULL, NULL);
30   gst_harness_teardown (h);
31 }
32
33 GST_START_TEST (test_harness_element_ref)
34 {
35   GstHarness *h = gst_harness_new ("identity");
36   GstHarnessThread *threads[100];
37   gint i;
38
39   for (i = 0; i < G_N_ELEMENTS (threads); i++)
40     threads[i] = gst_harness_stress_custom_start (h, NULL,
41         create_destroy_element_harness, h->element, 0);
42   for (i = 0; i < G_N_ELEMENTS (threads); i++)
43     gst_harness_stress_thread_stop (threads[i]);
44
45   fail_unless_equals_int (G_OBJECT (h->element)->ref_count, 1);
46
47   gst_harness_teardown (h);
48 }
49
50 GST_END_TEST;
51
52
53 GST_START_TEST (test_src_harness)
54 {
55   GstHarness *h = gst_harness_new ("identity");
56
57   /* add a fakesrc that syncs to the clock and a
58      capsfilter that adds some caps to it */
59   gst_harness_add_src_parse (h,
60       "fakesrc sync=1 ! capsfilter caps=\"mycaps\"", TRUE);
61
62   /* this cranks the clock and transfers the resulting buffer
63      from the src-harness into the identity element */
64   gst_harness_push_from_src (h);
65
66   /* verify that identity outputs a buffer by pulling and unreffing */
67   gst_buffer_unref (gst_harness_pull (h));
68
69   gst_harness_teardown (h);
70 }
71
72 GST_END_TEST;
73
74 GST_START_TEST (test_src_harness_no_forwarding)
75 {
76   GstHarness *h = gst_harness_new ("identity");
77
78   /* turn of forwarding of necessary events */
79   gst_harness_set_forwarding (h, FALSE);
80
81   /* add a fakesrc that syncs to the clock and a
82      capsfilter that adds some caps to it */
83   gst_harness_add_src_parse (h,
84       "fakesrc sync=1 ! capsfilter caps=\"mycaps\"", TRUE);
85
86   /* start the fakesrc to produce the first events */
87   gst_harness_play (h->src_harness);
88
89   /* transfer STREAM_START event */
90   gst_harness_src_push_event (h);
91
92   /* crank the clock to produce the CAPS and SEGMENT events */
93   gst_harness_crank_single_clock_wait (h->src_harness);
94
95   /* transfer CAPS event */
96   gst_harness_src_push_event (h);
97
98   /* transfer SEGMENT event */
99   gst_harness_src_push_event (h);
100
101   /* now transfer the buffer produced by exploiting
102      the ability to say 0 cranks but 1 push */
103   gst_harness_src_crank_and_push_many (h, 0, 1);
104
105   /* and verify that the identity element outputs it */
106   gst_buffer_unref (gst_harness_pull (h));
107
108   gst_harness_teardown (h);
109 }
110
111 GST_END_TEST;
112
113 GST_START_TEST (test_add_sink_harness_without_sinkpad)
114 {
115   GstHarness *h = gst_harness_new ("fakesink");
116
117   gst_harness_add_sink (h, "fakesink");
118
119   gst_harness_teardown (h);
120 }
121
122 GST_END_TEST;
123
124 static GstEvent *
125 create_new_stream_start_event (GstHarness * h, gpointer data)
126 {
127   guint *counter = data;
128   gchar *stream_id = g_strdup_printf ("streamid/%d", *counter);
129   GstEvent *event = gst_event_new_stream_start (stream_id);
130   g_free (stream_id);
131   (*counter)++;
132   return event;
133 }
134
135 static void
136 push_query (gpointer data, gpointer user_data)
137 {
138   GstHarness *h = user_data;
139   GstCaps *caps = gst_caps_new_empty_simple ("mycaps");
140   GstQuery *query = gst_query_new_allocation (caps, FALSE);
141   gst_caps_unref (caps);
142   gst_pad_peer_query (h->srcpad, query);
143   gst_query_unref (query);
144 }
145
146 GST_START_TEST (test_forward_event_and_query_to_sink_harness_while_teardown)
147 {
148   GstHarness *h = gst_harness_new ("identity");
149   guint counter = 0;
150   GstHarnessThread *e_thread = gst_harness_stress_push_event_with_cb_start (h,
151       create_new_stream_start_event, &counter, NULL);
152   GstHarnessThread *q_thread = gst_harness_stress_custom_start (h, NULL,
153       push_query, h, 0);
154   gdouble duration = 1.0;
155   GTimer *timer = g_timer_new ();
156
157   while (g_timer_elapsed (timer, NULL) < duration) {
158     gst_harness_add_sink (h, "fakesink");
159     g_thread_yield ();
160   }
161
162   g_timer_destroy (timer);
163   gst_harness_stress_thread_stop (q_thread);
164   gst_harness_stress_thread_stop (e_thread);
165   gst_harness_teardown (h);
166 }
167
168 GST_END_TEST;
169
170 static Suite *
171 gst_harness_suite (void)
172 {
173   Suite *s = suite_create ("GstHarness");
174   TCase *tc_chain = tcase_create ("harness");
175
176   suite_add_tcase (s, tc_chain);
177
178   tcase_add_test (tc_chain, test_harness_element_ref);
179   tcase_add_test (tc_chain, test_src_harness);
180   tcase_add_test (tc_chain, test_src_harness_no_forwarding);
181   tcase_add_test (tc_chain, test_add_sink_harness_without_sinkpad);
182
183   tcase_add_test (tc_chain,
184       test_forward_event_and_query_to_sink_harness_while_teardown);
185
186   return s;
187 }
188
189 GST_CHECK_MAIN (gst_harness);