diff --git a/ChangeLog b/ChangeLog index f6cf28582f..d2dc87b4a8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2006-10-19 Tim-Philipp Müller + + * tests/check/Makefile.am: + * tests/check/elements/.cvsignore: + * tests/check/elements/subparse.c: (buffer_from_static_string), + (setup_subparse), (teardown_subparse), (test_srt_do_test), + (GST_START_TEST), (subparse_suite): + Add very simple unit test for subparse. + 2006-10-19 Tim-Philipp Müller * gst/subparse/gstsubparse.c: (strip_trailing_newlines), diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 69beb9b86b..f57e688fed 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -58,6 +58,7 @@ check_PROGRAMS = \ elements/gdppay \ elements/multifdsink \ elements/playbin \ + elements/subparse \ elements/videorate \ elements/videotestsrc \ elements/volume \ @@ -116,6 +117,9 @@ elements_gdppay_LDADD = $(GST_GDP_LIBS) $(LDADD) elements_playbin_LDADD = $(GST_BASE_LIBS) $(LDADD) elements_playbin_CFLAGS = $(GST_BASE_CFLAGS) $(AM_CFLAGS) +elements_subparse_LDADD = $(LDADD) +elements_subparse_CFLAGS = $(CFLAGS) $(AM_CFLAGS) + elements_volume_LDADD = \ $(GST_BASE_LIBS) \ $(LDADD) diff --git a/tests/check/elements/.gitignore b/tests/check/elements/.gitignore index ab83d0f0d9..0e31e6beed 100644 --- a/tests/check/elements/.gitignore +++ b/tests/check/elements/.gitignore @@ -14,3 +14,4 @@ vorbisdec ffmpegcolorspace vorbistag playbin +subparse diff --git a/tests/check/elements/subparse.c b/tests/check/elements/subparse.c new file mode 100644 index 0000000000..c34c739d88 --- /dev/null +++ b/tests/check/elements/subparse.c @@ -0,0 +1,184 @@ +/* GStreamer unit tests for subparse + * + * Copyright (C) 2006 Tim-Philipp Müller + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("text/plain; text/x-pango-markup") + ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("ANY") + ); + +static GstElement *subparse; +static GstPad *mysrcpad, *mysinkpad; + +static GstBuffer * +buffer_from_static_string (const gchar * s) +{ + GstBuffer *buf; + + buf = gst_buffer_new (); + GST_BUFFER_DATA (buf) = (guint8 *) s; + GST_BUFFER_SIZE (buf) = strlen (s); + GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_READONLY); + + return buf; +} + +static struct +{ + const gchar *in; + GstClockTime from_ts; + GstClockTime to_ts; + const gchar *out; +} srt1_input[] = { + { + "1\n00:00:01,000 --> 00:00:02,000\nOne\n\n", + 1 * GST_SECOND, 2 * GST_SECOND, "One"}, { + "2\n00:00:02,000 --> 00:00:03,000\nTwo\n\n", + 2 * GST_SECOND, 3 * GST_SECOND, "Two"}, { + "3\n00:00:03,000 --> 00:00:04,000\nThree\n\n", + 3 * GST_SECOND, 4 * GST_SECOND, "Three"}, { + "4\n00:00:04,000 --> 00:00:05,000\nFour\n\n", + 4 * GST_SECOND, 5 * GST_SECOND, "Four"}, { + "5\n00:00:05,000 --> 00:00:06,000\nFive\n", + 5 * GST_SECOND, 6 * GST_SECOND, "Five"} +}; + +static void +setup_subparse (void) +{ + subparse = gst_check_setup_element ("subparse"); + + mysrcpad = gst_check_setup_src_pad (subparse, &srctemplate, NULL); + mysinkpad = gst_check_setup_sink_pad (subparse, &sinktemplate, NULL); + + gst_pad_set_active (mysrcpad, TRUE); + gst_pad_set_active (mysinkpad, TRUE); + + fail_unless_equals_int (gst_element_set_state (subparse, GST_STATE_PLAYING), + GST_STATE_CHANGE_SUCCESS); +} + +static void +teardown_subparse (void) +{ + GST_DEBUG ("cleaning up"); + + g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL); + g_list_free (buffers); + buffers = NULL; + + gst_check_teardown_sink_pad (subparse); + gst_check_teardown_src_pad (subparse); + gst_check_teardown_element (subparse); + subparse = NULL; + mysrcpad = NULL; + mysinkpad = NULL; +} + +static void +test_srt_do_test (guint start_idx, guint num) +{ + guint n; + + GST_LOG ("srt test: start_idx = %u, num = %u", start_idx, num); + + setup_subparse (); + + for (n = start_idx; n < start_idx + num; ++n) { + GstBuffer *buf; + + buf = buffer_from_static_string (srt1_input[n].in); + fail_unless_equals_int (gst_pad_push (mysrcpad, buf), GST_FLOW_OK); + } + + gst_pad_push_event (mysrcpad, gst_event_new_eos ()); + + fail_unless_equals_int (g_list_length (buffers), num); + + for (n = start_idx; n < start_idx + num; ++n) { + const GstStructure *buffer_caps_struct; + GstBuffer *buf; + gchar *out; + guint out_size; + + buf = g_list_nth_data (buffers, n - start_idx); + fail_unless (buf != NULL); + fail_unless (GST_BUFFER_TIMESTAMP_IS_VALID (buf), NULL); + fail_unless (GST_BUFFER_DURATION_IS_VALID (buf), NULL); + fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (buf), + srt1_input[n].from_ts); + fail_unless_equals_uint64 (GST_BUFFER_DURATION (buf), + srt1_input[n].to_ts - srt1_input[n].from_ts); + out = (gchar *) GST_BUFFER_DATA (buf); + out_size = GST_BUFFER_SIZE (buf); + /* shouldn't have trailing newline characters */ + fail_if (out_size > 0 && out[out_size - 1] == '\n'); + fail_unless (strncmp (out, srt1_input[n].out, out_size) == 0); + /* check caps */ + fail_unless (GST_BUFFER_CAPS (buf) != NULL); + buffer_caps_struct = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0); + fail_unless (gst_structure_has_name (buffer_caps_struct, "text/plain") + || gst_structure_has_name (buffer_caps_struct, "text/x-pango-markup")); + } + + teardown_subparse (); +} + +GST_START_TEST (test_srt) +{ + test_srt_do_test (0, G_N_ELEMENTS (srt1_input)); + + /* make sure everything works fine if we don't start with chunk 1 */ + test_srt_do_test (1, G_N_ELEMENTS (srt1_input) - 1); + test_srt_do_test (2, G_N_ELEMENTS (srt1_input) - 2); + test_srt_do_test (3, G_N_ELEMENTS (srt1_input) - 3); + test_srt_do_test (4, G_N_ELEMENTS (srt1_input) - 4); + + /* try with empty input, immediate EOS */ + test_srt_do_test (5, G_N_ELEMENTS (srt1_input) - 5); +} + +GST_END_TEST; + +static Suite * +subparse_suite (void) +{ + Suite *s = suite_create ("subparse"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + + tcase_add_test (tc_chain, test_srt); + + return s; +} + +GST_CHECK_MAIN (subparse);