gio: Remove unused function
[platform/upstream/gstreamer.git] / tests / check / libs / navigation.c
1 /* GStreamer
2  *
3  * unit tests for the navigation interface library
4  *
5  * Copyright (C) 2009 Jan Schmidt <thaytan@noraisin.net>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/check/gstcheck.h>
28
29 #include <gst/interfaces/navigation.h>
30
31 #include <string.h>
32
33 #define TEST_ELEMENT_TYPE (test_element_get_type())
34
35 typedef struct TestElement TestElement;
36 typedef struct TestElementClass TestElementClass;
37
38 struct TestElement
39 {
40   GstElement parent;
41
42   GstNavigationEventType sent_type;
43   const gchar *sent_key;
44   gdouble sent_x, sent_y;
45   gint sent_button;
46   GstNavigationCommand sent_command;
47 };
48
49 struct TestElementClass
50 {
51   GstElementClass parent_class;
52 };
53
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);
58
59 GST_BOILERPLATE_FULL (TestElement, test_element, GstElement, GST_TYPE_ELEMENT,
60     init_interface);
61
62 static void
63 test_element_navigation_interface_init (GstNavigationInterface * klass)
64 {
65   klass->send_event = nav_send_event;
66 }
67
68 static void
69 init_interface (GType type)
70 {
71   static const GInterfaceInfo navigation_iface_info = {
72     (GInterfaceInitFunc) test_element_navigation_interface_init,
73     NULL,
74     NULL,
75   };
76   static const GInterfaceInfo implements_iface_info = {
77     (GInterfaceInitFunc) gst_implements_interface_init,
78     NULL,
79     NULL,
80   };
81
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);
86 }
87
88 static void
89 test_element_base_init (gpointer klass)
90 {
91 }
92
93 static void
94 test_element_class_init (TestElementClass * klass)
95 {
96 }
97
98 static gboolean
99 test_element_interface_supported (GstImplementsInterface * ifacE,
100     GType interface_type)
101 {
102   if (interface_type == GST_TYPE_NAVIGATION)
103     return TRUE;
104
105   return FALSE;
106 }
107
108 static void
109 gst_implements_interface_init (GstImplementsInterfaceClass * klass)
110 {
111   klass->supported = test_element_interface_supported;
112 }
113
114 static void
115 test_element_init (TestElement * this, TestElementClass * klass)
116 {
117 }
118
119 static void
120 nav_send_event (GstNavigation * navigation, GstStructure * structure)
121 {
122   GstEvent *event = gst_event_new_navigation (structure);
123   GstNavigationEventType etype = gst_navigation_event_get_type (event);
124   TestElement *self = (TestElement *) (navigation);
125
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");
130
131   switch (etype) {
132     case GST_NAVIGATION_EVENT_KEY_PRESS:
133     case GST_NAVIGATION_EVENT_KEY_RELEASE:{
134       const gchar *key;
135       fail_unless (gst_navigation_event_parse_key_event (event, &key));
136       fail_unless (strcmp (key, self->sent_key) == 0);
137       break;
138     }
139     case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
140     case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:{
141       gint button;
142       gdouble x, y;
143       fail_unless (gst_navigation_event_parse_mouse_button_event (event,
144               &button, &x, &y));
145       fail_unless (button == self->sent_button);
146       fail_unless (x == self->sent_x);
147       fail_unless (y == self->sent_y);
148       break;
149     }
150     case GST_NAVIGATION_EVENT_MOUSE_MOVE:{
151       gdouble x, y;
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);
155       break;
156     }
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);
161     }
162     default:
163       break;
164   }
165
166   gst_event_unref (event);
167 }
168
169 GST_START_TEST (test_events)
170 {
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
183   };
184   gint i;
185
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",
189       "1");
190
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",
194       "2");
195
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",
200       0, 50, 100);
201
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);
208
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]);
213   }
214
215   gst_object_unref (test_element);
216 }
217
218 GST_END_TEST;
219
220 GST_START_TEST (test_messages)
221 {
222   GstMessage *m;
223   /* GST_NAVIGATION_MESSAGE_MOUSE_OVER */
224   {
225     gboolean active;
226     m = gst_navigation_message_new_mouse_over (NULL, TRUE);
227     fail_if (m == NULL);
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);
234
235     m = gst_navigation_message_new_mouse_over (NULL, FALSE);
236     fail_if (m == NULL);
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);
243   }
244
245   /* GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED */
246   {
247     m = gst_navigation_message_new_commands_changed (NULL);
248     fail_if (m == 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);
253   }
254
255   /* GST_NAVIGATION_MESSAGE_ANGLES_CHANGED */
256   {
257     guint angle, angles;
258     m = gst_navigation_message_new_angles_changed (NULL, 1, 5);
259     fail_if (m == NULL);
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,
264             &angles));
265     fail_unless (angle == 1);
266     fail_unless (angles == 5);
267     gst_message_unref (m);
268   }
269 }
270
271 GST_END_TEST;
272
273 GST_START_TEST (test_queries)
274 {
275   GstQuery *q;
276
277   /* GST_NAVIGATION_QUERY_COMMANDS */
278   {
279     guint n;
280     GstNavigationCommand cmd;
281
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);
292
293     fail_unless (gst_navigation_query_parse_commands_length (q, NULL));
294     fail_unless (gst_navigation_query_parse_commands_nth (q, 2, NULL));
295
296     gst_query_unref (q);
297   }
298
299   /* GST_NAVIGATION_QUERY_ANGLES */
300   {
301     guint angle, 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);
310
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));
314
315     gst_query_unref (q);
316   }
317
318 }
319
320 GST_END_TEST;
321
322 static Suite *
323 navigation_suite (void)
324 {
325   Suite *s = suite_create ("navigation interface");
326   TCase *tc_chain = tcase_create ("notifications");
327
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);
332
333   return s;
334 }
335
336 int
337 main (int argc, char **argv)
338 {
339   int nf;
340
341   Suite *s = navigation_suite ();
342   SRunner *sr = srunner_create (s);
343
344   gst_check_init (&argc, &argv);
345
346   srunner_run_all (sr, CK_NORMAL);
347   nf = srunner_ntests_failed (sr);
348   srunner_free (sr);
349
350   return nf;
351 }