Add automated tests on function-type FSCommand parameter passing.
[gnash.git] / testsuite / misc-ming.all / key_event_test.c
blob327d285fe9b317c830f1d6a853c5bca47c296857
1 /*
2 * Copyright (C) 2005, 2006, 2007, 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
18 */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <ming.h>
25 #include "ming_utils.h"
27 #define OUTPUT_VERSION 6
28 #define OUTPUT_FILENAME "key_event_test.swf"
30 SWFDisplayItem add_static_mc(SWFMovie mo, const char* name, int depth);
32 SWFDisplayItem
33 add_static_mc(SWFMovie mo, const char* name, int depth)
35 SWFShape sh;
36 SWFMovieClip mc;
37 SWFDisplayItem it;
39 mc = newSWFMovieClip();
40 sh = make_fill_square (300, 300, 60, 60, 255, 0, 0, 255, 0, 0);
41 SWFMovieClip_add(mc, (SWFBlock)sh);
42 SWFMovieClip_nextFrame(mc);
44 it = SWFMovie_add(mo, (SWFBlock)mc);
45 SWFDisplayItem_setDepth(it, depth);
46 SWFDisplayItem_setName(it, name);
48 return it;
52 int
53 main(int argc, char** argv)
55 SWFMovie mo;
56 SWFMovieClip dejagnuclip;
57 SWFDisplayItem it, it1, it2, it3;
59 const char *srcdir=".";
60 if ( argc>1 )
61 srcdir=argv[1];
62 else
64 fprintf(stderr, "Usage: %s <mediadir>\n", argv[0]);
65 return 1;
68 Ming_init();
69 mo = newSWFMovieWithVersion(OUTPUT_VERSION);
70 SWFMovie_setDimension(mo, 800, 600);
71 // low frame rate is needed for visual checking
72 SWFMovie_setRate (mo, 1.0);
74 dejagnuclip = get_dejagnu_clip((SWFBlock)get_default_font(srcdir), 10, 0, 0, 800, 600);
75 SWFMovie_add(mo, (SWFBlock)dejagnuclip);
76 add_actions(mo,
77 "test1=0; test2=0; test3=0; test4=0; test5='0'; test6=0; "
78 "keyPressed=false; keyReleased=false;"
79 "haslooped1=false; haslooped2=false;");
80 SWFMovie_nextFrame(mo); // _root frame1
82 // test1:
83 // (1)onKeyDown, onKeyPress and onKeyUp are not global functions
84 // (2)test that global Key object can be overridden
85 // (3)after overriden, previously registered handlers could still respond to new key events
86 add_actions(mo,
87 "_root.var1 = 0; _root.var2 = 0;"
88 "l = new Object();"
89 "l.onKeyDown = function () { _root.note('l.onKeyDown'); _root.var1+=1; _root.Play(); }; "
90 "l.onKeyUp = function () { _root.note('l.onKeyUp'); _root.var2+=1;}; "
91 " Key.addListener(l);"
92 "check_equals(typeof(Key), 'object');"
93 "check_equals(typeof(onKeyUp), 'undefined');"
94 "check_equals(typeof(onKeyDown), 'undefined');"
95 "check_equals(typeof(onKeyPress), 'undefined');"
96 "stop();"
97 "_root.note('1. Press a single key to continue the test');"
99 SWFMovie_nextFrame(mo); // _root frame2
101 SWFMovie_nextFrame(mo); // _root frame3
103 add_actions(mo,
104 "stop();"
105 "check_equals(var1, 1); "
106 "check_equals(var2, 1); "
107 "Key = 3;"
108 "check_equals(typeof(Key), 'number');"
109 "_root.note('2. Press a single key to continue the test');"
111 SWFMovie_nextFrame(mo); // _root frame4
113 SWFMovie_nextFrame(mo); // _root frame5
115 add_actions(mo,
116 "stop();"
117 "check_equals(var1, 2); "
118 "check_equals(var2, 2);"
119 "delete Key; "
120 "check_equals(typeof(Key), 'object');"
121 "Key.removeListener(l);"
122 "_root.note('3. Press a single key to continue the test');"
123 "obj1=new Object(); "
124 " obj1.onKeyDown=function() {"
125 " _root.note('obj1.onKeyDown');"
126 " _root.play();"
127 "}; "
128 " Key.addListener(obj1); "
130 SWFMovie_nextFrame(mo); // _root frame6
132 add_actions(mo,
133 "check_equals(var1, 2);"
134 "check_equals(var2, 2);"
135 "Key.removeListener(obj1);"
136 "delete l; delete obj1; "
138 SWFMovie_nextFrame(mo); // _root frame7
140 // test2:
141 // test removing of static clip key listeners
142 SWFMovie_nextFrame(mo); // _root frame8
144 it = add_static_mc(mo, "listenerClip1", 20);
145 SWFDisplayItem_addAction(it,
146 newSWFAction(" _root.note('listenerClip2.onClipKeyDown'); "
147 " _root.test2++; "
148 "if(!_root.haslooped1){"
149 " _root.haslooped1=true;"
150 " _root.gotoAndPlay(_root._currentframe-1);"
151 "} else {"
152 " _root.gotoAndPlay(_root._currentframe+1);"
155 SWFACTION_KEYDOWN);
156 add_actions(mo,
157 "stop();"
158 "_root.note('4. Press a single key to continue the test');"
160 SWFMovie_nextFrame(mo); // _root frame9
162 check_equals(mo, "_root.test2", "2");
163 SWFDisplayItem_remove(it);
164 SWFMovie_nextFrame(mo); // _root frame10
167 // test3:
168 // test removing of dynamic sprite key listeners
169 SWFMovie_nextFrame(mo); // _root frame11
171 add_actions(mo,
172 "stop();"
173 "_root.note('5. Press a single key to continue the test');"
174 "_root.createEmptyMovieClip('dynamic_mc', -10);"
175 "dynamic_mc.onKeyDown = function() "
177 " _root.note('dynamic_mc.onKeyDown triggered');"
178 " _root.check_equals(this, _root.dynamic_mc);"
179 " _root.test3++;"
180 " if(!_root.haslooped2){"
181 " _root.haslooped2=true;"
182 " _root.gotoAndPlay(_root._currentframe-1);"
183 " _root.check_equals(_root._currentframe, 11);"
184 " } else {"
185 " _root.gotoAndPlay(_root._currentframe+1);"
186 " _root.check_equals(_root._currentframe, 13);"
187 " }"
188 "};"
189 "Key.addListener(dynamic_mc);"
191 SWFMovie_nextFrame(mo); // _root frame12
193 check_equals(mo, "_root.test3", "2");
194 add_actions(mo, "dynamic_mc.swapDepths(10); dynamic_mc.removeMovieClip();");
195 SWFMovie_nextFrame(mo); // _root frame13
197 // test4:
198 // GC test
199 add_actions(mo,
200 "_root.note('6. Press a single key to continue the test');"
201 " obj2 = new Object(); "
202 " obj2.x = 100; "
203 " obj2.onKeyDown = function () { "
204 " _root.note('obj2.onKeyDown triggered');"
205 " _root.test4++; "
206 " _root.objRef = this; "
207 " _root.play();"
208 " };"
209 " Key.addListener(obj2); "
210 // After deleting obj2, we still have a key listener kept alive!
211 " delete obj2; "
212 " stop();"
214 check_equals(mo, "_root.test4", "0");
215 SWFMovie_nextFrame(mo); // _root frame14
217 check_equals(mo, "objRef.x", "100");
218 check_equals(mo, "_root.test4", "1");
219 add_actions(mo,
220 "stop();"
221 "_root.note('7. Press a single key to continue the test');"
222 "Key.removeListener(objRef); "
223 // check that objRef is still alive
224 "check_equals(typeof(objRef), 'object');"
225 // delete the objRef, no object and no key listener now.
226 "delete objRef;"
227 "obj3=new Object(); "
228 "obj3.onKeyDown=function() {"
229 " _root.note('obj3.onKeyDown');"
230 " _root.gotoAndPlay(_currentframe+1);"
231 "}; "
232 "Key.addListener(obj3); "
234 SWFMovie_nextFrame(mo); // _root frame15
236 check_equals(mo, "_root.test4", "1");
237 add_actions(mo,
238 "Key.removeListener(obj3);"
239 "delete obj3; "
241 SWFMovie_nextFrame(mo); // _root frame16
243 // test5:
244 // test key listeners invoking order.
245 // expected behaviour:
246 // (1)for DisplayObject key listeners, first added last called
247 // (2)for general object listeners, first added first called
248 // (3)for DisplayObject listeners, user defined onKeyDown/Up won't be called
249 // if not registered to the global Key object.
250 it1 = add_static_mc(mo, "ls1", 30);
251 SWFDisplayItem_addAction(it1,
252 compileSWFActionCode(
253 "_root.note('ls1.onClipKeyDown');"
254 "_root.test5 += '+ls1';"
256 SWFACTION_KEYDOWN);
257 SWFMovie_nextFrame(mo); // _root frame17
259 it2 = add_static_mc(mo, "ls2", 31);
260 SWFDisplayItem_addAction(it2,
261 compileSWFActionCode(
262 "_root.note('ls2.onClipKeyDown');"
263 "_root.test5 += '+ls2';"
265 SWFACTION_KEYDOWN);
266 SWFMovie_nextFrame(mo); // _root frame18
268 it3 = add_static_mc(mo, "ls3", 29);
269 SWFDisplayItem_addAction(it3,
270 compileSWFActionCode(
271 "_root.note('ls3.onClipKeyDown');"
272 "_root.test5 += '+ls3';"
274 SWFACTION_KEYDOWN);
275 SWFMovie_nextFrame(mo); // _root frame19
277 add_actions(mo,
278 "obj1=new Object();"
279 "obj1.onKeyDown = function () { "
280 " _root.note('obj1.onKeyDown');"
281 " _root.test5 += '+obj1'; "
282 " _root.gotoAndPlay(_root._currentframe+1);"
283 "}; "
284 "Key.addListener(obj1);"
285 "ls1.onKeyDown = function () {"
286 " _root.note('ls1.onKeyDown');"
287 " _root.test5 += '+ls1';"
288 "}; "
289 "Key.addListener(ls1);"
290 "obj2=new Object();"
291 "obj2.onKeyDown = function () {"
292 " _root.note('obj2.onKeyDown');"
293 " _root.test5 += '+obj2';"
294 "}; "
295 "Key.addListener(obj2);"
296 "ls2.onKeyDown = function () {"
297 " _root.note('ls2.onKeyDown');"
298 " _root.test5 += '+ls2';"
299 "}; "
300 "Key.addListener(ls2);"
301 "obj3=new Object();"
302 "obj3.onKeyDown = function () {"
303 " _root.note('obj3.onKeyDown');"
304 " _root.test5 += '+obj3';"
305 "}; "
306 "Key.addListener(obj3);"
307 "ls3.onKeyDown = function () {"
308 " _root.note('ls3.onKeyDown');"
309 " _root.test5 += '+ls3';"
310 "}; "
311 "stop(); "
312 "_root.note('8. Press a single key to continue the test');"
314 SWFMovie_nextFrame(mo); // _root frame20
316 SWFMovie_nextFrame(mo); // _root frame21
318 add_actions(mo,
319 "stop(); "
320 "_root.note('9. Press a single key to continue the test');"
322 SWFDisplayItem_remove(it1);
323 SWFDisplayItem_remove(it2);
324 SWFDisplayItem_remove(it3);
325 SWFMovie_nextFrame(mo); // _root frame22
327 check_equals(mo, "test5", "'0+ls3+ls2+ls1+obj1+ls1+obj2+ls2+obj3+obj1+obj2+obj3'");
329 add_actions(mo,
330 "o = new Object();"
331 "_root.t = '';"
332 "o.onKeyDown = function() { t = _root.ff.text; play(); };"
333 "Key.addListener(o);"
334 "_root.createTextField('ff', 987, 300, 20, 200, 40);"
335 "_root.ff.type = 'input';"
336 "_root.ff.text = 'Input here';"
337 "_root.ff.border = true;"
338 "_root.note('10. Click on the TextField and type \"i\"');"
339 "stop();"
342 SWFMovie_nextFrame(mo); // _root frame23
344 // The listener is called before text is updated!
345 check_equals(mo, "_root.t", "'Input here'");
346 check_equals(mo, "_root.ff.text", "'Input herei'");
349 add_actions(mo, "totals(); stop();");
350 SWFMovie_nextFrame(mo); // _root frame24
351 //Output movie
352 puts("Saving " OUTPUT_FILENAME );
353 SWFMovie_save(mo, OUTPUT_FILENAME);
355 return 0;