1 | dojo.provide("dojo.tests._base.connect"); |
---|
2 | |
---|
3 | hub = function(){ |
---|
4 | }; |
---|
5 | |
---|
6 | failures = 0; |
---|
7 | bad = function(){ |
---|
8 | failures++; |
---|
9 | }; |
---|
10 | |
---|
11 | good = function(){ |
---|
12 | }; |
---|
13 | |
---|
14 | // make 'iterations' connections to hub |
---|
15 | // roughly half of which will be to 'good' and |
---|
16 | // half to 'bad' |
---|
17 | // all connections to 'bad' are disconnected |
---|
18 | // test can then be performed on the values |
---|
19 | // 'failures' and 'successes' |
---|
20 | markAndSweepTest = function(iterations){ |
---|
21 | var marked = []; |
---|
22 | // connections |
---|
23 | for(var i=0; i<iterations; i++){ |
---|
24 | if(Math.random() < 0.5){ |
---|
25 | marked.push(dojo.connect('hub', bad)); |
---|
26 | }else{ |
---|
27 | dojo.connect('hub', good); |
---|
28 | } |
---|
29 | } |
---|
30 | // Randomize markers (only if the count isn't very high) |
---|
31 | if(i < Math.pow(10, 4)){ |
---|
32 | var rm = [ ]; |
---|
33 | while(marked.length){ |
---|
34 | var m = Math.floor(Math.random() * marked.length); |
---|
35 | rm.push(marked[m]); |
---|
36 | marked.splice(m, 1); |
---|
37 | } |
---|
38 | marked = rm; |
---|
39 | } |
---|
40 | for(var m=0; m<marked.length; m++){ |
---|
41 | dojo.disconnect(marked[m]); |
---|
42 | } |
---|
43 | // test |
---|
44 | failures = 0; |
---|
45 | hub(); |
---|
46 | // return number of disconnected functions that fired (should be 0) |
---|
47 | return failures; |
---|
48 | }; |
---|
49 | |
---|
50 | markAndSweepSubscribersTest = function(iterations){ |
---|
51 | var topic = "hubbins"; |
---|
52 | var marked = []; |
---|
53 | // connections |
---|
54 | for(var i=0; i<iterations; i++){ |
---|
55 | if(Math.random() < 0.5){ |
---|
56 | marked.push(dojo.subscribe(topic, bad)); |
---|
57 | }else{ |
---|
58 | dojo.subscribe(topic, good); |
---|
59 | } |
---|
60 | } |
---|
61 | // Randomize markers (only if the count isn't very high) |
---|
62 | if(i < Math.pow(10, 4)){ |
---|
63 | var rm = [ ]; |
---|
64 | while(marked.length){ |
---|
65 | var m = Math.floor(Math.random() * marked.length); |
---|
66 | rm.push(marked[m]); |
---|
67 | marked.splice(m, 1); |
---|
68 | } |
---|
69 | marked = rm; |
---|
70 | } |
---|
71 | for(var m=0; m<marked.length; m++){ |
---|
72 | dojo.unsubscribe(marked[m]); |
---|
73 | } |
---|
74 | // test |
---|
75 | failures = 0; |
---|
76 | dojo.publish(topic); |
---|
77 | // return number of unsubscribed functions that fired (should be 0) |
---|
78 | return failures; |
---|
79 | }; |
---|
80 | |
---|
81 | tests.register("tests._base.connect", |
---|
82 | [ |
---|
83 | function smokeTest(t){ |
---|
84 | // foo sets ok to false |
---|
85 | var ok = false; |
---|
86 | var foo = { "foo": function(){ ok=false; } }; |
---|
87 | // connected function sets ok to true |
---|
88 | dojo.connect(foo, "foo", null, function(){ ok=true; }); |
---|
89 | foo.foo(); |
---|
90 | t.is(true, ok); |
---|
91 | }, |
---|
92 | function basicTest(t){ |
---|
93 | var out = ''; |
---|
94 | var obj = { |
---|
95 | foo: function(){ |
---|
96 | out += 'foo'; |
---|
97 | }, |
---|
98 | bar: function(){ |
---|
99 | out += 'bar'; |
---|
100 | }, |
---|
101 | baz: function(){ |
---|
102 | out += 'baz'; |
---|
103 | } |
---|
104 | }; |
---|
105 | // |
---|
106 | var foobar = dojo.connect(obj, "foo", obj, "bar"); |
---|
107 | dojo.connect(obj, "bar", obj, "baz"); |
---|
108 | // |
---|
109 | out = ''; |
---|
110 | obj.foo(); |
---|
111 | t.is('foobarbaz', out); |
---|
112 | // |
---|
113 | out = ''; |
---|
114 | obj.bar(); |
---|
115 | t.is('barbaz', out); |
---|
116 | // |
---|
117 | out = ''; |
---|
118 | obj.baz(); |
---|
119 | t.is('baz', out); |
---|
120 | // |
---|
121 | dojo.connect(obj, "foo", obj, "baz"); |
---|
122 | dojo.disconnect(foobar); |
---|
123 | // |
---|
124 | out = ''; |
---|
125 | obj.foo(); |
---|
126 | t.is('foobaz', out); |
---|
127 | // |
---|
128 | out = ''; |
---|
129 | obj.bar(); |
---|
130 | t.is('barbaz', out); |
---|
131 | // |
---|
132 | out = ''; |
---|
133 | obj.baz(); |
---|
134 | t.is('baz', out); |
---|
135 | }, |
---|
136 | function hubConnectDisconnect1000(t){ |
---|
137 | t.is(0, markAndSweepTest(1000)); |
---|
138 | }, |
---|
139 | function args4Test(t){ |
---|
140 | // standard 4 args test |
---|
141 | var ok, obj = { foo: function(){ok=false;}, bar: function(){ok=true;} }; |
---|
142 | dojo.connect(obj, "foo", obj, "bar"); |
---|
143 | obj.foo(); |
---|
144 | t.is(true, ok); |
---|
145 | }, |
---|
146 | function args3Test(t){ |
---|
147 | // make some globals |
---|
148 | var ok; |
---|
149 | dojo.global["gFoo"] = function(){ok=false;}; |
---|
150 | dojo.global["gOk"] = function(){ok=true;}; |
---|
151 | // 3 arg shorthand for globals (a) |
---|
152 | var link = dojo.connect("gFoo", null, "gOk"); |
---|
153 | gFoo(); |
---|
154 | dojo.disconnect(link); |
---|
155 | t.is(true, ok); |
---|
156 | // 3 arg shorthand for globals (b) |
---|
157 | link = dojo.connect(null, "gFoo", "gOk"); |
---|
158 | gFoo(); |
---|
159 | dojo.disconnect(link); |
---|
160 | t.is(true, ok); |
---|
161 | // verify disconnections |
---|
162 | gFoo(); |
---|
163 | t.is(false, ok); |
---|
164 | }, |
---|
165 | function args2Test(t){ |
---|
166 | // make some globals |
---|
167 | var ok; |
---|
168 | dojo.global["gFoo"] = function(){ok=false;}; |
---|
169 | dojo.global["gOk"] = function(){ok=true;}; |
---|
170 | // 2 arg shorthand for globals |
---|
171 | var link = dojo.connect("gFoo", "gOk"); |
---|
172 | gFoo(); |
---|
173 | dojo.disconnect(link); |
---|
174 | t.is(true, ok); |
---|
175 | // 2 arg shorthand for globals, alternate scoping |
---|
176 | link = dojo.connect("gFoo", gOk); |
---|
177 | gFoo(); |
---|
178 | dojo.disconnect(link); |
---|
179 | t.is(true, ok); |
---|
180 | }, |
---|
181 | function scopeTest1(t){ |
---|
182 | var foo = { ok: true, foo: function(){this.ok=false;} }; |
---|
183 | var bar = { ok: false, bar: function(){this.ok=true;} }; |
---|
184 | // link foo.foo to bar.bar with natural scope |
---|
185 | var link = dojo.connect(foo, "foo", bar, "bar"); |
---|
186 | foo.foo(); |
---|
187 | t.is(false, foo.ok); |
---|
188 | t.is(true, bar.ok); |
---|
189 | }, |
---|
190 | function scopeTest2(t){ |
---|
191 | var foo = { ok: true, foo: function(){this.ok=false;} }; |
---|
192 | var bar = { ok: false, bar: function(){this.ok=true;} }; |
---|
193 | // link foo.foo to bar.bar such that scope is always 'foo' |
---|
194 | var link = dojo.connect(foo, "foo", bar.bar); |
---|
195 | foo.foo(); |
---|
196 | t.is(true, foo.ok); |
---|
197 | t.is(false, bar.ok); |
---|
198 | }, |
---|
199 | function pubsub(t){ |
---|
200 | var count = 0; |
---|
201 | dojo.subscribe("/test/blah", function(first, second){ |
---|
202 | t.is("first", first); |
---|
203 | t.is("second", second); |
---|
204 | count++; |
---|
205 | }); |
---|
206 | dojo.publish("/test/blah", ["first", "second"]); |
---|
207 | t.is(1, count); |
---|
208 | }, |
---|
209 | function connectPublisher(t){ |
---|
210 | var foo = { inc: 0, foo: function(){ this.inc++; } }; |
---|
211 | var bar = { inc: 0, bar: function(){ this.inc++; } }; |
---|
212 | var c1h = dojo.connectPublisher("/blah", foo, "foo"); |
---|
213 | var c2h = dojo.connectPublisher("/blah", foo, "foo"); |
---|
214 | dojo.subscribe("/blah", bar, "bar"); |
---|
215 | foo.foo(); |
---|
216 | t.is(1, foo.inc); |
---|
217 | t.is(2, bar.inc); |
---|
218 | dojo.disconnect(c1h); |
---|
219 | foo.foo(); |
---|
220 | t.is(2, foo.inc); |
---|
221 | t.is(3, bar.inc); |
---|
222 | dojo.disconnect(c2h); |
---|
223 | foo.foo(); |
---|
224 | t.is(3, foo.inc); |
---|
225 | t.is(3, bar.inc); |
---|
226 | }, |
---|
227 | function publishSubscribe1000(t){ |
---|
228 | t.is(markAndSweepSubscribersTest(1000), 0); |
---|
229 | }, |
---|
230 | function performanceAdd(){ |
---|
231 | function listener(){} |
---|
232 | for(var i = 0;i < 1000; i++){ |
---|
233 | var foo = {}; |
---|
234 | dojo.connect(foo, "bar", listener); |
---|
235 | } |
---|
236 | }, |
---|
237 | function performanceFire(){ |
---|
238 | var foo = {}; |
---|
239 | function listener(){} |
---|
240 | dojo.connect(foo, "bar", listener); |
---|
241 | for(var i = 0;i < 100000; i++){ |
---|
242 | foo.bar(); |
---|
243 | } |
---|
244 | } |
---|
245 | ] |
---|
246 | ); |
---|