Skip to content

Commit 8e1290c

Browse files
committed
Merge pull request #331 from PolymerElements/fix-focus-tests
clean up focus/blur tests
2 parents cc40add + eb4c38a commit 8e1290c

File tree

3 files changed

+84
-63
lines changed

3 files changed

+84
-63
lines changed

paper-input-behavior.html

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,8 @@
365365

366366
listeners: {
367367
'addon-attached': '_onAddonAttached',
368-
'focus': '_onFocus'
369368
},
370369

371-
observers: [
372-
'_focusedControlStateChanged(focused)'
373-
],
374-
375370
keyBindings: {
376371
'shift+tab:keydown': '_onShiftTabDown'
377372
},
@@ -440,12 +435,17 @@
440435
},
441436

442437
/**
443-
* Forward focus to inputElement
438+
* Forward focus to inputElement. Overriden from IronControlState.
444439
*/
445-
_onFocus: function() {
446-
if (!this._shiftTabPressed) {
440+
_focusBlurHandler: function(event) {
441+
if (this._shiftTabPressed)
442+
return;
443+
444+
Polymer.IronControlState._focusBlurHandler.call(this, event);
445+
446+
// Forward the focus to the nested input.
447+
if (this.focused)
447448
this._focusableElement.focus();
448-
}
449449
},
450450

451451
/**
@@ -497,24 +497,6 @@
497497
return placeholder || alwaysFloatLabel;
498498
},
499499

500-
_focusedControlStateChanged: function(focused) {
501-
// IronControlState stops the focus and blur events in order to redispatch them on the host
502-
// element, but paper-input-container listens to those events. Since there are more
503-
// pending work on focus/blur in IronControlState, I'm putting in this hack to get the
504-
// input focus state working for now.
505-
if (!this.$.container) {
506-
this.$.container = Polymer.dom(this.root).querySelector('paper-input-container');
507-
if (!this.$.container) {
508-
return;
509-
}
510-
}
511-
if (focused) {
512-
this.$.container._onFocus();
513-
} else {
514-
this.$.container._onBlur();
515-
}
516-
},
517-
518500
_updateAriaLabelledBy: function() {
519501
var label = Polymer.dom(this.root).querySelector('label');
520502
if (!label) {

test/paper-input.html

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,51 @@
196196
input = fixture('basic');
197197
});
198198

199-
test('focus/blur events fired on host element', function() {
200-
var nFocusEvents = 0;
201-
var nBlurEvents = 0;
199+
// At the moment, it is very hard to correctly fire exactly
200+
// one focus/blur events on a paper-input. This is because
201+
// when a paper-input is focused, it needs to focus
202+
// its underlying native input, which will also fire a `blur`
203+
// event.
204+
test('focus events fired on host element', function() {
202205
input.addEventListener('focus', function(event) {
203-
nFocusEvents += 1;
204206
assert(input.focused, 'input is focused');
205-
MockInteractions.blur(input.inputElement);
206207
});
207-
input.addEventListener('blur', function() {
208-
nBlurEvents += 1;
209-
assert(!input.focused, 'input is blurred');
208+
MockInteractions.focus(input);
209+
});
210+
211+
test('focus events fired on host element if nested element is focused', function() {
212+
input.addEventListener('focus', function(event) {
213+
assert(input.focused, 'input is focused');
210214
});
211215
MockInteractions.focus(input.inputElement);
212-
assert.isTrue(nFocusEvents >= 1, 'focus event fired');
213-
assert.isTrue(nBlurEvents >= 1, 'blur event fired');
214216
});
215217

218+
test('blur events fired on host element', function() {
219+
MockInteractions.focus(input);
220+
input.addEventListener('blur', function(event) {
221+
assert(!input.focused, 'input is blurred');
222+
});
223+
MockInteractions.blur(input);
224+
});
225+
226+
test('blur events fired on host element nested element is blurred', function() {
227+
MockInteractions.focus(input);
228+
input.addEventListener('blur', function(event) {
229+
assert(!input.focused, 'input is blurred');
230+
});
231+
MockInteractions.blur(input.inputElement);
232+
});
233+
234+
test('focusing then bluring sets the focused attribute correctly', function() {
235+
MockInteractions.focus(input);
236+
assert(input.focused, 'input is focused');
237+
MockInteractions.blur(input);
238+
assert(!input.focused, 'input is blurred');
239+
MockInteractions.focus(input.inputElement);
240+
assert(input.focused, 'input is focused');
241+
MockInteractions.blur(input.inputElement);
242+
assert(!input.focused, 'input is blurred');
243+
});
216244
});
217245

218246
suite('focused styling (integration test)', function() {

test/paper-textarea.html

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232
</template>
3333
</test-fixture>
3434

35-
<test-fixture id="has-tabindex">
36-
<template>
37-
<paper-textarea tabindex="0"></paper-textarea>
38-
</template>
39-
</test-fixture>
40-
4135
<test-fixture id="label">
4236
<template>
4337
<paper-textarea label="foo"></paper-textarea>
@@ -139,33 +133,50 @@
139133
input = fixture('basic');
140134
});
141135

142-
test('focus/blur events fired on host element', function() {
143-
var nFocusEvents = 0;
144-
var nBlurEvents = 0;
136+
// At the moment, it is very hard to correctly fire exactly
137+
// one focus/blur events on a paper-textarea. This is because
138+
// when a paper-textarea is focused, it needs to focus
139+
// its underlying native textarea, which will also fire a `blur`
140+
// event.
141+
test('focus events fired on host element', function() {
145142
input.addEventListener('focus', function(event) {
146-
nFocusEvents += 1;
147143
assert(input.focused, 'input is focused');
148-
MockInteractions.blur(input.inputElement.textarea);
149144
});
150-
input.addEventListener('blur', function() {
151-
nBlurEvents += 1;
152-
assert(!input.focused, 'input is blurred');
145+
MockInteractions.focus(input);
146+
});
147+
148+
test('focus events fired on host element if nested element is focused', function() {
149+
input.addEventListener('focus', function(event) {
150+
assert(input.focused, 'input is focused');
153151
});
154152
MockInteractions.focus(input.inputElement.textarea);
155-
assert.isTrue(nFocusEvents >= 1, 'focus event fired');
156-
assert.isTrue(nBlurEvents >= 1, 'blur event fired')
157153
});
158154

159-
test('focus a textarea with tabindex', function(done) {
160-
var input = fixture('has-tabindex');
161-
flush(function() {
162-
assert.notEqual(document.activeElement, input._focusableElement);
163-
MockInteractions.focus(input);
164-
setTimeout(function() {
165-
assert.equal(document.activeElement, input.shadowRoot ? input : input._focusableElement);
166-
done();
167-
}, 1);
168-
})
155+
test('blur events fired on host element', function() {
156+
MockInteractions.focus(input);
157+
input.addEventListener('blur', function(event) {
158+
assert(!input.focused, 'input is blurred');
159+
});
160+
MockInteractions.blur(input);
161+
});
162+
163+
test('blur events fired on host element nested element is blurred', function() {
164+
MockInteractions.focus(input);
165+
input.addEventListener('blur', function(event) {
166+
assert(!input.focused, 'input is blurred');
167+
});
168+
MockInteractions.blur(input.inputElement.textarea);
169+
});
170+
171+
test('focusing then bluring sets the focused attribute correctly', function() {
172+
MockInteractions.focus(input);
173+
assert(input.focused, 'input is focused');
174+
MockInteractions.blur(input);
175+
assert(!input.focused, 'input is blurred');
176+
MockInteractions.focus(input.inputElement.textarea);
177+
assert(input.focused, 'input is focused');
178+
MockInteractions.blur(input.inputElement.textarea);
179+
assert(!input.focused, 'input is blurred');
169180
});
170181
});
171182

0 commit comments

Comments
 (0)