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 static void init_interface (GType type);
55 static void gst_implements_interface_init (GstImplementsInterfaceClass * klass);
56 static void nav_send_event (GstNavigation * navigation,
57 GstStructure * structure);
59 GST_BOILERPLATE_FULL (TestElement, test_element, GstElement, GST_TYPE_ELEMENT,
63 test_element_navigation_interface_init (GstNavigationInterface * klass)
65 klass->send_event = nav_send_event;
69 init_interface (GType type)
71 static const GInterfaceInfo navigation_iface_info = {
72 (GInterfaceInitFunc) test_element_navigation_interface_init,
76 static const GInterfaceInfo implements_iface_info = {
77 (GInterfaceInitFunc) gst_implements_interface_init,
82 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
83 &implements_iface_info);
84 g_type_add_interface_static (type, GST_TYPE_NAVIGATION,
85 &navigation_iface_info);
89 test_element_base_init (gpointer klass)
94 test_element_class_init (TestElementClass * klass)
99 test_element_interface_supported (GstImplementsInterface * ifacE,
100 GType interface_type)
102 if (interface_type == GST_TYPE_NAVIGATION)
109 gst_implements_interface_init (GstImplementsInterfaceClass * klass)
111 klass->supported = test_element_interface_supported;
115 test_element_init (TestElement * this, TestElementClass * klass)
120 nav_send_event (GstNavigation * navigation, GstStructure * structure)
122 GstEvent *event = gst_event_new_navigation (structure);
123 GstNavigationEventType etype = gst_navigation_event_get_type (event);
124 TestElement *self = (TestElement *) (navigation);
126 fail_if (etype == GST_NAVIGATION_EVENT_INVALID,
127 "Received navigation event could not be parsed");
128 fail_unless (etype == self->sent_type,
129 "Received navigation event did not match sent");
132 case GST_NAVIGATION_EVENT_KEY_PRESS:
133 case GST_NAVIGATION_EVENT_KEY_RELEASE:{
135 fail_unless (gst_navigation_event_parse_key_event (event, &key));
136 fail_unless (strcmp (key, self->sent_key) == 0);
139 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
140 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:{
143 fail_unless (gst_navigation_event_parse_mouse_button_event (event,
145 fail_unless (button == self->sent_button);
146 fail_unless (x == self->sent_x);
147 fail_unless (y == self->sent_y);
150 case GST_NAVIGATION_EVENT_MOUSE_MOVE:{
152 fail_unless (gst_navigation_event_parse_mouse_move_event (event, &x, &y));
153 fail_unless (x == self->sent_x);
154 fail_unless (y == self->sent_y);
157 case GST_NAVIGATION_EVENT_COMMAND:{
158 GstNavigationCommand cmd;
159 fail_unless (gst_navigation_event_parse_command (event, &cmd));
160 fail_unless (cmd == self->sent_command);
166 gst_event_unref (event);
169 GST_START_TEST (test_events)
171 /* Create an empty GstElement that has a GstNavigation interface and then
172 * send some navigation events and validate them */
173 TestElement *test_element =
174 (TestElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
175 GstNavigationCommand cmds[] = {
176 GST_NAVIGATION_COMMAND_MENU1, GST_NAVIGATION_COMMAND_MENU2,
177 GST_NAVIGATION_COMMAND_MENU3, GST_NAVIGATION_COMMAND_MENU4,
178 GST_NAVIGATION_COMMAND_MENU5, GST_NAVIGATION_COMMAND_MENU6,
179 GST_NAVIGATION_COMMAND_MENU7, GST_NAVIGATION_COMMAND_LEFT,
180 GST_NAVIGATION_COMMAND_RIGHT, GST_NAVIGATION_COMMAND_UP,
181 GST_NAVIGATION_COMMAND_DOWN, GST_NAVIGATION_COMMAND_ACTIVATE,
182 GST_NAVIGATION_COMMAND_PREV_ANGLE, GST_NAVIGATION_COMMAND_NEXT_ANGLE
186 test_element->sent_type = GST_NAVIGATION_EVENT_KEY_PRESS;
187 test_element->sent_key = "1";
188 gst_navigation_send_key_event (GST_NAVIGATION (test_element), "key-press",
191 test_element->sent_type = GST_NAVIGATION_EVENT_KEY_RELEASE;
192 test_element->sent_key = "2";
193 gst_navigation_send_key_event (GST_NAVIGATION (test_element), "key-release",
196 test_element->sent_type = GST_NAVIGATION_EVENT_MOUSE_MOVE;
197 test_element->sent_x = 50;
198 test_element->sent_y = 100;
199 gst_navigation_send_mouse_event (GST_NAVIGATION (test_element), "mouse-move",
202 test_element->sent_type = GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS;
203 test_element->sent_x = 10;
204 test_element->sent_y = 20;
205 test_element->sent_button = 1;
206 gst_navigation_send_mouse_event (GST_NAVIGATION (test_element),
207 "mouse-button-press", 1, 10, 20);
209 for (i = 0; i < G_N_ELEMENTS (cmds); i++) {
210 test_element->sent_type = GST_NAVIGATION_EVENT_COMMAND;
211 test_element->sent_command = cmds[i];
212 gst_navigation_send_command (GST_NAVIGATION (test_element), cmds[i]);
215 gst_object_unref (test_element);
220 GST_START_TEST (test_messages)
223 /* GST_NAVIGATION_MESSAGE_MOUSE_OVER */
226 m = gst_navigation_message_new_mouse_over (NULL, TRUE);
228 fail_unless (gst_navigation_message_get_type (m) ==
229 GST_NAVIGATION_MESSAGE_MOUSE_OVER);
230 fail_unless (GST_MESSAGE_SRC (m) == NULL);
231 fail_unless (gst_navigation_message_parse_mouse_over (m, &active));
232 fail_unless (active == TRUE);
233 gst_message_unref (m);
235 m = gst_navigation_message_new_mouse_over (NULL, FALSE);
237 fail_unless (GST_MESSAGE_SRC (m) == NULL);
238 fail_unless (gst_navigation_message_get_type (m) ==
239 GST_NAVIGATION_MESSAGE_MOUSE_OVER);
240 fail_unless (gst_navigation_message_parse_mouse_over (m, &active));
241 fail_unless (active == FALSE);
242 gst_message_unref (m);
245 /* GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED */
247 m = gst_navigation_message_new_commands_changed (NULL);
249 fail_unless (GST_MESSAGE_SRC (m) == NULL);
250 fail_unless (gst_navigation_message_get_type (m) ==
251 GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED);
252 gst_message_unref (m);
255 /* GST_NAVIGATION_MESSAGE_ANGLES_CHANGED */
258 m = gst_navigation_message_new_angles_changed (NULL, 1, 5);
260 fail_unless (GST_MESSAGE_SRC (m) == NULL);
261 fail_unless (gst_navigation_message_get_type (m) ==
262 GST_NAVIGATION_MESSAGE_ANGLES_CHANGED);
263 fail_unless (gst_navigation_message_parse_angles_changed (m, &angle,
265 fail_unless (angle == 1);
266 fail_unless (angles == 5);
267 gst_message_unref (m);
273 GST_START_TEST (test_queries)
277 /* GST_NAVIGATION_QUERY_COMMANDS */
280 GstNavigationCommand cmd;
282 q = gst_navigation_query_new_commands ();
283 fail_unless (q != NULL);
284 fail_unless (gst_navigation_query_get_type (q) ==
285 GST_NAVIGATION_QUERY_COMMANDS);
286 gst_navigation_query_set_commands (q, 3, GST_NAVIGATION_COMMAND_LEFT,
287 GST_NAVIGATION_COMMAND_MENU1, GST_NAVIGATION_COMMAND_MENU5);
288 fail_unless (gst_navigation_query_parse_commands_length (q, &n));
289 fail_unless (n == 3);
290 fail_unless (gst_navigation_query_parse_commands_nth (q, 1, &cmd));
291 fail_unless (cmd == GST_NAVIGATION_COMMAND_MENU1);
293 fail_unless (gst_navigation_query_parse_commands_length (q, NULL));
294 fail_unless (gst_navigation_query_parse_commands_nth (q, 2, NULL));
299 /* GST_NAVIGATION_QUERY_ANGLES */
302 q = gst_navigation_query_new_angles ();
303 fail_unless (q != NULL);
304 fail_unless (gst_navigation_query_get_type (q) ==
305 GST_NAVIGATION_QUERY_ANGLES);
306 gst_navigation_query_set_angles (q, 4, 8);
307 fail_unless (gst_navigation_query_parse_angles (q, &angle, &angles));
308 fail_unless (angle == 4);
309 fail_unless (angles == 8);
311 fail_unless (gst_navigation_query_parse_angles (q, NULL, &angles));
312 fail_unless (gst_navigation_query_parse_angles (q, &angle, NULL));
313 fail_unless (gst_navigation_query_parse_angles (q, NULL, NULL));
323 navigation_suite (void)
325 Suite *s = suite_create ("navigation interface");
326 TCase *tc_chain = tcase_create ("notifications");
328 suite_add_tcase (s, tc_chain);
329 tcase_add_test (tc_chain, test_events);
330 tcase_add_test (tc_chain, test_messages);
331 tcase_add_test (tc_chain, test_queries);
337 main (int argc, char **argv)
341 Suite *s = navigation_suite ();
342 SRunner *sr = srunner_create (s);
344 gst_check_init (&argc, &argv);
346 srunner_run_all (sr, CK_NORMAL);
347 nf = srunner_ntests_failed (sr);