Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / custom / isolated-world.html
blob61d08d12ee6c0f120e8ea0e00cc74972ac8b7723
1 <!DOCTYPE html>
2 <script src="../../../resources/js-test.js"></script>
3 <body>
4 <div id="container-a"><span is="x-a" id="x"></span></div>
5 <div id="container-b"><span is="x-a" id="y"></span></div>
6 <script>
7 description('Tests that accessing custom elements from an isolated world ' +
8 'does not cause worlds to collide and destroy the galaxy.');
10 shouldBeNonNull('window.testRunner', 'this test requires testRunner');
12 var containerA = document.querySelector('#container-a');
14 // Access an upgrade candidate from an isolated world.
15 const world = 1;
16 testRunner.evaluateScriptInIsolatedWorld(
17 world,
18 'window.containerA = document.querySelector("#container-a"); ' +
19 'window.a = containerA.firstElementChild; ' +
20 'containerA.remove();');
22 // Registering an exception in the isolated world should raise an exception.
23 result = testRunner.evaluateScriptInIsolatedWorldAndReturnValue(
24 world,
25 '(function () { ' +
26 ' var proto = Object.create(HTMLSpanElement.prototype); ' +
27 ' proto.createdCallback = function () { ' +
28 ' console.log(this.id + " entered (isolated)"); ' +
29 ' }; ' +
30 ' try { ' +
31 ' document.registerElement("x-a", {extends: "span", prototype: proto}); ' +
32 ' return "register succeeded"; ' +
33 ' } catch (e) { ' +
34 ' return e.code; ' +
35 ' } ' +
36 '})()');
38 shouldBe('result', 'DOMException.NOT_SUPPORTED_ERR',
39 'calling register from an isolated world should throw an exception');
41 // Now access this element from the main world.
42 a = containerA.querySelector('#x');
43 shouldBe('a.__proto__', 'HTMLSpanElement.prototype',
44 'the main world should see the usual main world prototype');
46 // Upgrade from the main world
48 var proto = Object.create(HTMLSpanElement.prototype);
49 invocations = [];
50 proto.attachedCallback = function () {
51 invocations.push(this.id + ' entered (main)');
53 document.registerElement('x-a', {extends: 'span', prototype: proto});
54 shouldBe('invocations', '["y entered (main)"]',
55 'only the element in the document should generate entered events');
57 // Insert from the isolated world
59 invocations = [];
60 testRunner.evaluateScriptInIsolatedWorld(world++,
61 'document.body.appendChild(a);');
62 shouldBe('invocations', '["x entered (main)"]',
63 'modification in the isolated world should have caused callbacks ' +
64 'in the main world');
66 // Examine prototypes from the isolated world
68 proto.p = 'p';
69 HTMLSpanElement.prototype.q = 'q';
71 result = testRunner.evaluateScriptInIsolatedWorldAndReturnValue(
72 world,
73 '(function () { ' +
74 ' try { ' +
75 ' var messages = []; ' +
76 ' function log(message) { messages.push(message); } ' +
77 ' function inspect(obj) { ' +
78 ' log(obj.id + " prototype is HTMLSpanElement.prototype? " + ' +
79 ' (obj.__proto__ === HTMLSpanElement.prototype)); ' +
80 ' log(obj.id + ".p ~> " + obj.p); ' +
81 ' log(obj.id + ".q ~> " + obj.q); ' +
82 ' } ' +
83 ' inspect(window.a); ' +
84 ' inspect(document.querySelector("#y")); ' +
85 ' return messages.join(", "); ' +
86 ' } catch (e) { ' +
87 ' return e.toString(); ' +
88 ' } ' +
89 '})()');
90 shouldBe(
91 'result',
92 '"x prototype is HTMLSpanElement.prototype? true,' +
93 ' x.p ~> undefined,' +
94 ' x.q ~> undefined,' +
95 ' y prototype is HTMLSpanElement.prototype? true,' +
96 ' y.p ~> undefined,' +
97 ' y.q ~> undefined"',
98 'the isolated world should not see main world prototypes');
100 // Lastly, collect some wrappers (e.g #b in isolated world)
101 document.querySelector('#container-b').innerHTML = '';
102 gc();
103 testPassed('did not crash');
104 </script>