add more new objects
[swfdec.git] / test / swfdec_test.c
blob2da4af0c8a69ff1776330803e237bb2e0acdd1f3
1 /* Swfdec
2 * Copyright (C) 2008 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 <stdlib.h>
25 #include <string.h>
27 #include <swfdec/swfdec.h>
28 /* FIXME: no internal headers please */
29 #include <swfdec/swfdec_as_array.h>
30 #include <swfdec/swfdec_as_internal.h>
32 #include "swfdec_test_function.h"
33 #include "swfdec_test_initialize.h"
34 #include "swfdec_test_test.h"
36 /*** VERIFICATION OF ENVIRONMENT ***/
38 static gboolean
39 check_cairo (gboolean verbose)
41 #define CAIRO_MIN_MAJOR 1
42 #define CAIRO_MIN_MINOR 7
43 #define CAIRO_MIN_MICRO 1
44 if (CAIRO_VERSION < CAIRO_VERSION_ENCODE (CAIRO_MIN_MAJOR, CAIRO_MIN_MINOR, CAIRO_MIN_MICRO)) {
45 g_print ("ERROR: Cairo version %s cannot be used to run tests; must be at least %u.%u.%u.\n",
46 CAIRO_VERSION_STRING, CAIRO_MIN_MAJOR, CAIRO_MIN_MINOR, CAIRO_MIN_MICRO);
47 return FALSE;
48 } else if (verbose) {
49 g_print (" OK: Cairo version %s is ok; must be at least %u.%u.%u.\n",
50 CAIRO_VERSION_STRING, CAIRO_MIN_MAJOR, CAIRO_MIN_MINOR, CAIRO_MIN_MICRO);
52 return TRUE;
55 /* FIXME */
56 #include <swfdec/swfdec_audio_decoder.h>
57 static gboolean
58 check_codecs (gboolean verbose)
60 if (!swfdec_audio_decoder_prepare (SWFDEC_AUDIO_CODEC_MP3,
61 swfdec_audio_format_new (44100, 2, TRUE), NULL)) {
62 g_print ("ERROR: MP3 support is not available.\n");
63 return FALSE;
64 } else if (verbose) {
65 g_print (" OK: MP3 support is available.\n");
67 return TRUE;
70 static gboolean
71 check_system (gboolean verbose)
73 gboolean ret = TRUE;
75 /* We want to run all checks, so don't use && here */
76 ret &= check_cairo (verbose);
77 ret &= check_codecs (verbose);
79 return ret;
83 /* Start of script file */
84 #define SWFDEC_TEST_FILE_ID "Swfdec Test Script\0\1"
85 /* Flash version the script engine runs in */
86 #define SWFDEC_TEST_VERSION 8
88 static SwfdecScript *
89 load_script (const char *filename)
91 SwfdecBuffer *file, *buffer;
92 SwfdecScript *script;
93 GError *error = NULL;
95 if (filename == NULL)
96 filename = "default.sts";
98 file = swfdec_buffer_new_from_file (filename, &error);
99 if (file == NULL) {
100 g_print ("ERROR: %s\n", error->message);
101 g_error_free (error);
102 return NULL;
104 if (file->length < sizeof (SWFDEC_TEST_FILE_ID) + 1 ||
105 memcmp (file->data, SWFDEC_TEST_FILE_ID, sizeof (SWFDEC_TEST_FILE_ID) != 0)) {
106 g_print ("ERROR: %s is not a Swfdec test script\n", filename);
107 swfdec_buffer_unref (file);
108 return NULL;
110 buffer = swfdec_buffer_new_subbuffer (file, sizeof (SWFDEC_TEST_FILE_ID),
111 file->length - sizeof (SWFDEC_TEST_FILE_ID));
112 swfdec_buffer_unref (file);
113 script = swfdec_script_new (buffer, "main", SWFDEC_TEST_VERSION);
114 return script;
118 main (int argc, char **argv)
120 char *script_filename = NULL;
121 GError *error = NULL;
122 SwfdecAsContext *context;
123 SwfdecAsObject *array;
124 SwfdecScript *script;
125 SwfdecAsValue val;
126 int i, ret;
127 gboolean dump = FALSE;
128 gboolean no_check = FALSE, only_check = FALSE;
130 GOptionEntry options[] = {
131 { "dump", 'd', 0, G_OPTION_ARG_NONE, &dump, "dump informative output on failure", FALSE },
132 { "no-check", 0, 0, G_OPTION_ARG_NONE, &no_check, "don't check if the system is ok for running the testsuite", FALSE },
133 { "self-check", 0, 0, G_OPTION_ARG_NONE, &only_check, "run a system check and exit", FALSE },
134 { "player", 'p', 0, G_OPTION_ARG_STRING, &swfdec_test_plugin_name, "player to test", "NAME" },
135 { "script", 's', 0, G_OPTION_ARG_STRING, &script_filename, "script to execute if not ./default.sts", "FILENAME" },
136 { NULL }
138 GOptionContext *ctx;
140 /* set the right warning levels */
141 g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING);
142 /* by default get rid of the loads of warnings the tests produce */
143 g_setenv ("SWFDEC_DEBUG", "2", FALSE);
145 g_thread_init (NULL);
146 swfdec_init ();
148 ctx = g_option_context_new ("");
149 g_option_context_add_main_entries (ctx, options, "options");
150 g_option_context_parse (ctx, &argc, &argv, &error);
151 g_option_context_free (ctx);
153 if (error) {
154 g_printerr ("ERROR: wrong command line arguments: %s\n", error->message);
155 g_error_free (error);
156 return EXIT_FAILURE;
159 if (only_check || !no_check) {
160 gboolean result = check_system (only_check);
161 if (!result) {
162 g_print ("ERROR: System checked failed, aborting. Use --no-check to disable.\n");
163 return 1;
164 } else if (only_check) {
165 return 0;
168 g_assert (!only_check);
170 /* allow env vars instead of options - eases running make check with different settings */
171 if (swfdec_test_plugin_name == NULL)
172 swfdec_test_plugin_name = g_strdup (g_getenv ("SWFDEC_TEST_PLAYER"));
174 script = load_script (script_filename);
175 g_free (script_filename);
176 if (script == NULL)
177 return EXIT_FAILURE;
179 context = g_object_new (SWFDEC_TYPE_AS_CONTEXT, NULL);
180 swfdec_as_context_startup (context);
182 SWFDEC_AS_VALUE_SET_BOOLEAN (&val, dump);
183 swfdec_as_object_set_variable (context->global,
184 swfdec_as_context_get_string (context, "dump"), &val);
186 swfdec_test_function_init_context (context);
187 swfdec_as_context_run_init_script (context, swfdec_test_initialize,
188 sizeof (swfdec_test_initialize), SWFDEC_TEST_VERSION);
190 array = swfdec_as_array_new (context);
191 if (array == NULL) {
192 g_print ("ERROR: Not enough memory");
193 return EXIT_FAILURE;
195 if (argc < 2) {
196 GDir *dir;
197 const char *file;
198 dir = g_dir_open (".", 0, NULL);
199 while ((file = g_dir_read_name (dir))) {
200 if (!g_str_has_suffix (file, ".swf"))
201 continue;
202 SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_get_string (context, file));
203 swfdec_as_array_push (SWFDEC_AS_ARRAY (array), &val);
205 g_dir_close (dir);
206 } else {
207 for (i = 1; i < argc; i++) {
208 SWFDEC_AS_VALUE_SET_STRING (&val, swfdec_as_context_get_string (context, argv[i]));
209 swfdec_as_array_push (SWFDEC_AS_ARRAY (array), &val);
212 SWFDEC_AS_VALUE_SET_OBJECT (&val, array);
213 swfdec_as_object_set_variable (context->global,
214 swfdec_as_context_get_string (context, "filenames"), &val);
215 swfdec_as_object_run (context->global, script);
216 if (swfdec_as_context_catch (context, &val)) {
217 g_print ("ERROR: %s\n", swfdec_as_value_to_string (context, &val));
218 ret = EXIT_FAILURE;
219 } else {
220 g_print ("SUCCESS\n");
221 ret = EXIT_SUCCESS;
224 swfdec_script_unref (script);
225 g_object_unref (context);
227 return ret;