Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / tests / check / elements / zbar.c
1 /* GStreamer zbar element unit test
2  *
3  * Copyright (C) 2010 Tim-Philipp Müller  <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gst/check/gstcheck.h>
22
23 GST_START_TEST (test_still_image)
24 {
25   GstMessage *zbar_msg = NULL;
26   GstElement *pipeline, *src, *dec, *csp, *zbar, *sink;
27   const gchar *type, *symbol;
28   gchar *path;
29   int qual;
30
31   pipeline = gst_pipeline_new ("pipeline");
32
33   src = gst_element_factory_make ("filesrc", NULL);
34   dec = gst_element_factory_make ("pngdec", NULL);
35   csp = gst_element_factory_make ("ffmpegcolorspace", NULL);
36   zbar = gst_element_factory_make ("zbar", NULL);
37   sink = gst_element_factory_make ("fakesink", NULL);
38
39   path = g_build_filename (GST_TEST_FILES_PATH, "barcode.png", NULL);
40   GST_LOG ("reading file '%s'", path);
41   g_object_set (src, "location", path, NULL);
42   g_free (path);
43
44   gst_bin_add_many (GST_BIN (pipeline), src, dec, csp, zbar, sink, NULL);
45   fail_unless (gst_element_link_many (src, dec, csp, zbar, sink, NULL));
46
47   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_PLAYING),
48       GST_STATE_CHANGE_ASYNC);
49
50   do {
51     GstMessage *msg;
52
53     msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline), -1,
54         GST_MESSAGE_ELEMENT | GST_MESSAGE_EOS | GST_MESSAGE_ERROR);
55
56     GST_INFO ("message: %" GST_PTR_FORMAT, msg);
57
58     fail_if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
59
60     if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_EOS) {
61       gst_message_unref (msg);
62       break;
63     }
64
65     if (GST_MESSAGE_SRC (msg) == GST_OBJECT_CAST (zbar) && zbar_msg == NULL) {
66       zbar_msg = msg;
67     } else {
68       gst_message_unref (msg);
69     }
70   } while (1);
71
72   fail_unless (zbar_msg != NULL);
73   fail_unless (gst_structure_has_name (zbar_msg->structure, "barcode"));
74   fail_unless (gst_structure_has_field (zbar_msg->structure, "timestamp"));
75   fail_unless (gst_structure_has_field (zbar_msg->structure, "type"));
76   fail_unless (gst_structure_has_field (zbar_msg->structure, "symbol"));
77   fail_unless (gst_structure_has_field (zbar_msg->structure, "quality"));
78   fail_unless (gst_structure_get_int (zbar_msg->structure, "quality", &qual));
79   fail_unless (qual >= 90);
80   type = gst_structure_get_string (zbar_msg->structure, "type");
81   fail_unless_equals_string (type, "EAN-13");
82   symbol = gst_structure_get_string (zbar_msg->structure, "symbol");
83   fail_unless_equals_string (symbol, "9876543210128");
84
85   fail_unless_equals_int (gst_element_set_state (pipeline, GST_STATE_NULL),
86       GST_STATE_CHANGE_SUCCESS);
87
88   gst_object_unref (pipeline);
89   gst_message_unref (zbar_msg);
90 }
91
92 GST_END_TEST;
93
94 static Suite *
95 zbar_suite (void)
96 {
97   Suite *s = suite_create ("zbar");
98   TCase *tc_chain = tcase_create ("general");
99
100   suite_add_tcase (s, tc_chain);
101
102   if (!gst_default_registry_check_feature_version ("pngdec", 0, 10, 25)) {
103     GST_INFO ("Skipping test, pngdec either not available or too old");
104   } else {
105     tcase_add_test (tc_chain, test_still_image);
106   }
107
108   return s;
109 }
110
111 GST_CHECK_MAIN (zbar);