~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to src/maasserver/static/jslibs/yui/3.4.1/tests/event/tests/delegate.html

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!doctype html>
2
 
<html>
3
 
<head>
4
 
    <meta charset="utf-8">
5
 
        <title>YUI Event Delegate Tests</title>
6
 
</head>
7
 
<body lang="en" class="yui3-skin-sam">
8
 
    <div id="doc">
9
 
 
10
 
                <div id="mod1">
11
 
                        <div id="mod-header" class="hd"><h3 class="title">H3 - Title</h3></div>
12
 
                        <div id="mod-body" class="bd">
13
 
                                <p>simple paragraph with a link <a href="#"id="firstlink">simple link</a></p>
14
 
                                <p>another paragraph with a complex link <a href="#" id="secondlink"><strong>strong within link</strong><img alt="fake image" id="fakeimage" /> - complex <span id="spanwithinlink">link</span></a></p>
15
 
                        </div>
16
 
                </div>
17
 
 
18
 
                <div id="container">
19
 
                        <ul id="list-1">
20
 
                                <li id="item-1"><em>Item Type One</em></li>
21
 
                                <li id="item-2"><em>Item Type Two</em></li>
22
 
                                <li id="item-3"><em>Item Type Three</em></li>
23
 
                        </ul>
24
 
                </div>
25
 
 
26
 
        <ul id="list-2">
27
 
            <li id="list-2-li-1"><em><em>Item One</em></em></li>
28
 
            <li id="list-2-li-2"><em>Item Two</em></li>
29
 
            <li id="list-2-li-3"><em>Item Three</em>
30
 
                <ul id="list-3">
31
 
                    <li id="list-3-li-1"><em>Item Three - One</em></li>
32
 
                    <li id="list-3-li-2"><em>Item Three - Two</em></li>
33
 
                    <li id="list-3-li-3"><em>Item Three - Three</em></li>
34
 
                </ul>            
35
 
            </li>        
36
 
        </ul>
37
 
        
38
 
        <div id="div-1" class="div-a">
39
 
            <div id="div-1-1" class="div-b">
40
 
              <div id="div-1-1-1" class="div-c"><em>Item One</em></div>
41
 
            </div>
42
 
            <div id="div-1-2" class="div-x">
43
 
                <em>Item Two</em>
44
 
                <div id="div-1-2-1" class="div-b">
45
 
                    <em id="em-1-2-1-1">Item Two - One</em>
46
 
                    <div id="div-1-2-1-1" class="div-c"><em>Item Two - Two</em></div>
47
 
                </div>
48
 
            </div>
49
 
        </div>        
50
 
 
51
 
        <div id="a">
52
 
            <p>Div A</p>
53
 
            <div id="b" class="remove">
54
 
                <p>Div B <em id="b2" class="remove">I'm a span!</em></p>
55
 
                <div id="c" class="remove">
56
 
                    <p>Div C</p>
57
 
                </div>
58
 
            </div>
59
 
        </div>
60
 
 
61
 
    </div>
62
 
        <script src="../../../build/yui/yui.js"></script>
63
 
        <script src="window-focus.js"></script>
64
 
        <script>
65
 
                YUI({
66
 
            filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
67
 
            allowRollup: false,
68
 
            lazyEventFacade: true,
69
 
                logExclude: { Dom: true, Selector: true, Node: true, attribute: true, base: true, event: true, widget: true}
70
 
                }).use('console', 'test', 'event', 'event-simulate', 'window-focus', function (Y) {
71
 
                        
72
 
            var suite = new Y.Test.Suite("Delegate");
73
 
 
74
 
                    // creating the console...
75
 
                        (new Y.Console()).render();
76
 
                        
77
 
            function click(id) {
78
 
                Y.Event.simulate(document.getElementById(id), 'click');
79
 
            }
80
 
 
81
 
                        // starting the testing process
82
 
                        
83
 
                        // add the test cases and suites 
84
 
                        suite.add(new Y.Test.Case({
85
 
                                
86
 
                                name: "Event Delegate Tests",
87
 
                                
88
 
                                test_simple_delegate_for_anchors: function(){
89
 
                                        
90
 
                                        var foo = false, t, ct, boundEl;
91
 
                                        
92
 
                    // the detach prefix broke when delegate from converted
93
 
                    // from a custom event to a function (the function should
94
 
                    // simply delegated to the custom event)
95
 
                                        // Y.delegate('foo|click', function(e) {
96
 
                                        Y.delegate('click', function(e) {
97
 
                                                foo = true;
98
 
                                                t = e.target;
99
 
                                                ct = e.currentTarget;
100
 
                                                boundEl = e.container;
101
 
                                        }, '#mod1', 'a');
102
 
                                        
103
 
                    click('firstlink');
104
 
 
105
 
                                        Y.Assert.isTrue(foo, "simple delegation fails, mod1 should pickup the event and test target [firstlink]");
106
 
                                        // Y.Assert.areEqual(t, Y.one('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching node");
107
 
 
108
 
                                        Y.Assert.areEqual(ct, Y.one('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
109
 
                                        Y.Assert.areEqual(t, Y.one('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
110
 
                                        Y.Assert.areEqual(boundEl, Y.one('#mod1'), "event delegate works but the container property should be the bound element");
111
 
                                        
112
 
                                },
113
 
                                
114
 
                                test_multiple_selectors: function () {
115
 
 
116
 
                                        var foo = false, t, ct, boundEl;
117
 
 
118
 
                                        Y.delegate('click', function(e) {
119
 
                                                foo = true;
120
 
                                                t = e.target;
121
 
                                                ct = e.currentTarget;
122
 
                                                boundEl = e.container;
123
 
                                        }, '#mod1', '.hd,.bd');                                 
124
 
                                        
125
 
 
126
 
                    click('mod-header');
127
 
 
128
 
                                        Y.Assert.areEqual(ct, Y.one('#mod-header'), "event delegate works but the matched element is an incorrect node, should be the matching node");
129
 
 
130
 
                    click('mod-body');
131
 
 
132
 
                                        Y.Assert.areEqual(ct, Y.one('#mod-body'), "event delegate works but the matched element is an incorrect node, should be the matching node");
133
 
                                        
134
 
                                },
135
 
 
136
 
                test_filter_function: function () {
137
 
                    var count = 0,
138
 
                        handle;
139
 
 
140
 
                    handle = Y.delegate('click', function (e) {
141
 
                        e.preventDefault();
142
 
                        count++;
143
 
                    }, '#mod1', function (target, e) {
144
 
                        return target.get('tagName').toLowerCase() === 'a';
145
 
                    });
146
 
 
147
 
                    click('spanwithinlink');
148
 
 
149
 
                                        Y.Assert.areSame(1, count);
150
 
 
151
 
                    handle.detach();
152
 
                },
153
 
 
154
 
                test_filter_function_for_focus: function () {
155
 
                    var count = 0,
156
 
                        handle;
157
 
 
158
 
                    if (Y.isWindowInFocus()) {
159
 
                        handle = Y.delegate('focus', function (e) {
160
 
                            count++;
161
 
                        }, '#mod1', function (target, e) {
162
 
                            return target.get('tagName').toLowerCase() === 'a';
163
 
                        });
164
 
 
165
 
                        Y.one('#spanwithinlink').focus();
166
 
                        Y.one('#fakeimage').focus();
167
 
                        Y.one('#secondlink').focus();
168
 
 
169
 
                        Y.Assert.areSame(1, count);
170
 
 
171
 
                        handle.detach();
172
 
                    } else {
173
 
                        Y.log("Window is not focused.  Can't properly test.",
174
 
                            "warn", "TestRunner");
175
 
                    }
176
 
                },
177
 
 
178
 
                                test_document_as_container: function () {
179
 
 
180
 
                                        var foo = false, t, ct, boundEl;
181
 
                                        
182
 
                                        Y.delegate('click', function(e) {
183
 
                                                foo = true;
184
 
                                                t = e.target;
185
 
                                                ct = e.currentTarget;
186
 
                                                boundEl = e.container;
187
 
                                        }, Y.one('#mod1').get('ownerDocument'), 'a');
188
 
                                        
189
 
                    click('firstlink');
190
 
 
191
 
                                        Y.Assert.isTrue(foo, "simple delegation fails, mod1 should pickup the event and test target [firstlink]");
192
 
 
193
 
                                        Y.Assert.areEqual(ct, Y.one('#firstlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
194
 
                                        Y.Assert.areEqual(t, Y.one('#firstlink'), "event delegate works but the target is an incorrect node, should be the actual click target");
195
 
                                        Y.Assert.areEqual(boundEl, Y.one('#mod1').get('ownerDocument'), "event delegate works but the container property should be the bound element");
196
 
                                        
197
 
                                },                      
198
 
                                
199
 
                                test_checking_delegation_target: function(){
200
 
                                        
201
 
                                        var foo = false, t, ct, boundEl;
202
 
                                        
203
 
                                        Y.delegate('click', function(e) {
204
 
                                                foo = true;
205
 
                                                t = e.target;
206
 
                                                ct = e.currentTarget;
207
 
                                                boundEl = e.container;
208
 
                                        }, '#mod1', 'a');
209
 
                                        
210
 
                    click('fakeimage');
211
 
 
212
 
                                        Y.Assert.isTrue(foo, "delegation fails for an image within an anchor, mod1 should pickup the event and test target [secondlink]");
213
 
                                        // in this case, the target should be the anchor, and not the image
214
 
                                        // Y.Assert.areEqual(t, Y.one('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching node");
215
 
 
216
 
                    // this has been changed.  the target is unchanged, the anchor is
217
 
 
218
 
                                        Y.Assert.areEqual(ct, Y.one('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching node");
219
 
                                        Y.Assert.areEqual(t, Y.one('#fakeimage'), "event delegate works but the target is an incorrect node, should be the actual click target");
220
 
                                        Y.Assert.areEqual(boundEl, Y.one('#mod1'), "event delegate works but the container property should be the bound element");
221
 
                                        
222
 
                                },
223
 
                                
224
 
                                test_including_container_in_selector: function(){
225
 
                                        
226
 
                                        var foo = false, t;
227
 
                                        
228
 
                                        Y.delegate('click', function(e) {
229
 
                                                foo = true;
230
 
                                                t = e.target;
231
 
                                        }, '#mod1', '#mod1 a');
232
 
                                        
233
 
                    click('firstlink');
234
 
 
235
 
                                        Y.Assert.isFalse(foo, "delegation fails, the container (specified in the on) can not be part of the selectors");
236
 
                                        
237
 
                                },
238
 
                                
239
 
                                test_targeting_container_without_selectors: function(){
240
 
                                        
241
 
                                        var foo = false, t;
242
 
                                        
243
 
                                        Y.delegate('click', function(e) {
244
 
                                                foo = true;
245
 
                                                t = e.target;
246
 
                                        }, '#mod1');
247
 
                                        
248
 
                    click('firstlink');
249
 
 
250
 
                                        Y.Assert.isFalse(foo, "delegation fails, delegation without at least one selector should never trigger an event");
251
 
                                },
252
 
 
253
 
                                test_multiple_selectors_one_match: function(){
254
 
                                        
255
 
                                        var foo = false, t;
256
 
                                        
257
 
                                        Y.delegate('click', function(e) {
258
 
                                                foo = true;
259
 
                                                t = e.target;
260
 
                                        }, '#mod1', 'a,a span');
261
 
                                        
262
 
                    click('firstlink');
263
 
 
264
 
                                        Y.Assert.isTrue(foo, "multiple selectors fails, delegate should be able to match different selectors");
265
 
                                        Y.Assert.areEqual(t, Y.one('#firstlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
266
 
                                        
267
 
                                },
268
 
                                
269
 
                                test_multiple_delegate_matches: function(){
270
 
                                        
271
 
                                        var foo1 = false, foo2 = false, t1, t2, ct1, ct2;
272
 
                                        
273
 
                                        Y.delegate('click', function(e) {
274
 
                                                foo1 = true;
275
 
                                                t1 = e.target;
276
 
                                                ct1 = e.currentTarget;
277
 
                                        }, '#mod1', 'a');
278
 
                                        
279
 
                                        Y.delegate('click', function(e) {
280
 
                                                foo2 = true;
281
 
                                                t2 = e.target;
282
 
                                                ct2 = e.currentTarget;
283
 
                                        }, '#mod1', 'a span');
284
 
                                        
285
 
                    click('spanwithinlink');
286
 
 
287
 
                                        Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
288
 
                                        Y.Assert.isTrue(foo2, "second match fail, delegate should be able to match [a span]");
289
 
                                        Y.Assert.areEqual(ct1, Y.one('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching selector");
290
 
                                        Y.Assert.areEqual(t1, Y.one('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the clicked node");
291
 
                                        Y.Assert.areEqual(ct2, Y.one('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
292
 
                                        Y.Assert.areEqual(t2, Y.one('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the clicked");
293
 
                                        
294
 
                                },
295
 
                                
296
 
                                test_bubble_up_after_delegate: function(){
297
 
                                        
298
 
                                        var foo1 = false,
299
 
                        foo2 = false,
300
 
                        t1, t2, ct1, ct2, container1, container2, this1, this2;
301
 
                                        
302
 
                                        Y.delegate('click', function(e) {
303
 
                                                foo1 = true;
304
 
                                                t1   = e.target;
305
 
                                                ct1  = e.currentTarget;
306
 
                        container1 = e.container;
307
 
                        this1 = this;
308
 
                                        }, '#mod1', 'a');
309
 
                                        
310
 
                                        Y.on('click', function(e) {
311
 
                                                foo2 = true;
312
 
                                                t2   = e.target;
313
 
                        ct2  = e.currentTarget;
314
 
                        container2 = e.container;
315
 
                        this2 = this;
316
 
                                        }, '#doc');
317
 
                                        
318
 
                    click('spanwithinlink');
319
 
 
320
 
                                        Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
321
 
                                        Y.Assert.isTrue(foo2, "second match fail, the event doesn't bubble up after the delegate routine");
322
 
                    // changed
323
 
                                        // Y.Assert.areEqual(t1, Y.one('#secondlink'), "event delegate works but the target is an incorrect node, should be the matching selector");
324
 
                                        Y.Assert.areEqual(ct1, Y.one('#secondlink'), "event delegate works but the currentTarget is an incorrect node, should be the matching selector");
325
 
                                        Y.Assert.areEqual(t1, Y.one('#spanwithinlink'), "event delegate works but the target is an incorrect node, should be the actual target");
326
 
 
327
 
                    // obsolete
328
 
                                        Y.Assert.areEqual(t2, Y.one('#spanwithinlink'), "event delegate works but it doesn't restore e.target to the original value.");
329
 
 
330
 
                    Y.Assert.areSame(ct2, Y.one('#doc'));
331
 
                    Y.Assert.areSame(container1, Y.one('#mod1'));
332
 
                    Y.Assert.isUndefined(container2);
333
 
                    Y.Assert.areSame(this1, Y.one('#secondlink'));
334
 
                    Y.Assert.areSame(this2, Y.one('#doc'));
335
 
                                },
336
 
                                
337
 
                                test_bubble_up_after_delegate_halt: function(){
338
 
                                        
339
 
                                        var foo1 = false, foo2 = false;
340
 
                                        
341
 
                                        Y.delegate('click', function(e) {
342
 
                                                foo1 = true;
343
 
                                                e.halt();
344
 
                                        }, '#mod1', 'a');
345
 
                                        
346
 
                                        Y.on('click', function(e) {
347
 
                                                foo2 = true;
348
 
                                        }, '#doc');
349
 
                                        
350
 
                    click('spanwithinlink');
351
 
 
352
 
                                        Y.Assert.isTrue(foo1, "first match fail, delegate should be able to match [a]");
353
 
                                        Y.Assert.isFalse(foo2, "the listener for 'doc' got executed, which means that e.halt fails during the delegate routine");
354
 
                                },
355
 
                                
356
 
                                test_direct_descendant_combinator: function () {
357
 
 
358
 
                    var matchID = null;
359
 
                                    
360
 
                    Y.one("#list-2").delegate("click", function (e) {
361
 
                        matchID = this.get("id");
362
 
                    }, ">li");                              
363
 
 
364
 
                    click('list-3-li-1');
365
 
 
366
 
                    Y.Assert.areEqual('list-2-li-3', matchID, "The currentTarget is an incorrect node, should be the matching node.");
367
 
                                    
368
 
                                    
369
 
                                    matchID = null;
370
 
 
371
 
                    Y.one("#div-1").delegate("click", function (e) {
372
 
                        matchID = this.get("id");
373
 
                    }, ">.div-b");                                  
374
 
 
375
 
                    click('div-1-1-1');
376
 
 
377
 
                    Y.Assert.areEqual('div-1-1', matchID, "The currentTarget is an incorrect node, should be the matching node.");
378
 
 
379
 
 
380
 
                                    matchID = null;
381
 
                                    
382
 
                    click('div-1-2-1');
383
 
 
384
 
                    Y.Assert.isNull(matchID, "The currentTarget is an incorrect node, should be the matching node.");
385
 
                                    
386
 
                                },
387
 
                
388
 
                test_multiple_matches_in_subtree: function () {
389
 
                    var ids = [];
390
 
 
391
 
                    Y.delegate('click', function (e) {
392
 
                        ids.push(this.get('id'));
393
 
                    }, '#div-1', 'div');
394
 
 
395
 
                                        Y.Event.simulate(Y.one('#div-1-2-1-1 em')._node, 'click');
396
 
 
397
 
                    Y.Assert.areSame(4, ids.length, "Delegate handler should have fired three times, once for each div in #div-1");
398
 
                    Y.Assert.areSame('div-1-2-1-1', ids[0], "First delegate callback should be from the deepest div in the subtree");
399
 
                    Y.Assert.areSame('div-1-2-1', ids[1]);
400
 
                    Y.Assert.areSame('div-1-2', ids[2]);
401
 
                    Y.Assert.areSame('div-1', ids[3]);
402
 
                },
403
 
 
404
 
                test_removing_parent_in_subscriber: function () {
405
 
                    var count = 0;
406
 
 
407
 
                    Y.one('#a').delegate('click', function (e) {
408
 
                        this.get('parentNode').remove();
409
 
                        count++;
410
 
                    }, '.remove');
411
 
 
412
 
                    click('b2');
413
 
 
414
 
                    Y.Assert.areSame(2, count);
415
 
                },
416
 
 
417
 
                                test_successful_purge: function () {
418
 
                                
419
 
                                        var bHandler1Called = false,
420
 
                                                bHandler2Called = false;
421
 
                                
422
 
                                        Y.delegate('click', function(e) {
423
 
                                                bHandler1Called = true;
424
 
                                        }, '#mod1', 'a');
425
 
 
426
 
                                        Y.Event.purgeElement('#mod1');
427
 
 
428
 
                                        //      Test #1: Make sure first handler is not called after 
429
 
                                        //      calling Y.Event.purgeElement
430
 
 
431
 
                    click('firstlink');
432
 
 
433
 
                                        Y.Assert.isFalse(bHandler1Called, "No delegated listeners should be called after a call to Y.Event.purgeElement");
434
 
 
435
 
 
436
 
                                        //      Test #2: Make sure second handler is called after 
437
 
                                        //      calling Y.Event.purgeElement
438
 
 
439
 
                                        Y.delegate('click', function(e) {
440
 
                                                bHandler2Called = true;
441
 
                                        }, '#mod1', 'a');
442
 
 
443
 
                    click('firstlink');
444
 
 
445
 
                                        Y.Assert.isFalse(bHandler1Called, "The first listener should not be be called.");
446
 
                                        Y.Assert.isTrue(bHandler2Called, "The second delegated listener should be called.");
447
 
 
448
 
                                        bHandler1Called = false;
449
 
                                        bHandler2Called = false;                                        
450
 
                                        
451
 
                                        //      Test #3: Make sure first handler is not called after 
452
 
                                        //      calling Y.Event.purgeElement and passing in a type
453
 
                                        Y.Event.purgeElement('#mod1', false, 'click');
454
 
 
455
 
                    click('firstlink');
456
 
 
457
 
                                        Y.Assert.isFalse(bHandler2Called, "The second listener should not be be called.");
458
 
 
459
 
                                        //      Test #4: Make sure that it is possible to delegate 
460
 
                                        //      a listener to a new element with the same id of 
461
 
                                        //      an element that used to exist in the DOM.
462
 
 
463
 
                                        bHandler1Called = false;
464
 
                                        bHandler2Called = false;                                        
465
 
 
466
 
                                        Y.delegate("click", function () {
467
 
                                                bHandler1Called = true;
468
 
                                        }, "#list-1", ">li");
469
 
 
470
 
                                        Y.Event.purgeElement('#list-1');
471
 
 
472
 
                                        Y.one("#container").setContent('<ul id="list-1"><li id="item-1"><em>Item Type One</em></li><li id="item-2"><em>Item Type Two</em></li><li id="item-3"><em>Item Type Three</em></li></ul>');
473
 
 
474
 
                                        Y.delegate("click", function () {
475
 
                                                bHandler2Called = true;
476
 
                                        }, "#list-1", ">li");
477
 
 
478
 
                    click('item-1');
479
 
 
480
 
                                        Y.Assert.isFalse(bHandler1Called, "The first listener should not be be called.");
481
 
                                        Y.Assert.isTrue(bHandler2Called, "The second delegated listener should be called.");
482
 
                                        
483
 
                                },
484
 
                                
485
 
                                test_successful_detach: function () {                           
486
 
                                
487
 
                                        var bHandler1Called = false,
488
 
                                                bHandler2Called = false;
489
 
 
490
 
                                        var handle = Y.delegate('click', function(e) {
491
 
                                                bHandler1Called = true;
492
 
                                        }, '#mod1', 'a');
493
 
 
494
 
                                        handle.detach();
495
 
 
496
 
                                        //      Test #1: Make sure first handler is not called after 
497
 
                                        //      being detached
498
 
 
499
 
                    click('firstlink');
500
 
 
501
 
                                        Y.Assert.isFalse(bHandler1Called, "Listener should not be called after being detached.");
502
 
 
503
 
 
504
 
                                        //      Test #2: Make sure second handler is called after 
505
 
                                        //      detaching the first.
506
 
 
507
 
                                        Y.delegate('click', function(e) {
508
 
                                                bHandler2Called = true;
509
 
                                        }, '#mod1', 'a');
510
 
 
511
 
                    click('firstlink');
512
 
 
513
 
                                        Y.Assert.isFalse(bHandler1Called, "The first listener should not be be called.");
514
 
                                        Y.Assert.isTrue(bHandler2Called, "The second delegated listener should be called.");
515
 
 
516
 
 
517
 
                                        //      Test #3: Make sure that it is possible to delegate 
518
 
                                        //      a listener to a new element with the same id of 
519
 
                                        //      an element that used to exist in the DOM.
520
 
 
521
 
                                        bHandler1Called = false;
522
 
                                        bHandler2Called = false;                                        
523
 
 
524
 
                                        handle = Y.delegate("click", function () {
525
 
                                                bHandler1Called = true;
526
 
                                        }, "#list-1", ">li");
527
 
 
528
 
                                        handle.detach();
529
 
 
530
 
                                        Y.one("#container").setContent('<ul id="list-1"><li id="item-1"><em>Item Type One</em></li><li id="item-2"><em>Item Type Two</em></li><li id="item-3"><em>Item Type Three</em></li></ul>');
531
 
 
532
 
                                        Y.delegate("click", function () {
533
 
                                                bHandler2Called = true;
534
 
                                        }, "#list-1", ">li");
535
 
 
536
 
                    click('item-1');
537
 
 
538
 
                                        Y.Assert.isFalse(bHandler1Called, "The first listener should not be be called.");
539
 
                                        Y.Assert.isTrue(bHandler2Called, "The second delegated listener should be called.");
540
 
                                        
541
 
                                },
542
 
 
543
 
                "Y.delegate('cat|type') should subscribe event": function () {
544
 
                    var count = 0,
545
 
                        cat = Y.guid(),
546
 
                        handle;
547
 
 
548
 
                    handle = Y.delegate(cat + "|click", function () {
549
 
                        count++;
550
 
                    }, '#container', 'li');
551
 
 
552
 
                    click('item-1');
553
 
 
554
 
                    Y.Assert.areSame(1, count);
555
 
 
556
 
                    handle.detach();
557
 
 
558
 
                    click('item-1');
559
 
 
560
 
                    Y.Assert.areSame(1, count, "category sub did not detach with handle");
561
 
                },
562
 
 
563
 
                "node.delegate('cat|type') should subscribe event": function () {
564
 
                    var count = 0,
565
 
                        cat = Y.guid(),
566
 
                        handle;
567
 
 
568
 
                    handle = Y.one('#container').delegate(cat + "|click", function () {
569
 
                        count++;
570
 
                    }, 'li');
571
 
 
572
 
                    click('item-1');
573
 
 
574
 
                    Y.Assert.areSame(1, count);
575
 
 
576
 
                    handle.detach();
577
 
 
578
 
                    click('item-1');
579
 
 
580
 
                    Y.Assert.areSame(1, count, "category sub did not detach with handle");
581
 
                },
582
 
 
583
 
                "node.detach('cat|type', fn) should detach delegate": function () {
584
 
                    var count = 0,
585
 
                        target = Y.one("#container"),
586
 
                        cat = Y.guid(),
587
 
                        handle;
588
 
 
589
 
                    function increment() {
590
 
                        count++;
591
 
                    }
592
 
 
593
 
                    handle = target.delegate(cat + "|click", increment, 'li');
594
 
 
595
 
                    click('item-1');
596
 
 
597
 
                    Y.Assert.areSame(1, count);
598
 
 
599
 
                    target.detach(cat + '|click', increment);
600
 
 
601
 
                    click('item-1');
602
 
 
603
 
                    Y.Assert.areSame(1, count);
604
 
                },
605
 
 
606
 
                "node.detach('cat|type') should detach delegate": function () {
607
 
                    var count = 0,
608
 
                        target = Y.one("#container"),
609
 
                        cat = Y.guid(),
610
 
                        handle;
611
 
 
612
 
                    function increment() {
613
 
                        count++;
614
 
                    }
615
 
 
616
 
                    handle = target.delegate(cat + "|click", increment, 'li');
617
 
 
618
 
                    click('item-1');
619
 
 
620
 
                    Y.Assert.areSame(1, count);
621
 
 
622
 
                    target.detach(cat + '|click');
623
 
 
624
 
                    click('item-1');
625
 
 
626
 
                    Y.Assert.areSame(1, count);
627
 
                },
628
 
 
629
 
                "node.detach('cat|*') should detach delegate": function () {
630
 
                    var count = 0,
631
 
                        target = Y.one("#container"),
632
 
                        cat = Y.guid(),
633
 
                        handle;
634
 
 
635
 
                    function increment() {
636
 
                        count++;
637
 
                    }
638
 
 
639
 
                    handle = target.delegate(cat + "|click", increment, 'li');
640
 
 
641
 
                    click('item-1');
642
 
 
643
 
                    Y.Assert.areSame(1, count);
644
 
 
645
 
                    target.detach(cat + '|*');
646
 
 
647
 
                    click('item-1');
648
 
 
649
 
                    Y.Assert.areSame(1, count);
650
 
                },
651
 
 
652
 
                testPassingObjectForMultipleSubscriptions: function () {
653
 
                    var clickOk = false,
654
 
                        mousedownOk = false;
655
 
 
656
 
                    function clicked() {
657
 
                        clickOk = true;
658
 
                    }
659
 
                    function mousedowned() {
660
 
                        mousedownOk = true;
661
 
                    }
662
 
 
663
 
                    Y.one('#div-1').delegate({
664
 
                        click: clicked,
665
 
                        mousedown: mousedowned
666
 
                    }, 'em');
667
 
 
668
 
                    Y.Event.simulate(document.getElementById('em-1-2-1-1'), 'mousedown');
669
 
                    click('em-1-2-1-1');
670
 
 
671
 
                    Y.Assert.isTrue(clickOk);
672
 
                    Y.Assert.isTrue(mousedownOk);
673
 
                },
674
 
 
675
 
                testThisObjOverrideWithObjectSubscription: function () {
676
 
                    var thisObj;
677
 
 
678
 
                    Y.one('#div-1').delegate({
679
 
                        click: function () {
680
 
                            thisObj = this;
681
 
                        }
682
 
                    }, 'em', { foo: true });
683
 
 
684
 
                    click('em-1-2-1-1');
685
 
 
686
 
                    Y.Assert.isObject(thisObj);
687
 
                    Y.Assert.isTrue(thisObj.foo);
688
 
                },
689
 
 
690
 
                testPassingExtraArgsWithObjectSubscription: function () {
691
 
                    var arg;
692
 
 
693
 
                    Y.one('#div-1').delegate({
694
 
                        click: function (e, extraArg) {
695
 
                            arg = extraArg;
696
 
                        }
697
 
                    }, 'em', null, "extra arg");
698
 
 
699
 
                    click('em-1-2-1-1');
700
 
 
701
 
                    Y.Assert.areSame("extra arg", arg);
702
 
                },
703
 
 
704
 
                testDetachingObjectSubscriptionViaHandle: function () {
705
 
                    var count = 0,
706
 
                        handle;
707
 
 
708
 
                    handle = Y.one('#div-1').delegate({
709
 
                        click: function (e, extraArg) {
710
 
                            count++;
711
 
                        }
712
 
                    }, 'em');
713
 
 
714
 
                    click('em-1-2-1-1');
715
 
 
716
 
                    Y.Assert.areSame(1, count);
717
 
 
718
 
                    handle.detach();
719
 
 
720
 
                    click('em-1-2-1-1');
721
 
 
722
 
                    Y.Assert.areSame(1, count);
723
 
                },
724
 
 
725
 
                testPassingArrayForMultipleSubscriptions: function () {
726
 
                    var count = 0;
727
 
 
728
 
                    Y.one('#div-1').delegate(['click', 'mousedown'], function () {
729
 
                        count++;
730
 
                    }, 'em');
731
 
 
732
 
                    Y.Event.simulate(document.getElementById('em-1-2-1-1'), 'mousedown');
733
 
                    click('em-1-2-1-1');
734
 
 
735
 
                    Y.Assert.areSame(2, count);
736
 
                }
737
 
                                
738
 
                                
739
 
                                /* 
740
 
                                 * Other things that I consider should be tested in the future:
741
 
                                 * - stopping the event, verifying the event ourside of the container
742
 
                                 * - stopping the event and verify what happen with multiple matches
743
 
                                 */
744
 
                                
745
 
                        })); 
746
 
                        
747
 
            suite.add(new Y.Test.Case({
748
 
                name: "Bugs",
749
 
 
750
 
                // Existing unfixed bugs
751
 
                _should: {
752
 
                    fail: {
753
 
                        //test_filter_function_for_focus_from_body: 2529142,
754
 
                        //"stopPropagation should stop multiple matches": 2529388
755
 
                    }
756
 
                },
757
 
 
758
 
                test_filter_function_for_focus_from_body: function () {
759
 
                    var count = 0,
760
 
                        handle;
761
 
 
762
 
                    if (Y.isWindowInFocus()) {
763
 
                        handle = Y.one('body').delegate('focus', function (e) {
764
 
                            e.preventDefault();
765
 
                            count++;
766
 
                        }, '#firstlink');
767
 
 
768
 
                        Y.one('#firstlink').focus();
769
 
 
770
 
                        Y.Assert.areSame(1, count);
771
 
                    } else {
772
 
                        Y.log("Window is not focused.  Can't properly test.",
773
 
                            "warn", "TestRunner");
774
 
                    }
775
 
                },
776
 
 
777
 
                "stopPropagation should stop multiple matches": function () {
778
 
                    var count = 0,
779
 
                        stop = false,
780
 
                        halt = false,
781
 
                        handle;
782
 
 
783
 
                    handle = Y.delegate('click', function (e) {
784
 
                        count++;
785
 
                        if (halt) {
786
 
                            e.halt();
787
 
                        } else if (stop) {
788
 
                            e.stopPropagation();
789
 
                        }
790
 
                    }, '#doc', 'ul');
791
 
 
792
 
                    click('list-3-li-2');
793
 
 
794
 
                    Y.Assert.isTrue((count > 1));
795
 
 
796
 
                    count = 0;
797
 
                    stop = true;
798
 
 
799
 
                    click('list-3-li-2');
800
 
 
801
 
                    Y.Assert.areSame(1, count);
802
 
 
803
 
                    count = 0;
804
 
                    halt = true;
805
 
 
806
 
                    click('list-3-li-2');
807
 
 
808
 
                    Y.Assert.areSame(1, count);
809
 
                }
810
 
            }));
811
 
 
812
 
                        //run all tests 
813
 
            Y.Test.Runner.setName("Delegate");
814
 
            Y.Test.Runner.add(suite);
815
 
                        Y.Test.Runner.run();
816
 
                });
817
 
        </script>
818
 
</body>
819
 
</html>