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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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/video/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 gdouble sent_delta_x, sent_delta_y;
46 gint sent_button;
47 GstNavigationCommand sent_command;
48 };
49
50 struct TestElementClass
51 {
52 GstElementClass parent_class;
53 };
54
55 GType test_element_get_type (void);
56
57 static void init_interface (GType type);
58 static void nav_send_event (GstNavigation * navigation,
59 GstStructure * structure);
60
61 G_DEFINE_TYPE_WITH_CODE (TestElement, test_element, GST_TYPE_ELEMENT,
62 init_interface (g_define_type_id));
63
64 static void
test_element_navigation_interface_init(GstNavigationInterface * iface)65 test_element_navigation_interface_init (GstNavigationInterface * iface)
66 {
67 iface->send_event = nav_send_event;
68 }
69
70 static void
init_interface(GType type)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
79 g_type_add_interface_static (type, GST_TYPE_NAVIGATION,
80 &navigation_iface_info);
81 }
82
83 static void
test_element_class_init(TestElementClass * klass)84 test_element_class_init (TestElementClass * klass)
85 {
86 }
87
88 static void
test_element_init(TestElement * this)89 test_element_init (TestElement * this)
90 {
91 }
92
93 static void
nav_send_event(GstNavigation * navigation,GstStructure * structure)94 nav_send_event (GstNavigation * navigation, GstStructure * structure)
95 {
96 GstEvent *event = gst_event_new_navigation (structure);
97 GstNavigationEventType etype = gst_navigation_event_get_type (event);
98 TestElement *self = (TestElement *) (navigation);
99
100 fail_if (etype == GST_NAVIGATION_EVENT_INVALID,
101 "Received navigation event could not be parsed");
102 fail_unless (etype == self->sent_type,
103 "Received navigation event did not match sent");
104
105 switch (etype) {
106 case GST_NAVIGATION_EVENT_KEY_PRESS:
107 case GST_NAVIGATION_EVENT_KEY_RELEASE:{
108 const gchar *key;
109 fail_unless (gst_navigation_event_parse_key_event (event, &key));
110 fail_unless (strcmp (key, self->sent_key) == 0);
111 break;
112 }
113 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
114 case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:{
115 gint button;
116 gdouble x, y;
117 fail_unless (gst_navigation_event_parse_mouse_button_event (event,
118 &button, &x, &y));
119 fail_unless (button == self->sent_button);
120 fail_unless (x == self->sent_x);
121 fail_unless (y == self->sent_y);
122 break;
123 }
124 case GST_NAVIGATION_EVENT_MOUSE_MOVE:{
125 gdouble x, y;
126 fail_unless (gst_navigation_event_parse_mouse_move_event (event, &x, &y));
127 fail_unless (x == self->sent_x);
128 fail_unless (y == self->sent_y);
129 break;
130 }
131 case GST_NAVIGATION_EVENT_MOUSE_SCROLL:{
132 gdouble x, y, delta_x, delta_y;
133 fail_unless (gst_navigation_event_parse_mouse_scroll_event (event, &x, &y,
134 &delta_x, &delta_y));
135 fail_unless (x == self->sent_x);
136 fail_unless (y == self->sent_y);
137 fail_unless (delta_x == self->sent_delta_x);
138 fail_unless (delta_y == self->sent_delta_y);
139 break;
140 }
141 case GST_NAVIGATION_EVENT_COMMAND:{
142 GstNavigationCommand cmd;
143 fail_unless (gst_navigation_event_parse_command (event, &cmd));
144 fail_unless (cmd == self->sent_command);
145 }
146 default:
147 break;
148 }
149
150 gst_event_unref (event);
151 }
152
GST_START_TEST(test_events)153 GST_START_TEST (test_events)
154 {
155 /* Create an empty GstElement that has a GstNavigation interface and then
156 * send some navigation events and validate them */
157 TestElement *test_element =
158 (TestElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
159 GstNavigationCommand cmds[] = {
160 GST_NAVIGATION_COMMAND_MENU1, GST_NAVIGATION_COMMAND_MENU2,
161 GST_NAVIGATION_COMMAND_MENU3, GST_NAVIGATION_COMMAND_MENU4,
162 GST_NAVIGATION_COMMAND_MENU5, GST_NAVIGATION_COMMAND_MENU6,
163 GST_NAVIGATION_COMMAND_MENU7, GST_NAVIGATION_COMMAND_LEFT,
164 GST_NAVIGATION_COMMAND_RIGHT, GST_NAVIGATION_COMMAND_UP,
165 GST_NAVIGATION_COMMAND_DOWN, GST_NAVIGATION_COMMAND_ACTIVATE,
166 GST_NAVIGATION_COMMAND_PREV_ANGLE, GST_NAVIGATION_COMMAND_NEXT_ANGLE
167 };
168 gint i;
169
170 test_element->sent_type = GST_NAVIGATION_EVENT_KEY_PRESS;
171 test_element->sent_key = "1";
172 gst_navigation_send_key_event (GST_NAVIGATION (test_element), "key-press",
173 "1");
174
175 test_element->sent_type = GST_NAVIGATION_EVENT_KEY_RELEASE;
176 test_element->sent_key = "2";
177 gst_navigation_send_key_event (GST_NAVIGATION (test_element), "key-release",
178 "2");
179
180 test_element->sent_type = GST_NAVIGATION_EVENT_MOUSE_MOVE;
181 test_element->sent_x = 50;
182 test_element->sent_y = 100;
183 gst_navigation_send_mouse_event (GST_NAVIGATION (test_element), "mouse-move",
184 0, 50, 100);
185
186 test_element->sent_type = GST_NAVIGATION_EVENT_MOUSE_SCROLL;
187 test_element->sent_x = 60;
188 test_element->sent_y = 120;
189 test_element->sent_delta_x = 2;
190 test_element->sent_delta_y = 3;
191 gst_navigation_send_mouse_scroll_event (GST_NAVIGATION (test_element),
192 60, 120, 2, 3);
193
194 test_element->sent_type = GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS;
195 test_element->sent_x = 10;
196 test_element->sent_y = 20;
197 test_element->sent_button = 1;
198 gst_navigation_send_mouse_event (GST_NAVIGATION (test_element),
199 "mouse-button-press", 1, 10, 20);
200
201 for (i = 0; i < G_N_ELEMENTS (cmds); i++) {
202 test_element->sent_type = GST_NAVIGATION_EVENT_COMMAND;
203 test_element->sent_command = cmds[i];
204 gst_navigation_send_command (GST_NAVIGATION (test_element), cmds[i]);
205 }
206
207 gst_object_unref (test_element);
208 }
209
210 GST_END_TEST;
211
GST_START_TEST(test_messages)212 GST_START_TEST (test_messages)
213 {
214 GstMessage *m;
215 /* GST_NAVIGATION_MESSAGE_MOUSE_OVER */
216 {
217 gboolean active;
218 m = gst_navigation_message_new_mouse_over (NULL, TRUE);
219 fail_if (m == NULL);
220 fail_unless (gst_navigation_message_get_type (m) ==
221 GST_NAVIGATION_MESSAGE_MOUSE_OVER);
222 fail_unless (GST_MESSAGE_SRC (m) == NULL);
223 fail_unless (gst_navigation_message_parse_mouse_over (m, &active));
224 fail_unless (active == TRUE);
225 gst_message_unref (m);
226
227 m = gst_navigation_message_new_mouse_over (NULL, FALSE);
228 fail_if (m == NULL);
229 fail_unless (GST_MESSAGE_SRC (m) == NULL);
230 fail_unless (gst_navigation_message_get_type (m) ==
231 GST_NAVIGATION_MESSAGE_MOUSE_OVER);
232 fail_unless (gst_navigation_message_parse_mouse_over (m, &active));
233 fail_unless (active == FALSE);
234 gst_message_unref (m);
235 }
236
237 /* GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED */
238 {
239 m = gst_navigation_message_new_commands_changed (NULL);
240 fail_if (m == NULL);
241 fail_unless (GST_MESSAGE_SRC (m) == NULL);
242 fail_unless (gst_navigation_message_get_type (m) ==
243 GST_NAVIGATION_MESSAGE_COMMANDS_CHANGED);
244 gst_message_unref (m);
245 }
246
247 /* GST_NAVIGATION_MESSAGE_ANGLES_CHANGED */
248 {
249 guint angle, angles;
250 m = gst_navigation_message_new_angles_changed (NULL, 1, 5);
251 fail_if (m == NULL);
252 fail_unless (GST_MESSAGE_SRC (m) == NULL);
253 fail_unless (gst_navigation_message_get_type (m) ==
254 GST_NAVIGATION_MESSAGE_ANGLES_CHANGED);
255 fail_unless (gst_navigation_message_parse_angles_changed (m, &angle,
256 &angles));
257 fail_unless (angle == 1);
258 fail_unless (angles == 5);
259 gst_message_unref (m);
260 }
261 }
262
263 GST_END_TEST;
264
GST_START_TEST(test_queries)265 GST_START_TEST (test_queries)
266 {
267 GstQuery *q;
268
269 /* GST_NAVIGATION_QUERY_COMMANDS */
270 {
271 guint n;
272 GstNavigationCommand cmd;
273
274 q = gst_navigation_query_new_commands ();
275 fail_unless (q != NULL);
276 fail_unless (gst_navigation_query_get_type (q) ==
277 GST_NAVIGATION_QUERY_COMMANDS);
278 gst_navigation_query_set_commands (q, 3, GST_NAVIGATION_COMMAND_LEFT,
279 GST_NAVIGATION_COMMAND_MENU1, GST_NAVIGATION_COMMAND_MENU5);
280 fail_unless (gst_navigation_query_parse_commands_length (q, &n));
281 fail_unless (n == 3);
282 fail_unless (gst_navigation_query_parse_commands_nth (q, 1, &cmd));
283 fail_unless (cmd == GST_NAVIGATION_COMMAND_MENU1);
284
285 fail_unless (gst_navigation_query_parse_commands_length (q, NULL));
286 fail_unless (gst_navigation_query_parse_commands_nth (q, 2, NULL));
287
288 gst_query_unref (q);
289 }
290
291 /* GST_NAVIGATION_QUERY_ANGLES */
292 {
293 guint angle, angles;
294 q = gst_navigation_query_new_angles ();
295 fail_unless (q != NULL);
296 fail_unless (gst_navigation_query_get_type (q) ==
297 GST_NAVIGATION_QUERY_ANGLES);
298 gst_navigation_query_set_angles (q, 4, 8);
299 fail_unless (gst_navigation_query_parse_angles (q, &angle, &angles));
300 fail_unless (angle == 4);
301 fail_unless (angles == 8);
302
303 fail_unless (gst_navigation_query_parse_angles (q, NULL, &angles));
304 fail_unless (gst_navigation_query_parse_angles (q, &angle, NULL));
305 fail_unless (gst_navigation_query_parse_angles (q, NULL, NULL));
306
307 gst_query_unref (q);
308 }
309
310 }
311
312 GST_END_TEST;
313
314 static Suite *
navigation_suite(void)315 navigation_suite (void)
316 {
317 Suite *s = suite_create ("navigation interface");
318 TCase *tc_chain = tcase_create ("notifications");
319
320 suite_add_tcase (s, tc_chain);
321 tcase_add_test (tc_chain, test_events);
322 tcase_add_test (tc_chain, test_messages);
323 tcase_add_test (tc_chain, test_queries);
324
325 return s;
326 }
327
328 GST_CHECK_MAIN (navigation);
329