1 /* GStreamer unit test for the gnomevfssink element
3 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
4 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
29 #include <glib/gstdio.h>
32 #include <unistd.h> /* for close() */
35 #include <gst/check/gstcheck.h>
37 static GstPad *mysrcpad;
39 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
45 setup_gnomevfssink (void)
47 GstElement *gnomevfssink;
49 GST_DEBUG ("setup_gnomevfssink");
50 gnomevfssink = gst_check_setup_element ("gnomevfssink");
51 mysrcpad = gst_check_setup_src_pad (gnomevfssink, &srctemplate, NULL);
52 gst_pad_set_active (mysrcpad, TRUE);
57 cleanup_gnomevfssink (GstElement * gnomevfssink)
59 gst_pad_set_active (mysrcpad, FALSE);
60 gst_check_teardown_src_pad (gnomevfssink);
61 gst_check_teardown_element (gnomevfssink);
65 /* this queries via the element vfunc, which is currently not implemented */
66 #define CHECK_QUERY_POSITION(gnomevfssink,format,position) \
68 GstFormat fmt = format; \
70 fail_unless (gst_element_query_position (gnomevfssink, &fmt, &pos)); \
71 fail_unless_equals_int (pos, position); \
74 #define CHECK_QUERY_POSITION(gnomevfssink,format,position) \
76 GstFormat fmt = format; \
79 pad = gst_element_get_static_pad (gnomevfssink, "sink"); \
80 fail_unless (gst_pad_query_position (pad, &fmt, &pos)); \
81 fail_unless_equals_int (pos, position); \
82 gst_object_unref (pad); \
86 #define PUSH_BYTES(num_bytes) \
88 GstBuffer *buf = gst_buffer_new_and_alloc(num_bytes); \
89 GRand *rand = g_rand_new_with_seed (num_bytes); \
91 for (i = 0; i < num_bytes; ++i) \
92 GST_BUFFER_DATA(buf)[i] = (g_rand_int (rand) >> 24) & 0xff; \
93 fail_unless_equals_int (gst_pad_push (mysrcpad, buf), GST_FLOW_OK); \
97 /* TODO: we don't check that the data is actually written to the right
98 * position after a seek */
99 GST_START_TEST (test_seeking)
102 GstElement *gnomevfssink;
106 tmpdir = g_get_tmp_dir ();
110 /* this is just silly, but gcc warns if we try to use tpmnam() */
112 g_build_filename (tmpdir, "gstreamer-gnomevfssink-test-XXXXXX", NULL);
113 fd = g_mkstemp (tmp_fn);
115 GST_ERROR ("can't create temp file %s: %s", tmp_fn, g_strerror (errno));
119 /* don't want the file, just a filename (hence silly, see above) */
123 gnomevfssink = setup_gnomevfssink ();
125 GST_LOG ("using temp file '%s'", tmp_fn);
126 g_object_set (gnomevfssink, "location", tmp_fn, NULL);
128 fail_unless_equals_int (gst_element_set_state (gnomevfssink,
129 GST_STATE_PLAYING), GST_STATE_CHANGE_ASYNC);
132 /* Test that gnomevfssink is seekable with a file fd */
133 /* gnomevfssink doesn't implement seekable query at the moment */
134 GstQuery *seeking_query;
137 fail_unless ((seeking_query = gst_query_new_seeking (GST_FORMAT_BYTES))
139 fail_unless (gst_element_query (gnomevfssink, seeking_query) == TRUE);
140 gst_query_parse_seeking (seeking_query, NULL, &seekable, NULL, NULL);
141 fail_unless (seekable == TRUE);
142 gst_query_unref (seeking_query);
145 fail_unless (gst_pad_push_event (mysrcpad,
146 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0)));
148 CHECK_QUERY_POSITION (gnomevfssink, GST_FORMAT_BYTES, 0);
150 /* push buffer with size 0 and NULL data */
152 CHECK_QUERY_POSITION (gnomevfssink, GST_FORMAT_BYTES, 0);
155 CHECK_QUERY_POSITION (gnomevfssink, GST_FORMAT_BYTES, 1);
158 CHECK_QUERY_POSITION (gnomevfssink, GST_FORMAT_BYTES, 100);
161 CHECK_QUERY_POSITION (gnomevfssink, GST_FORMAT_BYTES, 8900);
163 if (gst_pad_push_event (mysrcpad,
164 gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 8800, -1,
167 /* make sure that new position is reported immediately */
168 CHECK_QUERY_POSITION (gnomevfssink, GST_FORMAT_BYTES, 8800);
170 CHECK_QUERY_POSITION (gnomevfssink, GST_FORMAT_BYTES, 8801);
172 CHECK_QUERY_POSITION (gnomevfssink, GST_FORMAT_BYTES, 18057);
174 GST_INFO ("seeking not supported for tempfile?!");
177 fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
179 fail_unless_equals_int (gst_element_set_state (gnomevfssink, GST_STATE_NULL),
180 GST_STATE_CHANGE_SUCCESS);
183 cleanup_gnomevfssink (gnomevfssink);
185 /* check that we wrote data to the right position after the seek */
190 fail_unless (g_file_get_contents (tmp_fn, &data, &len, NULL),
191 "Failed to read in newly-created file '%s'", tmp_fn);
192 fail_unless_equals_int (len, 18057);
194 /* we wrote 9256 bytes at position 8801 */
195 GRand *rand = g_rand_new_with_seed (9256);
198 for (i = 0; i < 9256; ++i) {
199 guint8 byte_written = *(((guint8 *) data) + 8801 + i);
201 fail_unless_equals_int (byte_written, g_rand_int (rand) >> 24);
215 GST_START_TEST (test_coverage)
217 GstElement *gnomevfssink;
222 gnomevfssink = setup_gnomevfssink ();
223 bus = gst_bus_new ();
225 gst_element_set_bus (gnomevfssink, bus);
227 g_object_set (gnomevfssink, "location", "/i/do/not/exist", NULL);
228 g_object_get (gnomevfssink, "location", &location, NULL);
229 fail_unless_equals_string (location, "/i/do/not/exist");
232 fail_unless_equals_int (gst_element_set_state (gnomevfssink,
233 GST_STATE_PLAYING), GST_STATE_CHANGE_FAILURE);
235 /* a state change and an error */
236 fail_if ((message = gst_bus_pop (bus)) == NULL);
237 fail_unless_message_error (message, RESOURCE, OPEN_WRITE);
238 gst_message_unref (message);
240 g_object_set (gnomevfssink, "location", NULL, NULL);
241 g_object_get (gnomevfssink, "location", &location, NULL);
245 gst_element_set_bus (gnomevfssink, NULL);
246 gst_object_unref (GST_OBJECT (bus));
247 cleanup_gnomevfssink (gnomevfssink);
252 GST_START_TEST (test_uri_interface)
254 GstElement *gnomevfssink;
258 gnomevfssink = setup_gnomevfssink ();
259 bus = gst_bus_new ();
261 gst_element_set_bus (gnomevfssink, bus);
263 g_object_set (G_OBJECT (gnomevfssink), "location", "/i/do/not/exist", NULL);
264 g_object_get (G_OBJECT (gnomevfssink), "location", &location, NULL);
265 fail_unless_equals_string (location, "/i/do/not/exist");
268 location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (gnomevfssink));
269 fail_unless_equals_string (location, "file://%2Fi%2Fdo%2Fnot%2Fexist");
271 /* should accept file:///foo/bar URIs */
272 fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (gnomevfssink),
274 location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (gnomevfssink));
275 fail_unless_equals_string (location, "file://%2Ffoo%2Fbar");
276 g_object_get (G_OBJECT (gnomevfssink), "location", &location, NULL);
277 fail_unless_equals_string (location, "/foo/bar");
280 /* should accept file://localhost/foo/bar URIs */
281 fail_unless (gst_uri_handler_set_uri (GST_URI_HANDLER (gnomevfssink),
282 "file://localhost/foo/baz"));
283 location = (gchar *) gst_uri_handler_get_uri (GST_URI_HANDLER (gnomevfssink));
284 fail_unless_equals_string (location, "file://%2Ffoo%2Fbaz");
285 g_object_get (G_OBJECT (gnomevfssink), "location", &location, NULL);
286 fail_unless_equals_string (location, "/foo/baz");
289 /* should fail with other hostnames */
290 fail_if (gst_uri_handler_set_uri (GST_URI_HANDLER (gnomevfssink),
291 "file://hostname/foo/foo"));
294 gst_element_set_bus (gnomevfssink, NULL);
295 gst_object_unref (GST_OBJECT (bus));
296 cleanup_gnomevfssink (gnomevfssink);
302 gnomevfssink_suite (void)
304 Suite *s = suite_create ("gnomevfssink");
305 TCase *tc_chain = tcase_create ("general");
307 suite_add_tcase (s, tc_chain);
309 /* FIXME: these two tests fail right now because of uri/location stuff */
311 tcase_add_test (tc_chain, test_coverage);
312 tcase_add_test (tc_chain, test_uri_interface);
314 tcase_add_test (tc_chain, test_seeking);
319 GST_CHECK_MAIN (gnomevfssink);