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", "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() {
63
function PluginTwo() {
64
PluginTwo.superclass.constructor.apply(this, arguments);
67
PluginTwo.NAME = "pluginTwo";
73
Y.extend(PluginTwo, Y.Plugin.Base, {
74
initializer: function() {
80
function PluginThree() {
81
PluginThree.superclass.constructor.apply(this, arguments);
83
PluginThree.NS = "three";
84
PluginThree.NAME = "pluginThree";
90
Y.extend(PluginThree, Y.Plugin.Base, {
91
initializer: function() {
97
function PluginFour() {
98
PluginFour.superclass.constructor.apply(this, arguments);
100
PluginFour.NS = "four";
101
PluginFour.NAME = "pluginFour";
108
Y.extend(PluginFour, Y.Plugin.Base, {
109
initializer: function(cfg) {
110
this.doBefore("methodA", this._methodA, this);
111
this.doAfter("methodB", this._methodB, this);
112
this.doBefore("mainAChange", this._onAChange);
113
this.doAfter("mainBChange", this._afterBChange);
116
_methodA : function(stack) {
117
stack.push("PluginFour.methodA");
120
_methodB : function(stack) {
121
stack.push("PluginFour.methodB");
124
_onAChange : function(e) {
126
e.stack.push("PluginFour._onAChange");
130
_afterBChange : function(e) {
132
e.stack.push("PluginFour._afterBChange");
137
function Host(config) {
138
Host.superclass.constructor.apply(this, arguments);
151
Y.extend(Host, Y.Base, {
152
methodA : function(stack) {
154
stack.push("Host.methodA");
158
methodB : function(stack) {
160
stack.push("Host.methodB");
164
initializer : function() {
168
function ExtendedHost() {
169
ExtendedHost.superclass.constructor.apply(this, arguments);
172
Y.extend(ExtendedHost, Host, {
173
methodC : function() {},
174
initializer : function() {}
177
var basicTemplate = {
181
testPlugDuringConstruction : function() {
183
plugins:[PluginOne, {fn:PluginTwo, cfg:{two:4}}, {fn:PluginThree, cfg:{three:6}}]
186
Y.ObjectAssert.ownsKey("one", h1);
187
Y.Assert.isInstanceOf(PluginOne, h1.one);
188
Y.ObjectAssert.ownsKey("two", h1);
189
Y.Assert.isInstanceOf(PluginTwo, h1.two);
190
Y.ObjectAssert.ownsKey("three", h1);
191
Y.Assert.isInstanceOf(PluginThree, h1.three);
194
plugins:[PluginOne, PluginTwo]
197
Y.ObjectAssert.ownsKey("one", h2);
198
Y.Assert.isInstanceOf(PluginOne, h2.one);
199
Y.ObjectAssert.ownsKey("two", h2);
200
Y.Assert.isInstanceOf(PluginTwo, h2.two);
201
Y.Assert.isUndefined(h2.three);
204
testPlugUsingPlug : function() {
208
h1.plug(PluginTwo, {two:4});
209
h1.plug({fn:PluginThree, cfg:{three:6}});
211
Y.ObjectAssert.ownsKey("one", h1);
212
Y.Assert.isInstanceOf(PluginOne, h1.one);
214
Y.ObjectAssert.ownsKey("two", h1);
215
Y.Assert.isInstanceOf(PluginTwo, h1.two);
217
Y.ObjectAssert.ownsKey("three", h1);
218
Y.Assert.isInstanceOf(PluginThree, h1.three);
221
h2.plug([PluginOne, {fn:PluginTwo, cfg:{two:8}}]);
223
Y.ObjectAssert.ownsKey("one", h2);
224
Y.Assert.isInstanceOf(PluginOne, h2.one);
226
Y.ObjectAssert.ownsKey("two", h2);
227
Y.Assert.isInstanceOf(PluginTwo, h2.two);
229
Y.Assert.isUndefined(h2.three);
232
testUnplug : function() {
235
plugins:[PluginOne, PluginTwo]
238
Y.ObjectAssert.ownsKey("one", h1);
239
Y.Assert.isInstanceOf(PluginOne, h1.one);
241
Y.ObjectAssert.ownsKey("two", h1);
242
Y.Assert.isInstanceOf(PluginTwo, h1.two);
246
Y.Assert.isUndefined(h1.one);
248
Y.ObjectAssert.ownsKey("two", h1);
249
Y.Assert.isInstanceOf(PluginTwo, h1.two);
251
h1.unplug(PluginTwo);
253
Y.Assert.isUndefined(h1.one);
254
Y.Assert.isUndefined(h1.two);
257
testUnplugPlug : function() {
260
plugins:[PluginOne, PluginTwo]
263
Y.ObjectAssert.ownsKey("one", h1);
264
Y.Assert.isInstanceOf(PluginOne, h1.one);
266
Y.ObjectAssert.ownsKey("two", h1);
267
Y.Assert.isInstanceOf(PluginTwo, h1.two);
269
h1.unplug(PluginOne);
271
Y.Assert.isUndefined(h1.one);
275
Y.ObjectAssert.ownsKey("one", h1);
276
Y.Assert.isInstanceOf(PluginOne, h1.one);
279
testPluginHost : function() {
283
Y.Assert.areSame(h1, h1.one.get("host"));
286
testPlugFormatsDefault : function() {
289
Y.Assert.areEqual(1, h1.one.get("a"));
292
testPlugFormatsCustom : function() {
294
h1.plug(PluginOne, {a:10});
295
Y.Assert.areEqual(10, h1.one.get("a"));
298
testPlugFormatsCustomLiteral : function() {
300
h1.plug({fn:PluginOne, cfg:{a:10}});
301
Y.Assert.areEqual(10, h1.one.get("a"));
304
testPlugFormatsArrayDefault : function() {
306
h1.plug([PluginOne, PluginTwo]);
307
Y.Assert.areEqual(1, h1.one.get("a"));
308
Y.Assert.areEqual(2, h1.two.get("b"));
311
testPlugFormatsArrayCustom : function() {
313
h1.plug([{fn:PluginOne, cfg:{a:10}}, {fn:PluginTwo, cfg:{b:20}}]);
314
Y.Assert.areEqual(10, h1.one.get("a"));
315
Y.Assert.areEqual(20, h1.two.get("b"));
318
testPlugFormatsArrayMixed : function() {
320
h1.plug([{fn:PluginOne, cfg:{a:10}}, PluginTwo]);
321
Y.Assert.areEqual(10, h1.one.get("a"));
322
Y.Assert.areEqual(2, h1.two.get("b"));
325
testPlugSamePluginTwice : function() {
328
var expectedEvents = ["PluginOne destroy"];
329
var actualEvents = [];
331
h1.plug(PluginOne, {a:10});
332
Y.Assert.areEqual(10, h1.one.get("a"));
334
h1.one.on("destroy", function(e) {
335
actualEvents.push("PluginOne destroy");
338
h1.plug(PluginOne, {a:20});
339
Y.Assert.areEqual(20, h1.one.get("a"));
341
h1.unplug(PluginOne);
344
testPluginEventListeners : function() {
346
var h1 = new Host(), stack;
349
h1.set("mainA", 10, {stack:stack});
350
h1.set("mainB", 20, {stack:stack});
352
Y.ArrayAssert.itemsAreEqual([], stack);
356
h1.set("mainA", 11, {stack:stack});
357
h1.set("mainB", 21, {stack:stack});
359
Y.ArrayAssert.itemsAreEqual(["PluginFour._onAChange", "PluginFour._afterBChange"], stack);
361
h1.unplug(PluginFour);
364
h1.set("mainA", 12, {stack:stack});
365
h1.set("mainB", 22, {stack:stack});
367
Y.ArrayAssert.itemsAreEqual([], stack);
370
testPluginMethodInjection : function() {
371
var h1 = new Host(), stack;
377
Y.ArrayAssert.itemsAreEqual(["Host.methodA", "Host.methodB"], stack);
385
Y.ArrayAssert.itemsAreEqual(["PluginFour.methodA", "Host.methodA", "Host.methodB", "PluginFour.methodB"], stack);
387
h1.unplug(PluginFour);
393
Y.ArrayAssert.itemsAreEqual(["Host.methodA", "Host.methodB"], stack);
396
// Execute Last - will affect Host, ExtendedHost classes on the page
397
testStaticPlug : function() {
399
Y.Base.plug(Host, PluginOne);
400
Y.Base.plug(Host, [{fn:PluginTwo, cfg:{}}, PluginThree]);
404
Y.ObjectAssert.ownsKey("one", h1);
405
Y.Assert.isInstanceOf(PluginOne, h1.one);
407
Y.ObjectAssert.ownsKey("two", h1);
408
Y.Assert.isInstanceOf(PluginTwo, h1.two);
410
Y.ObjectAssert.ownsKey("three", h1);
411
Y.Assert.isInstanceOf(PluginThree, h1.three);
413
Y.Base.unplug(Host, PluginThree);
417
Y.Base.plug(ExtendedHost, PluginThree);
418
Y.Base.unplug(ExtendedHost, PluginOne);
422
Y.ObjectAssert.ownsKey("one", h2);
423
Y.Assert.isInstanceOf(PluginOne, h2.one);
424
Y.ObjectAssert.ownsKey("two", h2);
425
Y.Assert.isInstanceOf(PluginTwo, h2.two);
426
Y.Assert.isUndefined(h2.three);
428
var h3 = new ExtendedHost();
430
Y.Assert.isUndefined(h3.one);
431
Y.ObjectAssert.ownsKey("two", h3);
432
Y.Assert.isInstanceOf(PluginTwo, h3.two);
434
Y.ObjectAssert.ownsKey("three", h3);
435
Y.Assert.isInstanceOf(PluginThree, h3.three);
438
testHostDestroy : function() {
441
plugins:[PluginOne, PluginTwo]
445
h1.one.after("destroy", function(e) {
446
destroyed.push("one");
448
h1.two.after("destroy", function(e) {
449
destroyed.push("two");
454
Y.Assert.isUndefined(h1.one);
455
Y.Assert.isUndefined(h1.two);
457
Y.ArrayAssert.itemsAreEqual(["one", "two"], destroyed);
464
var suite = new Y.Test.Suite("Plugin Tests");
465
suite.add(new Y.Test.Case(basicTemplate));
467
Y.Test.Runner.setName("Plugin Tests");
468
Y.Test.Runner.add(suite);
469
Y.Test.Runner.disableLogging();
474
Y.one("#btnRun").set("disabled", false).on("click", function() {
476
console = new Y.Console({
483
entryTemplate: '<pre class="{entry_class} {cat_class} {src_class}">'+
484
'<span class="{entry_cat_class}">{label}</span>'+
485
'<span class="{entry_content_class}">{message}</span>'+
490
Y.Test.Runner.enableLogging();
495
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>