~smagoun/whoopsie/whoopsie-lp1017637

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/tests/event-custom/tests/customevent.html

  • Committer: Evan Dandrea
  • Date: 2012-05-09 05:53:45 UTC
  • Revision ID: evan.dandrea@canonical.com-20120509055345-z2j41tmcbf4as5uf
The backend now lives in lp:daisy and the website (errors.ubuntu.com) now lives in lp:errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2
 
<html>
3
 
<head>
4
 
<title>YUI3 Custom Event Tests</title>
5
 
<script type="text/javascript" src="../../../build/yui/yui.js"></script>
6
 
</head>
7
 
 
8
 
<body class="yui3-skin-sam">
9
 
<h1>Event Tests</h1>
10
 
<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
11
 
<div id="adiv">a div</div>
12
 
<div id="demo" class="yui-module">
13
 
    <div class="yui-hd">
14
 
        <h4>Animation Demo</h4>
15
 
        <a title="remove module" class="yui-remove"><em>x</em></a>
16
 
 
17
 
    </div>
18
 
    <div class="yui-bd">
19
 
        <p>This an example of what you can do with the YUI Animation Utility.</p>
20
 
        <p><em>Follow the instructions above to see the animation in action.</em></p>
21
 
    </div>
22
 
</div>
23
 
 
24
 
 
25
 
 
26
 
<script type="text/javascript">
27
 
 
28
 
(function() {
29
 
 
30
 
    var global_notified;
31
 
 
32
 
    //YUI.add("selector-native", function(){});
33
 
    YUI({
34
 
        filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
35
 
        allowRollup: false,
36
 
        onCSS: function(Y) {
37
 
            Y.log('CSS is done loading', 'info', 'testcase');
38
 
        },
39
 
        logExclude: { get: true, Dom: true, Selector: true, Node: true, attribute: true, event: true, base: true, widget: true },
40
 
 
41
 
        filters: {
42
 
            base: 'raw',
43
 
            // dom: null,
44
 
            attribute: 'min'
45
 
        }
46
 
    }).use("dump", "test", "anim", "console", function(Y) {
47
 
 
48
 
        // Y.Global.on('yui:log', function(e) {
49
 
        //     console.log('GLOBAL LOG: ' + e.msg);
50
 
        // });
51
 
 
52
 
        var button = Y.one('#btnRun');
53
 
 
54
 
        // Set up the page
55
 
        button.set("disabled", false);
56
 
        Y.on("click", function() {
57
 
            Y.Test.Runner.run();
58
 
        }, button);
59
 
 
60
 
        var myConsole = new Y.Console().render();
61
 
        Y.log('{}');
62
 
 
63
 
        var testEventTarget = new Y.Test.Case({
64
 
            name: "Custom event tests",
65
 
 
66
 
            _should: {
67
 
                fail: {
68
 
                    testStopFnOnceFromBubbleTarget: "ticket pending",
69
 
                    testStopFnFromBubbleTarget: "ticket pending"
70
 
                }
71
 
            },
72
 
 
73
 
            testAugment: function() {
74
 
 
75
 
                var fired = false;
76
 
 
77
 
                var O = function(id) {
78
 
                    this.id = id;
79
 
                    Y.log('O constructor executed ' + id);
80
 
                }
81
 
 
82
 
                O.prototype = {
83
 
                    oOo: function(ok) {
84
 
                        Y.log('oOo');
85
 
                    }
86
 
                }
87
 
 
88
 
                // pass configuration info into EventTarget with the following
89
 
                // construct
90
 
                Y.augment(O, Y.EventTarget, null, null, {
91
 
                    emitFacade: true
92
 
                });
93
 
 
94
 
                var o = new O(),
95
 
                handle = o.on('testAugment', function(e, arg1, arg2) {
96
 
                    Y.Assert.isTrue(this instanceof O);
97
 
                    Y.Assert.isTrue(e instanceof Y.EventFacade);
98
 
                    Y.Assert.isTrue(e.foo === 'afoo');
99
 
                    Y.Assert.isTrue(e.details[1] === 1);
100
 
                    Y.Assert.isTrue(arg1 === 1);
101
 
                    Y.Assert.isTrue(arg2 === 2);
102
 
                    fired = true;
103
 
                });
104
 
 
105
 
                o.fire('testAugment', { foo: 'afoo' }, 1, 2);
106
 
 
107
 
                Y.Assert.isTrue(fired);
108
 
 
109
 
                handle.detach();
110
 
 
111
 
                // if the first argument is not an object, the
112
 
                // event facade is moved in front of the args rather
113
 
                // than overwriting existing object.
114
 
                o.on('testAugment', function(e, arg1, arg2) {
115
 
                    Y.Assert.areEqual(1, arg1);
116
 
                    Y.Assert.areEqual(2, arg2);
117
 
                });
118
 
 
119
 
                o.fire('testAugment', 1, 2);
120
 
 
121
 
            },
122
 
 
123
 
            test_detach_by_signature: function() {
124
 
 
125
 
                var anim = new Y.Anim({
126
 
                        node: '#demo',
127
 
                        to: { opacity: 0 }
128
 
                    }),
129
 
                    count = 0;
130
 
                    tester = function() {
131
 
                        count++;
132
 
                        Y.detach('foo', tester);
133
 
                    };
134
 
 
135
 
                Y.on('foo', tester);
136
 
 
137
 
                Y.fire('foo');
138
 
                Y.fire('foo');
139
 
                Y.Assert.areEqual(1, count);
140
 
 
141
 
                var onEnd = function() {
142
 
                    count++;
143
 
                    // this.detach('anim:end', onEnd);
144
 
                    this.detach('end', onEnd);
145
 
                    this.setAttrs({
146
 
                        to: { height: 1 },
147
 
                        easing: Y.Easing.bounceOut
148
 
                    });
149
 
                    this.run();
150
 
 
151
 
                    if (count > 2) {
152
 
                        throw new Error('onEnd should only have happened once');
153
 
                    }
154
 
                };
155
 
 
156
 
                // anim.on('end', onEnd);
157
 
 
158
 
                anim.run();
159
 
                anim.run();
160
 
 
161
 
 
162
 
            },
163
 
 
164
 
            testExtend: function() {
165
 
 
166
 
                var fired = false;
167
 
 
168
 
                var Base = function() {
169
 
                    Y.log('Base constructor executed');
170
 
                    arguments.callee.superclass.constructor.apply(this, arguments);
171
 
                }
172
 
 
173
 
                Y.extend(Base, Y.EventTarget, {
174
 
                    base: function() {
175
 
                        Y.log('all your base...');
176
 
                    }
177
 
                });
178
 
 
179
 
                var b = new Base();
180
 
                b.on('testExtend', function(arg1, arg2) {
181
 
                    Y.Assert.isTrue(this instanceof Base);
182
 
                    Y.Assert.isTrue(arg1 === 1);
183
 
                    Y.Assert.isTrue(arg2 === 2);
184
 
                    fired = true;
185
 
                });
186
 
 
187
 
                b.fire('testExtend', 1, 2);
188
 
 
189
 
                Y.Assert.isTrue(fired);
190
 
            },
191
 
 
192
 
            testPrefix: function() {
193
 
 
194
 
                var fired1 = false,
195
 
                    fired2 = false;
196
 
 
197
 
                var O = function(id) {
198
 
                    this.id = id;
199
 
                    Y.log('O constructor executed ' + id);
200
 
                }
201
 
 
202
 
                O.prototype = {
203
 
                    oOo: function(ok) {
204
 
                        Y.log('oOo');
205
 
                    }
206
 
                }
207
 
 
208
 
                // pass configuration info into EventTarget with the following
209
 
                // construct
210
 
                Y.augment(O, Y.EventTarget, null, null, {
211
 
                    emitFacade: true,
212
 
                    prefix: 'prefix'
213
 
                });
214
 
 
215
 
                var o = new O();
216
 
                o.on('testPrefix', function(e, arg1, arg2) {
217
 
                    Y.Assert.isTrue(this instanceof O);
218
 
                    fired1 = true;
219
 
                });
220
 
 
221
 
                o.on('prefix:testPrefix', function(e, arg1, arg2) {
222
 
                    Y.Assert.isTrue(this instanceof O);
223
 
                    fired2 = true;
224
 
                });
225
 
 
226
 
                o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
227
 
 
228
 
                Y.Assert.isTrue(fired1);
229
 
                // Y.Assert.isTrue(fired2);
230
 
 
231
 
                fired1 = false;
232
 
                fired2 = false;
233
 
 
234
 
                o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
235
 
                Y.Assert.isTrue(fired1);
236
 
                Y.Assert.isTrue(fired2);
237
 
            },
238
 
 
239
 
            testDetachKey: function() {
240
 
 
241
 
                var fired1 = false,
242
 
                    fired2 = false;
243
 
 
244
 
                Y.on('handle|test:event', function() {
245
 
                    fired1 = true;
246
 
                });
247
 
 
248
 
                // one listener
249
 
                Y.fire('test:event');
250
 
                Y.Assert.isTrue(fired1);
251
 
                Y.Assert.isFalse(fired2);
252
 
 
253
 
                Y.detach('handle|test:event');
254
 
 
255
 
                fired1 = false;
256
 
                fired2 = false;
257
 
 
258
 
                Y.on('handle|test:event', function() {
259
 
                    fired2 = true;
260
 
                });
261
 
 
262
 
                // first lisener detached, added a new listener
263
 
                Y.fire('test:event');
264
 
                Y.Assert.isFalse(fired1);
265
 
                Y.Assert.isTrue(fired2);
266
 
 
267
 
                Y.detach('handle|test:event');
268
 
                fired1 = false;
269
 
                fired2 = false;
270
 
 
271
 
                Y.after('handle|test:event', function(arg1) {
272
 
                    Y.Assert.areEqual('orange', arg1);
273
 
                    Y.Assert.isTrue(fired1);
274
 
                    fired2 = true;
275
 
                });
276
 
 
277
 
                // comma or pipe
278
 
                Y.on('handle|test:event', function(arg1) {
279
 
                    Y.Assert.areEqual('orange', arg1);
280
 
                    Y.Assert.isFalse(fired2);
281
 
                    fired1 = true;
282
 
                });
283
 
 
284
 
                // testing on and after order
285
 
                Y.fire('test:event', 'orange');
286
 
 
287
 
                fired1 = false;
288
 
                fired2 = false;
289
 
 
290
 
                // spaces after the comma or lack thereof should have
291
 
                // no effect on the addition or removal of listeners
292
 
                var ret = Y.detach('handle|test:event');
293
 
 
294
 
                Y.Assert.areEqual(Y, ret);
295
 
 
296
 
                // added both an on listener and an after listener,
297
 
                // then detached both
298
 
                Y.fire('test:event', 'orange');
299
 
                Y.Assert.isFalse(fired1);
300
 
                Y.Assert.isFalse(fired2);
301
 
 
302
 
            },
303
 
 
304
 
            testDetachAllByKey: function() {
305
 
 
306
 
                var fired1 = false,
307
 
                    fired2 = false;
308
 
 
309
 
                Y.after('handle|event2', function() {
310
 
                    fired2 = true;
311
 
                });
312
 
 
313
 
                Y.on('handle|event2', function() {
314
 
                    fired1 = true;
315
 
                });
316
 
 
317
 
                // detachAll
318
 
                Y.detach('handle|*');
319
 
 
320
 
                Y.fire('event2');
321
 
 
322
 
                Y.Assert.isFalse(fired1, 'fired1, the after listener should not have fired.');
323
 
                Y.Assert.isFalse(fired2, 'fired2, the on listener should not have fired.');
324
 
 
325
 
            },
326
 
 
327
 
            testChain: function() {
328
 
 
329
 
                var fired1 = false,
330
 
                    fired2 = false,
331
 
                    fired3 = false,
332
 
                    fired4 = false,
333
 
                    fired5 = false;
334
 
 
335
 
                // should be executed once, after f2
336
 
                var f1 = function() {
337
 
                    Y.Assert.isTrue(fired2);
338
 
                    fired1 = true;
339
 
                };
340
 
 
341
 
                // should be executed once, before f1
342
 
                var f2 = function() {
343
 
                    Y.Assert.isFalse(fired1);
344
 
                    fired2 = true;
345
 
                };
346
 
 
347
 
                // should be executed once, different event from f1 and f2
348
 
                var f3 = function() {
349
 
                    fired3 = true;
350
 
                };
351
 
 
352
 
                // detached before fired, should not executed
353
 
                var f4 = function() {
354
 
                    fired4 = true;
355
 
                };
356
 
 
357
 
                // should fire once, preserving the custom prefix rather
358
 
                // than using the configured event target prefix
359
 
                var f5 = function() {
360
 
                    fired5 = true;
361
 
                };
362
 
 
363
 
                // configure chaining via global default or on the event target
364
 
                YUI({ /* chain: true */
365
 
                    base:'../../../build/',
366
 
                    logInclude: {
367
 
                        test: true
368
 
                    }
369
 
                }).use('event-custom', function(Y2) {
370
 
 
371
 
                    var o = new Y2.EventTarget({
372
 
                        prefix: 'foo',
373
 
                        chain : true
374
 
                    });
375
 
 
376
 
                    // without event target prefix manipulation (incomplete now)
377
 
                    // @TODO an error here is throwing an uncaught exception rather than failing the test
378
 
                    // Y2.after('p:e', f1).on('p:e', f2).on('p:e2', f3).on('detach, p:e', f4).detach('detach, p:e').fire('p:e').fire('p:e2');
379
 
 
380
 
                    // with event target prefix manipulation ('e' is the same event as 'foo:e',
381
 
                    // but 'pre:e' is a different event only accessible by using that exact name)
382
 
o.after('e', f1).on('foo:e', f2).on('foo:e2', f3).on('detach, e', f4).detach('detach,e').fire('foo:e').fire('e2').on('pre:e', f5).fire('pre:e');
383
 
 
384
 
                    Y.Assert.isTrue(fired1);  // verifies chaining, on/after order, and adding the event target prefix
385
 
                    Y.Assert.isTrue(fired2);  // verifies chaining, on/after order, and accepting the prefix in the event name
386
 
                    Y.Assert.isTrue(fired3);  // verifies no interaction between events, and prefix manipulation
387
 
                    Y.Assert.isFalse(fired4); // verifies detach works (regardless of spaces after comma)
388
 
                    Y.Assert.isTrue(fired5);  // verifies custom prefix
389
 
 
390
 
                });
391
 
 
392
 
            },
393
 
 
394
 
            testObjType: function() {
395
 
                var f1, f2;
396
 
                Y.on({
397
 
                    'y:click': function() {f1 = true},
398
 
                    'y:clack': function() {f2 = true}
399
 
                });
400
 
 
401
 
                Y.fire('y:click');
402
 
                Y.fire('y:clack');
403
 
 
404
 
                Y.Assert.isTrue(f1);
405
 
                Y.Assert.isTrue(f2);
406
 
            },
407
 
 
408
 
            testBubble: function() {
409
 
                var count = 0,
410
 
                    ret,
411
 
                    config = {
412
 
                        emitFacade: true,
413
 
                        bubbles: true
414
 
                    },
415
 
                    a = new Y.EventTarget(config),
416
 
                    b = new Y.EventTarget(config);
417
 
 
418
 
                b.addTarget(a);
419
 
 
420
 
                // this should not be necessary // fixed
421
 
                // b.publish('test:foo');
422
 
 
423
 
                a.on('test:foo', function(e) {
424
 
                    count++;
425
 
                    // we will fire this on the parent, so that should be the target
426
 
                    Y.Assert.areEqual(b, e.target);
427
 
                    Y.Assert.areEqual(a, e.currentTarget);
428
 
                });
429
 
 
430
 
                ret = b.fire('test:foo', {}, b);
431
 
 
432
 
                Y.Assert.areEqual(1, count);
433
 
                Y.Assert.isTrue(ret);
434
 
 
435
 
                b.on('test:foo', function(e) {
436
 
                    e.stopPropagation();
437
 
                });
438
 
 
439
 
                ret = b.fire('test:foo', {}, b);
440
 
 
441
 
                Y.Assert.areEqual(1, count);
442
 
                Y.Assert.isFalse(ret);
443
 
            },
444
 
 
445
 
 
446
 
            testPreventFnOnce: function() {
447
 
                var count = 0;
448
 
                Y.publish('y:foo1', {
449
 
                    emitFacade: true,
450
 
                    preventedFn: function() {
451
 
                        count++;
452
 
                        Y.Assert.isTrue(this instanceof YUI);
453
 
                    }
454
 
                });
455
 
 
456
 
                Y.on('y:foo1', function(e) {
457
 
                    e.preventDefault();
458
 
                });
459
 
 
460
 
                Y.on('y:foo1', function(e) {
461
 
                    e.preventDefault();
462
 
                });
463
 
 
464
 
                Y.fire('y:foo1');
465
 
 
466
 
                Y.Assert.areEqual(1, count);
467
 
            },
468
 
 
469
 
            testPreventFromBubbleTarget: function () {
470
 
                var count = 0,
471
 
                    target = new Y.EventTarget({ prefix: 'x' });
472
 
 
473
 
                target.publish('foo', {
474
 
                    emitFacade: true,
475
 
                    preventedFn: function() {
476
 
                        count++;
477
 
                    }
478
 
                });
479
 
 
480
 
                target.addTarget(Y);
481
 
 
482
 
                Y.on('x:foo', function(e) {
483
 
                    e.preventDefault();
484
 
                });
485
 
 
486
 
                target.fire('foo');
487
 
 
488
 
                Y.Assert.areEqual(1, count);
489
 
            },
490
 
 
491
 
            testPreventedFnOnceFromBubbleTarget: function () {
492
 
                var count = 0,
493
 
                    target = new Y.EventTarget({ prefix: 'x' });
494
 
 
495
 
                target.publish('foo', {
496
 
                    emitFacade: true,
497
 
                    preventedFn: function() {
498
 
                        count++;
499
 
                    }
500
 
                });
501
 
 
502
 
                target.addTarget(Y);
503
 
 
504
 
                Y.on('x:foo', function(e) {
505
 
                    e.preventDefault();
506
 
                });
507
 
 
508
 
                Y.on('x:foo', function(e) {
509
 
                    e.preventDefault();
510
 
                });
511
 
 
512
 
                target.fire('foo');
513
 
 
514
 
                Y.Assert.areEqual(1, count);
515
 
 
516
 
                target.on('foo', function (e) {
517
 
                    e.preventDefault();
518
 
                });
519
 
 
520
 
                target.fire('foo');
521
 
 
522
 
                Y.Assert.areEqual(2, count);
523
 
            },
524
 
 
525
 
            testStopFnOnce: function () {
526
 
                var count = 0,
527
 
                    target = new Y.EventTarget({ prefix: 'a' });
528
 
 
529
 
                target.publish('foo', {
530
 
                    emitFacade: true,
531
 
                    stoppedFn: function () {
532
 
                        count++;
533
 
                    }
534
 
                });
535
 
 
536
 
                target.on('foo', function (e) {
537
 
                    e.stopPropagation();
538
 
                });
539
 
 
540
 
                target.on('foo', function (e) {
541
 
                    e.stopPropagation();
542
 
                });
543
 
 
544
 
                target.fire('foo');
545
 
 
546
 
                Y.Assert.areEqual(1, count);
547
 
            },
548
 
 
549
 
            testStopFnFromBubbleTarget: function () {
550
 
                var count = 0,
551
 
                    origin = new Y.EventTarget({ prefix: 'a' }),
552
 
                    targetB = new Y.EventTarget({ prefix: 'b' });
553
 
 
554
 
                origin.publish('foo', {
555
 
                    emitFacade: true,
556
 
                    stoppedFn: function () {
557
 
                        count++;
558
 
                    }
559
 
                });
560
 
 
561
 
                targetB.on('foo', function (e) {
562
 
                    e.stopPropagation();
563
 
                });
564
 
 
565
 
                origin.fire('foo');
566
 
 
567
 
                Y.Assert.areEqual(1, count);
568
 
            },
569
 
 
570
 
            testStopFnOnceFromBubbleTarget: function () {
571
 
                var count = 0,
572
 
                    origin = new Y.EventTarget({ prefix: 'a' }),
573
 
                    targetB = new Y.EventTarget({ prefix: 'b' }),
574
 
                    targetC = new Y.EventTarget({ prefix: 'c' });
575
 
 
576
 
                origin.publish('foo', {
577
 
                    emitFacade: true,
578
 
                    stoppedFn: function() {
579
 
                        count++;
580
 
                    }
581
 
                });
582
 
 
583
 
                targetB.addTarget(targetC);
584
 
                origin.addTarget(targetB);
585
 
 
586
 
                targetB.on('foo', function (e) {
587
 
                    e.stopPropagation();
588
 
                });
589
 
 
590
 
                targetB.on('foo', function (e) {
591
 
                    e.stopPropagation();
592
 
                });
593
 
 
594
 
                origin.fire('foo');
595
 
 
596
 
                Y.Assert.areEqual(1, count, "stopProp called twice from bubble target resulted in stoppedFn called wrong number of times");
597
 
 
598
 
                count = 0;
599
 
 
600
 
                targetC.on('foo', function (e) {
601
 
                    e.stopPropagation();
602
 
                });
603
 
 
604
 
                origin.fire('foo');
605
 
 
606
 
                Y.Assert.areEqual(1, count, "stopProp called from intermediate bubble target didn't prevent stoppedFn call from subsequent bubble target");
607
 
 
608
 
                count = 0;
609
 
 
610
 
                origin.on('foo', function (e) {
611
 
                    e.stopPropagation();
612
 
                });
613
 
 
614
 
                origin.fire('foo');
615
 
 
616
 
                Y.Assert.areEqual(1, count, "stopProp called from event origin subscription didn't prevent calls to stoppedFn from bubble target");
617
 
            },
618
 
 
619
 
            testDetachHandle: function() {
620
 
                var count = 0, handle, handle2;
621
 
                Y.publish('y:foo', {
622
 
                    emitFacade: true
623
 
                });
624
 
 
625
 
                Y.on('y:foo', function(e) {
626
 
                    count++;
627
 
                    handle2.detach();
628
 
                });
629
 
 
630
 
                handle = Y.on('y:foo', function(e) {
631
 
                    count += 100;
632
 
                });
633
 
 
634
 
                handle2 = Y.on('y:foo', function(e) {
635
 
                    count += 1000;
636
 
                });
637
 
 
638
 
                Y.detach(handle);
639
 
 
640
 
                Y.fire('y:foo');
641
 
 
642
 
                Y.Assert.areEqual(1, count);
643
 
 
644
 
                count = 0;
645
 
 
646
 
                var handle3 = Y.on('y:click', function() {
647
 
                    count++;
648
 
                    handle3.detach();
649
 
                });
650
 
 
651
 
                Y.fire('y:click');
652
 
                Y.fire('y:click');
653
 
 
654
 
                var o = new Y.EventTarget();
655
 
 
656
 
                count = 0;
657
 
 
658
 
                o.on('foo', function(e) {
659
 
                    count++;
660
 
                });
661
 
 
662
 
                o.on('foo', function(e) {
663
 
                    count++;
664
 
                });
665
 
 
666
 
                o.detachAll();
667
 
 
668
 
                o.fire('foo');
669
 
 
670
 
                Y.Assert.areEqual(0, count);
671
 
 
672
 
                var handle3 = Y.on('y:click', function() {
673
 
                    count++;
674
 
                });
675
 
 
676
 
                // detachAll can't be allowed to work on the YUI instance.
677
 
                Y.detachAll();
678
 
 
679
 
                Y.fire('y:click');
680
 
 
681
 
                Y.Assert.areEqual(1, count);
682
 
            },
683
 
 
684
 
            testBroadcast: function() {
685
 
                var o = new Y.EventTarget(), s1, s2, s3, s4;
686
 
 
687
 
                o.publish('y:foo2', {
688
 
                    emitFacade: true,
689
 
                    broadcast: 1
690
 
                });
691
 
 
692
 
                Y.on('y:foo2', function() {
693
 
                    Y.log('Y foo2 executed');
694
 
                    s1 = 1;
695
 
                });
696
 
 
697
 
                Y.Global.on('y:foo2', function() {
698
 
                    Y.log('GLOBAL foo2 executed');
699
 
                    s2 = 1;
700
 
                });
701
 
 
702
 
                o.fire('y:foo2');
703
 
 
704
 
                Y.Assert.areEqual(1, s1);
705
 
                Y.Assert.areNotEqual(1, s2);
706
 
 
707
 
                s1 = 0;
708
 
                s2 = 0;
709
 
 
710
 
                o.publish('y:bar', {
711
 
                    emitFacade: true,
712
 
                    broadcast: 2
713
 
                });
714
 
 
715
 
                Y.on('y:bar', function() {
716
 
                    Y.log('Y bar executed');
717
 
                    s3 = 1;
718
 
                });
719
 
 
720
 
                Y.Global.on('y:bar', function() {
721
 
                    Y.log('GLOBAL bar executed');
722
 
                    s4 = 1;
723
 
                });
724
 
 
725
 
                o.fire('y:bar');
726
 
 
727
 
                Y.Assert.areEqual(1, s3);
728
 
                Y.Assert.areEqual(1, s4);
729
 
 
730
 
                Y.Global.on('y:bar', function(e) {
731
 
                    Y.Assert.areEqual(0, e.stopped);
732
 
                    // Y.Assert.areEqual(0, e._event.stopped);
733
 
                    Y.log('GLOBAL bar executed');
734
 
                    e.stopPropagation();
735
 
                });
736
 
 
737
 
                o.fire('y:bar');
738
 
                o.fire('y:bar');
739
 
 
740
 
                Y.Global.detachAll();
741
 
 
742
 
            },
743
 
 
744
 
            test_fire_once: function() {
745
 
 
746
 
                var notified = 0;
747
 
 
748
 
                Y.publish('fireonce', {
749
 
                    fireOnce: true
750
 
                });
751
 
 
752
 
                Y.fire('fireonce', 'foo', 'bar');
753
 
 
754
 
                Y.on('fireonce', function(arg1, arg2) {
755
 
                    notified++;
756
 
                    Y.Assert.areEqual('foo', arg1, 'arg1 not correct for lazy fireOnce listener')
757
 
                    Y.Assert.areEqual('bar', arg2, 'arg2 not correct for lazy fireOnce listener')
758
 
                });
759
 
 
760
 
                Y.fire('fireonce', 'foo2', 'bar2');
761
 
                Y.fire('fireonce', 'foo3', 'bar3');
762
 
 
763
 
                global_notified = false;
764
 
 
765
 
                Y.on('fireonce', function(arg1, arg2) {
766
 
                    Y.log('the notification is asynchronous, so I need to wait for this test');
767
 
                    Y.Assert.areEqual(1, notified, 'listener notified more than once.');
768
 
                    global_notified = true;
769
 
                });
770
 
 
771
 
                // it is no longer asynchronous
772
 
                // Y.Assert.isFalse(global_notified, 'notification was not asynchronous');
773
 
 
774
 
            },
775
 
 
776
 
            test_async_fireonce: function() {
777
 
                Y.Assert.isTrue(global_notified, 'asynchronous notification did not seem to work.');
778
 
            },
779
 
 
780
 
            test_node_publish: function() {
781
 
                var node = Y.one('#adiv');
782
 
 
783
 
                var preventCount = 0, heard = 0;
784
 
                node.publish('foo1', {
785
 
                    emitFacade: true,
786
 
                    // should only be called once
787
 
                    preventedFn: function() {
788
 
                        preventCount++;
789
 
                        Y.Assert.isTrue(this instanceof Y.Node);
790
 
                    }
791
 
                });
792
 
 
793
 
                node.on('foo1', function(e) {
794
 
                    Y.Assert.areEqual('faking foo', e.type);
795
 
                    Y.Assert.areEqual('foo1', e._type);
796
 
                    heard++;
797
 
                    e.preventDefault();
798
 
                });
799
 
 
800
 
                node.on('foo1', function(e) {
801
 
                    heard++;
802
 
                    e.preventDefault();
803
 
                });
804
 
 
805
 
                node.fire('foo1', {
806
 
                    type: 'faking foo'
807
 
                });
808
 
 
809
 
                Y.Assert.areEqual(1, preventCount);
810
 
                Y.Assert.areEqual(2, heard);
811
 
            },
812
 
 
813
 
            // SRC, ON
814
 
            // BUBBLE, ON
815
 
            // BUBBLE, DEFAULT BEHAVIOR
816
 
            // BUBBLE, AFTER
817
 
            // SRC, DEFAULT BEHAVIOR
818
 
            // SRC, AFTER
819
 
            __testBubbleSequence300GA: function() {
820
 
                var count = 0,
821
 
                    called = null,
822
 
                    fn = function() {
823
 
                        called = this.name;
824
 
                    },
825
 
                    config = {
826
 
                        emitFacade: true,
827
 
                        bubbles: true
828
 
                    },
829
 
                    leaf = new Y.EventTarget(config),
830
 
                    branch = new Y.EventTarget(config),
831
 
                    root = new Y.EventTarget(config);
832
 
 
833
 
                leaf.name = 'leaf';
834
 
                branch.name = 'branch';
835
 
                root.name = 'root';
836
 
 
837
 
                leaf.addTarget(branch);
838
 
                branch.addTarget(root);
839
 
 
840
 
                leaf.publish('test:foo', { defaultFn: fn});
841
 
                branch.publish('test:foo', { defaultFn: fn});
842
 
                root.publish('test:foo', { defaultFn: fn});
843
 
 
844
 
                leaf.on('test:foo', function(e) {
845
 
                    Y.Assert.areEqual(0, count, 'leaf.on should be first');
846
 
                    Y.Assert.isNull(called, 'leaf.on should be executed before any default function');
847
 
                    count++;
848
 
                });
849
 
 
850
 
                branch.on('test:foo', function() {
851
 
                    Y.Assert.areEqual(1, count, 'branch.on should be second');
852
 
                    Y.Assert.isNull(called, 'branch.on should be executed before any default function');
853
 
                    count++;
854
 
                });
855
 
 
856
 
                root.on('test:foo', function() {
857
 
                    Y.Assert.areEqual(2, count, 'root.on should be third');
858
 
                    Y.Assert.isNull(called, 'root.on should be executed before any default function');
859
 
                    count++;
860
 
                });
861
 
 
862
 
                root.after('test:foo', function() {
863
 
                    Y.Assert.areEqual(3, count, 'root.after should be fourth');
864
 
                    Y.Assert.areEqual('root', called, 'root.after should be executed after the root default function');
865
 
                    count++;
866
 
                });
867
 
 
868
 
                branch.after('test:foo', function() {
869
 
                    Y.Assert.areEqual(4, count, 'branch.after should be fifth');
870
 
                    Y.Assert.areEqual('branch', called, 'branch.after should be executed after the branch default function');
871
 
                    count++;
872
 
                });
873
 
 
874
 
                leaf.after('test:foo', function(e) {
875
 
                    Y.Assert.areEqual(5, count, 'leaf.after should be sixth and last');
876
 
                    Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the leaf default function');
877
 
                    count++;
878
 
                });
879
 
 
880
 
                leaf.fire('test:foo', {}, leaf);
881
 
                Y.Assert.areEqual(6, count);
882
 
 
883
 
            },
884
 
 
885
 
            // Ideally it should be this, but the defaultFn order is the least important bit
886
 
            // and there are issues changing the order.
887
 
            // SRC, ON
888
 
            // BUBBLE, ON
889
 
            // SRC, DEFAULT BEHAVIOR
890
 
            // BUBBLE, DEFAULT BEHAVIOR (unless configured to only execute the default function on the target)
891
 
            // SRC, AFTER
892
 
            // BUBBLE, AFTER
893
 
 
894
 
            // The actual order is this:
895
 
            // SRC, ON
896
 
            // BUBBLE, ON
897
 
            // BUBBLE, DEFAULT BEHAVIOR
898
 
            // SRC, DEFAULT BEHAVIOR (unless configured to only execute the default function on the target)
899
 
            // SRC, AFTER
900
 
            // BUBBLE, AFTER
901
 
            testAlternativeSequencePost300GA: function() {
902
 
                var count = 0,
903
 
                    called = null,
904
 
                    fn = function() {
905
 
                        called = this.name;
906
 
                    },
907
 
                    config = {
908
 
                        emitFacade: true,
909
 
                        bubbles: true
910
 
                    },
911
 
                    leaf = new Y.EventTarget(config),
912
 
                    branch = new Y.EventTarget(config),
913
 
                    root = new Y.EventTarget(config);
914
 
 
915
 
                leaf.name = 'leaf';
916
 
                branch.name = 'branch';
917
 
                root.name = 'root';
918
 
 
919
 
                leaf.addTarget(branch);
920
 
                branch.addTarget(root);
921
 
 
922
 
                leaf.publish('test:foo', { defaultFn: fn});
923
 
                branch.publish('test:foo', { defaultFn: fn});
924
 
                root.publish('test:foo', { defaultFn: fn});
925
 
 
926
 
                leaf.on('test:foo', function(e) {
927
 
                    Y.Assert.areEqual(0, count, 'leaf.on should be first');
928
 
                    Y.Assert.isNull(called, 'leaf.on should be executed before any default function');
929
 
                    count++;
930
 
                });
931
 
 
932
 
                branch.on('test:foo', function() {
933
 
                    Y.Assert.areEqual(1, count, 'branch.on should be second');
934
 
                    Y.Assert.isNull(called, 'branch.on should be executed before any default function');
935
 
                    count++;
936
 
                });
937
 
 
938
 
                root.on('test:foo', function() {
939
 
                    Y.Assert.areEqual(2, count, 'root.on should be third');
940
 
                    Y.Assert.isNull(called, 'root.on should be executed before any default function');
941
 
                    count++;
942
 
                });
943
 
 
944
 
                leaf.after('test:foo', function(e) {
945
 
                    Y.Assert.areEqual(3, count, 'leaf.after should be fourth');
946
 
                    // Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
947
 
                    Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
948
 
                    count++;
949
 
                });
950
 
 
951
 
                branch.after('test:foo', function() {
952
 
                    Y.Assert.areEqual(4, count, 'branch.after should be fifth');
953
 
                    // Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
954
 
                    Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
955
 
                    count++;
956
 
                });
957
 
 
958
 
                root.after('test:foo', function() {
959
 
                    Y.Assert.areEqual(5, count, 'root.after should be sixth and last');
960
 
                    // Y.Assert.areEqual('root', called, 'leaf.after should be executed after the root default function');
961
 
                    Y.Assert.areEqual('leaf', called, 'leaf.after should be executed after the root default function');
962
 
                    count++;
963
 
                });
964
 
 
965
 
                leaf.fire('test:foo', {}, leaf);
966
 
                Y.Assert.areEqual(6, count, 'total subscriber count');
967
 
 
968
 
            },
969
 
 
970
 
            testStarSubscriber: function() {
971
 
                var count = 0,
972
 
                    ret,
973
 
                    config = {
974
 
                        emitFacade: true,
975
 
                        bubbles: true,
976
 
                        prefix: 'stars'
977
 
                    },
978
 
                    z = new Y.EventTarget(config),
979
 
                    a = new Y.EventTarget(config),
980
 
                    b = new Y.EventTarget(config);
981
 
 
982
 
                b.addTarget(a);
983
 
                a.addTarget(z);
984
 
 
985
 
                z.on('*:foo', function(e) {
986
 
                    count++;
987
 
                    // b -> a -> z -- the parent's parent should be the target
988
 
                    Y.Assert.areEqual(b, e.target);
989
 
                    Y.Assert.areEqual(z, e.currentTarget);
990
 
                    switch (count) {
991
 
                        case 1:
992
 
                            Y.Assert.areEqual('a:foo', e.type);
993
 
                            break;
994
 
                        case 2:
995
 
                            Y.Assert.areEqual('b:foo', e.type);
996
 
                            break;
997
 
                        case 3:
998
 
                            Y.Assert.areEqual('stars:foo', e.type);
999
 
                            break;
1000
 
                    }
1001
 
                });
1002
 
 
1003
 
                ret = b.fire('a:foo', {}, b);
1004
 
 
1005
 
                Y.Assert.areEqual(1, count);
1006
 
                Y.Assert.isTrue(ret);
1007
 
 
1008
 
                ret = b.fire('b:foo', {}, b);
1009
 
 
1010
 
                Y.Assert.areEqual(2, count);
1011
 
                Y.Assert.isTrue(ret);
1012
 
 
1013
 
                // if the event target is not configured with a prefix, this won't work by design.
1014
 
                ret = b.fire('foo', {}, b);
1015
 
                Y.Assert.areEqual(3, count);
1016
 
 
1017
 
                Y.Assert.isTrue(ret);
1018
 
            },
1019
 
 
1020
 
            testPreventBubble: function() {
1021
 
                var count = 0,
1022
 
                    ret,
1023
 
                    config = {
1024
 
                        emitFacade: true,
1025
 
                        bubbles: true,
1026
 
                        prefix: 'stars'
1027
 
                    },
1028
 
                    z = new Y.EventTarget(config),
1029
 
                    a = new Y.EventTarget(config),
1030
 
                    b = new Y.EventTarget(config);
1031
 
 
1032
 
                b.addTarget(a);
1033
 
                a.addTarget(z);
1034
 
 
1035
 
                z.after('*:foo', function(e) {
1036
 
 
1037
 
                    // e.preventDefault();
1038
 
                    Y.Assert.areEqual(b, e.target);
1039
 
 
1040
 
                });
1041
 
 
1042
 
                ret = b.fire('a:foo', {}, b);
1043
 
 
1044
 
                Y.Assert.areEqual(0, count);
1045
 
            },
1046
 
 
1047
 
            test_listen_once: function() {
1048
 
 
1049
 
                var count = 0;
1050
 
 
1051
 
                Y.once(['foo', 'bar'], function(e) {
1052
 
                    count++;
1053
 
                });
1054
 
 
1055
 
                Y.fire('foo', 'bar');
1056
 
                Y.fire('bar', 'bar');
1057
 
 
1058
 
                Y.Assert.areEqual(2, count);
1059
 
 
1060
 
                Y.fire('foo', 'bar');
1061
 
                Y.fire('bar', 'bar');
1062
 
 
1063
 
                Y.Assert.areEqual(2, count);
1064
 
 
1065
 
            },
1066
 
 
1067
 
            test_array_type_param: function() {
1068
 
                var result = '';
1069
 
 
1070
 
                var handle1 = Y.after(['foo', 'bar'], function(type) {
1071
 
                    result += 'after' + type;
1072
 
                });
1073
 
 
1074
 
                var handle2 = Y.on(['foo', 'bar'], function(type) {
1075
 
                    result += 'on' + type;
1076
 
                });
1077
 
 
1078
 
                Y.fire('foo', 'foo');
1079
 
                Y.fire('bar', 'bar');
1080
 
 
1081
 
                Y.Assert.areEqual('onfooafterfooonbarafterbar', result);
1082
 
 
1083
 
                handle1.detach();
1084
 
                handle2.detach();
1085
 
 
1086
 
                Y.fire('foo', 'foo');
1087
 
                Y.fire('bar', 'bar');
1088
 
 
1089
 
                Y.Assert.areEqual('onfooafterfooonbarafterbar', result);
1090
 
            },
1091
 
 
1092
 
            test_bubble_config: function() {
1093
 
 
1094
 
                var a = new Y.EventTarget(),
1095
 
                    b = new Y.EventTarget(),
1096
 
                    result;
1097
 
 
1098
 
                a.publish("foo", {
1099
 
                    emitFacade: true
1100
 
                });
1101
 
                a.addTarget(b);
1102
 
 
1103
 
                b.on("foo", function(e) {
1104
 
                    result = (e instanceof Y.EventFacade);
1105
 
                });
1106
 
 
1107
 
                a.fire("foo");
1108
 
 
1109
 
                Y.Assert.isTrue(result);
1110
 
 
1111
 
            },
1112
 
 
1113
 
            test_onceAfter: function () {
1114
 
                var a = new Y.EventTarget({ emitFacade: true, prefix: 'a' }),
1115
 
                    result = '';
1116
 
 
1117
 
                a.on('foo', function () { result += 'A'; });
1118
 
                a.once('foo', function () { result += 'B'; });
1119
 
                a.after('foo', function () { result += 'C'; });
1120
 
                a.onceAfter('foo', function () { result += 'D'; });
1121
 
 
1122
 
                a.fire('foo');
1123
 
                a.fire('foo');
1124
 
 
1125
 
                Y.Assert.areSame("ABCDAC", result);
1126
 
            },
1127
 
 
1128
 
            test_multiple_object_publish: function () {
1129
 
                var target = new Y.EventTarget({ emitFacade: true, prefix: 'a' }),
1130
 
                    pass;
1131
 
 
1132
 
                target.publish({ foo: {} });
1133
 
                target.publish({
1134
 
                    bar: {
1135
 
                        defaultFn: function () { pass = true; }
1136
 
                    }
1137
 
                });
1138
 
 
1139
 
                target.fire('bar');
1140
 
 
1141
 
                Y.Assert.isTrue(pass);
1142
 
            }
1143
 
        });
1144
 
 
1145
 
        Y.Test.Runner.setName("CustomEvent");
1146
 
        Y.Test.Runner.add(testEventTarget);
1147
 
        Y.Test.Runner.run();
1148
 
    });
1149
 
 
1150
 
   //  YUI({
1151
 
   //      base: "../../../build/",
1152
 
   //      filter: "debug",
1153
 
   //      combine: false,
1154
 
   //      useConsole: true,
1155
 
   //      logExclude: {Dom: true, Selector: true, Node: true, attribute: true, base: true, loader: true, get: true, widget: true}
1156
 
   //  }).use("datasource", function(Y) {
1157
 
   //      Y.log('loaded datasource: ' + Y.DataSource);
1158
 
   //  });
1159
 
 
1160
 
 
1161
 
})();
1162
 
</script>
1163
 
</body>
1164
 
</html>