-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
339 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
<!doctype html> | ||
<!-- | ||
@license | ||
Copyright (c) 2017 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 | ||
--> | ||
<meta charset="utf-8"> | ||
<script src="../../../webcomponentsjs/webcomponents-loader.js"></script> | ||
<script src="../../../web-component-tester/browser.js"></script> | ||
<link rel="import" href="../../polymer-element.html"> | ||
<link rel="import" href="../../lib/elements/dom-if.html"> | ||
<link rel="import" href="../../lib/mixins/lazy-upgrade-mixin.html"> | ||
<link rel="import" href="../../lib/utils/async.html"> | ||
<link rel="import" href="../../lib/utils/debounce.html"> | ||
|
||
<script> | ||
let Loaded = new Promise((resolve) => { | ||
addEventListener('WebComponentsReady', () => {resolve()}); | ||
}); | ||
Loaded.then(function() { | ||
let async = Polymer.Async.timeOut.after(150); | ||
window.LazyParentMixin = class LazyParent extends Polymer.LazyUpgradeMixin(Polymer.Element) { | ||
static get observers() { | ||
return ['lazyUpgradingChanged(_lazyUpgrading)'] | ||
} | ||
connectedCallback() { | ||
this.upgradedChildren = []; | ||
this.addEventListener('rendered', (e) => { | ||
this.upgradedChildren.push(e.composedPath()[0]); | ||
}); | ||
super.connectedCallback(); | ||
} | ||
lazyUpgradingChanged() { | ||
this.__lazyDebouncer = Polymer.Debouncer.debounce(this.__lazyDebouncer, async, () => { | ||
this.dispatchEvent(new Event('lazy-upgrade-finished')); | ||
}); | ||
} | ||
} | ||
}); | ||
</script> | ||
|
||
<dom-module id="lazy-child"> | ||
<template> | ||
<style> | ||
:host(:not([disable-upgrade])) { | ||
display: block; | ||
border: 2px solid blue; | ||
height: 20px; | ||
} | ||
</style> | ||
</template> | ||
<script> | ||
Loaded.then(function() { | ||
class LazyChild extends Polymer.Element { | ||
static get is() {return 'lazy-child'} | ||
ready() { | ||
super.ready(); | ||
this.dispatchEvent(new Event('rendered', {bubbles: true, composed: true})); | ||
} | ||
} | ||
customElements.define('lazy-child', LazyChild); | ||
}); | ||
</script> | ||
</dom-module> | ||
|
||
<dom-module id="lazy-upgrade-shadow"> | ||
<template> | ||
<lazy-child id="one" disable-upgrade lazy-upgrade></lazy-child> | ||
<lazy-child id="two" disable-upgrade lazy-upgrade="1"></lazy-child> | ||
<lazy-child id="three"></lazy-child> | ||
<dom-if lazy-upgrade="1"> | ||
<lazy-child id="four" disable-upgrade lazy-upgrade></lazy-child> | ||
</dom-if> | ||
<lazy-child id="five" disable-upgrade></lazy-child> | ||
<dom-if lazy-upgrade> | ||
<lazy-child id="six"></lazy-child> | ||
</dom-if> | ||
</template> | ||
<script> | ||
Loaded.then(function() { | ||
customElements.define('lazy-upgrade-shadow', class extends window.LazyParentMixin { | ||
static get is() { return 'lazy-upgrade-shadow' } | ||
}); | ||
}); | ||
</script> | ||
</dom-module> | ||
|
||
<dom-module id="lazy-parent"> | ||
<script> | ||
Loaded.then(function() { | ||
customElements.define('lazy-parent', class extends window.LazyParentMixin {}); | ||
}); | ||
</script> | ||
</dom-module> | ||
|
||
<template id="only-lazy-upgrade"> | ||
<dom-if> | ||
<template> | ||
<lazy-child id="one"></lazy-child> | ||
</template> | ||
</dom-if> | ||
<dom-if lazy-upgrade> | ||
<template> | ||
<lazy-child id="two"></lazy-child> | ||
</template> | ||
</dom-if> | ||
<lazy-child id="three" disable-upgrade></lazy-child> | ||
<lazy-child id="four" disable-upgrade lazy-upgrade></lazy-child> | ||
<dom-if lazy-upgrade> | ||
<template> | ||
<lazy-child id="five" disable-upgrade></lazy-child> | ||
<lazy-child id="six" disable-upgrade lazy-upgrade></lazy-child> | ||
</template> | ||
</dom-if> | ||
</template> | ||
|
||
<template id="critical"> | ||
<lazy-child id="lazy-one" disable-upgrade lazy-upgrade></lazy-child> | ||
<lazy-child id="critical"></lazy-child> | ||
<lazy-child id="lazy-two" disable-upgrade lazy-upgrade></lazy-child> | ||
</template> | ||
|
||
<template id="ordered"> | ||
<lazy-child id="one" disable-upgrade lazy-upgrade="3"></lazy-child> | ||
<lazy-child id="two" disable-upgrade lazy-upgrade="2"></lazy-child> | ||
<lazy-child id="three" disable-upgrade lazy-upgrade="1"></lazy-child> | ||
<lazy-child id="four" disable-upgrade lazy-upgrade></lazy-child> | ||
<lazy-child id="five" disable-upgrade lazy-upgrade="2"></lazy-child> | ||
</template> | ||
|
||
<dom-module id="no-upgrade-shadow"> | ||
<template> | ||
<lazy-child id="one" disable-upgrade lazy-upgrade></lazy-child> | ||
<dom-if if> | ||
<template> | ||
<lazy-child id="two" disable-upgrade lazy-upgrade></lazy-child> | ||
</template> | ||
</dom-if> | ||
</template> | ||
</dom-module> | ||
<script> | ||
Loaded.then(() => { | ||
customElements.define('no-upgrade-shadow', class extends Polymer.Element { | ||
static get is() { return 'no-upgrade-shadow' } | ||
}); | ||
}); | ||
</script> | ||
|
||
<template id="scoped"> | ||
<no-upgrade-shadow></no-upgrade-shadow> | ||
<lazy-child id="three" disable-upgrade lazy-upgrade></lazy-child> | ||
</template> | ||
|
||
<script> | ||
suite('Lazy Upgrade Mixin', () => { | ||
function assertUpgraded(node, expected) { | ||
let disabled = node.hasAttribute('disable-upgrade'); | ||
assert.notEqual(disabled, expected, 'upgrade status was not expected'); | ||
} | ||
|
||
function assertRendered(node, expected) { | ||
let border = getComputedStyle(node).getPropertyValue('border-top-width'); | ||
assert.equal(border, expected ? '2px' : '0px', 'render status was not expected'); | ||
} | ||
|
||
test('fires "lazy-upgrade-finished" event when done', (done) => { | ||
let el = document.createElement('lazy-parent'); | ||
el.addEventListener('lazy-upgrade-finished', () => { | ||
document.body.removeChild(el) | ||
done(); | ||
}); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
test('Lazy Upgrading shows critical section first, lazy elements later', (done) => { | ||
let template = document.querySelector('template#critical'); | ||
let el = document.createElement('lazy-parent'); | ||
el.appendChild(document.importNode(template.content, true)); | ||
let children = el.querySelectorAll('lazy-child'); | ||
el.addEventListener('lazy-upgrade-finished', () => { | ||
for (let i = 0; i < children.length; i++) { | ||
assertUpgraded(children[i], true); | ||
} | ||
el.parentNode.removeChild(el); | ||
done(); | ||
}) | ||
document.body.appendChild(el); | ||
assertUpgraded(children[0], false); | ||
assertUpgraded(children[1], true); | ||
assertUpgraded(children[2], false); | ||
}); | ||
|
||
test('Lazy Upgrading activates elements with lazy-upgrade attribute', (done) => { | ||
let template = document.querySelector('template#only-lazy-upgrade'); | ||
let el = document.createElement('lazy-parent'); | ||
el.appendChild(document.importNode(template.content, true)); | ||
el.addEventListener('lazy-upgrade-finished', () => { | ||
let children = el.querySelectorAll('lazy-child'); | ||
assert.equal(children.length, 5); | ||
assertRendered(children[0], true); | ||
assertRendered(children[1], false); | ||
assertRendered(children[2], true); | ||
assertRendered(children[3], false); | ||
assertRendered(children[4], true); | ||
document.body.removeChild(el); | ||
done(); | ||
}); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
test('Lazy upgrading respects ordering', (done) => { | ||
let template = document.querySelector('template#ordered'); | ||
let el = document.createElement('lazy-parent'); | ||
el.appendChild(document.importNode(template.content, true)); | ||
el.addEventListener('lazy-upgrade-finished', () => { | ||
let expected = [ | ||
"four", | ||
"three", | ||
"two", | ||
"five", | ||
"one" | ||
]; | ||
document.body.removeChild(el); | ||
assert.deepEqual(el.upgradedChildren.map(n => n.id), expected); | ||
done(); | ||
}); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
test('Shadow DOM works the same', (done) => { | ||
let el = document.createElement('lazy-upgrade-shadow'); | ||
el.addEventListener('lazy-upgrade-finished', () => { | ||
let children = el.shadowRoot.querySelectorAll('lazy-child'); | ||
assert.equal(children.length, 6); | ||
let expected = [ | ||
"three", | ||
"six", | ||
"one", | ||
"four", | ||
"two" | ||
]; | ||
assert.deepEqual(el.upgradedChildren.map(n => n.id), expected); | ||
document.body.removeChild(el); | ||
done(); | ||
}); | ||
document.body.appendChild(el); | ||
}); | ||
|
||
test('Candidates are only found in the same scope', (done) => { | ||
let template = document.querySelector('template#scoped'); | ||
let el = document.createElement('lazy-parent'); | ||
el.addEventListener('lazy-upgrade-finished', () => { | ||
let expected = [ | ||
"three" | ||
]; | ||
assert.deepEqual(el.upgradedChildren.map(n => n.id), expected); | ||
document.body.removeChild(el); | ||
done(); | ||
}); | ||
el.appendChild(document.importNode(template.content, true)); | ||
document.body.appendChild(el); | ||
}); | ||
}) | ||
</script> |