2015-06-19 09:36:51 -07:00
|
|
|
<!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>paper-dialog-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>
|
|
|
|
<script src="../../test-fixture/test-fixture-mocha.js"></script>
|
|
|
|
|
|
|
|
<link rel="import" href="../../test-fixture/test-fixture.html">
|
|
|
|
<link rel="import" href="test-dialog.html">
|
2016-01-29 19:43:11 -07:00
|
|
|
<link rel="import" href="test-buttons.html">
|
2015-06-19 09:36:51 -07:00
|
|
|
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<test-fixture id="basic">
|
|
|
|
<template>
|
|
|
|
<test-dialog>
|
|
|
|
<p>Dialog</p>
|
|
|
|
<div class="buttons">
|
2015-06-25 18:13:51 -07:00
|
|
|
<button extra>extra</button>
|
2015-06-19 09:36:51 -07:00
|
|
|
<button dialog-dismiss>dismiss</button>
|
|
|
|
<button dialog-confirm>confirm</button>
|
|
|
|
</div>
|
|
|
|
</test-dialog>
|
|
|
|
</template>
|
|
|
|
</test-fixture>
|
|
|
|
|
2016-01-29 19:43:11 -07:00
|
|
|
<test-fixture id="buttons">
|
|
|
|
<template>
|
|
|
|
<test-dialog>
|
|
|
|
<p>Dialog with test-buttons</p>
|
|
|
|
<test-buttons class="buttons"></test-buttons>
|
|
|
|
</test-dialog>
|
|
|
|
</template>
|
|
|
|
</test-fixture>
|
|
|
|
|
2015-06-19 09:36:51 -07:00
|
|
|
<test-fixture id="modal">
|
|
|
|
<template>
|
|
|
|
<test-dialog modal>
|
|
|
|
<p>Dialog</p>
|
|
|
|
<div class="buttons">
|
|
|
|
<button dialog-dismiss>dismiss</button>
|
|
|
|
<button dialog-confirm autofocus>confirm</button>
|
|
|
|
</div>
|
|
|
|
</test-dialog>
|
|
|
|
</template>
|
|
|
|
</test-fixture>
|
|
|
|
|
|
|
|
<test-fixture id="backdrop">
|
|
|
|
<template>
|
|
|
|
<test-dialog with-backdrop>
|
|
|
|
<p>Dialog</p>
|
|
|
|
<div class="buttons">
|
|
|
|
<button dialog-dismiss>dismiss</button>
|
|
|
|
<button dialog-confirm autofocus>confirm</button>
|
|
|
|
</div>
|
|
|
|
</test-dialog>
|
|
|
|
</template>
|
|
|
|
</test-fixture>
|
|
|
|
|
|
|
|
<test-fixture id="header">
|
|
|
|
<template>
|
|
|
|
<test-dialog>
|
|
|
|
<h2>Dialog</h2>
|
|
|
|
<div class="buttons">
|
|
|
|
<button dialog-dismiss>dismiss</button>
|
|
|
|
<button dialog-confirm autofocus>confirm</button>
|
|
|
|
</div>
|
|
|
|
</test-dialog>
|
|
|
|
</template>
|
|
|
|
</test-fixture>
|
|
|
|
|
|
|
|
<test-fixture id="header-with-id">
|
|
|
|
<template>
|
|
|
|
<test-dialog>
|
|
|
|
<h2 id="header">Dialog</h2>
|
|
|
|
<div class="buttons">
|
|
|
|
<button dialog-dismiss>dismiss</button>
|
|
|
|
<button dialog-confirm autofocus>confirm</button>
|
|
|
|
</div>
|
|
|
|
</test-dialog>
|
|
|
|
</template>
|
|
|
|
</test-fixture>
|
|
|
|
|
2016-01-29 19:43:11 -07:00
|
|
|
<test-fixture id="multiple">
|
|
|
|
<template>
|
|
|
|
<test-dialog modal id="dialog1">
|
|
|
|
<p>Dialog 1</p>
|
|
|
|
</test-dialog>
|
|
|
|
<test-dialog modal id="dialog2">
|
|
|
|
<p>Dialog 2</p>
|
|
|
|
</test-dialog>
|
|
|
|
</template>
|
|
|
|
</test-fixture>
|
|
|
|
|
|
|
|
<test-fixture id="nestedmodals">
|
|
|
|
<template>
|
|
|
|
<test-dialog modal opened>
|
|
|
|
<p>Dialog 1</p>
|
|
|
|
<div class="buttons">
|
|
|
|
<button dialog-dismiss>dismiss</button>
|
|
|
|
<button dialog-confirm autofocus>confirm</button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<test-dialog modal opened>
|
|
|
|
<p>Dialog 2</p>
|
|
|
|
<div class="buttons">
|
|
|
|
<button dialog-dismiss>dismiss</button>
|
|
|
|
<button dialog-confirm autofocus>confirm</button>
|
|
|
|
</div>
|
|
|
|
</test-dialog>
|
|
|
|
</test-dialog>
|
|
|
|
</template>
|
|
|
|
</test-fixture>
|
|
|
|
|
2015-06-19 09:36:51 -07:00
|
|
|
<script>
|
|
|
|
|
|
|
|
function runAfterOpen(dialog, cb) {
|
|
|
|
dialog.addEventListener('iron-overlay-opened', function() {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
dialog.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
suite('basic', function() {
|
|
|
|
|
|
|
|
test('clicking dialog does not cancel the dialog', function(done) {
|
|
|
|
var dialog = fixture('basic');
|
|
|
|
runAfterOpen(dialog, function() {
|
|
|
|
dialog.addEventListener('iron-overlay-closed', function() {
|
|
|
|
assert('dialog should not close');
|
|
|
|
});
|
|
|
|
dialog.click();
|
|
|
|
setTimeout(function() {
|
|
|
|
done();
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clicking dialog-dismiss button closes the dialog without confirmation', function(done) {
|
|
|
|
var dialog = fixture('basic');
|
|
|
|
runAfterOpen(dialog, function() {
|
|
|
|
dialog.addEventListener('iron-overlay-closed', function(event) {
|
|
|
|
assert.isFalse(event.detail.canceled, 'dialog is not canceled');
|
|
|
|
assert.isFalse(event.detail.confirmed, 'dialog is not confirmed');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
Polymer.dom(dialog).querySelector('[dialog-dismiss]').click();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-29 19:43:11 -07:00
|
|
|
test('dialog-dismiss button inside a custom element is handled', function(done) {
|
|
|
|
var dialog = fixture('buttons');
|
|
|
|
runAfterOpen(dialog, function() {
|
|
|
|
dialog.addEventListener('iron-overlay-closed', function(event) {
|
|
|
|
assert.isFalse(event.detail.canceled, 'dialog is not canceled');
|
|
|
|
assert.isFalse(event.detail.confirmed, 'dialog is not confirmed');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
Polymer.dom(dialog).querySelector('test-buttons').$.dismiss.click();
|
|
|
|
// We don't wait too long to fail.
|
|
|
|
setTimeout(function didNotClose() {
|
|
|
|
done(new Error('dialog-dismiss click did not close overlay'));
|
|
|
|
}, 20);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-19 09:36:51 -07:00
|
|
|
test('clicking dialog-confirm button closes the dialog with confirmation', function(done) {
|
|
|
|
var dialog = fixture('basic');
|
|
|
|
runAfterOpen(dialog, function() {
|
|
|
|
dialog.addEventListener('iron-overlay-closed', function(event) {
|
|
|
|
assert.isFalse(event.detail.canceled, 'dialog is not canceled');
|
|
|
|
assert.isTrue(event.detail.confirmed, 'dialog is confirmed');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
Polymer.dom(dialog).querySelector('[dialog-confirm]').click();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-29 19:43:11 -07:00
|
|
|
test('dialog-confirm button inside a custom element is handled', function(done) {
|
|
|
|
var dialog = fixture('buttons');
|
2015-06-19 09:36:51 -07:00
|
|
|
runAfterOpen(dialog, function() {
|
2016-01-29 19:43:11 -07:00
|
|
|
dialog.addEventListener('iron-overlay-closed', function(event) {
|
|
|
|
assert.isFalse(event.detail.canceled, 'dialog is not canceled');
|
|
|
|
assert.isTrue(event.detail.confirmed, 'dialog is confirmed');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
Polymer.dom(dialog).querySelector('test-buttons').$.confirm.click();
|
|
|
|
// We don't wait too long to fail.
|
|
|
|
setTimeout(function didNotClose() {
|
|
|
|
done(new Error('dialog-confirm click did not close overlay'));
|
|
|
|
}, 20);
|
2015-06-19 09:36:51 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-29 19:43:11 -07:00
|
|
|
test('clicking dialog-dismiss button closes only the dialog where is contained', function(done) {
|
|
|
|
var dialog = fixture('nestedmodals');
|
|
|
|
var innerDialog = Polymer.dom(dialog).querySelector('test-dialog');
|
|
|
|
Polymer.dom(innerDialog).querySelector('[dialog-dismiss]').click();
|
|
|
|
setTimeout(function() {
|
|
|
|
assert.isFalse(innerDialog.opened, 'inner dialog is closed');
|
|
|
|
assert.isTrue(dialog.opened, 'dialog is still open');
|
|
|
|
done();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clicking dialog-confirm button closes only the dialog where is contained', function(done) {
|
|
|
|
var dialog = fixture('nestedmodals');
|
|
|
|
var innerDialog = Polymer.dom(dialog).querySelector('test-dialog');
|
|
|
|
Polymer.dom(innerDialog).querySelector('[dialog-confirm]').click();
|
|
|
|
setTimeout(function() {
|
|
|
|
assert.isFalse(innerDialog.opened, 'inner dialog is closed');
|
|
|
|
assert.isTrue(dialog.opened, 'dialog is still open');
|
|
|
|
done();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
|
2015-06-19 09:36:51 -07:00
|
|
|
test('modal dialog has backdrop', function() {
|
|
|
|
var dialog = fixture('modal');
|
|
|
|
assert.isTrue(dialog.withBackdrop, 'withBackdrop is true');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('modal dialog has no-cancel-on-outside-click', function() {
|
|
|
|
var dialog = fixture('modal');
|
|
|
|
assert.isTrue(dialog.noCancelOnOutsideClick, 'noCancelOnOutsideClick is true');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clicking outside a modal dialog does not move focus from dialog', function(done) {
|
|
|
|
var dialog = fixture('modal');
|
|
|
|
runAfterOpen(dialog, function() {
|
2016-01-29 19:43:11 -07:00
|
|
|
document.body.click();
|
2015-06-19 09:36:51 -07:00
|
|
|
setTimeout(function() {
|
|
|
|
assert.equal(document.activeElement, Polymer.dom(dialog).querySelector('[autofocus]'), 'document.activeElement is the autofocused button');
|
|
|
|
done();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-25 18:13:51 -07:00
|
|
|
test('removing a child element on click does not cause an exception', function(done) {
|
|
|
|
var dialog = fixture('basic');
|
|
|
|
runAfterOpen(dialog, function() {
|
|
|
|
var button = Polymer.dom(dialog).querySelector('[extra]');
|
|
|
|
button.addEventListener('click', function(event) {
|
|
|
|
Polymer.dom(event.target.parentNode).removeChild(event.target);
|
|
|
|
// should not throw exception here
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
button.click();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-29 19:43:11 -07:00
|
|
|
test('multiple modal dialogs opened, handle focus change', function(done) {
|
|
|
|
var dialogs = fixture('multiple');
|
|
|
|
var focusChange = sinon.stub();
|
|
|
|
document.body.addEventListener('focus', focusChange, true);
|
|
|
|
|
|
|
|
runAfterOpen(dialogs[0], function() {
|
|
|
|
// Wait 10ms to allow focus changes
|
|
|
|
dialogs[1].async(dialogs[1].open, 10);
|
|
|
|
dialogs[1].addEventListener('iron-overlay-opened', function() {
|
|
|
|
// Wait 10ms to allow focus changes
|
|
|
|
setTimeout(function() {
|
|
|
|
// Should not enter in an infinite loop.
|
|
|
|
assert.equal(focusChange.callCount, 2, 'focus change count');
|
|
|
|
done();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('multiple modal dialogs opened, handle backdrop click', function(done) {
|
|
|
|
var dialogs = fixture('multiple');
|
|
|
|
var focusChange = sinon.stub();
|
|
|
|
document.body.addEventListener('focus', focusChange, true);
|
|
|
|
|
|
|
|
runAfterOpen(dialogs[0], function() {
|
|
|
|
// Wait 10ms to allow focus changes
|
|
|
|
dialogs[1].async(dialogs[1].open, 10);
|
|
|
|
dialogs[1].addEventListener('iron-overlay-opened', function() {
|
|
|
|
// This will trigger click listener for both dialogs.
|
|
|
|
document.body.click();
|
|
|
|
// Wait 10ms to allow focus changes
|
|
|
|
setTimeout(function() {
|
|
|
|
// Should not enter in an infinite loop.
|
|
|
|
assert.equal(focusChange.callCount, 2, 'focus change count');
|
|
|
|
done();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('focus is given to the autofocus element when clicking on backdrop', function(done) {
|
|
|
|
var dialog = fixture('modal');
|
|
|
|
dialog.addEventListener('iron-overlay-opened', onFirstOpen);
|
|
|
|
dialog.open();
|
|
|
|
|
|
|
|
function onFirstOpen() {
|
|
|
|
dialog.removeEventListener('iron-overlay-opened', onFirstOpen);
|
|
|
|
dialog.addEventListener('iron-overlay-closed', onFirstClose);
|
|
|
|
// Set the focus on dismiss button
|
|
|
|
// Calling .focus() won't trigger the dialog._onFocus
|
|
|
|
Polymer.dom(dialog).querySelector('[dialog-dismiss]').dispatchEvent(new Event('focus'));
|
|
|
|
// Close the dialog
|
|
|
|
dialog.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onFirstClose() {
|
|
|
|
dialog.removeEventListener('iron-overlay-closed', onFirstClose);
|
|
|
|
dialog.addEventListener('iron-overlay-opened', onSecondOpen);
|
|
|
|
dialog.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onSecondOpen() {
|
|
|
|
document.body.click();
|
|
|
|
setTimeout(function() {
|
|
|
|
assert.equal(document.activeElement, Polymer.dom(dialog).querySelector('[autofocus]'), 'document.activeElement is the autofocused button');
|
|
|
|
done();
|
|
|
|
}, 10);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-06-19 09:36:51 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
suite('a11y', function() {
|
|
|
|
|
|
|
|
test('dialog has role="dialog"', function() {
|
|
|
|
var dialog = fixture('basic');
|
|
|
|
assert.equal(dialog.getAttribute('role'), 'dialog', 'has role="dialog"');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('dialog has aria-modal=false', function() {
|
|
|
|
var dialog = fixture('basic');
|
|
|
|
assert.equal(dialog.getAttribute('aria-modal'), 'false', 'has aria-modal="false"');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('modal dialog has aria-modal=true', function() {
|
|
|
|
var dialog = fixture('modal');
|
|
|
|
assert.equal(dialog.getAttribute('aria-modal'), 'true', 'has aria-modal="true"');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('dialog with header has aria-labelledby', function() {
|
|
|
|
var dialog = fixture('header');
|
|
|
|
var header = Polymer.dom(dialog).querySelector('h2');
|
|
|
|
assert.ok(header.getAttribute('id'), 'header has auto-generated id');
|
|
|
|
assert.equal(dialog.getAttribute('aria-labelledby'), header.getAttribute('id'), 'aria-labelledby is set to header id');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('dialog with lazily created header has aria-labelledby', function(done) {
|
|
|
|
var dialog = fixture('basic');
|
|
|
|
var header = document.createElement('h2');
|
|
|
|
Polymer.dom(dialog).appendChild(header);
|
|
|
|
Polymer.dom.flush();
|
|
|
|
setTimeout(function() {
|
|
|
|
assert.ok(header.getAttribute('id'), 'header has auto-generated id');
|
|
|
|
assert.equal(dialog.getAttribute('aria-labelledby'), header.getAttribute('id'), 'aria-labelledby is set to header id');
|
|
|
|
done();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('dialog with header with id preserves id and has aria-labelledby', function() {
|
|
|
|
var dialog = fixture('header-with-id');
|
|
|
|
var header = Polymer.dom(dialog).querySelector('h2');
|
|
|
|
assert.equal(header.getAttribute('id'), 'header', 'header has preset id');
|
|
|
|
assert.equal(dialog.getAttribute('aria-labelledby'), 'header', 'aria-labelledby is set to header id');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|