Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1325)

Side by Side Diff: third_party/WebKit/LayoutTests/custom-elements/spec/define-element.html

Issue 2089383003: Added test for step 2, 14 in define element. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: patch update Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <title>Custom Elements: defineElement</title> 2 <title>Custom Elements: defineElement</title>
3 <link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#cus tomelementsregistry"> 3 <link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#cus tomelementsregistry">
4 <meta name="author" title="Dominic Cooney" href="mailto:dominicc@chromium.org"> 4 <meta name="author" title="Dominic Cooney" href="mailto:dominicc@chromium.org">
5 <script src="../../resources/testharness.js"></script> 5 <script src="../../resources/testharness.js"></script>
6 <script src="../../resources/testharness-helpers.js"></script>
7 <script src="../../resources/testharnessreport.js"></script> 6 <script src="../../resources/testharnessreport.js"></script>
8 <script src="resources/custom-elements-helpers.js"></script> 7 <script src="resources/custom-elements-helpers.js"></script>
9 <body> 8 <body>
10 <script> 9 <script>
11 // TODO(dominicc): Merge these tests with 10 // TODO(dominicc): Merge these tests with
12 // https://github.com/w3c/web-platform-tests/pull/2940 11 // https://github.com/w3c/web-platform-tests/pull/2940
13 12
14 'use strict'; 13 'use strict';
15 14
15 function assert_throws_dom_exception(global_context, code, func, description) {
16 let exception;
17 assert_throws(code, () => {
18 try {
19 func.call(this);
20 } catch(e) {
21 exception = e;
22 throw e;
23 }
24 }, description);
25 assert_true(exception instanceof global_context.DOMException, 'DOMException on the appropriate window');
26 }
27
16 test_with_window((w) => { 28 test_with_window((w) => {
17 assert_throws(TypeError.prototype, () => { 29 assert_throws(TypeError.prototype, () => {
18 w.customElements.define('a-a', 42); 30 w.customElements.define('a-a', 42);
19 }, 'defining a number "constructor" should throw a TypeError'); 31 }, 'defining a number "constructor" should throw a TypeError');
20 assert_throws(TypeError.prototype, () => { 32 assert_throws(TypeError.prototype, () => {
21 w.customElements.define('a-a', () => {}); 33 w.customElements.define('a-a', () => {});
22 }, 'defining an arrow function "constructor" should throw a TypeError'); 34 }, 'defining an arrow function "constructor" should throw a TypeError');
23 assert_throws(TypeError.prototype, () => { 35 assert_throws(TypeError.prototype, () => {
24 w.customElements.define('a-a', { m() {} }.m); 36 w.customElements.define('a-a', { m() {} }.m);
25 }, 'defining a concise method "constructor" should throw a TypeError'); 37 }, 'defining a concise method "constructor" should throw a TypeError');
(...skipping 15 matching lines...) Expand all
41 '-not-initial-a-z', '0not-initial-a-z', 'Not-initial-a-z', 53 '-not-initial-a-z', '0not-initial-a-z', 'Not-initial-a-z',
42 'intermediate-UPPERCASE-letters', 54 'intermediate-UPPERCASE-letters',
43 'bad-\u00b6', 'bad-\u00b8', 'bad-\u00bf', 'bad-\u00d7', 'bad-\u00f7', 55 'bad-\u00b6', 'bad-\u00b8', 'bad-\u00bf', 'bad-\u00d7', 'bad-\u00f7',
44 'bad-\u037e', 'bad-\u037e', 'bad-\u2000', 'bad-\u200e', 'bad-\u203e', 56 'bad-\u037e', 'bad-\u037e', 'bad-\u2000', 'bad-\u200e', 'bad-\u203e',
45 'bad-\u2041', 'bad-\u206f', 'bad-\u2190', 'bad-\u2bff', 'bad-\u2ff0', 57 'bad-\u2041', 'bad-\u206f', 'bad-\u2190', 'bad-\u2bff', 'bad-\u2ff0',
46 'bad-\u3000', 'bad-\ud800', 'bad-\uf8ff', 'bad-\ufdd0', 'bad-\ufdef', 58 'bad-\u3000', 'bad-\ud800', 'bad-\uf8ff', 'bad-\ufdd0', 'bad-\ufdef',
47 'bad-\ufffe', 'bad-\uffff', 'bad-' + String.fromCodePoint(0xf0000) 59 'bad-\ufffe', 'bad-\uffff', 'bad-' + String.fromCodePoint(0xf0000)
48 ]; 60 ];
49 class X extends w.HTMLElement {} 61 class X extends w.HTMLElement {}
50 invalid_names.forEach((name) => { 62 invalid_names.forEach((name) => {
51 assert_throws('SYNTAX_ERR', () => { 63 assert_throws_dom_exception(w, 'SYNTAX_ERR', () => {
52 w.customElements.define(name, X); 64 w.customElements.define(name, X);
53 }, `defining an element named "${name}" should throw a SyntaxError`); 65 })
54 }); 66 });
55 }, 'Invalid names'); 67 }, 'Invalid names');
56 68
57 test_with_window((w) => { 69 test_with_window((w) => {
58 class X extends w.HTMLElement {} 70 class X extends w.HTMLElement {}
59 class Y extends w.HTMLElement {} 71 class Y extends w.HTMLElement {}
60 w.customElements.define('a-a', X); 72 w.customElements.define('a-a', X);
61 assert_throws('NotSupportedError', () => { 73 assert_throws_dom_exception(w, 'NotSupportedError', () => {
62 w.customElements.define('a-a', Y); 74 w.customElements.define('a-a', Y);
63 }, 'defining an element with a name that is already defined should throw ' + 75 }, 'defining an element with a name that is already defined should throw ' +
64 'a NotSupportedError'); 76 'a NotSupportedError');
65 }, 'Duplicate name'); 77 }, 'Duplicate name');
66 78
67 // TODO(dominicc): Update this (perhaps by removing this comment) when
68 // https://github.com/whatwg/html/pull/1333 lands/issue
69 // https://github.com/whatwg/html/issues/1329 is closed.
70 test_with_window((w) => { 79 test_with_window((w) => {
71 class Y extends w.HTMLElement {} 80 class Y extends w.HTMLElement {}
72 let X = (function () {}).bind({}); 81 let X = (function () {}).bind({});
73 Object.defineProperty(X, 'prototype', { 82 Object.defineProperty(X, 'prototype', {
74 get() { 83 get() {
75 assert_throws('NotSupportedError', () => { 84 assert_throws_dom_exception(w, 'NotSupportedError', () => {
76 w.customElements.define('a-a', Y); 85 w.customElements.define('a-a', Y);
77 }, 'defining an element with a name that is being defined should ' + 86 }, 'defining an element with a name that is being defined should ' +
78 'throw a NotSupportedError'); 87 'throw a NotSupportedError');
79 return new Object(); 88 return new Object();
80 } 89 }
81 }); 90 });
82 // TODO(dominicc): When callback retrieval is implemented, change this
83 // to pass a valid constructor and recursively call define when retrieving
84 // callbacks instead; then it is possible to assert the first definition
85 // worked:
86 // let element = Reflect.construct(HTMLElement, [], X);
87 // assert_equals(element.localName, 'a-a');
88 w.customElements.define('a-a', X); 91 w.customElements.define('a-a', X);
92 assert_equals(w.customElements.get('a-a'), X, 'the first definition should hav e worked');
89 }, 'Duplicate name defined recursively'); 93 }, 'Duplicate name defined recursively');
90 94
91 test_with_window((w) => { 95 test_with_window((w) => {
92 class X extends w.HTMLElement {} 96 class X extends w.HTMLElement {}
93 w.customElements.define('a-a', X); 97 w.customElements.define('a-a', X);
94 assert_throws('NotSupportedError', () => { 98 assert_throws_dom_exception(w, 'NotSupportedError', () => {
95 w.customElements.define('a-b', X); 99 w.customElements.define('a-b', X);
96 }, 'defining an element with a constructor that is already in the ' + 100 }, 'defining an element with a constructor that is already in the ' +
97 'registry should throw a NotSupportedError'); 101 'registry should throw a NotSupportedError');
98 }, 'Reused constructor'); 102 }, 'Reused constructor');
99 103
100 // TODO(dominicc): Update this (perhaps by removing this comment) when
101 // https://github.com/whatwg/html/pull/1333 lands/issue
102 // https://github.com/whatwg/html/issues/1329 is closed.
103 test_with_window((w) => { 104 test_with_window((w) => {
104 let X = (function () {}).bind({}); 105 let X = (function () {}).bind({});
105 Object.defineProperty(X, 'prototype', { 106 Object.defineProperty(X, 'prototype', {
106 get() { 107 get() {
107 assert_throws('NotSupportedError', () => { 108 assert_throws_dom_exception(w, 'NotSupportedError', () => {
108 w.customElements.define('second-name', X); 109 w.customElements.define('second-name', X);
109 }, 'defining an element with a constructor that is being defined ' + 110 }, 'defining an element with a constructor that is being defined ' +
110 'should throw a NotSupportedError'); 111 'should throw a NotSupportedError');
111 return new Object(); 112 return new Object();
112 } 113 }
113 }); 114 });
114 // TODO(dominicc): When callback retrieval is implemented, change this
115 // to pass a valid constructor and recursively call define when retrieving
116 // callbacks instead; then it is possible to assert the first definition
117 // worked:
118 // let element = Reflect.construct(HTMLElement, [], X);
119 // assert_equals(element.localName, 'a-a');
120 w.customElements.define('first-name', X); 115 w.customElements.define('first-name', X);
116 assert_equals(w.customElements.get('first-name'), X, 'the first definition sho uld have worked');
121 }, 'Reused constructor recursively'); 117 }, 'Reused constructor recursively');
122 118
123 test_with_window((w) => { 119 test_with_window((w) => {
124 function F() {}
125 F.prototype = 42;
126 assert_throws(TypeError.prototype, () => {
127 w.customElements.define('a-a', F);
128 }, 'defining an element with a constructor with a prototype that is not an ' +
129 'object should throw a TypeError');
130 }, 'Retrieved prototype is a non-object');
131
132 test_with_window((w) => {
133 assert_throws(TypeError.prototype, () => { 120 assert_throws(TypeError.prototype, () => {
134 let not_a_constructor = () => {}; 121 let not_a_constructor = () => {};
135 let invalid_name = 'annotation-xml'; 122 let invalid_name = 'annotation-xml';
136 w.customElements.define(invalid_name, not_a_constructor); 123 w.customElements.define(invalid_name, not_a_constructor);
137 }, 'defining an element with an invalid name and invalid constructor ' + 124 }, 'defining an element with an invalid name and invalid constructor ' +
138 'should throw a TypeError for the constructor and not a SyntaxError'); 125 'should throw a TypeError for the constructor and not a SyntaxError');
139 126
140 class C extends w.HTMLElement {} 127 class C extends w.HTMLElement {}
141 w.customElements.define('a-a', C); 128 w.customElements.define('a-a', C);
142 assert_throws('SYNTAX_ERR', () => { 129 assert_throws_dom_exception(w, 'SYNTAX_ERR', () => {
143 let invalid_name = 'annotation-xml'; 130 let invalid_name = 'annotation-xml';
144 let reused_constructor = C; 131 let reused_constructor = C;
145 w.customElements.define(invalid_name, reused_constructor); 132 w.customElements.define(invalid_name, reused_constructor);
146 }, 'defining an element with an invalid name and a reused constructor ' + 133 }, 'defining an element with an invalid name and a reused constructor ' +
147 'should throw a SyntaxError for the name and not a NotSupportedError'); 134 'should throw a SyntaxError for the name and not a NotSupportedError');
148 }, 'Order of checks'); 135 }, 'Order of checks');
149 136
150 test_with_window((w) => { 137 test_with_window((w) => {
151 let doc = w.document; 138 let doc = w.document;
152 doc.body.innerHTML = ` 139 doc.body.innerHTML = `
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 constructor() { 189 constructor() {
203 super(); 190 super();
204 invocations.push(this); 191 invocations.push(this);
205 } 192 }
206 } 193 }
207 w.customElements.define('a-a', C); 194 w.customElements.define('a-a', C);
208 assert_array_equals([a], invocations, 195 assert_array_equals([a], invocations,
209 'the constructor should have been invoked once for the ' + 196 'the constructor should have been invoked once for the ' +
210 'elements in the shadow tree'); 197 'elements in the shadow tree');
211 }, 'Upgrade: shadow tree'); 198 }, 'Upgrade: shadow tree');
199
200 // Final step in Step 14
201 // 14. Finally, if the first set of steps threw an exception, then rethrow that exception,
202 // and terminate this algorithm.
203 test_with_window((w) => {
204 class Y extends w.HTMLElement {}
205 let X = (function () {}).bind({});
206 Object.defineProperty(X, 'prototype', {
207 get() { throw { name: 42 }; }
208 });
209 assert_throws({ name: 42 }, () => {
210 w.customElements.define('a-a', X);
211 }, 'should rethrow constructor exception');
212 w.customElements.define('a-a', Y);
213 assert_equals(w.customElements.get('a-a'), Y, 'the same name can be registered after failure');
214 }, 'If an exception is thrown, rethrow that exception and terminate the algorith m');
215
216 // 14.1 Let prototype be Get(constructor, "prototype"). Rethrow any exceptions.
217 test_with_window((w) => {
218 let X = (function () {}).bind({});
219 Object.defineProperty(X, 'prototype', {
220 get() { throw { name: 'prototype throws' }; }
221 });
222 assert_throws({ name: 'prototype throws' }, () => {
223 w.customElements.define('a-a', X);
224 }, 'Exception from Get(constructor, prototype) should be rethrown');
225 }, 'Rethrow any exceptions thrown while getting prototype');
226
227 // 14.2 If Type(prototype) is not Object, then throw a TypeError exception.
228 test_with_window((w) => {
229 function F() {}
230 F.prototype = 42;
231 assert_throws(TypeError.prototype, () => {
232 w.customElements.define('a-a', F);
233 }, 'defining an element with a constructor with a prototype that is not an ' +
234 'object should throw a TypeError');
235 }, 'Retrieved prototype is a non-object');
236
237 // 14.3 Let connectedCallback be Get(prototype, "connectedCallback"). Rethrow an y exceptions.
238 // 14.5 Let disconnectedCallback be Get(prototype, "disconnectedCallback"). Reth row any exceptions.
239 // 14.7 Let attributeChangedCallback be Get(prototype, "attributeChangedCallback "). Rethrow any exceptions.
240 // Note that this test implicitly tests order of callback retrievals.
241 // Callbacks are defined in reverse order.
242 let callbacks_in_reverse = ['attributeChangedCallback', 'disconnectedCallback', 'connectedCallback'];
243 function F_for_callbacks_in_reverse() {};
244 callbacks_in_reverse.forEach((callback) => {
245 test_with_window((w) => {
246 Object.defineProperty(F_for_callbacks_in_reverse.prototype, callback, {
247 get() { throw { name: callback }; }
248 });
249 assert_throws({ name: callback }, () => {
250 w.customElements.define('a-a', F_for_callbacks_in_reverse);
251 }, 'Exception from Get(prototype, callback) should be rethrown');
252 }, 'Rethrow any exceptions thrown while retrieving ' + callback);
253 });
254
255 // 14.4 If connectedCallback is not undefined, and IsCallable(connectedCallback) is false,
256 // then throw a TypeError exception.
257 // 14.6 If disconnectedCallback is not undefined, and IsCallable(disconnectedCal lback) is false,
258 // then throw a TypeError exception.
259 // 14.9. If attributeChangedCallback is not undefined, then
260 // 1. If IsCallable(attributeChangedCallback) is false, then throw a TypeE rror exception.
261 callbacks_in_reverse.forEach((callback) => {
262 test_with_window((w) => {
263 function F() {}
264 Object.defineProperty(F.prototype, callback, {
265 get() { return new Object(); }
266 });
267 assert_throws(TypeError.prototype, () => {
268 w.customElements.define('a-a', F);
269 }, 'defining an element with a constructor with a callback that is ' +
270 'not undefined and not callable should throw a TypeError');
271 }, 'If retrieved callback '+ callback + ' is not undefined and not callable, t hrow TypeError');
272 });
273
274 // 14.9.2 Let observedAttributesIterable be Get(constructor, "observedAttributes ").
275 // Rethrow any exceptions.
276 test_with_window((w) => {
277 class X extends w.HTMLElement{
278 constructor() { super(); }
279 attributeChangedCallback() {}
280 static get observedAttributes() { throw { name: 'observedAttributes throws' }; }
281 }
282 assert_throws({ name: 'observedAttributes throws' }, () => {
283 w.customElements.define('a-a', X);
284 }, 'Exception from Get(constructor, observedAttributes) should be rethrown');
285 }, 'Rethrow any exceptions thrown while getting observedAttributes');
286
287 // 14.9.3 If observedAttributesIterable is not undefined, then set observedAttri butes
288 // to the result of converting observedAttributesIterable to a sequence<D OMString>.
289 // Rethrow any exceptions.
290 test_with_window((w) => {
291 class X extends w.HTMLElement{
292 constructor() { super(); }
293 attributeChangedCallback() {}
294 static get observedAttributes() { return new RegExp(); }
295 }
296 assert_throws(TypeError.prototype, () => {
297 w.customElements.define('a-a', X);
298 }, 'converting RegExp to sequence<DOMString> should throw TypeError');
299 }, 'exception thrown while converting observedAttributes to sequence<DOMString> ' +
300 'should be rethrown');
301
302 // 14.9.2 test Get(constructor, observedAttributes) does not throw if
303 // attributeChangedCallback is undefined.
304 test_with_window((w) => {
305 let observedAttributes_invoked = false;
306 let X = (function () {}).bind({});
307 Object.defineProperty(X, 'observedAttributes', {
308 get() { observedAttributes_invoked = true; }
309 });
310 assert_false( observedAttributes_invoked, 'Get(constructor, observedAttributes ) should not be invoked');
311 }, 'Get(constructor, observedAttributes)' +
312 'should not execute if attributeChangedCallback is undefined');
313
314 // step 2
315 // 2. If constructor is an interface object whose corresponding interface either is
316 // HTMLElement or has HTMLElement in its set of inherited interfaces, throw
317 // a TypeError and abort these steps.
318 // 3. If name is not a valid custom element name, then throw a "SyntaxError" DOM Exception
319 // and abort these steps.
320 test_with_window((w) => {
321 let invalid_name = 'annotation-xml';
322 // TODO(davaajav): change this to TypeError, when we add a failure expectation to this file
323 assert_throws_dom_exception(w, 'SYNTAX_ERR', () => {
324 w.customElements.define(invalid_name, HTMLElement);
325 }, 'defining a constructor that is an interface object whose interface is HTML Element' +
326 'should throw TypeError not SyntaxError');
327 }, 'Invalid constructor');
328
329 // step 2
330 test_with_window((w) => {
331 let invalid_name = 'annotation-xml';
332 assert_throws_dom_exception(w, 'SYNTAX_ERR', () => {
333 w.customElements.define(invalid_name, HTMLButtonElement);
334 }, 'defining a constructor that is an interface object who has HTMLElement' +
335 'in its set of inhertied interfaces should throw TypeError not SyntaxErro r');
336 }, 'Invalid constructor');
337
338 // step 2
339 test_with_window((w) => {
340 let invalid_name = 'annotation-xml';
341 assert_throws_dom_exception(w, 'SYNTAX_ERR', () => {
342 w.customElements.define(invalid_name, class extends HTMLElement {});
343 }, 'defining author-defined custom element constructor' +
344 'should pass this step without throwing TypeError');
345 }, 'Invalid constructor');
212 </script> 346 </script>
347 </body>
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698