3 * unit tests for the navigation interface library
5 * Copyright (C) 2009 Jan Schmidt <thaytan@noraisin.net>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include <gst/check/gstcheck.h>
29 #include <gst/interfaces/navigation.h>
33 #define TEST_ELEMENT_TYPE (test_element_get_type())
35 typedef struct TestElement TestElement;
36 typedef struct TestElementClass TestElementClass;
42 GstNavigationEventType sent_type;
43 const gchar *sent_key;
44 gdouble sent_x, sent_y;
46 GstNavigationCommand sent_command;
49 struct TestElementClass
51 GstElementClass parent_class;
54 GType test_element_get_type (void);
56 static void init_interface (GType type);
57 static void gst_implements_interface_init (GstImplementsInterfaceClass * klass);
58 static void nav_send_event (GstNavigation * navigation,
59 GstStructure * structure);
61 GST_BOILERPLATE_FULL (TestElement, test_element, GstElement, GST_TYPE_ELEMENT,
65 test_element_navigation_interface_init (GstNavigationInterface * klass)
67 klass->send_event = nav_send_event;
71 init_interface (GType type)
73 static const GInterfaceInfo navigation_iface_info = {
74 (GInterfaceInitFunc) test_element_navigation_interface_init,
78 static const GInterfaceInfo implements_iface_info = {
79 (GInterfaceInitFunc) gst_implements_interface_init,
84 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
85 &implements_iface_info);
86 g_type_add_interface_static (type, GST_TYPE_NAVIGATION,
87 &navigation_iface_info);
91 test_element_base_init (gpointer klass)
96 test_element_class_init (TestElementClass * klass)
101 test_element_interface_supported (GstImplementsInterface * ifacE,
102 GType interface_type)
104 if (interface_type == GST_TYPE_NAVIGATION)
111 gst_implements_interface_init (GstImplementsInterfaceClass * klass)
113 klass->supported = test_element_interface_supported;
117 test_element_init (TestElement * this, TestElementClass * klass)
122 nav_send_event (GstNavigation * navigation, GstStructure * structure)
124 GstEvent *event = gst_event_new_navigation (structure);
125 GstNavigationEventType etype = gst_navigation_event_get_type (event);
126 TestElement *self = (TestElement *) (navigation);
128 fail_if (etype == GST_NAVIGATION_EVENT_INVALID,
129 "Received navigation event could not be parsed");
130 fail_unless (etype == self->sent_type,
131 "Received navigation event did not match sent");
134 case GST_NAVIGATION_EVENT_KEY_PRESS:
135 case GST_NAVIGATION_EVENT_KEY_RELEASE:{
137 fail_unless (gst_navigation_event_parse_key_event (event, &key));
138 fail_unless (strcmp (key, self->sent_key) == 0);
141 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
142 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:{
145 fail_unless (gst_navigation_event_parse_mouse_button_event (event,
147 fail_unless (button == self->sent_button);
148 fail_unless (x == self->sent_x);
149 fail_unless (y == self->sent_y);
152 case GST_NAVIGATION_EVENT_MOUSE_MOVE:{
154 fail_unless (gst_navigation_event_parse_mouse_move_event (event, &x, &y));
155 fail_unless (x == self->sent_x);
156 fail_unless (y == self->sent_y);
159 case GST_NAVIGATION_EVENT_COMMAND:{
160 GstNavigationCommand cmd;
161 fail_unless (gst_navigation_event_parse_command (event, &cmd));
162 fail_unless (cmd == self->sent_command);
168 gst_event_unref (event);
171 GST_START_TEST (test_events)
173 /* Create an empty GstElement that has a GstNavigation interface and then
174 * send some navigation events and validate them */
175 TestElement *test_element =
176 (TestElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
177 GstNavigationCommand cmds[] = {
178 GST_NAVIGATION_COMMAND_MENU1, GST_NAVIGATION_COMMAND_MENU2,
179 GST_NAVIGATION_COMMAND_MENU3, GST_NAVIGATION_COMMAND_MENU4,
180 GST_NAVIGATION_COMMAND_MENU5, GST_NAVIGATION_COMMAND_MENU6,
181 GST_NAVIGATION_COMMAND_MENU7, GST_NAVIGATION_COMMAND_LEFT,
182 GST_NAVIGATION_COMMAND_RIGHT, GST_NAVIGATION_COMMAND_UP,
183 GST_NAVIGATION_COMMAND_DOWN, GST_NAVIGATION_COMMAND_ACTIVATE,
184 GST_NAVIGATION_COMMAND_PREV_ANGLE, GST_NAVIGATION_COMMAND_NEXT_ANGLE
188 test_element->sent_type = GST_NAVIGATION_EVENT_KEY_PRESS;
189 test_element->sent_key = "1";
190 gst_navigation_send_key_event (GST_NAVIGATION (test_element), "key-press",
193 test_element->sent_type = GST_NAVIGATION_EVENT_KEY_RELEASE;
194 test_element->sent_key = "2";
195 gst_navigation_send_key_event (GST_NAVIGATION (test_element), "key-release",
198 test_element->sent_type = GST_NAVIGATION_EVENT_MOUSE_MOVE;
199 test_element->sent_x = 50;
200 test_element->sent_y = 100;
201 gst_navigation_send_mouse_event (GST_NAVIGATION (test_element), "mouse-move",
204 test_element->sent_type = GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS;
205 test_element->sent_x = 10;
206 test_element->sent_y = 20;
207 test_element->sent_button = 1;
208 gst_navigation_send_mouse_event (GST_NAVIGATION (test_element),
209 "mouse-button-press", 1, 10, 20);
211 for (i = 0; i < G_N_ELEMENTS (cmds); i++) {
212 test_element->sent_type = GST_NAVIGATION_EVENT_COMMAND;
213 test_element->sent_command = cmds[i];
214 gst_navigation_send_command (GST_NAVIGATION (test_element), cmds[i]);
217 gst_object_unref (test_element);
222 GST_START_TEST (test_messages)
225 /* GST_NAVIGATION_MESSAGE_MOUSE_OVER */
228 m = gst_navigation_message_new_mouse_over (NULL, TRUE);
230 fail_unless (gst_navigation_message_get_type (m) ==
231 GST_NAVIGATION_MESSAGE_MOUSE_OVER);
232 fail_unless (GST_MESSAGE_SRC (m) == NULL);
233 fail_unless (gst_navigation_message_parse_mouse_over (m, &active));
234 fail_unless (active == TRUE);
235 gst_message_unref (m);
237 m = gst_navigation_message_new_mouse_over (NULL, FALSE);
239 fail_unless (GST_MESSAGE_SRC (m) == NULL);
240 fail_unless (gst_navigation_message_get_type (m) ==
241 GST_NAVIGATION_MESSAGE_MOUSE_OVER);
242 fail_unless (gst_navigation_message_parse_mouse_over (m, &active));
243 fail_unless (active == FALSE);
244 gst_message_unref (m);
247 /* GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED */
249 m = gst_navigation_message_new_commands_changed (NULL);
251 fail_unless (GST_MESSAGE_SRC (m) == NULL);
252 fail_unless (gst_navigation_message_get_type (m) ==
253 GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED);
254 gst_message_unref (m);
257 /* GST_NAVIGATION_MESSAGE_ANGLES_CHANGED */
260 m = gst_navigation_message_new_angles_changed (NULL, 1, 5);
262 fail_unless (GST_MESSAGE_SRC (m) == NULL);
263 fail_unless (gst_navigation_message_get_type (m) ==
264 GST_NAVIGATION_MESSAGE_ANGLES_CHANGED);
265 fail_unless (gst_navigation_message_parse_angles_changed (m, &angle,
267 fail_unless (angle == 1);
268 fail_unless (angles == 5);
269 gst_message_unref (m);
275 GST_START_TEST (test_queries)
279 /* GST_NAVIGATION_QUERY_COMMANDS */
282 GstNavigationCommand cmd;
284 q = gst_navigation_query_new_commands ();
285 fail_unless (q != NULL);
286 fail_unless (gst_navigation_query_get_type (q) ==
287 GST_NAVIGATION_QUERY_COMMANDS);
288 gst_navigation_query_set_commands (q, 3, GST_NAVIGATION_COMMAND_LEFT,
289 GST_NAVIGATION_COMMAND_MENU1, GST_NAVIGATION_COMMAND_MENU5);
290 fail_unless (gst_navigation_query_parse_commands_length (q, &n));
291 fail_unless (n == 3);
292 fail_unless (gst_navigation_query_parse_commands_nth (q, 1, &cmd));
293 fail_unless (cmd == GST_NAVIGATION_COMMAND_MENU1);
295 fail_unless (gst_navigation_query_parse_commands_length (q, NULL));
296 fail_unless (gst_navigation_query_parse_commands_nth (q, 2, NULL));
301 /* GST_NAVIGATION_QUERY_ANGLES */
304 q = gst_navigation_query_new_angles ();
305 fail_unless (q != NULL);
306 fail_unless (gst_navigation_query_get_type (q) ==
307 GST_NAVIGATION_QUERY_ANGLES);
308 gst_navigation_query_set_angles (q, 4, 8);
309 fail_unless (gst_navigation_query_parse_angles (q, &angle, &angles));
310 fail_unless (angle == 4);
311 fail_unless (angles == 8);
313 fail_unless (gst_navigation_query_parse_angles (q, NULL, &angles));
314 fail_unless (gst_navigation_query_parse_angles (q, &angle, NULL));
315 fail_unless (gst_navigation_query_parse_angles (q, NULL, NULL));
325 navigation_suite (void)
327 Suite *s = suite_create ("navigation interface");
328 TCase *tc_chain = tcase_create ("notifications");
330 suite_add_tcase (s, tc_chain);
331 tcase_add_test (tc_chain, test_events);
332 tcase_add_test (tc_chain, test_messages);
333 tcase_add_test (tc_chain, test_queries);
339 main (int argc, char **argv)
343 Suite *s = navigation_suite ();
344 SRunner *sr = srunner_create (s);
346 gst_check_init (&argc, &argv);
348 srunner_run_all (sr, CK_NORMAL);
349 nf = srunner_ntests_failed (sr);