Skip to content

Commit

Permalink
Share more code between tests imported from WebKit. (web-platform-tes…
Browse files Browse the repository at this point in the history
…ts#3853)

Also move defineNewCustomElement (renamed to define_new_custom_element) and assert_attribute_log_entry
from reactions.js to custom-elements-helpers.js so that we can reuse them in the newly added tests.

Finally, rename log() function on returned by define_new_custom_element to takeLog()
and renamed log() on the object returned by takeLog() to last() to make their semantics clear.
  • Loading branch information
rniwa authored and Takayoshi Kochi committed Oct 6, 2016
1 parent 90b63d7 commit c7fbe2d
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 364 deletions.
20 changes: 10 additions & 10 deletions custom-elements/adopted-callback.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link rel="help" href="https://w3c.github.io/webcomponents/spec/custom/#dfn-connected-callback">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/document-types.js"></script>
<script src="./resources/custom-elements-helpers.js"></script>
</head>
<body>
<div id="log"></div>
Expand All @@ -28,7 +28,7 @@
assert_array_equals(calls, ['connected']);
}, 'Inserting a custom element into the owner document must not enqueue and invoke adoptedCallback');

DocumentTypes.forEach(function (entry) {
document_types().forEach(function (entry) {
if (entry.isOwner)
return;

Expand All @@ -42,7 +42,7 @@
doc.documentElement.appendChild(instance);
assert_array_equals(calls, ['adopted', document, doc, 'connected']);
});
}, 'Inserting a custom element into a ' + documentName + ' must enqueue and invoke adoptedCallback');
}, 'Inserting a custom element into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
Expand All @@ -52,7 +52,7 @@
doc.documentElement.appendChild(instance);
assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']);
});
}, 'Moving a custom element from the owner document into a ' + documentName + ' must enqueue and invoke adoptedCallback');
}, 'Moving a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
Expand All @@ -63,7 +63,7 @@
doc.documentElement.appendChild(parent);
assert_array_equals(calls, ['adopted', document, doc, 'connected']);
});
}, 'Inserting an ancestor of custom element into a ' + documentName + ' must enqueue and invoke adoptedCallback');
}, 'Inserting an ancestor of custom element into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
Expand All @@ -75,7 +75,7 @@
doc.documentElement.appendChild(parent);
assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']);
});
}, 'Moving an ancestor of custom element from the owner document into a ' + documentName + ' must enqueue and invoke adoptedCallback');
}, 'Moving an ancestor of custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
Expand All @@ -88,7 +88,7 @@
shadowRoot.appendChild(instance);
assert_array_equals(calls, ['adopted', document, doc, 'connected']);
});
}, 'Inserting a custom element into a shadow tree in a ' + documentName + ' must enqueue and invoke adoptedCallback');
}, 'Inserting a custom element into a shadow tree in ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
Expand All @@ -101,7 +101,7 @@
doc.documentElement.appendChild(host);
assert_array_equals(calls, ['adopted', document, doc, 'connected']);
});
}, 'Inserting the shadow host of a custom element into a ' + documentName + ' must enqueue and invoke adoptedCallback');
}, 'Inserting the shadow host of a custom element into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
Expand All @@ -115,7 +115,7 @@
doc.documentElement.appendChild(host);
assert_array_equals(calls, ['disconnected', 'adopted', document, doc, 'connected']);
});
}, 'Moving the shadow host of a custom element from the owner document into a ' + documentName + ' must enqueue and invoke adoptedCallback');
}, 'Moving the shadow host of a custom element from the owner document into ' + documentName + ' must enqueue and invoke adoptedCallback');

promise_test(function () {
return getDocument().then(function (doc) {
Expand All @@ -127,7 +127,7 @@
shadowRoot.appendChild(instance);
assert_array_equals(calls, ['adopted', document, doc]);
});
}, 'Inserting a custom element into a detached shadow tree that belongs to a ' + documentName + ' must enqueue and invoke adoptedCallback');
}, 'Inserting a custom element into a detached shadow tree that belongs to ' + documentName + ' must enqueue and invoke adoptedCallback');
});

</script>
Expand Down
175 changes: 103 additions & 72 deletions custom-elements/attribute-changed-callback.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,186 +7,217 @@
<link rel="help" href="https://w3c.github.io/webcomponents/spec/custom/#dfn-attribute-changed-callback">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/custom-elements-helpers.js"></script>
</head>
<body>
<div id="log"></div>
<script>

var argumentList = [];
class MyCustomElement extends HTMLElement {
attributeChangedCallback(name, oldValue, newValue, namespace) {
argumentList.push({arguments: arguments, value: this.getAttributeNS(namespace, name)});
}
}
MyCustomElement.observedAttributes = ['title', 'id', 'r'];
customElements.define('my-custom-element', MyCustomElement);
var customElement = define_new_custom_element(['title', 'id', 'r']);

test(function () {
var instance = document.createElement('my-custom-element');
argumentList = [];
const instance = document.createElement(customElement.name);
assert_array_equals(customElement.takeLog().types(), ['constructed']);

instance.setAttribute('title', 'foo');
assert_equals(instance.getAttribute('title'), 'foo');
assert_equals(argumentList.length, 1);
assert_equals(argumentList[0].value, 'foo');
assert_array_equals(argumentList[0].arguments, ['title', null, 'foo', null]);
var logEntries = customElement.takeLog();
assert_array_equals(logEntries.types(), ['attributeChanged']);
assert_attribute_log_entry(logEntries.last(), {name: 'title', oldValue: null, newValue: 'foo', namespace: null});

instance.removeAttribute('title');
assert_equals(instance.getAttribute('title'), null);
assert_equals(argumentList.length, 2);
assert_equals(argumentList[1].value, null);
assert_array_equals(argumentList[1].arguments, ['title', 'foo', null, null]);

var logEntries = customElement.takeLog();
assert_array_equals(logEntries.types(), ['attributeChanged']);
assert_attribute_log_entry(logEntries.last(), {name: 'title', oldValue: 'foo', newValue: null, namespace: null});
}, 'setAttribute and removeAttribute must enqueue and invoke attributeChangedCallback');

test(function () {
var instance = document.createElement('my-custom-element');
argumentList = [];
var instance = document.createElement(customElement.name);
assert_array_equals(customElement.takeLog().types(), ['constructed']);

instance.setAttributeNS('http://www.w3.org/2000/svg', 'title', 'hello');
assert_equals(instance.getAttribute('title'), 'hello');
assert_equals(argumentList.length, 1);
assert_equals(argumentList[0].value, 'hello');
assert_array_equals(argumentList[0].arguments, ['title', null, 'hello', 'http://www.w3.org/2000/svg']);
var logEntries = customElement.takeLog();
assert_array_equals(logEntries.types(), ['attributeChanged']);
assert_attribute_log_entry(logEntries.last(), {name: 'title', oldValue: null, newValue: 'hello', namespace: 'http://www.w3.org/2000/svg'});

instance.removeAttributeNS('http://www.w3.org/2000/svg', 'title');
assert_equals(instance.getAttribute('title'), null);
assert_equals(argumentList.length, 2);
assert_equals(argumentList[1].value, null);
assert_array_equals(argumentList[1].arguments, ['title', 'hello', null, 'http://www.w3.org/2000/svg']);

var logEntries = customElement.takeLog();
assert_array_equals(logEntries.types(), ['attributeChanged']);
assert_attribute_log_entry(logEntries.last(), {name: 'title', oldValue: 'hello', newValue: null, namespace: 'http://www.w3.org/2000/svg'});
}, 'setAttributeNS and removeAttributeNS must enqueue and invoke attributeChangedCallback');

test(function () {
var instance = document.createElement('my-custom-element');
argumentList = [];
var instance = document.createElement(customElement.name);
assert_array_equals(customElement.takeLog().types(), ['constructed']);

var attr = document.createAttribute('id');
attr.value = 'bar';
instance.setAttributeNode(attr);

assert_equals(instance.getAttribute('id'), 'bar');
assert_equals(argumentList.length, 1);
assert_equals(argumentList[0].value, 'bar');
assert_array_equals(argumentList[0].arguments, ['id', null, 'bar', null]);
var logEntries = customElement.takeLog();
assert_array_equals(logEntries.types(), ['attributeChanged']);
assert_attribute_log_entry(logEntries.last(), {name: 'id', oldValue: null, newValue: 'bar', namespace: null});

instance.removeAttributeNode(attr);
assert_equals(instance.getAttribute('id'), null);
assert_equals(argumentList.length, 2);
assert_equals(argumentList[1].value, null);
assert_array_equals(argumentList[1].arguments, ['id', 'bar', null, null]);

}, 'setAttributeNode and removeAttributeNS must enqueue and invoke attributeChangedCallback');
var logEntries = customElement.takeLog();
assert_array_equals(logEntries.types(), ['attributeChanged']);
assert_attribute_log_entry(logEntries.last(), {name: 'id', oldValue: 'bar', newValue: null, namespace: null});
}, 'setAttributeNode and removeAttributeNode must enqueue and invoke attributeChangedCallback for an HTML attribute');

test(function () {
var instance = document.createElement('my-custom-element');
argumentList = [];
const instance = document.createElement(customElement.name);
assert_array_equals(customElement.takeLog().types(), ['constructed']);

var attr = document.createAttributeNS('http://www.w3.org/2000/svg', 'r');
const attr = document.createAttributeNS('http://www.w3.org/2000/svg', 'r');
attr.value = '100';
instance.setAttributeNode(attr);

assert_equals(instance.getAttribute('r'), '100');
assert_equals(argumentList.length, 1);
assert_equals(argumentList[0].value, '100');
assert_array_equals(argumentList[0].arguments, ['r', null, '100', 'http://www.w3.org/2000/svg']);
var logEntries = customElement.takeLog();
assert_array_equals(logEntries.types(), ['attributeChanged']);
assert_attribute_log_entry(logEntries.last(), {name: 'r', oldValue: null, newValue: '100', namespace: 'http://www.w3.org/2000/svg'});

instance.removeAttributeNode(attr);
assert_equals(instance.getAttribute('r'), null);
assert_equals(argumentList.length, 2);
assert_equals(argumentList[1].value, null);
assert_array_equals(argumentList[1].arguments, ['r', '100', null, 'http://www.w3.org/2000/svg']);
}, 'setAttributeNode and removeAttributeNS must enqueue and invoke attributeChangedCallback');
var logEntries = customElement.takeLog();
assert_array_equals(logEntries.types(), ['attributeChanged']);
assert_attribute_log_entry(logEntries.last(), {name: 'r', oldValue: '100', newValue: null, namespace: 'http://www.w3.org/2000/svg'});
}, 'setAttributeNode and removeAttributeNS must enqueue and invoke attributeChangedCallback for an SVG attribute');

test(function () {
var callsToOld = [];
var callsToNew = [];
const callsToOld = [];
const callsToNew = [];
class CustomElement extends HTMLElement { }
CustomElement.prototype.attributeChangedCallback = function () {
callsToOld.push(Array.from(arguments));
CustomElement.prototype.attributeChangedCallback = function (...args) {
callsToOld.push(create_attribute_changed_callback_log(this, ...args));
}
CustomElement.observedAttributes = ['title'];
customElements.define('element-with-mutated-attribute-changed-callback', CustomElement);
CustomElement.prototype.attributeChangedCallback = function () {
callsToNew.push(Array.from(arguments));
CustomElement.prototype.attributeChangedCallback = function (...args) {
callsToNew.push(create_attribute_changed_callback_log(this, ...args));
}

var instance = document.createElement('element-with-mutated-attribute-changed-callback');
const instance = document.createElement('element-with-mutated-attribute-changed-callback');
instance.setAttribute('title', 'hi');
assert_equals(instance.getAttribute('title'), 'hi');
assert_array_equals(callsToNew, []);
assert_equals(callsToOld.length, 1);
assert_array_equals(callsToOld[0], ['title', null, 'hi', null]);
assert_attribute_log_entry(callsToOld[0], {name: 'title', oldValue: null, newValue: 'hi', namespace: null});
}, 'Mutating attributeChangedCallback after calling customElements.define must not affect the callback being invoked');

test(function () {
var calls = [];
const calls = [];
class CustomElement extends HTMLElement {
attributeChangedCallback() {
calls.push(Array.from(arguments));
attributeChangedCallback(...args) {
calls.push(create_attribute_changed_callback_log(this, ...args));
}
}
CustomElement.observedAttributes = ['title'];
customElements.define('element-not-observing-id-attribute', CustomElement);

var instance = document.createElement('element-not-observing-id-attribute');
const instance = document.createElement('element-not-observing-id-attribute');
assert_equals(calls.length, 0);
instance.setAttribute('title', 'hi');
assert_equals(calls.length, 1);
assert_array_equals(calls[0], ['title', null, 'hi', null]);
assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: 'hi', namespace: null});
instance.setAttribute('id', 'some');
assert_equals(calls.length, 1);
}, 'attributedChangedCallback must not be invoked when the observed attributes does not contain the attribute.');
assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: 'hi', namespace: null});
}, 'attributedChangedCallback must not be invoked when the observed attributes does not contain the attribute');

test(function () {
var calls = [];
const calls = [];
class CustomElement extends HTMLElement { }
CustomElement.prototype.attributeChangedCallback = function () {
calls.push(Array.from(arguments));
CustomElement.prototype.attributeChangedCallback = function (...args) {
calls.push(create_attribute_changed_callback_log(this, ...args));
}
CustomElement.observedAttributes = ['title', 'lang'];
customElements.define('element-with-mutated-observed-attributes', CustomElement);
CustomElement.observedAttributes = ['title', 'id'];

var instance = document.createElement('element-with-mutated-observed-attributes');
const instance = document.createElement('element-with-mutated-observed-attributes');
instance.setAttribute('title', 'hi');
assert_equals(calls.length, 1);
assert_array_equals(calls[0], ['title', null, 'hi', null]);
assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: 'hi', namespace: null});

instance.setAttribute('id', 'some');
assert_equals(calls.length, 1);

instance.setAttribute('lang', 'en');
assert_equals(calls.length, 2);
assert_array_equals(calls[0], ['title', null, 'hi', null]);
assert_array_equals(calls[1], ['lang', null, 'en', null]);
assert_attribute_log_entry(calls[1], {name: 'lang', oldValue: null, newValue: 'en', namespace: null});
}, 'Mutating observedAttributes after calling customElements.define must not affect the set of attributes for which attributedChangedCallback is invoked');

test(function () {
var calls = [];
class CustomElement extends HTMLElement { }
CustomElement.prototype.attributeChangedCallback = function () {
calls.push(Array.from(arguments));
CustomElement.prototype.attributeChangedCallback = function (...args) {
calls.push(create_attribute_changed_callback_log(this, ...args));
}
CustomElement.observedAttributes = { [Symbol.iterator]: function *() { yield 'lang'; yield 'style'; } };
customElements.define('element-with-generator-observed-attributes', CustomElement);

var instance = document.createElement('element-with-generator-observed-attributes');
instance.setAttribute('lang', 'en');
assert_equals(calls.length, 1);
assert_array_equals(calls[0], ['lang', null, 'en', null]);
assert_attribute_log_entry(calls[0], {name: 'lang', oldValue: null, newValue: 'en', namespace: null});

instance.setAttribute('lang', 'ja');
assert_equals(calls.length, 2);
assert_array_equals(calls[1], ['lang', 'en', 'ja', null]);
assert_attribute_log_entry(calls[1], {name: 'lang', oldValue: 'en', newValue: 'ja', namespace: null});

instance.setAttribute('title', 'hello');
assert_equals(calls.length, 2);

instance.setAttribute('style', 'font-size: 2rem');
assert_equals(calls.length, 3);
assert_array_equals(calls[2], ['style', null, 'font-size: 2rem', null]);
assert_attribute_log_entry(calls[2], {name: 'style', oldValue: null, newValue: 'font-size: 2rem', namespace: null});
}, 'attributedChangedCallback must be enqueued for attributes specified in a non-Array iterable observedAttributes');

test(function () {
var calls = [];
class CustomElement extends HTMLElement { }
CustomElement.prototype.attributeChangedCallback = function (...args) {
calls.push(create_attribute_changed_callback_log(this, ...args));
}
CustomElement.observedAttributes = ['style'];
customElements.define('element-with-style-attribute-observation', CustomElement);

var instance = document.createElement('element-with-style-attribute-observation');
assert_equals(calls.length, 0);

instance.style.fontSize = '10px';
assert_equals(calls.length, 1);
assert_attribute_log_entry(calls[0], {name: 'style', oldValue: null, newValue: 'font-size: 10px;', namespace: null});

instance.style.fontSize = '20px';
assert_equals(calls.length, 2);
assert_attribute_log_entry(calls[1], {name: 'style', oldValue: 'font-size: 10px;', newValue: 'font-size: 20px;', namespace: null});

}, 'attributedChangedCallback must be enqueued for style attribute change by mutating inline style declaration');

test(function () {
var calls = [];
class CustomElement extends HTMLElement { }
CustomElement.prototype.attributeChangedCallback = function (...args) {
calls.push(create_attribute_changed_callback_log(this, ...args));
}
CustomElement.observedAttributes = ['title'];
customElements.define('element-with-no-style-attribute-observation', CustomElement);

var instance = document.createElement('element-with-no-style-attribute-observation');
assert_equals(calls.length, 0);
instance.style.fontSize = '10px';
assert_equals(calls.length, 0);
instance.title = 'hello';
assert_attribute_log_entry(calls[0], {name: 'title', oldValue: null, newValue: 'hello', namespace: null});
}, 'attributedChangedCallback must not be enqueued when mutating inline style declaration if the style attribute is not observed');

</script>
</body>
</html>
Loading

0 comments on commit c7fbe2d

Please sign in to comment.