Macro qtTrIdx() replaced by tr() and QT_TRANSLATE_NOOP()
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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 GType test_element_get_type (void);
55
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);
60
61 GST_BOILERPLATE_FULL (TestElement, test_element, GstElement, GST_TYPE_ELEMENT,
62     init_interface);
63
64 static void
65 test_element_navigation_interface_init (GstNavigationInterface * klass)
66 {
67   klass->send_event = nav_send_event;
68 }
69
70 static void
71 init_interface (GType type)
72 {
73   static const GInterfaceInfo navigation_iface_info = {
74     (GInterfaceInitFunc) test_element_navigation_interface_init,
75     NULL,
76     NULL,
77   };
78   static const GInterfaceInfo implements_iface_info = {
79     (GInterfaceInitFunc) gst_implements_interface_init,
80     NULL,
81     NULL,
82   };
83
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);
88 }
89
90 static void
91 test_element_base_init (gpointer klass)
92 {
93 }
94
95 static void
96 test_element_class_init (TestElementClass * klass)
97 {
98 }
99
100 static gboolean
101 test_element_interface_supported (GstImplementsInterface * ifacE,
102     GType interface_type)
103 {
104   if (interface_type == GST_TYPE_NAVIGATION)
105     return TRUE;
106
107   return FALSE;
108 }
109
110 static void
111 gst_implements_interface_init (GstImplementsInterfaceClass * klass)
112 {
113   klass->supported = test_element_interface_supported;
114 }
115
116 static void
117 test_element_init (TestElement * this, TestElementClass * klass)
118 {
119 }
120
121 static void
122 nav_send_event (GstNavigation * navigation, GstStructure * structure)
123 {
124   GstEvent *event = gst_event_new_navigation (structure);
125   GstNavigationEventType etype = gst_navigation_event_get_type (event);
126   TestElement *self = (TestElement *) (navigation);
127
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");
132
133   switch (etype) {
134     case GST_NAVIGATION_EVENT_KEY_PRESS:
135     case GST_NAVIGATION_EVENT_KEY_RELEASE:{
136       const gchar *key;
137       fail_unless (gst_navigation_event_parse_key_event (event, &key));
138       fail_unless (strcmp (key, self->sent_key) == 0);
139       break;
140     }
141     case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
142     case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:{
143       gint button;
144       gdouble x, y;
145       fail_unless (gst_navigation_event_parse_mouse_button_event (event,
146               &button, &x, &y));
147       fail_unless (button == self->sent_button);
148       fail_unless (x == self->sent_x);
149       fail_unless (y == self->sent_y);
150       break;
151     }
152     case GST_NAVIGATION_EVENT_MOUSE_MOVE:{
153       gdouble x, y;
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);
157       break;
158     }
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);
163     }
164     default:
165       break;
166   }
167
168   gst_event_unref (event);
169 }
170
171 GST_START_TEST (test_events)
172 {
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
185   };
186   gint i;
187
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",
191       "1");
192
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",
196       "2");
197
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",
202       0, 50, 100);
203
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);
210
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]);
215   }
216
217   gst_object_unref (test_element);
218 }
219
220 GST_END_TEST;
221
222 GST_START_TEST (test_messages)
223 {
224   GstMessage *m;
225   /* GST_NAVIGATION_MESSAGE_MOUSE_OVER */
226   {
227     gboolean active;
228     m = gst_navigation_message_new_mouse_over (NULL, TRUE);
229     fail_if (m == NULL);
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);
236
237     m = gst_navigation_message_new_mouse_over (NULL, FALSE);
238     fail_if (m == NULL);
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);
245   }
246
247   /* GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED */
248   {
249     m = gst_navigation_message_new_commands_changed (NULL);
250     fail_if (m == 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);
255   }
256
257   /* GST_NAVIGATION_MESSAGE_ANGLES_CHANGED */
258   {
259     guint angle, angles;
260     m = gst_navigation_message_new_angles_changed (NULL, 1, 5);
261     fail_if (m == NULL);
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,
266             &angles));
267     fail_unless (angle == 1);
268     fail_unless (angles == 5);
269     gst_message_unref (m);
270   }
271 }
272
273 GST_END_TEST;
274
275 GST_START_TEST (test_queries)
276 {
277   GstQuery *q;
278
279   /* GST_NAVIGATION_QUERY_COMMANDS */
280   {
281     guint n;
282     GstNavigationCommand cmd;
283
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);
294
295     fail_unless (gst_navigation_query_parse_commands_length (q, NULL));
296     fail_unless (gst_navigation_query_parse_commands_nth (q, 2, NULL));
297
298     gst_query_unref (q);
299   }
300
301   /* GST_NAVIGATION_QUERY_ANGLES */
302   {
303     guint angle, 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);
312
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));
316
317     gst_query_unref (q);
318   }
319
320 }
321
322 GST_END_TEST;
323
324 static Suite *
325 navigation_suite (void)
326 {
327   Suite *s = suite_create ("navigation interface");
328   TCase *tc_chain = tcase_create ("notifications");
329
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);
334
335   return s;
336 }
337
338 int
339 main (int argc, char **argv)
340 {
341   int nf;
342
343   Suite *s = navigation_suite ();
344   SRunner *sr = srunner_create (s);
345
346   gst_check_init (&argc, &argv);
347
348   srunner_run_all (sr, CK_NORMAL);
349   nf = srunner_ntests_failed (sr);
350   srunner_free (sr);
351
352   return nf;
353 }