~bcsaller/juju-gui/charmFind

« back to all changes in this revision

Viewing changes to lib/yui/tests/pluginhost/tests/plugins.html

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

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>PluginHost Tests</title>
5
 
 
6
 
</head>
7
 
<body class="yui3-skin-sam">
8
 
 
9
 
    <script type="text/javascript" src="../../../build/yui/yui.js"></script>
10
 
 
11
 
    <style type="text/css">
12
 
        #console .yui3-console-entry {
13
 
            padding:2px;
14
 
            margin:0px;
15
 
            min-height:0;
16
 
        }
17
 
 
18
 
        #console .yui3-console-entry-fail .yui3-console-entry-cat {
19
 
            background-color:red;
20
 
        }
21
 
 
22
 
        #console .yui3-console-entry-pass .yui3-console-entry-cat {
23
 
            background-color:green;
24
 
        }
25
 
 
26
 
        #console .yui3-console-entry-perf .yui3-console-entry-cat {
27
 
            background-color:blue;
28
 
        }
29
 
 
30
 
        #console {
31
 
            position:static;
32
 
        }
33
 
 
34
 
        html, body {
35
 
            height:100%;
36
 
        }
37
 
    </style>
38
 
 
39
 
    <script type="text/javascript">
40
 
        YUI({
41
 
            useBrowserConsole:false,
42
 
            filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min'
43
 
        }).use("basebase", "base-pluginhost", "base-build", "console", "plugin", "test", function(Y) {
44
 
 
45
 
            function PluginOne() {
46
 
                PluginOne.superclass.constructor.apply(this, arguments);
47
 
            }
48
 
            PluginOne.NS = "one";
49
 
            PluginOne.NAME = "pluginOne";
50
 
            PluginOne.ATTRS = {
51
 
                a: {
52
 
                    value:1
53
 
                }
54
 
            };
55
 
 
56
 
            Y.extend(PluginOne, Y.Plugin.Base, {
57
 
                initializer: function() {
58
 
                },
59
 
                method : function() {
60
 
                }
61
 
            });
62
 
 
63
 
            // ---
64
 
 
65
 
            function PluginTwo() {
66
 
                PluginTwo.superclass.constructor.apply(this, arguments);
67
 
            }
68
 
            PluginTwo.NS = "two";
69
 
            PluginTwo.NAME = "pluginTwo";
70
 
            PluginTwo.ATTRS = {
71
 
                b: {
72
 
                    value:2
73
 
                }
74
 
            };
75
 
            Y.extend(PluginTwo, Y.Plugin.Base, {
76
 
                initializer: function() {
77
 
                },
78
 
                method : function() {
79
 
                }
80
 
            });
81
 
 
82
 
            // ---
83
 
 
84
 
            var PluginThree = Y.Base.create("pluginThree", Y.Plugin.Base, [], {
85
 
                initializer: function() {
86
 
                },
87
 
                method : function() {
88
 
                }
89
 
            }, {
90
 
                NS : "three",
91
 
                ATTRS : {
92
 
                    c: {
93
 
                        value:3
94
 
                    }
95
 
                }
96
 
            });
97
 
 
98
 
            // ---
99
 
 
100
 
            function PluginFour() {
101
 
                PluginFour.superclass.constructor.apply(this, arguments);
102
 
            }
103
 
            PluginFour.NS = "four";
104
 
            PluginFour.NAME = "pluginFour";
105
 
            PluginFour.ATTRS = {
106
 
                d: {
107
 
                    value:4
108
 
                }
109
 
            };
110
 
 
111
 
            Y.extend(PluginFour, Y.Plugin.Base, {
112
 
                initializer: function(cfg) {
113
 
                    this.doBefore("methodA", this._methodA, this);
114
 
                    this.doAfter("methodB", this._methodB, this);
115
 
                    this.doBefore("mainAChange", this._onAChange);
116
 
                    this.doAfter("mainBChange", this._afterBChange);
117
 
                },
118
 
 
119
 
                _methodA : function(stack) {
120
 
                    stack.push("PluginFour.methodA");
121
 
                },
122
 
 
123
 
                _methodB : function(stack) {
124
 
                    stack.push("PluginFour.methodB");
125
 
                },
126
 
 
127
 
                _onAChange : function(e) {
128
 
                    if (e.stack) {
129
 
                        e.stack.push("PluginFour._onAChange");
130
 
                    }
131
 
                },
132
 
 
133
 
                _afterBChange : function(e) {
134
 
                    if (e.stack) {
135
 
                        e.stack.push("PluginFour._afterBChange");
136
 
                    }
137
 
                }
138
 
            });
139
 
 
140
 
            function Host(config) {
141
 
                Host.superclass.constructor.apply(this, arguments);
142
 
            }
143
 
 
144
 
            Host.NAME = "host";
145
 
            Host.ATTRS = {
146
 
                mainA : {
147
 
                    value: "mainA"
148
 
                },
149
 
                mainB : {
150
 
                    value: "mainB"
151
 
                }
152
 
            };
153
 
 
154
 
            Y.extend(Host, Y.Base, {
155
 
                methodA : function(stack) {
156
 
                    if (stack) {
157
 
                        stack.push("Host.methodA");
158
 
                    }
159
 
                },
160
 
 
161
 
                methodB : function(stack) {
162
 
                    if (stack) {
163
 
                        stack.push("Host.methodB");
164
 
                    }
165
 
                },
166
 
 
167
 
                initializer : function() {
168
 
                }
169
 
            });
170
 
 
171
 
            function ExtendedHost() {
172
 
                ExtendedHost.superclass.constructor.apply(this, arguments);
173
 
            }
174
 
 
175
 
            Y.extend(ExtendedHost, Host, {
176
 
                methodC : function() {},
177
 
                initializer : function() {}
178
 
            });
179
 
 
180
 
            var basicTemplate = {
181
 
 
182
 
                name: "Basic Tests",
183
 
 
184
 
                testPlugDuringConstruction : function() {
185
 
                    var h1 = new Host({
186
 
                        plugins:[PluginOne, {fn:PluginTwo, cfg:{two:4}}, {fn:PluginThree, cfg:{three:6}}]
187
 
                    });
188
 
 
189
 
                    Y.ObjectAssert.ownsKey("one", h1);
190
 
                    Y.Assert.isInstanceOf(PluginOne, h1.one);
191
 
                    Y.ObjectAssert.ownsKey("two", h1);
192
 
                    Y.Assert.isInstanceOf(PluginTwo, h1.two);
193
 
                    Y.ObjectAssert.ownsKey("three", h1);
194
 
                    Y.Assert.isInstanceOf(PluginThree, h1.three);
195
 
 
196
 
                    var h2 = new Host({
197
 
                        plugins:[PluginOne, PluginTwo]
198
 
                    });
199
 
 
200
 
                    Y.ObjectAssert.ownsKey("one", h2);
201
 
                    Y.Assert.isInstanceOf(PluginOne, h2.one);
202
 
                    Y.ObjectAssert.ownsKey("two", h2);
203
 
                    Y.Assert.isInstanceOf(PluginTwo, h2.two);
204
 
                    Y.Assert.isUndefined(h2.three);
205
 
                },
206
 
 
207
 
                testPlugUsingPlug : function() {
208
 
                    var h1 = new Host();
209
 
 
210
 
                    h1.plug(PluginOne);
211
 
                    h1.plug(PluginTwo, {two:4});
212
 
                    h1.plug({fn:PluginThree, cfg:{three:6}});
213
 
 
214
 
                    Y.ObjectAssert.ownsKey("one", h1);
215
 
                    Y.Assert.isInstanceOf(PluginOne, h1.one);
216
 
 
217
 
                    Y.ObjectAssert.ownsKey("two", h1);
218
 
                    Y.Assert.isInstanceOf(PluginTwo, h1.two);
219
 
 
220
 
                    Y.ObjectAssert.ownsKey("three", h1);
221
 
                    Y.Assert.isInstanceOf(PluginThree, h1.three);
222
 
 
223
 
                    var h2 = new Host();
224
 
                    h2.plug([PluginOne, {fn:PluginTwo, cfg:{two:8}}]);
225
 
 
226
 
                    Y.ObjectAssert.ownsKey("one", h2);
227
 
                    Y.Assert.isInstanceOf(PluginOne, h2.one);
228
 
 
229
 
                    Y.ObjectAssert.ownsKey("two", h2);
230
 
                    Y.Assert.isInstanceOf(PluginTwo, h2.two);
231
 
 
232
 
                    Y.Assert.isUndefined(h2.three);
233
 
                },
234
 
 
235
 
                testUnplug : function() {
236
 
 
237
 
                    var h1 = new Host({
238
 
                        plugins:[PluginOne, PluginTwo]
239
 
                    });
240
 
 
241
 
                    Y.ObjectAssert.ownsKey("one", h1);
242
 
                    Y.Assert.isInstanceOf(PluginOne, h1.one);
243
 
 
244
 
                    Y.ObjectAssert.ownsKey("two", h1);
245
 
                    Y.Assert.isInstanceOf(PluginTwo, h1.two);
246
 
 
247
 
                    h1.unplug("one");
248
 
 
249
 
                    Y.Assert.isUndefined(h1.one);
250
 
 
251
 
                    Y.ObjectAssert.ownsKey("two", h1);
252
 
                    Y.Assert.isInstanceOf(PluginTwo, h1.two);
253
 
 
254
 
                    h1.unplug(PluginTwo);
255
 
 
256
 
                    Y.Assert.isUndefined(h1.one);
257
 
                    Y.Assert.isUndefined(h1.two);
258
 
                },
259
 
                
260
 
                testUnplugNonExistant : function() {
261
 
                    var h1 = new Host();
262
 
 
263
 
                    var NonExistant = function() {}
264
 
                    NonExistant.NS = "foo";
265
 
                    
266
 
                    var SharedNS = function() {}
267
 
                    SharedNS.NS = "one";
268
 
 
269
 
                    try {
270
 
                        h1.unplug("foo");
271
 
                        h1.unplug(NonExistant);
272
 
                    } catch (e) {
273
 
                        Y.Assert.fail("Error unplugging a non-plugged in plugin");
274
 
                    }
275
 
 
276
 
                    h1.plug(PluginOne);
277
 
                    
278
 
                    try {
279
 
                        // Unplugging a diff plugin with the same NS
280
 
                        h1.unplug(SharedNS);                        
281
 
                    } catch (e) {
282
 
                        Y.Assert.fail("Error unplugging a plugin which shares a NS with a plugged in plugin");
283
 
                    }
284
 
 
285
 
                    Y.Assert.isTrue(h1.one instanceof PluginOne);
286
 
                },
287
 
 
288
 
                testUnplugPlug : function() {
289
 
 
290
 
                    var h1 = new Host({
291
 
                        plugins:[PluginOne, PluginTwo]
292
 
                    });
293
 
 
294
 
                    Y.ObjectAssert.ownsKey("one", h1);
295
 
                    Y.Assert.isInstanceOf(PluginOne, h1.one);
296
 
 
297
 
                    Y.ObjectAssert.ownsKey("two", h1);
298
 
                    Y.Assert.isInstanceOf(PluginTwo, h1.two);
299
 
 
300
 
                    h1.unplug(PluginOne);
301
 
 
302
 
                    Y.Assert.isUndefined(h1.one);
303
 
 
304
 
                    h1.plug(PluginOne);
305
 
 
306
 
                    Y.ObjectAssert.ownsKey("one", h1);
307
 
                    Y.Assert.isInstanceOf(PluginOne, h1.one);
308
 
                },
309
 
 
310
 
                testPluginHost : function() {
311
 
                    var h1 = new Host();
312
 
                    h1.plug(PluginOne);
313
 
 
314
 
                    Y.Assert.areSame(h1, h1.one.get("host"));
315
 
                },
316
 
 
317
 
                testPlugFormatsDefault : function() {
318
 
                    var h1 = new Host();
319
 
                    h1.plug(PluginOne);
320
 
                    Y.Assert.areEqual(1, h1.one.get("a"));
321
 
                },
322
 
 
323
 
                testPlugFormatsCustom : function() {
324
 
                    var h1 = new Host();
325
 
                    h1.plug(PluginOne, {a:10});
326
 
                    Y.Assert.areEqual(10, h1.one.get("a"));
327
 
                },
328
 
 
329
 
                testPlugFormatsCustomLiteral : function() {
330
 
                    var h1 = new Host();
331
 
                    h1.plug({fn:PluginOne, cfg:{a:10}});
332
 
                    Y.Assert.areEqual(10, h1.one.get("a"));
333
 
                },
334
 
 
335
 
                testPlugFormatsArrayDefault : function() {
336
 
                    var h1 = new Host();
337
 
                    h1.plug([PluginOne, PluginTwo]);
338
 
                    Y.Assert.areEqual(1, h1.one.get("a"));
339
 
                    Y.Assert.areEqual(2, h1.two.get("b"));
340
 
                },
341
 
 
342
 
                testPlugFormatsArrayCustom : function() {
343
 
                    var h1 = new Host();
344
 
                    h1.plug([{fn:PluginOne, cfg:{a:10}}, {fn:PluginTwo, cfg:{b:20}}]);
345
 
                    Y.Assert.areEqual(10, h1.one.get("a"));
346
 
                    Y.Assert.areEqual(20, h1.two.get("b"));
347
 
                },
348
 
 
349
 
                testPlugFormatsArrayMixed : function() {
350
 
                    var h1 = new Host();
351
 
                    h1.plug([{fn:PluginOne, cfg:{a:10}}, PluginTwo]);
352
 
                    Y.Assert.areEqual(10, h1.one.get("a"));
353
 
                    Y.Assert.areEqual(2, h1.two.get("b"));
354
 
                },
355
 
 
356
 
                testPlugSamePluginTwice : function() {
357
 
                    var h1 = new Host();
358
 
 
359
 
                    var expectedEvents = ["PluginOne destroy"];
360
 
                    var actualEvents = [];
361
 
 
362
 
                    h1.plug(PluginOne, {a:10});
363
 
                    Y.Assert.areEqual(10, h1.one.get("a"));
364
 
 
365
 
                    h1.one.on("destroy", function(e) {
366
 
                        actualEvents.push("PluginOne destroy");
367
 
                    });
368
 
 
369
 
                    h1.plug(PluginOne, {a:20});
370
 
                    Y.Assert.areEqual(20, h1.one.get("a"));
371
 
 
372
 
                    h1.unplug(PluginOne);
373
 
                },
374
 
                
375
 
                testPluginHostReference : function() {
376
 
 
377
 
                    var h1 = new Host();
378
 
                    h1.plug(PluginOne);
379
 
 
380
 
                    Y.Assert.areEqual(h1, h1.one.get("host"));
381
 
 
382
 
                    h1.one.set("host", this);
383
 
 
384
 
                    Y.Assert.areEqual(h1, h1.one.get("host"));    
385
 
                },
386
 
 
387
 
                testPluginEventListeners : function() {
388
 
 
389
 
                    var h1 = new Host(), stack;
390
 
 
391
 
                    stack = [];
392
 
                    h1.set("mainA", 10, {stack:stack});
393
 
                    h1.set("mainB", 20, {stack:stack});
394
 
 
395
 
                    Y.ArrayAssert.itemsAreEqual([], stack);
396
 
 
397
 
                    h1.plug(PluginFour);
398
 
 
399
 
                    h1.set("mainA", 11, {stack:stack});
400
 
                    h1.set("mainB", 21, {stack:stack});
401
 
 
402
 
                    Y.ArrayAssert.itemsAreEqual(["PluginFour._onAChange", "PluginFour._afterBChange"], stack);
403
 
 
404
 
                    h1.unplug(PluginFour);
405
 
 
406
 
                    stack = [];
407
 
                    h1.set("mainA", 12, {stack:stack});
408
 
                    h1.set("mainB", 22, {stack:stack});
409
 
 
410
 
                    Y.ArrayAssert.itemsAreEqual([], stack);
411
 
                },
412
 
 
413
 
                testPluginMethodInjection : function() {
414
 
                    var h1 = new Host(), stack;
415
 
 
416
 
                    stack = [];
417
 
                    h1.methodA(stack);
418
 
                    h1.methodB(stack);
419
 
 
420
 
                    Y.ArrayAssert.itemsAreEqual(["Host.methodA", "Host.methodB"], stack);
421
 
 
422
 
                    h1.plug(PluginFour);
423
 
 
424
 
                    stack = [];
425
 
                    h1.methodA(stack);
426
 
                    h1.methodB(stack);
427
 
 
428
 
                    Y.ArrayAssert.itemsAreEqual(["PluginFour.methodA", "Host.methodA", "Host.methodB", "PluginFour.methodB"], stack);
429
 
 
430
 
                    h1.unplug(PluginFour);
431
 
 
432
 
                    stack = [];
433
 
                    h1.methodA(stack);
434
 
                    h1.methodB(stack);
435
 
 
436
 
                    Y.ArrayAssert.itemsAreEqual(["Host.methodA", "Host.methodB"], stack);
437
 
                },
438
 
 
439
 
                // Execute Last - will affect Host, ExtendedHost classes on the page
440
 
                testStaticPlug : function() {
441
 
 
442
 
                    Y.Base.plug(Host, PluginOne);
443
 
                    Y.Base.plug(Host, [{fn:PluginTwo, cfg:{}}, PluginThree]);
444
 
 
445
 
                    var h1 = new Host();
446
 
 
447
 
                    Y.ObjectAssert.ownsKey("one", h1);
448
 
                    Y.Assert.isInstanceOf(PluginOne, h1.one);
449
 
 
450
 
                    Y.ObjectAssert.ownsKey("two", h1);
451
 
                    Y.Assert.isInstanceOf(PluginTwo, h1.two);
452
 
 
453
 
                    Y.ObjectAssert.ownsKey("three", h1);
454
 
                    Y.Assert.isInstanceOf(PluginThree, h1.three);
455
 
 
456
 
                    Y.Base.unplug(Host, PluginThree);
457
 
 
458
 
                    // ---
459
 
 
460
 
                    Y.Base.plug(ExtendedHost, PluginThree);
461
 
                    Y.Base.unplug(ExtendedHost, PluginOne);
462
 
 
463
 
                    var h2 = new Host();
464
 
 
465
 
                    Y.ObjectAssert.ownsKey("one", h2);
466
 
                    Y.Assert.isInstanceOf(PluginOne, h2.one);
467
 
                    Y.ObjectAssert.ownsKey("two", h2);
468
 
                    Y.Assert.isInstanceOf(PluginTwo, h2.two);
469
 
                    Y.Assert.isUndefined(h2.three);
470
 
 
471
 
                    var h3 = new ExtendedHost();
472
 
 
473
 
                    Y.Assert.isUndefined(h3.one);
474
 
                    Y.ObjectAssert.ownsKey("two", h3);
475
 
                    Y.Assert.isInstanceOf(PluginTwo, h3.two);
476
 
 
477
 
                    Y.ObjectAssert.ownsKey("three", h3);
478
 
                    Y.Assert.isInstanceOf(PluginThree, h3.three);
479
 
                },
480
 
 
481
 
                testHostDestroy : function() {
482
 
 
483
 
                    var h1 = new Host({
484
 
                        plugins:[PluginOne, PluginTwo]
485
 
                    });
486
 
 
487
 
                    var destroyed = [];
488
 
                    h1.one.after("destroy", function(e) {
489
 
                        destroyed.push("one");
490
 
                    });
491
 
                    h1.two.after("destroy", function(e) {
492
 
                        destroyed.push("two");
493
 
                    });
494
 
 
495
 
                    h1.destroy();
496
 
 
497
 
                    Y.Assert.isUndefined(h1.one);
498
 
                    Y.Assert.isUndefined(h1.two);
499
 
 
500
 
                    Y.ArrayAssert.itemsAreEqual(["one", "two"], destroyed);
501
 
 
502
 
 
503
 
                }
504
 
 
505
 
            };
506
 
 
507
 
            var suite = new Y.Test.Suite("Plugin Tests");
508
 
            suite.add(new Y.Test.Case(basicTemplate));
509
 
 
510
 
            Y.Test.Runner.setName("Plugin Tests");
511
 
            Y.Test.Runner.add(suite);
512
 
            Y.Test.Runner.disableLogging();
513
 
            Y.Test.Runner.run();
514
 
 
515
 
            var console;
516
 
 
517
 
            Y.one("#btnRun").set("disabled", false).on("click", function() {
518
 
                if (!console) {
519
 
                    console = new Y.Console({
520
 
                        id:"console",
521
 
                        width:"100%",
522
 
                        height:"90%",
523
 
                        verbose : false,
524
 
                        printTimeout: 0,
525
 
                        newestOnTop : false,
526
 
                        entryTemplate: '<pre class="{entry_class} {cat_class} {src_class}">'+
527
 
                                '<span class="{entry_cat_class}">{label}</span>'+
528
 
                                '<span class="{entry_content_class}">{message}</span>'+
529
 
                        '</pre>'
530
 
                    }).render();
531
 
                }
532
 
 
533
 
                Y.Test.Runner.enableLogging();
534
 
                Y.Test.Runner.run();
535
 
            });
536
 
        });
537
 
    </script>
538
 
    <p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
539
 
</body>
540
 
</html>