From ce447f126f61442610d67b234307f8e9ee450e0c Mon Sep 17 00:00:00 2001
From: Andrew Esh <andrew.esh@qterics.com>
Date: Mon, 31 Oct 2016 09:22:58 -0500
Subject: [PATCH 3/3] Add tests for smoke

---
 tests/check/Makefile.am         |   6 +
 tests/check/elements/jpegdec.c  |   1 +
 tests/check/elements/smokedec.c | 157 +++++++++++++++++++++++++
 tests/check/elements/smokeenc.c | 254 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 418 insertions(+)
 create mode 100644 tests/check/elements/smokedec.c
 create mode 100644 tests/check/elements/smokeenc.c

diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am
index aa40c70..4c80653 100644
--- a/tests/check/Makefile.am
+++ b/tests/check/Makefile.am
@@ -513,6 +513,12 @@ elements_jpegdec_LDADD = $(GST_PLUGINS_BASE_LIBS) -lgstapp-$(GST_API_VERSION) -l
 elements_jpegenc_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(AM_CFLAGS)
 elements_jpegenc_LDADD = $(GST_PLUGINS_BASE_LIBS) -lgstapp-$(GST_API_VERSION) $(GST_BASE_LIBS) $(LDADD)
 
+elements_smokedec_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GIO_CFLAGS) $(AM_CFLAGS)
+elements_smokedec_LDADD = $(GST_PLUGINS_BASE_LIBS) -lgstapp-$(GST_API_VERSION) -lgstpbutils-$(GST_API_VERSION) $(GST_BASE_LIBS) $(GIO_LIBS) $(LDADD)
+
+elements_smokeenc_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(AM_CFLAGS)
+elements_smokeenc_LDADD = $(GST_PLUGINS_BASE_LIBS) -lgstapp-$(GST_API_VERSION) $(GST_BASE_LIBS) $(LDADD)
+
 elements_matroskamux_LDADD = $(GST_BASE_LIBS) $(LDADD) $(LIBM)
 
 elements_mulawdec_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS) $(AM_CFLAGS)
diff --git a/tests/check/elements/jpegdec.c b/tests/check/elements/jpegdec.c
index f6ac523..8413dc4 100644
--- a/tests/check/elements/jpegdec.c
+++ b/tests/check/elements/jpegdec.c
@@ -36,6 +36,7 @@ GST_START_TEST (test_jpegdec_explicit)
   pipeline = gst_pipeline_new (NULL);
   source = gst_element_factory_make ("filesrc", NULL);
   dec = gst_element_factory_make ("jpegdec", NULL);
+  fail_unless (dec != NULL);
   sink = gst_element_factory_make ("appsink", NULL);
 
   gst_bin_add_many (GST_BIN (pipeline), source, dec, sink, NULL);
diff --git a/tests/check/elements/smokedec.c b/tests/check/elements/smokedec.c
new file mode 100644
index 0000000..01afbb6
--- /dev/null
+++ b/tests/check/elements/smokedec.c
@@ -0,0 +1,157 @@
+/* GStreamer
+ * unit test for smokedec
+ * Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
+ * Copyright (C) <2012> Mathias Hasselmann <mathias@openismus.com>
+ *
+ * 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.
+ */
+
+#include <unistd.h>
+
+#include <gio/gio.h>
+#include <gst/check/gstcheck.h>
+#include <gst/app/gstappsink.h>
+#include <gst/pbutils/gstdiscoverer.h>
+
+/* Verify smokedec is working when explictly requested by a pipeline. */
+GST_START_TEST (test_smokedec_explicit)
+{
+  GstElement *pipeline, *source, *dec, *sink;
+  GstSample *sample;
+
+  /* construct a pipeline that explicitly uses smokedec */
+  pipeline = gst_pipeline_new (NULL);
+  source = gst_element_factory_make ("filesrc", NULL);
+  dec = gst_element_factory_make ("smokedec", NULL);
+  fail_unless (dec != NULL);
+  sink = gst_element_factory_make ("appsink", NULL);
+
+  gst_bin_add_many (GST_BIN (pipeline), source, dec, sink, NULL);
+  gst_element_link_many (source, dec, sink, NULL);
+
+  /* point that pipeline to our test image */
+  {
+    char *filename = g_build_filename (GST_TEST_FILES_PATH, "image.jpg", NULL);
+    g_object_set (G_OBJECT (source), "location", filename, NULL);
+    g_free (filename);
+  }
+
+  gst_element_set_state (pipeline, GST_STATE_PLAYING);
+
+  sample = gst_app_sink_pull_sample (GST_APP_SINK (sink));
+  fail_unless (GST_IS_SAMPLE (sample));
+
+  /* do some basic checks to verify image decoding */
+  {
+    GstCaps *decoded;
+    GstCaps *expected;
+
+    decoded = gst_sample_get_caps (sample);
+    expected = gst_caps_from_string ("video/x-raw, width=120, height=160");
+
+    fail_unless (gst_caps_is_always_compatible (decoded, expected));
+
+    gst_caps_unref (expected);
+  }
+  gst_sample_unref (sample);
+
+  /* wait for EOS */
+  sample = gst_app_sink_pull_sample (GST_APP_SINK (sink));
+  fail_unless (sample == NULL);
+  fail_unless (gst_app_sink_is_eos (GST_APP_SINK (sink)));
+
+  gst_element_set_state (pipeline, GST_STATE_NULL);
+  gst_object_unref (pipeline);
+}
+
+GST_END_TEST;
+
+/* Verify JPEG discovery is working. Right now smokedec would be used,
+ * but I have no idea how to actually verify this. */
+GST_START_TEST (test_smokedec_discover)
+{
+  GstDiscoverer *disco;
+  GError *error = NULL;
+  char *uri;
+  GstDiscovererInfo *info;
+  GstDiscovererVideoInfo *video;
+
+  disco = gst_discoverer_new (5 * GST_SECOND, &error);
+
+  fail_unless (GST_IS_DISCOVERER (disco));
+  fail_unless (error == NULL, "%s", (error ? error->message : ""));
+
+  {
+    GFile *testdir = g_file_new_for_path (GST_TEST_FILES_PATH);
+    GFile *testfile = g_file_resolve_relative_path (testdir, "image.jpg");
+    uri = g_file_get_uri (testfile);
+    g_object_unref (testfile);
+    g_object_unref (testdir);
+  }
+
+  info = gst_discoverer_discover_uri (disco, uri, &error);
+  fail_unless (GST_IS_DISCOVERER_INFO (info));
+  fail_unless (error == NULL, "%s: %s", uri, (error ? error->message : ""));
+
+  fail_unless_equals_string (gst_discoverer_info_get_uri (info), uri);
+  fail_unless_equals_int (gst_discoverer_info_get_result (info),
+      GST_DISCOVERER_OK);
+
+  video =
+      GST_DISCOVERER_VIDEO_INFO (gst_discoverer_info_get_stream_info (info));
+  fail_unless (video != NULL);
+
+  fail_unless (gst_discoverer_video_info_is_image (video));
+  fail_unless_equals_int (gst_discoverer_video_info_get_width (video), 120);
+  fail_unless_equals_int (gst_discoverer_video_info_get_height (video), 160);
+
+  gst_discoverer_info_unref (video);
+  gst_discoverer_info_unref (info);
+  g_free (uri);
+  g_object_unref (disco);
+}
+
+GST_END_TEST;
+
+static Suite *
+smokedec_suite (void)
+{
+  Suite *s = suite_create ("smokedec");
+  TCase *tc_chain = tcase_create ("general");
+
+  suite_add_tcase (s, tc_chain);
+  tcase_add_test (tc_chain, test_smokedec_explicit);
+  tcase_add_test (tc_chain, test_smokedec_discover);
+
+  return s;
+}
+
+int
+main (int argc, char **argv)
+{
+  int nf;
+
+  Suite *s = smokedec_suite ();
+  SRunner *sr = srunner_create (s);
+
+  gst_check_init (&argc, &argv);
+
+  srunner_run_all (sr, CK_NORMAL);
+  nf = srunner_ntests_failed (sr);
+  srunner_free (sr);
+
+  return nf;
+}
diff --git a/tests/check/elements/smokeenc.c b/tests/check/elements/smokeenc.c
new file mode 100644
index 0000000..e567bc1
--- /dev/null
+++ b/tests/check/elements/smokeenc.c
@@ -0,0 +1,254 @@
+/* GStreamer
+ *
+ * unit test for smokeenc
+ *
+ * Copyright (C) <2010> Thiago Santos <thiago.sousa.santos@collabora.co.uk>
+ *
+ * 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.
+ */
+
+#include <unistd.h>
+
+#include <gst/check/gstcheck.h>
+#include <gst/app/gstappsink.h>
+
+/* For ease of programming we use globals to keep refs for our floating
+ * sink pads we create; otherwise we always have to do get_pad,
+ * get_peer, and then remove references in every test function */
+static GstPad *mysinkpad;
+static GstPad *mysrcpad;
+
+#define JPEG_CAPS_STRING "image/jpeg"
+
+#define JPEG_CAPS_RESTRICTIVE "image/jpeg, " \
+    "width = (int) [100, 200], " \
+    "framerate = (fraction) 25/1, " \
+    "extraparameter = (string) { abc, def }"
+
+static GstStaticPadTemplate jpeg_sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS (JPEG_CAPS_STRING));
+
+static GstStaticPadTemplate any_sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS_ANY);
+
+static GstStaticPadTemplate jpeg_restrictive_sinktemplate =
+GST_STATIC_PAD_TEMPLATE ("sink",
+    GST_PAD_SINK,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS (JPEG_CAPS_RESTRICTIVE));
+
+static GstStaticPadTemplate any_srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
+    GST_PAD_SRC,
+    GST_PAD_ALWAYS,
+    GST_STATIC_CAPS_ANY);
+
+
+static GstElement *
+setup_smokeenc (GstStaticPadTemplate * sinktemplate)
+{
+  GstElement *smokeenc;
+
+  GST_DEBUG ("setup_smokeenc");
+  smokeenc = gst_check_setup_element ("smokeenc");
+  mysinkpad = gst_check_setup_sink_pad (smokeenc, sinktemplate);
+  mysrcpad = gst_check_setup_src_pad (smokeenc, &any_srctemplate);
+  gst_pad_set_active (mysrcpad, TRUE);
+  gst_pad_set_active (mysinkpad, TRUE);
+
+  return smokeenc;
+}
+
+static void
+cleanup_smokeenc (GstElement * smokeenc)
+{
+  GST_DEBUG ("cleanup_smokeenc");
+  gst_element_set_state (smokeenc, GST_STATE_NULL);
+
+  gst_pad_set_active (mysrcpad, FALSE);
+  gst_pad_set_active (mysinkpad, FALSE);
+  gst_check_teardown_sink_pad (smokeenc);
+  gst_check_teardown_src_pad (smokeenc);
+  gst_check_teardown_element (smokeenc);
+}
+
+static GstBuffer *
+create_video_buffer (GstCaps * caps)
+{
+  GstElement *pipeline;
+  GstElement *cf;
+  GstElement *sink;
+  GstSample *sample;
+  GstBuffer *buffer;
+
+  pipeline =
+      gst_parse_launch
+      ("videotestsrc num-buffers=1 ! capsfilter name=cf ! appsink name=sink",
+      NULL);
+  g_assert (pipeline != NULL);
+
+  cf = gst_bin_get_by_name (GST_BIN (pipeline), "cf");
+  sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
+
+  g_object_set (G_OBJECT (cf), "caps", caps, NULL);
+
+  gst_element_set_state (pipeline, GST_STATE_PLAYING);
+
+  sample = gst_app_sink_pull_sample (GST_APP_SINK (sink));
+
+  gst_element_set_state (pipeline, GST_STATE_NULL);
+  gst_object_unref (pipeline);
+  gst_object_unref (sink);
+  gst_object_unref (cf);
+
+  buffer = gst_sample_get_buffer (sample);
+  gst_buffer_ref (buffer);
+
+  gst_sample_unref (sample);
+
+  return buffer;
+}
+
+
+GST_START_TEST (test_smokeenc_getcaps)
+{
+  GstElement *smokeenc;
+  GstPad *sinkpad;
+  GstCaps *caps;
+  GstStructure *structure;
+  gint fps_n;
+  gint fps_d;
+  const GValue *value;
+
+  /* we are going to do some get caps to confirm it doesn't return non-subset
+   * caps */
+
+  smokeenc = setup_smokeenc (&any_sinktemplate);
+  sinkpad = gst_element_get_static_pad (smokeenc, "sink");
+  /* this should assert if non-subset */
+  caps = gst_pad_query_caps (sinkpad, NULL);
+  gst_caps_unref (caps);
+  gst_object_unref (sinkpad);
+  cleanup_smokeenc (smokeenc);
+
+  smokeenc = setup_smokeenc (&jpeg_sinktemplate);
+  sinkpad = gst_element_get_static_pad (smokeenc, "sink");
+  /* this should assert if non-subset */
+  caps = gst_pad_query_caps (sinkpad, NULL);
+  gst_caps_unref (caps);
+  gst_object_unref (sinkpad);
+  cleanup_smokeenc (smokeenc);
+
+  /* now use a more restricted one and check the resulting caps */
+  smokeenc = setup_smokeenc (&jpeg_restrictive_sinktemplate);
+  sinkpad = gst_element_get_static_pad (smokeenc, "sink");
+  /* this should assert if non-subset */
+  caps = gst_pad_query_caps (sinkpad, NULL);
+  structure = gst_caps_get_structure (caps, 0);
+
+  /* check the width */
+  value = gst_structure_get_value (structure, "width");
+  fail_unless (gst_value_get_int_range_min (value) == 100);
+  fail_unless (gst_value_get_int_range_max (value) == 200);
+
+  fail_unless (gst_structure_get_fraction (structure, "framerate", &fps_n,
+          &fps_d));
+  fail_unless (fps_n == 25);
+  fail_unless (fps_d == 1);
+
+  gst_caps_unref (caps);
+  gst_object_unref (sinkpad);
+  cleanup_smokeenc (smokeenc);
+}
+
+GST_END_TEST;
+
+
+GST_START_TEST (test_smokeenc_different_caps)
+{
+  GstElement *smokeenc;
+  GstBuffer *buffer;
+  GstCaps *caps;
+  GstCaps *allowed_caps;
+
+  /* now use a more restricted one and check the resulting caps */
+  smokeenc = setup_smokeenc (&any_sinktemplate);
+  gst_element_set_state (smokeenc, GST_STATE_PLAYING);
+
+  /* push first buffer with 800x600 resolution */
+  caps = gst_caps_new_simple ("video/x-raw", "width", G_TYPE_INT,
+      800, "height", G_TYPE_INT, 600, "framerate",
+      GST_TYPE_FRACTION, 1, 1, "format", G_TYPE_STRING, "I420", NULL);
+  gst_check_setup_events (mysrcpad, smokeenc, caps, GST_FORMAT_TIME);
+  fail_unless ((buffer = create_video_buffer (caps)) != NULL);
+  gst_caps_unref (caps);
+  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
+
+  /* check the allowed caps to see if a second buffer with a different
+   * caps could be negotiated */
+  allowed_caps = gst_pad_get_allowed_caps (mysrcpad);
+
+  /* the caps we want to negotiate to */
+  caps = gst_caps_new_simple ("video/x-raw", "width", G_TYPE_INT,
+      640, "height", G_TYPE_INT, 480, "framerate",
+      GST_TYPE_FRACTION, 1, 1, "format", G_TYPE_STRING, "I420", NULL);
+  fail_unless (gst_caps_can_intersect (allowed_caps, caps));
+  fail_unless (gst_pad_set_caps (mysrcpad, caps));
+
+  /* push second buffer with 640x480 resolution */
+  buffer = create_video_buffer (caps);
+  gst_caps_unref (caps);
+  fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
+
+  gst_caps_unref (allowed_caps);
+  gst_element_set_state (smokeenc, GST_STATE_NULL);
+  cleanup_smokeenc (smokeenc);
+}
+
+GST_END_TEST;
+
+static Suite *
+smokeenc_suite (void)
+{
+  Suite *s = suite_create ("smokeenc");
+  TCase *tc_chain = tcase_create ("general");
+
+  suite_add_tcase (s, tc_chain);
+  tcase_add_test (tc_chain, test_smokeenc_getcaps);
+  tcase_add_test (tc_chain, test_smokeenc_different_caps);
+
+  return s;
+}
+
+int
+main (int argc, char **argv)
+{
+  int nf;
+
+  Suite *s = smokeenc_suite ();
+  SRunner *sr = srunner_create (s);
+
+  gst_check_init (&argc, &argv);
+
+  srunner_run_all (sr, CK_NORMAL);
+  nf = srunner_ntests_failed (sr);
+  srunner_free (sr);
+
+  return nf;
+}
-- 
2.10.1

