2 * Copyright (C) 2005, 2006, 2007, 2009, 2010,
3 * 2011 Free Software Foundation, Inc.
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.
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
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
);
33 add_static_mc(SWFMovie mo
, const char* name
, int depth
)
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
);
53 main(int argc
, char** argv
)
56 SWFMovieClip dejagnuclip
;
57 SWFDisplayItem it
, it1
, it2
, it3
;
59 const char *srcdir
=".";
64 fprintf(stderr
, "Usage: %s <mediadir>\n", argv
[0]);
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
);
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
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
87 "_root.var1 = 0; _root.var2 = 0;"
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');"
97 "_root.note('1. Press a single key to continue the test');"
99 SWFMovie_nextFrame(mo
); // _root frame2
101 SWFMovie_nextFrame(mo
); // _root frame3
105 "check_equals(var1, 1); "
106 "check_equals(var2, 1); "
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
117 "check_equals(var1, 2); "
118 "check_equals(var2, 2);"
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');"
128 " Key.addListener(obj1); "
130 SWFMovie_nextFrame(mo
); // _root frame6
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
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'); "
148 "if(!_root.haslooped1){"
149 " _root.haslooped1=true;"
150 " _root.gotoAndPlay(_root._currentframe-1);"
152 " _root.gotoAndPlay(_root._currentframe+1);"
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
168 // test removing of dynamic sprite key listeners
169 SWFMovie_nextFrame(mo
); // _root frame11
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);"
180 " if(!_root.haslooped2){"
181 " _root.haslooped2=true;"
182 " _root.gotoAndPlay(_root._currentframe-1);"
183 " _root.check_equals(_root._currentframe, 11);"
185 " _root.gotoAndPlay(_root._currentframe+1);"
186 " _root.check_equals(_root._currentframe, 13);"
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
200 "_root.note('6. Press a single key to continue the test');"
201 " obj2 = new Object(); "
203 " obj2.onKeyDown = function () { "
204 " _root.note('obj2.onKeyDown triggered');"
206 " _root.objRef = this; "
209 " Key.addListener(obj2); "
210 // After deleting obj2, we still have a key listener kept alive!
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");
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.
227 "obj3=new Object(); "
228 "obj3.onKeyDown=function() {"
229 " _root.note('obj3.onKeyDown');"
230 " _root.gotoAndPlay(_currentframe+1);"
232 "Key.addListener(obj3); "
234 SWFMovie_nextFrame(mo
); // _root frame15
236 check_equals(mo
, "_root.test4", "1");
238 "Key.removeListener(obj3);"
241 SWFMovie_nextFrame(mo
); // _root frame16
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';"
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';"
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';"
275 SWFMovie_nextFrame(mo
); // _root frame19
279 "obj1.onKeyDown = function () { "
280 " _root.note('obj1.onKeyDown');"
281 " _root.test5 += '+obj1'; "
282 " _root.gotoAndPlay(_root._currentframe+1);"
284 "Key.addListener(obj1);"
285 "ls1.onKeyDown = function () {"
286 " _root.note('ls1.onKeyDown');"
287 " _root.test5 += '+ls1';"
289 "Key.addListener(ls1);"
291 "obj2.onKeyDown = function () {"
292 " _root.note('obj2.onKeyDown');"
293 " _root.test5 += '+obj2';"
295 "Key.addListener(obj2);"
296 "ls2.onKeyDown = function () {"
297 " _root.note('ls2.onKeyDown');"
298 " _root.test5 += '+ls2';"
300 "Key.addListener(ls2);"
302 "obj3.onKeyDown = function () {"
303 " _root.note('obj3.onKeyDown');"
304 " _root.test5 += '+obj3';"
306 "Key.addListener(obj3);"
307 "ls3.onKeyDown = function () {"
308 " _root.note('ls3.onKeyDown');"
309 " _root.test5 += '+ls3';"
312 "_root.note('8. Press a single key to continue the test');"
314 SWFMovie_nextFrame(mo
); // _root frame20
316 SWFMovie_nextFrame(mo
); // _root frame21
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'");
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\"');"
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
352 puts("Saving " OUTPUT_FILENAME
);
353 SWFMovie_save(mo
, OUTPUT_FILENAME
);