jellyfin-web/dashboard-ui/bower_components/iron-overlay-behavior/test/iron-overlay-behavior.html
2016-03-30 22:00:05 -04:00

1022 lines
39 KiB
HTML

<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<title>iron-overlay-behavior tests</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes">
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<link rel="import" href="../../iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="test-overlay.html">
<link rel="import" href="test-overlay2.html">
<link rel="import" href="test-buttons.html">
<link rel="import" href="test-menu-button.html">
<style is="custom-style">
iron-overlay-backdrop {
/* For quicker tests */
--iron-overlay-backdrop: {
transition: none;
}
}
</style>
</head>
<body>
<test-fixture id="basic">
<template>
<test-overlay>
Basic Overlay
</test-overlay>
</template>
</test-fixture>
<test-fixture id="opened">
<template>
<test-overlay opened>
Basic Overlay
</test-overlay>
</template>
</test-fixture>
<test-fixture id="autofocus">
<template>
<test-overlay>
Autofocus
<button autofocus>button</button>
</test-overlay>
</template>
</test-fixture>
<test-fixture id="focusables">
<template>
<test-overlay tabindex="-1">
<h2>Focusables (no tabindex)</h2>
<div>
<input class="focusable1" placeholder="1 (nested)">
</div>
<button class="focusable2">1</button>
<button disabled> disabled button</button>
<div tabindex="-1">not focusable</div>
<button class="focusable3">2</button>
</test-overlay>
<test-overlay tabindex="-1">
<h2>Focusables (with tabindex)</h2>
<div tabindex="-1">not focusable</div>
<div tabindex="3" class="focusable3">3</div>
<div tabindex="4" class="focusable4">4</div>
<div tabindex="5" class="focusable5">5</div>
<div>
<div tabindex="1" class="focusable1">1 (nested)</div>
<div tabindex="6" class="focusable6">6 (nested)</div>
</div>
<div tabindex="2" class="focusable2">2</div>
</test-overlay>
</template>
</test-fixture>
<test-fixture id="backdrop">
<template>
<test-overlay with-backdrop>
Overlay with backdrop
</test-overlay>
</template>
</test-fixture>
<test-fixture id="multiple">
<template>
<test-overlay class="overlay-1">
Test overlay 1
</test-overlay>
<test-overlay class="overlay-2">
Test overlay 2
</test-overlay>
<test-overlay2 class="overlay-3">
Other overlay 3
</test-overlay2>
</template>
</test-fixture>
<test-fixture id="composed">
<template>
<test-menu-button>
Composed overlay
<button>Button</button>
</test-menu-button>
</template>
</test-fixture>
<test-buttons id="buttons"></test-buttons>
<input id="focusInput" placeholder="focus input">
<script>
function runAfterOpen(overlay, callback) {
overlay.addEventListener('iron-overlay-opened', function() {
Polymer.Base.async(callback, 1);
});
overlay.open();
}
function runAfterClose(overlay, callback) {
overlay.addEventListener('iron-overlay-closed', callback);
overlay.close();
}
suite('basic overlay', function() {
var overlay;
setup(function() {
overlay = fixture('basic');
});
test('overlay starts hidden', function() {
assert.isFalse(overlay.opened, 'overlay starts closed');
assert.equal(getComputedStyle(overlay).display, 'none', 'overlay starts hidden');
});
test('overlay open/close events', function(done) {
var nevents = 0;
overlay.addEventListener('iron-overlay-opened', function() {
nevents += 1;
overlay.opened = false;
});
overlay.addEventListener('iron-overlay-closed', function() {
nevents += 1;
assert.equal(nevents, 2, 'opened and closed events fired');
done();
});
overlay.opened = true;
});
test('closed overlay does not refit on iron-resize', function() {
var spy = sinon.spy(overlay, 'refit');
overlay.fire('iron-resize');
assert.isFalse(spy.called, 'overlay should not refit');
});
test('open() triggers iron-resize', function(done) {
var callCount = 0;
// Ignore iron-resize triggered by window resize.
window.addEventListener('resize', function() { callCount--; }, true);
overlay.addEventListener('iron-resize', function() { callCount++; });
runAfterOpen(overlay, function() {
assert.equal(callCount, 1, 'iron-resize called once before iron-overlay-opened');
done();
});
});
test('close() triggers iron-resize', function(done) {
runAfterOpen(overlay, function() {
var spy = sinon.stub();
overlay.addEventListener('iron-resize', spy);
runAfterClose(overlay, function() {
assert.equal(spy.callCount, 1, 'iron-resize called once before iron-overlay-closed');
done();
});
});
});
test('closed overlay does not trigger iron-resize when its content changes', function() {
// Ignore iron-resize triggered by window resize.
var callCount = 0;
window.addEventListener('resize', function() { callCount--; }, true);
overlay.addEventListener('iron-resize', function() { callCount++; });
Polymer.dom(overlay).appendChild(document.createElement('div'));
Polymer.dom.flush();
assert.equal(callCount, 0, 'iron-resize should not be called');
});
test('open overlay triggers iron-resize when its content changes', function() {
runAfterOpen(overlay, function() {
var spy = sinon.stub();
overlay.addEventListener('iron-resize', spy);
Polymer.dom(overlay).appendChild(document.createElement('div'));
Polymer.dom.flush();
assert.equal(spy.callCount, 1, 'iron-resize should be called once');
});
});
test('close an overlay quickly after open', function(done) {
// first, open the overlay
overlay.open();
overlay.async(function() {
// during the opening transition, close the overlay
this.close();
// wait for any exceptions to be thrown until the transition is done
this.async(function() {
done();
}, 300);
});
});
test('clicking an overlay does not close it', function(done) {
runAfterOpen(overlay, function() {
var spy = sinon.stub();
overlay.addEventListener('iron-overlay-closed', spy);
MockInteractions.tap(overlay);
overlay.async(function() {
assert.isFalse(spy.called, 'iron-overlay-closed should not fire');
done();
}, 10);
});
});
test('clicking outside fires iron-overlay-canceled', function(done) {
runAfterOpen(overlay, function() {
overlay.addEventListener('iron-overlay-canceled', function(event) {
assert.equal(event.detail.target, document.body, 'detail contains original click event');
done();
});
MockInteractions.tap(document.body);
});
});
test('clicking outside closes the overlay', function(done) {
runAfterOpen(overlay, function() {
overlay.addEventListener('iron-overlay-closed', function(event) {
assert.isTrue(event.detail.canceled, 'overlay is canceled');
done();
});
MockInteractions.tap(document.body);
});
});
test('iron-overlay-canceled event can be prevented', function(done) {
runAfterOpen(overlay, function() {
overlay.addEventListener('iron-overlay-canceled', function(event) {
event.preventDefault();
});
var spy = sinon.stub();
overlay.addEventListener('iron-overlay-closed', spy);
MockInteractions.tap(document.body);
Polymer.Base.async(function() {
assert.isTrue(overlay.opened, 'overlay is still open');
assert.isFalse(spy.called, 'iron-overlay-closed not fired');
done();
}, 10);
});
});
test('cancel an overlay with esc key', function(done) {
runAfterOpen(overlay, function() {
overlay.addEventListener('iron-overlay-canceled', function(event) {
assert.equal(event.detail.type, 'keydown');
done();
});
MockInteractions.pressAndReleaseKeyOn(document, 27);
});
});
test('close an overlay with esc key', function(done) {
runAfterOpen(overlay, function() {
overlay.addEventListener('iron-overlay-closed', function(event) {
assert.isTrue(event.detail.canceled, 'overlay is canceled');
done();
});
MockInteractions.pressAndReleaseKeyOn(document, 27);
});
});
test('no-cancel-on-outside-click property', function(done) {
overlay.noCancelOnOutsideClick = true;
runAfterOpen(overlay, function() {
var spy = sinon.stub();
overlay.addEventListener('iron-overlay-closed', spy);
MockInteractions.tap(document.body);
Polymer.Base.async(function() {
assert.isFalse(spy.called, 'iron-overlay-closed should not fire');
done();
}, 10);
});
});
test('no-cancel-on-esc-key property', function(done) {
overlay.noCancelOnEscKey = true;
runAfterOpen(overlay, function() {
var spy = sinon.stub();
overlay.addEventListener('iron-overlay-closed', spy);
MockInteractions.pressAndReleaseKeyOn(document, 27);
Polymer.Base.async(function() {
assert.isFalse(spy.called, 'iron-overlay-cancel should not fire');
done();
}, 10);
});
});
test('with-backdrop sets tabindex=-1 and removes it', function() {
overlay.withBackdrop = true;
assert.equal(overlay.getAttribute('tabindex'), '-1', 'tabindex is -1');
overlay.withBackdrop = false;
assert.isFalse(overlay.hasAttribute('tabindex'), 'tabindex removed');
});
test('with-backdrop does not override tabindex if already set', function() {
overlay.setAttribute('tabindex', '1');
overlay.withBackdrop = true;
assert.equal(overlay.getAttribute('tabindex'), '1', 'tabindex is 1');
overlay.withBackdrop = false;
assert.equal(overlay.getAttribute('tabindex'), '1', 'tabindex is still 1');
});
});
suite('keyboard event listener', function() {
var overlay;
var preventKeyDown = function(event) {
event.preventDefault();
event.stopPropagation();
}
suiteSetup(function() {
// Worst case scenario: listener with useCapture = true that prevents & stops propagation
// added before the overlay is initialized.
document.addEventListener('keydown', preventKeyDown, true);
});
setup(function() {
overlay = fixture('basic');
});
suiteTeardown(function() {
document.removeEventListener('keydown', preventKeyDown, true);
});
test('cancel an overlay with esc key even if event is prevented by other listeners', function(done) {
runAfterOpen(overlay, function() {
overlay.addEventListener('iron-overlay-canceled', function(event) {
done();
});
MockInteractions.pressAndReleaseKeyOn(document, 27);
});
});
});
suite('opened overlay', function() {
var overlay;
setup(function() {
overlay = fixture('opened');
});
test('overlay open by default', function(done) {
overlay.addEventListener('iron-overlay-opened', function() {
assert.isTrue(overlay.opened, 'overlay starts opened');
assert.notEqual(getComputedStyle(overlay).display, 'none', 'overlay starts showing');
done();
});
});
test('overlay positioned & sized properly', function(done) {
overlay.addEventListener('iron-overlay-opened', function() {
var s = getComputedStyle(overlay);
assert.closeTo(parseFloat(s.left), (window.innerWidth - overlay.offsetWidth) / 2, 1, 'centered horizontally');
assert.closeTo(parseFloat(s.top), (window.innerHeight - overlay.offsetHeight) / 2, 1, 'centered vertically');
done();
});
});
test('open overlay refits on iron-resize', function(done) {
var spy = sinon.spy(overlay, 'refit');
// At this point, overlay is still opening.
overlay.fire('iron-resize');
assert.isFalse(spy.called, 'overlay did not refit while animating');
overlay.addEventListener('iron-overlay-opened', function() {
overlay.fire('iron-resize');
assert.isTrue(spy.called, 'overlay did refit');
done();
});
});
});
suite('focus handling', function() {
var overlay;
setup(function() {
overlay = fixture('autofocus');
});
test('node with autofocus is focused', function(done) {
runAfterOpen(overlay, function() {
assert.equal(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> is focused');
done();
});
});
test('no-auto-focus will not focus node with autofocus', function(done) {
overlay.noAutoFocus = true;
runAfterOpen(overlay, function() {
assert.notEqual(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> not focused after opened');
done();
});
// In Safari the element with autofocus will immediately receive focus when displayed for the first time http://jsbin.com/woroci/2/
// Ensure this is not the case for overlay.
assert.notEqual(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> not immediately focused');
});
test('no-cancel-on-outside-click property; focus stays on overlay when click outside', function(done) {
overlay.noCancelOnOutsideClick = true;
runAfterOpen(overlay, function() {
MockInteractions.tap(document.body);
Polymer.Base.async(function() {
assert.equal(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> is focused');
done();
}, 10);
});
});
test('with-backdrop traps the focus within the overlay', function(done) {
var focusSpy = sinon.stub();
var button = document.createElement('button');
document.body.appendChild(button);
button.addEventListener('focus', focusSpy, true);
overlay.withBackdrop = true;
runAfterOpen(overlay, function() {
// Try to steal the focus
MockInteractions.focus(button);
assert.equal(Polymer.dom(overlay).querySelector('[autofocus]'), document.activeElement, '<button autofocus> is focused');
assert.equal(focusSpy.callCount, 0, 'button in body did not get the focus');
document.body.removeChild(button);
done();
});
});
});
suite('focusable nodes', function() {
var overlay, overlayWithTabIndex;
setup(function() {
var f = fixture('focusables');
overlay = f[0];
overlayWithTabIndex = f[1];
});
test('_focusableNodes returns nodes that are focusable', function() {
var focusableNodes = overlay._focusableNodes;
assert.equal(focusableNodes.length, 3, '3 nodes are focusable');
assert.equal(focusableNodes[0], Polymer.dom(overlay).querySelector('.focusable1'));
assert.equal(focusableNodes[1], Polymer.dom(overlay).querySelector('.focusable2'));
assert.equal(focusableNodes[2], Polymer.dom(overlay).querySelector('.focusable3'));
});
test('_focusableNodes includes overlay if it has a valid tabindex', function() {
overlay.setAttribute('tabindex', '0');
var focusableNodes = overlay._focusableNodes;
assert.equal(focusableNodes.length, 4, '4 focusable nodes');
assert.notEqual(focusableNodes.indexOf(overlay), -1, 'overlay is included');
});
test('_focusableNodes respects the tabindex order', function() {
var focusableNodes = overlayWithTabIndex._focusableNodes;
assert.equal(focusableNodes.length, 6, '6 nodes are focusable');
assert.equal(focusableNodes[0], Polymer.dom(overlayWithTabIndex).querySelector('.focusable1'));
assert.equal(focusableNodes[1], Polymer.dom(overlayWithTabIndex).querySelector('.focusable2'));
assert.equal(focusableNodes[2], Polymer.dom(overlayWithTabIndex).querySelector('.focusable3'));
assert.equal(focusableNodes[3], Polymer.dom(overlayWithTabIndex).querySelector('.focusable4'));
assert.equal(focusableNodes[4], Polymer.dom(overlayWithTabIndex).querySelector('.focusable5'));
assert.equal(focusableNodes[5], Polymer.dom(overlayWithTabIndex).querySelector('.focusable6'));
});
test('with-backdrop: TAB & Shift+TAB wrap focus', function(done) {
overlay.withBackdrop = true;
var focusableNodes = overlay._focusableNodes;
runAfterOpen(overlay, function() {
// Go to last element.
MockInteractions.focus(focusableNodes[focusableNodes.length-1]);
// Simulate TAB & focus out of overlay.
MockInteractions.pressAndReleaseKeyOn(document, 9);
MockInteractions.focus(document.body);
assert.equal(focusableNodes[0], document.activeElement, 'focus wrapped to first focusable');
// Simulate Shift+TAB & focus out of overlay.
MockInteractions.pressAndReleaseKeyOn(document, 9, ['shift']);
MockInteractions.focus(document.body);
assert.equal(focusableNodes[focusableNodes.length-1], document.activeElement, 'focus wrapped to last focusable');
done();
});
});
test('with-backdrop: TAB & Shift+TAB wrap focus respecting tabindex', function(done) {
overlayWithTabIndex.withBackdrop = true;
var focusableNodes = overlayWithTabIndex._focusableNodes;
runAfterOpen(overlayWithTabIndex, function() {
// Go to last element.
MockInteractions.focus(focusableNodes[focusableNodes.length-1]);
// Simulate TAB & focus out of overlay.
MockInteractions.pressAndReleaseKeyOn(document, 9);
MockInteractions.focus(document.body);
assert.equal(focusableNodes[0], document.activeElement, 'focus wrapped to first focusable');
// Simulate Shift+TAB & focus out of overlay.
MockInteractions.pressAndReleaseKeyOn(document, 9, ['shift']);
MockInteractions.focus(document.body);
assert.equal(focusableNodes[focusableNodes.length-1], document.activeElement, 'focus wrapped to last focusable');
done();
});
});
});
suite('Polymer.IronOverlayManager.deepActiveElement', function() {
test('handles document.body', function () {
document.body.focus();
assert.equal(Polymer.IronOverlayManager.deepActiveElement, document.body);
});
test('handles light dom', function () {
var focusable = document.getElementById('focusInput');
focusable.focus();
assert.equal(Polymer.IronOverlayManager.deepActiveElement, focusable, 'input is handled');
focusable.blur();
});
test('handles shadow dom', function () {
var focusable = document.getElementById('buttons').$.button0;
focusable.focus();
assert.equal(Polymer.IronOverlayManager.deepActiveElement, focusable);
focusable.blur();
});
});
suite('restore-focus-on-close', function() {
var overlay;
setup(function () {
overlay = fixture('autofocus');
overlay.restoreFocusOnClose = true;
});
teardown(function () {
// No matter what, return the focus to body!
document.body.focus();
});
test('does not return focus on close by default (restore-focus-on-close=false)', function(done) {
overlay.restoreFocusOnClose = false;
var focusable = document.getElementById('focusInput');
focusable.focus();
runAfterOpen(overlay, function() {
runAfterClose(overlay, function() {
assert.notEqual(Polymer.IronOverlayManager.deepActiveElement, focusable, 'focus is not restored to focusable');
done();
});
});
});
test('overlay returns focus on close', function(done) {
var focusable = document.getElementById('focusInput');
focusable.focus();
runAfterOpen(overlay, function() {
runAfterClose(overlay, function() {
assert.equal(Polymer.IronOverlayManager.deepActiveElement, focusable, 'focus restored to focusable');
done();
});
});
});
test('overlay returns focus on close (ShadowDOM)', function(done) {
var focusable = document.getElementById('buttons').$.button0;
focusable.focus();
runAfterOpen(overlay, function() {
runAfterClose(overlay, function() {
assert.equal(Polymer.IronOverlayManager.deepActiveElement, focusable, 'focus restored to focusable');
done();
});
});
});
test('overlay does not return focus to elements contained in another overlay', function(done) {
var overlay2 = fixture('basic');
// So it doesn't interfere with focus changes.
overlay2.noAutoFocus = true;
var focusable = document.createElement('input');
runAfterOpen(overlay2,function () {
Polymer.dom(overlay2).appendChild(focusable);
focusable.focus();
runAfterOpen(overlay, function() {
runAfterClose(overlay, function() {
assert.notEqual(Polymer.IronOverlayManager.deepActiveElement, focusable, 'focus not restored to focusable inside overlay2');
done();
});
});
});
});
test('overlay does not return focus to elements that are not in the body anymore', function(done) {
var focusable = document.createElement('input');
document.body.appendChild(focusable);
focusable.focus();
var focusSpy = sinon.spy(focusable, 'focus');
runAfterOpen(overlay, function() {
document.body.removeChild(focusable);
runAfterClose(overlay, function() {
assert.isFalse(focusSpy.called, 'focus not called');
done();
});
});
});
});
suite('overlay with backdrop', function() {
var overlay;
setup(function() {
overlay = fixture('backdrop');
});
test('backdrop is opened when overlay is opened', function(done) {
assert.isDefined(overlay.backdropElement, 'backdrop is defined');
runAfterOpen(overlay, function() {
assert.isTrue(overlay.backdropElement.opened, 'backdrop is opened');
assert.isDefined(overlay.backdropElement.parentNode, 'backdrop is inserted in the DOM');
done();
});
});
test('backdrop appears behind the overlay', function(done) {
runAfterOpen(overlay, function() {
styleZ = parseInt(window.getComputedStyle(overlay).zIndex, 10);
backdropStyleZ = parseInt(window.getComputedStyle(overlay.backdropElement).zIndex, 10);
assert.isTrue(styleZ > backdropStyleZ, 'overlay has higher z-index than backdrop');
done();
});
});
test('backdrop is removed when overlay is closed', function(done) {
runAfterOpen(overlay, function() {
runAfterClose(overlay, function() {
assert.isFalse(overlay.backdropElement.opened, 'backdrop is closed');
assert.isNotOk(overlay.backdropElement.parentNode, 'backdrop is removed from the DOM');
assert.lengthOf(document.querySelectorAll('iron-overlay-backdrop'), 0, 'no backdrop elements on body');
done();
});
});
});
test('backdrop is removed when the element is removed from DOM', function(done) {
runAfterOpen(overlay, function() {
Polymer.dom(overlay).parentNode.removeChild(overlay);
// Ensure detached is executed.
Polymer.dom.flush();
assert.isFalse(overlay.backdropElement.opened, 'backdrop is closed');
assert.isNotOk(overlay.backdropElement.parentNode, 'backdrop is removed from the DOM');
assert.lengthOf(document.querySelectorAll('iron-overlay-backdrop'), 0, 'no backdrop elements on body');
assert.isNotOk(overlay._manager.currentOverlay(), 'currentOverlay ok');
done();
});
});
test('manager.getBackdrops() immediately updated on opened changes', function() {
overlay.opened = true;
assert.equal(Polymer.IronOverlayManager.getBackdrops().length, 1, 'overlay added to manager backdrops');
overlay.opened = false;
assert.equal(Polymer.IronOverlayManager.getBackdrops().length, 0, 'overlay removed from manager backdrops');
});
test('updating with-backdrop to false closes backdrop', function(done) {
runAfterOpen(overlay, function() {
overlay.withBackdrop = false;
assert.isFalse(overlay.backdropElement.opened, 'backdrop is closed');
assert.isNotObject(overlay.backdropElement.parentNode, 'backdrop is removed from document');
done();
});
});
test('backdrop is removed when toggling overlay opened', function(done) {
overlay.open();
assert.isOk(overlay.backdropElement.parentNode, 'backdrop is immediately inserted in the document');
runAfterClose(overlay, function() {
assert.isFalse(overlay.backdropElement.opened, 'backdrop is closed');
assert.isNotOk(overlay.backdropElement.parentNode, 'backdrop is removed from document');
done();
});
});
});
suite('multiple overlays', function() {
var overlay1, overlay2;
setup(function() {
var f = fixture('multiple');
overlay1 = f[0];
overlay2 = f[1];
});
test('new overlays appear on top', function(done) {
runAfterOpen(overlay1, function() {
runAfterOpen(overlay2, function() {
var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
var styleZ1 = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
assert.isTrue(styleZ1 > styleZ, 'overlay2 has higher z-index than overlay1');
done();
});
});
});
test('ESC closes only the top overlay', function(done) {
runAfterOpen(overlay1, function() {
runAfterOpen(overlay2, function() {
MockInteractions.pressAndReleaseKeyOn(document, 27);
assert.isFalse(overlay2.opened, 'overlay2 was closed');
assert.isTrue(overlay1.opened, 'overlay1 is still opened');
done();
});
});
});
test('close an overlay in proximity to another overlay', function(done) {
// Open and close a separate overlay.
overlay1.open();
overlay1.close();
// Open the overlay we care about.
overlay2.open();
// Immediately close the first overlay.
// Wait for infinite recursion, otherwise we win.
runAfterClose(overlay2, function() {
done();
})
});
});
suite('Manager overlays in sync', function() {
var overlay1, overlay2;
var overlays;
setup(function() {
var f = fixture('multiple');
overlay1 = f[0];
overlay2 = f[1];
overlays = Polymer.IronOverlayManager._overlays;
});
test('no duplicates after attached', function(done) {
overlay1 = document.createElement('test-overlay');
overlay1.addEventListener('iron-overlay-opened',function() {
assert.equal(overlays.length, 1, 'correct count after open and attached');
document.body.removeChild(overlay1);
done();
});
overlay1.opened = true;
assert.equal(overlays.length, 1, 'immediately updated');
document.body.appendChild(overlay1);
});
test('open twice handled', function() {
overlay1.open();
assert.equal(overlays.length, 1, '1 overlay after open');
overlay1.open();
assert.equal(overlays.length, 1, '1 overlay after second open');
});
test('close handled', function() {
overlay1.open();
overlay1.close();
assert.equal(overlays.length, 0, '0 overlays after close');
});
test('open/close brings overlay on top', function() {
overlay1.open();
overlay2.open();
assert.equal(overlays.indexOf(overlay1), 0, 'overlay1 at index 0');
assert.equal(overlays.indexOf(overlay2), 1, 'overlay2 at index 1');
overlay1.close();
overlay1.open();
assert.equal(overlays.indexOf(overlay1), 1, 'overlay1 moved at index 1');
assert.isAbove(parseInt(overlay1.style.zIndex), parseInt(overlay2.style.zIndex), 'overlay1 on top of overlay2');
});
});
suite('z-ordering', function() {
var originalMinimumZ;
var overlay1, overlay2;
setup(function() {
var f = fixture('multiple');
overlay1 = f[0];
overlay2 = f[1];
originalMinimumZ = Polymer.IronOverlayManager._minimumZ;
});
teardown(function() {
Polymer.IronOverlayManager._minimumZ = originalMinimumZ;
});
// for iframes
test('default z-index is greater than 100', function(done) {
runAfterOpen(overlay1, function() {
var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
assert.isTrue(styleZ > 100, 'overlay1 z-index is <= 100');
done();
});
});
test('ensureMinimumZ() effects z-index', function(done) {
Polymer.IronOverlayManager.ensureMinimumZ(1000);
runAfterOpen(overlay1, function() {
var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
assert.isTrue(styleZ > 1000, 'overlay1 z-index is <= 1000');
done();
});
});
test('ensureMinimumZ() never decreases the minimum z-index', function(done) {
Polymer.IronOverlayManager.ensureMinimumZ(1000);
Polymer.IronOverlayManager.ensureMinimumZ(500);
runAfterOpen(overlay1, function() {
var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
assert.isTrue(styleZ > 1000, 'overlay1 z-index is <= 1000');
done();
});
});
});
suite('multiple overlays with backdrop', function() {
var overlay1, overlay2, overlay3;
setup(function() {
var f = fixture('multiple');
overlay1 = f[0];
overlay2 = f[1];
overlay3 = f[2];
overlay1.withBackdrop = overlay2.withBackdrop = overlay3.withBackdrop = true;
});
test('multiple overlays share the same backdrop', function() {
assert.isTrue(overlay1.backdropElement === overlay2.backdropElement, 'overlay1 and overlay2 have the same backdrop element');
assert.isTrue(overlay1.backdropElement === overlay3.backdropElement, 'overlay1 and overlay3 have the same backdrop element');
});
test('only one iron-overlay-backdrop in the DOM', function() {
// Open them all.
overlay1.opened = overlay2.opened = overlay3.opened = true;
assert.lengthOf(document.querySelectorAll('iron-overlay-backdrop'), 1, 'only one backdrop element in the DOM');
});
test('iron-overlay-backdrop is removed from the DOM when all overlays with backdrop are closed', function(done) {
// Open & close them all.
overlay1.opened = overlay2.opened = overlay3.opened = true;
overlay1.opened = overlay2.opened = overlay3.opened = false;
Polymer.Base.async(function() {
assert.lengthOf(document.querySelectorAll('iron-overlay-backdrop'), 0, 'backdrop element removed from the DOM');
done();
}, 1);
});
test('newest overlay appear on top', function(done) {
runAfterOpen(overlay1, function() {
runAfterOpen(overlay2, function() {
var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
var style1Z = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
var bgStyleZ = parseInt(window.getComputedStyle(overlay1.backdropElement).zIndex, 10);
assert.isTrue(style1Z > styleZ, 'overlay2 has higher z-index than overlay1');
assert.isTrue(styleZ > bgStyleZ, 'overlay1 has higher z-index than backdrop');
done();
});
});
});
test('click events handled only by top overlay', function(done) {
var btn = document.createElement('button');
btn.addEventListener('tap', overlay2.close.bind(overlay2));
Polymer.dom(overlay2).appendChild(btn);
runAfterOpen(overlay1, function() {
runAfterOpen(overlay2, function() {
MockInteractions.tap(btn);
assert.isFalse(overlay2.opened, 'overlay2 closed');
assert.isTrue(overlay1.opened, 'overlay1 still opened');
done();
});
});
});
test('updating with-backdrop updates z-index', function(done) {
runAfterOpen(overlay1, function() {
runAfterOpen(overlay2, function() {
overlay1.withBackdrop = false;
var styleZ = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
var style1Z = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
var bgStyleZ = parseInt(window.getComputedStyle(overlay1.backdropElement).zIndex, 10);
assert.isTrue(style1Z > bgStyleZ, 'overlay2 has higher z-index than backdrop');
assert.isTrue(styleZ < bgStyleZ, 'overlay1 has lower z-index than backdrop');
done();
});
});
});
});
suite('overlay in composed tree', function() {
test('click on overlay content does not close it', function(done) {
var composed = fixture('composed');
// Opens overlay.
MockInteractions.tap(composed.$.trigger);
composed.$.dropdown.addEventListener('iron-overlay-opened', function() {
// Tap on button inside overlay.
MockInteractions.tap(Polymer.dom(composed).querySelector('button'));
Polymer.Base.async(function(){
assert.isTrue(composed.$.dropdown.opened, 'overlay still opened');
done();
}, 1);
});
});
});
suite('always-on-top', function() {
var overlay1, overlay2;
setup(function() {
var f = fixture('multiple');
overlay1 = f[0];
overlay2 = f[1];
overlay1.alwaysOnTop = true;
});
test('stays on top', function(done) {
runAfterOpen(overlay1, function() {
runAfterOpen(overlay2, function() {
var zIndex1 = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
var zIndex2 = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
assert.isAbove(zIndex1, zIndex2, 'overlay1 on top');
assert.equal(Polymer.IronOverlayManager.currentOverlay(), overlay1, 'currentOverlay ok');
done();
});
});
});
test('stays on top also if another overlay is with-backdrop', function(done) {
overlay2.withBackdrop = true;
runAfterOpen(overlay1, function() {
runAfterOpen(overlay2, function() {
var zIndex1 = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
var zIndex2 = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
assert.isAbove(zIndex1, zIndex2, 'overlay1 on top');
assert.equal(Polymer.IronOverlayManager.currentOverlay(), overlay1, 'currentOverlay ok');
done();
});
});
});
test('last overlay with always-on-top wins', function(done) {
overlay2.alwaysOnTop = true;
runAfterOpen(overlay1, function() {
runAfterOpen(overlay2, function() {
var zIndex1 = parseInt(window.getComputedStyle(overlay1).zIndex, 10);
var zIndex2 = parseInt(window.getComputedStyle(overlay2).zIndex, 10);
assert.isAbove(zIndex2, zIndex1, 'overlay2 on top');
assert.equal(Polymer.IronOverlayManager.currentOverlay(), overlay2, 'currentOverlay ok');
done();
});
});
});
});
suite('a11y', function() {
test('overlay has aria-hidden=true when opened', function() {
var overlay = fixture('basic');
assert.equal(overlay.getAttribute('aria-hidden'), 'true', 'overlay has aria-hidden="true"');
overlay.open();
assert.isFalse(overlay.hasAttribute('aria-hidden'), 'overlay does not have aria-hidden attribute');
overlay.close();
assert.equal(overlay.getAttribute('aria-hidden'), 'true', 'overlay has aria-hidden="true"');
});
});
</script>
</body>
</html>