Cross Reference: /yui3/src/widget-parent/tests/parent-child.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Widget Parent Child Test Suite</title>
<script src="/build/yui/yui.js"></script>
<style type="text/css">
#console .yui3-console-entry {
padding:2px;
margin:0px;
min-height:0;
}
#console .yui3-console-entry-fail .yui3-console-entry-cat {
background-color:red;
}
#console .yui3-console-entry-pass .yui3-console-entry-cat {
background-color:green;
}
#console .yui3-console-entry-perf .yui3-console-entry-cat {
background-color:blue;
}
#console {
position:static;
}
html, body {
height:100%;
}
</style>
</head>
<body class="yui3-skin-sam">
<script type="text/javascript">
YUI({
useBrowserConsole:false,
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min'
}).use("node-event-simulate", "async-queue", "widget", "widget-parent", "widget-child", "test", "console", function (Y) {
var RootWidget = Y.RootWidget = Y.Base.create("rootwidget", Y.Widget, [Y.WidgetParent]);
var ParentWidget = Y.ParentWidget = Y.Base.create("parentwidget", Y.Widget, [Y.WidgetParent, Y.WidgetChild], {
renderUI: function () {
ParentWidget.superclass.renderUI.apply(this, arguments);
if (this.get("depth") > -1) {
this.get("contentBox").insert("<em>" + this.get("label") + "</em>");
}
}
}, {
ATTRS: {
label: {
validator: Y.Lang.isString
}
}
});
var ChildWidget = Y.ChildWidget = Y.Base.create("childwidget", Y.Widget, [Y.WidgetChild], {
renderUI: function () {
this.get("contentBox").setContent(this.get("label"));
}
}, {
ATTRS: {
label: {
validator: Y.Lang.isString
}
}
});
var suite = new Y.Test.Suite("Widget ParentChild Tests");
suite.add(
new Y.Test.Case({
name: "Widget Parent API, Widget Child API Tests",
testAdd: function() {
var root = new Y.ParentWidget(),
child,
MyChild;
child = root.add({ type: "ChildWidget", label: "Child One" });
Y.Assert.isNotUndefined(child, "Add should return a reference to the child that was created and added.");
Y.assert((root.size() == 1), "The root Widget should have one child");
child = root.add({ childType: ChildWidget, label: "Child Two" });
Y.Assert.isNotUndefined(child, "Add should return a reference to the child that was created and added.");
Y.assert((root.size() == 2), "The root Widget should have two children");
try {
child = root.add({ label: "Invalid Child" });
Y.Assert.isUndefined(child, "Add should return undefined since the type of the child is undefined.");
Y.assert((root.size() == 2), "The root Widget should have two children");
} catch (e) {
Y.Assert.isNotUndefined(e, "The \"add\" method should throw an error since the type of the child was not specified and the defaultChildType attribute is not defined.");
}
try {
child = root.add({ childType: "MyChild", label: "Invalid Child" });
Y.Assert.isUndefined(child, "The \"add\" method should return undefined since the type of the child is invalid.");
Y.assert((root.size() == 2), "The root Widget should have two children");
} catch (e) {
Y.Assert.isNotUndefined(e, "The \"add\" method should throw an error since the type of the child is invalid.");
}
try {
child = root.add({ type: MyChild, label: "Invalid Child" });
Y.Assert.isUndefined(child, "Add should return undefined since the type of the child is invalid.");
Y.assert((root.size() == 2), "The root Widget should have two children");
} catch (e) {
Y.Assert.isNotUndefined(e, "The \"add\" method should throw an error since the type of the child is invalid.");
}
root.set("defaultChildType", "ChildWidget");
child = root.add({ label: "Child Three" });
Y.Assert.isNotUndefined(child, "Add should return a reference to the child that was created and added.");
Y.assert((root.size() == 3), "The root Widget should have three children");
root.set("defaultChildType", ChildWidget);
child = root.add({ label: "Child Four" });
Y.Assert.isNotUndefined(child, "Add should return a reference to the child that was created and added.");
Y.assert((root.size() == 4), "The root Widget should have four children");
root.set("defaultChildType", "MyChild");
Y.Assert.areEqual(root.get("defaultChildType"), Y.ChildWidget, "Setter for \"defaultChildType\" should disallow setting the attribute to an invalid value.");
root.set("defaultChildType", MyChild);
Y.Assert.areEqual(root.get("defaultChildType"), Y.ChildWidget, "Setter for \"defaultChildType\" should disallow setting the attribute to an invalid value.");
root.render();
root.destroy();
},
testInsert: function () {
var widget = new Y.ParentWidget({
id: "widget-1",
children: [
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
]
});
widget.render();
widget.add({ type: Y.ChildWidget, label: "Child One", id: "inserted-child-1" }, 0);
Y.Assert.areEqual(widget.item(0).get("id"), "inserted-child-1", "The newly inserted child should be the parent's first child.");
Y.Assert.areEqual(widget.size(), 5, "The widget should have five children.");
widget.add([
{ type: Y.ChildWidget, label: "Child One", id: "inserted-child-2" },
{ type: Y.ChildWidget, label: "Child One", id: "inserted-child-3" }
], 2);
Y.Assert.areEqual(widget.item(2).get("id"), "inserted-child-2", "The child with the id of \"inserted-child-2\" should be the parent's third child.");
Y.Assert.areEqual(widget.item(3).get("id"), "inserted-child-3", "The child with the id of \"inserted-child-3\" should be the parent's fourth child.");
Y.Assert.areEqual(widget.item(4).get("id"), "child-2", "The child with the id of \"child-2\" should now be the parent's fith child.");
Y.Assert.areEqual(widget.size(), 7, "The widget should have seven children.");
widget.destroy();
},
testPreRenderedChildWithEmptyParent : function() {
// Based on pull request from andreas-karlsson
// https://github.com/yui/yui3/pull/25#issuecomment-2103536
var widget = new Y.ParentWidget({
id:"widget-1"
});
widget.render();
var child = new Y.ChildWidget({
id:"child-1"
});
child.render();
widget.add(child);
Y.Assert.areEqual(1, widget.size(1), "expected one child");
var renderedChildren = widget.get("contentBox").get("children");
Y.Assert.areEqual(1, renderedChildren.size(), "expected one child node");
widget.destroy();
},
testAddOutOfOrderPreRender : function() {
var widget = new Y.ParentWidget({
id:"widget-1"
});
widget.add({type: Y.ChildWidget, label: "Child Two", id: "child-2" }, 1);
widget.add({type: Y.ChildWidget, label: "Child One", id: "child-1" }, 0);
widget.add({type: Y.ChildWidget, label: "Child Three", id: "child-3" }, 2);
widget.render();
var children = widget.get("children");
Y.Assert.areEqual(widget.item(0).get("id"), "child-1", "child-1 is out of order");
Y.Assert.areEqual(widget.item(1).get("id"), "child-2", "child-2 is out of order");
Y.Assert.areEqual(widget.item(2).get("id"), "child-3", "child-3 is out of order");
var renderedChildren = widget.get("contentBox").get("children");
Y.Assert.areEqual(renderedChildren.item(0).get("id"), "child-1", "child-1 is out of order");
Y.Assert.areEqual(renderedChildren.item(1).get("id"), "child-2", "child-2 is out of order");
Y.Assert.areEqual(renderedChildren.item(2).get("id"), "child-3", "child-3 is out of order");
widget.destroy();
},
testAddOutOfOrderPostRender : function() {
var widget = new Y.ParentWidget({
id:"widget-1"
});
widget.render();
widget.add({type: Y.ChildWidget, label: "Child Two", id: "child-2" }, 1);
widget.add({type: Y.ChildWidget, label: "Child One", id: "child-1" }, 0);
widget.add({type: Y.ChildWidget, label: "Child Three", id: "child-3" }, 2);
Y.Assert.areEqual(widget.item(0).get("id"), "child-1", "child-1 is out of order");
Y.Assert.areEqual(widget.item(1).get("id"), "child-2", "child-2 is out of order");
Y.Assert.areEqual(widget.item(2).get("id"), "child-3", "child-3 is out of order");
var renderedChildren = widget.get("contentBox").get("children");
Y.Assert.areEqual(renderedChildren.item(0).get("id"), "child-1", "child-1 is out of order");
Y.Assert.areEqual(renderedChildren.item(1).get("id"), "child-2", "child-2 is out of order");
Y.Assert.areEqual(renderedChildren.item(2).get("id"), "child-3", "child-3 is out of order");
widget.destroy();
},
testRemove: function () {
var widget = new Y.ParentWidget({
id: "widget-1",
children: [
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
]
});
widget.render();
var childSelector = "." + widget.item(0).getClassName(),
parentBB = widget.get("boundingBox");
Y.Assert.areEqual(widget.remove(1).get("id"), "child-2", "The parent's \"remove\" method should return a reference to the child removed.");
Y.Assert.areEqual(parentBB.one("#child-2"), null, "child-2's bounding box should have been removed");
Y.Assert.areEqual(parentBB.all(childSelector).size(), 3, "The widget should now have three children bounding boxes.");
Y.Assert.areEqual(widget.size(), 3, "The widget should now have three children.");
Y.Assert.areEqual(widget.item(0).remove().get("id"), "child-1", "If a child calls the \"remove\" method on itself, the \"remove\" method should return a reference to the child.");
Y.Assert.areEqual(parentBB.one("#child-1"), null, "child-1's bounding box should have been removed");
Y.Assert.areEqual(parentBB.all(childSelector).size(), 2, "The widget should now have two children bounding boxes.");
Y.Assert.areEqual(widget.size(), 2, "The widget should now have two children.");
var removed = widget.removeAll();
Y.Assert.areEqual(removed.size(), 2, "The \"removeAll\" method should return a Y.ArrayList instance with a size of 2.");
Y.Assert.areEqual(widget.size(), 0, "The widget should now have no children.");
Y.Assert.areEqual(parentBB.all(childSelector).size(), 0, "The widget should now have no children bounding boxes.");
widget.destroy();
},
testRemoveSelectedChild : function() {
var widget = new Y.ParentWidget({
id: "widget-1",
children: [
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
]
});
widget.render();
var childSelector = "." + widget.item(0).getClassName(),
parentBB = widget.get("boundingBox");
widget.selectChild(1);
Y.Assert.areSame(widget.item(1), widget.get("selection"));
Y.Assert.areEqual(widget.remove(1).get("id"), "child-2", "The parent's \"remove\" method should return a reference to the child removed.");
Y.Assert.areEqual(parentBB.one("#child-2"), null, "child-2's bounding box should have been removed");
Y.Assert.areEqual(parentBB.all(childSelector).size(), 3, "The widget should now have three children bounding boxes.");
Y.Assert.areEqual(widget.size(), 3, "The widget should now have three children.");
Y.Assert.areEqual(0, widget.get("selected"));
Y.Assert.isNull(widget.get("selection"));
widget.destroy();
},
testRemoveFocusedChild : function() {
var widget = new Y.ParentWidget({
id: "widget-1",
children: [
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
]
});
widget.render();
var childSelector = "." + widget.item(0).getClassName(),
parentBB = widget.get("boundingBox");
widget.item(1).focus();
Y.Assert.areSame(widget.item(1), widget.get("activeDescendant"));
var removedChild = widget.remove(1);
Y.Assert.isFalse(removedChild.get("focused"));
Y.Assert.areEqual(removedChild.get("id"), "child-2", "The parent's \"remove\" method should return a reference to the child removed.");
Y.Assert.areEqual(parentBB.one("#child-2"), null, "child-2's bounding box should have been removed");
Y.Assert.areEqual(parentBB.all(childSelector).size(), 3, "The widget should now have three children bounding boxes.");
Y.Assert.areEqual(widget.size(), 3, "The widget should now have three children.");
Y.Assert.isNull(widget.get("activeDescendant"));
widget.destroy();
},
testSingleSelection: function () {
var root = new Y.ParentWidget({
id: "new-widget",
children: [
{ type: Y.ChildWidget, label: "Child One" },
{ type: Y.ChildWidget, label: "Child Two" },
{ type: Y.ChildWidget, label: "Child Three" },
{ type: Y.ChildWidget, label: "Child Four" }
]
});
root.render();
root.item(0).set("selected", 1);
root.item(1).set("selected", 1);
// Confirm the selection
Y.Assert.areEqual(root.item(1).get("selected"), 1, "The second child of the parent widget should be selected");
Y.Assert.areEqual(root.get("selected"), 2, "The root's \"selected\" attribute should return 2");
Y.Assert.areEqual(root.get("selection"), root.item(1), "The parent's \"selection\" attribute should return a reference to its second child.");
// Select another child and confirm the previously selected
// child is deselected and and the parent's selection is
// correctly updated.
root.item(2).set("selected", 1);
Y.Assert.areEqual(root.item(1).get("selected"), 0, "The parent's second child should not be selected");
Y.Assert.areEqual(root.item(2).get("selected"), 1, "The parent's third child should be selected");
Y.Assert.areEqual(root.get("selection"), root.item(2), "The parent's \"selection\" attribute should return a reference to its third child.");
// Confirm deselection
root.item(2).set("selected", 0);
Y.Assert.areEqual(root.item(2).get("selected"), 0, "The parent's third child should not be selected");
Y.Assert.areEqual(root.get("selected"), 0, "The parent's \"selected\" attribute should return 0");
Y.Assert.isNull(root.get("selection"), "The parent's \"selection\" attribute should return null.");
root.destroy();
var tree = new Y.ParentWidget({
id: "tree",
children: [
{ type: Y.ChildWidget, id: "leaf-1", label: "Leaf One" },
{ type: Y.ChildWidget, id: "leaf-2", label: "Leaf Two" },
{ type: Y.ParentWidget, id: "subtree", label: "Subtree", children: [
{ type: Y.ChildWidget, id: "subtree-leaf-1", label: "Subtree - Leaf One" },
{ type: Y.ChildWidget, id: "subtree-leaf-2", label: "Subtree - Leaf Two" },
{ type: Y.ChildWidget, id: "subtree-leaf-3", label: "Subtree - Leaf Three" },
{ type: Y.ChildWidget, id: "subtree-leaf-4", label: "Subtree - Leaf Four" }
]}
]
});
tree.render();
// Select a child in the subtree to test if the selection is represented at the root level
tree.item(2).selectChild(0);
Y.Assert.areEqual(tree.get("selected"), 2, "The root's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.item(2).get("selected"), 2, "The subtree's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.get("selection").get("id"), "subtree", "The root's \"selection\" attribute should return the subtree.");
Y.Assert.areEqual(tree.item(2).get("selection").get("id"), "subtree-leaf-1", "The subtree's \"selection\" attribute should return the first child.");
// Select a child in the root to confirm that the subtree's selection is cleared
tree.item(0).set("selected", 1);
Y.Assert.areEqual(tree.get("selected"), 2, "The root's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.get("selection").get("id"), "leaf-1", "The root's \"selection\" attribute should return a reference to its first child.");
Y.Assert.areEqual(tree.item(2).get("selected"), 0, "The subtree should no longer be selected.");
Y.Assert.areEqual(tree.item(2).get("selection"), null, "The subtree's \"selection\" attribute should return null.");
// Select a new child in the subtree to test if the selection is represented at the root level
tree.item(2).item(2).set("selected", 1);
Y.Assert.areEqual(tree.get("selected"), 2, "The root's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.item(2).get("selected"), 2, "The subtree's \"selected\" attribute should be 2 (indicating partially selected).");
Y.Assert.areEqual(tree.get("selection").get("id"), "subtree", "The root's \"selection\" attribute should return the subtree.");
Y.Assert.areEqual(tree.item(2).get("selection").get("id"), "subtree-leaf-3", "The subtree's \"selection\" attribute should return its third child.");
// Confirm that we can't do a multiple selection
var evts = [];
tree.on("selectedChange", function() {
evts.push("root:onSelectedChange");
});
tree.after("selectedChange", function() {
evts.push("root:afterSelectedChange");
});
tree.set("selected", 1);
Y.ArrayAssert.itemsAreEqual(["root:onSelectedChange"], evts);
Y.Assert.areEqual(2, tree.get("selected"));
evts = [];
tree.item(2).on("selectedChange", function() {
evts.push("subTree:onSelectedChange");
});
tree.item(2).after("selectedChange", function() {
evts.push("subTree:afterSelectedChange");
});
tree.item(2).set("selected", 1);
Y.ArrayAssert.itemsAreEqual(["subTree:onSelectedChange", "root:onSelectedChange"], evts);
Y.Assert.areEqual(2, tree.get("selected"));
tree.destroy();
},
testMultipleSelection: function () {
var root = new Y.ParentWidget({
id: "m-s-widget",
multiple: true,
children: [
{ type: Y.ChildWidget, label: "Child One" },
{ type: Y.ChildWidget, label: "Child Two" },
{ type: Y.ChildWidget, label: "Child Three" },
{ type: Y.ChildWidget, label: "Child Four" }
]
});
root.render();
root.item(0).set("selected", 1);
root.item(1).set("selected", 1);
// Confirm that both children are selected and
// represented in the parent's selection.
Y.Assert.areEqual(root.item(0).get("selected"), 1, "The first child of the parent widget should be selected.");
Y.Assert.areEqual(root.item(1).get("selected"), 1, "The second child of the parent widget should be selected.");
Y.Assert.areEqual(root.get("selection").size(), 2, "The \"selection\" attribute should return an ArrayList with a size of 2.");
Y.Assert.areEqual(root.get("selected"), 2, "The root's \"selection\" attribute should be 2 (indicating partially selected).");
// Select remaining children to confirm all children
// are represented in the parent's selection and that
// the parent's "selected" attribute returns 1.
root.item(2).set("selected", 1);
root.item(3).set("selected", 1);
Y.Assert.areEqual(root.get("selection").size(), 4, "The \"selection\" attribute should return an ArrayList with a size of 4.");
Y.Assert.areEqual(root.get("selected"), 1, "The root's \"selected\" attribute should return 1 (indicating fully selected).");
// Deselect all children and confirm the results
root.get("selection").each(function (child) {
child.set("selected", 0);
});
Y.Assert.isNull(root.get("selection"), "The root's \"selection\" attribute should return null.");
Y.Assert.areEqual(root.get("selected"), 0, "The root's \"selected\" attribute should return 0");
// Select all children via selectAll() to confirm all
// children are represented in the parent's selection and
// that the parent's "selected" attribute returns 1.
root.selectAll();
Y.Assert.areEqual(root.get("selected"), 1, "The root's \"selected\" attribute should return 1");
Y.Assert.areEqual(root.get("selection").size(), 4, "The root's \"selection\" attribute should return an ArrayList with a size of 4.");
// Deselect all children via deselectAll() to confirm all
// children are no longer selected and that the parent's
// selection is empty.
root.deselectAll();
Y.Assert.areEqual(root.get("selected"), 0, "The root's \"selected\" attribute should return 0");
Y.Assert.areEqual(root.get("selection"), null, "The root's \"selection\" attribute should return an ArrayList with a size of 4.");
root.add({ type: Y.ParentWidget, id: "subtree", label: "Subtree", children: [
{ type: Y.ChildWidget, id: "subtree-leaf-1", label: "Subtree - Leaf One" },
{ type: Y.ChildWidget, id: "subtree-leaf-2", label: "Subtree - Leaf Two" },
{ type: Y.ChildWidget, id: "subtree-leaf-3", label: "Subtree - Leaf Three" },
{ type: Y.ChildWidget, id: "subtree-leaf-4", label: "Subtree - Leaf Four" }
]});
root.render();
Y.Assert.isTrue(root.item(4).get("multiple"), "The \"multiple\" attribute of the nested parent widget should be true.");
root.destroy();
},
testAncestorNavigation: function () {
var widget = new Y.ParentWidget({
id: "widget-1",
label: "Parent",
children: [
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
]
});
var root = new Y.RootWidget({ id: "rootwidget-1" });
root.add(widget);
root.render();
// Confirm that the "root" attribute is
// is not presently constrained by type since the ROOT_TYPE
// property is not yet set.
Y.Assert.areEqual(widget.item(0).get("root").get("id"), "rootwidget-1", "The \"root\" attribute of \"child-1\" should return a reference to rootwidget-1");
Y.Assert.areEqual(widget.get("root").get("id"), "rootwidget-1", "The \"root\" attribute of \"widget-1\" should return a reference to rootwidget-1");
Y.Assert.isFalse(widget.isRoot(), "\"widget-1\" should not be considered root.");
Y.Assert.areEqual(widget.item(0).get("depth"), 1, "The \"depth\" attribute of \"child-1\" should return 1.");
Y.Assert.areEqual(widget.item(0).ancestor(0).get("id"), "widget-1", "The ancestor of \"child-1\" at depth 0 should be \"widget-1\"");
Y.Assert.areEqual(widget.item(0).ancestor(-1).get("id"), "rootwidget-1", "The ancestor of \"child-1\" at depth -1 should be \"rootwidget-1\"");
// Confirm that the root attribute will be constrained to
// a particular type when the ROOT_TYPE is defined.
Y.ParentWidget.prototype.ROOT_TYPE = Y.ParentWidget;
Y.ChildWidget.prototype.ROOT_TYPE = Y.ParentWidget;
Y.Assert.areEqual(widget.item(0).get("root").get("id"), "widget-1", "The \"root\" attribute of \"child-1\" should return a reference to widget-1");
Y.Assert.areEqual(widget.get("root").get("id"), "widget-1", "The \"root\" attribute of \"widget-1\" should return a reference to widget-1");
Y.Assert.isTrue(widget.isRoot(), "Calling the \"isRoot\" method on \"widget-1\" should return \"widget-1\"");
Y.Assert.areEqual(widget.item(0).get("depth"), 0, "The \"depth\" attribute of \"child-1\" should return 0");
Y.Assert.areEqual(widget.item(0).ancestor(-1).get("id"), "widget-1", "The ancestor of \"child-1\" at depth -1 should be \"widget-1\""); // parent depth
root.destroy();
// So tests can be rerun
Y.ParentWidget.prototype.ROOT_TYPE = null;
Y.ChildWidget.prototype.ROOT_TYPE = null;
},
testSiblingNavigation: function () {
var widget = new Y.ParentWidget({
id: "widget-1",
children: [
{ type: Y.ChildWidget, label: "Child One", id: "child-1" },
{ type: Y.ChildWidget, label: "Child Two", id: "child-2" },
{ type: Y.ChildWidget, label: "Child Three", id: "child-3" },
{ type: Y.ChildWidget, label: "Child Four", id: "child-4" }
]
});
widget.render();
Y.Assert.areEqual(widget.item(3).get("index"), 3, "The \"index\" attribute of \"child-4\" should return 3.");
Y.Assert.isUndefined(widget.item(3).next(), "\"child-4\" should have no next sibling");
Y.Assert.areEqual(widget.item(3).next(true).get("id"), "child-1", "Calling the \"next\" method with the circular flag should return a reference to \"child-1\"");
Y.Assert.isUndefined(widget.item(0).previous(), "\"child-1\" should have no previous sibling");
Y.Assert.areEqual(widget.item(0).previous(true).get("id"), "child-4", "Calling the \"previous\" method with the circular flag should return a reference to \"child-4\"");
widget.destroy();
},
testActiveDescendant: function () {
var tree = new Y.ParentWidget({
id: "tree",
children: [
{ type: Y.ChildWidget, id: "leaf-1", label: "Leaf One" },
{ type: Y.ChildWidget, id: "leaf-2", label: "Leaf Two" },
{ type: Y.ParentWidget, id: "subtree", label: "Subtree", children: [
{ type: Y.ChildWidget, id: "subtree-leaf-1", label: "Subtree - Leaf One" },
{ type: Y.ChildWidget, id: "subtree-leaf-2", label: "Subtree - Leaf Two" },
{ type: Y.ChildWidget, id: "subtree-leaf-3", label: "Subtree - Leaf Three" },
{ type: Y.ChildWidget, id: "subtree-leaf-4", label: "Subtree - Leaf Four" }
]}
]
});
tree.render();
tree.item(2).item(0).focus();
Y.Assert.areEqual(tree.item(2).get("activeDescendant"), tree.item(2).item(0), "The \"activeDescendant\" attribute of \"subtree\" should return a reference to \"subtree-leaf-1\"");
Y.Assert.areEqual(tree.get("activeDescendant"), tree.item(2).item(0), "The \"activeDescendant\" attribute of the root widget should return a reference to the \"subtree\"");
tree.destroy();
},
testFullDestroy: function () {
var expected = ["leaf-1", "leaf-2", "subtree-leaf-1", "subtree-leaf-2", "subtree", "tree"];
var destroyed = [];
var tree = new Y.ParentWidget({
id: "tree",
children: [
{ type: Y.ChildWidget, id: "leaf-1", label: "Leaf One" },
{ type: Y.ChildWidget, id: "leaf-2", label: "Leaf Two" },
{ type: Y.ParentWidget, id: "subtree", label: "Subtree", children: [
{ type: Y.ChildWidget, id: "subtree-leaf-1", label: "Subtree - Leaf One" },
{ type: Y.ChildWidget, id: "subtree-leaf-2", label: "Subtree - Leaf Two" }
]}
]
});
tree.render();
// We're only doing this the long way, because the event bubbling doesn't
// seem to be working (subtree is missing). Otherwise we would use tree.on("*:destroy");
tree.each(function(child) {
var id = child.get("id");
child.after("destroy", function(e) {
destroyed.push(id);
Y.Assert.isNull(Y.Node.one("#" + id), "Child bounding box still in DOM");
});
if (child.hasImpl(Y.WidgetParent)) {
child.each(function(subtreechild) {
var id = subtreechild.get("id");
subtreechild.after("destroy", function(e) {
destroyed.push(id);
Y.Assert.isNull(Y.Node.one("#" + id), "Subtree child Bounding box still in DOM");
});
});
}
});
var rootId = tree.get("id");
tree.after("destroy", function(e) {
destroyed.push(rootId);
Y.Assert.isNull(Y.Node.one("#" + rootId), "Root Bounding box still in DOM");
});
tree.destroy();
Y.ArrayAssert.itemsAreEqual(expected, destroyed, "Unexpected destroy events");
},
testRemoveChildOnDestroy: function () {
var tree = new Y.ParentWidget({
id: "tree",
children: [
{ type: Y.ChildWidget, id: "leaf-1", label: "Leaf One" },
{ type: Y.ChildWidget, id: "leaf-2", label: "Leaf Two" },
{ type: Y.ParentWidget, id: "subtree", label: "Subtree", children: [
{ type: Y.ChildWidget, id: "subtree-leaf-1", label: "Subtree - Leaf One" },
{ type: Y.ChildWidget, id: "subtree-leaf-2", label: "Subtree - Leaf Two" }
]}
]
});
tree.render();
var subtree = tree.item(2);
Y.Assert.areEqual(3, tree.size(), "Initial tree size is incorrect");
Y.Assert.areEqual(2, subtree.size(), "Initial tree size is incorrect");
tree.item(1).destroy();
Y.Assert.areEqual(2, tree.size(), "Item 1 doesn't seem to have been removed. Tree size is the same");
Y.Assert.areEqual("subtree", tree.item(1).get("id"), "Item 1 doesn't seem to have been removed");
subree = tree.item(1);
subtree.item(0).destroy();
Y.Assert.areEqual(1, subtree.size(), "Subtree item 0 doesn't seem to have been removed. subtree size is the same");
Y.Assert.areEqual("subtree-leaf-2", subtree.item(0).get("id"), "Subtree item 0 doesn't seem to have been removed");
Y.Assert.areEqual(subtree.item(1), null, "Subtree item 1 should have moved up to item 0");
tree.destroy();
},
testUIEvents : function() {
var expectedEvents = [
"parentwidget:click",
"parentwidget:click"
],
actualEvents = [],
expectedChildEvents = [
"childwidget:click",
"childwidget:click"
],
actualChildEvents = [];
var widget = new Y.ParentWidget({
id: "widget-1",
children: [
{ type: Y.ChildWidget, label: '<span class="uitarget">Child One - Click Target</span>', id: "child-1" },
{ type: Y.ChildWidget, label: '<span class="uitarget">Child Two - Click Target</span>', id: "child-2" }
]
});
widget.render();
widget.on("click", function(e) {
actualEvents.push(e.type);
});
widget.item(0).on("click", function(e) {
actualChildEvents.push(e.type);
});
widget.item(1).on("click", function(e) {
actualChildEvents.push(e.type);
});
Y.Node.one("#child-1 .uitarget").simulate("click");
Y.Node.one("#child-2 .uitarget").simulate("click");
Y.ArrayAssert.itemsAreEqual(expectedEvents, actualEvents, "Unexpected UI events");
Y.ArrayAssert.itemsAreEqual(expectedChildEvents, actualChildEvents, "Unexpected Child UI events");
widget.destroy();
},
testCustomAsyncRender : function() {
// NOTE: This is not a unit test for "out-of-the-box" code per-se.
// It's testing an additional check for "rendered", added to _uiAddChild, specifically
// for a customization which modified child.render() to be async.
// See http://yuilibrary.com/projects/yui3/ticket/2529863 for more details.
var test = this;
var AsyncRenderParent = Y.Base.create("asyncParent", Y.Widget, [Y.WidgetParent], {
add: function() {
var parent = this;
parent.renderQ = new Y.AsyncQueue();
Y.WidgetParent.prototype.add.apply(parent, arguments);
parent.renderQ.after("complete", function() {
parent.fire("asyncAddComplete");
});
parent.renderQ.run();
}
});
var AsyncRenderChild = Y.Base.create("asyncChild", Y.Widget, [Y.WidgetChild], {
render: function() {
var w = this,
args = arguments;
this.get("parent").renderQ.add(function() {
AsyncRenderChild.superclass.render.apply(w, args);
});
}
});
var widget = new AsyncRenderParent({
id:"widget-1"
});
widget.render();
widget.after("asyncAddComplete", function(e) {
test.resume(function() {
Y.Assert.areEqual(widget.item(0).get("id"), "child-1", "child-1 is out of order");
Y.Assert.areEqual(widget.item(1).get("id"), "child-2", "child-2 is out of order");
Y.Assert.areEqual(widget.item(2).get("id"), "child-3", "child-3 is out of order");
var renderedChildren = widget.get("contentBox").get("children");
Y.Assert.areEqual(renderedChildren.item(0).get("id"), "child-1", "child-1 is out of order");
Y.Assert.areEqual(renderedChildren.item(1).get("id"), "child-2", "child-2 is out of order");
Y.Assert.areEqual(renderedChildren.item(2).get("id"), "child-3", "child-3 is out of order");
widget.destroy();
});
});
widget.add([
{type: AsyncRenderChild, label: "Child One", id: "child-1"},
{type: AsyncRenderChild, label: "Child Two", id: "child-2"},
{type: AsyncRenderChild, label: "Child Three", id: "child-3"}
]);
test.wait();
}
}));
Y.Test.Runner.setName("Widget ParentChild Tests");
Y.Test.Runner.add(suite);
Y.Test.Runner.disableLogging();
Y.Test.Runner.run();
var console;
Y.one("#btnRun").set("disabled", false).on("click", function() {
if (!console) {
console = new Y.Console({
id:"console",
width:"100%",
height:"90%",
verbose : false,
printTimeout: 0,
newestOnTop : false,
entryTemplate: '<pre class="{entry_class} {cat_class} {src_class}">'+
'<span class="{entry_cat_class}">{label}</span>'+
'<span class="{entry_content_class}">{message}</span>'+
'</pre>'
}).render();
}
Y.Test.Runner.enableLogging();
Y.Test.Runner.run();
});
});
</script>
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
</body>
</html>