Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

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