9fef46dcbbcd9b35b6b5e98da7e052f6945560db.svn-base 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. if (!dojo._hasResource["tests._base.connect"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["tests._base.connect"] = true;
  6. dojo.provide("tests._base.connect");
  7. hub = function() {
  8. }
  9. failures = 0;
  10. bad = function() {
  11. failures++;
  12. }
  13. good = function() {
  14. }
  15. // make 'iterations' connections to hub
  16. // roughly half of which will be to 'good' and
  17. // half to 'bad'
  18. // all connections to 'bad' are disconnected
  19. // test can then be performed on the values
  20. // 'failures' and 'successes'
  21. markAndSweepTest = function(iterations) {
  22. var marked = [];
  23. // connections
  24. for (var i = 0; i < iterations; i++) {
  25. if (Math.random() < 0.5) {
  26. marked.push(dojo.connect('hub', bad));
  27. } else {
  28. dojo.connect('hub', good);
  29. }
  30. }
  31. // Randomize markers (only if the count isn't very high)
  32. if (i < Math.pow(10, 4)) {
  33. var rm = [];
  34. while (marked.length) {
  35. var m = Math.floor(Math.random() * marked.length);
  36. rm.push(marked[m]);
  37. marked.splice(m, 1);
  38. }
  39. marked = rm;
  40. }
  41. for (var m = 0; m < marked.length; m++) {
  42. dojo.disconnect(marked[m]);
  43. }
  44. // test
  45. failures = 0;
  46. hub();
  47. // return number of disconnected functions that fired (should be 0)
  48. return failures;
  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. tests.register("tests._base.connect", [function smokeTest(t) {
  81. // foo sets ok to false
  82. var ok = false;
  83. var foo = {
  84. "foo" : function() {
  85. ok = false;
  86. }
  87. };
  88. // connected function sets ok to true
  89. dojo.connect(foo, "foo", null, function() {
  90. ok = true;
  91. });
  92. foo.foo();
  93. t.is(true, ok);
  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. }, function hubConnectDisconnect1000(t) {
  138. t.is(0, markAndSweepTest(1000));
  139. }, function args4Test(t) {
  140. // standard 4 args test
  141. var ok, obj = {
  142. foo : function() {
  143. ok = false;
  144. },
  145. bar : function() {
  146. ok = true
  147. }
  148. };
  149. dojo.connect(obj, "foo", obj, "bar");
  150. obj.foo();
  151. t.is(true, ok);
  152. }, function args3Test(t) {
  153. // make some globals
  154. var ok;
  155. dojo.global["gFoo"] = function() {
  156. ok = false;
  157. };
  158. dojo.global["gOk"] = function() {
  159. ok = true;
  160. };
  161. // 3 arg shorthand for globals (a)
  162. var link = dojo.connect("gFoo", null, "gOk");
  163. gFoo();
  164. dojo.disconnect(link);
  165. t.is(true, ok);
  166. // 3 arg shorthand for globals (b)
  167. link = dojo.connect(null, "gFoo", "gOk");
  168. gFoo();
  169. dojo.disconnect(link);
  170. t.is(true, ok);
  171. // verify disconnections
  172. gFoo();
  173. t.is(false, ok);
  174. }, function args2Test(t) {
  175. // make some globals
  176. var ok;
  177. dojo.global["gFoo"] = function() {
  178. ok = false;
  179. };
  180. dojo.global["gOk"] = function() {
  181. ok = true;
  182. };
  183. // 2 arg shorthand for globals
  184. var link = dojo.connect("gFoo", "gOk");
  185. gFoo();
  186. dojo.disconnect(link);
  187. t.is(true, ok);
  188. // 2 arg shorthand for globals, alternate scoping
  189. link = dojo.connect("gFoo", gOk);
  190. gFoo();
  191. dojo.disconnect(link);
  192. t.is(true, ok);
  193. }, function scopeTest1(t) {
  194. var foo = {
  195. ok : true,
  196. foo : function() {
  197. this.ok = false;
  198. }
  199. };
  200. var bar = {
  201. ok : false,
  202. bar : function() {
  203. this.ok = true
  204. }
  205. };
  206. // link foo.foo to bar.bar with natural scope
  207. var link = dojo.connect(foo, "foo", bar, "bar");
  208. foo.foo();
  209. t.is(false, foo.ok);
  210. t.is(true, bar.ok);
  211. }, function scopeTest2(t) {
  212. var foo = {
  213. ok : true,
  214. foo : function() {
  215. this.ok = false;
  216. }
  217. };
  218. var bar = {
  219. ok : false,
  220. bar : function() {
  221. this.ok = true
  222. }
  223. };
  224. // link foo.foo to bar.bar such that scope is always
  225. // 'foo'
  226. var link = dojo.connect(foo, "foo", bar.bar);
  227. foo.foo();
  228. t.is(true, foo.ok);
  229. t.is(false, bar.ok);
  230. }, function connectPublisher(t) {
  231. var foo = {
  232. inc : 0,
  233. foo : function() {
  234. this.inc++;
  235. }
  236. };
  237. var bar = {
  238. inc : 0,
  239. bar : function() {
  240. this.inc++;
  241. }
  242. };
  243. var c1h = dojo.connectPublisher("/blah", foo, "foo");
  244. var c2h = dojo.connectPublisher("/blah", foo, "foo");
  245. dojo.subscribe("/blah", bar, "bar");
  246. foo.foo();
  247. t.is(1, foo.inc);
  248. t.is(2, bar.inc);
  249. dojo.disconnect(c1h);
  250. foo.foo();
  251. t.is(2, foo.inc);
  252. t.is(3, bar.inc);
  253. dojo.disconnect(c2h);
  254. foo.foo();
  255. t.is(3, foo.inc);
  256. t.is(3, bar.inc);
  257. }, function publishSubscribe1000(t) {
  258. t.is(markAndSweepSubscribersTest(1000), 0);
  259. }]);
  260. }