Update with current status
[gnash.git] / testsuite / misc-ming.all / DefineEditTextVariableNameTest.c
blob520f9f279648580324bb19f6883622c4a5a94a0f
1 /*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
3 * 2011 Free Software Foundation, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
22 * Test DefineEditText tag with VariableName
24 * Uses "embedded" font in two textfields with initial
25 * texts "Hello world" and "Hi there".
27 * Then, every second it toggles the text between "Hello",
28 * "World" and "" (the empty string) in the first textfield
29 * and between "Hi", "There" and "" (the empty string) in
30 * the second textfield.
32 * The text is set assigning a value to the associated
33 * VariableName and verified by getting the TextField's
34 * 'text' member.
36 * Verifications use the Dejagnu intterface, so you'll see
37 * visual traces of failures and a final visual report
38 * or successes and failures.
40 * The first TextField DisplayObjects is stored inside a MovieClip,
41 * and its variables is set on the root.
43 * The second TextField DisplayObjects is stored inside a MovieClip,
44 * and its variables is set on a third MovieClip which is placed
45 * *after* the definition of the TextField. Things should still work.
47 * Note that the ActionScript code also tries to *move* the DisplayObject trough
48 * the VariableName (incdement varname._x).
49 * The correct behaviour is for the DisplayObject to NOT move
51 * run as ./DefineEditTextVariableNameTest
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <ming.h>
58 #include "ming_utils.h"
60 #define OUTPUT_VERSION 6
61 #define OUTPUT_FILENAME "DefineEditTextVariableNameTest.swf"
63 void add_text_field(SWFMovieClip mo, SWFBlock font, const char* varname, const char* text);
64 void set_text(SWFMovie mo, const char* txt, const char* varname);
65 void shift_horizontally(SWFMovie mo, const char* what, int howmuch);
66 void set_x(SWFMovie mo, const char* what, int x);
67 void setVariableName(SWFMovie mo, const char* txt, const char* varname);
69 void
70 shift_horizontally(SWFMovie mo, const char* what, int howmuch)
72 static const size_t BUFLEN = 256;
74 char buf[BUFLEN];
75 SWFAction ac;
77 snprintf(buf, BUFLEN, "%s._x += %d;", what, howmuch);
78 buf[BUFLEN-1] = '\0';
80 ac = compileSWFActionCode(buf);
81 SWFMovie_add(mo, (SWFBlock)ac);
84 void
85 set_x(SWFMovie mo, const char* what, int x)
87 static const size_t BUFLEN = 256;
89 char buf[BUFLEN];
90 SWFAction ac;
92 snprintf(buf, BUFLEN, "%s._x = %d;", what, x);
93 buf[BUFLEN-1] = '\0';
95 ac = compileSWFActionCode(buf);
96 SWFMovie_add(mo, (SWFBlock)ac);
100 void
101 set_text(SWFMovie mo, const char* txt, const char* varname)
103 static const size_t BUFLEN = 512;
105 char buf[BUFLEN];
106 SWFAction ac;
107 snprintf(buf, BUFLEN, "%s = \"%s\"; ",
108 varname, txt);
109 buf[BUFLEN-1] = '\0';
110 ac = compileSWFActionCode(buf);
111 SWFMovie_add(mo, (SWFBlock)ac);
114 // @param txt: make of textfield
115 // @param varname: variable name to set
116 void
117 setVariableName(SWFMovie mo, const char* txt, const char* varname)
119 static const size_t BUFLEN = 512;
121 char buf[BUFLEN];
122 SWFAction ac;
123 snprintf(buf, BUFLEN, "%s.variable = \"%s\"; ", txt, varname);
124 buf[BUFLEN-1] = '\0';
125 ac = compileSWFActionCode(buf);
126 SWFMovie_add(mo, (SWFBlock)ac);
129 void
130 add_text_field(SWFMovieClip mo, SWFBlock font, const char* varname,
131 const char* text)
133 SWFTextField tf;
134 SWFDisplayItem it;
136 tf = newSWFTextField();
137 SWFTextField_setFlags(tf, SWFTEXTFIELD_DRAWBOX);
139 SWFTextField_setFont(tf, font);
141 /* Associate the textField instance with a named variable*/
142 if(varname)
144 SWFTextField_setVariableName(tf, varname);
146 if(text)
148 SWFTextField_addChars(tf, text);
149 SWFTextField_addString(tf, text);
152 it = SWFMovieClip_add(mo, (SWFBlock)tf);
153 SWFDisplayItem_setName(it, "textfield");
154 SWFMovieClip_nextFrame(mo);
157 static void
158 testVariableNameGetSet(SWFMovie mo, const char *varName1, const char *varName2)
160 char tmp[1024];
162 set_text(mo, "Hello", varName1);
163 shift_horizontally(mo, varName1, 10);
164 check_equals(mo, "mc1.textfield.text", "'Hello'");
165 check_equals(mo, varName1, "'Hello'");
166 check_equals(mo, "mc1.textfield._x", "0");
167 check_equals(mo, "mc1._height", "16");
168 check_equals(mo, "mc1._width", "136");
169 check_equals(mo, "mc2._height", "16");
170 check_equals(mo, "mc2._width", "100");
172 set_text(mo, "Hi", varName2);
173 shift_horizontally(mo, varName2, 10);
174 check_equals(mo, "mc2.textfield.text", "'Hi'");
175 check_equals(mo, varName2, "'Hi'");
176 check_equals(mo, "mc2.textfield._x", "150");
177 snprintf(tmp, 1024, "'%s'", varName2);
178 check_equals(mo, "mc2.textfield.variable", tmp);
180 SWFMovie_nextFrame(mo); /* showFrame */
182 set_text(mo, "World", varName1);
183 shift_horizontally(mo, varName1, 10);
184 check_equals(mo, "mc1.textfield.text", "'World'");
185 check_equals(mo, varName1, "'World'");
186 check_equals(mo, "mc1.textfield._x", "0");
188 set_text(mo, "There", varName2);
189 shift_horizontally(mo, varName2, 10);
190 check_equals(mo, "mc2.textfield.text", "'There'");
191 check_equals(mo, varName2, "'There'");
192 check_equals(mo, "mc2.textfield._x", "150");
194 SWFMovie_nextFrame(mo); /* showFrame */
196 set_text(mo, "", varName1);
197 shift_horizontally(mo, varName1, 10);
198 check_equals(mo, "mc1.textfield.text", "''");
199 check_equals(mo, varName1, "''");
200 check_equals(mo, "mc1.textfield._x", "0");
202 set_text(mo, "", varName2);
203 shift_horizontally(mo, varName2, 10);
204 check_equals(mo, "mc2.textfield.text", "''");
205 check_equals(mo, varName2, "''");
206 check_equals(mo, "mc2.textfield._x", "150");
208 SWFMovie_nextFrame(mo); /* showFrame */
212 main(int argc, char** argv)
214 SWFMovie mo;
215 SWFMovieClip mc1, mc2, mc3, mc4, mc5, mc6, mc7;
216 SWFDisplayItem it;
217 const char *srcdir=".";
218 /* The variable name for textfield */
219 char* varName1 = "_root.testName";
220 char* varName2 = "_root.mc3.testName";
221 char* varName3 = "uninitalized_text_var";
222 SWFFont bfont;
225 /*********************************************
227 * Initialization
229 *********************************************/
231 if ( argc>1 ) srcdir=argv[1];
232 else
234 fprintf(stderr, "Usage: %s <mediadir>\n", argv[0]);
235 return 1;
238 puts("Setting things up");
240 Ming_init();
241 Ming_useSWFVersion (OUTPUT_VERSION);
242 Ming_setScale(20.0); /* let's talk pixels */
244 mo = newSWFMovie();
245 SWFMovie_setRate(mo, 2);
246 SWFMovie_setDimension(mo, 628, 451);
248 bfont = get_default_font(srcdir);
250 /***************************************************
252 * Add MovieClips mc1 and mc2 with their textfield
254 ***************************************************/
257 mc1 = newSWFMovieClip();
258 add_text_field(mc1, (SWFBlock)bfont, varName1, "Hello World");
259 it = SWFMovie_add(mo, (SWFBlock)mc1);
260 SWFDisplayItem_setName(it, "mc1");
261 set_x(mo, "mc1.textfield", 0);
265 mc2 = newSWFMovieClip();
266 add_text_field(mc2, (SWFBlock)bfont, varName2, "Hi There");
267 it = SWFMovie_add(mo, (SWFBlock)mc2);
268 SWFDisplayItem_setName(it, "mc2");
269 set_x(mo, "mc2.textfield", 150);
272 /*********************************************
274 * Now add mc3, which is referenced by
275 * mc2.textfield variableName (after it's placement)
277 *********************************************/
280 mc3 = newSWFMovieClip();
281 it = SWFMovie_add(mo, (SWFBlock)mc3);
282 SWFDisplayItem_setName(it, "mc3");
283 /*SWFMovie_nextFrame(mo);*/
288 /*********************************************
290 * Add xtrace code
292 *********************************************/
294 /*add_xtrace_function(mo, 3000, 0, 50, 400, 800);*/
295 add_dejagnu_functions(mo, (SWFBlock)bfont, 3000, 0, 50, 400, 800);
297 /*********************************************
299 * Set and get value of the textfields using
300 * their variableName
302 *********************************************/
304 testVariableNameGetSet(mo, varName1, varName2);
306 /*********************************************
308 * Now change the textfields 'variable' property
309 * and test again
311 *********************************************/
314 const char* varName1bis = "_root.varName1bis";
315 const char* varName2bis = "_root.varName2bis";
316 setVariableName(mo, "mc1.textfield", varName1bis);
317 setVariableName(mo, "mc2.textfield", varName2bis);
318 testVariableNameGetSet(mo, varName1bis, varName2bis);
322 SWFMovie_nextFrame(mo);
324 // test that uninitialized textfield instance variables are not
325 // visible in the timeline where the textfield instance is placed.
327 mc4 = newSWFMovieClip();
328 add_text_field(mc4, (SWFBlock)bfont, varName3, NULL);
329 it = SWFMovie_add(mo, (SWFBlock)mc4);
330 SWFDisplayItem_setName(it, "mc4");
331 SWFDisplayItem_moveTo(it, 300, 300);
332 check_equals(mo, "typeof(mc4.textfield)", "'object'");
333 check_equals(mo, "typeof(mc4.uninitalized_text_var)", "'undefined'");
334 SWFMovie_nextFrame(mo);
336 check_equals(mo, "typeof(mc4.uninitalized_text_var)", "'undefined'");
337 add_actions(mo,
338 "mc4.textfield.text = 100;"
339 "mc4.textfield._width = 30;");
340 check_equals(mo, "typeof(mc4.uninitalized_text_var)", "'string'");
341 check_equals(mo, "mc4.uninitalized_text_var", "100");
342 SWFMovie_nextFrame(mo);
345 // (1) test deletion of textField variable
347 mc5 = newSWFMovieClip();
348 add_text_field(mc5, (SWFBlock)bfont, "text_var5", NULL);
349 it = SWFMovie_add(mo, (SWFBlock)mc5);
350 SWFDisplayItem_setName(it, "mc5");
351 SWFDisplayItem_moveTo(it, 400, 300);
352 check_equals(mo, "typeof(mc5.textfield)", "'object'");
353 check_equals(mo, "typeof(mc5.text_var5)", "'undefined'");
354 add_actions(mo,
355 "mc5.text_var5 = 'intial_text';"
356 "delete mc5.text_var5;");
357 xcheck_equals(mo, "typeof(mc5.text_var5)", "'undefined'");
358 add_actions(mo,
359 "mc5.textfield.text = 'new_text';"
360 "mc5.textfield._width = 60;");
361 check_equals(mo, "mc5.text_var5", "'new_text'");
362 add_actions(mo, "mc5.text_var5 = 'change back';");
363 check_equals(mo, "mc5.textfield.text", "'change back'");
364 add_actions(mo,
365 "delete mc5.text_var5;"
366 "mc5.text_var5 = 'revival';");
367 add_actions(mo, "mc5.textfield.text = 'revival';");
368 SWFMovie_nextFrame(mo);
371 // test deletion of textField variable(another one).
373 mc6 = newSWFMovieClip();
374 add_text_field(mc6, (SWFBlock)bfont, "text_var6", "initial_text");
375 it = SWFMovie_add(mo, (SWFBlock)mc6);
376 SWFDisplayItem_setName(it, "mc6");
377 SWFDisplayItem_moveTo(it, 500, 300);
378 check_equals(mo, "typeof(mc6.textfield)", "'object'");
379 check_equals(mo, "typeof(mc6.text_var6)", "'string'");
380 add_actions(mo,
381 "delete mc6.text_var6;");
382 xcheck_equals(mo, "typeof(mc6.text_var6)", "'undefined'");
383 check_equals(mo, "mc6.textfield.text", "'initial_text'");
384 add_actions(mo,
385 "mc6.textfield.text = 'new_text';"
386 "mc6.textfield._width = 60;");
387 check_equals(mo, "mc6.text_var6", "'new_text'");
388 add_actions(mo, "mc6.text_var6 = 'change back';");
389 check_equals(mo, "mc6.textfield.text", "'change back'");
390 SWFMovie_nextFrame(mo);
393 // test protection of textField instance variable.
395 mc7 = newSWFMovieClip();
396 add_text_field(mc7, (SWFBlock)bfont, "text_var7", "initial_text");
397 it = SWFMovie_add(mo, (SWFBlock)mc7);
398 SWFDisplayItem_setName(it, "mc7");
399 SWFDisplayItem_moveTo(it, 400, 400);
400 check_equals(mo, "typeof(mc7.textfield)", "'object'");
401 check_equals(mo, "typeof(mc7.text_var7)", "'string'");
402 add_actions(mo,
403 "ASSetPropFlags(_root.mc7, null, 7, 7);"
404 "mc7.textfield.text = 'new_text';");
405 check_equals(mo, "mc7.text_var7", "'initial_text'");
406 check_equals(mo, "mc7.textfield.text", "'new_text'");
407 SWFMovie_nextFrame(mo);
408 /*********************************************
410 * Print test results
412 *********************************************/
414 SWFMovie_nextFrame(mo); /* showFrame */
415 print_tests_summary(mo);
416 add_actions(mo, "stop();");
417 SWFMovie_nextFrame(mo); /* showFrame */
420 /*****************************************************
422 * Output movie
424 *****************************************************/
426 puts("Saving " OUTPUT_FILENAME );
427 SWFMovie_save(mo, OUTPUT_FILENAME);
429 return 0;