4
<title>YUI focus/blur synth Tests</title>
6
<script src="../../../build/yui/yui.js"></script>
7
<script src="window-focus.js"></script>
9
<body class="yui3-skin-sam">
12
<div class="not-focusable">
13
<button id="button-1">Click Me!</button>
14
<a id="anchor-1" href="http://www.yahoo.com">Click Me!</a>
15
<input type="text" id="text-1">
21
//lazyEventFacade: true,
22
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min',
24
}).use('console', 'test', 'event', 'event-simulate', 'node-event-delegate', 'window-focus', function (Y) {
26
var Assert = Y.Assert;
28
new Y.Console({ /*useBrowserConsole: true*/ }).render();
30
Y.Test.Runner.add(new Y.Test.Case({
31
name: "Event Focus And Blur Test",
35
//test_purge_focus: 2528244, // fixed
36
//test_purge_blur: 2528244 // fixed
40
tearDown: function () {
41
Y.one('#container').purge(true);
44
test_add_focus: function(){
51
onFocus = function(e) {
59
handle = Y.on('focus', onFocus, '#container');
61
Y.one('#button-1').focus();
63
if (Y.isWindowInFocus()) {
64
Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
65
Assert.areEqual(Y.one('#button-1'), target, "the target is the incorrect node, should be the actual focus target");
66
Assert.areEqual(Y.one('#container'), boundEl, "the default scope should be the bound element");
72
Y.one('#text-1').focus();
73
Y.one('#button-1').focus();
75
Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
76
Assert.areEqual(Y.one('#button-1'), target, "the target is the incorrect node, should be the actual focus target");
77
Assert.areEqual(Y.one('#container'), boundEl, "the default scope should be the bound element");
79
Y.one('#button-1').blur();
81
Y.log("Window is not focused.", "warn", "TestRunner");
88
test_remove_focus: function () {
92
onFocus = function(e) {
98
var handle = Y.on('focus', onFocus, '#container');
100
if (Y.isWindowInFocus()) {
101
Y.one('#button-1').focus();
103
Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
107
Y.one('#button-1').blur();
111
Y.one('#button-1').focus();
113
Assert.isFalse(foo, "container should not pickup the focus event after listener is removed");
115
Y.one('#button-1').blur();
117
Y.log("Window is not focused.", "warn", "TestRunner");
121
test_purge_focus: function () {
125
onFocus = function(e) {
131
if (Y.isWindowInFocus()) {
132
Y.on('focus', onFocus, '#container');
134
Y.one('#button-1').focus();
136
Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
138
Y.Event.purgeElement('#container', false, 'focus');
140
Y.one('#button-1').blur();
144
Y.one('#button-1').focus();
146
Assert.isFalse(foo, "container should not pickup the focus event after listener has been purged");
148
Y.log("Window is not focused.", "warn", "TestRunner");
152
test_bubble_order: function () {
154
doc = Y.one(Y.config.doc),
155
body = Y.one('body'),
156
container = Y.one('#container'),
159
function onFocus(e) {
163
if (Y.isWindowInFocus()) {
165
container.on('focus', onFocus),
166
doc.on('focus', onFocus),
167
body.on('focus', onFocus));
169
Y.one('#anchor-1').focus();
171
new Y.EventHandle(handles).detach();
173
Y.ArrayAssert.itemsAreSame([container, body, doc], from, "Incorrect bubble order");
175
Y.log("Window is not focused.", "warn", "TestRunner");
179
test_add_blur: function () {
186
onBlur = function(e) {
194
if (Y.isWindowInFocus()) {
195
handle = Y.on('blur', onBlur, '#container');
197
Y.one('#button-1').focus();
198
Y.one('#button-1').blur();
200
Assert.isTrue(foo, "simple blur fails, container should pickup the focus event");
201
Assert.areEqual(target, Y.one('#button-1'), "the target is the incorrect node, should be the actual blur target");
202
Assert.areEqual(boundEl, Y.one('#container'), "the default scope should be the bound element");
208
Y.one('#button-1').focus();
209
Y.one('#text-1').focus();
211
Assert.isTrue(foo, "simple blur fails, container should pickup the focus event");
212
Assert.areEqual(target, Y.one('#button-1'), "the target is the incorrect node, should be the actual blur target");
213
Assert.areEqual(boundEl, Y.one('#container'), "the default scope should be the bound element");
215
Y.log("Window is not focused.", "warn", "TestRunner");
219
test_remove_blur: function () {
221
onBlur = function(e) {
225
if (Y.isWindowInFocus()) {
226
var handle = Y.on('blur', onBlur, '#container');
228
Y.one('#button-1').focus();
229
Y.one('#button-1').blur();
231
Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
234
Y.one('#button-1').focus();
235
Y.one('#text-1').focus();
236
Assert.isFalse(foo, "container should not pickup the blur event after listener has been removed");
238
Y.log("Window is not focused.", "warn", "TestRunner");
242
test_purge_blur: function () {
244
onBlur = function(e) {
248
if (Y.isWindowInFocus()) {
249
Y.on('blur', onBlur, '#container');
251
Y.one('#button-1').focus();
252
Y.one('#button-1').blur();
253
Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
255
Y.Event.purgeElement('#container', false, 'blur');
256
Y.one('#button-1').focus();
257
Y.one('#text-1').focus();
258
Assert.isFalse(foo, "container should not pickup the blur event after listener has been purged");
260
Y.log("Window is not focused.", "warn", "TestRunner");
264
"test delegating with filter matching non-focusable elements": function () {
265
var button = Y.one('#button-1'),
266
container = Y.one('#container'),
267
notFocusable = Y.one('.not-focusable'),
270
if (Y.isWindowInFocus()) {
271
handle = container.delegate('focus', function (e) {
273
Assert.areSame(button, e.target);
274
Assert.areSame(notFocusable, e.currentTarget);
275
Assert.areSame(notFocusable, this);
276
Assert.areSame(container, e.container);
277
}, '.not-focusable');
279
Y.one('#button-1').focus();
283
Assert.isTrue(called);
288
"test stopping propagation for on() subs": function () {
290
outerCalled, innerCalled;
292
// Make sure button-1 isn't focused
293
Y.one('#anchor-1').focus();
295
handles.push(Y.one('#container').on('focus', function () {
298
handles.push(Y.one('.not-focusable').on('focus', function (e) {
303
Y.one('#button-1').focus();
305
new Y.EventHandle(handles).detach();
307
Assert.isTrue(innerCalled);
308
Assert.isUndefined(outerCalled);
311
"test stopping propagation for delegate() subs matching the same node": function () {
315
// Matches at the same level, stopProp should not prevent
316
// both delegate callbacks
318
Y.one('#container').delegate('focus', function (e) {
321
}, '.not-focusable'),
322
Y.one('doc').delegate('focus', function () {
324
}, '.not-focusable'));
326
Y.one('#anchor-1').focus();
328
new Y.EventHandle(handles).detach();
330
Assert.areSame(2, called);
333
"test stopping propagation for delegate() subs not matching the same node": function () {
337
// Matches at the same level, stopProp should not prevent
338
// both delegate callbacks
340
Y.one('#container').delegate('focus', function (e) {
343
}, '.not-focusable'),
344
Y.one('doc').delegate('focus', function () {
348
Y.one('#button-1').focus();
350
new Y.EventHandle(handles).detach();
352
Assert.areSame(1, called);
355
"test stopImmediatePropagation for on() and delegate() subs": function () {
360
Y.one('#container').delegate('focus', function (e) {
362
e.stopImmediatePropagation();
363
}, '.not-focusable'),
364
Y.one('doc').delegate('focus', function () {
366
}, '.not-focusable'));
368
Y.one('#anchor-1').focus();
370
new Y.EventHandle(handles).detach();
372
Assert.areSame(1, called);
375
"test callback order from a mix of subs": function () {
380
Y.one('#container').delegate('focus', function () {
382
}, '.not-focusable'),
383
Y.one('#button-1').on('focus', function () {
386
Y.one('doc').delegate('focus', function () {
388
}, '.not-focusable'),
389
Y.one('#container').on('focus', function () {
394
Y.one('#button-1').focus();
396
Y.ArrayAssert.itemsAreSame([1, 2, 3, 4], order);
401
Y.Test.Runner.setName("EventFocusBlur");