get rid of constuct function types in the test runner
[swfdec.git] / test / swfdec_test_test.c
blob5e49d36c3542f71fed78da962b1d7ff46b34d4c9
1 /* Swfdec
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <string.h>
25 #include <unistd.h>
27 #include "swfdec_test_test.h"
28 #include "swfdec_test_buffer.h"
29 #include "swfdec_test_function.h"
30 #include "swfdec_test_image.h"
31 #include "swfdec_test_socket.h"
32 #include "swfdec_test_utils.h"
34 /*** PLUGIN HANDLING ***/
36 #define SWFDEC_TEST_TEST_FROM_PLUGIN(x) \
37 SWFDEC_TEST_TEST ((gpointer) ((guint8 *) (x) - G_STRUCT_OFFSET (SwfdecTestTest, plugin)))
39 char *swfdec_test_plugin_name = NULL;
41 static void
42 swfdec_test_test_quit (SwfdecTestPlugin *plugin)
44 SwfdecTestTest *test = SWFDEC_TEST_TEST_FROM_PLUGIN (plugin);
46 test->plugin_quit = TRUE;
49 static void
50 swfdec_test_test_error (SwfdecTestPlugin *plugin, const char *description)
52 SwfdecTestTest *test = SWFDEC_TEST_TEST_FROM_PLUGIN (plugin);
54 if (test->plugin_error)
55 return;
56 test->plugin_error = TRUE;
57 swfdec_test_throw (swfdec_gc_object_get_context (test), description);
60 static void
61 swfdec_test_test_trace (SwfdecTestPlugin *plugin, const char *message)
63 SwfdecTestTest *test = SWFDEC_TEST_TEST_FROM_PLUGIN (plugin);
64 gsize len = strlen (message);
65 SwfdecBuffer *buffer;
67 buffer = swfdec_buffer_new (len + 1);
68 memcpy (buffer->data, message, len);
69 buffer->data[len] = '\n';
70 swfdec_buffer_queue_push (test->trace, buffer);
73 static void
74 swfdec_test_test_launch (SwfdecTestPlugin *plugin, const char *url)
76 SwfdecTestTest *test = SWFDEC_TEST_TEST_FROM_PLUGIN (plugin);
77 gsize len = strlen (url);
78 SwfdecBuffer *buffer;
80 buffer = swfdec_buffer_new (len + 1);
81 memcpy (buffer->data, url, len);
82 buffer->data[len] = '\n';
83 swfdec_buffer_queue_push (test->launched, buffer);
86 static void
87 swfdec_test_test_request_socket (SwfdecTestPlugin *plugin,
88 SwfdecTestPluginSocket *psock)
90 SwfdecTestTest *test = SWFDEC_TEST_TEST_FROM_PLUGIN (plugin);
92 swfdec_test_socket_new (test, psock);
95 static void
96 swfdec_test_test_load_plugin (SwfdecTestTest *test, const char *filename)
98 memset (&test->plugin, 0, sizeof (SwfdecTestPlugin));
99 /* initialize test->plugin */
100 /* FIXME: This assumes filenames - do we wanna allow http? */
101 if (g_path_is_absolute (filename)) {
102 test->plugin.filename = g_strdup (filename);
103 } else {
104 char *cur = g_get_current_dir ();
105 test->plugin.filename = g_build_filename (cur, filename, NULL);
106 g_free (cur);
108 test->plugin.trace = swfdec_test_test_trace;
109 test->plugin.launch = swfdec_test_test_launch;
110 test->plugin.quit = swfdec_test_test_quit;
111 test->plugin.error = swfdec_test_test_error;
112 test->plugin.request_socket = swfdec_test_test_request_socket;
114 /* load the right values */
115 if (swfdec_test_plugin_name) {
116 void (*init) (SwfdecTestPlugin *plugin);
117 char *dir = g_build_filename (g_get_home_dir (), ".swfdec-test", NULL);
118 char *name = g_module_build_path (dir, swfdec_test_plugin_name);
119 g_free (dir);
120 test->module = g_module_open (name, G_MODULE_BIND_LOCAL);
121 if (test->module == NULL) {
122 swfdec_test_throw (swfdec_gc_object_get_context (test), "could not find player \"%s\"",
123 swfdec_test_plugin_name);
124 return;
126 if (!g_module_symbol (test->module, "swfdec_test_plugin_init", (gpointer) &init)) {
127 g_module_close (test->module);
128 test->module = NULL;
130 init (&test->plugin);
131 } else {
132 swfdec_test_plugin_swfdec_new (&test->plugin);
134 test->plugin_loaded = TRUE;
137 static void
138 swfdec_test_test_unload_plugin (SwfdecTestTest *test)
140 if (!test->plugin_loaded)
141 return;
143 /* unload all objects generated by the plugin first */
144 while (test->sockets)
145 swfdec_test_socket_close (test->sockets->data);
146 test->pending_sockets = NULL;
148 test->plugin.finish (&test->plugin);
149 g_free (test->plugin.filename);
150 if (test->module) {
151 g_module_close (test->module);
152 test->module = NULL;
154 test->plugin_quit = FALSE;
155 test->plugin_error = FALSE;
156 test->plugin_loaded = FALSE;
159 /*** SWFDEC_TEST_TEST ***/
161 G_DEFINE_TYPE (SwfdecTestTest, swfdec_test_test, SWFDEC_TYPE_AS_RELAY)
163 static void
164 swfdec_test_test_dispose (GObject *object)
166 SwfdecTestTest *test = SWFDEC_TEST_TEST (object);
168 test->plugin_error = TRUE; /* set to avoid callbacks into the engine */
169 swfdec_test_test_unload_plugin (test);
171 if (test->trace) {
172 swfdec_buffer_queue_unref (test->trace);
173 test->trace = NULL;
176 if (test->launched) {
177 swfdec_buffer_queue_unref (test->launched);
178 test->launched = NULL;
181 g_free (test->filename);
182 test->filename = NULL;
184 G_OBJECT_CLASS (swfdec_test_test_parent_class)->dispose (object);
187 static void
188 swfdec_test_test_class_init (SwfdecTestTestClass *klass)
190 GObjectClass *object_class = G_OBJECT_CLASS (klass);
192 object_class->dispose = swfdec_test_test_dispose;
195 static void
196 swfdec_test_test_init (SwfdecTestTest *test)
198 test->trace = swfdec_buffer_queue_new ();
199 test->launched = swfdec_buffer_queue_new ();
202 static void
203 swfdec_test_do_reset (SwfdecTestTest *test, const char *filename)
205 swfdec_test_test_unload_plugin (test);
206 swfdec_buffer_queue_clear (test->trace);
207 if (filename == NULL)
208 return;
210 swfdec_test_test_load_plugin (test, filename);
213 SWFDEC_TEST_FUNCTION ("Test_advance", swfdec_test_test_advance)
214 void
215 swfdec_test_test_advance (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
216 SwfdecAsValue *argv, SwfdecAsValue *retval)
218 SwfdecTestTest *test;
219 int msecs;
221 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "i", &msecs);
223 if (msecs < 0 || !test->plugin_loaded || test->plugin_error || test->plugin_quit)
224 return;
226 if (test->plugin.advance) {
227 test->plugin.advance (&test->plugin, msecs);
228 } else {
229 swfdec_test_throw (cx, "plugin doesn't implement advance");
233 SWFDEC_TEST_FUNCTION ("Test_reset", swfdec_test_test_reset)
234 void
235 swfdec_test_test_reset (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
236 SwfdecAsValue *argv, SwfdecAsValue *retval)
238 SwfdecTestTest *test;
239 const char *filename = NULL;
241 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "|s", &filename);
243 swfdec_test_do_reset (test, filename);
246 SWFDEC_TEST_FUNCTION ("Test_mouse_move", swfdec_test_test_mouse_move)
247 void
248 swfdec_test_test_mouse_move (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
249 SwfdecAsValue *argv, SwfdecAsValue *retval)
251 SwfdecTestTest *test;
252 double x, y;
254 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "nn", &x, &y);
256 if (!test->plugin_loaded || test->plugin_error || test->plugin_quit)
257 return;
259 if (test->plugin.advance) {
260 test->plugin.mouse_move (&test->plugin, x, y);
261 } else {
262 swfdec_test_throw (cx, "plugin doesn't implement mouse_move");
266 SWFDEC_TEST_FUNCTION ("Test_mouse_press", swfdec_test_test_mouse_press)
267 void
268 swfdec_test_test_mouse_press (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
269 SwfdecAsValue *argv, SwfdecAsValue *retval)
271 SwfdecTestTest *test;
272 double x, y;
273 int button = 1;
275 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "nn|i", &x, &y, &button);
277 if (!test->plugin_loaded || test->plugin_error || test->plugin_quit)
278 return;
280 button = CLAMP (button, 1, 32);
281 if (test->plugin.advance) {
282 test->plugin.mouse_press (&test->plugin, x, y, button);
283 } else {
284 swfdec_test_throw (cx, "plugin doesn't implement mouse_press");
288 SWFDEC_TEST_FUNCTION ("Test_mouse_release", swfdec_test_test_mouse_release)
289 void
290 swfdec_test_test_mouse_release (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
291 SwfdecAsValue *argv, SwfdecAsValue *retval)
293 SwfdecTestTest *test;
294 double x, y;
295 int button = 1;
297 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "nn|i", &x, &y, &button);
299 if (!test->plugin_loaded || test->plugin_error || test->plugin_quit)
300 return;
302 button = CLAMP (button, 1, 32);
303 if (test->plugin.advance) {
304 test->plugin.mouse_release (&test->plugin, x, y, button);
305 } else {
306 swfdec_test_throw (cx, "plugin doesn't implement mouse_release");
310 SWFDEC_TEST_FUNCTION ("Test_render", swfdec_test_test_render)
311 void
312 swfdec_test_test_render (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
313 SwfdecAsValue *argv, SwfdecAsValue *retval)
315 SwfdecTestTest *test;
316 SwfdecTestImage *image;
317 int x = 0, y = 0, w = 0, h = 0;
319 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "|iiii", &x, &y, &w, &h);
321 if (!test->plugin_loaded || test->plugin_error || test->plugin_quit)
322 return;
324 if (argc == 0) {
325 w = test->plugin.width;
326 h = test->plugin.height;
328 image = swfdec_test_image_new (cx, w, h);
330 if (test->plugin.screenshot) {
331 test->plugin.screenshot (&test->plugin,
332 cairo_image_surface_get_data (SWFDEC_TEST_IMAGE (image)->surface),
333 x, y, w, h);
334 SWFDEC_AS_VALUE_SET_OBJECT (retval, swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (image)));
335 } else {
336 swfdec_test_throw (cx, "plugin doesn't implement render");
340 SWFDEC_TEST_FUNCTION ("Test", swfdec_test_test_new)
341 void
342 swfdec_test_test_new (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
343 SwfdecAsValue *argv, SwfdecAsValue *retval)
345 SwfdecTestTest *test;
346 const char *filename = NULL;
348 if (!swfdec_as_context_is_constructing (cx))
349 return;
351 SWFDEC_AS_CHECK (0, NULL, "|s", &filename);
353 test = g_object_new (SWFDEC_TYPE_TEST_TEST, "context", cx, NULL);
354 swfdec_test_do_reset (test, filename);
356 swfdec_as_object_set_relay (object, SWFDEC_AS_RELAY (test));
357 SWFDEC_AS_VALUE_SET_OBJECT (retval, object);
360 SWFDEC_TEST_FUNCTION ("Test_get_launched", swfdec_test_test_get_launched)
361 void
362 swfdec_test_test_get_launched (SwfdecAsContext *cx, SwfdecAsObject *object,
363 guint argc, SwfdecAsValue *argv, SwfdecAsValue *retval)
365 SwfdecTestTest *test;
366 SwfdecTestBuffer *buf;
367 SwfdecBuffer *buffer;
368 gsize len;
370 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "");
372 len = swfdec_buffer_queue_get_depth (test->launched);
373 buffer = swfdec_buffer_queue_peek (test->launched, len);
374 buf = swfdec_test_buffer_new (cx, buffer);
375 SWFDEC_AS_VALUE_SET_OBJECT (retval, swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (buf)));
378 SWFDEC_TEST_FUNCTION ("Socket_getSocket", swfdec_test_test_getSocket)
379 void
380 swfdec_test_test_getSocket (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
381 SwfdecAsValue *argv, SwfdecAsValue *retval)
383 SwfdecTestTest *test;
385 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "");
387 if (test->pending_sockets == NULL) {
388 if (test->sockets == NULL) {
389 SWFDEC_AS_VALUE_SET_NULL (retval);
390 } else {
391 test->pending_sockets = test->sockets;
392 SWFDEC_AS_VALUE_SET_OBJECT (retval, test->pending_sockets->data);
394 } else {
395 if (test->pending_sockets->next == NULL) {
396 SWFDEC_AS_VALUE_SET_NULL (retval);
397 } else {
398 test->pending_sockets = test->pending_sockets->next;
399 SWFDEC_AS_VALUE_SET_OBJECT (retval, test->pending_sockets->data);
404 SWFDEC_TEST_FUNCTION ("Test_get_trace", swfdec_test_test_get_trace)
405 void
406 swfdec_test_test_get_trace (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
407 SwfdecAsValue *argv, SwfdecAsValue *retval)
409 SwfdecTestTest *test;
410 SwfdecTestBuffer *buf;
411 SwfdecBuffer *buffer;
412 gsize len;
414 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "");
416 len = swfdec_buffer_queue_get_depth (test->trace);
417 buffer = swfdec_buffer_queue_peek (test->trace, len);
418 buf = swfdec_test_buffer_new (cx, buffer);
419 SWFDEC_AS_VALUE_SET_OBJECT (retval, swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (buf)));
422 SWFDEC_TEST_FUNCTION ("Test_get_quit", swfdec_test_test_get_quit)
423 void
424 swfdec_test_test_get_quit (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
425 SwfdecAsValue *argv, SwfdecAsValue *retval)
427 SwfdecTestTest *test;
429 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "");
431 /* FIXME: or not quit on error? */
432 SWFDEC_AS_VALUE_SET_BOOLEAN (retval, !test->plugin_loaded || test->plugin_error || test->plugin_quit);
435 SWFDEC_TEST_FUNCTION ("Test_get_rate", swfdec_test_test_get_rate)
436 void
437 swfdec_test_test_get_rate (SwfdecAsContext *cx, SwfdecAsObject *object, guint argc,
438 SwfdecAsValue *argv, SwfdecAsValue *retval)
440 SwfdecTestTest *test;
442 SWFDEC_AS_CHECK (SWFDEC_TYPE_TEST_TEST, &test, "");
444 if (!test->plugin_loaded)
445 return;
447 swfdec_as_value_set_number (cx, retval, test->plugin.rate / 256.0);