1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4
<title>PluginHost Tests</title>
7
<body class="yui3-skin-sam">
9
<script type="text/javascript" src="../../../build/yui/yui.js"></script>
11
<style type="text/css">
12
#console .yui3-console-entry {
18
#console .yui3-console-entry-fail .yui3-console-entry-cat {
22
#console .yui3-console-entry-pass .yui3-console-entry-cat {
23
background-color:green;
26
#console .yui3-console-entry-perf .yui3-console-entry-cat {
27
background-color:blue;
39
<script type="text/javascript">
41
useBrowserConsole:false,
42
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min'
43
}).use("basebase", "base-pluginhost", "base-build", "console", "plugin", "test", function(Y) {
45
function PluginOne() {
46
PluginOne.superclass.constructor.apply(this, arguments);
49
PluginOne.NAME = "pluginOne";
56
Y.extend(PluginOne, Y.Plugin.Base, {
57
initializer: function() {
65
function PluginTwo() {
66
PluginTwo.superclass.constructor.apply(this, arguments);
69
PluginTwo.NAME = "pluginTwo";
75
Y.extend(PluginTwo, Y.Plugin.Base, {
76
initializer: function() {
84
var PluginThree = Y.Base.create("pluginThree", Y.Plugin.Base, [], {
85
initializer: function() {
100
function PluginFour() {
101
PluginFour.superclass.constructor.apply(this, arguments);
103
PluginFour.NS = "four";
104
PluginFour.NAME = "pluginFour";
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);
119
_methodA : function(stack) {
120
stack.push("PluginFour.methodA");
123
_methodB : function(stack) {
124
stack.push("PluginFour.methodB");
127
_onAChange : function(e) {
129
e.stack.push("PluginFour._onAChange");
133
_afterBChange : function(e) {
135
e.stack.push("PluginFour._afterBChange");
140
function Host(config) {
141
Host.superclass.constructor.apply(this, arguments);
154
Y.extend(Host, Y.Base, {
155
methodA : function(stack) {
157
stack.push("Host.methodA");
161
methodB : function(stack) {
163
stack.push("Host.methodB");
167
initializer : function() {
171
function ExtendedHost() {
172
ExtendedHost.superclass.constructor.apply(this, arguments);
175
Y.extend(ExtendedHost, Host, {
176
methodC : function() {},
177
initializer : function() {}
180
var basicTemplate = {
184
testPlugDuringConstruction : function() {
186
plugins:[PluginOne, {fn:PluginTwo, cfg:{two:4}}, {fn:PluginThree, cfg:{three:6}}]
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);
197
plugins:[PluginOne, PluginTwo]
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);
207
testPlugUsingPlug : function() {
211
h1.plug(PluginTwo, {two:4});
212
h1.plug({fn:PluginThree, cfg:{three:6}});
214
Y.ObjectAssert.ownsKey("one", h1);
215
Y.Assert.isInstanceOf(PluginOne, h1.one);
217
Y.ObjectAssert.ownsKey("two", h1);
218
Y.Assert.isInstanceOf(PluginTwo, h1.two);
220
Y.ObjectAssert.ownsKey("three", h1);
221
Y.Assert.isInstanceOf(PluginThree, h1.three);
224
h2.plug([PluginOne, {fn:PluginTwo, cfg:{two:8}}]);
226
Y.ObjectAssert.ownsKey("one", h2);
227
Y.Assert.isInstanceOf(PluginOne, h2.one);
229
Y.ObjectAssert.ownsKey("two", h2);
230
Y.Assert.isInstanceOf(PluginTwo, h2.two);
232
Y.Assert.isUndefined(h2.three);
235
testUnplug : function() {
238
plugins:[PluginOne, PluginTwo]
241
Y.ObjectAssert.ownsKey("one", h1);
242
Y.Assert.isInstanceOf(PluginOne, h1.one);
244
Y.ObjectAssert.ownsKey("two", h1);
245
Y.Assert.isInstanceOf(PluginTwo, h1.two);
249
Y.Assert.isUndefined(h1.one);
251
Y.ObjectAssert.ownsKey("two", h1);
252
Y.Assert.isInstanceOf(PluginTwo, h1.two);
254
h1.unplug(PluginTwo);
256
Y.Assert.isUndefined(h1.one);
257
Y.Assert.isUndefined(h1.two);
260
testUnplugNonExistant : function() {
263
var NonExistant = function() {}
264
NonExistant.NS = "foo";
266
var SharedNS = function() {}
271
h1.unplug(NonExistant);
273
Y.Assert.fail("Error unplugging a non-plugged in plugin");
279
// Unplugging a diff plugin with the same NS
282
Y.Assert.fail("Error unplugging a plugin which shares a NS with a plugged in plugin");
285
Y.Assert.isTrue(h1.one instanceof PluginOne);
288
testUnplugPlug : function() {
291
plugins:[PluginOne, PluginTwo]
294
Y.ObjectAssert.ownsKey("one", h1);
295
Y.Assert.isInstanceOf(PluginOne, h1.one);
297
Y.ObjectAssert.ownsKey("two", h1);
298
Y.Assert.isInstanceOf(PluginTwo, h1.two);
300
h1.unplug(PluginOne);
302
Y.Assert.isUndefined(h1.one);
306
Y.ObjectAssert.ownsKey("one", h1);
307
Y.Assert.isInstanceOf(PluginOne, h1.one);
310
testPluginHost : function() {
314
Y.Assert.areSame(h1, h1.one.get("host"));
317
testPlugFormatsDefault : function() {
320
Y.Assert.areEqual(1, h1.one.get("a"));
323
testPlugFormatsCustom : function() {
325
h1.plug(PluginOne, {a:10});
326
Y.Assert.areEqual(10, h1.one.get("a"));
329
testPlugFormatsCustomLiteral : function() {
331
h1.plug({fn:PluginOne, cfg:{a:10}});
332
Y.Assert.areEqual(10, h1.one.get("a"));
335
testPlugFormatsArrayDefault : function() {
337
h1.plug([PluginOne, PluginTwo]);
338
Y.Assert.areEqual(1, h1.one.get("a"));
339
Y.Assert.areEqual(2, h1.two.get("b"));
342
testPlugFormatsArrayCustom : function() {
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"));
349
testPlugFormatsArrayMixed : function() {
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"));
356
testPlugSamePluginTwice : function() {
359
var expectedEvents = ["PluginOne destroy"];
360
var actualEvents = [];
362
h1.plug(PluginOne, {a:10});
363
Y.Assert.areEqual(10, h1.one.get("a"));
365
h1.one.on("destroy", function(e) {
366
actualEvents.push("PluginOne destroy");
369
h1.plug(PluginOne, {a:20});
370
Y.Assert.areEqual(20, h1.one.get("a"));
372
h1.unplug(PluginOne);
375
testPluginHostReference : function() {
380
Y.Assert.areEqual(h1, h1.one.get("host"));
382
h1.one.set("host", this);
384
Y.Assert.areEqual(h1, h1.one.get("host"));
387
testPluginEventListeners : function() {
389
var h1 = new Host(), stack;
392
h1.set("mainA", 10, {stack:stack});
393
h1.set("mainB", 20, {stack:stack});
395
Y.ArrayAssert.itemsAreEqual([], stack);
399
h1.set("mainA", 11, {stack:stack});
400
h1.set("mainB", 21, {stack:stack});
402
Y.ArrayAssert.itemsAreEqual(["PluginFour._onAChange", "PluginFour._afterBChange"], stack);
404
h1.unplug(PluginFour);
407
h1.set("mainA", 12, {stack:stack});
408
h1.set("mainB", 22, {stack:stack});
410
Y.ArrayAssert.itemsAreEqual([], stack);
413
testPluginMethodInjection : function() {
414
var h1 = new Host(), stack;
420
Y.ArrayAssert.itemsAreEqual(["Host.methodA", "Host.methodB"], stack);
428
Y.ArrayAssert.itemsAreEqual(["PluginFour.methodA", "Host.methodA", "Host.methodB", "PluginFour.methodB"], stack);
430
h1.unplug(PluginFour);
436
Y.ArrayAssert.itemsAreEqual(["Host.methodA", "Host.methodB"], stack);
439
// Execute Last - will affect Host, ExtendedHost classes on the page
440
testStaticPlug : function() {
442
Y.Base.plug(Host, PluginOne);
443
Y.Base.plug(Host, [{fn:PluginTwo, cfg:{}}, PluginThree]);
447
Y.ObjectAssert.ownsKey("one", h1);
448
Y.Assert.isInstanceOf(PluginOne, h1.one);
450
Y.ObjectAssert.ownsKey("two", h1);
451
Y.Assert.isInstanceOf(PluginTwo, h1.two);
453
Y.ObjectAssert.ownsKey("three", h1);
454
Y.Assert.isInstanceOf(PluginThree, h1.three);
456
Y.Base.unplug(Host, PluginThree);
460
Y.Base.plug(ExtendedHost, PluginThree);
461
Y.Base.unplug(ExtendedHost, PluginOne);
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);
471
var h3 = new ExtendedHost();
473
Y.Assert.isUndefined(h3.one);
474
Y.ObjectAssert.ownsKey("two", h3);
475
Y.Assert.isInstanceOf(PluginTwo, h3.two);
477
Y.ObjectAssert.ownsKey("three", h3);
478
Y.Assert.isInstanceOf(PluginThree, h3.three);
481
testHostDestroy : function() {
484
plugins:[PluginOne, PluginTwo]
488
h1.one.after("destroy", function(e) {
489
destroyed.push("one");
491
h1.two.after("destroy", function(e) {
492
destroyed.push("two");
497
Y.Assert.isUndefined(h1.one);
498
Y.Assert.isUndefined(h1.two);
500
Y.ArrayAssert.itemsAreEqual(["one", "two"], destroyed);
507
var suite = new Y.Test.Suite("Plugin Tests");
508
suite.add(new Y.Test.Case(basicTemplate));
510
Y.Test.Runner.setName("Plugin Tests");
511
Y.Test.Runner.add(suite);
512
Y.Test.Runner.disableLogging();
517
Y.one("#btnRun").set("disabled", false).on("click", function() {
519
console = new Y.Console({
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>'+
533
Y.Test.Runner.enableLogging();
538
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>