1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
2
"http://www.w3.org/TR/html4/strict.dtd">
6
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7
<title>YUI FocusManager Node Plugin Tests</title>
9
<style type="text/css">
13
background-color: #ccc;
14
border: solid 1px #333;
25
border: solid 1px #ffcc00;
26
background-color: yellow;
33
<body class="yui3-skin-sam">
37
<a href="#" class="focusable"><em>One</em></a>
39
<li><a href="#" class="focusable">One</a></li>
40
<li><a href="#" class="focusable">Two</a></li>
41
<li><a href="#" class="focusable">Three</a></li>
42
<li><a href="#" class="focusable">Four</a></li>
45
<li><a href="#" class="focusable" tabindex="0">Two</a></li>
46
<li><a href="#" class="focusable">Three</a></li>
47
<li><a href="#" class="focusable">Four</a></li>
48
<li><input type="button" name="button-1" value="Five" class="focusable" disabled></li>
49
<li><input type="button" name="button-2" value="Six" class="focusable"></li>
50
<li><button type="button" class="focusable" disabled><em>Seven</em></button></li>
51
<li><button type="button" class="focusable"><em><em>Eight</em></em></button></li>
56
<a href="#" class="focusable"><em>One</em></a>
58
<li><a id="anchor-1" href="#" class="focusable">One</a></li>
59
<li><a href="#" class="focusable">Two</a></li>
60
<li><a href="#" class="focusable">Three</a></li>
61
<li><a href="#" class="focusable">Four</a></li>
64
<li><a href="#" class="focusable">Two</a></li>
65
<li><a href="#" class="focusable">Three</a></li>
66
<li><a href="#" class="focusable">Four</a></li>
67
<li><input type="button" name="button-1" value="Five" class="focusable" disabled></li>
68
<li><input type="button" name="button-2" value="Six" class="focusable"></li>
69
<li><button type="button" class="focusable" disabled><em>Seven</em></button></li>
70
<li><button type="button" class="focusable"><em><em>Eight</em></em></button></li>
76
<script src="../../../build/yui/yui.js" type="text/javascript"></script>
77
<script type="text/javascript">
79
YUI({timeout: 10000, filter:'raw'}).use("node-event-simulate", "node-focusmanager", "test", "console", function(Y) {
81
Y.on("contentready", function () {
83
var myConsole = new Y.Console().render();
85
var testFocusManager = new Y.Test.Case({
87
name: "FocusManager Tests",
89
testActiveDescendant: function() {
92
// If no value is specified for the "activeDescendant"
93
// attribute, the value is interpreted from the markup
94
// by using the first descendant whose tabIndex attribute
97
var oList1 = Y.one("#list-1");
99
oList1.plug(Y.Plugin.NodeFocusManager, {
100
descendants: ">li>.focusable",
101
keys: { next: "down:40", previous: "down:38" },
102
focusClass: "focused",
106
Y.Assert.isTrue((oList1.focusManager.get("activeDescendant") === 1));
110
// If no value is specified for the "activeDescendant"
111
// attribute, the value is interpreted from the markup
112
// by using the first descendant whose tabIndex attribute
115
var oList2 = Y.one("#list-2");
117
oList2.plug(Y.Plugin.NodeFocusManager, {
118
descendants: ">li>.focusable",
119
keys: { next: "down:40", previous: "down:38" },
120
focusClass: "focused",
124
Y.Assert.isTrue((oList2.focusManager.get("activeDescendant") === 0));
127
// Clean up for next set of tests
128
oList1.unplug("focusManager");
129
oList2.unplug("focusManager");
133
// The value specified for the activeDescendant attribute
134
// via the configuration object, should supercede the
135
// value interpreted via the markup
136
oList1.plug(Y.Plugin.NodeFocusManager, {
137
descendants: ">li>.focusable",
139
keys: { next: "down:40", previous: "down:38" },
140
focusClass: "focused",
144
Y.Assert.isTrue((oList1.focusManager.get("activeDescendant") === 3));
148
// Setting the "activeDescendant" attribute to -1 should
149
// result in none of the descendants being focusable via
151
oList1.focusManager.set("activeDescendant", -1);
153
// Test to make sure that the "tabIndex" attribute of all
154
// descendants is set to "-1"
155
oList1.focusManager.get("descendants").each(function (node) {
156
Y.Assert.isTrue( (node.get("tabIndex") === -1) );
159
// Calling "focus" with no arguments should result in none
160
// of the descendants being focused
161
oList1.focusManager.focus();
162
Y.Assert.isFalse(oList1.focusManager.get("focused"));
166
// With the "activeDescendant" attribute set to "-1",
167
// the user can still focus a descendant by mousing
169
oList1.focusManager.get("descendants").item(0).simulate("mousedown");
170
Y.Assert.isTrue(oList1.focusManager.get("focused"));
174
// The "activeDescendant" attribute should return the
175
// index of the currently focused descendant
176
Y.Assert.isTrue((oList1.focusManager.get("activeDescendant") === 0));
180
// With the "activeDescendant" attribute set to "-1",
181
// the user can focus a descendant programmically by
182
// calling the "focus" method and passing in the index
183
// of the descendant to be focused
184
oList1.focusManager.set("activeDescendant", -1);
185
oList1.focusManager.focus(1);
186
Y.Assert.isTrue(oList1.focusManager.get("focused"));
188
// The "activeDescendant" attribute should return the
189
// index of the currently focused descendant
190
Y.Assert.isTrue((oList1.focusManager.get("activeDescendant") === 1));
194
// The "activeDescendant" attribute can also be set to a
196
oList1.focusManager.focus(oList1.focusManager.get("descendants").item(2));
197
Y.Assert.isTrue(oList1.focusManager.get("focused"));
199
// The "activeDescendant" attribute should return the
200
// index of the currently focused descendant
201
Y.Assert.isTrue((oList1.focusManager.get("activeDescendant") === 2));
205
// The "activeDescendant" attribute cannot be set to the
206
// index of a disabled Node...
207
oList1.focusManager.focus(4);
208
Y.Assert.isTrue((oList1.focusManager.get("activeDescendant") === 2));
210
// ...Or a disabled Node reference
211
oList1.focusManager.focus(oList1.focusManager.get("descendants").item(4));
212
Y.Assert.isTrue((oList1.focusManager.get("activeDescendant") === 2));
217
// There should be one element with the CSS class of
219
Y.Assert.isTrue((Y.all(".focused").size() === 1));
221
oList1.focusManager.blur();
223
// There should be no elements with the CSS class of
225
Y.Assert.isTrue((Y.all(".focused").size() === 0));
228
// Clean up for next set of tests
229
oList1.unplug("focusManager");
233
testDescendants: function() {
236
// Check to make sure that the "descendants" attribute
237
// returns a NodeList of the correct size
239
var oList1 = Y.one("#list-1");
241
oList1.plug(Y.Plugin.NodeFocusManager, {
242
descendants: ">li>.focusable",
243
keys: { next: "down:40", previous: "down:38" },
244
focusClass: "focused",
248
Y.Assert.isTrue((oList1.focusManager.get("descendants").size() === 8));
251
// Can update the "descendants" attribute on the fly
252
oList1.focusManager.set("descendants", ".focusable");
253
Y.Assert.isTrue((oList1.focusManager.get("descendants").size() === 12));
257
// Can modify the child nodes of the current owner Node
258
// of the FocusManager and call "refresh" to sync the
259
// number of descendants
260
var oSublist = Y.one("#list-1-2"),
261
oParentNode = oSublist.get("parentNode");
263
oParentNode.removeChild(oSublist);
265
oList1.focusManager.refresh();
266
Y.Assert.isTrue((oList1.focusManager.get("descendants").size() === 8));
268
// Clean up for next set of tests
269
oList1.unplug("focusManager");
273
// Start by plugging the Focus Manager into an empty
274
// container, then update the descendants and call
275
// the "refresh" method to update the managed descendants.
276
var oList3 = Y.one("#list-3");
278
oList3.plug(Y.Plugin.NodeFocusManager, {
279
descendants: ">li>.focusable",
280
keys: { next: "down:40", previous: "down:38" }
283
Y.Assert.isTrue((oList3.focusManager.get("descendants").size() === 0));
285
oList3.append('<li><a href="#" class="focusable">One</a></li><li><a href="#" class="focusable">Two</a></li><li><a href="#" class="focusable">Three</a></li><li><a href="#" class="focusable">Four</a></li>');
287
oList3.focusManager.refresh();
289
Y.Assert.isTrue((oList3.focusManager.get("descendants").size() === 4));
291
oList3.unplug("focusManager");
295
testFocusedAttribute: function() {
298
// The "focused" attribute should be false by default
299
var oList2 = Y.one("#list-2");
301
oList2.plug(Y.Plugin.NodeFocusManager, {
302
descendants: ">li>.focusable",
303
keys: { next: "down:40", previous: "down:38" },
304
focusClass: "focused",
308
Y.Assert.isFalse(oList2.focusManager.get("focused"));
312
// Calling the "focus" method with no arguments should
313
// result in the "focused" attribute being set to true
314
// and the "activeDescendant" attribute being 0
315
oList2.focusManager.focus();
316
Y.Assert.isTrue(oList2.focusManager.get("focused"));
317
Y.Assert.isTrue((oList2.focusManager.get("activeDescendant") === 0));
321
// Calling the "blur" method should result in the
322
// "focused" attribute being set to false, the
323
// removal of the the "focused" CSS class,
324
// and the "activeDescendant" attribute being still 0
325
oList2.focusManager.blur();
326
Y.Assert.isTrue((Y.all(".focused").size() === 0));
327
Y.Assert.isFalse(oList2.focusManager.get("focused"));
328
Y.Assert.isTrue((oList2.focusManager.get("activeDescendant") === 0));
332
// Focusing another element in the DOM will result in the
333
// "focused" attribute being set to false, the
334
// removal of the "focused" CSS class,
335
// and the "activeDescendant" attribute being still 0
336
oList2.focusManager.focus();
337
Y.one("#anchor-1").focus();
338
Y.Assert.isTrue((Y.all(".focused").size() === 0));
339
Y.Assert.isFalse(oList2.focusManager.get("focused"));
340
Y.Assert.isTrue((oList2.focusManager.get("activeDescendant") === 0));
342
// Clean up for next set of tests
343
oList2.unplug("focusManager");
347
testFocusClassAttribute: function() {
349
var oList1 = Y.one("#list-1");
351
oList1.plug(Y.Plugin.NodeFocusManager, {
352
descendants: ">li>.focusable",
353
keys: { next: "down:40", previous: "down:38" },
354
focusClass: "focused",
359
oList1.focusManager.focus();
361
// TEST #1: Make sure that the "focused" class is
362
// applied to the first descendant
364
var nodes = oList1.all(".focused");
366
Y.Assert.isTrue(nodes.size() === 1);
367
Y.Assert.isTrue(oList1.focusManager.get("descendants").item(0).hasClass("focused"));
369
oList1.focusManager.blur();
372
// TEST #2: Make sure that the "focused" class is not
373
// not applied to any of the descendants
375
nodes = oList1.all(".focused");
377
Y.Assert.isTrue(nodes.size() === 0);
378
Y.Assert.isFalse(oList1.focusManager.get("descendants").item(0).hasClass("focused"));
381
// TEST #3: Make sure that the "focused" class is
382
// applied to the parent node of the first descendant
384
oList1.focusManager.set("focusClass", {
385
className: "focused",
386
fn: function (node) {
387
return node.get("parentNode");
392
oList1.focusManager.focus();
394
nodes = oList1.all(".focused");
396
Y.Assert.isTrue(nodes.size() === 1);
397
Y.Assert.isTrue(oList1.focusManager.get("descendants").item(0).get("parentNode").hasClass("focused"));
400
// TEST #4: Make sure that the "focused" class is not
401
// not applied to any of the descendants
403
oList1.focusManager.blur();
405
nodes = oList1.all(".focused");
407
Y.Assert.isTrue(nodes.size() === 0);
408
Y.Assert.isFalse(oList1.focusManager.get("descendants").item(0).get("parentNode").hasClass("focused"));
415
Y.Test.Runner.add(testFocusManager);