From 24cfd608c637e0d1c7d09eaee55383d3fe35e867 Mon Sep 17 00:00:00 2001 From: Jan Schmidt Date: Tue, 3 Jul 2018 00:30:12 +1000 Subject: [PATCH] switchbin: Add a basic unit-test Test the basic function of a switchbin - that it correctly selects between 2 processing paths based on caps --- tests/check/elements/switchbin.c | 111 +++++++++++++++++++++++++++++++ tests/check/meson.build | 1 + 2 files changed, 112 insertions(+) create mode 100644 tests/check/elements/switchbin.c diff --git a/tests/check/elements/switchbin.c b/tests/check/elements/switchbin.c new file mode 100644 index 0000000000..b51ba656df --- /dev/null +++ b/tests/check/elements/switchbin.c @@ -0,0 +1,111 @@ +/* GStreamer + * + * unit test for switchbin element + * Copyright (C) 2018 Jan Schmidt + * + * 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +GST_START_TEST (test_switchbin_simple) +{ + GstBus *bus; + GstElement *switchbin, *e0, *e1; + GstCaps *c0, *c1; + GstHarness *h; + guint path_index; + + GstBuffer *in_buf; + GstBuffer *out_buf; + + bus = gst_bus_new (); + + switchbin = gst_element_factory_make ("switchbin", NULL); + fail_unless (switchbin != NULL); + g_object_set (switchbin, "num-paths", 2, NULL); + gst_element_set_bus (switchbin, bus); + h = gst_harness_new_with_element (switchbin, "sink", "src"); + + e0 = gst_element_factory_make ("identity", NULL); + c0 = gst_caps_from_string ("audio/x-raw,format=S16LE,rate=48000,channels=2"); + e1 = gst_element_factory_make ("identity", NULL); + c1 = gst_caps_from_string ("audio/x-raw,format=S16LE,rate=44100,channels=1"); + + /* switchbin owns the elements after this */ + gst_child_proxy_set (GST_CHILD_PROXY (switchbin), + "path0::element", e0, "path0::caps", c0, + "path1::element", e1, "path1::caps", c1, NULL); + + /* Create a small buffer push it in, pull it out, + * and check the switchbin selected the right path for these caps */ + gst_harness_set_src_caps (h, c0); + in_buf = gst_harness_create_buffer (h, 480); + gst_harness_push (h, in_buf); + out_buf = gst_harness_pull (h); + fail_unless (in_buf == out_buf); + gst_buffer_unref (out_buf); + g_object_get (switchbin, "current-path", &path_index, NULL); + fail_unless (path_index == 0); + + /* Change caps, push more */ + gst_harness_set_src_caps (h, c1); + in_buf = gst_harness_create_buffer (h, 480); + gst_harness_push (h, in_buf); + out_buf = gst_harness_pull (h); + fail_unless (in_buf == out_buf); + gst_buffer_unref (out_buf); + g_object_get (switchbin, "current-path", &path_index, NULL); + fail_unless (path_index == 1); + + while (TRUE) { + GstMessage *msg = gst_bus_pop (bus); + if (!msg) + break; + + GST_DEBUG ("got message %s", + gst_message_type_get_name (GST_MESSAGE_TYPE (msg))); + fail_if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR); + gst_message_unref (msg); + } + + gst_harness_teardown (h); + gst_bus_set_flushing (bus, TRUE); + gst_object_unref (bus); + gst_object_unref (switchbin); +} + +GST_END_TEST; + +static Suite * +switchbin_suite (void) +{ + Suite *s = suite_create ("switchbin"); + TCase *tc_basic = tcase_create ("general"); + + suite_add_tcase (s, tc_basic); + tcase_add_test (tc_basic, test_switchbin_simple); + + return s; +} + +GST_CHECK_MAIN (switchbin); diff --git a/tests/check/meson.build b/tests/check/meson.build index f9f3146708..4c913af8d8 100644 --- a/tests/check/meson.build +++ b/tests/check/meson.build @@ -43,6 +43,7 @@ base_tests = [ [['elements/rtponviftimestamp.c']], [['elements/rtpsrc.c']], [['elements/rtpsink.c']], + [['elements/switchbin.c']], [['elements/videoframe-audiolevel.c']], [['elements/viewfinderbin.c']], [['libs/h264parser.c'], false, [gstcodecparsers_dep]],