Add automated tests on function-type FSCommand parameter passing.
[gnash.git] / testsuite / misc-ming.all / ming_utils.c
blob235cb63f9d5fda70f9a1735f3c5a3291c22d95f0
1 /*
2 * Copyright (C) 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #include "ming_utils.h"
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <ming.h>
25 #include <stdarg.h>
27 void add_xtrace_function_clip(SWFMovieClip mo, SWFBlock font, int depth, int x, int y, int width, int height);
28 static SWFAction get_dejagnu_actions(void);
30 static const char* asciichars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{};:.>,</?'\"\\|`~\t";
32 void
33 add_xtrace_function_clip(SWFMovieClip mc, SWFBlock font, int depth, int x, int y, int width, int height)
35 SWFTextField tf;
36 SWFDisplayItem it;
37 int flags;
39 tf = newSWFTextField();
41 SWFTextField_setFont(tf, font);
43 /* setting flags seem unneeded */
44 flags = 0;
45 //flags |= SWFTEXTFIELD_USEFONT;
46 flags |= SWFTEXTFIELD_WORDWRAP;
47 //flags |= SWFTEXTFIELD_NOEDIT;
48 SWFTextField_setFlags(tf, flags);
50 /* Add all ascii chars */
51 SWFTextField_addChars(tf, asciichars);
52 SWFTextField_addString(tf, " - xtrace enabled -\n");
54 SWFTextField_setBounds(tf, width, height);
57 * Hopefully we have a *single* _root.
59 SWFTextField_setVariableName(tf, "_root._trace_text");
62 * Set flags explicitly so that the field is selectable
63 * and you can cut&paste results.
64 * (the default seems to include SWFTEXTFIELD_NOSELECT)
66 SWFTextField_setFlags(tf, SWFTEXTFIELD_NOEDIT);
68 /*SWFTextField_setHeight(tf, 240);*/
69 /*SWFTextField_setColor(tf, 0x00, 0x00, 0x00, 0xff);*/
70 /*SWFTextField_setAlignment(tf, SWFTEXTFIELD_ALIGN_LEFT);*/
71 /*SWFTextField_setLeftMargin(tf, 0);*/
72 /*SWFTextField_setRightMargin(tf, 0);*/
73 /*SWFTextField_setIndentation(tf, 0);*/
74 /*SWFTextField_setLineSpacing(tf, 40);*/
75 /*SWFTextField_setLineSpacing(tf, 40);*/
77 it = SWFMovieClip_add(mc, (SWFBlock)tf);
78 SWFDisplayItem_moveTo(it, x, y);
79 SWFDisplayItem_setDepth(it, depth);
80 SWFDisplayItem_setName(it, "_xtrace_win");
82 add_clip_actions(mc,
83 " _root.xtrace = function (msg) { "
84 " _root._trace_text += msg + '\n'; "
85 "};");
89 void add_xtrace_function(SWFMovie mo, SWFBlock font, int depth, int x, int y, int width, int height);
91 void
92 add_xtrace_function(SWFMovie mo, SWFBlock font, int depth, int x, int y, int width, int height)
94 SWFTextField tf;
95 SWFDisplayItem it;
97 tf = newSWFTextField();
99 SWFTextField_setFont(tf, font);
101 /* setting flags seem unneeded */
102 /*SWFTextField_setFlags(tf, SWFTEXTFIELD_USEFONT|SWFTEXTFIELD_NOEDIT);*/
104 /* Add all ascii chars */
105 SWFTextField_addChars(tf, asciichars);
106 SWFTextField_addString(tf, " - xtrace enabled -\n");
108 SWFTextField_setBounds(tf, width, height);
111 * Hopefully we have a *single* _root.
113 SWFTextField_setVariableName(tf, "_root._trace_text");
115 /*SWFTextField_setHeight(tf, 240);*/
116 /*SWFTextField_setColor(tf, 0x00, 0x00, 0x00, 0xff);*/
117 /*SWFTextField_setAlignment(tf, SWFTEXTFIELD_ALIGN_LEFT);*/
118 /*SWFTextField_setLeftMargin(tf, 0);*/
119 /*SWFTextField_setRightMargin(tf, 0);*/
120 /*SWFTextField_setIndentation(tf, 0);*/
121 /*SWFTextField_setLineSpacing(tf, 40);*/
122 /*SWFTextField_setLineSpacing(tf, 40);*/
124 it = SWFMovie_add(mo, (SWFBlock)tf);
125 SWFDisplayItem_moveTo(it, x, y);
126 SWFDisplayItem_setDepth(it, depth);
127 SWFDisplayItem_setName(it, "_xtrace_win");
129 add_actions(mo,
130 " _root.xtrace = function (msg) { "
131 " trace (msg); "
132 " _root._trace_text += msg + '\n'; "
133 "};");
136 SWFShape
137 make_square(int x, int y, int width, int height, byte r, byte g, byte b)
139 SWFShape sh = newSWFShape();
140 SWFShape_setLineStyle(sh, 1, r, g, b, 255);
141 SWFShape_movePenTo(sh, x, y);
142 SWFShape_drawLineTo(sh, x, y+height);
143 SWFShape_drawLineTo(sh, x+width, y+height);
144 SWFShape_drawLineTo(sh, x+width, y);
145 SWFShape_drawLineTo(sh, x, y);
147 return sh;
150 SWFShape
151 make_fill_square(int x, int y, int width, int height, byte or, byte og, byte ob, byte fr, byte fg, byte fb)
153 SWFShape sh = newSWFShape();
154 SWFFillStyle fs = SWFShape_addSolidFillStyle(sh, fr, fg, fb, 255);
155 SWFShape_setLineStyle(sh, 1, or, og, ob, 255);
156 SWFShape_setLeftFillStyle(sh, fs);
157 SWFShape_movePenTo(sh, x, y);
158 SWFShape_drawLineTo(sh, x, y+height);
159 SWFShape_drawLineTo(sh, x+width, y+height);
160 SWFShape_drawLineTo(sh, x+width, y);
161 SWFShape_drawLineTo(sh, x, y);
163 return sh;
166 static SWFAction
167 get_dejagnu_actions()
169 static const char *buf =
170 "TestState = function() {\n"
171 " this.passed = 0;\n"
172 " this.failed = 0;\n"
173 " this.xpassed = 0;\n"
174 " this.xfailed = 0;\n"
175 " this.untest = 0;\n"
176 " this.unresolve = 0;\n"
177 "};\n"
178 "TestState.prototype.note = function (msg) {\n"
179 " _root.xtrace(msg);\n"
180 " trace (msg); "
181 "};\n"
182 "TestState.prototype.fail = function (why) {\n"
183 " this.failed++;\n"
184 " var msg = 'FAILED: '+why;\n"
185 " _root.xtrace(msg);\n"
186 " trace (msg); "
187 "};\n"
188 "TestState.prototype.xfail = function(why) {\n"
189 " this.xfailed++;\n"
190 " var msg = 'XFAILED: '+why;\n"
191 " _root.xtrace(msg);\n"
192 " trace (msg); "
193 "};\n"
194 "TestState.prototype.pass = function(why) {\n"
195 " this.passed++;\n"
196 " var msg = 'PASSED: '+why;\n"
197 // don't visually print successes, still use 'trace' for them
198 // " _root.xtrace(msg);\n"
199 " trace (msg); "
200 "};\n"
201 "TestState.prototype.xpass = function(why) {\n"
202 " this.xpassed++;\n"
203 " var msg = 'XPASSED: '+why;\n"
204 // don't visually print successes, even if unexpected,
205 // still use 'trace' for them
206 // " _root.xtrace(msg);\n"
207 " trace (msg); "
208 "};\n"
209 "TestState.prototype.printtotals = function() {\n"
210 " this.note('#passed: '+ this.passed);\n"
211 " this.note('#failed: '+ this.failed);\n"
212 " if ( this.xpassed ) {\n"
213 " this.note('#unexpected successes: '+ this.xpassed);\n"
214 " }\n"
215 " if ( this.xfailed ) {\n"
216 " this.note('#expected failures: '+ this.xfailed);\n"
217 " }\n"
218 " this.note('#total tests run: '+ this.testcount());\n"
219 "};\n"
220 "TestState.prototype.totals = function(exp, msg) {\n"
221 " var obt = this.testcount(); "
222 " if ( exp != undefined && obt != exp ) { "
223 " this.fail('Tests run '+obt+' (expected '+exp+') ['+msg+']'); "
224 " }"
225 " this.printtotals();"
226 "};\n"
227 "TestState.prototype.xtotals = function(exp, msg) {\n"
228 " var obt = this.testcount(); "
229 " if ( exp != undefined && obt != exp ) { "
230 " this.xfail('Tests run '+obt+' (expected '+exp+') ['+msg+']'); "
231 " } else {"
232 " this.xpass('Tests run: '+obt+' ['+msg+']'); "
233 " }"
234 " this.printtotals();"
235 "};\n"
236 "TestState.prototype.testcount = function() {\n"
237 " c=this.passed;\n"
238 " c+=this.failed;\n"
239 " if ( this.xpassed ) c+=this.xpassed;\n"
240 " if ( this.xfailed ) c+=this.xfailed;\n"
241 " return c;"
242 "};\n"
244 "_root.runtest = new TestState();\n"
246 "_root.check_equals = function(obt, exp, msg) {"
247 " if ( obt == exp ) "
248 " {"
249 " if ( msg != undefined ) _root.runtest.pass(obt+' == '+exp+' ('+msg+')');"
250 " else _root.runtest.pass(obt+' == '+exp);"
251 " }"
252 " else "
253 " {"
254 " if ( msg != undefined )"
255 " {"
256 " _root.runtest.fail('expected: '+exp+' , obtained: '+obt+' ('+msg+')');"
257 " }"
258 " else _root.runtest.fail('expected: '+exp+' , obtained: '+obt);"
259 " }"
260 "};\n"
262 "_root.xcheck_equals = function(obt, exp) {"
263 " if ( obt == exp )"
264 " {"
265 " if ( msg != undefined )"
266 " {"
267 " _root.runtest.xpass(obt+' == '+exp+' ('+msg+')');\n"
268 " }"
269 " else"
270 " {"
271 " _root.runtest.xpass(obt+' == '+exp);\n"
272 " }"
273 " }"
274 " else"
275 " {"
276 " if ( msg != undefined )"
277 " {"
278 " _root.runtest.xfail('expected: '+exp+' , obtained: '+obt);\n"
279 " }"
280 " else"
281 " {"
282 " _root.runtest.xfail('expected: '+exp+' , obtained: '+obt);\n"
283 " }"
284 " }"
285 "};\n"
287 "_root.check = function(a, msg) {\n"
288 " if ( a ) _root.runtest.pass(msg != undefined ? msg : a);\n"
289 " else _root.runtest.fail(msg != undefined ? msg : a);\n"
290 "};\n"
292 "_root.xcheck = function(a, msg) {\n"
293 " if ( a ) _root.runtest.xpass(msg != undefined ? msg : a);\n"
294 " else _root.runtest.xfail(msg != undefined ? msg : a);\n"
295 "};\n"
297 "_root.fail = function(msg) {\n"
298 " _root.runtest.fail(msg);\n"
299 "};\n"
301 "_root.xfail = function(msg) {\n"
302 " _root.runtest.xfail(msg);\n"
303 "};\n"
305 "_root.pass = function(msg) {\n"
306 " _root.runtest.pass(msg);\n"
307 "};\n"
309 "_root.xpass = function(msg) {\n"
310 " _root.runtest.xpass(msg);\n"
311 "};\n"
313 "_root.note = function(msg) {\n"
314 " _root.xtrace(msg);\n"
315 " trace(msg);\n"
316 "};\n"
318 "_root.totals = function(exp, info) {\n"
319 " _root.runtest.totals(exp, info);\n"
320 "};\n"
322 "_root.xtotals = function(exp, info) {\n"
323 " _root.runtest.xtotals(exp, info);\n"
324 "};\n"
326 "_root.dejagnu_module_initialized = 1;\n";
328 return compileSWFActionCode(buf);
331 SWFMovieClip
332 get_dejagnu_clip(SWFBlock font, int depth, int x, int y, int width, int height)
334 SWFMovieClip mc = newSWFMovieClip();
335 SWFAction ac = get_dejagnu_actions();
337 add_xtrace_function_clip(mc, font, depth, x, y, width, height);
339 SWFMovieClip_add(mc, (SWFBlock)ac);
341 SWFMovieClip_nextFrame(mc);
343 return mc;
346 void
347 add_dejagnu_functions(SWFMovie mo, SWFBlock font,
348 int depth, int x, int y, int width, int height)
350 SWFAction ac = get_dejagnu_actions();
352 add_xtrace_function(mo, font, depth, x, y, width, height);
354 SWFMovie_add(mo, (SWFBlock)ac);
357 void
358 add_clip_actions(SWFMovieClip mo, const char* code)
360 SWFAction ac;
361 ac = compileSWFActionCode(code);
362 SWFMovieClip_add(mo, (SWFBlock)ac);
365 #ifdef MING_SUPPORTS_INIT_ACTIONS
366 void
367 add_clip_init_actions(SWFMovieClip mo, const char* code)
369 SWFAction ac;
370 ac = compileSWFActionCode(code);
371 SWFMovieClip_addInitAction(mo, ac);
373 #endif // MING_SUPPORTS_INIT_ACTIONS
375 SWFAction
376 compile_actions(const char* fmt, ...)
378 SWFAction ac;
379 size_t BUFFER_SIZE = 65535;
380 va_list ap;
381 char tmp[BUFFER_SIZE];
383 va_start (ap, fmt);
384 vsnprintf (tmp, BUFFER_SIZE, fmt, ap);
385 tmp[BUFFER_SIZE-1] = '\0';
387 ac = compileSWFActionCode(tmp);
388 va_end (ap);
389 return ac;
392 void
393 add_actions(SWFMovie mo, const char* code)
395 SWFAction ac;
396 ac = compileSWFActionCode(code);
397 SWFMovie_add(mo, (SWFBlock)ac);
400 void
401 print_tests_summary(SWFMovie mo)
403 add_actions(mo, "runtest.totals();");
406 SWFFont
407 get_default_font(const char* mediadir)
409 FILE *font_file;
410 char fdbfont[256];
412 sprintf(fdbfont, "%s/Bitstream-Vera-Sans.fdb", mediadir);
414 font_file = fopen(fdbfont, "r");
415 if ( font_file == NULL )
417 perror(fdbfont);
418 exit(1);
420 return loadSWFFontFromFile(font_file);