jellyfin-web/dashboard-ui/bower_components/iron-dropdown/test/iron-dropdown.html

429 lines
14 KiB
HTML
Raw Normal View History

2015-08-12 14:39:02 -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>
<meta charset="UTF-8">
<title>iron-dropdown basic tests</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../web-component-tester/browser.js"></script>
<script src="../../test-fixture/test-fixture-mocha.js"></script>
<script src="../../iron-test-helpers/mock-interactions.js"></script>
<link rel="import" href="../iron-dropdown.html">
<link rel="import" href="../../test-fixture/test-fixture.html">
</head>
2015-12-15 11:24:41 -07:00
<style>
body {
margin: 0;
padding: 0;
}
2016-03-11 20:29:37 -07:00
.container {
display: block;
position: relative;
width: 100px;
height: 100px;
background-color: yellow;
}
.positioned {
position: absolute;
top: 40px;
left: 40px;
}
.dropdown-content {
width: 50px;
height: 50px;
background-color: orange;
}
.full-screen {
width: 100vw;
height: 100vh;
}
2015-12-15 11:24:41 -07:00
</style>
2015-08-12 14:39:02 -07:00
<body>
<test-fixture id="TrivialDropdown">
<template>
<iron-dropdown>
2016-03-11 20:29:37 -07:00
<div class="dropdown-content"></div>
2015-08-12 14:39:02 -07:00
</iron-dropdown>
</template>
</test-fixture>
2015-09-22 21:00:30 -07:00
<test-fixture id="NonLockingDropdown">
<template>
2015-12-15 11:24:41 -07:00
<iron-dropdown allow-outside-scroll>
<div class="dropdown-content">I don't lock scroll!</div>
2015-09-22 21:00:30 -07:00
</iron-dropdown>
</template>
</test-fixture>
2015-08-12 14:39:02 -07:00
<test-fixture id="AlignedDropdown">
<template>
2016-03-11 20:29:37 -07:00
<div class="container">
2015-08-12 14:39:02 -07:00
<iron-dropdown horizontal-align="right" vertical-align="top">
2016-03-11 20:29:37 -07:00
<div class="dropdown-content full-screen"></div>
2015-08-12 14:39:02 -07:00
</iron-dropdown>
</div>
</template>
</test-fixture>
2015-12-15 11:24:41 -07:00
<!-- Absolutely position the dropdown so that it has enough space to move around -->
<test-fixture id="OffsetDropdownTopLeft">
<template>
2016-03-11 20:29:37 -07:00
<div class="container positioned">
2015-12-15 11:24:41 -07:00
<iron-dropdown>
2016-03-11 20:29:37 -07:00
<div class="dropdown-content"></div>
2015-12-15 11:24:41 -07:00
</iron-dropdown>
</div>
</template>
</test-fixture>
<test-fixture id="OffsetDropdownBottomRight">
<template>
2016-03-11 20:29:37 -07:00
<div class="container positioned">
2015-12-15 11:24:41 -07:00
<iron-dropdown horizontal-align="right" vertical-align="bottom">
2016-03-11 20:29:37 -07:00
<div class="dropdown-content"></div>
2015-12-15 11:24:41 -07:00
</iron-dropdown>
</div>
</template>
</test-fixture>
2015-08-18 21:08:03 -07:00
<test-fixture id="FocusableContentDropdown">
<template>
<iron-dropdown>
<div class="dropdown-content" tabindex="0">
<div class="subcontent" tabindex="0"></div>
</div>
</iron-dropdown>
</template>
</test-fixture>
2015-12-15 11:24:41 -07:00
<test-fixture id="RTLDropdownLeft">
<template>
2016-03-11 20:29:37 -07:00
<div dir="rtl" class="container">
2015-12-15 11:24:41 -07:00
<iron-dropdown>
2016-03-11 20:29:37 -07:00
<div class="dropdown-content"></div>
2015-12-15 11:24:41 -07:00
</iron-dropdown>
</div>
</template>
</test-fixture>
<test-fixture id="RTLDropdownRight">
<template>
2016-03-11 20:29:37 -07:00
<div dir="rtl" class="container">
2015-12-15 11:24:41 -07:00
<iron-dropdown horizontal-align="right">
2016-03-11 20:29:37 -07:00
<div class="dropdown-content"></div>
2015-12-15 11:24:41 -07:00
</iron-dropdown>
</div>
</template>
</test-fixture>
2015-08-12 14:39:02 -07:00
<script>
function elementIsVisible(element) {
var contentRect = element.getBoundingClientRect();
var computedStyle = window.getComputedStyle(element);
return computedStyle.display !== 'none' &&
contentRect.width > 0 &&
contentRect.height > 0;
}
2016-02-05 23:33:34 -07:00
function runAfterOpen(overlay, cb) {
overlay.addEventListener('iron-overlay-opened', function () {
Polymer.Base.async(cb, 1);
});
overlay.open();
}
2015-08-12 14:39:02 -07:00
suite('<iron-dropdown>', function() {
var dropdown;
2015-08-18 21:08:03 -07:00
var content;
2015-08-12 14:39:02 -07:00
suite('basic', function() {
setup(function() {
dropdown = fixture('TrivialDropdown');
2015-08-18 21:08:03 -07:00
content = Polymer.dom(dropdown).querySelector('.dropdown-content');
2015-08-12 14:39:02 -07:00
});
test('effectively hides the dropdown content', function() {
expect(elementIsVisible(content)).to.be.equal(false);
});
test('shows dropdown content when opened', function(done) {
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-08-12 14:39:02 -07:00
expect(elementIsVisible(content)).to.be.equal(true);
done();
});
});
test('hides dropdown content when outside is clicked', function(done) {
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-08-12 14:39:02 -07:00
expect(elementIsVisible(content)).to.be.equal(true);
2016-02-05 23:33:34 -07:00
MockInteractions.tap(dropdown.parentNode);
2015-12-15 11:24:41 -07:00
Polymer.Base.async(function() {
2016-02-05 23:33:34 -07:00
expect(elementIsVisible(content)).to.be.equal(false);
done();
}, 10);
2015-08-12 14:39:02 -07:00
});
2015-08-18 21:08:03 -07:00
});
suite('when content is focusable', function() {
setup(function() {
dropdown = fixture('FocusableContentDropdown');
content = Polymer.dom(dropdown).querySelector('.dropdown-content');
});
test('focuses the content when opened', function(done) {
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-08-18 21:08:03 -07:00
expect(document.activeElement).to.be.equal(content);
done();
});
});
test('focuses a configured focus target', function(done) {
var focusableChild = Polymer.dom(content).querySelector('div[tabindex]');
dropdown.focusTarget = focusableChild;
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-08-18 21:08:03 -07:00
expect(document.activeElement).to.not.be.equal(content);
expect(document.activeElement).to.be.equal(focusableChild);
done();
});
});
2015-08-12 14:39:02 -07:00
});
});
2015-09-22 21:00:30 -07:00
suite('locking scroll', function() {
var dropdown;
2016-02-05 23:33:34 -07:00
setup(function() {
dropdown = fixture('TrivialDropdown');
});
test('should lock, only once', function(done) {
var openCount = 0;
runAfterOpen(dropdown, function() {
expect(Polymer.IronDropdownScrollManager._lockingElements.length)
.to.be.equal(1);
expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(document.body))
.to.be.equal(true);
2016-03-11 20:29:37 -07:00
2016-02-05 23:33:34 -07:00
if(openCount === 0) {
// This triggers a second `pushScrollLock` with the same element, however
// that should not add the element to the `_lockingElements` stack twice
dropdown.close();
dropdown.open();
} else {
done();
}
openCount++;
});
});
});
suite('non locking scroll', function() {
var dropdown;
2015-09-22 21:00:30 -07:00
setup(function() {
dropdown = fixture('NonLockingDropdown');
});
2015-12-15 11:24:41 -07:00
test('can be disabled with `allowOutsideScroll`', function(done) {
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-12-15 11:24:41 -07:00
expect(Polymer.IronDropdownScrollManager.elementIsScrollLocked(document.body))
.to.be.equal(false);
done();
});
2015-09-22 21:00:30 -07:00
});
});
2015-08-12 14:39:02 -07:00
suite('aligned dropdown', function() {
var parent;
setup(function() {
parent = fixture('AlignedDropdown');
dropdown = parent.querySelector('iron-dropdown');
});
test('can be re-aligned to the right and the top', function(done) {
var parentRect;
var dropdownRect;
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-08-12 14:39:02 -07:00
dropdownRect = dropdown.getBoundingClientRect();
parentRect = parent.getBoundingClientRect();
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.top, parentRect.top, 'top ok');
assert.equal(dropdownRect.left, 0, 'left ok');
assert.equal(dropdownRect.bottom, document.documentElement.clientHeight, 'bottom ok');
assert.equal(dropdownRect.right, parentRect.right, 'right ok');
2015-08-12 14:39:02 -07:00
done();
2016-02-05 23:33:34 -07:00
});
2015-08-12 14:39:02 -07:00
});
test('can be re-aligned to the bottom', function(done) {
var parentRect;
var dropdownRect;
dropdown.verticalAlign = 'bottom';
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-08-12 14:39:02 -07:00
parentRect = parent.getBoundingClientRect();
dropdownRect = dropdown.getBoundingClientRect();
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.top, 0, 'top ok');
assert.equal(dropdownRect.left, 0, 'left ok');
assert.equal(dropdownRect.bottom, parentRect.bottom, 'bottom ok');
assert.equal(dropdownRect.right, parentRect.right, 'right ok');
done();
});
});
2015-08-12 14:39:02 -07:00
2016-03-11 20:29:37 -07:00
test('handles parent\'s stacking context', function(done) {
var parentRect;
var dropdownRect;
// This will create a new stacking context.
parent.style.transform = 'translateZ(0)';
runAfterOpen(dropdown, function () {
dropdownRect = dropdown.getBoundingClientRect();
parentRect = parent.getBoundingClientRect();
assert.equal(dropdownRect.top, parentRect.top, 'top ok');
assert.equal(dropdownRect.left, 0, 'left ok');
assert.equal(dropdownRect.bottom, document.documentElement.clientHeight, 'bottom ok');
assert.equal(dropdownRect.right, parentRect.right, 'right ok');
2015-08-12 14:39:02 -07:00
done();
2016-02-05 23:33:34 -07:00
});
2015-08-12 14:39:02 -07:00
});
2015-12-15 11:24:41 -07:00
});
2015-08-12 14:39:02 -07:00
2015-12-15 11:24:41 -07:00
suite('when align is left/top, with an offset', function() {
var dropdownRect;
var offsetDropdownRect;
var dropdown;
setup(function() {
var parent = fixture('OffsetDropdownTopLeft');
dropdown = parent.querySelector('iron-dropdown');
});
2015-08-12 14:39:02 -07:00
2015-12-15 11:24:41 -07:00
test('can be offset towards the bottom right', function(done) {
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-12-15 11:24:41 -07:00
dropdownRect = dropdown.getBoundingClientRect();
2015-08-12 14:39:02 -07:00
2015-12-15 11:24:41 -07:00
dropdown.verticalOffset = 10;
dropdown.horizontalOffset = 10;
offsetDropdownRect = dropdown.getBoundingClientRect();
2015-08-12 14:39:02 -07:00
2015-12-15 11:24:41 -07:00
// verticalAlign is top, so a positive offset moves down.
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.top + 10, offsetDropdownRect.top, 'top ok');
2015-12-15 11:24:41 -07:00
// horizontalAlign is left, so a positive offset moves to the right.
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.left + 10, offsetDropdownRect.left, 'left ok');
2015-12-15 11:24:41 -07:00
done();
2016-02-05 23:33:34 -07:00
});
2015-12-15 11:24:41 -07:00
});
2015-08-12 14:39:02 -07:00
2015-12-15 11:24:41 -07:00
test('can be offset towards the top left', function(done) {
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-12-15 11:24:41 -07:00
dropdownRect = dropdown.getBoundingClientRect();
dropdown.verticalOffset = -10;
dropdown.horizontalOffset = -10;
offsetDropdownRect = dropdown.getBoundingClientRect();
// verticalAlign is top, so a negative offset moves up.
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.top - 10, offsetDropdownRect.top, 'top ok');
2015-12-15 11:24:41 -07:00
// horizontalAlign is left, so a negative offset moves to the left.
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.left - 10, offsetDropdownRect.left, 'left ok');
2015-12-15 11:24:41 -07:00
done();
2016-02-05 23:33:34 -07:00
});
2015-12-15 11:24:41 -07:00
});
});
suite('when align is right/bottom, with an offset', function() {
var dropdownRect;
var offsetDropdownRect;
var dropdown;
setup(function() {
var parent = fixture('OffsetDropdownBottomRight');
dropdown = parent.querySelector('iron-dropdown');
});
test('can be offset towards the top left', function(done) {
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-12-15 11:24:41 -07:00
dropdownRect = dropdown.getBoundingClientRect();
dropdown.verticalOffset = 10;
dropdown.horizontalOffset = 10;
offsetDropdownRect = dropdown.getBoundingClientRect();
// verticalAlign is bottom, so a positive offset moves up.
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.bottom - 10, offsetDropdownRect.bottom, 'bottom ok');
2015-12-15 11:24:41 -07:00
// horizontalAlign is right, so a positive offset moves to the left.
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.right - 10, offsetDropdownRect.right, 'right ok');
2015-12-15 11:24:41 -07:00
done();
2016-02-05 23:33:34 -07:00
});
2015-12-15 11:24:41 -07:00
});
test('can be offset towards the bottom right', function(done) {
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-12-15 11:24:41 -07:00
dropdownRect = dropdown.getBoundingClientRect();
dropdown.verticalOffset = -10;
dropdown.horizontalOffset = -10;
offsetDropdownRect = dropdown.getBoundingClientRect();
// verticalAlign is bottom, so a negative offset moves down.
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.bottom + 10, offsetDropdownRect.bottom, 'bottom ok');
2015-12-15 11:24:41 -07:00
// horizontalAlign is right, so a positive offset moves to the right.
2016-03-11 20:29:37 -07:00
assert.equal(dropdownRect.right + 10, offsetDropdownRect.right, 'right ok');
2015-12-15 11:24:41 -07:00
done();
2016-02-05 23:33:34 -07:00
});
2015-12-15 11:24:41 -07:00
});
});
suite('RTL', function() {
var dropdown;
var dropdownRect;
test('with horizontalAlign=left', function(done) {
var parent = fixture('RTLDropdownLeft');
dropdown = parent.querySelector('iron-dropdown');
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-12-15 11:24:41 -07:00
// In RTL, if `horizontalAlign` is "left", that's the same as
// being right-aligned in LTR. So the dropdown should be in the top
// right corner.
dropdownRect = dropdown.getBoundingClientRect();
expect(dropdownRect.top).to.be.equal(0);
expect(dropdownRect.right).to.be.equal(100);
done();
2015-08-12 14:39:02 -07:00
});
});
2015-12-15 11:24:41 -07:00
test('with horizontalAlign=right', function(done) {
var parent = fixture('RTLDropdownRight');
dropdown = parent.querySelector('iron-dropdown');
2016-02-05 23:33:34 -07:00
runAfterOpen(dropdown, function () {
2015-12-15 11:24:41 -07:00
// In RTL, if `horizontalAlign` is "right", that's the same as
// being left-aligned in LTR. So the dropdown should be in the top
// left corner.
dropdownRect = dropdown.getBoundingClientRect();
expect(dropdownRect.top).to.be.equal(0);
expect(dropdownRect.left).to.be.equal(0);
done();
});
});
2015-08-12 14:39:02 -07:00
});
});
</script>
</body>
</html>