Add automated tests on function-type FSCommand parameter passing.
[gnash.git] / testsuite / actionscript.all / AsBroadcaster.as
blobe6978940e903f4f60007dada9889d0dd85c3ec63
1 //
2 // Copyright (C) 2007, 2009, 2010 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.
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 // Test case for TextField ActionScript class
19 // compile this test case with Ming makeswf, and then
20 // execute it like this gnash -1 -r 0 -v out.swf
23 rcsid="AsBroadcaster.as";
24 #include "check.as"
26 #if OUTPUT_VERSION < 6
28 note();
29 note("AsBroadcaster exists but doesn't provide any 'prototype' or 'initialize' for SWF < 6");
31 check_equals(typeof(AsBroadcaster), 'function'); // ???
32 xcheck_equals(typeof(AsBroadcaster.prototype), 'undefined');
33 check_equals(typeof(AsBroadcaster.initialize), 'undefined');
35 check_totals(3);
37 #else // OUTPUT_VERSION >= 6
39 check_equals(typeof(AsBroadcaster), 'function'); // ???
40 check_equals(typeof(AsBroadcaster.prototype), 'object');
41 check_equals(AsBroadcaster.__proto__, Function.prototype);
42 check_equals(typeof(AsBroadcaster.initialize), 'function');
43 check(AsBroadcaster.hasOwnProperty('initialize'));
44 check(!AsBroadcaster.prototype.hasOwnProperty('initialize'));
46 // These functions are available as AsBroadcaster "statics"
47 // and a lookup should be issued by 'initalize' so that overridden
48 // functions are attached to the initialized object rather then
49 // the original one (from swfdec/test/trace/asbroadcaster-override.as)
50 check_equals(typeof(AsBroadcaster.addListener), 'function');
51 check(AsBroadcaster.hasOwnProperty('addListener'));
52 check_equals(typeof(AsBroadcaster.removeListener), 'function');
53 check(AsBroadcaster.hasOwnProperty('removeListener'));
54 check_equals(typeof(AsBroadcaster.broadcastMessage), 'function');
55 check(AsBroadcaster.hasOwnProperty('broadcastMessage'));
57 bc = new AsBroadcaster;
58 check_equals(typeof(bc), 'object');
59 check(bc instanceof AsBroadcaster);
60 check(bc instanceof Object);
61 check_equals(typeof(bc.addListener), 'undefined');
62 check_equals(typeof(bc.removeListener), 'undefined');
63 check_equals(typeof(bc.broadcastMessage), 'undefined');
64 check_equals(typeof(bc.initialize), 'undefined');
66 bcast = new Object;
68 check_equals(typeof(bcast._listeners), 'undefined');
69 check_equals(typeof(bcast.addListener), 'undefined');
70 check_equals(typeof(bcast.removeListener), 'undefined');
71 check_equals(typeof(bcast.broadcastMessage), 'undefined');
73 AsBroadcaster.initialize(bcast);
75 check_equals(typeof(bcast._listeners), 'object');
76 check(bcast._listeners instanceof Array);
77 check_equals(bcast._listeners.length, 0);
78 check_equals(typeof(bcast.addListener), 'function');
79 check_equals(typeof(bcast.removeListener), 'function');
80 check_equals(typeof(bcast.broadcastMessage), 'function');
82 bob = { _listeners:5, addListener:"string" };
83 check_equals(bob._listeners, 5);
84 check_equals(bob.addListener, "string");
85 AsBroadcaster.initialize(bob);
86 check_equals(typeof(bob._listeners), "object");
87 check_equals(typeof(bob.addListener), "function");
89 //--------------------------------
90 // Some insane calls...
91 //--------------------------------
93 ret = bcast.addListener();
94 check_equals(typeof(ret), 'boolean');
95 check_equals(ret, true);
96 check_equals(bcast._listeners.length, 1); // !!
98 ret = bcast.addListener();
99 check_equals(bcast._listeners.length, 1); // undefined was already there as an element...
101 ret = bcast.addListener(2);
102 check_equals(typeof(ret), 'boolean');
103 check_equals(ret, true);
104 check_equals(bcast._listeners.length, 2); // !!
106 ret = bcast.addListener(2);
107 check_equals(bcast._listeners.length, 2); // 2 was already there as an element ...
108 ret = bcast.addListener(3);
109 check_equals(bcast._listeners.length, 3); // 3 is a new element
111 ret = bcast.removeListener(); // will remove the undefined value !
112 check_equals(typeof(ret), 'boolean');
113 check_equals(ret, true);
114 check_equals(bcast._listeners.length, 2); // element 'undefined' was removed
116 ret = bcast.removeListener(2); // will remove the element number:2 !
117 check_equals(typeof(ret), 'boolean');
118 check_equals(ret, true);
119 check_equals(bcast._listeners.length, 1); // element '2' was removed
121 ret = bcast.removeListener(3); // will remove the element number:3 !
122 check_equals(typeof(ret), 'boolean');
123 check_equals(ret, true);
124 check_equals(bcast._listeners.length, 0); // element '3' was removed
126 ret = bcast.removeListener(); // no such element ?
127 check_equals(typeof(ret), 'boolean');
128 check_equals(ret, false);
130 o = new Object; o.valueOf = function() { return 'yes I am'; };
131 bcast.addListener(o);
132 check_equals(bcast._listeners.length, 1);
133 ret = bcast.removeListener('yes I am'); // valueOf invoked
134 check_equals(typeof(ret), 'boolean');
135 check_equals(ret, true);
136 check_equals(bcast._listeners.length, 0); // element '3' was removed
138 o.addListener = bcast.addListener;
139 check_equals(typeof(o._listeners), 'undefined');
140 check_equals(typeof(o.removeListenerCalled), 'undefined');
141 ret = o.addListener(); // automatically attempts to call o.removeListener()
142 check_equals(typeof(ret), 'boolean');
143 check_equals(ret, true);
144 check_equals(typeof(o._listeners), 'undefined');
146 o.removeListener = function() { this.removeListenerCalled = true; };
147 ret = o.addListener(); // automatically calls o.removeListener()
148 check_equals(typeof(ret), 'boolean');
149 check_equals(ret, true);
150 check_equals(typeof(o._listeners), 'undefined');
151 check_equals(typeof(o.removeListenerCalled), 'boolean');
152 check_equals(o.removeListenerCalled, true);
154 o.removeListener = bcast.removeListener;
155 o._listeners = new Object();
156 o._listeners.push = function() { this.pushCalled = true; this.length++; };
157 o._listeners.splice = function() { this.spliceCalled = true; };
158 o._listeners.length = 1;
159 o._listeners['0'] = 5;
160 ret = o.addListener(5); // automatically calls o._listeners.splice and o._listeners.push
161 // Gnash fails as it gives up if _listeners isn't an array
162 check_equals(o._listeners.pushCalled, true);
163 check_equals(o._listeners.length, 2);
164 check_equals(o._listeners.spliceCalled, true);
166 dang = createEmptyMovieClip('dangling', 1);
167 check_equals(typeof(dang.addListener), 'undefined');
168 dang.removeMovieClip();
169 AsBroadcaster.initialize(dang); // can't initialize a dangling thing
170 check_equals(typeof(dang.addListener), 'undefined');
171 createEmptyMovieClip('dangling', 2);
172 AsBroadcaster.initialize(dang); // but can initialize a rebound thing
173 check_equals(typeof(dang.addListener), 'function');
175 //--------------------------------
176 // A bit more sane calls...
177 //--------------------------------
179 counter = 0;
181 onTest = function()
183 //note(" Called "+this.name+".onTest (order "+this.order+"->"+(counter+1)+")");
184 this.order = ++counter;
187 a = new Object; a.name = 'a'; a.onTest = onTest;
188 b = new Object; b.name = 'b'; b.onTest = onTest;
190 ret = bcast.addListener(a);
191 check_equals(typeof(ret), 'boolean');
192 check_equals(ret, true);
193 ret = bcast.addListener(b);
194 check_equals(typeof(ret), 'boolean');
195 check_equals(ret, true);
196 //note("Broadcasting");
197 ret = bcast.broadcastMessage('onTest');
198 check_equals(typeof(ret), 'boolean');
199 check_equals(ret, true);
200 check_equals(a.order, 1);
201 check_equals(b.order, 2);
203 ret = bcast.addListener(b); // b is not added again
204 check_equals(typeof(ret), 'boolean');
205 check_equals(ret, true);
206 //note("Broadcasting");
207 bcast.broadcastMessage('onTest');
208 check_equals(a.order, 3);
209 check_equals(b.order, 4);
211 ret = bcast.addListener(a); // listener a is moved from first to last position to _listeners
212 check_equals(typeof(ret), 'boolean');
213 check_equals(ret, true);
214 //note("Broadcasting");
215 bcast.broadcastMessage('onTest');
216 check_equals(b.order, 5);
217 check_equals(a.order, 6);
219 bcast._listeners.push(a); // force double a listener
220 //note("Broadcasting");
221 bcast.broadcastMessage('onTest');
222 check_equals(b.order, 7);
223 check_equals(a.order, 9); // a.order was set twice
225 bcast.addListener(a); // first a is moved from first to last position to _listeners
226 //note("Broadcasting");
227 ret = bcast.broadcastMessage('onTest');
228 check_equals(typeof(ret), 'boolean');
229 check_equals(ret, true);
230 check_equals(b.order, 10);
231 check_equals(a.order, 12); // a is still set twice
233 bcast._listeners.push(b); // force double b, order should now be: b,a,a,b
234 //note("Broadcasting");
235 bcast.broadcastMessage('onTest');
236 check_equals(b.order, 16);
237 check_equals(a.order, 15);
239 ret = bcast.addListener(b); // *first* b is removed, another one added, new order is a,a,b,b
240 check_equals(typeof(ret), 'boolean');
241 check_equals(ret, true);
242 //note("Broadcasting");
243 bcast.broadcastMessage('onTest');
244 check_equals(a.order, 18);
245 check_equals(b.order, 20);
247 ret = bcast.removeListener(b); // only first is removed
248 check_equals(typeof(ret), 'boolean');
249 check_equals(ret, true);
250 check_equals(bcast._listeners.length, 3); // expect: a,a,b
251 bcast.broadcastMessage('onTest');
252 check_equals(a.order, 22);
253 check_equals(b.order, 23);
255 ret = bcast.broadcastMessage('onUnexistent');
256 check_equals(ret, true);
257 bcast._listeners.length=0;
258 ret = bcast.broadcastMessage('onUnexistent');
259 check_equals(typeof(ret), 'undefined');
261 //--------------------------------
262 // broadcastMessage with args
263 //--------------------------------
265 _root.total = 0;
266 o = {};
267 o.addThis = function(what)
269 //note("Arg0 is "+what);
270 _root.total += what;
272 o.setSum = function()
274 _root.total = 0;
275 for (var i=0; i< arguments.length; ++i)
277 //note("Arg "+i+" is "+arguments[i]);
278 _root.total += arguments[i];
281 bcast.addListener(o);
282 bcast.broadcastMessage('addThis', 3);
283 check_equals(_root.total, 3);
284 bcast.broadcastMessage('addThis', 2);
285 check_equals(_root.total, 5);
286 bcast.broadcastMessage('setSum', 1, 2, 3, 4);
287 check_equals(_root.total, 10);
288 bcast.broadcastMessage('setSum', 1, 2, 3, 4, 5, 6, 7, 8);
289 check_equals(_root.total, 36);
290 bcast.broadcastMessage('setSum', 'one', 'two', 'three');
291 check_equals(_root.total, '0onetwothree');
293 //--------------------------------
294 // event handlers calling super
295 //--------------------------------
297 function A1() {}
298 A1.prototype.add = function(o) { o.msg += 'A'; };
299 function B1() {}
300 B1.prototype = new A1;
301 B1.prototype.add = function(o) { super.add(o); o.msg += 'B'; };
303 bobj = new B1;
304 o = { msg:'' };
305 bobj.add(o);
306 check_equals(o.msg, "AB");
307 o.msg = '';
309 bcast._listeners.length=0;
310 bcast.addListener(bobj);
311 bcast.broadcastMessage('add', o);
312 check_equals(o.msg, "AB");
314 //-----------------------------------------------------------------------------------
315 // TODO: test override of AsBroadcaster.{addListener,removeListener,broadcastMessage}
316 // swfdec contains tests for this, which should now be pretty succeeding except for
317 // not-directly related checks which trigger failure due to all-or-nothing nature of
318 // the swfdec testsuite.
319 // See swfdec/test/trace/asbroadcaster-override.as for more info
320 //-----------------------------------------------------------------------------------
322 check_totals(115);
324 #endif // OUTPUT_VERSION < 6