tests/check/: use the new macro
[platform/upstream/gstreamer.git] / tests / check / elements / fdsrc.c
1 /* GStreamer
2  *
3  * unit test for fdsrc
4  *
5  * Copyright (C) <2005> Jan Schmidt <thaytan at mad dot scientist dot com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include <gst/check/gstcheck.h>
29
30 gboolean have_eos = FALSE;
31
32 GstPad *mysinkpad;
33
34 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS_ANY);
38
39 gboolean
40 event_func (GstPad * pad, GstEvent * event)
41 {
42   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
43     have_eos = TRUE;
44     gst_event_unref (event);
45     return TRUE;
46   }
47
48   gst_event_unref (event);
49   return FALSE;
50 }
51
52 GstElement *
53 setup_fdsrc ()
54 {
55   GstElement *fdsrc;
56
57   GST_DEBUG ("setup_fdsrc");
58   fdsrc = gst_check_setup_element ("fdsrc");
59   mysinkpad = gst_check_setup_sink_pad (fdsrc, &sinktemplate, NULL);
60   gst_pad_set_event_function (mysinkpad, event_func);
61   gst_pad_set_active (mysinkpad, TRUE);
62   return fdsrc;
63 }
64
65 void
66 cleanup_fdsrc (GstElement * fdsrc)
67 {
68   gst_check_teardown_sink_pad (fdsrc);
69   gst_check_teardown_element (fdsrc);
70 }
71
72 GST_START_TEST (test_num_buffers)
73 {
74   GstElement *src;
75   gint pipe_fd[2];
76   gchar data[4096];
77
78   fail_if (pipe (pipe_fd) < 0);
79
80   src = setup_fdsrc ();
81   g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
82   g_object_set (G_OBJECT (src), "fd", pipe_fd[0], NULL);
83   fail_unless (gst_element_set_state (src,
84           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
85       "could not set to playing");
86
87   memset (data, 0, 4096);
88   while (!have_eos) {
89     fail_if (write (pipe_fd[1], data, 4096) < 0);
90     g_usleep (100);
91   }
92
93   fail_unless (g_list_length (buffers) == 3);
94   fail_unless (gst_element_set_state (src,
95           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
96
97   /* cleanup */
98   cleanup_fdsrc (src);
99   close (pipe_fd[0]);
100   close (pipe_fd[1]);
101   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
102   g_list_free (buffers);
103 }
104
105 GST_END_TEST;
106
107 GST_START_TEST (test_nonseeking)
108 {
109   GstElement *src;
110   GstQuery *seeking_query;
111   gint pipe_fd[2];
112   gchar data[4096];
113   gboolean seekable;
114
115   fail_if (pipe (pipe_fd) < 0);
116
117   src = setup_fdsrc ();
118   g_object_set (G_OBJECT (src), "num-buffers", 3, NULL);
119   g_object_set (G_OBJECT (src), "fd", pipe_fd[0], NULL);
120   fail_unless (gst_element_set_state (src,
121           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
122       "could not set to paused");
123
124   memset (data, 0, 4096);
125   fail_if (write (pipe_fd[1], data, 4096) < 0);
126
127   /* Test that fdsrc is non-seekable with a pipe */
128   fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
129       != NULL);
130   fail_unless (gst_element_query (src, seeking_query) == TRUE);
131   gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
132   fail_unless (seekable == FALSE);
133   gst_query_unref (seeking_query);
134
135   fail_unless (gst_element_set_state (src,
136           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
137
138   /* cleanup */
139   cleanup_fdsrc (src);
140   close (pipe_fd[0]);
141   close (pipe_fd[1]);
142 }
143
144 GST_END_TEST;
145
146 GST_START_TEST (test_seeking)
147 {
148   GstElement *src;
149   gint in_fd;
150   GstQuery *seeking_query;
151   gboolean seekable;
152
153 #ifndef TESTFILE
154 #error TESTFILE not defined
155 #endif
156   fail_if ((in_fd = open (TESTFILE, O_RDONLY)) < 0);
157   src = setup_fdsrc ();
158
159   g_object_set (G_OBJECT (src), "fd", in_fd, NULL);
160   fail_unless (gst_element_set_state (src,
161           GST_STATE_PAUSED) == GST_STATE_CHANGE_SUCCESS,
162       "could not set to paused");
163
164   /* Test that fdsrc is seekable with a file fd */
165   fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
166       != NULL);
167   fail_unless (gst_element_query (src, seeking_query) == TRUE);
168   gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
169   fail_unless (seekable == TRUE);
170   gst_query_unref (seeking_query);
171
172   fail_unless (gst_element_set_state (src,
173           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
174
175   /* cleanup */
176   cleanup_fdsrc (src);
177   close (in_fd);
178 }
179
180 GST_END_TEST;
181
182 Suite *
183 fdsrc_suite (void)
184 {
185   Suite *s = suite_create ("fdsrc");
186   TCase *tc_chain = tcase_create ("general");
187
188   suite_add_tcase (s, tc_chain);
189   tcase_add_test (tc_chain, test_num_buffers);
190   tcase_add_test (tc_chain, test_nonseeking);
191   tcase_add_test (tc_chain, test_seeking);
192
193   return s;
194 }
195
196 GST_CHECK_MAIN (fdsrc);