summaryrefslogtreecommitdiff
path: root/includes/js/dojo/tests/_base/connect.js
blob: 37618617fd5cb7f47eaa31662f78df07631114c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
if(!dojo._hasResource["tests._base.connect"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["tests._base.connect"] = true;
dojo.provide("tests._base.connect");

hub = function(){
}

failures = 0;
bad = function(){
	failures++;
}

good = function(){
}

// make 'iterations' connections to hub
// roughly half of which will be to 'good' and 
// half to 'bad'
// all connections to 'bad' are disconnected
// test can then be performed on the values
// 'failures' and 'successes'
markAndSweepTest = function(iterations){
	var marked = [];
	// connections
	for(var i=0; i<iterations; i++){
		if(Math.random() < 0.5){
			marked.push(dojo.connect('hub', bad));
		}else{
			dojo.connect('hub', good);
		}
	}
	// Randomize markers (only if the count isn't very high)
	if(i < Math.pow(10, 4)){
		var rm = [ ];
		while(marked.length){
			var m = Math.floor(Math.random() * marked.length);
			rm.push(marked[m]);
			marked.splice(m, 1);
		}
		marked = rm;				
	} 
	for(var m=0; m<marked.length; m++){
		dojo.disconnect(marked[m]);
	}
	// test
	failures = 0;
	hub();
	// return number of disconnected functions that fired (should be 0)
	return failures;
}

markAndSweepSubscribersTest = function(iterations){
	var topic = "hubbins";
	var marked = [];
	// connections
	for(var i=0; i<iterations; i++){
		if(Math.random() < 0.5){
			marked.push(dojo.subscribe(topic, bad));
		}else{
			dojo.subscribe(topic, good);
		}
	}
	// Randomize markers (only if the count isn't very high)
	if(i < Math.pow(10, 4)){
		var rm = [ ];
		while(marked.length){
			var m = Math.floor(Math.random() * marked.length);
			rm.push(marked[m]);
			marked.splice(m, 1);
		}
		marked = rm;				
	} 
	for(var m=0; m<marked.length; m++){
		dojo.unsubscribe(marked[m]);
	}
	// test
	failures = 0;
	dojo.publish(topic);
	// return number of unsubscribed functions that fired (should be 0)
	return failures;
}

tests.register("tests._base.connect",
	[
		function smokeTest(t){
			// foo sets ok to false
			var ok = false;
			var foo = { "foo": function(){ ok=false; } };
			// connected function sets ok to true
			dojo.connect(foo, "foo", null, function(){ ok=true; });
			foo.foo();
			t.is(true, ok);
		},
		function basicTest(t) {
			var out = '';
			var obj = {
				foo: function() {
					out += 'foo';
				},
				bar: function() {
					out += 'bar';
				},
				baz: function() {
					out += 'baz';
				}
			};
			//
			var foobar = dojo.connect(obj, "foo", obj, "bar");
			dojo.connect(obj, "bar", obj, "baz");
			//
			out = '';
			obj.foo();
			t.is('foobarbaz', out);
			//
			out = '';
			obj.bar();
			t.is('barbaz', out);
			//
			out = '';
			obj.baz();
			t.is('baz', out);
			//
			dojo.connect(obj, "foo", obj, "baz");
			dojo.disconnect(foobar);
			//
			out = '';
			obj.foo();
			t.is('foobaz', out);
			//
			out = '';
			obj.bar();
			t.is('barbaz', out);
			//
			out = '';
			obj.baz();
			t.is('baz', out);
		},
		function hubConnectDisconnect1000(t){
			t.is(0, markAndSweepTest(1000));
		},
		function args4Test(t){
			// standard 4 args test
			var ok, obj = { foo: function(){ok=false;}, bar: function(){ok=true} };
			dojo.connect(obj, "foo", obj, "bar");
			obj.foo();
			t.is(true, ok);
		},
		function args3Test(t){
			// make some globals
			var ok;
			dojo.global["gFoo"] = function(){ok=false;};
			dojo.global["gOk"] = function(){ok=true;};
			// 3 arg shorthand for globals (a)
			var link = dojo.connect("gFoo", null, "gOk");
			gFoo();
			dojo.disconnect(link);
			t.is(true, ok);
			// 3 arg shorthand for globals (b)
			link = dojo.connect(null, "gFoo", "gOk");
			gFoo();
			dojo.disconnect(link);
			t.is(true, ok);
			// verify disconnections 
			gFoo();
			t.is(false, ok);
		},
		function args2Test(t){
			// make some globals
			var ok;
			dojo.global["gFoo"] = function(){ok=false;};
			dojo.global["gOk"] = function(){ok=true;};
			// 2 arg shorthand for globals 
			var link = dojo.connect("gFoo", "gOk");
			gFoo();
			dojo.disconnect(link);
			t.is(true, ok);
			// 2 arg shorthand for globals, alternate scoping 
			link = dojo.connect("gFoo", gOk);
			gFoo();
			dojo.disconnect(link);
			t.is(true, ok);
		},
		function scopeTest1(t){
			var foo = { ok: true, foo: function(){this.ok=false;} };
			var bar = { ok: false, bar: function(){this.ok=true} };
			// link foo.foo to bar.bar with natural scope
			var link = dojo.connect(foo, "foo", bar, "bar");
			foo.foo();
			t.is(false, foo.ok);
			t.is(true, bar.ok);
		},		
		function scopeTest2(t){
			var foo = { ok: true, foo: function(){this.ok=false;} };
			var bar = { ok: false, bar: function(){this.ok=true} };
			// link foo.foo to bar.bar such that scope is always 'foo'
			var link = dojo.connect(foo, "foo", bar.bar);
			foo.foo();
			t.is(true, foo.ok);
			t.is(false, bar.ok);
		},
		function connectPublisher(t){
			var foo = { inc: 0, foo: function(){ this.inc++; } };
			var bar = { inc: 0, bar: function(){ this.inc++; } };
			var c1h = dojo.connectPublisher("/blah", foo, "foo");
			var c2h = dojo.connectPublisher("/blah", foo, "foo");
			dojo.subscribe("/blah", bar, "bar");
			foo.foo();
			t.is(1, foo.inc);
			t.is(2, bar.inc);
			dojo.disconnect(c1h);
			foo.foo();
			t.is(2, foo.inc);
			t.is(3, bar.inc);
			dojo.disconnect(c2h);
			foo.foo();
			t.is(3, foo.inc);
			t.is(3, bar.inc);
		},
		function publishSubscribe1000(t){
			t.is(markAndSweepSubscribersTest(1000), 0);
		}
	]
);

}